We used a 3rd party app for Freight Management and it used the default invoice template without activating the Accounting module of Odoo for the Exports and Imports. I have modified the code to have a new model for Cartage and this is used by another (sister) company and based the new invoicing method snippet on the original. So far there is no error in the code, but more on the setting for the product/service. The company for cartage is invoicing its sisters company. The error so far we are encountering is "Invalid Operation: Any journal item on a receivable account must have a due date and vice versa." I do not know if our code is the issue or we really have config issues. Where should I enter the due date and vice-versa?
Here is the code snippet:
def action_create_invoice(self):
current_company = self.env.company
cartage_charges = self.env['cartage.service.charge'].with_company(current_company).search([
('commissioned_vehicle_id', '=', self.id),
('invoiced', '=', False)
])
if not cartage_charges:
raise UserError("No cartage service charges found to invoice.")
bill_cartage_list = cartage_charges.mapped('bill_cartage')
sale_journal = self.env['account.journal'].search([
('company_id', '=', current_company.id),
('type', '=', 'sale')
], limit=1)
if not sale_journal:
raise UserError(
f"Configuration Error: No Sales Journal of type 'sale' found for company '{current_company.name}'."
)
for bill_cartage in set(bill_cartage_list):
partner = self._get_partner_from_bill_cartage(bill_cartage)
charges_to_invoice = cartage_charges.filtered(
lambda c: c.bill_cartage == bill_cartage and not c.invoiced
)
if not charges_to_invoice:
continue
_logger.info(f"DEBUG: Creating invoice for bill_cartage: {bill_cartage}")
_logger.info(f"DEBUG: Partner: {partner.name} (ID: {partner.id})")
_logger.info(f"DEBUG: Current Company: {current_company.name} (ID: {current_company.id})")
_logger.info(f"DEBUG: Sale Journal: {sale_journal.name} (ID: {sale_journal.id})")
_logger.info(f"DEBUG: Number of charges to invoice: {len(charges_to_invoice)}")
# Build invoice lines
invoice_line_ids = []
for charge in charges_to_invoice:
if not charge.service_id:
raise UserError(f"Service ID is missing for charge.")
product = charge.service_id
# Verify income account exists
income_account = product.property_account_income_id or \
product.categ_id.property_account_income_categ_id
if not income_account:
raise UserError(
f"No income account configured for product '{product.display_name}'."
)
invoice_line_ids.append((0, 0, {
'product_id': product.id,
'name': charge.name or product.name,
'quantity': 1.0,
'price_unit': charge.amount_price,
}))
# Create invoice - Odoo will auto-compute payment terms from partner
invoice = self.env['account.move'].with_context(
default_move_type='out_invoice'
).create({
'partner_id': partner.id,
'move_type': 'out_invoice',
'invoice_date': fields.Date.today(),
'invoice_date_due': fields.Date.today(),
'commissioned_vehicle_id': self.id,
'company_id': current_company.id,
'journal_id': sale_journal.id,
'invoice_line_ids': invoice_line_ids,
})
# Mark as invoiced
charges_to_invoice.write({'invoiced': True})
return self.button_customer_invoices()