Inital commit
This commit is contained in:
@@ -13,10 +13,9 @@
|
||||
|
||||
namespace Colissimo;
|
||||
|
||||
use Colissimo\Model\ColissimoFreeshippingQuery;
|
||||
use Colissimo\Model\Config\ColissimoConfigValue;
|
||||
use Propel\Runtime\Connection\ConnectionInterface;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Exception\OrderException;
|
||||
use Thelia\Install\Database;
|
||||
use Thelia\Model\Country;
|
||||
use Thelia\Module\AbstractDeliveryModule;
|
||||
@@ -31,37 +30,52 @@ class Colissimo extends AbstractDeliveryModule
|
||||
|
||||
const JSON_PRICE_RESOURCE = "/Config/prices.json";
|
||||
|
||||
const MESSAGE_DOMAIN = 'colissimo';
|
||||
const DOMAIN_NAME = 'colissimo';
|
||||
|
||||
public static function getPrices()
|
||||
{
|
||||
if (null === self::$prices) {
|
||||
self::$prices = json_decode(file_get_contents(sprintf('%s%s', __DIR__, self::JSON_PRICE_RESOURCE)), true);
|
||||
self::$prices = json_decode(Colissimo::getConfigValue(ColissimoConfigValue::PRICES, null), true);
|
||||
}
|
||||
|
||||
return self::$prices;
|
||||
}
|
||||
|
||||
public function isValidDelivery(Country $country) {
|
||||
public function postActivation(ConnectionInterface $con = null)
|
||||
{
|
||||
self::setConfigValue(ColissimoConfigValue::ENABLED, 1);
|
||||
|
||||
$areaId = $country->getAreaId();
|
||||
$database = new Database($con);
|
||||
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
|
||||
}
|
||||
|
||||
$prices = self::getPrices();
|
||||
public function isValidDelivery(Country $country)
|
||||
{
|
||||
if (0 == self::getConfigValue(ColissimoConfigValue::ENABLED, 1)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Check if Colissimo delivers the area */
|
||||
if (isset($prices[$areaId]) && isset($prices[$areaId]["slices"])) {
|
||||
if (null !== $area = $this->getAreaForCountry($country)) {
|
||||
$areaId = $area->getId();
|
||||
|
||||
// Yes ! Check if the cart weight is below slice limit
|
||||
$areaPrices = $prices[$areaId]["slices"];
|
||||
ksort($areaPrices);
|
||||
$prices = self::getPrices();
|
||||
|
||||
/* Check cart weight is below the maximum weight */
|
||||
end($areaPrices);
|
||||
$maxWeight = key($areaPrices);
|
||||
/* Check if Colissimo delivers the area */
|
||||
if (isset($prices[$areaId]) && isset($prices[$areaId]["slices"])) {
|
||||
// Yes ! Check if the cart weight is below slice limit
|
||||
$areaPrices = $prices[$areaId]["slices"];
|
||||
ksort($areaPrices);
|
||||
|
||||
$cartWeight = $this->getRequest()->getSession()->getCart()->getWeight();
|
||||
/* Check cart weight is below the maximum weight */
|
||||
end($areaPrices);
|
||||
$maxWeight = key($areaPrices);
|
||||
|
||||
if ($cartWeight <= $maxWeight) return true;
|
||||
$cartWeight = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->getWeight();
|
||||
|
||||
if ($cartWeight <= $maxWeight) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -76,7 +90,7 @@ class Colissimo extends AbstractDeliveryModule
|
||||
*/
|
||||
public static function getPostageAmount($areaId, $weight)
|
||||
{
|
||||
$freeshipping = ColissimoFreeshippingQuery::create()->getLast();
|
||||
$freeshipping = Colissimo::getConfigValue(ColissimoConfigValue::FREE_SHIPPING);
|
||||
$postage = 0;
|
||||
if (!$freeshipping) {
|
||||
$prices = self::getPrices();
|
||||
@@ -84,7 +98,11 @@ class Colissimo extends AbstractDeliveryModule
|
||||
/* check if Colissimo delivers the asked area */
|
||||
if (!isset($prices[$areaId]) || !isset($prices[$areaId]["slices"])) {
|
||||
throw new DeliveryException(
|
||||
Translator::getInstance()->trans("Colissimo delivery unavailable for the delivery country", [], self::MESSAGE_DOMAIN)
|
||||
Translator::getInstance()->trans(
|
||||
"Colissimo delivery unavailable for the delivery country",
|
||||
[],
|
||||
self::DOMAIN_NAME
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -99,7 +117,7 @@ class Colissimo extends AbstractDeliveryModule
|
||||
Translator::getInstance()->trans(
|
||||
"Colissimo delivery unavailable for this cart weight (%weight kg)",
|
||||
array("%weight" => $weight),
|
||||
self::MESSAGE_DOMAIN
|
||||
self::DOMAIN_NAME
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -118,13 +136,6 @@ class Colissimo extends AbstractDeliveryModule
|
||||
|
||||
}
|
||||
|
||||
public function postActivation(ConnectionInterface $con = null)
|
||||
{
|
||||
$database = new Database($con);
|
||||
|
||||
$database->insertSql(null, array(__DIR__ . '/Config/thelia.sql'));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* calculate and return delivery price
|
||||
@@ -135,13 +146,32 @@ class Colissimo extends AbstractDeliveryModule
|
||||
*/
|
||||
public function getPostage(Country $country)
|
||||
{
|
||||
$cartWeight = $this->getRequest()->getSession()->getCart()->getWeight();
|
||||
$cartWeight = $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->getWeight();
|
||||
|
||||
$postage = self::getPostageAmount(
|
||||
$country->getAreaId(),
|
||||
$this->getAreaForCountry($country)->getId(),
|
||||
$cartWeight
|
||||
);
|
||||
|
||||
return $postage;
|
||||
}
|
||||
}
|
||||
|
||||
public function update($currentVersion, $newVersion, ConnectionInterface $con = null)
|
||||
{
|
||||
$uploadDir = __DIR__ . '/Config/prices.json';
|
||||
|
||||
$database = new Database($con);
|
||||
|
||||
$tableExists = $database->execute("SHOW TABLES LIKE 'colissimo_freeshipping'")->rowCount();
|
||||
|
||||
if (Colissimo::getConfigValue(ColissimoConfigValue::FREE_SHIPPING, null) == null && $tableExists) {
|
||||
$result = $database->execute('SELECT active FROM colissimo_freeshipping WHERE id=1')->fetch()["active"];
|
||||
Colissimo::setConfigValue(ColissimoConfigValue::FREE_SHIPPING, $result);
|
||||
$database->execute("DROP TABLE `colissimo_freeshipping`");
|
||||
}
|
||||
|
||||
if (is_readable($uploadDir) && Colissimo::getConfigValue(ColissimoConfigValue::PRICES, null) == null) {
|
||||
Colissimo::setConfigValue(ColissimoConfigValue::PRICES, file_get_contents($uploadDir));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user