def _create_invoice_for_po(self, purchase_order, date): move_form = Form(self.env['account.move'].with_context( default_move_type='in_invoice', default_date=date)) move_form.invoice_date = date move_form.partner_id = self.partner_a move_form.currency_id = self.currency_data['currency'] move_form.purchase_id = purchase_order return move_form.save()
def init_invoice(cls, move_type, partner=None, invoice_date=None, post=False, products=[], amounts=[], taxes=None): move_form = Form(cls.env['account.move'].with_context( default_move_type=move_type, account_predictive_bills_disable_prediction=True)) move_form.invoice_date = invoice_date or fields.Date.from_string( '2019-01-01') move_form.partner_id = partner or cls.partner_a for product in products: with move_form.invoice_line_ids.new() as line_form: line_form.product_id = product if taxes: line_form.tax_ids.clear() line_form.tax_ids.add(taxes) for amount in amounts: with move_form.invoice_line_ids.new() as line_form: line_form.name = "test line" # We use account_predictive_bills_disable_prediction context key so that # this doesn't trigger prediction in case enterprise (hence account_predictive_bills) is installed line_form.price_unit = amount if taxes: line_form.tax_ids.clear() line_form.tax_ids.add(taxes) rslt = move_form.save() if post: rslt.action_post() return rslt
def test_out_refund_reverse_caba(self): tax_waiting_account = self.env['account.account'].create({ 'name': 'TAX_WAIT', 'code': 'TWAIT', 'user_type_id': self.env.ref('account.data_account_type_current_liabilities').id, 'reconcile': True, 'company_id': self.company_data['company'].id, }) tax_final_account = self.env['account.account'].create({ 'name': 'TAX_TO_DEDUCT', 'code': 'TDEDUCT', 'user_type_id': self.env.ref('account.data_account_type_current_assets').id, 'company_id': self.company_data['company'].id, }) tax_base_amount_account = self.env['account.account'].create({ 'name': 'TAX_BASE', 'code': 'TBASE', 'user_type_id': self.env.ref('account.data_account_type_current_assets').id, 'company_id': self.company_data['company'].id, }) self.env.company.account_cash_basis_base_account_id = tax_base_amount_account tax_tags = defaultdict(dict) for line_type, repartition_type in [(l, r) for l in ('invoice', 'refund') for r in ('base', 'tax')]: tax_tags[line_type][repartition_type] = self.env[ 'account.account.tag'].create({ 'name': '%s %s tag' % (line_type, repartition_type), 'applicability': 'taxes', 'country_id': self.env.ref('base.us').id, }) tax = self.env['account.tax'].create({ 'name': 'cash basis 10%', 'type_tax_use': 'sale', 'amount': 10, 'tax_exigibility': 'on_payment', 'cash_basis_transition_account_id': tax_waiting_account.id, 'invoice_repartition_line_ids': [ (0, 0, { 'factor_percent': 100, 'repartition_type': 'base', 'tag_ids': [(6, 0, tax_tags['invoice']['base'].ids)], }), (0, 0, { 'factor_percent': 100, 'repartition_type': 'tax', 'account_id': tax_final_account.id, 'tag_ids': [(6, 0, tax_tags['invoice']['tax'].ids)], }), ], 'refund_repartition_line_ids': [ (0, 0, { 'factor_percent': 100, 'repartition_type': 'base', 'tag_ids': [(6, 0, tax_tags['refund']['base'].ids)], }), (0, 0, { 'factor_percent': 100, 'repartition_type': 'tax', 'account_id': tax_final_account.id, 'tag_ids': [(6, 0, tax_tags['refund']['tax'].ids)], }), ], }) # create invoice move_form = Form(self.env['account.move'].with_context( default_move_type='out_refund')) move_form.partner_id = self.partner_a move_form.invoice_date = fields.Date.from_string('2017-01-01') with move_form.invoice_line_ids.new() as line_form: line_form.product_id = self.product_a line_form.tax_ids.clear() line_form.tax_ids.add(tax) invoice = move_form.save() invoice.action_post() # make payment self.env['account.payment.register'].with_context( active_model='account.move', active_ids=invoice.ids).create({ 'payment_date': invoice.date, })._create_payments() # check caba move partial_rec = invoice.mapped('line_ids.matched_debit_ids') caba_move = self.env['account.move'].search([('tax_cash_basis_rec_id', '=', partial_rec.id)]) expected_values = [ { 'tax_line_id': False, 'tax_repartition_line_id': False, 'tax_ids': [], 'tax_tag_ids': [], 'account_id': tax_base_amount_account.id, 'debit': 0.0, 'credit': 1000.0, }, { 'tax_line_id': False, 'tax_repartition_line_id': False, 'tax_ids': tax.ids, 'tax_tag_ids': tax_tags['refund']['base'].ids, 'account_id': tax_base_amount_account.id, 'debit': 1000.0, 'credit': 0.0, }, { 'tax_line_id': False, 'tax_repartition_line_id': False, 'tax_ids': [], 'tax_tag_ids': [], 'account_id': tax_waiting_account.id, 'debit': 0.0, 'credit': 100.0, }, { 'tax_line_id': tax.id, 'tax_repartition_line_id': tax.refund_repartition_line_ids.filtered( lambda x: x.repartition_type == 'tax').id, 'tax_ids': [], 'tax_tag_ids': tax_tags['refund']['tax'].ids, 'account_id': tax_final_account.id, 'debit': 100.0, 'credit': 0.0, }, ] self.assertRecordValues(caba_move.line_ids, expected_values) # unreconcile credit_aml = invoice.line_ids.filtered('credit') credit_aml.remove_move_reconcile() # check caba move reverse is same as caba move with only debit/credit inverted reversed_caba_move = self.env['account.move'].search([ ('reversed_entry_id', '=', caba_move.id) ]) for value in expected_values: value.update({ 'debit': value['credit'], 'credit': value['debit'], }) self.assertRecordValues(reversed_caba_move.line_ids, expected_values)