237 lines
7.4 KiB
PHP
237 lines
7.4 KiB
PHP
<?php
|
|
|
|
class BalikNaPostu extends Delivery
|
|
{
|
|
use DatabaseCommunication;
|
|
|
|
public static $type = Delivery::TYPE_POINT;
|
|
public static $className = 'Česká pošta - Balík na poštu';
|
|
|
|
protected $templateDescription = 'deliveries/delivery.BalikNaPostu.description.tpl';
|
|
protected $templateInit = 'deliveries/delivery.BalikNaPostu.init.tpl';
|
|
|
|
protected $AdminTemplateSettings = 'deliveries/block.delivery.BalikNaPostu.tpl';
|
|
|
|
public static $tableName = '`kupshop_shared`.`delivery_balik_na_postu`';
|
|
|
|
protected $heureka_delivery_id = 'CESKA_POSTA_NAPOSTU';
|
|
protected $zbozi_delivery_id = 'CESKA_POSTA_NA_POSTU';
|
|
|
|
protected $track_and_trace = 'https://www.postaonline.cz/trackandtrace/-/zasilka/cislo?parcelNumbers=[PACKAGE_ID]';
|
|
|
|
public static $xmlFile = 'http://napostu.cpost.cz/vystupy/napostu.xml';
|
|
public static $xlsDelivery = 'https://www.ceskaposta.cz/documents/10180/483935/zv_doruc_pasmo-sluzby.xls';
|
|
|
|
public $defaultIcon = '../../common/static/deliveries/cp-na-postu.svg';
|
|
|
|
public $psc;
|
|
|
|
public $config = [
|
|
// L (nejdelší strana do 100 cm)
|
|
'maxLength' => 100.0, // Maximální rozměr strany položky
|
|
'maxSumOfDimensions' => 300.0, // Maximální součet rozměrů všech tří stran položky
|
|
// legacy config:
|
|
'max_length' => 98, // Maximální rozměr jedné strany zásilky
|
|
'max_dimensions' => 300, // Maximální součet rozměrů všech tří stran zásilky
|
|
];
|
|
|
|
public function getName()
|
|
{
|
|
$info = self::getInfo($this->psc);
|
|
|
|
return parent::getName().' - '.$info['name'];
|
|
}
|
|
|
|
public function storeDeliveryInfo($data)
|
|
{
|
|
parent::storeDeliveryInfo($data);
|
|
|
|
$psc = trim($data['balik_na_postu_zip'] ?? '');
|
|
if (!empty($psc)) {
|
|
if (strlen($psc) != 5) {
|
|
return $this->data;
|
|
}
|
|
|
|
return $this->data += [
|
|
'psc' => $psc,
|
|
];
|
|
}
|
|
|
|
$point = $data['balik_na_postu_point'] ?? '';
|
|
$point = json_decode($point, true);
|
|
|
|
if (empty($point)) {
|
|
return $this->data;
|
|
}
|
|
|
|
return $this->data += [
|
|
'psc' => getVal('zip', $point) ?? getVal('psc', $point),
|
|
'point_name' => getVal('name', $point),
|
|
'address' => getVal('address', $point),
|
|
];
|
|
}
|
|
|
|
public function getInfo($PSC = null)
|
|
{
|
|
if (is_null($PSC) && getVal('point_name', $this->data)) {
|
|
$data = [];
|
|
$data['psc'] = getVal('psc', $this->data);
|
|
$data['name'] = getVal('point_name', $this->data);
|
|
$data['address'] = getVal('address', $this->data);
|
|
} else {
|
|
if (is_null($PSC)) {
|
|
$PSC = getVal('psc', $this->data);
|
|
}
|
|
|
|
$data = sqlQueryBuilder()->select('*')->from(self::$tableName)
|
|
->andWhere(\Query\Operator::equals(['psc' => $PSC]))
|
|
->execute()->fetch();
|
|
|
|
if (!$data) {
|
|
return ['name' => 'Neznámá pošta'];
|
|
}
|
|
|
|
$data['hours'] = unserialize($data['hours']);
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @param $data admin order data
|
|
*/
|
|
public function applyToOrder(&$data, $order)
|
|
{
|
|
parent::applyToOrder($data, $order);
|
|
|
|
$info = $this->getInfo();
|
|
if (empty($info['psc'])) {
|
|
return 'Není zvolena žádná pošta!';
|
|
}
|
|
$data['delivery_zip'] = $info['psc'];
|
|
$address = explode(', ', $info['address']);
|
|
$data['delivery_street'] = reset($address);
|
|
$data['delivery_city'] = $info['name'];
|
|
$data['delivery_country'] = 'CZ';
|
|
$data['delivery_firm'] = '';
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param $cart CartBase
|
|
*/
|
|
public function applyToCart(CartBase $cart)
|
|
{
|
|
parent::applyToCart($cart);
|
|
|
|
$info = $this->getInfo();
|
|
|
|
if (empty($info['psc'])) {
|
|
return;
|
|
}
|
|
|
|
if (isset($cart->invoice['name'])) {
|
|
$cart->delivery['name'] = $cart->invoice['name'];
|
|
$cart->delivery['surname'] = $cart->invoice['surname'];
|
|
}
|
|
|
|
$cart->delivery['zip'] = $info['psc'];
|
|
$address = explode(', ', $info['address']);
|
|
$cart->delivery['street'] = reset($address);
|
|
$cart->delivery['city'] = end($address);
|
|
$cart->delivery['country'] = 'CZ';
|
|
$cart->delivery['firm'] = '';
|
|
}
|
|
|
|
public function printDeliveryInfo()
|
|
{
|
|
$info = $this->getInfo();
|
|
|
|
return "<strong>{$info['name']}</strong> PSČ: <strong>{$info['psc']}</strong>";
|
|
}
|
|
|
|
public function getPointId()
|
|
{
|
|
return $this->data['psc'] ?? null;
|
|
}
|
|
|
|
public static function generateOpeningHours($days)
|
|
{
|
|
$hours = [];
|
|
foreach ($days->den as $day) {
|
|
$fromTo = [];
|
|
foreach ($day->od_do as $od_do) {
|
|
$fromTo[] = [strval($od_do->od), strval($od_do->do)];
|
|
}
|
|
$hours[strval($day['name'])] = $fromTo;
|
|
}
|
|
|
|
return $hours;
|
|
}
|
|
|
|
public function checkSelected(Cart $cart, ?Payment $payment = null)
|
|
{
|
|
if (empty($this->data['psc']) && $cart->hasData('delivery')) {
|
|
throw new \KupShop\OrderingBundle\Exception\DeliveryException(translate_shop('no_zipcode', 'delivery'));
|
|
}
|
|
|
|
return parent::checkSelected($cart, $payment);
|
|
}
|
|
|
|
public function import()
|
|
{
|
|
$xml = simplexml_load_file(self::$xmlFile);
|
|
|
|
if (count($xml->row) > 1000) {
|
|
sqlGetConnection()->transactional(function () use ($xml) {
|
|
global $cfg;
|
|
sqlQuery('DELETE FROM '.self::$tableName.' WHERE 1');
|
|
|
|
foreach ($xml->row as $post) {
|
|
if (strval($post->V_PROVOZU) == 'A') {
|
|
continue;
|
|
}
|
|
|
|
$data = [
|
|
'psc' => strval($post->PSC),
|
|
'name' => strval($post->NAZ_PROV),
|
|
'address' => strval($post->ADRESA),
|
|
'hours' => serialize(self::generateOpeningHours($post->OTV_DOBA)),
|
|
'region' => strval($post->OKRES),
|
|
];
|
|
|
|
sqlQuery(
|
|
'INSERT INTO '.self::$tableName.'
|
|
SET name=:name, psc=:psc, address=:address, hours=:hours, region=:region',
|
|
$data
|
|
);
|
|
}
|
|
|
|
try {
|
|
file_put_contents($cfg['Path']['data'].'tmp/CPpasmo.xls', fopen(self::$xlsDelivery, 'r'));
|
|
|
|
$import = new AutomaticImportTransform($cfg['Path']['data'].'tmp/CPpasmo.xls');
|
|
$users_array = $import->LoadBinfileAsArray();
|
|
|
|
foreach ($users_array['Doruč_pásmo'] as $post) {
|
|
$psc = substr($post['B'], 0, 5);
|
|
sqlQuery('UPDATE '.self::$tableName.' SET time_zone = 1 WHERE psc = :psc', ['psc' => $psc]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
$sentry = getRaven();
|
|
$sentry->captureMessage(
|
|
'CeskaPosta odpoledni doruceni: file not found',
|
|
[],
|
|
[
|
|
'extra' => [
|
|
'err_msg' => $e->getMessage(),
|
|
],
|
|
]
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|