Skip to content

Commit

Permalink
Inativar/Ativar item na assinatura (#142)
Browse files Browse the repository at this point in the history
* feat: removed deprecated magento 2 classes

* feat: removed deprecated magento 2 classes

* fix: remove unused patch

* fix: call non existing class

* fix: return composer version

* Versionamento da 2.1.0

* feat: adding and deleting items in the subscription

* feat: exclude and include subscription items

* fix: adding validation so you can't exclude shipping or leave a subscription without a product

* feat: add duration in subscription item

* fix: add permanent option in item cycles

* refactor: restoring OrderCreator helper

* fix: adjust creation of new orders

* refactor: create observer to update subscription data

* feat: add quantity in subscription item

* fix: error updating subscription items in the observer

* fix: add validate quantity greater than zero

* fix: add validate quantity greater than zero

* fix: add validate quantity greater than zero

* fix: customer id error when create register

* fix: customer id error when create register

* fix: fixing zero price validations

* fix: finalizing orders with zero prices

* feat: adding option to change subscription item status

* feat: new card layout

* fix: same input id for different fields

* fix: cacheable false for product view

* fix: add cache key info product block

* fix: add discount in subscriptions

* fix: correcting quantity validation in shipping

* feat: added all available brands

* fix: removed status on item creation

* fix: correcting duplication of items on the invoice

* fix: adjusting the addition of temporary items

* fix: removing the item when creating the invoice

* fix: removing the item when creating the invoice

* fix: removing the item when creating the invoice

* fix: removing the item when creating the invoice

* fix: removing the item when creating the invoice

* fix: removing the item when creating the invoice

* fix: removing the item when creating the invoice

* fix: removing duplication in translation

* fix: remove duplicate itens

---------

Co-authored-by: Thiago Contardi <[email protected]>
Co-authored-by: Thiago Contardi <[email protected]>
Co-authored-by: Thais Kusuki <[email protected]>
Co-authored-by: Thais Kusuki <[email protected]>
Co-authored-by: Iago Cedran <[email protected]>
Co-authored-by: Contardi <[email protected]>
  • Loading branch information
7 people authored Feb 6, 2025
1 parent d0d7107 commit 6cbeaef
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 40 deletions.
48 changes: 42 additions & 6 deletions Controller/Adminhtml/Subscription/SaveSubscriptionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,29 @@
*/
class SaveSubscriptionItem extends Action
{
/** @var ProductItems */
/**
* @var ProductItems
*/
protected $productItems;

/** @var VindiSubscriptionItemRepository */
/**
* @var VindiSubscriptionItemRepository
*/
protected $subscriptionItemRepository;

/** @var JsonFactory */
/**
* @var JsonFactory
*/
protected $resultJsonFactory;

/** @var VindiSubscriptionItemCollectionFactory */
/**
* @var VindiSubscriptionItemCollectionFactory
*/
protected $vindiSubscriptionItemCollectionFactory;

/**
* SaveSubscriptionItem constructor.
*
* @param Context $context
* @param ProductItems $productItems
* @param VindiSubscriptionItemRepository $subscriptionItemRepository
Expand Down Expand Up @@ -72,18 +81,40 @@ public function execute()
$entityId = $postData['entity_id'] ?? null;
$price = $postData['settings']['price'] ?? null;
$quantity = $postData['settings']['quantity'] ?? null;
$status = $postData['settings']['status'] ?? null;

if (!$entityId || ($price === null && $quantity === null)) {
throw new LocalizedException(__('You must provide at least one of the fields: price or quantity.'));
}

$subscriptionItem = $this->subscriptionItemRepository->getById($entityId);
$productCode = $subscriptionItem->getProductCode();
$subscriptionId = $subscriptionItem->getSubscriptionId();

if ($productCode === 'frete' && $quantity !== null) {
throw new LocalizedException(__('The quantity of the shipping item cannot be changed. Only the price can be updated.'));
}

if ($status !== null) {
if ($productCode === 'frete' && $status !== 'active') {
throw new LocalizedException(__('Shipping items must remain active.'));
}

if ($status === 'inactive') {
$itemsCollection = $this->vindiSubscriptionItemCollectionFactory->create();
$itemsCollection->addFieldToFilter('subscription_id', $subscriptionId)
->addFieldToFilter('product_code', ['neq' => 'frete'])
->addFieldToFilter('entity_id', ['neq' => $entityId])
->addFieldToFilter('status', 'active');

$activeItems = $itemsCollection->getSize();

if ($activeItems == 0) {
throw new LocalizedException(__('A subscription must have at least one non-shipping active item.'));
}
}
}

$data = [];
if ($price !== null) {
$price = number_format((float)$price, 2, '.', '');
Expand All @@ -92,13 +123,14 @@ public function execute()

if ($quantity !== null) {
$data['quantity'] = (int)$quantity;

if ($quantity > 1) {
$data['pricing_schema']['schema_type'] = 'per_unit';
}
}

$subscriptionId = $subscriptionItem->getSubscriptionId();
if ($status !== null) {
$data['status'] = $status;
}

if ((float)$price === 0.00 && $productCode !== 'frete') {
$itemsCollection = $this->vindiSubscriptionItemCollectionFactory->create();
Expand Down Expand Up @@ -129,6 +161,10 @@ public function execute()
$subscriptionItem->setQuantity($quantity);
}

if ($status !== null) {
$subscriptionItem->setStatus($status);
}

$this->subscriptionItemRepository->save($subscriptionItem);

$this->_eventManager->dispatch(
Expand Down
37 changes: 3 additions & 34 deletions Model/VindiSubscriptionItem/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,11 @@

class DataProvider extends \Magento\Ui\DataProvider\ModifierPoolDataProvider
{
/**
* @var \Vindi\Payment\Model\ResourceModel\VindiSubscriptionItem\Collection
*/
protected $collection;

/**
* @var DataPersistorInterface
*/
protected $dataPersistor;

/**
* @var array
*/
protected $loadedData;

/**
* @var Data
*/
protected $helper;

/**
* DataProvider constructor.
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CollectionFactory $subscriptionItemCollectionFactory
* @param DataPersistorInterface $dataPersistor
* @param Data $helper
* @param array $meta
* @param array $data
* @param PoolInterface|null $pool
*/
public function __construct(
string $name,
string $primaryFieldName,
Expand All @@ -58,9 +31,6 @@ public function __construct(
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data, $pool);
}

/**
* @return array
*/
public function getData()
{
if (isset($this->loadedData)) {
Expand All @@ -69,12 +39,11 @@ public function getData()

$items = $this->collection->getItems();

/** @var \Vindi\Payment\Model\VindiSubscriptionItem $subscriptionItem */
foreach ($items as $subscriptionItem) {
$result['settings'] = $subscriptionItem->getData();
$price = number_format((float) $result['settings']['price'], 2, '.', '');
$result['settings']['price'] = $price;
$result['entity_id'] = $subscriptionItem->getEntityId();
$result['entity_id'] = $subscriptionItem->getId();

$result['settings']['price'] = number_format((float) $subscriptionItem->getPrice(), 2, '.', '');

$this->loadedData[$subscriptionItem->getEntityId()] = $result;
}
Expand Down
6 changes: 6 additions & 0 deletions i18n/pt_BR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,12 @@
"This subscription item no longer exists.","Este item de assinatura não existe mais."
"Failed to delete the item from the subscription.","Falha ao excluir o item da assinatura."
"This subscription no longer exists.","Esta assinatura não existe mais."
"Shipping items must remain active.","Itens de frete devem permanecer ativos."
"A subscription must have at least one non-shipping active item.","Uma assinatura deve ter pelo menos um item ativo (sem contar o frete)."
"inactive","Inativo"
"active","Ativo"
"Inactive","Inativo"
"Active","Ativo"
"This payment link has expired.","Este link de pagamento expirou."
"The payment success page has already been accessed.","A página de sucesso do pagamento já foi acessada."
"Card Information","Dados do Cartão"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,29 @@
<dataScope>quantity</dataScope>
</settings>
</field>
<field name="status" sortOrder="30" formElement="checkbox">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">menu</item>
<item name="default" xsi:type="string">active</item>
</item>
</argument>
<settings>
<dataType>boolean</dataType>
<label translate="true">Status</label>
<dataScope>status</dataScope>
</settings>
<formElements>
<checkbox>
<settings>
<valueMap>
<map name="false" xsi:type="string">inactive</map>
<map name="true" xsi:type="string">active</map>
</valueMap>
<prefer>toggle</prefer>
</settings>
</checkbox>
</formElements>
</field>
</fieldset>
</form>

0 comments on commit 6cbeaef

Please sign in to comment.