bills.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /**
  2. * Created by Mai on 2017/4/1.
  3. */
  4. var Bills = {
  5. createNew: function (project) {
  6. var billsTreeSetting = {
  7. id: 'ID',
  8. pid: 'ParentID',
  9. nid: 'NextSiblingID',
  10. rootId: -1,
  11. autoUpdate: true
  12. };
  13. // 用户定义private方法
  14. var tools = {
  15. coverseTreeUpdateData: function (datas, projectID) {
  16. var updateDatas = [];
  17. datas.forEach(function (data) {
  18. var updateData = {};
  19. data.data.projectID = projectID;
  20. if (data.type === idTree.updateType.new) {
  21. updateData.updateType = 'ut_create';
  22. updateData.updateData = data.data;
  23. } else if (data.type === idTree.updateType.update) {
  24. updateData.updateType = 'ut_update';
  25. updateData.updateData = data.data;
  26. } else if (data.type === idTree.updateType.delete) {
  27. updateData.updateType = 'ut_delete';
  28. updateData.updateData = data.data;
  29. }
  30. updateDatas.push(updateData);
  31. });
  32. return updateDatas;
  33. },
  34. formatBillsUpdateData: function (data) {
  35. let uData = JSON.parse(JSON.stringify(data));
  36. delete uData.feesIndex;
  37. delete uData.flagsIndex;
  38. if (uData.quantity) {
  39. uData.quantity = uData.quantity.toFixed(2);
  40. }
  41. if (uData.fees) {
  42. for (let fee of uData.fees) {
  43. fee.unitFee = fee.unitFee.toFixed(2);
  44. fee.totalFee = fee.totalFee.toFixed(2);
  45. fee.tenderUnitFee = fee.tenderUnitFee.toFixed(2);
  46. fee.tenderTotalFee = fee.tenderTotalFee.toFixed(2);
  47. }
  48. }
  49. return uData;
  50. }
  51. };
  52. // 所有通过this访问的属性,都不应在此单元外部进行写入操作
  53. var bills = function (proj) {
  54. this.project = proj;
  55. this.datas = null;
  56. this.tree = idTree.createNew(billsTreeSetting);
  57. var sourceType = ModuleNames.bills;
  58. this.getSourceType = function () {
  59. return sourceType;
  60. }
  61. proj.registerModule(ModuleNames.bills, this);
  62. };
  63. // 从后台获取数据
  64. /*bills.prototype.pullData = function (){
  65. this.project.pullData(
  66. '/bills/getData',
  67. {projectID: this.project.ID},
  68. function(result){
  69. if (result.error ===0){
  70. this.loadDatas(result.data);
  71. }
  72. else {
  73. // to do: ?错误处理需要细化
  74. alert(result.message);
  75. }
  76. },
  77. function (){}// to do: 错误处理需要细化
  78. )
  79. };*/
  80. // prototype用于定义public方法
  81. bills.prototype.loadData = function (datas) {
  82. this.datas = datas;
  83. // generate Fees & Flags Index, For View & Calculate
  84. this.datas.forEach(function (data) {
  85. if (data.quantity) {
  86. data.quantity = parseFloat(data.quantity);
  87. }
  88. data.feesIndex = {};
  89. if (data.fees) {
  90. data.fees.forEach(function (fee) {
  91. fee.unitFee = parseFloat(fee.unitFee);
  92. fee.totalFee = parseFloat(fee.totalFee);
  93. fee.tenderUnitFee = parseFloat(fee.tenderUnitFee);
  94. fee.tenderTotalFee = parseFloat(fee.tenderTotalFee);
  95. data.feesIndex[fee.fieldName] = fee;
  96. });
  97. }
  98. data.flagsIndex = {};
  99. if (data.flags) {
  100. data.flags.forEach(function (flag) {
  101. data.flagsIndex[flag.fieldName] = flag;
  102. });
  103. }
  104. });
  105. // datas load to Tree
  106. this.tree.loadDatas(this.datas);
  107. };
  108. bills.prototype.setMaxID = function (ID) {
  109. this.tree.maxNodeID(ID);
  110. };
  111. // 提交数据后的错误处理方法
  112. bills.prototype.doAfterUpdate = function(err, data){
  113. console.log(data)
  114. if(data.quantityRefresh){
  115. this.refreshDatas(data,'quantity');
  116. }
  117. $.bootstrapLoading.end();
  118. };
  119. bills.prototype.refreshDatas = function(data,fieldName){
  120. var dataIndex = _.findIndex(this.datas,function(item) {
  121. return item.ID ==data.billID;
  122. });
  123. this.datas[dataIndex][fieldName] = data[fieldName];
  124. if(fieldName=='quantity'){
  125. this.datas[dataIndex]['isFromDetail']=1
  126. }
  127. var controller = projectObj.mainController;
  128. var selected = controller.sheet.getSelections();
  129. var col = _.findIndex(project.projSetting.main_tree_col.cols,function (col) {
  130. return col.data.field ==fieldName;
  131. });
  132. controller.sheet.getCell(selected[0].row,col).value(data[fieldName]);
  133. };
  134. bills.prototype.getCounterData = function (count) {
  135. var updateData = {'projectID': this.project.ID()};
  136. if (count) {
  137. updateData[this.getSourceType()] = this.tree.maxNodeID() + count;
  138. } else {
  139. updateData[this.getSourceType()] = this.tree.maxNodeID() + 1;
  140. }
  141. return updateData;
  142. };
  143. bills.prototype.insertBills = function (parentId, nextSiblingId) {
  144. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  145. var that = this, newData = null;
  146. insertData.forEach(function (data) {
  147. if (data.type === idTree.updateType.new) {
  148. newData = data.data;
  149. }
  150. });
  151. this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()],
  152. [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
  153. //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
  154. this.datas.push(newData);
  155. return this.tree.insert(parentId, nextSiblingId);
  156. };
  157. bills.prototype.insertStdBills = function (parentId, nextSiblingId, stdBillsData) {
  158. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  159. var newData = null, that = this;
  160. insertData.forEach(function (data) {
  161. if (data.type === idTree.updateType.new) {
  162. data.data.code = that.newFormatCode(stdBillsData.code);
  163. data.data.name = stdBillsData.name;
  164. data.data.unit = stdBillsData.unit;
  165. // 工程量计算规则
  166. data.data.ruleText = stdBillsData.ruleText;
  167. // 说明(补注)
  168. data.data.comments = stdBillsData.recharge;
  169. //zhong 特征及内容
  170. data.data.jobContent = stdBillsData.jobContent;
  171. data.data.itemCharacter = stdBillsData.itemCharacter;
  172. data.data.jobContentText = stdBillsData.jobContentText;
  173. data.data.itemCharacterText = stdBillsData.itemCharacterText;
  174. data.data.programID = stdBillsData.engineering;
  175. //zhong
  176. newData = data.data;
  177. }
  178. });
  179. this.project.pushNow('insertStdBills', [this.getSourceType(), this.project.projCounter()],
  180. [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
  181. this.datas.push(newData);
  182. return this.tree.insertByData(newData, parentId, nextSiblingId);
  183. };
  184. bills.prototype.deleteBills = function (node) {
  185. let deleteNode = function (node) {
  186. this.project.Ration.deleteByBills([node]);
  187. // this.project.VolumePrice.deleteByBills([node]);
  188. return this.tree.delete(node);
  189. }
  190. var deleteData = this.tree.getDeleteData(node);
  191. var ration_glj =projectObj.project.ration_glj;
  192. // let modules =[ModuleNames.bills, ModuleNames.ration, ModuleNames.ration_glj, ModuleNames.volume_price];
  193. let modules =[ModuleNames.bills, ModuleNames.ration, ModuleNames.ration_glj];
  194. let deleteDatas=[tools.coverseTreeUpdateData(deleteData, this.project.ID()),
  195. this.project.Ration.getDeleteDataByBill([node]), ration_glj.getDeleteDataByBills(deleteData),
  196. // this.project.VolumePrice.getDeleteDataByBills([node])
  197. ];
  198. project.ration_glj.deleteByBills(deleteData);
  199. project.quantity_detail.deleteByBills(deleteData);
  200. project.pushNow('deleteBills', modules, deleteDatas);
  201. this.datas.splice(this.datas.indexOf(node.data), 1);
  202. return this.tree.delete(node);
  203. };
  204. bills.prototype.upMoveBills = function (node) {
  205. var upMoveData = node.getUpMoveData();
  206. project.pushNow('upMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(upMoveData, this.project.ID()));
  207. return node.upMove();
  208. };
  209. bills.prototype.downMoveBills = function (node) {
  210. var downMoveData = node.getDownMoveData();
  211. project.pushNow('downMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(downMoveData, this.project.ID()));
  212. return node.downMove();
  213. };
  214. bills.prototype.upLevelBills = function (node) {
  215. var upLevelData = node.getUpLevelData();
  216. project.pushNow('upLevelBills', this.getSourceType(), tools.coverseTreeUpdateData(upLevelData, this.project.ID()));
  217. return node.upLevel();
  218. };
  219. bills.prototype.downLevelBills = function (node) {
  220. var downLevelData = node.getDownLevelData();
  221. project.pushNow('downLevelBills', [this.getSourceType()], [tools.coverseTreeUpdateData(downLevelData, this.project.ID())]);
  222. return node.downLevel();
  223. };
  224. bills.prototype.updateField = function (node, field, newValue) {
  225. calcFees.setFee(node.data, field, newValue);
  226. let updateData = [];
  227. let data = {'ID': node.getID(), 'projectID': this.project.ID()};
  228. data[field] = newValue;
  229. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(data)});
  230. this.project.pushNow('updateBills', this.getSourceType(), updateData);
  231. };
  232. bills.prototype.getUpdateAllData = function () {
  233. let updateData = [];
  234. for (let data of this.datas) {
  235. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(data)});
  236. }
  237. return updateData;
  238. };
  239. bills.prototype.updateAll = function () {
  240. this.project.pushNow('updateAllBills', this.getSourceType(), this.getUpdateAllData());
  241. };
  242. bills.prototype.getUpdateNodesData = function (nodes) {
  243. let updateData = [];
  244. for (let node of nodes) {
  245. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(node.data)});
  246. }
  247. return updateData;
  248. }
  249. bills.prototype.updateNodes = function (nodes, updateNow) {
  250. if (updateNow) {
  251. this.project.pushNow('updateBills', this.getSourceType(), this.getUpdateNodesData(nodes));
  252. } else {
  253. this.project.push(this.getSourceType(), this.getUpdateNodesData(nodes));
  254. }
  255. };
  256. bills.prototype.sameStdCode = function (stdCode, filterCode) {
  257. let reg = new RegExp('^' + stdCode), matchs= [];
  258. for (let data of this.datas) {
  259. if (data.code && data.code.length === 12 && reg.test(data.code) && data.code !== filterCode) {
  260. matchs.push(data.code);
  261. }
  262. }
  263. return matchs;
  264. }
  265. bills.prototype.newFormatCode = function (stdCode, filterCode) {
  266. let matchs = this.sameStdCode(stdCode, filterCode);
  267. let format = function (Number) {
  268. let s = Number + '';
  269. while (s.length < 3) {
  270. s = '0' + s;
  271. }
  272. return s;
  273. }
  274. for (i = 0; i <= matchs.length; i++) {
  275. let formatCode = stdCode + format(i+1);
  276. if (matchs.indexOf(formatCode) === -1) {
  277. return formatCode;
  278. }
  279. }
  280. };
  281. bills.prototype.replaceBills = function (node, stdBillsData, code) {
  282. let updateData = [];
  283. node.data.code = code;
  284. if (stdBillsData) {
  285. node.data.name = stdBillsData.name;
  286. node.data.unit = stdBillsData.unit;
  287. // 工程量计算规则
  288. node.data.ruleText = stdBillsData.ruleText;
  289. // 说明(补注)
  290. node.data.comments = stdBillsData.recharge;
  291. // 工作内容
  292. node.data.jobContent = stdBillsData.jobContent;
  293. node.data.jobContentText = stdBillsData.jobContentText;
  294. // 特征
  295. node.data.itemCharacter = stdBillsData.itemCharacter;
  296. node.data.itemCharacterText = stdBillsData.itemCharacterText;
  297. node.data.programID = stdBillsData.engineering;
  298. }
  299. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(node.data)});
  300. this.project.pushNow('replaceBills', this.getSourceType(), updateData);
  301. return node;
  302. };
  303. bills.prototype.sameStdCodeBillsData = function (stdCode) {
  304. let reg = new RegExp('^' + stdCode);
  305. for (let data of this.datas) {
  306. if (data.code && data.code.length === 12 && reg.test(data.code) && /^[\d]+$/.test(data.code)) {
  307. return data;
  308. }
  309. }
  310. return null;
  311. }
  312. return new bills(project);
  313. }
  314. };