bills.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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.insertSpecialBill=function(parentId, nextSiblingId,isUserAdd,type){
  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. if(isUserAdd==true){//如果是用户新增的
  149. data.data.isAdd = 1;
  150. }
  151. data.data.type = type;
  152. newData = data.data;
  153. }
  154. });
  155. this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()],
  156. [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
  157. //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
  158. this.datas.push(newData);
  159. return this.tree.insertByData(newData,parentId, nextSiblingId);
  160. };
  161. bills.prototype.insertBills = function (parentId, nextSiblingId) {
  162. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  163. var that = this, newData = null;
  164. insertData.forEach(function (data) {
  165. if (data.type === idTree.updateType.new) {
  166. data.data.type = billType.BILL;
  167. newData = data.data;
  168. }
  169. });
  170. this.project.pushNow('insertBills', [this.getSourceType(), this.project.projCounter()],
  171. [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
  172. //project.pushNow('insertBills', ModuleNames.bills, tools.coverseTreeUpdateData(insertData));
  173. this.datas.push(newData);
  174. return this.tree.insertByData(newData,parentId, nextSiblingId);
  175. };
  176. bills.prototype.insertStdBills = function (parentId, nextSiblingId, stdBillsData) {
  177. var insertData = this.tree.getInsertData(parentId, nextSiblingId);
  178. var newData = null, that = this;
  179. insertData.forEach(function (data) {
  180. if (data.type === idTree.updateType.new) {
  181. data.data.code = that.newFormatCode(stdBillsData.code);
  182. data.data.name = stdBillsData.name;
  183. data.data.unit = stdBillsData.unit;
  184. // 工程量计算规则
  185. data.data.ruleText = stdBillsData.ruleText;
  186. // 说明(补注)
  187. data.data.comments = stdBillsData.recharge;
  188. //zhong 特征及内容
  189. data.data.jobContent = stdBillsData.jobContent;
  190. data.data.itemCharacter = stdBillsData.itemCharacter;
  191. data.data.jobContentText = stdBillsData.jobContentText;
  192. data.data.itemCharacterText = stdBillsData.itemCharacterText;
  193. data.data.programID = stdBillsData.engineering;
  194. data.data.type = billType.BILL;//插入清单类型
  195. //zhong
  196. newData = data.data;
  197. }
  198. });
  199. this.project.pushNow('insertStdBills', [this.getSourceType(), this.project.projCounter()],
  200. [ tools.coverseTreeUpdateData(insertData, this.project.ID()), this.getCounterData()]);
  201. this.datas.push(newData);
  202. return this.tree.insertByData(newData, parentId, nextSiblingId);
  203. };
  204. bills.prototype.deleteBills = function (node) {
  205. let deleteNode = function (node) {
  206. this.project.Ration.deleteByBills([node]);
  207. // this.project.VolumePrice.deleteByBills([node]);
  208. return this.tree.delete(node);
  209. }
  210. var deleteData = this.tree.getDeleteData(node);
  211. var ration_glj =projectObj.project.ration_glj;
  212. // let modules =[ModuleNames.bills, ModuleNames.ration, ModuleNames.ration_glj, ModuleNames.volume_price];
  213. let modules =[ModuleNames.bills, ModuleNames.ration, ModuleNames.ration_glj];
  214. let deleteDatas=[tools.coverseTreeUpdateData(deleteData, this.project.ID()),
  215. this.project.Ration.getDeleteDataByBill([node]), ration_glj.getDeleteDataByBills(deleteData),
  216. // this.project.VolumePrice.getDeleteDataByBills([node])
  217. ];
  218. project.ration_glj.deleteByBills(deleteData);
  219. project.quantity_detail.deleteByBills(deleteData);
  220. project.pushNow('deleteBills', modules, deleteDatas);
  221. this.datas.splice(this.datas.indexOf(node.data), 1);
  222. return this.tree.delete(node);
  223. };
  224. bills.prototype.upMoveBills = function (node) {
  225. var upMoveData = node.getUpMoveData();
  226. project.pushNow('upMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(upMoveData, this.project.ID()));
  227. return node.upMove();
  228. };
  229. bills.prototype.downMoveBills = function (node) {
  230. var downMoveData = node.getDownMoveData();
  231. project.pushNow('downMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(downMoveData, this.project.ID()));
  232. return node.downMove();
  233. };
  234. bills.prototype.upLevelBills = function (node) {
  235. var upLevelData = node.getUpLevelData();
  236. project.pushNow('upLevelBills', this.getSourceType(), tools.coverseTreeUpdateData(upLevelData, this.project.ID()));
  237. return node.upLevel();
  238. };
  239. bills.prototype.downLevelBills = function (node) {
  240. var downLevelData = node.getDownLevelData();
  241. project.pushNow('downLevelBills', [this.getSourceType()], [tools.coverseTreeUpdateData(downLevelData, this.project.ID())]);
  242. return node.downLevel();
  243. };
  244. bills.prototype.updateField = function (node, field, newValue) {
  245. calcFees.setFee(node.data, field, newValue);
  246. let updateData = [];
  247. let data = {'ID': node.getID(), 'projectID': this.project.ID()};
  248. data[field] = newValue;
  249. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(data)});
  250. this.project.pushNow('updateBills', this.getSourceType(), updateData);
  251. };
  252. bills.prototype.getUpdateAllData = function () {
  253. let updateData = [];
  254. for (let data of this.datas) {
  255. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(data)});
  256. }
  257. return updateData;
  258. };
  259. bills.prototype.updateAll = function () {
  260. this.project.pushNow('updateAllBills', this.getSourceType(), this.getUpdateAllData());
  261. };
  262. bills.prototype.getUpdateNodesData = function (nodes) {
  263. let updateData = [];
  264. for (let node of nodes) {
  265. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(node.data)});
  266. }
  267. return updateData;
  268. }
  269. bills.prototype.updateNodes = function (nodes, updateNow) {
  270. if (updateNow) {
  271. this.project.pushNow('updateBills', this.getSourceType(), this.getUpdateNodesData(nodes));
  272. } else {
  273. this.project.push(this.getSourceType(), this.getUpdateNodesData(nodes));
  274. }
  275. };
  276. bills.prototype.sameStdCode = function (stdCode, filterCode) {
  277. let reg = new RegExp('^' + stdCode), matchs= [];
  278. for (let data of this.datas) {
  279. if (data.code && data.code.length === 12 && reg.test(data.code) && data.code !== filterCode) {
  280. matchs.push(data.code);
  281. }
  282. }
  283. return matchs;
  284. }
  285. bills.prototype.newFormatCode = function (stdCode, filterCode) {
  286. let matchs = this.sameStdCode(stdCode, filterCode);
  287. let format = function (Number) {
  288. let s = Number + '';
  289. while (s.length < 3) {
  290. s = '0' + s;
  291. }
  292. return s;
  293. }
  294. for (i = 0; i <= matchs.length; i++) {
  295. let formatCode = stdCode + format(i+1);
  296. if (matchs.indexOf(formatCode) === -1) {
  297. return formatCode;
  298. }
  299. }
  300. };
  301. bills.prototype.replaceBills = function (node, stdBillsData, code) {
  302. let updateData = [];
  303. node.data.code = code;
  304. if (stdBillsData) {
  305. node.data.name = stdBillsData.name;
  306. node.data.unit = stdBillsData.unit;
  307. // 工程量计算规则
  308. node.data.ruleText = stdBillsData.ruleText;
  309. // 说明(补注)
  310. node.data.comments = stdBillsData.recharge;
  311. // 工作内容
  312. node.data.jobContent = stdBillsData.jobContent;
  313. node.data.jobContentText = stdBillsData.jobContentText;
  314. // 特征
  315. node.data.itemCharacter = stdBillsData.itemCharacter;
  316. node.data.itemCharacterText = stdBillsData.itemCharacterText;
  317. node.data.programID = stdBillsData.engineering;
  318. }
  319. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(node.data)});
  320. this.project.pushNow('replaceBills', this.getSourceType(), updateData);
  321. return node;
  322. };
  323. bills.prototype.sameStdCodeBillsData = function (stdCode) {
  324. let reg = new RegExp('^' + stdCode);
  325. for (let data of this.datas) {
  326. if (data.code && data.code.length === 12 && reg.test(data.code) && /^[\d]+$/.test(data.code)) {
  327. return data;
  328. }
  329. }
  330. return null;
  331. };
  332. bills.prototype.getSubdivisionProjectLeavesID=function () {//取所有分部分项工程清单叶子节点ID
  333. let roots = projectObj.project.mainTree.roots;//所有根节点
  334. let subdivisionNode = null;
  335. for(let r of roots){
  336. if(isFlag(r.data)&&r.data.flagsIndex.fixed.flag==fixedFlag.SUB_ENGINERRING) {
  337. subdivisionNode = r;
  338. break;
  339. }
  340. }
  341. let nodes = this.getLeavesBillNodes(subdivisionNode);
  342. return _.map(nodes,"data.ID");
  343. };
  344. bills.prototype.getTechLeavesID=function () {//取所有分计算技术措施项目清单叶子节点ID
  345. let items = projectObj.project.mainTree.roots;//所有节点;
  346. let techNode = null;
  347. for(let item of items){
  348. if(isFlag(item.data)&&item.data.flagsIndex.fixed.flag==fixedFlag.CONSTRUCTION_TECH){
  349. techNode = item;
  350. break;
  351. }
  352. }
  353. let nodes = this.getLeavesBillNodes(techNode);
  354. return _.map(nodes,"data.ID");
  355. };
  356. bills.prototype.getLeavesBillNodes = function (rnode) {//取该节点下的所有清单叶子节点
  357. let leaves = [];
  358. getLeaves(rnode,leaves);
  359. return leaves;
  360. function getLeaves(node,children) {
  361. if(node){
  362. if(node.source.children.length>0){
  363. for(let c of node.children){
  364. getLeaves(c,children)
  365. }
  366. }else {
  367. children.push(node);
  368. }
  369. }
  370. }
  371. };
  372. bills.prototype.getRootNode = function (node) {
  373. if(node.parent){
  374. return this.getRootNode(node.parent)
  375. }else {
  376. return node
  377. }
  378. };
  379. bills.prototype.isFBFX = function (node) {//判读是否属于分部分项
  380. let rootNode = this.getRootNode(node);
  381. if(isFlag(rootNode.data)&&rootNode.data.flagsIndex.fixed.flag==fixedFlag.SUB_ENGINERRING){
  382. return true
  383. }else {
  384. return false
  385. }
  386. };
  387. return new bills(project);
  388. }
  389. };
  390. function isDef(v) {
  391. return v !== undefined && v !== null;
  392. }
  393. function isFlag(v) {
  394. return this.isDef(v.flagsIndex) && this.isDef(v.flagsIndex.fixed);
  395. }