Files
kupshop/bundles/KupShop/DeliveryPriceListBundle/Admin/deliveryPriceLists.php
2025-08-02 16:30:27 +02:00

467 lines
17 KiB
PHP

<?php
class deliveryPriceLists extends Window
{
protected $nameField = 'name';
protected $tableName = 'delivery_pricelists';
/** @var \Doctrine\ORM\EntityManager em */
private $em;
public function __construct()
{
$this->em = \KupShop\KupShopBundle\Util\Compat\ServiceContainer::getService('doctrine.orm.entity_manager');
}
public function get_vars()
{
$vars = parent::get_vars();
$ID = $this->getID();
$pageVars = getVal('body', $vars);
$pageVars['countries'] = $this->getCountries();
$pageVars['currencies'] = $this->getCurrencies();
$pageVars['regions'] = $this->getRegions($ID);
$pageVars['regionsCount'] = count($pageVars['regions']);
$pageVars['weightGroups'] = $this->getWeightGroups($ID);
$pageVars['prices'] = $this->getPrices($ID);
$regionsIds = $this->getRegionsIds($ID);
$pageVars['regionCountries'] = $this->getRegionCountries($regionsIds);
if (findModule('delivery_pricelist', 'insurance')) {
$pageVars['insuranceCountries'] = $this->getInsuranceCountries();
}
$vars['body'] = $pageVars;
return $vars;
}
public function handle()
{
parent::handle();
$this->checkDelete();
}
public function handleDelete()
{
$ID = $this->getID();
$priceList = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\PriceList::class);
$priceList = $priceList->findOneBy(['id' => $ID]);
if ($priceList) {
$this->em->remove($priceList);
$this->em->flush();
$this->em->clear();
writeDownActivity(sprintf(translate('activityDeleted'), $ID));
redirect("launch.php?s={$this->getName()}.php&acn=erased");
}
}
public function handleUpdate()
{
$SQL = parent::handleUpdate();
if ($SQL) {
$ID = $this->getID();
$data = $this->getData();
// Tohle tady je bohužel potřeba, protože jinak to ukládalo
$this->em->clear();
// Price updating
if (isset($data['price'])) {
foreach ($data['price'] as $ids => $price) {
if (empty($price)) {
$price = 0;
}
$ids = explode('-', $ids);
if ($data['showPricesWithVat'] != 'Y') {
$price = toDecimal($price)->addVat(getAdminVat()['value']);
}
$this->updateRegionPrice($ID, $ids, $price);
}
}
// Weight Groups Updating
if (isset($data['weight_group'])) {
foreach ($data['weight_group'] as $id => $maxWeight) {
if (!empty($maxWeight)) {
$this->updateWeightGroup($id, $maxWeight);
}
}
}
// New weight group
if (isset($data['new_weight_group'])) {
foreach ($data['new_weight_group'] as $key => $maxWeight) {
if (!empty($maxWeight)) {
$id_wg = $this->updateWeightGroup(null, $maxWeight);
// add prices
if ($id_wg && isset($data['price_new'])) {
foreach ($data['price_new'][$key] as $region => $price) {
$ids = [0 => $region, 1 => $id_wg];
if ($data['showPricesWithVat'] != 'Y') {
$price = toDecimal($price)->addVat(getVat());
}
$this->updateRegionPrice($ID, $ids, $price);
}
}
}
}
}
// REGIONS
// add new region
if (isset($data['region'])) {
foreach ($data['region'] as $key => $region) {
$id = 0;
if (!empty($region['name'])) {
if ($key > 0) {
$id = $key;
}
$idRegion = $this->updateRegion($id, $region['name']);
if ($idRegion && $id == 0) {
$id = $idRegion;
}
if (isset($region['country'])) {
$this->updateRegionCountries($id, $region);
}
}
}
}
// insurance
if (findModule('delivery_pricelist', 'insurance')) {
if (!isset($data['insuranceCountries'])) {
$data['insuranceCountries'] = [];
}
$this->updateInsurance($data['insuranceCountries']);
}
}
return $SQL;
}
private function checkDelete()
{
$deleteWg = getVal('deleteWg');
$deleteRegion = getVal('deleteRegion');
$deleteCountry = getVal('deleteCountry');
// Delete Weight Group
if (isset($deleteWg)) {
$key = array_keys($deleteWg);
$this->deleteWeightGroup($key[0]);
}
// Delete Region
if (isset($deleteRegion)) {
$key = array_keys($deleteRegion);
$this->deleteRegion($key[0]);
}
// Delete Country
if (isset($deleteCountry)) {
$region = array_keys($deleteCountry)[0];
$country = array_keys($deleteCountry[$region])[0];
$this->deleteCountry($region, $country);
}
}
private function deleteCountry($region, $country)
{
$this->deleteSQL('delivery_pricelists_regions_countries', [
'id_region' => $region,
'id_country' => $country,
'id_pricelist' => $this->getID(),
]);
}
private function updateInsurance($countries)
{
$pricelist = $this->getID();
$insuranceCountries = $this->getInsuranceCountries();
foreach ($countries as $country) {
$check = sqlQueryBuilder()->select('*')->from('delivery_pricelists_insurance')
->where('id_pricelist = :id_pricelist AND id_country = :id_country')
->setParameters(['id_pricelist' => $pricelist, 'id_country' => $country])->execute();
if ($check->rowCount() == 0) {
// insert
$this->insertSQL('delivery_pricelists_insurance', ['id_pricelist' => $pricelist, 'id_country' => $country]);
}
$countries[$country] = $country;
}
// deleting
if ($insuranceCountries) {
foreach ($insuranceCountries as $country) {
if (!array_key_exists($country['id_country'], $countries)) {
sqlQueryBuilder()->delete('delivery_pricelists_insurance')->where('id_pricelist = :id_pricelist AND id_country = :id_country')
->setParameters(['id_pricelist' => $pricelist, 'id_country' => $country['id_country']])->execute();
}
}
}
}
private function getInsuranceCountries()
{
$data = sqlQueryBuilder()->select('*')->from('delivery_pricelists_insurance')
->where('id_pricelist = :id_pricelist')
->setParameters(['id_pricelist' => $this->getID()])->execute()->fetchAll();
if ($data) {
return \KupShop\KupShopBundle\Util\Functional\Mapping::mapKeys(
$data,
function ($key, $value) {
return [$value['id_country'], $value];
}
);
}
return [];
}
private function deleteRegion($id)
{
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\Region::class);
$region = $repository->findOneBy(['id' => $id]);
if ($region) {
$this->em->remove($region);
$this->em->flush();
$this->em->clear();
}
}
private function deleteWeightGroup($id)
{
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\WeightGroup::class);
$weightGroup = $repository->findOneBy(['id' => $id]);
if ($weightGroup) {
$this->em->remove($weightGroup);
$this->em->flush();
$this->em->clear();
}
}
private function updateRegionCountries($regionId, $region)
{
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\RegionCountry::class);
foreach ($region['country'] as $key => $country) {
$ids = explode('-', $country);
if (count($ids) > 1) {
$newId = $ids[0];
$country = $ids[1];
}
$data = $repository->findOneBy(['region' => $regionId, 'country' => $country]);
if ($data) {
// update
if (isset($newId)) {
/** @var \KupShop\I18nBundle\Entity\Country $country */
$countryId = $this->em->getRepository(\KupShop\I18nBundle\Entity\Country::class)->findOneBy(['id' => $newId]);
$data->setCountry($countryId);
}
$data->setDeliveryTimeMin(!empty($region['deliveryTimeMin'][$key]) ? $region['deliveryTimeMin'][$key] : null);
$data->setDeliveryTimeMax(!empty($region['deliveryTimeMax'][$key]) ? $region['deliveryTimeMax'][$key] : null);
$this->em->persist($data);
$this->em->flush();
$this->em->clear();
} else {
// insert
$data = new \KupShop\DeliveryPriceListBundle\Entity\RegionCountry();
/** @var \KupShop\DeliveryPriceListBundle\Entity\Price $pricelistId */
$pricelistId = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\PriceList::class)->findOneBy(['id' => $this->getID()]);
$data->setPricelist($pricelistId);
/** @var \KupShop\DeliveryPriceListBundle\Entity\Region $region */
$regionId = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\Region::class)->findOneBy(['id' => $regionId]);
$data->setRegion($regionId);
/** @var \KupShop\I18nBundle\Entity\Country $country */
$country = $this->em->getRepository(\KupShop\I18nBundle\Entity\Country::class)->findOneBy(['id' => $country]);
$data->setCountry($country);
$data->setDeliveryTimeMin(!empty($region['deliveryTimeMin'][$key]) ? $region['deliveryTimeMin'][$key] : null);
$data->setDeliveryTimeMax(!empty($region['deliveryTimeMax'][$key]) ? $region['deliveryTimeMax'][$key] : null);
$this->em->persist($data);
$this->em->flush();
$this->em->clear();
}
}
}
private function updateRegion($id, $name)
{
$priceListId = $this->getID();
$insert = false;
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\Region::class);
/** @var \KupShop\DeliveryPriceListBundle\Entity\Region $data */
$data = $repository->findOneBy(['id' => $id]);
if (!empty($data)) {
// update
$data->setName($name);
} else {
// insert region
$insert = true;
$data = new \KupShop\DeliveryPriceListBundle\Entity\Region();
$priceList = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\PriceList::class)->findOneBy(['id' => $priceListId]);
$data->setPriceList($priceList);
$data->setName($name);
}
$this->em->persist($data);
$this->em->flush();
$this->em->clear();
if ($insert) {
return $data->getId();
}
return false;
}
private function updateWeightGroup($id, $newMaxWeight)
{
$priceListId = $this->getID();
$insert = false;
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\WeightGroup::class);
/** @var \KupShop\DeliveryPriceListBundle\Entity\WeightGroup $data */
$data = $repository->findOneBy(['id' => $id, 'priceList' => $priceListId]);
if (!empty($data)) {
// update weight group
$data->setMaxWeight($newMaxWeight);
} else {
// insert new weight group
$insert = true;
$data = new \KupShop\DeliveryPriceListBundle\Entity\WeightGroup();
$priceList = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\PriceList::class)->findOneBy(['id' => $priceListId]);
$data->setPriceList($priceList);
$data->setMaxWeight($newMaxWeight);
}
$this->em->persist($data);
$this->em->flush();
$this->em->clear();
if ($insert) {
return $data->getId();
}
return false;
}
// prices updating
private function updateRegionPrice($pricelistId, $ids, $price)
{
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\Price::class);
/** @var \KupShop\DeliveryPriceListBundle\Entity\Price $data */
$data = $repository->findOneBy(['priceList' => $pricelistId, 'region' => $ids[0], 'weightGroup' => $ids[1]]);
if (!empty($data)) {
// update
$data->setPrice($this->preparePrice($price));
} else {
// insert new
$data = new \KupShop\DeliveryPriceListBundle\Entity\Price();
/** @var \KupShop\DeliveryPriceListBundle\Entity\PriceList $priceList */
$priceList = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\PriceList::class)->findOneBy(['id' => $pricelistId]);
$data->setPriceList($priceList);
/** @var \KupShop\DeliveryPriceListBundle\Entity\Region $region */
$region = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\Region::class)->findOneBy(['id' => $ids[0]]);
$data->setRegion($region);
/** @var \KupShop\DeliveryPriceListBundle\Entity\WeightGroup $weightGroup */
$weightGroup = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\WeightGroup::class)->findOneBy(['id' => $ids[1]]);
$data->setWeightGroup($weightGroup);
$data->setPrice($this->preparePrice($price));
}
$this->em->persist($data);
$this->em->flush();
$this->em->clear();
}
private function getRegionsIds($id)
{
$array = [];
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\Region::class);
$data = $repository->findBy(['priceList' => $id]);
foreach ($data as $d) {
$array[] = $d->getId();
}
return $array;
}
private function getRegionCountries($regions)
{
$array = [];
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\RegionCountry::class);
$data = $repository->findBy(['region' => $regions, 'pricelist' => $this->getID()]);
/** @var \KupShop\DeliveryPriceListBundle\Entity\RegionCountry $d */
foreach ($data as $d) {
if ($d->getCountry()) {
$array[$d->getRegion()->getId()][$d->getCountry()->getId()] = [
'min' => $d->getDeliveryTimeMin(),
'max' => $d->getDeliveryTimeMax(),
];
}
}
return $array;
}
private function getPrices($ID)
{
$array = [];
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\Price::class);
$data = $repository->findBy(['priceList' => $ID]);
/** @var \KupShop\DeliveryPriceListBundle\Entity\Price $d */
foreach ($data as $d) {
/** @var \KupShop\DeliveryPriceListBundle\Entity\WeightGroup $wg */
$wg = $d->getWeightGroup();
/** @var \KupShop\DeliveryPriceListBundle\Entity\Region $region */
$region = $d->getRegion();
/** @var \KupShop\DeliveryPriceListBundle\Entity\Price $price */
$price = $d->getPrice();
if ($wg) {
$array[$wg->getId()][$region->getId()] = $price;
}
}
return $array;
}
private function getWeightGroups($id)
{
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\WeightGroup::class);
return $repository->findBy(['priceList' => $id], ['maxWeight' => 'ASC']);
}
private function getRegions($id)
{
$repository = $this->em->getRepository(\KupShop\DeliveryPriceListBundle\Entity\Region::class);
return $repository->findBy(['priceList' => $id], ['name' => 'ASC']);
}
private function getCountries()
{
$repository = $this->em->getRepository(\KupShop\I18nBundle\Entity\Country::class);
return $repository->findAll();
}
private function getCurrencies()
{
$repository = $this->em->getRepository(\KupShop\I18nBundle\Entity\Currency::class);
return $repository->findAll();
}
}
$deliveryPricesLists = new deliveryPriceLists();
$deliveryPricesLists->run();