diff --git a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php index 47095df5..b7cf6913 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/CreateBookingPaymentLogic.php @@ -10,16 +10,16 @@ use App\Classes\Modules\Bookings\Services\CalculatesBookingOutstanding; use App\Classes\Modules\Bookings\Services\FetchesBookingQuotation; use App\Classes\Modules\Companies\Services\FetchesCompanyPaymentAttemptLimit; use App\Classes\Modules\Currencies\DataTransferObjects\CurrencyConversionObject; -use App\Classes\Modules\Vouchers\DataTransferObjects\VoucherObject; +//use App\Classes\Modules\Vouchers\DataTransferObjects\VoucherObject; use App\Classes\Modules\Transactions\DataTransferObjects\TransactionObject; use App\Classes\Modules\Transactions\Services\CreatesTransaction; use App\Classes\Modules\Transactions\Services\GeneratesTransactionBillNumber; use App\Classes\Modules\Billplzs\Services\CreatesBillplzBill; use App\Classes\Modules\Transactions\Services\UpdatesTransactionStatus; -use App\Classes\Modules\Vouchers\Services\FetchesVoucher; -use App\Classes\Modules\Vouchers\Services\CreatesVoucher; -use App\Classes\Modules\Vouchers\Services\CreatesVoucherRedemption; -use App\Classes\Modules\Vouchers\Services\RedeemsVoucher; +//use App\Classes\Modules\Vouchers\Services\FetchesVoucher; +//use App\Classes\Modules\Vouchers\Services\CreatesVoucher; +//use App\Classes\Modules\Vouchers\Services\CreatesVoucherRedemption; +//use App\Classes\Modules\Vouchers\Services\RedeemsVoucher; use App\Classes\Modules\Wallets\Services\UpdatesWalletBalance; use App\Classes\ValueObjects\Constants\ApprovalStatus; use App\Classes\ValueObjects\Constants\PaymentMethodType; @@ -35,6 +35,30 @@ use App\Classes\Modules\Wallets\Services\RecalculatesWalletBalance; class CreateBookingPaymentLogic extends AbstractControllerLogic { + /** + * @param FetchesBookingQuotation $fetchBookingQuotation + * @param FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit + * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber + * @param CreatesTransaction $createsTransaction + * @param CalculatesBookingOutstanding $calculatesBookingOutstanding + * @param CreatesBillplzBill $createsBillplzBill + * @param UpdatesWalletBalance $updatesWalletBalance + * @param UpdatesTransactionStatus $updatesTransactionStatus + * @param RecalculatesWalletBalance $recalculatesWalletBalance + */ + public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, RecalculatesWalletBalance $recalculatesWalletBalance) + { + $this->fetchBookingQuotation = $fetchBookingQuotation; + $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; + $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; + $this->createsTransaction = $createsTransaction; + $this->calculatesBookingOutstanding = $calculatesBookingOutstanding; + $this->createsBillplzBill = $createsBillplzBill; + $this->updatesWalletBalance = $updatesWalletBalance; + $this->updatesTransactionStatus = $updatesTransactionStatus; + $this->recalculatesWalletBalance = $recalculatesWalletBalance; + } + /** @@ -74,51 +98,20 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic /** @var RecalculatesWalletBalance */ private $recalculatesWalletBalance; - - /** @var FetchesVoucher */ - private $fetchesVoucher; - - /** @var CreatesVoucher */ - private $createsVoucher; - - /** @var CreatesVoucherRedemption */ - private $createsVoucherRedemption; - - /** @var RedeemsVoucher */ - private $redeemsVoucher; +// +// /** @var FetchesVoucher */ +// private $fetchesVoucher; +// +// /** @var CreatesVoucher */ +// private $createsVoucher; +// +// /** @var CreatesVoucherRedemption */ +// private $createsVoucherRedemption; +// +// /** @var RedeemsVoucher */ +// private $redeemsVoucher; - /** - * @param FetchesBookingQuotation $fetchBookingQuotation - * @param FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit - * @param GeneratesTransactionBillNumber $generatesTransactionBillNumber - * @param CreatesTransaction $createsTransaction - * @param CalculatesBookingOutstanding $calculatesBookingOutstanding - * @param CreatesBillplzBill $createsBillplzBill - * @param UpdatesWalletBalance $updatesWalletBalance - * @param UpdatesTransactionStatus $updatesTransactionStatus - * @param RecalculatesWalletBalance $recalculatesWalletBalance - * @param FetchesVoucher $fetchesVoucher - * @param CreatesVoucher $createsVoucher - * @param CreatesVoucherRedemption $createsVoucherRedemption - * @param RedeemsVoucher $redeemsVoucher - */ - public function __construct(FetchesBookingQuotation $fetchBookingQuotation, FetchesCompanyPaymentAttemptLimit $fetchesCompanyPaymentAttemptLimit, GeneratesTransactionBillNumber $generatesTransactionBillNumber, CreatesTransaction $createsTransaction, CalculatesBookingOutstanding $calculatesBookingOutstanding, CreatesBillplzBill $createsBillplzBill, UpdatesWalletBalance $updatesWalletBalance, UpdatesTransactionStatus $updatesTransactionStatus, RecalculatesWalletBalance $recalculatesWalletBalance, FetchesVoucher $fetchesVoucher, CreatesVoucher $createsVoucher, CreatesVoucherRedemption $createsVoucherRedemption, RedeemsVoucher $redeemsVoucher) - { - $this->fetchBookingQuotation = $fetchBookingQuotation; - $this->fetchesCompanyPaymentAttemptLimit = $fetchesCompanyPaymentAttemptLimit; - $this->generatesTransactionBillNumber = $generatesTransactionBillNumber; - $this->createsTransaction = $createsTransaction; - $this->calculatesBookingOutstanding = $calculatesBookingOutstanding; - $this->createsBillplzBill = $createsBillplzBill; - $this->updatesWalletBalance = $updatesWalletBalance; - $this->updatesTransactionStatus = $updatesTransactionStatus; - $this->recalculatesWalletBalance = $recalculatesWalletBalance; - $this->fetchesVoucher = $fetchesVoucher; - $this->createsVoucher = $createsVoucher; - $this->createsVoucherRedemption = $createsVoucherRedemption; - $this->redeemsVoucher = $redeemsVoucher; - } public function logic(Request $request) : JsonResponse { @@ -143,7 +136,7 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic $this->updatesTransactionStatus->execute($transaction, ApprovalStatus::PENDING_VERIFICATION); } - $this->handleVoucherCode($request, $booking, $configurations, $transaction); + // $this->handleVoucherCode($request, $booking, $configurations, $transaction); //todo-important: uncomment this for voucherify return $this->resourceResponse(new TransactionResource($transaction)); } @@ -234,25 +227,25 @@ class CreateBookingPaymentLogic extends AbstractControllerLogic ); } - private function handleVoucherCode(Request $request, Booking $booking, CalculationObject $configurations, Transaction $transaction): void - { - $voucherCode = $request->input('voucher_code'); - if($voucherCode){ - $employee = $booking->company->employees()->first(); - - $result = $this->redeemsVoucher->execute($voucherCode, $configurations->getSubTotal(), $employee); - $voucher = $result->voucher; - $redemptionId = $result->id; - - $voucherValue = $voucher->discount->amount_off ?? ($voucher->discount->percent_off ?? 0.00); - $voucherObject = new VoucherObject($voucher->code, $voucher->metadata->name ?? "", $voucher->discount->type, $voucherValue); - $voucher = $this->createsVoucher->execute($voucherObject); - - if(!$voucher) - $voucher = $this->fetchesVoucher->execute(['code' => $voucherObject->getCode()]); - - $this->createsVoucherRedemption->execute($redemptionId, $voucher->id, $employee->id, $configurations->getVoucherDiscountAmount()); - } - } +// private function handleVoucherCode(Request $request, Booking $booking, CalculationObject $configurations, Transaction $transaction): void +// { +// $voucherCode = $request->input('voucher_code'); +// if($voucherCode){ +// $employee = $booking->company->employees()->first(); +// +// $result = $this->redeemsVoucher->execute($voucherCode, $configurations->getSubTotal(), $employee); +// $voucher = $result->voucher; +// $redemptionId = $result->id; +// +// $voucherValue = $voucher->discount->amount_off ?? ($voucher->discount->percent_off ?? 0.00); +// $voucherObject = new VoucherObject($voucher->code, $voucher->metadata->name ?? "", $voucher->discount->type, $voucherValue); +// $voucher = $this->createsVoucher->execute($voucherObject); +// +// if(!$voucher) +// $voucher = $this->fetchesVoucher->execute(['code' => $voucherObject->getCode()]); +// +// $this->createsVoucherRedemption->execute($redemptionId, $voucher->id, $employee->id, $configurations->getVoucherDiscountAmount()); +// } +// } } diff --git a/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessorV2.php b/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessorV2.php index 3a2d2a5b..cd5990f1 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessorV2.php +++ b/app/Classes/Modules/PerfexCRM/Processors/TransactionToPerfexCRMProcessorV2.php @@ -20,8 +20,9 @@ class TransactionToPerfexCRMProcessorV2 public function execute(Transaction $model, int $status) { try { - if($model->owner instanceof \App\Models\Booking) { - $bookingInfo = $this->extractBookingInfo($model); + if(in_array($model->type, [TransactionType::PAYMENT, TransactionType::BILL, TransactionType::PURCHASE_ORDER])) { + $transaction = ($model->type === TransactionType::BILL ? $model->owner : $model); + $bookingInfo = $this->extractBookingInfo($transaction); $projectName = 'Exchange | ' . $bookingInfo['serviceTypeName'] . ' | ' . $bookingInfo['bookingMarking']; $tasks = $this->defineTasks($model, $status); @@ -37,7 +38,7 @@ class TransactionToPerfexCRMProcessorV2 [], $tasks ); - $this->dispatchUpdateJob($model, $status, $updatePerfexCRMObject); + $this->dispatchUpdateJob($transaction, $status, $updatePerfexCRMObject); } } } catch (\Exception $exception) { @@ -63,17 +64,87 @@ class TransactionToPerfexCRMProcessorV2 private function defineTasks(Transaction $model, int $status): array { $tasks = []; + $ownerServiceId = $model->type === TransactionType::BILL ? $model->owner->owner->service_id : $model->owner->service_id; - if($status === ApprovalStatus::PENDING_VERIFICATION && (int) $model->type === TransactionType::PAYMENT) { - $tasks = $this->definePaymentTasks($model); - } elseif($status === ApprovalStatus::PENDING_VERIFICATION && $model->type === TransactionType::PURCHASE_ORDER) { - if($model->owner->service_id === 1 || $model->owner->service_id === 3) { - $tasks = [PerfexCRMTasks::TASK_PURCHASE_ORDER_1]; - } - } elseif($status === ApprovalStatus::APPROVED && $model->type === TransactionType::PAYMENT) { - $task = PerfexCRMTasks::TASK_1_DAY_TRANSFER_2; - $task['status'] = PerfexCRMTaskStatus::COMPLETED; - $tasks = [$task]; + if ($status === ApprovalStatus::PENDING_VERIFICATION) { + $tasks = $this->handlePendingVerificationStatus($model, $ownerServiceId); + } elseif ($status === ApprovalStatus::APPROVED) { + $tasks = $this->handleApprovedStatus($model, $ownerServiceId); + } + + return $tasks; + } + + private function handlePendingVerificationStatus(Transaction $model, int $serviceId): array + { + $tasks = []; + switch($model->type) { + case TransactionType::PAYMENT: + $tasks = $this->definePaymentTasks($model); + break; + case TransactionType::PURCHASE_ORDER: + if ($serviceId === 1 || $serviceId === 3) { + $task = PerfexCRMTasks::TASK_PURCHASE_ORDER_1; + $task['status'] = PerfexCRMTaskStatus::IN_PROGRESS; + $tasks = [$task]; + } + break; + case TransactionType::BILL: + $tasks = $this->handleBillPendingStatus($serviceId); + break; + } + + return $tasks; + } + + private function handleApprovedStatus(Transaction $model, int $serviceId): array + { + $tasks = []; + switch($model->type) { + case TransactionType::PAYMENT: + $tasks = $this->handlePaymentApprovedStatus($serviceId); + break; + case TransactionType::BILL: + if ($serviceId === 4) { + $tasks = $this->completeTask(PerfexCRMTasks::TASK_1688_PAYMENT_9, PerfexCRMTasks::TASK_1688_PAYMENT_10); + } + break; + } + + return $tasks; + } + + private function handlePaymentApprovedStatus(int $serviceId): array + { + $tasks = []; + switch ($serviceId) { + case 1: + $tasks = $this->completeTask(PerfexCRMTasks::TASK_1_DAY_TRANSFER_2, PerfexCRMTasks::TASK_1_DAY_TRANSFER_5); + break; + case 3: + $tasks = $this->completeTask(PerfexCRMTasks::TASK_3_DAY_TRANSFER_2, PerfexCRMTasks::TASK_3_DAY_TRANSFER_5); + break; + case 4: + $tasks = $this->completeTask(PerfexCRMTasks::TASK_1688_PAYMENT_2, PerfexCRMTasks::TASK_1688_PAYMENT_5); + break; + } + + return $tasks; + } + + private function handleBillPendingStatus(int $serviceId): array + { + $tasks = []; + switch ($serviceId) { + case 1: + $tasks = $this->completeTask(PerfexCRMTasks::TASK_1_DAY_TRANSFER_5, PerfexCRMTasks::TASK_1_DAY_TRANSFER_6); + break; + case 3: + $tasks = $this->completeTask(PerfexCRMTasks::TASK_3_DAY_TRANSFER_5, PerfexCRMTasks::TASK_3_DAY_TRANSFER_6); + break; + case 4: + $tasks = $this->completeTask(PerfexCRMTasks::TASK_1688_PAYMENT_5, PerfexCRMTasks::TASK_1688_PAYMENT_7); + break; } return $tasks; @@ -145,7 +216,7 @@ class TransactionToPerfexCRMProcessorV2 PerfexCRMTasks::TASK_1688_PAYMENT_10, PerfexCRMTasks::TASK_1688_PAYMENT_11, PerfexCRMTasks::TASK_1688_PAYMENT_12, - PerfexCRMTasks::TASK_1688_PAYMENT_13 +// PerfexCRMTasks::TASK_1688_PAYMENT_13 ]; } @@ -162,8 +233,14 @@ class TransactionToPerfexCRMProcessorV2 private function dispatchUpdateJob(Transaction $model, int $status, UpdatePerfexCRMObject $updatePerfexCRMObject) { - Log::info(json_encode($updatePerfexCRMObject->getTasks())); $withInvoice = ($model->type === TransactionType::PAYMENT && $model->owner === Booking::class && $status === ApprovalStatus::APPROVED); UpdatePerfexCRMPrelude::dispatch($model, $updatePerfexCRMObject, $withInvoice); } + + private function completeTask($startTask, $endTask) { + $startTask['status'] = PerfexCRMTaskStatus::COMPLETED; + $endTask['status'] = PerfexCRMTaskStatus::IN_PROGRESS; + + return [$startTask, $endTask]; + } } diff --git a/app/Classes/Modules/PerfexCRM/Processors/UpdatePerfexCRMProcessor.php b/app/Classes/Modules/PerfexCRM/Processors/UpdatePerfexCRMProcessor.php index 0bfe2ba5..7527760f 100644 --- a/app/Classes/Modules/PerfexCRM/Processors/UpdatePerfexCRMProcessor.php +++ b/app/Classes/Modules/PerfexCRM/Processors/UpdatePerfexCRMProcessor.php @@ -205,8 +205,9 @@ class UpdatePerfexCRMProcessor // Get existing or create task $result = $this->fetchesPerfexCRMTask->execute($tasks[$count]['name'], $milestoneId, 'project', $projectId); if(isset($result->payload)){ - Log::info(json_encode([$taskStatus != PerfexCRMTaskStatus::NOT_STARTED, $result->payload[0]['status'] == PerfexCRMTaskStatus::NOT_STARTED])); - if($taskStatus != PerfexCRMTaskStatus::NOT_STARTED && $result->payload[0]['status'] == PerfexCRMTaskStatus::NOT_STARTED) + //&& $result->payload[0]['status'] == PerfexCRMTaskStatus::NOT_STARTED + Log::info(json_encode([$taskStatus])); + if($taskStatus != PerfexCRMTaskStatus::NOT_STARTED ) { $task = $result->payload[0]; $result = $this->updatesPerfexCRMTask->execute($task['id'], $task['name'], $task['milestone'], $task['rel_id'], $taskStatus, $task['startdate'], is_null($task['duedate']) ? '': $task['duedate']); diff --git a/app/Classes/Modules/Transactions/Processors/CreateSupplierTransactionProcessor.php b/app/Classes/Modules/Transactions/Processors/CreateSupplierTransactionProcessor.php index 65a52d99..9b944a5b 100644 --- a/app/Classes/Modules/Transactions/Processors/CreateSupplierTransactionProcessor.php +++ b/app/Classes/Modules/Transactions/Processors/CreateSupplierTransactionProcessor.php @@ -90,10 +90,11 @@ class CreateSupplierTransactionProcessor $object = new TransactionObject($billNumber, TransactionType::BILL, $supplier->id, 1, $supplier->banks()->where('default', true)->first()->id, PaymentMethodType::CASH, $payment->original_amount * (1 / $rate), $payment->original_amount, 1, $payment->original_currency_id, - $rate, 0, $serviceCharge, null, ApprovalStatus::PENDING_VERIFICATION); + $rate, 0, $serviceCharge, null, ApprovalStatus::PENDING_SUBMISSION); /** @var Transaction $billTransaction */ $billTransaction = $this->createsTransaction->execute($payment, $object); + $this->updatesTransactionStatus->execute($billTransaction, ApprovalStatus::PENDING_VERIFICATION); $this->pushBill($billTransaction); $transferFeeNumber = $this->generatesTransactionBillNumber->execute('TRFR-'); @@ -140,4 +141,4 @@ class CreateSupplierTransactionProcessor $this->transferFee->push($transferFee); } -} \ No newline at end of file +}