use Yii;
use yii\console\Controller;
+use yii\db\Expression;
use yii_app\records\PricesDynamic;
class PriceController extends Controller {
public function actionFixActiveNullRegion() {
$pricesDynamics = PricesDynamic::find()->where(['or', ['region_id' => null], ['region_id' => 52]])
- ->andWhere(['active' => 1])->orderBy(['date_from' => SORT_ASC])->all();
+ ->andWhere(['active' => 1])
+ //->andWhere(['product_id' => ['5466448f-946a-11e2-bf3d-1c6f659fb563', 'c552f4d8-0a1e-11e5-bd74-1c6f659fb563', '8476f3e1-a9ee-11e4-bedc-1c6f659fb563']])
+ ->orderBy(['date_from' => SORT_ASC])
+ ->all();
$p = [];
foreach ($pricesDynamics as $pricesDynamic) {
/** @var $pricesDynamic PricesDynamic */
$p[$pricesDynamic->region_id ?? 0][$pricesDynamic->product_id][] = [
'id' => $pricesDynamic->id,
- 'date_to' => $pricesDynamic->date_to,
+ 'date_from' => $pricesDynamic->date_from,
];
}
- foreach ($p[0] as $productId => $priceDynamicDatas) {
- if ($p[52][$productId]) {
+ foreach ($p[0] ?? [] as $productId => $priceDynamicDatas) {
+ if (isset($p[52][$productId])) {
$cnt = count($priceDynamicDatas);
- for ($ind = 0; $ind < $cnt - 1; $cnt++) {
+ for ($ind = 0; $ind < $cnt; $ind++) {
if ($ind < $cnt - 1) {
- PricesDynamic::updateAll(['active' => 0, 'region_id' => 52, 'date_to' => $priceDynamicDatas[$ind+1]['date_to']],
+ PricesDynamic::updateAll(['active' => 0, 'region_id' => 52, 'date_to' => $priceDynamicDatas[$ind+1]['date_from']],
['id' => $priceDynamicDatas[$ind]['id']]);
echo '1';
} else {
- PricesDynamic::updateAll(['active' => 0, 'region_id' => 52, 'date_to' => $p[52][$productId][count($p[52][$productId]) - 1]['date_to']],
+ PricesDynamic::updateAll(['active' => 0, 'region_id' => 52, 'date_to' => $p[52][$productId][count($p[52][$productId]) - 1]['date_from']],
['id' => $priceDynamicDatas[$ind]['id']]);
echo '2';
}