Blog

News & Events
Creating Journal Entries in Odoo 16
December 13, 2022
Creating accounting entries or journal entries in Odoo using code:
For example:
If we want, to generate a journal entry in petty cash module customization.
Petty cash:
When a company gives cash to the allowed person to meet the expenses of the company, it is known as petty cash. So in the petty cash customization, there will be two accounts. The first one is the main account from which the money will be credited to the account for petty cash and the second one is the petty cash account. So a journal entry will be created including the two accounts.
In the example, we can see there is a user and custodian in which the employee is related to the particular user. Then they give two fields for the two accounts. For the first one, we have to select from which account the money will be credited. So just give the many2one field of account. Account. Then it will display all accounts. Next, we have the field for petty cash field. It should be automatically filled concurrently by entering the employee name through the change function.
So an example would be like this:
class PettyCashRequest(models.Model): _name = 'petty.cash.request' _description = 'Petty Cash Request' _rec_name = 'name' state = fields.Selection([ ('draft', 'Draft'), ('submit', 'Submitted'), ('review','Reviewed'), ('approve','Approved') ],'Status', readonly=True, copy=False, default='draft') name=fields.Char('NewOrderReference',required=True,index=True,copy=False,default='New') user_id = fields.Many2one('res.users',string = "User") employee_id = fields.Many2one('hr.employee',string ="Custodian") description = fields.Char(string ="Description") amount = fields.Float("Petty Cash Amount") date = fields.Date(default=fields.Date.today()) user_petty_cash_account_id = fields.Many2one('account.account',string="Petty Cash Account") journal_entry_id = fields.Many2one('account.move' ,string ="Journal Entry" ) credit_account_id = fields.Many2one('account.account',string="Credit Account") @api.onchange('user_id') def _onchange_user_id(self): for rec in self: if rec.user_id: emp_obj = self.env['hr.employee'].search([('user_id','=',rec.user_id.id)]) rec.employee_id = emp_obj.id if emp_obj.petty_cash_id: rec.user_petty_cash_account_id = emp_obj.petty_cash_id.id def button_submit(self): for rec in self: rec.state = "submit" def button_review(self): for rec in self: rec.state = "review" def button_approve(self): for rec in self: if not rec.user_petty_cash_account_id and rec.credit_account_id: raise UserError(_('Please Enter Debit Account and Credit Account')) list_val = [] list_val.append( ( 0, 0, { 'account_id' : rec.user_petty_cash_account_id.id, 'debit' : rec.amount, 'credit' : 0.0 }, ) ) list_val.append( ( 0, 0, { 'account_id' : rec.credit_account_id.id, 'credit' : rec.amount, 'debit' : 0.0 }, ) ) journal_id = self.env['account.journal'].search([('name', '=', 'Miscellaneous Operations')]) record = self.env['account.move'].create({'date' : date.today() , 'line_ids' : list_val , 'journal_id' : journal_id.id , 'type' : 'entry'}) record.action_post() rec.write({'state' : 'approve' , "journal_entry_id" : record.id}) @api.model def create(self, vals): if vals.get('name', 'New') == 'New': vals['name'] = self.env['ir.sequence'].next_by_code('petty.cash.request.sequence') or 'New' result = super(PettyCashRequest, self).create(vals) return result