Skip to content

Commit

Permalink
refactored calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Jul 6, 2020
1 parent acacacb commit 630fdb6
Show file tree
Hide file tree
Showing 20 changed files with 550 additions and 872 deletions.
36 changes: 10 additions & 26 deletions app/Abstracts/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,35 +87,19 @@ public function getPaidAttribute()
return false;
}

static $currencies;

$paid = 0;
$reconciled = $reconciled_amount = 0;

if ($this->transactions->count()) {
if (empty($currencies)) {
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
}
$code = $this->currency_code;
$rate = config('money.' . $code . '.rate');
$precision = config('money.' . $code . '.precision');

if ($this->transactions->count()) {
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->getAmountConvertedToDefault();

$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->getAmountConvertedFromDefault();
$amount = $item->amount;

if ($code != $item->currency_code) {
$amount = $this->convertBetween($amount, $item->currency_code, $item->currency_rate, $code, $rate);
}

$paid += $amount;
Expand All @@ -126,13 +110,13 @@ public function getPaidAttribute()
}
}

if ($this->amount == $reconciled_amount) {
if (bccomp(round($this->amount, $precision), round($reconciled_amount, $precision), $precision) === 0) {
$reconciled = 1;
}

$this->setAttribute('reconciled', $reconciled);

return $paid;
return round($paid, $precision);
}
/**
* Get the status label.
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Purchases/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function show(Bill $bill)
$bill->grand_total = money($total, $currency->code)->getAmount();

if (!empty($bill->paid)) {
$bill->grand_total = round($bill->total - $bill->paid, $currency->precision) ;
$bill->grand_total = round($bill->total - $bill->paid, $currency->precision);
}

return view('purchases.bills.show', compact('bill', 'accounts', 'currencies', 'currency', 'account_currency_code', 'vendors', 'categories', 'payment_methods', 'date_format'));
Expand Down
59 changes: 17 additions & 42 deletions app/Jobs/Banking/CreateDocumentTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use App\Jobs\Sale\CreateInvoiceHistory;
use App\Models\Banking\Transaction;
use App\Models\Sale\Invoice;
use App\Models\Setting\Currency;
use App\Traits\Currencies;
use Date;

class CreateDocumentTransaction extends Job
{
use Currencies;

protected $model;

protected $request;
Expand Down Expand Up @@ -64,13 +66,10 @@ protected function prepareRequest()
{
$this->request['company_id'] = session('company_id');
$this->request['currency_code'] = isset($this->request['currency_code']) ? $this->request['currency_code'] : $this->model->currency_code;

$this->currency = Currency::where('code', $this->request['currency_code'])->first();

$this->request['type'] = ($this->model instanceof Invoice) ? 'income' : 'expense';
$this->request['paid_at'] = isset($this->request['paid_at']) ? $this->request['paid_at'] : Date::now()->format('Y-m-d');
$this->request['amount'] = isset($this->request['amount']) ? $this->request['amount'] : ($this->model->amount - $this->model->paid);
$this->request['currency_rate'] = $this->currency->rate;
$this->request['currency_rate'] = config('money.' . $this->request['currency_code'] . '.rate');
$this->request['account_id'] = isset($this->request['account_id']) ? $this->request['account_id'] : setting('default.account');
$this->request['document_id'] = isset($this->request['document_id']) ? $this->request['document_id'] : $this->model->id;
$this->request['contact_id'] = isset($this->request['contact_id']) ? $this->request['contact_id'] : $this->model->contact_id;
Expand All @@ -81,57 +80,33 @@ protected function prepareRequest()

protected function checkAmount()
{
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();

$default_amount = (double) $this->request['amount'];
$code = $this->request['currency_code'];
$rate = $this->request['currency_rate'];
$precision = config('money.' . $code . '.precision');

if ($this->model->currency_code == $this->request['currency_code']) {
$amount = $default_amount;
} else {
$default_amount_model = new Transaction();
$default_amount_model->default_currency_code = $this->model->currency_code;
$default_amount_model->amount = $default_amount;
$default_amount_model->currency_code = $this->request['currency_code'];
$default_amount_model->currency_rate = $currencies[$this->request['currency_code']];

$default_amount = (double) $default_amount_model->getAmountConvertedToDefault();
$amount = $this->request['amount'] = round($this->request['amount'], $precision);

$convert_amount_model = new Transaction();
$convert_amount_model->default_currency_code = $this->request['currency_code'];
$convert_amount_model->amount = $default_amount;
$convert_amount_model->currency_code = $this->model->currency_code;
$convert_amount_model->currency_rate = $currencies[$this->model->currency_code];
if ($this->model->currency_code != $code) {
$converted_amount = $this->convertBetween($amount, $code, $rate, $this->model->currency_code, $this->model->currency_rate);

$amount = (double) $convert_amount_model->getAmountConvertedFromDefault();
$amount = round($converted_amount, $precision);
}

$total_amount = $this->model->amount - $this->model->paid;
$total_amount = round($this->model->amount - $this->model->paid, $precision);
unset($this->model->reconciled);

$compare = bccomp($amount, $total_amount, $this->currency->precision);
$compare = bccomp($amount, $total_amount, $precision);

if ($compare === 1) {
$error_amount = $total_amount;

if ($this->model->currency_code != $this->request['currency_code']) {
$error_amount_model = new Transaction();
$error_amount_model->default_currency_code = $this->request['currency_code'];
$error_amount_model->amount = $error_amount;
$error_amount_model->currency_code = $this->model->currency_code;
$error_amount_model->currency_rate = $currencies[$this->model->currency_code];

$error_amount = (double) $error_amount_model->getAmountConvertedToDefault();

$convert_amount_model = new Transaction();
$convert_amount_model->default_currency_code = $this->model->currency_code;
$convert_amount_model->amount = $error_amount;
$convert_amount_model->currency_code = $this->request['currency_code'];
$convert_amount_model->currency_rate = $currencies[$this->request['currency_code']];
if ($this->model->currency_code != $code) {
$converted_amount = $this->convertBetween($total_amount, $this->model->currency_code, $this->model->currency_rate, $code, $rate);

$error_amount = (double) $convert_amount_model->getAmountConvertedFromDefault();
$error_amount = round($converted_amount, $precision);
}

$message = trans('messages.error.over_payment', ['amount' => money($error_amount, $this->request['currency_code'], true)]);
$message = trans('messages.error.over_payment', ['amount' => money($error_amount, $code, true)]);

throw new \Exception($message);
} else {
Expand Down
40 changes: 11 additions & 29 deletions app/Jobs/Banking/CreateTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
use App\Models\Banking\Transfer;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Traits\Currencies;

class CreateTransfer extends Job
{
use Currencies;

protected $transfer;

protected $request;
Expand All @@ -33,18 +36,19 @@ public function __construct($request)
public function handle()
{
\DB::transaction(function () {
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();

$expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first();
$income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first();

$expense_currency_rate = config('money.' . $expense_currency_code . '.rate');
$income_currency_rate = config('money.' . $income_currency_code . '.rate');

$expense_transaction = Transaction::create([
'company_id' => $this->request['company_id'],
'type' => 'expense',
'account_id' => $this->request->get('from_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $expense_currency_code,
'currency_rate' => $currencies[$expense_currency_code],
'currency_rate' => $expense_currency_rate,
'amount' => $this->request->get('amount'),
'contact_id' => 0,
'description' => $this->request->get('description'),
Expand All @@ -53,33 +57,11 @@ public function handle()
'reference' => $this->request->get('reference'),
]);

$amount = $this->request->get('amount');

// Convert amount if not same currency
if ($expense_currency_code != $income_currency_code) {
$default_currency = setting('default.currency', 'USD');

$default_amount = $this->request->get('amount');

if ($default_currency != $expense_currency_code) {
$default_amount_model = new Transfer();

$default_amount_model->default_currency_code = $default_currency;
$default_amount_model->amount = $this->request->get('amount');
$default_amount_model->currency_code = $expense_currency_code;
$default_amount_model->currency_rate = $currencies[$expense_currency_code];

$default_amount = $default_amount_model->getAmountConvertedToDefault();
}

$transfer_amount = new Transfer();

$transfer_amount->default_currency_code = $expense_currency_code;
$transfer_amount->amount = $default_amount;
$transfer_amount->currency_code = $income_currency_code;
$transfer_amount->currency_rate = $currencies[$income_currency_code];

$amount = $transfer_amount->getAmountConvertedFromDefault();
} else {
$amount = $this->request->get('amount');
$amount = $this->convertBetween($amount, $expense_currency_code, $expense_currency_rate, $income_currency_code, $income_currency_rate);
}

$income_transaction = Transaction::create([
Expand All @@ -88,7 +70,7 @@ public function handle()
'account_id' => $this->request->get('to_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $income_currency_code,
'currency_rate' => $currencies[$income_currency_code],
'currency_rate' => $income_currency_rate,
'amount' => $amount,
'contact_id' => 0,
'description' => $this->request->get('description'),
Expand Down
40 changes: 11 additions & 29 deletions app/Jobs/Banking/UpdateTransfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
use App\Models\Banking\Transfer;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Traits\Currencies;

class UpdateTransfer extends Job
{
use Currencies;

protected $transfer;

protected $request;
Expand All @@ -35,11 +38,12 @@ public function __construct($transfer, $request)
public function handle()
{
\DB::transaction(function () {
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();

$expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first();
$income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first();

$expense_currency_rate = config('money.' . $expense_currency_code . '.rate');
$income_currency_rate = config('money.' . $income_currency_code . '.rate');

$expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id);
$income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id);

Expand All @@ -49,7 +53,7 @@ public function handle()
'account_id' => $this->request->get('from_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $expense_currency_code,
'currency_rate' => $currencies[$expense_currency_code],
'currency_rate' => $expense_currency_rate,
'amount' => $this->request->get('amount'),
'contact_id' => 0,
'description' => $this->request->get('description'),
Expand All @@ -58,33 +62,11 @@ public function handle()
'reference' => $this->request->get('reference'),
]);

$amount = $this->request->get('amount');

// Convert amount if not same currency
if ($expense_currency_code != $income_currency_code) {
$default_currency = setting('default.currency', 'USD');

$default_amount = $this->request->get('amount');

if ($default_currency != $expense_currency_code) {
$default_amount_model = new Transfer();

$default_amount_model->default_currency_code = $default_currency;
$default_amount_model->amount = $this->request->get('amount');
$default_amount_model->currency_code = $expense_currency_code;
$default_amount_model->currency_rate = $currencies[$expense_currency_code];

$default_amount = $default_amount_model->getAmountConvertedToDefault();
}

$transfer_amount = new Transfer();

$transfer_amount->default_currency_code = $expense_currency_code;
$transfer_amount->amount = $default_amount;
$transfer_amount->currency_code = $income_currency_code;
$transfer_amount->currency_rate = $currencies[$income_currency_code];

$amount = $transfer_amount->getAmountConvertedFromDefault();
} else {
$amount = $this->request->get('amount');
$amount = $this->convertBetween($amount, $expense_currency_code, $expense_currency_rate, $income_currency_code, $income_currency_rate);
}

$income_transaction->update([
Expand All @@ -93,7 +75,7 @@ public function handle()
'account_id' => $this->request->get('to_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $income_currency_code,
'currency_rate' => $currencies[$income_currency_code],
'currency_rate' => $income_currency_rate,
'amount' => $amount,
'contact_id' => 0,
'description' => $this->request->get('description'),
Expand Down

0 comments on commit 630fdb6

Please sign in to comment.