diff options
| author | Mqdd <ahmadmiqdad27@gmail.com> | 2025-11-19 10:57:24 +0700 |
|---|---|---|
| committer | Mqdd <ahmadmiqdad27@gmail.com> | 2025-11-19 10:57:24 +0700 |
| commit | 6ec6f33cc2579be1b926371f11fffe0c9f5ddad0 (patch) | |
| tree | b89e6dd10d369a5962fa36f5fabe01216dcde1d4 /indoteknik_custom/models | |
| parent | f317a1965ce183ea44c8be9c2a75ea210ea0f256 (diff) | |
| parent | e22ed5070117edbd7757ce409cb779da7fdc5c12 (diff) | |
Merge branch 'odoo-backup' of https://bitbucket.org/altafixco/indoteknik-addons into crm_mqdd
Diffstat (limited to 'indoteknik_custom/models')
19 files changed, 862 insertions, 161 deletions
diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 7f946b57..30898b99 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -164,3 +164,5 @@ from . import sj_tele from . import partial_delivery from . import domain_apo from . import sourcing_job_order +from . import uom_uom +from . import commission_internal diff --git a/indoteknik_custom/models/account_move.py b/indoteknik_custom/models/account_move.py index 684ef335..6212664e 100644 --- a/indoteknik_custom/models/account_move.py +++ b/indoteknik_custom/models/account_move.py @@ -3,7 +3,7 @@ from odoo.exceptions import AccessError, UserError, ValidationError from markupsafe import escape as html_escape from datetime import timedelta, date, datetime from pytz import timezone, utc -import logging +import logging, json import base64 import PyPDF2 import os @@ -11,6 +11,7 @@ import re from terbilang import Terbilang from collections import defaultdict from odoo.tools.misc import formatLang +import socket _logger = logging.getLogger(__name__) @@ -32,7 +33,6 @@ class AccountMove(models.Model): new_due_date = fields.Date(string='New Due') counter = fields.Integer(string="Counter", default=0) cost_centre_id = fields.Many2one('cost.centre', string='Cost Centre') - analytic_account_ids = fields.Many2many('account.analytic.account', string='Analytic Account') due_line = fields.One2many('due.extension.line', 'invoice_id', compute='_compute_due_line', string='Due Extension Lines') no_faktur_pajak = fields.Char(string='No Faktur Pajak') date_completed = fields.Datetime(string='Date Completed') @@ -108,6 +108,31 @@ class AccountMove(models.Model): ) internal_notes_contact = fields.Text(related='partner_id.comment', string="Internal Notes", readonly=True, help="Internal Notes dari contact utama customer.") + payment_info = fields.Text( + string="Payment Info", + compute='_compute_payment_info', + store=False, + help="Informasi pembayaran yang diambil dari payment yang sudah direkonsiliasi ke invoice ini." + ) + + def _compute_payment_info(self): + for rec in self: + summary = "" + try: + widget_data = rec.invoice_payments_widget + if widget_data: + data = json.loads(widget_data) + lines = [] + for item in data.get('content', []): + amount = item.get('amount', 0.0) + date = item.get('date') or item.get('payment_date') or '' + formatted_amount = formatLang(self.env, amount, currency_obj=rec.currency_id) + lines.append(f"<li><i>Paid on {date}</i> - {formatted_amount}</li>") + summary = f"<ul>{''.join(lines)}</ul>" if lines else (data.get('title', '') or "") + except Exception: + summary = "" + rec.payment_info = summary + # def _check_and_lock_cbd(self): # cbd_term = self.env['account.payment.term'].browse(26) # today = date.today() @@ -161,7 +186,8 @@ class AccountMove(models.Model): def action_sync_promise_date(self): self.ensure_one() finance_user_ids = [688] - if self.env.user.id not in finance_user_ids: + is_it = self.env.user.has_group('indoteknik_custom.group_role_it') + if self.env.user.id not in finance_user_ids and not is_it: raise UserError('Hanya Finance (Widya) yang dapat menggunakan fitur ini.') if not self.customer_promise_date: raise UserError("Isi Janji Bayar terlebih dahulu sebelum melakukan sinkronisasi.") @@ -169,10 +195,11 @@ class AccountMove(models.Model): other_invoices = self.env['account.move'].search([ ('id', '!=', self.id), ('partner_id', '=', self.partner_id.id), - ('invoice_date_due', '=', self.invoice_date_due), + ('payment_state', 'not in', ['paid', 'in_payment', 'reversed']), ('move_type', '=', 'out_invoice'), ('state', '=', 'posted'), - ('date_terima_tukar_faktur', '!=', False) + ('date_terima_tukar_faktur', '!=', False), + ('invoice_payment_term_id.name', 'ilike', 'tempo') ]) lines = [] for inv in other_invoices: @@ -192,7 +219,16 @@ class AccountMove(models.Model): 'target': 'new', } + @staticmethod + def is_local_env(): + hostname = socket.gethostname().lower() + keywords = ['andri', 'miqdad', 'fin', 'stephan', 'hafid', 'nathan'] + return any(keyword in hostname for keyword in keywords) + def send_due_invoice_reminder(self): + if self.is_local_env(): + _logger.warning("📪 Local environment detected — skip sending email reminders.") + return today = fields.Date.today() target_dates = [ today + timedelta(days=7), @@ -214,6 +250,9 @@ class AccountMove(models.Model): self._send_invoice_reminders(invoices, mode='due') def send_overdue_invoice_reminder(self): + if self.is_local_env(): + _logger.warning("📪 Local environment detected — skip sending email reminders.") + return today = fields.Date.today() invoices = self.env['account.move'].search([ ('move_type', '=', 'out_invoice'), @@ -229,6 +268,9 @@ class AccountMove(models.Model): self._send_invoice_reminders(invoices, mode='overdue') def _send_invoice_reminders(self, invoices, mode): + if self.is_local_env(): + _logger.warning("📪 Local environment detected — skip sending email reminders.") + return today = fields.Date.today() template = self.env.ref('indoteknik_custom.mail_template_invoice_due_reminder') diff --git a/indoteknik_custom/models/account_move_due_extension.py b/indoteknik_custom/models/account_move_due_extension.py index 55fc6c65..951d9745 100644 --- a/indoteknik_custom/models/account_move_due_extension.py +++ b/indoteknik_custom/models/account_move_due_extension.py @@ -115,6 +115,11 @@ class DueExtension(models.Model): self.order_id.check_credit_limit() self.order_id.approval_status = 'pengajuan1' return self.order_id._create_approval_notification('Sales Manager') + + if self.order_id._requires_approval_team_sales(): + self.order_id.check_credit_limit() + self.order_id.approval_status = 'pengajuan1' + return self.order_id._create_approval_notification('Team Sales') sales = self.env['sale.order'].browse(self.order_id.id) diff --git a/indoteknik_custom/models/advance_payment_request.py b/indoteknik_custom/models/advance_payment_request.py index 5a465ca4..ec23de63 100644 --- a/indoteknik_custom/models/advance_payment_request.py +++ b/indoteknik_custom/models/advance_payment_request.py @@ -159,7 +159,7 @@ class AdvancePaymentRequest(models.Model): self.nominal = self.grand_total_reimburse def _compute_is_current_user_ap(self): - ap_user_ids = [23, 9468] + ap_user_ids = [23, 9468, 16729] is_ap = self.env.user.id in ap_user_ids for line in self: line.is_current_user_ap = is_ap @@ -372,8 +372,9 @@ class AdvancePaymentRequest(models.Model): def action_view_journal_uangmuka(self): self.ensure_one() - ap_user_ids = [23, 9468] - if self.env.user.id not in ap_user_ids: + ap_user_ids = [23, 9468, 16729] + is_it = self.env.user.has_group('indoteknik_custom.group_role_it') + if self.env.user.id not in ap_user_ids and not is_it: raise UserError('Hanya User AP yang dapat menggunakan fitur ini.') if not self.move_id: @@ -497,14 +498,14 @@ class AdvancePaymentRequest(models.Model): # jakarta_tz = pytz.timezone('Asia/Jakarta') # now = datetime.now(jakarta_tz).replace(tzinfo=None) - ap_user_ids = [23, 9468] + ap_user_ids = [23, 9468, 16729] if self.env.user.id not in ap_user_ids: raise UserError('Hanya User AP yang dapat menggunakan fitur ini.') for rec in self: if not rec.attachment_file_image and not rec.attachment_file_pdf: raise UserError( - f'Tidak bisa konfirmasi pembayaran PUM {rec.name or ""} ' + f'Tidak bisa konfirmasi pembayaran {rec.number or ""} ' f'karena belum ada bukti attachment (PDF/Image).' ) @@ -572,7 +573,7 @@ class AdvancePaymentRequest(models.Model): ) elif rec.status == 'pengajuan2': - ap_user_ids = [23, 9468] # List user ID yang boleh approve sebagai Finance AP + ap_user_ids = [23, 9468, 16729] # List user ID yang boleh approve sebagai Finance AP if self.env.user.id not in ap_user_ids: raise UserError("Hanya AP yang berhak menyetujui tahap ini.") rec.name_approval_ap = self.env.user.name @@ -614,7 +615,7 @@ class AdvancePaymentRequest(models.Model): def action_ap_only(self): self.ensure_one() - ap_user_ids = [23, 9468] # Ganti sesuai kebutuhan + ap_user_ids = [23, 9468, 16729] # Ganti sesuai kebutuhan if self.env.user.id not in ap_user_ids: raise UserError('Hanya User AP yang dapat menggunakan fitur ini.') @@ -845,9 +846,9 @@ class AdvancePaymentUsageLine(models.Model): attachment_filename_pdf = fields.Char(string='Filename PDF') account_id = fields.Many2one( - 'account.account', string='Jenis Biaya', tracking=3, - domain="[('id', 'in', [484, 486, 488, 506, 507, 625, 471, 519, 527, 528, 529, 530, 565])]" # ID Jenis Biaya yang dibutuhkan + 'account.account', string='Jenis Biaya', tracking=3 # ID Jenis Biaya yang dibutuhkan ) + # domain="[('id', 'in', [484, 486, 488, 506, 507, 625, 471, 519, 527, 528, 529, 530, 565])]" # ID Jenis Biaya yang dibutuhkan is_current_user_ap = fields.Boolean( string="Is Current User AP", @@ -855,7 +856,7 @@ class AdvancePaymentUsageLine(models.Model): ) def _compute_is_current_user_ap(self): - ap_user_ids = [23, 9468] + ap_user_ids = [23, 9468, 16729] is_ap = self.env.user.id in ap_user_ids for line in self: line.is_current_user_ap = is_ap @@ -875,7 +876,7 @@ class AdvancePaymentUsageLine(models.Model): @api.onchange('done_attachment') def _onchange_done_attachment(self): - ap_user_ids = [23, 9468] # List user ID yang boleh approve sebagai Finance AP + ap_user_ids = [23, 9468, 16729] # List user ID yang boleh approve sebagai Finance AP if self.done_attachment and self.env.user.id not in ap_user_ids: self.done_attachment = False @@ -908,8 +909,7 @@ class ReimburseLine(models.Model): date = fields.Date(string='Tanggal', required=True, default=fields.Date.today) account_id = fields.Many2one( 'account.account', - string='Jenis Biaya', tracking=3, - domain="[('id', 'in', [484, 486, 527, 529, 530, 471, 473, 492, 493, 488, 625, 528, 533, 534])]" + string='Jenis Biaya', tracking=3 ) description = fields.Text(string='Description', required=True, tracking=3) distance_departure = fields.Float(string='Pergi (Km)', tracking=3) @@ -944,7 +944,7 @@ class ReimburseLine(models.Model): ) def _compute_is_current_user_ap(self): - ap_user_ids = [23, 9468] + ap_user_ids = [23, 9468, 16729] is_ap = self.env.user.id in ap_user_ids for line in self: line.is_current_user_ap = is_ap @@ -989,7 +989,7 @@ class AdvancePaymentSettlement(models.Model): name = fields.Char(string='Nama', readonly=True, tracking=3) title = fields.Char(string='Judul', tracking=3) goals = fields.Text(string='Tujuan', tracking=3) - related = fields.Char(string='Terkait', tracking=3) + related = fields.Char(string='Dok. Terkait', tracking=3) # pemberian_line_ids = fields.One2many( # 'advance.payment.settlement.line', 'realization_id', string='Rincian Pemberian' @@ -1008,7 +1008,13 @@ class AdvancePaymentSettlement(models.Model): # value_down_payment = fields.Float(string='PUM', tracking=3) remaining_value = fields.Float(string='Sisa Uang PUM', tracking=3, compute='_compute_remaining_value') - note_approval = fields.Text(string='Note Persetujuan', tracking=3) + def _get_default_note_approval(self): + template = ( + "Demikian dokumen Realisasi Uang Muka ini saya buat, dengan ini saya meminta persetujuan dibawah atas hasil penggunaan uang muka yang saya gunakan untuk kebutuhan realisasi " + ) + return template + + note_approval = fields.Text(string='Note Persetujuan', tracking=3, default=_get_default_note_approval) name_approval_departement = fields.Char(string='Approval Departement') name_approval_ap = fields.Char(string='Approval AP') @@ -1081,13 +1087,13 @@ class AdvancePaymentSettlement(models.Model): ) def _compute_is_current_user_ap(self): - ap_user_ids = [23, 9468] + ap_user_ids = [23, 9468, 16729] is_ap = self.env.user.id in ap_user_ids for line in self: line.is_current_user_ap = is_ap def action_toggle_check_attachment(self): - ap_user_ids = [23, 9468] + ap_user_ids = [23, 9468, 16729] if self.env.user.id not in ap_user_ids: raise UserError('Hanya User AP yang dapat menggunakan tombol ini.') @@ -1121,8 +1127,9 @@ class AdvancePaymentSettlement(models.Model): def action_view_journal_uangmuka(self): self.ensure_one() - ap_user_ids = [23, 9468] - if self.env.user.id not in ap_user_ids: + ap_user_ids = [23, 9468, 16729] + is_it = self.env.user.has_group('indoteknik_custom.group_role_it') + if self.env.user.id not in ap_user_ids and not is_it: raise UserError('Hanya User AP yang dapat menggunakan fitur ini.') if not self.move_id: @@ -1185,7 +1192,7 @@ class AdvancePaymentSettlement(models.Model): def action_cab(self): self.ensure_one() - ap_user_ids = [23, 9468] # List user ID yang boleh approve sebagai Finance AP + ap_user_ids = [23, 9468, 16729] # List user ID yang boleh approve sebagai Finance AP if self.env.user.id not in ap_user_ids: raise UserError('Hanya User AP yang dapat menggunakan ini.') if self.move_id: @@ -1194,7 +1201,8 @@ class AdvancePaymentSettlement(models.Model): if not self.pum_id or not self.pum_id.move_id: raise UserError("PUM terkait atau CAB belum tersedia.") - partner_id = self.pum_id.user_id.partner_id.id + # partner_id = self.pum_id.user_id.partner_id.id + partner_id = self.pum_id.applicant_name.partner_id.id cab_move = self.pum_id.move_id # Account Bank Intransit dari CAB: @@ -1310,7 +1318,7 @@ class AdvancePaymentSettlement(models.Model): ) elif rec.status == 'pengajuan2': - ap_user_ids = [23, 9468] # List user ID yang boleh approve sebagai Finance AP + ap_user_ids = [23, 9468, 16729] # List user ID yang boleh approve sebagai Finance AP if self.env.user.id not in ap_user_ids: raise UserError("Hanya AP yang berhak menyetujui tahap ini.") rec.name_approval_ap = self.env.user.name @@ -1413,7 +1421,7 @@ class AdvancePaymentCreateBill(models.TransientModel): apr_id = fields.Many2one('advance.payment.request', string='Advance Payment Request', required=True) account_id = fields.Many2one( 'account.account', string='Bank Intransit', required=True, - domain="[('id', 'in', [573, 389, 392])]" # ID Bank Intransit + domain="[('id', 'in', [573, 389, 392, 683, 380])]" # ID Bank Intransit ) nominal = fields.Float(string='Nominal', related='apr_id.nominal') @@ -1424,7 +1432,8 @@ class AdvancePaymentCreateBill(models.TransientModel): # raise UserError('Hanya AP yang dapat menggunakan ini.') apr = self.apr_id - partner_id = apr.user_id.partner_id.id + # partner_id = apr.user_id.partner_id.id + partner_id = apr.applicant_name.partner_id.id ref_label = f'{apr.number} - {apr.detail_note or "-"}' @@ -1496,8 +1505,9 @@ class CreateReimburseCabWizard(models.TransientModel): raise UserError("Tidak ada rincian reimburse yang bisa dijurnalkan.") lines = [] - partner_id = request.user_id.partner_id.id - + # partner_id = request.user_id.partner_id.id + partner_id = request.applicant_name.partner_id.id + ref_label = f'{request.number} - {request.detail_note or "-"}' # 1. Buat Jurnal DEBIT dari setiap baris reimburse for line in request.reimburse_line_ids: if not line.account_id: @@ -1515,12 +1525,11 @@ class CreateReimburseCabWizard(models.TransientModel): lines.append((0, 0, { 'account_id': self.account_id.id, 'partner_id': partner_id, - 'name': f'Reimburse {request.number}', + 'name': ref_label, 'debit': 0, 'credit': request.grand_total_reimburse, })) - ref_label = f'{request.number} - {request.detail_note or "-"}' # 3. Buat Journal Entry move = self.env['account.move'].create({ diff --git a/indoteknik_custom/models/automatic_purchase.py b/indoteknik_custom/models/automatic_purchase.py index d9ec17f4..4b0ce325 100644 --- a/indoteknik_custom/models/automatic_purchase.py +++ b/indoteknik_custom/models/automatic_purchase.py @@ -239,7 +239,8 @@ class AutomaticPurchase(models.Model): 'picking_type_id': 28, 'date_order': current_time, 'from_apo': True, - 'note_description': 'Automatic PO' + 'note_description': 'Automatic PO', + 'show_description': False if vendor_id == 5571 else True, } new_po = self.env['purchase.order'].create(param_header) @@ -283,7 +284,8 @@ class AutomaticPurchase(models.Model): 'ending_price': line.last_price, 'taxes_id': [(6, 0, [line.taxes_id.id])] if line.taxes_id else False, 'so_line_id': sales_match.sale_line_id.id if sales_match else None, - 'so_id': sales_match.sale_id.id if sales_match else None + 'so_id': sales_match.sale_id.id if sales_match else None, + 'show_description': False if vendor_id == 5571 else True, } new_po_line = self.env['purchase.order.line'].create(param_line) line.current_po_id = new_po.id diff --git a/indoteknik_custom/models/commision.py b/indoteknik_custom/models/commision.py index a937e2d0..441dd54f 100644 --- a/indoteknik_custom/models/commision.py +++ b/indoteknik_custom/models/commision.py @@ -423,8 +423,8 @@ class CustomerCommision(models.Model): def action_confirm_customer_commision(self): jakarta_tz = pytz.timezone('Asia/Jakarta') now = datetime.now(jakarta_tz) - - now_naive = now.replace(tzinfo=None) + now_utc = now.astimezone(pytz.utc) + now_naive = now_utc.replace(tzinfo=None) if not self.status or self.status == 'draft': self.status = 'pengajuan1' diff --git a/indoteknik_custom/models/commission_internal.py b/indoteknik_custom/models/commission_internal.py new file mode 100644 index 00000000..cd6da380 --- /dev/null +++ b/indoteknik_custom/models/commission_internal.py @@ -0,0 +1,392 @@ +from odoo import models, api, fields +from odoo.exceptions import AccessError, UserError, ValidationError +from datetime import timedelta, date +import logging + +_logger = logging.getLogger(__name__) + + +class CommissionInternal(models.Model): + _name = 'commission.internal' + _description = 'Commission Internal' + _order = 'date_doc, id desc' + _inherit = ['mail.thread'] + _rec_name = 'number' + + number = fields.Char(string='Document No', index=True, copy=False, readonly=True) + date_doc = fields.Date(string='Document Date', required=True) + month = fields.Selection([ + ('01', 'January'), ('02', 'February'), ('03', 'March'), + ('04', 'April'), ('05', 'May'), ('06', 'June'), + ('07', 'July'), ('08', 'August'), ('09', 'September'), + ('10', 'October'), ('11', 'November'), ('12', 'December') + ], string="Commission Month") + year = fields.Selection([(str(y), str(y)) for y in range(2025, 2036)], string="Commission Year") + description = fields.Char(string='Description') + comment = fields.Char(string='Comment') + commission_internal_line = fields.One2many('commission.internal.line', 'commission_internal_id', string='Lines', + auto_join=True, order='account_move_id asc') + commission_internal_result = fields.One2many('commission.internal.result', 'commission_internal_id', string='Result', + auto_join=True, order='account_move_id asc') + + @api.model + def create(self, vals): + vals['number'] = self.env['ir.sequence'].next_by_code('commission.internal') or '0' + result = super(CommissionInternal, self).create(vals) + return result + + def _get_commission_internal_bank_account_ids(self): + bank_ids = self.env['ir.config_parameter'].sudo().get_param('commission.internal.bank.account.id') + if not bank_ids: + return [] + return [int(x.strip()) for x in bank_ids.split(',') if x.strip().isdigit()] + + def _get_period_range(self, period_year, period_month): + """Return (date_start, date_end) using separate year and month fields.""" + self.ensure_one() # make sure it's called on a single record + + year_str = period_year or '' + month_str = period_month or '' + + # Validate both fields exist + if not (year_str.isdigit() and month_str.isdigit()): + return None, None + + year = int(year_str) + month = int(month_str) + + # First day of this month + dt_start = date(year, month, 1) + + # Compute first day of next month + if month == 12: + next_month = date(year + 1, 1, 1) + else: + next_month = date(year, month + 1, 1) + + # Last day = one day before next month's first day + dt_end = next_month - timedelta(days=1) + + return dt_start, dt_end + + # CREDIT > 0 + def _calculate_exclude_credit(self): + query = [ + ('commission_internal_id.id', '=', self.id), + ('credit', '>', 0), + ('status', '=', False) + ] + lines = self.env['commission.internal.line'].search(query) + for line in lines: + line.helper1 = 'CREDIT' + + # INV/20 + def _calculate_keyword_invoice(self): + query = [ + ('commission_internal_id.id', '=', self.id), + ('helper1', '=', False), + ('label', 'ilike', '%INV/20%'), + ] + lines = self.env['commission.internal.line'].search(query) + # parse label and set helper + for line in lines: + line.helper1 = 'INV/20' + + # ONGKOS KIRIM SO/20 + def _calculate_keyword_deliveryamt(self): + query = [ + ('commission_internal_id.id', '=', self.id), + ('helper1', '=', False), + ('label', 'ilike', '%ONGKOS KIRIM SO/20%'), + ] + lines = self.env['commission.internal.line'].search(query) + for line in lines: + line.helper1 = 'ONGKOS KIRIM SO/20' + + # Payment SO/20 + def _calculate_keyword_payment(self): + query = [ + ('commission_internal_id.id', '=', self.id), + ('helper1', '=', False), + ('label', 'ilike', '%Payment SO/20%'), + ] + lines = self.env['commission.internal.line'].search(query) + for line in lines: + line.helper1 = 'Payment SO/20' + + # UANG MUKA PENJUALAN SO/20 + def _calculate_keyword_dp(self): + query = [ + ('commission_internal_id.id', '=', self.id), + ('helper1', '=', False), + ('label', 'ilike', '%UANG MUKA PENJUALAN SO/20%'), + ] + lines = self.env['commission.internal.line'].search(query) + for line in lines: + line.helper1 = 'UANG MUKA PENJUALAN SO/20' + + def _calculate_keyword_undefined(self): + query = [ + ('commission_internal_id.id', '=', self.id), + ('helper1', '=', False), + ] + lines = self.env['commission.internal.line'].search(query) + for line in lines: + line.helper1 = 'UNDEFINED' + + def _parse_label_helper2(self): + exception = ['CREDIT', 'UNDEFINED'] + query = [ + ('commission_internal_id.id', '=', self.id), + ('helper1', 'not in', exception) + ] + lines = self.env['commission.internal.line'].search(query) + for line in lines: + clean_label = line.label.replace('-', ' ').replace(',', ' ') + list_label = clean_label.split() + list_helper2 = [] + for key in list_label: + clean_key = key.replace(',', '') # delete commas for make sure + if clean_key[:6] == 'INV/20': + list_helper2.append(clean_key) + elif clean_key[:5] == 'SO/20': + list_invoice = self._switch_so_to_inv(clean_key) + str_invoice = ' '.join(list_invoice) + list_helper2.append(str_invoice) + result_helper2 = ' '.join(list_helper2) + line.helper2 = result_helper2 + + def _switch_so_to_inv(self, order): + list_state = ['sale', 'done'] + result = [] + query = [ + ('state', 'in', list_state), + ('name', '=', order) + ] + sales = self.env['sale.order'].search(query) + if sales: + for sale in sales: + if sale.invoice_ids: + for invoice in sale.invoice_ids: + if invoice.state == 'posted': + result.append(invoice.name) + else: + result.append(order) + else: + result.append(order) + return result + + # fill later TODO @stephan + def calculate_commission_internal_result(self): + exception = ['ONGKOS KIRIM SO/20'] + query = [ + ('commission_internal_id.id', '=', self.id), + ('helper2', '!=', False), + ('helper1', 'not in', exception) + ] + lines = self.env['commission.internal.line'].search(query) + all_invoices_and_sales = [] + for line in lines: + list_so = list_invoice = [] + list_key = line.helper2.split() + for key in list_key: + if key[:6] == 'INV/20': + list_invoice.append(key) + if key[:5] == 'SO/20': + list_so.append(key) + invoices = self.env['account.move'].search([('name', 'in', list_invoice)]) + orders = self.env['sale.order'].search([('name', 'in', list_so)]) + invoice_data = invoices.mapped(lambda r: { + 'res_name': 'account.move', + 'res_id': r.id, + 'name': r.name, + 'date': r.invoice_date, + 'customer': r.partner_id.name, + 'salesperson': r.user_id.name, + 'amount_untaxed': r.amount_untaxed, + 'amount_tax': r.amount_tax, + 'amount_total': r.amount_total, + 'uang_masuk_line_id': line.account_move_line_id.id, + 'uang_masuk_id': line.account_move_id.id, + 'date_uang_masuk': line.date, + 'label_uang_masuk': line.label, + 'nomor_uang_masuk': line.number, + 'uang_masuk': line.balance, + # 'linenetamt_prorate': net_amount_prorate, + 'helper1': line.helper1, + 'commission_internal_id': line.commission_internal_id.id, + 'commission_internal_line_id': line.id, + 'helper2': r.state, + }) + sale_data = orders.mapped(lambda r: { + 'res_name': 'sale.order', + 'res_id': r.id, + 'name': r.name, + 'date': r.date_order, + 'customer': r.partner_id.name, + 'salesperson': r.user_id.name, + 'amount_untaxed': r.amount_untaxed, + 'amount_tax': r.amount_tax, + 'amount_total': r.grand_total, + 'uang_masuk_line_id': line.account_move_line_id.id, + 'uang_masuk_id': line.account_move_id.id, + 'date_uang_masuk': line.date, + 'label_uang_masuk': line.label, + 'nomor_uang_masuk': line.number, + 'uang_masuk': line.balance, + # 'linenetamt_prorate': net_amount_prorate, + 'helper1': line.helper1, + 'commission_internal_id': line.commission_internal_id.id, + 'commission_internal_line_id': line.id, + 'helper2': r.state, + }) + invoices_and_sales = invoice_data + sale_data + sum_amount_total = sum(item['amount_total'] for item in invoices_and_sales) + for item in invoices_and_sales: + item['sum_amount_total'] = sum_amount_total + all_invoices_and_sales.extend(invoices_and_sales) + + for data in all_invoices_and_sales: + # total_amount = sum(item.get('amount_total', 0.0) for item in invoices_and_sales) + # net_amount_prorate = data.get('amount_total', 0.0) * prorate + prorate = data.get('amount_total', 0.0) / data.get('sum_amount_total', 0.0) + net_amount_prorate = data.get('uang_masuk', 0.0) * prorate + self.env['commission.internal.result'].create([{ + 'commission_internal_id': data['commission_internal_id'], + 'commission_internal_line_id': data['commission_internal_line_id'], + 'date_doc': data['date'], + 'number': data['name'], + 'res_name': data['res_name'], + 'res_id': data['res_id'], + 'name': data['name'], + 'salesperson': data['salesperson'], + 'totalamt': data['amount_total'], + 'uang_masuk_line_id': data['uang_masuk_line_id'], + 'uang_masuk_id': data['uang_masuk_id'], + 'date_uang_masuk': data['date_uang_masuk'], + 'label_uang_masuk': data['label_uang_masuk'], + 'nomor_uang_masuk': data['nomor_uang_masuk'], + 'uang_masuk': data['uang_masuk'], + 'linenetamt_prorate': net_amount_prorate, + 'helper1': data['helper1'], + 'helper2': data['helper2'] + }]) + print(1) + + # this button / method works for train data in July 2025 + def calculate_commission_internal_from_keyword(self): + if not self.commission_internal_line: + raise UserError('Commission Internal Line kosong, click Copy GL terlebih dahulu') + # execute helper1 for mark keyword + self._calculate_exclude_credit() + self._calculate_keyword_invoice() + self._calculate_keyword_deliveryamt() + self._calculate_keyword_payment() + self._calculate_keyword_dp() + self._calculate_keyword_undefined() + # execute helper2 for parse the label into INV/ or SO/ and switch SO to INV if available + self._parse_label_helper2() + + def generate_commission_from_generate_ledger(self): + if self.commission_internal_line: + raise UserError('Harus hapus semua line jika ingin generate ulang') + if not self.month: + raise UserError('Commission Month harus diisi') + if not self.year: + raise UserError('Commission Year harus diisi') + + dt_start, dt_end = self._get_period_range(self.year, self.month) + + account_bank_ids = self._get_commission_internal_bank_account_ids() + query = [ + ('move_id.state', '=', 'posted'), + ('account_id', 'in', account_bank_ids), + ('date', '>=', dt_start), + ('date', '<=', dt_end) + ] + ledgers = self.env['account.move.line'].search(query) + count = 0 + for ledger in ledgers: + _logger.info("Read General Ledger Account Move Line %s" % ledger.id) + self.env['commission.internal.line'].create([{ + 'commission_internal_id': self.id, + 'date': ledger.date, + 'number': ledger.move_id.name, + 'account_move_id': ledger.move_id.id, + 'account_move_line_id': ledger.id, + 'account_id': ledger.account_id.id, + 'label': ledger.name, + 'debit': ledger.debit, + 'credit': ledger.credit, + 'balance': ledger.balance + }]) + count += 1 + _logger.info("Commission Internal Line generated %s" % count) + + +class CommissionInternalLine(models.Model): + _name = 'commission.internal.line' + _description = 'Line' + _order = 'number asc, id' + + commission_internal_id = fields.Many2one('commission.internal', string='Internal Ref', required=True, + ondelete='cascade', index=True, copy=False) + date = fields.Date(string='Date') + number = fields.Char(string='Number') + account_move_id = fields.Many2one('account.move', string='Account Move') + account_move_line_id = fields.Many2one('account.move.line', string='Account Move Line') + account_id = fields.Many2one('account.account', string='Account') + label = fields.Char(string='Label') + debit = fields.Float(string='Debit') + credit = fields.Float(string='Credit') + balance = fields.Float(string='Balance') + ongkir = fields.Float(string='Ongkir') + refund = fields.Float(string='Refund') + pph = fields.Float(string='PPh23') + others = fields.Float(string='Others') + linenetamt = fields.Float(string='Net Amount') + dpp = fields.Float(string='DPP') + status = fields.Char(string='Status') + salespersons = fields.Char(string='Salespersons') + invoices = fields.Char(string='Invoices') + helper1 = fields.Char(string='Helper1') + helper2 = fields.Char(string='Helper2') + helper3 = fields.Char(string='Helper3') + helper4 = fields.Char(string='Helper4') + helper5 = fields.Char(string='Helper5') + + +class CommissionInternalResult(models.Model): + _name = 'commission.internal.result' + _description = 'Result' + _order = 'number asc, id' + + commission_internal_id = fields.Many2one('commission.internal', string='Internal Ref', required=True, + ondelete='cascade', index=True, copy=False) + commission_internal_line_id = fields.Many2one('commission.internal.line', string='Line Ref') + res_name = fields.Char(string='Res Name') + res_id = fields.Integer(string='Res ID') + date_doc = fields.Date(string='Date Doc') + number = fields.Char(string='Number') + name = fields.Char(string='Name') + salesperson = fields.Char(string='Salesperson') + totalamt = fields.Float(string='Total Amount') + uang_masuk_line_id = fields.Many2one('account.move.line', string='Uang Masuk Line ID') + uang_masuk_id = fields.Many2one('account.move', string='Uang Masuk ID') + date_uang_masuk = fields.Date(string='Date Uang Masuk') + label_uang_masuk = fields.Char(string='Label Uang Masuk') + nomor_uang_masuk = fields.Char(string='Nomor Uang Masuk') + uang_masuk = fields.Float(string='Uang Masuk') + ongkir = fields.Float(string='Ongkir') + refund = fields.Float(string='Refund') + pph = fields.Float(string='PPh23') + others = fields.Float(string='Others') + linenetamt = fields.Float(string='Net Amount') + linenetamt_prorate = fields.Float(string='Net Amount Prorate') + dpp = fields.Float(string='DPP') + status = fields.Char(string='Status') + helper1 = fields.Char(string='Helper1') + helper2 = fields.Char(string='Helper2') + helper3 = fields.Char(string='Helper3') + helper4 = fields.Char(string='Helper4') + helper5 = fields.Char(string='Helper5') diff --git a/indoteknik_custom/models/coretax_fatur.py b/indoteknik_custom/models/coretax_fatur.py index ce94306f..cabcd5d6 100644 --- a/indoteknik_custom/models/coretax_fatur.py +++ b/indoteknik_custom/models/coretax_fatur.py @@ -147,7 +147,7 @@ class CoretaxFaktur(models.Model): subtotal = line_price_subtotal quantity = line_quantity total_discount = round(line_discount, 2) - + coretax_id = line.product_uom_id.coretax_id # Calculate other tax values otherTaxBase = round(subtotal * (11 / 12), 2) if subtotal else 0 vat_amount = round(otherTaxBase * 0.12, 2) @@ -159,7 +159,7 @@ class CoretaxFaktur(models.Model): ET.SubElement(good_service, 'Opt').text = 'A' ET.SubElement(good_service, 'Code').text = '000000' ET.SubElement(good_service, 'Name').text = line_name - ET.SubElement(good_service, 'Unit').text = 'UM.0018' + ET.SubElement(good_service, 'Unit').text = coretax_id # ET.SubElement(good_service, 'Price').text = str(round(line_price_unit, 2)) if line_price_unit else '0' ET.SubElement(good_service, 'Price').text = str(price_per_unit) ET.SubElement(good_service, 'Qty').text = str(quantity) diff --git a/indoteknik_custom/models/dunning_run.py b/indoteknik_custom/models/dunning_run.py index 9feea1d1..2562c305 100644 --- a/indoteknik_custom/models/dunning_run.py +++ b/indoteknik_custom/models/dunning_run.py @@ -150,4 +150,5 @@ class DunningRunLine(models.Model): open_amt = fields.Float(string='Open Amount') due_date = fields.Date(string='Due Date') payment_term = fields.Many2one('account.payment.term', related='invoice_id.invoice_payment_term_id', string='Payment Term') + information_line = fields.Text(string='Information') diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index 534d8122..3312e7fd 100755 --- a/indoteknik_custom/models/purchase_order.py +++ b/indoteknik_custom/models/purchase_order.py @@ -7,6 +7,8 @@ from pytz import timezone, utc import io import base64 from odoo.tools import lazy_property +import socket + try: from odoo.tools.misc import xlsxwriter except ImportError: @@ -120,7 +122,13 @@ class PurchaseOrder(models.Model): string='Show Description', default=True ) - + + @staticmethod + def is_local_env(): + hostname = socket.gethostname().lower() + keywords = ['andri', 'miqdad', 'fin', 'stephan', 'hafid', 'nathan'] + return any(keyword in hostname for keyword in keywords) + @api.onchange('show_description') def onchange_show_description(self): if self.show_description == True: @@ -1123,7 +1131,7 @@ class PurchaseOrder(models.Model): if not self.not_update_purchasepricelist: self.add_product_to_pricelist() for line in self.order_line: - if not line.product_id.public_categ_ids: + if line.product_id.type == 'product' and not line.product_id.categ_id: raise UserError("Product %s kategorinya kosong" % line.product_id.name) if not line.product_id.purchase_ok: raise UserError("Terdapat barang yang tidak bisa diproses") @@ -1139,6 +1147,9 @@ class PurchaseOrder(models.Model): break if send_email: + if self.is_local_env(): + _logger.warning("📪 Local environment detected — skip sending email reminders.") + return self._send_mail() if self.revisi_po: diff --git a/indoteknik_custom/models/refund_sale_order.py b/indoteknik_custom/models/refund_sale_order.py index e6547a88..c6db2174 100644 --- a/indoteknik_custom/models/refund_sale_order.py +++ b/indoteknik_custom/models/refund_sale_order.py @@ -326,8 +326,23 @@ class RefundSaleOrder(models.Model): domain.append(('ref', 'ilike', n)) moves2 = self.env['account.move'].search(domain) + moves3 = self.env['account.move'] + if so_ids: + so_names = self.env['sale.order'].browse(so_ids).mapped('name') + domain = [ + ('journal_id', '=', 11), + ('state', '=', 'posted'), + ('ref', 'ilike', 'uang muka penjualan') + ] + if so_names: + domain += ['|'] * (len(so_names) - 1) + for n in so_names: + domain.append(('ref', 'ilike', n)) + moves3 = self.env['account.move'].search(domain) + has_moves = bool(moves) has_moves2 = bool(moves2) + has_moves3 = bool(moves3) has_piutangmdr = bool(piutangmdr) has_piutangbca = bool(piutangbca) has_misc = bool(misc) @@ -349,6 +364,8 @@ class RefundSaleOrder(models.Model): # sisanya bisa dijumlahkan tanpa konflik if has_moves2: amounts.append(sum(moves2.mapped('amount_total_signed'))) + if has_moves3: + amounts.append(sum(moves3.mapped('amount_total_signed'))) if has_piutangbca: amounts.append(sum(piutangbca.mapped('amount_total_signed'))) if has_piutangmdr: @@ -573,9 +590,10 @@ class RefundSaleOrder(models.Model): domain = [ ('journal_id', '=', 11), ('state', '=', 'posted'), - '|', + '|', '|', ('ref', 'ilike', 'dp'), ('ref', 'ilike', 'payment'), + ('ref', 'ilike', 'uang muka penjualan'), ] domain += ['|'] * (len(so_names) - 1) for n in so_names: @@ -653,6 +671,7 @@ class RefundSaleOrder(models.Model): ('journal_id', '=', 13), ('state', '=', 'posted'), ]) + moves2 = self.env['account.move'] if so_ids: so_records = self.env['sale.order'].browse(so_ids) @@ -668,9 +687,26 @@ class RefundSaleOrder(models.Model): domain.append(('ref', 'ilike', n)) moves2 = self.env['account.move'].search(domain) + + moves3 = self.env['account.move'] + if so_ids: + so_records = self.env['sale.order'].browse(so_ids) + so_names = so_records.mapped('name') + + domain = [ + ('journal_id', '=', 11), + ('state', '=', 'posted'), + ('ref', 'ilike', 'uang muka penjualan') + ] + domain += ['|'] * (len(so_names) - 1) + for n in so_names: + domain.append(('ref', 'ilike', n)) + + moves3 = self.env['account.move'].search(domain) has_moves = bool(moves) has_moves2 = bool(moves2) + has_moves3 = bool(moves3) has_piutangmdr = bool(piutangmdr) has_piutangbca = bool(piutangbca) has_misc = bool(misc) @@ -685,6 +721,8 @@ class RefundSaleOrder(models.Model): amounts.append(sum(moves.mapped('amount_total_signed'))) if has_moves2: amounts.append(sum(moves2.mapped('amount_total_signed'))) + if has_moves3: + amounts.append(sum(moves3.mapped('amount_total_signed'))) if has_piutangbca: amounts.append(sum(piutangbca.mapped('amount_total_signed'))) if has_piutangmdr: @@ -712,39 +750,39 @@ class RefundSaleOrder(models.Model): if self.sale_order_ids: self.partner_id = self.sale_order_ids[0].partner_id - @api.constrains('sale_order_ids') - def _check_sale_orders_payment(self): - """ Validasi SO harus punya uang masuk (Journal Uang Muka / Midtrans) """ - for rec in self: - invalid_orders = [] - - for so in rec.sale_order_ids: - # cari journal uang muka - moves = self.env['account.move'].search([ - ('sale_id', '=', so.id), - ('journal_id', '=', 11), # Journal Uang Muka - ('state', '=', 'posted'), - ]) - piutangbca = self.env['account.move'].search([ - ('ref', 'in', rec.invoice_ids.mapped('name')), - ('journal_id', '=', 4), - ('state', '=', 'posted'), - ]) - piutangmdr = self.env['account.move'].search([ - ('ref', 'in', rec.invoice_ids.mapped('name')), - ('journal_id', '=', 7), - ('state', '=', 'posted'), - ]) - - if not moves and so.payment_status != 'settlement' and not piutangbca and not piutangmdr: - invalid_orders.append(so.name) - - if invalid_orders: - raise ValidationError( - f"Tidak dapat membuat refund untuk SO {', '.join(invalid_orders)} " - "karena tidak memiliki Record Uang Masuk (Journal Uang Muka/Payment Invoice/Midtrans).\n" - "Pastikan semua SO yang dipilih sudah memiliki Record pembayaran yang valid." - ) + # @api.constrains('sale_order_ids') + # def _check_sale_orders_payment(self): + # """ Validasi SO harus punya uang masuk (Journal Uang Muka / Midtrans) """ + # for rec in self: + # invalid_orders = [] + + # for so in rec.sale_order_ids: + # # cari journal uang muka + # moves = self.env['account.move'].search([ + # ('sale_id', '=', so.id), + # ('journal_id', '=', 11), # Journal Uang Muka + # ('state', '=', 'posted'), + # ]) + # piutangbca = self.env['account.move'].search([ + # ('ref', 'in', rec.invoice_ids.mapped('name')), + # ('journal_id', '=', 4), + # ('state', '=', 'posted'), + # ]) + # piutangmdr = self.env['account.move'].search([ + # ('ref', 'in', rec.invoice_ids.mapped('name')), + # ('journal_id', '=', 7), + # ('state', '=', 'posted'), + # ]) + + # if not moves and so.payment_status != 'settlement' and not piutangbca and not piutangmdr: + # invalid_orders.append(so.name) + + # if invalid_orders: + # raise ValidationError( + # f"Tidak dapat membuat refund untuk SO {', '.join(invalid_orders)} " + # "karena tidak memiliki Record Uang Masuk (Journal Uang Muka/Payment Invoice/Midtrans).\n" + # "Pastikan semua SO yang dipilih sudah memiliki Record pembayaran yang valid." + # ) @api.onchange('refund_type') def _onchange_refund_type(self): @@ -989,7 +1027,7 @@ class RefundSaleOrder(models.Model): for rec in self: if self.env.uid not in allowed_user_ids and not is_fat: raise UserError("❌ Hanya user yang bersangkutan atau Finance (FAT) yang bisa melakukan penolakan.") - if rec.status not in ['refund', 'reject']: + if rec.status != 'reject': rec.status = 'reject' rec.status_payment = 'reject' diff --git a/indoteknik_custom/models/res_partner.py b/indoteknik_custom/models/res_partner.py index ef1a5cf4..7f4feb75 100644 --- a/indoteknik_custom/models/res_partner.py +++ b/indoteknik_custom/models/res_partner.py @@ -182,6 +182,7 @@ class ResPartner(models.Model): payment_history_url = fields.Text(string='Payment History URL') is_cbd_locked = fields.Boolean("Locked to CBD?", default=False, tracking=True, help="Jika dicentang, maka partner ini terkunci pada payment term CBD karena memiliki invoice yang sudah jatuh tempo lebih dari 30 hari.") + cbd_lock_date = fields.Datetime("CBD Lock Date", tracking=True, help="Tanggal ketika partner ini dikunci pada payment term CBD.") @api.model @@ -199,6 +200,9 @@ class ResPartner(models.Model): string='Previous Payment Term' ) + property_product_pricelist = fields.Many2one( + tracking=True + ) @api.depends("street", "street2", "city", "state_id", "country_id", "blok", "nomor", "rt", "rw", "kelurahan_id", "kecamatan_id") diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 494aeaa2..2ed4046f 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -398,6 +398,48 @@ class SaleOrder(models.Model): compute="_compute_partner_is_cbd_locked" ) internal_notes_contact = fields.Text(related='partner_id.comment', string="Internal Notes", readonly=True, help="Internal Notes dari contact utama customer.") + is_so_fiktif = fields.Boolean('SO Fiktif?', tracking=3) + team_id = fields.Many2one(tracking=True) + + + + def action_set_shipping_id(self): + for rec in self: + bu_pick = self.env['stock.picking'].search([ + ('state', 'not in', ['cancel']), + ('picking_type_id', '=', 30), + ('sale_id', '=', rec.id), + ('linked_manual_bu_out', '=', False) + ]) + # bu_out = bu_pick_has_out.mapped('linked_manual_bu_out') + bu_out = self.env['stock.picking'].search([ + ('sale_id', '=', rec.id), + ('picking_type_id', '=', 29), + ('state', 'not in', ['cancel', 'done']) + ]) + bu_pick_has_out = self.env['stock.picking'].search([ + ('state', 'not in', ['cancel']), + ('picking_type_id', '=', 30), + ('sale_id', '=', rec.id), + ('linked_manual_bu_out.id', '=', bu_out.id), + ('linked_manual_bu_out.state', 'not in', ['done', 'cancel']) + ]) + for pick in bu_pick_has_out: + linked_out = pick.linked_manual_bu_out + if pick.real_shipping_id != rec.real_shipping_id or linked_out.partner_id != rec.partner_shipping_id: + pick.real_shipping_id = rec.real_shipping_id + pick.partner_id = rec.partner_shipping_id + linked_out.partner_id = rec.partner_shipping_id + linked_out.real_shipping_id = rec.real_shipping_id + _logger.info('Updated bu_pick [%s] and bu_out [%s]', pick.name, linked_out.name) + + for pick in bu_pick: + if pick.real_shipping_id != rec.real_shipping_id: + pick.real_shipping_id = rec.real_shipping_id + pick.partner_id = rec.partner_shipping_id + bu_out.partner_id = rec.partner_shipping_id + bu_out.real_shipping_id = rec.real_shipping_id + _logger.info('Updated bu_pick [%s] without bu_out', pick.name) def action_open_partial_delivery_wizard(self): # raise UserError("Fitur ini sedang dalam pengembangan") @@ -2278,7 +2320,7 @@ class SaleOrder(models.Model): raise UserError("Terdapat DUPLIKASI data pada Product {}".format(line.product_id.display_name)) def sale_order_approve(self): - # self.check_duplicate_product() + self.check_duplicate_product() self.check_product_bom() self.check_credit_limit() self.check_limit_so_to_invoice() @@ -2342,27 +2384,38 @@ class SaleOrder(models.Model): # return self._create_notification_action('Notification', # 'Terdapat invoice yang telah melewati batas waktu, mohon perbarui pada dokumen Due Extension') - if not order.with_context(ask_approval=True)._is_request_to_own_team_leader(): - return self._create_notification_action( - 'Peringatan', - 'Hanya bisa konfirmasi SO tim Anda.' - ) if order._requires_approval_margin_leader(): order.approval_status = 'pengajuan2' + order.message_post(body="Mengajukan approval ke Pimpinan") return self._create_approval_notification('Pimpinan') elif order._requires_approval_margin_manager(): self.check_product_bom() self.check_credit_limit() self.check_limit_so_to_invoice() order.approval_status = 'pengajuan1' + order.message_post(body="Mengajukan approval ke Sales Manager") return self._create_approval_notification('Sales Manager') elif order._requires_approval_team_sales(): self.check_product_bom() self.check_credit_limit() self.check_limit_so_to_invoice() order.approval_status = 'pengajuan1' + order.message_post(body="Mengajukan approval ke Team Sales") return self._create_approval_notification('Team Sales') + + if not order.with_context(ask_approval=True)._is_request_to_own_team_leader(): + return self._create_notification_action( + 'Peringatan', + 'Hanya bisa konfirmasi SO tim Anda.' + ) + user = self.env.user + is_sales_admin = user.id in (3401, 20, 3988, 17340) + if is_sales_admin: + order.approval_status = 'pengajuan1' + order.message_post(body="Mengajukan approval ke Sales") + return self._create_approval_notification('Sales') + raise UserError("Bisa langsung Confirm") def send_notif_to_salesperson(self, cancel=False): @@ -2548,7 +2601,7 @@ class SaleOrder(models.Model): for order in self: order._validate_delivery_amt() order._validate_uniform_taxes() - # order.check_duplicate_product() + order.check_duplicate_product() order.check_product_bom() order.check_credit_limit() order.check_limit_so_to_invoice() @@ -2597,6 +2650,7 @@ class SaleOrder(models.Model): return self._create_approval_notification('Sales Manager') elif order._requires_approval_team_sales(): order.approval_status = 'pengajuan1' + order.message_post(body="Mengajukan approval ke Team Sales") return self._create_approval_notification('Team Sales') order.approval_status = 'approved' @@ -2703,6 +2757,7 @@ class SaleOrder(models.Model): def _requires_approval_team_sales(self): return ( 18 <= self.total_percent_margin <= 24 + # self.total_percent_margin >= 18 and self.env.user.id not in [11, 9, 375] # Eko, Ade, Putra and not self.env.user.is_sales_manager and not self.env.user.is_leader @@ -2716,12 +2771,12 @@ class SaleOrder(models.Model): if user.is_leader or user.is_sales_manager: return True - if user.id in (3401, 20, 3988, 17340): # admin (fida, nabila, ninda) - raise UserError("Yahaha gabisa confirm so, minta ke sales nya ajah") - - if self.env.context.get("ask_approval") and user.id in (3401, 20, 3988): + if self.env.context.get("ask_approval") and user.id in (3401, 20, 3988, 17340): return True - + + if not self.env.context.get("ask_approval") and user.id in (3401, 20, 3988, 17340): # admin (fida, nabila, ninda, feby) + raise UserError("Sales Admin tidak bisa confirm SO, silahkan hubungi Salesperson yang bersangkutan.") + salesperson_id = self.user_id.id approver_id = user.id team_leader_id = self.team_id.user_id.id @@ -3373,6 +3428,9 @@ class SaleOrder(models.Model): # if updated_vals: # line.write(updated_vals) + + if 'real_shipping_id' in vals: + self.action_set_shipping_id() return res def button_refund(self): diff --git a/indoteknik_custom/models/sj_tele.py b/indoteknik_custom/models/sj_tele.py index 53ba26fc..ed363f59 100644 --- a/indoteknik_custom/models/sj_tele.py +++ b/indoteknik_custom/models/sj_tele.py @@ -5,6 +5,7 @@ import json import logging, subprocess import time from collections import OrderedDict +import socket _logger = logging.getLogger(__name__) @@ -20,7 +21,16 @@ class SjTele(models.Model): date_doc_kirim = fields.Datetime(string='Tanggal Kirim SJ') is_sent = fields.Boolean(default=False) + @staticmethod + def is_local_env(): + hostname = socket.gethostname().lower() + keywords = ['andri', 'miqdad', 'fin', 'stephan', 'hafid', 'nathan'] + return any(keyword in hostname for keyword in keywords) + def woi(self): + if self.is_local_env(): + _logger.warning("📪 Local environment detected — skip sending email reminders.") + return bot_mqdd = '8203414501:AAHy_XwiUAVrgRM2EJzW7sZx9npRLITZpb8' chat_id_mqdd = '-1003087280519' api_base = f'https://api.telegram.org/bot{bot_mqdd}' diff --git a/indoteknik_custom/models/stock_move.py b/indoteknik_custom/models/stock_move.py index 8f8ba66f..cac88287 100644 --- a/indoteknik_custom/models/stock_move.py +++ b/indoteknik_custom/models/stock_move.py @@ -186,9 +186,19 @@ class StockMoveLine(models.Model): line_no = fields.Integer('No', default=0) note = fields.Char('Note') manufacture = fields.Many2one('x_manufactures', string="Brands", related="product_id.x_manufacture", store=True) - outstanding_qty = fields.Float( - string='Outstanding Qty', - compute='_compute_delivery_line_status', + qty_yang_mau_dikirim = fields.Float( + string='Qty yang Mau Dikirim', + compute='_compute_delivery_status_detail', + store=False + ) + qty_terkirim = fields.Float( + string='Qty Terkirim', + compute='_compute_delivery_status_detail', + store=False + ) + qty_gantung = fields.Float( + string='Qty Gantung', + compute='_compute_delivery_status_detail', store=False ) delivery_status = fields.Selection([ @@ -196,46 +206,80 @@ class StockMoveLine(models.Model): ('partial', 'Partial'), ('partial_final', 'Partial Final'), ('full', 'Full'), - ], string='Delivery Status', compute='_compute_delivery_line_status', store=False) + ], string='Delivery Status', compute='_compute_delivery_status_detail', store=False) @api.depends('qty_done', 'product_uom_qty', 'picking_id.state') - def _compute_delivery_line_status(self): - for line in self: - line.outstanding_qty = 0.0 - line.delivery_status = 'none' - - picking = line.picking_id - if not picking or picking.picking_type_id.code != 'outgoing': + def _compute_delivery_status_detail(self): + for picking in self: + # Default values + picking.qty_yang_mau_dikirim = 0.0 + picking.qty_terkirim = 0.0 + picking.qty_gantung = 0.0 + picking.delivery_status = 'none' + + # Hanya berlaku untuk pengiriman (BU/OUT) + if picking.picking_id.picking_type_id.code != 'outgoing': continue - total_qty = line.move_id.product_uom_qty or 0 - done_qty = line.qty_done or 0 - - line.outstanding_qty = max(total_qty - done_qty, 0) + if picking.picking_id.name not in ['BU/OUT']: + continue - if total_qty == 0: + move_lines = picking + if not move_lines: continue - if done_qty == 0: - line.delivery_status = 'none' - elif done_qty > 0: - has_other_out = self.env['stock.picking'].search_count([ - ('group_id', '=', picking.group_id.id), - ('name', 'ilike', 'BU/OUT'), - ('id', '!=', picking.id), - ('state', '=', 'done'), - ]) - if has_other_out and done_qty == total_qty: - line.delivery_status = 'partial_final' - elif not has_other_out and done_qty >= total_qty: - line.delivery_status = 'full' - elif has_other_out and done_qty < total_qty: - line.delivery_status = 'partial' - elif done_qty < total_qty: - line.delivery_status = 'partial' - else: - line.delivery_status = 'none' + # ====================== + # HITUNG QTY + # ====================== + total_qty = move_lines.product_uom_qty + + done_qty_total = move_lines.move_id.sale_line_id.qty_delivered + order_qty_total = move_lines.move_id.sale_line_id.product_uom_qty + gantung_qty_total = order_qty_total - done_qty_total - total_qty + + picking.qty_yang_mau_dikirim = total_qty + picking.qty_terkirim = done_qty_total + picking.qty_gantung = gantung_qty_total + + # if total_qty == 0: + # picking.delivery_status = 'none' + # continue + + # if done_qty_total == 0: + # picking.delivery_status = 'none' + # continue + + # ====================== + # CEK BU/OUT LAIN (BACKORDER) + # ====================== + # has_other_out = self.env['stock.picking'].search_count([ + # ('group_id', '=', picking.group_id.id), + # ('name', 'ilike', 'BU/OUT'), + # ('id', '!=', picking.id), + # ('state', 'in', ['assigned', 'waiting', 'confirmed', 'done']), + # ]) + + # ====================== + # LOGIKA STATUS + # ====================== + if gantung_qty_total == 0 and done_qty_total == 0: + # Semua barang udah terkirim, ga ada picking lain + picking.delivery_status = 'full' + + elif gantung_qty_total > 0 and total_qty > 0 and done_qty_total == 0: + # Masih ada picking lain dan sisa gantung → proses masih jalan + picking.delivery_status = 'partial' + + # elif gantung_qty_total > 0: + # # Ini picking terakhir, tapi qty belum full + # picking.delivery_status = 'partial_final' + + elif gantung_qty_total == 0 and done_qty_total > 0 and total_qty > 0: + # Udah kirim semua tapi masih ada picking lain (rare case) + picking.delivery_status = 'partial_final' + else: + picking.delivery_status = 'none' # Ambil uom dari stock move @api.model diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py index 7f8523a3..e7686b75 100644 --- a/indoteknik_custom/models/stock_picking.py +++ b/indoteknik_custom/models/stock_picking.py @@ -5,16 +5,16 @@ from collections import defaultdict from datetime import timedelta, datetime from datetime import timedelta, datetime as waktu from itertools import groupby -import pytz, requests, json, requests +import pytz, json from dateutil import parser import datetime import hmac import hashlib -import base64 import requests import time import logging import re +import base64 _logger = logging.getLogger(__name__) @@ -89,6 +89,7 @@ class StockPicking(models.Model): readonly=True, related="id", ) + sj_documentations = fields.One2many('stock.picking.sj.document','picking_id', string='Dokumentasi SJ (Multi)') sj_documentation = fields.Binary(string="Dokumentasi Surat Jalan") paket_documentation = fields.Binary(string="Dokumentasi Paket") dispatch_documentation = fields.Binary(string="Dokumentasi Dispatch") @@ -198,6 +199,19 @@ class StockPicking(models.Model): ('partial_final', 'Partial Final'), ('full', 'Full'), ], string='Delivery Status', compute='_compute_delivery_status_detail', store=False) + so_num = fields.Char('SO Number', compute='_get_so_num') + is_so_fiktif = fields.Boolean('SO Fiktif?', compute='_compute_is_so_fiktif', tracking=3) + + @api.depends('sale_id.is_so_fiktif') + def _compute_is_so_fiktif(self): + for picking in self: + picking.is_so_fiktif = picking.sale_id.is_so_fiktif if picking.sale_id else False + + + @api.depends('group_id') + def _get_so_num(self): + for record in self: + record.so_num = record.group_id.name @api.depends('move_line_ids_without_package.qty_done', 'move_line_ids_without_package.product_uom_qty', 'state') def _compute_delivery_status_detail(self): @@ -212,6 +226,9 @@ class StockPicking(models.Model): if picking.picking_type_id.code != 'outgoing': continue + if picking.name not in ['BU/OUT']: + continue + move_lines = picking.move_line_ids_without_package if not move_lines: continue @@ -240,12 +257,12 @@ class StockPicking(models.Model): # ====================== # CEK BU/OUT LAIN (BACKORDER) # ====================== - has_other_out = self.env['stock.picking'].search_count([ - ('group_id', '=', picking.group_id.id), - ('name', 'ilike', 'BU/OUT'), - ('id', '!=', picking.id), - ('state', 'in', ['assigned', 'waiting', 'confirmed', 'done']), - ]) + # has_other_out = self.env['stock.picking'].search_count([ + # ('group_id', '=', picking.group_id.id), + # ('name', 'ilike', 'BU/OUT'), + # ('id', '!=', picking.id), + # ('state', 'in', ['assigned', 'waiting', 'confirmed', 'done']), + # ]) # ====================== # LOGIKA STATUS @@ -457,7 +474,6 @@ class StockPicking(models.Model): except ValueError: return False - def action_get_kgx_pod(self, shipment=False): self.ensure_one() @@ -807,6 +823,37 @@ class StockPicking(models.Model): picking.envio_cod_value = data.get("cod_value", 0.0) picking.envio_cod_status = data.get("cod_status") + images_data = data.get('images', []) + for img in images_data: + image_url = img.get('image') + if image_url: + try: + # Download image from URL + img_response = requests.get(image_url) + img_response.raise_for_status() + + # Encode image to base64 + image_base64 = base64.b64encode(img_response.content) + + # Create attachment in Odoo + attachment = self.env['ir.attachment'].create({ + 'name': 'Envio Image', + 'type': 'binary', + 'datas': image_base64, + 'res_model': picking._name, + 'res_id': picking.id, + 'mimetype': 'image/png', + }) + + # Post log note with attachment + picking.message_post( + body="Image Envio", + attachment_ids=[attachment.id] + ) + + except Exception as e: + picking.message_post(body=f"Gagal ambil image Envio: {str(e)}") + # Menyimpan log terbaru logs = data.get("logs", []) if logs and isinstance(logs, list) and logs[0]: @@ -1178,7 +1225,8 @@ class StockPicking(models.Model): self.sale_id.date_doc_kirim = self.date_doc_kirim def action_assign(self): - if self.env.context.get('default_picking_type_id') and ('BU/INPUT' not in self.name or 'BU/PUT' not in self.name): + if self.env.context.get('default_picking_type_id') and ( + 'BU/INPUT' not in self.name or 'BU/PUT' not in self.name): pickings_to_assign = self.filtered( lambda p: not (p.sale_id and p.sale_id.hold_outgoing) ) @@ -1194,18 +1242,15 @@ class StockPicking(models.Model): return res - def ask_approval(self): # if self.env.user.is_accounting: # if self.env.user.is_accounting and self.location_id.id == 57 or self.location_id == 57 and self.approval_status in ['pengajuan1', ''] and 'BU/IU' in self.name and self.approval_status == 'pengajuan1': # raise UserError("Bisa langsung set ke approval logistik") if self.env.user.is_accounting and self.approval_status == "pengajuan2" and 'BU/IU' in self.name: raise UserError("Tidak perlu ask approval sudah approval logistik") - if self.env.user.is_logistic_approver and self.location_id.id == 57 or self.location_id== 57 and self.approval_status == 'pengajuan2' and 'BU/IU' in self.name: + if self.env.user.is_logistic_approver and self.location_id.id == 57 or self.location_id == 57 and self.approval_status == 'pengajuan2' and 'BU/IU' in self.name: raise UserError("Bisa langsung Validate") - - # for calendar distribute only # if self.is_internal_use: # stock_move_lines = self.env['stock.move.line'].search([ @@ -1228,7 +1273,6 @@ class StockPicking(models.Model): raise UserError("Qty tidak boleh 0") pick.approval_status = 'pengajuan1' - def ask_receipt_approval(self): if self.env.user.is_logistic_approver: raise UserError('Bisa langsung validate tanpa Ask Receipt') @@ -1372,6 +1416,8 @@ class StockPicking(models.Model): group_id = self.env.ref('indoteknik_custom.group_role_merchandiser').id users_in_group = self.env['res.users'].search([('groups_id', 'in', [group_id])]) active_model = self.env.context.get('active_model') + if self.is_so_fiktif == True: + raise UserError("SO Fiktif tidak bisa di validate") if self.location_id.id == 47 and self.env.user.id not in users_in_group.mapped( 'id') and self.state_approve_md != 'done': self.state_approve_md = 'waiting' if self.state_approve_md != 'pending' else 'pending' @@ -1394,7 +1440,7 @@ class StockPicking(models.Model): and self.create_date > threshold_datetime and not self.so_lama): raise UserError(_("Tidak ada scan koli! Harap periksa kembali.")) - + if 'BU/OUT/' in self.name: self.driver_departure_date = datetime.datetime.utcnow() @@ -1410,6 +1456,9 @@ class StockPicking(models.Model): if len(self.check_product_lines) == 0 and 'BU/PICK/' in self.name: raise UserError(_("Tidak ada Check Product! Harap periksa kembali.")) + if len(self.check_product_lines) == 0 and 'BU/INPUT/' in self.name: + raise UserError(_("Tidak ada Check Product! Harap periksa kembali.")) + if self.total_koli > self.total_so_koli: raise UserError(_("Total Koli (%s) dan Total SO Koli (%s) tidak sama! Harap periksa kembali.") % (self.total_koli, self.t1otal_so_koli)) @@ -1436,13 +1485,16 @@ class StockPicking(models.Model): # if self.is_internal_use and not self.env.user.is_logistic_approver and self.location_id.id == 57 and self.approval_status == 'pengajuan2': # raise UserError("Harus di Approve oleh Logistik") - if self.is_internal_use and self.approval_status in ['pengajuan1', '', False] and 'BU/IU' in self.name and self.is_bu_iu == True: + if self.is_internal_use and self.approval_status in ['pengajuan1', '', + False] and 'BU/IU' in self.name and self.is_bu_iu == True: raise UserError("Tidak Bisa Validate, set approval status ke approval logistik terlebih dahhulu") - if self.is_internal_use and not self.env.user.is_logistic_approver and self.approval_status in ['pengajuan2'] and self.is_bu_iu == True and 'BU/IU' in self.name: + if self.is_internal_use and not self.env.user.is_logistic_approver and self.approval_status in [ + 'pengajuan2'] and self.is_bu_iu == True and 'BU/IU' in self.name: raise UserError("Harus di Approve oleh Logistik") - if self.is_internal_use and not self.env.user.is_accounting and self.approval_status in ['pengajuan1', '', False] and self.is_bu_iu == False: + if self.is_internal_use and not self.env.user.is_accounting and self.approval_status in ['pengajuan1', '', + False] and self.is_bu_iu == False: raise UserError("Harus di Approve oleh Accounting") if self.picking_type_id.id == 28 and not self.env.user.is_logistic_approver: @@ -1468,7 +1520,6 @@ class StockPicking(models.Model): current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') self.date_reserved = current_time - # Validate Qty Demand Can't higher than Qty Product if self.location_dest_id.id == 58 and 'BU/INPUT/' in self.name: for move in self.move_ids_without_package: @@ -1490,9 +1541,9 @@ class StockPicking(models.Model): self.check_koli() res = super(StockPicking, self).button_validate() - # Penambahan link PO di Stock Journal untuk Picking BD + # Penambahan link PO di Stock Journal for picking in self: - if picking.name and 'BD/' in picking.name and picking.purchase_id: + if picking.name and picking.purchase_id: stock_journal = self.env['account.move'].search([ ('ref', 'ilike', picking.name + '%'), ('journal_id', '=', 3) # Stock Journal ID @@ -1551,6 +1602,10 @@ class StockPicking(models.Model): elif self.tukar_guling_po_id: self.tukar_guling_po_id.update_doc_state() + user = self.env.user + if not user.has_group('indoteknik_custom.group_role_logistic') and 'BU/IU' in self.name: + raise UserWarning('Validate hnaya bisa di lakukan oleh logistik') + return res def automatic_reserve_product(self): @@ -1721,6 +1776,15 @@ class StockPicking(models.Model): 'indoteknik_custom.group_role_logistic') and self.picking_type_code == 'outgoing': raise UserError("Button ini hanya untuk Logistik") + if len(self.check_product_lines) >= 1: + raise UserError("Tidak Bisa cancel karena sudah di check product") + + if not self.env.user.is_logistic_approver and not self.env.user.has_group('indoteknik_custom.group_role_logistic'): + for picking in self: + if picking.name and ('BU/PICK' in picking.name or 'BU/OUT' in picking.name or 'BU/ORT' in picking.name or 'BU/SRT' in picking.name): + if picking.state not in ['cancel']: + raise UserError("Button ini hanya untuk Logistik") + res = super(StockPicking, self).action_cancel() return res @@ -1869,7 +1933,8 @@ class StockPicking(models.Model): 'name': move_line.product_id.name, 'code': move_line.product_id.default_code, 'qty': move_line.qty_done, - 'image': self.env['ir.attachment'].api_image('product.template', 'image_128', move_line.product_id.product_tmpl_id.id), + 'image': self.env['ir.attachment'].api_image('product.template', 'image_128', + move_line.product_id.product_tmpl_id.id), }) response = { @@ -2235,7 +2300,6 @@ class CheckProduct(models.Model): _order = 'picking_id, id' _inherit = ['barcodes.barcode_events_mixin'] - picking_id = fields.Many2one( 'stock.picking', string='Picking Reference', @@ -2688,8 +2752,6 @@ class ScanKoli(models.Model): out_move.qty_done += qty_to_assign qty_koli -= qty_to_assign - - def _reset_qty_done_if_no_scan(self, picking_id): product_bu_pick = self.env['stock.move.line'].search([('picking_id', '=', picking_id)]) @@ -2748,4 +2810,15 @@ class WarningModalWizard(models.TransientModel): def action_continue(self): if self.picking_id: return self.picking_id.with_context(skip_koli_check=True).button_validate() - return {'type': 'ir.actions.act_window_close'}
\ No newline at end of file + return {'type': 'ir.actions.act_window_close'} + + +class StockPickingSjDocument(models.Model): + _name = 'stock.picking.sj.document' + _description = 'Dokumentasi Surat Jalan (Multi)' + _order = 'sequence, id' + _rec_name = 'id' + + picking_id = fields.Many2one('stock.picking', required=True, ondelete='cascade') + image = fields.Binary('Gambar', required=True, attachment=True) + sequence = fields.Integer('Urutan', default=10)
\ No newline at end of file diff --git a/indoteknik_custom/models/tukar_guling.py b/indoteknik_custom/models/tukar_guling.py index 99a74505..aa116ce3 100644 --- a/indoteknik_custom/models/tukar_guling.py +++ b/indoteknik_custom/models/tukar_guling.py @@ -737,14 +737,18 @@ class TukarGuling(models.Model): if mapping_koli and record.operations.picking_type_id.id == 29: for prod in mapping_koli.mapped('product_id'): qty_total = sum(mk.qty_return for mk in mapping_koli.filtered(lambda m: m.product_id == prod)) - move = bu_out.move_lines.filtered(lambda m: m.product_id == prod) - if not move: - raise UserError(f"Move BU/OUT tidak ditemukan untuk produk {prod.display_name}") - srt_return_lines.append((0, 0, { - 'product_id': prod.id, - 'quantity': qty_total, - 'move_id': move.id, - })) + + move_lines = bu_out.move_line_ids.filtered( + lambda ml: ml.product_id == prod and ml.qty_done > 0 and not ml.package_id + ) + + for ml in move_lines: + srt_return_lines.append((0, 0, { + 'product_id': ml.product_id.id, + 'quantity': ml.qty_done, + 'move_id': ml.move_id.id, + })) + _logger.info(f"📟 SRT line: {prod.display_name} | qty={qty_total}") elif not mapping_koli and record.operations.picking_type_id.id == 29: diff --git a/indoteknik_custom/models/tukar_guling_po.py b/indoteknik_custom/models/tukar_guling_po.py index 2a5ca3dd..739898a1 100644 --- a/indoteknik_custom/models/tukar_guling_po.py +++ b/indoteknik_custom/models/tukar_guling_po.py @@ -366,8 +366,8 @@ class TukarGulingPO(models.Model): # if bu_put: # raise UserError("❌ Tidak bisa retur BU/INPUT karena BU/PUT sudah Done!") - if self.operations.picking_type_id.id == 28 and tipe == 'tukar_guling': - raise UserError("❌ BU/INPUT tidak boleh di retur tukar guling") + # if self.operations.picking_type_id.id == 28 and tipe == 'tukar_guling': + # raise UserError("❌ BU/INPUT tidak boleh di retur tukar guling") # if self.operations.picking_type_id.id != 28: # if self._is_already_returned(self.operations): diff --git a/indoteknik_custom/models/uom_uom.py b/indoteknik_custom/models/uom_uom.py new file mode 100644 index 00000000..32e53d73 --- /dev/null +++ b/indoteknik_custom/models/uom_uom.py @@ -0,0 +1,6 @@ +from odoo import fields, models, api, _ + +class Uom(models.Model): + _inherit = 'uom.uom' + + coretax_id = fields.Char(string='Coretax ID') |
