Skip to content

Commit

Permalink
#35
Browse files Browse the repository at this point in the history
Implemented feature to restrict sending by trials numbers
Minor optimizations
  • Loading branch information
Ruslan Mavlyanov authored and luckyraul committed Mar 20, 2019
1 parent 2d876b9 commit 253ee1c
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 74 deletions.
62 changes: 62 additions & 0 deletions .jeeves.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Mygento:
Kkm:
TransactionAttempt:
gui: false
comment: 'Table for kkm transaction attempts'
columns:
id:
type: int
pk: true
identity: true
unsigned: true
comment: 'Attempt ID'
order_id:
type: int
nullable: false
unsigned: true
comment: 'Order id'
operation:
type: smallint
nullable: false
comment: 'Operation type. Sell or Refund.'
length: 10
sales_entity_increment_id:
type: varchar
length: 50
nullable: false
comment: 'Invoice or Creditmemo increment id'
status:
type: smallint
message:
type: text
nullable: true
number_of_trials:
type: smallint
nullable: true
comment: 'Count of attempts to make KKM transaction'
created_at:
type: timestamp
nullable: false
default: 'CURRENT_TIMESTAMP'
comment: 'Create time'
on_update: false
updated_at:
type: timestamp
nullable: false
default: 'CURRENT_TIMESTAMP'
comment: 'Modify time'
on_update: true
indexes:
IX_STATUS:
columns: ['status']
IX_ORDER_ID:
columns: ['order_id']
IX_SALES_ENTITY_U:
type: 'unique'
columns: ['operation', 'sales_entity_increment_id']
fk:
FK_ATTMPT_ORDER_ID_SAL_ORDER_ENT_ID:
column: 'order_id'
referenceTable: 'sales_order'
referenceColumn: 'entity_id'
indexName: 'IX_ORDER_ID'
17 changes: 11 additions & 6 deletions Api/Data/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ interface RequestInterface
const SELL_OPERATION_TYPE = 1;
const REFUND_OPERATION_TYPE = 2;

/**
* @return mixed
*/
public function __toArray();

/**
* @return string
*/
Expand Down Expand Up @@ -131,7 +136,7 @@ public function setCompanyEmail($companyEmail): self;

/**
* @param ItemInterface $item
* @return $this
* @return RequestInterface
*/
public function addItem(ItemInterface $item): self;

Expand Down Expand Up @@ -195,13 +200,13 @@ public function setSalesEntityId($id): self;
public function getSalesEntityId(): int;

/**
* @param int $count
* @return $this
* @return bool
*/
public function setRetryCount($count): self;
public function isIgnoreTrialsNum();

/**
* @return int|null
* @param bool $ignore
* @return $this
*/
public function getRetryCount();
public function setIgnoreTrialsNum($ignore);
}
1 change: 1 addition & 0 deletions Api/TransactionAttemptRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getById($entityId);
* Retrieve TransactionAttempt
* @param int $operation
* @param string $entityIncrementId
* @return \Mygento\Kkm\Api\Data\TransactionAttemptInterface
*/
public function getByEntityId($operation, $entityIncrementId);

Expand Down
2 changes: 1 addition & 1 deletion Console/SendRefund.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
//Oтправка
$output->writeln("<comment>1. Sending creditmemo {$incrementId} ...</comment>");

$this->processor->proceedRefund($creditmemo, true);
$this->processor->proceedRefund($creditmemo, true, true);

$transactions = $this->transactionHelper->getTransactionsByCreditmemo($creditmemo);

Expand Down
2 changes: 1 addition & 1 deletion Console/SendSell.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
//Oтправка
$output->writeln("<comment>1. Sending invoice {$incrementId} ...</comment>");

$this->processor->proceedSell($invoice, true);
$this->processor->proceedSell($invoice, true, true);

$transactions = $this->transactionHelper->getTransactionsByInvoice($invoice);

Expand Down
12 changes: 8 additions & 4 deletions Controller/Adminhtml/Cheque/Resend.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,20 @@ public function execute()
switch ($entityType) {
case 'invoice':
$entity = $this->invoiceRepository->get($id);
$this->processor->proceedSell($entity);
$comment = 'Cheque was sent to KKM.';
$this->processor->proceedSell($entity, false, true);
$comment = 'Cheque ';
break;
case 'creditmemo':
$entity = $this->creditmemoRepository->get($id);
$this->processor->proceedRefund($entity);
$comment = 'Refund was sent to KKM.';
$this->processor->proceedRefund($entity, false, true);
$comment = 'Refund ';
break;
}

$comment .= $this->kkmHelper->isMessageQueueEnabled()
? 'was placed to queue for further sending.'
: 'was sent to KKM.';

$this->getMessageManager()->addSuccessMessage(__($comment));
} catch (NoSuchEntityException $exc) {
$this->getMessageManager()->addErrorMessage(
Expand Down
8 changes: 8 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ public function isMessageQueueEnabled()
{
return (bool) $this->getConfig('general/async_enabled');
}

/**
* @return int
*/
public function getMaxTrials()
{
return (int) $this->getConfig('general/max_trials');
}
}
13 changes: 13 additions & 0 deletions Helper/TransactionAttempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ public function __construct(
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
* Returns trials number of sending this request
* @param RequestInterface $request
* @return int|null
*/
public function getTrials(RequestInterface $request)
{
$attempt = $this->attemptRepository
->getByEntityId($request->getOperationType(), $request->getSalesEntityId());

return $attempt->getNumberOfTrials();
}

/**
* Create new attempt based on request
* @param RequestInterface $request
Expand Down
4 changes: 2 additions & 2 deletions Model/Atol/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function sendRefund($request): ResponseInterface
{
$debugData = [];
$this->kkmHelper->info('START Sending creditmemo');
$this->kkmHelper->debug('Request', $request->jsonSerialize());
$this->kkmHelper->debug('Request', $request->__toArray());

$request = $debugData['request'] = json_encode($request);

Expand Down Expand Up @@ -182,7 +182,7 @@ public function sendSell($request): ResponseInterface
{
$debugData = [];
$this->kkmHelper->info('START Sending invoice');
$this->kkmHelper->debug('Request:', $request->jsonSerialize());
$this->kkmHelper->debug('Request:', $request->__toArray());

$request = $debugData['request'] = json_encode($request);

Expand Down
39 changes: 39 additions & 0 deletions Model/Atol/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
use Mygento\Kkm\Api\Data\ItemInterface;
use Mygento\Kkm\Api\Data\RequestInterface;

/**
* Class Request
* @package Mygento\Kkm\Model\Atol
* @SuppressWarnings(PHPMD.TooManyFields)
*/
abstract class Request implements \JsonSerializable, RequestInterface
{
// phpcs:disable
Expand Down Expand Up @@ -49,6 +54,11 @@ abstract class Request implements \JsonSerializable, RequestInterface
*/
protected $date = '';

/**
* @var bool
*/
protected $ignoreTrialsNum = false;

/**
* Request constructor.
* @param \Magento\Framework\Stdlib\DateTime\Timezone $date
Expand All @@ -59,6 +69,17 @@ public function __construct(
$this->date = $date;
}

/**
* @inheritDoc
*/
public function __toArray()
{
$array = $this->jsonSerialize();
$array['ignore_trials'] = $this->ignoreTrialsNum;

return $array;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -364,4 +385,22 @@ public function getRetryCount()
{
return $this->retryCount;
}

/**
* @inheritDoc
*/
public function isIgnoreTrialsNum()
{
return (bool) $this->ignoreTrialsNum;
}

/**
* @inheritDoc
*/
public function setIgnoreTrialsNum($ignore)
{
$this->ignoreTrialsNum = (bool) $ignore;

return $this;
}
}
12 changes: 11 additions & 1 deletion Model/Atol/Vendor.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,20 @@ public function addCommentToOrder($entity, ResponseInterface $response, $txnId =
* @throws CreateDocumentFailedException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @return ResponseInterface
* @return ResponseInterface|null
*/
private function sendRequest($request, $callback, $entity = null)
{
$trials = $this->attemptHelper->getTrials($request);
$maxTrials = $this->kkmHelper->getMaxTrials();

//Don't send if trials number exceeded
if ($trials >= $maxTrials && !$request->isIgnoreTrialsNum()) {
$this->kkmHelper->debug('Request is skipped. Max num of trials exceeded');

return null;
}

$entity = $entity ?? $this->requestHelper->getEntityByRequest($request);

//Register sending Attempt
Expand Down
19 changes: 10 additions & 9 deletions Model/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

namespace Mygento\Kkm\Model;

use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\InvoiceInterface;

class Processor
{
const TOPIC_NAME_SELL = 'mygento.kkm.message.sell';
Expand Down Expand Up @@ -50,23 +47,25 @@ public function __construct(
/**
* @param \Magento\Sales\Api\Data\InvoiceInterface $invoice
* @param bool $sync
* @param bool $ignoreTrials
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Mygento\Kkm\Exception\CreateDocumentFailedException
* @throws \Mygento\Kkm\Exception\VendorBadServerAnswerException
* @return bool
*/
public function proceedSell(InvoiceInterface $invoice, $sync = false)
public function proceedSell($invoice, $sync = false, $ignoreTrials = false)
{
$request = $this->vendor->buildRequest($invoice);
$request->setIgnoreTrialsNum($ignoreTrials);

if ($sync || !$this->helper->isMessageQueueEnabled()) {
$this->helper->debug('Queue is disabled. Sending request directly: ', $request->jsonSerialize());
$this->helper->debug('Sending request without Queue: ', $request->__toArray());
$this->vendor->sendSellRequest($request);

return true;
}

$this->helper->debug('Publish request: ', $request->jsonSerialize());
$this->helper->debug('Publish request: ', $request->__toArray());
$this->publisher->publish(self::TOPIC_NAME_SELL, $request);

return true;
Expand All @@ -75,23 +74,25 @@ public function proceedSell(InvoiceInterface $invoice, $sync = false)
/**
* @param \Magento\Sales\Api\Data\CreditmemoInterface $creditmemo
* @param bool $sync
* @param bool $ignoreTrials
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Mygento\Kkm\Exception\CreateDocumentFailedException
* @throws \Mygento\Kkm\Exception\VendorBadServerAnswerException
* @return bool
*/
public function proceedRefund(CreditmemoInterface $creditmemo, $sync = false)
public function proceedRefund($creditmemo, $sync = false, $ignoreTrials = false)
{
$request = $this->vendor->buildRequest($creditmemo);
$request->setIgnoreTrialsNum($ignoreTrials);

if ($sync || !$this->helper->isMessageQueueEnabled()) {
$this->helper->debug('Sending request directly:', $request->jsonSerialize());
$this->helper->debug('Sending request without Queue:', $request->__toArray());
$this->vendor->sendRefundRequest($request);

return true;
}

$this->helper->debug('Publish request to queue:', $request->jsonSerialize());
$this->helper->debug('Publish request to queue:', $request->__toArray());
$this->publisher->publish(self::TOPIC_NAME_REFUND, $request);

return true;
Expand Down
Loading

0 comments on commit 253ee1c

Please sign in to comment.