diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-08-18 11:42:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-08-18 11:42:50 +0700 |
| commit | 9a97cf1790f913a9fcb9ea4744154305d6ca1ced (patch) | |
| tree | 9be27427180db8f7778bcd8cf8350e0464f06ce6 | |
| parent | 2d400be8110ebc7471022455c59a135658d014ed (diff) | |
sales order approval
| -rwxr-xr-x | indoteknik_custom/__manifest__.py | 1 | ||||
| -rwxr-xr-x | indoteknik_custom/models/__init__.py | 1 | ||||
| -rwxr-xr-x | indoteknik_custom/models/purchase_order_line.py | 3 | ||||
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 113 | ||||
| -rwxr-xr-x | indoteknik_custom/views/sale_order.xml | 41 |
5 files changed, 159 insertions, 0 deletions
diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index 58b8a3df..61b51eed 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -30,6 +30,7 @@ 'views/x_product_tags.xml', 'views/stock_vendor.xml', 'views/crm_lead.xml', + 'views/sale_order.xml', 'report/report.xml', 'report/report_banner_banner.xml', 'report/report_banner_banner2.xml', diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 02e223fd..a94ed2d3 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -16,3 +16,4 @@ from . import purchase_pricelist from . import purchase_order_line from . import sale_monitoring_detail from . import sale_monitoring +from . import sale_order diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py index 74b46e4c..22d65ee8 100755 --- a/indoteknik_custom/models/purchase_order_line.py +++ b/indoteknik_custom/models/purchase_order_line.py @@ -81,3 +81,6 @@ class PurchaseOrderLine(models.Model): price_unit = product_supplierinfo.price self.price_unit = price_unit + return { + 'warning': {'title': "Warning", 'message': 'ABCDEF', 'type': 'notification'}, + } diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py new file mode 100755 index 00000000..ed2676b8 --- /dev/null +++ b/indoteknik_custom/models/sale_order.py @@ -0,0 +1,113 @@ +from odoo import fields, models, api, _ +from odoo.exceptions import AccessError, UserError, ValidationError + +import warnings + + +class SaleOrder(models.Model): + _inherit = 'sale.order' + 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.Integer('Approval Status', copy=False) + approval_status = fields.Selection([ + ('pengajuan1', 'Approval Adela'), + ('pengajuan2', 'Approval Tyas'), + ('approved', 'Approved'), + ], string='Approval Status', readonly=True, copy=False, index=True, tracking=3) + + def sale_order_approve(self): + for order in self: + for line in order.order_line: + if (line.item_percent_margin <= 15 or line.item_percent_margin == 100) and self.env.user.id != 6: + order.approval_status = "pengajuan2" + break + elif line.item_percent_margin <= 40 and (self.env.user.id != 8 or self.env.user.id != 7): + order.approval_status = 'pengajuan1' + break + else: + raise UserError("Bisa langsung Confirm") + + def action_cancel(self): + cancel_warning = self._show_cancel_wizard() + if cancel_warning: + return { + 'name': _('Cancel Sales Order'), + 'view_mode': 'form', + 'res_model': 'sale.order.cancel', + 'view_id': self.env.ref('sale.sale_order_cancel_view_form').id, + 'type': 'ir.actions.act_window', + 'context': {'default_order_id': self.id}, + 'target': 'new' + } + inv = self.invoice_ids.filtered(lambda inv: inv.state == 'draft') + inv.button_cancel() + for order in self: + order.approval_status = False + return self.write({'state': 'cancel'}) + + def compute_total_margin(self): + for order in self: + total_margin = total_percent_margin = 0 + for line in order.order_line: + total_margin += line.item_margin + order.total_margin = total_margin + total_percent_margin = total_margin / order.amount_untaxed * 100 + order.total_percent_margin = total_percent_margin + + def action_confirm(self): + if self._get_forbidden_state_confirm() & set(self.mapped('state')): + raise UserError(_( + 'It is not allowed to confirm an order in the following states: %s' + ) % (', '.join(self._get_forbidden_state_confirm()))) + + # custom approval start here + for order in self: + for line in order.order_line: + if (line.item_percent_margin <= 15 or line.item_percent_margin == 100) and self.env.user.id != 6: + raise UserError("Need Tyas / Akbar Approval") + elif line.item_percent_margin <= 40 and self.env.user.id != 8: + raise UserError("Need Adela Approval") + order.approval_status = 'approved' + + for order in self.filtered(lambda order: order.partner_id not in order.message_partner_ids): + order.message_subscribe([order.partner_id.id]) + self.write(self._prepare_confirmation_values()) + + # Context key 'default_name' is sometimes propagated up to here. + # We don't need it and it creates issues in the creation of linked records. + context = self._context.copy() + context.pop('default_name', None) + + self.with_context(context)._action_confirm() + if self.env.user.has_group('sale.group_auto_done_setting'): + self.action_done() + # return True + return { + 'warning': {'title': "Warning", 'message': 'ABCDEF', 'type': 'notification'}, + } + + +class SaleOrderLine(models.Model): + _inherit = 'sale.order.line' + item_margin = fields.Float( + 'Total Margin', compute='compute_item_margin', + help="Total Margin in Sales Order Header") + item_percent_margin = fields.Float( + 'Total Percent Margin', compute='compute_item_margin', + help="Total % Margin in Sales Order Header") + + def compute_item_margin(self): + for line in self: + subtotal_untaxed = line.price_subtotal + purchase_pricelist = self.env['purchase.pricelist'].search( + [('product_id', '=', line.product_id.id)], limit=1, order='product_price') + purchase_pricelist_untaxed = 0 + if purchase_pricelist.product_price > 0: + purchase_pricelist_untaxed = purchase_pricelist.product_price / 1.11 + margin_per_item = subtotal_untaxed - (purchase_pricelist_untaxed * line.product_uom_qty) + line.item_margin = margin_per_item + line.item_percent_margin = margin_per_item / subtotal_untaxed * 100 diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml new file mode 100755 index 00000000..e0b4ab4d --- /dev/null +++ b/indoteknik_custom/views/sale_order.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<odoo> + <data> + <record id="sale_order_form_view_inherit" model="ir.ui.view"> + <field name="name">Sale Order</field> + <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> + <xpath expr="//form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='price_total']" position="after"> + <field name="item_margin" groups="sales_team.group_sale_manager"/> + <field name="item_percent_margin" groups="sales_team.group_sale_manager"/> + </xpath> + <field name="amount_total" position="after"> + <field name="total_margin" groups="sales_team.group_sale_manager"/> + <field name="total_percent_margin" groups="sales_team.group_sale_manager"/> + </field> + </field> + </record> + </data> + <data> + <record id="sale_order_tree_view_inherit" model="ir.ui.view"> + <field name="name">Sale Order</field> + <field name="model">sale.order</field> + <field name="inherit_id" ref="sale.view_quotation_tree_with_onboarding"/> + <field name="arch" type="xml"> + <field name="state" position="after"> + <field name="approval_status" /> + </field> + </field> + </record> + </data> +</odoo>
\ No newline at end of file |
