1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
from odoo import models, fields, api, _
from datetime import datetime
import base64
import xlrd
from odoo.exceptions import ValidationError, UserError
import requests
import json
import hmac
from hashlib import sha256
API_BASE_URL = "https://api.ginee.com"
BATCH_GET_URI = '/openapi/order/v1/batch-get'
LIST_ORDER_URI = '/openapi/order/v2/list-order'
ACCESS_KEY = '24bb6a1ec618ec6a'
SECRET_KEY = '32e4a78ad05ee230'
class UploadGinee(models.Model):
_name = "upload.ginee"
_description = "Upload Ginee"
_order = "create_date desc"
_rec_name = "number"
ginee_lines = fields.One2many('upload.ginee.line', 'upload_ginee_id',
string='Lines', copy=False, auto_join=True)
number = fields.Char('Number', copy=False)
date_upload = fields.Datetime('Upload Date', copy=False)
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):
vals['number'] = self.env['ir.sequence'].next_by_code('upload.ginee') or '/'
return super().create(vals)
def action_import_excel(self):
self.ensure_one()
if not self.excel_file:
raise ValidationError(_("Please upload an Excel file first."))
try:
file_content = base64.b64decode(self.excel_file)
workbook = xlrd.open_workbook(file_contents=file_content)
sheet = workbook.sheet_by_index(0)
except:
raise ValidationError(_("Invalid Excel file format."))
header = [str(sheet.cell(0, col).value).strip().lower() for col in range(sheet.ncols)]
expected_headers = ['marketplace', 'shop', 'invoice']
if not all(h in header for h in expected_headers):
raise ValidationError(_("Invalid Excel format. Expected columns: Marketplace, Shop, Invoice"))
marketplace_col = header.index('marketplace')
shop_col = header.index('shop')
invoice_col = header.index('invoice')
# Store rows for validation and processing
rows_data = []
for row_idx in range(1, sheet.nrows):
try:
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), ('upload_ginee_id', '!=', False)
])
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})
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _('Success'),
'message': _('Imported %s lines from Excel.') % len(line_vals_list),
'sticky': False,
'next': {'type': 'ir.actions.act_window_close'},
}
}
def _show_notification(self, message):
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _('Success'),
'message': message,
'sticky': False,
}
}
def action_get_order_id(self):
self.date_upload = datetime.utcnow()
self.ginee_lines.get_order_id()
def action_create_detail_order(self):
self.date_upload = datetime.utcnow()
self.ginee_lines.create_so_and_detail_order()
# def action_get_order_id_and_create_detail_order(self):
# self.date_upload = datetime.utcnow()
# self.ginee_lines.get_order_id()
# self.ginee_lines.create_so_and_detail_order()
def action_get_order_id_and_create_detail_order(self):
self.date_upload = datetime.utcnow()
if self.upload_type == 'rehit':
for line in self.ginee_lines:
queue_job = self.env['queue.job'].search([('res_id', '=', line.id), ('method_name', '=', 'get_order_id_and_create_detail_order'), ('state', '!=', 'error')], limit=1)
if queue_job:
continue
self.env['queue.job'].create({
'name': f'Get Order Ginee {line.invoice_marketplace}',
'model_name': 'upload.ginee.line',
'method_name': 'get_order_id_and_create_detail_order',
'res_id': line.id,
})
else:
self.ginee_lines.get_order_id()
self.ginee_lines.create_so_and_detail_order()
class UploadGineeLine(models.Model):
_name = "upload.ginee.line"
_description = "Upload Ginee Line"
_inherit = ['mail.thread']
upload_ginee_id = fields.Many2one('upload.ginee', string='Upload')
marketplace = fields.Char('Marketplace')
shop = fields.Char('Shop')
invoice_marketplace = fields.Char('Invoice Marketplace')
order_id = fields.Char('Order ID')
message_error = fields.Text('Error Message')
detail_order_id = fields.Many2one('detail.order', string='Detail Order')
is_grouped = fields.Boolean('Is Grouped', default=False)
group_key = fields.Char('Group Key')
def get_order_id_and_create_detail_order(self):
self.get_order_id()
self.create_so_and_detail_order()
def _process_grouped_blibli_orders(self, lines):
"""Process a group of BLIBLI orders with the same invoice prefix"""
order_ids = [l.order_id for l in lines if l.order_id]
if not order_ids:
raise UserError(_('Order ID is empty for one or more records in group!'))
# ===== 1. Fast duplicate check (still same behavior) =====
existing_detail = self.env['detail.order'].search(
[('detail_order', 'ilike', order_ids[0])],
limit=1
)
if existing_detail:
return existing_detail
# ===== 2. Single API call =====
data = lines[0]._call_api(
BATCH_GET_URI,
{"orderIds": order_ids}
)
orders_data = data.get('data', [])
if not orders_data:
raise UserError(_('No data returned from BLIBLI API'))
# ===== 3. Combine items (lebih ringkas) =====
combined_items = [
item
for order in orders_data
for item in order.get('items', [])
]
base_order = orders_data[0]
# ===== 4. Build grouped payload =====
combined_json_data = {
'data': [{
**base_order,
'items': combined_items,
# 'externalOrderId': ', '.join(lines.mapped('invoice_marketplace')),
# 'orderId': ', '.join(order_ids),
'externalOrderId': ', '.join(l.invoice_marketplace for l in lines),
'orderId': ', '.join(l.order_id for l in lines),
}]
}
return self.env['detail.order'].create({
'detail_order': json.dumps(combined_json_data, indent=4),
'source': 'manual',
})
def _sign_request(self, uri):
"""Membuat tanda tangan sesuai format yang berhasil"""
sign_data = f'POST${uri}$'
signature = hmac.new(
SECRET_KEY.encode('utf-8'),
sign_data.encode('utf-8'),
digestmod=sha256
).digest()
return f"{ACCESS_KEY}:{base64.b64encode(signature).decode('ascii')}"
def _call_api(self, uri, payload):
"""Memanggil API dengan autentikasi yang benar"""
headers = {
'Content-Type': 'application/json',
'X-Advai-Country': 'ID',
'Authorization': self._sign_request(uri)
}
response = requests.post(
f"{API_BASE_URL}{uri}",
headers=headers,
data=json.dumps(payload)
)
if response.status_code != 200:
error_msg = f"API Error ({response.status_code}): {response.text}"
raise UserError(_(error_msg))
return response.json()
def create_so_and_detail_order(self):
grouped_lines = {}
# ===== 1. Grouping (lebih rapi & cepat) =====
for rec in self.filtered(lambda r: not r.detail_order_id):
if rec.upload_ginee_id.upload_type == 'blibli' and '-' in rec.invoice_marketplace:
key = rec.invoice_marketplace.split('-')[0]
else:
key = rec.id
grouped_lines.setdefault(key, []).append(rec)
# ===== 2. Preload sale.order (hindari query berulang) =====
invoice_list = [
line.invoice_marketplace
for lines in grouped_lines.values()
for line in lines
if len(lines) == 1
]
existing_so_map = {}
if invoice_list:
so_records = self.env['sale.order'].search([
('invoice_mp', 'in', invoice_list)
])
for so in so_records:
existing_so_map.setdefault(so.invoice_mp, []).append(so.name)
# ===== 3. Process per group =====
for _, lines in grouped_lines.items():
try:
# ===== GROUPED BLIBLI =====
if len(lines) > 1:
detail_order = self._process_grouped_blibli_orders(lines)
detail_order.execute_queue_detail()
for line in lines:
line.write({
'message_error': 'Success (grouped)',
'detail_order_id': detail_order.id
})
# ===== SINGLE LINE =====
else:
line = lines[0]
if line.invoice_marketplace in existing_so_map:
raise UserError(_(
"Invoice Marketplace %s sudah terdaftar di Sale Order: %s"
) % (
line.invoice_marketplace,
', '.join(existing_so_map[line.invoice_marketplace])
))
if not line.order_id:
raise UserError(_('Order ID is empty!'))
data = line._call_api(
BATCH_GET_URI,
{"orderIds": [line.order_id]}
)
detail_order = self.env['detail.order'].create({
'detail_order': json.dumps(data, indent=4),
'source': 'manual',
})
detail_order.execute_queue_detail()
line.write({
'message_error': 'Success',
'detail_order_id': detail_order.id
})
except Exception as e:
self.env['upload.ginee.line'].browse(
[l.id for l in lines]
).write({
'message_error': str(e)
})
def get_order_id(self):
for rec in self:
try:
if rec.order_id:
continue
if self.search_count([
('marketplace', '=', rec.marketplace),
('invoice_marketplace', '=', rec.invoice_marketplace),
('id', '!=', rec.id)
]):
raise UserError(_(
"Invoice %s already exists") % rec.invoice_marketplace)
data = rec._call_api(
LIST_ORDER_URI,
{
"channel": rec.marketplace,
"orderNumbers": [rec.invoice_marketplace]
}
)
orders = data.get('data', {}).get('content', [])
if orders:
rec.order_id = orders[0].get('orderId')
rec.message_error = 'Success'
else:
raise UserError(_("No orders found for invoice: %s") % rec.invoice_marketplace)
except Exception as e:
rec.message_error = str(e)
raise
|