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.

153 lines
4.4 KiB

  1. package controller;
  2. import db.Operation.OperationType;
  3. using Lambda;
  4. /**
  5. * Distribution validation
  6. * @author fbarbut
  7. */
  8. class Validate extends controller.Controller
  9. {
  10. public var date : Date;
  11. public var user : db.User;
  12. public var place : db.Place;
  13. @tpl('validate/user.mtt')
  14. public function doDefault(){
  15. view.member = user;
  16. if (!app.user.amap.hasShopMode()){
  17. //get last operations and check balance
  18. view.operations = db.Operation.getLastOperations(this.user,place.amap,10);
  19. view.balance = db.UserAmap.get(this.user, place.amap).balance;
  20. }
  21. var b = db.Basket.get(user, place, date);
  22. view.orders = service.OrderService.prepare(b.getOrders());
  23. view.place = place;
  24. view.date = date;
  25. view.basket = b;
  26. checkToken();
  27. }
  28. public function doDeleteOp(op:db.Operation){
  29. if (checkToken()){
  30. op.lock();
  31. op.delete();
  32. service.PaymentService.updateUserBalance(user, app.user.amap);
  33. throw Ok("/validate/"+date+"/"+place.id+"/"+user.id, t._("Operation deleted"));
  34. }
  35. }
  36. public function doValidateOp(op:db.Operation){
  37. if (checkToken()){
  38. op.lock();
  39. op.pending = false;
  40. op.update();
  41. service.PaymentService.updateUserBalance(user, app.user.amap);
  42. throw Ok("/validate/"+date+"/"+place.id+"/"+user.id, t._("Operation validated"));
  43. }
  44. }
  45. @tpl('form.mtt')
  46. public function doAddRefund(){
  47. if (!app.user.isContractManager()) throw t._("Forbidden access");
  48. var o = new db.Operation();
  49. o.user = user;
  50. o.date = Date.now();
  51. var b = db.Basket.get(user, place, date);
  52. var op = b.getOrderOperation(false);
  53. if(op==null) throw "unable to find related order operation";
  54. var f = new sugoi.form.Form(t._("payment"));
  55. f.addElement(new sugoi.form.elements.StringInput("name", t._("Label"), t._("Refund"), true));
  56. f.addElement(new sugoi.form.elements.FloatInput("amount", t._("Amount"), null, true));
  57. f.addElement(new sugoi.form.elements.DatePicker("date", "Date", Date.now(), true));
  58. var paymentTypes = service.PaymentService.getPaymentTypesForManualEntry(app.user.amap);
  59. f.addElement(new sugoi.form.elements.StringSelect("Mtype", t._("Payment type"), paymentTypes, null, true));
  60. if (f.isValid()){
  61. f.toSpod(o);
  62. o.type = db.Operation.OperationType.Payment;
  63. var data : db.Operation.PaymentInfos = {type:f.getValueOf("Mtype")};
  64. o.data = data;
  65. o.group = app.user.amap;
  66. o.user = user;
  67. o.relation = op;
  68. o.amount = 0-Math.abs(o.amount);
  69. o.insert();
  70. App.current.event(NewOperation(o));
  71. service.PaymentService.updateUserBalance(user, app.user.amap);
  72. throw Ok("/validate/"+date+"/"+place.id+"/"+user.id, t._("Refund saved"));
  73. }
  74. view.title = t._("Key-in a refund for ::user::",{user:user.getCoupleName()});
  75. view.form = f;
  76. }
  77. @tpl('form.mtt')
  78. public function doAddPayment(){
  79. if (!app.user.isContractManager()) throw Error("/",t._("Forbidden access"));
  80. var o = new db.Operation();
  81. o.user = user;
  82. o.date = Date.now();
  83. var f = new sugoi.form.Form("payment");
  84. f.addElement(new sugoi.form.elements.StringInput("name", t._("Label"), t._("Additional payment"), true));
  85. f.addElement(new sugoi.form.elements.FloatInput("amount", t._("Amount"), null, true));
  86. f.addElement(new sugoi.form.elements.DatePicker("date", t._("Date"), Date.now(), true));
  87. var paymentTypes = service.PaymentService.getPaymentTypesForManualEntry(app.user.amap);
  88. f.addElement(new sugoi.form.elements.StringSelect("Mtype", t._("Payment type"), paymentTypes, null, true));
  89. var b = db.Basket.get(user, place, date);
  90. var op = b.getOrderOperation(false);
  91. if(op==null) throw "unable to find related order operation";
  92. if (f.isValid()){
  93. f.toSpod(o);
  94. o.type = db.Operation.OperationType.Payment;
  95. var data : db.Operation.PaymentInfos = {type:f.getValueOf("Mtype")};
  96. o.data = data;
  97. o.group = app.user.amap;
  98. o.user = user;
  99. o.relation = op;
  100. o.insert();
  101. service.PaymentService.updateUserBalance(user, app.user.amap);
  102. throw Ok("/validate/"+date+"/"+place.id+"/"+user.id, t._("Payment saved"));
  103. }
  104. view.title = t._("Key-in a payment for ::user::",{user:user.getCoupleName()});
  105. view.form = f;
  106. }
  107. public function doValidate(){
  108. if (checkToken()){
  109. var basket = db.Basket.get(user, place, date);
  110. service.PaymentService.validateBasket(basket);
  111. throw Ok("/distribution/validate/"+date+"/"+place.id, t._("Order validated"));
  112. }
  113. }
  114. }