From ca6788686eda20455357adecb3c2d10b90ac7034 Mon Sep 17 00:00:00 2001 From: cuneytsenturk Date: Fri, 17 Aug 2018 18:17:33 +0300 Subject: [PATCH] refs #451 Bill create and edit item price mask --- app/Http/Controllers/Expenses/Bills.php | 50 +++- app/Http/Controllers/Incomes/Invoices.php | 2 +- app/Http/Controllers/Modals/BillPayments.php | 246 ++++++++++++++++++ .../views/expenses/bills/create.blade.php | 47 +++- resources/views/expenses/bills/edit.blade.php | 76 ++++-- resources/views/expenses/bills/show.blade.php | 166 +----------- .../views/modals/bills/payment.blade.php | 176 +++++++++++++ routes/web.php | 3 +- 8 files changed, 579 insertions(+), 187 deletions(-) create mode 100644 app/Http/Controllers/Modals/BillPayments.php create mode 100644 resources/views/modals/bills/payment.blade.php diff --git a/app/Http/Controllers/Expenses/Bills.php b/app/Http/Controllers/Expenses/Bills.php index 0df02e1b0..c55c70998 100644 --- a/app/Http/Controllers/Expenses/Bills.php +++ b/app/Http/Controllers/Expenses/Bills.php @@ -66,16 +66,35 @@ class Bills extends Controller { $paid = 0; - foreach ($bill->payments as $item) { - $amount = $item->amount; + // Get Bill Payments + if ($bill->payments->count()) { + $_currencies = Currency::enabled()->pluck('rate', 'code')->toArray(); - if ($bill->currency_code != $item->currency_code) { - $item->default_currency_code = $bill->currency_code; + foreach ($bill->payments as $item) { + $default_amount = $item->amount; - $amount = $item->getDynamicConvertedAmount(); + if ($bill->currency_code != $item->currency_code) { + $default_amount_model = new InvoicePayment(); + + $default_amount_model->default_currency_code = $bill->currency_code; + $default_amount_model->amount = $default_amount; + $default_amount_model->currency_code = $item->currency_code; + $default_amount_model->currency_rate = $_currencies[$item->currency_code]; + + $default_amount = (double) $default_amount_model->getDivideConvertedAmount(); + } + + $convert_amount = new InvoicePayment(); + + $convert_amount->default_currency_code = $item->currency_code; + $convert_amount->amount = $default_amount; + $convert_amount->currency_code = $bill->currency_code; + $convert_amount->currency_rate = $_currencies[$bill->currency_code]; + + $amount = (double) $convert_amount->getDynamicConvertedAmount(); + + $paid += $amount; } - - $paid += $amount; } $bill->paid = $paid; @@ -106,13 +125,15 @@ class Bills extends Controller $currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code'); + $currency = Currency::where('code', '=', setting('general.default_currency'))->first(); + $items = Item::enabled()->orderBy('name')->pluck('name', 'id'); $taxes = Tax::enabled()->orderBy('rate')->get()->pluck('title', 'id'); $categories = Category::enabled()->type('expense')->orderBy('name')->pluck('name', 'id'); - return view('expenses.bills.create', compact('vendors', 'currencies', 'items', 'taxes', 'categories')); + return view('expenses.bills.create', compact('vendors', 'currencies', 'currency', 'items', 'taxes', 'categories')); } /** @@ -329,13 +350,15 @@ class Bills extends Controller $currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code'); + $currency = Currency::where('code', '=', $bill->currency_code)->first(); + $items = Item::enabled()->orderBy('name')->pluck('name', 'id'); $taxes = Tax::enabled()->orderBy('rate')->get()->pluck('title', 'id'); $categories = Category::enabled()->type('expense')->orderBy('name')->pluck('name', 'id'); - return view('expenses.bills.edit', compact('bill', 'vendors', 'currencies', 'items', 'taxes', 'categories')); + return view('expenses.bills.edit', compact('bill', 'vendors', 'currencies', 'currency', 'items', 'taxes', 'categories')); } /** @@ -727,11 +750,19 @@ class Bills extends Controller $taxes = Tax::enabled()->orderBy('rate')->get()->pluck('title', 'id'); + $currency = Currency::where('code', '=', $request['currency_code'])->first(); + + // it should be integer for amount mask + $currency->precision = (int) $currency->precision; + $html = view('expenses.bills.item', compact('item_row', 'taxes'))->render(); return response()->json([ 'success' => true, 'error' => false, + 'data' => [ + 'currency' => $currency + ], 'message' => 'null', 'html' => $html, ]); @@ -740,6 +771,7 @@ class Bills extends Controller return response()->json([ 'success' => false, 'error' => true, + 'data' => 'null', 'message' => trans('issue'), 'html' => 'null', ]); diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index 96ef3805c..d8d1d57c3 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -371,7 +371,7 @@ class Invoices extends Controller $currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code'); - $currency = Currency::where('code', '=', setting('general.default_currency'))->first(); + $currency = Currency::where('code', '=', $invoice->currency_code)->first(); $items = Item::enabled()->orderBy('name')->pluck('name', 'id'); diff --git a/app/Http/Controllers/Modals/BillPayments.php b/app/Http/Controllers/Modals/BillPayments.php new file mode 100644 index 000000000..df7b23d82 --- /dev/null +++ b/app/Http/Controllers/Modals/BillPayments.php @@ -0,0 +1,246 @@ +middleware('permission:create-expenses-bills')->only(['create', 'store', 'duplicate', 'import']); + $this->middleware('permission:read-expenses-bills')->only(['index', 'show', 'edit', 'export']); + $this->middleware('permission:update-expenses-bills')->only(['update', 'enable', 'disable']); + $this->middleware('permission:delete-expenses-bills')->only('destroy'); + } + + /** + * Show the form for creating a new resource. + * + * @return Response + */ + public function create(Bill $bill) + { + $accounts = Account::enabled()->orderBy('name')->pluck('name', 'id'); + + $currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray(); + + $currency = Currency::where('code', setting('general.default_currency'))->first(); + + $account_currency_code = Account::where('id', setting('general.default_account'))->pluck('currency_code')->first(); + + $payment_methods = Modules::getPaymentMethods(); + + $bill->paid = $this->getPaid($bill); + + // Get Bill Totals + foreach ($bill->totals as $bill_total) { + $bill->{$bill_total->code} = $bill_total->amount; + } + + $bill->grand_total = $bill->total; + + if (!empty($paid)) { + $bill->grand_total = $bill->total - $paid; + } + + $html = view('modals.bills.payment', compact('bill', 'accounts', 'account_currency_code', 'currencies', 'currency', 'payment_methods'))->render(); + + return response()->json([ + 'success' => true, + 'error' => false, + 'message' => 'null', + 'html' => $html, + ]); + } + + /** + * Store a newly created resource in storage. + * + * @param Request $request + * + * @return Response + */ + public function store(Bill $bill, Request $request) + { + // Get currency object + $currencies = Currency::enabled()->pluck('rate', 'code')->toArray(); + $currency = Currency::where('code', $request['currency_code'])->first(); + + $request['currency_code'] = $currency->code; + $request['currency_rate'] = $currency->rate; + + $total_amount = $bill->amount; + + $default_amount = (double) $request['amount']; + + if ($bill->currency_code != $request['currency_code']) { + $default_amount_model = new BillPayment(); + + $default_amount_model->default_currency_code = $bill->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(); + } + + $convert_amount = new BillPayment(); + + $convert_amount->default_currency_code = $request['currency_code']; + $convert_amount->amount = $default_amount; + $convert_amount->currency_code = $bill->currency_code; + $convert_amount->currency_rate = $currencies[$bill->currency_code]; + + $amount = (double) $convert_amount->getDynamicConvertedAmount(); + + if ($bill->payments()->count()) { + $total_amount -= $this->getPaid($bill); + } + + // For amount cover integer + $multiplier = 1; + + for ($i = 0; $i < $currency->precision; $i++) { + $multiplier *= 10; + } + + $amount_check = $amount * $multiplier; + $total_amount_check = $total_amount * $multiplier; + + if ($amount_check > $total_amount_check) { + $error_amount = $total_amount; + + if ($bill->currency_code != $request['currency_code']) { + $error_amount_model = new BillPayment(); + + $error_amount_model->default_currency_code = $request['currency_code']; + $error_amount_model->amount = $error_amount; + $error_amount_model->currency_code = $bill->currency_code; + $error_amount_model->currency_rate = $currencies[$bill->currency_code]; + + $error_amount = (double) $error_amount_model->getDivideConvertedAmount(); + } + + $convert_amount = new BillPayment(); + + $convert_amount->default_currency_code = $bill->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) { + $bill->bill_status_code = 'paid'; + } else { + $bill->bill_status_code = 'partial'; + } + + $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); + + // Upload attachment + if ($request->file('attachment')) { + $media = $this->getMedia($request->file('attachment'), 'bills'); + + $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([ + 'success' => true, + 'error' => false, + 'data' => $bill_payment, + 'message' => $message, + 'html' => 'null', + ]); + } + + protected function getPaid($bill) + { + $paid = 0; + + // Get Bill Payments + if ($bill->payments->count()) { + $_currencies = Currency::enabled()->pluck('rate', 'code')->toArray(); + + foreach ($bill->payments as $item) { + $default_amount = $item->amount; + + if ($bill->currency_code != $item->currency_code) { + $default_amount_model = new BillPayment(); + + $default_amount_model->default_currency_code = $bill->currency_code; + $default_amount_model->amount = $default_amount; + $default_amount_model->currency_code = $item->currency_code; + $default_amount_model->currency_rate = $_currencies[$item->currency_code]; + + $default_amount = (double) $default_amount_model->getDivideConvertedAmount(); + } + + $convert_amount = new BillPayment(); + + $convert_amount->default_currency_code = $item->currency_code; + $convert_amount->amount = $default_amount; + $convert_amount->currency_code = $bill->currency_code; + $convert_amount->currency_rate = $_currencies[$bill->currency_code]; + + $amount = (double) $convert_amount->getDynamicConvertedAmount(); + + $paid += $amount; + } + } + + return $paid; + } +} diff --git a/resources/views/expenses/bills/create.blade.php b/resources/views/expenses/bills/create.blade.php index b126a0c38..f60fe4506 100644 --- a/resources/views/expenses/bills/create.blade.php +++ b/resources/views/expenses/bills/create.blade.php @@ -139,6 +139,7 @@ {!! Form::close() !!} + @endsection @push('js') @@ -164,7 +165,7 @@ url: '{{ url("expenses/bills/addItem") }}', type: 'GET', dataType: 'JSON', - data: {item_row: item_row}, + data: {item_row: item_row, currency_code : $('#currency_code').val()}, success: function(json) { if (json['success']) { $('#items tbody #addItem').before(json['html']); @@ -179,6 +180,19 @@ } }); + var currency = json['data']['currency']; + + $("#item-price-" + item_row).maskMoney({ + thousands : currency.thousands_separator, + decimal : currency.decimal_mark, + precision : currency.precision, + allowZero : true, + prefix : (currency.symbol_first) ? currency.symbol : '', + suffix : (currency.symbol_first) ? '' : currency.symbol + }); + + $("#item-price-" + item_row).trigger('focusout'); + item_row++; } } @@ -186,6 +200,20 @@ }); $(document).ready(function(){ + $(".input-price").maskMoney({ + thousands : '{{ $currency->thousands_separator }}', + decimal : '{{ $currency->decimal_mark }}', + precision : {{ $currency->precision }}, + allowZero : true, + @if($currency->symbol_first) + prefix : '{{ $currency->symbol }}' + @else + suffix : '{{ $currency->symbol }}' + @endif + }); + + $('.input-price').trigger('focusout'); + //Date picker $('#billed_at').datepicker({ format: 'yyyy-mm-dd', @@ -341,6 +369,23 @@ $('#currency_code').val(data.currency_code); $('#currency_rate').val(data.currency_rate); + $('.input-price').each(function(){ + amount = $(this).maskMoney('unmasked')[0]; + + $(this).maskMoney({ + thousands : data.thousands_separator, + decimal : data.decimal_mark, + precision : data.precision, + allowZero : true, + prefix : (data.symbol_first) ? data.symbol : '', + suffix : (data.symbol_first) ? '' : data.symbol + }); + + $(this).val(amount); + + $(this).trigger('focusout'); + }); + // This event Select2 Stylesheet $('#currency_code').trigger('change'); } diff --git a/resources/views/expenses/bills/edit.blade.php b/resources/views/expenses/bills/edit.blade.php index cf5409f04..142f39eef 100644 --- a/resources/views/expenses/bills/edit.blade.php +++ b/resources/views/expenses/bills/edit.blade.php @@ -161,6 +161,19 @@ } }); + var currency = json['data']['currency']; + + $("#item-price-" + item_row).maskMoney({ + thousands : currency.thousands_separator, + decimal : currency.decimal_mark, + precision : currency.precision, + allowZero : true, + prefix : (currency.symbol_first) ? currency.symbol : '', + suffix : (currency.symbol_first) ? '' : currency.symbol + }); + + $("#item-price-" + item_row).trigger('focusout'); + item_row++; } } @@ -168,6 +181,20 @@ }); $(document).ready(function(){ + $(".input-price").maskMoney({ + thousands : '{{ $currency->thousands_separator }}', + decimal : '{{ $currency->decimal_mark }}', + precision : {{ $currency->precision }}, + allowZero : true, + @if($currency->symbol_first) + prefix : '{{ $currency->symbol }}' + @else + suffix : '{{ $currency->symbol }}' + @endif + }); + + $('.input-price').trigger('focusout'); + totalItem(); //Date picker @@ -216,24 +243,24 @@ }); @if($bill->attachment) - attachment_html = ''; - attachment_html += ' '; - attachment_html += ' '; - attachment_html += ' {{ $bill->attachment->basename }}'; - attachment_html += ' '; - attachment_html += ' '; - attachment_html += ' {!! Form::open(['id' => 'attachment-' . $bill->attachment->id, 'method' => 'DELETE', 'url' => [url('uploads/' . $bill->attachment->id)], 'style' => 'display:inline']) !!}'; - attachment_html += ' '; - attachment_html += ' '; - attachment_html += ' '; - attachment_html += ' {!! Form::close() !!}'; - attachment_html += ''; + attachment_html = ''; + attachment_html += ' '; + attachment_html += ' '; + attachment_html += ' {{ $bill->attachment->basename }}'; + attachment_html += ' '; + attachment_html += ' '; + attachment_html += ' {!! Form::open(['id' => 'attachment-' . $bill->attachment->id, 'method' => 'DELETE', 'url' => [url('uploads/' . $bill->attachment->id)], 'style' => 'display:inline']) !!}'; + attachment_html += ' '; + attachment_html += ' '; + attachment_html += ' '; + attachment_html += ' {!! Form::close() !!}'; + attachment_html += ''; - $('.fancy-file .fake-file').append(attachment_html); + $('.fancy-file .fake-file').append(attachment_html); - $(document).on('click', '#remove-attachment', function (e) { - confirmDelete("#attachment-{!! $bill->attachment->id !!}", "{!! trans('general.attachment') !!}", "{!! trans('general.delete_confirm', ['name' => '' . $bill->attachment->basename . '', 'type' => strtolower(trans('general.attachment'))]) !!}", "{!! trans('general.cancel') !!}", "{!! trans('general.delete') !!}"); - }); + $(document).on('click', '#remove-attachment', function (e) { + confirmDelete("#attachment-{!! $bill->attachment->id !!}", "{!! trans('general.attachment') !!}", "{!! trans('general.delete_confirm', ['name' => '' . $bill->attachment->basename . '', 'type' => strtolower(trans('general.attachment'))]) !!}", "{!! trans('general.cancel') !!}", "{!! trans('general.delete') !!}"); + }); @endif var autocomplete_path = "{{ url('common/items/autocomplete') }}"; @@ -350,6 +377,23 @@ $('#currency_code').val(data.currency_code); $('#currency_rate').val(data.currency_rate); + $('.input-price').each(function(){ + amount = $(this).maskMoney('unmasked')[0]; + + $(this).maskMoney({ + thousands : data.thousands_separator, + decimal : data.decimal_mark, + precision : data.precision, + allowZero : true, + prefix : (data.symbol_first) ? data.symbol : '', + suffix : (data.symbol_first) ? '' : data.symbol + }); + + $(this).val(amount); + + $(this).trigger('focusout'); + }); + // This event Select2 Stylesheet $('#currency_code').trigger('change'); } diff --git a/resources/views/expenses/bills/show.blade.php b/resources/views/expenses/bills/show.blade.php index 5d0e10d41..ac5573d91 100644 --- a/resources/views/expenses/bills/show.blade.php +++ b/resources/views/expenses/bills/show.blade.php @@ -316,110 +316,6 @@ @push('scripts') @endpush diff --git a/resources/views/modals/bills/payment.blade.php b/resources/views/modals/bills/payment.blade.php new file mode 100644 index 000000000..3ae1ec289 --- /dev/null +++ b/resources/views/modals/bills/payment.blade.php @@ -0,0 +1,176 @@ + + + \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 9c0b5ad0c..72beef45a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -79,7 +79,7 @@ Route::group(['middleware' => 'language'], function () { Route::get('bills/{bill}/print', 'Expenses\Bills@printBill'); Route::get('bills/{bill}/pdf', 'Expenses\Bills@pdfBill'); Route::get('bills/{bill}/duplicate', 'Expenses\Bills@duplicate'); - Route::get('bills/addItem', 'Expenses\Bills@addItem')->name('bill.add.item'); + Route::get('bills/addItem', 'Expenses\Bills@addItem')->middleware(['money'])->name('bill.add.item'); Route::post('bills/payment', 'Expenses\Bills@payment')->middleware(['dateformat', 'money'])->name('bill.payment'); Route::delete('bills/payment/{payment}', 'Expenses\Bills@paymentDestroy'); Route::post('bills/import', 'Expenses\Bills@import')->name('bills.import'); @@ -168,6 +168,7 @@ Route::group(['middleware' => 'language'], function () { Route::resource('customers', 'Modals\Customers'); Route::resource('vendors', 'Modals\Vendors'); Route::resource('invoices/{invoice}/payment', 'Modals\InvoicePayments', ['middleware' => ['dateformat', 'money']]); + Route::resource('bills/{bill}/payment', 'Modals\BillPayments', ['middleware' => ['dateformat', 'money']]); }); /* @deprecated */