diff options
Diffstat (limited to 'indoteknik_custom/models/purchase_order.py')
| -rwxr-xr-x | indoteknik_custom/models/purchase_order.py | 155 |
1 files changed, 131 insertions, 24 deletions
diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index 799c4db0..d487ada3 100755 --- a/indoteknik_custom/models/purchase_order.py +++ b/indoteknik_custom/models/purchase_order.py @@ -30,6 +30,11 @@ class PurchaseOrder(models.Model): ('pengajuan2', 'Approval Pimpinan'), #akbar - 7 temporary not used ('approved', 'Approved'), ], string='Approval Status', readonly=True, copy=False, index=True, tracking=3) + approval_status_unlock = fields.Selection([ + ('pengajuanFinance', 'Pengajuan Finance'), + ('approvedFinance', 'Approved Finance'), + ('approved', 'Approved'), + ], string='Approval Status Unlock', readonly=True, copy=False, index=True, tracking=3) delivery_amount = fields.Float('Delivery Amount', compute='compute_delivery_amount') delivery_amt = fields.Float('Delivery Amt') total_margin = fields.Float( @@ -75,29 +80,40 @@ class PurchaseOrder(models.Model): exclude_incoming = fields.Boolean(string='Exclude Incoming', default=False, help='Centang jika tidak mau masuk perhitungan Incoming Qty') not_update_purchasepricelist = fields.Boolean(string='Not Update Purchase Pricelist?') - # total_cost_service = fields.Float(string='Total Cost Service') + reason_unlock = fields.Char(string='Alasan unlock', tracking=3) + # total_cost_service = fields.Float(string='Total Cost Service' ) # total_delivery_amt = fields.Float(string='Total Delivery Amt') + total_cost_service = fields.Float(string='Total Cost Service') + total_delivery_amt = fields.Float(string='Total Delivery Amt') + store_name = fields.Char(string='Nama Toko') - # @api.onchange('total_cost_service') - # def _onchange_total_cost_service(self): - # for order in self: - # lines = order.order_line - # if lines: - # # Hitung nilai rata-rata cost_service - # per_line_cost_service = order.total_cost_service / len(lines) - # for line in lines: - # line.cost_service = per_line_cost_service - - # @api.onchange('total_delivery_amt') - # def _onchange_total_delivery_amt(self): - # for order in self: - # lines = order.order_line - # if lines: - # # Hitung nilai rata-rata delivery_amt - # per_line_delivery_amt = order.total_delivery_amt / len(lines) - # for line in lines: - # line.delivery_amt = per_line_delivery_amt + @api.onchange('total_cost_service') + def _onchange_total_cost_service(self): + for order in self: + lines = order.order_line + if order.total_cost_service > 0: + if lines: + # Hitung nilai rata-rata cost_service + per_line_cost_service = order.total_cost_service / len(lines) + for line in lines: + line.cost_service = per_line_cost_service + else: + for line in lines: + line.cost_service = 0 + @api.onchange('total_delivery_amt') + def _onchange_total_delivery_amt(self): + for order in self: + lines = order.order_line + if order.total_delivery_amt > 0: + if lines: + # Hitung nilai rata-rata delivery_amt + per_line_delivery_amt = order.total_delivery_amt / len(lines) + for line in lines: + line.delivery_amt = per_line_delivery_amt + else: + for line in lines: + line.delivery_amt = 0 def _compute_total_margin_match(self): for purchase in self: match = self.env['purchase.order.sales.match'] @@ -238,6 +254,7 @@ class PurchaseOrder(models.Model): move_line.name = '[IT.121456] Down Payment' move_line.partner_id = self.partner_id.id + # Tambahkan lognote lognote_message = ( f"Vendor bill created from: {self.name} ({self.partner_ref})" ) @@ -252,6 +269,7 @@ class PurchaseOrder(models.Model): 'domain': [('id', '=', bills.id)] } + def get_date_done(self): picking = self.env['stock.picking'].search([ ('purchase_id', '=', self.id), @@ -779,8 +797,12 @@ class PurchaseOrder(models.Model): def po_approve(self): greater_than_plafon, message = self._get_msg_plafon_qty() different_vendor_message = self.check_different_vendor_so() # Panggil fungsi check_different_vendor_so - - if self.env.user.is_leader or self.env.user.has_group('indoteknik_custom.group_role_merchandiser'): + if self.approval_status_unlock == 'pengajuanFinance': + if self.env.user.is_accounting: + self.approval_status_unlock = 'approvedFinance' + else: + raise UserError("Bisa langsung Confirm, menunggu persetujuan Finance jika ingin unlock PO") + elif self.env.user.is_leader or self.env.user.has_group('indoteknik_custom.group_role_merchandiser'): raise UserError("Bisa langsung Confirm") elif self.total_percent_margin == self.total_so_percent_margin and self.matches_so and not greater_than_plafon and not different_vendor_message: raise UserError("Bisa langsung Confirm") @@ -809,7 +831,17 @@ class PurchaseOrder(models.Model): subtype_id=self.env.ref("mail.mt_note").id ) - + def po_approve_unlock(self): + if self.approval_status_unlock == 'pengajuanFinance': + if self.env.user.is_accounting: + self.approval_status_unlock = 'approvedFinance' + else: + raise UserError("Menunggu persetujuan Finance jika ingin unlock PO") + elif self.approval_status_unlock == 'approvedFinance': + raise UserError("PO bisa langsung di unlock") + else: + raise UserError("Menunggu persetujuan Finance jika ingin unlock PO") + def check_different_vendor_so(self): vendor_po = self.partner_id.id message = '' @@ -883,6 +915,13 @@ class PurchaseOrder(models.Model): sales_price -= sale_order_line.fee_third_party_line sum_sales_price += sales_price purchase_price = line.price_subtotal + if line.ending_price > 0: + if line.taxes_id.id == 22: + ending_price = line.ending_price / 1.11 + purchase_price = ending_price + else: + purchase_price = line.ending_price + # purchase_price = line.price_subtotal if line.order_id.delivery_amount > 0: purchase_price += line.delivery_amt_line if line.order_id.delivery_amt > 0: @@ -926,7 +965,14 @@ class PurchaseOrder(models.Model): sales_price -= (sale_order_line.fee_third_party_line / sale_order_line.product_uom_qty) * line.qty_po sum_sales_price += sales_price - purchase_price = po_line.price_subtotal / po_line.product_qty * line.qty_po + + purchase_price = po_line.price_subtotal + if po_line.ending_price > 0: + if po_line.taxes_id.id == 22: + ending_price = po_line.ending_price / 1.11 + purchase_price = ending_price + else: + purchase_price = po_line.ending_price if line.purchase_order_id.delivery_amount > 0: purchase_price += (po_line.delivery_amt_line / po_line.product_qty) * line.qty_po if line.purchase_order_id.delivery_amt > 0: @@ -945,6 +991,19 @@ class PurchaseOrder(models.Model): self.total_percent_margin = 0 self.total_so_margin = 0 self.total_so_percent_margin = 0 + + + if sum_so_margin != 0 and sum_sales_price != 0 and sum_margin != 0: + self.total_so_margin = sum_so_margin + self.total_so_percent_margin = round((sum_so_margin / sum_sales_price), 2) * 100 + self.total_margin = sum_margin + self.total_percent_margin = round((sum_margin / sum_sales_price), 2) * 100 + + else: + self.total_margin = 0 + self.total_percent_margin = 0 + self.total_so_margin = 0 + self.total_so_percent_margin = 0 def compute_amt_total_without_service(self): for order in self: @@ -953,3 +1012,51 @@ class PurchaseOrder(models.Model): if line.product_id.type == 'product': sum_price_total += line.price_total order.amount_total_without_service = sum_price_total + + def button_unlock(self): + for order in self: + # Check if any order line has received_qty not equal to 0 + if self.env.user.is_accounting: + order.state = 'purchase' + order.approval_status_unlock = 'approved' + break + for line in order.order_line: + if line.qty_received > 0: + if order.approval_status_unlock == 'approvedFinance': + order.approval_status_unlock = 'approved' + order.state = 'purchase' + break + if order.approval_status_unlock == 'pengajuanFinance': + raise UserError(_( + "Menunggu Approve Dari Finance." + )) + else: + return { + 'type': 'ir.actions.act_window', + 'name': _('Untuk mengubah PO butuh approve dari Finance. Berikan alasan anda unlock PO!'), + 'res_model': 'purchase.order.unlock.wizard', + 'view_mode': 'form', + 'target': 'new', + 'context': { + 'default_purchase_order_id': order.id + } + } + + return super(PurchaseOrder, self).button_unlock() + + +class PurchaseOrderUnlockWizard(models.TransientModel): + _name = 'purchase.order.unlock.wizard' + _description = 'Wizard untuk memberikan alasan unlock PO' + + purchase_order_id = fields.Many2one('purchase.order', string='Purchase Order', required=True) + alasan = fields.Text(string='Alasan', required=True) + + def confirm_reject(self): + order = self.purchase_order_id + if order: + order.write({'reason_unlock': self.alasan}) + order.approval_status_unlock = 'pengajuanFinance' + return {'type': 'ir.actions.act_window_close'} + + |
