Improved catalog rule package code style

This commit is contained in:
Jitendra Singh 2020-02-19 17:32:35 +05:30
parent a31fba56b1
commit 46b4b533c3
6 changed files with 70 additions and 57 deletions

View File

@ -78,8 +78,9 @@ class CatalogRuleIndex
public function reindexProduct($product)
{
try {
if (! $product->getTypeInstance()->priceRuleCanBeApplied())
if (! $product->getTypeInstance()->priceRuleCanBeApplied()) {
return;
}
$productIds = $product->getTypeInstance()->isComposite()
? $product->getTypeInstance()->getChildrenIds()
@ -119,8 +120,9 @@ class CatalogRuleIndex
{
static $catalogRules;
if ($catalogRules)
if ($catalogRules) {
return $catalogRules;
}
$catalogRules = $this->catalogRuleRepository->scopeQuery(function($query) {
return $query->where(function ($query1) {

View File

@ -85,16 +85,16 @@ class CatalogRuleProduct
foreach ($rule->channels()->pluck('id') as $channelId) {
foreach ($rule->customer_groups()->pluck('id') as $customerGroupId) {
$rows[] = [
'starts_from' => $startsFrom,
'ends_till' => $endsTill,
'catalog_rule_id' => $rule->id,
'channel_id' => $channelId,
'starts_from' => $startsFrom,
'ends_till' => $endsTill,
'catalog_rule_id' => $rule->id,
'channel_id' => $channelId,
'customer_group_id' => $customerGroupId,
'product_id' => $productId,
'discount_amount' => $rule->discount_amount,
'action_type' => $rule->action_type,
'end_other_rules' => $rule->end_other_rules,
'sort_order' => $rule->sort_order,
'product_id' => $productId,
'discount_amount' => $rule->discount_amount,
'action_type' => $rule->action_type,
'end_other_rules' => $rule->end_other_rules,
'sort_order' => $rule->sort_order,
];
if (count($rows) == $batchCount) {
@ -106,8 +106,9 @@ class CatalogRuleProduct
}
}
if (! empty($rows))
if (! empty($rows)) {
$this->catalogRuleProductRepository->getModel()->insert($rows);
}
}
/**
@ -126,11 +127,13 @@ class CatalogRuleProduct
->leftJoin('channels', 'product_flat.channel', '=', 'channels.code')
->whereIn('channels.id', $rule->channels()->pluck('id')->toArray());
if ($product)
if ($product) {
$qb->where('products.id', $product->id);
}
if (! $rule->conditions)
if (! $rule->conditions) {
return $qb;
}
$appliedAttributes = [];
@ -139,8 +142,9 @@ class CatalogRuleProduct
|| ! isset($condition['value'])
|| is_null($condition['value'])
|| $condition['value'] == ''
|| in_array($condition['attribute'], $appliedAttributes))
|| in_array($condition['attribute'], $appliedAttributes)) {
continue;
}
$appliedAttributes[] = $condition['attribute'];
@ -155,8 +159,9 @@ class CatalogRuleProduct
$validatedProductIds = [];
foreach ($qb->get() as $product) {
if (! $product->getTypeInstance()->priceRuleCanBeApplied())
if (! $product->getTypeInstance()->priceRuleCanBeApplied()) {
continue;
}
if ($this->validator->validate($rule, $product)) {
if ($product->getTypeInstance()->isComposite()) {
@ -181,8 +186,9 @@ class CatalogRuleProduct
{
$attribute = $this->attributeRepository->findOneByField('code', $attributeCode);
if (! $attribute)
if (! $attribute) {
return $query;
}
$query = $query->leftJoin('product_attribute_values as ' . 'pav_' . $attribute->code, function($qb) use($attribute) {
$qb = $qb->where('pav_' . $attribute->code . '.channel', $attribute->value_per_channel ? core()->getDefaultChannelCode() : null)
@ -218,8 +224,9 @@ class CatalogRuleProduct
$qb = $this->addAttributeToSelect('price', $qb);
if ($product) {
if (! $product->getTypeInstance()->priceRuleCanBeApplied())
if (! $product->getTypeInstance()->priceRuleCanBeApplied()) {
return $qb;
}
if ($product->getTypeInstance()->isComposite()) {
$qb->whereIn('catalog_rule_products.product_id', $product->getTypeInstance()->getChildrenIds());

View File

@ -72,9 +72,9 @@ class CatalogRuleProductPrice
public function indexRuleProductPrice($batchCount, $product = null)
{
$dates = [
'current' => $currentDate = Carbon::now(),
'current' => $currentDate = Carbon::now(),
'previous' => (clone $currentDate)->subDays('1')->setTime(23, 59, 59),
'next' => (clone $currentDate)->addDays('1')->setTime(0, 0, 0),
'next' => (clone $currentDate)->addDays('1')->setTime(0, 0, 0),
];
$prices = $endRuleFlags = [];
@ -102,19 +102,20 @@ class CatalogRuleProductPrice
{
$priceKey = $date->getTimestamp() . '-' . $productKey;
if (isset($endRuleFlags[$priceKey]))
if (isset($endRuleFlags[$priceKey])) {
continue;
}
if (! isset($prices[$priceKey])) {
$prices[$priceKey] = [
'rule_date' => $date,
'catalog_rule_id' => $row->catalog_rule_id,
'channel_id' => $row->channel_id,
'rule_date' => $date,
'catalog_rule_id' => $row->catalog_rule_id,
'channel_id' => $row->channel_id,
'customer_group_id' => $row->customer_group_id,
'product_id' => $row->product_id,
'price' => $this->calculate($row),
'starts_from' => $row->starts_from,
'ends_till' => $row->ends_till,
'product_id' => $row->product_id,
'price' => $this->calculate($row),
'starts_from' => $row->starts_from,
'ends_till' => $row->ends_till,
];
} else {
$prices[$priceKey]['price'] = $this->calculate($row, $prices[$priceKey]);
@ -124,8 +125,9 @@ class CatalogRuleProductPrice
$prices[$priceKey]['ends_till'] = min($prices[$priceKey]['ends_till'], $row->ends_till);
}
if ($row->end_other_rules)
if ($row->end_other_rules) {
$endRuleFlags[$priceKey] = true;
}
}
}
@ -201,17 +203,18 @@ class CatalogRuleProductPrice
} else {
$customerGroup = $this->customerGroupRepository->findOneByField('code', 'guest');
if (! $customerGroup)
if (! $customerGroup) {
return;
}
$customerGroupId = $customerGroup->id;
}
return $this->catalogRuleProductPriceRepository->findOneWhere([
'product_id' => $product->id,
'channel_id' => core()->getCurrentChannel()->id,
'product_id' => $product->id,
'channel_id' => core()->getCurrentChannel()->id,
'customer_group_id' => $customerGroupId,
'rule_date' => Carbon::now()->format('Y-m-d'),
'rule_date' => Carbon::now()->format('Y-m-d'),
]);
}
}

View File

@ -83,12 +83,12 @@ class CatalogRuleController extends Controller
public function store()
{
$this->validate(request(), [
'name' => 'required',
'channels' => 'required|array|min:1',
'name' => 'required',
'channels' => 'required|array|min:1',
'customer_groups' => 'required|array|min:1',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'discount_amount' => 'required|numeric'
]);
@ -130,12 +130,12 @@ class CatalogRuleController extends Controller
public function update(Request $request, $id)
{
$this->validate(request(), [
'name' => 'required',
'channels' => 'required|array|min:1',
'name' => 'required',
'channels' => 'required|array|min:1',
'customer_groups' => 'required|array|min:1',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'discount_amount' => 'required|numeric'
]);

View File

@ -36,7 +36,8 @@ class CatalogRuleServiceProvider extends ServiceProvider
*/
protected function registerCommands()
{
if ($this->app->runningInConsole())
if ($this->app->runningInConsole()) {
$this->commands([PriceRuleIndex::class]);
}
}
}

View File

@ -141,18 +141,18 @@ class CatalogRuleRepository extends Repository
{
$attributes = [
[
'key' => 'product',
'label' => trans('admin::app.promotions.catalog-rules.product-attribute'),
'key' => 'product',
'label' => trans('admin::app.promotions.catalog-rules.product-attribute'),
'children' => [
[
'key' => 'product|category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.catalog-rules.categories'),
'key' => 'product|category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.catalog-rules.categories'),
'options' => $this->categoryRepository->getCategoryTree()
], [
'key' => 'product|attribute_family_id',
'type' => 'select',
'label' => trans('admin::app.promotions.catalog-rules.attribute_family'),
'key' => 'product|attribute_family_id',
'type' => 'select',
'label' => trans('admin::app.promotions.catalog-rules.attribute_family'),
'options' => $this->getAttributeFamilies()
]
]
@ -175,9 +175,9 @@ class CatalogRuleRepository extends Repository
$attributeType = 'integer';
$attributes[0]['children'][] = [
'key' => 'product|' . $attribute->code,
'type' => $attribute->type,
'label' => $attribute->name,
'key' => 'product|' . $attribute->code,
'type' => $attribute->type,
'label' => $attribute->name,
'options' => $options
];
}
@ -196,7 +196,7 @@ class CatalogRuleRepository extends Repository
foreach ($this->taxCategoryRepository->all() as $taxCategory) {
$taxCategories[] = [
'id' => $taxCategory->id,
'id' => $taxCategory->id,
'admin_name' => $taxCategory->name,
];
}
@ -215,7 +215,7 @@ class CatalogRuleRepository extends Repository
foreach ($this->attributeFamilyRepository->all() as $attributeFamily) {
$attributeFamilies[] = [
'id' => $attributeFamily->id,
'id' => $attributeFamily->id,
'admin_name' => $attributeFamily->name,
];
}