|
@@ -0,0 +1,252 @@
|
|
|
|
+openerp.account_bank_payslip_import = function (instance, local) {
|
|
|
|
+ local.widgetInstance = null;
|
|
|
|
+ local.parentInstance = null;
|
|
|
|
+
|
|
|
|
+ local.PayslipImportWidget = instance.Widget.extend({
|
|
|
|
+ template: 'account_bank_payslip_import.PayslipImport',
|
|
|
|
+ id : undefined,
|
|
|
|
+ hrPaylslip : [],
|
|
|
|
+ hrPaylslipLine : [],
|
|
|
|
+ payslipImport : [],
|
|
|
|
+ hrEmployee : [],
|
|
|
|
+ statementLine:[],
|
|
|
|
+
|
|
|
|
+ init: function (parent) {
|
|
|
|
+ this._super(parent);
|
|
|
|
+ },
|
|
|
|
+ // Actualizar id del Objeto
|
|
|
|
+ updateId : function(id){
|
|
|
|
+ var self = this;
|
|
|
|
+ self.id=id;
|
|
|
|
+ },
|
|
|
|
+ start: function () {
|
|
|
|
+ var self = this;
|
|
|
|
+ this.$el.click(function (e){
|
|
|
|
+ self.fetchInitial();
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ // Iniciar
|
|
|
|
+ fetchInitial: function(){
|
|
|
|
+ var self= this;
|
|
|
|
+ self.fetchBankStatementLine().then(function(statementLine){
|
|
|
|
+ return statementLine;
|
|
|
|
+ }).then(function(statementLine){
|
|
|
|
+ self.statementLine = statementLine;
|
|
|
|
+ return self.fetchHrPayslip(statementLine);
|
|
|
|
+ }).then(function(hrPaylslip){
|
|
|
|
+ self.hrPaylslip = hrPaylslip;
|
|
|
|
+ return self.fetchHrPayslipLine(hrPaylslip);
|
|
|
|
+ }).then(function(hrPaylslipLine){
|
|
|
|
+ self.hrPaylslipLine = hrPaylslipLine;
|
|
|
|
+ return self.fetchEmployee();
|
|
|
|
+ }).then(function(hrEmployee){
|
|
|
|
+ self.hrEmployee=hrEmployee;
|
|
|
|
+ return self.fetchJoinPayslip();
|
|
|
|
+ }).then(function(payslipImport){
|
|
|
|
+ self.payslipImport = payslipImport;
|
|
|
|
+ return self.insertBankStatementLine();
|
|
|
|
+ }).then(function(bankLine){
|
|
|
|
+ local.parentInstance.reload();
|
|
|
|
+ if (bankLine){
|
|
|
|
+ instance.web.notification.do_notify("Felicitación","Las nóminas han sido importado con éxito.");
|
|
|
|
+ }else{
|
|
|
|
+ instance.web.notification.do_warn("Atención ","No hay nóminas para ser importadas.");
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ // linea de Caja
|
|
|
|
+ fetchBankStatementLine : function(){
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+
|
|
|
|
+ var fields=['id','ref'];
|
|
|
|
+ var domain=[['statement_id','=',self.id]];
|
|
|
|
+
|
|
|
|
+ var statementLine = new instance.web.Model('account.bank.statement.line');
|
|
|
|
+
|
|
|
|
+ statementLine.query(fields).filter(domain).all().then(function (results){
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ // consulta Nomina
|
|
|
|
+ fetchHrPayslip : function(statementLine){
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer =$.Deferred();
|
|
|
|
+
|
|
|
|
+ var ref = _.flatten(_.map(statementLine,function(map){
|
|
|
|
+ return map.ref;
|
|
|
|
+ }));
|
|
|
|
+
|
|
|
|
+ var fields =['id','number','employee_id','state'];
|
|
|
|
+ var domain =[['state','=','done'],['number','not in',ref]];
|
|
|
|
+ var hrPaylslip = new instance.web.Model('hr.payslip');
|
|
|
|
+
|
|
|
|
+ hrPaylslip.query(fields).filter(domain).all().then(function(results){
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ // consulta Lineas de Nomina
|
|
|
|
+ fetchHrPayslipLine : function(hrPaylsli){
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer =$.Deferred();
|
|
|
|
+ payslip = _.flatten(_.map(hrPaylsli,function(map){
|
|
|
|
+ return map.id;
|
|
|
|
+ }));
|
|
|
|
+
|
|
|
|
+ var fields =['id','slip_id','code','amount','name'];
|
|
|
|
+ var domain =[['code','=','NET'],['slip_id','in',payslip]];
|
|
|
|
+ var hrPaylslip = new instance.web.Model('hr.payslip.line');
|
|
|
|
+
|
|
|
|
+ hrPaylslip.query(fields).filter(domain).all().then(function(results){
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ // consultar employee
|
|
|
|
+ fetchEmployee : function(){
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var id_employee =_.flatten(_.map(self.hrPaylslip, function(map){
|
|
|
|
+ return map.employee_id[0];
|
|
|
|
+ }));
|
|
|
|
+
|
|
|
|
+ var fields=['id','name','address_home_id'];
|
|
|
|
+ var domain=[['id','in',id_employee]];
|
|
|
|
+
|
|
|
|
+ var hrEmployee= new instance.web.Model('hr.employee');
|
|
|
|
+
|
|
|
|
+ hrEmployee.query(fields).filter(domain).all().then(function(results){
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ // Generar Objeto principal
|
|
|
|
+ fetchJoinPayslip:function(){
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var itemPayslip;
|
|
|
|
+ var itemLine;
|
|
|
|
+ var payslipImport=[];
|
|
|
|
+ var employee;
|
|
|
|
+
|
|
|
|
+ for (var i = 0; i < self.hrPaylslip.length; i++) {
|
|
|
|
+ itemPayslip = self.hrPaylslip[i];
|
|
|
|
+ itemLine = self.getPayslipLine(itemPayslip.id);
|
|
|
|
+ employee = self.getEmployee(itemPayslip.employee_id[0]);
|
|
|
|
+
|
|
|
|
+ if (itemLine){
|
|
|
|
+ payslipImport.push({
|
|
|
|
+ statement_id: this.id,
|
|
|
|
+ name : itemLine.slip_id[1],
|
|
|
|
+ ref :itemPayslip.number,
|
|
|
|
+ partner_id :employee.address_home_id[0],
|
|
|
|
+ amount : itemLine.amount*-1
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ defer.resolve(payslipImport);
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ // Obtener Payslip Linea
|
|
|
|
+ getPayslipLine : function(id_slip){
|
|
|
|
+ var self = this;
|
|
|
|
+ return _.filter(self.hrPaylslipLine,function(item){
|
|
|
|
+ return item.slip_id[0] === id_slip;
|
|
|
|
+ }).shift();
|
|
|
|
+ },
|
|
|
|
+ // obtener partner
|
|
|
|
+ getEmployee : function(employee_id){
|
|
|
|
+ var self = this;
|
|
|
|
+ return _.filter(self.hrEmployee,function(item){
|
|
|
|
+ return item.id === employee_id;
|
|
|
|
+ }).shift();
|
|
|
|
+ },
|
|
|
|
+ // insertar la Linea
|
|
|
|
+ insertBankStatementLine : function(){
|
|
|
|
+ var self =this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var accountBanckStatementLine = new instance.web.Model('account.bank.statement.line');
|
|
|
|
+
|
|
|
|
+ self.asyncLoopFactory(self.payslipImport.length, function (loop) {
|
|
|
|
+ var objeto = self.payslipImport[loop.iteration()];
|
|
|
|
+
|
|
|
|
+ accountBanckStatementLine.call('create',[objeto], {
|
|
|
|
+ context: new instance.web.CompoundContext()
|
|
|
|
+ }).then(function (results) {
|
|
|
|
+ loop.next();
|
|
|
|
+ });
|
|
|
|
+ }, function () {
|
|
|
|
+ defer.resolve(self.payslipImport.length);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ 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) {
|
|
|
|
+ instance.web.FormView.include({
|
|
|
|
+ load_record: function (record) {
|
|
|
|
+ this._super.apply(this, arguments);
|
|
|
|
+
|
|
|
|
+ if (this.model !== 'account.bank.statement') return;
|
|
|
|
+
|
|
|
|
+ local.parentInstance = this;
|
|
|
|
+
|
|
|
|
+ if (local.widgetInstance) {
|
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this.$el.find('.import_payslip').length != 0) return;
|
|
|
|
+
|
|
|
|
+ local.widgetInstance = new local.PayslipImportWidget(this);
|
|
|
|
+
|
|
|
|
+ var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
|
|
|
|
+ elemento = elemento.find('.oe_right.oe_button_box.payslip_import');
|
|
|
|
+
|
|
|
|
+ local.widgetInstance.appendTo(elemento);
|
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|