diff options
| -rwxr-xr-x | indoteknik_custom/models/purchase_order.py | 50 | ||||
| -rwxr-xr-x | indoteknik_custom/models/purchase_order_line.py | 32 | ||||
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 115 | ||||
| -rwxr-xr-x | indoteknik_custom/views/purchase_order.xml | 8 | ||||
| -rwxr-xr-x | indoteknik_custom/views/sale_order.xml | 6 |
5 files changed, 162 insertions, 49 deletions
diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index cb048182..fd02cd2b 100755 --- a/indoteknik_custom/models/purchase_order.py +++ b/indoteknik_custom/models/purchase_order.py @@ -5,7 +5,20 @@ class PurchaseOrder(models.Model): _inherit = 'purchase.order' sale_order_id = fields.Many2one('sale.order', string='Sale Order') - procurement_status = fields.Char(string='Procurement Status', compute='get_procurement_status',readonly=True) + procurement_status = fields.Char(string='Procurement Status', compute='get_procurement_status', readonly=True) + total_margin = fields.Float( + 'Total Margin', compute='compute_total_margin', + help="Total Margin in Sales Order Header") + total_percent_margin = fields.Float( + 'Total Percent Margin', compute='compute_total_margin', + help="Total % Margin in Sales Order Header") + approval_status = fields.Selection([ + ('pengajuan1', 'Approval Manager'), #siapa? + ('pengajuan2', 'Approval Pimpinan'), #akbar + ('approved', 'Approved'), + ], string='Approval Status', readonly=True, copy=False, index=True, tracking=3) + count_line_product = fields.Float('Count Line Product', compute='compute_count_line_product') + delivery_amount = fields.Float('Delivery Amount', compute='compute_delivery_amount') def get_procurement_status(self): for purchase_order in self: @@ -44,3 +57,38 @@ class PurchaseOrder(models.Model): } self.env['purchase.order.line'].sudo().create(values) + def compute_count_line_product(self): + for order in self: + count = 0 + for line in order.order_line: + if line.product_id.type == 'product': + count += 1 + if count == 0: + order.count_line_product = 1 + else: + order.count_line_product = count + + def compute_delivery_amount(self): + for order in self: + amount = 0 + for line in order.order_line: + if line.product_id.type == 'service': + amount += line.price_total + order.delivery_amount = amount + + def compute_total_margin(self): + for po in self: + po.total_margin = 0 + po.total_percent_margin = 0 + for po in self: + total_margin = total_percent_margin = 0 + for line in po.order_line: + if not line.product_id: + po.total_margin = 0 + po.total_percent_margin = 0 + continue + total_margin += line.item_margin + po.total_margin = total_margin + if po.amount_untaxed > 0: + total_percent_margin = round((total_margin / po.amount_untaxed), 2) * 100 + po.total_percent_margin = total_percent_margin diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py index b4be9ffc..24afcab6 100755 --- a/indoteknik_custom/models/purchase_order_line.py +++ b/indoteknik_custom/models/purchase_order_line.py @@ -4,6 +4,15 @@ from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT class PurchaseOrderLine(models.Model): _inherit = 'purchase.order.line' + sales_price = fields.Float( + 'Sales Price', compute='compute_item_margin', + help="Sales Price in Purchase Order Line") + item_margin = fields.Float( + 'Total Margin', compute='compute_item_margin', + help="Total Margin in Purchase Order Line") + item_percent_margin = fields.Float( + 'Total Percent Margin', compute='compute_item_margin', + help="Total % Margin in Purchase Order Line") # Override method from addons/purchase/models/purchase.py @api.onchange('product_qty', 'product_uom') @@ -25,3 +34,26 @@ class PurchaseOrderLine(models.Model): self.price_unit = price_unit return res + + def compute_item_margin(self): + for line in self: + if not line.product_id or line.price_unit <= 0 or line.product_uom_qty <= 0 or line.product_id.type == 'service': + line.item_margin = 0 + line.item_percent_margin = 0 + continue + sale_order_line = self.env['sale.order.line'].search( + [('product_id', '=', line.product_id.id), + ('order_id', '=', line.order_id.sale_order_id.id)], limit=1, order='price_reduce_taxexcl') + + # untaxed sales price + sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty + # must add expedition price in sales_price, expedition payed by customer? + # sales_price -= round((line.order_id.sale_order_id.delivery_amount / line.order_id.sale_order_id.count_line_product), 2) + # untaxed purchase price + purchase_price = line.price_subtotal + # must add expedition price in purchase_price as expenses + purchase_price += round((line.order_id.delivery_amount / line.order_id.count_line_product), 2) + margin_per_item = sales_price - purchase_price + line.item_margin = margin_per_item + if sales_price > 0: + line.item_percent_margin = round((margin_per_item / sales_price), 2) * 100
\ No newline at end of file diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 1db8a445..7366d67a 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -19,6 +19,9 @@ class SaleOrder(models.Model): ], string='Approval Status', readonly=True, copy=False, index=True, tracking=3) carrier_id = fields.Many2one('delivery.carrier', string='Shipping Method') have_visit_service = fields.Boolean(string='Have Visit Service', help='To compute is customer get visit service', compute='_compute_have_visit_service') + count_line_product = fields.Float('Count Line Product', compute='compute_count_line_product') + delivery_amount = fields.Float('Delivery Amount', compute='compute_delivery_amount') + def _compute_have_visit_service(self): limit = 20000000 @@ -26,39 +29,56 @@ class SaleOrder(models.Model): if self.amount_total > limit: self.have_visit_service = True - # def sale_order_approve(self): - # for order in self: - # if order.state == 'cancel' or order.state == 'done' or order.state == 'sale': - # raise UserError("Status harus draft atau sent") - # approval1 = approval2 = 0 - # for line in order.order_line: - # if not line.product_id: - # continue - # if (line.item_percent_margin <= 15 or line.item_percent_margin == 100) and ( - # self.env.user.id != 6 and self.env.user.id != 7): - # approval2 += 1 - # # order.approval_status = "pengajuan2" - # # break - # elif line.item_percent_margin <= 40 and (self.env.user.id != 8 and self.env.user.id != 6 and self.env.user.id != 7): - # approval1 += 1 - # # order.approval_status = 'pengajuan1' - # # break - # if approval2 > 0: - # order.approval_status = 'pengajuan2' - # elif approval1 > 0: - # order.approval_status = 'pengajuan1' - # else: - # raise UserError("Bisa langsung Confirm") + def sale_order_approve(self): + raise UserError("Bisa langsung Confirm") + # for order in self: + # if order.state == 'cancel' or order.state == 'done' or order.state == 'sale': + # raise UserError("Status harus draft atau sent") + # approval1 = approval2 = 0 + # for line in order.order_line: + # if not line.product_id: + # continue + # if (line.item_percent_margin <= 15 or line.item_percent_margin == 100) and ( + # self.env.user.id != 6 and self.env.user.id != 7): + # approval2 += 1 + # # order.approval_status = "pengajuan2" + # # break + # elif line.item_percent_margin <= 40 and (self.env.user.id != 8 and self.env.user.id != 6 and self.env.user.id != 7): + # approval1 += 1 + # # order.approval_status = 'pengajuan1' + # # break + # if approval2 > 0: + # order.approval_status = 'pengajuan2' + # elif approval1 > 0: + # order.approval_status = 'pengajuan1' + # else: + # raise UserError("Bisa langsung Confirm") - # def action_cancel(self): - # self.approval_status = False - # return super(SaleOrder, self).action_cancel() + def action_cancel(self): + # self.approval_status = False + return super(SaleOrder, self).action_cancel() def action_confirm(self): - for line in self.order_line: - if line.product_id.id == 232383: - raise UserError(_('Tidak bisa Confirm menggunakan Produk Sementara')) res = super(SaleOrder, self).action_confirm() + # for line in self.order_line: + # if line.product_id.id == 232383: + # raise UserError(_('Tidak bisa Confirm menggunakan Produk Sementara')) + # for order in self: + # approval1 = approval2 = 0 + # for line in order.order_line: + # if not line.product_id: + # continue + # if (line.item_percent_margin <= 15 or line.item_percent_margin == 100) and ( + # self.env.user.id != 6 and self.env.user.id != 7): + # approval2 += 1 + # elif line.item_percent_margin <= 40 and ( + # self.env.user.id != 8 and self.env.user.id != 6 and self.env.user.id != 7): + # approval1 += 1 + # if approval2 > 0: + # raise UserError("Need Tyas / Akbar Approval, atau Approval manual dan lampirkan di Log Internal") + # elif approval1 > 0: + # raise UserError("Need Adela Approval") + # order.approval_status = 'approved' return res def compute_total_margin(self): @@ -75,25 +95,24 @@ class SaleOrder(models.Model): total_percent_margin = round((total_margin / order.amount_untaxed), 4) * 100 order.total_percent_margin = total_percent_margin - # def action_confirm(self): - # res = super(SaleOrder, self).action_confirm() - # for order in self: - # approval1 = approval2 = 0 - # for line in order.order_line: - # if not line.product_id: - # continue - # if (line.item_percent_margin <= 15 or line.item_percent_margin == 100) and ( - # self.env.user.id != 6 and self.env.user.id != 7): - # approval2 += 1 - # elif line.item_percent_margin <= 40 and ( - # self.env.user.id != 8 and self.env.user.id != 6 and self.env.user.id != 7): - # approval1 += 1 - # if approval2 > 0: - # raise UserError("Need Tyas / Akbar Approval, atau Approval manual dan lampirkan di Log Internal") - # elif approval1 > 0: - # raise UserError("Need Adela Approval") - # order.approval_status = 'approved' - # return res + def compute_count_line_product(self): + for order in self: + count = 0 + for line in order.order_line: + if line.product_id.type == 'product': + count += 1 + if count == 0: + order.count_line_product = 1 + else: + order.count_line_product = count + + def compute_delivery_amount(self): + for order in self: + amount = 0 + for line in order.order_line: + if line.product_id.type == 'service': + amount += line.price_total + order.delivery_amount = amount class SaleOrderLine(models.Model): diff --git a/indoteknik_custom/views/purchase_order.xml b/indoteknik_custom/views/purchase_order.xml index f209d18a..7df2ca9f 100755 --- a/indoteknik_custom/views/purchase_order.xml +++ b/indoteknik_custom/views/purchase_order.xml @@ -17,6 +17,14 @@ <field name="date_order" position="before"> <field name="sale_order_id" attrs="{'readonly': [('state', 'not in', ['draft'])]}"/> </field> + <xpath expr="//form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='price_subtotal']" position="after"> + <field name="item_margin"/> + <field name="item_percent_margin"/> + </xpath> + <field name="amount_total" position="after"> + <field name="total_margin"/> + <field name="total_percent_margin"/> + </field> </field> </record> </data> diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml index 19182a6b..b2466b0d 100755 --- a/indoteknik_custom/views/sale_order.xml +++ b/indoteknik_custom/views/sale_order.xml @@ -6,6 +6,12 @@ <field name="model">sale.order</field> <field name="inherit_id" ref="sale.view_order_form"/> <field name="arch" type="xml"> + <button id="action_confirm" position="after"> + <button name="sale_order_approve" + string="Ask Approval" + type="object" + /> + </button> <field name="payment_term_id" position="after"> <field name="approval_status" /> </field> |
