130 lines
4.2 KiB
PHP
130 lines
4.2 KiB
PHP
<?php
|
|
/**
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This file is licenced under the Software License Agreement.
|
|
* With the purchase or the installation of the software in your application
|
|
* you accept the licence agreement.
|
|
*
|
|
* You must not modify, adapt or create derivative works of this source code
|
|
*
|
|
* @author PrestaHelp.com
|
|
* @copyright 2019 PrestaHelp
|
|
* @license LICENSE.txt
|
|
*/
|
|
|
|
class InpostSettings
|
|
{
|
|
public static function createCarrier($id, $name)
|
|
{
|
|
$id_carrier = (int)Configuration::get($id);
|
|
$carrier = Carrier::getCarrierByReference((int)$id_carrier);
|
|
if ($id_carrier && Validate::isLoadedObject($carrier)) {
|
|
if (!$carrier->deleted) {
|
|
return true;
|
|
} else {
|
|
$carrier->deleted = 0;
|
|
return (bool)$carrier->save();
|
|
}
|
|
} else {
|
|
$carrier = new Carrier();
|
|
$carrier->name = $name;
|
|
$carrier->active = 1;
|
|
$carrier->is_free = 0;
|
|
$carrier->shipping_handling = 1;
|
|
$carrier->shipping_external = 0;
|
|
$carrier->shipping_method = 1;
|
|
$carrier->grade = 0;
|
|
$carrier->is_module = 1;
|
|
$carrier->need_range = 1;
|
|
$carrier->range_behavior = 1;
|
|
$carrier->external_module_name = 'inpostship';
|
|
$carrier->url = 'https://inpost.pl/sledzenie-przesylek?number=@';
|
|
$delay = array();
|
|
foreach (Language::getLanguages(false) as $language) {
|
|
$delay[$language['id_lang']] = $name;
|
|
}
|
|
$carrier->delay = $delay;
|
|
if (!$carrier->save()) {
|
|
return false;
|
|
}
|
|
$range_obj = $carrier->getRangeObject();
|
|
$range_obj->id_carrier = (int)$carrier->id;
|
|
$range_obj->delimiter1 = 0;
|
|
$range_obj->delimiter2 = 25;
|
|
if (!$range_obj->save()) {
|
|
return false;
|
|
}
|
|
if (!self::assignGroups($carrier)) {
|
|
return false;
|
|
}
|
|
if (!self::createZone($carrier->id)) {
|
|
return false;
|
|
}
|
|
if (!self::createDelivery($carrier->id, $range_obj->id, $id)) {
|
|
return false;
|
|
}
|
|
if (!Configuration::updateValue($id, 'a:1:{i:0;s:1:"'.$carrier->id.'";}')) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static function assignGroups($carrier)
|
|
{
|
|
$groups = array();
|
|
foreach (Group::getGroups((int)Context::getContext()->language->id) as $group) {
|
|
$groups[] = $group['id_group'];
|
|
}
|
|
if (version_compare(_PS_VERSION_, '1.5.5', '<')) {
|
|
if (!self::setGroupsOld((int)$carrier->id, $groups)) {
|
|
return false;
|
|
}
|
|
} else {
|
|
if (!$carrier->setGroups($groups)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
protected static function setGroupsOld($id_carrier, $groups)
|
|
{
|
|
foreach ($groups as $id_group) {
|
|
if (!Db::getInstance()->execute('
|
|
INSERT INTO `' . _DB_PREFIX_ . 'carrier_group` (`id_carrier`, `id_group`)
|
|
VALUES ("' . (int)$id_carrier . '", "' . (int)$id_group . '")
|
|
')) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private static function createZone($id_carrier)
|
|
{
|
|
return DB::getInstance()->Execute('
|
|
INSERT INTO `'._DB_PREFIX_.'carrier_zone`
|
|
(`id_carrier`, `id_zone`)
|
|
VALUES
|
|
("'.(int)$id_carrier.'", "1")');
|
|
}
|
|
|
|
private static function createDelivery($id_carrier, $id_range, $id = null)
|
|
{
|
|
$price = 10;
|
|
if (!empty($id) && $id == 'INPOSTSHIP_CARRIER_5') {
|
|
$price = 15;
|
|
}
|
|
if (!empty($id) && $id == 'INPOSTSHIP_CARRIER_6') {
|
|
$price = 18;
|
|
}
|
|
return DB::getInstance()->Execute('
|
|
INSERT INTO `'._DB_PREFIX_.'delivery`
|
|
(`id_carrier`, `id_range_weight`, `id_zone`, `price`)
|
|
VALUES
|
|
("'.(int)$id_carrier.'", "'.(int)$id_range.'", "1", "'.$price.'")');
|
|
}
|
|
}
|