diff --git a/app/Classes/Modules/Bookings/ControllersLogic/UpdateBookingAmountWithPOLogic.php b/app/Classes/Modules/Bookings/ControllersLogic/UpdateBookingAmountWithPOLogic.php index eaa477a5..02d79e6b 100644 --- a/app/Classes/Modules/Bookings/ControllersLogic/UpdateBookingAmountWithPOLogic.php +++ b/app/Classes/Modules/Bookings/ControllersLogic/UpdateBookingAmountWithPOLogic.php @@ -7,6 +7,7 @@ use App\Classes\Exceptions\MalformedRequestException; use App\Classes\General\Abstracts\AbstractControllerLogic; use App\Classes\Modules\Bookings\Services\FetchesBooking; use App\Classes\Modules\Bookings\Services\UpdatesBookingFixedAmount; +use App\Classes\Modules\Bookings\Services\CalculatesBookingOutstanding; use App\Classes\ValueObjects\Constants\TransactionType; use App\Http\Resources\BookingResource; use App\Models\Booking; @@ -32,15 +33,21 @@ class UpdateBookingAmountWithPOLogic extends AbstractControllerLogic /** @var UpdatesBookingFixedAmount */ private $updatesBookingFixedAmount; + /** @var CalculatesBookingOutstanding */ + private $calculatesBookingOutstanding; + + /** * UpdateBookingAmountWithPOLogic constructor. * @param FetchesBooking $fetchesBooking * @param UpdatesBookingFixedAmount $updatesBookingFixedAmount + * @param CalculatesBookingOutstanding $calculatesBookingOutstanding */ - public function __construct(FetchesBooking $fetchesBooking, UpdatesBookingFixedAmount $updatesBookingFixedAmount) + public function __construct(FetchesBooking $fetchesBooking, UpdatesBookingFixedAmount $updatesBookingFixedAmount, CalculatesBookingOutstanding $calculatesBookingOutstanding) { $this->fetchesBooking = $fetchesBooking; $this->updatesBookingFixedAmount = $updatesBookingFixedAmount; + $this->calculatesBookingOutstanding = $calculatesBookingOutstanding; } /** @@ -54,8 +61,13 @@ class UpdateBookingAmountWithPOLogic extends AbstractControllerLogic { /** @var Booking $booking */ $booking = $this->fetchesBooking->execute(['id' => $request->route('id') ?? $id]); + $outstandingAmount = $this->calculatesBookingOutstanding->execute($booking); $bookingAttribute = $booking->attributesKVP()->where('key', "BOOKING_AMOUNT_UPDATE")->first(); + if(!($outstandingAmount > 0 && $bookingAttribute)){ + throw new MalformedRequestException('Purchase Order at this point can only be edited after editing the booking amount'); + } + if($bookingAttribute){ $total = collect($request->input('products'))->sum(function($product){ return $product['quantity'] * floatval(str_replace(',', '', $product['unit_price'])); diff --git a/resources/assets/vue/components/bookings/forms/PurchaseOrderFormComponent.vue b/resources/assets/vue/components/bookings/forms/PurchaseOrderFormComponent.vue index becc83d0..8c3a40ff 100644 --- a/resources/assets/vue/components/bookings/forms/PurchaseOrderFormComponent.vue +++ b/resources/assets/vue/components/bookings/forms/PurchaseOrderFormComponent.vue @@ -236,7 +236,7 @@ }, created() { this.products = this.data.purchase_order ? this.data.purchase_order.details : []; - this.submitted = this.data.purchase_order ? this.data.purchase_order.status === 1 || this.data.purchase_order.status === 2: false; + this.submitted = this.data.purchase_order ? true : false; // this.submitted = this.data.purchase_order ? this.data.purchase_order.status === 1 || this.data.purchase_order.status === 2: false; }, computed: { productTotal(){ @@ -248,17 +248,23 @@ }, 0); }, allowPOEditing() { + //Condition 1 const noPaymentsMade = Math.round((this.data.paid_amount + Number.EPSILON) * 100) / 100 === 0; - const noPendingVerifications = this.data.payment_history.every(payment => payment.status !== 1); + const noPaymentPendingVerifications = this.data.payment_history.every(payment => payment.status !== 1); - return noPaymentsMade && noPendingVerifications; + //Condition 2 + const paymentsMade = Math.round((this.data.paid_amount + Number.EPSILON) * 100) / 100 > 0; + const outstandingAmount = Math.round((this.data.outstanding_amount + Number.EPSILON) * 100) / 100 > 0; + const allPaymentApproved = this.data.payment_history.every(payment => payment.status === 2); + + return (noPaymentsMade && noPaymentPendingVerifications) || (paymentsMade && outstandingAmount && allPaymentApproved); } }, watch: { 'data': function () { if (this.data && this.data.purchase_order && this.data.purchase_order.details) { this.products = this.data.purchase_order.details; - this.submitted = this.data.purchase_order ? this.data.purchase_order.status === 1 || this.data.purchase_order.status === 2: false; + this.submitted = this.data.purchase_order ? true : false; // this.submitted = this.data.purchase_order ? this.data.purchase_order.status === 1 || this.data.purchase_order.status === 2: false; } else { this.products = []; }