bank reconciliation

This commit is contained in:
denisdulici
2018-10-27 17:57:40 +03:00
parent 42f6b00485
commit 268a4aa9d5
29 changed files with 1209 additions and 88 deletions

View File

@ -0,0 +1,45 @@
<?php
namespace App\Models\Banking;
use App\Models\Model;
use Sofa\Eloquence\Eloquence;
class Reconciliation extends Model
{
use Eloquence;
protected $table = 'reconciliations';
protected $dates = ['deleted_at', 'started_at', 'ended_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'account_id', 'started_at', 'ended_at', 'closing_balance', 'reconciled'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['created_at', 'account_id', 'started_at', 'ended_at', 'closing_balance', 'reconciled'];
public function account()
{
return $this->belongsTo('App\Models\Banking\Account');
}
/**
* Convert closing balance to double.
*
* @param string $value
* @return void
*/
public function setClosingBalanceAttribute($value)
{
$this->attributes['closing_balance'] = (double) $value;
}
}

View File

@ -3,6 +3,7 @@
namespace App\Models\Expense;
use App\Models\Model;
use App\Models\Setting\Currency;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
@ -22,7 +23,7 @@ class Bill extends Model
*
* @var array
*/
protected $appends = ['attachment', 'discount'];
protected $appends = ['attachment', 'discount', 'paid'];
protected $dates = ['deleted_at', 'billed_at', 'due_at'];
@ -199,4 +200,55 @@ class Bill extends Model
return $percent;
}
/**
* Get the paid amount.
*
* @return string
*/
public function getPaidAttribute()
{
$paid = 0;
$reconciled = $reconciled_amount = 0;
if ($this->payments->count()) {
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
foreach ($this->payments as $item) {
if ($this->currency_code == $item->currency_code) {
$amount = (double) $item->amount;
} else {
$default_model = new BillPayment();
$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 BillPayment();
$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->getDynamicConvertedAmount();
}
$paid += $amount;
if ($item->reconciled) {
$reconciled_amount = +$amount;
}
}
}
if ($this->amount == $reconciled_amount) {
$reconciled = 1;
}
$this->setAttribute('reconciled', $reconciled);
return $paid;
}
}

View File

@ -3,6 +3,7 @@
namespace App\Models\Income;
use App\Models\Model;
use App\Models\Setting\Currency;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Incomes;
@ -23,7 +24,7 @@ class Invoice extends Model
*
* @var array
*/
protected $appends = ['attachment', 'discount'];
protected $appends = ['attachment', 'discount', 'paid'];
protected $dates = ['deleted_at', 'invoiced_at', 'due_at'];
@ -56,6 +57,8 @@ class Invoice extends Model
'notes' => 2,
];
protected $reconciled_amount = [];
/**
* Clonable relationships.
*
@ -201,4 +204,55 @@ class Invoice extends Model
return $percent;
}
/**
* Get the paid amount.
*
* @return string
*/
public function getPaidAttribute()
{
$paid = 0;
$reconciled = $reconciled_amount = 0;
if ($this->payments->count()) {
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
foreach ($this->payments as $item) {
if ($this->currency_code == $item->currency_code) {
$amount = (double) $item->amount;
} else {
$default_model = new InvoicePayment();
$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 InvoicePayment();
$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->getDynamicConvertedAmount();
}
$paid += $amount;
if ($item->reconciled) {
$reconciled_amount = +$amount;
}
}
}
if ($this->amount == $reconciled_amount) {
$reconciled = 1;
}
$this->setAttribute('reconciled', $reconciled);
return $paid;
}
}

View File

@ -115,4 +115,16 @@ class Model extends Eloquent
{
return $query->where('enabled', 0);
}
/**
* Scope to only include reconciled models.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $value
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeReconciled($query, $value = 1)
{
return $query->where('reconciled', $value);
}
}