bills.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Created by jimiz on 2017/4/1.
  3. */
  4. var mongoose = require("mongoose");
  5. var db = require("../db/project_db");
  6. var subSchema = require("./billsSubSchemas");
  7. var deleteSchema = require('../../../public/models/deleteSchema');
  8. var Schema = mongoose.Schema;
  9. var counter = require("../../../public/counter/counter.js");
  10. var billsSchema = new Schema({
  11. ID: Number,
  12. ParentID: Number,
  13. NextSiblingID: Number,
  14. projectID: Number,
  15. serialNo: Number,
  16. chapterID: Number,
  17. code: String,
  18. fullCode: String,
  19. name: String,
  20. unit: String,
  21. quantity: String, // Decimal
  22. programID: Number,
  23. comments: String,
  24. // 调价
  25. xs_Labour: String, // Decimal
  26. xs_Material: String, // Decimal
  27. xs_Machine: String, // Decimal
  28. xs_FeeRate: String, // Decimal
  29. xs_LabourPrice: String, // Decimal
  30. xs_MaterialPrice: String, // Decimal
  31. xs_MachinePrice: String, // Decimal
  32. isTender_Labour: Boolean,
  33. isTender_Material: Boolean,
  34. isTender_Machine: Boolean,
  35. tenderTargetPrice: String, // Decimal
  36. tenderTargetUnitPrice: String, // Decimal
  37. // 费用字段
  38. fees: [subSchema.feesSchema],
  39. // 标记字段
  40. flags: [subSchema.flagsSchema],
  41. deleteInfo: deleteSchema
  42. });
  43. var bills = db.model("bills", billsSchema);
  44. var billsDAO = function(){};
  45. billsDAO.prototype.getData = function(projectID, callback){
  46. bills.find({'$or': [{projectID: projectID, deleteInfo: null}, {projectID: projectID, 'deleteInfo.deleted': {$in: [null, false]}}]}, '-_id', function(err, datas){
  47. if (!err) {
  48. callback(0, '', datas);
  49. } else {
  50. callback(1, '', null);
  51. };
  52. });
  53. };
  54. // get All Project Bills, include deleted
  55. billsDAO.prototype.getProjectBills = function (projectId, callback) {
  56. if (callback) {
  57. bills.find({projectID: projectId}, '-_id').exec()
  58. .then(function (result, err) {
  59. if (err) {
  60. callback(1, '找不到模板', null);
  61. } else {
  62. callback(0, '', template);
  63. }
  64. });
  65. return null;
  66. } else {
  67. return bills.find({projectID: projectId}, '-_id').exec();
  68. }
  69. }
  70. billsDAO.prototype.AddBillsFromTemplate = function (datas, callback) {
  71. bills.collection.insert(datas, callback);
  72. };
  73. billsDAO.prototype.save = function(datas, callback){
  74. var data, errList = [], updateLength = 0;
  75. var updateFunc = function (err, errData) {
  76. if (err){
  77. errList.push(errData);
  78. };
  79. };
  80. if (datas){
  81. for (var i = 0; i < datas.length; i++){
  82. data = datas[i];
  83. if (data.updateType === 'update') {
  84. delete data.updateType;
  85. data.save(updateFunc);
  86. } else if (data.updateType === 'create') {
  87. delete data.updateType;
  88. data.save(updateFunc);
  89. } else if (data.updateType === 'delete') {
  90. delete data.updateType;
  91. data.remove(updateFunc);
  92. };
  93. };
  94. if (errList.length > 0){
  95. callback(1, errList);
  96. } else {
  97. callback(0, null);
  98. };
  99. };
  100. };
  101. billsDAO.prototype.getItemTemplate = function(callback){
  102. var data = new bills;
  103. /* to do: 需要根据标准配置库填充fees和flags字段,是否需要更多的参数? */
  104. callback(0, '', data);
  105. };
  106. billsDAO.prototype.allocIDs = function(IDStep, callback){
  107. counter.counterDAO.getIDAfterCount(counter.moduleName.bills, IDStep, function(err, highID){
  108. var lowID = highID - IDStep + 1;
  109. callback(0, '', {lowID: lowID, highID: highID});
  110. });
  111. };
  112. module.exports = new billsDAO();