$uniqueProductsData = [];
if ($startDate && $endDate && $storeId) {
+
+ // Получаем продажи и возвраты
+ $salesAndReturns = MotivationService::getSalesAndReturns($startDate, $endDate, $storeId);
+ $salesRecords = $salesAndReturns['salesRecords'];
+ $returnedSales = $salesAndReturns['returnedSales'];
$totalSum = MotivationService::getSelfCostSumByStore($startDate, $endDate, $storeId);
// Получаем данные о продуктах и себестоимости
'price' => $item['price'],
'm_price' => $item['m_price'],
'quantity' => $item['quantity'],
+ 'summ' => $item['summ'],
+ 'discount' => $item['discount'],
+
];
} else {
$groupedProducts[$productId]['quantity'] += $item['quantity'];
'uniqueProductsData' => $uniqueProductsData,
'totalSum' => $totalSum,
'totalProductsSum' => $totalProductsSum,
+ 'salesRecords' => $salesRecords ?? [],
+ 'returnedSales' => $returnedSales ?? [],
]);
}
}
\ No newline at end of file
$salesProducts = SalesProducts::find()
->select(['check_id', 'product_id', 'quantity'])
->where(['check_id' => $salesIds])
+ ->andWhere(['type_id' => 1])
->asArray()
->all();
// Находим товары, проданные в выбранных продажах
$salesProducts = SalesProducts::find()
- ->select(['check_id', 'product_id', 'quantity', 'price'])
+ ->select(['check_id', 'product_id', 'quantity', 'price', 'summ', 'discount'])
->where(['check_id' => $salesIds])
->andWhere(['type_id' => 1])
// ->andWhere(['>', 'price', '0' ])
'quantity' => $quantity,
'check_id' => $product['check_id'],
'm_price' => $product['price'],
+ 'summ' => $product['summ'],
+ 'discount' => $product['discount'],
];
}
}
+ public static function getSalesAndReturns($startDate, $endDate, $storeId)
+ {
+ $storeId = (int)$storeId;
+ $startDate = date('Y-m-d', strtotime($startDate));
+ $endDate = date('Y-m-d 23:59:59', strtotime($endDate));
+
+ // Находим все продажи за указанный период
+ $salesRecords = Sales::find()
+ // ->select(['id', 'date'])
+ ->where(['store_id' => $storeId, 'operation' => Sales::OPERATION_SALE])
+ ->andWhere(['>=', 'date', $startDate])
+ ->andWhere(['<=', 'date', $endDate])
+ ->asArray()
+ ->all();
+
+ $endDatePlusThreeWeeks = date('Y-m-d 23:59:59', strtotime($endDate . ' +3 weeks'));
+
+ // Находим возвраты за указанный период + 3 недели
+ $returnedSales = Sales::find()
+ // ->select(['id', 'date', 'sales_check'])
+ ->where(['store_id' => $storeId, 'operation' => Sales::OPERATION_RETURN])
+ ->andWhere(['>=', 'date', $startDate])
+ ->andWhere(['<=', 'date', $endDatePlusThreeWeeks])
+ ->andWhere(['is not', 'sales_check', null])
+ ->asArray()
+ ->all();
+
+ return [
+ 'salesRecords' => $salesRecords,
+ 'returnedSales' => $returnedSales,
+ ];
+ }
+
public static function getCostConsumablesSalesSupportByStore($startDate, $endDate, $storeId) {
$exportImportTable = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', 'entity_id' => $storeId, 'export_id' => 1])->one();
/* @var $uniqueProductsData array */
/* @var $totalSum float */
/* @var $totalProductsSum float */
+/* @var $salesRecords array */
+/* @var $returnedSales array */
$this->title = 'Себестоимость товаров по магазинам';
?>
['attribute' => 'check_id', 'label' => 'ID чека'],
['attribute' => 'product_name', 'label' => 'Название'],
['attribute' => 'm_price', 'label' => 'Цена'],
+ ['attribute' => 'summ', 'label' => 'Сумма', 'footer' => array_sum(array_column($data, 'summ')),],
+ ['attribute' => 'discount', 'label' => 'Скидка', 'footer' => array_sum(array_column($data, 'discount')),],
['attribute' => 'price', 'label' => 'Себестоимость ед'],
[
'attribute' => 'quantity',
],
]); ?>
<?php endif; ?>
+
+ <?php if (!empty($salesRecords)): ?>
+ <h2>Продажи за период</h2>
+ <?= GridView::widget([
+ 'dataProvider' => new \yii\data\ArrayDataProvider([
+ 'allModels' => $salesRecords,
+ 'pagination' => false,
+ ]),
+ 'showFooter' => true, // Показываем подвал таблицы
+ 'columns' => [
+ ['attribute' => 'id', 'label' => 'ID продажи'],
+ ['attribute' => 'operation', 'label' => 'Операция'],
+ ['attribute' => 'status', 'label' => 'Статус'],
+ [
+ 'attribute' => 'summ',
+ 'label' => 'Сумма',
+ 'footer' => array_sum(array_column($salesRecords, 'summ')), // Сумма всех значений столбца "Сумма"
+ ],
+ ['attribute' => 'sales_check', 'label' => 'Чек возврата'],
+ ['attribute' => 'date', 'label' => 'Дата'],
+ ],
+ ]); ?>
+ <?php endif; ?>
+
+ <?php if (!empty($returnedSales)): ?>
+ <h2>Возвраты за период</h2>
+ <?= GridView::widget([
+ 'dataProvider' => new \yii\data\ArrayDataProvider([
+ 'allModels' => $returnedSales,
+ 'pagination' => false,
+ ]),
+ 'columns' => [
+ ['attribute' => 'id', 'label' => 'ID возврата'],
+ ['attribute' => 'date', 'label' => 'Дата'],
+ ['attribute' => 'sales_check', 'label' => 'ID продажи'],
+ ],
+ ]); ?>
+ <?php endif; ?>
+
+
</div>
\ No newline at end of file