summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2025-05-31 04:19:04 +0000
committerIT Fixcomart <it@fixcomart.co.id>2025-05-31 04:19:04 +0000
commitec6fdea2682ae2d66f0d16f6d47a903c9eed99e9 (patch)
tree87e61452d5378740edf06afe9cdea17400aa5d22
parent2f89da69c9ec8d78f187be5afd254cdd594ee24a (diff)
parentc652ee3e1f652d23e37833f01e3fdd7aa8e52021 (diff)
Merged in no-webhook-biteship (pull request #320)
(andri) add tracking biteship pada BU OUT & fix delivery departure & arrival date
-rw-r--r--indoteknik_custom/models/stock_picking.py55
-rw-r--r--indoteknik_custom/views/stock_picking.xml7
2 files changed, 61 insertions, 1 deletions
diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py
index 54da700b..4d38e5b3 100644
--- a/indoteknik_custom/models/stock_picking.py
+++ b/indoteknik_custom/models/stock_picking.py
@@ -1551,7 +1551,60 @@ class StockPicking(models.Model):
except Exception as e :
_logger.error(f"Error fetching Biteship order for picking {self.id}: {str(e)}")
return { 'error': str(e) }
-
+
+ def action_sync_biteship_tracking(self):
+ for picking in self:
+ if not picking.biteship_id:
+ raise UserError("Tracking Biteship tidak tersedia.")
+
+ histori = picking.get_manifest_biteship()
+ updated_fields = {}
+ seen_logs = set()
+
+ manifests = sorted(histori.get("manifests", []), key=lambda m: m.get("datetime") or "")
+
+ for manifest in manifests:
+ status = manifest.get("status", "").lower()
+ dt_str = manifest.get("datetime")
+ desc = manifest.get("description")
+ dt = False
+
+ try:
+ local_dt_str = picking._convert_to_local_time(dt_str)
+ dt = fields.Datetime.from_string(local_dt_str)
+ except Exception as e:
+ _logger.warning(f"[Biteship Sync] Gagal parse datetime: {e}")
+ continue
+
+ # Update tanggal ke field
+ if status == "picked" and dt and not picking.driver_departure_date:
+ updated_fields["driver_departure_date"] = dt
+
+ if status == "delivered" and dt and not picking.driver_arrival_date:
+ updated_fields["driver_arrival_date"] = dt
+
+ # Buat log unik
+ if dt and desc:
+ desc_clean = ' '.join(desc.strip().split())
+ log_line = f"[TRACKING] {status} - {dt.strftime('%d %b %Y %H:%M')}: {desc_clean}"
+ if not picking._has_existing_log(log_line):
+ picking.message_post(body=log_line)
+ seen_logs.add(log_line)
+
+ if updated_fields:
+ picking.write(updated_fields)
+
+ def _has_existing_log(self, log_line):
+ self.ensure_one()
+ self.env.cr.execute("""
+ SELECT 1 FROM mail_message
+ WHERE model = %s AND res_id = %s
+ AND subtype_id IS NOT NULL
+ AND body ILIKE %s
+ LIMIT 1
+ """, (self._name, self.id, f"%{log_line}%"))
+ return self.env.cr.fetchone() is not None
+
def _convert_to_local_time(self, iso_date):
try:
dt_with_tz = waktu.fromisoformat(iso_date)
diff --git a/indoteknik_custom/views/stock_picking.xml b/indoteknik_custom/views/stock_picking.xml
index c916f2ef..9b16639c 100644
--- a/indoteknik_custom/views/stock_picking.xml
+++ b/indoteknik_custom/views/stock_picking.xml
@@ -62,6 +62,12 @@
string="Biteship"
type="object"
/>
+ <button name="action_sync_biteship_tracking"
+ type="object"
+ string="Lacak dari Biteship"
+ class="btn-primary"
+ attrs="{'invisible': [('biteship_id', '=', False)]}"
+ />
<button name="track_envio_shipment"
string="Tracking Envio"
type="object"
@@ -178,6 +184,7 @@
<field name="note_info"/>
<field name="responsible" />
<field name="carrier_id"/>
+ <field name="biteship_id" invisible="1"/>
<field name="out_code" attrs="{'invisible': [['out_code', '=', False]]}"/>
<field name="picking_code" attrs="{'invisible': [['picking_code', '=', False]]}"/>
<field name="picking_code" string="Picking code (akan digenerate ketika sudah di-validate)" attrs="{'invisible': [['picking_code', '!=', False]]}"/>