diff --git a/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php index 44aceebc..6ed08994 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php @@ -64,56 +64,50 @@ class AutoPurchaseOrderFillLogic extends AbstractControllerLogic */ public function logic(Request $request) : JsonResponse { - try { - $bookings = Booking::whereMonth('created_at', 9) - ->whereYear('created_at', 2021) - ->whereDoesntHave('transactions', function($q){ - $q->where('type', TransactionType::PURCHASE_ORDER); - $q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]); - })->get(); + $bookings = Booking::whereMonth('created_at', 9) + ->whereYear('created_at', 2021) + ->whereDoesntHave('transactions', function($q){ + $q->where('type', TransactionType::PURCHASE_ORDER); + $q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]); + })->get(); + foreach ($bookings as $booking) { + $po = Transaction::where('type', TransactionType::PURCHASE_ORDER) + ->where('status', ApprovalStatus::APPROVED)->where('issuer', $booking->company_id) + ->select('*', DB::raw('abs(amount - ' . $booking->fix_amount . ') as nearest_price'))->orderBy('nearest_price')->first(); - foreach ($bookings as $booking) { + + if (!$po) { $po = Transaction::where('type', TransactionType::PURCHASE_ORDER) - ->where('status', ApprovalStatus::APPROVED)->where('issuer', $booking->company_id) - ->select('*', DB::raw('abs(amount - ' . $booking->fix_amount . ') as nearest_price'))->orderBy('nearest_price')->first(); - - - if (!$po) { - $po = Transaction::where('type', TransactionType::PURCHASE_ORDER) - ->where('status', ApprovalStatus::APPROVED)->select('*', DB::raw('abs(amount - ' . $booking->fix_amount . ') as nearest_price'))->orderBy('nearest_price')->first(); - } - - $products = $this->generatesPurchaseOrderProducts->execute($po, $booking->fix_amount); - - $deference = $booking->fix_amount - $products->sum('total'); - - if($deference > -150 && $deference < 150 && $deference != 0) { - - $products->push([ - 'description' => $deference < 0 ? 'Discount':'Shipping Fee', - 'quantity' => 1, - 'stockCode' => '', - 'total' => $deference, - 'unit_price' => $deference - ]); - } - - $billNumber = $this->generatesTransactionBillNumber->execute('XPO-'); - - $total = $products->sum('total'); - - $object = new TransactionObject($billNumber, TransactionType::PURCHASE_ORDER, $booking->company->id, 1, - 1, PaymentMethodType::CASH, - $total, $total, $booking->fix_currency_id, $booking->fix_currency_id, - 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, $products->toArray()); - -// $this->createPurchaseOrderTransactionProcessor->execute($booking, $object); + ->where('status', ApprovalStatus::APPROVED)->select('*', DB::raw('abs(amount - ' . $booking->fix_amount . ') as nearest_price'))->orderBy('nearest_price')->first(); } - return $this->response([]); - } catch (\Exception $exception){ - dd($exception); + $products = $this->generatesPurchaseOrderProducts->execute($po, $booking->fix_amount); + + $deference = $booking->fix_amount - $products->sum('total'); + + if($deference > -150 && $deference < 150 && $deference != 0) { + + $products->push([ + 'description' => $deference < 0 ? 'Discount':'Shipping Fee', + 'quantity' => 1, + 'stockCode' => '', + 'total' => $deference, + 'unit_price' => $deference + ]); + } + + $billNumber = $this->generatesTransactionBillNumber->execute('XPO-'); + + $total = $products->sum('total'); + + $object = new TransactionObject($billNumber, TransactionType::PURCHASE_ORDER, $booking->company->id, 1, + 1, PaymentMethodType::CASH, + $total, $total, $booking->fix_currency_id, $booking->fix_currency_id, + 1, 0, 0, null, ApprovalStatus::PENDING_SUBMISSION, $products->toArray()); + + $this->createPurchaseOrderTransactionProcessor->execute($booking, $object); } + return $this->response([]); } } \ No newline at end of file diff --git a/app/Classes/Modules/Transactions/Services/GeneratesPurchaseOrderProducts.php b/app/Classes/Modules/Transactions/Services/GeneratesPurchaseOrderProducts.php index 16d162eb..647fac1e 100644 --- a/app/Classes/Modules/Transactions/Services/GeneratesPurchaseOrderProducts.php +++ b/app/Classes/Modules/Transactions/Services/GeneratesPurchaseOrderProducts.php @@ -30,14 +30,15 @@ class GeneratesPurchaseOrderProducts $amountDifference = $amount - $transaction->amount; $transactionDetails = $transaction->transactionDetails()->select('*', DB::raw('abs(price - '.abs($amountDifference).') as nearest_price'))->orderBy('nearest_price')->get(); foreach ($transactionDetails as $product) { - $units = floor(abs($amountDifference) / $product->price); - $quantity = $product->quantity; if($product->price <= 0){ $amountDifference = $amountDifference + ($product->price * $product->quantity); continue; } + $units = floor(abs($amountDifference) / $product->price); + $quantity = $product->quantity; + if($amountDifference > 0){ $quantity = $product->quantity + $units; $amountDifference = $amountDifference - ($product->price * $units);