diff options
| -rw-r--r-- | indoteknik_api/controllers/api_v1/stock_picking.py | 119 | ||||
| -rw-r--r-- | indoteknik_custom/models/keywords.py | 125 | ||||
| -rw-r--r-- | indoteknik_custom/views/keywords.xml | 3 | ||||
| -rw-r--r-- | indoteknik_custom/views/stock_picking.xml | 18 |
4 files changed, 179 insertions, 86 deletions
diff --git a/indoteknik_api/controllers/api_v1/stock_picking.py b/indoteknik_api/controllers/api_v1/stock_picking.py index def55435..e33d01c1 100644 --- a/indoteknik_api/controllers/api_v1/stock_picking.py +++ b/indoteknik_api/controllers/api_v1/stock_picking.py @@ -121,68 +121,141 @@ class StockPicking(controller.Controller): return self.response(picking.get_tracking_detail()) - @http.route(prefix + 'stock-picking/<scanid>/documentation', auth='public', methods=['PUT', 'OPTIONS'], csrf=False) + @http.route(prefix + 'stock-picking/<scanid>/documentation', + auth='public', methods=['PUT', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized() def write_partner_stock_picking_documentation(self, scanid, **kw): - paket_document = kw.get('paket_document') if 'paket_document' in kw else None - dispatch_document = kw.get('dispatch_document') if 'dispatch_document' in kw else None - self_pu = kw.get('self_pu') if 'self_pu' in kw else None - # ===== Cari picking by id / picking_code ===== - picking = False - # if scanid.isdigit() and int(scanid) < 2147483646: - # picking = request.env['stock.picking'].search([('id', '=', int(scanid))], limit=1) - # if not picking: - # picking = request.env['stock.picking'].search([('picking_code', '=', scanid)], limit=1) + env = request.env + Picking = env['stock.picking'].sudo() + SjDoc = env['stock.picking.sj.document'].sudo() + + paket_document = kw.get('paket_document') + dispatch_document = kw.get('dispatch_document') + self_pu = kw.get('self_pu') + # ===== Cari picking (FAST PATH) ===== + picking = False if scanid.isdigit(): - picking = request.env['stock.picking'].browse(int(scanid)) + picking = Picking.browse(int(scanid)) if not picking.exists(): picking = False if not picking: - picking = request.env['stock.picking'].search( - [('picking_code', '=', scanid)], - limit=1 - ) + picking = Picking.search([('picking_code', '=', scanid)], limit=1) if not picking: return self.response(code=403, description='picking not found') - # ===== Ambil MULTIPLE SJ dari form: sj_documentations=...&sj_documentations=... ===== + # ===== Ambil SJ list ===== form = request.httprequest.form or {} - sj_list = form.getlist('sj_documentations') # list of base64 strings + sj_list = form.getlist('sj_documentations') - # fallback: kalau FE kirim single dengan nama yang sama (bukan list) - if not sj_list and 'sj_documentations' in kw and kw.get('sj_documentations'): + if not sj_list and kw.get('sj_documentations'): sj_list = [kw.get('sj_documentations')] + # ===== Prepare write params ===== params = {} + now = fields.Datetime.now() + if paket_document: params['paket_documentation'] = paket_document - params['driver_arrival_date'] = datetime.utcnow() + params['driver_arrival_date'] = now + if dispatch_document: params['dispatch_documentation'] = dispatch_document + if sj_list and self_pu: - params['driver_arrival_date'] = datetime.utcnow() + params['driver_arrival_date'] = now if params: picking.write(params) + # ===== BULK CREATE SJ (BIG WIN) ===== if sj_list: - Child = request.env['stock.picking.sj.document'].sudo() seq = (picking.sj_documentations[:1].sequence or 10) if picking.sj_documentations else 10 + vals_list = [] + for b64 in sj_list: if not b64: continue - Child.create({ + vals_list.append({ 'picking_id': picking.id, 'sequence': seq, 'image': b64, }) seq += 10 + if vals_list: + SjDoc.create(vals_list) + return self.response({'name': picking.name}) + + # @http.route(prefix + 'stock-picking/<scanid>/documentation', auth='public', methods=['PUT', 'OPTIONS'], csrf=False) + # @controller.Controller.must_authorized() + # def write_partner_stock_picking_documentation(self, scanid, **kw): + # paket_document = kw.get('paket_document') if 'paket_document' in kw else None + # dispatch_document = kw.get('dispatch_document') if 'dispatch_document' in kw else None + # self_pu = kw.get('self_pu') if 'self_pu' in kw else None + + # # ===== Cari picking by id / picking_code ===== + # picking = False + # # if scanid.isdigit() and int(scanid) < 2147483646: + # # picking = request.env['stock.picking'].search([('id', '=', int(scanid))], limit=1) + # # if not picking: + # # picking = request.env['stock.picking'].search([('picking_code', '=', scanid)], limit=1) + + # if scanid.isdigit(): + # picking = request.env['stock.picking'].browse(int(scanid)) + # if not picking.exists(): + # picking = False + + # if not picking: + # picking = request.env['stock.picking'].search( + # [('picking_code', '=', scanid)], + # limit=1 + # ) + # if not picking: + # return self.response(code=403, description='picking not found') + + # # ===== Ambil MULTIPLE SJ dari form: sj_documentations=...&sj_documentations=... ===== + # form = request.httprequest.form or {} + # sj_list = form.getlist('sj_documentations') # list of base64 strings + + # # fallback: kalau FE kirim single dengan nama yang sama (bukan list) + # if not sj_list and 'sj_documentations' in kw and kw.get('sj_documentations'): + # sj_list = [kw.get('sj_documentations')] + + # params = {} + # if paket_document: + # params['paket_documentation'] = paket_document + # params['driver_arrival_date'] = datetime.utcnow() + # if dispatch_document: + # params['dispatch_documentation'] = dispatch_document + # if sj_list and self_pu: + # params['driver_arrival_date'] = datetime.utcnow() + + # if params: + # picking.write(params) + + + # vals_list = [] + # for b64 in sj_list: + # if not b64: + # continue + # vals_list.append({ + # 'picking_id': picking.id, + # 'sequence': seq, + # 'image': b64, + # }) + # seq += 10 + + # if vals_list: + # Child.create(vals_list) + + + # return self.response({'name': picking.name}) + # @http.route(prefix + 'stock-picking/<scanid>/documentation', auth='public', methods=['PUT', 'OPTIONS'], csrf=False) # @controller.Controller.must_authorized() # def write_partner_stock_picking_documentation(self, scanid, **kw): diff --git a/indoteknik_custom/models/keywords.py b/indoteknik_custom/models/keywords.py index 28dfa09e..495a42cc 100644 --- a/indoteknik_custom/models/keywords.py +++ b/indoteknik_custom/models/keywords.py @@ -1,4 +1,5 @@ from itertools import product +from multiprocessing import Condition from odoo import fields, models, api, tools, _ import logging import re @@ -64,56 +65,58 @@ class Keywords(models.Model): # raise UserError("Tidak bisa create/write/duplicate karena keywords sudah dipakai") - # def generate_products(self): - # for record in self: - # if not record.keywords: - # continue - - # keyword = "%" + record.keywords.strip() + "%" - - # # Query dasar - # sql = """ - # SELECT pp.id - # FROM product_product pp - # JOIN product_template pt ON pt.id = pp.product_tmpl_id - # LEFT JOIN product_public_category_product_template_rel rel - # ON rel.product_template_id = pt.id - # WHERE ( - # pt.name ILIKE %s - # OR pt.website_description ILIKE %s - # AND pt.unpublished = False - # AND pt.product_rating >= 8 - # ) - # """ - - # params = [keyword, keyword] - - # # Filter kategori ke child - # if record.category_id: - # child_categs = self.env['product.public.category'].search([ - # ('id', 'child_of', record.category_id.id) - # ]) - # sql += " AND rel.product_public_category_id = ANY(%s)" - # params.append(child_categs.ids) - - # # Exec SQL - # self.env.cr.execute(sql, params) - # rows = self.env.cr.fetchall() - - # product_ids = [r[0] for r in rows] - - # if not product_ids: - # raise UserError("Tidak berhasil menemukan barang") - - # record.with_context(skip_generate=True).write({ - # 'product_ids': [(6, 0, product_ids)] - # }) - - # _logger.info( - # "Product Found: Found %s products for keyword '%s'", - # len(product_ids), - # record.keywords - # ) + def generate_products(self): + for record in self: + if not record.keywords: + continue + if record.skip: + continue + + keyword = "%" + record.keywords.strip() + "%" + + # Query dasar + sql = """ + SELECT pp.id + FROM product_product pp + JOIN product_template pt ON pt.id = pp.product_tmpl_id + LEFT JOIN product_public_category_product_template_rel rel + ON rel.product_template_id = pt.id + WHERE ( + pt.name ILIKE %s + OR pt.website_description ILIKE %s + AND pt.unpublished = False + AND pt.product_rating >= 8 + ) + """ + + params = [keyword, keyword] + + # Filter kategori ke child + if record.category_id: + child_categs = self.env['product.public.category'].search([ + ('id', 'child_of', record.category_id.id) + ]) + sql += " AND rel.product_public_category_id = ANY(%s)" + params.append(child_categs.ids) + + # Exec SQL + self.env.cr.execute(sql, params) + rows = self.env.cr.fetchall() + + product_ids = [r[0] for r in rows] + + if not product_ids: + raise UserError("Tidak berhasil menemukan barang") + + record.with_context(skip_generate=True).write({ + 'product_ids': [(6, 0, product_ids)] + }) + + _logger.info( + "Product Found: Found %s products for keyword '%s'", + len(product_ids), + record.keywords + ) def sync_solr(self): # solr_model = self.env['apache.solr'] @@ -138,14 +141,14 @@ class Keywords(models.Model): solr.add(documents) return True - # @api.model - # def create(self, vals): - # record = super().create(vals) - # # record.generate_products() - # return record - - # def write(self, vals): - # result = super().write(vals) - # # if not self.env.context.get("skip_generate") and not self.skip: - # # self.generate_products() - # return result + @api.model + def create(self, vals): + record = super().create(vals) + # record.generate_products() + return record + + def write(self, vals): + result = super().write(vals) + # if not self.env.context.get("skip_generate") and not self.skip: + # self.generate_products() + return result diff --git a/indoteknik_custom/views/keywords.xml b/indoteknik_custom/views/keywords.xml index f5374655..878952f8 100644 --- a/indoteknik_custom/views/keywords.xml +++ b/indoteknik_custom/views/keywords.xml @@ -19,6 +19,9 @@ <field name="model">keywords</field> <field name="arch" type="xml"> <form> + <header> + <button name="generate_products" string="Generate Product Manual" type="object" class="oe_highlight"/> + </header> <sheet> <div class="oe_title"> <h1> diff --git a/indoteknik_custom/views/stock_picking.xml b/indoteknik_custom/views/stock_picking.xml index 5671c9f0..1bb7174e 100644 --- a/indoteknik_custom/views/stock_picking.xml +++ b/indoteknik_custom/views/stock_picking.xml @@ -20,6 +20,8 @@ <field name="sj_return_date" optional="hide"/> <field name="date_reserved" optional="hide"/> <field name="state_reserve" optional="hide"/> + <field name="linked_out_picking_id" optional="hide"/> + <field name="linked_manual_bu_out" optional="hide"/> <field name="state_packing" widget="badge" decoration-success="state_packing == 'packing_done'" decoration-danger="state_packing == 'not_packing'" optional="hide"/> <field name="final_seq"/> @@ -361,12 +363,24 @@ <widget name="web_ribbon" title="Administrasi" bg_color="bg-danger" - attrs="{'invisible': ['|', ('tukar_guling_id','=',False), ('is_rev_tg','=', False)]}"/> + attrs="{ + 'invisible': [ + '|','|', + ('tukar_guling_id', '=', False), + ('is_rev_tg', '=', False), + ('picking_type_id', 'not in', [29, 30]) + ]}"/> <widget name="web_ribbon" title="Perlu Dikirim" bg_color="bg-success" - attrs="{'invisible': ['|', ('tukar_guling_id','=',False), ('is_rev_tg','=', True)]}"/> + attrs="{ + 'invisible': [ + '|','|', + ('tukar_guling_id', '=', False), + ('is_rev_tg', '=', True), + ('picking_type_id', 'not in', [29, 30]) + ]}"/> </xpath> </field> </record> |
