From f03e6dc2ee2f3cd34eca15e81cd8964c07089ef4 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Thu, 12 Sep 2024 10:26:48 +0700 Subject: new window --- indoteknik_custom/__manifest__.py | 1 + indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/approval_unreserve.py | 69 ++++++++++++++++++++++++ indoteknik_custom/security/ir.model.access.csv | 2 + indoteknik_custom/views/approval_unreserve.xml | 75 ++++++++++++++++++++++++++ indoteknik_custom/views/ir_sequence.xml | 10 ++++ 6 files changed, 158 insertions(+) create mode 100644 indoteknik_custom/models/approval_unreserve.py create mode 100644 indoteknik_custom/views/approval_unreserve.xml diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index 8d8e8cec..e1a67592 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -142,6 +142,7 @@ 'views/approval_date_doc.xml', 'views/partner_payment_term.xml', 'views/vendor_payment_term.xml', + 'views/approval_unreserve.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 e9ce587c..fe3e02d4 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -125,3 +125,4 @@ from . import sale_order_multi_uangmuka_penjualan from . import shipment_group from . import sales_order_reject from . import approval_date_doc +from . import approval_unreserve diff --git a/indoteknik_custom/models/approval_unreserve.py b/indoteknik_custom/models/approval_unreserve.py new file mode 100644 index 00000000..4feb51fa --- /dev/null +++ b/indoteknik_custom/models/approval_unreserve.py @@ -0,0 +1,69 @@ +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 ApprovalUnreserve(models.Model): + _name = "approval.unreserve" + _description = "Approval Unreserve" + _inherit = ['mail.thread'] + _rec_name = 'number' + + number = fields.Char(string='Document No', index=True, copy=False, readonly=True, tracking=True, default='New') + approval_line = fields.One2many('approval.unreserve.line', 'approval_id', string='Approval Unreserve Lines', auto_join=True) + state = fields.Selection([ + ('draft', 'Draft'), + ('waiting_approval', 'Waiting for Approval'), + ('approved', 'Approved'), + ('rejected', 'Rejected') + ], string="Status", default='draft', tracking=True) + request_date = fields.Date(string="Request Date", default=fields.Date.today, tracking=True) + approved_by = fields.Many2one('res.users', string="Approved By", readonly=True, tracking=True) + rejection_reason = fields.Text(string="Rejection Reason", tracking=True) + + @api.model + def create(self, vals): + if vals.get('number', 'New') == 'New': + vals['number'] = self.env['ir.sequence'].next_by_code('approval.unreserve') or 'New' + return super(ApprovalUnreserve, self).create(vals) + + def action_submit_for_approval(self): + self.write({'state': 'waiting_approval'}) + + def action_approve(self): + if self.state != 'waiting_approval': + raise UserError("Approval can only be done in 'Waiting for Approval' state") + self.write({ + 'state': 'approved', + 'approved_by': self.env.user.id + }) + # Trigger the unreserve function + self._trigger_unreserve() + + def action_reject(self, reason): + if self.state != 'waiting_approval': + raise UserError("Rejection can only be done in 'Waiting for Approval' state") + self.write({ + 'state': 'rejected', + 'rejection_reason': reason + }) + + def _trigger_unreserve(self): + # Get the related stock moves and perform the unreserve for approved lines + stock_move_obj = self.env['stock.move'] + for line in self.approval_line: + move = stock_move_obj.browse(line.move_id.id) + # Use the custom _do_unreserve method you defined earlier + move._do_unreserve(product=line.product_id, quantity=line.unreserve_qty) + +class ApprovalUnreserveLine(models.Model): + _name = 'approval.unreserve.line' + _description = 'Approval Unreserve Line' + _order = 'approval_id, id' + + approval_id = fields.Many2one('approval.unreserve', string='Approval Reference', required=True, ondelete='cascade', index=True, copy=False) + move_id = fields.Many2one('stock.move', string="Stock Move", required=True) + product_id = fields.Many2one('product.product', string="Product", related='move_id.product_id', readonly=True) + unreserve_qty = fields.Float(string="Quantity to Unreserve", required=True) diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index 5e7554a5..f017545d 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -134,3 +134,5 @@ access_shipment_group,access.shipment.group,model_shipment_group,,1,1,1,1 access_shipment_group_line,access.shipment.group.line,model_shipment_group_line,,1,1,1,1 access_sales_order_reject,access.sales.order.reject,model_sales_order_reject,,1,1,1,1 access_approval_date_doc,access.approval.date.doc,model_approval_date_doc,,1,1,1,1 +access_approval_unreserve,access.approval.unreserve,model_approval_unreserve,,1,1,1,1 +access_approval_unreserve_line,access.approval.unreserve.line,model_approval_unreserve_line,,1,1,1,1 diff --git a/indoteknik_custom/views/approval_unreserve.xml b/indoteknik_custom/views/approval_unreserve.xml new file mode 100644 index 00000000..cd238125 --- /dev/null +++ b/indoteknik_custom/views/approval_unreserve.xml @@ -0,0 +1,75 @@ + + + + approval.unreserve.tree + approval.unreserve + + + + + + + + + + + + approval.unreserve.line.tree + approval.unreserve.line + + + + + + + + + + + approval.unreserve.form + approval.unreserve + +
+
+
+ + + + + + + + + + + + + + + +
+ + +
+
+
+
+ + + Approval Unreserve + ir.actions.act_window + approval.unreserve + tree,form + + + +
diff --git a/indoteknik_custom/views/ir_sequence.xml b/indoteknik_custom/views/ir_sequence.xml index b2768c71..dd501d8c 100644 --- a/indoteknik_custom/views/ir_sequence.xml +++ b/indoteknik_custom/views/ir_sequence.xml @@ -20,6 +20,16 @@ 1 1 + + + Approval Unreserve + approval.unreserve + TRUE + AU/%(year)s/ + 5 + 1 + 1 + Logbook SJ -- cgit v1.2.3