--- /dev/null
+<?php
+
+namespace yii_app\actions\timetable;
+
+use yii\base\Action;
+use yii_app\records\AdminCheckin;
+use yii_app\records\TimetablePlan;
+
+class JoinMissingShiftsWithCheckinsAction extends Action
+{
+ public function run($days = 30) {
+ $checkins = AdminCheckin::find()->where(['>=', 'date', date('Y-m-d', strtotime('-' . $days . ' day', time()))])
+ ->andWhere(['plan_id' => null])->all();
+ foreach ($checkins as $checkin) {
+ /** @var $checkin AdminCheckin */
+ $planSlots = TimetablePlan::find()
+ ->andWhere(['store_id' => $checkin->store_id, 'admin_id' => $checkin->admin_id])
+ ->andWhere(['OR',
+ [
+ 'BETWEEN', 'datetime_start',
+ date('Y-m-d H:i:s', strtotime($checkin->time)),
+ date('Y-m-d H:i:s', strtotime('+6 hour', strtotime($checkin->time)))],
+ [
+ 'BETWEEN', 'datetime_end',
+ date('Y-m-d H:i:s', strtotime('-6 hour', strtotime($checkin->time))),
+ date('Y-m-d H:i:s', strtotime($checkin->time))],
+ [
+ 'AND',
+ ['<', 'datetime_start', date('Y-m-d H:i:s', strtotime($checkin->time))],
+ ['>', 'datetime_end', date('Y-m-d H:i:s', strtotime($checkin->time))]
+ ]
+ ])
+ ->one();
+ /** @var $planSlots TimetablePlan */
+ if ($planSlots) {
+ $checkin->plan_id = $planSlots->id;
+ $checkin->save();
+ if ($checkin->getErrors()) {
+ var_dump($checkin->getErrors());
+ }
+ }
+ }
+ return 'ok';
+ }
+}
\ No newline at end of file