Skip to content

Commit

Permalink
fix relation
Browse files Browse the repository at this point in the history
  • Loading branch information
martiendt committed May 28, 2020
1 parent 8352c87 commit 7c1f542
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/Exceptions/PointException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
24 changes: 0 additions & 24 deletions app/Model/Finance/Payment/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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?
Expand Down
14 changes: 10 additions & 4 deletions app/Model/Finance/PaymentOrder/PaymentOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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();

Expand Down
9 changes: 8 additions & 1 deletion app/Traits/Model/Finance/PaymentRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 7c1f542

Please sign in to comment.