diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreateGroupsLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreateGroupsLogic.php index c750f3f2..135ae29d 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreateGroupsLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreateGroupsLogic.php @@ -22,6 +22,7 @@ use App\Models\GroupTransaction; use App\Http\Resources\WalletTransactionResource; use App\Models\Wallet; use App\Classes\Modules\Transactions\Processors\ReleaseGoodsToCustomerProcessor; +use App\Classes\Modules\Transactions\Processors\CheckStorageInvoiceTransactionProcessor; class CreateGroupsLogic extends AbstractControllerLogic @@ -60,6 +61,8 @@ class CreateGroupsLogic extends AbstractControllerLogic /** @var ReleaseGoodsToCustomerProcessor */ private $releaseGoodsToCustomerProcessor; + /** @var CheckStorageInvoiceTransactionProcessor */ + private $checkStorageInvoiceTransactionProcessor; public function __construct( FetchesTransaction $fetchesTransaction, @@ -68,7 +71,8 @@ class CreateGroupsLogic extends AbstractControllerLogic CreateWalletTopUpTransactionProcessor $createWalletTopUpTransactionProcessor, CreatePaymentTransactionProcessor $createPaymentTransactionProcessor, CreatesGroup $createsGroup, - ReleaseGoodsToCustomerProcessor $releaseGoodsToCustomerProcessor + ReleaseGoodsToCustomerProcessor $releaseGoodsToCustomerProcessor, + CheckStorageInvoiceTransactionProcessor $checkStorageInvoiceTransactionProcessor ) { $this->fetchesTransaction = $fetchesTransaction; @@ -78,6 +82,7 @@ class CreateGroupsLogic extends AbstractControllerLogic $this->createPaymentTransactionProcessor = $createPaymentTransactionProcessor; $this->createsGroup = $createsGroup; $this->releaseGoodsToCustomerProcessor = $releaseGoodsToCustomerProcessor; + $this->checkStorageInvoiceTransactionProcessor = $checkStorageInvoiceTransactionProcessor; } public function logic(Request $request) : JsonResponse @@ -87,6 +92,52 @@ class CreateGroupsLogic extends AbstractControllerLogic // todo-new: check why is this not working // $invoices = $this->fetchesTransaction->execute(['id_in' => $invoice_ids]); $invoices = Transaction::whereIn('id', $invoice_ids)->get(); + $typeOneInvoices = $invoices->filter(function ($invoice) { + return $invoice->type == 1; + }); + + // Check amount to be paid by customer 1 + $amount = $request->input('amount'); + if($typeOneInvoices) + { + foreach ($typeOneInvoices as $invoice) { + $storages = $this->checkStorageInvoiceTransactionProcessor->executeShippingTransaction($invoice); + $invoice['storages'] = $storages; + } + + $totalAmount = $typeOneInvoices->reduce(function ($total, $transaction) { + $topLevelAmount = $transaction->amount ?? 0; + $storagesAmount = collect($transaction['storages'] ?? [])->reduce(function ($sum, $storage) { + $storageInvoiceAmount = $storage['storageInvoice']['amount'] ?? 0; + return $sum + $storageInvoiceAmount; + }, 0); + return $total + $topLevelAmount + $storagesAmount; + + }, 0); + $diff = abs($amount - $totalAmount); + if($diff > 0.01){ + $response = ['message' => 'Some information has been updated, please reload the page to proceed. [ref: 001]']; + return $this->response(['data' => $response]); + } + } + + // Check amount to be paid by customer 2 + $totalAmount = 0; + $proceed = false; + foreach ($invoices as $invoice) { + $totalAmount += $invoice->amount; + } + if(abs($totalAmount - $amount ) > 0.01){ + $proceed = true; + } + + if($proceed){ + // $response = ['message' => 'Total: '.$totalAmount. ', submitted amount: '.$request->input('amount').', proceed: true']; + $response = ['message' => 'Some information has been updated, please reload the page to proceed. [ref: 002]']; + return $this->response(['data' => $response]); + } + + $isInvoicesApprove = $this->checkIfInvoicesAreApprove($invoices); if(!$isInvoicesApprove){ $response = ['message' => 'One of the transactions might be suspended; please check if there are any disputes in progress.']; @@ -95,7 +146,6 @@ class CreateGroupsLogic extends AbstractControllerLogic else{ $paymentMethodStr = $request->input('payment_method'); $paymentMethod = PaymentMethodType::PAYMENT_METHODS[$paymentMethodStr]; - $amount = $request->input('amount'); $bankCode = $request->input('bank_code'); $result = $this->processInvoices($invoices, $paymentMethodStr, $amount, $bankCode); diff --git a/app/Classes/Modules/Transactions/ControllersLogic/VerifyTransactionsLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/VerifyTransactionsLogic.php new file mode 100644 index 00000000..317af93b --- /dev/null +++ b/app/Classes/Modules/Transactions/ControllersLogic/VerifyTransactionsLogic.php @@ -0,0 +1,62 @@ + 'Verify Transactions', + 'message' => 'You have successfully verified submitted transactions' + ]; + } + + /** @var CheckStorageInvoiceTransactionProcessor */ + private $checkStorageInvoiceTransactionProcessor; + + + public function __construct( + CheckStorageInvoiceTransactionProcessor $checkStorageInvoiceTransactionProcessor + ) + { + $this->checkStorageInvoiceTransactionProcessor = $checkStorageInvoiceTransactionProcessor; + } + + public function logic(Request $request) : JsonResponse + { + $invoice_ids = json_decode($request->route('transaction_ids')); + $invoices = Transaction::whereIn('id', $invoice_ids)->get(); + + $result = $this->processInvoices($invoices); + return $this->collectionResponse(TransactionVerifyResource::collection($result)); + + } + + private function processInvoices($invoices){ + foreach ($invoices as $invoice) { + $storages = $this->checkStorageInvoiceTransactionProcessor->executeShippingTransaction($invoice); + $invoice['storages'] = $storages; + } + return $invoices; + } +} diff --git a/app/Classes/Modules/Transactions/Processors/CheckStorageInvoiceTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CheckStorageInvoiceTransactionProcessor.php index d5cd0e3c..93d80c14 100644 --- a/app/Classes/Modules/Transactions/Processors/CheckStorageInvoiceTransactionProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/CheckStorageInvoiceTransactionProcessor.php @@ -128,33 +128,58 @@ class CheckStorageInvoiceTransactionProcessor $packingLists = $order->destinationWarehousePackages; foreach ($packingLists as $packingList){ - $arrivalDateAtChinaWarehouse = $this->getArrivalDateAtChinaWarehoue($packingList); - LogHelper::channel('storage_invoices')->info('arrivalDateAtChinaWarehouse: '.json_encode($arrivalDateAtChinaWarehouse)); - LogHelper::channel('storage_invoices')->info('destinationWarehousePackage: '.json_encode($packingList)); - $eta = $this->getEtaFromPackingList($packingList); - if($arrivalDateAtChinaWarehouse && $eta){ - $transactions = $packingList->transactions()->where('transactions.type', TransactionType::SHIPPING_INVOICE)->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->get(); - // $transactions = $destinationWarehousePackage->transactions()->where('transactions.type', TransactionType::SHIPPING_INVOICE)->where('transactions.status', ApprovalStatus::APPROVED)->get(); + $newResults = $this->getResults($packingList, $order, $is_credit_term); + $results = array_merge($results, $newResults); + } - /** @var Transaction $invoice_transaction */ - foreach ($transactions as $invoice_transaction){ - $result = null; - if(!$is_credit_term){ - $result = $this->processSingleTransactionOfTypeShippingInvoice($invoice_transaction, $packingList, $order->company_module_id, $eta); - } - else{ - $result = $this->isPaidOrWaivedStorageInvoiceExist($invoice_transaction, $packingList, $eta); - } - if($result){ - $results[] = $result; - } - } + return $results; + } + + public function executeShippingTransaction(Transaction $shippingInvoice){ + LogHelper::channel('storage_invoices')->info('shippingInvoice: '.json_encode($shippingInvoice)); + $results = []; + $pL = $shippingInvoice->owner; + $order = $pL->owner; + $is_credit_term = $order->companyModule->inviters()->withPivot('is_credit_term')->first()->pivot->is_credit_term; + $packingLists = $order->destinationWarehousePackages; + + foreach ($packingLists as $packingList){ + if($packingList->id === $pL->id){ + $newResults = $this->getResults($packingList, $order, $is_credit_term); + $results = array_merge($results, $newResults); } } return $results; } + private function getResults($packingList, $order, $is_credit_term){ + $results = []; + $arrivalDateAtChinaWarehouse = $this->getArrivalDateAtChinaWarehoue($packingList); + LogHelper::channel('storage_invoices')->info('arrivalDateAtChinaWarehouse: '.json_encode($arrivalDateAtChinaWarehouse)); + LogHelper::channel('storage_invoices')->info('destinationWarehousePackage: '.json_encode($packingList)); + $eta = $this->getEtaFromPackingList($packingList); + if($arrivalDateAtChinaWarehouse && $eta){ + $transactions = $packingList->transactions()->where('transactions.type', TransactionType::SHIPPING_INVOICE)->whereIn('transactions.status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])->get(); + // $transactions = $destinationWarehousePackage->transactions()->where('transactions.type', TransactionType::SHIPPING_INVOICE)->where('transactions.status', ApprovalStatus::APPROVED)->get(); + + /** @var Transaction $invoice_transaction */ + foreach ($transactions as $invoice_transaction){ + $result = null; + if(!$is_credit_term){ + $result = $this->processSingleTransactionOfTypeShippingInvoice($invoice_transaction, $packingList, $order->company_module_id, $eta); + } + else{ + $result = $this->isPaidOrWaivedStorageInvoiceExist($invoice_transaction, $packingList, $eta); + } + if($result){ + $results[] = $result; + } + } + } + return $results; + } + private function getArrivalDateAtChinaWarehoue($packingList){ if($packingList->type === PackingListType::SHIPPING_PACKING_LIST){ $receive_packing_list = PackingList::where('reference', $packingList->reference)->where('type', PackingListType::WAREHOUSE_RECEIVE_LIST)->first(); diff --git a/app/Http/Controllers/Transactions/CreateGroupsController.php b/app/Http/Controllers/Transactions/CreateGroupsController.php index c0863e7e..ac2c49af 100644 --- a/app/Http/Controllers/Transactions/CreateGroupsController.php +++ b/app/Http/Controllers/Transactions/CreateGroupsController.php @@ -12,7 +12,7 @@ class CreateGroupsController { /** * @param Request $request - * @param CreatePurchaseOrderTransactionLogic $logic + * @param CreateGroupsLogic $logic * @return JsonResponse */ public function create(Request $request, CreateGroupsLogic $logic) : JsonResponse { diff --git a/app/Http/Controllers/Transactions/VerifyTransactionsController.php b/app/Http/Controllers/Transactions/VerifyTransactionsController.php new file mode 100644 index 00000000..a4d476d8 --- /dev/null +++ b/app/Http/Controllers/Transactions/VerifyTransactionsController.php @@ -0,0 +1,21 @@ +execute($request); + } +} diff --git a/app/Http/Resources/TransactionVerifyResource.php b/app/Http/Resources/TransactionVerifyResource.php new file mode 100644 index 00000000..ac5e03b5 --- /dev/null +++ b/app/Http/Resources/TransactionVerifyResource.php @@ -0,0 +1,24 @@ + $this->id, + 'amount' => (double) $this->amount, + 'storages' => $this->storages ? $this->storages : null, + ]; + } +} diff --git a/resources/assets/vue/general/mixins/request.js b/resources/assets/vue/general/mixins/request.js index 54ca321a..13babef8 100644 --- a/resources/assets/vue/general/mixins/request.js +++ b/resources/assets/vue/general/mixins/request.js @@ -22,7 +22,7 @@ export default { } successNotification ? this.$store.dispatch('createNotification', {title: response.title, message: response.message, type: 'success'}): null; - this.successHandler(response) + this.successHandler(response, section) }); diff --git a/routes/transaction.php b/routes/transaction.php index 08db2447..7b39290c 100644 --- a/routes/transaction.php +++ b/routes/transaction.php @@ -15,6 +15,8 @@ Route::group(['prefix' => 'transactions', 'namespace' => 'Transactions', 'as' => Route::get('wallet/list', 'ListWalletTransactionsController@list')->name('wallet.list'); + Route::get('/verify/{transaction_ids}', 'VerifyTransactionsController@verify')->name('verify.transactions'); + Route::group(['prefix' => 'payment', 'as' => 'payment.'], function () { Route::post('/create', 'CreatePaymentTransactionController@create')->name('create'); Route::post('/upload-verification-document/{transaction_id}', 'UploadPaymentVerificationDocumentController@upload')->name('verification.create');