// GLOBAL VARIABLES var fnibrand_brand_id_selected = ""; var fnibrand_model_id_selected = ""; var fnibrand_variant_id_selected = ""; var shown = true; $(function(){ // initializations $('#fnibrand-dd-models-brand').select2({ placeholder: "Specify model's brand", allowClear: false }); $('#fnibrand-dd-variants-model').select2({ placeholder: "Specify variant's model", allowClear: false }); $('#add-dd-models-brand').select2({ placeholder: "Select brand", allowClear: false }); $('#add-dd-variants-brand').select2({ placeholder: "Select brand", allowClear: false }); $('#add-dd-variants-model').select2({ placeholder: "Select model", allowClear: false }); $('#fnibrand-update-brand').attr('disabled', true); $('#fnibrand-brand-name').attr('disabled', true); $('#fnibrand-brand-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-search-model').attr('disabled', true); $('#fnibrand-update-model').attr('disabled', true); $('#fnibrand-model-name').attr('disabled', true); $('#fnibrand-dd-models-brand').attr('disabled', true); $('#fnibrand-model-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-search-variant').attr('disabled', true); $('#fnibrand-update-variant').attr('disabled', true); $('#fnibrand-variant-name').attr('disabled', true); $('#fnibrand-dd-variants-model').attr('disabled', true); $('#fnibrand-variant-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-add-new').on("click", function() { shown ? $(this).hideBalloon() : $(this).showBalloon(); shown = !shown; }).showBalloon({ position: 'right', html: true, css: { color: 'black' }, contents: `
` }); $('#fnibrand-add-new').hideBalloon(); shown = !shown; }); function refreshFniConfigBrandList(){ $.ajax({ url:"app/finance/configurations/finance_brand.php", type:"POST", data: { type: 4 // reload list w/o brand id }, beforeSend: function(){ }, success: function(result) { $('#fnibrand-brand-list').html(''); $('#fnibrand-brand-list').append("\"" + result + "\""); }, }) } function refreshFniConfigModelList(){ $.ajax({ url:"app/finance/configurations/finance_model.php", type:"POST", data: { type: 4, brand_id: fnibrand_brand_id_selected }, beforeSend: function(){ }, success: function(result) { $('#fnibrand-model-list').html(''); $('#fnibrand-model-list').append("\"" + result + "\""); }, }) } function refreshFniConfigVariantList(){ $.ajax({ url:"app/finance/configurations/finance_variant.php", type:"POST", data: { type: 4, model_id: fnibrand_model_id_selected }, beforeSend: function(){ }, success: function(result) { $('#fnibrand-variant-status-checkbox').bootstrapSwitch('disabled', false); $('#fnibrand-variant-list').html(''); $('#fnibrand-variant-list').append("\"" + result + "\""); }, }) } function delay(callback, ms) { var timer = 0; return function() { var context = this, args = arguments; clearTimeout(timer); timer = setTimeout(function () { callback.apply(context, args); }, ms || 0); }; } $(document).on('mouseup', function(e) { var container = $("#fnibrand-add-new"); if (!container.is(e.target) && container.has(e.target).length === 0) { container.hideBalloon(); shown = !shown; } }); $("input[data-bootstrap-switch]").each(function(){ $(this).bootstrapSwitch('state', $(this).prop('checked')); }); $('#fnibrand-submit-brand').on('click', function(){ var new_brand = $('#fnibrand-new-brand-name').val(); if(new_brand == ""){ toastr.remove(); toastr.error('Brand name field is empty'); return; } $.ajax({ url:"app/finance/configurations/finance_brand.php", type:"POST", dataType: 'json', data: { type: 1, new_brand: new_brand }, beforeSend: function(){ }, success: function(result) { if(result.status == 0){ toastr.remove(); toastr.error(result.message); } else if(result.status == 1){ toastr.remove(); toastr.success("New brand added successfully"); $('#new-brand-name').val(""); $('#modal-fnibrand-add-brand').modal('hide'); refreshFniConfigBrandList(); } }, }); }); $('#fnibrand-submit-model').on('click', function(){ var models_brand = $('#add-dd-models-brand').val(); if(models_brand === null || models_brand == "" || typeof models_brand === "undefined"){ models_brand = 0; } var new_model = $('#fnibrand-new-model-name').val(); if(models_brand == 0){ toastr.remove(); toastr.error('Please specify Model\'s brand'); return; } if(new_model == ""){ toastr.remove(); toastr.error('Model name field is empty'); return; } $.ajax({ url:"app/finance/configurations/finance_model.php", type:"POST", dataType: 'json', data: { type: 1, models_brand: models_brand, new_model: new_model }, beforeSend: function(){ }, success: function(result) { if(result.status == 0){ toastr.remove(); toastr.error(result.message); } else if(result.status == 1){ toastr.remove(); toastr.success("New model added successfully"); $('#new-model-name').val(""); $('#modal-fnibrand-add-model').modal('hide'); refreshFniConfigModelList(); } }, }); }); $('#fnibrand-submit-variant').on('click', function(){ var variants_model = $('#add-dd-variants-model').val(); if(variants_model === null || variants_model == "" || typeof variants_model === "undefined"){ variants_model = 0; } var new_variant_name = $('#fnibrand-new-variant-name').val(); if(variants_model == 0){ toastr.remove(); toastr.error('Please specify Variant\'s model'); return; } if(new_variant_name == ""){ toastr.remove(); toastr.error('Variant name field is empty'); return; } $.ajax({ url:"app/finance/configurations/finance_variant.php", type:"POST", dataType: 'json', data: { type: 1, variants_model: variants_model, new_variant_name: new_variant_name }, beforeSend: function(){ }, success: function(result) { if(result.status == 0){ toastr.remove(); toastr.error(result.message); } else if(result.status == 1){ toastr.remove(); toastr.success("New variant added successfully"); $('#fnibrand-new-variant-name').val(""); $('#modal-fnibrand-add-variant').modal('hide'); refreshFniConfigVariantList(); } }, }); }); $('#fnibrand-brand-list').on('change', function(){ fnibrand_brand_id_selected = $('#fnibrand-brand-list').val(); $.ajax({ url:"app/finance/configurations/finance_brand.php", type:"POST", dataType: 'json', data: { brand_id: fnibrand_brand_id_selected, type: 2 // reload/load brand }, beforeSend: function(){ }, success: function(result) { $('#fnibrand-brand-name').val(result.name); $('#fnibrand-brand-added-by').text(result.added_by); $('#fnibrand-brand-last-modified-by').text(result.last_modified_by + "\n" + "(" + result.date_modified + ")"); $('#fnibrand-brand-date-added').text(result.date_added); $('#fnibrand-brand-status-checkbox').bootstrapSwitch('state', result.status); $('#fnibrand-update-brand').attr('disabled', false); $('#fnibrand-brand-name').attr('disabled', false); $('#fnibrand-brand-status-checkbox').bootstrapSwitch('disabled', false); $('#fnibrand-search-model').attr('disabled', false); refreshFniConfigModelList(); }, }) }); $('#fnibrand-model-list').on('change', function(){ fnibrand_model_id_selected = $('#fnibrand-model-list').val(); $.ajax({ url:"app/finance/configurations/finance_model.php", type:"POST", dataType: 'json', data: { model_id: fnibrand_model_id_selected, type: 2 // reload/load model }, beforeSend: function(){ }, success: function(result) { $('#fnibrand-dd-models-brand').val(result.brand_id).trigger('change'); $('#fnibrand-model-name').val(result.name); $('#fnibrand-model-added-by').text(result.added_by); $('#fnibrand-model-last-modified-by').text(result.last_modified_by + "\n" + "(" + result.date_modified + ")"); $('#fnibrand-model-date-added').text(result.date_added); $('#fnibrand-model-status-checkbox').bootstrapSwitch('disabled', false); $('#fnibrand-model-status-checkbox').bootstrapSwitch('state', result.status); $('#fnibrand-update-model').attr('disabled', false); $('#fnibrand-model-status-checkbox').bootstrapSwitch('disabled', false); $('#fnibrand-model-name').attr('disabled', false); $('#fnibrand-dd-models-brand').attr('disabled', false); $('#fnibrand-search-variant').attr('disabled', false); refreshFniConfigVariantList(); }, }) }); $('#fnibrand-variant-list').on('change', function(){ fnibrand_variant_id_selected = $('#fnibrand-variant-list').val(); $.ajax({ url:"app/finance/configurations/finance_variant.php", type:"POST", dataType: 'json', data: { variant_id: fnibrand_variant_id_selected, type: 2 // reload/load variant }, beforeSend: function(){ }, success: function(result) { $('#fnibrand-dd-variants-model').val(result.model_id).trigger('change'); $('#fnibrand-variant-name').val(result.name); $('#fnibrand-variant-added-by').text(result.added_by); $('#fnibrand-variant-last-modified-by').text(result.last_modified_by + "\n" + "(" + result.date_modified + ")"); $('#fnibrand-variant-date-added').text(result.date_added); $('#fnibrand-variant-status-checkbox').bootstrapSwitch('disabled', false); $('#fnibrand-variant-status-checkbox').bootstrapSwitch('state', result.status); $('#fnibrand-update-variant').attr('disabled', false); $('#fnibrand-variant-status-checkbox').bootstrapSwitch('disabled', false); $('#fnibrand-variant-name').attr('disabled', false); $('#fnibrand-dd-variants-model').attr('disabled', false); refreshFniConfigVariantList(); }, }) }); $('#fnibrand-update-brand').on('click', function (){ var update_brand_name = $('#fnibrand-brand-name').val(); var status = $('#fnibrand-brand-status-checkbox').bootstrapSwitch('state'); if(fnibrand_brand_id_selected == ""){ toastr.remove(); toastr.error("Please select brand name to be modified first"); return; } if(update_brand_name == ""){ toastr.remove(); toastr.error("Brand name is empty"); return; } $.ajax({ url:"app/finance/configurations/finance_brand.php", type:"POST", dataType: 'json', data: { type: 3, // update brand brand_id: fnibrand_brand_id_selected, new_brand_name: update_brand_name, status: status }, beforeSend: function(){ }, success: function(result) { if(result.status == 0){ toastr.remove(); toastr.error("Brand already existing"); return; } if(result.status == 1){ toastr.remove(); toastr.success("Brand name successfully updated"); refreshFniConfigBrandList(); $('#fnibrand-brand-name').val(""); $('#fnibrand-brand-added-by').text(""); $('#fnibrand-brand-date-added').text(""); $('#fnibrand-brand-last-modified-by').text(""); $('#fnibrand-brand-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-update-brand').attr('disabled', true); $('#fnibrand-brand-name').attr('disabled', true); $('#fnibrand-brand-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-search-model').attr('disabled', true); $('#fnibrand-update-model').attr('disabled', true); $('#fnibrand-model-name').attr('disabled', true); $('#fnibrand-dd-models-brand').attr('disabled', true); $('#fnibrand-model-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-model-list').html(''); $('#fnibrand-search-variant').attr('disabled', true); $('#fnibrand-update-variant').attr('disabled', true); $('#fnibrand-variant-name').attr('disabled', true); $('#fnibrand-dd-variants-model').attr('disabled', true); $('#fnibrand-variant-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-variant-list').html(''); // $("#fnibrand-brand-list").select2("val", "0"); return; } }, }) }); $('#fnibrand-update-model').on('click', function (){ var update_model_name = $('#fnibrand-model-name').val(); var status = $('#fnibrand-model-status-checkbox').bootstrapSwitch('state'); var models_brand = $('#fnibrand-dd-models-brand').val(); if(fnibrand_model_id_selected == ""){ toastr.remove(); toastr.error("Please select model name to be modified first"); return; } if(update_model_name == ""){ toastr.remove(); toastr.error("Model name is empty"); return; } if(models_brand == ""){ toastr.remove(); toastr.error("Please specify model's brand"); return; } $.ajax({ url:"app/finance/configurations/finance_model.php", type:"POST", dataType: 'json', data: { type: 3, // update model model_id: fnibrand_model_id_selected, new_model_name: update_model_name, new_models_brand: models_brand, status: status }, beforeSend: function(){ }, success: function(result) { if(result.status == 0){ toastr.remove(); toastr.error("Model already existing"); return; } if(result.status == 1){ toastr.remove(); toastr.success("Model name successfully updated"); refreshFniConfigModelList(); $('#fnibrand-model-name').val(""); $('#fnibrand-dd-models-brand').val(0); $('#fnibrand-model-added-by').text(""); $('#fnibrand-model-date-added').text(""); $('#fnibrand-model-last-modified-by').text(""); $('#fnibrand-model-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-search-model').attr('disabled', true); $('#fnibrand-update-model').attr('disabled', true); $('#fnibrand-model-name').attr('disabled', true); $('#fnibrand-dd-models-brand').attr('disabled', true); $('#fnibrand-model-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-search-variant').attr('disabled', true); $('#fnibrand-update-variant').attr('disabled', true); $('#fnibrand-variant-name').attr('disabled', true); $('#fnibrand-dd-variants-model').attr('disabled', true); $('#fnibrand-variant-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-variant-list').html(''); // $("#fnibrand-brand-list").select2("val", "0"); return; } }, }) }); $('#fnibrand-update-variant').on('click', function (){ var update_variant_name = $('#fnibrand-variant-name').val(); var status = $('#fnibrand-variant-status-checkbox').bootstrapSwitch('state'); var variants_model = $('#fnibrand-dd-variants-model').val(); if(fnibrand_variant_id_selected == ""){ toastr.remove(); toastr.error("Please select variant name to be modified first"); return; } if(update_variant_name == ""){ toastr.remove(); toastr.error("Variant name is empty"); return; } if(variants_model == ""){ toastr.remove(); toastr.error("Please specify variant's model"); return; } $.ajax({ url:"app/finance/configurations/finance_variant.php", type:"POST", dataType: 'json', data: { type: 3, // update variant variant_id: fnibrand_variant_id_selected, new_variant_name: update_variant_name, new_variants_model: variants_model, status: status }, beforeSend: function(){ }, success: function(result) { if(result.status == 0){ toastr.remove(); toastr.error("Variant already existing"); return; } if(result.status == 1){ toastr.remove(); toastr.success("Variant name successfully updated"); refreshFniConfigVariantList(); $('#fnibrand-variant-name').val(""); $('#fnibrand-dd-variants-model').val(0); $('#fnibrand-variant-added-by').text(""); $('#fnibrand-variant-date-added').text(""); $('#fnibrand-variant-last-modified-by').text(""); $('#fnibrand-variant-status-checkbox').bootstrapSwitch('disabled', true); $('#fnibrand-search-variant').attr('disabled', true); $('#fnibrand-update-variant').attr('disabled', true); $('#fnibrand-variant-name').attr('disabled', true); $('#fnibrand-dd-variants-model').attr('disabled', true); $('#fnibrand-variant-status-checkbox').bootstrapSwitch('disabled', true); // $("#fnibrand-brand-list").select2("val", "0"); return; } }, }) }); $('#fnibrand-search-brand').on('keyup', delay(function (e){ var search_brand = $('#fnibrand-search-brand').val(); fnibrand_brand_id_selected = ""; $.ajax({ url:"app/finance/configurations/finance_brand.php", type:"POST", data: { type: 5, search_item: search_brand }, beforeSend: function(){ }, success: function(result){ $('#fnibrand-brand-list').html(''); $('#fnibrand-brand-list').append("\"" + result + "\""); } }); }, 500)); $('#fnibrand-search-model').on('keyup', delay(function (e){ var search_model = $('#fnibrand-search-model').val(); fnibrand_model_id_selected = ""; $.ajax({ url:"app/finance/configurations/finance_model.php", type:"POST", data: { type: 5, brand_id: fnibrand_brand_id_selected, search_item: search_model }, beforeSend: function(){ }, success: function(result){ $('#fnibrand-model-list').html(''); $('#fnibrand-model-list').append("\"" + result + "\""); } }); }, 500)); $('#fnibrand-search-variant').on('keyup', delay(function (e){ var search_variant = $('#fnibrand-search-variant').val(); fnibrand_variant_id_selected = ""; $.ajax({ url:"app/finance/configurations/finance_variant.php", type:"POST", data: { type: 5, model_id: fnibrand_model_id_selected, search_item: search_variant }, beforeSend: function(){ }, success: function(result){ $('#fnibrand-variant-list').html(''); $('#fnibrand-variant-list').append("\"" + result + "\""); } }); }, 500)); $('#add-dd-variants-brand').on('change', function(){ var variants_brand = $('#add-dd-variants-brand').val(); $.ajax({ url:"app/finance/dd_get_model.php", type:"POST", async: false, data: { brand_id: variants_brand, type: 1 }, beforeSend: function(){ }, success: function(result){ $('#add-dd-variants-model').html(''); $('#add-dd-variants-model').append("\"\""); $('#add-dd-variants-model').append("\"" + result + "\""); } }); }); $('#close-modal').on('click', function(){ $('#modal-add-nature').modal('hide'); $('#new-nature-name').val(""); });