-
  In order to check the Account_voucher module with multi-currency in OpenERP, 
  I create 2 Invoices in USD and make 2 Payments one in USD and another in EUR, based on the currency rating on that particular date
-
  I set the income and expense currency accounts on the main company
-
  !python {model: res.company}: |
    from datetime import datetime
    vals = {
      'income_currency_exchange_account_id': ref('account.o_expense'),
      'expense_currency_exchange_account_id': ref('account.o_expense')}
    self.write(cr, uid, ref('base.main_company'), vals)
-
  I create a bank journal with EUR as currency
-
  !record {model: account.journal, id: bank_journal_EUR}:
    name: Bank Journal(EUR)
    code: BEUR
    type: bank
    analytic_journal_id: account.sit
    sequence_id: account.sequence_bank_journal
    default_debit_account_id: account.cash
    default_credit_account_id: account.cash
    currency: base.EUR
    company_id: base.main_company
-
  I create a bank journal with USD as currency
-
  !record {model: account.journal, id: bank_journal_USD}:
    name: Bank Journal(USD)
    code: BUSD
    type: bank
    analytic_journal_id: account.sit
    sequence_id: account.sequence_bank_journal
    default_debit_account_id: account.cash
    default_credit_account_id: account.cash
    currency: base.USD
    company_id: base.main_company
-
  I create the first invoice on 1st January for 200 USD
-
  !record {model: account.invoice, id: account_first_invoice_jan_suppl}:
    account_id: account.a_pay
    type : in_invoice
    company_id: base.main_company
    currency_id: base.USD
    date_invoice: !eval "'%s-01-01' %(datetime.now().year)"
    period_id: account.period_1
    invoice_line:
      - account_id: account.a_expense
        name: '[PCSC234] PC Assemble SC234'
        price_unit: 200.0
        quantity: 1.0
        product_id: product.product_product_3
        uos_id: product.product_uom_unit
    journal_id: account.sales_journal
    partner_id: base.res_partner_19
    reference_type: none
    check_total : 200
-
  I Validate invoice by clicking on Validate button
-
  !workflow {model: account.invoice, action: invoice_open, ref: account_first_invoice_jan_suppl}
-
  I check that first invoice move is correct for debtor account(debit - credit == -150)
-
  !python {model: account.invoice}: |
    invoice_id = self.browse(cr, uid, ref("account_first_invoice_jan_suppl"))
    assert invoice_id.move_id, "Move not created for open invoice"
    move_line_obj = self.pool.get('account.move.line')
    move_lines = move_line_obj.search(cr, uid, [('move_id', '=', invoice_id.move_id.id), ('account_id', '=', invoice_id.account_id.id)])
    move_line = move_line_obj.browse(cr, uid, move_lines[0])
    assert (move_line.debit - move_line.credit == -150.00), "Invoice move is incorrect for debtors account"
-
  I create the second invoice on 1st February for 100 USD
-
  !record {model: account.invoice, id: account_second_invoice_feb_suppl}:
    account_id: account.a_pay
    company_id: base.main_company
    currency_id: base.USD
    date_invoice: !eval "'%s-02-01' %(datetime.now().year)"
    period_id: account.period_2
    type : in_invoice
    invoice_line:
      - account_id: account.a_expense
        name: '[PCSC234] PC Assemble SC234'
        price_unit: 100.0
        quantity: 1.0
        product_id: product.product_product_3
        uos_id: product.product_uom_unit
    journal_id: account.sales_journal
    partner_id: base.res_partner_19
    reference_type: none
    check_total : 100.0
-
  I Validate invoice by clicking on Validate button
-
  !workflow {model: account.invoice, action: invoice_open, ref: account_second_invoice_feb_suppl}
-
  I check that second invoice move is correct for debtor account (debit - credit == -80)
-
  !python {model: account.invoice}: |
    invoice_id = self.browse(cr, uid, ref("account_second_invoice_feb_suppl"))
    assert invoice_id.move_id, "Move not created for open invoice"
    move_line_obj = self.pool.get('account.move.line')
    move_lines = move_line_obj.search(cr, uid, [('move_id', '=', invoice_id.move_id.id), ('account_id', '=', invoice_id.account_id.id)])
    move_line = move_line_obj.browse(cr, uid, move_lines[0])
    assert (move_line.debit - move_line.credit == -80), "Invoice move is incorrect for debtors account"
-
  I set the context that will be used for the encoding of all the vouchers of this file
-
  !context
    'type': 'payment'
-
  I create the first voucher of payment with values 240 EUR, journal EUR
-
  !record {model: account.voucher, id: account_voucher_1_case2_suppl, view: view_vendor_payment_form}:
    account_id: account.cash
    amount: 240.0
    company_id: base.main_company
    journal_id: bank_journal_EUR
    name: 'First payment: Case 2 SUPPL USD/EUR',
    partner_id: base.res_partner_19
    period_id: account.period_3
    date: !eval time.strftime("%Y-03-01")
    payment_option: 'with_writeoff'
    writeoff_acc_id: account.a_expense
    comment: 'Write Off'

-
  I fill amounts 180 for the invoice of 200$ and 70 for the invoice of 100$
-
  !python {model: account.voucher}: |
    vals = {}
    voucher_id = self.browse(cr, uid, ref('account_voucher_1_case2_suppl'))
    data = []
    for item in voucher_id.line_cr_ids:
        if item.amount_unreconciled == 200.00:
            data += [(item.id, 180.0)]
        else:
            data += [(item.id, 70.0)]
    for line_id, amount in data:
        self.pool.get('account.voucher.line').write(cr, uid, [line_id], {'amount': amount})
    assert (voucher_id.state=='draft'), "Voucher is not in draft state"
-
  I check that writeoff amount computed is -15.0
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'First payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    voucher_id = self.browse(cr, uid, voucher[0])
    assert (voucher_id.writeoff_amount == -15.0), "Writeoff amount is not -15.0"
-
  I check that currency rate difference is 34.0
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'First payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    voucher_id = self.browse(cr, uid, voucher[0])
    assert (voucher_id.currency_rate_difference == 34.0), "Currency rate difference is not 34.0"
-
  I confirm the voucher
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'First payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    self.signal_workflow(cr, uid, voucher, 'proforma_voucher')
-
  I check that the move of my voucher is valid
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'First payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    voucher_id = self.browse(cr, uid, voucher[0])
    move_line_obj = self.pool.get('account.move.line')
    move_lines = move_line_obj.search(cr, uid, [('move_id', '=', voucher_id.move_id.id)])
    for move_line in move_line_obj.browse(cr, uid, move_lines):
        assert move_line.state == 'valid', "Voucher move is not valid"
-
  I check that my creditor account is correct
-
  I check that the creditor account has 2 new lines with 180 and 70 in amount_currency columns and their debit columns are respectively 135 and 56 and currency is USD($).
-
  I check that my currency rate difference is correct. -34 in credit with no amount_currency
-
  I check that my writeoff is correct. -15 in credit with no amount_currency
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'First payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    voucher_id = self.browse(cr, uid, voucher[0])
    move_line_obj = self.pool.get('account.move.line')
    move_lines = move_line_obj.search(cr, uid, [('move_id', '=', voucher_id.move_id.id)])
    for move_line in move_line_obj.browse(cr, uid, move_lines):
        if move_line.amount_currency == 180.00:
            assert move_line.debit == 135.00, "Creditor account has wrong entry."
        elif move_line.amount_currency == 70.00:
            assert move_line.debit == 56.00, "Debtor account has wrong entry."
        elif move_line.debit == 34.00:
            assert move_line.amount_currency == 0.00, "Incorrect Currency Difference."
        elif move_line.debit == 15.00:
            assert move_line.amount_currency == 0.00, "Writeoff amount is wrong."
-
  I check the residual amount of Invoice1, should be 20 in residual currency and 15 in amount_residual
-
  !python {model: account.invoice}: |
    invoice_id = self.browse(cr, uid, ref("account_first_invoice_jan_suppl"))
    move_line_obj = self.pool.get('account.move.line')
    move_lines = move_line_obj.search(cr, uid, [('move_id', '=', invoice_id.move_id.id), ('invoice', '=', invoice_id.id), ('account_id', '=', invoice_id.account_id.id)])
    move_line = move_line_obj.browse(cr, uid, move_lines[0])
    assert (move_line.amount_residual_currency == 20.0 and move_line.amount_residual == 15) , "Residual amount is not correct for first Invoice"
-
  I check the residual amuont of Invoice2, should be 30 in residual currency and 24 in amount_residual
-
   !python {model: account.invoice}: |
    invoice_id = self.browse(cr, uid, ref("account_second_invoice_feb_suppl"))
    move_line_obj = self.pool.get('account.move.line')
    move_lines = move_line_obj.search(cr, uid, [('move_id', '=', invoice_id.move_id.id), ('invoice', '=', invoice_id.id), ('account_id', '=', invoice_id.account_id.id)])
    move_line = move_line_obj.browse(cr, uid, move_lines[0])
    assert (move_line.amount_residual_currency == 30 and move_line.amount_residual == 24) , "Residual amount is not correct for second Invoice"
-
  I create the second voucher of payment with values 45 USD, journal USD, 
-
  !record {model: account.voucher, id: account_voucher_2_case2_suppl, view: view_vendor_payment_form}:
    account_id: account.cash
    amount: 45.0
    company_id: base.main_company
    journal_id: bank_journal_USD
    partner_id: base.res_partner_19
    period_id: account.period_3
    date: !eval time.strftime("%Y-04-01")
    payment_option: 'with_writeoff'
    writeoff_acc_id: account.a_expense
    comment: 'Write Off'
    name: 'Second payment: Case 2 SUPPL USD/EUR'

-
  I fill amounts 20 for the invoice of 200$ and 30 for the invoice of 100$>
-
  !python {model: account.voucher}: |
    vals = {}
    voucher_id = self.browse(cr, uid, ref('account_voucher_2_case2_suppl'))
    data = []
    for item in voucher_id.line_cr_ids:
        if item.amount_unreconciled == 20.00:
            data += [(item.id, 20.0)]
        else:
            data += [(item.id, 30.0)]
    for line_id, amount in data:
        self.pool.get('account.voucher.line').write(cr, uid, [line_id], {'amount': amount})
    assert (voucher_id.state=='draft'), "Voucher is not in draft state"
-
  I check that writeoff amount computed is -5.0
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'Second payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    voucher_id = self.browse(cr, uid, voucher[0])
    assert (voucher_id.writeoff_amount == 5.0), "Writeoff amount is not 5.0"
-
  I check that currency rate difference is 8.50
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'Second payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    voucher_id = self.browse(cr, uid, voucher[0])
    assert (voucher_id.currency_rate_difference == 8.50), "Currency rate difference is not 8.50"
-
  I confirm the voucher
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'Second payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    self.signal_workflow(cr, uid, voucher, 'proforma_voucher')
-
  I check that my voucher state is posted
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'Second payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    voucher_id = self.browse(cr, uid, voucher[0])
    assert voucher_id.state == 'posted', "Voucher state is not posted"
-
  I check that my creditor account is correct
-
  I check that the creditor account has 2 new lines with 20 and 30 in amount_currency columns and their debit columns are respectively 15 and 24 and currency is USD($).
-
  I check that my currency rate difference is correct. 8.50 in debit with no amount_currency
-
  I check that my writeoff is correct. 4.75 in credit and 5 in amount_currency
-
  !python {model: account.voucher}: |
    voucher = self.search(cr, uid, [('name', '=', 'Second payment: Case 2 SUPPL USD/EUR'), ('partner_id', '=', ref('base.res_partner_19'))])
    voucher_id = self.browse(cr, uid, voucher[0])
    move_line_obj = self.pool.get('account.move.line')
    move_lines = move_line_obj.search(cr, uid, [('move_id', '=', voucher_id.move_id.id)])
    for move_line in move_line_obj.browse(cr, uid, move_lines):
        if move_line.amount_currency == 20.00:
            assert move_line.debit == 15.00, "Debtor account has wrong entry."
        elif move_line.amount_currency == 30.00:
            assert move_line.debit == 24.00, "Debtor account has wrong entry."
        elif move_line.debit == 8.50:
            assert move_line.amount_currency == 0.00, "Incorrect Currency Difference."
        elif move_line.amount_currency == -5.00:
            assert move_line.credit == 4.75, "Writeoff amount is wrong."
-
  I check the residual amount of invoice 1, should be 0 in residual currency and 0 in amount_residual and paid
-
  !python {model: account.invoice}: |
    invoice_id = self.browse(cr, uid, ref("account_first_invoice_jan_suppl"))
    move_line_obj = self.pool.get('account.move.line')
    move_lines = move_line_obj.search(cr, uid, [('move_id', '=', invoice_id.move_id.id), ('invoice', '=', invoice_id.id), ('account_id', '=', invoice_id.account_id.id)])
    move_line = move_line_obj.browse(cr, uid, move_lines[0])
    assert (move_line.amount_residual_currency == 0.0 and move_line.amount_residual == 0.0 and invoice_id.state == 'paid') , "Residual amount is not correct for first Invoice"
-    
  I check the residual amount of invoice 2, should be 0 in residual currency and 0 in amount_residual and paid
-
   !python {model: account.invoice}: |
    invoice_id = self.browse(cr, uid, ref("account_second_invoice_feb_suppl"))
    move_line_obj = self.pool.get('account.move.line')
    move_lines = move_line_obj.search(cr, uid, [('move_id', '=', invoice_id.move_id.id), ('invoice', '=', invoice_id.id), ('account_id', '=', invoice_id.account_id.id)])
    move_line = move_line_obj.browse(cr, uid, move_lines[0])
    assert (move_line.amount_residual_currency == 0.0 and move_line.amount_residual == 0.0 and invoice_id.state == 'paid') , "Residual amount is not correct for second Invoice"
