diff --git a/app/Exceptions/PointException.php b/app/Exceptions/PointException.php index 1102ad1b1..5e4fdf1e8 100644 --- a/app/Exceptions/PointException.php +++ b/app/Exceptions/PointException.php @@ -6,8 +6,8 @@ class PointException extends Exception { - public function __construct() + public function __construct($message = 'something went wrong') { - parent::__construct('something went wrong', 422); + parent::__construct($message, 422); } } diff --git a/app/Model/Finance/Payment/Payment.php b/app/Model/Finance/Payment/Payment.php index 83cc1b7d2..97c8c8306 100644 --- a/app/Model/Finance/Payment/Payment.php +++ b/app/Model/Finance/Payment/Payment.php @@ -4,7 +4,6 @@ use App\Exceptions\BranchNullException; use App\Exceptions\PointException; -use App\Model\Accounting\ChartOfAccount; use App\Model\Accounting\Journal; use App\Model\Finance\PaymentOrder\PaymentOrder; use App\Model\Form; @@ -36,29 +35,6 @@ class Payment extends TransactionModel 'amount' => 'double', ]; - public function paymentAccount() - { - return $this->belongsTo(ChartOfAccount::class, 'payment_account_id'); - } - - public function details() - { - return $this->hasMany(PaymentDetail::class, 'payment_id'); - } - - public function form() - { - return $this->morphOne(Form::class, 'formable'); - } - - /** - * Get all of the owning paymentable models. - */ - public function paymentable() - { - return $this->morphTo(); - } - public function isAllowedToUpdate() { // TODO isAllowed to update? diff --git a/app/Model/Finance/PaymentOrder/PaymentOrder.php b/app/Model/Finance/PaymentOrder/PaymentOrder.php index 068339971..4a44abd6c 100644 --- a/app/Model/Finance/PaymentOrder/PaymentOrder.php +++ b/app/Model/Finance/PaymentOrder/PaymentOrder.php @@ -3,6 +3,7 @@ namespace App\Model\Finance\PaymentOrder; use App\Exceptions\IsReferencedException; +use App\Exceptions\PointException; use App\Model\Form; use App\Model\TransactionModel; use App\Traits\Model\Finance\PaymentOrderJoin; @@ -53,17 +54,17 @@ public function setPaymentTypeAttribute($value) public function isAllowedToUpdate() { - // Check if not referenced by purchase order + // Check if not referenced by another form if (optional($this->payment)->count()) { - throw new IsReferencedException('Cannot edit form because referenced by purchase receive', $this->payment); + throw new IsReferencedException('Cannot edit form because it is already paid', $this->payment); } } public function isAllowedToDelete() { - // Check if not referenced by purchase order + // Check if not referenced by another form if (optional($this->payment)->count()) { - throw new IsReferencedException('Cannot edit form because referenced by purchase receive', $this->payment); + throw new IsReferencedException('Cannot delete form because it is already paid', $this->payment); } } @@ -75,6 +76,11 @@ public static function create($data) $paymentOrderDetails = self::mapPaymentOrderDetails($data['details'] ?? []); $paymentOrder->amount = self::calculateAmount($paymentOrderDetails); + + if ($paymentOrder->amount < 0) { + throw new PointException('You have negative amount'); + } + $paymentOrder->paymentable_name = $paymentOrder->paymentable->name; $paymentOrder->save(); diff --git a/app/Traits/Model/Finance/PaymentRelation.php b/app/Traits/Model/Finance/PaymentRelation.php index f766ca409..ba6097fde 100644 --- a/app/Traits/Model/Finance/PaymentRelation.php +++ b/app/Traits/Model/Finance/PaymentRelation.php @@ -3,7 +3,9 @@ namespace App\Traits\Model\Finance; +use App\Model\Accounting\ChartOfAccount; use App\Model\Finance\Payment\Payment; +use App\Model\Finance\Payment\PaymentDetail; use App\Model\Finance\PaymentOrder\PaymentOrderDetail; use App\Model\Form; use App\Model\Purchase\PurchaseOrder\PurchaseOrder; @@ -25,9 +27,14 @@ public function paymentable() return $this->morphTo(); } + public function paymentAccount() + { + return $this->belongsTo(ChartOfAccount::class, 'payment_account_id'); + } + public function details() { - return $this->hasMany(PaymentOrderDetail::class); + return $this->hasMany(PaymentDetail::class, 'payment_id'); } // Select relation that not archived and not canceled