Slink/sos_inventory/wizard/sfg_bom_bulk_upload_wizard.py

81 lines
3.0 KiB
Python
Executable File

import base64
from odoo import models, fields, api
from openpyxl import load_workbook
from io import BytesIO
class BulkUploadWizard(models.TransientModel):
_name = 'bulk_upload_wizard'
_description = 'Wizard for Bulk Uploading Materials'
upload_file = fields.Binary("Upload File", required=True)
sos_sfg_id = fields.Many2one('sos_sfg_bom', string="Related SFG")
def upload_action(self):
file_content = base64.b64decode(self.upload_file)
file_stream = BytesIO(file_content)
workbook = load_workbook(file_stream)
worksheet = workbook.active
sfg_record = self.sos_sfg_id.id
missing_components = []
for row in worksheet.iter_rows(min_row=2, values_only=True): # Adjust min_row based on your file structure
try:
# Skip rows with None or NA values
if any(cell is None or cell == 'NA' for cell in row):
continue
material_name, qty = row
component = self.env['sos_material'].search([('part_no', '=', str(material_name).strip())])
sfg_bom_line_model = self.env['sos_sfg_bom_line']
if component:
sfg_bom_line_model.create({'bom_id': sfg_record,
'primary_component_id':component.id,
'quantity':int(qty)
})
else:
missing_components.append({'name': material_name, 'quantity': qty})
except Exception as e:
# Log the error and continue with the next row
print(f"Error processing row {row}: {e}")
continue
# Implement your logic for handling missing components here
if missing_components:
# Log or create action for missing components
return self.create_wizard_with_lines(missing_components, self.id)
return {
'type': 'ir.actions.client',
'tag': 'reload'
}
@api.model
def create_wizard_with_lines(self, missing_components, bulk_wizard_id):
# Create line data for missing components
line_data = [
{
'missing_component_name': component['name'],
'missing_component_quantity': component['quantity'] # Assuming you have this field in your line model
} for component in missing_components
]
# Create the wizard and lines in one go
wizard = self.env['missing_component_wizard'].create({
'missing_component_line_ids': [(0, 0, line) for line in line_data]
})
view_id = self.env.ref('sos_inventory.view_missing_component_wizard_form').id
return {
'type': 'ir.actions.act_window',
'name': 'Handle Missing Components',
'view_mode': 'form',
'res_model': 'missing_component_wizard',
'view_id': view_id,
'target': 'new',
'res_id': wizard.id,
'context': {
'default_sos_sfg_id': self.id
}
}