From: vladfo Date: Fri, 11 Oct 2024 07:35:45 +0000 (+0300) Subject: Добавил интерфейс вывода себестоимости X-Git-Tag: 1.6~31^2~16 X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=7ba641eed620b76845637e6467877453b8a2995d;p=erp24_rep%2Fyii-erp24%2F.git Добавил интерфейс вывода себестоимости --- diff --git a/erp24/actions/motivation/TestSelfCostAction.php b/erp24/actions/motivation/TestSelfCostAction.php new file mode 100644 index 00000000..b286e9b4 --- /dev/null +++ b/erp24/actions/motivation/TestSelfCostAction.php @@ -0,0 +1,59 @@ +select(['id', 'name']) + ->where(['visible' => 1]) + ->asArray() + ->all(); + + $storeList = ArrayHelper::map($stores, 'id', 'name'); + + // Создаем модель формы + $model = new DynamicModel(['startDate', 'endDate', 'storeId']); + $model->addRule(['startDate', 'endDate'], 'date', ['format' => 'php:Y-m-d']) + ->addRule(['storeId'], 'integer'); + + // Загружаем данные из GET-параметров + if ($model->load(Yii::$app->request->get()) && $model->validate()) { + $startDate = $model->startDate; + $endDate = $model->endDate; + $storeId = $model->storeId; + } else { + $startDate = ''; + $endDate = ''; + $storeId = ''; + } + + $data = []; + $totalSum = 0.0; + + if ($startDate && $endDate && $storeId) { + $totalSum = MotivationService::getSelfCostSumByStore($startDate, $endDate, $storeId); + + // Получаем данные о продуктах и себестоимости + $salesProducts = MotivationService::getSalesProductsDetails($startDate, $endDate, $storeId); + $data = $salesProducts; + } + + return $this->controller->render('test-self-cost', [ + 'storeList' => $storeList, + 'model' => $model, + 'data' => $data, + 'totalSum' => $totalSum, + ]); + } +} \ No newline at end of file diff --git a/erp24/controllers/MotivationController.php b/erp24/controllers/MotivationController.php index 2839c8f9..d7a516ee 100644 --- a/erp24/controllers/MotivationController.php +++ b/erp24/controllers/MotivationController.php @@ -22,6 +22,7 @@ class MotivationController extends Controller 'mounth-salary' => \yii_app\actions\motivation\SumPreviousMonthSalaryAction::class, 'sum-salary-by-month' => \yii_app\actions\motivation\SumSalaryByMonthAction::class, 'test-fact' => \yii_app\actions\motivation\TestFactAction::class, + 'test-selfcost' => \yii_app\actions\motivation\TestSelfCostAction::class, ]; } diff --git a/erp24/services/MotivationService.php b/erp24/services/MotivationService.php index 3ab5b416..3b7bdad1 100644 --- a/erp24/services/MotivationService.php +++ b/erp24/services/MotivationService.php @@ -1908,6 +1908,106 @@ class MotivationService return $totalSum; } + + /** + * Получение информации о продуктах и их себестоимости по магазину за определенный промежуток дат. + * + * @param string $startDate Дата начала периода (формат Y-m-d). + * @param string $endDate Дата конца периода (формат Y-m-d). + * @param int $storeId ID магазина. + * @return array Массив с данными о товарах, их себестоимости и дате. + */ + public static function getSalesProductsDetails($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(['sales_check']) + ->where(['store_id' => $storeId, 'operation' => Sales::OPERATION_RETURN]) + ->andWhere(['>=', 'date', $startDate]) + ->andWhere(['<=', 'date', $endDatePlusThreeWeeks]) + ->andWhere(['is not', 'sales_check', null]) + ->asArray() + ->all(); + + $returnedSalesIds = array_column($returnedSales, 'sales_check'); + + // Фильтруем продажи, исключая возвраты + $salesRecords = array_filter($salesRecords, function($sale) use ($returnedSalesIds) { + return !in_array($sale['id'], $returnedSalesIds); + }); + + if (empty($salesRecords)) { + return []; + } + + $salesIds = array_column($salesRecords, 'id'); + $saleDates = array_column($salesRecords, 'date', 'id'); + + // Находим товары, проданные в выбранных продажах + $salesProducts = SalesProducts::find() + ->select(['check_id', 'product_id', 'quantity']) + ->where(['check_id' => $salesIds]) + ->asArray() + ->all(); + + $productIds = array_unique(array_column($salesProducts, 'product_id')); + $selfCosts = SelfCostProduct::find() + ->select(['product_guid', 'price', 'date']) + ->where(['store_id' => $storeId, 'product_guid' => $productIds]) + ->andWhere(['date' => array_map(fn($date) => date('Y-m-d', strtotime($date)), $saleDates)]) + ->asArray() + ->all(); + + $selfCostsMap = []; + foreach ($selfCosts as $cost) { + $selfCostsMap[$cost['product_guid']][$cost['date']] = $cost['price']; + } + + // Находим имена продуктов + $products = Products1c::find() + ->select(['id', 'name']) + ->where(['id' => $productIds]) + ->asArray() + ->all(); + + $productNamesMap = ArrayHelper::map($products, 'id', 'name'); + + $result = []; + + foreach ($salesProducts as $product) { + $productId = $product['product_id']; + $quantity = (float)$product['quantity']; + $checkDate = date('Y-m-d', strtotime($saleDates[$product['check_id']])); + + $price = $selfCostsMap[$productId][$checkDate] ?? 0.0; + + $result[] = [ + 'product_id' => $productId, + 'product_name' => $productNamesMap[$productId] ?? 'Неизвестный товар', + 'price' => $price, + 'date' => $checkDate, + ]; + } + + return $result; + } + + public static function getCostConsumablesSalesSupportByStore($startDate, $endDate, $storeId) { $exportImportTable = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', 'entity_id' => $storeId, 'export_id' => 1])->one(); diff --git a/erp24/views/motivation/test-self-cost.php b/erp24/views/motivation/test-self-cost.php new file mode 100644 index 00000000..7c964e9b --- /dev/null +++ b/erp24/views/motivation/test-self-cost.php @@ -0,0 +1,64 @@ +title = 'Себестоимость товаров по магазинам'; +?> + +
+

title) ?>

+ +
+ ['motivation/test-selfcost'], 'method' => 'get']); ?> + + field($model, 'startDate')->input('date') ?> + + field($model, 'endDate')->input('date') ?> + + field($model, 'storeId')->widget(Select2::classname(), [ + 'data' => $storeList, + 'options' => ['placeholder' => 'Выберите магазин'], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ]); ?> + +
+ 'btn btn-primary']) ?> +
+ + +
+ + +

Результаты

+ + + + + + + + + + + + + + + + + + + +
ID товараНазваниеЦенаДата
+

Общая стоимость:

+ +
\ No newline at end of file