class PriceController extends Controller {
public function actionFixActiveNullRegion() {
- $pricesDynamics = PricesDynamic::find()->where(['or', ['region_id' => null], ['region_id' => 52]])->andWhere(['active' => 1])->all();
+ $pricesDynamics = PricesDynamic::find()->where(['or', ['region_id' => null], ['region_id' => 52]])
+ ->andWhere(['active' => 1])->orderBy(['date_from' => SORT_ASC])->all();
$p = [];
foreach ($pricesDynamics as $pricesDynamic) {
/** @var $pricesDynamic PricesDynamic */
- $p[$pricesDynamic->region_id ?? 0][$pricesDynamic->product_id][$pricesDynamic->id] = true;
+ $p[$pricesDynamic->region_id ?? 0][$pricesDynamic->product_id][] = [
+ 'id' => $pricesDynamic->id,
+ 'date_to' => $pricesDynamic->date_to,
+ ];
}
- $indices = [];
- foreach ($p[0] as $productId => $pdId) {
+ foreach ($p[0] as $productId => $priceDynamicDatas) {
if ($p[52][$productId]) {
- $indices[] = array_keys($pdId)[0];
+ $cnt = count($priceDynamicDatas);
+ for ($ind = 0; $ind < $cnt - 1; $cnt++) {
+ if ($ind < $cnt - 1) {
+ PricesDynamic::updateAll(['active' => 0, 'date_to' => $priceDynamicDatas[$ind+1]['date_to']],
+ ['id' => $priceDynamicDatas[$ind]['id']]);
+ echo '1';
+ }
+ }
}
}
- PricesDynamic::updateAll(['active' => 0], ['id' => $indices]);
return 'ok';
}
}