diff options
Diffstat (limited to 'indoteknik_custom/models/purchase_order.py')
| -rwxr-xr-x | indoteknik_custom/models/purchase_order.py | 159 |
1 files changed, 148 insertions, 11 deletions
diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index 0e39d12a..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,7 +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?') + 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 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'] @@ -115,6 +153,7 @@ class PurchaseOrder(models.Model): 'ref': self.name, 'invoice_date': current_date, 'date': current_date, + 'invoice_origin': self.name, 'move_type': 'in_invoice' } @@ -165,6 +204,11 @@ class PurchaseOrder(models.Model): self.bills_pelunasan_id = bills.id + lognote_message = ( + f"Vendor bill created from: {self.name} ({self.partner_ref})" + ) + bills.message_post(body=lognote_message) + return { 'name': _('Account Move'), 'view_mode': 'tree,form', @@ -174,12 +218,10 @@ class PurchaseOrder(models.Model): 'domain': [('id', '=', bills.id)] } - - def create_bill_dp(self): if not self.env.user.is_accounting: raise UserError('Hanya Accounting yang bisa bikin bill dp') - + current_date = datetime.utcnow() data_bills = { 'partner_id': self.partner_id.id, @@ -187,8 +229,8 @@ class PurchaseOrder(models.Model): 'ref': self.name, 'invoice_date': current_date, 'date': current_date, + 'invoice_origin': self.name, 'move_type': 'in_invoice' - } bills = self.env['account.move'].create([data_bills]) @@ -197,14 +239,13 @@ class PurchaseOrder(models.Model): data_line_bills = { 'move_id': bills.id, - 'product_id': product_dp.id, #product down payment - 'account_id': 401, #Uang Muka persediaan barang dagang + 'product_id': product_dp.id, # product down payment + 'account_id': 401, # Uang Muka persediaan barang dagang 'quantity': 1, 'product_uom_id': 1, 'tax_ids': [line[0].taxes_id.id for line in self.order_line], } - bills_line = self.env['account.move.line'].create([data_line_bills]) self.bills_dp_id = bills.id @@ -213,6 +254,12 @@ 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})" + ) + bills.message_post(body=lognote_message) + return { 'name': _('Account Move'), 'view_mode': 'tree,form', @@ -222,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), @@ -749,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") @@ -779,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 = '' @@ -853,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: @@ -896,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: @@ -915,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: @@ -923,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'} + + |
