Files
kupshop/bundles/KupShop/CeskaPostaBundle/Tests/CeskaPostaTest.php
2025-08-02 16:30:27 +02:00

205 lines
8.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace KupShop\CeskaPostaBundle\Tests;
use KupShop\CeskaPostaBundle\CeskaPosta;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
class CeskaPostaTest extends \DatabaseTestCase
{
/** @var CeskaPosta */
protected $service;
protected $doSendParcels = false;
public static function setUpBeforeClass(): void
{
if (!findModule(\Modules::CESKA_POSTA)) {
self::markTestSkipped();
}
}
protected function setUp(): void
{
parent::setUp();
$this->service = ServiceContainer::getService(CeskaPosta::class);
$this->service->configure('363134001', 'M51535', '40003', '1');
// $this->doSendParcels = true;
}
public function testCeskaPostaGetStats()
{
$dateEnd = date('Y-m-d\T23:59:59.000P');
$dateBegin = date('Y-m-d\T00:00:00.000P', strtotime('-1 week'));
$request = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<b2bRequest xmlns="https://b2b.postaonline.cz/schema/B2BCommon-v1" xmlns:ns2="https://b2b.postaonline.cz/schema/POLServices-v1">
<header>
<idExtTransaction>1</idExtTransaction>
<timeStamp>'.date('Y-m-d\TH:i:s.vP').'</timeStamp>
<idContract>363134001</idContract>
</header>
<serviceData>
<ns2:getStats>
<ns2:dateBegin>'.$dateBegin.'</ns2:dateBegin>
<ns2:dateEnd>'.$dateEnd.'</ns2:dateEnd>
</ns2:getStats>
</serviceData>
</b2bRequest>';
$this->service->request_url = 'https://b2b.postaonline.cz/services/POLService/v1/getStats';
$this->service->sendToCeskaPosta($request, $this->service->request_url);
$this->assertEquals('', $this->service->error);
$this->assertContains('<v1_1:getStatsResponse>', $this->service->response);
$this->assertContains('<v1_1:importAll>', $this->service->response);
}
public function testCeskaPostaSendParcels()
{
$orderID = 34340; // EU - FI; RR
$order = new \Order();
if ($this->doSendParcels && $order->createFromDB($orderID)) {
$b2bRequest = $this->prepareCeskaPostaRequest($order, $this->service);
$idTransaction = $this->service->sendParcels($b2bRequest);
$this->assertEquals('', $this->service->error);
$this->assertNotFalse($idTransaction);
$parcelCode = $this->service->getResultParcels($idTransaction);
$this->assertEquals('', $this->service->error);
$this->assertNotFalse($parcelCode);
$idForm = $this->getParcelType($parcelCode);
$this->assertEquals('39', $idForm); // 39 - RR
$pdf = $this->service->getParcelsPrinting($parcelCode, $idForm);
$this->assertEquals('', $this->service->error);
$this->assertNotFalse($pdf);
}
}
public function testCeskaPostaGetResultParcels()
{
$idTransaction = '5650B272-0161-4000-E000-33BC0AA06329';
$parcelCode = $this->service->getResultParcels($idTransaction);
$this->assertEquals('', $this->service->error);
$this->assertNotFalse($parcelCode);
$this->assertEquals('CV200180271CZ', $parcelCode);
}
public function testCeskaPostaGetParcelsPrinting()
{
$parcelCode = 'CV200180271CZ';
$idForm = $this->getParcelType($parcelCode);
$this->assertEquals('61', $idForm); // 61 - CV
$pdf = $this->service->getParcelsPrinting($parcelCode, $idForm);
$this->assertEquals('', $this->service->error);
$this->assertNotFalse($pdf);
}
public function prepareCeskaPostaRequest($order, $service_CeskaPosta)
{
$b2bRequest = $service_CeskaPosta->get_b2bRequest($order, 'sendParcels');
$ns2 = $service_CeskaPosta->ns2;
$serviceData = $b2bRequest->serviceData;
$doParcelData = $serviceData->children('ns2', true)->sendParcels->doParcelData;
$doParcelParams = $serviceData->children('ns2', true)->sendParcels->doParcelData->doParcelParams;
$country = !empty($order->delivery_country) ? $order->delivery_country : $order->invoice_country;
$delivery_type = $order->getDeliveryType();
$delivery = $delivery_type->getDelivery();
// 9 - Small package by AIR mail - check
// 10 - Czech Post - regional delivery
if (($delivery->id == 9) || ($delivery->id == 10)) {
$doParcelParams->addChild('ns2:prefixParcelCode', 'RR', $ns2);
if ($country == 'CZ') {
// service 50: Doporučená psaní
$doParcelData->addChild('ns2:doParcelServices', null, $ns2)->addChild('ns2:service', '50', $ns2);
} else {
// service 53: Doporučená zásilka do zahraničí
$doParcelData->addChild('ns2:doParcelServices', null, $ns2)->addChild('ns2:service', '53', $ns2);
}
} else {
$prefixParcelCode = 'CS';
if (findModule(Modules::DELIVERY_PRICELIST)) {
if (isset($delivery->id_pricelist)) {
$priceList = ServiceContainer::getService(\KupShop\DeliveryPriceListBundle\DeliveryPriceCalculator::class);
$priceList->setPricelistId($delivery->id_pricelist);
$priceList->setCountry($country);
if ($priceList->hasCountryInsurance($country)) {
$prefixParcelCode = 'CV';
// insuredValue = udaná cena
$doParcelParams->addChild('ns2:insuredValue', $order->total_price, $ns2);
}
}
}
$doParcelParams->addChild('ns2:prefixParcelCode', $prefixParcelCode, $ns2);
if ($delivery->id == 1) {
// 1 - Czech Post - priority (AIR)
// service 9: Prioritně
$doParcelData->addChild('ns2:doParcelServices', null, $ns2)->addChild('ns2:service', '9', $ns2);
}
// service 4C: Nevracet (Dispozice pro nakládání se zásilkou)
$doParcelData->addChild('ns2:doParcelServices', null, $ns2)->addChild('ns2:service', '4C', $ns2);
}
$cd = (strpos($order->flags, 'CD') !== false);
$mrn = $order->getData('mrn');
if ($mrn) {
$doParcelParams->addChild('ns2:mrn', $mrn, $ns2);
}
if ($cd) {
// service 44: MZ Zboží s VDD (vývozní doprovodný doklad pro uplatnění odpočtu DPH)
// služba 44, zboží pro všechny zásilky s VDD (s celními doklady, MRN)
$doParcelData->addChild('ns2:doParcelServices', null, $ns2)->addChild('ns2:service', '44', $ns2);
$doParcelCustomsDeclaration = $doParcelData->addChild('ns2:doParcelCustomsDeclaration', null, $ns2);
// Kategorie zásilky: jiné
$doParcelCustomsDeclaration->addChild('ns2:category', '991', $ns2);
// Měna celní hodnoty: CZK
$doParcelCustomsDeclaration->addChild('ns2:customValCur', 'CZK', $ns2);
$doParcelCustomsGoods = $doParcelCustomsDeclaration->addChild('ns2:doParcelCustomsGoods', null, $ns2);
$doParcelCustomsGoods->addChild('ns2:sequence', '1', $ns2);
// Popis zboží
$doParcelCustomsGoods->addChild('ns2:customCont', 'spare parts / náhradní díly', $ns2);
$doParcelCustomsGoods->addChild('ns2:quantity', '1', $ns2);
$doParcelCustomsGoods->addChild('ns2:weight', $order->getTotalWeight(), $ns2);
// Celní hodnota
$doParcelCustomsGoods->addChild('ns2:customVal', $order->total_price, $ns2);
// HS kód ?
$doParcelCustomsGoods->addChild('ns2:hsCode', '1', $ns2);
// Země původu zboží
$doParcelCustomsGoods->addChild('ns2:iso', 'CZ', $ns2);
} else {
if ($country != 'CZ') {
// service 43: MZ Zboží/Dárek/Ostatní ( pro mezinárodní zásilky) Odpovědní obálka (pro zásilky DR, DV, DE, NP, NV, NA, BN a BE)
// služba 43, zboží pro všechny zásilky bez VDD (bez celních dokladú, MRN)
$doParcelData->addChild('ns2:doParcelServices', null, $ns2)->addChild('ns2:service', '43', $ns2);
}
}
return $b2bRequest;
}
private function getParcelType($parcelCode)
{
$idForm = '39'; // ? adresní štítek bianco - samostatný (na šířku)
$parcelType = strtoupper(substr($parcelCode, 0, 2));
switch ($parcelType) {
case 'CV':
$idForm = '61';
break;
case 'CS':
$idForm = '58';
break;
case 'RR':
$idForm = '39';
break;
}
return $idForm;
}
public function getDataSet()
{
return $this->getJsonDataSetFromFile();
}
}