~herrera-dev/addons-vauxoo/review_changes_user_story

« back to all changes in this revision

Viewing changes to account_voucher_tax/account_voucher.py

  • Committer: Nhomar Hernandez
  • Date: 2013-03-28 01:46:42 UTC
  • mfrom: (525.1.14 vaddons)
  • Revision ID: nhomar@gmail.com-20130328014642-yxbvi7l8wrji8xl2
[MERGE] Various merges see commit history.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
##############################################################################
26
26
 
27
27
from osv import osv, fields
 
28
from tools.translate import _
 
29
import release
 
30
 
28
31
 
29
32
class account_voucher(osv.osv):
30
33
    _inherit = 'account.voucher'
31
34
    
32
 
    def voucher_move_line_create(self, cr, uid, voucher_id, line_total, move_id, company_currency, current_currency, context=None):
 
35
    _columns={
 
36
        'move_id2':fields.many2one('account.move', 'Account Entry Tax'),
 
37
        'move_ids2': fields.related('move_id2','line_id', type='one2many', relation='account.move.line', string='Journal Items Tax', readonly=True),
 
38
 
 
39
        }
 
40
    
 
41
    def get_rate(self, cr, uid, move_id, context=None):
 
42
        move_obj = self.pool.get('account.move')
 
43
        if not context:
 
44
            context = {}
 
45
        for move in move_obj.browse(cr, uid, [move_id], context):
 
46
            for line in move.line_id:
 
47
                amount_base = line.debit or line.credit or 0
 
48
                rate = 1
 
49
                if amount_base and line.amount_currency:
 
50
                    rate=amount_base/line.amount_currency
 
51
                    return rate
 
52
        return rate
 
53
    
 
54
    def get_percent_pay_vs_invoice(self, cr, uid, amount_original,amount, context=None):
 
55
        return amount_original and amount/amount_original or 1.0
 
56
    
 
57
    def get_partial_amount_tax_pay(self, cr, uid, tax_amount,tax_base, context=None):
 
58
        return tax_amount*tax_base
 
59
    
 
60
    def voucher_move_line_tax_create(self, cr, uid, voucher_id, move_id, context=None):
33
61
        move_obj = self.pool.get('account.move')
34
62
        move_line_obj = self.pool.get('account.move.line')
35
63
        invoice_obj = self.pool.get('account.invoice')
36
64
        currency_obj = self.pool.get('res.currency')
37
 
        res=super(account_voucher, self).voucher_move_line_create(cr, uid, voucher_id, line_total, move_id, company_currency, current_currency, context=None)
 
65
        company_currency = self._get_company_currency(cr, uid, voucher_id, context)
 
66
        current_currency = self._get_current_currency(cr, uid, voucher_id, context)
 
67
        new_move=move_obj.create(cr, uid, self.account_move_get(cr, uid, voucher_id, context=context), context=context)
38
68
        for voucher in self.browse(cr,uid,[voucher_id],context=context):
39
69
            lines=[]
40
70
            for line in voucher.line_ids:
 
71
                factor=self.get_percent_pay_vs_invoice(cr,uid,line.amount_original, line.amount,context=context)
41
72
                if line.amount>0:
42
73
                    invoice_ids=invoice_obj.search(cr,uid,[('move_id','=',line.move_line_id.move_id.id)],context=context)
43
74
                    for invoice in invoice_obj.browse(cr,uid,invoice_ids,context=context):
44
75
                        for tax in invoice.tax_line:
45
76
                            if tax.tax_id.tax_voucher_ok:
 
77
                                base_amount=self.get_partial_amount_tax_pay(cr, uid, tax.tax_id.amount, tax.base, context=context)
46
78
                                move_ids=[]
47
79
                                account=tax.tax_id.account_collected_voucher_id.id
48
 
                                credit_amount=currency_obj.compute(cr, uid, line.move_line_id.currency_id.id,company_currency, ((tax.tax_id.amount*tax.base)*(line.amount/line.amount_original)), context=context)
 
80
                                credit_amount= float('%.*f' % (2,(base_amount*factor)))
 
81
                                if credit_amount:
 
82
                                    if abs(float('%.*f' % (2,credit_amount))-base_amount)<=.02:
 
83
                                        credit_amount=credit_amount-abs(float('%.*f' % (2,credit_amount))-base_amount)
 
84
                                    if abs(float('%.*f' % (2,credit_amount))+ (base_amount*(1-factor))-base_amount)<.02:
 
85
                                        credit_amount=credit_amount-abs(float('%.*f' % (2,credit_amount))+ (base_amount*(1-factor))-base_amount)
 
86
                                #context['date']=invoice.date_invoice
 
87
                                if company_currency==current_currency:
 
88
                                    rate_move=self.get_rate(cr,uid,line.move_line_id.move_id.id,context=context)
 
89
                                    credit_amount=credit_amount*rate_move
 
90
                                else:
 
91
                                    credit_amount=currency_obj.compute(cr, uid, line.move_line_id.currency_id.id,company_currency, float('%.*f' % (2,credit_amount)), round=False, context=context)
49
92
                                debit_amount=0.0
50
93
                                if tax.tax_id.amount<0:
51
94
                                    credit_amount=0.0
52
 
                                    debit_amount=(-1.0*currency_obj.compute(cr, uid, line.move_line_id.currency_id.id,company_currency, ((tax.tax_id.amount*tax.base)*(line.amount/line.amount_original)), context=context))
53
 
                                if invoice.type=='out_invoice':## considerar que hacer con refund
 
95
                                    debit_amount=float('%.*f' % (2,(base_amount*factor)))
 
96
                                    if debit_amount: 
 
97
                                        if abs(float('%.*f' % (2,debit_amount))-base_amount)<=.02:
 
98
                                            debit_amount=debit_amount-abs(float('%.*f' % (2,debit_amount))-base_amount)
 
99
                                        if abs(float('%.*f' % (2,debit_amount))+ (base_amount*(1-factor))-base_amount)<.02:
 
100
                                            debit_amount=debit_amount-abs(float('%.*f' % (2,debit_amount))+ (base_amount*(1-factor))-base_amount)
 
101
                                        debit_amount=(-1.0*currency_obj.compute(cr, uid, line.move_line_id.currency_id.id,company_currency, float('%.*f' % (2,debit_amount)), round=False, context=context))
 
102
                                if invoice.type=='out_invoice':## TODO refund
54
103
                                    account=tax.tax_id.account_paid_voucher_id.id
55
104
                                    credit_amount, debit_amount=debit_amount, credit_amount
56
105
                                move_line={
58
107
                                    'period_id': voucher.period_id.id,
59
108
                                    'name': tax.name or '/',
60
109
                                    'account_id': tax.account_id.id,
61
 
                                    'move_id': int(move_id),
 
110
                                    'move_id': int(new_move),
62
111
                                    'partner_id': voucher.partner_id.id,
63
112
                                    'company_id':company_currency,
64
113
                                    'currency_id': line.move_line_id and (company_currency <> current_currency and current_currency) or False,
70
119
                                    
71
120
                                    }
72
121
                                if company_currency!=current_currency:
73
 
                                    move_line['amount_currency']=currency_obj.compute(cr, uid, company_currency, current_currency,credit_amount, context=context)
 
122
                                    move_line['amount_currency']=currency_obj.compute(cr, uid, company_currency, current_currency,(credit_amount or debit_amount), round=False, context=context)
74
123
                                move_ids.append(move_line_obj.create(cr,uid,move_line,context=context))
75
124
                                move_line={
76
125
                                    'journal_id': voucher.journal_id.id,
77
126
                                    'period_id': voucher.period_id.id,
78
127
                                    'name': tax.name or '/',
79
128
                                    'account_id': account,
80
 
                                    'move_id': int(move_id),
 
129
                                    'move_id': int(new_move),
81
130
                                    'partner_id': voucher.partner_id.id,
82
131
                                    'company_id':company_currency,
83
 
                                    #'currency_id': line.move_line_id and (company_currency <> line.move_line_id.currency_id.id and line.move_line_id.currency_id.id) or False,
84
132
                                    'currency_id': line.move_line_id and (company_currency <> current_currency and current_currency) or False,
85
133
                                    'quantity': 1,
86
134
                                    'credit': debit_amount,
89
137
                                    'date': voucher.date,
90
138
                                    }
91
139
                                if company_currency!=current_currency:
92
 
                                    move_line['amount_currency']=currency_obj.compute(cr, uid, company_currency, current_currency,credit_amount, context=context)
 
140
                                    move_line['amount_currency']=currency_obj.compute(cr, uid, company_currency, current_currency,(debit_amount or credit_amount), round=False, context=context)
93
141
                                move_line_obj.create(cr,uid,move_line,context=context)
 
142
                                account_income_id = voucher.company_id.income_currency_exchange_account_id.id
 
143
                                account_expense_id = voucher.company_id.expense_currency_exchange_account_id.id
 
144
                                for m in move_obj.browse(cr,uid,[move_id],context=context):
 
145
                                    for mlines in m.line_id:
 
146
                                        dif=0
 
147
                                        if mlines.account_id.id==account_income_id:
 
148
                                            account=account_expense_id
 
149
                                            if invoice.type=='out_invoice':
 
150
                                                credit=(debit_amount-tax.tax_amount)
 
151
                                                debit=0.0
 
152
                                                dif=1
 
153
                                            else:
 
154
                                                credit=0.0
 
155
                                                debit=(credit_amount-tax.tax_amount)
 
156
                                                dif=1
 
157
                                        if mlines.account_id.id==account_expense_id:
 
158
                                            account=account_income_id
 
159
                                            if invoice.type=='out_invoice':
 
160
                                                credit=0.0
 
161
                                                debit=(debit_amount-tax.tax_amount)
 
162
                                                dif=1
 
163
                                            else:
 
164
                                                credit=(credit_amount-tax.tax_amount)
 
165
                                                debit=0.0
 
166
                                                dif=1
 
167
                                        if dif:
 
168
                                            if invoice.type=='out_invoice':## TODO refund
 
169
                                                credit, debit=debit, credit
 
170
                                            move_line = {
 
171
                                                'journal_id': voucher.journal_id.id,
 
172
                                                'period_id': voucher.period_id.id,
 
173
                                                'name': _('change')+': '+(line.name or '/'),
 
174
                                                'account_id': account,
 
175
                                                'move_id': int(new_move),
 
176
                                                'partner_id': voucher.partner_id.id,
 
177
                                                'currency_id': line.move_line_id and (company_currency <> current_currency and current_currency) or False,
 
178
                                                'amount_currency': 0.0,
 
179
                                                'quantity': 1,
 
180
                                                'credit': credit,
 
181
                                                'debit': debit,
 
182
                                                'date': line.voucher_id.date,
 
183
                                            }
 
184
                                            if company_currency!=current_currency:
 
185
                                                move_line['amount_currency']=currency_obj.compute(cr, uid, company_currency, current_currency,debit, round=False, context=context)
 
186
                                            move_line_obj.create(cr,uid,move_line,context=context)
 
187
                                            move_line_counterpart = {
 
188
                                                'journal_id': voucher.journal_id.id,
 
189
                                                'period_id': voucher.period_id.id,
 
190
                                                'name': _('change')+': '+(line.name or '/'),
 
191
                                                'account_id': tax.account_id.id,
 
192
                                                'move_id': int(new_move),
 
193
                                                'amount_currency': 0.0,
 
194
                                                'partner_id': voucher.partner_id.id,
 
195
                                                'currency_id': line.move_line_id and (company_currency <> current_currency and current_currency) or False,
 
196
                                                'quantity': 1,
 
197
                                                'credit': debit,
 
198
                                                'debit': credit,
 
199
                                                'date': line.voucher_id.date,
 
200
                                            }
 
201
                                            if company_currency!=current_currency:
 
202
                                                move_line['amount_currency']=currency_obj.compute(cr, uid, company_currency, current_currency,debit, round=False, context=context)
 
203
                                            move_ids.append(move_line_obj.create(cr,uid,move_line_counterpart,context=context))
94
204
                                for mov_line in invoice.move_id.line_id:
95
205
                                    if mov_line.account_id.id==tax.account_id.id:
96
206
                                        move_ids.append(mov_line.id)
98
208
                                    self.pool.get('account.move.line').reconcile(cr, uid, move_ids, 'manual', writeoff_acc_id=tax.account_id.id, writeoff_period_id=voucher.period_id.id, writeoff_journal_id=voucher.journal_id.id)
99
209
                                else:
100
210
                                    self.pool.get('account.move.line').reconcile_partial(cr, uid, move_ids, 'manual', context)
 
211
            self.write(cr,uid,voucher_id,{'move_id2':new_move},context=context)
 
212
        return new_move
 
213
    
 
214
    def voucher_move_line_create(self, cr, uid, voucher_id, line_total, move_id, company_currency, current_currency, context=None):
 
215
        move_obj = self.pool.get('account.move')
 
216
        move_line_obj = self.pool.get('account.move.line')
 
217
        invoice_obj = self.pool.get('account.invoice')
 
218
        currency_obj = self.pool.get('res.currency')
 
219
        res=super(account_voucher, self).voucher_move_line_create(cr, uid, voucher_id, line_total, move_id, company_currency, current_currency, context=None)
 
220
        new=self.voucher_move_line_tax_create(cr,uid, voucher_id, move_id, context=context)
101
221
        return res
102
 
account_voucher()
 
 
b'\\ No newline at end of file'
 
222
account_voucher()