-
Notifications
You must be signed in to change notification settings - Fork 18
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
Feature/export orders #111
base: develop
Are you sure you want to change the base?
Changes from 2 commits
cce2a3b
cbac0ab
028f80d
d1e2603
94cb232
608a1c3
cc89d2a
3f1cc45
002ec2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
<?php namespace Lovata\OrdersShopaholic\Classes\Event\Order; | ||
|
||
use DB; | ||
use Lovata\OrdersShopaholic\Models\OrderExport; | ||
use Lovata\Toolbox\Classes\Helper\UserHelper; | ||
|
||
use Lovata\OrdersShopaholic\Controllers\Orders; | ||
|
@@ -18,11 +20,12 @@ public function subscribe() | |
{ | ||
Orders::extend(function($obController) { | ||
$this->extendConfig($obController); | ||
$this->extendExportConfig($obController); | ||
}); | ||
} | ||
|
||
/** | ||
* Extend products controller | ||
* Extend config. | ||
* @param Orders $obController | ||
*/ | ||
protected function extendConfig($obController) | ||
|
@@ -40,4 +43,58 @@ protected function extendConfig($obController) | |
|
||
$obController->relationConfig = $obController->mergeConfig($obController->relationConfig, $sConfigPath); | ||
} | ||
|
||
/** | ||
* Extend export config. | ||
* @param Orders $obController | ||
* @throws \SystemException | ||
*/ | ||
protected function extendExportConfig($obController) | ||
{ | ||
/** @var Orders $obController */ | ||
if (is_array($obController->importExportConfig)) { | ||
$arConfig = $obController->importExportConfig; | ||
} else { | ||
$arConfig = (array)$obController->makeConfig('$/lovata/ordersshopaholic/controllers/orders/'.$obController->importExportConfig); | ||
} | ||
|
||
$arFiledList = (array) array_get($arConfig, 'export.list.columns'); | ||
$arFiledList = array_merge($arFiledList, $this->getExportFieldList()); | ||
|
||
array_set($arConfig, 'export.list.columns', $arFiledList); | ||
$obController->importExportConfig = $arConfig; | ||
} | ||
|
||
/** | ||
* Get export field list. | ||
*/ | ||
protected function getExportFieldList() : array | ||
{ | ||
$arFieldList = [ | ||
OrderExport::RELATION_STATUS => ['label' => 'lovata.toolbox::lang.field.status'], | ||
'order_number' => ['label' => 'lovata.ordersshopaholic::lang.field.order_number'], | ||
OrderExport::RELATION_CURRENCY => ['label' => 'lovata.shopaholic::lang.field.currency'], | ||
'total_price' => ['label' => 'lovata.ordersshopaholic::lang.field.total_price'], | ||
'shipping_price' => ['label' => 'lovata.ordersshopaholic::lang.field.shipping_price'], | ||
'position_total_price' => ['label' => 'lovata.ordersshopaholic::lang.field.position_total_price'], | ||
OrderExport::RELATION_SHIPPING_TYPE => ['label' => 'lovata.ordersshopaholic::lang.field.shipping_type'], | ||
OrderExport::RELATION_PAYMENT_METHOD => ['label' => 'lovata.ordersshopaholic::lang.field.payment_method'], | ||
'created_at' => ['label' => 'lovata.toolbox::lang.field.created_at'], | ||
'updated_at' => ['label' => 'lovata.toolbox::lang.field.updated_at'], | ||
]; | ||
|
||
$arPropertyList = (array) DB::table('lovata_orders_shopaholic_addition_properties') | ||
->where('active', true) | ||
->lists('name', 'code'); | ||
|
||
if (empty($arPropertyList)) { | ||
return $arFieldList; | ||
} | ||
|
||
foreach ($arPropertyList as $sField => $sLabel) { | ||
$arFieldList[$sField] = ['label' => $sLabel]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь возможно можно сделать 'property.'.$sField There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нет. в метод модели будет передоваться просто массив с ключом 'property.field' |
||
} | ||
|
||
return $arFieldList; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,27 @@ | ||
<div data-control="toolbar"> | ||
<a href="<?= Backend::url('lovata/ordersshopaholic/orders/create') ?>" class="btn btn-primary oc-icon-plus"> | ||
<?= e(trans('backend::lang.form.create')) ?> | ||
</a> | ||
<div class="scoreboard"> | ||
<div data-control="toolbar"> | ||
<div class="scoreboard-item title-value"> | ||
<a href="<?= Backend::url('lovata/ordersshopaholic/orders/create') ?>" class="btn btn-primary oc-icon-plus"> | ||
<?= e(trans('backend::lang.form.create')) ?> | ||
</a> | ||
</div> | ||
<span class="analytics-ajax"></span> | ||
<div class="scoreboard-item title-value"> | ||
<div class="dropdown dropdown-fixed"> | ||
<button type="button" class="btn btn-default oc-icon-upload" data-toggle="dropdown"> | ||
<?= e(trans('lovata.toolbox::lang.button.export_in_csv')) ?> | ||
</button> | ||
<ul class="dropdown-menu" data-dropdown-title="<?= e(trans('lovata.toolbox::lang.button.export_in_csv')) ?>"> | ||
<li> | ||
<a href="<?= Backend::url('lovata/ordersshopaholic/orders/export') ?>" | ||
tabindex="-1" | ||
class="oc-icon-upload" | ||
> | ||
<?= e(trans('lovata.ordersshopaholic::lang.button.orders')) ?> | ||
</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export: | ||
title: 'lovata.ordersshopaholic::lang.order.export_title' | ||
modelClass: Lovata\OrdersShopaholic\Models\OrderExport | ||
list: | ||
columns: |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php Block::put('breadcrumb') ?> | ||
<ul> | ||
<li><a href="<?= Backend::url('lovata/shopaholic/products') ?>"><?= e(trans('lovata.shopaholic::lang.product.list_title')) ?></a></li> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Некорректная сслыка |
||
<li><?= e($this->pageTitle) ?></li> | ||
</ul> | ||
<?php Block::endPut() ?> | ||
|
||
<?= Form::open(['class' => 'layout']) ?> | ||
|
||
<div class="layout-row"> | ||
<?= $this->exportRender() ?> | ||
</div> | ||
|
||
<div class="form-buttons"> | ||
<button | ||
type="submit" | ||
data-control="popup" | ||
data-handler="onExportLoadForm" | ||
data-keyboard="false" | ||
class="btn btn-primary" | ||
data-stripe-load-indicator | ||
> | ||
<?= e(trans('lovata.toolbox::lang.button.export_button')) ?> | ||
</button> | ||
</div> | ||
|
||
<?= Form::close() ?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
<?php namespace Lovata\OrdersShopaholic\Models; | ||
|
||
use Backend\Models\ExportModel; | ||
use DB; | ||
|
||
/** | ||
* Class OrderExport | ||
* | ||
* @package Lovata\OrdersShopaholic\Models | ||
* @author Sergey Zakharevich, [email protected], LOVATA Group | ||
* | ||
* @mixin \October\Rain\Database\Builder | ||
* @mixin \Eloquent | ||
* | ||
* @property Status $status | ||
* @method static Status|\October\Rain\Database\Relations\BelongsTo status() | ||
*/ | ||
class OrderExport extends ExportModel | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему нельзя сделать наследование в основном классе? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а если потом появится импорт заказов? |
||
{ | ||
const FIELD_TOTAL_PRICE = 'total_price'; | ||
const FIELD_POSITION_TOTAL_PRICE = 'position_total_price'; | ||
|
||
const RELATION_STATUS = 'status'; | ||
const RELATION_SHIPPING_TYPE = 'shipping_type'; | ||
const RELATION_PAYMENT_METHOD = 'payment_method'; | ||
const RELATION_CURRENCY = 'currency'; | ||
const RELATION_ORDER_POSITION = 'order_position'; | ||
|
||
const RELATION_LIST = [ | ||
self::RELATION_STATUS, | ||
self::RELATION_SHIPPING_TYPE, | ||
self::RELATION_PAYMENT_METHOD, | ||
self::RELATION_CURRENCY, | ||
self::RELATION_ORDER_POSITION, | ||
]; | ||
|
||
/** @var string */ | ||
public $table = 'lovata_orders_shopaholic_orders'; | ||
/** @var array */ | ||
public $belongsTo = ['status' => [Status::class, 'order' => 'sort_order asc']]; | ||
/** @var array */ | ||
protected $arOrderColumnList = []; | ||
/** @var array */ | ||
protected $arRelationColumnList = []; | ||
/** @var array */ | ||
protected $arPropertyColumnList = []; | ||
|
||
/** | ||
* Export data. | ||
* @param array|null $arColumns | ||
* @param string|null $sSessionKey | ||
* @return array | ||
*/ | ||
public function exportData($arColumns, $sSessionKey = null) : array | ||
{ | ||
$arList = []; | ||
|
||
if (empty($arColumns)) { | ||
return $arList; | ||
} | ||
|
||
$this->init($arColumns); | ||
|
||
$obOrderList = Order::with($this->arRelationColumnList)->get(); | ||
|
||
if ($obOrderList->isEmpty()) { | ||
return $arList; | ||
} | ||
|
||
foreach ($obOrderList as $obOrder) { | ||
$arRow = $this->prepareRow($obOrder); | ||
|
||
if (empty($arRow)) { | ||
continue; | ||
} | ||
|
||
$arList[] = $arRow; | ||
} | ||
|
||
return $arList; | ||
} | ||
|
||
/** | ||
* Init. | ||
* @param array|null $arColumns | ||
* @return void | ||
*/ | ||
protected function init($arColumns) | ||
{ | ||
if (empty($arColumns) || !is_array($arColumns)) { | ||
return; | ||
} | ||
|
||
$arPropertyList = (array) DB::table('lovata_orders_shopaholic_addition_properties') | ||
->where('active', true) | ||
->lists('code'); | ||
|
||
foreach ($arColumns as $sColumn) { | ||
// Init relations. | ||
if (in_array($sColumn, self::RELATION_LIST)) { | ||
$this->arRelationColumnList[] = $sColumn; | ||
// Init field. | ||
} elseif (in_array($sColumn, $arPropertyList)) { | ||
$this->arPropertyColumnList[] = $sColumn; | ||
} else { | ||
if ($sColumn == self::FIELD_TOTAL_PRICE || $sColumn == self::FIELD_POSITION_TOTAL_PRICE) { | ||
$this->arRelationColumnList[] = self::RELATION_ORDER_POSITION; | ||
} | ||
$this->arOrderColumnList[] = $sColumn; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Prepare row. | ||
* @param Order $obOrder | ||
* @return array | ||
*/ | ||
protected function prepareRow(Order $obOrder) : array | ||
{ | ||
$arOrderData = $this->prepareOrderData($obOrder); | ||
$arOrderRelationsData = $this->prepareOrderRelationsData($obOrder); | ||
$arOrderPropertiesData = $this->prepareOrderPropertiesData($obOrder); | ||
|
||
return array_merge($arOrderData, $arOrderRelationsData, $arOrderPropertiesData); | ||
} | ||
|
||
/** | ||
* Prepare order data. | ||
* @param Order $obOrder | ||
* @return array | ||
*/ | ||
protected function prepareOrderData(Order $obOrder) : array | ||
{ | ||
$arResult = []; | ||
|
||
if (empty($this->arOrderColumnList)) { | ||
return $arResult; | ||
} | ||
|
||
foreach ($this->arOrderColumnList as $sField) { | ||
$arResult[$sField] = $obOrder->$sField; | ||
} | ||
|
||
return $arResult; | ||
} | ||
|
||
/** | ||
* Prepare order relations data. | ||
* @param Order $obOrder | ||
* @return array | ||
*/ | ||
protected function prepareOrderRelationsData(Order $obOrder) : array | ||
{ | ||
$arResult = []; | ||
|
||
if (empty($this->arRelationColumnList)) { | ||
return $arResult; | ||
} | ||
|
||
if (!empty($obOrder->status) && in_array(self::RELATION_STATUS, $this->arRelationColumnList)) { | ||
$arResult[self::RELATION_STATUS] = $obOrder->status->name; | ||
} | ||
if (!empty($obOrder->shipping_type) | ||
&& in_array(self::RELATION_SHIPPING_TYPE, $this->arRelationColumnList)) { | ||
$arResult[self::RELATION_SHIPPING_TYPE] = $obOrder->shipping_type->name; | ||
} | ||
if (!empty($obOrder->payment_method) | ||
&& in_array(self::RELATION_PAYMENT_METHOD, $this->arRelationColumnList)) { | ||
$arResult[self::RELATION_PAYMENT_METHOD] = $obOrder->payment_method->name; | ||
} | ||
if (!empty($obOrder->currency) && in_array(self::RELATION_CURRENCY, $this->arRelationColumnList)) { | ||
$arResult[self::RELATION_CURRENCY] = $obOrder->currency->symbol; | ||
} | ||
|
||
return $arResult; | ||
} | ||
|
||
/** | ||
* Prepare order data. | ||
* @param Order $obOrder | ||
* @return array | ||
*/ | ||
protected function prepareOrderPropertiesData(Order $obOrder) : array | ||
{ | ||
$arResult = []; | ||
|
||
if (empty($obOrder->property) || empty($this->arPropertyColumnList)) { | ||
return $arResult; | ||
} | ||
|
||
foreach ($this->arPropertyColumnList as $sField) { | ||
$arResult[$sField] = array_get($obOrder->property, $sField); | ||
} | ||
|
||
return $arResult; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему не использовать модель для запроса?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
по примеру с импорта брал