Skip to Content
Menu
This question has been flagged
2 Replies
4357 Views

Hello, could someone help me with the creation of journal entries?

I am creating a custom module and I want to create journal entries with a button, the problem I have is that if there is more than one line or product the values of my journal entries are always those of the first line or product, for example if I have 3 products 3 journal items are created but all take the same value which would be the first product.


this is my PY


    def action_entry(self):
for rec in self:
for line in rec.avg_landed_cost_lines:
debit = line.average_landed_cost
credit = line.average_landed_cost
move = {
'name': '/',
'journal_id': rec.journal_id.id,
'date': rec.fecha,
'ref': rec.name,

'line_ids': [(0, 0, {
'name': line.product_id.name or '/',
'debit': debit,
'account_id': line.product_id.property_account_expense_id.id,
'partner_id': rec.order_id.partner_id.id,
}), (0, 0, {
'name': line.product_id.name or '/',
'credit': credit,
'account_id': line.product_id.property_account_income_id.id,
'partner_id': rec.order_id.partner_id.id,
})]
}
move_id = line.env['account.move'].create(move)
move_id.post()
return rec.write({'state': 'progress', 'move_id': move_id.id})


thanks!

Avatar
Discard
Best Answer

Hi Pamela Castaneda,

Your line_ids is not updated. Please try this code. 

def action_entry(self):
for rec in self:
move = {
'name': '/',
'journal_id': rec.journal_id.id,
'date': rec.fecha,
'ref': rec.name,
}
line_ids = []
for line in rec.avg_landed_cost_lines:
debit = line.average_landed_cost
credit = line.average_landed_cost

line_ids += [(0, 0, {
'name': line.product_id.name or '/',
'debit': debit,
'account_id': line.product_id.property_account_expense_id.id,
'partner_id': rec.order_id.partner_id.id
}), (0, 0, {
'name': line.product_id.name or '/',
'credit': credit,
'account_id': line.product_id.property_account_income_id.id,
'partner_id': rec.order_id.partner_id.id
})
]
if line_ids:
move.update({'line_ids': line_ids})
move_id = line.env['account.move'].create(move)
move_id.post()
rec.write({'state': 'progress', 'move_id': move_id.id})
return True



Hope it will help you.

Avatar
Discard
Best Answer

Hi ,

You have not added the line product amount in you code just add line items amount field it will work . please refer the code below and re format the code.

def action_entry(self):
for rec in self:
for line in rec.avg_landed_cost_lines: 
 
move = {
'name': '/',
'journal_id': rec\\.journal_id\\.id,
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'date':\\ rec\\.fecha,
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'ref':\\ rec\\.name,

\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'line_ids':\\ \\[\\(0,\\ 0,\\ \\{
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'name':\\ line\\.product_id\\.name\\ or\\ '/',
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'debit'_=\\ line\\.average_landed_cost 
\\ \\ \\ \\ \\ \\ 'credit'\\ =\\ line\\.average_landed_cost
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'account_id':\\ line\\.product_id\\.property_account_expense_id\\.id,
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'partner_id':\\ rec\\.order_id\\.partner_id\\.id,
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\}\\),\\ \\(0,\\ 0,\\ \\{
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'name':\\ line\\.product_id\\.name\\ or\\ '/',
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'debit'_=\\ line\\.average_landed_cost 
\\ \\ \\ \\ \\ \\ 'credit'\\ =\\ line\\.average_landed_cost
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'account_id':\\ line\\.product_id\\.property_account_income_id\\.id,
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ 'partner_id':\\ rec\\.order_id\\.partner_id\\.id,
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\}\\)\\]
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\}
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ move_id\\ =\\ line\\.env\\['account\\.move'\\]\\.create\\(move\\)
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ move_id\\.post\\(\\)
\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ return\\ rec\\.write\\(\\{'state':\\ 'progress',\\ 'move_id':\\ move_id.id})

Avatar
Discard
Related Posts Replies Views Activity
1
Jun 22
6757
1
Sep 21
2037
0
Aug 21
95
0
Jul 21
172
0
May 21
955