~vauxoo/openerp-venezuela-localization/ovl70-whiva-rev2-kty

« back to all changes in this revision

Viewing changes to l10n_ve_withholding_islr/model/islr_wh_doc.py

  • Committer: Humberto Arocha
  • Date: 2013-01-15 00:39:13 UTC
  • mfrom: (765.2.71 ovl70-whislr-rev-yani)
  • Revision ID: humbertoarocha@gmail.com-20130115003913-v7uic3vlaixjn78j
 
[MERGE] Third Revision to WH ISLR

Show diffs side-by-side

added added

removed removed

Lines of Context:
96
96
            ('out_refund','Customer Invoice Refund'),
97
97
            ],'Type', readonly=True, help="Voucher type"),
98
98
        'state': fields.selection([
99
 
            ('to_process','To Process'),
100
 
            ('progress','Progress'),
101
99
            ('draft','Draft'),
102
100
            ('confirmed', 'Confirmed'),
103
101
            ('done','Done'),
117
115
        'invoice_id':fields.many2one('account.invoice','Invoice',readonly=False,help="Invoice to make the accounting entry"),
118
116
        'islr_wh_doc_id': fields.one2many('account.invoice','islr_wh_doc_id','Invoices',states={'draft':[('readonly',False)]}),
119
117
        'user_id': fields.many2one('res.users', 'Salesman', readonly=True, states={'draft':[('readonly',False)]}),
 
118
        'automatic_income_wh' : fields.boolean('Automatic Income Withhold',
 
119
            help='When the whole process will be check automatically, ' \
 
120
                    'and if everything is Ok, will be set to done'),
120
121
    }
121
122
 
122
123
    _defaults = {
129
130
                self.pool.get('res.users').browse(cr, uid, uid,
130
131
                    context=context).company_id.id,
131
132
        'user_id': lambda s, cr, u, c: u,
 
133
        'automatic_income_wh' : False,
132
134
    }
133
135
 
134
136
    def check_income_wh(self, cr, uid, ids, context=None):
137
139
        obj = self.browse(cr, uid, ids[0],context=context)
138
140
        res = {}
139
141
        for wh_line in obj.invoice_ids:
140
 
            if not wh_line.islr_xml_id:
 
142
            if not (wh_line.islr_xml_id or wh_line.iwdl_ids):
141
143
                res[wh_line.id] = (wh_line.invoice_id.name, 
142
144
                    wh_line.invoice_id.number, wh_line.invoice_id.reference)
143
145
        if res:
148
150
            raise osv.except_osv(_('Invoices with Missing Withheld Taxes!'),note)
149
151
        return True
150
152
 
 
153
    def check_auto_wh(self, cr, uid, ids, context=None):
 
154
        context = context or {}
 
155
        ids = isinstance(ids, (int, long)) and [ids] or ids
 
156
        obj = self.browse(cr, uid, ids[0],context=context)
 
157
        return obj.automatic_income_wh or False
 
158
 
151
159
    def compute_amount_wh(self, cr, uid, ids, context=None):
152
160
        context = context or {}      
153
161
        ids = isinstance(ids, (int, long)) and [ids] or ids
156
164
        for iwdi_brw in iwd_brw.invoice_ids:
157
165
            iwdi_obj.load_taxes(cr, uid, iwdi_brw.id, context=context)    
158
166
        return True
 
167
        
159
168
    def validate(self, cr,uid,ids,*args):
160
169
 
161
170
        if args[0]in ['in_invoice','in_refund'] and args[1] and args[2]:
253
262
    def action_cancel(self,cr,uid,ids,context={}):
254
263
        #~ if self.browse(cr,uid,ids)[0].type=='in_invoice':
255
264
            #~ return True
 
265
        self.pool.get('islr.wh.doc'). write(cr,uid,ids,{'automatic_income_wh':False})
 
266
       
256
267
        self.cancel_move(cr,uid,ids)
257
268
        self.action_cancel_process(cr,uid,ids,context=context)
258
269
        return True
444
455
                'amount_islr_ret': 0.0,
445
456
                'base_ret': 0.0
446
457
            }
447
 
            for line in ret_line.islr_xml_id:
448
 
                res[ret_line.id]['amount_islr_ret'] += line.wh
449
 
                res[ret_line.id]['base_ret'] += line.base
 
458
            if ret_line.invoice_id.type in ('in_invoice','in_refund'):
 
459
                for line in ret_line.islr_xml_id:
 
460
                    res[ret_line.id]['amount_islr_ret'] += line.wh
 
461
                    res[ret_line.id]['base_ret'] += line.base
 
462
            else:
 
463
                for line in ret_line.iwdl_ids:
 
464
                    res[ret_line.id]['amount_islr_ret'] += line.amount
 
465
                    res[ret_line.id]['base_ret'] += line.base_amount
450
466
 
451
467
        return res
452
468
 
456
472
        'islr_xml_id':fields.one2many('islr.xml.wh.line','islr_wh_doc_inv_id','Withholding Lines'),
457
473
        'amount_islr_ret':fields.function(_amount_all, method=True, digits=(16,4), string='Withheld Amount', multi='all', help="Amount withheld from the base amount"),
458
474
        'base_ret': fields.function(_amount_all, method=True, digits=(16,4), string='Base Amount', multi='all', help="Amount where a withholding is going to be compute from"),
 
475
        'iwdl_ids':fields.one2many('islr.wh.doc.line','iwdi_id','Withholding Concepts'),
459
476
    }
460
477
    _rec_rame = 'invoice_id'
461
478
    
475
492
 
476
493
    def _get_wh(self, cr, uid, ids, concept_id, context=None):
477
494
        '''
478
 
        Returns a dictionary containing all the values ​​of the retention of an invoice line.
 
495
        Returns a dictionary containing all the values of the retention of an invoice line.
479
496
        '''
 
497
        #TODO: Change the signature of this method
 
498
        # This record already has the concept_id built-in
480
499
        context= context or {}
481
500
        ids = isinstance(ids, (int, long)) and [ids] or ids
482
501
        ixwl_obj= self.pool.get('islr.xml.wh.line')
488
507
        residence = self._get_residence(cr, uid, vendor, buyer)
489
508
        nature = self._get_nature(cr, uid, vendor)
490
509
 
 
510
        concept_id =iwdl_brw.concept_id.id 
491
511
        #rate_base,rate_minimum,rate_wh_perc,rate_subtract,rate_code,rate_id,rate_name 
492
512
        rate_tuple = self._get_rate(cr, uid, concept_id, residence, nature, context=context)
493
513
        base = 0
494
 
        for line in iwdl_brw.xml_ids:
495
 
            base += line.account_invoice_line_id.price_subtotal
496
 
        apply = apply and base >= rate_tuple[0]*rate_tuple[1]/100.0
497
 
        wh = 0.0
498
 
        subtract = apply and rate_tuple[3] or 0.0
499
 
        subtract_write=0.0
500
 
        wh_concept = 0.0
501
 
        sb_concept = subtract 
502
 
        for line in iwdl_brw.xml_ids:
503
 
            if apply: 
504
 
                wh_calc = (rate_tuple[0]/100.0)*rate_tuple[2]*line.account_invoice_line_id.price_subtotal/100.0
505
 
                if subtract >= wh_calc:
506
 
                    wh = 0.0
507
 
                    subtract -= wh_calc
508
 
                else:
509
 
                    wh = wh_calc - subtract
510
 
                    subtract_write= subtract
511
 
                    subtract=0.0
512
 
            ixwl_obj.write(cr,uid,line.id,{'wh':wh, 'sustract':subtract or subtract_write},
513
 
                    context=context)
514
 
            wh_concept+=wh
515
 
        iwdl_obj.write(cr, uid, ids[0],{'amount':wh_concept,
516
 
            'subtract':sb_concept, 'base_amount': base},context=context)
 
514
 
 
515
        if iwdl_brw.invoice_id.type in ('in_invoice','in_refund'):
 
516
            for line in iwdl_brw.xml_ids:
 
517
                base += line.account_invoice_line_id.price_subtotal
 
518
            apply = apply and base >= rate_tuple[0]*rate_tuple[1]/100.0
 
519
            wh = 0.0
 
520
            subtract = apply and rate_tuple[3] or 0.0
 
521
            subtract_write=0.0
 
522
            wh_concept = 0.0
 
523
            sb_concept = subtract 
 
524
            for line in iwdl_brw.xml_ids:
 
525
                if apply: 
 
526
                    wh_calc = (rate_tuple[0]/100.0)*rate_tuple[2]*line.account_invoice_line_id.price_subtotal/100.0
 
527
                    if subtract >= wh_calc:
 
528
                        wh = 0.0
 
529
                        subtract -= wh_calc
 
530
                    else:
 
531
                        wh = wh_calc - subtract
 
532
                        subtract_write= subtract
 
533
                        subtract=0.0
 
534
                ixwl_obj.write(cr,uid,line.id,{'wh':wh, 'sustract':subtract or subtract_write},
 
535
                        context=context)
 
536
                wh_concept+=wh
 
537
        else:
 
538
            for line in iwdl_brw.invoice_id.invoice_line:
 
539
                if line.concept_id.id == concept_id:
 
540
                    base += line.price_subtotal
 
541
 
 
542
            apply = apply and base >= rate_tuple[0]*rate_tuple[1]/100.0
 
543
            sb_concept = apply and rate_tuple[3] or 0.0
 
544
            if apply:
 
545
                wh_concept = (rate_tuple[0]/100.0)*rate_tuple[2]*base/100.0
 
546
                wh_concept -= sb_concept
 
547
        values = {
 
548
            'amount':wh_concept,
 
549
            'subtract':sb_concept, 
 
550
            'base_amount': base,
 
551
            'retencion_islr':rate_tuple[2], 
 
552
            }
 
553
        iwdl_obj.write(cr, uid, ids[0],values,context=context)
517
554
        return True 
518
555
 
519
556
 
527
564
        rates = {}
528
565
        wh_perc= {}
529
566
        xmls = {}
530
 
        if ret_line.invoice_id:
 
567
 
 
568
        if not ret_line.invoice_id:
 
569
            return True
 
570
 
 
571
        if ret_line.invoice_id.type in ('in_invoice','in_refund'):
531
572
            #~ Searching & Unlinking for xml lines from the current invoice
532
573
            xml_lines = ixwl_obj.search(cr, uid, [('islr_wh_doc_inv_id', '=', ret_line.id)],context=context)
533
574
            if xml_lines:
568
609
                        'xml_ids': [(6,0,xmls[concept_id])],
569
610
                        }, context=context)
570
611
                self._get_wh(cr, uid, iwdl_id, concept_id, context=context)
 
612
        else:
 
613
            #~ Searching & Unlinking for concept lines from the current invoice
 
614
            iwdl_ids = iwdl_obj.search(cr, uid, [('iwdi_id', '=',ret_line.id)],
 
615
                    context=context)
 
616
            if iwdl_ids:
 
617
                iwdl_obj.unlink(cr, uid, iwdl_ids)
 
618
                iwdl_ids=[]
 
619
 
 
620
            concept_list = self._get_concepts(cr, uid, ret_line.invoice_id.id,
 
621
                    context=context)
 
622
            for concept_id in concept_list:
 
623
                iwdl_id=iwdl_obj.create(cr,uid,
 
624
                        {'islr_wh_doc_id':ret_line.islr_wh_doc_id.id,
 
625
                        'concept_id':concept_id,
 
626
                        'invoice_id': ret_line.invoice_id.id,
 
627
                        }, context=context)
 
628
                iwdl_ids+=[iwdl_id]
 
629
                self._get_wh(cr, uid, iwdl_id, concept_id, context=context)
 
630
            self.write(cr,uid,ids[0],{'iwdl_ids':[(6,0,iwdl_ids)]})
571
631
        return True
572
 
        
 
632
            
573
633
    def _get_partners(self, cr, uid, invoice):
574
634
        '''
575
635
        Se obtiene: el id del vendedor, el id del comprador de la factura y el campo booleano que determina si el comprador es agente de retencion.
696
756
        'move_id': fields.many2one('account.move', 'Journal Entry', readonly=True, help="Accounting voucher"),
697
757
        'islr_rates_id': fields.many2one('islr.rates','Rates', help="Withhold rates"),
698
758
        'xml_ids':fields.one2many('islr.xml.wh.line','islr_wh_doc_line_id','XML Lines'),        
 
759
        'iwdi_id': fields.many2one('islr.wh.doc.invoices','Withheld Invoice',help="Withheld Invoices"),
699
760
    }
700
761
 
701
762
islr_wh_doc_line()