catalog rule form complete, now moving towards catalog rule products

This commit is contained in:
Prashant Singh 2019-05-22 17:21:31 +05:30
parent 725b70ea19
commit 57dcce2366
11 changed files with 224 additions and 13 deletions

View File

@ -48,7 +48,7 @@ class CatalogRuleDataGrid extends DataGrid
$this->addColumn([
'index' => 'starts_from',
'label' => trans('admin::app.datagrid.starts_from'),
'label' => trans('admin::app.datagrid.starts-from'),
'type' => 'date',
'searchable' => false,
'sortable' => true,
@ -57,7 +57,7 @@ class CatalogRuleDataGrid extends DataGrid
$this->addColumn([
'index' => 'ends_till',
'label' => trans('admin::app.datagrid.ends_till'),
'label' => trans('admin::app.datagrid.ends-till'),
'type' => 'date',
'searchable' => false,
'sortable' => true,

View File

@ -634,7 +634,7 @@ Route::group(['middleware' => ['web']], function () {
])->name('admin.catalog-rule.edit');
Route::post('/catalog-rule/edit/{id}', 'Webkul\Discount\Http\Controllers\CatalogRuleController@update')->defaults('_config', [
'redirect' => 'admin.catalog-rule.edit'
'redirect' => 'admin.catalog-rule.index'
])->name('admin.catalog-rule.update');
Route::post('fetch/options', 'Webkul\Discount\Http\Controllers\CatalogRuleController@fetchAttributeOptions')->name('admin.catalog-rule.options');

View File

@ -153,7 +153,9 @@ return [
'role' => 'Role',
'sub-total' => 'Sub Total',
'no-of-products' => 'Number of Products',
'attribute-family' => 'Attribute Family'
'attribute-family' => 'Attribute Family',
'starts-from' => 'Starts From',
'ends-till' => 'Ends Till',
],
'account' => [
@ -803,7 +805,9 @@ return [
'status' => [
'success' => 'Success! rule created',
'failed' => 'Error! failed to create rule'
'failed' => 'Error! failed to create rule',
'update-success' => 'Success! rule updated',
'update-failed' => 'Error! cannot update'
],
'catalog' => [

View File

@ -12,8 +12,7 @@
@push('scripts')
<script type="text/x-template" id="catalog-rule-form-template">
<div>
{{ dd($catalog_rule[5]) }}
<form method="POST" action="{{ route('admin.catalog-rule.update') }}" @submit.prevent="onSubmit">
<form method="POST" action="{{ route('admin.catalog-rule.update', $catalog_rule[5]->id) }}" @submit.prevent="onSubmit">
@csrf
<div class="page-header">

View File

@ -26,14 +26,14 @@ class CreateCatalogRulesTable extends Migration
$table->datetime('starts_from')->nullable();
$table->datetime('ends_till')->nullable();
$table->boolean('status')->default(1);
$table->integer('priority')->unsigned();
$table->integer('priority')->unsigned()->default(0.0000);
$table->json('conditions')->nullable();
$table->json('actions')->nullable();
$table->boolean('end_other_rules')->default(1);
$table->integer('sort_order')->unsigned()->default(0);
$table->string('action_type')->nullable();
$table->decimal('dis_amt', 12, 4)->default(0.0000);
$table->decimal('dis_qty', 12, 4)->nullable();
$table->decimal('disc_amount', 12, 4)->default(0.0000);
$table->decimal('disc_percent', 12, 4)->default(0.0000);
$table->timestamps();
});
}

View File

@ -153,13 +153,13 @@ class CatalogRuleController extends Controller
$catalogRule = $this->catalogRule->create($catalog_rule);
foreach($catalog_rule_channels as $catalog_rule_channel) {
$catalog_rule_channels['catalog_rule_id'] = $catalogRule['id'];
$catalog_rule_channels['catalog_rule_id'] = $catalogRule->id;
$catalog_rule_channels['channel_id'] = $catalog_rule_channel;
$catalogRuleChannels = $this->catalogRuleChannels->create($catalog_rule_channels);
}
foreach ($catalog_rule_customer_groups as $catalog_rule_channel) {
$catalog_rule_customer_groups['catalog_rule_id'] = $catalogRule['id'];
$catalog_rule_customer_groups['catalog_rule_id'] = $catalogRule->id;
$catalog_rule_customer_groups['customer_group_id'] = $catalog_rule_channel;
$catalogRuleCustomerGroups = $this->catalogRuleCustomerGroups->create($catalog_rule_customer_groups);
}
@ -184,6 +184,77 @@ class CatalogRuleController extends Controller
return view($this->_config['view'])->with('catalog_rule', [$this->attribute->getPartial(), $this->category->getPartial(), $this->fetchOptionableAttributes(), $this->appliedConfig, $this->appliedConditions, $catalog_rule, $catalog_rule_channels, $catalog_rule_customer_groups]);
}
public function update($id)
{
$this->validate(request(), [
'name' => 'required|string',
'description' => 'string',
'customer_groups' => 'required',
'channels' => 'required',
'starts_from' => 'required|date',
'ends_till' => 'required|date',
'status' => 'required|boolean',
'end_other_rules' => 'required|boolean',
'priority' => 'required|numeric',
'criteria' => 'required',
'all_conditions' => 'required|array',
'apply' => 'required|numeric|min:0|max:3',
'disc_amount' => 'sometimes',
'disc_percent' => 'sometimes',
]);
$catalog_rule = request()->all();
$catalog_rule_channels = array();
$catalog_rule_customer_groups = array();
$catalog_rule_channels = $catalog_rule['channels'];
$catalog_rule_customer_groups = $catalog_rule['customer_groups'];
unset($catalog_rule['channels']); unset($catalog_rule['customer_groups']);
unset($catalog_rule['criteria']);
$catalog_rule['conditions'] = $catalog_rule['all_conditions'];
unset($catalog_rule['all_conditions']);
if (isset($catalog_rule['disc_amount'])) {
$catalog_rule['action_type'] = $catalog_rule['apply'];
$catalog_rule['actions'] = [
'action_type' => $catalog_rule['apply'],
'disc_amount' => $catalog_rule['disc_amount']
];
} else if (isset($catalog_rule['disc_percent'])) {
$catalog_rule['action_type'] = $catalog_rule['apply'];
$catalog_rule['actions'] = [
'action_type' => $catalog_rule['apply'],
'disc_percent' => $catalog_rule['disc_percent'],
];
}
unset($catalog_rule['apply']);
unset($catalog_rule['attributes']);
unset($catalog_rule['_token']);
unset($catalog_rule['all_actions']);
$catalog_rule['actions'] = json_encode($catalog_rule['actions']);
$catalog_rule['conditions'] = json_encode($catalog_rule['conditions']);
$catalogRule = $this->catalogRule->update($catalog_rule, $id);
$catalogRuleChannels = $this->catalogRule->ChannelSync($catalog_rule_channels, $catalogRule);
$catalogRuleCustomerGroups = $this->catalogRule->CustomerGroupSync($catalog_rule_customer_groups, $catalogRule);
if($catalogRule && $catalogRuleChannels && $catalogRuleCustomerGroups) {
session()->flash('info', trans('admin::app.promotion.status.update-success'));
return redirect()->route($this->_config['redirect']);
} else {
session()->flash('error', trans('admin::app.promotion.status.update-failed'));
return redirect()->back();
}
}
public function fetchOptionableAttributes()
{
$attributesWithOptions = array();

View File

@ -4,10 +4,22 @@ namespace Webkul\Discount\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Discount\Contracts\CatalogRule as CatalogRuleContract;
use Webkul\Discount\Models\CatalogRuleChannelsProxy as CatalogRuleChannels;
use Webkul\Discount\Models\CatalogRuleCustomerGroupsProxy as CatalogRuleCustomerGroups;
class CatalogRule extends Model implements CatalogRuleContract
{
protected $table = 'catalog_rules';
protected $guarded = ['created_at', 'updated_at'];
public function channels()
{
return $this->hasMany(CatalogRuleChannels::modelClass());
}
public function customer_groups()
{
return $this->hasMany(CatalogRuleCustomerGroups::modelClass());
}
}

View File

@ -4,10 +4,16 @@ namespace Webkul\Discount\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Discount\Contracts\CatalogRuleChannels as CatalogRuleChannelsContract;
use Webkul\Discount\Models\CatalogRuleProxy as CatalogRule;
class CatalogRuleChannels extends Model implements CatalogRuleChannelsContract
{
protected $table = 'catalog_rule_channels';
protected $guarded = ['created_at', 'updated_at'];
public function catalog_rule()
{
return $this->belongsTo(CatalogRule::modelClass(), 'catalog_rule_id');
}
}

View File

@ -4,10 +4,16 @@ namespace Webkul\Discount\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Discount\Contracts\CatalogRuleCustomerGroups as CatalogRuleCustomerGroupsContract;
use Webkul\Discount\Models\CatalogRuleProxy as CatalogRule;
class CatalogRuleCustomerGroups extends Model implements CatalogRuleCustomerGroupsContract
{
protected $table = 'catalog_rule_customer_groups';
protected $guarded = ['created_at', 'updated_at'];
public function catalog_rule()
{
return $this->belongsTo(CatalogRule::modelClass(), 'catalog_rule_id');
}
}

View File

@ -10,7 +10,7 @@ use Webkul\Core\Eloquent\Repository;
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartRuleRepository extends Repository
class CartRuleChannelsRepository extends Repository
{
/**
* Specify Model class name

View File

@ -3,6 +3,9 @@
namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
use Webkul\Discount\Repositories\CatalogRuleChannelsRepository as CatalogRuleChannels;
use Webkul\Discount\Repositories\CatalogRuleCustomerGroupsRepository as CatalogRuleCustomerGroups;
use Illuminate\Container\Container as App;
/**
* Catalog Rule Reposotory
@ -12,6 +15,21 @@ use Webkul\Core\Eloquent\Repository;
*/
class CatalogRuleRepository extends Repository
{
protected $catalogRuleChannels;
protected $catalogRuleCustomerGroups;
/**
*
*/
public function __construct(CatalogRuleChannels $catalogRuleChannels, CatalogRuleCustomerGroups $catalogRuleCustomerGroups, App $app)
{
$this->catalogRuleChannels = $catalogRuleChannels;
$this->catalogRuleCustomerGroups = $catalogRuleCustomerGroups;
parent::__construct($app);
}
/**
* Specify Model class name
*
@ -21,4 +39,99 @@ class CatalogRuleRepository extends Repository
{
return 'Webkul\Discount\Contracts\CatalogRule';
}
/**
* To sync the customer groups related records
*/
public function CustomerGroupSync($newCustomerGroups, $catalogRule)
{
$oldCustomerGroups = array();
foreach ($catalogRule->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) {
$this->catalogRuleCustomerGroups->find($oldCustomerGroup['id'])->delete();
} 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;
$data['catalog_rule_id'] = $catalogRule->id;
$this->catalogRuleCustomerGroups->create($data);
}
return true;
}
/**
* To sync the channels related records
*/
public function ChannelSync($newChannels, $catalogRule)
{
$oldChannels = array();
foreach ($catalogRule->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) {
$this->catalogRuleChannels->find($oldChannel['id'])->delete();
} 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;
$data['catalog_rule_id'] = $catalogRule->id;
$this->catalogRuleChannels->create($data);
}
return true;
}
}