refs #749 Invoice payment fixed.
This commit is contained in:
parent
d05caa7152
commit
0939d60615
@ -307,6 +307,15 @@ class Bills extends Controller
|
||||
$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();
|
||||
|
@ -488,6 +488,7 @@ class Invoices extends Controller
|
||||
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;
|
||||
@ -497,16 +498,28 @@ class Invoices extends Controller
|
||||
|
||||
$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();
|
||||
|
||||
$request_invoice->amount = (float) $request['amount'];
|
||||
$request_invoice->currency_code = $currency->code;
|
||||
$request_invoice->currency_rate = $currency->rate;
|
||||
$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']];
|
||||
|
||||
$amount = $request_invoice->getConvertedAmount();
|
||||
$default_amount = (double) $default_amount_model->getDivideConvertedAmount();
|
||||
|
||||
$convert_amount = new InvoicePayment();
|
||||
|
||||
$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()) {
|
||||
@ -523,15 +536,41 @@ class Invoices extends Controller
|
||||
$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';
|
||||
|
@ -11,6 +11,7 @@ use App\Models\Expense\BillHistory;
|
||||
use App\Models\Setting\Currency;
|
||||
use App\Utilities\Modules;
|
||||
use App\Traits\Uploads;
|
||||
use App\Jobs\Expense\CreateBillPayment;
|
||||
|
||||
class BillPayments extends Controller
|
||||
{
|
||||
@ -166,20 +167,7 @@ class BillPayments extends Controller
|
||||
|
||||
$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')) {
|
||||
@ -188,15 +176,6 @@ class BillPayments extends Controller
|
||||
$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([
|
||||
|
@ -11,6 +11,7 @@ use App\Models\Income\InvoiceHistory;
|
||||
use App\Models\Setting\Currency;
|
||||
use App\Utilities\Modules;
|
||||
use App\Traits\Uploads;
|
||||
use App\Jobs\Income\CreateInvoicePayment;
|
||||
|
||||
class InvoicePayments extends Controller
|
||||
{
|
||||
@ -130,7 +131,7 @@ class InvoicePayments extends Controller
|
||||
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];
|
||||
@ -166,20 +167,7 @@ class InvoicePayments extends Controller
|
||||
|
||||
$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')) {
|
||||
@ -188,15 +176,6 @@ class InvoicePayments extends Controller
|
||||
$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([
|
||||
|
Loading…
x
Reference in New Issue
Block a user