diff options
Diffstat (limited to 'fixco_custom')
| -rw-r--r-- | fixco_custom/models/upload_ginee.py | 142 | ||||
| -rwxr-xr-x | fixco_custom/views/detail_order.xml | 4 | ||||
| -rw-r--r-- | fixco_custom/views/upload_ginee.xml | 10 |
3 files changed, 106 insertions, 50 deletions
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 @@ <button name="execute_queue" string="Create Detail Order" type="object" - attrs="{'invisible': [('detail_order', '!=', False)]}" + attrs="{'invisible': [('detail_order', '=', False)]}" /> <button name="execute_queue_detail" string="Create SO" type="object" - attrs="{'invisible': [('sale_id', '!=', False)]}" + attrs="{'invisible': [('sale_id', '=', False)]}" /> </header> <sheet> diff --git a/fixco_custom/views/upload_ginee.xml b/fixco_custom/views/upload_ginee.xml index ec8aae4..de99c3d 100644 --- a/fixco_custom/views/upload_ginee.xml +++ b/fixco_custom/views/upload_ginee.xml @@ -9,6 +9,7 @@ <field name="number"/> <field name="date_upload"/> <field name="user_id"/> + <field name="upload_type"/> </tree> </field> </record> @@ -19,10 +20,10 @@ <field name="arch" type="xml"> <form string="Upload Ginee"> <header> - <button name="action_import_excel" string="Import Excel" type="object" class="oe_highlight"/> - <button name="action_get_order_id" string="Get Order ID" type="object" class="oe_highlight"/> - <button name="action_create_detail_order" string="Create Detail Order" type="object" class="oe_highlight"/> - <button name="action_get_order_id_and_create_detail_order" string="Get Order ID And Create Detail Order" type="object" class="oe_highlight"/> + <button name="action_import_excel" string="Import Excel" type="object" class="oe_highlight" attrs="{'invisible': [('number', '=', False)]}"/> + <button name="action_get_order_id" string="Get Order ID" type="object" class="oe_highlight" attrs="{'invisible': [('number', '=', False)]}"/> + <button name="action_create_detail_order" string="Create Detail Order" type="object" class="oe_highlight" attrs="{'invisible': [('number', '=', False)]}"/> + <button name="action_get_order_id_and_create_detail_order" string="Get Order ID And Create Detail Order" type="object" class="oe_highlight" attrs="{'invisible': [('number', '=', False)]}"/> <field name="number" widget="field_no_edit" options="{'no_open': True}"/> <field name="date_upload"/> <field name="user_id" widget="field_no_edit" options="{'no_open': True}"/> @@ -31,6 +32,7 @@ <group> <field name="excel_file" filename="filename"/> <field name="filename" invisible="1"/> + <field name="upload_type" required="1"/> </group> <field name="ginee_lines"> <tree editable="bottom"> |
