You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

468 lines
13 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package service;
  2. import Common;
  3. import tink.core.Error;
  4. /**
  5. * Order Service
  6. * @author web-wizard,fbarbut
  7. */
  8. class OrderService {
  9. static function canHaveFloatQt(product:db.Product):Bool {
  10. return product.hasFloatQt || product.wholesale || product.variablePrice;
  11. }
  12. /**
  13. * Make a product Order
  14. *
  15. * @param quantity
  16. * @param productId
  17. */
  18. public static function make(user:db.User, quantity:Float, product:db.Product, ?distribId:Int, ?paid:Bool, ?user2:db.User, ?invert:Bool):db.UserContract {
  19. var t = sugoi.i18n.Locale.texts;
  20. if (product.contract.type == db.Contract.TYPE_VARORDER && distribId == null)
  21. throw "You have to provide a distribId";
  22. if (quantity == null)
  23. throw "Quantity is null";
  24. // quantity
  25. if (!canHaveFloatQt(product)) {
  26. if (!tools.FloatTool.isInt(quantity)) {
  27. throw new tink.core.Error(t._("Error : product \"::product::\" quantity should be integer", {product: product.name}));
  28. }
  29. }
  30. // multiweight : make one row per product
  31. if (product.multiWeight && quantity > 1.0) {
  32. if (product.multiWeight && quantity != Math.abs(quantity))
  33. throw t._("multi-weighing products should be ordered only with integer quantities");
  34. var o = null;
  35. for (i in 0...Math.round(quantity)) {
  36. o = make(user, 1, product, distribId, paid, user2, invert);
  37. }
  38. return o;
  39. }
  40. // checks
  41. if (quantity <= 0)
  42. return null;
  43. // check for previous orders on the same distrib
  44. var prevOrders = new List<db.UserContract>();
  45. if (distribId == null) {
  46. prevOrders = db.UserContract.manager.search($product == product && $user == user, true);
  47. } else {
  48. prevOrders = db.UserContract.manager.search($product == product && $user == user && $distributionId == distribId, true);
  49. }
  50. // Create order object
  51. var o = new db.UserContract();
  52. o.product = product;
  53. o.quantity = quantity;
  54. o.productPrice = product.price;
  55. if (product.contract.hasPercentageOnOrders()) {
  56. o.feesRate = product.contract.percentageValue;
  57. }
  58. o.user = user;
  59. if (user2 != null) {
  60. o.user2 = user2;
  61. if (invert != null)
  62. o.flags.set(InvertSharedOrder);
  63. }
  64. if (paid != null)
  65. o.paid = paid;
  66. if (distribId != null)
  67. o.distribution = db.Distribution.manager.get(distribId);
  68. // cumulate quantities if there is a similar previous order
  69. if (prevOrders.length > 0 && !product.multiWeight) {
  70. for (prevOrder in prevOrders) {
  71. // if (!prevOrder.paid) {
  72. o.quantity += prevOrder.quantity;
  73. prevOrder.delete();
  74. // }
  75. }
  76. }
  77. // create a basket object
  78. if (distribId != null) {
  79. var dist = o.distribution;
  80. var basket = db.Basket.getOrCreate(user, dist.place, dist.date);
  81. o.basket = basket;
  82. }
  83. o.insert();
  84. // Stocks
  85. if (o.product.stock != null) {
  86. var c = o.product.contract;
  87. if (c.hasStockManagement()) {
  88. // trace("stock for "+quantity+" x "+product.name);
  89. if (o.product.stock == 0) {
  90. if (App.current.session != null) {
  91. App.current.session.addMessage(t._("There is no more '::productName::' in stock, we removed it from your order",
  92. {productName: o.product.name}), true);
  93. }
  94. o.quantity -= quantity;
  95. if (o.quantity <= 0) {
  96. o.delete();
  97. return null;
  98. }
  99. } else if (o.product.stock - quantity < 0) {
  100. var canceled = quantity - o.product.stock;
  101. o.quantity -= canceled;
  102. o.update();
  103. if (App.current.session != null) {
  104. var msg = t._("We reduced your order of '::productName::' to quantity ::oQuantity:: because there is no available products anymore",
  105. {productName: o.product.name, oQuantity: o.quantity});
  106. App.current.session.addMessage(msg, true);
  107. }
  108. o.product.lock();
  109. o.product.stock = 0;
  110. o.product.update();
  111. App.current.event(StockMove({product: o.product, move: 0 - (quantity - canceled)}));
  112. } else {
  113. o.product.lock();
  114. o.product.stock -= quantity;
  115. o.product.update();
  116. App.current.event(StockMove({product: o.product, move: 0 - quantity}));
  117. }
  118. }
  119. }
  120. return o;
  121. }
  122. /**
  123. * Edit an existing order (quantity)
  124. */
  125. public static function edit(order:db.UserContract, newquantity:Float, ?paid:Bool, ?user2:db.User, ?invert:Bool) {
  126. var t = sugoi.i18n.Locale.texts;
  127. order.lock();
  128. // quantity
  129. if (newquantity == null)
  130. newquantity = 0;
  131. if (!canHaveFloatQt(order.product)) {
  132. if (!tools.FloatTool.isInt(newquantity)) {
  133. throw new tink.core.Error(t._("Error : product \"::product::\" quantity should be integer", {product: order.product.name}));
  134. }
  135. }
  136. // paid
  137. if (paid != null) {
  138. order.paid = paid;
  139. } else {
  140. if (order.quantity < newquantity)
  141. order.paid = false;
  142. }
  143. // shared order
  144. if (user2 != null) {
  145. order.user2 = user2;
  146. if (invert == true)
  147. order.flags.set(InvertSharedOrder);
  148. if (invert == false)
  149. order.flags.unset(InvertSharedOrder);
  150. } else {
  151. order.user2 = null;
  152. order.flags.unset(InvertSharedOrder);
  153. }
  154. // stocks
  155. var e:Event = null;
  156. if (order.product.stock != null) {
  157. var c = order.product.contract;
  158. if (c.hasStockManagement()) {
  159. if (newquantity < order.quantity) {
  160. // on commande moins que prévu : incrément de stock
  161. order.product.lock();
  162. order.product.stock += (order.quantity - newquantity);
  163. e = StockMove({product: order.product, move: 0 - (order.quantity - newquantity)});
  164. } else {
  165. // on commande plus que prévu : décrément de stock
  166. var addedquantity = newquantity - order.quantity;
  167. if (order.product.stock - addedquantity < 0) {
  168. // stock is not enough, reduce order
  169. newquantity = order.quantity + order.product.stock;
  170. if (App.current.session != null)
  171. App.current.session.addMessage(t._("We reduced your order of '::productName::' to quantity ::oQuantity:: because there is no available products anymore",
  172. {
  173. productName: order.product.name,
  174. oQuantity: newquantity
  175. }), true);
  176. e = StockMove({product: order.product, move: 0 - order.product.stock});
  177. order.product.lock();
  178. order.product.stock = 0;
  179. } else {
  180. // stock is big enough
  181. order.product.lock();
  182. order.product.stock -= addedquantity;
  183. e = StockMove({product: order.product, move: 0 - addedquantity});
  184. }
  185. }
  186. order.product.update();
  187. }
  188. }
  189. // update order
  190. if (newquantity == 0) {
  191. order.quantity = 0;
  192. order.paid = true;
  193. order.update();
  194. } else {
  195. order.quantity = newquantity;
  196. order.update();
  197. }
  198. App.current.event(e);
  199. return order;
  200. }
  201. /**
  202. * Delete an order
  203. */
  204. public static function delete(order:db.UserContract) {
  205. var t = sugoi.i18n.Locale.texts;
  206. if (order == null)
  207. throw new Error(t._("This order has already been deleted."));
  208. order.lock();
  209. if (order.quantity == 0) {
  210. var contract = order.product.contract;
  211. var user = order.user;
  212. // Amap Contract
  213. if (contract.type == db.Contract.TYPE_CONSTORDERS) {
  214. order.delete();
  215. if (contract.amap.hasPayments()) {
  216. var orders = contract.getUserOrders(user);
  217. if (orders.length == 0) {
  218. var operation = db.Operation.findCOrderTransactionFor(contract, user);
  219. if (operation != null)
  220. operation.delete();
  221. }
  222. }
  223. } else { // Variable orders contract
  224. // Get the basket for this user
  225. var place = order.distribution.place;
  226. var basket = db.Basket.get(user, place, order.distribution.date);
  227. if (contract.amap.hasPayments()) {
  228. var orders = basket.getOrders();
  229. // Check if it is the last order, if yes then delete the related operation
  230. if (orders.length == 1 && orders.first().id == order.id) {
  231. var operation = db.Operation.findVOrderTransactionFor(order.distribution.getKey(), user, place.amap);
  232. if (operation != null)
  233. operation.delete();
  234. }
  235. }
  236. order.delete();
  237. }
  238. } else {
  239. throw new Error(t._("Deletion not possible: quantity is not zero."));
  240. }
  241. }
  242. /**
  243. * Prepare a simple dataset, ready to be displayed
  244. */
  245. public static function prepare(orders:Iterable<db.UserContract>):Array<UserOrder> {
  246. var out = new Array<UserOrder>();
  247. var orders = Lambda.array(orders);
  248. var view = App.current.view;
  249. var t = sugoi.i18n.Locale.texts;
  250. for (o in orders) {
  251. var x:UserOrder = cast {};
  252. x.id = o.id;
  253. x.userId = o.user.id;
  254. x.userName = o.user.getCoupleName();
  255. x.userEmail = o.user.email;
  256. // shared order
  257. if (o.user2 != null) {
  258. x.userId2 = o.user2.id;
  259. x.userName2 = o.user2.getCoupleName();
  260. x.userEmail2 = o.user2.email;
  261. }
  262. // deprecated
  263. x.productId = o.product.id;
  264. x.productRef = o.product.ref;
  265. x.productQt = o.product.qt;
  266. x.productUnit = o.product.unitType;
  267. x.productPrice = o.productPrice;
  268. x.productImage = o.product.getImage();
  269. x.productHasFloatQt = o.product.hasFloatQt;
  270. x.productHasVariablePrice = o.product.variablePrice;
  271. // new way
  272. x.product = o.product.infos();
  273. x.product.price = o.productPrice; // do not use current price, but price of the order
  274. x.quantity = o.quantity;
  275. // smartQt
  276. if (x.quantity == 0.0) {
  277. x.smartQt = t._("Canceled");
  278. } else if (x.productHasFloatQt || x.productHasVariablePrice || o.product.wholesale) {
  279. x.smartQt = view.smartQt(x.quantity, x.productQt, x.productUnit);
  280. } else {
  281. x.smartQt = Std.string(x.quantity);
  282. }
  283. // product name.
  284. if (x.productHasVariablePrice || x.productQt == null || x.productUnit == null) {
  285. x.productName = o.product.name;
  286. } else {
  287. x.productName = o.product.name + " " + view.formatNum(x.productQt) + " " + view.unit(x.productUnit, x.productQt > 1);
  288. }
  289. x.subTotal = o.quantity * o.productPrice;
  290. var c = o.product.contract;
  291. if (o.feesRate != 0) {
  292. x.fees = x.subTotal * (o.feesRate / 100);
  293. x.percentageName = c.percentageName;
  294. x.percentageValue = o.feesRate;
  295. x.total = x.subTotal + x.fees;
  296. } else {
  297. x.total = x.subTotal;
  298. }
  299. // flags
  300. x.paid = o.paid;
  301. x.invertSharedOrder = o.flags.has(InvertSharedOrder);
  302. x.contractId = c.id;
  303. x.contractName = c.name;
  304. x.canModify = o.canModify();
  305. out.push(x);
  306. }
  307. return sort(out);
  308. }
  309. /**
  310. * Confirms an order : create real orders from tmp orders in session
  311. * @param order
  312. */
  313. public static function confirmSessionOrder(tmpOrder:OrderInSession) {
  314. var orders = [];
  315. var user = db.User.manager.get(tmpOrder.userId);
  316. for (o in tmpOrder.products) {
  317. o.product = db.Product.manager.get(o.productId);
  318. orders.push(make(user, o.quantity, o.product, o.distributionId));
  319. }
  320. App.current.event(MakeOrder(orders));
  321. App.current.session.data.order = null;
  322. return orders;
  323. }
  324. /**
  325. * Send an order-by-products report to the coordinator
  326. */
  327. public static function sendOrdersByProductReport(d:db.Distribution) {
  328. var t = sugoi.i18n.Locale.texts;
  329. var m = new sugoi.mail.Mail();
  330. m.addRecipient(d.contract.contact.email, d.contract.contact.getName());
  331. m.setSender(App.config.get("default_email"), t._("Cagette.net"));
  332. m.setSubject('[${d.contract.amap.name}] Distribution du ${App.current.view.dDate(d.date)} (${d.contract.name})');
  333. var orders = service.ReportService.getOrdersByProduct(d);
  334. var html = App.current.processTemplate("mail/ordersByProduct.mtt", {
  335. contract: d.contract,
  336. distribution: d,
  337. orders: orders,
  338. formatNum: App.current.view.formatNum,
  339. currency: App.current.view.currency,
  340. dDate: App.current.view.dDate,
  341. hHour: App.current.view.hHour,
  342. group: d.contract.amap
  343. });
  344. m.setHtmlBody(html);
  345. App.sendMail(m);
  346. }
  347. /**
  348. * Order summary for a member
  349. * WARNING : its for one distrib, not for a whole basket !
  350. */
  351. public static function sendOrderSummaryToMembers(d:db.Distribution) {
  352. var t = sugoi.i18n.Locale.texts;
  353. var title = '[${d.contract.amap.name}] Votre commande pour le ${App.current.view.dDate(d.date)} (${d.contract.name})';
  354. for (user in d.getUsers()) {
  355. var m = new sugoi.mail.Mail();
  356. m.addRecipient(user.email, user.getName(), user.id);
  357. if (user.email2 != null)
  358. m.addRecipient(user.email2, user.getName(), user.id);
  359. m.setSender(App.config.get("default_email"), t._("Cagette.net"));
  360. m.setSubject(title);
  361. var orders = prepare(d.contract.getUserOrders(user, d));
  362. var html = App.current.processTemplate("mail/orderSummaryForMember.mtt", {
  363. contract: d.contract,
  364. distribution: d,
  365. orders: orders,
  366. formatNum: App.current.view.formatNum,
  367. currency: App.current.view.currency,
  368. dDate: App.current.view.dDate,
  369. hHour: App.current.view.hHour,
  370. group: d.contract.amap
  371. });
  372. m.setHtmlBody(html);
  373. App.sendMail(m);
  374. }
  375. }
  376. public static function sort(orders:Array<UserOrder>) {
  377. // order by lastname (+lastname2 if exists), then contract
  378. orders.sort(function(a, b) {
  379. if (a.userName
  380. + a.userId
  381. + a.userName2
  382. + a.userId2
  383. + a.contractId > b.userName
  384. + b.userId
  385. + b.userName2
  386. + b.userId2
  387. + b.contractId) {
  388. return 1;
  389. }
  390. if (a.userName
  391. + a.userId
  392. + a.userName2
  393. + a.userId2
  394. + a.contractId < b.userName
  395. + b.userId
  396. + b.userName2
  397. + b.userId2
  398. + b.contractId) {
  399. return -1;
  400. }
  401. return 0;
  402. });
  403. return orders;
  404. }
  405. }