|
@@ -0,0 +1,201 @@
|
|
|
|
+openerp.account_bank_voucher_import = function (instance, local) {
|
|
|
|
+ local.widgetInstance = null;
|
|
|
|
+ local.parentInstance = null;
|
|
|
|
+
|
|
|
|
+ local.VoucherImportWidget = instance.Widget.extend({
|
|
|
|
+ template: 'account_bank_voucher_import.VoucherImport',
|
|
|
|
+ id: undefined,
|
|
|
|
+ accountStatement: [],
|
|
|
|
+ statementLine: [],
|
|
|
|
+ accountVoucher: [],
|
|
|
|
+ voucherImport: [],
|
|
|
|
+
|
|
|
|
+ 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() {
|
|
|
|
+ self.fetchInitial();
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ // Iniciar
|
|
|
|
+ fetchInitial: function() {
|
|
|
|
+ var self = this;
|
|
|
|
+
|
|
|
|
+ self.$el.attr("disabled", true);
|
|
|
|
+
|
|
|
|
+ self.fetchBankStatement().then(function(accountStatement) {
|
|
|
|
+ return accountStatement;
|
|
|
|
+ }).then(function(accountStatement) {
|
|
|
|
+ self.accountStatement = accountStatement;
|
|
|
|
+ return self.fetchVoucher();
|
|
|
|
+ }).then(function(accountVouche) {
|
|
|
|
+ self.accountVouche = accountVouche;
|
|
|
|
+ return self.fetchJoinVoucher();
|
|
|
|
+ }).then(function(voucherImport) {
|
|
|
|
+ self.voucherImport = voucherImport;
|
|
|
|
+ return self.insertBankStatementLine();
|
|
|
|
+ }).then(function(bankLine) {
|
|
|
|
+ local.parentInstance.reload();
|
|
|
|
+ if (bankLine) {
|
|
|
|
+ instance.web.notification.do_notify("Felicitaciones","Has importado los pagos con éxito.");
|
|
|
|
+ }else {
|
|
|
|
+ instance.web.notification.do_warn("Atención ","No tienes pagos que importar en esa caja.");
|
|
|
|
+ }
|
|
|
|
+ self.$el.removeAttr("disabled");
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+ // Registro de Caja
|
|
|
|
+ fetchBankStatement: function() {
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var fields = ['id', 'journal_id'];
|
|
|
|
+ var domain = [['id', '=', self.id]];
|
|
|
|
+ var statement = new instance.web.Model('account.bank.statement');
|
|
|
|
+
|
|
|
|
+ statement.query(fields).filter(domain).all().then(function (results) {
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ //Consultar Voucher
|
|
|
|
+ fetchVoucher: function() {
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var journal = _.flatten(_.map(self.accountStatement,function(map) {
|
|
|
|
+ return map.journal_id[0];
|
|
|
|
+ }));
|
|
|
|
+ var fields = ['id', 'partner_id', 'amount', 'journal_id', 'reference', 'move_id', 'account_id', 'state', 'type'];
|
|
|
|
+ var domain = [['state', '=', 'posted'], ['journal_id', 'in', journal], ["bank_statement_line_ids", '=', false]];
|
|
|
|
+
|
|
|
|
+ var accountVouche = new instance.web.Model('account.voucher');
|
|
|
|
+
|
|
|
|
+ accountVouche.query(fields).filter(domain).all().then(function(results) {
|
|
|
|
+ defer.resolve(results);
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ // Generar Objeto principal
|
|
|
|
+ fetchJoinVoucher:function() {
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var itemvoucher;
|
|
|
|
+ var voucherImport = [];
|
|
|
|
+ var ammount = 0;
|
|
|
|
+ for (var i = 0; i < self.accountVouche.length; i++) {
|
|
|
|
+ itemvoucher = self.accountVouche[i];
|
|
|
|
+ if (itemvoucher.type === 'payment'){
|
|
|
|
+ ammount = (itemvoucher.amount * -1 );
|
|
|
|
+ } else {
|
|
|
|
+ ammount = (itemvoucher.amount);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ voucherImport.push({
|
|
|
|
+ statement_id: this.id,
|
|
|
|
+ name: itemvoucher.reference,
|
|
|
|
+ partner_id: itemvoucher.partner_id[0],
|
|
|
|
+ amount: ammount,
|
|
|
|
+ voucher_id: itemvoucher.id,
|
|
|
|
+ journal_id: itemvoucher.journal_id[0],
|
|
|
|
+ account_id: itemvoucher.account_id[0],
|
|
|
|
+ journal_entry_id: itemvoucher.move_id[0]
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ defer.resolve(voucherImport);
|
|
|
|
+
|
|
|
|
+ return defer;
|
|
|
|
+ },
|
|
|
|
+ // insertar la Linea
|
|
|
|
+ insertBankStatementLine: function() {
|
|
|
|
+ var self = this;
|
|
|
|
+ var defer = $.Deferred();
|
|
|
|
+ var accountBanckStatementLine = new instance.web.Model('account.bank.statement.line');
|
|
|
|
+
|
|
|
|
+ self.asyncLoopFactory(self.voucherImport.length, function (loop) {
|
|
|
|
+ var objeto = self.voucherImport[loop.iteration()];
|
|
|
|
+
|
|
|
|
+ accountBanckStatementLine.call('create',[objeto], {
|
|
|
|
+ context: new instance.web.CompoundContext()
|
|
|
|
+ }).then(function() {
|
|
|
|
+ loop.next();
|
|
|
|
+ });
|
|
|
|
+ }, function() {
|
|
|
|
+ defer.resolve(self.voucherImport.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-voucher').length !== 0) return;
|
|
|
|
+
|
|
|
|
+ local.widgetInstance = new local.VoucherImportWidget(this);
|
|
|
|
+
|
|
|
|
+ var elemento = this.$el.find('.oe_form_sheet.oe_form_sheet_width');
|
|
|
|
+ elemento = elemento.find('.oe_right.oe_button_box.voucher-import');
|
|
|
|
+
|
|
|
|
+ local.widgetInstance.appendTo(elemento);
|
|
|
|
+ local.widgetInstance.updateId(record.id);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|