25
25
##############################################################################
27
27
from osv import osv, fields
28
from tools.translate import _
29
32
class account_voucher(osv.osv):
30
33
_inherit = 'account.voucher'
32
def voucher_move_line_create(self, cr, uid, voucher_id, line_total, move_id, company_currency, current_currency, context=None):
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),
41
def get_rate(self, cr, uid, move_id, context=None):
42
move_obj = self.pool.get('account.move')
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
49
if amount_base and line.amount_currency:
50
rate=amount_base/line.amount_currency
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
57
def get_partial_amount_tax_pay(self, cr, uid, tax_amount,tax_base, context=None):
58
return tax_amount*tax_base
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):
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)
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)
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)))
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
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)
50
93
if tax.tax_id.amount<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)))
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
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))
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,
86
134
'credit': debit_amount,
89
137
'date': voucher.date,
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:
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)
155
debit=(credit_amount-tax.tax_amount)
157
if mlines.account_id.id==account_expense_id:
158
account=account_income_id
159
if invoice.type=='out_invoice':
161
debit=(debit_amount-tax.tax_amount)
164
credit=(credit_amount-tax.tax_amount)
168
if invoice.type=='out_invoice':## TODO refund
169
credit, debit=debit, credit
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,
182
'date': line.voucher_id.date,
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,
199
'date': line.voucher_id.date,
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)
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)
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)
b'\\ No newline at end of file'