payslip_payments.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. openerp.eiru_payslip_payments = function(instance, local) {
  2. local.widgetInstance = null;
  3. local.parentInstance = null;
  4. var Qweb = openerp.web.qweb;
  5. local.PayslipPaymentsWidget = instance.Widget.extend({
  6. template: 'eiru_payslip_payments.PayslipWidget',
  7. id: undefined,
  8. payslip: [],
  9. payslipLine: [],
  10. accountJournal: [],
  11. company: [],
  12. currency: [],
  13. payslipNew: [],
  14. init: function(parent) {
  15. this._super(parent);
  16. this.buttons = parent.$buttons;
  17. },
  18. start: function() {
  19. var self = this;
  20. this.$el.click(function(){
  21. self.fectchInitial()
  22. });
  23. self.buttons.click(function(e) {
  24. /* E (Editar) */
  25. if (e.target.accessKey === 'E')
  26. self.$el.css('display','none');
  27. /* S (Guarrdar) */
  28. if (e.target.accessKey === 'S')
  29. self.$el.css('display','flex');
  30. /* D (Cancelar) */
  31. if (e.target.accessKey === 'D')
  32. self.$el.css('display','flex');
  33. });
  34. },
  35. updateId: function(id) {
  36. var self = this;
  37. self.id = id;
  38. },
  39. // Metodo Inicial
  40. fectchInitial: function() {
  41. var self = this;
  42. self.fectchHrPayslip().then(function(payslip) {
  43. return payslip;
  44. }).then(function(payslip) {
  45. self.payslip = payslip;
  46. return self.fectchHrPayslipLine(payslip);
  47. }).then(function(payslipLine) {
  48. self.payslipLine = payslipLine;
  49. return self.fectchJournal();
  50. }).then(function(accountJournal) {
  51. self.accountJournal = accountJournal;
  52. return self.fectchCompany();
  53. }).then(function(company) {
  54. self.company = company;
  55. return self.fectchCurency();
  56. }).then(function(currency) {
  57. self.currency = currency;
  58. return self.joinPayslip();
  59. }).then(function(payslipNew) {
  60. self.payslipNew = payslipNew;
  61. if (self.payslipNew.length <= 0) {
  62. instance.web.notification.do_warn("Atención","No tienes nominas que pagar");
  63. return
  64. }
  65. return self.asyncPaymentsPayslip();
  66. });
  67. },
  68. // payslip
  69. fectchHrPayslip: function() {
  70. var self = this;
  71. var defer = $.Deferred();
  72. var fields = ['id', 'name', 'employee_id', 'date_from', 'date_to', 'move_id', 'number', 'line_ids', 'journal_id'];
  73. var domain = [['payslip_run_id', 'in', [self.id]], ['state', '=', 'done']];
  74. var payslip = new instance.web.Model('hr.payslip');
  75. payslip.query(fields).filter(domain).all().then(function(results) {
  76. defer.resolve(results);
  77. });
  78. return defer;
  79. },
  80. // Hr payslip Line
  81. fectchHrPayslipLine: function(payslip) {
  82. var defer = $.Deferred();
  83. var payslip_id = _.map(payslip, function(map) {
  84. return map.id;
  85. });
  86. var fields = ['id', 'name', 'code', 'total', 'amount', 'slip_id'];
  87. var domain = [['slip_id', 'in', payslip_id], ['code', '=', 'NET']];
  88. var payslipLine = new instance.web.Model('hr.payslip.line');
  89. payslipLine.query(fields).filter(domain).all().then(function(results) {
  90. defer.resolve(results);
  91. });
  92. return defer;
  93. },
  94. // Diario de Salario
  95. fectchJournal: function() {
  96. var defer = $.Deferred();
  97. var fields = ['id', 'name', 'code', 'type', 'currency', 'default_debit_account_id', 'default_credit_account_id'];
  98. var domain = [['active', '=', true], ['type', 'in', ['bank', 'cash']], ['currency', '=', false]];
  99. var journalSalario = new instance.web.Model('account.journal');
  100. journalSalario.query(fields).filter(domain).all().then(function(results) {
  101. defer.resolve(results);
  102. });
  103. return defer;
  104. },
  105. // Company
  106. fectchCompany: function() {
  107. var defer = $.Deferred();
  108. var fields = ['id','name', 'currency_id'];
  109. var domain = [['id', '=', 1]];
  110. var resCompanyIds = new instance.web.Model('res.company');
  111. resCompanyIds.query(fields).filter(domain).all().then(function (results) {
  112. defer.resolve(results);
  113. });
  114. return defer;
  115. },
  116. // Res currecy
  117. fectchCurency : function() {
  118. var defer = $.Deferred();
  119. var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
  120. var domain = [['active','=', true]];
  121. var resCurrecy = new instance.web.Model('res.currency');
  122. resCurrecy.query(fields).filter(domain).all().then(function(results) {
  123. defer.resolve(results);
  124. });
  125. return defer;
  126. },
  127. // get payslipLine
  128. getPayslipLine: function(slip_id) {
  129. var self = this;
  130. return _.filter(self.payslipLine, function(filter) {
  131. return filter.slip_id[0] === slip_id;
  132. });
  133. },
  134. // Unir Payslip con payslipLine
  135. joinPayslip: function() {
  136. var self = this;
  137. var defer = $.Deferred();
  138. var PayslipNew = [];
  139. var line;
  140. _.each(self.payslip, function(item) {
  141. line = self.getPayslipLine(item.id).shift();
  142. if (!line){
  143. line = {};
  144. line.total =0;
  145. }
  146. PayslipNew.push({
  147. 'id': item.id,
  148. 'name': item.name,
  149. 'employee_id': item.employee_id[0],
  150. 'employee': item.employee_id[1],
  151. 'date_from': item.date_from,
  152. 'date_to': item.date_to,
  153. 'periodo': moment(item.date_from).format('DD/MM/YYYY')+" - "+moment(item.date_to).format('DD/MM/YYYY'),
  154. 'number': item.number,
  155. 'ammout': line.total
  156. });
  157. });
  158. defer.resolve(PayslipNew);
  159. return defer;
  160. },
  161. // Moneda de la compania
  162. getCurrencyCompany: function(id_currency) {
  163. var self = this;
  164. return _.filter(self.currency, function(item) {
  165. return item.id === id_currency;
  166. });
  167. },
  168. // Async loop
  169. asyncPaymentsPayslip: function() {
  170. var self = this;
  171. var defer = $.Deferred();
  172. var company = self.company.shift();
  173. var currency = self.getCurrencyCompany(company.currency_id[0]).shift();
  174. self.asyncLoopFactory(self.payslipNew.length, function(loop) {
  175. var payslip = self.payslipNew[loop.iteration()];
  176. self.showPayments(payslip, currency).then(function(results) {
  177. self.reloadLine();
  178. if (results){
  179. loop.next();
  180. }
  181. });
  182. }, function(results) {
  183. defer.resolve(results);
  184. });
  185. return defer;
  186. },
  187. // reloadLine
  188. reloadLine: function() {
  189. local.parentInstance.reload();
  190. },
  191. // Ejc. el Modal
  192. showPayments: function(payslip, currency) {
  193. var self = this;
  194. var defer = $.Deferred();
  195. var state = true;
  196. var headerModalName = [{
  197. name: payslip.name
  198. }];
  199. var dataPayslip = [{
  200. employee: payslip.employee,
  201. periodo: payslip.periodo,
  202. ref: payslip.number,
  203. amount: accounting.formatMoney(payslip.ammout, currency.symbol, currency.decimal_places, currency.thousands_separator, currency.decimal_separator)
  204. }];
  205. var modal = Qweb.render('EiruPayslipPaymentsModal', {
  206. data: dataPayslip,
  207. dataName: headerModalName,
  208. journal: self.accountJournal
  209. });
  210. $('.openerp_webclient_container').after(modal);
  211. $('.expired-account-modal').modal();
  212. // Referencia de Pago\
  213. var journal_ref = $('.expired-account-modal').find('.journal-ref');
  214. var journal = $('.expired-account-modal').find('.current-journal');
  215. // Click Cerrar
  216. $('.expired-account-modal').on('hidden.bs.modal', function (e) {
  217. defer.resolve(false);
  218. self.removeModal(e);
  219. });
  220. // clcik boton pagar
  221. var contenido = $('.expired-account-modal').find('.payments-payslip');
  222. contenido.click(function(e) {
  223. self.paymentsPayslip(payslip,journal.val(), journal_ref.val()).then(function(insert) {
  224. return insert;
  225. }).then(function(journal) {
  226. if (!journal)
  227. state = false;
  228. defer.resolve(state);
  229. })
  230. self.removeModal(e);
  231. });
  232. return defer;
  233. },
  234. // Remover
  235. removeModal: function() {
  236. $('.expired-account-modal').remove();
  237. $('.modal-backdrop').remove();
  238. },
  239. // Pagara la Nomina
  240. paymentsPayslip: function(payslip, journal, journal_ref) {
  241. var defer = $.Deferred();
  242. var HrPayslip = new instance.web.Model('hr.payslip');
  243. HrPayslip.call('create_from_prayslip',[
  244. {
  245. id: payslip.id,
  246. journal_id: journal,
  247. journal_ref: journal_ref
  248. }
  249. ],{
  250. context: new instance.web.CompoundContext()
  251. }).then(function(results) {
  252. defer.resolve(results);
  253. });
  254. return defer;
  255. },
  256. /* ---------------------------------------------------------------------
  257. * Description: Async loop util v2 written by Robert Gauto.
  258. * --------------------------------------------------------------------*/
  259. asyncLoopFactory: function (iterations, func, callback) {
  260. var index = 0;
  261. var done = false;
  262. var loop = {
  263. next: function() {
  264. if (done) {
  265. return;
  266. }
  267. if (index < iterations) {
  268. index++;
  269. func(loop);
  270. } else {
  271. done = true;
  272. callback();
  273. }
  274. },
  275. iteration: function () {
  276. return index - 1;
  277. },
  278. break: function () {
  279. done = true;
  280. callback();
  281. }
  282. };
  283. loop.next();
  284. return loop;
  285. },
  286. });
  287. if (instance.web && instance.web.FormView) {
  288. instance.web.FormView.include({
  289. load_record: function(record) {
  290. this._super.apply(this, arguments);
  291. if (this.model !== 'hr.payslip.run')
  292. return;
  293. local.parentInstance = this;
  294. if (local.widgetInstance) {
  295. local.widgetInstance.updateId(record.id);
  296. }
  297. if (this.$el.find('.payslip-payments').length !== 0 )
  298. return;
  299. local.widgetInstance = new local.PayslipPaymentsWidget(this);
  300. var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
  301. elemento = elemento.find('.oe_right.oe_button_box.eiru-payslip-payments');
  302. local.widgetInstance.appendTo(elemento);
  303. local.widgetInstance.updateId(record.id);
  304. },
  305. });
  306. }
  307. }