setState(self::STATUS_NEW); $this->newRecord = true; } public function getStateId() { return $this->stateObject->getStateId(); } public function startWork() { $this->stateObject->startWork(); } public function sell() { $this->stateObject->sell(); } public function cancel() { $this->stateObject->cancel(); } public function remove() { $this->stateObject->remove(); } public function getState() { return $this->getStateId(); } public function saveStatus() { $sql = "UPDATE reservations SET status='" . $this->stateObject->getStateId() . "' WHERE id ='" . $this->id . "'"; mysql_query($sql); } public function saveNote($note) { $sql = "UPDATE reservations SET note='" . $note . "' WHERE id ='" . $this->id . "'"; mysql_query($sql); } public function delete() { } public function setState($val) { $states = [ self::STATUS_NEW => 'StateNew', self::STATUS_WORK => 'StateWork', self::STATUS_CANCEL => 'StateCancel', self::STATUS_BOUGHT => 'StateBought', self::STATUS_DELETE => 'StateDelete', ]; assert('isset($states[$val])'); $this->status = (int)$val; $class = $states[$val]; $this->stateObject = new $class($this); } public static function getStatusList() { return [ self::STATUS_NEW => 'Новая', self::STATUS_WORK => 'В работе', self::STATUS_CANCEL => 'Бронь снята', self::STATUS_BOUGHT => 'Выкуплена', self::STATUS_DELETE => 'Удалена', ]; } public static function getPaymentList() { return [ self::PAYMENT_NONE => '', self::PAYMENT_MORTGAGE => 'Ипотека', self::PAYMENT_FULL_PRICE => '100% стоимости', self::PAYMENT_FULL_SUBSIDY => 'Рассрочка', self::PAYMENT_MILITARY_MORTGAGE => "Военная ипотека", ]; } public function getApartment() { //todo } public function getFullname() { return $this->secondname . ' ' . $this->firstname . ' ' . $this->surname; } public function getStateName() { $states = self::getStatusList(); assert('isset($states[$this->status])'); return $states[$this->status]; } //todo рассылка писем по смене статуса public function getImages() { return ReservationImage::findForReservation($this->id); } public function save() { try { if ($this->newRecord) { $sql = "INSERT INTO reservations (object_id, user_id, firstname, secondname, surname, birth_date, phone, passport_series, passport_num, passport_place, passport_date, passport_address, payment, bank, info, status, fee, object_desc, created_at, base_price, full_price, apartment_number) VALUES ('" . $this->object_id . "', '" . $this->user_id . "', '" . $this->firstname . "', '" . $this->secondname . "', '" . $this->surname . "', '" . $this->birth_date . "', '" . $this->phone . "', '" . $this->passport_series . "', '" . $this->passport_num . "', '" . $this->passport_place . "', '" . $this->passport_date . "', '" . $this->passport_address . "', '" . $this->payment . "', '" . $this->bank . "', '" . $this->info . "', '" . $this->stateObject->getStateId() . "', '" . $this->fee . "', '" . $this->object_desc . "', NOW(), '" . $this->base_price . "', '" . $this->full_price . "', '" . $this->apartment_number . "')"; mysql_query($sql); $err = mysql_errno(); if ($err > 0) { echo mysql_error(); return false; } if (!isset($this->id)) { $this->id = mysql_insert_id(); } } else { throw new Exception("update reservation not implemented"); } return true; } catch (Exception $e) { echo $e->getMessage(); return false; } } public function getIsNewRecord() { return $this->newRecord; } public static function deleteAll($condition = '') { throw new Exception("deleteALL not implemented"); } public static function className() { return get_called_class(); } public static function findOne($id) { $sql = "SELECT * FROM reservations WHERE id = '$id'"; $rez = mysql_query($sql); if (mysql_num_rows($rez)) { $reservation = new Reservation(); $reservation->newRecord = false; $dbObject = mysql_fetch_array($rez); $reservation->id = $dbObject['id']; $reservation->user_id = $dbObject['user_id']; $reservation->object_id = $dbObject['object_id']; $reservation->created_at = $dbObject['created_at']; $reservation->object_desc = $dbObject['object_desc']; $reservation->firstname = $dbObject['firstname']; $reservation->secondname = $dbObject['secondname']; $reservation->surname = $dbObject['surname']; $reservation->birth_date = $dbObject['birth_date']; if (isset($dbObject['status'])) { $reservation->setState((int)$dbObject['status']); } $reservation->phone = $dbObject['phone']; $reservation->base_price = $dbObject['base_price']; $reservation->full_price = $dbObject['full_price']; $reservation->passport_series = $dbObject['passport_series']; $reservation->passport_num = $dbObject['passport_num']; $reservation->passport_place = $dbObject['passport_place']; $reservation->passport_date = $dbObject['passport_date']; $reservation->passport_address = $dbObject['passport_address']; $reservation->payment = $dbObject['payment']; $reservation->fee = $dbObject['fee']; $reservation->bank = $dbObject['bank']; $reservation->info = $dbObject['info']; $reservation->note = $dbObject['note']; $reservation->created_at = $dbObject['created_at']; $reservation->apartment_number = $dbObject['apartment_number']; return $reservation; } return null; } public static function count($userId) { $sql = "SELECT count(id) FROM reservations WHERE status <> '" . Reservation::STATUS_DELETE . "'"; if ($userId) { $sql = "SELECT count(id) FROM reservations WHERE user_id = '$userId' AND status <> '" . Reservation::STATUS_DELETE . "'"; } $rez = mysql_query($sql); return mysql_result($rez, 0); } /** * @param $userId * @param $from * @param $to * @return Reservation[] */ public static function search($userId, $from, $to) { $sql = "SELECT r.* FROM reservations r WHERE status <> '" . Reservation::STATUS_DELETE . "' ORDER BY id DESC LIMIT $from, $to"; if ($userId) { $sql = "SELECT r.* FROM reservations r WHERE user_id = '$userId' AND status <> '" . Reservation::STATUS_DELETE . "' ORDER BY id DESC LIMIT $from, $to"; } $rez = mysql_query($sql); $reservations = array(); if (mysql_num_rows($rez) > 0) { while ($dbObject = mysql_fetch_assoc($rez)) { $reservation = new Reservation(); $reservation->newRecord = false; $reservation->id = $dbObject['id']; $reservation->user_id = $dbObject['user_id']; $reservation->object_id = $dbObject['object_id']; $reservation->created_at = $dbObject['created_at']; $reservation->object_desc = $dbObject['object_desc']; $reservation->firstname = $dbObject['firstname']; $reservation->secondname = $dbObject['secondname']; $reservation->surname = $dbObject['surname']; $reservation->birth_date = $dbObject['birth_date']; if (isset($dbObject['status'])) { $reservation->setState((int)$dbObject['status']); } $reservation->phone = $dbObject['phone']; $reservation->base_price = $dbObject['base_price']; $reservation->full_price = $dbObject['full_price']; $reservation->passport_series = $dbObject['passport_series']; $reservation->passport_num = $dbObject['passport_num']; $reservation->passport_place = $dbObject['passport_place']; $reservation->passport_date = $dbObject['passport_date']; $reservation->passport_address = $dbObject['passport_address']; $reservation->payment = $dbObject['payment']; $reservation->fee = $dbObject['fee']; $reservation->bank = $dbObject['bank']; $reservation->info = $dbObject['info']; $reservation->note = $dbObject['note']; $reservation->created_at = $dbObject['created_at']; $reservation->apartment_number = $dbObject['apartment_number']; $reservations[] = $reservation; } } return $reservations; } }