|
@@ -5,7 +5,8 @@
|
|
|
var Ration = {
|
|
|
createNew: function (project) {
|
|
|
// 用户定义private方法
|
|
|
- var tools = {};
|
|
|
+ var tools = {
|
|
|
+ };
|
|
|
|
|
|
// 所有通过this访问的属性,都不应在此单元外部进行写入操作
|
|
|
var ration = function (proj) {
|
|
@@ -17,27 +18,23 @@ var Ration = {
|
|
|
return sourceType;
|
|
|
}
|
|
|
proj.registerModule(ModuleNames.ration, this);
|
|
|
+
|
|
|
+ var maxRationID = 0;
|
|
|
+ this.getNewRationID = function () {
|
|
|
+ return maxRationID += 1;
|
|
|
+ };
|
|
|
+ this.maxRationID = function (maxID) {
|
|
|
+ if (arguments.length === 0) {
|
|
|
+ return maxRationID;
|
|
|
+ } else {
|
|
|
+ maxRationID = Math.max(maxID, maxRationID);
|
|
|
+ }
|
|
|
+ };
|
|
|
};
|
|
|
|
|
|
- // 从后台获取数据
|
|
|
- /*ration.prototype.pullData = function (){
|
|
|
- this.project.pullData(
|
|
|
- '/ration/getData',
|
|
|
- {projectID: this.project.ID},
|
|
|
- function(result){
|
|
|
- if (result.error ===0){
|
|
|
- this.loadDatas(result.data);
|
|
|
- }
|
|
|
- else {
|
|
|
- // to do: 错误处理需要细化
|
|
|
- alert(result.message);
|
|
|
- }
|
|
|
- },
|
|
|
- function (){}//to do: 错误处理需要细化
|
|
|
- )
|
|
|
- };*/
|
|
|
// prototype用于定义public方法
|
|
|
ration.prototype.loadData = function (datas) {
|
|
|
+ var that = this;
|
|
|
this.datas = datas;
|
|
|
// generate Fees & Flags Index,For View & Calculate
|
|
|
this.datas.forEach(function (data) {
|
|
@@ -49,6 +46,7 @@ var Ration = {
|
|
|
data.flags.forEach(function (flag) {
|
|
|
data.FlagsIndex[flag.fieldName] = flag;
|
|
|
});
|
|
|
+ that.maxRationID(data.ID);
|
|
|
});
|
|
|
};
|
|
|
|
|
@@ -57,6 +55,97 @@ var Ration = {
|
|
|
// to do
|
|
|
};
|
|
|
|
|
|
+ ration.prototype.getTempRationData = function (id, billsID, serialNo) {
|
|
|
+ var newData = {'ID': id, 'serialNo': serialNo, projectID: this.project.ID()};
|
|
|
+ newData[project.masterField.ration] = billsID;
|
|
|
+ return newData;
|
|
|
+ };
|
|
|
+
|
|
|
+ ration.prototype.getBillsSortRation = function (billsID) {
|
|
|
+ var rations = this.datas.filter(function (data) {
|
|
|
+ return data[project.masterField.ration] === billsID;
|
|
|
+ });
|
|
|
+ rations.sort(function (x, y) {
|
|
|
+ return x.serialNo - y.serialNo;
|
|
|
+ });
|
|
|
+ return rations;
|
|
|
+ };
|
|
|
+
|
|
|
+ ration.prototype.getInsertRationData = function (billsID, preRation) {
|
|
|
+ var br = this.getBillsSortRation(billsID);
|
|
|
+ var updateData = [];
|
|
|
+ if (preRation) {
|
|
|
+ var preIndex = br.indexOf(preRation), i;
|
|
|
+ updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1)});
|
|
|
+ for (i = preIndex + 1; i < br.length; i++) {
|
|
|
+ updateData.push({updateType: 'ut_update', updateData: this.getTempRationData(br[i].ID, billsID, i < br.length - 1 ? br[i+1].serialNo : br[i].serialNo + 1)});
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ updateData.push({updateType: 'ut_create', updateData: this.getTempRationData(this.maxRationID() + 1, billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1)});
|
|
|
+ }
|
|
|
+ return updateData;
|
|
|
+ };
|
|
|
+
|
|
|
+ ration.prototype.getCounterData = function (count) {
|
|
|
+ var updateData = {'projectID': this.project.ID()};
|
|
|
+ if (count) {
|
|
|
+ updateData[this.getSourceType()] = this.maxRationID() + count;
|
|
|
+ } else {
|
|
|
+ updateData[this.getSourceType()] = this.maxRationID() + 1;
|
|
|
+ }
|
|
|
+ return updateData;
|
|
|
+ };
|
|
|
+
|
|
|
+ ration.prototype.insertRation = function (billsID, preRation) {
|
|
|
+ var br = this.getBillsSortRation(billsID);
|
|
|
+ /*this.project.pushNow('insertRation', [this.getSourceType(), 'proj_counter'],
|
|
|
+ [this.getInsertRationData(billsID, preRation), this.getCounterData()]);*/
|
|
|
+ this.project.pushNow('insertRation', [this.getSourceType()], [this.getInsertRationData(billsID, preRation)]);
|
|
|
+
|
|
|
+ var newRation = null;
|
|
|
+ if (preRation) {
|
|
|
+ var preIndex = br.indexOf(preRation), i;
|
|
|
+ newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
|
|
|
+ this.datas.push(newRation);
|
|
|
+ for (i = preIndex + 1; i < br.length; i++) {
|
|
|
+ br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
|
|
|
+ this.datas.push(newRation);
|
|
|
+ }
|
|
|
+ return newRation;
|
|
|
+ };
|
|
|
+
|
|
|
+ ration.prototype.insertStdRation = function (billsID, preRation, std) {
|
|
|
+ var br = this.getBillsSortRation(billsID), updateData = this.getInsertRationData(billsID, preRation);
|
|
|
+ updateData.forEach(function (data) {
|
|
|
+ if (data.updateType === 'ut_create') {
|
|
|
+ data.updateData.code = std.code;
|
|
|
+ data.updateData.name = std.name;
|
|
|
+ data.updateData.unit = std.unit;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ this.project.pushNow('insertRation', [this.getSourceType()], [updateData]);
|
|
|
+
|
|
|
+ var newRation = null;
|
|
|
+ if (preRation) {
|
|
|
+ var preIndex = br.indexOf(preRation), i;
|
|
|
+ newRation = this.getTempRationData(this.getNewRationID(), billsID, preIndex < br.length - 1 ? br[preIndex + 1].serialNo : br[preIndex].serialNo + 1);
|
|
|
+ newRation.code = std.code;
|
|
|
+ newRation.name = std.name;
|
|
|
+ newRation.unit = std.unit;
|
|
|
+ this.datas.push(newRation);
|
|
|
+ for (i = preIndex + 1; i < br.length; i++) {
|
|
|
+ br[i].serialNo = i < br.length - 1 ? br [i + 1].serialNo : br[i].serialNo + 1;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ newRation = this.getTempRationData(this.getNewRationID(), billsID, br.length > 0 ? br[br.length - 1].serialNo + 1 : 1);
|
|
|
+ this.datas.push(newRation);
|
|
|
+ }
|
|
|
+ return newRation;
|
|
|
+ };
|
|
|
+
|
|
|
return new ration(project);
|
|
|
}
|
|
|
};
|