ration_item.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 adjGljId = data.gljId, adjBasePrice = data.basePrice, adjGljType = data.gljType,
  206. repId = data.repId, lastOpr = data.lastOpr;
  207. async.waterfall([
  208. function (cb) {
  209. rationItemModel.find({'rationGljList.gljId': adjGljId}, function (err, result) {
  210. if(err){
  211. cb(err);
  212. }
  213. else{
  214. cb(null, result);
  215. }
  216. });
  217. },
  218. function (result, cb) {
  219. async.each(result, function (rationItem, ecb) {
  220. let rationGljList = rationItem.rationGljList,
  221. gljIds = [];
  222. rationGljList.forEach(function (rationGlj) {
  223. gljIds.push(rationGlj.gljId);
  224. });
  225. gljDao.getGljItems(gljIds, function(err, gljItems){
  226. if(err){
  227. ecb(err);
  228. }
  229. else{
  230. let gljArr = [];
  231. for(let i=0; i<gljItems.length; i++){
  232. let gljParentType = -1;
  233. if(gljItems[i].ID === adjGljId){
  234. gljItems[i].gljType = adjGljType;
  235. }
  236. if(gljItems[i].gljType <= 3){
  237. gljParentType = gljItems[i].gljType;
  238. }
  239. if(gljItems[i].gljType > 200 && gljItems[i].gljType < 300){
  240. gljParentType = 2;
  241. }
  242. if(gljItems[i].gljType > 300 && gljItems[i].gljType < 400){
  243. gljParentType = 3;
  244. }
  245. if(gljItems[i].ID === adjGljId){
  246. gljArr.push({gljId: gljItems[i].ID, basePrice: adjBasePrice, gljParentType: gljParentType});
  247. }
  248. else {
  249. gljArr.push({gljId: gljItems[i].ID, basePrice: gljItems[i].basePrice, gljParentType: gljParentType});
  250. }
  251. }
  252. gljArr.forEach(function (gljItem) {
  253. rationGljList.forEach(function (rationGlj) {
  254. if(gljItem.gljId === rationGlj.gljId){
  255. gljItem.consumeAmt = rationGlj.consumeAmt;
  256. }
  257. })
  258. });
  259. //recalculate the price of ration
  260. let labourPrc = [], materialPrc = [], machinePrc = [], singlePrc, updatePrc = {labourPrice: 0, materialPrice: 0, machinePrice: 0, basePrice: 0};
  261. gljArr.forEach(function (gljItem) {
  262. if(gljItem.gljParentType !== -1){
  263. singlePrc = round(gljItem.basePrice * gljItem.consumeAmt, 3);
  264. if(gljItem.gljParentType === 1){
  265. labourPrc.push(singlePrc);
  266. }
  267. else if(gljItem.gljParentType ===2){
  268. materialPrc.push(singlePrc);
  269. }
  270. else{
  271. machinePrc.push(singlePrc);
  272. }
  273. }
  274. });
  275. if(labourPrc.length > 0){
  276. let sumLaP = 0;
  277. for(let i=0; i<labourPrc.length; i++){
  278. sumLaP += labourPrc[i];
  279. }
  280. updatePrc.labourPrice = round(sumLaP, 2);
  281. }
  282. if(materialPrc.length > 0){
  283. let sumMtP = 0;
  284. for(let i= 0; i<materialPrc.length; i++){
  285. sumMtP += materialPrc[i];
  286. }
  287. updatePrc.materialPrice = round(sumMtP, 2);
  288. }
  289. if(machinePrc.length > 0){
  290. let sumMaP = 0;
  291. for(let i =0; i< machinePrc.length; i++){
  292. sumMaP += machinePrc[i];
  293. }
  294. updatePrc.machinePrice = round(sumMaP, 2);
  295. }
  296. updatePrc.basePrice = updatePrc.labourPrice + updatePrc.materialPrice + updatePrc.machinePrice;
  297. //updateDataBase
  298. rationItemModel.update({ID: rationItem.ID}, {$set: {labourPrice: updatePrc.labourPrice, materialPrice: updatePrc.materialPrice,
  299. machinePrice: updatePrc.machinePrice, basePrice: updatePrc.basePrice}},
  300. function (err, result) {
  301. if(err){
  302. ecb(err);
  303. }
  304. else {
  305. ecb(null);
  306. }
  307. });
  308. }
  309. });
  310. }, function(err){
  311. if(err){
  312. cb(err);
  313. }
  314. else {
  315. cb(null);
  316. }
  317. });
  318. },
  319. function (cb) {
  320. rationRepositoryDao.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  321. if(err){
  322. cb(err);
  323. }
  324. else{
  325. cb(null);
  326. }
  327. })
  328. }
  329. ], function (err) {
  330. if(err){
  331. callback(err, 'Error');
  332. }
  333. else{
  334. callback(null, '');
  335. }
  336. });
  337. };
  338. rationItemDAO.prototype.getRationGljIds = function (data, callback) {
  339. let repId = data.repId;
  340. rationItemModel.find({rationRepId: repId}, function (err, result) {
  341. if(err){
  342. callback(err, 'Error', null);
  343. }
  344. else{
  345. let rstIds = [], newRst = [];
  346. result.forEach(function (data) {
  347. if(data.rationGljList.length >0){
  348. data.rationGljList.forEach(function (gljObj) {
  349. rstIds.push(gljObj.gljId);
  350. })
  351. }
  352. });
  353. for(let i= 0; i< rstIds.length; i++){
  354. if(newRst.indexOf(rstIds[i]) === -1){
  355. newRst.push(rstIds[i]);
  356. }
  357. }
  358. callback(null, '', newRst);
  359. }
  360. });
  361. };
  362. rationItemDAO.prototype.getRationsCodes = function (data, callback) {
  363. let repId = data.repId;
  364. rationItemModel.find({rationRepId: repId}, function (err, result) {
  365. if(err){
  366. callback(err, 'Error', null);
  367. }
  368. else{
  369. let rstCodes = [];
  370. result.forEach(function (rationItem) {
  371. rstCodes.push(rationItem.code);
  372. });
  373. callback(null, 'get all rationCodes success', rstCodes);
  374. }
  375. })
  376. };
  377. module.exports = new rationItemDAO()