Files
exchange-2.0/app/Classes/Modules/Bookings/ControllersLogic/AutoPurchaseOrderFillLogic.php
T
2022-01-12 15:29:14 +08:00

115 lines
4.0 KiB
PHP

<?php
namespace App\Classes\Modules\Bookings\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transactions\ControllersLogic\CreatePurchaseOrderTransactionLogic;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Booking;
use App\Models\Transaction;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class AutoPurchaseOrderFillLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => '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::whereMonth('created_at', 7)
->whereYear('created_at', 2021)
->whereDoesntHave('transactions', function($q){
$q->where('type', TransactionType::PURCHASE_ORDER);
$q->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::APPROVED]);
});
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([]);
}
}