Files
shipping-portal/app/Classes/Modules/Transactions/ControllersLogic/CreateGroupsLogic.php
T
2023-03-21 14:11:42 +08:00

255 lines
9.9 KiB
PHP

<?php
namespace App\Classes\Modules\Transactions\ControllersLogic;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\Services\FetchesTransaction;
use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber;
use App\Classes\Modules\Transactions\Services\CreatesTransactionableTransaction;
use App\Classes\Modules\Transactions\Services\CreatesPaymentTransaction;
use App\Classes\Modules\Transactions\Services\CreatesTransaction;
use App\Classes\Modules\Billplzs\Services\CreatesBillplzBill;
use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject;
use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\Modules\Wallets\Services\UpdatesWalletBalance;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Http\Resources\TransactionResource;
use App\Models\Group;
use App\Models\Transaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Models\Wallet;
use App\Classes\Modules\Orders\Processors\UpdateDoFromVTPortalProcessor;
use App\Classes\Modules\Orders\Processors\UpdateDoFromYDPortalProcessor;
class CreateGroupsLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Create Group Transaction',
'message' => 'You have successfully created a group transaction'
];
}
/** @var FetchesTransaction */
private $fetchesTransaction;
/** @var GeneratesTransactionBillNumber */
private $generatesTransactionBillNumber;
/** @var CreatesTransactionableTransaction */
private $createsTransactionableTransaction;
/** @var CreatesBillplzBill */
private $createsBillplzBill;
/** @var UpdatesWalletBalance */
private $updatesWalletBalance;
/** @var CreatesTransaction */
private $createsTransaction;
/** @var CreatesPaymentTransaction */
private $createsPaymentTransaction;
/** @var UpdateDoFromVTPortalProcessor */
private $updateDoFromVTPortalProcessor;
/** @var UpdateDoFromYDPortalProcessor */
private $updateDoFromYDPortalProcessor ;
/** @var UpdatesTransactionStatus */
private $updatesTransactionStatus ;
public function __construct(
FetchesTransaction $fetchesTransaction,
GeneratesTransactionBillNumber $generatesTransactionBillNumber,
CreatesTransactionableTransaction $createsTransactionableTransaction,
CreatesBillplzBill $createsBillplzBill,
UpdatesWalletBalance $updatesWalletBalance,
CreatesTransaction $createsTransaction,
CreatesPaymentTransaction $createsPaymentTransaction,
UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor,
UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor,
UpdatesTransactionStatus $updatesTransactionStatus
)
{
$this->fetchesTransaction = $fetchesTransaction;
$this->generatesTransactionBillNumber = $generatesTransactionBillNumber;
$this->createsTransactionableTransaction = $createsTransactionableTransaction;
$this->createsBillplzBill = $createsBillplzBill;
$this->updatesWalletBalance = $updatesWalletBalance;
$this->createsPaymentTransaction = $createsPaymentTransaction;
$this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor;
$this->updateDoFromYDPortalProcessor = $updateDoFromYDPortalProcessor;
$this->updatesTransactionStatus = $updatesTransactionStatus;
}
public function logic(Request $request) : JsonResponse
{
$invoice_ids = json_decode($request->route('transaction_ids'));
$billNumber = $this->generatesTransactionBillNumber->execute('PYMT-');
$approvalStatus = ApprovalStatus::PENDING_SUBMISSION;
$payment_method = PaymentMethodType::PAYMENT_METHODS[$request->input('payment_method')];
if (count($invoice_ids) > 1) {
dd('more than 1 invoice');
// creates group transaction
$group = new Group();
$issuer = '';
$receiver = '';
$amount = 0;
$original_amount = 0;
$currency_id = 0;
$original_currency_id = '';
$currency_rate = '';
$tax = 0;
$service_charge = 0;
// todo-new: check why is this not working
// $invoices = $this->fetchesTransaction->execute(['id_in' => $invoice_ids]);
$invoices = Transaction::whereIn('id', $invoice_ids)->get();
foreach ($invoices as $invoice) {
// todo-new: attach transaction to the group id
// create one payment for each, then mark as pending_approval
$issuer = $invoice->issuer;
$receiver = $invoice->receiver;
$amount += $invoice->amount;
$original_amount += $invoice->original_amount;
$currency_id = $invoice->currency_id;
$original_currency_id = $invoice->original_currency_id;
$currency_rate = $invoice->currency_rate;
$tax += $invoice->tax;
$service_charge += $invoice->service_charge;
}
$group->issuer = $issuer;
$group->receiver = $receiver;
$group->reference = $billNumber;
$group->amount = $amount;
$group->original_amount = $original_amount;
$group->currency_id = $currency_id;
$group->original_currency_id = $original_currency_id;
$group->currency_rate = $currency_rate;
$group->tax = $tax;
$group->service_charge = $service_charge;
$group->save();
// attach payment on group transaction
// $invoice_transaction = $this->fetchesTransaction->execute(['id' => $invoice_ids->first()]);
// $amount = $request->input('amount');
// $company_module = $invoice_transaction->owner()->first()->owner()->first()->companyModule()->first();
$invoice_transaction = $group;
$transferDetails = $billNumber;
$company_module = $group->receiverCompany;
} else {
$invoice_transaction = $this->fetchesTransaction->execute(['id' => $invoice_ids[0]]);
$amount = $request->input('amount');
$company_module = $invoice_transaction->owner()->first()->owner()->first()->companyModule()->first();
$transferDetails = $invoice_transaction->bill_no;
}
$payment_reference = null;
if ($payment_method == PaymentMethodType::PAYMENT_GATEWAY) {
// create billplz transaction
$payment_method = PaymentMethodType::PAYMENT_GATEWAY;
$billPlzBill = $this->createsBillplzBill->execute(
$company_module->name,
(app()->environment(['production'])) ? $company_module->employees()->first()->email : 'uldvstar@gmail.com',
'This payment is for the invoice number . ' . $transferDetails,
$amount,
$billNumber,
$request->input('bank_code'),
true
);
$payment_reference = $billPlzBill->id;
}
else if ($payment_method === PaymentMethodType::WALLET) {
/** @var Wallet $wallet */
$wallet = $company_module->wallets()->first();
if((float) number_format(($wallet->amount - $amount),2) < 0){
throw new MalformedRequestException('Insufficient wallet balance. Please Top up your wallet.');
}
$walletPaymentBillNumber = $this->generatesTransactionBillNumber->execute('PYMT-');
$transaction_object = new TransactionObject($walletPaymentBillNumber, TransactionType::PAYMENT, 1, $company_module->id, 1, PaymentMethodType::WALLET, $amount, $amount, 1, 1, 1, 0, 0, null, ApprovalStatus::APPROVED, [], '');
$transaction = $this->createsTransactionableTransaction->execute($wallet, $transaction_object);
$payment_reference = $walletPaymentBillNumber;
$this->updatesWalletBalance->execute($wallet, ($amount * -1));
if (count($invoice_ids) > 1) {
// do one by one
// dd($invoices);
// todo-new: update all the transaction inside
} else {
// $invoice_transaction;
$packingList = $invoice_transaction->owner;
$order = $packingList->owner;
$packingList->status = ApprovalStatus::APPROVED;
$packingList->save();
}
if(app()->environment('production')){
$this->updateDoFromVTPortalProcessor->execute($packingList);
$this->updateDoFromYDPortalProcessor->execute($packingList);
}
$approvalStatus = ApprovalStatus::APPROVED;
// update invoice to completed
$this->updatesTransactionStatus->execute($invoice_transaction, ApprovalStatus::COMPLETED);
}
else {
$payment_method = PaymentMethodType::CASH;
}
// createsPaymentTransaction
$object = new TransactionObject(
$billNumber,
TransactionType::PAYMENT,
$company_module->id,
1,
1,
$payment_method,
$request->input('amount'),
$request->input('amount'),
1,
1,
0,
0,
0,
null,
$approvalStatus,
null,
$payment_reference
);
$payment_transaction = $this->createsPaymentTransaction->execute($invoice_transaction, $object);
return $this->resourceResponse(new TransactionResource($payment_transaction));
}
}