Przeglądaj źródła

[ADD] Reconciliar Nomina de funcionario

adrielso 7 lat temu
rodzic
commit
f39f05d084

+ 1 - 0
__init__.py

@@ -1 +1,2 @@
 # -*- coding: utf-8 -*-
+from model import eiru_payslip_payments

+ 64 - 0
model/eiru_payslip_payments.py

@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+from openerp import api, fields, models
+from openerp.exceptions import except_orm
+
+class HrPayslip(models.Model):
+    _inherit = 'hr.payslip'
+
+    @api.model
+    def create_from_prayslip(self, values):
+        ### period Actual
+        period = self.env['account.period'].search([('date_start', '<=', fields.Date.context_today(self) ),('date_stop', '>=', fields.Date.context_today(self))])
+        ### Diario & Moneda
+        journal = self.env['account.journal'].browse(int(values['journal_id']))
+        ### payslip
+        payslip = self.env['hr.payslip'].browse([(values['id'])])
+        ### payslip line
+        payslip_line = self.env['hr.payslip.line'].search([('slip_id', '=', payslip.id), ('code','=', 'NET')])
+        # Move lines
+        moveline = self.env['account.move.line'].search([('move_id', 'in', [payslip.move_id.id]),('partner_id', 'in', [payslip.employee_id.address_home_id.id]), ('account_id', '=', 75)])
+
+        line_move = []
+        for line in moveline:
+            line_move.append({
+                'name': ":/ "+str(line.name),
+                'debit': line.credit if line.credit > 0 else 0.0 ,
+                'credit': line.debit if line.debit > 0 else 0.0 ,
+                'counterpart_move_line_id': line.id,
+            })
+
+        ### Consultar Caja  Abierta, Método de Pagos, Fecha de Hoy
+        bank_statement = self.env['account.bank.statement'].search([('journal_id', '=', [journal.id]), ('date', '=', fields.Date.context_today(self))])
+        ### Estructur statement
+        bank = {
+            'journal_id': journal.id,
+            'period_id': period.id,
+            'date': fields.Date.context_today(self),
+            'user_id': self.env.user.id,
+            'state': 'open' if journal.type == 'cash' else 'draft',
+        }
+
+        if bank_statement:
+            if len(bank_statement) == 1:
+                bank_statement.write(bank)
+            else:
+                bank_statement[len(bank_statement) -1].write(bank)
+        else:
+            bank_statement = bank_statement.create(bank)
+
+        bank_statement_line = {
+            'statement_id': bank_statement.id,
+            'name': payslip.name,
+            'partner_id': payslip.employee_id.address_home_id.id,
+            'amount': (-1* payslip_line.amount) ,
+            'ref': payslip.number,
+            'note': values['journal_ref']
+        }
+
+        line_statement = self.env['account.bank.statement.line'].create(bank_statement_line)
+        reconciled = line_statement.process_reconciliation(line_move)
+
+        if reconciled:
+            payslip.write({'state': 'paid'})
+
+        return True if (reconciled) else False

+ 1 - 3
static/src/css/style.css

@@ -9,10 +9,9 @@
     padding-bottom: 8px;
     font-size: 13px;
 }
-
 .label-descrip.payslip-name {
     width: 100%;
-    height: 30px
+    height: 30px;
 }
 .label-descrip.labe-input {
     padding-top: 5px;
@@ -39,7 +38,6 @@
     width: 200px;
     height: 30px;
 }
-/*botones*/
 .dismmis-modal {
     width: 100px;
     height: 30px;

+ 209 - 68
static/src/js/payslip_payments.js

@@ -8,6 +8,11 @@ openerp.eiru_payslip_payments = function(instance, local) {
         template: 'eiru_payslip_payments.PayslipWidget',
         id: undefined,
         payslip: [],
+        payslipLine: [],
+        accountJournal: [],
+        company: [],
+        currency: [],
+        payslipNew: [],
 
         init: function(parent) {
             this._super(parent);
@@ -16,40 +21,47 @@ openerp.eiru_payslip_payments = function(instance, local) {
             var self = this;
             this.$el.click(function(){
                 self.fectchInitial()
-                // self.showPayments();
             });
         },
         updateId: function(id) {
             var self = this;
             self.id = id;
         },
-
+        // Metodo Inicial
         fectchInitial: function() {
             var self = this;
             self.fectchHrPayslip().then(function(payslip) {
                 return payslip;
             }).then(function(payslip) {
                 self.payslip = payslip;
-                // console.log(self.payslip);
                 return self.fectchHrPayslipLine(payslip);
             }).then(function(payslipLine) {
                 self.payslipLine = payslipLine;
-                return self.fectchJournalSalario();
-            }).then(function(journalSalario) {
-                self.journalSalario = journalSalario;
-                console.log(journalSalario);
-
-                // console.log(payslipLine);
-                // local.parentInstance.reload();
-                // if (!payslip)
-                //     instance.web.notification.do_warn("Atención","No existe nomina para ser generada, en el periodo seleccionado");
+                return self.fectchJournal();
+            }).then(function(accountJournal) {
+                self.accountJournal = accountJournal;
+                return self.fectchCompany();
+            }).then(function(company) {
+                self.company = company;
+                return self.fectchCurency();
+            }).then(function(currency) {
+                self.currency = currency;
+                return self.joinPayslip();
+            }).then(function(payslipNew) {
+                self.payslipNew = payslipNew;
+                if (self.payslipNew.length <= 0) {
+                    instance.web.notification.do_warn("Atención","No tienes nominas que pagar");
+                    return
+                }
+                return self.asyncPaymentsPayslip();
             });
         },
+        // payslip
         fectchHrPayslip: function() {
             var self = this;
             var defer = $.Deferred();
             var fields = ['id', 'name', 'employee_id', 'date_from', 'date_to', 'move_id', 'number', 'line_ids', 'journal_id'];
-            var domain = [['payslip_run_id', 'in', [self.id]],['state', '=', 'done']];
+            var domain = [['payslip_run_id', 'in', [self.id]], ['state', '=', 'done']];
             var payslip = new instance.web.Model('hr.payslip');
             payslip.query(fields).filter(domain).all().then(function(results) {
                 defer.resolve(results);
@@ -59,13 +71,11 @@ openerp.eiru_payslip_payments = function(instance, local) {
         },
         // Hr payslip Line
         fectchHrPayslipLine: function(payslip) {
-            var self = this;
             var defer = $.Deferred();
-
             var payslip_id = _.map(payslip, function(map) {
                 return map.id;
             });
-            var fields = ['id', 'name', 'code', 'total', 'amount'];
+            var fields = ['id', 'name', 'code', 'total', 'amount', 'slip_id'];
             var domain = [['slip_id', 'in', payslip_id], ['code', '=', 'NET']];
             var payslipLine = new instance.web.Model('hr.payslip.line');
             payslipLine.query(fields).filter(domain).all().then(function(results) {
@@ -75,15 +85,10 @@ openerp.eiru_payslip_payments = function(instance, local) {
             return defer;
         },
         // Diario de Salario
-        fectchJournalSalario: function() {
-            var self = this;
+        fectchJournal: function() {
             var defer = $.Deferred();
-            var journal_id = _.map(self.payslip, function(map) {
-                return map.journal_id[0];
-            });
-            console.log(journal_id);
             var fields = ['id', 'name', 'code', 'type', 'currency', 'default_debit_account_id', 'default_credit_account_id'];
-            var domain = [['id', 'in', journal_id]];
+            var domain = [['active', '=', true], ['type', 'in', ['bank', 'cash']], ['currency', '=', false]];
             var journalSalario = new instance.web.Model('account.journal');
             journalSalario.query(fields).filter(domain).all().then(function(results) {
                 defer.resolve(results);
@@ -91,31 +96,109 @@ openerp.eiru_payslip_payments = function(instance, local) {
 
             return defer;
         },
-        // fectchGenerateNomina: function() {
-        //     var self = this;
-        //     var defer = $.Deferred();
-        //     var hr_payslip = new instance.web.Model('hr.payslip');
-        //
-        //     hr_payslip.call('generate_payroll_eiru',[self.id], {
-        //         context: new instance.web.CompoundContext()
-        //     }).then(function(results) {
-        //         defer.resolve(results);
-        //     });
-        //     return defer;
-        // },
-
-        showPayments: function() {
+        // Company
+        fectchCompany: function() {
+            var defer = $.Deferred();
+            var fields = ['id','name', 'currency_id'];
+            var domain = [['id', '=', 1]];
+            var resCompanyIds = new instance.web.Model('res.company');
+            resCompanyIds.query(fields).filter(domain).all().then(function (results) {
+                defer.resolve(results);
+            });
+
+            return defer;
+        },
+        // Res currecy
+        fectchCurency : function() {
+            var defer = $.Deferred();
+            var fields = ['id','name', 'symbol', 'rate_silent', 'base', 'decimal_separator', 'decimal_places', 'thousands_separator', 'symbol_position'];
+            var domain = [['active','=', true]];
+            var resCurrecy = new instance.web.Model('res.currency');
+            resCurrecy.query(fields).filter(domain).all().then(function(results) {
+                defer.resolve(results);
+            });
+
+            return defer;
+        },
+        // get payslipLine
+        getPayslipLine: function(slip_id) {
+            var self = this;
+            return _.filter(self.payslipLine, function(filter) {
+                return filter.slip_id[0] === slip_id;
+            });
+        },
+        // Unir Payslip con payslipLine
+        joinPayslip: function() {
+            var self = this;
+            var defer = $.Deferred();
+            var PayslipNew = [];
+            var line;
+            _.each(self.payslip, function(item) {
+                line = self.getPayslipLine(item.id).shift();
+                if (!line){
+                    line = {};
+                    line.total =0;
+                }
+                PayslipNew.push({
+                    'id': item.id,
+                    'name': item.name,
+                    'employee_id': item.employee_id[0],
+                    'employee': item.employee_id[1],
+                    'date_from': item.date_from,
+                    'date_to': item.date_to,
+                    'periodo': moment(item.date_from).format('DD/MM/YYYY')+" - "+moment(item.date_to).format('DD/MM/YYYY'),
+                    'number': item.number,
+                    'ammout': line.total
+                });
+            });
+            defer.resolve(PayslipNew);
+
+            return defer;
+        },
+        // Moneda de la compania
+        getCurrencyCompany: function(id_currency) {
+            var self = this;
+            return _.filter(self.currency, function(item) {
+                return item.id === id_currency;
+            });
+        },
+        // Async loop
+        asyncPaymentsPayslip: function() {
+            var self = this;
+            var defer = $.Deferred();
+            var company = self.company.shift();
+            var currency = self.getCurrencyCompany(company.currency_id[0]).shift()
+
+            self.asyncLoopFactory(self.payslipNew.length, function(loop) {
+                var payslip = self.payslipNew[loop.iteration()];
+                self.showPayments(payslip, currency).then(function(results) {
+                    self.reloadLine()
+                    if (results){
+                        loop.next();
+                    }
+                });
+            }, function(results) {
+                defer.resolve(results);
+            });
+            return defer;
+        },
+        // reloadLine
+        reloadLine: function() {
+            local.parentInstance.reload();
+        },
+        // Ejc. el Modal
+        showPayments: function(payslip, currency) {
             var self = this;
-            // Nomina Detalle
+            var defer = $.Deferred();
+            var state = true;
             var headerModalName = [{
-                name: "Nómina salarial de Adrielso Kunert para diciembre-2017"
+                name: payslip.name
             }];
-            //
-            dataPayslip = [{
-                employee: 'Adrielso kunert',
-                periodo: '01-10-2017 -30-10-2017',
-                salario: '5.000.000',
-                ref: '	SLIP/062'
+
+            var dataPayslip = [{
+                employee: payslip.employee,
+                periodo: payslip.periodo,
+                ref: payslip.number
             }]
 
             var modal = Qweb.render('EiruPayslipPaymentsModal', {
@@ -123,45 +206,102 @@ openerp.eiru_payslip_payments = function(instance, local) {
                 dataName: headerModalName,
             });
 
-
             $('.openerp_webclient_container').after(modal);
             $('.expired-account-modal').modal();
-
+            // Total
+            $('.expired-account-modal').find('.amount-net').val(accounting.formatMoney(payslip.ammout, currency.symbol, currency.decimal_places, currency.thousands_separator, currency.decimal_separator))
+            // Referencia de Pago
+            var journal_ref = $('.expired-account-modal').find('.journal-ref');
             // Cargara los metodo de pago
             var journal =  $('.expired-account-modal').find('.current-journal');
-            console.log(journal);
-            journal.append('<option value="1">TODAS LAS SUC.</option>');
-            journal.append('<option value="2">TODAS LAS SUC.</option>');
-            journal.append('<option value="3">TODAS LAS SUC.</option>');
-            journal.append('<option value="4">TODAS LAS SUC.</option>');
-            journal.append('<option value="5">TODAS LAS SUC.</option>');
-            // Actualizar monto a pagar
-            $('.expired-account-modal').find('.amount-net').val('5.000.000')
-
-            // Selecion de Journal
-            journal.click(function(e){
-                $('.expired-account-modal').find('.amount-net').val(journal.val())
+            _.each(self.accountJournal, function(item) {
+                journal.append('<option value="'+item.id+'">'+item.name+'</option>');
             })
             // Click Cerrar
             $('.expired-account-modal').on('hidden.bs.modal', function (e) {
+                // self.reloadLine()
+                defer.resolve(false);
                 self.removeModal(e);
-            })
 
+                // return defer;
+            })
             // clcik boton pagar
             var contenido = $('.expired-account-modal').find('.payments-payslip');
-            contenido.click(function (e) {
-                // $(contenido).find('tr').removeClass('table-row-select');
-                // $(e.target).closest('tr').addClass('table-row-select');
-                // var chirdren_id =$(e.target).closest('tr').children()[0].textContent;
-                // self.renderForm(chirdren_id);
-                // console.log(e);
+            contenido.click(function(e) {
+
+                self.paymentsPayslip(payslip,journal.val(), journal_ref.val()).then(function(insert) {
+                    return insert;
+                }).then(function(journal) {
+                    if (!journal)
+                        state = false;
+
+                    // self.reloadLine()
+                    defer.resolve(state);
+                })
+                self.removeModal(e);
             });
+
+
+            return defer;
         },
-        // Remover <Modal></Modal>
-        removeModal: function (e) {
+        // Remover
+        removeModal: function() {
             $('.expired-account-modal').remove();
             $('.modal-backdrop').remove();
         },
+        // Pagara la Nomina
+        paymentsPayslip: function(payslip, journal, journal_ref) {
+            var defer = $.Deferred();
+            var  HrPayslip = new instance.web.Model('hr.payslip');
+            HrPayslip.call('create_from_prayslip',[
+                {
+                    id: payslip.id,
+                    journal_id: journal,
+                    journal_ref: journal_ref
+                }
+            ],{
+                context: new instance.web.CompoundContext()
+            }).then(function(results) {
+                defer.resolve(results);
+            });
+
+            return defer;
+        },
+        /* ---------------------------------------------------------------------
+        * Description: Async loop util v2 written by Robert Gauto.
+        * --------------------------------------------------------------------*/
+        asyncLoopFactory: function (iterations, func, callback) {
+            var index = 0;
+            var done = false;
+            var loop = {
+                next: function() {
+                    if (done) {
+                        return;
+                    }
+
+                    if (index < iterations) {
+                        index++;
+                        func(loop);
+
+                    } else {
+                        done = true;
+                        callback();
+                    }
+                },
+
+                iteration: function () {
+                    return index - 1;
+                },
+
+                break: function () {
+                    done = true;
+                    callback();
+                }
+            };
+
+            loop.next();
+            return loop;
+        },
     });
 
     if (instance.web && instance.web.FormView) {
@@ -191,4 +331,5 @@ openerp.eiru_payslip_payments = function(instance, local) {
             }
         });
     }
+
 }

+ 1 - 43
static/src/xml/hr_payslip_payments.xml

@@ -13,7 +13,6 @@
                     </div>
                     <!-- Body -->
                     <div class="modal-body">
-                        <!-- Descripcion de la nominas  -->
                         <div class="row" t-foreach="dataName" t-as="head">
                             <div class="col-xs-12">
                                 <label class="label-descrip payslip-name">
@@ -21,7 +20,6 @@
                                 </label>
                             </div>
                         </div>
-                        <!-- Datos del Empleado  -->
                         <div class="row" t-foreach="data" t-as="data">
                             <div class="col-xs-6">
                                 <label class="label-descrip">Empleado : </label>
@@ -45,58 +43,18 @@
                                 <label class="label-descrip labe-input">Monto a Pagar : </label>
                                 <input class="details-employee amount-net" readonly='readonly'></input>
                             </div>
-                            <!-- Metodo de pagos -->
+
                             <div class="col-xs-6">
                                 <label class="label-descrip labe-input">Método de pago: </label>
                                 <select class="current-journal">
                                 </select>
                             </div>
-                            <!-- Referencia de pagos -->
                             <div class="col-xs-6">
                                 <label class="label-descrip labe-input">Referencia de pago: </label>
                                 <input type="text" class="journal-ref"></input>
                             </div>
                         </div>
                     </div>
-                    <!-- <div class="oe_view_manager_body table-model"> -->
-                        <!-- tabla Head -->
-                        <!-- <div class="modal-head-wrapper">
-                            <table class="oe_list_content">
-                                <thead >
-                                    <tr class="oe_list_header_columns" >
-                                        <th t-foreach="dataThead" t-as="head" class="oe_list_header_char oe_sortable">
-                                            <t t-esc="head_value.title"/>
-                                        </th>
-                                    </tr>
-                                </thead>
-                            </table>
-                        </div> -->
-                        <!-- table body -->
-                        <!-- <div class="modal-items-wrapper">
-                            <table class="oe_list_content">
-                                <tbody class="table-tbody">
-                                    <tr t-foreach="data" t-as="field">
-                                        <td><t t-esc="field_value.invoice[0]"/></td>
-                                        <td><t t-esc="field_value.partner_id[1]"/></td>
-                                        <td><t t-esc="field_value.ref"/></td>
-                                        <td>
-                                            <t t-set="vencimiento" t-value="field_value.date_maturity"/>
-                                            <t t-js="ctx">
-                                                ctx.vencimiento = $.timeago(moment(ctx.vencimiento).format("YYYY-DD-MM"));
-                                            </t>
-                                            <t t-esc="vencimiento"/></td>
-                                        <td>
-                                            <t t-set="monto" t-value="field_value.amount_residual"/>
-                                            <t t-js="ctx">
-                                                ctx.monto = accounting.formatNumber(ctx.monto,0,'.',',');
-                                            </t>
-                                            <t t-esc="monto"/>
-                                        </td>
-                                    </tr>
-                                </tbody>
-                            </table>
-                        </div> -->
-                    <!-- </div> -->
                     <!-- Pie de Pagina -->
                     <div class="modal-footer">
                         <button type="button" class="oe_button oe_form_button oe_highlight payments-payslip">Pagar</button>