|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DpdConnect\classes; |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of the Prestashop Shipping module of DPD Nederland B.V. |
| 7 | + * |
| 8 | + * Copyright (C) 2017 DPD Nederland B.V. |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU General Public License as published by |
| 12 | + * the Free Software Foundation, either version 3 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU General Public License |
| 21 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | + |
| 24 | +use Db; |
| 25 | +use Carrier; |
| 26 | +use DbQuery; |
| 27 | +use CarrierModule; |
| 28 | +use Configuration; |
| 29 | + |
| 30 | +class DpdCarrier extends CarrierModule |
| 31 | +{ |
| 32 | + public $carrierNames; |
| 33 | + public $dpdPrefix; |
| 34 | + |
| 35 | + public function __construct() |
| 36 | + { |
| 37 | + $this->dpdPrefix = 'dpdconnect_'; |
| 38 | + |
| 39 | + $this->carrierNames['predict'] = [ |
| 40 | + 'name' => 'DPD Predict', |
| 41 | + 'description' => $this->l('DPD predict delivery'), |
| 42 | + 'type' => 'b2c' |
| 43 | + ]; |
| 44 | + |
| 45 | + $this->carrierNames['parcelshop'] = [ |
| 46 | + 'name' => 'DPD Parcelshop', |
| 47 | + 'description' => $this->l('deliver it at a Parcelshop'), |
| 48 | + 'type' => 'b2c' |
| 49 | + ]; |
| 50 | + |
| 51 | + $this->carrierNames['saturday'] = [ |
| 52 | + 'name' => 'DPD Zaterdag', |
| 53 | + 'description' => $this->l('only deliver at a saturday'), |
| 54 | + 'type' => 'b2c', |
| 55 | + ]; |
| 56 | + |
| 57 | + $this->carrierNames['classic_saturday'] = [ |
| 58 | + 'name' => 'DPD Classic Zaterdag', |
| 59 | + 'description' => $this->l('only deliver at a saturday'), |
| 60 | + 'type' => 'b2b' |
| 61 | + ]; |
| 62 | + |
| 63 | + $this->carrierNames['classic'] = [ |
| 64 | + 'name' => 'DPD Classic', |
| 65 | + 'description' => $this->l('DPD classic delivery'), |
| 66 | + 'type' => 'b2b' |
| 67 | + ]; |
| 68 | + |
| 69 | + $this->carrierNames['guarantee18'] = [ |
| 70 | + 'name' => 'Guarantee 18:00', |
| 71 | + 'description' => $this->l('DPD Guarantee 18:00 delivery'), |
| 72 | + 'type' => 'b2b' |
| 73 | + ]; |
| 74 | + |
| 75 | + $this->carrierNames['express12'] = [ |
| 76 | + 'name' => 'Express 12:00', |
| 77 | + 'description' => $this->l('DPD Express 12:00 delivery'), |
| 78 | + 'type' => 'b2b' |
| 79 | + ]; |
| 80 | + |
| 81 | + $this->carrierNames['express10'] = [ |
| 82 | + 'name' => 'Express 10:00', |
| 83 | + 'description' => $this->l('DPD Express 10:00 delivery'), |
| 84 | + 'type' => 'b2b' |
| 85 | + ]; |
| 86 | + } |
| 87 | + |
| 88 | + public function createCarriers() |
| 89 | + { |
| 90 | + foreach ($this->carrierNames as $prefix => $info) { |
| 91 | + if (empty(Configuration::get($this->dpdPrefix . strtolower($prefix)))) { |
| 92 | + $carrier = new Carrier(); |
| 93 | + |
| 94 | + $carrier->url ='//tracking.dpd.de/parcelstatus?query=@'; |
| 95 | + $carrier->name = $info['name']; |
| 96 | + $carrier->delay[Configuration::get('PS_LANG_DEFAULT')] = (string)$info['description']; |
| 97 | + $carrier->active = 0; |
| 98 | + $carrier->deleted = 1; |
| 99 | + $carrier->shipping_handling = 1; |
| 100 | + $carrier->range_behavior = 0; |
| 101 | + $carrier->shipping_external = 0; |
| 102 | + $carrier->add(); |
| 103 | + $carrier->id_reference = $carrier->id; |
| 104 | + $carrier->update(); |
| 105 | + Configuration::updateValue($this->dpdPrefix . strtolower($prefix), $carrier->id); |
| 106 | + |
| 107 | +// copy(dirname(__DIR__) . '/../logo.png', _PS_SHIP_IMG_DIR_ . '/' . (int)$carrier->id . '.jpg'); //assign carrier logo |
| 108 | + } |
| 109 | + } |
| 110 | + $this->setCarrierForAccountType(); |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + public function setCarrierForAccountType() |
| 115 | + { |
| 116 | + $accountType = Configuration::get($this->dpdPrefix . 'account_type'); |
| 117 | + |
| 118 | + foreach ($this->carrierNames as $prefix => $info) { |
| 119 | + $configCarrierId = Configuration::get($this->dpdPrefix . $prefix); |
| 120 | + |
| 121 | + $carrierId = $this->getLatestCarrierByReferenceId($configCarrierId, false); |
| 122 | + if ($info['type'] == $accountType) { |
| 123 | + $this->unDeleteCarrier($carrierId); |
| 124 | + } else { |
| 125 | + $this->softDeleteCarriers($carrierId); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + public function deleteCarriers() |
| 132 | + { |
| 133 | + foreach ($this->carrierNames as $prefix => $info) { |
| 134 | + $carrier_id = $this->getLatestCarrierByReferenceId(Configuration::get($this->dpdPrefix . strtolower($prefix))); |
| 135 | + if ($this->softDeleteCarriers($carrier_id)) { |
| 136 | + $output = true; |
| 137 | + } else { |
| 138 | + $output = false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return $output; |
| 143 | + } |
| 144 | + public function getLatestCarrierByReferenceId($id_carrier, $except_deleted = true) |
| 145 | + { |
| 146 | + if ($id_carrier === false) { |
| 147 | + return; |
| 148 | + } |
| 149 | + $sql = new DbQuery(); |
| 150 | + $sql->from('carrier'); |
| 151 | + $sql->select('id_carrier'); |
| 152 | + $sql->where('id_reference = ' . pSQL($id_carrier)); |
| 153 | + if ($except_deleted) { |
| 154 | + $sql->where('deleted != 1'); |
| 155 | + } |
| 156 | + $sql->orderBy('id_carrier DESC'); |
| 157 | + $sql->limit(1); |
| 158 | + |
| 159 | + $result = Db::getInstance()->ExecuteS($sql); |
| 160 | + if (count($result) == 0) { |
| 161 | + return $id_carrier; |
| 162 | + } else { |
| 163 | + return current($result)['id_carrier']; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + public function ifHasSameReferenceId($carrierId, $referenceId) |
| 168 | + { |
| 169 | + $tempCarrier = new Carrier($carrierId); |
| 170 | + return $tempCarrier->id_reference == $referenceId; |
| 171 | + } |
| 172 | + |
| 173 | + public function softDeleteCarriers($carrier_id) |
| 174 | + { |
| 175 | + Db::getInstance()->update('carrier', ['deleted' => 1], 'id_carrier = '. pSQL($carrier_id)); |
| 176 | + return true; |
| 177 | + } |
| 178 | + |
| 179 | + public function unDeleteCarrier($carrier_id) |
| 180 | + { |
| 181 | + Db::getInstance()->update('carrier', ['deleted' => 0], 'id_carrier = '. pSQL($carrier_id)); |
| 182 | + return true; |
| 183 | + } |
| 184 | + |
| 185 | + public function checkIfSaturdayAllowed() |
| 186 | + { |
| 187 | + $showfromday = Configuration::get('dpdconnect_saturday_showfromday'); |
| 188 | + $showfromtime = Configuration::get('dpdconnect_saturday_showfromtime'); |
| 189 | + $showtilltime = Configuration::get('dpdconnect_saturday_showtilltime'); |
| 190 | + $showtillday = Configuration::get('dpdconnect_saturday_showtillday'); |
| 191 | + if (empty($showfromday) || empty($showfromtime) || empty($showtillday) || empty($showtilltime)) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + $showfromtime = explode(':', $showfromtime); |
| 195 | + $firstDate = new \DateTime($showfromday . ' this week ' . $showfromtime[0] . ' hours ' . $showfromtime[1] . ' minutes 00 seconds'); |
| 196 | + $showtilltime = explode(':', $showtilltime); |
| 197 | + $lastDate = new \DateTime($showtillday . ' this week ' . $showtilltime[0] . ' hours ' . $showtilltime[1] . ' minutes 59 seconds'); |
| 198 | + |
| 199 | + $today = new \DateTime(); |
| 200 | + |
| 201 | + return $today >= $firstDate && $today <= $lastDate; |
| 202 | + } |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + public function getOrderShippingCost($params, $shipping_cost) |
| 207 | + { |
| 208 | + } |
| 209 | + |
| 210 | + public function getOrderShippingCostExternal($params) |
| 211 | + { |
| 212 | + } |
| 213 | + |
| 214 | + public function getShortNameShipping($carrierId) |
| 215 | + { |
| 216 | + $tempCarrier = new Carrier($carrierId); |
| 217 | + $tempCarrierReferenceId = $tempCarrier->id_reference; |
| 218 | + |
| 219 | + foreach ($this->carrierNames as $prefix => $info) { |
| 220 | + if (Configuration::get($this->dpdPrefix . $prefix) == $tempCarrierReferenceId) { |
| 221 | + if ($prefix == 'guarantee18') { |
| 222 | + return 'DPD 18'; |
| 223 | + } elseif ($prefix == 'classic_saturday') { |
| 224 | + return 'DPD B2B Sat'; |
| 225 | + } elseif ($prefix == 'saturday') { |
| 226 | + return 'DPD B2C Sat'; |
| 227 | + } elseif ($prefix == 'express12') { |
| 228 | + return 'DPD 12'; |
| 229 | + } elseif ($prefix == 'express10') { |
| 230 | + return 'DPD 10'; |
| 231 | + } else { |
| 232 | + return $tempCarrier->name; |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + |
| 239 | + public function checkIfCarrierIsDpd($carrierId) |
| 240 | + { |
| 241 | + $dpdCarrierIds = []; |
| 242 | + foreach ($this->carrierNames as $prefix => $info) { |
| 243 | + $dpdCarrierIds[] = Configuration::get($this->dpdPrefix . strtolower($prefix)); |
| 244 | + } |
| 245 | + return in_array($carrierId, $dpdCarrierIds); |
| 246 | + } |
| 247 | +} |
0 commit comments