Skip to content

Commit

Permalink
Fixes code style
Browse files Browse the repository at this point in the history
  • Loading branch information
olance committed Apr 17, 2020
1 parent e9cbd16 commit ac2bfc8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 30 deletions.
6 changes: 4 additions & 2 deletions alma/controllers/hook/paymentOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,18 @@ public function run($params)

private function createPaymentOption($forEUComplianceModule, $ctaText, $action)
{
$baseDir = _PS_MODULE_DIR_ . $this->module->name;

if ($forEUComplianceModule) {
$logo = Media::getMediaPath(_PS_MODULE_DIR_ . $this->module->name . '/views/img/alma_payment_logos.svg');
$logo = Media::getMediaPath($baseDir . '/views/img/alma_payment_logos.svg');
$paymentOption = array(
'cta_text' => $ctaText,
'action' => $action,
'logo' => $logo
);
} else {
$paymentOption = new PrestaShop\PrestaShop\Core\Payment\PaymentOption();
$logo = Media::getMediaPath(_PS_MODULE_DIR_ . $this->module->name . '/views/img/tiny_alma_payment_logos.svg');
$logo = Media::getMediaPath($baseDir . '/views/img/tiny_alma_payment_logos.svg');
$paymentOption
->setModuleName($this->module->name)
->setCallToActionText($ctaText)
Expand Down
6 changes: 4 additions & 2 deletions alma/includes/AlmaPaymentValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,11 @@ public function validatePayment($almaPaymentId)
"merchant_reference" => $order->reference,
));
} catch (RequestError $e) {
AlmaLogger::instance()->error("[Alma] Error updating order reference {$order->reference}: {$e->getMessage()}");
$msg = "[Alma] Error updating order reference {$order->reference}: {$e->getMessage()}";
AlmaLogger::instance()->error($msg);
} catch (PrestaShopException $e) {
AlmaLogger::instance()->error("[Alma] Error updating order reference {$order->reference}: {$e->getMessage()}");
$msg = "[Alma] Error updating order reference {$order->reference}: {$e->getMessage()}";
AlmaLogger::instance()->error($msg);
}

$extraRedirectArgs = '';
Expand Down
86 changes: 65 additions & 21 deletions alma/includes/CartData.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class CartData
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
public static function cartInfo($cart) {
public static function cartInfo($cart)
{
return array(
"items" => self::getCartItems($cart),
"discounts" => self::getCartDiscounts($cart),
Expand All @@ -50,7 +51,8 @@ public static function cartInfo($cart) {
* @param Cart $cart
* @return bool|mixed
*/
private static function includeTaxes($cart) {
private static function includeTaxes($cart)
{
if (version_compare(_PS_VERSION_, '1.7', '>=')) {
$taxConfiguration = new TaxConfiguration();
return $taxConfiguration->includeTaxes();
Expand All @@ -74,15 +76,16 @@ private static function includeTaxes($cart) {
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
private static function getCartItems($cart) {
private static function getCartItems($cart)
{
$items = array();

$summaryDetails = $cart->getSummaryDetails($cart->id_lang, true);
$products = array_merge($summaryDetails['products'], $summaryDetails['gift_products']);
$productsDetails = self::getProductsDetails($products);
$combinationsNames = self::getProductsCombinations($cart, $products);

foreach ($products as $idx => $productRow) {
foreach ($products as $productRow) {
$product = new Product(null, false, $cart->id_lang);
$product->hydrate($productRow);

Expand All @@ -94,15 +97,36 @@ private static function getCartItems($cart) {
$manufacturer_name = $productsDetails[$pid]['manufacturer_name'];
}

$unitPrice = self::includeTaxes($cart) ? (float)$productRow['price_wt'] : (float)$productRow['price'];
$linePrice = self::includeTaxes($cart) ? (float)$productRow['total_wt'] : (float)$productRow['total'];

if (isset($productRow['gift'])) {
$isGift = (bool)$productRow['gift'];
} else {
$isGift = isset($productRow['is_gift']) ? (bool)$productRow['is_gift'] : null;
}

$pictureUrl = $link->getImageLink(
$productRow['link_rewrite'],
$productRow['id_image'],
self::getFormattedImageTypeName('large')
);

if (isset($productRow['is_virtual'])) {
$requiresShipping = !(bool)($productRow['is_virtual']);
} else {
$requiresShipping = !(bool)($productsDetails[$pid]['is_virtual']);
}

$data = array(
'sku' => $productRow['reference'],
'vendor' => $manufacturer_name,
'title' => $productRow['name'],
'variant_title' => null,
'quantity' => (int)$productRow['cart_quantity'],
'unit_price' => almaPriceToCents(self::includeTaxes($cart) ? (float)$productRow['price_wt'] : (float)$productRow['price']),
'line_price' => almaPriceToCents(self::includeTaxes($cart) ? (float)$productRow['total_wt'] : (float)$productRow['total']),
'is_gift' => isset($productRow['gift']) ? (bool)$productRow['gift'] : (isset($productRow['is_gift']) ? (bool)$productRow['is_gift'] : null),
'unit_price' => almaPriceToCents($unitPrice),
'line_price' => almaPriceToCents($linePrice),
'is_gift' => $isGift,
'categories' => array($productRow['category']),
'url' => $link->getProductLink(
$product,
Expand All @@ -116,8 +140,8 @@ private static function getCartItems($cart) {
false,
true
),
'picture_url' => $link->getImageLink($productRow['link_rewrite'], $productRow['id_image'], self::getFormattedImageTypeName('large')),
'requires_shipping' => !(bool)(isset($productRow['is_virtual']) ? $productRow['is_virtual'] : $productsDetails[$pid]['is_virtual']),
'picture_url' => $pictureUrl,
'requires_shipping' => $requiresShipping,
'taxes_included' => self::includeTaxes($cart),
);

Expand All @@ -135,7 +159,8 @@ private static function getCartItems($cart) {
return $items;
}

private static function getFormattedImageTypeName($name) {
private static function getFormattedImageTypeName($name)
{
if (version_compare(_PS_VERSION_, '1.7', '>=')) {
return ImageType::getFormattedName($name);
} else {
Expand All @@ -147,14 +172,15 @@ private static function getFormattedImageTypeName($name) {
* @param array $products
* @return array
*/
private static function getProductsDetails($products) {
private static function getProductsDetails($products)
{
$sql = new DbQuery();
$sql->select('p.`id_product`, p.`is_virtual`, m.`name` as manufacturer_name');
$sql->from('product', 'p');
$sql->innerJoin('manufacturer', 'm', 'm.`id_manufacturer` = p.`id_manufacturer`');

$in = array();
foreach ($products as $idx => $productRow) {
foreach ($products as $productRow) {
$in[] = $productRow['id_product'];
}

Expand Down Expand Up @@ -182,19 +208,34 @@ private static function getProductsDetails($products) {
* @param array $products
* @return array
*/
private static function getProductsCombinations($cart, $products) {
private static function getProductsCombinations($cart, $products)
{
$sql = new DbQuery();
$sql->select('CONCAT(p.`id_product`, "-", pa.`id_product_attribute`) as `unique_id`');

$combinationName = new DbQuery();
$combinationName->select('GROUP_CONCAT(DISTINCT CONCAT(agl.`name`, " - ", al.`name`) SEPARATOR ", ")');
$combinationName->from('product_attribute', 'pa2');
$combinationName->innerJoin('product_attribute_combination', 'pac', 'pac.`id_product_attribute` = pa2.`id_product_attribute`');
$combinationName->innerJoin(
'product_attribute_combination',
'pac',
'pac.`id_product_attribute` = pa2.`id_product_attribute`'
);
$combinationName->innerJoin('attribute', 'a', 'a.`id_attribute` = pac.`id_attribute`');
$combinationName->innerJoin('attribute_lang', 'al', 'a.id_attribute = al.id_attribute AND al.`id_lang` = ' . $cart->id_lang);
$combinationName->innerJoin(
'attribute_lang',
'al',
'a.id_attribute = al.id_attribute AND al.`id_lang` = ' . $cart->id_lang
);
$combinationName->innerJoin('attribute_group', 'ag', 'ag.`id_attribute_group` = a.`id_attribute_group`');
$combinationName->innerJoin('attribute_group_lang', 'agl', 'ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = ' . $cart->id_lang);
$combinationName->where('pa2.`id_product` = p.`id_product` AND pa2.`id_product_attribute` = pa.`id_product_attribute`');
$combinationName->innerJoin(
'attribute_group_lang',
'agl',
'ag.`id_attribute_group` = agl.`id_attribute_group` AND agl.`id_lang` = ' . $cart->id_lang
);
$combinationName->where(
'pa2.`id_product` = p.`id_product` AND pa2.`id_product_attribute` = pa.`id_product_attribute`'
);

/** @noinspection PhpUnhandledExceptionInspection */
$sql->select("({$combinationName->build()}) as combination_name");
Expand All @@ -205,12 +246,13 @@ private static function getProductsCombinations($cart, $products) {
// DbQuery::where joins all where clauses with `) AND (` so for ORs we need a fully built where condition
$where = '';
$op = '';
foreach ($products as $idx => $productRow) {
foreach ($products as $productRow) {
if (!isset($productRow['id_product_attribute']) || !(int)$productRow['id_product_attribute']) {
continue;
}

$where .= "{$op}(p.`id_product` = {$productRow['id_product']} AND pa.`id_product_attribute` = {$productRow['id_product_attribute']})";
$where .= "{$op}(p.`id_product` = {$productRow['id_product']}";
$where .= " AND pa.`id_product_attribute` = {$productRow['id_product_attribute']})";
$op = ' OR ';
}
$sql->where($where);
Expand All @@ -235,14 +277,16 @@ private static function getProductsCombinations($cart, $products) {
* @param Cart $cart
* @return array of discount items
*/
private static function getCartDiscounts($cart) {
private static function getCartDiscounts($cart)
{
$discounts = array();
$cartRules = $cart->getCartRules(CartRule::FILTER_ACTION_ALL, false);

foreach ($cartRules as $cartRule) {
$amount = self::includeTaxes($cart) ? (float)$cartRule["value_real"] : (float)$cartRule["value_tax_exc"];
$discounts[] = array(
"title" => isset($cartRule["name"]) ? $cartRule["name"] : $cartRule["description"],
"amount" => almaPriceToCents(self::includeTaxes($cart) ? (float)$cartRule["value_real"] : (float)$cartRule["value_tax_exc"])
"amount" => almaPriceToCents($amount)
);
}

Expand Down
14 changes: 9 additions & 5 deletions alma/includes/ShippingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class ShippingData
* @param Cart $cart
* @return array|null
*/
public static function shippingInfo($cart) {
public static function shippingInfo($cart)
{
$addressId = $cart->id_address_delivery;
$deliveryOption = $cart->getDeliveryOption(null, true);

Expand Down Expand Up @@ -66,7 +67,7 @@ public static function shippingInfo($cart) {

$shippingInfo["available_options"] = array();
$knownOptions = array();
foreach($deliveryOptionList[$addressId] as $key => $carriers) {
foreach ($deliveryOptionList[$addressId] as $carriers) {
foreach ($carriers['carrier_list'] as $id => $carrierOptionInfo) {
$carrierOption = new Carrier($id, $cart->id_lang);
if (!$carrierOption) {
Expand Down Expand Up @@ -97,7 +98,8 @@ public static function shippingInfo($cart) {
* @param $carrierInfo
* @return array
*/
private static function shippingInfoData($carrier, $carrierInfo) {
private static function shippingInfoData($carrier, $carrierInfo)
{
return array(
"amount" => almaPriceToCents((float)$carrierInfo["price_with_tax"]),
"carrier" => $carrier->name,
Expand All @@ -107,11 +109,13 @@ private static function shippingInfoData($carrier, $carrierInfo) {
);
}

private static function isExpressShipping($carrierInfo) {
private static function isExpressShipping($carrierInfo)
{
return null;
}

private static function isPickupShipping($carrierInfo) {
private static function isPickupShipping($carrierInfo)
{
return null;
}
}

0 comments on commit ac2bfc8

Please sign in to comment.