diff --git a/app/Http/Controllers/Incomes/Customers.php b/app/Http/Controllers/Incomes/Customers.php index 427e5bf78..8bb67dc30 100644 --- a/app/Http/Controllers/Incomes/Customers.php +++ b/app/Http/Controllers/Incomes/Customers.php @@ -365,9 +365,16 @@ class Customers extends Controller // Get currency object $currency = Currency::where('code', $currency_code)->first(); + $customer->currency_name = $currency->name; $customer->currency_code = $currency_code; $customer->currency_rate = $currency->rate; + $customer->thousands_separator = $currency->thousands_separator; + $customer->decimal_mark = $currency->decimal_mark; + $customer->precision = (int) $currency->precision; + $customer->symbol_first = $currency->symbol_first; + $customer->symbol = $currency->symbol; + return response()->json($customer); } diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index 02a983f3b..f280cf1cc 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -69,16 +69,35 @@ class Invoices extends Controller { $paid = 0; - foreach ($invoice->payments as $item) { - $amount = $item->amount; + // Get Invoice Payments + if ($invoice->payments->count()) { + $_currencies = Currency::enabled()->pluck('rate', 'code')->toArray(); - if ($invoice->currency_code != $item->currency_code) { - $item->default_currency_code = $invoice->currency_code; + foreach ($invoice->payments as $item) { + $default_amount = $item->amount; - $amount = $item->getDynamicConvertedAmount(); + if ($invoice->currency_code != $item->currency_code) { + $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 = $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 = $invoice->currency_code; + $convert_amount->currency_rate = $_currencies[$invoice->currency_code]; + + $amount = (double) $convert_amount->getDynamicConvertedAmount(); + + $paid += $amount; } - - $paid += $amount; } $invoice->paid = $paid; @@ -109,6 +128,8 @@ class Invoices 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'); @@ -117,7 +138,7 @@ class Invoices extends Controller $number = $this->getNextInvoiceNumber(); - return view('incomes.invoices.create', compact('customers', 'currencies', 'items', 'taxes', 'categories', 'number')); + return view('incomes.invoices.create', compact('customers', 'currencies', 'currency', 'items', 'taxes', 'categories', 'number')); } /** @@ -864,11 +885,19 @@ class Invoices 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('incomes.invoices.item', compact('item_row', 'taxes'))->render(); return response()->json([ 'success' => true, 'error' => false, + 'data' => [ + 'currency' => $currency + ], 'message' => 'null', 'html' => $html, ]); @@ -877,6 +906,7 @@ class Invoices extends Controller return response()->json([ 'success' => false, 'error' => true, + 'data' => 'null', 'message' => trans('issue'), 'html' => 'null', ]); diff --git a/app/Http/Controllers/Modals/InvoicePayments.php b/app/Http/Controllers/Modals/InvoicePayments.php new file mode 100644 index 000000000..b5f6eac45 --- /dev/null +++ b/app/Http/Controllers/Modals/InvoicePayments.php @@ -0,0 +1,246 @@ +middleware('permission:create-incomes-invoices')->only(['create', 'store', 'duplicate', 'import']); + $this->middleware('permission:read-incomes-invoices')->only(['index', 'show', 'edit', 'export']); + $this->middleware('permission:update-incomes-invoices')->only(['update', 'enable', 'disable']); + $this->middleware('permission:delete-incomes-invoices')->only('destroy'); + } + + /** + * Show the form for creating a new resource. + * + * @return Response + */ + public function create(Invoice $invoice) + { + $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(); + + $invoice->paid = $this->getPaid($invoice); + + // Get Invoice Totals + foreach ($invoice->totals as $invoice_total) { + $invoice->{$invoice_total->code} = $invoice_total->amount; + } + + $invoice->grand_total = $invoice->total; + + if (!empty($paid)) { + $invoice->grand_total = $invoice->total - $paid; + } + + $html = view('modals.invoices.payment', compact('invoice', '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(Invoice $invoice, 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 = $invoice->amount; + + $default_amount = (double) $request['amount']; + + if ($invoice->currency_code != $request['currency_code']) { + $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(); + } + + $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()) { + $total_amount -= $this->getPaid($invoice); + } + + // 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 ($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) { + $invoice->invoice_status_code = 'paid'; + } else { + $invoice->invoice_status_code = 'partial'; + } + + $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); + + // Upload attachment + if ($request->file('attachment')) { + $media = $this->getMedia($request->file('attachment'), 'invoices'); + + $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([ + 'success' => true, + 'error' => false, + 'data' => $invoice_payment, + 'message' => $message, + 'html' => 'null', + ]); + } + + protected function getPaid($invoice) + { + $paid = 0; + + // Get Invoice Payments + if ($invoice->payments->count()) { + $_currencies = Currency::enabled()->pluck('rate', 'code')->toArray(); + + foreach ($invoice->payments as $item) { + $default_amount = $item->amount; + + if ($invoice->currency_code != $item->currency_code) { + $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 = $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 = $invoice->currency_code; + $convert_amount->currency_rate = $_currencies[$invoice->currency_code]; + + $amount = (double) $convert_amount->getDynamicConvertedAmount(); + + $paid += $amount; + } + } + + return $paid; + } +} diff --git a/app/Http/Middleware/Money.php b/app/Http/Middleware/Money.php index ccd787e93..67f0bcef4 100644 --- a/app/Http/Middleware/Money.php +++ b/app/Http/Middleware/Money.php @@ -45,6 +45,7 @@ class Money } } + /* check item price use money if (isset($sale_price)) { $sale_price = money($sale_price, $currency_code)->getAmount(); @@ -56,6 +57,7 @@ class Money $request->request->set('purchase_price', $purchase_price); } + */ } return $next($request); diff --git a/app/Models/Income/InvoicePayment.php b/app/Models/Income/InvoicePayment.php index fe726b632..02e724a2b 100644 --- a/app/Models/Income/InvoicePayment.php +++ b/app/Models/Income/InvoicePayment.php @@ -101,4 +101,9 @@ class InvoicePayment extends Model return $this->getMedia('attachment')->last(); } + + public function getDivideConvertedAmount($format = false) + { + return $this->divide($this->amount, $this->currency_code, $this->currency_rate, $format); + } } diff --git a/resources/lang/en-GB/messages.php b/resources/lang/en-GB/messages.php index 4373f6cff..2ed96991c 100644 --- a/resources/lang/en-GB/messages.php +++ b/resources/lang/en-GB/messages.php @@ -12,7 +12,7 @@ return [ 'disabled' => ':type disabled!', ], 'error' => [ - 'over_payment' => 'Error: Payment not added! Amount passes the total.', + 'over_payment' => 'Error: Payment not added! Amount passes the total. You don\'t should max add amount: :amount', 'not_user_company' => 'Error: You are not allowed to manage this company!', 'customer' => 'Error: User not created! :name already uses this email address.', 'no_file' => 'Error: No file selected!', diff --git a/resources/views/common/items/create.blade.php b/resources/views/common/items/create.blade.php index 95cb07c9e..a6ada7179 100644 --- a/resources/views/common/items/create.blade.php +++ b/resources/views/common/items/create.blade.php @@ -3,133 +3,133 @@ @section('title', trans('general.title.new', ['type' => trans_choice('general.items', 1)])) @section('content') - -
- {!! Form::open(['route' => 'items.store', 'files' => true, 'role' => 'form']) !!} + +
+ {!! Form::open(['route' => 'items.store', 'files' => true, 'role' => 'form']) !!} -
- {{ Form::textGroup('name', trans('general.name'), 'id-card-o') }} +
+ {{ Form::textGroup('name', trans('general.name'), 'id-card-o') }} - {{ Form::textGroup('sku', trans('items.sku'), 'key') }} + {{ Form::textGroup('sku', trans('items.sku'), 'key') }} - {{ Form::textareaGroup('description', trans('general.description')) }} + {{ Form::textareaGroup('description', trans('general.description')) }} - {{ Form::textGroup('sale_price', trans('items.sales_price'), 'money') }} + {{ Form::textGroup('sale_price', trans('items.sales_price'), 'money') }} - {{ Form::textGroup('purchase_price', trans('items.purchase_price'), 'money') }} + {{ Form::textGroup('purchase_price', trans('items.purchase_price'), 'money') }} - {{ Form::textGroup('quantity', trans_choice('items.quantities', 1), 'cubes', ['required' => 'required'], '1') }} + {{ Form::textGroup('quantity', trans_choice('items.quantities', 1), 'cubes', ['required' => 'required'], '1') }} - {{ Form::selectGroup('tax_id', trans_choice('general.taxes', 1), 'percent', $taxes, setting('general.default_tax'), []) }} + {{ Form::selectGroup('tax_id', trans_choice('general.taxes', 1), 'percent', $taxes, setting('general.default_tax'), []) }} - @stack('category_id_input_start') -
- {!! Form::label('category_id', trans_choice('general.categories', 1), ['class' => 'control-label']) !!} -
-
- {!! Form::select('category_id', $categories, null, array_merge(['class' => 'form-control', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.categories', 1)])])) !!} -
- -
+ @stack('category_id_input_start') +
+ {!! Form::label('category_id', trans_choice('general.categories', 1), ['class' => 'control-label']) !!} +
+
+ {!! Form::select('category_id', $categories, null, array_merge(['class' => 'form-control', 'placeholder' => trans('general.form.select.field', ['field' => trans_choice('general.categories', 1)])])) !!} +
+
- {!! $errors->first('category_id', '

:message

') !!}
- @stack('category_id_input_end') - - {{ Form::fileGroup('picture', trans_choice('general.pictures', 1)) }} - - {{ Form::radioGroup('enabled', trans('general.enabled')) }} + {!! $errors->first('category_id', '

:message

') !!}
- + @stack('category_id_input_end') - - + {{ Form::fileGroup('picture', trans_choice('general.pictures', 1)) }} - {!! Form::close() !!} + {{ Form::radioGroup('enabled', trans('general.enabled')) }}
+ + + + + + {!! Form::close() !!} +
@endsection @push('js') - - + + @endpush @push('css') - - + + @endpush @push('scripts') - + + $("#sale_price").focusout(); + $("#purchase_price").focusout();*/ + + $('#enabled_1').trigger('click'); + + $('#name').focus(); + + $("#tax_id").select2({ + placeholder: { + id: '-1', // the value of the option + text: "{{ trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)]) }}" + } + }); + + $("#category_id").select2({ + placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.categories', 1)]) }}" + }); + + $('#picture').fancyfile({ + text : '{{ trans('general.form.select.file') }}', + style : 'btn-default', + placeholder : '{{ trans('general.form.no_file_selected') }}' + }); + }); + + $(document).on('click', '#button-category', function (e) { + $('#modal-create-category').remove(); + + $.ajax({ + url: '{{ url("modals/categories/create") }}', + type: 'GET', + dataType: 'JSON', + data: {type: 'item'}, + success: function(json) { + if (json['success']) { + $('body').append(json['html']); + } + } + }); + }); + @endpush diff --git a/resources/views/common/items/edit.blade.php b/resources/views/common/items/edit.blade.php index 70ef3a981..155e374b8 100644 --- a/resources/views/common/items/edit.blade.php +++ b/resources/views/common/items/edit.blade.php @@ -59,7 +59,7 @@ var text_no = '{{ trans('general.no') }}'; $(document).ready(function(){ - $("#sale_price").maskMoney({ + /*$("#sale_price").maskMoney({ thousands : '{{ $currency->thousands_separator }}', decimal : '{{ $currency->decimal_mark }}', precision : {{ $currency->precision }}, @@ -84,7 +84,7 @@ }); $("#sale_price").focusout(); - $("#purchase_price").focusout(); + $("#purchase_price").focusout();*/ $("#tax_id").select2({ placeholder: { diff --git a/resources/views/incomes/invoices/create.blade.php b/resources/views/incomes/invoices/create.blade.php index ec3d538c6..2b58ecc35 100644 --- a/resources/views/incomes/invoices/create.blade.php +++ b/resources/views/incomes/invoices/create.blade.php @@ -165,7 +165,7 @@ url: '{{ url("incomes/invoices/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']); @@ -180,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++; } } @@ -187,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 $('#invoiced_at').datepicker({ format: 'yyyy-mm-dd', @@ -338,9 +365,27 @@ $('#customer_tax_number').val(data.tax_number); $('#customer_phone').val(data.phone); $('#customer_address').val(data.address); + $('#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/incomes/invoices/item.blade.php b/resources/views/incomes/invoices/item.blade.php index eaaadc1b3..9efe56e6a 100644 --- a/resources/views/incomes/invoices/item.blade.php +++ b/resources/views/incomes/invoices/item.blade.php @@ -24,7 +24,7 @@ @stack('price_td_start') @stack('price_input_start') - + @stack('price_input_end') @stack('price_td_end') diff --git a/resources/views/incomes/invoices/show.blade.php b/resources/views/incomes/invoices/show.blade.php index cb7617c48..d291ca0b8 100644 --- a/resources/views/incomes/invoices/show.blade.php +++ b/resources/views/incomes/invoices/show.blade.php @@ -175,7 +175,7 @@ @permission('update-incomes-invoices')
  • {{ trans('invoices.mark_paid') }}
  • @endpermission - @if(empty($invoice->payments()->count()) || (!empty($invoice->payments()->count()) && $invoice->payments()->paid() != $invoice->amount)) + @if(empty($invoice->payments()->count()) || (!empty($invoice->payments()->count()) && $invoice->paid != $invoice->amount))
  • {{ trans('invoices.add_payment') }}
  • @endif
  • @@ -329,110 +329,6 @@ @push('scripts') @endpush diff --git a/resources/views/modals/invoices/payment.blade.php b/resources/views/modals/invoices/payment.blade.php new file mode 100644 index 000000000..b0542168c --- /dev/null +++ b/resources/views/modals/invoices/payment.blade.php @@ -0,0 +1,176 @@ + + + \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 3650ad85d..497bec472 100644 --- a/routes/web.php +++ b/routes/web.php @@ -53,7 +53,7 @@ Route::group(['middleware' => 'language'], function () { Route::get('invoices/{invoice}/print', 'Incomes\Invoices@printInvoice'); Route::get('invoices/{invoice}/pdf', 'Incomes\Invoices@pdfInvoice'); Route::get('invoices/{invoice}/duplicate', 'Incomes\Invoices@duplicate'); - Route::get('invoices/addItem', 'Incomes\Invoices@addItem')->name('invoice.add.item'); + Route::get('invoices/addItem', 'Incomes\Invoices@addItem')->middleware(['money'])->name('invoice.add.item'); Route::post('invoices/payment', 'Incomes\Invoices@payment')->middleware(['dateformat', 'money'])->name('invoice.payment'); Route::delete('invoices/payment/{payment}', 'Incomes\Invoices@paymentDestroy'); Route::post('invoices/import', 'Incomes\Invoices@import')->name('invoices.import'); @@ -167,6 +167,7 @@ Route::group(['middleware' => 'language'], function () { Route::resource('categories', 'Modals\Categories'); Route::resource('customers', 'Modals\Customers'); Route::resource('vendors', 'Modals\Vendors'); + Route::resource('invoices/{invoice}/payment', 'Modals\InvoicePayments', ['middleware' => ['dateformat', 'money']]); }); /* @deprecated */