fixed invoice/bill payment
This commit is contained in:
parent
f87a754066
commit
d3c49d11f6
194
app/Abstracts/DocumentModel.php
Normal file
194
app/Abstracts/DocumentModel.php
Normal file
@ -0,0 +1,194 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Abstracts;
|
||||||
|
|
||||||
|
use App\Abstracts\Model;
|
||||||
|
use App\Models\Banking\Transaction;
|
||||||
|
use App\Models\Setting\Currency;
|
||||||
|
use App\Traits\Currencies;
|
||||||
|
use App\Traits\DateTime;
|
||||||
|
use App\Traits\Media;
|
||||||
|
use App\Traits\Recurring;
|
||||||
|
use Bkwld\Cloner\Cloneable;
|
||||||
|
|
||||||
|
abstract class DocumentModel extends Model
|
||||||
|
{
|
||||||
|
use Cloneable, Currencies, DateTime, Media, Recurring;
|
||||||
|
|
||||||
|
public function scopeDue($query, $date)
|
||||||
|
{
|
||||||
|
return $query->whereDate('due_at', '=', $date);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopeAccrued($query)
|
||||||
|
{
|
||||||
|
return $query->where('status', '<>', 'draft');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopePaid($query)
|
||||||
|
{
|
||||||
|
return $query->where('status', '=', 'paid');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function scopeNotPaid($query)
|
||||||
|
{
|
||||||
|
return $query->where('status', '<>', 'paid');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current balance.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAttachmentAttribute($value)
|
||||||
|
{
|
||||||
|
if (!empty($value) && !$this->hasMedia('attachment')) {
|
||||||
|
return $value;
|
||||||
|
} elseif (!$this->hasMedia('attachment')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->getMedia('attachment')->last();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the discount percentage.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getDiscountAttribute()
|
||||||
|
{
|
||||||
|
$percent = 0;
|
||||||
|
|
||||||
|
$discount = $this->totals()->where('code', 'discount')->value('amount');
|
||||||
|
|
||||||
|
if ($discount) {
|
||||||
|
$sub_total = $this->totals()->where('code', 'sub_total')->value('amount');
|
||||||
|
|
||||||
|
$percent = number_format((($discount * 100) / $sub_total), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $percent;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the paid amount.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getPaidAttribute()
|
||||||
|
{
|
||||||
|
if (empty($this->amount)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$paid = 0;
|
||||||
|
$reconciled = $reconciled_amount = 0;
|
||||||
|
|
||||||
|
if ($this->transactions->count()) {
|
||||||
|
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
|
||||||
|
|
||||||
|
foreach ($this->transactions as $item) {
|
||||||
|
if ($this->currency_code == $item->currency_code) {
|
||||||
|
$amount = (double) $item->amount;
|
||||||
|
} else {
|
||||||
|
$default_model = new Transaction();
|
||||||
|
$default_model->default_currency_code = $this->currency_code;
|
||||||
|
$default_model->amount = $item->amount;
|
||||||
|
$default_model->currency_code = $item->currency_code;
|
||||||
|
$default_model->currency_rate = $currencies[$item->currency_code];
|
||||||
|
|
||||||
|
$default_amount = (double) $default_model->getDivideConvertedAmount();
|
||||||
|
|
||||||
|
$convert_model = new Transaction();
|
||||||
|
$convert_model->default_currency_code = $item->currency_code;
|
||||||
|
$convert_model->amount = $default_amount;
|
||||||
|
$convert_model->currency_code = $this->currency_code;
|
||||||
|
$convert_model->currency_rate = $currencies[$this->currency_code];
|
||||||
|
|
||||||
|
$amount = (double) $convert_model->getAmountConvertedFromCustomDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
$paid += $amount;
|
||||||
|
|
||||||
|
if ($item->reconciled) {
|
||||||
|
$reconciled_amount = +$amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->amount == $reconciled_amount) {
|
||||||
|
$reconciled = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->setAttribute('reconciled', $reconciled);
|
||||||
|
|
||||||
|
return $paid;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get the status label.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getStatusLabelAttribute()
|
||||||
|
{
|
||||||
|
switch ($this->status) {
|
||||||
|
case 'paid':
|
||||||
|
$label = 'success';
|
||||||
|
break;
|
||||||
|
case 'partial':
|
||||||
|
$label = 'info';
|
||||||
|
break;
|
||||||
|
case 'sent':
|
||||||
|
case 'received':
|
||||||
|
$label = 'danger';
|
||||||
|
break;
|
||||||
|
case 'viewed':
|
||||||
|
$label = 'warning';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$label = 'primary';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $label;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the amount without tax.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getAmountWithoutTaxAttribute()
|
||||||
|
{
|
||||||
|
$amount = $this->amount;
|
||||||
|
|
||||||
|
$this->totals()->where('code', 'tax')->each(function ($tax) use(&$amount) {
|
||||||
|
$amount -= $tax->amount;
|
||||||
|
});
|
||||||
|
|
||||||
|
return $amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert amount to double.
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setAmountAttribute($value)
|
||||||
|
{
|
||||||
|
$this->attributes['amount'] = (double) $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert currency rate to double.
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setCurrencyRateAttribute($value)
|
||||||
|
{
|
||||||
|
$this->attributes['currency_rate'] = (double) $value;
|
||||||
|
}
|
||||||
|
}
|
@ -45,7 +45,7 @@ class BillTransactions extends Controller
|
|||||||
|
|
||||||
$payment_methods = Modules::getPaymentMethods();
|
$payment_methods = Modules::getPaymentMethods();
|
||||||
|
|
||||||
$paid = $this->getPaidAmount($bill);
|
$paid = $bill->paid;
|
||||||
|
|
||||||
// Get Bill Totals
|
// Get Bill Totals
|
||||||
foreach ($bill->totals as $bill_total) {
|
foreach ($bill->totals as $bill_total) {
|
||||||
@ -96,44 +96,4 @@ class BillTransactions extends Controller
|
|||||||
|
|
||||||
return response()->json($response);
|
return response()->json($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPaidAmount($bill)
|
|
||||||
{
|
|
||||||
$paid = 0;
|
|
||||||
|
|
||||||
// Get Bill Transactions
|
|
||||||
if (!$bill->transactions->count()) {
|
|
||||||
return $paid;
|
|
||||||
}
|
|
||||||
|
|
||||||
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
|
|
||||||
|
|
||||||
foreach ($bill->transactions as $item) {
|
|
||||||
$default_amount = (double) $item->amount;
|
|
||||||
|
|
||||||
if ($bill->currency_code == $item->currency_code) {
|
|
||||||
$amount = $default_amount;
|
|
||||||
} else {
|
|
||||||
$default_amount_model = new Transaction();
|
|
||||||
$default_amount_model->default_currency_code = $bill->currency_code;
|
|
||||||
$default_amount_model->amount = $default_amount;
|
|
||||||
$default_amount_model->currency_code = $item->currency_code;
|
|
||||||
$default_amount_model->currency_rate = $currencies[$item->currency_code];
|
|
||||||
|
|
||||||
$default_amount = (double) $default_amount_model->getDivideConvertedAmount();
|
|
||||||
|
|
||||||
$convert_amount_model = new Transaction();
|
|
||||||
$convert_amount_model->default_currency_code = $item->currency_code;
|
|
||||||
$convert_amount_model->amount = $default_amount;
|
|
||||||
$convert_amount_model->currency_code = $bill->currency_code;
|
|
||||||
$convert_amount_model->currency_rate = $currencies[$bill->currency_code];
|
|
||||||
|
|
||||||
$amount = (double) $convert_amount_model->getAmountConvertedFromCustomDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
$paid += $amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $paid;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
namespace App\Http\Controllers\Modals;
|
namespace App\Http\Controllers\Modals;
|
||||||
|
|
||||||
use App\Abstracts\Http\Controller;
|
use App\Abstracts\Http\Controller;
|
||||||
use App\Events\Sale\PaymentReceived;
|
|
||||||
use App\Http\Requests\Banking\Transaction as Request;
|
use App\Http\Requests\Banking\Transaction as Request;
|
||||||
|
use App\Jobs\Banking\CreateDocumentTransaction;
|
||||||
use App\Models\Banking\Account;
|
use App\Models\Banking\Account;
|
||||||
use App\Models\Banking\Transaction;
|
use App\Models\Banking\Transaction;
|
||||||
use App\Models\Sale\Invoice;
|
use App\Models\Sale\Invoice;
|
||||||
@ -45,7 +45,7 @@ class InvoiceTransactions extends Controller
|
|||||||
|
|
||||||
$payment_methods = Modules::getPaymentMethods();
|
$payment_methods = Modules::getPaymentMethods();
|
||||||
|
|
||||||
$paid = $this->getPaidAmount($invoice);
|
$paid = $invoice->paid;
|
||||||
|
|
||||||
// Get Invoice Totals
|
// Get Invoice Totals
|
||||||
foreach ($invoice->totals as $invoice_total) {
|
foreach ($invoice->totals as $invoice_total) {
|
||||||
@ -83,7 +83,7 @@ class InvoiceTransactions extends Controller
|
|||||||
*/
|
*/
|
||||||
public function store(Invoice $invoice, Request $request)
|
public function store(Invoice $invoice, Request $request)
|
||||||
{
|
{
|
||||||
$response = $this->ajaxDispatch(new PaymentReceived($invoice, $request));
|
$response = $this->ajaxDispatch(new CreateDocumentTransaction($invoice, $request));
|
||||||
|
|
||||||
if ($response['success']) {
|
if ($response['success']) {
|
||||||
$response['redirect'] = route('invoices.show', $invoice->id);
|
$response['redirect'] = route('invoices.show', $invoice->id);
|
||||||
@ -97,44 +97,4 @@ class InvoiceTransactions extends Controller
|
|||||||
|
|
||||||
return response()->json($response);
|
return response()->json($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getPaidAmount($invoice)
|
|
||||||
{
|
|
||||||
$paid = 0;
|
|
||||||
|
|
||||||
// Get invoice transactions
|
|
||||||
if (!$invoice->transactions->count()) {
|
|
||||||
return $paid;
|
|
||||||
}
|
|
||||||
|
|
||||||
$_currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
|
|
||||||
|
|
||||||
foreach ($invoice->transactions as $item) {
|
|
||||||
$default_amount = $item->amount;
|
|
||||||
|
|
||||||
if ($invoice->currency_code == $item->currency_code) {
|
|
||||||
$amount = (double) $default_amount;
|
|
||||||
} else {
|
|
||||||
$default_amount_model = new Transaction();
|
|
||||||
$default_amount_model->default_currency_code = $invoice->currency_code;
|
|
||||||
$default_amount_model->amount = $default_amount;
|
|
||||||
$default_amount_model->currency_code = $item->currency_code;
|
|
||||||
$default_amount_model->currency_rate = $_currencies[$item->currency_code];
|
|
||||||
|
|
||||||
$default_amount = (double) $default_amount_model->getDivideConvertedAmount();
|
|
||||||
|
|
||||||
$convert_amount_model = new Transaction();
|
|
||||||
$convert_amount_model->default_currency_code = $item->currency_code;
|
|
||||||
$convert_amount_model->amount = $default_amount;
|
|
||||||
$convert_amount_model->currency_code = $invoice->currency_code;
|
|
||||||
$convert_amount_model->currency_rate = $_currencies[$invoice->currency_code];
|
|
||||||
|
|
||||||
$amount = (double) $convert_amount_model->getAmountConvertedFromCustomDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
$paid += $amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $paid;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,18 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Models\Purchase;
|
namespace App\Models\Purchase;
|
||||||
|
|
||||||
use App\Abstracts\Model;
|
use App\Abstracts\DocumentModel;
|
||||||
use App\Models\Banking\Transaction;
|
use App\Traits\Purchases;
|
||||||
use App\Models\Setting\Currency;
|
|
||||||
use App\Traits\Currencies;
|
|
||||||
use App\Traits\DateTime;
|
|
||||||
use App\Traits\Media;
|
|
||||||
use App\Traits\Recurring;
|
|
||||||
use Bkwld\Cloner\Cloneable;
|
|
||||||
|
|
||||||
class Bill extends Model
|
class Bill extends DocumentModel
|
||||||
{
|
{
|
||||||
use Cloneable, Currencies, DateTime, Media, Recurring;
|
use Purchases;
|
||||||
|
|
||||||
protected $table = 'bills';
|
protected $table = 'bills';
|
||||||
|
|
||||||
@ -92,31 +86,11 @@ class Bill extends Model
|
|||||||
return $this->hasMany('App\Models\Banking\Transaction', 'document_id')->where('type', 'expense');
|
return $this->hasMany('App\Models\Banking\Transaction', 'document_id')->where('type', 'expense');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scopeDue($query, $date)
|
|
||||||
{
|
|
||||||
return $query->whereDate('due_at', '=', $date);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function scopeLatest($query)
|
public function scopeLatest($query)
|
||||||
{
|
{
|
||||||
return $query->orderBy('billed_at', 'desc');
|
return $query->orderBy('billed_at', 'desc');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scopeAccrued($query)
|
|
||||||
{
|
|
||||||
return $query->where('status', '<>', 'draft');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function scopePaid($query)
|
|
||||||
{
|
|
||||||
return $query->where('status', '=', 'paid');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function scopeNotPaid($query)
|
|
||||||
{
|
|
||||||
return $query->where('status', '<>', 'paid');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function scopeNumber($query, $number)
|
public function scopeNumber($query, $number)
|
||||||
{
|
{
|
||||||
return $query->where('bill_number', '=', $number);
|
return $query->where('bill_number', '=', $number);
|
||||||
@ -126,158 +100,4 @@ class Bill extends Model
|
|||||||
{
|
{
|
||||||
$this->status = 'draft';
|
$this->status = 'draft';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert amount to double.
|
|
||||||
*
|
|
||||||
* @param string $value
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setAmountAttribute($value)
|
|
||||||
{
|
|
||||||
$this->attributes['amount'] = (double) $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert currency rate to double.
|
|
||||||
*
|
|
||||||
* @param string $value
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setCurrencyRateAttribute($value)
|
|
||||||
{
|
|
||||||
$this->attributes['currency_rate'] = (double) $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current balance.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAttachmentAttribute($value)
|
|
||||||
{
|
|
||||||
if (!empty($value) && !$this->hasMedia('attachment')) {
|
|
||||||
return $value;
|
|
||||||
} elseif (!$this->hasMedia('attachment')) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->getMedia('attachment')->last();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the discount percentage.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getDiscountAttribute()
|
|
||||||
{
|
|
||||||
$percent = 0;
|
|
||||||
|
|
||||||
$discount = $this->totals()->where('code', 'discount')->value('amount');
|
|
||||||
|
|
||||||
if ($discount) {
|
|
||||||
$sub_total = $this->totals()->where('code', 'sub_total')->value('amount');
|
|
||||||
|
|
||||||
$percent = number_format((($discount * 100) / $sub_total), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $percent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the amount without tax.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAmountWithoutTaxAttribute()
|
|
||||||
{
|
|
||||||
$amount = $this->amount;
|
|
||||||
|
|
||||||
$this->totals()->where('code', 'tax')->each(function ($tax) use(&$amount) {
|
|
||||||
$amount -= $tax->amount;
|
|
||||||
});
|
|
||||||
|
|
||||||
return $amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the paid amount.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getPaidAttribute()
|
|
||||||
{
|
|
||||||
if (empty($this->amount)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$paid = 0;
|
|
||||||
$reconciled = $reconciled_amount = 0;
|
|
||||||
|
|
||||||
if ($this->transactions->count()) {
|
|
||||||
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
|
|
||||||
|
|
||||||
foreach ($this->transactions as $item) {
|
|
||||||
if ($this->currency_code == $item->currency_code) {
|
|
||||||
$amount = (double) $item->amount;
|
|
||||||
} else {
|
|
||||||
$default_model = new Transaction();
|
|
||||||
$default_model->default_currency_code = $this->currency_code;
|
|
||||||
$default_model->amount = $item->amount;
|
|
||||||
$default_model->currency_code = $item->currency_code;
|
|
||||||
$default_model->currency_rate = $currencies[$item->currency_code];
|
|
||||||
|
|
||||||
$default_amount = (double) $default_model->getDivideConvertedAmount();
|
|
||||||
|
|
||||||
$convert_model = new Transaction();
|
|
||||||
$convert_model->default_currency_code = $item->currency_code;
|
|
||||||
$convert_model->amount = $default_amount;
|
|
||||||
$convert_model->currency_code = $this->currency_code;
|
|
||||||
$convert_model->currency_rate = $currencies[$this->currency_code];
|
|
||||||
|
|
||||||
$amount = (double) $convert_model->getAmountConvertedFromCustomDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
$paid += $amount;
|
|
||||||
|
|
||||||
if ($item->reconciled) {
|
|
||||||
$reconciled_amount = +$amount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->amount == $reconciled_amount) {
|
|
||||||
$reconciled = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->setAttribute('reconciled', $reconciled);
|
|
||||||
|
|
||||||
return $paid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the status label.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getStatusLabelAttribute()
|
|
||||||
{
|
|
||||||
switch ($this->status) {
|
|
||||||
case 'paid':
|
|
||||||
$label = 'success';
|
|
||||||
break;
|
|
||||||
case 'partial':
|
|
||||||
$label = 'info';
|
|
||||||
break;
|
|
||||||
case 'received':
|
|
||||||
$label = 'danger';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
$label = 'primary';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $label;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,19 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Models\Sale;
|
namespace App\Models\Sale;
|
||||||
|
|
||||||
use App\Abstracts\Model;
|
use App\Abstracts\DocumentModel;
|
||||||
use App\Models\Banking\Transaction;
|
|
||||||
use App\Models\Setting\Currency;
|
|
||||||
use App\Traits\Currencies;
|
|
||||||
use App\Traits\DateTime;
|
|
||||||
use App\Traits\Media;
|
|
||||||
use App\Traits\Recurring;
|
|
||||||
use App\Traits\Sales;
|
use App\Traits\Sales;
|
||||||
use Bkwld\Cloner\Cloneable;
|
|
||||||
|
|
||||||
class Invoice extends Model
|
class Invoice extends DocumentModel
|
||||||
{
|
{
|
||||||
use Cloneable, Currencies, DateTime, Media, Recurring, Sales;
|
use Sales;
|
||||||
|
|
||||||
protected $table = 'invoices';
|
protected $table = 'invoices';
|
||||||
|
|
||||||
@ -100,31 +93,11 @@ class Invoice extends Model
|
|||||||
return $this->hasMany('App\Models\Banking\Transaction', 'document_id')->where('type', 'income');
|
return $this->hasMany('App\Models\Banking\Transaction', 'document_id')->where('type', 'income');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scopeDue($query, $date)
|
|
||||||
{
|
|
||||||
return $query->whereDate('due_at', '=', $date);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function scopeLatest($query)
|
public function scopeLatest($query)
|
||||||
{
|
{
|
||||||
return $query->orderBy('invoiced_at', 'desc');
|
return $query->orderBy('invoiced_at', 'desc');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scopeAccrued($query)
|
|
||||||
{
|
|
||||||
return $query->where('status', '<>', 'draft');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function scopePaid($query)
|
|
||||||
{
|
|
||||||
return $query->where('status', '=', 'paid');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function scopeNotPaid($query)
|
|
||||||
{
|
|
||||||
return $query->where('status', '<>', 'paid');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function scopeNumber($query, $number)
|
public function scopeNumber($query, $number)
|
||||||
{
|
{
|
||||||
return $query->where('invoice_number', '=', $number);
|
return $query->where('invoice_number', '=', $number);
|
||||||
@ -135,161 +108,4 @@ class Invoice extends Model
|
|||||||
$this->status = 'draft';
|
$this->status = 'draft';
|
||||||
$this->invoice_number = $this->getNextInvoiceNumber();
|
$this->invoice_number = $this->getNextInvoiceNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert amount to double.
|
|
||||||
*
|
|
||||||
* @param string $value
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setAmountAttribute($value)
|
|
||||||
{
|
|
||||||
$this->attributes['amount'] = (double) $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert currency rate to double.
|
|
||||||
*
|
|
||||||
* @param string $value
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function setCurrencyRateAttribute($value)
|
|
||||||
{
|
|
||||||
$this->attributes['currency_rate'] = (double) $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current balance.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAttachmentAttribute($value)
|
|
||||||
{
|
|
||||||
if (!empty($value) && !$this->hasMedia('attachment')) {
|
|
||||||
return $value;
|
|
||||||
} elseif (!$this->hasMedia('attachment')) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->getMedia('attachment')->last();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the discount percentage.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getDiscountAttribute()
|
|
||||||
{
|
|
||||||
$percent = 0;
|
|
||||||
|
|
||||||
$discount = $this->totals()->where('code', 'discount')->value('amount');
|
|
||||||
|
|
||||||
if ($discount) {
|
|
||||||
$sub_total = $this->totals()->where('code', 'sub_total')->value('amount');
|
|
||||||
|
|
||||||
$percent = number_format((($discount * 100) / $sub_total), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $percent;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the amount without tax.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getAmountWithoutTaxAttribute()
|
|
||||||
{
|
|
||||||
$amount = $this->amount;
|
|
||||||
|
|
||||||
$this->totals()->where('code', 'tax')->each(function ($tax) use(&$amount) {
|
|
||||||
$amount -= $tax->amount;
|
|
||||||
});
|
|
||||||
|
|
||||||
return $amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the paid amount.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getPaidAttribute()
|
|
||||||
{
|
|
||||||
if (empty($this->amount)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$paid = 0;
|
|
||||||
$reconciled = $reconciled_amount = 0;
|
|
||||||
|
|
||||||
if ($this->transactions->count()) {
|
|
||||||
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
|
|
||||||
|
|
||||||
foreach ($this->transactions as $item) {
|
|
||||||
if ($this->currency_code == $item->currency_code) {
|
|
||||||
$amount = (double) $item->amount;
|
|
||||||
} else {
|
|
||||||
$default_model = new Transaction();
|
|
||||||
$default_model->default_currency_code = $this->currency_code;
|
|
||||||
$default_model->amount = $item->amount;
|
|
||||||
$default_model->currency_code = $item->currency_code;
|
|
||||||
$default_model->currency_rate = $currencies[$item->currency_code];
|
|
||||||
|
|
||||||
$default_amount = (double) $default_model->getDivideConvertedAmount();
|
|
||||||
|
|
||||||
$convert_model = new Transaction();
|
|
||||||
$convert_model->default_currency_code = $item->currency_code;
|
|
||||||
$convert_model->amount = $default_amount;
|
|
||||||
$convert_model->currency_code = $this->currency_code;
|
|
||||||
$convert_model->currency_rate = $currencies[$this->currency_code];
|
|
||||||
|
|
||||||
$amount = (double) $convert_model->getAmountConvertedFromCustomDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
$paid += $amount;
|
|
||||||
|
|
||||||
if ($item->reconciled) {
|
|
||||||
$reconciled_amount = +$amount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->amount == $reconciled_amount) {
|
|
||||||
$reconciled = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->setAttribute('reconciled', $reconciled);
|
|
||||||
|
|
||||||
return $paid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the status label.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getStatusLabelAttribute()
|
|
||||||
{
|
|
||||||
switch ($this->status) {
|
|
||||||
case 'paid':
|
|
||||||
$label = 'success';
|
|
||||||
break;
|
|
||||||
case 'partial':
|
|
||||||
$label = 'info';
|
|
||||||
break;
|
|
||||||
case 'sent':
|
|
||||||
$label = 'danger';
|
|
||||||
break;
|
|
||||||
case 'viewed':
|
|
||||||
$label = 'warning';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
$label = 'primary';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $label;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user