← Back to team overview

clearcorp team mailing list archive

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

 

Ronald Rubi has proposed merging lp:~rr.clearcorp/openerp-costa-rica/6.1-l10n_cr_exchange_rates into lp:openerp-costa-rica/6.1.

Requested reviews:
  CLEARCORP drivers (clearcorp-drivers)

For more details, see:
https://code.launchpad.net/~rr.clearcorp/openerp-costa-rica/6.1-l10n_cr_exchange_rates/+merge/194013

[IMP] Improve adjustment move in l10n_cr_exchange_rates
-- 
https://code.launchpad.net/~rr.clearcorp/openerp-costa-rica/6.1-l10n_cr_exchange_rates/+merge/194013
Your team CLEARCORP development team is subscribed to branch lp:openerp-costa-rica/6.1.
=== modified file 'l10n_cr_exchange_rates/l10n_cr_exchange_rates.py'
--- l10n_cr_exchange_rates/l10n_cr_exchange_rates.py	2013-07-29 22:56:56 +0000
+++ l10n_cr_exchange_rates/l10n_cr_exchange_rates.py	2013-11-05 21:34:22 +0000
@@ -85,46 +85,35 @@
     _name = "account.move"
     _inherit = "account.move"
     
-    def get_balance_amount(self, cr, uid, id, context):
+    def get_balance_amount(self, cr, uid, move_id, context):
         cr.execute( 'SELECT SUM(debit-credit) '\
                     'FROM account_move_line '\
-                    'WHERE move_id = %s ', (id,))
+                    'WHERE move_id = %s ', (move_id,))
         result = cr.fetchall()
         return result[0][0] or 0.00
     
-    def generate_adjustment_move(self, cr, uid, reference, journal, period, context=None):
+    def get_adjustment_amount(self, cr, uid, move_id, context):
+        cr.execute( 'SELECT SUM(debit-credit) '\
+                    'FROM account_move_line '\
+                    'WHERE adjustment = %s ', (move_id,))
+        result = cr.fetchall()
+        return result[0][0] or 0.00
+        
+    def create_move_lines_reconcile(self, cr, uid, move, exchange_rate_end_period, context=None):
         move_line_obj = self.pool.get('account.move.line')
-        res_currency_obj = self.pool.get('res.currency')
-        res_currency_rate_obj = self.pool.get('res.currency.rate')
-            
-        res_user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
-        company_currency = res_user.company_id.currency_id
-        name = reference + " " + period.name
-        
-        move_created = {
-                    'ref': name,
-                    'journal_id': journal.id,
-                    'period_id': period.id,
-                    'to_check': False,
-                    'company_id': res_user.company_id.id,
-                    }
-        move_created_id = self.create(cr, uid, move_created)
-        
-        total_credit = 0.00
-        total_debit = 0.00
-        exchange_rate_end_period = res_currency_obj._current_rate(cr, uid, [company_currency.id], period.date_stop, arg=None, context=context)[company_currency.id]
-        
-        account_ids = self.pool.get('account.account').search(cr, uid, [('exchange_rate_adjustment', '=', True)], context=context)
-        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)
-        lines = move_line_obj.browse(cr, uid, line_ids, context=context)
-        
-    
-        for line in lines:
+        account_account_obj = self.pool.get('account.account')
+        lines_created_ids = []
+        account_reconcile_ids = account_account_obj.search(cr, uid, [('exchange_rate_adjustment', '=', True), ('reconcile', '=', True)], context=context)
+        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)
+        lines_reconcile = move_line_obj.browse(cr, uid, line_reconcile_ids, context=context)
+        
+        for line in lines_reconcile:
             if line.move_id.state == 'draft' or not line.amount_currency:
                 continue
             
             sign_amount_currency = line.amount_currency < 0 and -1 or 1
             line_difference = 0
+            adjustment_amount = self.get_adjustment_amount(cr, uid, line.id, context=context)
             if line.credit != 0:
                 line_difference = sign_amount_currency * line.amount_currency * exchange_rate_end_period - line.credit
             elif line.debit != 0:
@@ -135,18 +124,17 @@
                 continue
             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:
                 credit = 0.00
-                debit = sign * line_difference
+                debit = sign * line_difference - adjustment_amount
             else:
-                credit = sign * line_difference
+                credit = sign * line_difference + adjustment_amount
                 debit = 0.00
-            
             move_line = {
                          'name': line.name or '',
                          'ref': line.ref or '',
                          'debit': debit,
                          'credit': credit,
                          'account_id':line.account_id.id,
-                         'move_id': move_created_id,
+                         'move_id': move.id,
                          'period_id': line.period_id.id,
                          'journal_id': line.journal_id.id,
                          'partner_id': line.partner_id.id,
@@ -154,11 +142,80 @@
                          'amount_currency': 0.00,
                          'state': 'valid',
                          'company_id': line.company_id.id,
+                         'adjustment': line.id,
                          }
-            line_created_id = move_line_obj.create(cr, uid, move_line)
-            move_line_obj.write(cr, uid, [line.id], {'adjustment' : line_created_id})
-    
-        amount = self.get_balance_amount(cr, uid, move_created_id, context=context)
+            new_move_line_id = move_line_obj.create(cr, uid, move_line, context=context)
+            lines_created_ids.append(new_move_line_id)
+            
+        return lines_created_ids
+        
+    def create_move_lines_unreconcile(self, cr, uid, move, exchange_rate_end_period, context=None):
+        move_line_obj = self.pool.get('account.move.line')
+        account_account_obj = self.pool.get('account.account')
+        lines_created_ids = []
+        account_unreconcile_ids = account_account_obj.search(cr, uid, [('exchange_rate_adjustment', '=', True), ('reconcile', '=', False)], context=context)
+
+        for account_id in account_unreconcile_ids:
+            total_credit = 0.00
+            total_debit = 0.00
+            adjustment_lines = []
+            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)
+            lines_unreconcile = move_line_obj.browse(cr, uid, line_unreconcile_ids, context=context)
+            for line in lines_unreconcile:
+                if line.move_id.state == 'draft' or not line.amount_currency:
+                    continue
+                
+                adjustment_lines.append(line.id)
+                sign_amount_currency = line.amount_currency < 0 and -1 or 1
+                line_difference = 0
+                if line.credit != 0:
+                    line_difference = sign_amount_currency * line.amount_currency * exchange_rate_end_period - line.credit
+                elif line.debit != 0:
+                    line_difference = sign_amount_currency * line.amount_currency * exchange_rate_end_period - line.debit
+                
+                sign = line_difference < 0 and -1 or 1
+                if line_difference == 0:
+                    continue
+                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:
+                    total_debit += sign * line_difference
+                else:
+                    total_credit += sign * line_difference
+            
+            account = account_account_obj.browse(cr, uid, account_id, context=context)
+            
+            if total_debit != 0.00 or total_credit != 0.00:
+                
+                if total_debit > total_credit:
+                    total_debit -= total_credit
+                    total_credit = 0.00
+                elif total_debit < total_credit:
+                    total_debit = 0.00
+                    total_credit -= total_debit
+                else:
+                    continue
+                
+                move_line = {
+                             'name': _('Unreconcile lines adjustment'),
+                             'debit': total_debit,
+                             'credit': total_credit,
+                             'account_id':account_id,
+                             'move_id': move.id,
+                             'period_id': move.period_id.id,
+                             'journal_id': move.journal_id.id,
+                             'currency_id': account.currency_id.id,
+                             'amount_currency': 0.00,
+                             'state': 'valid',
+                             'company_id': move.company_id.id,
+                             }
+                new_move_line_id = move_line_obj.create(cr, uid, move_line, context=context)
+                move_line_obj.write(cr, uid, adjustment_lines, {'adjustment' : new_move_line_id}, context=context)
+                lines_created_ids.append(new_move_line_id)
+        
+        return lines_created_ids
+            
+    def create_balance_line(self, cr, uid, move, res_user, name, context=None):    
+        move_line_obj = self.pool.get('account.move.line')
+        amount = self.get_balance_amount(cr, uid, move.id, context=context)
         if amount > 0:
             account_id = res_user.company_id.expense_currency_exchange_account_id.id
             credit = amount
@@ -173,14 +230,40 @@
                              'debit': debit,
                              'credit': credit,
                              'account_id': account_id,
-                             'move_id': move_created_id,
-                             'period_id': period.id,
-                             'journal_id': journal.id,
+                             'move_id': move.id,
+                             'period_id': move.period_id.id,
+                             'journal_id': move.journal_id.id,
                              'currency_id': False,
                              'amount_currency': 0.00,
                              'state': 'valid',
                              'company_id': res_user.company_id.id,
                              }
-        line_created_id = move_line_obj.create(cr, uid, move_line)
+        new_move_line_id = move_line_obj.create(cr, uid, move_line, context=context)
+        return new_move_line_id
+    
+    def generate_adjustment_move(self, cr, uid, reference, journal, period, context=None):
+        res_currency_obj = self.pool.get('res.currency')
+        res_currency_rate_obj = self.pool.get('res.currency.rate')
+        res_user_obj = self.pool.get('res.users')
+            
+        res_user = res_user_obj.browse(cr, uid, uid, context=context)
+        company_currency = res_user.company_id.currency_id
+        name = reference + " " + period.name
+        
+        move_created = {
+                    'ref': name,
+                    'journal_id': journal.id,
+                    'period_id': period.id,
+                    'to_check': False,
+                    'company_id': res_user.company_id.id,
+                    }
+        move_created_id = self.create(cr, uid, move_created)
+        move_created = self.browse(cr, uid, move_created_id, context=context)
+        
+        exchange_rate_end_period = res_currency_obj._current_rate(cr, uid, [company_currency.id], period.date_stop, arg=None, context=context)[company_currency.id]
+        lines_reconcile_ids = self.create_move_lines_reconcile(cr, uid, move_created, exchange_rate_end_period, context=context)
+        print lines_reconcile_ids
+        lines_unreconcile_ids = self.create_move_lines_unreconcile(cr, uid, move_created, exchange_rate_end_period, context=context)
+        balance_line_id = self.create_balance_line(cr, uid, move_created, res_user, name, context=context)
         return move_created_id
     


Follow ups