Creating Journal Entries in Odoo 16
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
Request Your Free Quote
In this code, we create a petty cash module.
First of all, we declare all the fields. For creating a journal entry, we need to add three fields. The first one is the petty cash account of the user. The second one is the credit account is the account the user selects and the last one is the journal entry field which is filled with the journal entry created. So our need is to create a journal entry when the approve button clicks. When the approve button clicks, first check if there is value in a petty cash account and credit account. If there is no value, it will throw a raised error in entering a value in the credit account and petty cash account.
If there is value in both fields, the amount is debited to a petty cash account and the amount is credited from the credit account. This is actually a process related to accounting. So a journal entry is created with both a debit account and credit account and that value will write on the field journal entry id. So how the journal entry creation takes place through code?
Petty cash workflow
First, the values are appended to a list. Then we perform a search in the model account. Journal records with names equal to miscellaneous operations. Then create a record in account move with values today date is passed in the date field, list values are passed on to the line of account move, journal id is passed in journal id and entry type is passed in the type field. Then give the record of the action post for automatically posting the journal entry when it is created. Thus journal entries in Odoo are created according to the petty cash workflow in this blog.
By running the code, the debit account is passed as the petty cash account id of the custodian. And the credit account value is passed as the credit account field value that the user selects.
DOWNLOAD ERP
"Unlock the Full Potential of Your Business with Odoo ERP!"
"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"
Get a Free Quote