Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promotion Configuration #20

Merged
merged 8 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Added the Promotions & Coupons CRUD
- Moved the Orders menu item to the top in the Shop section
- Added input validation at Property Value assignment
- Added the Promotion Rules/Actions CRUD

## 4.1.1
##### 2024-07-23
Expand Down
11 changes: 11 additions & 0 deletions src/Contracts/Requests/CreatePromotionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Contracts\Requests;

use Konekt\Concord\Contracts\BaseRequest;

interface CreatePromotionAction extends BaseRequest
{
}
11 changes: 11 additions & 0 deletions src/Contracts/Requests/CreatePromotionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Contracts\Requests;

use Konekt\Concord\Contracts\BaseRequest;

interface CreatePromotionRule extends BaseRequest
{
}
11 changes: 11 additions & 0 deletions src/Contracts/Requests/UpdatePromotionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Contracts\Requests;

use Konekt\Concord\Contracts\BaseRequest;

interface UpdatePromotionAction extends BaseRequest
{
}
11 changes: 11 additions & 0 deletions src/Contracts/Requests/UpdatePromotionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Contracts\Requests;

use Konekt\Concord\Contracts\BaseRequest;

interface UpdatePromotionRule extends BaseRequest
{
}
87 changes: 87 additions & 0 deletions src/Http/Controllers/PromotionActionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Http\Controllers;

use Konekt\AppShell\Http\Controllers\BaseController;
use Vanilo\Admin\Contracts\Requests\CreatePromotionAction;
use Vanilo\Admin\Contracts\Requests\UpdatePromotionAction;
use Vanilo\Promotion\Contracts\Promotion;
use Vanilo\Promotion\Contracts\PromotionAction;
use Vanilo\Promotion\Models\PromotionActionProxy;
use Vanilo\Promotion\PromotionActionTypes;

class PromotionActionController extends BaseController
{
public function create(Promotion $promotion)
{
return view('vanilo::promotion-action.create', [
'promotion' => $promotion,
'action' => app(PromotionAction::class),
'types' => PromotionActionTypes::choices(),
]);
}

public function store(Promotion $promotion, CreatePromotionAction $request)
{
try {
$promotionAction = PromotionActionProxy::create(
array_merge(
[
'promotion_id' => $promotion->id,
],
$request->validated(),
)
);

flash()->success(__('Action :title has been created', ['title' => $promotionAction->getTitle()]));
} catch (\Exception $e) {
flash()->error(__('Error: :msg', ['msg' => $e->getMessage()]));

return redirect()->back()->withInput();
}

return redirect(route('vanilo.admin.promotion.show', $promotion));
}

public function edit(Promotion $promotion, PromotionAction $promotionAction)
{
return view('vanilo::promotion-action.edit', [
'promotion' => $promotion,
'action' => $promotionAction,
'types' => PromotionActionTypes::choices(),
]);
}

public function update(Promotion $promotion, PromotionAction $promotionAction, UpdatePromotionAction $request)
{
try {
$promotionAction->update($request->validated());

flash()->success(__('The action :title has been updated', ['title' => $promotionAction->getTitle()]));
} catch (\Exception $e) {
flash()->error(__('Error: :msg', ['msg' => $e->getMessage()]));

return redirect()->back()->withInput();
}

return redirect()->route('vanilo.admin.promotion.show', $promotion);
}

public function destroy(Promotion $promotion, PromotionAction $promotionAction)
{
try {
$title = $promotionAction->getTitle();
$promotionAction->delete();

flash()->warning(__('The action :title has been deleted', ['title' => $title]));
} catch (\Exception $e) {
flash()->error(__('Error: :msg', ['msg' => $e->getMessage()]));

return redirect()->back();
}

return redirect()->route('vanilo.admin.promotion.show', $promotion);
}
}
87 changes: 87 additions & 0 deletions src/Http/Controllers/PromotionRuleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Http\Controllers;

use Konekt\AppShell\Http\Controllers\BaseController;
use Vanilo\Admin\Contracts\Requests\CreatePromotionRule;
use Vanilo\Admin\Contracts\Requests\UpdatePromotionRule;
use Vanilo\Promotion\Contracts\Promotion;
use Vanilo\Promotion\Contracts\PromotionRule;
use Vanilo\Promotion\Models\PromotionRuleProxy;
use Vanilo\Promotion\PromotionRuleTypes;

class PromotionRuleController extends BaseController
{
public function create(Promotion $promotion)
{
return view('vanilo::promotion-rule.create', [
'promotion' => $promotion,
'rule' => app(PromotionRule::class),
'types' => PromotionRuleTypes::choices(),
]);
}

public function store(Promotion $promotion, CreatePromotionRule $request)
{
try {
$promotionRule = PromotionRuleProxy::create(
array_merge(
[
'promotion_id' => $promotion->id,
],
$request->validated(),
)
);

flash()->success(__('Rule :title has been created', ['title' => $promotionRule->getTitle()]));
} catch (\Exception $e) {
flash()->error(__('Error: :msg', ['msg' => $e->getMessage()]));

return redirect()->back()->withInput();
}

return redirect(route('vanilo.admin.promotion.show', $promotion));
}

public function edit(Promotion $promotion, PromotionRule $promotionRule)
{
return view('vanilo::promotion-rule.edit', [
'promotion' => $promotion,
'rule' => $promotionRule,
'types' => PromotionRuleTypes::choices(),
]);
}

public function update(Promotion $promotion, PromotionRule $promotionRule, UpdatePromotionRule $request)
{
try {
$promotionRule->update($request->validated());

flash()->success(__('The rule :title has been updated', ['title' => $promotionRule->getTitle()]));
} catch (\Exception $e) {
flash()->error(__('Error: :msg', ['msg' => $e->getMessage()]));

return redirect()->back()->withInput();
}

return redirect()->route('vanilo.admin.promotion.show', $promotion);
}

public function destroy(Promotion $promotion, PromotionRule $promotionRule)
{
try {
$title = $promotionRule->getTitle();
$promotionRule->delete();

flash()->warning(__('The rule :title has been deleted', ['title' => $title]));
} catch (\Exception $e) {
flash()->error(__('Error: :msg', ['msg' => $e->getMessage()]));

return redirect()->back();
}

return redirect()->route('vanilo.admin.promotion.show', $promotion);
}
}
26 changes: 26 additions & 0 deletions src/Http/Requests/CreatePromotionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Vanilo\Admin\Contracts\Requests\CreatePromotionAction as CreatePromotionActionContract;
use Vanilo\Promotion\PromotionActionTypes;

class CreatePromotionAction extends FormRequest implements CreatePromotionActionContract
{
public function rules()
{
return [
'type' => ['required', Rule::in(PromotionActionTypes::ids())],
'configuration' => 'sometimes|json',
];
}

public function authorize()
{
return true;
}
}
26 changes: 26 additions & 0 deletions src/Http/Requests/CreatePromotionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Vanilo\Admin\Contracts\Requests\CreatePromotionRule as CreatePromotionRuleContract;
use Vanilo\Promotion\PromotionRuleTypes;

class CreatePromotionRule extends FormRequest implements CreatePromotionRuleContract
{
public function rules()
{
return [
'type' => ['required', Rule::in(PromotionRuleTypes::ids())],
'configuration' => 'sometimes|json',
];
}

public function authorize()
{
return true;
}
}
26 changes: 26 additions & 0 deletions src/Http/Requests/UpdatePromotionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Vanilo\Admin\Contracts\Requests\UpdatePromotionAction as UpdatePromotionActionContract;
use Vanilo\Promotion\PromotionActionTypes;

class UpdatePromotionAction extends FormRequest implements UpdatePromotionActionContract
{
public function rules()
{
return [
'type' => ['required', Rule::in(PromotionActionTypes::ids())],
'configuration' => 'sometimes|json',
];
}

public function authorize()
{
return true;
}
}
26 changes: 26 additions & 0 deletions src/Http/Requests/UpdatePromotionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Vanilo\Admin\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Vanilo\Admin\Contracts\Requests\UpdatePromotionRule as UpdatePromotionRuleContract;
use Vanilo\Promotion\PromotionRuleTypes;

class UpdatePromotionRule extends FormRequest implements UpdatePromotionRuleContract
{
public function rules()
{
return [
'type' => ['required', Rule::in(PromotionRuleTypes::ids())],
'configuration' => 'sometimes|json',
];
}

public function authorize()
{
return true;
}
}
10 changes: 10 additions & 0 deletions src/Providers/ModuleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use Vanilo\Admin\Http\Requests\CreatePaymentMethod;
use Vanilo\Admin\Http\Requests\CreateProduct;
use Vanilo\Admin\Http\Requests\CreatePromotion;
use Vanilo\Admin\Http\Requests\CreatePromotionAction;
use Vanilo\Admin\Http\Requests\CreatePromotionRule;
use Vanilo\Admin\Http\Requests\CreateProperty;
use Vanilo\Admin\Http\Requests\CreatePropertyValue;
use Vanilo\Admin\Http\Requests\CreatePropertyValueForm;
Expand All @@ -57,6 +59,8 @@
use Vanilo\Admin\Http\Requests\UpdatePaymentMethod;
use Vanilo\Admin\Http\Requests\UpdateProduct;
use Vanilo\Admin\Http\Requests\UpdatePromotion;
use Vanilo\Admin\Http\Requests\UpdatePromotionAction;
use Vanilo\Admin\Http\Requests\UpdatePromotionRule;
use Vanilo\Admin\Http\Requests\UpdateProperty;
use Vanilo\Admin\Http\Requests\UpdatePropertyValue;
use Vanilo\Admin\Http\Requests\UpdateShippingMethod;
Expand Down Expand Up @@ -120,6 +124,10 @@ class ModuleServiceProvider extends BaseBoxServiceProvider
UpdatePromotion::class,
CreateCoupon::class,
UpdateCoupon::class,
CreatePromotionRule::class,
UpdatePromotionRule::class,
CreatePromotionAction::class,
UpdatePromotionAction::class
];

public function register(): void
Expand All @@ -138,6 +146,8 @@ public function boot()
$aclResourceMapper->overrideResourcePlural('taxon', 'taxons');
$aclResourceMapper->addAlias('master product', 'product');
$aclResourceMapper->addAlias('master product variant', 'product');
$aclResourceMapper->addAlias('promotion rule', 'promotion');
$aclResourceMapper->addAlias('promotion action', 'promotion');
$aclResourceMapper->addAlias('zone member', 'zone');
$aclResourceMapper->addAlias('link', 'product');
$aclResourceMapper->addAlias('payment', 'order');
Expand Down
Loading