'use strict';
var sProgramId = getProgramId(),
sQueryPrgId = getQueryPrgId(),
sViewPrgId = sProgramId.replace('_Upd', '_View'),
sAction = getUrlParam('Action') || 'Add',
sDataId = getUrlParam('Guid'),
sCheckId = sDataId,
fnPageInit = function () {
var oCurData = { CheckOrder: [] },
oForm = $('#form_main'),
oGrid = null,
oValidator = null,
oTR = null,
oAddItem = {},
saUsers = [],
fnGetBillChangeInfo = function () {
var saList = [];
$('.billchangeinfo tr.data-item').each(function (idx) {
saList.push({
OldItem: $(this).find('[data-id="OldItem"]').val(),
OldAmountCurrency: $(this).find('[data-id="OldAmountCurrency"]').val(),
OldAmount: $(this).find('[data-id="OldAmount"]').val(),
NewItem: $(this).find('[data-id="NewItem"]').val(),
NewAmountCurrency: $(this).find('[data-id="NewAmountCurrency"]').val(),
NewAmount: $(this).find('[data-id="NewAmount"]').val()
});
});
return saList;
},
/**
* 獲取資料
*/
fnGet = function () {
var deferred = $.Deferred();
if (sDataId) {
return g_api.ConnectLite(sQueryPrgId, ComFn.GetOne,
{
Guid: sDataId
},
function (res) {
if (res.RESULT) {
var oRes = res.DATA.rel;
oCurData = oRes;
oCurData.BillChanges = $.parseJSON(oCurData.BillChanges);
oCurData.CheckOrder = $.parseJSON(oCurData.CheckOrder);
setFormVal(oForm, oRes);
$('.Applicant').text(oCurData.ApplicantName + '(' + oCurData.Applicant + ') ' + oCurData.DeptName);
fnGetUploadFiles(oCurData.Guid, fnUpload);
if (oCurData.Handle_DeptID) {
fnSetUserDrop([
{
Select: $('#Handle_Person'),
DepartmentID: oCurData.Handle_DeptID,
ShowId: true,
Select2: true,
Action: sAction,
DefultVal: oCurData.Handle_Person
}
]);
}
if (oCurData.Flows_Lock === 'Y') {
$(".checkordertoolbox").hide();
}
else {
$(".checkordertoolbox").show();
}
if (oCurData.Handle_Lock === 'Y') {
$("#Handle_DeptID,#Handle_Person").attr('disabled', true);
}
else {
$("#Handle_DeptID,#Handle_Person").removeAttr('disabled');
}
$('[class="data-item"]').remove();
$.each(oCurData.BillChanges.ChangeInfo || [], function (idx, item) {
var oTrNew = oTR.clone(true);
oTrNew.find('[data-id="OldItem"]').val(item.OldItem);
oTrNew.find('[data-id="OldAmountCurrency"]').val(item.OldAmountCurrency);
oTrNew.find('[data-id="OldAmount"]').val(item.OldAmount);
oTrNew.find('[data-id="NewItem"]').val(item.NewItem);
oTrNew.find('[data-id="NewAmountCurrency"]').val(item.NewAmountCurrency);
oTrNew.find('[data-id="NewAmount"]').val(item.NewAmount);
$('.billchangeinfo tr:last').before(oTrNew);
});
$("#jsGrid").jsGrid("loadData");
setNameById().done(function () {
getPageVal();//緩存頁面值,用於清除
});
moneyInput($('[data-type="int"]'), 2, true);
}
});
}
else {
$('.Applicant').text(parent.UserInfo.MemberName + '(' + parent.UserInfo.MemberID + ') ' + parent.UserInfo.DepartmentName);
$('#Applicant').val(parent.UserInfo.MemberID);
oCurData.CheckOrder = [];
oCurData.Guid = guid();
fnUpload();
return $.Deferred().resolve().promise();
}
},
/**
* 新增資料
* @param {String} sFlag 新增或儲存後新增
*/
fnAdd = function (flag) {
var data = getFormSerialize(oForm);
data = packParams(data);
data.OrgID = parent.OrgID;
data.Guid = oCurData.Guid;
data.SignedNumber = 'SerialNumber|' + parent.UserInfo.OrgID + '|BCA|MinYear|3|' + parent.UserInfo.ServiceCode + '|' + parent.UserInfo.ServiceCode;
data.CheckFlows = fnCheckFlows(oCurData, false, true, saUsers);
data.HandleFlows = fnHandleFlows(oCurData, saUsers);
data.BillChanges.ChangeInfo = fnGetBillChangeInfo();
data.BillChanges.CustomerName = $('#BillChanges_CustomerID option:selected').text();
data.BillChanges.ExhibitionName = $('#BillChanges_ExhibitionNO option:selected').text();
data.BillChanges.ResponsiblePersonName = $('#BillChanges_ResponsiblePerson option:selected').text();
data.BillChanges = JSON.stringify(data.BillChanges);
data.CheckOrder = JSON.stringify(oCurData.CheckOrder);
data.Status = 'A';
data.IsHandled = 'N';
data.Inspectors = '';
data.Reminders = '';
data.Flows_Lock = oCurData.Flows_Lock;
data.Handle_Lock = oCurData.Handle_Lock;
CallAjax(ComFn.W_Com, ComFn.GetAdd, {
Params: {
billchangeapply: data
}
}, function (res) {
if (res.d > 0) {
bRequestStorage = false;
if (flag == 'add') {
showMsgAndGo(i18next.t("message.Save_Success"), sProgramId, '?Action=Upd&Guid=' + data.Guid); // ╠message.Save_Success⇒新增成功╣
}
else {
showMsgAndGo(i18next.t("message.Save_Success"), sProgramId, '?Action=Add'); // ╠message.Save_Success⇒新增成功╣
}
}
else {
showMsg(i18next.t("message.Save_Failed"), 'error'); // ╠message.Save_Failed⇒新增失敗╣
}
}, function () {
showMsg(i18next.t("message.Save_Failed"), 'error'); // ╠message.Save_Failed⇒新增失敗╣
});
},
/**
* 修改資料
* @param {Boolean} balert 是否提示
*/
fnUpd = function (balert) {
var data = getFormSerialize(oForm);
data = packParams(data, 'upd');
data.CheckFlows = fnCheckFlows(oCurData, false, true, saUsers);
data.HandleFlows = fnHandleFlows(oCurData, saUsers);
data.BillChanges.ChangeInfo = fnGetBillChangeInfo();
data.BillChanges.CustomerName = $('#BillChanges_CustomerID option:selected').text();
data.BillChanges.ExhibitionName = $('#BillChanges_ExhibitionNO option:selected').text();
data.BillChanges.ResponsiblePersonName = $('#BillChanges_ResponsiblePerson option:selected').text();
data.BillChanges = JSON.stringify(data.BillChanges);
data.CheckOrder = JSON.stringify(oCurData.CheckOrder);
data.Flows_Lock = oCurData.Flows_Lock;
data.Handle_Lock = oCurData.Handle_Lock;
return CallAjax(ComFn.W_Com, ComFn.GetUpd, {
Params: {
billchangeapply: {
values: data,
keys: { Guid: sDataId }
}
}
}, function (res) {
if (res.d > 0) {
if (!balert) {
bRequestStorage = false;
showMsg(i18next.t("message.Modify_Success"), 'success'); //╠message.Modify_Success⇒修改成功╣
if (window.bLeavePage) {
setTimeout(function () {
pageLeave();
}, 1000);
}
}
}
else {
showMsg(i18next.t("message.Modify_Failed"), 'error');//╠message.Modify_Failed⇒修改失敗╣
}
}, function () {
showMsg(i18next.t("message.Modify_Failed"), 'error');//╠message.Modify_Failed⇒修改失敗╣
});
},
/**
* 資料刪除
*/
fnDel = function () {
CallAjax(ComFn.W_Com, ComFn.GetDel, {
Params: {
billchangeapply: {
Guid: sDataId
}
}
}, function (res) {
if (res.d > 0) {
DelTask(sDataId);
showMsgAndGo(i18next.t("message.Delete_Success"), sQueryPrgId); // ╠message.Delete_Success⇒刪除成功╣
}
else {
showMsg(i18next.t("message.Delete_Failed"), 'error'); // ╠message.Delete_Failed⇒刪除失敗╣
}
}, function () {
showMsg(i18next.t("message.Delete_Failed"), 'error'); // ╠message.Delete_Failed⇒刪除失敗╣
});
},
/**
* 設定客戶下拉選單
*/
setCustomersDrop = function () {
return g_api.ConnectLite(Service.sys, 'GetCustomerlist', {}, function (res) {
if (res.RESULT) {
var saCustomers = res.DATA.rel,
sCustomersOptionsHtml = createOptions(saCustomers, 'id', 'text');
$('#BillChanges_CustomerID').html(sCustomersOptionsHtml);
select2Init($('#BillChanges_CustomerID').parent());
}
});
},
/**
* 上傳附件
* @param {Array} files 上傳的文件
*/
fnUpload = function (files) {
var option = {};
option.input = $('#fileInput');
option.theme = 'dragdropbox';
option.folder = 'BillChangeApply';
option.type = 'list';
option.parentid = oCurData.Guid;
if (files) {
option.files = files;
}
fnUploadRegister(option);
},
/**
* 提交簽呈
*/
fnSubmitPetition = function () {
g_api.ConnectLite(sProgramId, 'BillChangeApplyToAudit', {
guid: oCurData.Guid
}, function (res) {
if (res.RESULT) {
showMsgAndGo(i18next.t("message.ToAudit_Success"), sViewPrgId, '?Action=Upd&Guid=' + oCurData.Guid);// ╠message.ToAudit_Success⇒提交審核成功╣
parent.msgs.server.pushTips(parent.fnReleaseUsers(res.DATA.rel));
}
else {
showMsg(i18next.t('message.ToAudit_Failed') + '
' + res.MSG, 'error'); // ╠message.ToAudit_Failed⇒提交審核失敗╣
}
}, function () {
showMsg(i18next.t('message.ToAudit_Failed'), 'error'); // ╠message.ToAudit_Failed⇒提交審核失敗╣
});
},
/**
* ToolBar 按鈕事件 function
* @param {Object} inst 按鈕物件對象
* @param {Object} e 事件對象
*/
fnButtonHandler = function (inst, e) {
var sId = inst.id;
switch (sId) {
case "Toolbar_Qry":
break;
case "Toolbar_Save":
if (!$("#form_main").valid()) {
oValidator.focusInvalid();
return false;
}
if (sAction === 'Add') {
fnAdd('add');
}
else {
fnUpd();
}
break;
case "Toolbar_ReAdd":
if (!$("#form_main").valid()) {
oValidator.focusInvalid();
return false;
}
fnAdd('readd');
break;
case "Toolbar_Clear":
clearPageVal();
break;
case "Toolbar_Leave":
pageLeave();
break;
case "Toolbar_Add":
break;
case "Toolbar_Upd":
break;
case "Toolbar_Copy":
break;
case "Toolbar_Petition":
if (!$("#form_main").valid()) {
oValidator.focusInvalid();
return false;
}
fnUpd(true).done(function () {
fnSubmitPetition();
});
break;
case "Toolbar_Del": // ╠message.ConfirmToDelete⇒確定要刪除嗎 ?╣ ╠common.Tips⇒提示╣
layer.confirm(i18next.t("message.ConfirmToDelete"), { icon: 3, title: i18next.t('common.Tips') }, function (index) {
fnDel();
layer.close(index);
});
break;
default:
alert("No handle '" + sId + "'");
break;
}
},
/**
* 初始化 function
*/
init = function () {
var saCusBtns = null;
if (sAction === 'Upd') {
saCusBtns = [{
id: 'Toolbar_Petition',
value: 'common.SubmitPetition'// ╠common.SubmitPetition⇒提交簽呈╣
}];
}
commonInit({
PrgId: sProgramId,
ButtonHandler: fnButtonHandler,
Buttons: saCusBtns,
GoTop: true
});
oValidator = $("#form_main").validate();
$.whenArray([
fnSetDeptDrop($('#Handle_DeptID')),
fnSetFlowDrop({
Flow_Type: parent.SysSet.Eip_005,
ShareTo: parent.UserID,
CallBack: function (data) {
$.each(data, function (idx, item) {
var saFlows = $.parseJSON(item.Flows),
saFlowsText = [],
sFlowsText = '';
$.each(saFlows, function (idx, flow) {
var sFlowType = i18next.t('common.' + flow.SignedWay);
if (flow.SignedWay !== 'flow1') {
saFlowsText.push(sFlowType + '(' + Enumerable.From(flow.SignedMember).ToString(",", "$.name") + ')');
}
else {
saFlowsText.push(Enumerable.From(flow.SignedMember).ToString(",", "$.name"));
}
});
sFlowsText = saFlowsText.join(' → ');
item.text = item.Flow_Name + ' - ' + (sFlowsText.length > 60 ? sFlowsText.substr(0, 60) + '...' : sFlowsText);
});
$('#FlowId').html(createOptions(data, 'Guid', 'text')).on('change', function () {
var sFlowId = this.value;
if (sFlowId) {
CallAjax(ComFn.W_Com, ComFn.GetOne, {
Type: '',
Params: {
checkflow: {
Guid: sFlowId
}
}
}, function (res) {
if (res.d) {
var oRes = $.parseJSON(res.d);
oRes.Flows = $.parseJSON(oRes.Flows);
oCurData.CheckOrder = oRes.Flows;
oCurData.Flows_Lock = oRes.Flows_Lock;
oCurData.Handle_Lock = oRes.Handle_Lock;
$("#Handle_DeptID").val(oRes.Handle_DeptID);
$("#Handle_Person").val(oRes.Handle_Person).trigger('change');
if (oRes.Flows_Lock === 'Y') {
$(".checkordertoolbox").hide();
}
else {
$(".checkordertoolbox").show();
}
if (oRes.Handle_Lock === 'Y') {
$("#Handle_DeptID,#Handle_Person").attr('disabled', true);
}
else {
$("#Handle_DeptID,#Handle_Person").removeAttr('disabled');
}
$("#jsGrid").jsGrid("loadData");
}
});
}
else {
oCurData.CheckOrder = [];
$(".checkordertoolbox").hide();
$("#jsGrid").jsGrid("loadData");
$("#Handle_DeptID,#Handle_Person").removeAttr('disabled');
}
});
}
}),
fnSetEpoDrop({
Select: $('#BillChanges_ExhibitionNO'),
IdName: 'ExhibitionCode',
TextName: 'ExhibitioShotName_TW',
Select2: true
}),
fnSetUserDrop([
{
ShowId: true,
Select: $('#Handle_Person,#BillChanges_ResponsiblePerson'),
Select2: true,
Action: sAction,
CallBack: function (data) {
saUsers = data;
}
}
]),
setCustomersDrop(),
fnSetArgDrop([
{
ArgClassID: 'Currency',
CallBack: function (data) {
$('.select_currency').html(createOptions(data, 'id', 'id'))[0].remove(0);
}
}
])]).done(function () {
$('.glyphicon-trash').on('click', function () {
$(this).parent().parent().remove();
});
$('[data-id="OldAmount"]').on('change', function () {
var iAllOldAmount = 0;
$('[data-id="OldAmount"]').each(function () {
iAllOldAmount += ($(this).attr('data-value') || 0) * 1;
});
$('#BillChanges_OldSumAmount').attr('data-value', iAllOldAmount).val(fMoney(iAllOldAmount, 2, 'NTD'));
});
$('[data-id="NewAmount"]').on('change', function () {
var iAllNewAmount = 0;
$('[data-id="NewAmount"]').each(function () {
iAllNewAmount += ($(this).attr('data-value') || 0) * 1;
});
$('#BillChanges_NewSumAmount').attr('data-value', iAllNewAmount).val(fMoney(iAllNewAmount, 2, 'NTD'));
});
oTR = $('.billchangeinfo tr').eq(1).clone(true);
$('.plustemplkey').on('click', function () {
var oTrNew = oTR.clone(true);
$('.billchangeinfo tr:last').before(oTrNew);
});
moneyInput($('[data-type="int"]'), 2, true);
fnGet();
});
$('#Handle_DeptID').on('change', function () {
fnSetUserDrop([
{
Select: $('#Handle_Person'),
DepartmentID: this.value,
ShowId: true,
Select2: true,
Action: sAction
}
]);
});
$('#Agent_Person').on('change', function () {
oCurData.Agent_Person = this.value;
});
$('.flowlink').on('click', function () {
var oOption = {};
oOption.SignedWay = this.id;
oOption.Callback = function (data) {
if (data.Users.length > 0) {
var oFlow = {};
if (data.FlowType === 'flow1') {
$.each(data.Users, function (idx, user) {
oFlow = {};
oFlow.id = guid();
oFlow.Order = oCurData.CheckOrder.length + 1;
oFlow.SignedWay = data.FlowType;
oFlow.SignedMember = [{
id: user.id,
name: user.name,
deptname: user.deptname,
jobname: user.jobname
}];
oCurData.CheckOrder.push(oFlow);
});
}
else {
var saSignedMember = [];
$.each(data.Users, function (idx, user) {
saSignedMember.push({
id: user.id,
name: user.name,
deptname: user.deptname,
jobname: user.jobname
});
});
oFlow.id = guid();
oFlow.Order = oCurData.CheckOrder.length + 1;
oFlow.SignedWay = data.FlowType;
oFlow.SignedMember = saSignedMember;
oCurData.CheckOrder.push(oFlow);
}
oCurData.CheckOrder = releaseGridList(oCurData.CheckOrder);
$("#jsGrid").jsGrid("loadData");
}
};
oPenUserListPop(oOption);
});
$("#jsGrid").jsGrid({
width: "100%",
height: "auto",
autoload: true,
filtering: false,
pageLoading: true,
pageIndex: 1,
pageSize: 10000,
fields: [
{
name: "Order", title: 'common.Order', width: 50, align: "center",
itemTemplate: function (val, item) {
return val < 10 ? '0' + val : val;
}
},
{
name: "SignedWay", title: 'common.SignedWay', width: 120, align: "center",
itemTemplate: function (val, item) {
return i18next.t('common.' + val);
}
},
{
type: "Icon", width: 50, align: "center",
itemTemplate: function (val, item) {
var oIcon = {
flow1: '',
flow2: '
',
flow3: '
',
flow4: '
'
},
sIcon = oIcon[item.SignedWay];
if (item.Order !== oCurData.CheckOrder.length) {
sIcon += '
'
}
return sIcon;
}
},
{
name: "SignedMember", title: 'common.SignedMember', width: 500,
itemTemplate: function (val, item) {
return Enumerable.From(val).ToString(",", "$.name");
}
},
{
type: "control", title: 'common.Action', width: 200,
itemTemplate: function (val, item) {
var oBtns = [$('