$this->layout = false;
Yii::$app->response->format = Response::FORMAT_RAW;
+ $mappingTypes = [
+ 'offline' => 'Офлайн',
+ 'online' => 'Онлайн',
+ 'write_offs' => 'Списания',
+ 'marketplace' => 'Маркетплейс'
+ ];
while (ob_get_level() > 0) {
ob_end_clean();
}
- $filters = $request->get();
+ $filters = [];
+ $request->get();
+ $filters['year'] = (int)$request->get('year');
+ $filters['week'] = (int)$request->get('week');
+ $filters['city'] = $request->get('city', null);
+ $filters['region'] = $request->get('region', null);
+ $filters['district'] = $request->get('district', null);
+ $filters['capacity_type'] = is_numeric($request->get('capacity_type')) ? (int)$request->get('capacity_type') : null;
+ $filters['store_type'] = is_numeric($request->get('store_type')) ? (int)$request->get('store_type') : null;
+ $filters['territorial_manager'] = is_numeric($request->get('territorial_manager')) ? (int)$request->get('territorial_manager') : null;
+ $filters['bush_chef_florist'] = is_numeric($request->get('bush_chef_florist')) ? (int)$request->get('bush_chef_florist') : null;
$query = Autoplannogramma::find()
->alias('a')
->leftJoin('products_1c_nomenclature p1n', 'p1n.id = a.product_id')
->leftJoin('city_store_params cp', 'cp.store_id = a.store_id')
+ ->leftJoin('city_store c', 'c.id = a.store_id')
// ->where(['p1n.category' => $category])
// ->andWhere(['p1n.subcategory' => $subcategory])
->andFilterWhere(['=', 'a.year', $filters['year']])
$query->andWhere(['in', 'a.store_id', $bushChefFloristStoreIds ?: [-1]]);
}
- $models = $query
+ $autoplannogrammData = $query
->select([
'a.id AS plan_id',
'p1n.id AS product_id',
+ 'a.product_id',
'p1n.name AS product_name',
+ 'p1n.category AS category',
+ 'p1n.subcategory AS subcategory',
+ 'p1n.species AS species',
'a.store_id',
+ 'c.name AS store_name',
'a.modify',
+ 'a.capacity_type',
'a.details',
- 'a.month'
+ 'a.month',
+ 'a.year',
+ 'a.week'
])
->asArray()
->all();
+ $detailsData = [];
+ foreach ($autoplannogrammData as $product) {
+ $data = $product['details'];
+ while (is_string($data)) {
+ $decoded = json_decode($data, true);
+ if (json_last_error() !== JSON_ERROR_NONE) {
+ break;
+ }
+ $data = $decoded;
+ }
+ $detailsArray = $data;
+ foreach ($detailsArray as $type => $typeArr) {
+ if (!isset($typeArr['groups']) ) {
+ continue;
+ }
+ $groups = $typeArr['groups'];
+ $groups[$type] = $typeArr['quantity'];
+ foreach ($groups as $group => $quantity) {
+ if (in_array($group, array_keys($mappingTypes))) {
+ $groupName = $mappingTypes[$group];
+ } else {
+ $groupName = $group;
+ }
+ $detailsData[] = [
+ 'product_name' => $product['product_name'],
+ 'category' => $product['category'],
+ 'subcategory' => $product['subcategory'],
+ 'species' => $product['species'],
+ 'month' => $product['month'],
+ 'year' => $product['year'],
+ 'week' => $product['week'] . '/' . $product['year'] . '/' . $product['month'],
+ 'type_pm' => $product['capacity_type'] == 1 ? 'max' : 'min',
+ 'shop' => $product['store_id'] . ' ' . $product['store_name'],
+ 'group_name' => $groupName,
+ 'quantity' => $quantity,
+ 'value_type' => $type,
+ ];
+ }
- foreach ($weeklySalesForecast as $fc ) {
- $rows[] = [
- 'week' => $fc['week'],
- 'type_pm' => 'max',
- 'shop' => $fc['store_id'],
- 'category' => $fc['category'],
- 'subcategory' => $fc['subcategory'],
- 'species' => $fc['species'],
- 'product_name' => $fc['product_id'],
- 'quantity' => $fc['forecast_week_pieces'],
- 'value_type' => 'offline',
- 'group_name' => 'Оффлайн',
- ];
+ }
}
-
-
-
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Отчёт');
$rowNum = 2;
- foreach ($rows as $row) {
+ foreach ($detailsData as $row) {
$sheet->setCellValue('A' . $rowNum, $row['week']);
$sheet->setCellValue('B' . $rowNum, $row['type_pm']);
$sheet->setCellValue('C' . $rowNum, $row['shop']);
row.style.display = "none";
}
});
+ $('#export-excel-btn').on('click', function (e) {
+ e.preventDefault();
+
+ const baseUrl = '/auto-plannogramma/export-excel';
+ const params = {
+ year: $('#year').val(),
+ week: $('#week').val(),
+ city: $('#city').val(),
+ region: $('#region').val(),
+ store_type: $('#store-type').val(),
+ capacity_type: $('#capacity-type').val() || null,
+ bush_chef_florist: $('#bush_chef_florist').val(),
+ territorial_manager: $('#territorial-manager').val(),
+ polnogramma_type: $('#polnogramma-type').val()
+ };
+
+ const query = Object.entries(params)
+ .filter(([_, v]) => v !== null && v !== '')
+ .map(([k, v]) => k + '=' + encodeURIComponent(v))
+ .join('&');
+
+ const url = baseUrl + (query ? '?' + query : '');
+
+ window.open(url, '_blank');
+ });
document.querySelectorAll(".category").forEach(category => {
category.addEventListener("click", function () {