diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-13 09:50:57 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-13 09:50:57 +0700 |
| commit | 32bf7b115ec71e72d9cde58bfa3c0304c4b1ffcb (patch) | |
| tree | 19257db60cdd44a5527825825ab0032a2693f74d /indoteknik_custom/models/sale_order.py | |
| parent | 3085bfa4333cbc99ed4a0e432c8313cf7009cd2a (diff) | |
| parent | 604ef36b09c2eb2cf89f5b592ab775ba87e0ce88 (diff) | |
Merge remote-tracking branch 'origin/staging' into real-stock
Diffstat (limited to 'indoteknik_custom/models/sale_order.py')
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 160 |
1 files changed, 146 insertions, 14 deletions
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 0a794f6d..5a3cada9 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -78,6 +78,21 @@ 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): + for order in self: + total = 0 + for line in order.order_line: + total += line.vendor_subtotal + order.purchase_total = total def generate_payment_link_midtrans_sales_order(self): # midtrans_url = 'https://app.sandbox.midtrans.com/snap/v1/transactions' # dev - sandbox @@ -217,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: @@ -292,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') @@ -324,16 +349,20 @@ class SaleOrder(models.Model): # must add product can sell validation if not line.product_id.product_tmpl_id.sale_ok: raise UserError('Product %s belum bisa dijual, harap hubungi finance' % line.product_id.display_name) - if line.product_id.id == 232383: + if line.product_id.id == 224484: 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") @@ -350,10 +379,91 @@ 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 + parent_id.customer_type = self.customer_type + parent_id.npwp = self.npwp + parent_id.sppkp = self.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') @@ -372,16 +482,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): @@ -462,7 +576,7 @@ class SaleOrderLine(models.Model): 'res.partner', string='Vendor', readonly=True, states={'draft': [('readonly', False)], 'sent': [('readonly', False)]}, change_default=True, index=True, tracking=1, - domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", ) + domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",) purchase_price = fields.Float('Purchase', required=True, digits='Product Price', default=0.0) purchase_tax_id = fields.Many2one('account.tax', string='Tax', domain=['|', ('active', '=', False), ('active', '=', True)]) @@ -470,6 +584,23 @@ class SaleOrderLine(models.Model): fee_third_party_line = fields.Float('FeeThirdPartyLine', compute='compute_fee_third_party_line', default=0) line_no = fields.Integer('No', default=0, copy=False) note_procurement = fields.Char(string='Note', help="Harap diisi jika ada keterangan tambahan dari Procurement, agar dapat dimonitoring") + vendor_subtotal = fields.Float(string='Vendor Subtotal', compute="_compute_vendor_subtotal") + + def _compute_vendor_subtotal(self): + for line in self: + if line.purchase_price > 0 and line.product_uom_qty > 0: + # product = line.product_id + + # if product: + # vendor_price = line.purchase_price + # if line.purchase_tax_id.price_include: + # vendor_price = line.purchase_price + # else: + # vendor_price = line.purchase_price + (line.purchase_price*11/100) + subtotal = line.purchase_price * line.product_uom_qty + line.vendor_subtotal = subtotal + else: + line.vendor_subtotal = 0 def compute_item_margin(self): for line in self: @@ -530,6 +661,7 @@ class SaleOrderLine(models.Model): [('product_id', '=', self.product_id.id)], limit=1, order='product_price ASC') line.vendor_id = purchase_price.vendor_id line.tax_id = line.order_id.sales_tax_id + line.purchase_price = purchase_price.product_price def compute_delivery_amt_line(self): for line in self: |
