var projectTable;
var projectRowID;
var projectPos;
var quotationTable;
var quotationRowID;
var quotationPos;
var quotation_total;
var deledMainItems = [];
var deledSubItems = [];
var preSubItem_name = '';
var hasBind = 'N';
$(document).ready(function () {
deptList();
companyList();
loadyearmonth();
loadQuotationTable();
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"
})
]
});
fpStart.setDate(new Date(startTxt + '/1'))
fpEnd.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 () {
quotation_total = 0;
$('#quotation_sa').val(service_text());
$('#quotation_method').val('add');
$('#clientModelQuotationModal').modal("toggle");
});
$('#dept_select').on('change', 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);
$('#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 += '
';
$('#modelItem_div').append(htmlCode);
$('#modelMainItemModal').modal('toggle');
}
updateGrandTotal();
});
});
function loadContactPersion() {
var data = $('#dt-responsive').DataTable().row(projectRowPos).data();
var uid = $('#quotation_company_uid').val();
var formData = {
company_uid: uid
}
$.ajax({
url: "/Api/contactPersonList",
type: "post",
data: formData,
success: function (data, textStatus, jqXHR) {
if (data.ret == "yes") {
var obj = data.contactPersons;
$("#contactPerson_uid").empty();
$.each(obj, function (i, item) {
$("#contactPerson_uid").append($("');
$("#modelQuotation_select").empty().append('');
$.each(obj, function (i, item) {
$("#modelProject_select").append($("