sarga/packages/Webkul/CartRule/src/Repositories/CartRuleRepository.php

475 lines
16 KiB
PHP
Raw Normal View History

2019-12-21 05:25:32 +00:00
<?php
namespace Webkul\CartRule\Repositories;
use Illuminate\Container\Container as App;
use Webkul\Core\Eloquent\Repository;
use Webkul\Attribute\Repositories\AttributeFamilyRepository;
use Webkul\Attribute\Repositories\AttributeRepository;
use Webkul\Category\Repositories\CategoryRepository;
use Webkul\Tax\Repositories\TaxCategoryRepository;
use Webkul\Core\Repositories\CountryRepository;
use Webkul\Core\Repositories\CountryStateRepository;
class CartRuleRepository extends Repository
{
/**
* AttributeFamilyRepository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Attribute\Repositories\AttributeFamilyRepository
2019-12-21 05:25:32 +00:00
*/
protected $attributeFamilyRepository;
/**
* AttributeRepository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Attribute\Repositories\AttributeRepository
2019-12-21 05:25:32 +00:00
*/
protected $attributeRepository;
/**
* CategoryRepository class
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Category\Repositories\CategoryRepository
2019-12-21 05:25:32 +00:00
*/
protected $categoryRepository;
/**
* CartRuleCouponRepository object
*
2020-03-05 05:34:57 +00:00
* @var Webkul\CartRule\Repositories\CartRuleCouponRepository
2019-12-21 05:25:32 +00:00
*/
protected $cartRuleCouponRepository;
/**
* TaxCategoryRepository class
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Tax\Repositories\TaxCategoryRepository
2019-12-21 05:25:32 +00:00
*/
protected $taxCategoryRepository;
/**
* CountryRepository class
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Repositories\CountryRepository
2019-12-21 05:25:32 +00:00
*/
protected $countryRepository;
/**
* CountryStateRepository class
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Core\Repositories\CountryStateRepository
2019-12-21 05:25:32 +00:00
*/
protected $countryStateRepository;
/**
* Create a new repository instance.
*
2020-03-05 05:34:57 +00:00
* @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository
* @param \Webkul\Attribute\Repositories\AttributeRepository $attributeRepository
* @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository
* @param \Webkul\CartRule\Repositories\CartRuleCouponRepository $cartRuleCouponRepository
* @param \Webkul\Tax\Repositories\TaxCategoryRepository $taxCategoryRepository
* @param \Webkul\Core\Repositories\CountryRepository $countryRepository
* @param \Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
* @param \Illuminate\Container\Container $app
2019-12-21 05:25:32 +00:00
* @return void
*/
public function __construct(
AttributeFamilyRepository $attributeFamilyRepository,
AttributeRepository $attributeRepository,
CategoryRepository $categoryRepository,
CartRuleCouponRepository $cartRuleCouponRepository,
TaxCategoryRepository $taxCategoryRepository,
CountryRepository $countryRepository,
CountryStateRepository $countryStateRepository,
App $app
)
{
$this->attributeFamilyRepository = $attributeFamilyRepository;
$this->attributeRepository = $attributeRepository;
$this->categoryRepository = $categoryRepository;
$this->cartRuleCouponRepository = $cartRuleCouponRepository;
$this->taxCategoryRepository = $taxCategoryRepository;
$this->countryRepository = $countryRepository;
$this->countryStateRepository = $countryStateRepository;
parent::__construct($app);
}
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
return 'Webkul\CartRule\Contracts\CartRule';
}
/**
2020-03-05 05:34:57 +00:00
* @param array $data
* @return \Webkul\CartRule\Contracts\CartRule
2019-12-21 05:25:32 +00:00
*/
public function create(array $data)
{
2020-02-20 12:26:14 +00:00
$data['starts_from'] = isset($data['starts_from']) && $data['starts_from'] ? $data['starts_from'] : null;
2019-12-21 05:25:32 +00:00
2020-02-20 12:26:14 +00:00
$data['ends_till'] = isset($data['ends_till']) && $data['ends_till'] ? $data['ends_till'] : null;
2019-12-21 05:25:32 +00:00
$data['status'] = ! isset($data['status']) ? 0 : 1;
$cartRule = parent::create($data);
$cartRule->channels()->sync($data['channels']);
$cartRule->customer_groups()->sync($data['customer_groups']);
if ($data['coupon_type'] && ! $data['use_auto_generation']) {
$this->cartRuleCouponRepository->create([
2020-02-27 08:03:03 +00:00
'cart_rule_id' => $cartRule->id,
'code' => $data['coupon_code'],
'usage_limit' => $data['usage_per_customer'] ?? 0,
'usage_per_customer' => $data['usage_per_customer'] ?? 0,
'is_primary' => 1,
2020-03-04 06:32:53 +00:00
'expired_at' => $data['ends_till'] ?: null,
2020-02-27 08:03:03 +00:00
]);
2019-12-21 05:25:32 +00:00
}
return $cartRule;
}
/**
2020-03-05 13:37:08 +00:00
* @param array $data
* @param int $id
2020-03-05 05:34:57 +00:00
* @param string $attribute
* @return \Webkul\CartRule\Contracts\CartRule
2019-12-21 05:25:32 +00:00
*/
public function update(array $data, $id, $attribute = "id")
{
$data['starts_from'] = $data['starts_from'] ?: null;
$data['ends_till'] = $data['ends_till'] ?: null;
$data['status'] = ! isset($data['status']) ? 0 : 1;
$data['conditions'] = $data['conditions'] ?? [];
$cartRule = $this->find($id);
parent::update($data, $id, $attribute);
$cartRule->channels()->sync($data['channels']);
$cartRule->customer_groups()->sync($data['customer_groups']);
if ($data['coupon_type']) {
if (! $data['use_auto_generation']) {
$cartRuleCoupon = $this->cartRuleCouponRepository->findOneWhere(['is_primary' => 1, 'cart_rule_id' => $cartRule->id]);
if ($cartRuleCoupon) {
$this->cartRuleCouponRepository->update([
2020-02-27 08:03:03 +00:00
'code' => $data['coupon_code'],
'usage_limit' => $data['uses_per_coupon'] ?? 0,
'usage_per_customer' => $data['usage_per_customer'] ?? 0,
2020-03-04 06:32:53 +00:00
'expired_at' => $data['ends_till'] ?: null,
2020-02-27 08:03:03 +00:00
], $cartRuleCoupon->id);
2019-12-21 05:25:32 +00:00
} else {
$this->cartRuleCouponRepository->create([
2020-02-27 08:03:03 +00:00
'cart_rule_id' => $cartRule->id,
'code' => $data['coupon_code'],
'usage_limit' => $data['uses_per_coupon'] ?? 0,
'usage_per_customer' => $data['usage_per_customer'] ?? 0,
'is_primary' => 1,
2020-03-04 06:32:53 +00:00
'expired_at' => $data['ends_till'] ?: null,
2020-02-27 08:03:03 +00:00
]);
2019-12-21 05:25:32 +00:00
}
} else {
$this->cartRuleCouponRepository->deleteWhere(['is_primary' => 1, 'cart_rule_id' => $cartRule->id]);
$this->cartRuleCouponRepository->getModel()->where('cart_rule_id', $cartRule->id)->update([
2020-02-27 08:03:03 +00:00
'usage_limit' => $data['uses_per_coupon'] ?? 0,
'usage_per_customer' => $data['usage_per_customer'] ?? 0,
2020-03-04 06:32:53 +00:00
'expired_at' => $data['ends_till'] ?: null,
2020-02-27 08:03:03 +00:00
]);
2019-12-21 05:25:32 +00:00
}
} else {
$cartRuleCoupon = $this->cartRuleCouponRepository->deleteWhere(['is_primary' => 1, 'cart_rule_id' => $cartRule->id]);
}
return $cartRule;
}
/**
* Returns attributes for cart rule conditions
*
* @return array
*/
public function getConditionAttributes()
{
$attributes = [
[
2020-02-19 11:49:52 +00:00
'key' => 'cart',
'label' => trans('admin::app.promotions.cart-rules.cart-attribute'),
2019-12-21 05:25:32 +00:00
'children' => [
[
2020-02-19 11:49:52 +00:00
'key' => 'cart|base_sub_total',
'type' => 'price',
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.subtotal'),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart|items_qty',
'type' => 'integer',
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.total-items-qty'),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart|payment_method',
'type' => 'select',
2019-12-21 05:25:32 +00:00
'options' => $this->getPaymentMethods(),
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.payment-method'),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart|shipping_method',
'type' => 'select',
2019-12-21 05:25:32 +00:00
'options' => $this->getShippingMethods(),
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.shipping-method'),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart|postcode',
'type' => 'text',
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.shipping-postcode'),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart|state',
'type' => 'select',
2019-12-21 05:25:32 +00:00
'options' => $this->groupedStatesByCountries(),
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.shipping-state'),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart|country',
'type' => 'select',
2019-12-21 05:25:32 +00:00
'options' => $this->getCountries(),
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.shipping-country'),
2019-12-21 05:25:32 +00:00
]
]
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart_item',
'label' => trans('admin::app.promotions.cart-rules.cart-item-attribute'),
2019-12-21 05:25:32 +00:00
'children' => [
[
2020-02-19 11:49:52 +00:00
'key' => 'cart_item|base_price',
'type' => 'price',
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.price-in-cart'),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart_item|quantity',
'type' => 'integer',
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.qty-in-cart'),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart_item|base_total_weight',
'type' => 'decimal',
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.total-weight'),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'cart_item|base_total',
'type' => 'price',
2020-03-04 06:32:53 +00:00
'label' => trans('admin::app.promotions.cart-rules.subtotal'),
2019-12-21 05:25:32 +00:00
]
]
], [
2020-02-19 11:49:52 +00:00
'key' => 'product',
'label' => trans('admin::app.promotions.cart-rules.product-attribute'),
2019-12-21 05:25:32 +00:00
'children' => [
[
2020-02-19 11:49:52 +00:00
'key' => 'product|category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.cart-rules.categories'),
2020-03-04 06:32:53 +00:00
'options' => $categories = $this->categoryRepository->getCategoryTree(),
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'product|children::category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.cart-rules.children-categories'),
2020-03-04 06:32:53 +00:00
'options' => $categories,
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'product|parent::category_ids',
'type' => 'multiselect',
'label' => trans('admin::app.promotions.cart-rules.parent-categories'),
2020-03-04 06:32:53 +00:00
'options' => $categories,
2019-12-21 05:25:32 +00:00
], [
2020-02-19 11:49:52 +00:00
'key' => 'product|attribute_family_id',
'type' => 'select',
'label' => trans('admin::app.promotions.cart-rules.attribute_family'),
2020-03-04 06:32:53 +00:00
'options' => $this->getAttributeFamilies(),
2019-12-21 05:25:32 +00:00
]
]
]
];
foreach ($this->attributeRepository->findWhereNotIn('type', ['textarea', 'image', 'file']) as $attribute) {
$attributeType = $attribute->type;
if ($attribute->code == 'tax_category_id') {
$options = $this->getTaxCategories();
} else {
$options = $attribute->options;
}
2020-02-19 11:49:52 +00:00
if ($attribute->validation == 'decimal') {
2019-12-21 05:25:32 +00:00
$attributeType = 'decimal';
2020-02-19 11:49:52 +00:00
}
2019-12-21 05:25:32 +00:00
2020-02-19 11:49:52 +00:00
if ($attribute->validation == 'numeric') {
2019-12-21 05:25:32 +00:00
$attributeType = 'integer';
2020-02-19 11:49:52 +00:00
}
2019-12-21 05:25:32 +00:00
$attributes[2]['children'][] = [
2020-02-19 11:49:52 +00:00
'key' => 'product|' . $attribute->code,
'type' => $attribute->type,
'label' => $attribute->name,
2020-03-04 06:32:53 +00:00
'options' => $options,
2019-12-21 05:25:32 +00:00
];
$attributes[2]['children'][] = [
2020-02-19 11:49:52 +00:00
'key' => 'product|children::' . $attribute->code,
'type' => $attribute->type,
'label' => trans('admin::app.promotions.cart-rules.attribute-name-children-only', ['attribute_name' => $attribute->name]),
2020-03-04 06:32:53 +00:00
'options' => $options,
2019-12-21 05:25:32 +00:00
];
$attributes[2]['children'][] = [
2020-02-19 11:49:52 +00:00
'key' => 'product|parent::' . $attribute->code,
'type' => $attribute->type,
'label' => trans('admin::app.promotions.cart-rules.attribute-name-parent-only', ['attribute_name' => $attribute->name]),
2020-03-04 06:32:53 +00:00
'options' => $options,
2019-12-21 05:25:32 +00:00
];
}
return $attributes;
}
/**
* Returns all payment methods
*
* @return array
*/
public function getPaymentMethods()
{
$methods = [];
foreach (config('paymentmethods') as $paymentMethod) {
$object = app($paymentMethod['class']);
$methods[] = [
2020-02-19 11:49:52 +00:00
'id' => $object->getCode(),
2020-03-04 06:32:53 +00:00
'admin_name' => $object->getTitle(),
2019-12-21 05:25:32 +00:00
];
}
return $methods;
}
/**
* Returns all shipping methods
*
* @return array
*/
public function getShippingMethods()
{
$methods = [];
foreach (config('carriers') as $shippingMethod) {
$object = app($shippingMethod['class']);
$methods[] = [
2020-02-19 11:49:52 +00:00
'id' => $object->getCode(),
2020-03-04 06:32:53 +00:00
'admin_name' => $object->getTitle(),
2019-12-21 05:25:32 +00:00
];
}
return $methods;
}
/**
* Returns all countries
*
* @return array
*/
public function getTaxCategories()
{
$taxCategories = [];
foreach ($this->taxCategoryRepository->all() as $taxCategory) {
$taxCategories[] = [
2020-02-19 11:49:52 +00:00
'id' => $taxCategory->id,
2019-12-21 05:25:32 +00:00
'admin_name' => $taxCategory->name,
];
}
return $taxCategories;
}
/**
* Returns all attribute families
*
* @return array
*/
public function getAttributeFamilies()
{
$attributeFamilies = [];
foreach ($this->attributeFamilyRepository->all() as $attributeFamily) {
$attributeFamilies[] = [
2020-02-19 11:49:52 +00:00
'id' => $attributeFamily->id,
2019-12-21 05:25:32 +00:00
'admin_name' => $attributeFamily->name,
];
}
return $attributeFamilies;
}
/**
* Returns all countries
*
* @return array
*/
public function getCountries()
{
$countries = [];
foreach ($this->countryRepository->all() as $country) {
$countries[] = [
2020-02-19 11:49:52 +00:00
'id' => $country->code,
2019-12-21 05:25:32 +00:00
'admin_name' => $country->name,
];
}
return $countries;
}
/**
* Retrieve all grouped states by country code
*
* @return array
*/
public function groupedStatesByCountries()
{
$collection = [];
foreach ($this->countryRepository->all() as $country) {
$countryStates = $this->countryStateRepository->findWhere(
2020-02-27 08:03:03 +00:00
['country_id' => $country->id],
['code', 'default_name as admin_name']
)->toArray();
2019-12-21 05:25:32 +00:00
2020-02-19 11:49:52 +00:00
if (! count($countryStates)) {
2019-12-21 05:25:32 +00:00
continue;
2020-02-19 11:49:52 +00:00
}
2019-12-21 05:25:32 +00:00
$collection[] = [
2020-02-19 11:49:52 +00:00
'id' => $country->code,
2019-12-21 05:25:32 +00:00
'admin_name' => $country->name,
2020-03-04 06:32:53 +00:00
'states' => $countryStates,
2019-12-21 05:25:32 +00:00
];
}
return $collection;
}
}