diff options
Diffstat (limited to 'indoteknik_custom/models/user_pengajuan_tempo_request.py')
| -rw-r--r-- | indoteknik_custom/models/user_pengajuan_tempo_request.py | 73 |
1 files changed, 62 insertions, 11 deletions
diff --git a/indoteknik_custom/models/user_pengajuan_tempo_request.py b/indoteknik_custom/models/user_pengajuan_tempo_request.py index b43f56ac..be4293a0 100644 --- a/indoteknik_custom/models/user_pengajuan_tempo_request.py +++ b/indoteknik_custom/models/user_pengajuan_tempo_request.py @@ -7,7 +7,7 @@ class RejectReasonWizard(models.TransientModel): _description = 'Wizard for Reject Reason' request_id = fields.Many2one('user.pengajuan.tempo.request', string='Request') - reason_reject = fields.Text(string='Reason for Rejection', required=True) + reason_reject = fields.Text(string='Reason for Rejection', required=True, tracking=True) def confirm_reject(self): tempo = self.request_id @@ -55,7 +55,27 @@ class UserPengajuanTempoRequest(models.Model): ('approval_director', 'Approved by Director'), ('reject', 'Rejected'), ], string='Status', readonly=True, copy=False, index=True, track_visibility='onchange', default='draft') - reason_reject = fields.Char(string='Limit Tempo') + last_state_tempo = fields.Selection([ + ('draft', 'Pengajuan Tempo'), + ('approval_sales', 'Approved by Sales Manager'), + ('approval_finance', 'Approved by Finance'), + ('approval_director', 'Approved by Director'), + ('reject', 'Rejected'), + ], string='Status',) + reason_reject = fields.Char(string='Reason Reaject', tracking=True, track_visibility='onchange') + state_tempo_text = fields.Char(string="Status", compute="_compute_state_tempo_text") + + @api.depends('state_tempo') + def _compute_state_tempo_text(self): + for record in self: + status_mapping = { + 'draft': "Menunggu Approve Manager", + 'approval_sales': "Menunggu Approve Finance", + 'approval_finance': "Menunggu Approve Direktur", + 'approval_director': "Approved", + 'reject': "Rejected", + } + record.state_tempo_text = status_mapping.get(record.state_tempo, "Unknown") # informasi perusahaan name_tempo = fields.Many2one('res.partner', string='Nama Perusahaan', related='pengajuan_tempo_id.name_tempo', store=True, tracking=True, readonly=False) @@ -71,7 +91,7 @@ class UserPengajuanTempoRequest(models.Model): website_tempo = fields.Char(string='Website', related='pengajuan_tempo_id.website_tempo', store=True, tracking=True, readonly=False) portal = fields.Boolean(string='Portal Website', related='pengajuan_tempo_id.portal', store=True, tracking=True, readonly=False) estimasi_tempo = fields.Char(string='Estimasi Pembelian Pertahun', related='pengajuan_tempo_id.estimasi_tempo', store=True, tracking=True, readonly=False) - tempo_duration_origin = fields.Many2one('account.payment.term', string='Durasi Tempo', related='pengajuan_tempo_id.tempo_duration', store=True, tracking=True, readonly=False, domain=[('id', 'in', [24, 25, 29, 32])]) + tempo_duration_origin = fields.Many2one('account.payment.term', string='Durasi Tempo', related='tempo_duration', store=True, tracking=True, readonly=False, domain=[('id', 'in', [24, 25, 29, 32])]) tempo_limit_origin = fields.Char(string='Limit Tempo', related='pengajuan_tempo_id.tempo_limit' , store=True, tracking=True, readonly=False) category_produk_ids = fields.Many2many('product.public.category', string='Kategori Produk yang Digunakan', related='pengajuan_tempo_id.category_produk_ids', readonly=False) @@ -334,13 +354,13 @@ class UserPengajuanTempoRequest(models.Model): @api.onchange('tempo_duration') def _tempo_duration_change(self): for tempo in self: - if tempo.env.user.id not in (7, 377, 12182): + if tempo.env.user.id not in (7, 688, 28, 377, 12182): raise UserError("Durasi tempo hanya bisa di ubah oleh Sales Manager atau Direktur") @api.onchange('tempo_limit') def _onchange_tempo_limit(self): for tempo in self: - if tempo.env.user.id not in (7, 377, 12182): + if tempo.env.user.id not in (7, 688, 28, 377, 12182): raise UserError("Limit tempo hanya bisa diubah oleh Sales Manager atau Direktur") def button_approve(self): for tempo in self: @@ -409,6 +429,9 @@ class UserPengajuanTempoRequest(models.Model): 'target': 'new', 'context': {'default_request_id': self.id}, } + def button_draft(self): + for tempo in self: + tempo.state_tempo = tempo.last_state_tempo if tempo.last_state_tempo else 'draft' def write(self, vals): is_approve = True if self.state_tempo == 'approval_director' or vals.get('state_tempo') == 'approval_director' else False @@ -507,10 +530,38 @@ class UserPengajuanTempoRequest(models.Model): # Buat kontak baru untuk company_id for contact_data in contacts_data: - self.env['res.partner'].create({ - "parent_id": self.user_company_id.id, # Hubungkan ke perusahaan - **contact_data, # Tambahkan data kontak - }) + existing_contact = self.env['res.partner'].search([ + ('parent_id', '=', self.user_company_id.id), + ('name', '=', contact_data['name']) + ], limit=1) + + if existing_contact: + # Pastikan tidak ada duplikasi nama dalam perusahaan yang sama + duplicate_check = self.env['res.partner'].search([ + ('name', '=', contact_data['name']), + ('id', '!=', existing_contact.id) # Hindari update yang menyebabkan duplikasi global + ], limit=1) + + if not duplicate_check: + # Perbarui hanya field yang tidak menyebabkan konflik + update_data = {k: v for k, v in contact_data.items() if k != 'name'} + existing_contact.write(update_data) + else: + raise UserError(f"Skipping update for {contact_data['name']} due to existing duplicate.") + else: + # Pastikan tidak ada partner lain dengan nama yang sama sebelum membuat baru + duplicate_check = self.env['res.partner'].search([ + ('name', '=', contact_data['name']) + ], limit=1) + + if duplicate_check: + # Jika nama sudah ada tetapi di perusahaan lain, tambahkan nama perusahaan + contact_data['name'] = f"{contact_data['name']} ({self.user_company_id.name})" + + self.env['res.partner'].create({ + "parent_id": self.user_company_id.id, # Hubungkan ke perusahaan + **contact_data, # Tambahkan data kontak + }) # Pengiriman self.user_company_id.pic_name = self.pengajuan_tempo_id.pic_name @@ -596,10 +647,10 @@ class UserPengajuanTempoRequest(models.Model): attachment_ids=[self.user_company_id.dokumen_tempat_bekerja.id]) # self.user_company_id.active = True # user.send_company_request_approve_mail() - self.user_company_id.property_payment_term_id = self.pengajuan_tempo_id.tempo_duration.id + self.user_company_id.property_payment_term_id = self.tempo_duration.id self.user_company_id.active_limit = True - self.user_company_id.warning_stage = float(limit_tempo) - (float(limit_tempo)/2) self.user_company_id.blocking_stage = limit_tempo + self.user_company_id.warning_stage = float(limit_tempo) - (float(limit_tempo)/2) # Internal Notes comment = [] |
