From 8305b66e83ec5b2221821fe48813da8f039d2373 Mon Sep 17 00:00:00 2001 From: glovetleong Date: Sun, 9 Jan 2022 22:40:41 +0800 Subject: [PATCH] auto generate product list --- .../AutoPackingListFillLogic.php | 116 ++++++++++++++++++ .../CreatePurchaseOrderTransactionLogic.php | 5 +- .../AutoPackingListFillController.php | 20 +++ routes/booking.php | 4 + 4 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 app/Classes/Modules/Bookings/ControllersLogic/AutoPackingListFillLogic.php create mode 100644 app/Http/Controllers/Bookings/AutoPackingListFillController.php diff --git a/app/Classes/Modules/Bookings/ControllersLogic/AutoPackingListFillLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/AutoPackingListFillLogic.php new file mode 100644 index 00000000..949973b7 --- /dev/null +++ b/app/Classes/Modules/Bookings/ControllersLogic/AutoPackingListFillLogic.php @@ -0,0 +1,116 @@ + 'Purchase Order Approval', + 'message' => 'You have successfully updated the Purchase order status' + ]; + } + + /** @var CreatePurchaseOrderTransactionLogic */ + private $createPurchaseOrderTransactionLogic; + + /** + * AutoPackingListFillLogic constructor. + * @param CreatePurchaseOrderTransactionLogic $createPurchaseOrderTransactionLogic + */ + public function __construct(CreatePurchaseOrderTransactionLogic $createPurchaseOrderTransactionLogic) + { + $this->createPurchaseOrderTransactionLogic = $createPurchaseOrderTransactionLogic; + } + + /** + * @param Request $request + * @return JsonResponse + * @throws \App\Classes\Exceptions\MalformedRequestException + */ + public function logic(Request $request) : JsonResponse + { + $booking = Booking::where('created_at', '>', '01-07-2021') + ->whereDoesntHave('transactions', function($q){ + $q->where('type', TransactionType::PURCHASE_ORDER); + $q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]); + }) + ->get(); + + + foreach ($booking as $key => $row) { + $amount = $row->fix_amount; + $other_transaction = Transaction:: + where('type', TransactionType::PURCHASE_ORDER) + ->where('status', ApprovalStatus::APPROVED) + ->where('amount', '<=', $amount) + ->where('issuer', $row->company_id) + ->first(); + + + if (!$other_transaction) { + $other_transaction = Transaction:: + where('type', TransactionType::PURCHASE_ORDER) + ->where('status', ApprovalStatus::APPROVED) + ->where('amount', '<=', $amount) + ->first(); + } + + if ($other_transaction) { + $other_transaction_detail = $other_transaction->transactionDetails()->get(); + + $other_amount = 0; + foreach ($other_transaction_detail as $key_2 => $row_2) { + + if ($amount > $other_amount) { + $other_amount += $row_2->amount; + $products[] = [ + 'description' => $row_2->product_name, + 'quantity' => (int) $row_2->quantity, + 'stockCode' => $row_2->product_code, + 'total' => $row_2->amount, + 'unit_price' => (int) $row_2->price, + ]; + } + } + + $remain_amount = $amount - $other_amount; + + if ($remain_amount > 0) { + $remain = [ + 'description' => 'other service charge', + 'quantity' => 1, + 'stockCode' => 'OTHER_SERVICE_CHARGE', + 'total' => $remain_amount, + 'unit_price' => $remain_amount, + ]; + + $products = array_merge([$remain], $products); + } + $request->request->add([ + 'products' => $products + ]); + + $create_transaction = $this->createPurchaseOrderTransactionLogic->logic($request, $row->id); + } + } + + return $this->response([]); + } +} \ No newline at end of file diff --git a/app/Classes/Modules/Transactions/ControllersLogic/CreatePurchaseOrderTransactionLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/CreatePurchaseOrderTransactionLogic.php index aa4c6f4d..afd589c6 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/CreatePurchaseOrderTransactionLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/CreatePurchaseOrderTransactionLogic.php @@ -83,16 +83,15 @@ class CreatePurchaseOrderTransactionLogic extends AbstractControllerLogic * @return JsonResponse * @throws \App\Classes\Exceptions\MalformedRequestException */ - public function logic(Request $request) : JsonResponse + public function logic(Request $request, $id = '') : JsonResponse { /** @var Booking $booking */ - $booking = $this->fetchesBooking->execute(['id' => $request->route('id')]); + $booking = $this->fetchesBooking->execute(['id' => $request->route('id') ?? $id]); /** @var Transaction $transaction */ $transaction = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first(); $billNumber = $this->generatesTransactionBillNumber->execute('PO-'); - $total = collect($request->input('products'))->sum(function($product){ return $product['quantity'] * floatval(str_replace(',', '', $product['unit_price'])); }); diff --git a/app/Http/Controllers/Bookings/AutoPackingListFillController.php b/app/Http/Controllers/Bookings/AutoPackingListFillController.php new file mode 100644 index 00000000..3c6580c0 --- /dev/null +++ b/app/Http/Controllers/Bookings/AutoPackingListFillController.php @@ -0,0 +1,20 @@ +execute($request); + } + +} \ No newline at end of file diff --git a/routes/booking.php b/routes/booking.php index 44767438..e19add01 100644 --- a/routes/booking.php +++ b/routes/booking.php @@ -31,4 +31,8 @@ Route::group(['prefix' => 'booking', 'as' => 'booking.', 'namespace' => 'Booking Route::post('/merge', 'MergeBookingController@merge')->name('merge'); Route::post('{id}/proforma/create', 'CreateProformaInvoiceTransaction@create')->name('proforma.create'); + + + Route::post('/auto-packing-list-fill', 'AutoPackingListFillController@auto')->name('assign'); + }); \ No newline at end of file