From 8cd04be89c3687c5fc9296299c0a7c9cf95f41ab Mon Sep 17 00:00:00 2001 From: rahul shukla Date: Fri, 14 Dec 2018 11:56:52 +0530 Subject: [PATCH] shipping & payment methods --- .../Controllers/ConfigurationController.php | 2 +- packages/Webkul/Admin/src/Http/routes.php | 16 +- .../views/configuration/field-type.blade.php | 26 ++- .../views/configuration/index.blade.php | 6 +- .../sales/field-types/select.blade.php | 20 -- .../sales/field-types/text.blade.php | 8 - .../sales/field-types/textarea.blade.php | 1 - .../sales/shipping-method.blade.php | 141 -------------- .../views/layouts/nav-aside.blade.php | 4 +- .../Resources/views/layouts/tabs.blade.php | 6 +- packages/Webkul/Core/src/Core.php | 70 ++++++- .../src/Repositories/CoreConfigRepository.php | 96 ++++++++++ packages/Webkul/Payment/src/Config/system.php | 172 ++++++++++++++++++ .../src/Providers/PaymentServiceProvider.php | 21 ++- 14 files changed, 385 insertions(+), 204 deletions(-) delete mode 100644 packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/select.blade.php delete mode 100644 packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/text.blade.php delete mode 100644 packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/textarea.blade.php delete mode 100644 packages/Webkul/Admin/src/Resources/views/configuration/sales/shipping-method.blade.php create mode 100644 packages/Webkul/Payment/src/Config/system.php diff --git a/packages/Webkul/Admin/src/Http/Controllers/ConfigurationController.php b/packages/Webkul/Admin/src/Http/Controllers/ConfigurationController.php index 28d291850..81d791c55 100644 --- a/packages/Webkul/Admin/src/Http/Controllers/ConfigurationController.php +++ b/packages/Webkul/Admin/src/Http/Controllers/ConfigurationController.php @@ -64,7 +64,7 @@ class ConfigurationController extends Controller */ public function store() { - $data = request()->all(); + $this->coreConfig->create(request()->all()); session()->flash('success', 'Shipping Method is created successfully'); diff --git a/packages/Webkul/Admin/src/Http/routes.php b/packages/Webkul/Admin/src/Http/routes.php index 4db6666b8..a878346b3 100644 --- a/packages/Webkul/Admin/src/Http/routes.php +++ b/packages/Webkul/Admin/src/Http/routes.php @@ -73,15 +73,19 @@ Route::group(['middleware' => ['web']], function () { 'view' => 'admin::configuration.index' ])->name('admin.configuration.index'); + Route::post('configuration/{slug?}/{slug2?}', 'Webkul\Admin\Http\Controllers\ConfigurationController@store')->defaults('_config', [ + 'redirect' => 'admin.configuration.index' + ])->name('admin.configuration.index.store'); + //Shipping Methods Routes - Route::get('configuration/sales/shipping-methods', 'Webkul\Admin\Http\Controllers\ConfigurationController@index')->defaults('_config', [ - 'view' => 'admin::configuration.sales.shipping-method' - ])->name('admin.configuration.sales.shipping_methods'); + // Route::get('configuration/sales/shipping-methods', 'Webkul\Admin\Http\Controllers\ConfigurationController@index')->defaults('_config', [ + // 'view' => 'admin::configuration.sales.shipping-method' + // ])->name('admin.configuration.sales.shipping_methods'); - Route::post('configuration/sales/shipping-methods', 'Webkul\Admin\Http\Controllers\ConfigurationController@store')->defaults('_config', [ - 'redirect' => 'admin.configuration.sales.shipping_methods' - ])->name('admin.configuration.sales.shipping_methods.store'); + // Route::post('configuration/sales/shipping-methods', 'Webkul\Admin\Http\Controllers\ConfigurationController@store')->defaults('_config', [ + // 'redirect' => 'admin.configuration.sales.shipping_methods' + // ])->name('admin.configuration.sales.shipping_methods.store'); // Reviews Routes diff --git a/packages/Webkul/Admin/src/Resources/views/configuration/field-type.blade.php b/packages/Webkul/Admin/src/Resources/views/configuration/field-type.blade.php index 76b17d35a..ba86a3c91 100644 --- a/packages/Webkul/Admin/src/Resources/views/configuration/field-type.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/configuration/field-type.blade.php @@ -8,9 +8,19 @@ $validations = implode('|', array_filter($validations)); - $name = $field['title']; + $data = config('carriers.flatrate.title'); + + $key = $item['key']; + $key = explode(".", $key); + array_shift($key); + $firstField = current($key); + $secondField = next($key); + $key = implode(".", $key); + + $name = $key . '.' . $field['name']; ?> -
+ +
- getConfigData($name, current($channel_locale), next($channel_locale)); - ?> - @if ($field['type'] == 'text') - + @elseif ($field['type'] == 'textarea') - + @elseif ($field['type'] == 'select') - @foreach($field['options'] as $option) @@ -71,5 +77,5 @@ @endif - @{{ errors.first('{!! $name !!}') }} + @{{ errors.first('{!! $name !!}') }}
\ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/configuration/index.blade.php b/packages/Webkul/Admin/src/Resources/views/configuration/index.blade.php index d9c1bd0e4..ea36e80ec 100644 --- a/packages/Webkul/Admin/src/Resources/views/configuration/index.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/configuration/index.blade.php @@ -55,7 +55,7 @@ @csrf() @if ($groups = array_get($config->items, request()->route('slug') . '.children.' . request()->route('slug2') . '.children')) - + @foreach ($groups as $key => $item) @@ -64,7 +64,7 @@ @foreach ($item['fields'] as $field) @include ('admin::configuration.field-type', ['field' => $field]) - + @endforeach
@@ -88,7 +88,7 @@ $('#channel-switcher').val() var query = '?channel=' + $('#channel-switcher').val() + '&locale=' + $('#locale-switcher').val(); - window.location.href = "{{ route('admin.configuration.sales.shipping_methods') }}" + query; + window.location.href = "{{ route('admin.configuration.index', [request()->route('slug'), request()->route('slug2')]) }}" + query; }) }); diff --git a/packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/select.blade.php b/packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/select.blade.php deleted file mode 100644 index 7ff78eeb2..000000000 --- a/packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/select.blade.php +++ /dev/null @@ -1,20 +0,0 @@ - diff --git a/packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/text.blade.php b/packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/text.blade.php deleted file mode 100644 index 5a68cef0e..000000000 --- a/packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/text.blade.php +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/textarea.blade.php b/packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/textarea.blade.php deleted file mode 100644 index 1f7584149..000000000 --- a/packages/Webkul/Admin/src/Resources/views/configuration/sales/field-types/textarea.blade.php +++ /dev/null @@ -1 +0,0 @@ - diff --git a/packages/Webkul/Admin/src/Resources/views/configuration/sales/shipping-method.blade.php b/packages/Webkul/Admin/src/Resources/views/configuration/sales/shipping-method.blade.php deleted file mode 100644 index 96990aeca..000000000 --- a/packages/Webkul/Admin/src/Resources/views/configuration/sales/shipping-method.blade.php +++ /dev/null @@ -1,141 +0,0 @@ -@extends('admin::layouts.content') - -@section('page_title') - {{ __('admin::app.configuration.sales.shipping-method.title') }} -@stop - -@section('content') -
- get('locale') ?: app()->getLocale(); ?> - get('channel') ?: core()->getDefaultChannelCode(); ?> - -
- - - -
-
- @csrf() - - @foreach (config('core.carriers') as $method => $carrier) - - -
- @foreach ($carrier as $field) - - - - @if (view()->exists($typeView = 'admin::configuration.sales.field-types.' . $field['type'])) - - - -
- - - getConfigData($name, current($channel_locale), next($channel_locale)); - ?> - - @include ($typeView) - - @{{ errors.first('{!! $name !!}') }} - -
- - @endif - @endforeach -
-
- - @endforeach - -
-
- -
-
-@stop - -@push('scripts') - -@endpush \ No newline at end of file diff --git a/packages/Webkul/Admin/src/Resources/views/layouts/nav-aside.blade.php b/packages/Webkul/Admin/src/Resources/views/layouts/nav-aside.blade.php index 0c91c86df..ffe657d4e 100644 --- a/packages/Webkul/Admin/src/Resources/views/layouts/nav-aside.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/layouts/nav-aside.blade.php @@ -15,11 +15,11 @@ @endforeach - + @else @foreach($config->items as $key => $item) - +
  • {{ isset($item['name']) ? $item['name'] : '' }} diff --git a/packages/Webkul/Admin/src/Resources/views/layouts/tabs.blade.php b/packages/Webkul/Admin/src/Resources/views/layouts/tabs.blade.php index 171e1ee84..f57ab829f 100644 --- a/packages/Webkul/Admin/src/Resources/views/layouts/tabs.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/layouts/tabs.blade.php @@ -4,7 +4,7 @@ currentKey); ?> - + @foreach(array_get($menu->items, implode('.children.', array_slice($keys, 0, 2)) . '.children') as $item)
  • @@ -12,9 +12,9 @@
  • @endforeach - + @else - + @if (array_get($config->items, request()->route('slug') . '.children')) @foreach (array_get($config->items, request()->route('slug') . '.children') as $key => $item) diff --git a/packages/Webkul/Core/src/Core.php b/packages/Webkul/Core/src/Core.php index 99dbcf257..f4c3ee1cb 100644 --- a/packages/Webkul/Core/src/Core.php +++ b/packages/Webkul/Core/src/Core.php @@ -490,9 +490,45 @@ class Core $locale = request()->get('locale') ?: app()->getLocale(); } - $coreConfigValue = $this->coreConfigRepository->findOneWhere([ - 'code' => $field - ]); + $fields = $this->getConfigField($field); + + $channel_based = false; + $locale_based = false; + + if (isset($fields['channel_based']) && $fields['channel_based']) { + $channel_based = true; + } + + if (isset($fields['locale_based']) && $fields['locale_based']) { + $locale_based = true; + } + + if (isset($fields['channel_based']) && $fields['channel_based']) { + if (isset($fields['locale_based']) && $fields['locale_based']) { + $coreConfigValue = $this->coreConfigRepository->findOneWhere([ + 'code' => $field, + 'channel_code' => $channel, + 'locale_code' => $locale + ]); + } else { + $coreConfigValue = $this->coreConfigRepository->findOneWhere([ + 'code' => $field, + 'channel_code' => $channel, + ]); + } + } else { + if (isset($fields['locale_based']) && $fields['locale_based']) { + $coreConfigValue = $this->coreConfigRepository->findOneWhere([ + 'code' => $field, + 'locale_code' => $locale + ]); + } + else { + $coreConfigValue = $this->coreConfigRepository->findOneWhere([ + 'code' => $field, + ]); + } + } if(!$coreConfigValue) return Config::get($field); @@ -608,7 +644,7 @@ class Core return date('Y-m-d', $end); } } - + /** * Method to sort through the acl items and put them in order * @@ -630,7 +666,31 @@ class Core }); return $this->convertToAssociativeArray($items); - } + } + + /** + * @param string $fieldName + * @return array + */ + public function getConfigField($fieldName) { + foreach (config('core') as $coreData) { + if (isset($coreData['fields'])) { + foreach ($coreData['fields'] as $field) { + + $key = $coreData['key']; + $key = explode(".", $key); + array_shift($key); + + $key = implode(".", $key); + $name = $key . '.' . $field['name']; + + if ($name == $fieldName ) { + return $field; + } + } + } + } + } public function convertToAssociativeArray($items) { diff --git a/packages/Webkul/Core/src/Repositories/CoreConfigRepository.php b/packages/Webkul/Core/src/Repositories/CoreConfigRepository.php index c5399a55e..fb9de9779 100644 --- a/packages/Webkul/Core/src/Repositories/CoreConfigRepository.php +++ b/packages/Webkul/Core/src/Repositories/CoreConfigRepository.php @@ -21,4 +21,100 @@ class CoreConfigRepository extends Repository { return 'Webkul\Core\Models\CoreConfig'; } + + /** + * @param array $data + * @return mixed + */ + public function create(array $data) + { + unset($data['_token']); + + if ($data['locale'] || $data['channel']) + { + $locale = $data['locale']; + $channel = $data['channel']; + unset($data['locale']); + unset($data['channel']); + } + + foreach ($data as $method => $value) + { + foreach ($value as $key => $formData) + { + foreach (array_keys($formData) as $title) + { + $fieldName = $method . '.' . $key . '.' .$title; + $value = $formData[$title]; + $field = core()->getConfigField($fieldName); + + $channel_based = false; + $locale_based = false; + + if (isset($field['channel_based']) && $field['channel_based']) { + $channel_based = true; + } + + if (isset($field['locale_based']) && $field['locale_based']) { + $locale_based = true; + } + + if (isset($field['channel_based']) && $field['channel_based']) + { + if (isset($field['locale_based']) && $field['locale_based']) + { + $coreConfigValue = $this->model + ->where('code', $fieldName) + ->where('locale_code', $locale) + ->where('channel_code', $channel) + ->get(); + } + else + { + $coreConfigValue = $this->model + ->where('code', $fieldName) + ->where('channel_code', $channel) + ->get(); + } + } else + { + if (isset($field['locale_based']) && $field['locale_based']) + { + $coreConfigValue = $this->model + ->where('code', $fieldName) + ->where('locale_code', $locale) + ->get(); + } + else + { + $coreConfigValue = $this->model + ->where('code', $fieldName) + ->get(); + } + } + + if (!count($coreConfigValue) > 0) + { + $this->model->create([ + 'code' => $fieldName, + 'value' => $value, + 'locale_code' => $locale_based ? $locale : null, + 'channel_code' => $channel_based ? $channel : null + ]); + } else + { + $updataData['code'] = $fieldName; + $updataData['value'] = $value; + $updataData['locale_code'] = $locale_based ? $locale : null; + $updataData['channel_code'] = $channel_based ? $channel : null; + + foreach ($coreConfigValue as $coreConfig) + { + $coreConfig->update($updataData); + } + } + } + } + } + } } \ No newline at end of file diff --git a/packages/Webkul/Payment/src/Config/system.php b/packages/Webkul/Payment/src/Config/system.php new file mode 100644 index 000000000..5556ba97d --- /dev/null +++ b/packages/Webkul/Payment/src/Config/system.php @@ -0,0 +1,172 @@ + 'sales', + 'name' => 'Sales', + 'sort' => 1 + ], [ + 'key' => 'sales.paymentmethods', + 'name' => 'Payment Methods', + 'sort' => 2, + ], [ + 'key' => 'sales.paymentmethods.cashondelivery', + 'name' => 'Cash On Delivery', + 'sort' => 1, + 'fields' => [ + [ + 'name' => 'title', + 'title' => 'Title', + 'type' => 'text', + 'validation' => 'required', + 'channel_based' => false, + 'locale_based' => true + ], [ + 'name' => 'description', + 'title' => 'Description', + 'type' => 'textarea', + 'channel_based' => false, + 'locale_based' => true + ], [ + 'name' => 'order_status', + 'title' => 'Order Status', + 'type' => 'select', + 'options' => [ + [ + 'title' => 'Pending', + 'value' => 'pending' + ], [ + 'title' => 'Approved', + 'value' => 'Approved' + ], [ + 'title' => 'Pending Payment', + 'value' => 'pending_payment' + ] + ], + 'validation' => 'required' + ], [ + 'name' => 'active', + 'title' => 'Status', + 'type' => 'select', + 'options' => [ + [ + 'title' => 'Active', + 'value' => true + ], [ + 'title' => 'Inactive', + 'value' => false + ] + ], + 'validation' => 'required' + ] + ] + ], [ + 'key' => 'sales.paymentmethods.moneytransfer', + 'name' => 'Money Transfer', + 'sort' => 2, + 'fields' => [ + [ + 'name' => 'title', + 'title' => 'Title', + 'type' => 'text', + 'validation' => 'required', + 'channel_based' => false, + 'locale_based' => true + ], [ + 'name' => 'description', + 'title' => 'Description', + 'type' => 'textarea', + 'channel_based' => false, + 'locale_based' => true + ], [ + 'name' => 'order_status', + 'title' => 'Order Status', + 'type' => 'select', + 'options' => [ + [ + 'title' => 'Pending', + 'value' => 'pending' + ], [ + 'title' => 'Approved', + 'value' => 'Approved' + ], [ + 'title' => 'Pending Payment', + 'value' => 'pending_payment' + ] + ], + 'validation' => 'required' + ], [ + 'name' => 'active', + 'title' => 'Status', + 'type' => 'select', + 'options' => [ + [ + 'title' => 'Active', + 'value' => true + ], [ + 'title' => 'Inactive', + 'value' => false + ] + ], + 'validation' => 'required' + ] + ] + ], [ + 'key' => 'sales.paymentmethods.paypal_standard', + 'name' => 'Paypal Standard', + 'sort' => 3, + 'fields' => [ + [ + 'name' => 'title', + 'title' => 'Title', + 'type' => 'text', + 'validation' => 'required', + 'channel_based' => false, + 'locale_based' => true + ], [ + 'name' => 'description', + 'title' => 'Description', + 'type' => 'textarea', + 'channel_based' => false, + 'locale_based' => true + ], [ + 'name' => 'order_status', + 'title' => 'Order Status', + 'type' => 'select', + 'options' => [ + [ + 'title' => 'Pending', + 'value' => 'pending' + ], [ + 'title' => 'Approved', + 'value' => 'Approved' + ], [ + 'title' => 'Pending Payment', + 'value' => 'pending_payment' + ] + ], + 'validation' => 'required' + ], [ + 'name' => 'active', + 'title' => 'Status', + 'type' => 'select', + 'options' => [ + [ + 'title' => 'Active', + 'value' => true + ], [ + 'title' => 'Inactive', + 'value' => false + ] + ], + 'validation' => 'required' + ], [ + 'name' => 'business_account', + 'title' => 'Business Account', + 'type' => 'select', + 'type' => 'text', + 'validation' => 'required' + ] + ] + ] +]; \ No newline at end of file diff --git a/packages/Webkul/Payment/src/Providers/PaymentServiceProvider.php b/packages/Webkul/Payment/src/Providers/PaymentServiceProvider.php index 738ecbe0d..1ace50e2a 100644 --- a/packages/Webkul/Payment/src/Providers/PaymentServiceProvider.php +++ b/packages/Webkul/Payment/src/Providers/PaymentServiceProvider.php @@ -19,7 +19,7 @@ class PaymentServiceProvider extends ServiceProvider { include __DIR__ . '/../Http/helpers.php'; } - + /** * Register services. * @@ -29,9 +29,7 @@ class PaymentServiceProvider extends ServiceProvider { $this->registerFacades(); - $this->mergeConfigFrom( - dirname(__DIR__) . '/Config/paymentmethods.php', 'paymentmethods' - ); + $this->registerConfig(); } /** * Register Bouncer as a singleton. @@ -47,4 +45,19 @@ class PaymentServiceProvider extends ServiceProvider return new Payment(); }); } + /** + * Register package config. + * + * @return void + */ + protected function registerConfig() + { + $this->mergeConfigFrom( + dirname(__DIR__) . '/Config/paymentmethods.php', 'paymentmethods' + ); + + $this->mergeConfigFrom( + dirname(__DIR__) . '/Config/system.php', 'core' + ); + } }