dev-resources.site
for different kinds of informations.
Odoo: Import CSV file and write into sale order lines
Published at
10/28/2024
Categories
Author
Jeevachaithanyan Sivanandan
Categories
1 categories in total
open
Odoo: Import CSV file and write into sale order lines
The method features:
- read an imported csv file
- check some validations
- write the values from the csv file into the sale order line and sale order
def action_import_csv(self):
if not self.file_name or not self.file_name.lower().endswith('.csv'):
raise UserError("Invalid file format! Please upload a CSV file.")
try:
csv_data = base64.b64decode(self.csv_file_upload).decode('utf-8')
csv_reader = csv.DictReader(StringIO(csv_data))
except Exception:
raise UserError("Failed to read CSV file. Ensure the file is properly formatted.")
sale_order = self.get_current_sale_order()
partner_id = None
updated_lines = {}
for i, row in enumerate(csv_reader):
if i == 0:
row_partner_id = int(row.get("Partner ID", 0) or 0)
partner_id = row_partner_id
if sale_order.partner_id.id != partner_id:
raise UserError("The Partner ID in the CSV does not match the Sale Order's Partner.")
elif row.get("Partner ID"):
raise UserError("Only the first row should contain a Partner ID. Subsequent rows should leave this field empty.")
sku = row.get("Product ID", "").strip()
quantity = float(row.get("Product Quantity", 0))
if not sku:
raise UserError("Product ID (SKU) is missing in one of the rows.")
product = self.env['product.product'].search([('default_code', '=', sku)], limit=1)
if not product:
raise UserError(f"Product with SKU {sku} not found in the system.")
sale_order_line = self.env['sale.order.line'].create({
'order_id': sale_order.id,
'product_id': product.id,
'product_uom_qty': quantity,
})
updated_lines[sku] = quantity
sale_order.write({
'partner_invoice_id': sale_order.partner_id.address_get(['invoice'])['invoice'],
'partner_shipping_id': sale_order.partner_id.address_get(['delivery'])['delivery'],
})
return {
'type': 'ir.actions.client',
'tag': 'reload',
}
Articles
12 articles in total
Python Program for a Turing Machine Simulator python Copy code
read article
Install Odoo v 17 with Python 3.12 in MSWindows 11
read article
Boosting Code Quality in Odoo with Type Hints: Benefits Beyond Performance
read article
Odoo: Import CSV file and write into sale order lines
currently reading
Odoo : create CSV file and download
read article
Odoo : how to create a Wizard with download a file and auto close
read article
How to Configure Multiple Conditions in Odoo XML Views
read article
Odoo : Patch a Javascript class
read article
how to create new permission groups, permission category
read article
Odoo User Permission and groups
read article
extended unpacking in Python
read article
odoo v14 and issues with requirements.txt
read article
Featured ones: