mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Working on credits on autobill
This commit is contained in:
parent
cd0dce3816
commit
459523722c
@ -52,7 +52,7 @@ class AutoBillInvoice extends AbstractService
|
|||||||
if ((int)$this->invoice->balance == 0)
|
if ((int)$this->invoice->balance == 0)
|
||||||
return $this->invoice->service()->markPaid()->save();
|
return $this->invoice->service()->markPaid()->save();
|
||||||
|
|
||||||
$this->applyCreditPayment();
|
$this->applyCreditPayment(); //if the credits cover the payments, we stop here, build the payment with credits and exit early
|
||||||
|
|
||||||
/* Determine $amount */
|
/* Determine $amount */
|
||||||
if ($this->invoice->partial > 0)
|
if ($this->invoice->partial > 0)
|
||||||
@ -112,22 +112,44 @@ class AutoBillInvoice extends AbstractService
|
|||||||
$is_partial_amount = true;
|
$is_partial_amount = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->payment = PaymentFactory::create($this->client->company_id, $this->client->user_id);
|
$used_credit = []
|
||||||
$this->payment->save();
|
|
||||||
|
|
||||||
$available_credits->each(function($credit) use($is_partial_amount){
|
foreach($available_credits as $key => $credit) {
|
||||||
|
|
||||||
//todo need to iterate until the partial or balance is completely consumed
|
if($is_partial_amount) {
|
||||||
//by the credit, any remaining balance is then dealt with by
|
|
||||||
//the gateway
|
//more credit than needed
|
||||||
//each time a credit is applied SAVE the invoice
|
if($credit->balance >= $this->invoice->partial) {
|
||||||
|
$used_credit[$key]['credit_id'] = $credit->id;
|
||||||
|
$used_credit[$key]['amount'] = $this->invoice->partial;
|
||||||
|
$this->invoice->partial = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$used_credit[$key]['credit_id'] = $credit->id;
|
||||||
|
$used_credit[$key]['amount'] = $credit->balance;
|
||||||
|
$this->invoice->partial -= $credit->balance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
//more credit than needed
|
||||||
|
if($credit->balance >= $this->invoice->balance) {
|
||||||
|
$used_credit[$key]['credit_id'] = $credit->id;
|
||||||
|
$used_credit[$key]['amount'] = $this->invoice->balance;
|
||||||
|
$this->invoice->balance = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$used_credit[$key]['credit_id'] = $credit->id;
|
||||||
|
$used_credit[$key]['amount'] = $credit->balance;
|
||||||
|
$this->invoice->balance -= $credit->balance;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if($credit->balance >= $amount){
|
|
||||||
// //current credit covers the total amount
|
|
||||||
|
|
||||||
// }
|
|
||||||
//return false to exit each loop
|
|
||||||
});
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -173,7 +195,6 @@ class AutoBillInvoice extends AbstractService
|
|||||||
|
|
||||||
$credit = $credit->calc()->getCredit();
|
$credit = $credit->calc()->getCredit();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user