$endDate = date('Y-m-d', strtotime($endDate));
- $sum = (float)SelfCostProduct::find()
- ->where(['store_id' => $storeId])
+ $salesRecords = Sales::find()
+ ->select(['id', 'date'])
+ ->where(['store_id' => $storeId, 'operation' => 'Продажа'])
->andWhere(['>=', 'date', $startDate])
->andWhere(['<=', 'date', $endDate])
- ->sum('price');
+ ->asArray()
+ ->all();
+
+
+ $endDatePlusThreeWeeks = date('Y-m-d', strtotime($endDate . ' +3 weeks'));
+
+ $returnedSales = Sales::find()
+ ->select(['sales_check'])
+ ->where(['store_id' => $storeId, 'operation' => 'Возврат'])
+ ->andWhere(['>=', 'date', $startDate])
+ ->andWhere(['<=', 'date', $endDatePlusThreeWeeks]) // $endDate + 3 недели
+ ->andWhere(['is not', 'sales_check', null]) // Проверяем, что поле sales_check не пустое
+ ->asArray()
+ ->all();
+
+
+ $returnedSalesIds = array_column($returnedSales, 'sales_check');
+
+
+ $salesRecords = array_filter($salesRecords, function($sale) use ($returnedSalesIds) {
+ return !in_array($sale['id'], $returnedSalesIds);
+ });
+
+ // Если нет подходящих чеков, сразу возвращаем 0
+ if (empty($salesRecords)) {
+ return 0.0;
+ }
+
- return $sum;
+ $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'];
+ }
+
+ $totalSum = 0.0;
+
+ foreach ($salesProducts as $product) {
+ $productId = $product['product_id'];
+ $quantity = (float)$product['quantity'];
+ $checkDate = date('Y-m-d', strtotime($saleDates[$product['check_id']]));
+
+ if (isset($selfCostsMap[$productId][$checkDate])) {
+ $totalSum += $selfCostsMap[$productId][$checkDate] * $quantity;
+ }
+ }
+
+ return $totalSum;
}
+ public static function getCostConsumablesSalesSupportByStore($startDate, $endDate, $storeId) {
+ $exportImportTable = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', 'entity_id' => $storeId, 'export_id' => 1])->one();
+
+ if ($exportImportTable) {
+ $writeOffs = WriteOffs::find()->alias('wo')->select(['wo.type', 'wo.date', 'wop.product_id', 'wop.quantity'])
+ ->rightJoin('write_offs_products wop', 'wop.write_offs_id = wo.id')
+ ->where(['between', 'wo.date', $startDate, $endDate])
+ ->andWhere(['wo.store_id' => $exportImportTable->export_val])
+ ->andWhere(['type' => 'Расходные материалы (обеспечение продаж)'])
+ ->asArray()->all();
+
+ $selfCostProduct = SelfCostProduct::find()->select(['price', 'product_guid', 'date'])
+ ->where(['between', 'date', $startDate, $endDate])
+ ->andWhere(['store_id' => $storeId])
+ ->asArray()->all();
+
+ $selfCostProductMap = [];
+ foreach ($selfCostProduct as $scp) {
+ $selfCostProductMap[$scp['date']][$scp['product_guid']] = floatval($scp['price']);
+ }
+
+ $sum = 0;
+ foreach($writeOffs as $data) {
+ $sum += ($selfCostProductMap[date("Y-m-d", strtotime($data['date']))][$data['product_id']] ?? 0)
+ * ($data['quantity'] ?? 0);
+ }
+
+ return $sum;
+ }
+
+ return 0;
+ }
+
/**
* Сохраняет мотивацию по себестоимости товара для указанного магазина и месяца.
*