from operator import index from odoo import models, fields, api from odoo.exceptions import UserError from pytz import timezone from datetime import datetime import requests import json import logging _logger = logging.getLogger(__name__) class ReportLogbookSJ(models.Model): _name = 'report.logbook.sj' _description = "Logbook SJ" _inherit = ['mail.thread'] _rec_name = 'name' name = fields.Char(string='Name', default='Logbook SJ') date = fields.Datetime(string='Date Created') date_approve = fields.Datetime(string='Date Approve', tracking=3) approve_by_finance = fields.Boolean(string='Approve By Finance', tracking=3) approve_by = fields.Many2one(comodel_name='res.users', string='Approve By', tracking=3) created_by = fields.Many2one(comodel_name='res.users', string='Created By', tracking=3) report_logbook_sj_line = fields.One2many( comodel_name='report.logbook.sj.line', inverse_name='report_logbook_sj_id', string='Logbook SJ Line' ) state = fields.Selection( [('belum_terima', 'Belum Terima'), ('terima_sebagian', 'Terima Sebagian'), ('terima_semua', 'Sudah di terima semua'), ], default='terima_semua', string='Status', tracking=True, ) sj_number = fields.Char(string='Picking', related='report_logbook_sj_line.name') count_line = fields.Char(string='Count Line', compute='_compute_count_line') @api.depends('report_logbook_sj_line') def _compute_count_line(self): for rec in self: rec.count_line = len(rec.report_logbook_sj_line) @api.model def create(self, vals): vals['name'] = self.env['ir.sequence'].next_by_code('report.logbook.sj') or '0' result = super(ReportLogbookSJ, self).create(vals) return result def approve(self): current_time = datetime.utcnow() if self.env.user.is_accounting: self.approve_by_finance = True self.date_approve = current_time self.approve_by = self.env.user.id if any(line.not_exist for line in self.report_logbook_sj_line): if all(line.not_exist for line in self.report_logbook_sj_line): self.state = 'belum_terima' else: self.state = 'terima_sebagian' else: 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() 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') departure_date = fields.Char(string='Departure Date') arrival_date = fields.Char(string='Arrival Date') carrier_id = fields.Many2one('delivery.carrier', string='Shipping Method') tracking_no = fields.Char(string='Tracking No') # 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)