summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/sale_order.py
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2024-09-24 09:03:52 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2024-09-24 09:03:52 +0700
commitcc4be72fc2f0d98f8bc2190ea5c404536bb3c56d (patch)
treea00f22741ba1ec63b59659834a9c6177b20db343 /indoteknik_custom/models/sale_order.py
parent1f9d0136758531831ea6c7a90556c9a472ed8d40 (diff)
parent649f3037e4357dab42d1a8d799e5f2a2f1fd2e52 (diff)
Merge branch 'production' into unreserved_permission
# Conflicts: # indoteknik_custom/models/sale_order.py
Diffstat (limited to 'indoteknik_custom/models/sale_order.py')
-rwxr-xr-xindoteknik_custom/models/sale_order.py119
1 files changed, 111 insertions, 8 deletions
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py
index 0690ffa2..d7b24b34 100755
--- a/indoteknik_custom/models/sale_order.py
+++ b/indoteknik_custom/models/sale_order.py
@@ -38,14 +38,14 @@ class SaleOrder(models.Model):
have_outstanding_po = fields.Boolean('Have Outstanding PO', compute='_have_outstanding_po')
purchase_ids = fields.Many2many('purchase.order', string='Purchases', compute='_get_purchases')
real_shipping_id = fields.Many2one(
- 'res.partner', string='Real Delivery Address', readonly=True, required=True,
+ 'res.partner', string='Real Delivery Address', readonly=True,
states={'draft': [('readonly', False)], 'sent': [('readonly', False)], 'sale': [('readonly', False)]},
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
- help="Dipakai untuk alamat tempel")
+ help="Dipakai untuk alamat tempel", tracking=True)
real_invoice_id = fields.Many2one(
'res.partner', string='Delivery Invoice Address', required=True,
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
- help="Dipakai untuk alamat tempel")
+ help="Dipakai untuk alamat tempel", tracking=True)
fee_third_party = fields.Float('Fee Pihak Ketiga')
so_status = fields.Selection([
('terproses', 'Terproses'),
@@ -113,7 +113,60 @@ class SaleOrder(models.Model):
use_button = fields.Boolean(string='Using Calculate Selling Price', copy=False)
unreserve_id = fields.Many2one('stock.picking', 'Unreserve Picking')
voucher_shipping_id = fields.Many2one(comodel_name='voucher', string='Voucher Shipping', copy=False)
-
+ margin_after_delivery_purchase = fields.Float(string='Margin After Delivery Purchase', compute='_compute_margin_after_delivery_purchase')
+ percent_margin_after_delivery_purchase = fields.Float(string='% Margin After Delivery Purchase', compute='_compute_margin_after_delivery_purchase')
+ purchase_delivery_amt = fields.Float(string='Purchase Delivery Amount', compute='_compute_purchase_delivery_amount')
+ type_promotion = fields.Char(string='Type Promotion', compute='_compute_type_promotion')
+ partner_invoice_id = fields.Many2one(
+ 'res.partner', string='Invoice Address',
+ readonly=True, required=True,
+ states={'draft': [('readonly', False)], 'sent': [('readonly', False)], 'sale': [('readonly', False)]},
+ domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
+ tracking=True, # Menambahkan tracking=True
+ )
+ partner_shipping_id = fields.Many2one(
+ 'res.partner', string='Delivery Address', readonly=True, required=True,
+ states={'draft': [('readonly', False)], 'sent': [('readonly', False)], 'sale': [('readonly', False)]},
+ domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", tracking=True)
+
+ payment_term_id = fields.Many2one(
+ 'account.payment.term', string='Payment Terms', check_company=True, # Unrequired company
+ domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", tracking=True)
+
+ def _compute_type_promotion(self):
+ for rec in self:
+ promotion_types = []
+ for promotion in rec.order_promotion_ids:
+ for line_program in promotion.program_line_id:
+ if line_program.promotion_type:
+ promotion_types.append(dict(line_program._fields['promotion_type'].selection).get(line_program.promotion_type))
+
+ # Removing duplicates by converting to a set, then back to a list
+ rec.type_promotion = ', '.join(sorted(set(promotion_types)))
+
+ def _compute_purchase_delivery_amount(self):
+ for order in self:
+ match = self.env['purchase.order.sales.match']
+ result2 = match.search([
+ ('sale_id.id', '=', order.id)
+ ])
+ delivery_amt = 0
+ for res in result2:
+ delivery_amt = res.delivery_amt
+ order.purchase_delivery_amt = delivery_amt
+
+ def _compute_margin_after_delivery_purchase(self):
+ for order in self:
+ order.margin_after_delivery_purchase = order.total_margin - order.purchase_delivery_amt
+ if order.amount_untaxed == 0:
+ order.percent_margin_after_delivery_purchase = 0
+ continue
+ if order.shipping_cost_covered == 'indoteknik':
+ delivery_amt = order.delivery_amt
+ else:
+ delivery_amt = 0
+ order.percent_margin_after_delivery_purchase = round((order.margin_after_delivery_purchase / (order.amount_untaxed-delivery_amt-order.fee_third_party)) * 100, 2)
+
def _compute_date_kirim(self):
for rec in self:
picking = self.env['stock.picking'].search([('sale_id', '=', rec.id), ('state', 'not in', ['cancel'])], order='date_doc_kirim desc', limit=1)
@@ -393,10 +446,22 @@ class SaleOrder(models.Model):
# return [('id', 'not in', order_ids)]
# return ['&', ('order_line.invoice_lines.move_id.move_type', 'in', ('out_invoice', 'out_refund')), ('order_line.invoice_lines.move_id', operator, value)]
- @api.onchange('partner_shipping_id')
- def onchange_partner_shipping(self):
- self.real_shipping_id = self.partner_shipping_id
- self.real_invoice_id = self.partner_invoice_id
+
+ def check_data_real_delivery_address(self):
+ real_delivery_address = self.real_shipping_id
+
+ if not real_delivery_address.state_id:
+ raise UserError('State Real Delivery Address harus diisi')
+ if not real_delivery_address.zip:
+ raise UserError('Zip code Real Delivery Address harus diisi')
+ if not real_delivery_address.mobile:
+ raise UserError('Mobile Real Delivery Address harus diisi')
+ if not real_delivery_address.phone:
+ raise UserError('Phone Real Delivery Address harus diisi')
+ if not real_delivery_address.kecamatan_id:
+ raise UserError('Kecamatan Real Delivery Address harus diisi')
+ if not real_delivery_address.kelurahan_id:
+ raise UserError('Kelurahan Real Delivery Address harus diisi')
@api.onchange('partner_id')
def onchange_partner_contact(self):
@@ -638,6 +703,9 @@ class SaleOrder(models.Model):
if not order.commitment_date and order.create_date > datetime(2024, 9, 12):
raise UserError("Expected Delivery Date kosong, wajib diisi")
+ if not order.real_shipping_id:
+ UserError('Real Delivery Address harus di isi')
+
if order.validate_partner_invoice_due():
return self._create_notification_action('Notification', 'Terdapat invoice yang telah melewati batas waktu, mohon perbarui pada dokumen Due Extension')
@@ -999,3 +1067,38 @@ class SaleOrder(models.Model):
order_line.tax_id = tax_id
order_line.discount = discount
order_line.order_id.use_button = True
+
+ @api.model
+ def create(self, vals):
+ # Ensure partner details are updated when a sale order is created
+ order = super(SaleOrder, self).create(vals)
+ order._update_partner_details()
+ return order
+
+ def write(self, vals):
+ # Call the super method to handle the write operation
+ res = super(SaleOrder, self).write(vals)
+
+ # Check if the update is coming from a save operation
+ if any(field in vals for field in ['sppkp', 'npwp', 'email', 'customer_type']):
+ self._update_partner_details()
+
+ return res
+
+ def _update_partner_details(self):
+ for order in self:
+ partner = order.partner_id.parent_id or order.partner_id
+ if partner:
+ # Update partner details
+ partner.sppkp = order.sppkp
+ partner.npwp = order.npwp
+ partner.email = order.email
+ partner.customer_type = order.customer_type
+
+ # Save changes to the partner record
+ partner.write({
+ 'sppkp': partner.sppkp,
+ 'npwp': partner.npwp,
+ 'email': partner.email,
+ 'customer_type': partner.customer_type,
+ }) \ No newline at end of file