summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-07-03 10:43:47 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-07-03 10:43:47 +0700
commit15ca04492b335ea314f296d072098e5bb3e5befc (patch)
tree1380e55de1e45e4832f3f0548b2342ee6d907f9d /indoteknik_custom/models
parentbba6f1f21dc0e62ae0a4f65ccbf3e2957cb07f3d (diff)
parent53b7bd51793802ebaa5e1bb9b8d525547158fe8b (diff)
Merge branch 'release' of bitbucket.org:altafixco/indoteknik-addons into release
Diffstat (limited to 'indoteknik_custom/models')
-rwxr-xr-xindoteknik_custom/models/__init__.py1
-rw-r--r--indoteknik_custom/models/account_move_due_extension.py151
-rw-r--r--indoteknik_custom/models/res_partner.py5
-rwxr-xr-xindoteknik_custom/models/sale_order.py136
4 files changed, 281 insertions, 12 deletions
diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py
index 3c6ce46c..166d43ad 100755
--- a/indoteknik_custom/models/__init__.py
+++ b/indoteknik_custom/models/__init__.py
@@ -63,3 +63,4 @@ from . import procurement_monitoring_detail
from . import brand_vendor
from . import manufacturing
from . import requisition
+from . import account_move_due_extension
diff --git a/indoteknik_custom/models/account_move_due_extension.py b/indoteknik_custom/models/account_move_due_extension.py
new file mode 100644
index 00000000..93dfe62b
--- /dev/null
+++ b/indoteknik_custom/models/account_move_due_extension.py
@@ -0,0 +1,151 @@
+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 DueExtension(models.Model):
+ _name = "due.extension"
+ _description = "Due Extension"
+ _inherit = ['mail.thread']
+ _rec_name = 'number'
+
+ number = fields.Char(string='Document No', index=True, copy=False, readonly=True, tracking=True)
+ partner_id = fields.Many2one('res.partner', string="Customer", readonly=True)
+ order_id = fields.Many2one('sale.order', string="SO", readonly=True)
+ due_line = fields.One2many('due.extension.line', 'due_id', string='Due Extension Lines', auto_join=True)
+ old_due = fields.Date(string="Old Due")
+ description = fields.Text(string="Description")
+ is_approve = fields.Boolean(string="Is Approve", readonly=True, tracking=True)
+ approval_status = fields.Selection([
+ ('pengajuan', 'Pengajuan'),
+ ('approved', 'Approved'),
+ ], string='Approval Status', readonly=True, copy=False, index=True, tracking=3)
+ day_extension = fields.Selection([
+ ('3', '3 Hari'),
+ ('7', '7 Hari'),
+ ('14', '14 Hari'),
+ ], string='Day Extension', help='Menambah Due Date yang sudah limit dari hari ini', tracking=True)
+
+ @api.model
+ def create(self, vals):
+ vals['number'] = self.env['ir.sequence'].next_by_code('due.extension') or '0'
+ result = super(DueExtension, self).create(vals)
+ return result
+
+ def due_extension_approval(self):
+ if not self.approval_status:
+ self.approval_status = 'pengajuan'
+ elif self.approval_status == 'pengajuan':
+ raise UserError('Anda sudah mengajukan ask approval')
+ elif self.approval_status == 'approved':
+ raise UserError('Document sudah di approve')
+
+ def due_extension_cancel(self):
+ if self.env.user.is_accounting:
+ if not self.approval_status or self.approval_status == 'pengajuan':
+ self.approval_status = False
+ sales = self.env['sale.order'].search([
+ ('id', '=', self.order_id.id)
+ ])
+
+ sales.action_cancel()
+ elif self.approval_status == 'approved':
+ raise UserError('Document sudah di approve, Tidak bisa di cancel')
+ else:
+ raise UserError('Hanya Finance yang bisa cancel')
+
+ def approve_new_due(self):
+ if self.env.user.is_accounting:
+ self.is_approve = True
+ self.approval_status = 'approved'
+
+ if self.partner_id:
+ if self.day_extension:
+ day_extension = int(self.day_extension)
+ new_due = date.today() + timedelta(days=day_extension)
+
+ for line in self.due_line:
+ line.invoice_id.invoice_date_due = new_due
+
+ if self.order_id._notification_margin_leader():
+ self.order_id.approval_status = 'pengajuan2'
+ return self.order_id._notification_has_margin_leader()
+
+ if self.order_id._notification_margin_manager():
+ self.order_id.approval_status = 'pengajuan1'
+ return self.order_id._notification_has_margin_manager()
+
+ sales = self.env['sale.order'].search([
+ ('id', '=', self.order_id.id)
+ ])
+
+ # sales.state = 'sale'
+ sales.action_confirm()
+ self.order_id.due_id = self.id
+ else:
+ raise UserError('Hanya Finance Yang Bisa Approve')
+
+ def generate_due_line(self):
+ partners = []
+ partners += self.partner_id.child_ids
+ partners.append(self.partner_id)
+
+
+ for partner in partners:
+ query = [
+ ('partner_id', '=', partner.id),
+ ('state', '=', 'posted'),
+ ('move_type', '=', 'out_invoice'),
+ ('amount_residual_signed', '>', 0)
+ ]
+ invoices = self.env['account.move'].search(query, order='invoice_date')
+ count = 0
+
+ for invoice in invoices:
+ if invoice.invoice_day_to_due < 0:
+ self.env['due.extension.line'].create([{
+ 'due_id': self.id,
+ 'partner_id': invoice.partner_id.id,
+ 'invoice_id': invoice.id,
+ 'date_invoice': invoice.invoice_date,
+ 'efaktur_id': invoice.efaktur_id.id,
+ 'reference': invoice.ref,
+ 'total_amt': invoice.amount_total,
+ 'open_amt': invoice.amount_residual_signed,
+ 'due_date': invoice.invoice_date_due
+ }])
+ count += 1
+ _logger.info("Due Extension Line generated %s" % count)
+ def unlink(self):
+ res = super(DueExtension, self).unlink()
+ if not self._name == 'due.extension':
+ raise UserError('Due Extension tidak bisa didelete')
+ return res
+
+
+class DueExtensionLine(models.Model):
+ _name = 'due.extension.line'
+ _description = 'Due Extension Line'
+ _order = 'due_id, id'
+
+ due_id = fields.Many2one('due.extension', string='Due Ref', required=True, ondelete='cascade', index=True, copy=False)
+ partner_id = fields.Many2one('res.partner', string='Customer')
+ invoice_id = fields.Many2one('account.move', string='Invoice')
+ date_invoice = fields.Date(string='Invoice Date')
+ efaktur_id = fields.Many2one('vit.efaktur', string='Faktur Pajak')
+ reference = fields.Char(string='Reference')
+ total_amt = fields.Float(string='Total Amount')
+ open_amt = fields.Float(string='Open Amount')
+ due_date = fields.Date(string='Due Date', compute="_compute_due_date")
+ day_to_due = fields.Integer(string='Day To Due', compute="_compute_day_to_due")
+
+ def _compute_day_to_due(self):
+ for line in self:
+ line.day_to_due = line.invoice_id.invoice_day_to_due
+
+ def _compute_due_date(self):
+ for line in self:
+ line.due_date = line.invoice_id.invoice_date_due
+
diff --git a/indoteknik_custom/models/res_partner.py b/indoteknik_custom/models/res_partner.py
index 77abaaf9..bdeb8a2c 100644
--- a/indoteknik_custom/models/res_partner.py
+++ b/indoteknik_custom/models/res_partner.py
@@ -13,6 +13,11 @@ class ResPartner(models.Model):
company_type_id = fields.Many2one('res.partner.company_type', string='Company Type')
custom_pricelist_id = fields.Many2one('product.pricelist', string='Price Matrix')
group_partner_id = fields.Many2one('group.partner', string='Group Partner')
+ customer_type = fields.Selection([
+ ('pkp', 'PKP'),
+ ('nonpkp', 'Non PKP')
+ ])
+ sppkp = fields.Char(string="SPPKP")
def unlink(self):
if self._name == 'res.partner':
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py
index 9407db30..c5169420 100755
--- a/indoteknik_custom/models/sale_order.py
+++ b/indoteknik_custom/models/sale_order.py
@@ -78,6 +78,13 @@ class SaleOrder(models.Model):
delivery_service_type = fields.Char(string='Delivery Service Type', help='data dari rajaongkir')
grand_total = fields.Monetary(string='Grand Total', help='Amount total + amount delivery', compute='_compute_grand_total')
payment_link_midtrans = fields.Char(string='Payment Link', help='Url payment yg digenerate oleh midtrans, harap diserahkan ke customer agar dapat dilakukan pembayaran secara mandiri')
+ due_id = fields.Many2one('due.extension', string="Due Extension", readonly=True, tracking=True)
+ customer_type = fields.Selection([
+ ('pkp', 'PKP'),
+ ('nonpkp', 'Non PKP')
+ ])
+ sppkp = fields.Char(string="SPPKP")
+ npwp = fields.Char(string="NPWP")
purchase_total = fields.Monetary(string='Purchase Total', compute='_compute_purchase_total')
def _compute_purchase_total(self):
@@ -225,6 +232,15 @@ class SaleOrder(models.Model):
def onchange_partner_shipping(self):
self.real_shipping_id = self.partner_shipping_id
+ @api.onchange('partner_id')
+ def onchange_partner_contact(self):
+ parent_id = self.partner_id.parent_id
+ parent_id = parent_id if parent_id else self.partner_id
+
+ self.npwp = parent_id.npwp
+ self.sppkp = parent_id.sppkp
+ self.customer_type = parent_id.customer_type
+
def _get_purchases(self):
po_state = ['done', 'draft', 'purchase']
for order in self:
@@ -300,6 +316,7 @@ class SaleOrder(models.Model):
def sale_order_approve(self):
# raise UserError("Bisa langsung Confirm")
self.check_due()
+
for order in self:
if order.warehouse_id.id != 8: #GD Bandengan
raise UserError('Gudang harus Bandengan')
@@ -336,12 +353,16 @@ class SaleOrder(models.Model):
raise UserError(_('Tidak bisa Confirm menggunakan Produk Sementara'))
if not line.vendor_id or not line.purchase_price:
raise UserError(_('Isi Vendor dan Harga Beli sebelum Request Approval'))
- if order.total_percent_margin <= 15 and not self.env.user.is_leader:
+
+ if order.validate_partner_invoice_due():
+ return self._notification_has_unapprove_due()
+
+ if order._notification_margin_leader():
order.approval_status = 'pengajuan2'
- elif order.total_percent_margin <= 22 and not self.env.user.is_leader and not self.env.user.is_sales_manager:
- order.approval_status = 'pengajuan1'
- elif order._have_outstanding_invoices() and not self.env.user.is_leader and not self.env.user.is_sales_manager:
+ return self._notification_has_margin_leader()
+ elif order._notification_margin_manager():
order.approval_status = 'pengajuan1'
+ return self._notification_has_margin_manager()
else:
raise UserError("Bisa langsung Confirm")
@@ -358,10 +379,97 @@ class SaleOrder(models.Model):
# raise UserError("PO harus di Cancel dahulu")
self.approval_status = False
+ self.due_id = False
return super(SaleOrder, self).action_cancel()
+
+ def validate_partner_invoice_due(self):
+ parent_id = self.partner_id.parent_id.id
+ parent_id = parent_id if parent_id else self.partner_id.id
+
+ if self.due_id and self.due_id.is_approve == False:
+ raise UserError('Document Over Due Yang Anda Buat Belum Di Approve')
+
+ if not self.env.user.is_leader and not self.env.user.is_sales_manager:
+ query = [
+ ('partner_id', '=', parent_id),
+ ('state', '=', 'posted'),
+ ('move_type', '=', 'out_invoice'),
+ ('amount_residual_signed', '>', 0)
+ ]
+ invoices = self.env['account.move'].search(query, order='invoice_date')
+ due_extension = self.env['due.extension'].create([{
+ 'partner_id': parent_id,
+ 'day_extension': '3',
+ 'order_id': self.id,
+ }])
+ due_extension.generate_due_line()
+ self.due_id = due_extension.id
+ if len(self.due_id.due_line) > 0:
+ return True
+ else:
+ due_extension.unlink()
+ return False
+
+ def _notification_margin_leader(self):
+ if self.total_percent_margin <= 15 and not self.env.user.is_leader:
+ return True
+ else:
+ return False
+
+ def _notification_margin_manager(self):
+ if self.total_percent_margin <= 22 and not self.env.user.is_leader and not self.env.user.is_sales_manager:
+ return True
+ else:
+ return False
+
+ def _notification_has_unapprove_due(self):
+ return {
+ 'type': 'ir.actions.client',
+ 'tag': 'display_notification',
+ 'params': {
+ 'title': 'Notification',
+ 'message': 'Ada Invoice Yang Sudah Over Due, Silahkan Memperbarui Over Due di Due Extension',
+ 'next': {'type': 'ir.actions.act_window_close'},
+ }
+ }
+
+ def _notification_has_margin_leader(self):
+ return {
+ 'type': 'ir.actions.client',
+ 'tag': 'display_notification',
+ 'params': {
+ 'title': 'Notification',
+ 'message': 'SO Harus Di Approve Oleh Pimpinan',
+ 'next': {'type': 'ir.actions.act_window_close'},
+ }
+ }
+
+ def _notification_has_margin_manager(self):
+ return {
+ 'type': 'ir.actions.client',
+ 'tag': 'display_notification',
+ 'params': {
+ 'title': 'Notification',
+ 'message': 'SO Harus Di Approve Oleh Sales Manager',
+ 'next': {'type': 'ir.actions.act_window_close'},
+ }
+ }
+
+ def _set_sppkp_npwp_contact(self):
+ parent_id = self.partner_id.parent_id
+ parent_id = parent_id if parent_id else self.partner_id
+
+ for contact in self:
+ partner_customer_type = contact.customer_type
+ parent_id.customer_type = partner_customer_type
+ partner_npwp = contact.npwp
+ parent_id.npwp = partner_npwp
+
+ partner_sppkp = contact.sppkp
+ parent_id.sppkp = partner_sppkp
+
def action_confirm(self):
- res = super(SaleOrder, self).action_confirm()
for order in self:
if order.warehouse_id.id != 8: #GD Bandengan
raise UserError('Gudang harus Bandengan')
@@ -380,16 +488,20 @@ class SaleOrder(models.Model):
raise UserError(_('Tidak bisa Confirm menggunakan Produk Sementara'))
if not line.vendor_id or not line.purchase_price or not line.purchase_tax_id:
raise UserError(_('Isi Vendor, Harga Beli, dan Tax sebelum Request Approval'))
- if order.total_percent_margin <= 15 and not self.env.user.is_leader:
- raise UserError("Harus diapprove oleh Pimpinan")
- elif order.total_percent_margin <= 22 and not self.env.user.is_leader and not self.env.user.is_sales_manager:
- raise UserError("Harus diapprove oleh Manager")
- elif order._have_outstanding_invoices() and not self.env.user.is_leader and not self.env.user.is_sales_manager:
- raise UserError("Ada invoice due date, harus diapprove oleh Manager")
+
+ if order.validate_partner_invoice_due():
+ return self._notification_has_unapprove_due()
+
+ if order._notification_margin_leader():
+ return self._notification_has_margin_leader()
+ elif order._notification_margin_manager():
+ return self._notification_has_margin_manager()
else:
order.approval_status = 'approved'
- order.calculate_line_no()
+ order._set_sppkp_npwp_contact()
+ order.calculate_line_no()
+ res = super(SaleOrder, self).action_confirm()
return res
def _have_outstanding_invoices(self):