2019-05-12 21:46:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Discount\Repositories;
|
|
|
|
|
|
|
|
|
|
use Webkul\Core\Eloquent\Repository;
|
2019-05-23 22:05:53 +00:00
|
|
|
use Webkul\Discount\Repositories\CartRuleChannelsRepository as CartRuleChannels;
|
|
|
|
|
use Webkul\Discount\Repositories\CartRuleCustomerGroupsRepository as CartRuleCustomerGroups;
|
|
|
|
|
use Illuminate\Container\Container as App;
|
2019-05-12 21:46:24 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cart Rule Reposotory
|
|
|
|
|
*
|
|
|
|
|
* @author Prashant Singh <prashant.singh852@webkul.com>
|
|
|
|
|
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
|
|
|
|
*/
|
|
|
|
|
class CartRuleRepository extends Repository
|
|
|
|
|
{
|
2019-05-23 22:05:53 +00:00
|
|
|
protected $cartRuleChannels;
|
|
|
|
|
|
|
|
|
|
protected $cartRuleCustomerGroups;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(CartRuleChannels $cartRuleChannels, CartRuleCustomerGroups $cartRuleCustomerGroups, App $app)
|
|
|
|
|
{
|
|
|
|
|
$this->cartRuleChannels = $cartRuleChannels;
|
|
|
|
|
$this->cartRuleCustomerGroups = $cartRuleCustomerGroups;
|
|
|
|
|
|
|
|
|
|
parent::__construct($app);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-12 21:46:24 +00:00
|
|
|
/**
|
|
|
|
|
* Specify Model class name
|
|
|
|
|
*
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
function model()
|
|
|
|
|
{
|
|
|
|
|
return 'Webkul\Discount\Contracts\CartRule';
|
|
|
|
|
}
|
2019-05-23 22:05:53 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* To sync the customer groups related records
|
|
|
|
|
*/
|
|
|
|
|
public function CustomerGroupSync($newCustomerGroups, $cartRule)
|
|
|
|
|
{
|
|
|
|
|
$oldCustomerGroups = array();
|
|
|
|
|
foreach ($cartRule->customer_groups as $oldCustomerGroup) {
|
|
|
|
|
array_push($oldCustomerGroups, ['id' => $oldCustomerGroup->id, 'customer_group_id' => $oldCustomerGroup->customer_group_id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($oldCustomerGroups as $key => $oldCustomerGroup) {
|
|
|
|
|
$found = 0;
|
|
|
|
|
foreach ($newCustomerGroups as $newCustomerGroup) {
|
|
|
|
|
if ($oldCustomerGroup['customer_group_id'] == $newCustomerGroup) {
|
|
|
|
|
$found = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($found == 0) {
|
2019-05-27 11:00:55 +00:00
|
|
|
$this->cartRuleCustomerGroups->find($oldCustomerGroup['id'])->delete();
|
2019-05-23 22:05:53 +00:00
|
|
|
} else {
|
|
|
|
|
$found = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//unset the commons
|
|
|
|
|
if (count($newCustomerGroups) && count($oldCustomerGroups)) {
|
|
|
|
|
foreach ($oldCustomerGroups as $oldCustomerGroup) {
|
|
|
|
|
$found = 0;
|
|
|
|
|
foreach ($newCustomerGroups as $key => $newCustomerGroup) {
|
|
|
|
|
if ($oldCustomerGroup['customer_group_id'] == $newCustomerGroup) {
|
|
|
|
|
unset($newCustomerGroups[$key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//create the left ones
|
|
|
|
|
foreach ($newCustomerGroups as $newCustomerGroup) {
|
|
|
|
|
$data['customer_group_id'] = $newCustomerGroup;
|
2019-05-24 07:39:53 +00:00
|
|
|
$data['cart_rule_id'] = $cartRule->id;
|
2019-05-23 22:05:53 +00:00
|
|
|
|
2019-05-24 07:39:53 +00:00
|
|
|
$this->cartRuleCustomerGroups->create($data);
|
2019-05-23 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* To sync the channels related records
|
|
|
|
|
*/
|
|
|
|
|
public function ChannelSync($newChannels, $cartRule)
|
|
|
|
|
{
|
|
|
|
|
$oldChannels = array();
|
|
|
|
|
foreach ($cartRule->channels as $oldChannel) {
|
|
|
|
|
array_push($oldChannels, ['id' => $oldChannel->id, 'channel_id' => $oldChannel->channel_id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($oldChannels as $key => $oldChannel) {
|
|
|
|
|
$found = 0;
|
|
|
|
|
foreach ($newChannels as $newChannel) {
|
|
|
|
|
if ($oldChannel['channel_id'] == $newChannel) {
|
|
|
|
|
$found = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($found == 0) {
|
2019-05-27 11:00:55 +00:00
|
|
|
$this->cartRuleChannels->find($oldChannel['id'])->delete();
|
2019-05-23 22:05:53 +00:00
|
|
|
} else {
|
|
|
|
|
$found = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//unset the commons
|
|
|
|
|
if (count($newChannels) && count($oldChannels)) {
|
|
|
|
|
foreach ($oldChannels as $oldChannel) {
|
|
|
|
|
$found = 0;
|
|
|
|
|
foreach ($newChannels as $key => $newChannel) {
|
|
|
|
|
if ($oldChannel['channel_id'] == $newChannel) {
|
|
|
|
|
unset($newChannels[$key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//create the left ones
|
|
|
|
|
foreach ($newChannels as $newChannel) {
|
|
|
|
|
$data['channel_id'] = $newChannel;
|
2019-05-24 07:39:53 +00:00
|
|
|
$data['cart_rule_id'] = $cartRule->id;
|
|
|
|
|
$this->cartRuleChannels->create($data);
|
2019-05-23 22:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-05-26 22:29:37 +00:00
|
|
|
|
|
|
|
|
public function LabelsSync($labels, $cartRule)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2019-05-23 22:05:53 +00:00
|
|
|
}
|