Skip to content

Commit

Permalink
feat: add order cart rule shop content
Browse files Browse the repository at this point in the history
  • Loading branch information
fox-john committed Aug 28, 2024
1 parent a1c1682 commit 91c370f
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 59 deletions.
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/front/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ services:
- '@PrestaShop\Module\PsEventbus\Repository\OrderRepository'
- '@PrestaShop\Module\PsEventbus\Repository\OrderHistoryRepository'
- '@PrestaShop\Module\PsEventbus\Formatter\ArrayFormatter'

PrestaShop\Module\PsEventbus\Service\ShopContent\OrderCartRulesService:
class: PrestaShop\Module\PsEventbus\Service\ShopContent\OrderCartRulesService
public: true
arguments:
- '@PrestaShop\Module\PsEventbus\Repository\OrderCartRuleRepository'
97 changes: 58 additions & 39 deletions src/Repository/OrderCartRuleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,96 @@

namespace PrestaShop\Module\PsEventbus\Repository;

class OrderCartRuleRepository
use PrestaShop\Module\PsEventbus\Interfaces\RepositoryInterface;

class OrderCartRuleRepository extends AbstractRepository implements RepositoryInterface
{
const TABLE_NAME = 'order_cart_rule';

/**
* @var \Db
* @return mixed
*
* @throws \PrestaShopException
*/
private $db;

public function __construct()
public function generateBaseQuery()
{
$this->db = \Db::getInstance();
$this->query = new \DbQuery();

$this->query->from(self::TABLE_NAME, 'ocr');

$this->query->select('ocr.id_order_cart_rule');
$this->query->select('ocr.id_order');
$this->query->select('ocr.id_cart_rule');
$this->query->select('ocr.id_order_invoice');
$this->query->select('ocr.name');
$this->query->select('ocr.value');
$this->query->select('ocr.value_tax_excl');
$this->query->select('ocr.free_shipping');

if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.7.0', '>=')) {
$this->query->select('ocr.deleted');
}
}

/**
* @return \DbQuery
* @param int $offset
* @param int $limit
* @param string $langIso
* @param bool $debug
*
* @return array<mixed>
*
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
*/
public function getBaseQuery()
public function getContentsForFull($offset, $limit, $langIso, $debug)
{
$query = new \DbQuery();
$this->generateBaseQuery();

$query->from(self::TABLE_NAME, 'ocr');
$this->query->limit((int) $limit, (int) $offset);

return $query;
return $this->runQuery($debug);
}

/**
* @param array<mixed> $orderIds
* @param int $limit
* @param array<mixed> $contentIds
* @param string $langIso
* @param bool $debug
*
* @return array<mixed>|bool|\mysqli_result|\PDOStatement|resource|null
* @return array<mixed>
*
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
*/
public function getOrderCartRules($orderIds)
public function getContentsForIncremental($limit, $contentIds, $langIso, $debug)
{
if (!$orderIds) {
return [];
}

$query = $this->getBaseQuery();
$this->generateBaseQuery();

$query->select('ocr.id_order_cart_rule,ocr.id_order,ocr.id_cart_rule,ocr.id_order_invoice,ocr.name,ocr.value,ocr.value_tax_excl, ocr.free_shipping');

if (defined('_PS_VERSION_') && version_compare(_PS_VERSION_, '1.7.7.0', '>=')) {
$query->select('ocr.deleted');
}
$query->where('ocr.id_order IN (' . implode(',', array_map('intval', $orderIds)) . ')');
$this->query->where('ocr.id_order IN(' . implode(',', array_map('intval', $contentIds)) . ')')
->limit($limit);

return $this->db->executeS($query);
return $this->runQuery($debug);
}

/**
* @param array<mixed> $orderIds
* @param int $offset
* @param string $langIso
* @param bool $debug
*
* @return array<mixed>
* @return int
*
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
*/
public function getOrderCartRuleIdsByOrderIds($orderIds)
public function countFullSyncContentLeft($offset, $langIso, $debug)
{
if (!$orderIds) {
return [];
}

$query = $this->getBaseQuery();
$result = $this->getContentsForFull($offset, 1, $langIso, $debug);

$query->select('ocr.id_order_cart_rule as id');
$query->where('ocr.id_order IN (' . implode(',', array_map('intval', $orderIds)) . ')');

$result = $this->db->executeS($query);
if (!is_array($result) || empty($result)) {
return 0;
}

return is_array($result) ? $result : [];
return count($result);
}
}
66 changes: 49 additions & 17 deletions src/Repository/OrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,58 @@ public function generateBaseQuery()
->where('o.id_shop = ' . (int) parent::getShopId())
->groupBy('o.id_order');

$this->query->select('o.id_order, o.reference, o.id_customer, o.id_cart, o.current_state');
$this->query->select('o.conversion_rate, o.total_paid_tax_excl, o.total_paid_tax_incl');
$this->query->select('IF((SELECT so.id_order FROM `' . _DB_PREFIX_ . 'orders` so WHERE so.id_customer = o.id_customer AND so.id_order < o.id_order LIMIT 1) > 0, 0, 1) as new_customer');
$this->query->select('c.iso_code as currency, SUM(os.total_products_tax_incl + os.total_shipping_tax_incl) as refund');
$this->query->select('SUM(os.total_products_tax_excl + os.total_shipping_tax_excl) as refund_tax_excl, o.module as payment_module');
$this->query->select('o.payment as payment_mode, o.total_paid_real, o.total_shipping as shipping_cost, o.date_add as created_at');
$this->query->select('o.date_upd as updated_at, o.id_carrier');
$this->query->select('o.id_order');
$this->query->select('o.reference');
$this->query->select('o.id_customer');
$this->query->select('o.id_cart');
$this->query->select('o.current_state');
$this->query->select('o.conversion_rate');
$this->query->select('o.total_paid_tax_excl');
$this->query->select('o.total_paid_tax_incl');

$this->query->select('c.iso_code as currency');
$this->query->select('o.module as payment_module');
$this->query->select('o.payment as payment_mode');
$this->query->select('o.total_paid_real');
$this->query->select('o.total_shipping as shipping_cost');
$this->query->select('o.date_add as created_at');
$this->query->select('o.date_upd as updated_at');
$this->query->select('o.id_carrier');
$this->query->select('o.payment as payment_name');
$this->query->select('CONCAT(CONCAT("delivery", ":", cntd.iso_code), ",", CONCAT("invoice", ":", cnti.iso_code)) as address_iso');
$this->query->select('o.valid as is_validated');
$this->query->select('ost.paid as is_paid');
$this->query->select('ost.shipped as is_shipped');
$this->query->select('osl.name as status_label');
$this->query->select('o.module as payment_name');

$this->query->select('o.id_shop_group, o.id_shop, o.id_lang, o.id_currency, o.recyclable, o.gift');
$this->query->select('o.total_discounts, o.total_discounts_tax_incl, o.total_discounts_tax_excl');
$this->query->select('o.total_products, o.total_products_wt, o.total_shipping_tax_incl, o.total_shipping_tax_excl');
$this->query->select('o.carrier_tax_rate, o.total_wrapping, o.total_wrapping_tax_incl, o.total_wrapping_tax_excl');
$this->query->select('o.round_mode, o.round_type, o.invoice_number, o.delivery_number, o.invoice_date, o.delivery_date, o.valid');
$this->query->select('o.id_shop_group');
$this->query->select('o.id_shop');
$this->query->select('o.id_lang');
$this->query->select('o.id_currency');
$this->query->select('o.recyclable');
$this->query->select('o.gift');
$this->query->select('o.total_discounts');
$this->query->select('o.total_discounts_tax_incl');
$this->query->select('o.total_discounts_tax_excl');
$this->query->select('o.total_products');
$this->query->select('o.total_products_wt');
$this->query->select('o.total_shipping_tax_incl');
$this->query->select('o.total_shipping_tax_excl');
$this->query->select('o.carrier_tax_rate');
$this->query->select('o.total_wrapping');
$this->query->select('o.total_wrapping_tax_incl');
$this->query->select('o.total_wrapping_tax_excl');
$this->query->select('o.round_mode');
$this->query->select('o.round_type');
$this->query->select('o.invoice_number');
$this->query->select('o.delivery_number');
$this->query->select('o.invoice_date');
$this->query->select('o.delivery_date');
$this->query->select('o.valid');

$this->query->select('SUM(os.total_products_tax_incl + os.total_shipping_tax_incl) as refund');
$this->query->select('SUM(os.total_products_tax_excl + os.total_shipping_tax_excl) as refund_tax_excl');
$this->query->select('CONCAT(CONCAT("delivery", ":", cntd.iso_code), ",", CONCAT("invoice", ":", cnti.iso_code)) as address_iso');
$this->query->select('IF((SELECT so.id_order FROM `' . _DB_PREFIX_ . 'orders` so WHERE so.id_customer = o.id_customer AND so.id_order < o.id_order LIMIT 1) > 0, 0, 1) as new_customer');
}

/**
Expand Down Expand Up @@ -104,12 +136,12 @@ public function getContentsForIncremental($limit, $contentIds, $langIso, $debug)
*/
public function countFullSyncContentLeft($offset, $langIso, $debug)
{
$orders = $this->getContentsForFull($offset, 1, $langIso, $debug);
$result = $this->getContentsForFull($offset, 1, $langIso, $debug);

if (!is_array($orders) || empty($orders)) {
if (!is_array($result) || empty($result)) {
return 0;
}

return count($orders);
return count($result);
}
}
6 changes: 6 additions & 0 deletions src/Service/FrontApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ public function handleDataSync($shopContent, $jobId, $langIso, $limit, $isFull,
CommonService::exitWithExceptionMessage($exception);
} catch (\Exception $exception) {
$this->errorHandler->handle($exception);

// if debug mode enabled, print error
if (_PS_MODE_DEV_ == true) {
throw $exception;
}

CommonService::dieWithResponse(['message' => 'An error occured. Please check shop logs for more information'], 500);
}
}
Expand Down
124 changes: 124 additions & 0 deletions src/Service/ShopContent/OrderCartRulesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace PrestaShop\Module\PsEventbus\Service\ShopContent;

use PrestaShop\Module\PsEventbus\Config\Config;
use PrestaShop\Module\PsEventbus\Interfaces\ShopContentServiceInterface;
use PrestaShop\Module\PsEventbus\Repository\OrderCartRuleRepository;

class OrderCartRulesService implements ShopContentServiceInterface
{
/** @var OrderCartRuleRepository */
private $orderCartRuleRepository;

public function __construct(
OrderCartRuleRepository $orderCartRuleRepository,
) {
$this->orderCartRuleRepository = $orderCartRuleRepository;
}

/**
* @param int $offset
* @param int $limit
* @param string $langIso
* @param bool $debug
*
* @return array<mixed>
*/
public function getContentsForFull($offset, $limit, $langIso, $debug)
{
$orders = $this->orderCartRuleRepository->getContentsForFull($offset, $limit, $langIso, $debug);

if (empty($orders)) {
return [];
}

$this->castOrderCartRules($orders, $langIso);

return array_map(function ($order) {
return [
'id' => $order['id_order'],
'collection' => Config::COLLECTION_ORDERS,
'properties' => $order,
];
}, $orders);
}

/**
* @param int $limit
* @param array<string, int> $contentIds
* @param string $langIso
* @param bool $debug
*
* @return array<mixed>
*/
public function getContentsForIncremental($limit, $contentIds, $langIso, $debug)
{
$orders = $this->orderCartRuleRepository->getContentsForIncremental($limit, $contentIds, $langIso, $debug);

if (empty($orders)) {
return [];
}

$this->castOrderCartRules($orders, $langIso);

return array_map(function ($order) {
return [
'id' => $order['id_order'],
'collection' => Config::COLLECTION_ORDERS,
'properties' => $order,
];
}, $orders);
}

/**
* @param int $offset
* @param string $langIso
* @param bool $debug
*
* @return int
*/
public function countFullSyncContentLeft($offset, $langIso, $debug)
{
return (int) $this->orderCartRuleRepository->countFullSyncContentLeft($offset, $langIso, $debug);
}

/**
* @param array<mixed> $orders
* @param string $langIso
*
* @return void
*/
public function castOrderCartRules(&$cartRules, $langIso)
{
foreach ($cartRules as &$cartRule) {
$cartRule['id_cart_rule'] = (int) $cartRule['id_cart_rule'];
$cartRule['id_customer'] = (int) $cartRule['id_customer'];
$cartRule['quantity'] = (int) $cartRule['quantity'];
$cartRule['quantity_per_user'] = (int) $cartRule['quantity_per_user'];
$cartRule['priority'] = (int) $cartRule['priority'];
$cartRule['partial_use'] = (bool) $cartRule['partial_use'];
$cartRule['minimum_amount'] = (float) $cartRule['minimum_amount'];
$cartRule['minimum_amount_tax'] = (bool) $cartRule['minimum_amount_tax'];
$cartRule['minimum_amount_currency'] = (int) $cartRule['minimum_amount_currency'];
$cartRule['minimum_amount_shipping'] = (bool) $cartRule['minimum_amount_shipping'];
$cartRule['country_restriction'] = (bool) $cartRule['country_restriction'];
$cartRule['carrier_restriction'] = (bool) $cartRule['carrier_restriction'];
$cartRule['group_restriction'] = (bool) $cartRule['group_restriction'];
$cartRule['cart_rule_restriction'] = (bool) $cartRule['cart_rule_restriction'];
$cartRule['product_restriction'] = (bool) $cartRule['product_restriction'];
$cartRule['shop_restriction'] = (bool) $cartRule['shop_restriction'];
$cartRule['free_shipping'] = (bool) $cartRule['free_shipping'];
$cartRule['reduction_percent'] = (float) $cartRule['reduction_percent'];
$cartRule['reduction_amount'] = (float) $cartRule['reduction_amount'];
$cartRule['reduction_tax'] = (bool) $cartRule['reduction_tax'];
$cartRule['reduction_currency'] = (int) $cartRule['reduction_currency'];
$cartRule['reduction_product'] = (int) $cartRule['reduction_product'];
$cartRule['reduction_exclude_special'] = (bool) $cartRule['reduction_exclude_special'];
$cartRule['gift_product'] = (int) $cartRule['gift_product'];
$cartRule['gift_product_attribute'] = (int) $cartRule['gift_product_attribute'];
$cartRule['highlight'] = (bool) $cartRule['highlight'];
$cartRule['active'] = (bool) $cartRule['active'];
}
}
}
4 changes: 2 additions & 2 deletions src/Service/SynchronizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function sendFullSync(
) {
$response = [];

$serviceName = str_replace('-', '', ucwords($shopContent, '-'));
$serviceName = str_replace('_', '', ucwords($shopContent, '_'));
$serviceId = 'PrestaShop\Module\PsEventbus\Service\ShopContent\\' . $serviceName . 'Service'; // faire un mapping entre le service et le nom du shopcontent

/** @var \Ps_eventbus */
Expand Down Expand Up @@ -152,7 +152,7 @@ public function sendIncrementalSync(
) {
$response = [];

$serviceName = str_replace('-', '', ucwords($shopContent, '-'));
$serviceName = str_replace('_', '', ucwords($shopContent, '_'));
$serviceId = 'PrestaShop\Module\PsEventbus\Service\ShopContent\\' . $serviceName . 'Service';

/** @var \Ps_eventbus */
Expand Down

0 comments on commit 91c370f

Please sign in to comment.