payslip_payments_unity.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. (function() {
  2. openerp.widgetInstanceUnity = null;
  3. openerp.parentInstanceUnity = {};
  4. var instance = openerp.web;
  5. openerp.payslipPaymentsUnity = openerp.Widget.extend({
  6. template: 'eiru_payslip_payments.PayslipUnityWidget',
  7. id: undefined,
  8. hrPayslip: [],
  9. payslipLine: [],
  10. accountJournal: [],
  11. resCompany: [],
  12. resCurrency: [],
  13. /* init */
  14. init: function (parent) {
  15. this._super(parent);
  16. this.buttons = parent.$buttons;
  17. },
  18. /* Start */
  19. start: function () {
  20. var self = this;
  21. this.$el.click(function(){
  22. self.fetchPaymentInit();
  23. });
  24. self.buttons.click(function(e) {
  25. /* E (Editar) */
  26. if (e.target.accessKey === 'E')
  27. self.$el.css('display','none');
  28. /* S (Guarrdar) */
  29. if (e.target.accessKey === 'S')
  30. self.$el.css('display','flex');
  31. /* D (Cancelar) */
  32. if (e.target.accessKey === 'D')
  33. self.$el.css('display','flex');
  34. });
  35. },
  36. /* Actualizar ID */
  37. updateId: function(id) {
  38. var self = this;
  39. self.id = id;
  40. },
  41. /* ---------------------------------------------------------------------
  42. * Description: Funcione Inicial Pagos de Nominas
  43. * -------------------------------------------------------------------*/
  44. /* Método Inicial */
  45. fetchPaymentInit: function() {
  46. var self = this;
  47. self.fetchPayslip().then(function(hrPayslip) {
  48. return hrPayslip;
  49. }).then(function(hrPayslip) {
  50. self.hrPayslip = hrPayslip;
  51. return self.fetchPayslipLine();
  52. }).then(function(payslipLine) {
  53. self.payslipLine = payslipLine;
  54. return self.fecthJournal();
  55. }).then(function(accountJournal) {
  56. self.accountJournal = accountJournal;
  57. return self.fecthCompany();
  58. }).then(function(resCompany) {
  59. self.resCompany = resCompany;
  60. return self.fecthCurrency();
  61. }).then(function(resCurrency) {
  62. self.resCurrency = resCurrency;
  63. return self.showPayslipPayments();
  64. }).then(function() {
  65. return self.reloadLine();
  66. });
  67. },
  68. /* Consultar nominas de funcionario */
  69. fetchPayslip: function() {
  70. var self = this;
  71. var fields = ['id', 'name', 'employee_id', 'date_from', 'date_to', 'move_id', 'number', 'line_ids', 'journal_id'];
  72. var domain = [['id', '=', self.id]];
  73. var payslip = new instance.Model('hr.payslip');
  74. return payslip.query(fields).filter(domain).all();
  75. },
  76. /* Consultar detalles nominas de funcionario */
  77. fetchPayslipLine: function() {
  78. var self = this;
  79. var fields = ['id', 'name', 'code', 'total', 'amount', 'slip_id'];
  80. var domain = [['slip_id', '=', self.id], ['code', '=', 'NET']];
  81. var payslipLine = new instance.Model('hr.payslip.line');
  82. return payslipLine.query(fields).filter(domain).all();
  83. },
  84. /* Consultar método de pagos */
  85. fecthJournal: function() {
  86. var fields = ['id', 'name', 'code', 'type', 'currency', 'default_debit_account_id', 'default_credit_account_id'];
  87. var domain = [['active', '=', true], ['type', 'in', ['bank', 'cash']], ['currency', '=', false]];
  88. var journalSalario = new instance.Model('account.journal');
  89. return journalSalario.query(fields).filter(domain).all();
  90. },
  91. /* Consultar Compania */
  92. fecthCompany: function() {
  93. var fields = ['id','name', 'currency_id'];
  94. var domain = [['id', '=', 1]];
  95. var resCompanyIds = new instance.Model('res.company');
  96. return resCompanyIds.query(fields).filter(domain).all();
  97. },
  98. /* Consultar Moneda Activas */
  99. fecthCurrency : function() {
  100. var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
  101. var domain = [['active','=', true]];
  102. var resCurrecy = new instance.Model('res.currency');
  103. return resCurrecy.query(fields).filter(domain).all();
  104. },
  105. /* Filtar moneda de la compania*/
  106. getCurrencyCompany: function(id) {
  107. var self = this;
  108. return _.filter(self.resCurrency, function(item) {
  109. return item.id === id;
  110. });
  111. },
  112. /* Activar el modal de pagos */
  113. showPayslipPayments: function(payslip, currency) {
  114. var self = this;
  115. var defer = $.Deferred();
  116. var hrPayslip = self.hrPayslip.shift();
  117. var payslipLine = self.payslipLine.shift();
  118. var company = self.resCompany.shift();
  119. var currency = self.getCurrencyCompany(company.currency_id[0]).shift();
  120. // cabecera
  121. var headerModalName = [{
  122. name: hrPayslip.name
  123. }];
  124. // ítem cuerpo
  125. var dataPayslip = [{
  126. employee: hrPayslip.employee_id[1],
  127. periodo: moment(hrPayslip.date_from).format('DD/MM/YYYY')+" - "+moment(hrPayslip.date_to).format('DD/MM/YYYY'),
  128. ref: hrPayslip.number,
  129. amount: accounting.formatMoney(payslipLine.amount, currency.symbol, currency.decimal_places, currency.thousands_separator, currency.decimal_separator)
  130. }];
  131. var payslipPayment = instance.qweb.render('EiruPayslipPaymentsModal', {
  132. data: dataPayslip,
  133. dataName: headerModalName,
  134. journal: self.accountJournal
  135. });
  136. $('.openerp_webclient_container').after(payslipPayment);
  137. $('.expired-account-modal').modal();
  138. var buttonPayment =$('.expired-account-modal').find('.payments-payslip');
  139. var journal_ref = $('.expired-account-modal').find('.journal-ref');
  140. var journal = $('.expired-account-modal').find('.current-journal');
  141. /* Cerrar el modal */
  142. $('.expired-account-modal').on('hidden.bs.modal', function (e) {
  143. defer.resolve(false);
  144. self.removeModal(e);
  145. });
  146. /* payments */
  147. buttonPayment.click(function(e) {
  148. if (!journal.val()) {
  149. instance.notification.do_warn("Atención","Seleccione un método de pago.");
  150. journal.css('border-color','red');
  151. return;
  152. }
  153. journal.css('border-color','#ccc');
  154. var payslip = new instance.Model('hr.payslip');
  155. payslip.call('create_from_prayslip', [
  156. {
  157. id: hrPayslip.id,
  158. journal_id: journal.val(),
  159. journal_ref: journal_ref.val()
  160. }
  161. ],{
  162. context: new instance.CompoundContext()
  163. }).then(function(results) {
  164. defer.resolve(results);
  165. if (results.length > 0){
  166. instance.notification.do_warn("Atención","No se pudo completar el pago de "+hrPayslip.name+" , porque ya se encuentra en un registros de caja/extractos bancarios (Referencia:"+results+")", true);
  167. }
  168. });
  169. self.removeModal(e);
  170. });
  171. return defer;
  172. },
  173. /* Remover la ventana modal */
  174. removeModal: function() {
  175. $('.expired-account-modal').remove();
  176. $('.modal-backdrop').remove();
  177. },
  178. /* Recargar la vista */
  179. reloadLine: function() {
  180. openerp.parentInstanceUnity.reload();
  181. },
  182. });
  183. if (openerp.web && openerp.web.FormView) {
  184. openerp.web.FormView.include({
  185. load_record: function(record) {
  186. this._super.apply(this, arguments);
  187. if (this.model !== 'hr.payslip')
  188. return;
  189. if (this.$el.find('.oe_form_sheet.oe_form_sheet_width').closest('.modal-content.openerp').length !== 0)
  190. return;
  191. openerp.parentInstanceUnity = this;
  192. if (openerp.widgetInstanceUnity) {
  193. openerp.widgetInstanceUnity.updateId(record.id);
  194. }
  195. if (this.$el.find('.payments-unity').length !== 0 )
  196. return;
  197. openerp.widgetInstanceUnity = new openerp.payslipPaymentsUnity(this);
  198. var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
  199. elemento = elemento.find('.payslip-payment-unity');
  200. openerp.widgetInstanceUnity.appendTo(elemento[0]);
  201. openerp.widgetInstanceUnity.updateId(record.id);
  202. }
  203. });
  204. }
  205. })();