Joywork/import/reservation/Reservation.php
2026-05-22 21:21:54 +03:00

343 lines
12 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* This is the model class for table "reservations".
*
* @property integer $id
* @property integer $object_id
* @property integer $user_id
* @property string $object_desc //текстовое поле описания в котором прихраниваем описание объекта, а может и еще какой мусор
* @property string $firstname
* @property string $secondname
* @property string $surname
* @property string $birth_date
* @property string $status
* @property string $phone
* @property string $base_price
* @property string $full_price
* @property integer $passport_series
* @property integer $passport_num
* @property string $passport_place
* @property string $passport_date
* @property string $passport_address
* @property integer $payment
* @property float $fee
* @property string $bank
* @property string $info
* @property string $note
* @property string $created_at
* @property string $apartment_number
* @property bool $newRecord
*/
class Reservation implements ImportRecord, IReservationState
{
const STATUS_NEW = 1;
const STATUS_WORK = 2;
const STATUS_CANCEL = 3;
const STATUS_BOUGHT = 4;
const STATUS_DELETE = 5;
const PAYMENT_NONE = 0;
const PAYMENT_MORTGAGE = 1;
const PAYMENT_FULL_PRICE = 2;
const PAYMENT_FULL_SUBSIDY = 3;
const PAYMENT_MILITARY_MORTGAGE = 4;
public $id;
public $object_id;
public $user_id;
public $object_desc; //текстовое поле описания в котором прихраниваем описание объекта, а может и еще какой мусор
public $firstname;
public $secondname;
public $surname;
public $birth_date;
public $status;
public $phone;
public $base_price;
public $full_price;
public $passport_series;
public $passport_num;
public $passport_place;
public $passport_date;
public $passport_address;
public $payment;
public $fee;
public $bank;
public $info;
public $note;
public $created_at;
public $newRecord;
public $apartment_number;
/**
* @var IReservationState
*/
private $stateObject;
public function __construct()
{
$this->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;
}
}