summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/tukar_guling.py
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2025-08-14 16:28:15 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2025-08-14 16:28:15 +0700
commitbfdaf12dc3e7abace11502fcf1cb6c87522f1a51 (patch)
tree1b7d44f2b6a5ad2539f181effb66e9e3a59c9e9a /indoteknik_custom/models/tukar_guling.py
parentad6ccce331dc8c83f2cacb82b0e3c94467ff9d85 (diff)
parentdb75081a2afcd369e8a0169f3fe2f080dbad0c4a (diff)
Merge branch 'odoo-backup' of bitbucket.org:altafixco/indoteknik-addons into odoo-backup
Diffstat (limited to 'indoteknik_custom/models/tukar_guling.py')
-rw-r--r--indoteknik_custom/models/tukar_guling.py60
1 files changed, 45 insertions, 15 deletions
diff --git a/indoteknik_custom/models/tukar_guling.py b/indoteknik_custom/models/tukar_guling.py
index 4c7722d5..4b03d4b0 100644
--- a/indoteknik_custom/models/tukar_guling.py
+++ b/indoteknik_custom/models/tukar_guling.py
@@ -32,7 +32,7 @@ class TukarGuling(models.Model):
'tukar_guling_id',
string='Transfers'
)
- # origin_so = fields.Many2one('sale.order', string='Origin SO')
+ origin_so = fields.Many2one('sale.order', string='Origin SO', compute='_compute_origin_so')
name = fields.Char('Number', required=True, copy=False, readonly=True, default='New')
date = fields.Datetime('Date', default=fields.Datetime.now, required=True)
operations = fields.Many2one(
@@ -83,19 +83,47 @@ class TukarGuling(models.Model):
invoice_id = fields.Many2many('account.move', string='Invoice Ref', readonly=True)
- @api.depends('origin')
+ @api.depends('origin', 'operations')
+ def _compute_origin_so(self):
+ for rec in self:
+ rec.origin_so = False
+ origin_str = rec.origin or rec.operations.origin
+ if origin_str:
+ so = self.env['sale.order'].search([('name', '=', origin_str)], limit=1)
+ rec.origin_so = so.id if so else False
+
+ @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:
@@ -144,8 +172,6 @@ class TukarGuling(models.Model):
if self.line_ids and from_return_picking:
# Hanya update origin, jangan ubah lines
- if self.operations.origin:
- self.origin = self.operations.origin
_logger.info("📌 Menggunakan product lines dari return wizard, tidak populate ulang.")
# 🚀 Tapi tetap populate mapping koli jika BU/OUT
@@ -177,6 +203,7 @@ class TukarGuling(models.Model):
# Set origin dari operations
if self.operations.origin:
self.origin = self.operations.origin
+ self.origin_so = self.operations.group_id.id
# Auto-populate lines dari move_ids operations
lines_data = []
@@ -332,17 +359,20 @@ class TukarGuling(models.Model):
# _("Tidak bisa memilih Return Type 'Revisi SO' karena dokumen %s sudah dibuat invoice.") % record.origin
# )
+
@api.model
def create(self, vals):
- # Generate sequence number
if not vals.get('name') or vals['name'] == 'New':
vals['name'] = self.env['ir.sequence'].next_by_code('tukar.guling')
- # Auto-fill origin from operations
- if not vals.get('origin') and vals.get('operations'):
+ if vals.get('operations'):
picking = self.env['stock.picking'].browse(vals['operations'])
if picking.origin:
vals['origin'] = picking.origin
+ # Find matching SO
+ so = self.env['sale.order'].search([('name', '=', picking.origin)], limit=1)
+ if so:
+ vals['origin_so'] = so.id
if picking.partner_id:
vals['partner_id'] = picking.partner_id.id