Skip to content

Commit

Permalink
Merge branch 'multiple-prices' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-kuendig committed May 14, 2018
2 parents ba10b76 + 1d9e230 commit 36f6594
Show file tree
Hide file tree
Showing 39 changed files with 1,930 additions and 990 deletions.
56 changes: 56 additions & 0 deletions Plugin.php
Expand Up @@ -2,6 +2,7 @@


use App;
use Backend\Widgets\Form;
use Event;
use Hashids\Hashids;
use OFFLINE\Mall\Classes\Customer\AuthManager;
Expand Down Expand Up @@ -34,6 +35,7 @@
use OFFLINE\Mall\FormWidgets\PropertyFields;
use OFFLINE\Mall\Models\Category;
use OFFLINE\Mall\Models\CurrencySettings;
use OFFLINE\Mall\Models\CustomerGroup;
use OFFLINE\Mall\Models\GeneralSettings;
use OFFLINE\Mall\Models\PaymentGatewaySettings;
use OFFLINE\Mall\Models\Tax;
Expand Down Expand Up @@ -208,5 +210,59 @@ protected function extendPlugins()
'otherKey' => 'tax_id',
];
});

$this->extendRainLabUser();
}

protected function extendRainLabUser()
{
// Add customer_group Relation
\RainLab\User\Models\User::extend(function ($model) {
$model->belongsTo = [
'customer_group' => [CustomerGroup::class, 'key' => 'offline_mall_customer_group_id'],
];
});

// Add Customer Groups menu entry to RainLab.User
Event::listen('backend.menu.extendItems', function ($manager) {
$manager->addSideMenuItems('RainLab.User', 'user', [
'users' => [
'label' => 'rainlab.user::lang.users.menu_label',
'url' => \Backend::url('rainlab/user/users'),
'icon' => 'icon-user',
'permissions' => ['rainlab.users.*'],
],
]);

$manager->addSideMenuItems('RainLab.User', 'user', [
'customer_groups' => [
'label' => 'offline.mall::lang.common.customer_groups',
'url' => \Backend::url('offline/mall/customergroups'),
'icon' => 'icon-users',
'permissions' => ['rainlab.users.*', 'offline.mall.manage_customer_groups'],
],
]);
});

// Add Customer Groups relation to RainLab.User form
Event::listen('backend.form.extendFields', function (Form $widget) {
if ( ! $widget->getController() instanceof \RainLab\User\Controllers\Users) {
return;
}

if ( ! $widget->model instanceof \RainLab\User\Models\User) {
return;
}

$widget->addTabFields([
'customer_group' => [
'label' => trans('offline.mall::lang.common.customer_group'),
'type' => 'relation',
'nameFrom' => 'name',
'emptyOption' => trans('offline.mall::lang.common.none'),
'tab' => 'rainlab.user::lang.user.account',
],
]);
});
}
}
20 changes: 14 additions & 6 deletions assets/backend.css
Expand Up @@ -75,11 +75,19 @@ table.table.data tbody tr.orderfooter-taxes td {
text-shadow: 1px 1px 0 rgba(0, 0, 0, .1)
}

.fancy-layout .relation-behavior .form-tabless-fields {
background: transparent;
padding: 0;
.modal-body--no-padding {
padding-left: 0;
padding-right: 0;
}

/** Highlights the product row **/
.price-table-modal table.data tr:first-child td {
border-bottom-color: #ccc;
}
.fancy-layout .relation-behavior .form-tabless-fields label {
color: inherit;
text-transform: none;
.price-table-modal table.data td.disabled {
color: #ccc;
}

.price-table-currency-selector .btn-link {
padding-left: 0;
}
24 changes: 24 additions & 0 deletions classes/seeders/CustomerGroupTableSeeder.php
@@ -0,0 +1,24 @@
<?php

namespace OFFLINE\Mall\Classes\Seeders;

use October\Rain\Database\Updates\Seeder;
use OFFLINE\Mall\Models\CustomerGroup;

class CustomerGroupTableSeeder extends Seeder
{
public function run()
{
if (app()->environment() === 'testing') {
$group = new CustomerGroup();
$group->name = 'Gold Partners';
$group->code = 'gold';
$group->save();

$group = new CustomerGroup();
$group->name = 'Silver Partners';
$group->code = 'silver';
$group->save();
}
}
}
71 changes: 40 additions & 31 deletions classes/seeders/CustomerTableSeeder.php
Expand Up @@ -5,6 +5,7 @@
use October\Rain\Database\Updates\Seeder;
use OFFLINE\Mall\Models\Address;
use OFFLINE\Mall\Models\Customer;
use OFFLINE\Mall\Models\CustomerGroup;
use RainLab\Location\Models\Country;
use RainLab\Location\Models\State;
use RainLab\User\Models\User;
Expand All @@ -14,37 +15,45 @@ class CustomerTableSeeder extends Seeder
public function run()
{
if (app()->environment() === 'testing') {
$user = new User();
$user->email = 'test@test.com';
$user->password = 'abcd';
$user->password_confirmation = 'abcd';
$user->is_activated = true;
$user->save();

$customer = new Customer();
$customer->name = 'Floaty McFloatface';
$customer->user_id = $user->id;
$customer->save();

$shippingAddress = new Address();
$shippingAddress->name = 'Float McFloatface';
$shippingAddress->lines = 'Street 12';
$shippingAddress->zip = '6000';
$shippingAddress->city = 'Lucerne';
$shippingAddress->state_id = State::where('name', 'Luzern')->first()->id;
$shippingAddress->country_id = Country::where('code', 'CH')->first()->id;

$customer->addresses()->save($shippingAddress);

$billingAddress = new Address();
$billingAddress->name = 'Float McFloatface';
$billingAddress->lines = 'Billing 12';
$billingAddress->zip = '6000';
$billingAddress->city = 'Lucerne';
$billingAddress->state_id = State::where('name', 'Luzern')->first()->id;
$billingAddress->country_id = Country::where('code', 'CH')->first()->id;

$customer->addresses()->save($billingAddress);
$this->createUser('test@test.com', CustomerGroup::first()->id);
$this->createUser('test2@test2.com');
}
}

protected function createUser($email, $customerGroupId = null)
{

$user = new User();
$user->email = $email;
$user->password = 'abcd';
$user->password_confirmation = 'abcd';
$user->is_activated = true;
$user->offline_mall_customer_group_id = $customerGroupId;
$user->save();

$customer = new Customer();
$customer->name = 'Floaty McFloatface';
$customer->user_id = $user->id;
$customer->save();

$shippingAddress = new Address();
$shippingAddress->name = 'Float McFloatface';
$shippingAddress->lines = 'Street 12';
$shippingAddress->zip = '6000';
$shippingAddress->city = 'Lucerne';
$shippingAddress->state_id = State::where('name', 'Luzern')->first()->id;
$shippingAddress->country_id = Country::where('code', 'CH')->first()->id;

$customer->addresses()->save($shippingAddress);

$billingAddress = new Address();
$billingAddress->name = 'Float McFloatface';
$billingAddress->lines = 'Billing 12';
$billingAddress->zip = '6000';
$billingAddress->city = 'Lucerne';
$billingAddress->state_id = State::where('name', 'Luzern')->first()->id;
$billingAddress->country_id = Country::where('code', 'CH')->first()->id;

$customer->addresses()->save($billingAddress);
}
}
14 changes: 12 additions & 2 deletions classes/traits/Price.php
Expand Up @@ -54,7 +54,11 @@ public function getAttribute($attribute)
$attribute = str_replace('_formatted', '', $attribute);
}

$value = parent::getAttribute($attribute);
if ($attribute === 'price' && method_exists($this, 'getUserSpecificPrice')) {
$value = $this->getUserSpecificPrice();
} else {
$value = parent::getAttribute($attribute);
}

// If the model already implements an accessor we don't mess with the attribute.
if (method_exists($this, sprintf('get%sAttribute', studly_case($attribute)))) {
Expand All @@ -66,7 +70,7 @@ public function getAttribute($attribute)
}

if (is_array($value)) {
$value = array_filter($this->fillMissingCurrencies($value), function($item) {
$value = array_filter($this->fillMissingCurrencies($value), function ($item) {
return $item !== null;
});
}
Expand Down Expand Up @@ -180,6 +184,12 @@ protected function useCurrency()
*/
protected function fillMissingCurrencies($value): array
{
// We are in the backend editing the price information. In this case
// we actually want missing currencies to be displayed as null values.
if (session()->get('mall.variants.disable-inheritance')) {
return $value;
}

$basePrice = $value[$this->baseCurrency['code']] ?? null;

return collect($value)->map(function ($price, $currency) use ($basePrice) {
Expand Down
29 changes: 29 additions & 0 deletions classes/traits/UserSpecificPrice.php
@@ -0,0 +1,29 @@
<?php

namespace OFFLINE\Mall\Classes\Traits;

use RainLab\User\Facades\Auth;

trait UserSpecificPrice
{
public function getUserSpecificPrice()
{
if ( ! $this->hasUserSpecificPrice()) {
return parent::getAttribute('price');
}

$group = optional(Auth::getUser())->offline_mall_customer_group_id;
$price = $this->customer_group_prices->where('customer_group_id', $group)->first()->getOriginal('price');

return $price ? json_decode($price, true) : parent::getAttribute('price');
}

protected function hasUserSpecificPrice(): bool
{
return ! app()->runningInBackend()
&& app()->has('user.auth')
&& optional(Auth::getUser())->offline_mall_customer_group_id !== null
&& $this->customer_group_prices->count() > 0;
}

}
33 changes: 33 additions & 0 deletions controllers/CustomerGroups.php
@@ -0,0 +1,33 @@
<?php namespace OFFLINE\Mall\Controllers;

use Backend\Behaviors\RelationController;
use Backend\Classes\Controller;
use BackendMenu;
use Backend\Behaviors\ListController;
use Backend\Behaviors\FormController;
use Backend\Behaviors\ReorderController;

class CustomerGroups extends Controller
{
public $implement = [
ListController::class,
FormController::class,
ReorderController::class,
RelationController::class,
];

public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $reorderConfig = 'config_reorder.yaml';
public $relationConfig = 'config_relation.yaml';

public $requiredPermissions = [
'offline.mall.manage_customer_groups',
];

public function __construct()
{
parent::__construct();
BackendMenu::setContext('RainLab.User', 'user', 'customer_groups');
}
}

0 comments on commit 36f6594

Please sign in to comment.