Merge branch 'dillon/34.8a-enhancement' into vapor/production

This commit is contained in:
Dillon Ngo
2024-11-02 06:08:31 +08:00
8 changed files with 208 additions and 24 deletions
@@ -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);
@@ -0,0 +1,62 @@
<?php
namespace App\Classes\Modules\Transactions\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Companies\Services\FetchesCompanyModule;
use App\Classes\Modules\Transactions\Processors\CreatePaymentTransactionProcessor;
use App\Classes\Modules\Groups\Services\CreatesGroup;
use App\Models\Transaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\Modules\Wallets\Processors\CreateWalletTopUpTransactionProcessor;
use App\Classes\Modules\Transactions\Processors\ReleaseGoodsToCustomerProcessor;
use App\Classes\Modules\Transactions\Processors\CheckStorageInvoiceTransactionProcessor;
use App\Http\Resources\TransactionVerifyResource;
class VerifyTransactionsLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => '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;
}
}
@@ -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();
@@ -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 {
@@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers\Transactions;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\Modules\Transactions\ControllersLogic\VerifyTransactionsLogic;
class VerifyTransactionsController
{
/**
* @param Request $request
* @param VerifyTransactionsLogic $logic
* @return JsonResponse
*/
public function verify(Request $request, VerifyTransactionsLogic $logic) : JsonResponse {
return $logic->execute($request);
}
}
@@ -0,0 +1,24 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class TransactionVerifyResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
// 'id' => $this->id,
'amount' => (double) $this->amount,
'storages' => $this->storages ? $this->storages : null,
];
}
}
+1 -1
View File
@@ -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)
});
+2
View File
@@ -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');