Kaynağa Gözat

[FX] Change State account.interest && account.interest.line

adrielso 6 yıl önce
ebeveyn
işleme
00d910a528

+ 70 - 12
models/account_interest.py

@@ -4,13 +4,16 @@ import openerp.addons.decimal_precision as dp
 from datetime import datetime
 from openerp.exceptions import ValidationError
 
+import logging
+_logger = logging.getLogger(__name__)
+
 class AccountInterest(models.Model):
     _name = 'account.interest'
 
     name = fields.Char()
     date = fields.Date('Date', help="Fecha de operación")
-    state = fields.Selection([('cancel', 'Cancelado'),('open', 'Abierto'),('paid', 'Pagado')],'Estado del pago', default="open", help="Estado")
     comment = fields.Text('Comment', help="Información adicional")
+    state = fields.Selection([('lines_paid', 'Sin interés'),('lines_open', 'Interés a cobrar.'),('paid', 'Factura pagada')],'Estado del pago', default="lines_open", help="Estado")
     ## Cliente
     customer_id = fields.Many2one('res.partner', string="customer")
     ## factura
@@ -63,7 +66,6 @@ class accountInvoiceInterest(models.Model):
     is_interest = fields.Boolean('Factura de interés', default=False, help="Indica si esta factura fue generado por un interés.")
     interest_line_ids = fields.One2many('account.interest.line', 'invoice', string=' Account Interest Line')
 
-
     '''  Unlink Invoice '''
     @api.multi
     def unlink(self):
@@ -84,31 +86,88 @@ class accountInvoiceInterest(models.Model):
     @api.multi
     def write(self, vals):
         invoice = super(accountInvoiceInterest, self).write(vals)
+        ''' Cambiar estado de lineas de interés'''
+        interestLinesIDS = self._change_state_account_interest_line(self)
+        ''' Cambiar estado del interés '''
+        interest= self._change_state_account_interest(self)
+
+        return invoice
+
+    ''' Cambiar estado de lineas de interés '''
+    def _change_state_account_interest_line(self, invoice):
+        _logger.info('Change state account.interest.line.')
+        linesIds = []
+        if (invoice.interest_line_ids):
+            linesIds = map(lambda x: x.id, invoice.interest_line_ids)
+
+        if (invoice.interest_ids):
+            linesIds = map(lambda x: x.id, invoice.interest_ids.lines_ids)
 
-        linesIds = map(lambda x: x.id, self.interest_line_ids)
         if (linesIds):
             interestLine = self.env['account.interest.line'].search([('id', 'in', linesIds)])
+            state = []
 
-            if (interestLine):
-                state = []
-                if (self.state == 'open'):
+            for line in interestLine:
+                lineInvoice = self.env['account.invoice'].browse(line.invoice.id)
+                if (not lineInvoice):
+                    state = {'state': 'open'}
+
+                if (lineInvoice.state == 'open'):
                     state = {'state': 'invoiced'}
 
-                if (self.state == 'cancel'):
+                if (lineInvoice.state == 'cancel'):
                     state = {'state': 'invoice_cancel'}
 
-                if (self.state == 'draft'):
+                if (lineInvoice.state == 'draft'):
                     state = {'state': 'invoice_draft'}
 
-                if (self.state == 'paid'):
+                if (lineInvoice.state == 'paid'):
                     state = {'state': 'paid'}
 
                 if (state):
-                    for line in interestLine:
-                        line.write(state)
+                    line.write(state)
+        return True
+
+    ''' Cambiar el Estado del account.interest '''
+    def _change_state_account_interest(self, invoice):
+        _logger.info('Change state account.interest')
+        interestIds = []
+        state = []
+
+        if (invoice.interest_line_ids):
+            interestIds = map(lambda x: x.interest_id.id, invoice.interest_line_ids)
+
+        if (invoice.interest_ids):
+            interestIds = map(lambda x: x.id, invoice.interest_ids)
+
+        if (interestIds):
+            accountInterest = self.env['account.interest'].search([('id', 'in', interestIds)])
+            if (accountInterest.lines_ids):
+                linesState = map(lambda x: x.state, accountInterest.lines_ids)
+
+                if (('discount'in linesState) or ('paid' in linesState)):
+                    state ={ 'state': 'lines_paid'}
+
+                    if ((accountInterest.invoice_id.id == invoice.id) and (invoice.state == 'paid')):
+                        state ={ 'state': 'paid'}
+
+                if (('invoiced' in  linesState) or ('invoice_cancel' in linesState) or ('invoice_draft' in linesState) or ('open' in linesState)):
+                    state ={'state': 'lines_open'}
+
+            if (state):
+                accountInterest.write(state)
 
         return True
 
+    ''' Verify Status Invoice and interest '''
+    @api.model
+    def eiru_invoice_verify_status(self):
+        invoices = self.env['account.invoice'].search([('type', '=', 'out_invoice')])
+        for invoice in invoices:
+            interestLine = self._change_state_account_interest_line(invoice)
+            interest = self._change_state_account_interest(invoice)
+
+        return True
 
 ''' Partner '''
 class ResPartnerInterest(models.Model):
@@ -137,7 +196,6 @@ class AccountInterestLine(models.Model):
                                 ('paid', 'Pagado')],'Estado del pago de la linea ', default="open")
     amount_dicount = fields.Float('amount disconut', digits_compute=dp.get_precision('Account'), help="Monto del descuento")
 
-
 '''
     Move Line
 '''

+ 4 - 0
static/src/css/style.css

@@ -1,6 +1,10 @@
 .button-interes-invoice {
   height: 35px;
 }
+.button-invoice-state{
+    width: auto;
+    float: left;
+}
 .interes-payments-discount {
     text-align: right;
     font-size: 11pt;

+ 3 - 1
static/src/js/account_interest_invoice.js

@@ -47,7 +47,9 @@
                 return self.fetchAccountInterestLine(self.id)
             }).then(function(accountInterestline) {
                 self.accountInterestline = accountInterestline
-                return self.showModalPayments();
+                if (!!accountInterestline.length){
+                    return self.showModalPayments();
+                }
             });
         },
         /* Get account.interest */

+ 70 - 0
static/src/js/verify_interest_invoice_status.js

@@ -0,0 +1,70 @@
+(function() {
+
+    openerp.widgetInstanceInterestInvoiceVerifyState = null;
+    openerp.parentInstanceInterestInvoiceVerifyState = {};
+    var QWeb = openerp.web.qweb;
+    var instanceWeb = openerp.web;
+    openerp.VerifyInterestInvoiceVerifyState = openerp.Widget.extend({
+        template: 'verifyInterestInvoice.Status',
+        buttons: undefined,
+        accountInvoice: [],
+        insteresConfig: [],
+        /* init */
+        init: function(parent) {
+            this._super(parent);
+            this.buttons = parent.$buttons;
+        },
+        start: function () {
+            var self = this;
+            this.$el.click(function(){
+                self.fetchInitial();
+            });
+        },
+        /* Reload Page*/
+        reloadPage: function() {
+            openerp.parentInstanceInterestInvoiceVerifyState.reload();
+        },
+        // /* Método inicial  */
+        fetchInitial: function() {
+            var self = this;
+            self.verifyInvoiceState().then(function(accountInvoice){
+                return accountInvoice;
+            }).then(function(accountInvoice) {
+                self.accountInvoice = accountInvoice;
+                instanceWeb.notification.do_warn("Atencion","Verificación Terminada.");
+                return self.reloadPage();
+            });
+        },
+        /* Verificar Estado de las factura */
+        verifyInvoiceState: function() {
+            var invoice = new instanceWeb.Model('account.invoice');
+            return invoice.call('eiru_invoice_verify_status',{
+                context: new instanceWeb.CompoundContext()
+            });
+        }
+    });
+
+    if (openerp.web && openerp.web.FormView) {
+        openerp.web.FormView.include({
+            load_record: function(record) {
+                this._super.apply(this, arguments);
+
+                if (this.model !== 'account.interest.config')
+                    return;
+
+                openerp.parentInstanceInterestInvoiceVerifyState = this;
+                if (openerp.widgetInstanceInterestInvoiceVerifyState) {
+                    if (this.$el.find('.button-invoice-state').length !== 0)
+                        return;
+                }
+                if (this.$el.find('.button-invoice-state').length !== 0)
+                    return;
+
+                openerp.widgetInstanceInterestInvoiceVerifyState = new openerp.VerifyInterestInvoiceVerifyState(this);
+                var element =this.$el.find('.oe_form').find('.verify-interest-invoice-state');
+
+                openerp.widgetInstanceInterestInvoiceVerifyState.appendTo(element[0]);
+            }
+        });
+    }
+})();

+ 0 - 11
static/src/xml/verify_interest.xml

@@ -1,11 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-
-<templates xml:space="preserve">
-    <t t-name="verify.Interest">
-        <div class="paymnet-charge">
-            <button class="verify-interest-partner oe_button oe_form_button oe_highlight">
-                <div>Verificar deuda</div>
-            </button>
-        </div>
-  </t>
-</templates>

+ 8 - 0
static/src/xml/verify_interest_invoice_status.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<templates xml:space="preserve">
+    <t t-name="verifyInterestInvoice.Status">
+        <button class="button-invoice-state oe_button oe_form_button oe_highlight">
+            <div>Verificar Estado de las Facturas.</div>
+        </button>
+  </t>
+</templates>

+ 1 - 1
views/account_interest.xml

@@ -53,7 +53,7 @@
 			<field name="name">eiru.account.interest.tree</field>
 			<field name="model">account.interest</field>
 			<field name="arch" type="xml">
-				<tree create="0" edit="0">
+				<tree colors="blue:state == 'lines_paid'; red:state == 'lines_open';  gray:state == 'paid'" create="0" edit="0">
 					<field name="name" string="Nombre" />
 					<field name="date" string="Fecha"/>
 					<field name="customer_id" string="Cliente"/>

+ 3 - 0
views/account_interest_config.xml

@@ -9,6 +9,9 @@
             <field name="model">account.interest.config</field>
             <field name="arch" type="xml">
                 <form string="Interés por mora" create="0" delete="0">
+                    <header>
+                        <div class="verify-interest-invoice-state"></div>
+                    </header>
                     <sheet>
                         <h1>
                             <field name='name'/>

+ 1 - 0
views/templates.xml

@@ -6,6 +6,7 @@
                 <script type="text/javascript" src="/eiru_account_interest/static/src/js/account_interest_invoice.js"/>
                 <script type="text/javascript" src="/eiru_account_interest/static/src/js/verify_interest_partner.js"/>
                 <script type="text/javascript" src="/eiru_account_interest/static/src/js/verify_interest_invoice.js"/>
+                <script type="text/javascript" src="/eiru_account_interest/static/src/js/verify_interest_invoice_status.js"/>
             </xpath>
         </template>
     </data>