public function flexEarlier($offerin_id, $whenStart = 0, $news = [], $tuples = []) { $offerin = Model::Offerin()->findOrFail((int) $offerin_id); $outs = array_merge(Model::offerouttrash()->where(['offerin_id', '=', (int) $offerin_id])->cursor()->toArray(), Model::offerout()->where(['offerin_id', '=', (int) $offerin_id])->where(['start', '=', (int) $offerin->start])->cursor()->toArray()); $whenStart = is_null($whenStart) ? 0 : $whenStart; $start = 0 == $whenStart || !is_integer($whenStart) ? $offerin->start : $whenStart; $ofCount = Model::offerout()->where(['offerin_id', '=', (int) $offerin_id])->where(['start', '=', (int) $offerin->start])->cursor()->count(); if (count($offerin->resellers) == $ofCount) { return []; return ['error' => 'no_more_reseller']; } if ($offerin->start - $start > 72 * 3600) { return $news; } if (!is_null($start)) { for ($i = $start - 1800; $i >= $start - 7200; $i -= 1800) { foreach ($outs as $out) { if (!in_array($out['reseller_id'], $tuples)) { $tmpStart = $i; $tmpEnd = $tmpStart + $out['duration']; $employees = $this->getEmployeesCan((int) $tmpStart, (int) $tmpEnd, (int) $out['reseller_id']); if (!empty($employees)) { unset($out['id']); unset($out['created_at']); unset($out['updated_at']); $out['reselleremployees'] = $employees; $out['start'] = $tmpStart; $out['end'] = $tmpEnd; $out['status_id'] = (int) lib('status')->getId('offerout', 'SHOWN'); $offerOut = Model::Offerout()->create($out)->save(); $out['offerout_id'] = $offerOut->id; $news[] = $out; $tuples[] = $out['reseller_id']; } } } } } return count($news) > $ofCount ? $news : $this->flexEarlier($offerin_id, $i, $news, $tuples); }