Merge lp:~rr.clearcorp/openerp-costa-rica/6.1-l10n_cr_exchange_rates into lp:openerp-costa-rica/6.1

Proposed by Ronald Rubi
Status: Merged
Merged at revision: 279
Proposed branch: lp:~rr.clearcorp/openerp-costa-rica/6.1-l10n_cr_exchange_rates
Merge into: lp:openerp-costa-rica/6.1
Diff against target: 219 lines (+124/-41)
1 file modified
l10n_cr_exchange_rates/l10n_cr_exchange_rates.py (+124/-41)
To merge this branch: bzr merge lp:~rr.clearcorp/openerp-costa-rica/6.1-l10n_cr_exchange_rates
Reviewer Review Type Date Requested Status
ClearCorp drivers Pending
Review via email: mp+194013@code.launchpad.net

Description of the change

[IMP] Improve adjustment move in l10n_cr_exchange_rates

To post a comment you must log in.
279. By Ronald Rubi

[MRG] Improve adjustment move in l10n_cr_exchange_rates

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'l10n_cr_exchange_rates/l10n_cr_exchange_rates.py'
2--- l10n_cr_exchange_rates/l10n_cr_exchange_rates.py 2013-07-29 22:56:56 +0000
3+++ l10n_cr_exchange_rates/l10n_cr_exchange_rates.py 2013-11-05 21:34:22 +0000
4@@ -85,46 +85,35 @@
5 _name = "account.move"
6 _inherit = "account.move"
7
8- def get_balance_amount(self, cr, uid, id, context):
9+ def get_balance_amount(self, cr, uid, move_id, context):
10 cr.execute( 'SELECT SUM(debit-credit) '\
11 'FROM account_move_line '\
12- 'WHERE move_id = %s ', (id,))
13+ 'WHERE move_id = %s ', (move_id,))
14 result = cr.fetchall()
15 return result[0][0] or 0.00
16
17- def generate_adjustment_move(self, cr, uid, reference, journal, period, context=None):
18+ def get_adjustment_amount(self, cr, uid, move_id, context):
19+ cr.execute( 'SELECT SUM(debit-credit) '\
20+ 'FROM account_move_line '\
21+ 'WHERE adjustment = %s ', (move_id,))
22+ result = cr.fetchall()
23+ return result[0][0] or 0.00
24+
25+ def create_move_lines_reconcile(self, cr, uid, move, exchange_rate_end_period, context=None):
26 move_line_obj = self.pool.get('account.move.line')
27- res_currency_obj = self.pool.get('res.currency')
28- res_currency_rate_obj = self.pool.get('res.currency.rate')
29-
30- res_user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
31- company_currency = res_user.company_id.currency_id
32- name = reference + " " + period.name
33-
34- move_created = {
35- 'ref': name,
36- 'journal_id': journal.id,
37- 'period_id': period.id,
38- 'to_check': False,
39- 'company_id': res_user.company_id.id,
40- }
41- move_created_id = self.create(cr, uid, move_created)
42-
43- total_credit = 0.00
44- total_debit = 0.00
45- exchange_rate_end_period = res_currency_obj._current_rate(cr, uid, [company_currency.id], period.date_stop, arg=None, context=context)[company_currency.id]
46-
47- account_ids = self.pool.get('account.account').search(cr, uid, [('exchange_rate_adjustment', '=', True)], context=context)
48- line_ids = move_line_obj.search(cr, uid, [('currency_id','!=',None), ('period_id','=',period.id), ('amount_currency','!=',0), ('account_id','in',account_ids), ('adjustment','=',None)], context=context)
49- lines = move_line_obj.browse(cr, uid, line_ids, context=context)
50-
51-
52- for line in lines:
53+ account_account_obj = self.pool.get('account.account')
54+ lines_created_ids = []
55+ account_reconcile_ids = account_account_obj.search(cr, uid, [('exchange_rate_adjustment', '=', True), ('reconcile', '=', True)], context=context)
56+ line_reconcile_ids = move_line_obj.search(cr, uid, [('currency_id','!=',None), ('period_id','=',move.period_id.id), ('amount_currency','!=',0), ('account_id','in',account_reconcile_ids), ('adjustment','=',None), ('reconcile_id','=',None)], context=context)
57+ lines_reconcile = move_line_obj.browse(cr, uid, line_reconcile_ids, context=context)
58+
59+ for line in lines_reconcile:
60 if line.move_id.state == 'draft' or not line.amount_currency:
61 continue
62
63 sign_amount_currency = line.amount_currency < 0 and -1 or 1
64 line_difference = 0
65+ adjustment_amount = self.get_adjustment_amount(cr, uid, line.id, context=context)
66 if line.credit != 0:
67 line_difference = sign_amount_currency * line.amount_currency * exchange_rate_end_period - line.credit
68 elif line.debit != 0:
69@@ -135,18 +124,17 @@
70 continue
71 elif line.credit == 0 and exchange_rate_end_period > line.amount_exchange_rate or line.debit == 0 and exchange_rate_end_period < line.amount_exchange_rate:
72 credit = 0.00
73- debit = sign * line_difference
74+ debit = sign * line_difference - adjustment_amount
75 else:
76- credit = sign * line_difference
77+ credit = sign * line_difference + adjustment_amount
78 debit = 0.00
79-
80 move_line = {
81 'name': line.name or '',
82 'ref': line.ref or '',
83 'debit': debit,
84 'credit': credit,
85 'account_id':line.account_id.id,
86- 'move_id': move_created_id,
87+ 'move_id': move.id,
88 'period_id': line.period_id.id,
89 'journal_id': line.journal_id.id,
90 'partner_id': line.partner_id.id,
91@@ -154,11 +142,80 @@
92 'amount_currency': 0.00,
93 'state': 'valid',
94 'company_id': line.company_id.id,
95+ 'adjustment': line.id,
96 }
97- line_created_id = move_line_obj.create(cr, uid, move_line)
98- move_line_obj.write(cr, uid, [line.id], {'adjustment' : line_created_id})
99-
100- amount = self.get_balance_amount(cr, uid, move_created_id, context=context)
101+ new_move_line_id = move_line_obj.create(cr, uid, move_line, context=context)
102+ lines_created_ids.append(new_move_line_id)
103+
104+ return lines_created_ids
105+
106+ def create_move_lines_unreconcile(self, cr, uid, move, exchange_rate_end_period, context=None):
107+ move_line_obj = self.pool.get('account.move.line')
108+ account_account_obj = self.pool.get('account.account')
109+ lines_created_ids = []
110+ account_unreconcile_ids = account_account_obj.search(cr, uid, [('exchange_rate_adjustment', '=', True), ('reconcile', '=', False)], context=context)
111+
112+ for account_id in account_unreconcile_ids:
113+ total_credit = 0.00
114+ total_debit = 0.00
115+ adjustment_lines = []
116+ line_unreconcile_ids = move_line_obj.search(cr, uid, [('currency_id','!=',None), ('period_id','=',move.period_id.id), ('amount_currency','!=',0), ('account_id','=',account_id), ('adjustment','=',None)], context=context)
117+ lines_unreconcile = move_line_obj.browse(cr, uid, line_unreconcile_ids, context=context)
118+ for line in lines_unreconcile:
119+ if line.move_id.state == 'draft' or not line.amount_currency:
120+ continue
121+
122+ adjustment_lines.append(line.id)
123+ sign_amount_currency = line.amount_currency < 0 and -1 or 1
124+ line_difference = 0
125+ if line.credit != 0:
126+ line_difference = sign_amount_currency * line.amount_currency * exchange_rate_end_period - line.credit
127+ elif line.debit != 0:
128+ line_difference = sign_amount_currency * line.amount_currency * exchange_rate_end_period - line.debit
129+
130+ sign = line_difference < 0 and -1 or 1
131+ if line_difference == 0:
132+ continue
133+ elif line.credit == 0 and exchange_rate_end_period > line.amount_exchange_rate or line.debit == 0 and exchange_rate_end_period < line.amount_exchange_rate:
134+ total_debit += sign * line_difference
135+ else:
136+ total_credit += sign * line_difference
137+
138+ account = account_account_obj.browse(cr, uid, account_id, context=context)
139+
140+ if total_debit != 0.00 or total_credit != 0.00:
141+
142+ if total_debit > total_credit:
143+ total_debit -= total_credit
144+ total_credit = 0.00
145+ elif total_debit < total_credit:
146+ total_debit = 0.00
147+ total_credit -= total_debit
148+ else:
149+ continue
150+
151+ move_line = {
152+ 'name': _('Unreconcile lines adjustment'),
153+ 'debit': total_debit,
154+ 'credit': total_credit,
155+ 'account_id':account_id,
156+ 'move_id': move.id,
157+ 'period_id': move.period_id.id,
158+ 'journal_id': move.journal_id.id,
159+ 'currency_id': account.currency_id.id,
160+ 'amount_currency': 0.00,
161+ 'state': 'valid',
162+ 'company_id': move.company_id.id,
163+ }
164+ new_move_line_id = move_line_obj.create(cr, uid, move_line, context=context)
165+ move_line_obj.write(cr, uid, adjustment_lines, {'adjustment' : new_move_line_id}, context=context)
166+ lines_created_ids.append(new_move_line_id)
167+
168+ return lines_created_ids
169+
170+ def create_balance_line(self, cr, uid, move, res_user, name, context=None):
171+ move_line_obj = self.pool.get('account.move.line')
172+ amount = self.get_balance_amount(cr, uid, move.id, context=context)
173 if amount > 0:
174 account_id = res_user.company_id.expense_currency_exchange_account_id.id
175 credit = amount
176@@ -173,14 +230,40 @@
177 'debit': debit,
178 'credit': credit,
179 'account_id': account_id,
180- 'move_id': move_created_id,
181- 'period_id': period.id,
182- 'journal_id': journal.id,
183+ 'move_id': move.id,
184+ 'period_id': move.period_id.id,
185+ 'journal_id': move.journal_id.id,
186 'currency_id': False,
187 'amount_currency': 0.00,
188 'state': 'valid',
189 'company_id': res_user.company_id.id,
190 }
191- line_created_id = move_line_obj.create(cr, uid, move_line)
192+ new_move_line_id = move_line_obj.create(cr, uid, move_line, context=context)
193+ return new_move_line_id
194+
195+ def generate_adjustment_move(self, cr, uid, reference, journal, period, context=None):
196+ res_currency_obj = self.pool.get('res.currency')
197+ res_currency_rate_obj = self.pool.get('res.currency.rate')
198+ res_user_obj = self.pool.get('res.users')
199+
200+ res_user = res_user_obj.browse(cr, uid, uid, context=context)
201+ company_currency = res_user.company_id.currency_id
202+ name = reference + " " + period.name
203+
204+ move_created = {
205+ 'ref': name,
206+ 'journal_id': journal.id,
207+ 'period_id': period.id,
208+ 'to_check': False,
209+ 'company_id': res_user.company_id.id,
210+ }
211+ move_created_id = self.create(cr, uid, move_created)
212+ move_created = self.browse(cr, uid, move_created_id, context=context)
213+
214+ exchange_rate_end_period = res_currency_obj._current_rate(cr, uid, [company_currency.id], period.date_stop, arg=None, context=context)[company_currency.id]
215+ lines_reconcile_ids = self.create_move_lines_reconcile(cr, uid, move_created, exchange_rate_end_period, context=context)
216+ print lines_reconcile_ids
217+ lines_unreconcile_ids = self.create_move_lines_unreconcile(cr, uid, move_created, exchange_rate_end_period, context=context)
218+ balance_line_id = self.create_balance_line(cr, uid, move_created, res_user, name, context=context)
219 return move_created_id
220

Subscribers

People subscribed via source and target branches