summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/report_logbook_sj.py
diff options
context:
space:
mode:
authorMiqdad <ahmadmiqdad27@gmail.com>2025-09-15 12:37:32 +0700
committerMiqdad <ahmadmiqdad27@gmail.com>2025-09-15 12:37:32 +0700
commit1afdf5c126bc41ece2e197610a340aab1d102c58 (patch)
tree71d3719ef03c7c1085bca07e5b01b2d6eacdc8a4 /indoteknik_custom/models/report_logbook_sj.py
parent1fd86434b0cc2deeeb32451cb5ffebba6785009d (diff)
<Miqdad> add line no
Diffstat (limited to 'indoteknik_custom/models/report_logbook_sj.py')
-rw-r--r--indoteknik_custom/models/report_logbook_sj.py58
1 files changed, 56 insertions, 2 deletions
diff --git a/indoteknik_custom/models/report_logbook_sj.py b/indoteknik_custom/models/report_logbook_sj.py
index 17119c12..66edbf99 100644
--- a/indoteknik_custom/models/report_logbook_sj.py
+++ b/indoteknik_custom/models/report_logbook_sj.py
@@ -1,3 +1,5 @@
+from operator import index
+
from odoo import models, fields, api
from odoo.exceptions import UserError
from pytz import timezone
@@ -60,9 +62,30 @@ class ReportLogbookSJ(models.Model):
self.state = 'terima_semua'
else:
raise UserError('Hanya Accounting yang bisa Approve')
-
+
+
+ def write(self, vals):
+ res = super(ReportLogbookSJ, self).write(vals)
+ if 'report_logbook_sj_line' in vals or any(f in vals for f in ()):
+ self._resequence_lines()
+ return res
+
+ def _resequence_lines(self):
+ for rec in self:
+ lines = rec.report_logbook_sj_line.sorted(key=lambda l: (l.line_num or 0, l.id))
+ for idx, line in enumerate(lines, start=1):
+ if line.line_num != idx:
+ line.line_num = idx
+
+ @api.onchange('report_logbook_sj_line')
+ def _onchange_report_logbook_sj_line(self):
+ self._resequence_lines()
+
+from odoo import models, fields, api
+
class ReportLogbookSJLine(models.Model):
_name = 'report.logbook.sj.line'
+ _order = 'sequence, id' # urut default di UI & ORM (drag pakai sequence)
name = fields.Char(string='SJ Number')
driver_id = fields.Many2one(comodel_name='res.users', string='Driver')
@@ -70,10 +93,41 @@ class ReportLogbookSJLine(models.Model):
arrival_date = fields.Char(string='Arrival Date')
carrier_id = fields.Many2one('delivery.carrier', string='Shipping Method')
tracking_no = fields.Char(string='Tracking No')
- logbook_sj_id = fields.Many2one('report.logbook.sj', string='Logbook SJ') # Corrected model name
+
+ # NOTE: field ini duplikat relasi; pakai salah satu saja.
+ # kamu boleh hapus logbook_sj_id kalau tidak dipakai di tempat lain.
+ logbook_sj_id = fields.Many2one('report.logbook.sj', string='Logbook SJ')
+
partner_id = fields.Many2one('res.partner', string='Customer')
picking_id = fields.Many2one('stock.picking', string='Picking')
sale_id = fields.Many2one('sale.order', string='Sale Order')
+
report_logbook_sj_id = fields.Many2one('report.logbook.sj', string='Logbook SJ')
not_exist = fields.Boolean(string='Not Exist')
note = fields.Char(string='Note')
+
+ sequence = fields.Integer(string='Sequence', default=0, index=True)
+
+ line_num = fields.Integer(string='No', compute='_compute_line_num', store=False)
+
+ @api.depends(
+ 'report_logbook_sj_id.report_logbook_sj_line',
+ 'report_logbook_sj_id.report_logbook_sj_line.sequence'
+ )
+ def _compute_line_num(self):
+ for parent in self.mapped('report_logbook_sj_id'):
+ lines = parent.report_logbook_sj_line.sorted(key=lambda l: (l.sequence or 0, l.id))
+ for i, l in enumerate(lines, start=1):
+ l.line_num = i
+ for rec in self.filtered(lambda r: not r.report_logbook_sj_id):
+ rec.line_num = 0
+
+ @api.model
+ def create(self, vals):
+ if not vals.get('sequence') and vals.get('report_logbook_sj_id'):
+ last = self.search(
+ [('report_logbook_sj_id', '=', vals['report_logbook_sj_id'])],
+ order='sequence desc, id desc', limit=1
+ )
+ vals['sequence'] = (last.sequence or 0) + 1
+ return super().create(vals)