# -*- coding: utf-8 -*- from odoo import api, fields, models, _ from odoo.exceptions import ValidationError class IntercompanySettlementWizard(models.TransientModel): _name = 'intercompany.settlement.wizard' _description = 'Intercompany Settlement Wizard' ho_company_id = fields.Many2one( 'res.company', string='Head Office', required=True, default=lambda self: self.env.company ) branch_company_id = fields.Many2one( 'res.company', string='Branch Company', required=True, domain="[('id', '!=', ho_company_id)]" ) amount = fields.Monetary( string='Settlement Amount', required=True ) currency_id = fields.Many2one( 'res.currency', string='Currency', related='ho_company_id.currency_id' ) date = fields.Date( string='Settlement Date', required=True, default=fields.Date.context_today ) ho_journal_id = fields.Many2one( 'account.journal', string='HO Bank/Cash Journal', required=True, domain="[('type', 'in', ('bank', 'cash')), ('company_id', '=', ho_company_id)]" ) branch_journal_id = fields.Many2one( 'account.journal', string='Branch Bank/Cash Journal', required=True, domain="[('type', 'in', ('bank', 'cash')), ('company_id', '=', branch_company_id)]" ) memo = fields.Char(string='Memo', default='Intercompany Settlement') def action_confirm_settlement(self): self.ensure_one() if self.amount <= 0: raise ValidationError(_("Settlement amount must be greater than zero.")) if not self.ho_company_id.intercompany_receivable_account_id: raise ValidationError(_("Please set an Intercompany Receivable Account on company %s.", self.ho_company_id.name)) if not self.branch_company_id.intercompany_payable_account_id: raise ValidationError(_("Please set an Intercompany Payable Account on company %s.", self.branch_company_id.name)) # 1. Branch Pays HO (Outbound Payment in Branch) # Dr Intercompany Payable, Cr Bank branch_payment_method = self.branch_journal_id._get_available_payment_method_lines('outbound').filtered(lambda x: x.code == 'manual')[:1] if not branch_payment_method: branch_payment_method = self.branch_journal_id._get_available_payment_method_lines('outbound')[:1] if not branch_payment_method: raise ValidationError(_("No payment method found for the branch journal.")) branch_payment_vals = { 'payment_type': 'outbound', 'partner_type': 'supplier', 'partner_id': self.ho_company_id.partner_id.id, 'amount': self.amount, 'currency_id': self.currency_id.id, 'date': self.date, 'journal_id': self.branch_journal_id.id, 'company_id': self.branch_company_id.id, 'payment_method_line_id': branch_payment_method.id, 'ref': self.memo, 'expense_account_id': self.branch_company_id.intercompany_payable_account_id.id, } branch_payment = self.env['account.payment'].sudo().with_company(self.branch_company_id).create(branch_payment_vals) branch_payment.action_post() # 2. HO Receives Payment (Inbound Payment in HO) # Dr Bank, Cr Intercompany Receivable ho_payment_method = self.ho_journal_id._get_available_payment_method_lines('inbound').filtered(lambda x: x.code == 'manual')[:1] if not ho_payment_method: ho_payment_method = self.ho_journal_id._get_available_payment_method_lines('inbound')[:1] if not ho_payment_method: raise ValidationError(_("No payment method found for the HO journal.")) ho_payment_vals = { 'payment_type': 'inbound', 'partner_type': 'customer', 'partner_id': self.branch_company_id.partner_id.id, 'amount': self.amount, 'currency_id': self.currency_id.id, 'date': self.date, 'journal_id': self.ho_journal_id.id, 'company_id': self.ho_company_id.id, 'payment_method_line_id': ho_payment_method.id, 'ref': self.memo, 'expense_account_id': self.ho_company_id.intercompany_receivable_account_id.id, } ho_payment = self.env['account.payment'].create(ho_payment_vals) ho_payment.action_post() return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': _('Settlement Successful'), 'message': _('Successfully settled %s from %s to %s.', self.amount, self.branch_company_id.name, self.ho_company_id.name), 'sticky': False, } }