Files
grzanieplus.pl/plugins/stPaczkomatyPlugin/lib/model/PaczkomatyPack.php
2025-03-12 17:06:23 +01:00

352 lines
10 KiB
PHP

<?php
class PaczkomatyPack extends BasePaczkomatyPack
{
protected $allegroTransaction = null;
protected $sendingMethod = null;
protected $dropoffPoint = null;
protected $endOfWeekCollection = false;
public function isAdminGeneratorPlainField(): bool
{
return !$this->isNew() && !in_array($this->getStatus(), array('created', 'offers_prepared', 'offer_selected'));
}
public function isAdminGeneratorActionVisible(string $name): bool
{
if ($name == 'download_label' && empty($this->getTrackingNumber()))
{
return false;
}
if ($name == 'dispatch_order' && (!stDeliveryTypeConfiguration::has('inpostk') || $this->getSendingMethod() != 'dispatch_order' || null !== $this->getDispatchOrderId() || empty($this->getTrackingNumber())))
{
return false;
}
if ($name == '_delete' && !empty($this->getStatus()) && !in_array($this->getStatus(), ['created', 'offers_prepared', 'offer_selected']))
{
return false;
}
return true;
}
public function getServiceType(): string
{
return $this->getOrder()->getOrderDelivery()->getDeliveryTypeService()->getLabel();
}
public function getOrderNumber()
{
return $this->getOrder() ? $this->getOrder()->getNumber() : null;
}
public function setOrder($order)
{
if ($this->isNew() && null === $this->aOrder)
{
parent::setOrder($order);
$userData = $order->getOrderUserDataDelivery();
$this->setCustomerEmail($order->getOptClientEmail());
$this->setCustomerPhone($userData->getPhone());
$this->setCustomerPickupPoint($order->getOrderDelivery()->getPickupPoint());
$this->setEndOfWeekCollection($order->getOrderDelivery()->getIsWeekendDelivery());
if ($this->hasCourierService())
{
$address = $userData->getAddress();
if (!empty($userData->getAddressMore()))
{
$address .= ' ' . $userData->getAddressMore();
}
$addressParser = new stAddressParser($address);
$this->setCustomerCompanyName($userData->getCompany());
$this->setCustomerName($userData->getFullName());
$this->setCustomerStreet($addressParser->getStreet());
$this->setCustomerBuildingNumber($addressParser->getBuilding(true));
$this->setCustomerCity($userData->getTown());
$this->setCustomerCountryCode($userData->getCountries()->getIsoA2());
$this->setCustomerPostCode($userData->getCode());
$this->setCustomerPhone($userData->getPhone());
}
if ($order->getOrderDelivery()->getDelivery())
{
$this->setPackType($order->getOrderDelivery()->getDelivery()->getPaczkomatySize());
}
}
}
public function getService()
{
$service = parent::getService();
if (null === $service)
{
$orderDelivery = $this->getOrder()->getOrderDelivery();
if ($this->getOrder()->isAllegroOrder())
{
$service = stInPostApi::getAllegroDeliveryToServiceMapping($orderDelivery->getOptAllegroDeliveryMethodId());
}
else
{
$service = $orderDelivery->getDeliveryTypeService()->getConfiguration()->getType() == 'inpostp' ? 'inpost_locker_standard' : 'inpost_courier_standard';
}
$this->setService($service);
}
return $service;
}
public function hasCourierService()
{
return in_array($this->getService(), stInPostApi::COURIER_SERVICES);
}
public function setCustomerPhone($v)
{
$v = str_replace(array('+48', ' ', '-'), '', $v);
return parent::setCustomerPhone($v);
}
public function setCustomerPickupPoint($v)
{
$this->setCustomerPaczkomat($v);
}
public function getCustomerPickupPoint()
{
return $this->getCustomerPaczkomat();
}
public function setDropOffPoint($v)
{
$this->dropoffPoint = $v;
}
public function getDropOffPoint()
{
return $this->dropoffPoint;
}
public function setEndOfWeekCollection($v)
{
$this->endOfWeekCollection = $v;
}
public function getEndOfWeekCollection()
{
return $this->endOfWeekCollection;
}
public function setTrackingNumber($v)
{
$this->setCode($v);
}
public function getTrackingNumber()
{
return $this->getCode();
}
public function getStatusLabel()
{
if (!$this->isAdminGeneratorPlainField())
{
return null;
}
$status = null;
$api = stInPostApi::getInstance();
if ($this->status)
{
try
{
$status = $api->getStatusTitleByName($this->status);
}
catch (stInPostApiException $e)
{
$status = null;
}
}
return $status;
}
/**
* Zwraca predefiniowany formiar paczki
*
* @return string Zwraca 'small', 'medium' or 'large'
*/
public function getParcelTemplate()
{
$templates = array(
'A' => 'small',
'B' => 'medium',
'C' => 'large',
);
return $templates[$this->getPackType()];
}
public function setParcelTemplate($template)
{
$types = array(
'small' => 'A',
'medium' => 'B',
'large' => 'C',
);
$this->setPackType($types[$template]);
}
public function delete($con = null)
{
$ret = parent::delete($con);
$delivery = $this->getOrder()->getOrderDelivery();
$delivery->setNumber(null);
$delivery->save();
return $ret;
}
public function save($con = null)
{
$orderNumberModified = $this->isColumnModified(PaczkomatyPackPeer::CODE);
$pickupPointModified = $this->isColumnModified(PaczkomatyPackPeer::CUSTOMER_PACZKOMAT);
$ret = parent::save($con);
if ($orderNumberModified)
{
$orderDelivery = $this->getOrder()->getOrderDelivery();
if ($orderDelivery)
{
$orderDelivery->setNumber($this->getTrackingNumber());
$orderDelivery->save();
}
}
if ($pickupPointModified)
{
$orderUserDataDelivery = $this->getOrder()->getOrderUserDataDelivery();
if ($orderUserDataDelivery)
{
$pickupPoint = stInPostApi::getInstance()->getPoint($this->getCustomerPickupPoint());
$address = $pickupPoint->address_details->street . ' ' . $pickupPoint->address_details->building_number;
if ($pickupPoint->address_details->flat_number)
{
$address .= '/' . $pickupPoint->address_details->flat_number;
}
$orderUserDataDelivery->setCompany('Paczkomat - ' . $pickupPoint->name);
$orderUserDataDelivery->setFullName(null);
$orderUserDataDelivery->setAddress($address);
$orderUserDataDelivery->setCode($pickupPoint->address_details->post_code);
$orderUserDataDelivery->setAddressMore(null);
$orderUserDataDelivery->setTown($pickupPoint->address_details->city);
$orderUserDataDelivery->save();
}
}
return $ret;
}
public function getCashOnDelivery($fetchFromOrder = false)
{
$amount = parent::getCashOnDelivery();
if (null === $amount && $fetchFromOrder)
{
$amount = stPrice::round($this->getOrder()->getUnpaidAmount());
if ($this->getOrder()->getOrderCurrency() != 'PLN')
{
$amount = $this->getOrder()->getOrderCurrency()->exchange($amount, true);
}
}
return $amount;
}
public function hasAllegroTransactionId()
{
return $this->getOrder()->isAllegroOrder();
}
public function hasAllegroInsurance()
{
return $this->hasAllegroTransactionId() && ($this->getOrder()->getOptTotalAmount() - $this->getOrder()->getOrderDelivery()->getCost(true) <= 5000);
}
public function getAllegroTransactionId()
{
if (null === $this->allegroTransaction)
{
if ($this->getOrder()->getOptAllegroCheckoutFormId() && !is_numeric($this->getOrder()->getOrderPayment()->getTransactionId()))
{
$api = stAllegroApi::getInstance();
$this->allegroTransaction = $api->getPaymentMapping($this->getOrder()->getOrderPayment()->getTransactionId());
}
else
{
$order = $this->getOrder();
$c = new Criteria();
$c->add(AllegroAuctionHasOrderPeer::ORDER_ID, $order->getId());
$allegroTransaction = AllegroAuctionHasOrderPeer::doSelectOne($c);
$this->allegroTransaction = $allegroTransaction ? $allegroTransaction->getTransId() : null;
}
}
return $this->allegroTransaction;
}
public function getInsurance($fetchFromOrder = false)
{
$amount = parent::getInsurance();
if (!$amount && $fetchFromOrder)
{
$amount = $this->getOrder()->getTotalAmount(true, true);
if ($this->getOrder()->getOrderCurrency() != 'PLN')
{
$amount = $this->getOrder()->getOrderCurrency()->exchange($amount, true);
}
}
return $amount;
}
public function hasCashOnDelivery()
{
if ($this->getHasCashOnDelivery())
{
return true;
}
$payment = $this->getOrder()->getOrderPayment();
return $payment && $payment->getPaymentType()->getIsCod();
}
}