Blame view

resources/views/expenses/bills/edit.blade.php 23 KB
b7c7a5f6   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
  @extends('layouts.admin')
  
  @section('title', trans('general.title.edit', ['type' => trans_choice('general.bills', 1)]))
  
  @section('content')
      <!-- Default box -->
      <div class="box box-success">
          {!! Form::model($bill, ['method' => 'PATCH', 'files' => true, 'url' => ['expenses/bills', $bill->id], 'role' => 'form']) !!}
  
          <div class="box-body">
              {{ Form::selectGroup('vendor_id', trans_choice('general.vendors', 1), 'user', $vendors) }}
  
              {{ Form::selectGroup('currency_code', trans_choice('general.currencies', 1), 'exchange', $currencies) }}
  
              {{ Form::textGroup('billed_at', trans('bills.bill_date'), 'calendar', ['id' => 'billed_at', 'class' => 'form-control', 'required' => 'required', 'data-inputmask' => '\'alias\': \'yyyy-mm-dd\'', 'data-mask' => '', 'autocomplete' => 'off'], Date::parse($bill->billed_at)->toDateString()) }}
  
              {{ Form::textGroup('due_at', trans('bills.due_date'), 'calendar', ['id' => 'due_at', 'class' => 'form-control', 'required' => 'required', 'data-inputmask' => '\'alias\': \'yyyy-mm-dd\'', 'data-mask' => '', 'autocomplete' => 'off'], Date::parse($bill->due_at)->toDateString()) }}
  
              {{ Form::textGroup('bill_number', trans('bills.bill_number'), 'file-text-o') }}
  
              {{ Form::textGroup('order_number', trans('bills.order_number'), 'shopping-cart',[]) }}
  
              <div class="form-group col-md-12">
                  {!! Form::label('items', trans_choice('general.items', 2), ['class' => 'control-label']) !!}
                  <div class="table-responsive">
                      <table class="table table-bordered" id="items">
                          <thead>
                              <tr style="background-color: #f9f9f9;">
                                  @stack('actions_th_start')
                                  <th width="5%"  class="text-center">{{ trans('general.actions') }}</th>
                                  @stack('actions_th_end')
                                  @stack('name_th_start')
                                  <th width="40%" class="text-left">{{ trans('general.name') }}</th>
                                  @stack('name_th_end')
                                  @stack('quantity_th_start')
                                  <th width="5%" class="text-center">{{ trans('bills.quantity') }}</th>
                                  @stack('quantity_th_end')
                                  @stack('price_th_start')
                                  <th width="10%" class="text-right">{{ trans('bills.price') }}</th>
                                  @stack('price_th_end')
                                  @stack('taxes_th_start')
                                  <th width="15%" class="text-right">{{ trans_choice('general.taxes', 1) }}</th>
                                  @stack('taxes_th_end')
                                  @stack('total_th_start')
                                  <th width="10%" class="text-right">{{ trans('bills.total') }}</th>
                                  @stack('total_th_end')
                              </tr>
                          </thead>
                          <tbody>
                              @php $item_row = 0; @endphp
                              @if(old('item'))
                                  @foreach(old('item') as $old_item)
                                      @php $item = (object) $old_item; @endphp
                                      @include('expenses.bills.item')
                                      @php $item_row++; @endphp
                                  @endforeach
                              @else
                                  @foreach($bill->items as $item)
                                      @include('expenses.bills.item')
                                      @php $item_row++; @endphp
                                  @endforeach
                                  @if (empty($bill->items))
                                      @include('expenses.bills.item')
                                  @endif
                              @endif
                              @php $item_row++; @endphp
                              @stack('add_item_td_start')
                              <tr id="addItem">
                                  <td class="text-center"><button type="button" id="button-add-item" data-toggle="tooltip" title="{{ trans('general.add') }}" class="btn btn-xs btn-primary" data-original-title="{{ trans('general.add') }}"><i class="fa fa-plus"></i></button></td>
                                  <td class="text-right" colspan="5"></td>
                              </tr>
                              @stack('add_item_td_end')
                              @stack('sub_total_td_start')
                              <tr>
                                  <td class="text-right" colspan="5"><strong>{{ trans('bills.sub_total') }}</strong></td>
                                  <td class="text-right"><span id="sub-total">0</span></td>
                              </tr>
                              @stack('sub_total_td_end')
                              @stack('add_discount_td_start')
                              <tr>
                                  <td class="text-right" style="vertical-align: middle;" colspan="5">
                                      <a href="javascript:void(0)" id="discount-text" rel="popover">{{ trans('bills.add_discount') }}</a>
                                  </td>
                                  <td class="text-right">
                                      <span id="discount-total"></span>
                                      {!! Form::hidden('discount', null, ['id' => 'discount', 'class' => 'form-control text-right']) !!}
                                  </td>
                              </tr>
                              @stack('add_discount_td_end')
                              @stack('tax_total_td_start')
                              <tr>
                                  <td class="text-right" colspan="5"><strong>{{ trans_choice('general.taxes', 1) }}</strong></td>
                                  <td class="text-right"><span id="tax-total">0</span></td>
                              </tr>
                              @stack('tax_total_td_end')
                              @stack('grand_total_td_start')
                              <tr>
                                  <td class="text-right" colspan="5"><strong>{{ trans('bills.total') }}</strong></td>
                                  <td class="text-right"><span id="grand-total">0</span></td>
                              </tr>
                              @stack('grand_total_td_end')
                          </tbody>
                      </table>
                  </div>
              </div>
  
              {{ Form::textareaGroup('notes', trans_choice('general.notes', 2)) }}
  
              {{ Form::selectGroup('category_id', trans_choice('general.categories', 1), 'folder-open-o', $categories) }}
  
              {{ Form::recurring('edit', $bill) }}
  
              {{ Form::fileGroup('attachment', trans('general.attachment'),[]) }}
  
              {{ Form::hidden('vendor_name', old('customer_name', null), ['id' => 'vendor_name']) }}
              {{ Form::hidden('vendor_email', old('vendor_email', null), ['id' => 'vendor_email']) }}
              {{ Form::hidden('vendor_tax_number', old('vendor_tax_number', null), ['id' => 'vendor_tax_number']) }}
              {{ Form::hidden('vendor_phone', old('vendor_phone', null), ['id' => 'vendor_phone']) }}
              {{ Form::hidden('vendor_address', old('vendor_address', null), ['id' => 'vendor_address']) }}
              {{ Form::hidden('currency_rate', old('currency_rate', null), ['id' => 'currency_rate']) }}
              {{ Form::hidden('bill_status_code', old('bill_status_code', null), ['id' => 'bill_status_code']) }}
              {{ Form::hidden('amount', old('amount', null), ['id' => 'amount']) }}
          </div>
          <!-- /.box-body -->
  
          @permission('update-expenses-bills')
          <div class="box-footer">
              {{ Form::saveButtons('expenses/bills') }}
          </div>
          <!-- /.box-footer -->
          @endpermission
          {!! Form::close() !!}
      </div>
  @endsection
  
  @push('js')
      <script src="{{ asset('vendor/almasaeed2010/adminlte/plugins/datepicker/bootstrap-datepicker.js') }}"></script>
      <script src="{{ asset('vendor/almasaeed2010/adminlte/plugins/datepicker/locales/bootstrap-datepicker.' . language()->getShortCode() . '.js') }}"></script>
      <script src="{{ asset('public/js/bootstrap-fancyfile.js') }}"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.1/bootstrap3-typeahead.min.js"></script>
  @endpush
  
  @push('css')
      <link rel="stylesheet" href="{{ asset('vendor/almasaeed2010/adminlte/plugins/datepicker/datepicker3.css') }}">
      <link rel="stylesheet" href="{{ asset('public/css/bootstrap-fancyfile.css') }}">
  @endpush
  
  @push('scripts')
      <script type="text/javascript">
          var item_row = '{{ $item_row }}';
  
          $(document).on('click', '#button-add-item', function (e) {
              var currency_code = $('#currency_code').val();
  
              $.ajax({
                  url: '{{ url("expenses/bills/addItem") }}',
                  type: 'GET',
                  dataType: 'JSON',
                  data: {item_row: item_row, currency_code : currency_code},
                  success: function(json) {
                      if (json['success']) {
                          $('#items tbody #addItem').before(json['html']);
                          //$('[rel=tooltip]').tooltip();
  
                          $('[data-toggle="tooltip"]').tooltip('hide');
  
                          $('#item-row-' + item_row + ' .tax-select2').select2({
                              placeholder: {
                                  id: '-1', // the value of the option
                                  text: "{{ trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)]) }}"
                              }
                          });
  
                          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++;
                      }
                  }
              });
          });
  
          $(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
              $('#billed_at').datepicker({
                  format: 'yyyy-mm-dd',
                  weekStart: 1,
                  autoclose: true,
                  language: '{{ language()->getShortCode() }}'
              });
  
              //Date picker
              $('#due_at').datepicker({
                  format: 'yyyy-mm-dd',
                  weekStart: 1,
                  autoclose: true,
                  language: '{{ language()->getShortCode() }}'
              });
  
              $(".tax-select2").select2({
                  placeholder: {
                      id: '-1', // the value of the option
                      text: "{{ trans('general.form.select.field', ['field' => trans_choice('general.taxes', 1)]) }}"
                  }
              });
  
              $("#vendor_id").select2({
                  placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.vendors', 1)]) }}"
              });
  
              $("#currency_code").select2({
                  placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.currencies', 1)]) }}"
              });
  
              $("#category_id").select2({
                  placeholder: "{{ trans('general.form.select.field', ['field' => trans_choice('general.categories', 1)]) }}"
              });
  
              $('#attachment').fancyfile({
                  text  : '{{ trans('general.form.select.file') }}',
                  style : 'btn-default',
                  @if($bill->attachment)
                  placeholder : '<?php echo $bill->attachment->basename; ?>'
                  @else
                  placeholder : '{{ trans('general.form.no_file_selected') }}'
                  @endif
              });
  
              @if($bill->attachment)
              attachment_html  = '<span class="attachment">';
              attachment_html += '    <a href="{{ url('uploads/' . $bill->attachment->id . '/download') }}">';
              attachment_html += '        <span id="download-attachment" class="text-primary">';
              attachment_html += '            <i class="fa fa-file-{{ $bill->attachment->aggregate_type }}-o"></i> {{ $bill->attachment->basename }}';
              attachment_html += '        </span>';
              attachment_html += '    </a>';
              attachment_html += '    {!! Form::open(['id' => 'attachment-' . $bill->attachment->id, 'method' => 'DELETE', 'url' => [url('uploads/' . $bill->attachment->id)], 'style' => 'display:inline']) !!}';
              attachment_html += '    <a id="remove-attachment" href="javascript:void();">';
              attachment_html += '        <span class="text-danger"><i class="fa fa fa-times"></i></span>';
              attachment_html += '    </a>';
              attachment_html += '    {!! Form::close() !!}';
              attachment_html += '</span>';
  
              $('.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' => '<strong>' . $bill->attachment->basename . '</strong>', 'type' => strtolower(trans('general.attachment'))]) !!}", "{!! trans('general.cancel') !!}", "{!! trans('general.delete')  !!}");
              });
              @endif
  
              var autocomplete_path = "{{ url('common/items/autocomplete') }}";
  
              $(document).on('click', '.form-control.typeahead', function() {
                  input_id = $(this).attr('id').split('-');
  
                  item_id = parseInt(input_id[input_id.length-1]);
  
                  $(this).typeahead({
                      minLength: 3,
                      displayText:function (data) {
                          return data.name + ' (' + data.sku + ')';
                      },
                      source: function (query, process) {
                          $.ajax({
                              url: autocomplete_path,
                              type: 'GET',
                              dataType: 'JSON',
                              data: 'query=' + query + '&type=bill&currency_code=' + $('#currency_code').val(),
                              success: function(data) {
                                  return process(data);
                              }
                          });
                      },
                      afterSelect: function (data) {
                          $('#item-id-' + item_id).val(data.item_id);
                          $('#item-quantity-' + item_id).val('1');
                          $('#item-price-' + item_id).val(data.purchase_price);
                          $('#item-tax-' + item_id).val(data.tax_id);
  
                          // This event Select2 Stylesheet
                          $('#item-price-' + item_id).trigger('focusout');
                          $('#item-tax-' + item_id).trigger('change');
  
                          $('#item-total-' + item_id).html(data.total);
  
                          totalItem();
                      }
                  });
              });
  
              $('a[rel=popover]').popover({
                  html: true,
                  placement: 'bottom',
                  title: '{{ trans('bills.discount') }}',
                  content: function () {
                      html  = '<div class="discount box-body">';
                      html += '    <div class="col-md-6">';
                      html += '        <div class="input-group" id="input-discount">';
                      html += '            {!! Form::number('pre-discount', null, ['id' => 'pre-discount', 'class' => 'form-control text-right']) !!}';
                      html += '            <div class="input-group-addon"><i class="fa fa-percent"></i></div>';
                      html += '        </div>';
                      html += '    </div>';
                      html += '    <div class="col-md-6">';
                      html += '        <div class="discount-description">';
                      html += '           {{ trans('bills.discount_desc') }}';
                      html += '        </div>';
                      html += '    </div>';
                      html += '</div>';
                      html += '<div class="discount box-footer">';
                      html += '    <div class="col-md-12">';
                      html += '        <div class="form-group no-margin">';
                      html += '            {!! Form::button('<span class="fa fa-save"></span> &nbsp;' . trans('general.save'), ['type' => 'button', 'id' => 'save-discount','class' => 'btn btn-success']) !!}';
                      html += '            <a href="javascript:void(0)" id="cancel-discount" class="btn btn-default"><span class="fa fa-times-circle"></span> &nbsp;{{ trans('general.cancel') }}</a>';
                      html += '       </div>';
                      html += '    </div>';
                      html += '</div>';
  
                      return html;
                  }
              });
  
              $(document).on('keyup', '#pre-discount', function(e){
                  e.preventDefault();
  
                  $('#discount').val($(this).val());
  
                  totalItem();
              });
  
              $(document).on('click', '#save-discount', function(){
                  $('a[rel=popover]').trigger('click');
              });
  
              $(document).on('click', '#cancel-discount', function(){
                  $('#discount').val('');
  
                  totalItem();
  
                  $('a[rel=popover]').trigger('click');
              });
  
              $(document).on('change', '#currency_code, #items tbody select', function(){
                  totalItem();
              });
  
             var focus = false;
      
              $(document).on('focusin', '#items .input-price', function(){
                  focus = true;
              });
  
              $(document).on('blur', '#items .input-price', function(){
                  if (focus) {
                      totalItem();
  
                      focus = false;
                  }
              });
  
              $(document).on('keyup', '#items tbody .form-control', function(){
                  if (!$(this).hasClass('input-price')) {
                      totalItem();
                  }
              });
  
              $(document).on('change', '#vendor_id', function (e) {
                  $.ajax({
                      url: '{{ url("expenses/vendors/currency") }}',
                      type: 'GET',
                      dataType: 'JSON',
                      data: 'vendor_id=' + $(this).val(),
                      success: function(data) {
                          $('#vendor_name').val(data.name);
                          $('#vendor_email').val(data.email);
                          $('#vendor_tax_number').val(data.tax_number);
                          $('#vendor_phone').val(data.phone);
                          $('#vendor_address').val(data.address);
  
                          $('#currency_code').val(data.currency_code);
                          $('#currency_rate').val(data.currency_rate);
  
                          $('.input-price').each(function(){
                              input_price_id = $(this).attr('id');
                              input_currency_id = input_price_id.replace('price', 'currency');
  
                              $('#' + input_currency_id).val(data.currency_code);
  
                              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');
                      }
                  });
              });
  
              @if(old('item'))
                  totalItem();
              @endif
          });
  
          function totalItem() {
              $.ajax({
                  url: '{{ url("common/items/totalItem") }}',
                  type: 'POST',
                  dataType: 'JSON',
                  data: $('#currency_code, #discount input[type=\'number\'], #items input[type=\'text\'], #items input[type=\'number\'], #items input[type=\'hidden\'], #items textarea, #items select'),
                  headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' },
                  success: function(data) {
                      if (data) {
                          $.each( data.items, function( key, value ) {
                              $('#item-total-' + key).html(value);
                          });
  
                          $('#discount-text').text(data.discount_text);
  
                          $('#sub-total').html(data.sub_total);
                          $('#discount-total').html(data.discount_total);
                          $('#tax-total').html(data.tax_total);
                          $('#grand-total').html(data.grand_total);
  
                          $('.input-price').each(function(){
                              input_price_id = $(this).attr('id');
                              input_currency_id = input_price_id.replace('price', 'currency');
  
                              $('#' + input_currency_id).val(data.currency_code);
  
                              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');
                          });
                      }
                  }
              });
          }
      </script>
  @endpush