var projectTable; var projectRowID; var projectPos; var quotationTable; var quotationRowID; var quotationPos; var historyTable; var historyDataTable; var historyRowID; var historyPos; var quotation_total; var deledMainItems = []; var deledSubItems = []; var deledPayments = []; var deledInvoices = []; var preSubItem_name = ''; var hasBind = 'N'; var tmpInvoiceItem; var tmpPaymentItem; $(document).ready(function () { deptList(); companyList(); loadyearmonth(); loadQuotationTable(); initInvoiceSelectItem(); loadHistoryTable(); var actualDate = new Date(); // convert to actual date var prevDate = new Date(actualDate.getFullYear(), actualDate.getMonth() - 13, actualDate.getDate()); var startTxt = prevDate.getFullYear().toString() + "/" + padding(prevDate.getMonth() + 1, 2); var endTxt = actualDate.getFullYear().toString() + "/" + padding(actualDate.getMonth() + 1, 2); const fpStart = flatpickr("#dateStart", { "locale": "zh_tw", "plugins": [ new monthSelectPlugin({ shorthand: true, //defaults to false dateFormat: "Y/m", //defaults to "F Y" altFormat: "Y M", //defaults to "F Y" theme: "light" // defaults to "light" }) ] }); const fpEnd = flatpickr("#dateEnd", { "locale": "zh_tw", "plugins": [ new monthSelectPlugin({ shorthand: true, //defaults to false dateFormat: "Y/m", //defaults to "F Y" altFormat: "Y M", //defaults to "F Y" theme: "light" // defaults to "light" }) ] }); const fpInvoiceDate = flatpickr("#invoice_date", { "locale": "zh_tw", "plugins": [ new monthSelectPlugin({ shorthand: true, //defaults to false dateFormat: "Y/m", //defaults to "F Y" altFormat: "Y M", //defaults to "F Y" theme: "light" // defaults to "light" }) ] }); fpStart.setDate(new Date(startTxt + '/1')); fpEnd.setDate(new Date(endTxt + '/1')); fpInvoiceDate.setDate(new Date(endTxt + '/1')); $("#quotation_range").flatpickr({ mode: 'range', onChange: function (selectedDates, dateStr, instance) { if (selectedDates.length == 2) { var dateStart = instance.formatDate(selectedDates[0], "Y/m/d"); var dateEnd = instance.formatDate(selectedDates[1], "Y/m/d"); $('#quotation_expStart').val(dateStart); $('#quotation_expEnd').val(dateEnd); // interact with selected dates here } } }) $('#projectSaveBtn').on('click', function () { var company_uid = $('#company_select').val(); var project_name = $('#project_name').val(); var project_ps = $('#project_ps').val(); var project_uid = $('#project_uid').val(); var method = $('#project_method').val(); var dept_uid = $('#dept_select').val(); var msg = ''; if (company_uid == "") { msg += '請選擇客戶公司!\n'; } if (project_name == '') { msg += '請輸入專案名稱!\n'; } if (msg != '') { alert(msg); return; } var formData = { method: method, project_uid: project_uid, project_name: project_name, company_uid: company_uid, project_ps: project_ps, dept_uid: dept_uid } $.ajax({ url: "/Api/addEditDelGetProject", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { var obj = data.projectViews[0]; if (method == "add") { projectTable.fnAddData(obj); } if (method == "edit") { projectTable.fnUpdate(obj, projectRowPos); } $('#clientProjectModal').modal('toggle'); } else { alert(data.message); if (data.err_code == "99999") { location.href = "/Root/Login"; } } }, error: function (jqXHR, textStatus, errorThrown) { alert('網路或伺服器發生錯誤,請稍後重試!'); } }); }); //新增報價單 $('#QuotationListNewBtn').on('click', function () { deledMainItems = []; deledSubItems = []; deledPayments = []; deledInvoices = []; $('#modelItem_div').html(''); $('#payment_group').find('ol').html(''); $('#invoice_group').find('ol').html(''); quotation_total = 0; $('#quotation_editType_div').show(); $('#quotation_sa').val(service_text()); $('#quotation_method').val('add'); $('#modelQuotationDialogSaveBtn').show(); $('#clientModelQuotationModal').modal("toggle"); }); $('#invoice_type').on('change', function () { if ($('#invoice_type').val() == "date") { $('#invoice_type_date').show(); $('#invoice_type_text').hide(); } else { $('#invoice_type_date').hide(); $('#invoice_type_text').show(); } }); $('#dept_select').on('change', function () { $('#dt-responsive').DataTable().ajax.reload(); }); $('#search_btn').on('click', function () { $('#dt-responsive').DataTable().ajax.reload(); }); $('#projectNewBtn').on('click', function () { $('#project_method').val('add'); $('#clientProjectModal').modal("toggle"); }); $("#modelProject_select").on('change', function () { var modelProj_uid = $("#modelProject_select").val(); $("#modelQuotation_select").empty().append(''); if (modelProj_uid != '') { var formData = { dept_uid: $('#dept_select').val(), modelProj_uid: modelProj_uid } $.ajax({ url: "/Api/modalQuotationList", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { var obj = data.modelQuotations; $.each(obj, function (i, item) { $("#modelQuotation_select").append($("").attr("value", tmpY).text(tmpY + " 年度")); } for (tmpM = 1; tmpM <= 12; tmpM++) { $("#project_month").append($("").attr("value", tmpM).text(tmpM + " 月")); } } //優惠價格變更 $('#quotation_specTotal').on('change', function () { var specTotal = $('#quotation_specTotal').val(); if (specTotal.isNumber() == false) { alert('請輸入正確的數字!(不能有符號)'); $('#quotation_specTotal').val(''); } else { updateGrandTotal(); } }); //新增主項目鈕 $('#addModelMainItemBtn').on('click', function () { $('#modelMainItem_name').typeahead('destroy'); loadMainItemTypeahead(); $('#modelMainItem_method').val('add'); $('#modelMainItemModal').modal('toggle'); }); //新增次項目對話框儲存鈕 $('#modelSubItemDialogSaveBtn').on('click', function () { var method = $('#modelSubItem_method').val(); var subItem_uid = $('#modelSubItem_uid').val(); var modelSubItem_uid = $('#parent_Item_uid').val(); var modelSubItem_name = $('#modelSubItem_name').val(); var modelSubItem_descript = $('#modelSubItem_descript').val(); var modelSubItem_price = $('#modelSubItem_price').val(); var modelSubItem_unitType = $('#modelSubItem_unitType').val(); var modelSubItem_number = $('#modelSubItem_number').val(); var modelSubItem_hasAC = 'N'; var parent_data_uid = $('#model_parent_data_uid').val(); if ($('#modelSubItem_hasAC').prop("checked")) { modelSubItem_hasAC = 'Y'; } else { modelSubItem_hasAC = 'N'; } var msg = ''; if (modelSubItem_price == '') { msg += '請輸入單價!\n'; } else { if (modelSubItem_price.isNumber() == false) { msg += '單價必須為數字!\n'; } } if (modelSubItem_number == '') { msg += '請輸入數量!\n'; } else { if (modelSubItem_number.isNumber() == false) { msg += '數量必須為數字!\n'; } } if (msg != '') { alert(msg); return; } var modelSubItem_subTotal = modelSubItem_price * modelSubItem_number; if (method == 'edit') { var datauid = $('#modelSubItem_uid').val(); var htmlObj = $('#modelItem_div').find('[data-uid="' + parent_data_uid + '"]').find('ol').find('[data-model-subitem-uid="' + datauid + '"]'); $(htmlObj).find('[data-name="subItem_uid"]').val(subItem_uid); $(htmlObj).find('[data-name="quotationSubItem_uid"]').val(modelSubItem_uid); $(htmlObj).find('[data-name="quotationSubItem_name"]').val(modelSubItem_name); $(htmlObj).find('[data-name="quotationSubItem_price"]').val(modelSubItem_price); $(htmlObj).find('[data-name="quotationSubItem_unitType"]').val(modelSubItem_unitType); $(htmlObj).find('[data-name="quotationSubItem_number"]').val(modelSubItem_number); $(htmlObj).find('[data-name="quotationSubItem_hasAC"]').val(modelSubItem_hasAC); $(htmlObj).find('[data-name="quotationSubItem_subTotal"]').val(modelSubItem_subTotal); $(htmlObj).find('[data-name="quotationSubItem_descript"]').val(modelSubItem_descript); $(htmlObj).children().children().find('[data-name="subname"]').text(modelSubItem_name); $(htmlObj).children().children().find('[data-name="subdesc"]').text(modelSubItem_descript); var detailData = ''; detailData += '單價 NT$' + AppendComma(modelSubItem_price) + ', 數量 ' + modelSubItem_number + ', 單位 ' + modelSubItem_unitType + '\r\n'; detailData += '小計 NT$' + AppendComma(modelSubItem_subTotal) + '\r\n'; detailData += 'AC(' + modelSubItem_hasAC + ')'; $(htmlObj).children().children().find('[data-name="subsummy"]').text(detailData); $('#modelSubItemModal').modal('toggle'); } if (method == 'add') { var datauid = Math.floor(Math.random() * 999999999); var htmlCode = ''; htmlCode += '
  • '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += ' '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += '

    '; htmlCode += ' ' + modelSubItem_name; htmlCode += '


    '; htmlCode += '

    '; htmlCode += ' ' + modelSubItem_descript; htmlCode += '


    '; htmlCode += '

    '; htmlCode += ' 單價 NT$' + AppendComma(modelSubItem_price) + ', 數量 ' + modelSubItem_number + ', 單位 ' + modelSubItem_unitType + '\r\n'; htmlCode += ' 小計 NT$' + AppendComma(modelSubItem_subTotal) + '\r\n'; htmlCode += ' AC(' + modelSubItem_hasAC + ')'; htmlCode += '

    '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += ' '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += '
  • '; var htmlObj = $('#modelItem_div').find('[data-uid="' + parent_data_uid + '"]').find('ol'); //var htmlObj = $('#' + parent_data_uid).find('ol'); htmlObj.append(htmlCode); var li_count = 0; htmlObj.find('li').each(function (i, item) { li_count++; }); if (li_count == 1) { $('#nest_' + parent_data_uid).nestable(); } $('#modelSubItemModal').modal('toggle'); } updateGrandTotal(); }); //新增主項目對話框儲存鈕 $('#modelMainItemDialogSaveBtn').on('click', function () { var modelMainItem_uid = $('#modelMainItem_uid').val(); var mainItem_uid = $('#parent_mainItem_uid').val(); var method = $('#modelMainItem_method').val(); var modelMainItem_name = $('#modelMainItem_name').val(); var modelMainItem_ac = $('#modelMainItem_ac').val(); if (modelMainItem_ac.isNumber() == false) { alert('AC必須為數字!'); return; } if (modelMainItem_name == "") { alert('主項目名稱不得為空白!'); return; } var ac_string = ""; if (modelMainItem_ac == 0) { ac_string = "無"; } else { ac_string = modelMainItem_ac + '%'; } if (method == 'edit') { var obj = $('#modelItem_div').find("[data-uid='" + modelMainItem_uid + "']"); $(obj).prop('data-main-uid', mainItem_uid); $(obj).find("[data-name='quotationMainItem_name']").val(modelMainItem_name); $(obj).find("[data-span='quotationMainItem_name']").text(modelMainItem_name); $(obj).find("[data-name='quotationMainItem_ac']").val(modelMainItem_ac); $('#modelMainItemModal').modal('toggle'); } if (method == 'add') { var datauid = Math.floor(Math.random() * 99999999); var htmlCode = ''; htmlCode += ''; htmlCode += '
    '; htmlCode += '
    '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' ' + modelMainItem_name + ''; htmlCode += ' AC: ' + ac_string + ' '; htmlCode += ' 項目合計: 0'; htmlCode += '
    '; htmlCode += ' '; htmlCode += '
    '; htmlCode += ' '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += ' '; htmlCode += '
      '; htmlCode += ' '; htmlCode += '
    '; htmlCode += '
    '; htmlCode += ' '; htmlCode += ' '; htmlCode += '
    '; $('#modelItem_div').append(htmlCode); $('#modelMainItemModal').modal('toggle'); } updateGrandTotal(); }); //新增付款方式按鈕 $('#addPaymentBtn').on('click', function () { $('#payment_method').val('add'); $('#paymentModal').modal('toggle'); }); //付款方式下拉 $('#paymentMethod_select').on('change', function () { var payment_method = $('#paymentMethod_select').val(); switch (payment_method) { case 'cheque': $('#payment_descript').val('票期 天').trigger('change'); break; case 'telegraphic': $('#payment_descript').val('入帳日 年 月 日').trigger('change'); break; case 'paycash': $('#payment_descript').val('').trigger('change'); break; case 'downpayment': $('#payment_descript').val('').trigger('change'); break; case 'custom': $('#payment_descript').val('').trigger('change'); break; default: console.log(`Sorry, we are out of ${expr}.`); } }); //付款方式儲存鈕 $('#paymentModalDialogSaveBtn').on('click', function () { var data_method = $('#payment_method').val(); var payment_method = $('#paymentMethod_select').val(); var payment_methodname = $("#paymentMethod_select option:selected").text(); var payment_descript = $('#payment_descript').val(); if (payment_method != 'paycash' && payment_method != 'downpayment') { if (payment_descript == '') { alert('請輸入付款資訊!'); return; } } var payment_display = payment_methodname; if (payment_descript != '') { payment_display = payment_display + ': ' + payment_descript; } if (data_method == 'add') { var htmlCode = ''; htmlCode += '
  • \n'; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' \n'; htmlCode += '
    ' + payment_display + '
    \n'; htmlCode += '
    \n'; htmlCode += ' '; htmlCode += ' \n'; htmlCode += '
    \n'; htmlCode += '
  • \n'; $('#payment_group').find('ol').append(htmlCode); } else { tmpPaymentItem.find("[data-name='payment_method']").val(payment_method); tmpPaymentItem.find("[data-name='payment_methodname']").val(payment_methodname); tmpPaymentItem.find("[data-name='payment_descript']").val(payment_descript); tmpPaymentItem.find("[data-name='payment_display']").text(payment_display); } $('#paymentModal').modal('toggle'); }); //新增發票項目 $('#addInvoiceBtn').on('click', function () { $('#invoice_name').typeahead('destroy'); initInvoiceSelectItem(); fpInvoiceDate.setDate(new Date(endTxt + '/1')); $('#invoice_type').val('date').trigger('change'); //$('#invoice_type_text').hide(); $('#invoice_method').val('add'); $('#invoiceModal').modal('toggle'); }); //儲存發票項目 $('#invoiceModalDialogSaveBtn').on('click', function () { var invoice_method = $('#invoice_method').val(); var invoice_name = $("#invoice_name").val(); var invoice_date = $('#invoice_date').val(); var invoice_type = $('#invoice_type').val(); var invoice_text = $('#invoice_text').val(); var invoice_noTaxMoney = $('#invoice_noTaxMoney').val().replace(/,/g, ""); if (invoice_noTaxMoney.isNumber() == false || invoice_noTaxMoney == '') { alert('發票金額得為純數字!'); return; } if (invoice_name == '') { alert('請輸入品項名稱!'); return; } if (invoice_type == 'date') { if (invoice_date == '') { alert('請輸入發票月份!'); return; } } if (invoice_type == 'text') { if (invoice_text == '') { alert('請輸入發票開立時間說明!'); return; } } if (invoice_method == 'add') { var invoice_display = "開立品項: " + invoice_name + ", 開立月份: " + invoice_date + ", 金額(未稅): " + AppendComma(invoice_noTaxMoney); if (invoice_type == 'text') { invoice_display = "開立品項: " + invoice_name + ", 開立月份: " + invoice_text + ", 金額(未稅): " + AppendComma(invoice_noTaxMoney); } var htmlCode = ''; htmlCode += '
  • \n'; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' '; htmlCode += ' \n'; htmlCode += ' ' + "開立品項: " + invoice_name + ' \n'; if (invoice_type == 'date') { htmlCode += ' ' + "開立月份: " + invoice_date + ' \n'; } else { htmlCode += ' ' + "開立月份: " + invoice_text + ' \n'; } htmlCode += ' ' + "金額(未稅): " + AppendComma(invoice_noTaxMoney) + ' \n'; htmlCode += ' '; htmlCode += '
    \n'; htmlCode += ' '; htmlCode += ' \n'; htmlCode += '
    \n'; htmlCode += '
  • \n'; $('#invoice_group').find('ol').append(htmlCode); } if (invoice_method == 'edit') { var invoice_display = "開立品項: " + invoice_name + ", 開立月份: " + invoice_date + ", 金額(未稅): " + AppendComma(invoice_noTaxMoney); if (invoice_type == 'text') { invoice_display = "開立品項: " + invoice_name + ", 開立月份: " + invoice_text + ", 金額(未稅): " + AppendComma(invoice_noTaxMoney); } tmpInvoiceItem.find("[data-name='invoice_name']").val(invoice_name); tmpInvoiceItem.find("[data-name='invoice_type']").val(invoice_type); tmpInvoiceItem.find("[data-name='invoice_date']").val(invoice_date); tmpInvoiceItem.find("[data-name='invoice_text']").val(invoice_text); tmpInvoiceItem.find("[data-name='invoice_noTaxMoney']").val(invoice_noTaxMoney); tmpInvoiceItem.find("[data-name='invoice_name_span']").text(invoice_name); if (invoice_type == 'date') { tmpInvoiceItem.find("[data-name='invoice_date_span']").text('開立月份: ' + invoice_date); } else { tmpInvoiceItem.find("[data-name='invoice_date_span']").text('開立月份: ' + invoice_text); } tmpInvoiceItem.find("[data-name='invoice_noTaxMoney_span']").text('金額(未稅): ' + AppendComma(invoice_noTaxMoney)); } $('#invoiceModal').modal('toggle'); }); //報價單儲存 $('#modelQuotationDialogSaveBtn').on('click', function () { updateGrandTotal(); var quotation_method = $('#quotation_method').val(); var quotation_uid = $('#quotation_uid').val(); var quotation_version = $('#quotation_version').val(); var quotation_prodMethod = ''; var quotation_date = $('#quotation_date').val(); var quotation_date_old = $('#quotation_date_old').val(); var quotation_expStart = $('#quotation_expStart').val(); var quotation_expEnd = $('#quotation_expEnd').val(); var quotation_expStart_old = $('#quotation_expStart_old').val(); var quotation_expEnd_old = $('#quotation_expEnd_old').val(); var quotation_name = $('#quotation_name').val(); var quotation_name_old = $('#quotation_name_old').val(); var contactPerson_uid = $('#contactPerson_uid').val(); var contactPerson_uid_old = $('#contactPerson_uid_old').val(); var company_uid = $('#quotation_company_uid').val(); var quotation_noTaxTotal = $('#quotation_noTaxTotal').val(); var quotation_noTaxTotal_old = $('#quotation_noTaxTotal_old').val(); var quotation_specTotal = $('#quotation_specTotal').val(); var quotation_specTotal_old = $('#quotation_specTotal_old').val(); var quotation_tax = $('#quotation_tax').val(); var quotation_tax_old = $('#quotation_tax_old').val(); var quotation_grandTotal = $('#quotation_grandTotal').val(); var quotation_grandTotal_old = $('#quotation_grandTotal_old').val(); var quotation_sa = $('#quotation_sa').val(); var quotation_sa_old = $('#quotation_sa_old').val(); var err_msg = ''; if (quotation_date == '') { err_msg += '請輸入報價日期!\n'; } if (quotation_name == '') { err_msg += '請輸入報價單名稱!\n'; } if (quotation_expStart == '' || quotation_expEnd == '') { err_msg += '請輸入報價有效起訖日期!\n'; } if (err_msg != '') { alert(err_msg); return; } var quotation_log = ''; if ($('#quotation_custom').prop('checked') == true) { quotation_prodMethod = 'custom'; } if ($('#quotation_temp').prop('checked') == true) { quotation_prodMethod = 'template'; } if (quotation_method == 'edit') { if (quotation_date != quotation_date_old) { quotation_log += '報價日期由 ' + quotation_date_old + ' 變更為 ' + quotation_date + '\n'; } if (quotation_expStart != quotation_expStart_old || quotation_expEnd != quotation_expEnd_old) { quotation_log += '報價有效期由 ' + quotation_expStart_old + '至' + quotation_expEnd_old + ' 變更為 ' + quotation_expStart + '至' + quotation_expEnd + '\n'; } if (quotation_name != quotation_name_old) { quotation_log += '報價單名稱由 ' + quotation_name_old + ' 變更為 ' + quotation_name + '\n'; } if (contactPerson_uid != contactPerson_uid_old) { quotation_log += '客戶窗口人選變更\n'; } if (quotation_noTaxTotal != quotation_noTaxTotal_old) { quotation_log += '專案總價由 ' + AppendComma(quotation_noTaxTotal_old) + ' 變更為 ' + AppendComma(quotation_noTaxTotal) + '\n'; } if (quotation_specTotal != quotation_specTotal_old) { quotation_log += '專案優惠總價由 ' + AppendComma(quotation_specTotal_old) + ' 變更為 ' + AppendComma(quotation_specTotal) + '\n'; } if (quotation_tax != quotation_tax_old) { quotation_log += '加值營業稅由 ' + AppendComma(quotation_tax_old) + ' 變更為 ' + AppendComma(quotation_tax) + '\n'; } if (quotation_grandTotal != quotation_grandTotal_old) { quotation_log += '實際總價由 ' + AppendComma(quotation_grandTotal_old) + ' 變更為 ' + AppendComma(quotation_grandTotal) + '\n'; } if (quotation_sa != quotation_sa_old) { quotation_log += '製作及維護服務協議條款內容有所變更\n'; } $.each(deledMainItems, function (index, value) { quotation_log += value.mainItem_msg + '\n'; }); $.each(deledSubItems, function (index, value) { quotation_log += value.subItem_msg + '\n'; }); $.each(deledPayments, function (index, value) { quotation_log += value.message + '\n'; }); $.each(deledInvoices, function (index, value) { quotation_log += value.message + '\n'; }); } var mainItems = []; //報價單項目 $('#modelItem_div .card').each(function (index, obj) { var data_type = $(obj).attr('data-type'); var mainItem_uid = $(obj).attr('data-main-uid'); var mainItem_uid_old = $(obj).find('[data-name="mainItem_uid_old"]').val(); var quotationMainItem_uid = $(obj).attr('data-uid'); var quotationMainItem_name = $(obj).find('[data-name="quotationMainItem_name"]').val(); var quotationMainItem_name_old = $(obj).find('[data-name="quotationMainItem_name_old"]').val(); var quotationMainItem_ac = $(obj).find('[data-name="quotationMainItem_ac"]').val(); var quotationMainItem_ac_old = $(obj).find('[data-name="quotationMainItem_ac_old"]').val(); var quotationMainItem_subTotal = $(obj).find('[data-name="quotationMainItem_subTotal"]').val(); var quotationMainItem_subTotal_old = $(obj).find('[data-name="quotationMainItem_subTotal_old"]').val(); if (quotation_method == 'edit') { if (data_type == 'add') { quotation_log += '新增報價主項目 - ' + quotationMainItem_name + '\n'; } if (data_type == 'edit') { if (quotationMainItem_name != quotationMainItem_name_old) { quotation_log += '報價主項目名稱由 [' + quotationMainItem_name_old + '] 更改為 [' + quotationMainItem_name + '] \n'; } if (quotationMainItem_ac != quotationMainItem_ac_old) { quotation_log += '報價主項目 [' + quotationMainItem_name + '] 的服務費比例由 [' + quotationMainItem_ac_old + '%] 更改為 [' + quotationMainItem_ac + '%] \n'; } } } var subitems = []; $(obj).find('ol li').each(function (i, item) { var data_method = $(item).attr('data-method'); var quotationSubItem_uid = $(item).attr('data-model-subitem-uid'); var subItem_uid = $(item).find('[data-name="subItem_uid"]').val(); var quotationSubItem_name = $(item).find('[data-name="quotationSubItem_name"]').val(); var quotationSubItem_price = $(item).find('[data-name="quotationSubItem_price"]').val(); var quotationSubItem_hasAC = $(item).find('[data-name="quotationSubItem_hasAC"]').val(); var quotationSubItem_unitType = $(item).find('[data-name="quotationSubItem_unitType"]').val(); var quotationSubItem_number = $(item).find('[data-name="quotationSubItem_number"]').val(); var quotationSubItem_subTotal = $(item).find('[data-name="quotationSubItem_subTotal"]').val(); var quotationSubItem_descript = $(item).find('[data-name="quotationSubItem_descript"]').val(); var subItem_uid_old = $(item).find('[data-name="subItem_uid_old"]').val(); var quotationSubItem_name_old = $(item).find('[data-name="quotationSubItem_name_old"]').val(); var quotationSubItem_price_old = $(item).find('[data-name="quotationSubItem_price_old"]').val(); var quotationSubItem_hasAC_old = $(item).find('[data-name="quotationSubItem_hasAC_old"]').val(); var quotationSubItem_unitType_old = $(item).find('[data-name="quotationSubItem_unitType_old"]').val(); var quotationSubItem_number_old = $(item).find('[data-name="quotationSubItem_number_old"]').val(); var quotationSubItem_subTotal_old = $(item).find('[data-name="quotationSubItem_subTotal_old"]').val(); var quotationSubItem_descript_old = $(item).find('[data-name="quotationSubItem_descript_old"]').val(); var subItemFormData = { quotationSubItem_uid: quotationSubItem_uid, subItem_uid: subItem_uid, quotationSubItem_name: quotationSubItem_name, quotationSubItem_price: quotationSubItem_price, quotationSubItem_unitType: quotationSubItem_unitType, quotationSubItem_number: quotationSubItem_number, quotationSubItem_hasAC: quotationSubItem_hasAC, quotationSubItem_subTotal: quotationSubItem_subTotal, quotationSubItem_descript: quotationSubItem_descript } subitems.push(subItemFormData); if (data_method == 'add' && quotation_method == 'edit') { if (quotationSubItem_name != quotationSubItem_name_old) { quotation_log += '報價主項目 [' + quotationMainItem_name + '] 中新增子項目 [' + quotationSubItem_name + '] \n'; } } if (data_method == 'edit') { if (quotationSubItem_name != quotationSubItem_name_old) { quotation_log += '報價主項目 [' + quotationMainItem_name + '] 的子項目名稱由 [' + quotationSubItem_name_old + '] 更改為 [' + quotationSubItem_name + '] \n'; } if (quotationSubItem_price != quotationSubItem_price_old) { quotation_log += '報價主項目 [' + quotationMainItem_name + '] 的子項目 [' + quotationSubItem_name + '] 的單價由 [' + AppendComma(quotationSubItem_price_old) + '] 更改為 [' + AppendComma(quotationSubItem_price) + '] \n'; } if (quotationSubItem_unitType != quotationSubItem_unitType_old) { quotation_log += '報價主項目 [' + quotationMainItem_name + '] 的子項目 [' + quotationSubItem_name + '] 的計價單位由 [' + quotationSubItem_unitType_old + '] 更改為 [' + quotationSubItem_unitType + '] \n'; } if (quotationSubItem_number != quotationSubItem_number_old) { quotation_log += '報價主項目 [' + quotationMainItem_name + '] 的子項目 [' + quotationSubItem_name + '] 的數量由 [' + AppendComma(quotationSubItem_number_old) + '] 更改為 [' + AppendComma(quotationSubItem_number) + '] \n'; } if (quotationSubItem_subTotal != quotationSubItem_subTotal_old) { quotation_log += '報價主項目 [' + quotationMainItem_name + '] 的子項目 [' + quotationSubItem_name + '] 的價格由 [' + AppendComma(quotationSubItem_subTotal_old) + '] 更改為 [' + AppendComma(quotationSubItem_subTotal) + '] \n'; } if (quotationSubItem_descript != quotationSubItem_descript_old) { quotation_log += '報價主項目 [' + quotationMainItem_name + '] 的子項目 [' + quotationSubItem_name + '] 的細項敘述有異動 \n'; } if (quotationSubItem_hasAC != quotationSubItem_hasAC_old) { quotation_log += '報價主項目 [' + quotationMainItem_name + '] 的子項目 [' + quotationSubItem_name + '] 的收取AC由 [' + quotationSubItem_hasAC_old + '] 改為 [' + quotationSubItem_hasAC + '] \n'; } } }); var mainItemFormData = { mainItem_uid: mainItem_uid, quotationMainItem_uid: quotationMainItem_uid, quotationMainItem_name: quotationMainItem_name, quotationMainItem_ac: quotationMainItem_ac, quotationMainItem_subTotal: quotationMainItem_subTotal, subitems: subitems } mainItems.push(mainItemFormData); }); var payments = []; $('#payment_div').find('ol li').each(function (i, item) { var payment_method = $(item).find('[data-name="payment_method"]').val(); var payment_method_old = $(item).find('[data-name="payment_method_old"]').val(); var payment_methodname = $(item).find('[data-name="payment_methodname"]').val(); var payment_methodname_old = $(item).find('[data-name="payment_methodname_old"]').val(); var payment_descript = $(item).find('[data-name="payment_descript"]').val(); var payment_descript_old = $(item).find('[data-name="payment_descript_old"]').val(); var data_method = $(item).find('[data-name="data_method"]').val(); if (quotation_method == 'edit' && data_method == 'add') { quotation_log += '付款方式增加了 [' + payment_methodname + ' - ' + payment_descript + ']\n'; } if (quotation_method == 'edit' && data_method == 'edit') { if (payment_method != payment_method_old) { quotation_log += '付款方式由 [' + payment_methodname_old + '] 更改為 [' + payment_methodname + ']\n'; } if (payment_descript != payment_descript_old) { quotation_log += '付款方式說明由 [' + payment_descript_old + '] 更改為 [' + payment_descript + ']\n'; } } var paymentFormData = { payment_method: payment_method, payment_methodname: payment_methodname, payment_descript: payment_descript } payments.push(paymentFormData); }); var invoices = []; $('#invoice_div').find('ol li').each(function (i, item) { var invoice_name = $(item).find('[data-name="invoice_name"]').val(); var invoice_name_old = $(item).find('[data-name="invoice_name_old"]').val(); var invoice_type = $(item).find('[data-name="invoice_type"]').val(); var invoice_type_old = $(item).find('[data-name="invoice_type_old"]').val(); var invoice_date = $(item).find('[data-name="invoice_date"]').val(); var invoice_date_old = $(item).find('[data-name="invoice_date_old"]').val(); var invoice_text = $(item).find('[data-name="invoice_text"]').val(); var invoice_text_old = $(item).find('[data-name="invoice_text_old"]').val(); var invoice_noTaxMoney = $(item).find('[data-name="invoice_noTaxMoney"]').val(); var invoice_noTaxMoney_old = $(item).find('[data-name="invoice_noTaxMoney_old"]').val(); var data_method = $(item).find('[data-name="data_method"]').val(); if (quotation_method == 'edit' && data_method == 'add') { quotation_log += '發票品項增加了 [' + invoice_name + ' (' + invoice_date + invoice_text + ') ' + AppendComma(invoice_noTaxMoney) + ']\n'; } if (quotation_method == 'edit' && data_method == 'edit') { if (invoice_name != invoice_name_old) { quotation_log += '發票品項由 [' + invoice_name_old +'] 更改為 [' + invoice_name + ']\n'; } if (invoice_type != invoice_type_old) { quotation_log += '發票月份樣式由 [' + invoice_type_old + '] 更改為 [' + invoice_type + ']\n'; } if (invoice_date != invoice_date_old) { quotation_log += '發票月份(date)由 [' + invoice_date_old + '] 更改為 [' + invoice_date + ']\n'; } if (invoice_text != invoice_text_old) { quotation_log += '發票月份(text)由 [' + invoice_text_old + '] 更改為 [' + invoice_text + ']\n'; } if (invoice_noTaxMoney != invoice_noTaxMoney_old) { quotation_log += '發票未稅金額由 [' + invoice_noTaxMoney_old + '] 更改為 [' + invoice_noTaxMoney + ']\n'; } } var invoiceFormData = { invoice_name: invoice_name, invoice_type: invoice_type, invoice_date: invoice_date, invoice_text: invoice_text, invoice_noTaxMoney: invoice_noTaxMoney } invoices.push(invoiceFormData); }); var formData = { method: quotation_method, project_uid: $('#quotation_project_uid').val(), dept_uid: $('#dept_select').val(), quotation_uid: quotation_uid, quotation_version: quotation_version, quotation_prodMethod: quotation_prodMethod, quotation_date: quotation_date, quotation_expStart: quotation_expStart, quotation_expEnd: quotation_expEnd, quotation_name: quotation_name, contactPerson_uid: contactPerson_uid, company_uid: company_uid, quotation_log: quotation_log, quotation_noTaxTotal: quotation_noTaxTotal, quotation_specTotal: quotation_specTotal, quotation_tax: quotation_tax, quotation_grandTotal: quotation_grandTotal, quotation_sa: quotation_sa, mainItems: JSON.stringify(mainItems), payments: JSON.stringify(payments), invoices: JSON.stringify(invoices) } if (quotation_log == '' && quotation_method == 'edit') { alert('資料沒有異動,取消儲存!'); return; } $.ajax({ url: "/Api/addEditDelQuotation", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { var obj = data.quotationView; if (quotation_method == "add") { quotationTable.fnAddData(obj); } if (quotation_method == "edit") { quotationTable.fnUpdate(obj, quotationRowPos); } $('#clientModelQuotationModal').modal('toggle'); } else { alert(data.message); if (data.err_code == "99999") { location.href = "/Root/Login"; } } }, error: function (jqXHR, textStatus, errorThrown) { alert('網路或伺服器發生錯誤,請稍後重試!'); } }); }); //另存報價單儲存鈕 $('#save_to_same_SaveBtn').on('click', function () { var quotation_uid = $('#save_to_same_quotation_uid').val(); var quotation_version = $('#save_to_same_quotation_version').val(); var quotation_name = $('#save_to_same_quotation_name').val(); if (quotation_name == '') { alert('請輸入新的報價單名稱!'); return; } var formData = { quotation_uid: quotation_uid, quotation_version: quotation_version, quotation_name: quotation_name, method: 'save_to_same' } $.ajax({ url: "/Api/saveas", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { obj = data.quotationView; quotationTable.fnAddData(obj); alert('另存至本專案為新報價單完成!'); $('#clientSaveToSameModal').modal('toggle'); $('#clientHistoryListModal').modal('toggle'); } else { alert(data.message); if (data.err_code == "99999") { location.href = "/Root/Login"; } } }, error: function (jqXHR, textStatus, errorThrown) { alert('網路或伺服器發生錯誤,請稍後重試!'); } }); }); //另存至其他專案 $('#save_to_other_SaveBtn').on('click', function () { var quotation_uid = $('#save_to_other_quotation_uid').val(); var quotation_version = $('#save_to_other_quotation_version').val(); var quotation_name = $('#save_to_other_quotation_name').val(); var project_value = $('#project_select').val(); if (project_value == '') { alert('請選擇要加入的專案'); return; } var arr = project_value.split('/'); var project_uid = arr[0]; var company_uid = arr[1]; var contactPerson_uid = $('#contactPerson_select').val(); if (quotation_name == '') { alert('請輸入新的報價單名稱!'); return; } if (contactPerson_uid == '') { alert('請選擇窗口!'); return; } var formData = { quotation_uid: quotation_uid, quotation_version: quotation_version, quotation_name: quotation_name, project_uid: project_uid, company_uid: company_uid, contactPerson_uid: contactPerson_uid, method: 'save_to_other' } $.ajax({ url: "/Api/saveas", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { obj = data.quotationView; if (obj.project_uid == $('#quotation_project_uid').val()) { quotationTable.fnAddData(obj); } alert('另存至其他專案完成!'); $('#clientSaveToOtherModal').modal('toggle'); $('#clientHistoryListModal').modal('toggle'); } else { alert(data.message); if (data.err_code == "99999") { location.href = "/Root/Login"; } } }, error: function (jqXHR, textStatus, errorThrown) { alert('網路或伺服器發生錯誤,請稍後重試!'); } }); }); //另存至新專案 $('#save_to_new_SaveBtn').on('click', function () { var quotation_uid = $('#save_to_new_quotation_uid').val(); var quotation_version = $('#save_to_new_quotation_version').val(); var quotation_name = $('#save_to_new_quotation_name').val(); var project_name = $('#save_to_new_project_name').val(); var company_uid = $('#company_new_select').val(); var contactPerson_uid = $('#contactPerson_new_select').val(); if (project_name == '') { alert('請輸入新專案名稱!'); return; } if (quotation_name == '') { alert('請輸入新報價單名稱!'); return; } if (company_uid == '') { aler('請選擇客戶公司!'); return; } if (contactPerson_uid == '') { alert('請選擇窗口!'); return; } var formData = { quotation_uid: quotation_uid, quotation_version: quotation_version, quotation_name: quotation_name, project_name: project_name, company_uid: company_uid, contactPerson_uid: contactPerson_uid, method: 'save_to_new' } $.ajax({ url: "/Api/saveas", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { obj = data.projectView; projectTable.fnAddData(obj); alert('另存至新專案完成!'); $('#clientSaveToNewModal').modal('toggle'); $('#clientHistoryListModal').modal('toggle'); } else { alert(data.message); if (data.err_code == "99999") { location.href = "/Root/Login"; } } }, error: function (jqXHR, textStatus, errorThrown) { alert('網路或伺服器發生錯誤,請稍後重試!'); } }); }); //下拉專案列表選擇 $('#project_select').on('change', function () { var project_value = $('#project_select').val(); if (project_value == '') { $('#contactPerson_select').empty(); return; } var arr = project_value.split('/'); var project_uid = arr[0]; var company_uid = arr[1]; var formData = { company_uid: company_uid } $.ajax({ url: "/Api/contactPersonList", type: "post", data: formData, success: function (data, textStatus, jqXHR) { if (data.ret == "yes") { var obj = data.contactPersons; $("#contactPerson_select").empty(); $.each(obj, function (i, item) { $("#contactPerson_select").append($("'); $("#modelQuotation_select").empty().append(''); $.each(obj, function (i, item) { $("#modelProject_select").append($("'); $.each(obj, function (i, item) { $("#project_select").append($("