refactored calculations
This commit is contained in:
		@@ -8,11 +8,13 @@ use App\Jobs\Purchase\CreateBillHistory;
 | 
			
		||||
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;
 | 
			
		||||
@@ -64,13 +66,10 @@ class CreateDocumentTransaction extends Job
 | 
			
		||||
    {
 | 
			
		||||
        $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;
 | 
			
		||||
@@ -81,57 +80,33 @@ class CreateDocumentTransaction extends Job
 | 
			
		||||
 | 
			
		||||
    protected function checkAmount()
 | 
			
		||||
    {
 | 
			
		||||
        $currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
 | 
			
		||||
        $code = $this->request['currency_code'];
 | 
			
		||||
        $rate = $this->request['currency_rate'];
 | 
			
		||||
        $precision = config('money.' . $code . '.precision');
 | 
			
		||||
 | 
			
		||||
        $default_amount = (double) $this->request['amount'];
 | 
			
		||||
        $amount = $this->request['amount'] = round($this->request['amount'], $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']];
 | 
			
		||||
        if ($this->model->currency_code != $code) {
 | 
			
		||||
            $converted_amount = $this->convertBetween($amount, $code, $rate, $this->model->currency_code, $this->model->currency_rate);
 | 
			
		||||
 | 
			
		||||
            $default_amount = (double) $default_amount_model->getAmountConvertedToDefault();
 | 
			
		||||
 | 
			
		||||
            $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];
 | 
			
		||||
 | 
			
		||||
            $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];
 | 
			
		||||
            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) $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']];
 | 
			
		||||
 | 
			
		||||
                $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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -8,9 +8,12 @@ use App\Models\Banking\Transaction;
 | 
			
		||||
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;
 | 
			
		||||
@@ -33,18 +36,19 @@ class CreateTransfer extends Job
 | 
			
		||||
    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'),
 | 
			
		||||
@@ -53,33 +57,11 @@ class CreateTransfer extends Job
 | 
			
		||||
                '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([
 | 
			
		||||
@@ -88,7 +70,7 @@ class CreateTransfer extends Job
 | 
			
		||||
                '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'),
 | 
			
		||||
 
 | 
			
		||||
@@ -8,9 +8,12 @@ use App\Models\Banking\Transaction;
 | 
			
		||||
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;
 | 
			
		||||
@@ -35,11 +38,12 @@ class UpdateTransfer extends Job
 | 
			
		||||
    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);
 | 
			
		||||
 | 
			
		||||
@@ -49,7 +53,7 @@ class UpdateTransfer extends Job
 | 
			
		||||
                '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'),
 | 
			
		||||
@@ -58,33 +62,11 @@ class UpdateTransfer extends Job
 | 
			
		||||
                '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([
 | 
			
		||||
@@ -93,7 +75,7 @@ class UpdateTransfer extends Job
 | 
			
		||||
                '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'),
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user