diff options
| author | it-fixcomart <it@fixcomart.co.id> | 2024-12-04 11:24:24 +0700 |
|---|---|---|
| committer | it-fixcomart <it@fixcomart.co.id> | 2024-12-04 11:24:24 +0700 |
| commit | 1094670e6fc35e5150b612b32e1f497b36c022b3 (patch) | |
| tree | 4482491680db1554c28fb380480ada5282fac8f0 /indoteknik_custom/models/stock_picking.py | |
| parent | c631620706a24426faea6b43c2a0602eebf9a5e4 (diff) | |
| parent | dbe24b9cd600c7b5a9d0587f80a782ed93c9a761 (diff) | |
Merge branch 'production' into iman/telegram
# Conflicts:
# indoteknik_custom/models/__init__.py
# indoteknik_custom/models/stock_picking.py
Diffstat (limited to 'indoteknik_custom/models/stock_picking.py')
| -rw-r--r-- | indoteknik_custom/models/stock_picking.py | 230 |
1 files changed, 210 insertions, 20 deletions
diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py index 5f2fe99a..2095e957 100644 --- a/indoteknik_custom/models/stock_picking.py +++ b/indoteknik_custom/models/stock_picking.py @@ -3,7 +3,7 @@ from odoo.exceptions import AccessError, UserError, ValidationError from odoo.tools.float_utils import float_is_zero from datetime import datetime from itertools import groupby -import pytz, datetime +import pytz, datetime, requests, json from odoo.http import request @@ -26,7 +26,7 @@ class StockPicking(models.Model): # Delivery Order driver_departure_date = fields.Datetime( - string='Driver Departure Date', + string='Delivery Departure Date', copy=False ) arrival_time = fields.Datetime( @@ -34,8 +34,7 @@ class StockPicking(models.Model): copy=False ) driver_arrival_date = fields.Datetime( - string='Driver Arrival Date', - readonly=True, + string='Delivery Arrival Date', copy=False ) delivery_tracking_no = fields.Char( @@ -54,6 +53,10 @@ class StockPicking(models.Model): readonly=True, copy=False ) + sj_documentation = fields.Binary(string="Dokumentasi Surat Jalan", ) + paket_documentation = fields.Binary(string="Dokumentasi Paket", ) + sj_return_date = fields.Datetime(string="SJ Return Date", ) + responsible = fields.Many2one('res.users', string='Responsible', tracking=True) approval_status = fields.Selection([ ('pengajuan1', 'Approval Accounting'), @@ -74,10 +77,8 @@ class StockPicking(models.Model): ('hold', 'Hold by Sales'), ('not_paid', 'Customer belum bayar'), ('partial', 'Kirim Parsial'), - ('not_complete', 'Belum Lengkap'), ('indent', 'Indent'), ('self_pickup', 'Barang belum di pickup Customer'), - ('delivery_route', 'Belum masuk rute pengiriman'), ('expedition_closed', 'Eskpedisi belum buka') ], string='Note Logistic', help='jika field ini diisi maka tidak akan dihitung ke lead time') waybill_id = fields.One2many(comodel_name='airway.bill', inverse_name='do_id', string='Airway Bill') @@ -95,16 +96,108 @@ class StockPicking(models.Model): date_availability = fields.Datetime(string="Date Availability", copy=False, tracking=True) sale_order = fields.Char(string='Matches SO', copy=False) printed_sj = fields.Boolean('Printed Surat Jalan', help='flag which is internal use or not') + printed_sj_retur = fields.Boolean('Printed Surat Jalan Retur', help='flag which is internal use or not') + date_printed_sj_retur = fields.Datetime(string='Status Printed Surat Jalan Retur', copy=False, tracking=True) invoice_status = fields.Selection([ ('upselling', 'Upselling Opportunity'), ('invoiced', 'Fully Invoiced'), ('to invoice', 'To Invoice'), ('no', 'Nothing to Invoice') ], string='Invoice Status', related="sale_id.invoice_status") - + note_return = fields.Text(string="Note Return", help="Catatan untuk kirim barang kembali") + + state_reserve = fields.Selection([ + ('waiting', 'Waiting For Fullfilment'), + ('ready', 'Ready to Ship'), + ('done', 'Done'), + ('cancel', 'Cancelled'), + ], string='Status Reserve', readonly=True, tracking=True, help="The current state of the stock picking.") + notee = fields.Text(string="Note") + + def action_send_to_biteship(self): + url = "https://api.biteship.com/v1/orders" + api_key = "biteship_test.eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSW5kb3Rla25payIsInVzZXJJZCI6IjY3MTViYTJkYzVkMjdkMDAxMjRjODk2MiIsImlhdCI6MTcyOTQ5ODAwMX0.L6C73couP4-cgVEfhKI2g7eMCMo3YOFSRZhS-KSuHNA" + + # Mencari data sale.order.line berdasarkan sale_id + products = self.env['sale.order.line'].search([('order_id', '=', self.sale_id.id)]) + + # Fungsi untuk membangun items_data dari order lines + def build_items_data(lines): + return [{ + "name": line.product_id.name, + "description": line.name, + "value": line.price_unit, + "quantity": line.product_uom_qty, + "weight": line.weight + } for line in lines] + + # Items untuk pengiriman standard + items_data_standard = build_items_data(products) + + # Items untuk pengiriman instant, mengambil product_id dari move_line_ids_without_package + items_data_instant = [] + for move_line in self.move_line_ids_without_package: + # Mencari baris di sale.order.line berdasarkan product_id dari move_line + order_line = self.env['sale.order.line'].search([ + ('order_id', '=', self.sale_id.id), + ('product_id', '=', move_line.product_id.id) + ], limit=1) + + if order_line: + items_data_instant.append({ + "name": order_line.product_id.name, + "description": order_line.name, + "value": order_line.price_unit, + "quantity": move_line.qty_done, # Menggunakan qty_done dari move_line + "weight": order_line.weight + }) + + payload = { + "shipper_contact_name": self.carrier_id.pic_name or '', + "shipper_contact_phone": self.carrier_id.pic_phone or '', + "shipper_organization": self.carrier_id.name, + "origin_contact_name": "PT. Indoteknik Dotcom Gemilang", + "origin_contact_phone": "081717181922", + "origin_address": "Jl. Bandengan Utara Komp A & BRT. Penjaringan, Kec. Penjaringan, Jakarta (BELAKANG INDOMARET) KOTA JAKARTA UTARA PENJARINGAN", + "origin_postal_code": 14440, + "destination_contact_name": self.real_shipping_id.name, + "destination_contact_phone": self.real_shipping_id.phone or self.real_shipping_id.mobile, + "destination_address": self.real_shipping_id.street, + "destination_postal_code": self.real_shipping_id.zip, + "courier_type": "reg", + "courier_company": self.carrier_id.name.lower(), + "delivery_type": "now", + "destination_postal_code": self.real_shipping_id.zip, + "items": items_data_standard + } + + # Cek jika pengiriman instant atau same_day + if "instant" in self.sale_id.delivery_service_type or "same_day" in self.sale_id.delivery_service_type: + payload.update({ + "origin_note": "BELAKANG INDOMARET", + "courier_company": self.carrier_id.name.lower(), + "courier_type": self.sale_id.delivery_service_type, + "delivery_type": "now", + "items": items_data_instant # Gunakan items untuk instant + }) + + headers = { + "Authorization": f"Bearer {api_key}", + "Content-Type": "application/json" + } + + # Kirim request ke Biteship + response = requests.post(url, headers=headers, json=payload) + + if response.status_code == 201: + return response.json() + else: + raise UserError(f"Error saat mengirim ke Biteship: {response.content}") + @api.constrains('driver_departure_date') def constrains_driver_departure_date(self): - self.date_doc_kirim = self.driver_departure_date + if not self.date_doc_kirim: + self.date_doc_kirim = self.driver_departure_date @api.constrains('arrival_time') def constrains_arrival_time(self): @@ -135,9 +228,44 @@ class StockPicking(models.Model): res = super(StockPicking, self).do_unreserve() current_time = datetime.datetime.utcnow() self.date_unreserve = current_time - + # self.check_state_reserve() + return res + # def check_state_reserve(self): + # do = self.search([ + # ('state', 'not in', ['cancel', 'draft', 'done']), + # ('picking_type_code', '=', 'outgoing') + # ]) + + # for rec in do: + # rec.state_reserve = 'ready' + # rec.date_reserved = datetime.datetime.utcnow() + + # for line in rec.move_ids_without_package: + # if line.product_uom_qty > line.reserved_availability: + # rec.state_reserve = 'waiting' + # rec.date_reserved = '' + # break + + def check_state_reserve(self): + pickings = self.search([ + ('state', 'not in', ['cancel', 'draft', 'done']), + ('picking_type_code', '=', 'outgoing') + ]) + + for picking in pickings: + fullfillments = self.env['sales.order.fullfillment'].search([ + ('sales_order_id', '=', picking.sale_id.id) + ]) + + picking.state_reserve = 'ready' + picking.date_reserved = picking.date_reserved or datetime.datetime.utcnow() + + if any(rec.reserved_from not in ['Inventory On Hand', 'Reserved from stock', 'Free Stock'] for rec in fullfillments): + picking.state_reserve = 'waiting' + picking.date_reserved = '' + def _create_approval_notification(self, approval_role): title = 'Warning' message = f'Butuh approval sales untuk unreserved' @@ -153,9 +281,9 @@ class StockPicking(models.Model): def _compute_shipping_status(self): for rec in self: status = 'pending' - if rec.driver_departure_date and not rec.driver_arrival_date: + if rec.driver_departure_date and not (rec.sj_return_date or rec.driver_arrival_date): status = 'shipment' - elif rec.driver_departure_date and rec.driver_arrival_date: + elif rec.driver_departure_date and (rec.sj_return_date or rec.driver_arrival_date): status = 'completed' rec.shipping_status = status @@ -276,6 +404,7 @@ class StockPicking(models.Model): current_time = datetime.datetime.utcnow() self.real_shipping_id = self.sale_id.real_shipping_id self.date_availability = current_time + # self.check_state_reserve() return res def ask_approval(self): @@ -311,6 +440,34 @@ class StockPicking(models.Model): self.approval_receipt_status = 'pengajuan1' def ask_return_approval(self): + for pick in self: + if self.env.user.is_accounting: + pick.approval_return_status = 'approved' + continue + + action = self.env['ir.actions.act_window']._for_xml_id('indoteknik_custom.action_stock_return_note_wizard') + + if self.picking_type_code == 'outgoing': + if self.env.user.id in [3988, 3401, 20] or ( + self.env.user.has_group('indoteknik_custom.group_role_purchasing') and 'Return of' in self.origin + ): + action['context'] = {'picking_ids': [x.id for x in self]} + return action + elif not self.env.user.has_group('indoteknik_custom.group_role_purchasing') and 'Return of' in self.origin: + raise UserError('Harus Purchasing yang Ask Return') + else: + raise UserError('Harus Sales Admin yang Ask Return') + + elif self.picking_type_code == 'incoming': + if self.env.user.has_group('indoteknik_custom.group_role_purchasing') or ( + self.env.user.id in [3988, 3401, 20] and 'Return of' in self.origin + ): + action['context'] = {'picking_ids': [x.id for x in self]} + return action + elif not self.env.user.id in [3988, 3401, 20] and 'Return of' in self.origin: + raise UserError('Harus Sales Admin yang Ask Return') + else: + raise UserError('Harus Purchasing yang Ask Return') telegram_data = { 'tittle': self.name, 'about': 'Permintaan retur ' + self.name, @@ -347,6 +504,7 @@ class StockPicking(models.Model): self.approval_return_status = 'approved' def calculate_line_no(self): + for picking in self: name = picking.group_id.name for move in picking.move_ids_without_package: @@ -401,8 +559,31 @@ class StockPicking(models.Model): ): raise UserError('Quantity reserved lebih besar dari quantity onhand di product') + def check_qty_done_stock(self): + for line in self.move_line_ids_without_package: + def check_qty_per_inventory(self, product, location): + quant = self.env['stock.quant'].search([ + ('product_id', '=', product.id), + ('location_id', '=', location.id), + ]) + + if quant: + return quant.quantity + + return 0 + + qty_onhand = check_qty_per_inventory(self, line.product_id, line.location_id) + if line.qty_done > qty_onhand: + raise UserError(f'{line.product_id.display_name} : Quantity Done melebihi Quantity Onhand') def button_validate(self): + if not self.env.user.is_logistic_approver and self.env.context.get('active_model') == 'stock.picking': + if self.origin and 'Return of' in self.origin: + raise UserError("Button ini hanya untuk Logistik") + + if self.picking_type_code == 'internal': + self.check_qty_done_stock() + if self._name != 'stock.picking': return super(StockPicking, self).button_validate() @@ -424,10 +605,10 @@ class StockPicking(models.Model): if self.location_dest_id.id == 47 and not self.env.user.is_purchasing_manager: raise UserError("Transfer ke gudang selisih harus di approve Rafly Hanggara") - if self.group_id.sale_id: - if self.group_id.sale_id.payment_link_midtrans: - if self.group_id.sale_id.payment_status != 'settlement': - raise UserError('Uang belum masuk (settlement), mohon konfirmasi ke sales atau finance') + # if self.group_id.sale_id: + # if self.group_id.sale_id.payment_link_midtrans: + # if self.group_id.sale_id.payment_status != 'settlement' and self.group_id.sale_id.state == 'draft': + # raise UserError('Uang belum masuk (settlement), mohon konfirmasi ke sales atau finance') if self.is_internal_use: self.approval_status = 'approved' @@ -447,12 +628,21 @@ class StockPicking(models.Model): self.date_reserved = current_time self.validation_minus_onhand_quantity() - + self.responsible = self.env.user.id res = super(StockPicking, self).button_validate() self.calculate_line_no() self.date_done = datetime.datetime.utcnow() + self.state_reserve = 'done' + return res + def action_cancel(self): + if not self.env.user.is_logistic_approver and self.env.context.get('active_model') == 'stock.picking': + if self.origin and 'Return of' in self.origin: + raise UserError("Button ini hanya untuk Logistik") + + res = super(StockPicking, self).action_cancel() return res + @api.model def create(self, vals): self._use_faktur(vals) @@ -521,13 +711,13 @@ class StockPicking(models.Model): manifest_datas = [] departure_date = self.driver_departure_date - arrival_date = self.driver_arrival_date + arrival_date = self.sj_return_date if self.sj_return_date else self.driver_arrival_date status = status_mapping.get(status_key) if not status: return manifest_datas - if arrival_date: + if arrival_date or self.sj_return_date: manifest_datas.append(self.create_manifest_data(status['arrival'], arrival_date)) if departure_date: manifest_datas.append(self.create_manifest_data(status['departure'], departure_date)) @@ -554,7 +744,7 @@ class StockPicking(models.Model): } if not self.waybill_id or len(self.waybill_id.manifest_ids) == 0: - response['delivered'] = self.driver_arrival_date != False + response['delivered'] = self.sj_return_date != False or self.driver_arrival_date != False return response response['delivery_order']['receiver_name'] = self.waybill_id.receiver_name @@ -587,4 +777,4 @@ class StockPicking(models.Model): formatted_fastest_eta = fastest_eta.strftime(format_time_fastest) formatted_longest_eta = longest_eta.strftime(format_time) - return f'{formatted_fastest_eta} - {formatted_longest_eta}' + return f'{formatted_fastest_eta} - {formatted_longest_eta}'
\ No newline at end of file |
