Files
vontor-cz/backend/thirdparty/zasilkovna/models.py
Brunobrno f14c09bf7a Refactor commerce models and add configuration app
Major refactor of commerce models: restructured Carrier, Payment, and DiscountCode models, improved order total calculation, and integrated Zasilkovna and Stripe logic. Added new configuration Django app for shop settings, updated Zasilkovna and Stripe models, and fixed Zasilkovna client WSDL URL. Removed unused serializers and views in commerce, and registered new apps in settings.
2025-11-14 02:21:20 +01:00

146 lines
5.7 KiB
Python

"""Models for integration with Packeta (Zásilkovna) API.
These models wrap calls to the SOAP API via the immutable client defined in
`client.py` (PacketaAPI). DO NOT modify the client; we only consume it here.
Workflow:
- Create a `PacketaPacket` instance locally with required recipient data.
- On first save (when `packet_id` is empty) call the remote API to create
the packet and persist the returned identifier + metadata.
- Use helper methods to refresh remote info, fetch label PDF, cancel packet.
- Group packets into a `PacketaShipment` and create shipment remotely.
Edge cases handled:
- API faults raise exceptions (to be surfaced by serializer validation).
- Missing remote fields are stored in `metadata` JSON.
- Label PDF stored as binary field; can be re-fetched if empty.
"""
import json
from django.db import models
from django.utils import timezone
from django.core.validators import RegexValidator
from django.core.files.base import ContentFile
from .client import PacketaAPI
from commerce.models import Order, Carrier
from configuration.models import Configuration
packeta_client = PacketaAPI() # single reusable instance
class ZasilkovnaPacket(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
class STATE(models.TextChoices):
PENDING = "PENDING", "Podáno"
SENDED = "SENDED", "Odesláno"
ARRIVED = "ARRIVED", "Doručeno"
CANCELED = "CANCELED", "Zrušeno"
RETURNING = "RETURNING", "Posláno zpátky"
RETURNED = "RETURNED", "Vráceno"
state = models.CharField(max_length=20, choices=STATE.choices, default=STATE.PENDING)
# ------- API -------
class BUISSNESS_ADDRESS_ID(models.IntegerChoices):
SHOP = 1, "address of buissnes"
addressId = models.IntegerField(help_text="ID adresy, v API rozhraní", choices=BUISSNESS_ADDRESS_ID.choices, default=BUISSNESS_ADDRESS_ID.SHOP)
packet_id = models.IntegerField(help_text="Číslo zásilky v Packetě (api)")
barcode = models.CharField(max_length=64, help_text="Čárový kód zásilky v Packetě")
weight = models.IntegerField(
default=0,
help_text="Hmotnost zásilky v gramech"
)
class PDF_SIZE(models.TextChoices):
A6_ON_A6 = ("A6 on A6", "105x148 mm (A6) label on a page of the same size")
A7_ON_A7 = ("A7 on A7", "105x74 mm (A7) label on a page of the same size")
A6_ON_A4 = ("A6 on A4", "105x148 mm (A6) label on a page of size 210x297 mm (A4)")
A7_ON_A4 = ("A7 on A4", "105x74 mm (A7) label on a page of size 210x297 mm (A4)")
A8_ON_A8 = ("A8 on A8", "50x74 mm (A8) label on a page of the same size")
size_of_pdf = models.CharField(max_length=20, choices=PDF_SIZE.choices, default=PDF_SIZE.A6_ON_A6)
# 🚚 návratové směrovací kódy (pro vrácení zásilky)
return_routing = models.JSONField(
default=list,
blank=True,
help_text="Seznam 2 routing stringů pro vrácení zásilky"
)
def save(self, *args, **kwargs):
# On first save, create the packet remotely if packet_id is not set
carrier = Carrier.objects.get(zasilkovna=self)
order = Order.objects.get(carrier=carrier)
if not self.packet_id:
response = packeta_client.create_packet(
address_id=self.addressId,
weight=self.weight,
number=order.id,
name=order.first_name,
surname=order.last_name,
company=order.company,
email=order.email,
addressId=Configuration.get_solo().zasilkovna_address_id,
#FIXME: udělat logiku pro počítaní dobírky a hodnoty zboží
cod=100.00,
value=100.00,
currency=Configuration.get_solo().currency,
eshop= Configuration.get_solo().name,
)
self.packet_id = response['packet_id']
self.barcode = response['barcode']
return super().save(args, **kwargs)
def cancel_packet(self):
"""Cancel this packet via the Packeta API."""
packeta_client.cancel_packet(self.packet_id)
self.state = self.STATE.CANCELED
self.save() # persist state change
def get_tracking_url(self):
"""Vrátí veřejnou URL pro sledování zásilky (Zásilkovna.cz)."""
base_url = "https://www.zasilkovna.cz/vyhledavani?query="
return f"{base_url}{self.barcode}"
def returning_packet(self):
"""Mark this packet as returning via the Packeta API."""
response = packeta_client.get_return_routing(self.packet_id)
self.return_routing = json.loads(response)
self.state = self.STATE.RETURNING
self.save() # persist state change
class ZasilkovnaShipment(models.Model):
created_at = models.DateTimeField(auto_now_add=True, editable=False)
shipment_id = models.CharField(max_length=255, unique=True, help_text="ID zásilky v Packetě", editable=False)
barcode = models.CharField(
max_length=64,
help_text="Čárový kód zásilky v Packetě (format: )",
validators=[
RegexValidator(r'D-***-XM-<id>', message="Neplatný formát čárového kódu.")
]
)
packets = models.ManyToManyField(ZasilkovnaPacket, related_name="shipments", help_text="Seznam zásilek v této zásilce (packet_id)")
def save(self, *args, **kwargs):
if not self.shipment_id:
response = packeta_client.create_shipment(
packet_ids=[packet.packet_id for packet in self.packets.all()]
)
self.shipment_id = response['shipment_id']
self.barcode = response['barcode']
return super().save(args, **kwargs)