Search code examples
pythonodooodoo-14

when record created, it wont autofill record field


I'm trying to make autofill record from bank.account.account to account.journal but it seems it didn't work. I wonder where is the mistake in this code

class BankAccounAccount(models.Model):
    _name = 'bank.account.account'
    _description = "Bank Account Account"
    _rec_name = 'acc_number'

    acc_number = fields.Char(string="Account Number", required=True)
    bank_id = fields.Many2one('res.bank', string="Bank")
    bank_bic = fields.Char(string="Bank Identifier Code")
    company_id = fields.Many2one('res.company', string="Company", default=lambda self: self.env.user.company_id.id)
    branch_id = fields.Many2one('res.branch', string="Branch")

    @api.model
    def create(self, values):
        res = super(BankAccounAccount, self).create(values)
        for rec in self:
            values = {
                'acc_number': rec.acc_number.id,
                'bank_id': rec.bank_id.id,
                'bank_bic': rec.bank_bic.id,
                'company_id': rec.company_id.id,
                'branch_id': rec.branch_id.id, 
            }
            self.env['account.journal'].create(values)
        return res

class AccountJournal(models.Model):
    _inherit = "account.journal"

    name = fields.Char(string='Journal Name', required=True,
        tracking=True)
    code = fields.Char(string='Short Code', size=5, required=True, help="Shorter name used for display. The journal entries of this journal will also be named using this prefix by default.",
        tracking=True)
    active = fields.Boolean(default=True, help="Set active to false to hide the Journal without removing it.",
        tracking=True)
    type = fields.Selection([
            ('sale', 'Sales'),
            ('purchase', 'Purchase'),
            ('cash', 'Cash'),
            ('bank', 'Bank'),
            ('general', 'Miscellaneous'),
        ], required=True,
        help="Select 'Sale' for customer invoices journals.\n"\
        "Select 'Purchase' for vendor bills journals.\n"\
        "Select 'Cash' or 'Bank' for journals that are used in customer or vendor payments.\n"\
        "Select 'General' for miscellaneous operations journals.",
        tracking=True)
    type_control_ids = fields.Many2many('account.account.type', 'journal_account_type_control_rel', 'journal_id', 'type_id', string='Allowed account types',
        tracking=True)
    account_control_ids = fields.Many2many('account.account', 'journal_account_control_rel', 'journal_id', 'account_id', string='Allowed accounts',
        check_company=True,
        domain="[('deprecated', '=', False), ('company_id', '=', company_id), ('is_off_balance', '=', False)]",
        tracking=True)
    default_account_type = fields.Many2one('account.account.type', compute="_compute_default_account_type",
        tracking=True)
    default_account_id = fields.Many2one(
        comodel_name='account.account', check_company=True, copy=False, ondelete='restrict',
        string='Default Account',
        domain="[('deprecated', '=', False), ('company_id', '=', company_id),"
               "'|', ('user_type_id', '=', default_account_type), ('user_type_id', 'in', type_control_ids),"
               "('user_type_id.type', 'not in', ('receivable', 'payable'))]",
        tracking=True)
    payment_debit_account_id = fields.Many2one(
        comodel_name='account.account', check_company=True, copy=False, ondelete='restrict',
        help="Incoming payments entries triggered by invoices/refunds will be posted on the Outstanding Receipts Account "
             "and displayed as blue lines in the bank reconciliation widget. During the reconciliation process, concerned "
             "transactions will be reconciled with entries on the Outstanding Receipts Account instead of the "
             "receivable account.", string='Outstanding Receipts Account',
        domain=lambda self: "[('deprecated', '=', False), ('company_id', '=', company_id), \
                             ('user_type_id.type', 'not in', ('receivable', 'payable')), \
                             '|', ('user_type_id', '=', %s), ('id', '=', default_account_id)]" % self.env.ref('account.data_account_type_current_assets').id,
        tracking=True)
    payment_credit_account_id = fields.Many2one(
        comodel_name='account.account', check_company=True, copy=False, ondelete='restrict',
        help="Outgoing payments entries triggered by bills/credit notes will be posted on the Outstanding Payments Account "
             "and displayed as blue lines in the bank reconciliation widget. During the reconciliation process, concerned "
             "transactions will be reconciled with entries on the Outstanding Payments Account instead of the "
             "payable account.", string='Outstanding Payments Account',
        domain=lambda self: "[('deprecated', '=', False), ('company_id', '=', company_id), \
                             ('user_type_id.type', 'not in', ('receivable', 'payable')), \
                             '|', ('user_type_id', '=', %s), ('id', '=', default_account_id)]" % self.env.ref('account.data_account_type_current_assets').id,
        tracking=True)
    suspense_account_id = fields.Many2one(
        comodel_name='account.account', check_company=True, ondelete='restrict', readonly=False, store=True,
        compute='_compute_suspense_account_id',
        help="Bank statements transactions will be posted on the suspense account until the final reconciliation "
             "allowing finding the right account.", string='Suspense Account',
        domain=lambda self: "[('deprecated', '=', False), ('company_id', '=', company_id), \
                             ('user_type_id.type', 'not in', ('receivable', 'payable')), \
                             ('user_type_id', '=', %s)]" % self.env.ref('account.data_account_type_current_liabilities').id,
        tracking=True)
    restrict_mode_hash_table = fields.Boolean(string="Lock Posted Entries with Hash",
        help="If ticked, the accounting entry or invoice receives a hash as soon as it is posted and cannot be modified anymore.",
        tracking=True)
    sequence = fields.Integer(help='Used to order Journals in the dashboard view', default=10,
        tracking=True)

    invoice_reference_type = fields.Selection(string='Communication Type', required=True, selection=[('none', 'Free'), ('partner', 'Based on Customer'), ('invoice', 'Based on Invoice')], default='invoice', help='You can set here the default communication that will appear on customer invoices, once validated, to help the customer to refer to that particular invoice when making the payment.',
        tracking=True)
    invoice_reference_model = fields.Selection(string='Communication Standard', required=True, selection=[('odoo', 'Odoo'),('euro', 'European')], default=_default_invoice_reference_model, help="You can choose different models for each type of reference. The default one is the Odoo reference.",
        tracking=True)

    #groups_id = fields.Many2many('res.groups', 'account_journal_group_rel', 'journal_id', 'group_id', string='Groups')
    currency_id = fields.Many2one('res.currency', help='The currency used to enter statement', string="Currency",
        tracking=True)
    company_id = fields.Many2one('res.company', string='Company', required=True, readonly=True, index=True, default=lambda self: self.env.company,
        help="Company related to this journal",
        tracking=True)
    country_code = fields.Char(related='company_id.country_id.code', readonly=True,
        tracking=True)

    refund_sequence = fields.Boolean(string='Dedicated Credit Note Sequence', help="Check this box if you don't want to share the same sequence for invoices and credit notes made from this journal", default=False,
        tracking=True)
    sequence_override_regex = fields.Text(help="Technical field used to enforce complex sequence composition that the system would normally misunderstand.\n"\
                                          "This is a regex that can include all the following capture groups: prefix1, year, prefix2, month, prefix3, seq, suffix.\n"\
                                          "The prefix* groups are the separators between the year, month and the actual increasing sequence number (seq).\n"\

                                          "e.g: ^(?P<prefix1>.*?)(?P<year>\d{4})(?P<prefix2>\D*?)(?P<month>\d{2})(?P<prefix3>\D+?)(?P<seq>\d+)(?P<suffix>\D*?)$",
        tracking=True)

    inbound_payment_method_ids = fields.Many2many(
        comodel_name='account.payment.method',
        relation='account_journal_inbound_payment_method_rel',
        column1='journal_id',
        column2='inbound_payment_method',
        domain=[('payment_type', '=', 'inbound')],
        string='Inbound Payment Methods',
        compute='_compute_inbound_payment_method_ids',
        store=True,
        readonly=False,
        help="Manual: Get paid by cash, check or any other method outside of Odoo.\n"
             "Electronic: Get paid automatically through a payment acquirer by requesting a transaction"
             " on a card saved by the customer when buying or subscribing online (payment token).\n"
             "Batch Deposit: Encase several customer checks at once by generating a batch deposit to"
             " submit to your bank. When encoding the bank statement in Odoo, you are suggested to"
             " reconcile the transaction with the batch deposit. Enable this option from the settings.",
        tracking=True
    )
    outbound_payment_method_ids = fields.Many2many(
        comodel_name='account.payment.method',
        relation='account_journal_outbound_payment_method_rel',
        column1='journal_id',
        column2='outbound_payment_method',
        domain=[('payment_type', '=', 'outbound')],
        string='Outbound Payment Methods',
        compute='_compute_outbound_payment_method_ids',
        store=True,
        readonly=False,
        help="Manual: Pay bill by cash or any other method outside of Odoo.\n"
             "Check: Pay bill by check and print it from Odoo.\n"
             "SEPA Credit Transfer: Pay bill from a SEPA Credit Transfer file you submit to your"
             " bank. Enable this option from the settings.",
        tracking=True
    )
    at_least_one_inbound = fields.Boolean(compute='_methods_compute', store=True,
        tracking=True)
    at_least_one_outbound = fields.Boolean(compute='_methods_compute', store=True,
        tracking=True)
    profit_account_id = fields.Many2one(
        comodel_name='account.account', check_company=True,
        help="Used to register a profit when the ending balance of a cash register differs from what the system computes",
        string='Profit Account',
        domain=lambda self: "[('deprecated', '=', False), ('company_id', '=', company_id), \
                             ('user_type_id.type', 'not in', ('receivable', 'payable')), \
                             ('user_type_id', 'in', %s)]" % [self.env.ref('account.data_account_type_revenue').id,
                                                             self.env.ref('account.data_account_type_other_income').id],
        tracking=True)
    loss_account_id = fields.Many2one(
        comodel_name='account.account', check_company=True,
        help="Used to register a loss when the ending balance of a cash register differs from what the system computes",
        string='Loss Account',
        domain=lambda self: "[('deprecated', '=', False), ('company_id', '=', company_id), \
                             ('user_type_id.type', 'not in', ('receivable', 'payable')), \
                             ('user_type_id', '=', %s)]" % self.env.ref('account.data_account_type_expenses').id,
        tracking=True)

    # Bank journals fields
    company_partner_id = fields.Many2one('res.partner', related='company_id.partner_id', string='Account Holder', readonly=True, store=False)
    bank_account_id = fields.Many2one('res.partner.bank',
        string="Bank Account",
        ondelete='restrict', copy=False,
        check_company=True,
        domain="[('partner_id','=', company_partner_id), '|', ('company_id', '=', False), ('company_id', '=', company_id)]",
        tracking=True)
    bank_statements_source = fields.Selection(selection=_get_bank_statements_available_sources, string='Bank Feeds', default='undefined', help="Defines how the bank statements will be registered",
        tracking=True)
    bank_acc_number = fields.Char(related='bank_account_id.acc_number', readonly=False,
        tracking=True)
    bank_id = fields.Many2one('res.bank', related='bank_account_id.bank_id', readonly=False,
        tracking=True)

    # Sale journals fields
    sale_activity_type_id = fields.Many2one('mail.activity.type', string='Schedule Activity', default=False, help="Activity will be automatically scheduled on payment due date, improving collection process.",
        tracking=True)
    sale_activity_user_id = fields.Many2one('res.users', string="Activity User", help="Leave empty to assign the Salesperson of the invoice.",
        tracking=True)
    sale_activity_note = fields.Text('Activity Summary',
        tracking=True)

    # alias configuration for journals
    alias_id = fields.Many2one('mail.alias', string='Email Alias', help="Send one separate email for each invoice.\n\n"
                                                                  "Any file extension will be accepted.\n\n"
                                                                  "Only PDF and XML files will be interpreted by Odoo", copy=False,
        tracking=True)
    alias_domain = fields.Char('Alias domain', compute='_compute_alias_domain', default=_default_alias_domain, compute_sudo=True,
        tracking=True)
    alias_name = fields.Char('Alias Name', copy=False, related='alias_id.alias_name', help="It creates draft invoices and bills by sending an email.", readonly=False,
        tracking=True)

    journal_group_ids = fields.Many2many('account.journal.group',
        domain="[('company_id', '=', company_id)]",
        check_company=True,
        string="Journal Groups",
        tracking=True)

    secure_sequence_id = fields.Many2one('ir.sequence',
        help='Sequence to use to ensure the securisation of data',
        check_company=True,
        readonly=True, copy=False,
        tracking=True)

Thanks

I've been stuck for weeks and already tried manipulating but none of them are working


Solution

  • To create account.journal record with bank type you need at least the below required fields (name, code, type, company_id).

    The code field in journal should be unique with size of 5 chars. I have added code field to your custom model and use it in create method.

    class BankAccounAccount(models.Model):
            _name = 'bank.account.account'
            _description = "Bank Account Account"
            _rec_name = 'acc_number'
    
            acc_number = fields.Char(string="Account Number", required=True)
            bank_id = fields.Many2one('res.bank', string="Bank")
            bank_bic = fields.Char(string="Bank Identifier Code")
            company_id = fields.Many2one('res.company', string="Company", default=lambda self: self.env.user.company_id.id)
            branch_id = fields.Many2one('res.branch', string="Branch")
            code = fields.Char(string='Short Code', size=5, required=True);  # You need to make sure that the entered code not already used by another journal
            
            @api.model
            def create(self, values):
                res = super(BankAccounAccount, self).create(values)
                for rec in res:
                    values = {
                        'company_id': rec.company_id.id,
                        'name': rec.acc_number,
                        'type':'bank',
                        'code': rec.code,
                        #'acc_number': rec.acc_number.id, # the acc_number field is not exists in 'account.journal' so you canot use it
                        #'bank_id': rec.bank_id.id,  # this bank_id field is related to bank_account_id.bank_id and bank_account_id is Many2One from 'res.partner.bank' 
                        #'bank_bic': rec.bank_bic.id, # the bank_bic field is not exists in 'account.journal' so you canot use it
                        #'branch_id': rec.branch_id.id, # the branch_id field is not exists in 'account.journal' unless 
                    }
                    self.env['account.journal'].create(values)  # make sure that the user you are using has access to account.journal unless use the below commented code
                    self.env['account.journal'].sudo().create(values)
                return res