diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-08-12 15:10:16 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-08-12 15:10:16 +0700 |
| commit | d896d5cb51437d366c10e854616faee46736688c (patch) | |
| tree | 066e2784c2bad72cbf925b94eb390d75460e4e94 /indoteknik_custom/models/tukar_guling_po.py | |
| parent | 10c2bf7111f0743941354e21f7b7ab57bb5b1d79 (diff) | |
<miqdad> get bill based on product
Diffstat (limited to 'indoteknik_custom/models/tukar_guling_po.py')
| -rw-r--r-- | indoteknik_custom/models/tukar_guling_po.py | 49 |
1 files changed, 38 insertions, 11 deletions
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: |
