first commit
This commit is contained in:
136
app/Models/Banking/Account.php
Normal file
136
app/Models/Banking/Account.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Banking;
|
||||
|
||||
use App\Models\Model;
|
||||
use Sofa\Eloquence\Eloquence;
|
||||
|
||||
class Account extends Model
|
||||
{
|
||||
use Eloquence;
|
||||
|
||||
protected $table = 'accounts';
|
||||
|
||||
/**
|
||||
* The accessors to append to the model's array form.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $appends = ['balance'];
|
||||
|
||||
/**
|
||||
* Attributes that should be mass-assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'name', 'number', 'currency_code', 'opening_balance', 'bank_name', 'bank_phone', 'bank_address', 'enabled'];
|
||||
|
||||
/**
|
||||
* Sortable columns.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $sortable = ['name', 'number', 'opening_balance', 'enabled'];
|
||||
|
||||
/**
|
||||
* Searchable rules.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $searchableColumns = [
|
||||
'name' => 10,
|
||||
'number' => 10,
|
||||
'bank_name' => 10,
|
||||
'bank_phone' => 5,
|
||||
'bank_address' => 2,
|
||||
];
|
||||
|
||||
public function currency()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
|
||||
}
|
||||
|
||||
public function invoice_payments()
|
||||
{
|
||||
return $this->hasMany('App\Models\Income\InvoicePayment');
|
||||
}
|
||||
|
||||
public function revenues()
|
||||
{
|
||||
return $this->hasMany('App\Models\Income\Revenue');
|
||||
}
|
||||
|
||||
public function bill_payments()
|
||||
{
|
||||
return $this->hasMany('App\Models\Expense\BillPayment');
|
||||
}
|
||||
|
||||
public function payments()
|
||||
{
|
||||
return $this->hasMany('App\Models\Expense\Payment');
|
||||
}
|
||||
|
||||
public function canDelete()
|
||||
{
|
||||
$error = false;
|
||||
|
||||
$bill_payments = $this->bill_payments();
|
||||
if ($bill_payments->count()) {
|
||||
$error['bills'] = $bill_payments->count();
|
||||
}
|
||||
|
||||
$payments = $this->payments();
|
||||
if ($payments->count()) {
|
||||
$error['payments'] = $payments->count();
|
||||
}
|
||||
|
||||
$invoice_payments = $this->invoice_payments();
|
||||
if ($invoice_payments->count()) {
|
||||
$error['invoices'] = $invoice_payments->count();
|
||||
}
|
||||
|
||||
$revenues = $this->revenues();
|
||||
if ($revenues->count()) {
|
||||
$error['revenues'] = $revenues->count();
|
||||
}
|
||||
|
||||
if ($error) {
|
||||
return $error;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current balance.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBalanceAttribute()
|
||||
{
|
||||
// Opening Balance
|
||||
$total = $this->opening_balance;
|
||||
|
||||
// Sum invoices
|
||||
foreach ($this->invoice_payments as $item) {
|
||||
$total += $item->amount;
|
||||
}
|
||||
|
||||
// Sum revenues
|
||||
foreach ($this->revenues as $item) {
|
||||
$total += $item->amount;
|
||||
}
|
||||
|
||||
// Subtract bills
|
||||
foreach ($this->bill_payments as $item) {
|
||||
$total -= $item->amount;
|
||||
}
|
||||
|
||||
// Subtract payments
|
||||
foreach ($this->payments as $item) {
|
||||
$total -= $item->amount;
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
}
|
||||
92
app/Models/Banking/Transaction.php
Normal file
92
app/Models/Banking/Transaction.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Banking;
|
||||
|
||||
use App\Models\Expense\Bill;
|
||||
use App\Models\Expense\Payment;
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Models\Income\Revenue;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Transaction extends Model
|
||||
{
|
||||
public static function getUserTransactions($user_id, $type)
|
||||
{
|
||||
$transactions = array();
|
||||
|
||||
switch ($type) {
|
||||
case 'payments':
|
||||
$bills = Bill::where('vendor_id', $user_id)->get();
|
||||
|
||||
foreach ($bills as $bill) {
|
||||
$bill_payments = $bill->payments;
|
||||
|
||||
if ($bill_payments) {
|
||||
foreach ($bill_payments as $bill_payment) {
|
||||
$transactions[] = (object) [
|
||||
'date' => $bill_payment->paid_at,
|
||||
'account' => $bill_payment->account->name,
|
||||
'type' => trans('invoices.status.partial'),
|
||||
'category' => trans_choice('general.invoices', 1),
|
||||
'description' => $bill_payment->description,
|
||||
'amount' => $bill_payment->amount,
|
||||
'currency_code' => $bill_payment->currency_code,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$payments = Payment::where('vendor_id', $user_id)->get();
|
||||
|
||||
foreach ($payments as $payment) {
|
||||
$transactions[] = (object) [
|
||||
'date' => $payment->paid_at,
|
||||
'account' => $payment->account->name,
|
||||
'type' => 'Expense',
|
||||
'category' => $payment->category->name,
|
||||
'description' => $payment->description,
|
||||
'amount' => $payment->amount,
|
||||
'currency_code' => $payment->currency_code,
|
||||
];
|
||||
}
|
||||
break;
|
||||
case 'revenues':
|
||||
$invoices = Invoice::where('customer_id', $user_id)->get();
|
||||
|
||||
foreach ($invoices as $invoice) {
|
||||
$invoice_payments = $invoice->payments;
|
||||
|
||||
if ($invoice_payments) {
|
||||
foreach ($invoice_payments as $invoice_payment) {
|
||||
$transactions[] = (object) [
|
||||
'date' => $invoice_payment->paid_at,
|
||||
'account' => $invoice_payment->account->name,
|
||||
'type' => trans('invoices.status.partial'),
|
||||
'category' => trans_choice('general.invoices', 1),
|
||||
'description' => $invoice_payment->description,
|
||||
'amount' => $invoice_payment->amount,
|
||||
'currency_code' => $invoice_payment->currency_code,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$revenues = Revenue::where('customer_id', $user_id)->get();
|
||||
|
||||
foreach ($revenues as $revenue) {
|
||||
$transactions[] = (object) [
|
||||
'date' => $revenue->paid_at,
|
||||
'account' => $revenue->account->name,
|
||||
'type' => trans_choice('general.payments', 1),
|
||||
'category' => $revenue->category->name,
|
||||
'description' => $revenue->description,
|
||||
'amount' => $revenue->amount,
|
||||
'currency_code' => $revenue->currency_code,
|
||||
];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $transactions;
|
||||
}
|
||||
}
|
||||
52
app/Models/Banking/Transfer.php
Normal file
52
app/Models/Banking/Transfer.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Banking;
|
||||
|
||||
use App\Models\Model;
|
||||
use App\Traits\Currencies;
|
||||
|
||||
class Transfer extends Model
|
||||
{
|
||||
use Currencies;
|
||||
|
||||
protected $table = 'transfers';
|
||||
|
||||
/**
|
||||
* Attributes that should be mass-assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = ['company_id', 'payment_id', 'revenue_id'];
|
||||
|
||||
/**
|
||||
* Sortable columns.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $sortable = ['payment.paid_at', 'payment.amount', 'payment.name', 'revenue.name'];
|
||||
|
||||
public function payment()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Expense\Payment');
|
||||
}
|
||||
|
||||
public function paymentAccount()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Banking\Account', 'payment.account_id', 'id');
|
||||
}
|
||||
|
||||
public function revenue()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Income\Revenue');
|
||||
}
|
||||
|
||||
public function revenueAccount()
|
||||
{
|
||||
return $this->belongsTo('App\Models\Banking\Account', 'revenue.account_id', 'id');
|
||||
}
|
||||
|
||||
public function getDynamicConvertedAmount($format = false)
|
||||
{
|
||||
return $this->dynamicConvert($this->default_currency_code, $this->amount, $this->currency_code, $this->currency_rate, $format);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user