From d896d5cb51437d366c10e854616faee46736688c Mon Sep 17 00:00:00 2001 From: Miqdad Date: Tue, 12 Aug 2025 15:10:16 +0700 Subject: get bill based on product --- indoteknik_custom/models/tukar_guling.py | 37 ++++++++++++++++------ indoteknik_custom/models/tukar_guling_po.py | 49 ++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/indoteknik_custom/models/tukar_guling.py b/indoteknik_custom/models/tukar_guling.py index 6aedb70e..ff641f34 100644 --- a/indoteknik_custom/models/tukar_guling.py +++ b/indoteknik_custom/models/tukar_guling.py @@ -92,19 +92,38 @@ class TukarGuling(models.Model): so = self.env['sale.order'].search([('name', '=', origin_str)], limit=1) rec.origin_so = so.id if so else False - @api.depends('origin') + @api.depends('origin', 'origin_so', 'partner_id', 'line_ids.product_id') def _compute_is_has_invoice(self): + Move = self.env['account.move'] for rec in self: - invoices = self.env['account.move'].search([ - ('invoice_origin', 'ilike', rec.origin), - ('move_type', '=', 'out_invoice'), # hanya invoice - ('state', 'not in', ['draft', 'cancel']) - ]) + rec.is_has_invoice = False + rec.invoice_id = [(5, 0, 0)] + + product_ids = rec.line_ids.mapped('product_id').ids + if not product_ids: + continue + + domain = [ + ('move_type', 'in', ['out_invoice', 'in_invoice']), + ('state', 'not in', ['draft', 'cancel']), + ('invoice_line_ids.product_id', 'in', product_ids), + ] + + if rec.partner_id: + domain.append(('partner_id', '=', rec.partner_id.id)) + + extra = [] + if rec.origin: + extra.append(('invoice_origin', 'ilike', rec.origin)) + if rec.origin_so: + extra.append(('invoice_line_ids.sale_line_ids.order_id', '=', rec.origin_so.id)) + if extra: + domain = domain + ['|'] * (len(extra) - 1) + extra + + invoices = Move.search(domain).with_context(active_test=False) if invoices: + rec.invoice_id = [(6, 0, invoices.ids)] rec.is_has_invoice = True - rec.invoice_id = invoices - else: - rec.is_has_invoice = False def set_opt(self): if not self.val_inv_opt and self.is_has_invoice == True: diff --git a/indoteknik_custom/models/tukar_guling_po.py b/indoteknik_custom/models/tukar_guling_po.py index 03d7668f..cc1c79c0 100644 --- a/indoteknik_custom/models/tukar_guling_po.py +++ b/indoteknik_custom/models/tukar_guling_po.py @@ -73,19 +73,46 @@ class TukarGulingPO(models.Model): so = self.env['purchase.order'].search([('name', '=', origin_str)], limit=1) rec.origin_po = so.id if so else False - @api.depends('origin') + @api.depends('origin', 'origin_po', 'vendor_id', 'line_ids.product_id') def _compute_is_has_bill(self): + Move = self.env['account.move'] for rec in self: - bills = self.env['account.move'].search([ - ('invoice_origin', 'ilike', rec.origin), - ('move_type', '=', 'in_invoice'), # hanya vendor bill - ('state', 'not in', ['draft', 'cancel']) - ]) - if bills: - rec.is_has_bill = True - rec.bill_id = bills - else: - rec.is_has_bill = False + # reset + rec.is_has_bill = False + rec.bill_id = [(5, 0, 0)] + + product_ids = rec.line_ids.mapped('product_id').ids + if not product_ids: + continue + + # dasar: bill atau vendor credit note yang linennya mengandung produk TG + domain = [ + ('move_type', 'in', ['in_invoice', 'in_refund']), + ('state', 'not in', ['draft', 'cancel']), + ('invoice_line_ids.product_id', 'in', product_ids), + ] + + # batasi ke vendor sama (kalau ada) + if rec.vendor_id: + domain.append(('partner_id', '=', rec.vendor_id.id)) + + # bantu pembatasan ke asal dokumen + extra = [] + if rec.origin: + extra.append(('invoice_origin', 'ilike', rec.origin)) + if rec.origin_po: + # di Odoo 14, invoice line biasanya link ke purchase.line lewat purchase_line_id + extra.append(('invoice_line_ids.purchase_line_id.order_id', '=', rec.origin_po.id)) + + # OR-kan semua extra filter jika ada + if extra: + domain = domain + ['|'] * (len(extra) - 1) + extra + + bills = Move.search(domain).with_context(active_test=False) + + # --- Opsi 1: minimal salah satu produk TG muncul di bill (default) --- + rec.bill_id = [(6, 0, bills.ids)] + rec.is_has_bill = bool(bills) def set_opt(self): if not self.val_bil_opt and self.is_has_bill == True: -- cgit v1.2.3