ration_item.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var mongoose = require("mongoose");
  5. var dbm = require("../../../config/db/db_manager");
  6. var db = dbm.getCfgConnection("scConstruct");
  7. var async = require("async");
  8. var Schema = mongoose.Schema;
  9. var rationGljItemSchema = mongoose.Schema({
  10. gljId: Number,
  11. consumeAmt: Number,
  12. proportion: Number //配合比,暂时无需使用,默认0
  13. }, { _id: false });
  14. var rationAssItemSchema = mongoose.Schema({
  15. name: String,
  16. assistID: Number,
  17. assistCode: String,
  18. stdValue: String,
  19. stepValue: String,
  20. decimal: Number,
  21. carryBit: String,
  22. minValue: String,
  23. maxValue: String
  24. }, { _id: false });
  25. var rationItemSchema = mongoose.Schema({
  26. ID:Number,
  27. code: String,
  28. name: String,
  29. unit: String,
  30. labourPrice: Number,
  31. materialPrice: Number,
  32. machinePrice: Number,
  33. basePrice: Number,
  34. sectionId: Number,
  35. rationRepId: Number,
  36. caption: String,
  37. feeType: Number,
  38. rationGljList: [rationGljItemSchema],
  39. rationCoeList: Array,
  40. rationAssList: [rationAssItemSchema]
  41. });
  42. var rationItemModel = db.model("std_ration_lib_ration_items",rationItemSchema, "std_ration_lib_ration_items")
  43. var counter = require('../../../public/counter/counter');
  44. let gljDao = require('./glj_repository');
  45. let moment = require('moment');
  46. let rationRepositoryDao = require('./repository_map');
  47. var rationItemDAO = function(){};
  48. rationItemDAO.prototype.getRationItemsBySection = function(sectionId,callback){
  49. rationItemModel.find({"sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  50. if(err) callback(true, "Fail to get items", "")
  51. else callback(false,"Get items successfully", data);
  52. })
  53. };
  54. rationItemDAO.prototype.mixUpdateRationItems = function(rationLibId, lastOpr, sectionId, updateItems, addItems, rIds, callback){
  55. var me = this;
  56. if (updateItems.length == 0 && rIds.length == 0) {
  57. me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
  58. } else {
  59. me.removeRationItems(rationLibId, lastOpr, rIds, function(err, message, docs) {
  60. if (err) {
  61. callback(true, "Fail to remove", false);
  62. } else {
  63. me.updateRationItems(rationLibId, lastOpr, sectionId, updateItems, function(err, results){
  64. if (err) {
  65. callback(true, "Fail to save", false);
  66. } else {
  67. if (addItems && addItems.length > 0) {
  68. me.addRationItems(rationLibId, lastOpr, sectionId, addItems, callback);
  69. } else {
  70. callback(false, "Save successfully", results);
  71. }
  72. }
  73. });
  74. }
  75. })
  76. }
  77. };
  78. rationItemDAO.prototype.removeRationItems = function(rationLibId, lastOpr, rIds,callback){
  79. if (rIds.length > 0) {
  80. rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  81. if (err) {
  82. callback(true, "Fail to remove", false);
  83. } else {
  84. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  85. if(!err){
  86. callback(false, "Remove successfully", docs);
  87. }
  88. })
  89. }
  90. })
  91. } else {
  92. callback(false, "No records were deleted!", null);
  93. }
  94. };
  95. rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
  96. rationItemModel.find({"rationRepId": repId, "code": {'$regex': code, $options: '$i'}, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
  97. if(err) callback(true, "Fail to get items", "")
  98. else callback(false,"Get items successfully", data);
  99. })
  100. };
  101. rationItemDAO.prototype.findRation = function (repId, keyword, callback) {
  102. var filter = {
  103. 'rationRepId': repId,
  104. '$and': [{
  105. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  106. }, {
  107. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  108. }]
  109. };
  110. rationItemModel.find(filter, function (err, data) {
  111. if (err) {
  112. callback(true, 'Fail to find ration', null);
  113. } else {
  114. callback(false, '', data);
  115. }
  116. })
  117. }
  118. rationItemDAO.prototype.getRationItem = function (repId, code, callback) {
  119. if (callback) {
  120. rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec()
  121. .then(function (result, err) {
  122. if (err) {
  123. callback(1, '找不到定额“' + code +'”' , null);
  124. } else {
  125. callback(0, '', result);
  126. }
  127. });
  128. return null;
  129. } else {
  130. return rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec();
  131. }
  132. };
  133. rationItemDAO.prototype.addRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  134. if (items && items.length > 0) {
  135. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  136. var maxId = result.value.sequence_value;
  137. var arr = [];
  138. for (var i = 0; i < items.length; i++) {
  139. var obj = new rationItemModel(items[i]);
  140. obj.ID = (maxId - (items.length - 1) + i);
  141. obj.sectionId = sectionId;
  142. obj.rationRepId = rationLibId;
  143. arr.push(obj);
  144. }
  145. rationItemModel.collection.insert(arr, null, function(err, docs){
  146. if (err) {
  147. callback(true, "Fail to save", false);
  148. } else {
  149. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  150. if(err){
  151. callback(true, "Fail to sava operator", false);
  152. }
  153. else{
  154. callback(false, "Save successfully", docs);
  155. }
  156. })
  157. }
  158. })
  159. });
  160. } else {
  161. callback(true, "Source error!", false);
  162. }
  163. };
  164. rationItemDAO.prototype.updateRationItems = function(rationLibId, lastOpr, sectionId, items,callback){
  165. var functions = [];
  166. for (var i=0; i < items.length; i++) {
  167. functions.push((function(doc) {
  168. return function(cb) {
  169. var filter = {};
  170. if (doc.ID) {
  171. filter.ID = doc.ID;
  172. } else {
  173. filter.sectionId = sectionId;
  174. if (rationLibId) filter.rationRepId = rationLibId;
  175. filter.code = doc.code;
  176. }
  177. rationItemModel.update(filter, doc, cb);
  178. };
  179. })(items[i]));
  180. }
  181. functions.push((function () {
  182. return function (cb) {
  183. rationRepositoryDao.updateOprArr({ID: rationLibId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  184. if(err){
  185. cb(err);
  186. }
  187. else{
  188. cb(null);
  189. }
  190. });
  191. }
  192. })());
  193. async.parallel(functions, function(err, results) {
  194. callback(err, results);
  195. });
  196. };
  197. //ration round func
  198. function round(v,e){
  199. var t=1;
  200. for(;e>0;t*=10,e--);
  201. for(;e<0;t/=10,e++);
  202. return Math.round(v*t)/t;
  203. }
  204. rationItemDAO.prototype.updateRationBasePrc = function (data, callback) {
  205. let basePrcArr = data.basePrcArr,
  206. // adjGljId = data.gljId, adjBasePrice = data.basePrice, adjGljType = data.gljType,
  207. repId = data.repId, lastOpr = data.lastOpr;
  208. //
  209. let updateArr;
  210. async.each(basePrcArr, function (basePrcObj, finalCb) {
  211. let adjGljId = basePrcObj.gljId, adjBasePrice = basePrcObj.basePrice, adjGljType = basePrcObj.gljType;
  212. async.waterfall([
  213. function (cb) {
  214. rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) {
  215. if(err){
  216. cb(err);
  217. }
  218. else{
  219. cb(null, result);
  220. }
  221. });
  222. },
  223. function (result, cb) {
  224. async.each(result, function (rationItem, ecb) {
  225. let rationGljList = rationItem.rationGljList,
  226. gljIds = [];
  227. rationGljList.forEach(function (rationGlj) {
  228. gljIds.push(rationGlj.gljId);
  229. });
  230. gljDao.getGljItems(gljIds, function(err, gljItems){
  231. if(err){
  232. ecb(err);
  233. }
  234. else{
  235. let gljArr = [];
  236. for(let i=0; i<gljItems.length; i++){
  237. let gljParentType = -1;
  238. if(gljItems[i].ID === adjGljId){
  239. gljItems[i].gljType = adjGljType;
  240. }
  241. if(gljItems[i].gljType <= 3){
  242. gljParentType = gljItems[i].gljType;
  243. }
  244. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  245. gljParentType = 2;
  246. }
  247. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  248. gljParentType = 3;
  249. }
  250. if(gljItems[i].ID === adjGljId){
  251. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  252. }
  253. else {
  254. gljArr.push({gljId: gljItems[i].ID, basePrice: gljItems[i].basePrice, gljParentType: gljParentType});
  255. }
  256. }
  257. gljArr.forEach(function (gljItem) {
  258. rationGljList.forEach(function (rationGlj) {
  259. if(gljItem.gljId === rationGlj.gljId){
  260. gljItem.consumeAmt = rationGlj.consumeAmt;
  261. }
  262. })
  263. });
  264. //recalculate the price of ration
  265. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  266. gljArr.forEach(function (gljItem) {
  267. if(gljItem.gljParentType !== -1){
  268. singlePrc = round(gljItem.basePrice * gljItem.consumeAmt, 3);
  269. if(gljItem.gljParentType === 1){
  270. labourPrc.push(singlePrc);
  271. }
  272. else if(gljItem.gljParentType ===2){
  273. materialPrc.push(singlePrc);
  274. }
  275. else{
  276. machinePrc.push(singlePrc);
  277. }
  278. }
  279. });
  280. if(labourPrc.length > 0){
  281. let sumLaP = 0;
  282. for(let i=0; i<labourPrc.length; i++){
  283. sumLaP += labourPrc[i];
  284. }
  285. updatePrc.labourPrice = round(sumLaP, 2);
  286. }
  287. if(materialPrc.length > 0){
  288. let sumMtP = 0;
  289. for(let i= 0; i<materialPrc.length; i++){
  290. sumMtP += materialPrc[i];
  291. }
  292. updatePrc.materialPrice = round(sumMtP, 2);
  293. }
  294. if(machinePrc.length > 0){
  295. let sumMaP = 0;
  296. for(let i =0; i< machinePrc.length; i++){
  297. sumMaP += machinePrc[i];
  298. }
  299. updatePrc.machinePrice = round(sumMaP, 2);
  300. }
  301. updatePrc.basePrice = updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice;
  302. //updateDataBase
  303. rationItemModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice, materialPrice: updatePrc.materialPrice,
  304. machinePrice: updatePrc.machinePrice, basePrice: updatePrc.basePrice}},
  305. function (err, result) {
  306. if(err){
  307. ecb(err);
  308. }
  309. else {
  310. ecb(null);
  311. }
  312. });
  313. }
  314. });
  315. }, function(err){
  316. if(err){
  317. cb(err);
  318. }
  319. else {
  320. cb(null);
  321. }
  322. });
  323. },
  324. function (cb) {
  325. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  326. if(err){
  327. cb(err);
  328. }
  329. else{
  330. cb(null);
  331. }
  332. })
  333. }
  334. ], function (err) {
  335. if(err){
  336. finalCb(err);
  337. }
  338. else{
  339. finalCb(null);
  340. }
  341. });
  342. }, function (err) {
  343. if(err){
  344. callback(err, 'Error');
  345. }
  346. else{
  347. callback(null, '');
  348. }
  349. });
  350. //
  351. /*async.waterfall([
  352. function (cb) {
  353. rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) {
  354. if(err){
  355. cb(err);
  356. }
  357. else{
  358. cb(null, result);
  359. }
  360. });
  361. },
  362. function (result, cb) {
  363. async.each(result, function (rationItem, ecb) {
  364. let rationGljList = rationItem.rationGljList,
  365. gljIds = [];
  366. rationGljList.forEach(function (rationGlj) {
  367. gljIds.push(rationGlj.gljId);
  368. });
  369. gljDao.getGljItems(gljIds, function(err, gljItems){
  370. if(err){
  371. ecb(err);
  372. }
  373. else{
  374. let gljArr = [];
  375. for(let i=0; i<gljItems.length; i++){
  376. let gljParentType = -1;
  377. if(gljItems[i].ID === adjGljId){
  378. gljItems[i].gljType = adjGljType;
  379. }
  380. if(gljItems[i].gljType <= 3){
  381. gljParentType = gljItems[i].gljType;
  382. }
  383. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  384. gljParentType = 2;
  385. }
  386. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  387. gljParentType = 3;
  388. }
  389. if(gljItems[i].ID === adjGljId){
  390. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  391. }
  392. else {
  393. gljArr.push({gljId: gljItems[i].ID, basePrice: gljItems[i].basePrice, gljParentType: gljParentType});
  394. }
  395. }
  396. gljArr.forEach(function (gljItem) {
  397. rationGljList.forEach(function (rationGlj) {
  398. if(gljItem.gljId === rationGlj.gljId){
  399. gljItem.consumeAmt = rationGlj.consumeAmt;
  400. }
  401. })
  402. });
  403. //recalculate the price of ration
  404. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  405. gljArr.forEach(function (gljItem) {
  406. if(gljItem.gljParentType !== -1){
  407. singlePrc = round(gljItem.basePrice * gljItem.consumeAmt, 3);
  408. if(gljItem.gljParentType === 1){
  409. labourPrc.push(singlePrc);
  410. }
  411. else if(gljItem.gljParentType ===2){
  412. materialPrc.push(singlePrc);
  413. }
  414. else{
  415. machinePrc.push(singlePrc);
  416. }
  417. }
  418. });
  419. if(labourPrc.length > 0){
  420. let sumLaP = 0;
  421. for(let i=0; i<labourPrc.length; i++){
  422. sumLaP += labourPrc[i];
  423. }
  424. updatePrc.labourPrice = round(sumLaP, 2);
  425. }
  426. if(materialPrc.length > 0){
  427. let sumMtP = 0;
  428. for(let i= 0; i<materialPrc.length; i++){
  429. sumMtP += materialPrc[i];
  430. }
  431. updatePrc.materialPrice = round(sumMtP, 2);
  432. }
  433. if(machinePrc.length > 0){
  434. let sumMaP = 0;
  435. for(let i =0; i< machinePrc.length; i++){
  436. sumMaP += machinePrc[i];
  437. }
  438. updatePrc.machinePrice = round(sumMaP, 2);
  439. }
  440. updatePrc.basePrice = updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice;
  441. //updateDataBase
  442. rationItemModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice, materialPrice: updatePrc.materialPrice,
  443. machinePrice: updatePrc.machinePrice, basePrice: updatePrc.basePrice}},
  444. function (err, result) {
  445. if(err){
  446. ecb(err);
  447. }
  448. else {
  449. ecb(null);
  450. }
  451. });
  452. }
  453. });
  454. }, function(err){
  455. if(err){
  456. cb(err);
  457. }
  458. else {
  459. cb(null);
  460. }
  461. });
  462. },
  463. function (cb) {
  464. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  465. if(err){
  466. cb(err);
  467. }
  468. else{
  469. cb(null);
  470. }
  471. })
  472. }
  473. ], function (err) {
  474. if(err){
  475. callback(err, 'Error');
  476. }
  477. else{
  478. callback(null, '');
  479. }
  480. });*/
  481. };
  482. rationItemDAO.prototype.getRationGljIds = function (data, callback) {
  483. let repId = data.repId;
  484. rationItemModel.find({rationRepId: repId}, function (err, result) {
  485. if(err){
  486. callback(err, 'Error', null);
  487. }
  488. else{
  489. let rstIds = [], newRst = [];
  490. result.forEach(function (data) {
  491. if(data.rationGljList.length >0){
  492. data.rationGljList.forEach(function (gljObj) {
  493. rstIds.push(gljObj.gljId);
  494. })
  495. }
  496. });
  497. for(let i= 0; i< rstIds.length; i++){
  498. if(newRst.indexOf(rstIds[i]) === -1){
  499. newRst.push(rstIds[i]);
  500. }
  501. }
  502. callback(null, '', newRst);
  503. }
  504. });
  505. };
  506. rationItemDAO.prototype.getRationsCodes = function (data, callback) {
  507. let repId = data.repId;
  508. rationItemModel.find({rationRepId: repId}, function (err, result) {
  509. if(err){
  510. callback(err, 'Error', null);
  511. }
  512. else{
  513. let rstCodes = [];
  514. result.forEach(function (rationItem) {
  515. rstCodes.push(rationItem.code);
  516. });
  517. callback(null, 'get all rationCodes success', rstCodes);
  518. }
  519. })
  520. };
  521. module.exports = new rationItemDAO()