Skip to content

Commit

Permalink
refs #749 Invoice payment fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Feb 2, 2019
1 parent d05caa7 commit 0939d60
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 57 deletions.
9 changes: 9 additions & 0 deletions app/Http/Controllers/Expenses/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,15 @@ public function markReceived(Bill $bill)
$bill->bill_status_code = 'received';
$bill->save();

// Add bill history
BillHistory::create([
'company_id' => $bill->company_id,
'bill_id' => $bill->id,
'status_code' => 'received',
'notify' => 0,
'description' => trans('bills.mark_recevied'),
]);

flash(trans('bills.messages.received'))->success();

return redirect()->back();
Expand Down
59 changes: 49 additions & 10 deletions app/Http/Controllers/Incomes/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ public function markPaid(Invoice $invoice)
public function payment(PaymentRequest $request)
{
// Get currency object
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
$currency = Currency::where('code', $request['currency_code'])->first();

$request['currency_code'] = $currency->code;
Expand All @@ -497,16 +498,28 @@ public function payment(PaymentRequest $request)

$total_amount = $invoice->amount;

$amount = (double) $request['amount'];
$default_amount = (double) $request['amount'];

if ($request['currency_code'] != $invoice->currency_code) {
$request_invoice = new Invoice();
if ($invoice->currency_code == $request['currency_code']) {
$amount = $default_amount;
} else {
$default_amount_model = new InvoicePayment();

$default_amount_model->default_currency_code = $invoice->currency_code;
$default_amount_model->amount = $default_amount;
$default_amount_model->currency_code = $request['currency_code'];
$default_amount_model->currency_rate = $currencies[$request['currency_code']];

$default_amount = (double) $default_amount_model->getDivideConvertedAmount();

$request_invoice->amount = (float) $request['amount'];
$request_invoice->currency_code = $currency->code;
$request_invoice->currency_rate = $currency->rate;
$convert_amount = new InvoicePayment();

$amount = $request_invoice->getConvertedAmount();
$convert_amount->default_currency_code = $request['currency_code'];
$convert_amount->amount = $default_amount;
$convert_amount->currency_code = $invoice->currency_code;
$convert_amount->currency_rate = $currencies[$invoice->currency_code];

$amount = (double) $convert_amount->getDynamicConvertedAmount();
}

if ($invoice->payments()->count()) {
Expand All @@ -523,15 +536,41 @@ public function payment(PaymentRequest $request)
$amount_check = (int) ($amount * $multiplier);
$total_amount_check = (int) (round($total_amount, $currency->precision) * $multiplier);

if ($amount > $total_amount) {
$message = trans('messages.error.over_payment');
if ($amount_check > $total_amount_check) {
$error_amount = $total_amount;

if ($invoice->currency_code != $request['currency_code']) {
$error_amount_model = new InvoicePayment();

$error_amount_model->default_currency_code = $request['currency_code'];
$error_amount_model->amount = $error_amount;
$error_amount_model->currency_code = $invoice->currency_code;
$error_amount_model->currency_rate = $currencies[$invoice->currency_code];

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

$convert_amount = new InvoicePayment();

$convert_amount->default_currency_code = $invoice->currency_code;
$convert_amount->amount = $error_amount;
$convert_amount->currency_code = $request['currency_code'];
$convert_amount->currency_rate = $currencies[$request['currency_code']];

$error_amount = (double) $convert_amount->getDynamicConvertedAmount();
}

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

return response()->json([
'success' => false,
'error' => true,
'data' => [
'amount' => $error_amount
],
'message' => $message,
'html' => 'null',
]);
} elseif ($amount == $total_amount) {
} elseif ($amount_check == $total_amount_check) {
$invoice->invoice_status_code = 'paid';
} else {
$invoice->invoice_status_code = 'partial';
Expand Down
25 changes: 2 additions & 23 deletions app/Http/Controllers/Modals/BillPayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Models\Setting\Currency;
use App\Utilities\Modules;
use App\Traits\Uploads;
use App\Jobs\Expense\CreateBillPayment;

class BillPayments extends Controller
{
Expand Down Expand Up @@ -166,20 +167,7 @@ public function store(Bill $bill, Request $request)

$bill->save();

$bill_payment_request = [
'company_id' => $request['company_id'],
'bill_id' => $request['bill_id'],
'account_id' => $request['account_id'],
'paid_at' => $request['paid_at'],
'amount' => $request['amount'],
'currency_code' => $request['currency_code'],
'currency_rate' => $request['currency_rate'],
'description' => $request['description'],
'payment_method' => $request['payment_method'],
'reference' => $request['reference']
];

$bill_payment = BillPayment::create($bill_payment_request);
$bill_payment = dispatch(new CreateBillPayment($request, $bill));

// Upload attachment
if ($request->file('attachment')) {
Expand All @@ -188,15 +176,6 @@ public function store(Bill $bill, Request $request)
$bill_payment->attachMedia($media, 'attachment');
}

$request['status_code'] = $bill->bill_status_code;
$request['notify'] = 0;

$desc_amount = money((float) $request['amount'], (string) $request['currency_code'], true)->format();

$request['description'] = $desc_amount . ' ' . trans_choice('general.payments', 1);

BillHistory::create($request->input());

$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);

return response()->json([
Expand Down
27 changes: 3 additions & 24 deletions app/Http/Controllers/Modals/InvoicePayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Models\Setting\Currency;
use App\Utilities\Modules;
use App\Traits\Uploads;
use App\Jobs\Income\CreateInvoicePayment;

class InvoicePayments extends Controller
{
Expand Down Expand Up @@ -130,7 +131,7 @@ public function store(Invoice $invoice, Request $request)
if ($invoice->currency_code != $request['currency_code']) {
$error_amount_model = new InvoicePayment();

$error_amount_model->default_currency_code = $request['currency_code'];
$error_amount_model->default_currency_code = $request['currency_code'];
$error_amount_model->amount = $error_amount;
$error_amount_model->currency_code = $invoice->currency_code;
$error_amount_model->currency_rate = $currencies[$invoice->currency_code];
Expand Down Expand Up @@ -166,20 +167,7 @@ public function store(Invoice $invoice, Request $request)

$invoice->save();

$invoice_payment_request = [
'company_id' => $request['company_id'],
'invoice_id' => $request['invoice_id'],
'account_id' => $request['account_id'],
'paid_at' => $request['paid_at'],
'amount' => $request['amount'],
'currency_code' => $request['currency_code'],
'currency_rate' => $request['currency_rate'],
'description' => $request['description'],
'payment_method' => $request['payment_method'],
'reference' => $request['reference']
];

$invoice_payment = InvoicePayment::create($invoice_payment_request);
$invoice_payment = dispatch(new CreateInvoicePayment($request, $invoice));

// Upload attachment
if ($request->file('attachment')) {
Expand All @@ -188,15 +176,6 @@ public function store(Invoice $invoice, Request $request)
$invoice_payment->attachMedia($media, 'attachment');
}

$request['status_code'] = $invoice->invoice_status_code;
$request['notify'] = 0;

$desc_amount = money((float) $request['amount'], (string) $request['currency_code'], true)->format();

$request['description'] = $desc_amount . ' ' . trans_choice('general.payments', 1);

InvoiceHistory::create($request->input());

$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);

return response()->json([
Expand Down

0 comments on commit 0939d60

Please sign in to comment.