bills.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 = stdBillsData.type;//插入清单类型
  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.singleDeleteBills=function(node,controller){
  225. let updateData = {};
  226. let updateNode={};
  227. let me = this;
  228. if(node){
  229. updateData[node.data.ID]=true;
  230. if(node.children.length>0){//有子项
  231. if(node.preSibling){//有前兄弟,则子项变成前兄弟的子项
  232. if(node.preSibling.children.length>0){//前兄弟有子项,
  233. let preNode = node.preSibling.children[node.preSibling.children.length-1];
  234. updateData[preNode.data.ID]={
  235. NextSiblingID:node.children[0].data.ID
  236. }
  237. updateNode[preNode.data.ID] = preNode;
  238. }
  239. for(let i=0;i<node.children.length;i++){
  240. updateData[node.children[i].data.ID]={
  241. ParentID:node.preSibling.data.ID
  242. };
  243. updateNode[node.children[i].data.ID]=node.children[i];
  244. }
  245. }else {//没有前兄弟,则子项升一级
  246. let parent = node.nextSibling.parent;
  247. for(let i=0;i<node.children.length;i++){
  248. if(i == node.children.length-1&&node.nextSibling){//最后一个子项,在有后兄弟的情况下,作为后兄弟的前兄弟
  249. updateData[node.children[i].data.ID]={
  250. ParentID:parent.data.ID,
  251. NextSiblingID:node.nextSibling.data.ID
  252. };
  253. }else {
  254. updateData[node.children[i].data.ID]={
  255. ParentID:parent.data.ID
  256. };
  257. }
  258. updateNode[node.children[i].data.ID]=node.children[i];
  259. }
  260. }
  261. }
  262. $.bootstrapLoading.start();
  263. CommonAjax.post("/bills/singleDelete", {updateData:updateData,projectID:node.data.projectID,user_id:userID}, function () {
  264. controller.singleDelete();//删除树节点
  265. me.tree.singleDelete(node);
  266. //更新缓存
  267. console.log(updateNode);
  268. _.remove(me.datas,{'ID':node.data.ID});
  269. for(let n_key in updateNode){
  270. let updateDoc = updateData[n_key];
  271. for(let u_key in updateDoc){
  272. updateNode[n_key].data[u_key] =updateDoc[u_key];
  273. }
  274. }
  275. $.bootstrapLoading.end();
  276. }, function () {
  277. $.bootstrapLoading.end();
  278. });
  279. }
  280. };
  281. bills.prototype.upMoveBills = function (node) {
  282. var upMoveData = node.getUpMoveData();
  283. project.pushNow('upMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(upMoveData, this.project.ID()));
  284. return node.upMove();
  285. };
  286. bills.prototype.downMoveBills = function (node) {
  287. var downMoveData = node.getDownMoveData();
  288. project.pushNow('downMoveBills', this.getSourceType(), tools.coverseTreeUpdateData(downMoveData, this.project.ID()));
  289. return node.downMove();
  290. };
  291. bills.prototype.upLevelBills = function (node) {
  292. var upLevelData = node.getUpLevelData();
  293. project.pushNow('upLevelBills', this.getSourceType(), tools.coverseTreeUpdateData(upLevelData, this.project.ID()));
  294. return node.upLevel();
  295. };
  296. bills.prototype.downLevelBills = function (node) {
  297. var downLevelData = node.getDownLevelData();
  298. project.pushNow('downLevelBills', [this.getSourceType()], [tools.coverseTreeUpdateData(downLevelData, this.project.ID())]);
  299. return node.downLevel();
  300. };
  301. bills.prototype.updateField = function (node, field, newValue) {
  302. calcFees.setFee(node.data, field, newValue);
  303. let updateData = [];
  304. let data = {'ID': node.getID(), 'projectID': this.project.ID()};
  305. data[field] = newValue;
  306. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(data)});
  307. this.project.pushNow('updateBills', this.getSourceType(), updateData);
  308. };
  309. bills.prototype.getUpdateAllData = function () {
  310. let updateData = [];
  311. for (let data of this.datas) {
  312. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(data)});
  313. }
  314. return updateData;
  315. };
  316. bills.prototype.updateAll = function () {
  317. this.project.pushNow('updateAllBills', this.getSourceType(), this.getUpdateAllData());
  318. };
  319. bills.prototype.getUpdateNodesData = function (nodes) {
  320. let updateData = [];
  321. for (let node of nodes) {
  322. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(node.data)});
  323. }
  324. return updateData;
  325. }
  326. bills.prototype.updateNodes = function (nodes, updateNow) {
  327. if (updateNow) {
  328. this.project.pushNow('updateBills', this.getSourceType(), this.getUpdateNodesData(nodes));
  329. } else {
  330. this.project.push(this.getSourceType(), this.getUpdateNodesData(nodes));
  331. }
  332. };
  333. bills.prototype.sameStdCode = function (stdCode, filterCode) {
  334. let reg = new RegExp('^' + stdCode), matchs= [];
  335. for (let data of this.datas) {
  336. if (data.code && data.code.length === 12 && reg.test(data.code) && data.code !== filterCode) {
  337. matchs.push(data.code);
  338. }
  339. }
  340. return matchs;
  341. }
  342. bills.prototype.newFormatCode = function (stdCode, filterCode) {
  343. let matchs = this.sameStdCode(stdCode, filterCode);
  344. let format = function (Number) {
  345. let s = Number + '';
  346. while (s.length < 3) {
  347. s = '0' + s;
  348. }
  349. return s;
  350. }
  351. for (i = 0; i <= matchs.length; i++) {
  352. let formatCode = stdCode + format(i+1);
  353. if (matchs.indexOf(formatCode) === -1) {
  354. return formatCode;
  355. }
  356. }
  357. };
  358. bills.prototype.replaceBills = function (node, stdBillsData, code) {
  359. let updateData = [];
  360. node.data.code = code;
  361. if (stdBillsData) {
  362. node.data.name = stdBillsData.name;
  363. node.data.unit = stdBillsData.unit;
  364. // 工程量计算规则
  365. node.data.ruleText = stdBillsData.ruleText;
  366. // 说明(补注)
  367. node.data.comments = stdBillsData.recharge;
  368. // 工作内容
  369. node.data.jobContent = stdBillsData.jobContent;
  370. node.data.jobContentText = stdBillsData.jobContentText;
  371. // 特征
  372. node.data.itemCharacter = stdBillsData.itemCharacter;
  373. node.data.itemCharacterText = stdBillsData.itemCharacterText;
  374. node.data.programID = stdBillsData.engineering;
  375. }
  376. updateData.push({'updateType': 'ut_update', 'updateData': tools.formatBillsUpdateData(node.data)});
  377. this.project.pushNow('replaceBills', this.getSourceType(), updateData);
  378. return node;
  379. };
  380. bills.prototype.sameStdCodeBillsData = function (stdCode) {
  381. let reg = new RegExp('^' + stdCode);
  382. for (let data of this.datas) {
  383. if (data.code && data.code.length === 12 && reg.test(data.code) && /^[\d]+$/.test(data.code)) {
  384. return data;
  385. }
  386. }
  387. return null;
  388. };
  389. bills.prototype.getSubdivisionProjectLeavesID=function () {//取所有分部分项工程清单叶子节点ID
  390. let roots = projectObj.project.mainTree.roots;//所有根节点
  391. let subdivisionNode = null;
  392. for(let r of roots){
  393. if(isFlag(r.data)&&r.data.flagsIndex.fixed.flag==fixedFlag.SUB_ENGINERRING) {
  394. subdivisionNode = r;
  395. break;
  396. }
  397. }
  398. let nodes = this.getLeavesBillNodes(subdivisionNode);
  399. return _.map(nodes,"data.ID");
  400. };
  401. bills.prototype.getTechLeavesID=function () {//取所有分计算技术措施项目清单叶子节点ID
  402. let items = projectObj.project.mainTree.roots;//所有节点;
  403. let techNode = null;
  404. for(let item of items){
  405. if(isFlag(item.data)&&item.data.flagsIndex.fixed.flag==fixedFlag.CONSTRUCTION_TECH){
  406. techNode = item;
  407. break;
  408. }
  409. }
  410. let nodes = this.getLeavesBillNodes(techNode);
  411. return _.map(nodes,"data.ID");
  412. };
  413. bills.prototype.getLeavesBillNodes = function (rnode) {//取该节点下的所有清单叶子节点
  414. let leaves = [];
  415. getLeaves(rnode,leaves);
  416. return leaves;
  417. function getLeaves(node,children) {
  418. if(node){
  419. if(node.source.children.length>0){
  420. for(let c of node.children){
  421. getLeaves(c,children)
  422. }
  423. }else {
  424. children.push(node);
  425. }
  426. }
  427. }
  428. };
  429. bills.prototype.getRootNode = function (node) {
  430. if(node.parent){
  431. return this.getRootNode(node.parent);
  432. }else {
  433. return node;
  434. }
  435. };
  436. bills.prototype.isFBFX = function (node) {//判读是否属于分部分项
  437. let rootNode = this.getRootNode(node);
  438. if(isFlag(rootNode.data)&&rootNode.data.flagsIndex.fixed.flag==fixedFlag.SUB_ENGINERRING){
  439. return true;
  440. }else {
  441. return false;
  442. }
  443. };
  444. bills.prototype.isEngineeringCost = function (node) {//判断这个节点是否是工程造价节点
  445. if(isFlag(node.data)&&node.data.flagsIndex.fixed.flag==fixedFlag.ENGINEERINGCOST){
  446. return true;
  447. }else {
  448. return false;
  449. }
  450. };
  451. return new bills(project);
  452. }
  453. };
  454. function isDef(v) {
  455. return v !== undefined && v !== null;
  456. }
  457. function isFlag(v) {
  458. return this.isDef(v.flagsIndex) && this.isDef(v.flagsIndex.fixed);
  459. }
  460. function getRootFixedNode(node) {
  461. let parent = node.parent;
  462. if(isFlag(node.data) && (node.data.flagsIndex.fixed.flag === fixedFlag.SUB_ENGINERRING
  463. || node.data.flagsIndex.fixed.flag === fixedFlag.MEASURE
  464. || node.data.flagsIndex.fixed.flag === fixedFlag.OTHER
  465. || node.data.flagsIndex.fixed.flag === fixedFlag.CHARGE
  466. || node.data.flagsIndex.fixed.flag === fixedFlag.TAX)){
  467. return node;
  468. }
  469. else {
  470. if(!parent){
  471. return node;
  472. }
  473. else {
  474. return getRootFixedNode(parent);
  475. }
  476. }
  477. }