From 87f6bc09d6fe91526116301375efc544f31be625 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Thu, 26 Jun 2025 11:20:25 +0700 Subject: upload_type and validation import excel --- fixco_custom/models/upload_ginee.py | 142 +++++++++++++++++++++++++----------- fixco_custom/views/detail_order.xml | 4 +- fixco_custom/views/upload_ginee.xml | 10 ++- 3 files changed, 106 insertions(+), 50 deletions(-) (limited to 'fixco_custom') diff --git a/fixco_custom/models/upload_ginee.py b/fixco_custom/models/upload_ginee.py index cfab74a..c19fa03 100644 --- a/fixco_custom/models/upload_ginee.py +++ b/fixco_custom/models/upload_ginee.py @@ -27,6 +27,10 @@ class UploadGinee(models.Model): user_id = fields.Many2one('res.users', 'Created By', default=lambda self: self.env.user.id) excel_file = fields.Binary('Excel File', attachment=True) filename = fields.Char('File Name') + upload_type = fields.Selection([ + ('rehit', 'Rehit'), + ('blibli', 'Blibli'), + ], 'Upload Type') @api.model def create(self, vals): @@ -55,27 +59,76 @@ class UploadGinee(models.Model): shop_col = header.index('shop') invoice_col = header.index('invoice') - line_vals_list = [] - for row in range(1, sheet.nrows): + # Store rows for validation and processing + rows_data = [] + for row_idx in range(1, sheet.nrows): try: - marketplace = str(sheet.cell(row, marketplace_col).value).strip() - shop = str(sheet.cell(row, shop_col).value).strip() - invoice_marketplace = str(sheet.cell(row, invoice_col).value).strip() - - line_vals = { - 'marketplace': marketplace, - 'shop': shop, - 'invoice_marketplace': invoice_marketplace, - 'upload_ginee_id': self.id, - } - line_vals_list.append((0, 0, line_vals)) + marketplace = str(sheet.cell(row_idx, marketplace_col).value).strip() + shop = str(sheet.cell(row_idx, shop_col).value).strip() + invoice_marketplace = str(sheet.cell(row_idx, invoice_col).value).strip() + rows_data.append((row_idx + 1, marketplace, shop, invoice_marketplace)) # +1 for 1-based Excel row except Exception as e: continue + # Validasi 1: Jika ada BLIBLI_ID di Marketplace, upload_type harus blibli + has_blibli_id = any("BLIBLI_ID" in row[1].upper() for row in rows_data) + if has_blibli_id and self.upload_type != 'blibli': + raise ValidationError(_("Excel contains BLIBLI_ID in Marketplace. Please select Blibli as the upload type.")) + + # Validasi 2: Untuk upload_type blibli, semua Marketplace harus mengandung BLIBLI_ID + if self.upload_type == 'blibli': + invalid_rows = [] + for row_data in rows_data: + row_num = row_data[0] + marketplace = row_data[1] + if "BLIBLI_ID" not in marketplace.upper(): + invalid_rows.append(str(row_num)) + + if invalid_rows: + error_msg = _("For Blibli uploads, 'Marketplace' must contain 'BLIBLI_ID'. Errors in rows: %s") + raise ValidationError(error_msg % ", ".join(invalid_rows)) + + # Validasi 3: Cek duplikat invoice_marketplace di sistem + existing_invoices = set() + invoice_to_check = [row_data[3] for row_data in rows_data] + + # Search in chunks to avoid too long SQL queries + chunk_size = 500 + for i in range(0, len(invoice_to_check), chunk_size): + chunk = invoice_to_check[i:i+chunk_size] + existing_records = self.env['upload.ginee.line'].search([ + ('invoice_marketplace', 'in', chunk) + ]) + existing_invoices.update(existing_records.mapped('invoice_marketplace')) + + duplicate_rows = [] + for row_data in rows_data: + row_num = row_data[0] + invoice_marketplace = row_data[3] + if invoice_marketplace in existing_invoices: + duplicate_rows.append(str(row_num)) + + if duplicate_rows: + error_msg = _("Invoice Marketplace already exists in the system. Duplicates found in rows: %s") + raise ValidationError(error_msg % ", ".join(duplicate_rows)) + + # Prepare lines for import + line_vals_list = [] + for row_data in rows_data: + marketplace = row_data[1] + shop = row_data[2] + invoice_marketplace = row_data[3] + line_vals = { + 'marketplace': marketplace, + 'shop': shop, + 'invoice_marketplace': invoice_marketplace, + 'upload_ginee_id': self.id, + } + line_vals_list.append((0, 0, line_vals)) + + # Update record self.ginee_lines.unlink() - self.write({ - 'ginee_lines': line_vals_list, - }) + self.write({'ginee_lines': line_vals_list}) return { 'type': 'ir.actions.client', @@ -157,32 +210,32 @@ class UploadGineeLine(models.Model): return response.json() def create_so_and_detail_order(self): - try: - for rec in self: - if not rec.order_id: - raise UserError(_('Order ID is empty!')) - - if self.env['detail.order'].search([('detail_order', 'ilike', rec.order_id)]): - raise UserError(_( - "Order ID %s already exists in Detail Orders") % rec.order_id) - - data = self._call_api(BATCH_GET_URI, {"orderIds": [rec.order_id]}) - detail_order = self.env['detail.order'].create({ - 'detail_order': json.dumps(data, indent=4), - 'source': 'manual', - }) - detail_order.execute_queue_detail() - - rec.update({ - 'message_error': 'Success', - 'detail_order_id': detail_order.id - }) - except Exception as e: - self.message_error = str(e) + for rec in self: + try: + if not rec.order_id: + raise UserError(_('Order ID is empty!')) + + if self.env['detail.order'].search([('detail_order', 'ilike', rec.order_id)]): + raise UserError(_( + "Order ID %s already exists in Detail Orders") % rec.order_id) + + data = self._call_api(BATCH_GET_URI, {"orderIds": [rec.order_id]}) + detail_order = self.env['detail.order'].create({ + 'detail_order': json.dumps(data, indent=4), + 'source': 'manual', + }) + detail_order.execute_queue_detail() + + rec.update({ + 'message_error': 'Success', + 'detail_order_id': detail_order.id + }) + except Exception as e: + rec.message_error = str(e) def get_order_id(self): - try: - for rec in self: + for rec in self: + try: if self.search_count([ ('marketplace', '=', rec.marketplace), ('invoice_marketplace', '=', rec.invoice_marketplace), @@ -191,7 +244,7 @@ class UploadGineeLine(models.Model): raise UserError(_( "Invoice %s already exists") % rec.invoice_marketplace) - data = self._call_api( + data = rec._call_api( LIST_ORDER_URI, { "channel": rec.marketplace, @@ -204,6 +257,7 @@ class UploadGineeLine(models.Model): rec.order_id = orders[0].get('orderId') rec.message_error = 'Success' else: - raise UserError(_("No orders found for invoice: %s") % self.invoice_marketplace) - except Exception as e: - self.message_error = str(e) \ No newline at end of file + raise UserError(_("No orders found for invoice: %s") % rec.invoice_marketplace) + + except Exception as e: + rec.message_error = str(e) \ No newline at end of file diff --git a/fixco_custom/views/detail_order.xml b/fixco_custom/views/detail_order.xml index beb6d5c..586e10d 100755 --- a/fixco_custom/views/detail_order.xml +++ b/fixco_custom/views/detail_order.xml @@ -27,12 +27,12 @@