Advertisement
Guest User

entire

a guest
May 11th, 2023
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. const items = req.body.Items;
  2. const PhoneNumber = req.body.phoneno;
  3. const paymentMethod = req.body.paymentMethod;
  4.  
  5. let failureArr = [],
  6. billingData = [],
  7. TotalAmount = 0,
  8. TotalProfit = 0;
  9.  
  10. for(let j = 0;j<items.length;j++){
  11. const elem = items[j];
  12. try {
  13. const doc = await shopCollection.findOne({ Barcode: elem.Barcode });
  14. if (!doc) {
  15. failureArr.push({ message: "cant find barcode " + elem.Barcode });
  16. return;
  17. }
  18.  
  19. await shopCollection.updateOne(
  20. { Barcode: elem.Barcode },
  21. {
  22. $set: {
  23. Quantity: { $subtract: ["$Quantity", -elem.Quantity] },
  24. SalesQuantity: {
  25. $add: [{ $ifNull: ["$SalesQuantity", 0] }, elem.Quantity],
  26. },
  27. Profit: { $subtract: ["$SalesPrice", "$CostPrice"] },
  28. TotalProfit: {
  29. $add: ["$TotalProfit", { $multiply: ["$Profit", elem.Quantity] }],
  30. },
  31. SalesTotal: {
  32. $add: [
  33. "$SalesTotal",
  34. { $multiply: ["$SalesPrice", elem.Quantity] },
  35. ],
  36. },
  37. },
  38. },
  39. { upsert: true, returnOriginal: false }
  40. );
  41.  
  42. const updatedDoc = await shopCollection.findOne({ Barcode: elem.Barcode });
  43.  
  44. billingData.push({
  45. Product: updatedDoc.Product,
  46. Price: updatedDoc.SalesPrice,
  47. Quantity: elem.Quantity,
  48. Amount: elem.Quantity * updatedDoc.SalesPrice,
  49. });
  50. TotalAmount += elem.Quantity * updatedDoc.SalesPrice;
  51. TotalProfit += (doc.CostPrice - updatedDoc.SalesPrice) * elem.Quantity;
  52. } catch (err) {
  53. failureArr.push({
  54. code: err,
  55. message: `failed to subtract quantity from ${elem.Barcode}`,
  56. });
  57. }
  58. };
  59.  
  60. const date = new Date();
  61. const day = date.getDate().toString().padStart(2, "0");
  62. const month = (date.getMonth() + 1).toString().padStart(2, "0");
  63. const year = date.getFullYear().toString().slice(-2);
  64. const formattedDate = `${day}-${month}-${year}`;
  65.  
  66. const filter = { date: formattedDate };
  67.  
  68. const setOnInsertUpdate = {
  69. $setOnInsert: {
  70. Date: formattedDate,
  71. Volume: 0,
  72. Profit: 0,
  73. Customers: 0,
  74. Log: [],
  75. }
  76. };
  77.  
  78. const incAndPushUpdate = {
  79. $inc: {
  80. Customers: 1,
  81. Volume: TotalAmount,
  82. Profit: TotalProfit,
  83. },
  84. $push: {
  85. Log: {
  86. Bill: billingData,
  87. Amount: TotalAmount,
  88. Profit: TotalProfit,
  89. Id: PhoneNumber ? PhoneNumber : "Unknown",
  90. },
  91. },
  92. };
  93.  
  94. const options = { upsert: true, returnOriginal: false };
  95.  
  96. // find the entry and update or create it
  97. shoppingLog.findOneAndUpdate(filter, setOnInsertUpdate, options)
  98. .then(() => {
  99. return shoppingLog.findOneAndUpdate(filter, incAndPushUpdate, options);
  100. })
  101. .catch((err) => {
  102. console.error(err);
  103. });
  104.  
  105.  
  106. if (PhoneNumber) {
  107. const upd1 = {
  108. $set: {
  109. LastVisited: formattedDate,
  110. TimesVisited: {
  111. $add: [{ $ifNull: ["$TimesVisited", 0] }, 1],
  112. },
  113. PurchaseVolume: {
  114. $add: [{ $ifNull: ["$PurchaseVolume", 0] }, TotalAmount],
  115. },
  116. Profit: {
  117. $add: [{ $ifNull: ["$Profit", 0] }, TotalProfit],
  118. },
  119. },
  120. $push: {
  121. Log: {
  122. Bill: billingData,
  123. Amount: TotalAmount,
  124. Profit: TotalProfit,
  125. Id: PhoneNumber ? PhoneNumber : "Unknown",
  126. Method: paymentMethod,
  127. },
  128. },
  129. };
  130.  
  131. const opts = { returnOriginal: false };
  132.  
  133. // find the entry and update or create it
  134. usersCollection.findOneAndUpdate(
  135. { PhoneNumber: PhoneNumber },
  136. upd1,
  137. opts,
  138. function (err, result) {
  139. if (err) {
  140. failureArr.push({
  141. message: "Updaing UsersCollection Failed",
  142. code: err,
  143. result,
  144. upd1,
  145. });
  146. }
  147. }
  148. );
  149. }
  150.  
  151. if (failureArr.length === 0) {
  152. return res
  153. .status(200)
  154. .json({ message: "Success", billingData, TotalAmount });
  155. } else {
  156. return res.status(400).json({
  157. message: "Somestuff failed",
  158. billingData,
  159. failureArr,
  160. TotalAmount,
  161. });
  162. }
  163. });
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement