use DateTime;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
+use yii_app\records\CityStore;
use yii_app\records\Motivation;
use yii_app\records\PricesDynamic;
use yii_app\records\Products1cAdditionalCharacteristics;
* @param string $month Месяц в формате "mm" (например, "03").
* @return float Цена, если найдена запись, иначе 0.
*/
- public static function getPriceForProductAndMonth($productId, $year, $month)
+ public static function getPriceForProductAndMonth($productId, $year, $month, $storeId)
{
- $dateToCheck = "$year-$month-01";
- $priceRecord = PricesDynamic::find()
- ->where(['product_id' => $productId, 'active' => 1])
- ->andWhere(['<=', 'date_from', $dateToCheck])
+ $monthStart = "$year-$month-01";
+ $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
+ $monthEnd = sprintf('%04d-%02d-%02d', $year, $month, $daysInMonth);
+
+ $priceRecords = PricesDynamic::find()
+ ->where(['product_id' => $productId])
+ ->andWhere(['<=', 'date_from', $monthEnd])
->andWhere([
'or',
- ['>=', 'date_to', $dateToCheck],
- ['date_to' => null]
- ])
- ->one();
- if ($priceRecord) {
- return (float)$priceRecord->price;
+ ['>=', 'date_to', $monthStart],
+ ['date_to' => '2100-01-01 03:00:00+03']
+ ]);
+ if (CityStore::find()->where(['id' => $storeId])->one()->city_id == 1342) {
+ $region = 52;
+ } elseif (CityStore::find()->where(['id' => $storeId])->one()->city_id == 1) {
+ $region = 77;
+ } else {
+ $region = null;
}
- return 0;
+ $priceRecords->andWhere(['or',['region_id' => $region],['region_id' => null]])->all();
+
+
+ if (!empty($priceRecords)) {
+ $totalPrice = 0;
+ $count = count($priceRecords);
+ foreach ($priceRecords as $record) {
+ $totalPrice += $record->price; // предполагаем, что поле с ценой называется "price"
+ }
+ $averagePrice = $totalPrice / $count;
+ } else {
+ $averagePrice = 0;
+ }
+
+
+ return $averagePrice;
}
/**
$mKey = $mInfo['month'];
$sales = isset($product['weekly_sales'][$mKey]) ? array_sum($product['weekly_sales'][$mKey]) : 0;
- $price = self::getPriceForProductAndMonth($guid, $mInfo['year'], $mInfo['month']);
+ $price = self::getPriceForProductAndMonth($guid, $mInfo['year'], $mInfo['month'], $storeId);
$monthlySales[$mKey] = $sales;
$weightedValue = $sales * $price * $mInfo['weight'];
$monthlyWeighted[$mKey] = $weightedValue;