eiru_utility_tool_sale.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. (function() {
  2. openerp.widgetInstanceEiruUtilityToolSale = null;
  3. openerp.parentInstanceEiruUtilityToolSale = null;
  4. var Qweb = openerp.web.qweb;
  5. var instance = openerp;
  6. var instanceWeb = openerp.web;
  7. openerp.EiruUtilityToolSale = instance.Widget.extend({
  8. template: 'EiruUtilityTool.Sale',
  9. id: undefined,
  10. order: [],
  11. calculatorTerm: [],
  12. termType: [],
  13. accountSaleTerm: [],
  14. /************
  15. | INIT |
  16. ************/
  17. init: function(parent) {
  18. this._super(parent);
  19. this.buttons = parent.$buttons;
  20. },
  21. /************
  22. | START |
  23. ************/
  24. start: function() {
  25. var self = this;
  26. this.$el.click(function() {
  27. self.fectchInitial();
  28. });
  29. self.buttons.click(function(e) {
  30. /* E (Editar) */
  31. if (e.target.accessKey === 'E')
  32. self.$el.css('display','none');
  33. /* S (Guarrdar) */
  34. if (e.target.accessKey === 'S')
  35. self.$el.css('display','flex');
  36. /* D (Cancelar) */
  37. if (e.target.accessKey === 'D')
  38. self.$el.css('display','flex');
  39. /* CREAR */
  40. if (e.target.accessKey === 'C')
  41. self.$el.css('display','none');
  42. });
  43. },
  44. /*===============
  45. | ID ORDER |
  46. ===============*/
  47. updateId: function(id) {
  48. var self = this;
  49. self.id = id;
  50. self.$el.css('display','flex');
  51. if (!id)
  52. self.$el.css('display','none');
  53. },
  54. /*=====================
  55. | Remover Modal |
  56. =====================*/
  57. removeModal: function() {
  58. $('.expired-account-modal').remove();
  59. $('.modal-backdrop').remove();
  60. },
  61. /*===============================
  62. | Recargar la Pagina |
  63. ===============================*/
  64. reloadPage: function() {
  65. openerp.parentInstanceEiruUtilityToolSale.reload();
  66. },
  67. /******************************
  68. || Metodo Inicial ||
  69. ******************************/
  70. fectchInitial: function() {
  71. var self = this;
  72. self.fetchSaleOrder(self.id).then(function(order) {
  73. return order;
  74. }).then(function(order) {
  75. self.order = order;
  76. return self.fetchTypeTerm();
  77. }).then(function(termType) {
  78. self.termType = termType;
  79. return self.fetchTermSale(self.id);
  80. }).then(function(accountSaleTerm) {
  81. self.accountSaleTerm = accountSaleTerm;
  82. return self.showModal();
  83. });
  84. },
  85. /*===============================
  86. | GET Order |
  87. ===============================*/
  88. fetchSaleOrder: function(id) {
  89. var order = new openerp.web.Model('sale.order');
  90. return order.call('getSaleOrder',[id],{
  91. context: new openerp.web.CompoundContext()
  92. });
  93. },
  94. /*===============================
  95. | GET Sale term |
  96. ===============================*/
  97. fetchTermSale: function(id) {
  98. var order = new openerp.web.Model('sale.order');
  99. return order.call('getSaleTerm',[id],{
  100. context: new openerp.web.CompoundContext()
  101. });
  102. },
  103. /*=========================================
  104. | GET Tipo de condicion de pagos |
  105. =========================================*/
  106. fetchTypeTerm: function() {
  107. var typeTerm = new openerp.web.Model('account.payment.term.type');
  108. return typeTerm.query(['id','name']).all()
  109. },
  110. /*===================
  111. | Modal |
  112. ===================*/
  113. showModal: function() {
  114. var self = this;
  115. var defer = $.Deferred();
  116. var state = true;
  117. /*Curency Order*/
  118. var saleOrder = self.order[0];
  119. var accountSaleTerm = self.accountSaleTerm[0];
  120. var currencyOrder = saleOrder.currency;
  121. // /*term type */
  122. var numberFormat = {
  123. 'decimalSeparator': ',',
  124. 'decimalPlaces': 0,
  125. 'thousandsSeparator': '.'
  126. }
  127. var interstFormat = {
  128. 'decimalSeparator': ',',
  129. 'decimalPlaces': 2,
  130. 'thousandsSeparator': '.'
  131. }
  132. var typeTerm = self.termType;
  133. if (!typeTerm.length){
  134. instanceWeb.notification.do_warn("Atencion", "No existe tipo de condiciones de pagos.");
  135. return ;
  136. }
  137. typeTerm.unshift({"id": '', 'name': ''})
  138. /* Modal */
  139. var modal = Qweb.render('EiruUtilityTool.SaleModal',{'termTypes':typeTerm});
  140. $('.openerp_webclient_container').after(modal);
  141. $('.expired-account-modal').modal();
  142. var amountOrder = $('.expired-account-modal').find('.amount-order'); //Valor de la orden.
  143. var symbolCurrency = $('.expired-account-modal').find('.symbol-currency'); //Symbolo de moneda.
  144. var dateSale = $('.expired-account-modal').find('.date-sale'); //Fecha de venta.
  145. var initialPaymentsAmount = $('.expired-account-modal').find('.initial-payments-amount'); //Valor de Entrega.
  146. var amountResidual = $('.expired-account-modal').find('.amount-residual'); //Valor Residual (Venta - Entrega).
  147. var interestQty = $('.expired-account-modal').find('.interest-qty'); // % de interest.
  148. var amountInterest = $('.expired-account-modal').find('.amount-interest'); //Total Interest.
  149. var btInterestCalc = $('.expired-account-modal').find('.bt-interest-calc'); //Botton CALCULAR Interest.
  150. var amountFinal = $('.expired-account-modal').find('.amount-final'); //Valor final (residual + interest).
  151. var dateQuotaInit = $('.expired-account-modal').find('.date-quota-init'); //Fecha de Primera Cuota.
  152. var termSelect = $('.expired-account-modal').find('.term-select'); //Termino de Pago.
  153. var amountQuota = $('.expired-account-modal').find('.amount-quota'); //Valor de Cuota.
  154. var qtyQuota = $('.expired-account-modal').find('.qty-quota'); //Cantidad de Cuota.
  155. var btQuotaCalc = $('.expired-account-modal').find('.bt-quota-calc'); //Calcular Cuotas.
  156. var tableRow = $('.expired-account-modal').find('.table-tbody'); //Table.
  157. var buttonAccept = $('.expired-account-modal').find('.button-accept'); //
  158. /*###### Data INIT ###############################################*/
  159. amountOrder.val(instanceWeb.formatCurrency(saleOrder.amountTotal, currencyOrder));
  160. amountResidual.val(instanceWeb.formatCurrency(saleOrder.amountTotal, currencyOrder));
  161. amountFinal.val(instanceWeb.formatCurrency(saleOrder.amountTotal, currencyOrder));
  162. symbolCurrency.text(currencyOrder.symbol);
  163. dateSale.val(moment(saleOrder.dateOrder).format("YYYY-MM-DD"));
  164. dateQuotaInit.val(moment(saleOrder.dateOrder).format("YYYY-MM-DD"));
  165. /*######## SALE TERM ##########################################*/
  166. if (!!accountSaleTerm) {
  167. amountOrder.val(instanceWeb.formatCurrency(accountSaleTerm.saleAmount, currencyOrder));
  168. initialPaymentsAmount.val(instanceWeb.formatCurrency(accountSaleTerm.salePaymentsInit, currencyOrder));
  169. amountResidual.val(instanceWeb.formatCurrency(accountSaleTerm.saleResidual, currencyOrder));
  170. interestQty.val(instanceWeb.formatCurrency(accountSaleTerm.interestQty, interstFormat));
  171. amountInterest.val(instanceWeb.formatCurrency(accountSaleTerm.interestAmount, currencyOrder));
  172. amountFinal.val(instanceWeb.formatCurrency(accountSaleTerm.amountTotal, currencyOrder));
  173. dateQuotaInit.val(!!accountSaleTerm.dateInit ?accountSaleTerm.dateInit :moment(saleOrder.dateOrder).format("YYYY-MM-DD"))
  174. termSelect.val(accountSaleTerm.termTypeId)
  175. amountQuota.val(instanceWeb.formatCurrency(accountSaleTerm.quotaAmount, currencyOrder));
  176. qtyQuota.val(instanceWeb.formatCurrency(accountSaleTerm.quotaQty, numberFormat));
  177. tableRow.find('tr').remove();
  178. var htmlLine = '';
  179. self.calculatorTerm = accountSaleTerm.lines
  180. _.each(self.calculatorTerm, function(line) {
  181. var dateMaturity = moment(line.date).format('DD/MM/YYYY');
  182. var amount = instanceWeb.formatCurrency(line.amount, currencyOrder) +" "+currencyOrder.symbol;
  183. htmlLine +='<tr><td><span>'+line.cuotaNumber+'</span></td><td><span>'+dateMaturity+'</span></td><td><span>'+amount+'</span></td></tr>';
  184. })
  185. tableRow.append(htmlLine);
  186. }
  187. /*###### FOCUSIN initialPaymentsAmount ##########################*/
  188. initialPaymentsAmount.focusin(function(e){
  189. var amount = instanceWeb.unFormatCurrency(initialPaymentsAmount.val());
  190. if (amount === 0)
  191. initialPaymentsAmount.val('');
  192. });
  193. initialPaymentsAmount.keyup(function(e) {
  194. var amountPayments = instanceWeb.unFormatCurrency(initialPaymentsAmount.val());
  195. var saleAmount = instanceWeb.unFormatCurrency(amountOrder.val());
  196. if (e.key === currencyOrder.decimalSeparator && currencyOrder.decimalPlaces > 0)
  197. return false ;
  198. initialPaymentsAmount.val(instanceWeb.formatCurrency(amountPayments, currencyOrder));
  199. amountResidual.val(instanceWeb.formatCurrency((saleAmount - amountPayments), currencyOrder))
  200. amountFinal.val(instanceWeb.formatCurrency((saleAmount - amountPayments), currencyOrder))
  201. interestQty.val(0)
  202. amountInterest.val(0)
  203. });
  204. initialPaymentsAmount.focusout(function(e) {
  205. var saleAmount = instanceWeb.unFormatCurrency(amountOrder.val());
  206. var amount = instanceWeb.unFormatCurrency(initialPaymentsAmount.val());
  207. if (amount > saleAmount) {
  208. instanceWeb.notification.do_warn("Atencion", "El valor de la entrega supera el valor de la venta.");
  209. initialPaymentsAmount.focus();
  210. return false;
  211. }
  212. $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','flex');
  213. self.saleTermPaymentsInitial(amount).then(function(paymentsUpdate) {
  214. return paymentsUpdate;
  215. }).then(function(paymentsUpdate) {
  216. $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','none');
  217. });
  218. amountResidual.val(instanceWeb.formatCurrency((saleAmount - amount), currencyOrder))
  219. amountFinal.val(instanceWeb.formatCurrency((saleAmount - amount), currencyOrder))
  220. interestQty.val(0)
  221. amountInterest.val(0)
  222. amountQuota.val(0)
  223. qtyQuota.val(0)
  224. self.calculatorTerm = [];
  225. tableRow.find('tr').remove();
  226. });
  227. /*###### interestQty focusin #####################################*/
  228. interestQty.focusin(function(e) {
  229. var values = instanceWeb.unFormatCurrency(interestQty.val());
  230. if (values === 0)
  231. interestQty.val('');
  232. });
  233. interestQty.keyup(function(e) {
  234. var qtyInterest = instanceWeb.unFormatCurrency(interestQty.val());
  235. if (e.key === interstFormat.decimalSeparator && interstFormat.decimalPlaces > 0)
  236. return false ;
  237. interestQty.val(instanceWeb.formatCurrency(qtyInterest, interstFormat));
  238. });
  239. interestQty.focusout(function(e) {
  240. amountQuota.val(0)
  241. qtyQuota.val(0)
  242. calculatorTerm = []
  243. tableRow.find('tr').remove();
  244. })
  245. /*###### CLICK btInterestCalc ###################################*/
  246. btInterestCalc.click(function(e) {
  247. var interest = instanceWeb.unFormatCurrency(interestQty.val());
  248. var residual = instanceWeb.unFormatCurrency(amountResidual.val());
  249. var paymentsAmount = instanceWeb.unFormatCurrency(initialPaymentsAmount.val());
  250. $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','flex');
  251. self.calculateInterest(interest, paymentsAmount, instanceWeb.formatCurrency(paymentsAmount, interstFormat)).then(function(interestcalc) {
  252. return interestcalc;
  253. }).then(function(interestcalc) {
  254. amountInterest.val(0)
  255. amountFinal.val(instanceWeb.formatCurrency(residual, currencyOrder))
  256. amountQuota.val(0)
  257. qtyQuota.val(0)
  258. calculatorTerm = []
  259. tableRow.find('tr').remove();
  260. if (interestcalc.state) {
  261. amountInterest.val(instanceWeb.formatCurrency(interestcalc.amountInterest, currencyOrder))
  262. amountFinal.val(instanceWeb.formatCurrency(residual + interestcalc.amountInterest, currencyOrder))
  263. }
  264. $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','none');
  265. });
  266. });
  267. /*###### VALOR DE LA CUOTA ######################################*/
  268. amountQuota.focusin(function(e) {
  269. var amount = instanceWeb.unFormatCurrency( amountQuota.val());
  270. if (amount === 0) {
  271. amountQuota.val('');
  272. qtyQuota.removeAttr("disabled");
  273. }
  274. });
  275. amountQuota.keyup(function(e) {
  276. qtyQuota.attr("disabled", true);
  277. if (e.key === currencyOrder.decimalSeparator && currencyOrder.decimalPlaces > 0)
  278. return false ;
  279. cuotaAmount = instanceWeb.unFormatCurrency(amountQuota.val());
  280. amountQuota.val(instanceWeb.formatCurrency(cuotaAmount, currencyOrder));
  281. });
  282. amountQuota.focusout(function(e) {
  283. var saleAmount = instanceWeb.unFormatCurrency(amountFinal.val());
  284. var amount = instanceWeb.unFormatCurrency(amountQuota.val());
  285. if (amount <= 0) {
  286. amountQuota.val('0');
  287. qtyQuota.removeAttr("disabled");
  288. qtyQuota.focus();
  289. }
  290. if (amount > (saleAmount)) {
  291. instanceWeb.notification.do_warn("Atencion", "El valor de la cuota no puede superar el Saldo.\nSaldo ="+saleAmount+"\nCuota = "+amount);
  292. amountQuota.focus();
  293. return false;
  294. }
  295. });
  296. /*###### CANTIDAD DE CUOTA #######################################*/
  297. qtyQuota.focusin(function(e){
  298. var qty = instanceWeb.unFormatCurrency( qtyQuota.val());
  299. if (qty === 0){
  300. amountQuota.removeAttr("disabled");
  301. qtyQuota.val('');
  302. }
  303. });
  304. qtyQuota.keyup(function(e) {
  305. amountQuota.attr("disabled", true);
  306. if (e.key === numberFormat.decimalSeparator && numberFormat.decimalPlaces > 0)
  307. return false ;
  308. cuotaAmount = instanceWeb.unFormatCurrency(qtyQuota.val());
  309. qtyQuota.val(instanceWeb.formatCurrency(cuotaAmount, numberFormat));
  310. });
  311. qtyQuota.focusout(function(e) {
  312. var qty = instanceWeb.unFormatCurrency(qtyQuota.val());
  313. if (qty <= 0) {
  314. qtyQuota.val('0');
  315. amountQuota.removeAttr("disabled");
  316. amountQuota.focus();
  317. }
  318. });
  319. /*###### GENERAR CUOTAS ##########################################*/
  320. btQuotaCalc.click(function(e) {
  321. tableRow.find('tr').remove();
  322. var htmlLine = '';
  323. var finalAmount = instanceWeb.unFormatCurrency(amountFinal.val());
  324. var quotaAmount = instanceWeb.unFormatCurrency(amountQuota.val());
  325. var quotaQty = instanceWeb.unFormatCurrency(qtyQuota.val());
  326. var amountPayments = instanceWeb.unFormatCurrency(initialPaymentsAmount.val());
  327. if (!dateQuotaInit.val()) {
  328. instanceWeb.notification.do_warn("Atencion", "Debes ingresa la fecha de la primera cuota.");
  329. dateQuotaInit.focus();
  330. return;
  331. }
  332. if(!termSelect.val()) {
  333. instanceWeb.notification.do_warn("Atencion", "Debes de Seleccionar el tipo de plazo de pago.");
  334. termSelect.focus();
  335. return;
  336. }
  337. if (quotaAmount > finalAmount) {
  338. instanceWeb.notification.do_warn("Atencion", "EL valor de la cuota no puede superar el valor del saldo.");
  339. quotaAmount.focus();
  340. return;
  341. }
  342. if (quotaAmount <=0 && quotaQty<= 0){
  343. instanceWeb.notification.do_warn("Atencion", "Debes ingresar un monto o la cantidad de las cuota.");
  344. return;
  345. }
  346. var termSale = {
  347. 'orderId': self.id,
  348. 'typeTerm': parseInt(termSelect.val().trim()),
  349. 'amountCuota': quotaAmount,
  350. 'qtyCuota': quotaQty,
  351. 'dateInit': dateQuotaInit.val(),
  352. 'amountResidual': finalAmount,
  353. 'amountPayments':amountPayments,
  354. }
  355. /* GENERAR LA LISTA DE CUOTA */
  356. $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','flex');
  357. self.calculatePaymentTerm(termSale).then(function(calculatorTerm) {
  358. return calculatorTerm;
  359. }).then(function(calculatorTerm) {
  360. self.calculatorTerm = calculatorTerm;
  361. $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','none');
  362. if (!!calculatorTerm.length) {
  363. _.each(calculatorTerm, function(line) {
  364. var dateMaturity = moment(line.date).format('DD/MM/YYYY');
  365. var amount = instanceWeb.formatCurrency(line.amount, currencyOrder) +" "+currencyOrder.symbol;
  366. htmlLine +='<tr><td><span>'+line.cuotaNumber+'</span></td><td><span>'+dateMaturity+'</span></td><td><span>'+amount+'</span></td></tr>';
  367. })
  368. tableRow.append(htmlLine);
  369. }
  370. });
  371. });
  372. /*###### ACEPTAR --> GUARDAR #####################################*/
  373. buttonAccept.click(function(e) {
  374. if (!self.calculatorTerm.length) {
  375. instanceWeb.notification.do_warn("Atencion", "Debes de calcular las cuota antes de guardar.");
  376. return ;
  377. }
  378. $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','flex');
  379. self.accountPaymentTermConfigurator(self.calculatorTerm, self.id).then(function(termConfigurator) {
  380. return termConfigurator;
  381. }).then(function(termConfigurator) {
  382. $('.expired-account-modal').find('.widget-content.widget-loading-term-configurator').css('display','none');
  383. self.removeModal(e);
  384. state = termConfigurator.state;
  385. self.reloadPage();
  386. });
  387. defer.resolve(state);
  388. });
  389. /*###### CERRAR ###########################################*/
  390. $('.expired-account-modal').on('hidden.bs.modal', function (e) {
  391. defer.resolve(false);
  392. self.removeModal(e);
  393. self.reloadPage();
  394. });
  395. return defer;
  396. },
  397. /*===============================
  398. | Calcular interest |
  399. ===============================*/
  400. calculateInterest: function(qty, amountPaymenst, paymentsFormat) {
  401. var self = this;
  402. var order = new instance.web.Model('sale.order');
  403. return order.call('calculateInterestSale',[self.id, qty, amountPaymenst, paymentsFormat], {
  404. context: new instance.web.CompoundContext()
  405. });
  406. },
  407. /*===========================
  408. | Update Patments Line |
  409. ===========================*/
  410. saleTermPaymentsInitial: function(amount) {
  411. var self = this;
  412. var order = new instance.web.Model('sale.order');
  413. return order.call('saleTermUpdatePaymentsInitial',[self.id, amount], {
  414. context: new instance.web.CompoundContext()
  415. });
  416. },
  417. /*===========================
  418. | CALCULATOR TERM |
  419. ===========================*/
  420. calculatePaymentTerm: function(values) {
  421. var order = new instance.web.Model('sale.order');
  422. return order.call('calculatePaymentTermSaleOrder',[values], {
  423. context: new instance.web.CompoundContext()
  424. });
  425. },
  426. /*===========================================
  427. | Create / writer - Term Paymen |
  428. ===========================================*/
  429. accountPaymentTermConfigurator: function(values, id){
  430. var term = new instance.web.Model('sale.order');
  431. return term.call('accountPaymentTermConfiguratorSale',[values, id], {
  432. context: new instance.web.CompoundContext()
  433. });
  434. }
  435. });
  436. /* INSTANCE*/
  437. if (instance.web && instance.web.FormView) {
  438. instance.web.FormView.include({
  439. load_record: function(record) {
  440. this._super.apply(this, arguments);
  441. if (this.model !== 'sale.order')
  442. return;
  443. openerp.parentInstanceEiruUtilityToolSale = this;
  444. if (openerp.widgetInstanceEiruUtilityToolSale) {
  445. openerp.widgetInstanceEiruUtilityToolSale.updateId(record.id);
  446. if (this.$el.find('.bottom-eiru-utility-tool-sale').length !== 0){
  447. return
  448. }
  449. }
  450. if (this.$el.find('.bottom-eiru-utility-tool-sale').length !== 0 )
  451. return;
  452. openerp.widgetInstanceEiruUtilityToolSale = new openerp.EiruUtilityToolSale(this);
  453. var elemento = this.$el.find('.oe_form').find('.eiru-sale-utility-tool-sale');
  454. openerp.widgetInstanceEiruUtilityToolSale.appendTo(elemento);
  455. openerp.widgetInstanceEiruUtilityToolSale.updateId(record.id);
  456. },
  457. });
  458. }
  459. })();