def _create_invoice_for_po(self, purchase_order, date):
     move_form = Form(
         self.env['account.move'].with_context(default_type='in_invoice'))
     move_form.invoice_date = date
     move_form.partner_id = self.test_partner
     move_form.currency_id = self.currency_two
     move_form.purchase_id = purchase_order
     return move_form.save()
    def test_in_refund_line_onchange_currency_1(self):
        # New journal having a foreign currency set.
        journal = self.company_data['default_journal_purchase'].copy()
        journal.currency_id = self.currency_data['currency']

        move_form = Form(self.invoice)
        move_form.journal_id = journal
        move_form.save()

        self.assertInvoiceValues(
            self.invoice, [
                {
                    **self.product_line_vals_1,
                    'currency_id':
                    journal.currency_id.id,
                    'amount_currency':
                    -800.0,
                    'credit':
                    400.0,
                },
                {
                    **self.product_line_vals_2,
                    'currency_id':
                    journal.currency_id.id,
                    'amount_currency':
                    -160.0,
                    'credit':
                    80.0,
                },
                {
                    **self.tax_line_vals_1,
                    'currency_id': journal.currency_id.id,
                    'amount_currency': -144.0,
                    'credit': 72.0,
                },
                {
                    **self.tax_line_vals_2,
                    'currency_id': journal.currency_id.id,
                    'amount_currency': -24.0,
                    'credit': 12.0,
                },
                {
                    **self.term_line_vals_1,
                    'currency_id': journal.currency_id.id,
                    'amount_currency': 1128.0,
                    'debit': 564.0,
                },
            ], {
                **self.move_vals,
                'currency_id': journal.currency_id.id,
                'journal_id': journal.id,
            })

        move_form = Form(self.invoice)
        # Change the date to get another rate: 1/3 instead of 1/2.
        move_form.date = fields.Date.from_string('2016-01-01')
        move_form.save()

        self.assertInvoiceValues(
            self.invoice, [
                {
                    **self.product_line_vals_1,
                    'currency_id':
                    journal.currency_id.id,
                    'amount_currency':
                    -800.0,
                    'credit':
                    266.67,
                },
                {
                    **self.product_line_vals_2,
                    'currency_id':
                    journal.currency_id.id,
                    'amount_currency':
                    -160.0,
                    'credit':
                    53.33,
                },
                {
                    **self.tax_line_vals_1,
                    'currency_id': journal.currency_id.id,
                    'amount_currency': -144.0,
                    'credit': 48.0,
                },
                {
                    **self.tax_line_vals_2,
                    'currency_id': journal.currency_id.id,
                    'amount_currency': -24.0,
                    'credit': 8.0,
                },
                {
                    **self.term_line_vals_1,
                    'currency_id': journal.currency_id.id,
                    'amount_currency': 1128.0,
                    'debit': 376.0,
                },
            ], {
                **self.move_vals,
                'currency_id': journal.currency_id.id,
                'journal_id': journal.id,
                'date': fields.Date.from_string('2016-01-01'),
            })

        move_form = Form(self.invoice)
        with move_form.invoice_line_ids.edit(0) as line_form:
            # 0.045 * 0.1 = 0.0045. As the foreign currency has a 0.001 rounding,
            # the result should be 0.005 after rounding.
            line_form.quantity = 0.1
            line_form.price_unit = 0.045
        move_form.save()

        self.assertInvoiceValues(
            self.invoice, [
                {
                    **self.product_line_vals_1,
                    'quantity': 0.1,
                    'price_unit': 0.05,
                    'price_subtotal': 0.005,
                    'price_total': 0.006,
                    'currency_id': journal.currency_id.id,
                    'amount_currency': -0.005,
                    'credit': 0.0,
                },
                {
                    **self.product_line_vals_2,
                    'currency_id':
                    journal.currency_id.id,
                    'amount_currency':
                    -160.0,
                    'credit':
                    53.33,
                },
                {
                    **self.tax_line_vals_1,
                    'price_unit': 24.0,
                    'price_subtotal': 24.001,
                    'price_total': 24.001,
                    'currency_id': journal.currency_id.id,
                    'amount_currency': -24.001,
                    'credit': 8.0,
                },
                {
                    **self.tax_line_vals_2,
                    'currency_id': journal.currency_id.id,
                    'amount_currency': -24.0,
                    'credit': 8.0,
                },
                {
                    **self.term_line_vals_1,
                    'currency_id': journal.currency_id.id,
                    'price_unit': -208.01,
                    'price_subtotal': -208.006,
                    'price_total': -208.006,
                    'amount_currency': 208.006,
                    'debit': 69.33,
                },
            ], {
                **self.move_vals,
                'currency_id': journal.currency_id.id,
                'journal_id': journal.id,
                'date': fields.Date.from_string('2016-01-01'),
                'amount_untaxed': 160.005,
                'amount_tax': 48.001,
                'amount_total': 208.006,
            })

        # The journal forces you to provide a secondary currency.
        with self.assertRaises(UserError), self.cr.savepoint():
            move_form = Form(self.invoice)
            move_form.currency_id = self.company_data['currency']
            move_form.save()

        # Exit the multi-currencies.
        journal.currency_id = False
        move_form = Form(self.invoice)
        move_form.currency_id = self.company_data['currency']
        move_form.save()

        self.assertInvoiceValues(
            self.invoice, [
                {
                    **self.product_line_vals_1,
                    'quantity': 0.1,
                    'price_unit': 0.05,
                    'price_subtotal': 0.01,
                    'price_total': 0.01,
                    'credit': 0.01,
                },
                self.product_line_vals_2,
                {
                    **self.tax_line_vals_1,
                    'price_unit': 24.0,
                    'price_subtotal': 24.0,
                    'price_total': 24.0,
                    'credit': 24.0,
                },
                self.tax_line_vals_2,
                {
                    **self.term_line_vals_1,
                    'price_unit': -208.01,
                    'price_subtotal': -208.01,
                    'price_total': -208.01,
                    'debit': 208.01,
                },
            ], {
                **self.move_vals,
                'currency_id': self.company_data['currency'].id,
                'journal_id': journal.id,
                'date': fields.Date.from_string('2016-01-01'),
                'amount_untaxed': 160.01,
                'amount_tax': 48.0,
                'amount_total': 208.01,
            })