← Back to team overview

clearcorp team mailing list archive

[Merge] lp:~rr.clearcorp/openobject-addons/6.1-ccorp-account_voucher into lp:~clearcorp-drivers/openobject-addons/6.1-ccorp

 

Ronald Rubi has proposed merging lp:~rr.clearcorp/openobject-addons/6.1-ccorp-account_voucher into lp:~clearcorp-drivers/openobject-addons/6.1-ccorp.

Requested reviews:
  CLEARCORP drivers (clearcorp-drivers)

For more details, see:
https://code.launchpad.net/~rr.clearcorp/openobject-addons/6.1-ccorp-account_voucher/+merge/179054

Fix voucher_move_line_create in account_voucher
-- 
https://code.launchpad.net/~rr.clearcorp/openobject-addons/6.1-ccorp-account_voucher/+merge/179054
Your team CLEARCORP development team is subscribed to branch lp:~clearcorp-drivers/openobject-addons/6.1-ccorp.
=== modified file 'account_voucher/account_voucher.py'
--- account_voucher/account_voucher.py	2013-02-14 02:41:29 +0000
+++ account_voucher/account_voucher.py	2013-08-07 21:46:27 +0000
@@ -26,6 +26,7 @@
 from osv import osv, fields
 import decimal_precision as dp
 from tools.translate import _
+from openerp.tools import float_compare
 
 class res_company(osv.osv):
     _inherit = "res.company"
@@ -1015,37 +1016,45 @@
         tot_line = line_total
         rec_lst_ids = []
 
-        voucher_brw = self.pool.get('account.voucher').browse(cr, uid, voucher_id, context)
+        date = self.read(cr, uid, voucher_id, ['date'], context=context)['date']
         ctx = context.copy()
-        ctx.update({'date': voucher_brw.date})
-        for line in voucher_brw.line_ids:
+        ctx.update({'date': date})
+        voucher = self.pool.get('account.voucher').browse(cr, uid, voucher_id, context=ctx)
+        voucher_currency = voucher.journal_id.currency or voucher.company_id.currency_id
+        ctx.update({
+            'voucher_special_currency_rate': voucher_currency.rate * voucher.payment_rate ,
+            'voucher_special_currency': voucher.payment_rate_currency_id and voucher.payment_rate_currency_id.id or False,})
+        prec = self.pool.get('decimal.precision').precision_get(cr, uid, 'Account')
+        for line in voucher.line_ids:
             #create one move line per voucher line where amount is not 0.0
-            if not line.amount:
+            # AND (second part of the clause) only if the original move line was not having debit = credit = 0 (which is a legal value)
+            if not line.amount and not (line.move_line_id and not float_compare(line.move_line_id.debit, line.move_line_id.credit, precision_rounding=prec) and not float_compare(line.move_line_id.debit, 0.0, precision_rounding=prec)):
                 continue
             # convert the amount set on the voucher line into the currency of the voucher's company
-            amount = self._convert_amount(cr, uid, line.untax_amount or line.amount, voucher_brw.id, context=ctx)
+            # this calls res_curreny.compute() with the right context, so that it will take either the rate on the voucher if it is relevant or will use the default behaviour
+            amount = self._convert_amount(cr, uid, line.untax_amount or line.amount, voucher.id, context=ctx)
             # if the amount encoded in voucher is equal to the amount unreconciled, we need to compute the
             # currency rate difference
             if line.amount == line.amount_unreconciled:
-                if voucher_brw.type == 'payment':
-                    currency_rate_difference = amount - line.move_line_id.amount_residual
-                else:
-                    currency_rate_difference = line.move_line_id.amount_residual - amount
+                if not line.move_line_id:
+                    raise osv.except_osv(_('Wrong voucher line'),_("The invoice you are willing to pay is not valid anymore."))
+                sign = voucher.type in ('payment', 'purchase') and -1 or 1
+                currency_rate_difference = sign * (line.move_line_id.amount_residual - amount)
             else:
                 currency_rate_difference = 0.0
             move_line = {
-                'journal_id': voucher_brw.journal_id.id,
-                'period_id': voucher_brw.period_id.id,
+                'journal_id': voucher.journal_id.id,
+                'period_id': voucher.period_id.id,
                 'name': line.name or '/',
                 'account_id': line.account_id.id,
                 'move_id': move_id,
-                'partner_id': voucher_brw.partner_id.id,
+                'partner_id': voucher.partner_id.id,
                 '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,
                 'analytic_account_id': line.account_analytic_id and line.account_analytic_id.id or False,
                 'quantity': 1,
                 'credit': 0.0,
                 'debit': 0.0,
-                'date': voucher_brw.date
+                'date': voucher.date
             }
             if amount < 0:
                 amount = -amount
@@ -1061,9 +1070,9 @@
                 tot_line -= amount
                 move_line['credit'] = amount
 
-            if voucher_brw.tax_id and voucher_brw.type in ('sale', 'purchase'):
+            if voucher.tax_id and voucher.type in ('sale', 'purchase'):
                 move_line.update({
-                    'account_tax_id': voucher_brw.tax_id.id,
+                    'account_tax_id': voucher.tax_id.id,
                 })
 
             if move_line.get('account_tax_id', False):
@@ -1075,35 +1084,26 @@
             foreign_currency_diff = 0.0
             amount_currency = False
             if line.move_line_id:
-                voucher_currency = voucher_brw.currency_id and voucher_brw.currency_id.id or voucher_brw.journal_id.company_id.currency_id.id
                 # We want to set it on the account move line as soon as the original line had a foreign currency
                 if line.move_line_id.currency_id and line.move_line_id.currency_id.id != company_currency:
-                    # we compute the amount in that foreign currency. 
+                    # we compute the amount in that foreign currency.
                     if line.move_line_id.currency_id.id == current_currency:
                         # if the voucher and the voucher line share the same currency, there is no computation to do
                         sign = (move_line['debit'] - move_line['credit']) < 0 and -1 or 1
                         amount_currency = sign * (line.amount)
-                    elif line.move_line_id.currency_id.id == voucher_brw.payment_rate_currency_id.id:
-                        # if the rate is specified on the voucher, we must use it
-                        voucher_rate = currency_obj.browse(cr, uid, voucher_currency, context=ctx).rate
-                        amount_currency = currency_obj.compute(cr, uid, voucher_brw.company_id.currency_id.id, voucher_brw.currency_id.id, (move_line['debit'] - move_line['credit']), context=context)
-                        amount_currency = amount_currency * voucher_brw.payment_rate
                     else:
-                        # otherwise we use the rates of the system (giving the voucher date in the context)
+                        # if the rate is specified on the voucher, it will be used thanks to the special keys in the context
+                        # otherwise we use the rates of the system
                         amount_currency = currency_obj.compute(cr, uid, company_currency, line.move_line_id.currency_id.id, move_line['debit']-move_line['credit'], context=ctx)
-                if line.amount == line.amount_unreconciled and line.move_line_id.currency_id.id == voucher_currency:
-                    # The move_line_id.amount_residual_currency is positive if receivable and debit, or payable and credit
-                    # The amount_currency is positive if debit (for the new line to create)
-                    if line.move_line_id.account_id and line.move_line_id.account_id.type == 'payable':
-                        foreign_currency_diff = line.move_line_id.amount_residual_currency - amount_currency
-                    else:
-                        foreign_currency_diff = line.move_line_id.amount_residual_currency + amount_currency
+                if line.amount == line.amount_unreconciled:
+                    sign = voucher.type in ('payment', 'purchase') and -1 or 1
+                    foreign_currency_diff = sign * line.move_line_id.amount_residual_currency + amount_currency
 
             move_line['amount_currency'] = amount_currency
             voucher_line = move_line_obj.create(cr, uid, move_line)
             rec_ids = [voucher_line, line.move_line_id.id]
 
-            if not currency_obj.is_zero(cr, uid, voucher_brw.company_id.currency_id, currency_rate_difference):
+            if not currency_obj.is_zero(cr, uid, voucher.company_id.currency_id, currency_rate_difference):
                 # Change difference entry in company currency
                 exch_lines = self._get_exchange_lines(cr, uid, line, move_id, currency_rate_difference, company_currency, current_currency, context=context)
                 new_id = move_line_obj.create(cr, uid, exch_lines[0],context)


Follow ups