$service = new AutoPlannogrammaService();
$data = $service->getMonthSubcategoryShareOrWriteOff($filters['plan_date'], $filters, $filters['type']);
- $flatData = [];
- //var_dump($data);die();
- foreach ($data as $storeId => $categories) {
- foreach ($categories as $row) {
- $flatData[] = [
- 'store_id' => (string)$storeId,
- 'category' => $row['category'] ?? null,
- 'subcategory' => $row['subcategory'] ?? null,
- 'base_total_store' => $row['base_total_store'] ?? null,
- 'new_total_store' => $row['new_total_store'] ?? null,
- 'total_sum' => $row['total_sum'] ?? null,
- 'percent' => $row['percent'] ?? null,
- ];
- }
- }
- $flatData = array_filter($flatData, function ($row) use ($filters) {
+
+ $flatData = array_filter($data, function ($row) use ($filters) {
foreach ($filters as $key => $value) {
if (empty($value)) continue;
if (!isset($row[$key])) continue;
$data = $service->getMonthSpeciesShareOrWriteOff($filters['plan_date'], $filters, $filters['type']);
- $flatData = [];
-
- foreach ($data as $storeId => $categories) {
- foreach ($categories as $row) {
- $flatData[] = [
- 'store_id' => (string)$storeId,
- 'category' => $row['category'] ?? null,
- 'subcategory' => $row['subcategory'] ?? null,
- 'species' => $row['species'] ?? null,
- 'base_total_store' => $row['base_total_store'] ?? null,
- 'new_total_store' => $row['new_total_store'] ?? null,
- 'total_sum' => $row['total_sum'] ?? null,
- 'percent' => $row['percent'] ?? null,
- ];
- }
- }
-
- $flatData = array_filter($flatData, function ($row) use ($filters) {
+ $flatData = array_filter($data, function ($row) use ($filters) {
foreach ($filters as $key => $value) {
if (empty($value)) continue;
if (!isset($row[$key])) continue;
? round($newSubTotal / $newCatTotal, 4)
: 0.0;
- $result[$sid][] = [
+ $result[] = [
+ 'store_id' => $sid,
'category' => $cat,
'subcategory' => $subcat,
'total_sum' => $newSubTotal,
? round($newSpecTotal / $newSubcatTotal, 4)
: 0.0;
- $finalResult[$sid][] = [
+ $finalResult[] = [
+ 'store_id' => $sid,
'category' => $cat,
'subcategory' => $subcat,
'species' => $spec ,
* component_guid, component_name, component_category,
* quantity, price, cost
*/
- public function getProductsComponentsInCategory(int $storeId, string $month, string $year, string $type = 'sales'): array
+ public function getProductsComponentsInCategory(int $storeId, string $month, string $year, string $type = self::TYPE_SALES): array
{
$region = CityStoreParams::find()
->where(['store_id' => $storeId])
->one()->address_region;
if (!$region) {
- // определяем регион по городу
$cityId = CityStore::find()->select('city_id')->where(['id' => $storeId])->scalar();
- if ($cityId == 1) {
- $region = BouquetComposition::REGION_MSK;
- } else {
- $region = BouquetComposition::REGION_NN;
- }
+ $region = $cityId == 1
+ ? BouquetComposition::REGION_MSK
+ : BouquetComposition::REGION_NN;
}
$monthStart = sprintf('%04d-%02d-01 00:00:00', $year, $month);
if ($type == self::TYPE_SALES) {
$salesProducts = Sales::find()
->alias('s')
- ->select(['s.id', 's.date', 's.operation', 'sp.product_id', 'p1c.type', 'p1c.components' , 'p1c.name'])
+ ->select([
+ 's.id',
+ 's.date',
+ 's.operation',
+ 'sp.product_id',
+ 'p1c.type',
+ 'p1c.components',
+ 'p1c.name',
+ 'sp.quantity AS quantity_product'
+ ])
->innerJoin(
['sp' => SalesProducts::tableName()],
's.id = sp.check_id'
->andWhere(['not', ['s.operation' => ['Удален', 'Удаление']]])
->andWhere(['between', 's.date', $monthStart, $monthEnd])
->andWhere(['not', ['p1c.components' => '']])
- //->andWhere(['not in', 'nom.category', ['', 'букет', 'сборка', 'сервис']])
->asArray()
->all();
- $componentProducts = $salesProducts;
+ $componentProducts = $salesProducts;
} else {
-
$writeOffsProducts = WriteOffs::find()
->alias('w')
->select([
- 'write_off_id' => 'w.id',
- 'write_off_date' => 'w.date',
- 'product_id' => 'wp.product_id',
- 'type' => 'p1c.type',
- 'components' => 'p1c.components',
- 'product_name' => 'p1c.name',
- 'store_id' => 'ex.entity_id',
+ 'w.id AS write_off_id',
+ 'w.date AS write_off_date',
+ 'wp.product_id',
+ 'p1c.type',
+ 'p1c.components',
+ 'p1c.name AS product_name',
+ 'ex.entity_id AS store_id',
+ 'wp.quantity AS quantity_product'
])
->innerJoin(
['wp' => WriteOffsProducts::tableName()],
)
->andWhere(['between', 'w.date', $monthStart, $monthEnd])
->andWhere(['ex.entity_id' => $storeId])
- //->andWhere(['not in', 'nom.category', ['', 'букет', 'сборка', 'сервис']])
- ->andWhere(['<>', 'p1c.components', ''])
+ ->andWhere(['not', ['p1c.components' => '']])
->asArray()
->all();
$componentProducts = $writeOffsProducts;
}
-
$components = [];
$rows = [];
foreach ($componentProducts as $cp) {
-
- $js = trim($cp['components']);
+ $js = trim($cp['components']);
if ($js === '' || $js[0] !== '{') {
continue;
}
if ($qty <= 0) {
continue;
}
-
$components[$guid] = true;
-
$rows[] = [
- 'sale_id' => $cp['id'],
- 'sale_date' => $cp['date'],
- 'product_id' => $cp['product_id'],
- 'product_name'=> $cp['name'],
- 'component_guid' => $guid,
- 'quantity' => $qty,
- 'type' => $type,
- 'operation' => $cp['operation'] ?? '',
+ 'record_id' => $cp['id'] ?? $cp['write_off_id'],
+ 'sale_date' => $cp['date'] ?? $cp['write_off_date'],
+ 'product_id' => $cp['product_id'],
+ 'product_name' => $cp['name'] ?? $cp['product_name'],
+ 'quantity_product' => $cp['quantity_product'],
+ 'component_guid' => $guid,
+ 'quantity' => $qty,
+ 'type' => $type,
+ 'operation' => $cp['operation'] ?? '',
];
}
}
if (empty($rows)) {
return [];
}
- $guids = array_keys($components);
+ $guids = array_keys($components);
$nomenclatures = Products1cNomenclature::find()
->andWhere(['id' => $guids])
->andWhere(['not in', 'category', ['', 'букет', 'сборка', 'сервис']])
$priceDynamics = PricesDynamic::find()
->andWhere(['region_id' => $region])
- ->andWhere(['product_id' => array_values( ArrayHelper::getColumn($nomenclatures, 'id') )])
+ ->andWhere(['product_id' => array_values(ArrayHelper::getColumn($nomenclatures, 'id'))])
->orderBy(['date_from' => SORT_DESC])
->all();
$pricesByProduct = [];
foreach ($priceDynamics as $pd) {
- /** @var PricesDynamic $pd */
- $pid = $pd->product_id;
- $pricesByProduct[$pid][] = $pd;
+ $pricesByProduct[$pd->product_id][] = $pd;
}
$result = [];
foreach ($rows as $r) {
- $guid = $r['component_guid'];
- $n = $nomenclatures[$guid] ?? null;
- $pid = $n?->id;
+ $guid = $r['component_guid'];
+ $n = $nomenclatures[$guid] ?? null;
+ $pid = $n?->id;
$price = 0;
- if ($pid && isset($pricesByProduct[$pid])) {
- foreach ($pricesByProduct[$pid] as $pd) {
- if ($pd->date_from <= $r['sale_date'] && $pd->date_to >= $r['sale_date']) {
+ foreach ($pricesByProduct[$pid] ?? [] as $pd) {
+ if ($pd->date_from <= $r['sale_date'] && $pd->date_to >= $r['sale_date']) {
+ if ($pid == '2b72702a-792f-11e8-9edd-1c6f659fb563') {
+ $price = 8.66;
+ } else {
$price = $pd->price;
- break;
}
+
+
+ break;
}
}
- $cost = $r['quantity'] * $price;
+ $cost = $r['quantity'] * $price * $r['quantity_product'];
+ $costComponent = $r['quantity'] * $price;
$result[] = [
- 'store_id' => $storeId,
- 'sale_id' => $r['sale_id'],
- 'sale_date' => $r['sale_date'],
- 'product_id' => $r['product_id'],
- 'product_name' => $r['product_name'],
- 'component_guid' => $guid,
- 'component_name' => $n?->name,
- 'component_category' => $n?->category,
- 'component_subcategory' => $n?->subcategory,
- 'component_species' => $n?->species,
- 'quantity' => $r['quantity'],
- 'price' => $price,
- 'cost' => $cost,
- 'month' => $month,
- 'year' => $year,
- 'type' => $r['type'],
- 'operation' => $r['operation'] ?? '',
+ 'store_id' => $storeId,
+ 'record_id' => $r['record_id'],
+ 'sale_date' => $r['sale_date'],
+ 'product_id' => $r['product_id'],
+ 'product_name' => $r['product_name'],
+ 'quantity_product' => $r['quantity_product'],
+ 'component_guid' => $guid,
+ 'component_name' => $n?->name,
+ 'component_category' => $n?->category,
+ 'component_subcategory'=> $n?->subcategory,
+ 'component_species' => $n?->species,
+ 'quantity' => $r['quantity'],
+ 'price' => $price,
+ 'cost' => $cost,
+ 'component_cost' => $costComponent,
+ 'type' => $type,
+ 'operation' => $r['operation'],
];
}
}
+
public function sumProductsComponentsByGroup(array $items, string $type, string $group = 'category'): array
{
$aggregated = [];
$category = $row['component_category'];
$subcategory = $row['component_subcategory'];
$species = $row['component_species'];
- $month = $row['month'];
- $year = $row['year'];
$operation = $row['operation'] ?? null;
$cost = (float)$row['cost'];
$keyParts[] = $species;
}
- $keyParts[] = $year;
- $keyParts[] = $month;
$key = implode('|', $keyParts);
'store_id' => $storeId,
'category' => $category,
'sum' => 0.0,
- 'month' => $month,
- 'year' => $year,
'type' => $type,
];
<?php
+
$columns = [
['attribute' => 'store_id', 'label' => 'Магазин', 'value' => function ($data) {
return CityStore::findOne($data['store_id'])->name ?? null;
['attribute' => 'category', 'label' => 'Категория'],
['attribute' => 'subcategory', 'label' => 'Подкатегория'],
['attribute' => 'species', 'label' => 'Тип'],
- ['attribute' => 'dirtyGoal', 'label' => 'Неочищенная цель', 'format' => ['decimal', 2]],
- ['attribute' => 'bouquetGoal', 'label' => 'Цель букета', 'format' => ['decimal', 2]],
- ['attribute' => 'noHistoryGoal', 'label' => 'Товары без истории', 'format' => ['decimal', 2]],
- ['attribute' => 'goal', 'label' => 'Очищенная цель', 'format' => ['decimal', 2]],
+ [
+ 'attribute'=>'dirtyGoal',
+ 'label'=>'Неочищенная цель',
+ 'format'=>['decimal',2],
+ 'pageSummary' => true,
+ ],
+ [
+ 'attribute'=>'bouquetGoal',
+ 'label'=>'Цель букета',
+ 'format'=>['decimal',2],
+ 'pageSummary' => true,
+ ],
+ [
+ 'attribute'=>'noHistoryGoal',
+ 'label'=>'Товары без истории',
+ 'format'=>['decimal',2],
+ 'pageSummary' => true,
+ ],
+ [
+ 'attribute'=>'goal',
+ 'label'=>'Очищенная цель',
+ 'format'=>['decimal',2],
+ 'pageSummary' => true,
+ ],
];
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
+ 'showPageSummary' => true,
'columns' => $columns,
]); ?>
<?php