Kaynağa Gözat

[ADD] Validacion en registro de caja, elminar lineas, cancelar caja

adrielso 7 yıl önce
ebeveyn
işleme
f8e4b28aa7

+ 13 - 11
__openerp__.py

@@ -2,19 +2,21 @@
 {
     'name': 'Account bank voucher import',
     'author':  'Adrielso Kunert',
-    'version': '8.0.1.1.0',
+    'version': '0.2',
     'category': 'account',
     'description': "Importar pagos en el registro de caja.",
-    'depends':[ 'account',
-                'eiru_assets'
-              ],
-    'data':   [
-                'views/voucher_import.xml',
-                'views/template.xml',
-              ],
-    'qweb':   [
-                'static/src/xml/*.xml'
-              ],
+    'depends': [
+        'account',
+        'account_voucher',
+        'eiru_assets'
+    ],
+    'data': [
+        'views/voucher_import.xml',
+        'views/template.xml',
+    ],
+    'qweb': [
+        'static/src/xml/*.xml'
+    ],
 
     'installable': True,
 }

BIN
model/__init__.pyc


+ 55 - 1
model/account_bank_voucher_import.py

@@ -1,8 +1,62 @@
 # -*- coding: utf-8 -*-
 from openerp import models, fields, api,  _
+from openerp.exceptions import Warning
 
+class AccountBankStatement(models.Model):
+    _inherit = 'account.bank.statement'
 
-class account_bank_statement_line(models.Model):
+    def button_cancel(self, cr, uid, ids, context=None):
+        context['cancel_from_statement'] = True
+        return super(AccountBankStatement, self).button_cancel(cr, uid, ids, context=context)
+
+class AccountBankStatementLine(models.Model):
     _inherit = 'account.bank.statement.line'
 
     voucher_id = fields.Many2one('account.voucher', 'Voucher', readonly=True)
+
+    def cancel(self, cr, uid, ids, context=None):
+        # if we are canceling the statement then we dont raise the warning
+        if context.get('cancel_from_statement', False):
+            none_voucher_ids = self.search(cr, uid, [('id', 'in', ids), ('voucher_id', '=', False)])
+            return super(AccountBankStatementLine, self).cancel(cr, uid, none_voucher_ids, context)
+
+        for line in self.browse(cr, uid, ids, context):
+            if line.voucher_id:
+                raise Warning(_("You can not cancel a line that has been imported from a Voucher, you should cancel the voucher first"))
+            return super(AccountBankStatementLine, self).cancel(cr, uid, ids, context)
+
+        account_move_obj = self.pool.get('account.move')
+        move_ids = []
+        for line in self.browse(cr, uid, ids, context=context):
+            if line.journal_entry_id:
+                move_ids.append(line.journal_entry_id.id)
+                for aml in line.journal_entry_id.line_id:
+                    if aml.reconcile_id:
+                        move_lines = [l.id for l in aml.reconcile_id.line_id]
+                        move_lines.remove(aml.id)
+                        self.pool.get('account.move.reconcile').unlink(cr, uid, [aml.reconcile_id.id], context=context)
+                        if len(move_lines) >= 2:
+                            self.pool.get('account.move.line').reconcile_partial(cr, uid, move_lines, 'auto', context=context)
+        if move_ids:
+            account_move_obj.button_cancel(cr, uid, move_ids, context=context)
+            account_move_obj.unlink(cr, uid, move_ids, context)
+
+    def unlink(self, cr, uid, ids, context=None):
+        line_voucher_ids = self.search(cr, uid, [('id', 'in', ids), ('voucher_id', '!=', False)])
+        # First remove journal_entry_id id in order to avoid constraint
+        self.write(cr, uid, line_voucher_ids, {'journal_entry_id': False})
+        return super(AccountBankStatementLine, self).unlink(cr, uid, ids, context=context)
+
+class AccountVoucher(models.Model):
+    _inherit = 'account.voucher'
+
+    bank_statement_line_ids = fields.One2many('account.bank.statement.line', 'voucher_id', string="Statement Lines")
+
+    @api.multi
+    def cancel_voucher(self):
+        # run with sudo because some users may not have access to statement line
+        if self.sudo().bank_statement_line_ids.statement_id.state == 'confirm':
+            raise Warning(_("You can not cancel a payment that is linked to a cash register."))
+        else:
+            super(AccountVoucher, self).cancel_voucher()
+            return self.sudo().bank_statement_line_ids.unlink()

BIN
model/account_bank_voucher_import.pyc


+ 20 - 35
static/src/js/account_bank_voucher_import.js

@@ -14,11 +14,11 @@ openerp.account_bank_voucher_import = function (instance, local) {
             this._super(parent);
         },
         // Actualizar id del Objeto
-        updateId: function(id){
+        updateId: function(id) {
             var self = this;
             self.id = id;
         },
-        start: function () {
+        start: function() {
             var self = this;
             this.$el.click(function() {
                 self.fetchInitial();
@@ -28,35 +28,35 @@ openerp.account_bank_voucher_import = function (instance, local) {
         fetchInitial: function() {
             var self = this;
 
+            self.$el.attr("disabled", true);
+
             self.fetchBankStatement().then(function(accountStatement) {
                 return accountStatement;
-            }).then(function(accountStatement){
+            }).then(function(accountStatement) {
                 self.accountStatement = accountStatement;
-                return self.fetchBankStatementLine();
-            }).then(function(statementLine){
-                self.statementLine = statementLine;
                 return self.fetchVoucher();
-            }).then(function(accountVouche){
+            }).then(function(accountVouche) {
                 self.accountVouche = accountVouche;
                 return self.fetchJoinVoucher();
-            }).then(function(voucherImport){
+            }).then(function(voucherImport) {
                 self.voucherImport = voucherImport;
                 return self.insertBankStatementLine();
-            }).then(function(bankLine){
+            }).then(function(bankLine) {
                 local.parentInstance.reload();
-                if (bankLine){
+                if (bankLine) {
                     instance.web.notification.do_notify("Felicitaciones","Has importado los pagos con éxito.");
-                }else{
+                }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', 'in', self.id]];
+            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) {
@@ -65,32 +65,16 @@ openerp.account_bank_voucher_import = function (instance, local) {
 
             return defer;
         },
-        // Linea del Registro de Caja
-        fetchBankStatementLine: function() {
-            var defer = $.Deferred();
-            var fields=['id', 'voucher_id'];
-            var domain=[['voucher_id', '!=', false]];
-
-            var statementLine = new instance.web.Model('account.bank.statement.line');
-            statementLine.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 voucher =_.flatten(_.map(self.statementLine,function(map) {
-                return map.voucher_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], ["id", 'not in', voucher]];
+            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) {
@@ -110,7 +94,7 @@ openerp.account_bank_voucher_import = function (instance, local) {
                 itemvoucher = self.accountVouche[i];
                 if (itemvoucher.type === 'payment'){
                     ammount = (itemvoucher.amount * -1 );
-                }else {
+                } else {
                     ammount = (itemvoucher.amount);
                 }
 
@@ -125,6 +109,7 @@ openerp.account_bank_voucher_import = function (instance, local) {
                     journal_entry_id: itemvoucher.move_id[0]
                 });
             }
+
             defer.resolve(voucherImport);
 
             return defer;
@@ -152,11 +137,11 @@ openerp.account_bank_voucher_import = function (instance, local) {
         /* ---------------------------------------------------------------------
         * Description: Async loop util v2 written by Robert Gauto.
         * --------------------------------------------------------------------*/
-        asyncLoopFactory : function (iterations, func, callback) {
+        asyncLoopFactory: function (iterations, func, callback) {
             var index = 0;
             var done = false;
             var loop = {
-                next: function () {
+                next: function() {
                     if (done) {
                         return;
                     }