$(document).ready(function() { $(document).on('change', '.purchase_quantity', function() { update_table_total($(this).closest('table')); }); $(document).on('change', '.unit_price', function() { update_table_total($(this).closest('table')); }); // Datepicker for expiration date - Persian support if (typeof is_persian_lang !== 'undefined' && is_persian_lang && typeof $.fn.persianDatepicker !== 'undefined') { $('.os_exp_date').persianDatepicker({ calendarType: 'persian', format: datepicker_date_format, autoClose: true }); } else { $('.os_exp_date').datepicker({ autoclose: true, format: datepicker_date_format, }); } $(document).on('click', '.add_stock_row', function() { var tr = $(this).data('row-html'); var key = parseInt($(this).data('sub-key')); tr = tr.replace(/\__subkey__/g, key); $(this).data('sub-key', key + 1); var $newRow = $(tr).insertAfter($(this).closest('tr')); // Datepicker for expiration date - Persian support if (typeof is_persian_lang !== 'undefined' && is_persian_lang && typeof $.fn.persianDatepicker !== 'undefined') { $newRow.find('.os_exp_date').persianDatepicker({ calendarType: 'persian', format: datepicker_date_format, autoClose: true }); } else { $newRow.find('.os_exp_date').datepicker({ autoclose: true, format: datepicker_date_format, }); } __init_datetime_field($(this).closest('tr').next('tr').find('.os_date')); }); $(document).on('click', '.add-opening-stock', function(e) { e.preventDefault(); $.ajax({ url: $(this).data('href'), dataType: 'html', success: function(result) { $('#opening_stock_modal') .html(result) .modal('show'); }, }); }); }); //Re-initialize data picker on modal opening $('#opening_stock_modal').on('shown.bs.modal', function(e) { // Datepicker for expiration date - Persian support if (typeof is_persian_lang !== 'undefined' && is_persian_lang && typeof $.fn.persianDatepicker !== 'undefined') { $('#opening_stock_modal .os_exp_date').persianDatepicker({ calendarType: 'persian', format: typeof datepicker_date_format !== 'undefined' ? datepicker_date_format : 'YYYY/MM/DD', autoClose: true, observer: true, timePicker: { enabled: false } }); } else { $('#opening_stock_modal .os_exp_date').datepicker({ autoclose: true, format: typeof datepicker_date_format !== 'undefined' ? datepicker_date_format : 'yyyy/mm/dd', }); } __init_datetime_field('#opening_stock_modal .os_date', { widgetPositioning: { horizontal: 'right', vertical: 'bottom' } }); }); $(document).on('click', 'button#add_opening_stock_btn', function(e) { e.preventDefault(); var btn = $(this); var data = $('form#add_opening_stock_form').serialize(); $.ajax({ method: 'POST', url: $('form#add_opening_stock_form').attr('action'), dataType: 'json', data: data, beforeSend: function(xhr) { __disable_submit_button(btn); }, success: function(result) { if (result.success == true) { $('#opening_stock_modal').modal('hide'); toastr.success(result.msg); } else { toastr.error(result.msg); } }, }); return false; }); function update_table_total(table) { var total_subtotal = 0; table.find('tbody tr').each(function() { var qty = __read_number($(this).find('.purchase_quantity')); var unit_price = __read_number($(this).find('.unit_price')); var row_subtotal = qty * unit_price; $(this) .find('.row_subtotal_before_tax') .text(__number_f(row_subtotal)); total_subtotal += row_subtotal; }); table.find('tfoot tr #total_subtotal').text(__currency_trans_from_en(total_subtotal, true)); table.find('tfoot tr #total_subtotal_hidden').val(total_subtotal); }