Joywork/import/obj/Apartment.php

352 lines
15 KiB
PHP
Raw Normal View History

2026-05-22 20:21:54 +02:00
<?php
/**
* This is the model class for table "apartments".
*
* @property integer $id
* @property integer $block_id
* @property integer $building_id
* @property integer $flat_floor
* @property integer $room_type_id
* @property double $space_total
* @property string $space_room
* @property string $space_kitchen
* @property string $space_balcony
* @property string $space_corridor
* @property string $space_watercloset
* @property double $height
* @property integer $flat_type_id
* @property integer decoration_id
* @property integer $subsidy
* @property integer $creditend
* @property integer $number
* @property integer $discount_price
* @property integer $base_price
* @property integer $meter_price
* @property integer $discount_meter_price
* @property integer $section
* @property integer $price
* @property integer $status
* @property integer $user_id
* @property string $image
* @property string $image2
* @property float $fee
* @property string $xml_updated_at
* @property string $created_at
* @property string $updated_at
* @property string $newRecord
*/
class Apartment implements ImportRecord, DocTarget
{
const PAYMENT_MORTGAGE = 1;
const PAYMENT_SUBSIDY = 2;
const PAYMENT_MILITARY = 3;
public $id;
public $block_id;
public $building_id;
public $flat_floor;
public $room_type_id;
public $space_total;
public $space_room;
public $space_kitchen;
public $space_balcony;
public $space_corridor;
public $space_watercloset;
public $height;
public $flat_type_id;
public $decoration_id;
public $subsidy;
public $creditend;
public $number;
public $discount_price;
public $base_price;
public $meter_price;
public $discount_meter_price;
public $section;
public $price;
public $status;
public $user_id;
public $image;
public $image2;
public $fee;
public $xml_updated_at;
public $created_at;
public $updated_at;
public $newRecord;
/**
* Apartment constructor.
*/
public function __construct()
{
$this->newRecord = true;
}
public function save()
{
try {
if ($this->newRecord) {
if (!isset($this->id)) {
$sql = "INSERT INTO apartments (block_id, building_id, room_type_id, space_total,
space_room, space_kitchen, space_balcony, space_corridor, space_watercloset, height,
number, section, flat_type_id, decoration_id, subsidy, creditend, price, discount_price,
base_price, meter_price, discount_meter_price, flat_floor, image, created_at, fee, image2, user_id) VALUES
('" . $this->block_id . "', '" . $this->building_id . "', '" . $this->room_type_id .
"', '" . $this->space_total . "', '" . $this->space_room . "', '" . $this->space_kitchen . "', '" . $this->space_balcony .
"', '" . $this->space_corridor . "', '" . $this->space_watercloset . "', '" . $this->height .
"', \"" . mysql_real_escape_string($this->number) . "\", \"" . mysql_real_escape_string($this->section) . "\", '" . $this->flat_type_id . "', '" . $this->decoration_id .
"', '" . $this->subsidy . "', '" . $this->creditend . "', '" . $this->price . "', '" . $this->discount_price .
"', '" . $this->base_price . "', '" . $this->meter_price . "', '" . $this->discount_meter_price .
"', '" . $this->flat_floor . "', '" . $this->image . "', NOW(), '" . $this->fee . "', '" . $this->image2 . "', '" . $this->user_id . "')";
} else {
$sql = "INSERT INTO apartments (id, block_id, building_id, room_type_id, space_total,
space_room, space_kitchen, space_balcony, space_corridor, space_watercloset, height,
number, section, flat_type_id, decoration_id, subsidy, creditend, price, discount_price,
base_price, meter_price, discount_meter_price, flat_floor, image, created_at, fee, image2, user_id) VALUES
('" . $this->id . "', '" . $this->block_id . "', '" . $this->building_id . "', '" . $this->room_type_id .
"', '" . $this->space_total . "', '" . $this->space_room . "', '" . $this->space_kitchen . "', '" . $this->space_balcony .
"', '" . $this->space_corridor . "', '" . $this->space_watercloset . "', '" . $this->height .
"', \"" . mysql_real_escape_string($this->number) . "\", \"" . mysql_real_escape_string($this->section) . "\", '" . $this->flat_type_id . "', '" . $this->decoration_id .
"', '" . $this->subsidy . "', '" . $this->creditend . "', '" . $this->price . "', '" . $this->discount_price .
"', '" . $this->base_price . "', '" . $this->meter_price . "', '" . $this->discount_meter_price .
"', '" . $this->flat_floor . "', '" . $this->image . "', NOW(), '" . $this->fee . "', '" . $this->image2 . "', '" . $this->user_id . "')";
}
mysql_query($sql);
$err = mysql_errno();
if ($err > 0) {
echo $sql;
echo mysql_error();
return false;
}
if (!isset($this->id)) {
$this->id = mysql_insert_id();
}
} else {
$sql = "UPDATE apartments SET block_id='" . $this->block_id . "', building_id = '" . $this->building_id . "', room_type_id = '" . $this->room_type_id .
"', space_total = '" . $this->space_total . "', space_room = '" . $this->space_room . "', space_kitchen = '" . $this->space_kitchen .
"', space_balcony = '" . $this->space_balcony . "', space_corridor = '" . $this->space_corridor . "', space_watercloset = '" . $this->space_watercloset .
"', height = '" . $this->height . "', number = \"" . mysql_real_escape_string($this->number) . "\", section = \"" . mysql_real_escape_string($this->section) . "\", flat_type_id = '" . $this->flat_type_id .
"', decoration_id = '" . $this->decoration_id . "', subsidy = '" . $this->subsidy . "', creditend = '" . $this->creditend . "', price = '" . $this->price .
"', discount_price ='" . $this->discount_price . "', base_price = '" . $this->base_price . "', meter_price = '" . $this->meter_price .
"', discount_meter_price = '" . $this->discount_meter_price . "', flat_floor = '" . $this->flat_floor . "', image = '" . $this->image .
"', updated_at = NOW(), fee = '" . $this->fee . "', image2 = '" . $this->image2 . "', user_id = '" . $this->user_id . "' WHERE id = '" . $this->id . "'";
mysql_query($sql);
$err = mysql_errno();
if ($err > 0) {
echo $sql;
echo mysql_error();
return false;
}
}
return true;
} catch (Exception $e) {
echo $e->getMessage();
return false;
}
}
public function getIsNewRecord()
{
return $this->newRecord;
}
public static function deleteAll($condition = '')
{
$sql = "DELETE from apartments WHERE " . $condition;
mysql_query($sql);
}
public static function className()
{
return get_called_class();
}
public static function findOne($id)
{
$sql = "SELECT * FROM apartments WHERE id='$id'";
$rez = mysql_query($sql);
if (mysql_num_rows($rez)) {
$apartment = new Apartment();
$apartment->newRecord = false;
$dbObject = mysql_fetch_array($rez);
$apartment->id = $dbObject['id'];
$apartment->number = $dbObject['number'];
$apartment->block_id = $dbObject['block_id'];
$apartment->building_id = $dbObject['building_id'];
$apartment->room_type_id = $dbObject['room_type_id'];
$apartment->space_total = $dbObject['space_total'];
$apartment->space_room = $dbObject['space_room'];
$apartment->space_kitchen = $dbObject['space_kitchen'];
$apartment->space_balcony = $dbObject['space_balcony'];
$apartment->space_corridor = $dbObject['space_corridor'];
$apartment->space_watercloset = $dbObject['space_watercloset'];
$apartment->height = $dbObject['height'];
$apartment->section = $dbObject['section'];
$apartment->decoration_id = $dbObject['decoration_id'];
$apartment->subsidy = $dbObject['subsidy'];
$apartment->creditend = $dbObject['creditend'];
$apartment->price = $dbObject['price'];
$apartment->discount_price = $dbObject['discount_price'];
$apartment->flat_type_id = $dbObject['flat_type_id'];
$apartment->base_price = $dbObject['base_price'];
$apartment->meter_price = $dbObject['meter_price'];
$apartment->discount_meter_price = $dbObject['discount_meter_price'];
$apartment->flat_floor = $dbObject['flat_floor'];
$apartment->image = $dbObject['image'];
$apartment->image2 = $dbObject['image2'];
$apartment->user_id = $dbObject['user_id'];
$apartment->fee = $dbObject['fee'];
$apartment->created_at = $dbObject['created_at'];
$apartment->updated_at = $dbObject['updated_at'];
return $apartment;
}
return null;
}
/**
* @return integer
*/
public function getDocTargetType()
{
return Document::TYPE_APARTMENT;
}
/**
* @return integer
*/
public function getDocTargetId()
{
return $this->id;
}
public static function getPaymentList()
{
return [
self::PAYMENT_MORTGAGE => 'Ипотека',
self::PAYMENT_SUBSIDY => 'Субсидии',
self::PAYMENT_MILITARY => 'Военная ипотека',
];
}
public static function count($blockId, $roomTypeIds, $decTypeId, $period, $stoim_ot, $stoim_do, $sq_all_ot, $sq_all_do, $flat_floor_ot = 0, $flat_floor_do = 0)
{
$sql = "SELECT count(id) FROM apartments ";
$wherePart = "WHERE block_id = '$blockId' ";
if (isset($roomTypeIds) && $roomTypeIds) {
$wherePart = $wherePart . " AND room_type_id in (" . implode(",", $roomTypeIds) . ")";
}
if (isset($decTypeId) && $decTypeId) {
$wherePart = $wherePart . " AND decoration_id = " . $decTypeId . "";
}
if (isset($stoim_ot) && $stoim_ot) {
$stoim_ot = intval(str_replace(' ', '', $stoim_ot));
$wherePart = $wherePart . " AND price >= " . $stoim_ot . "";
}
if (isset($stoim_do) && $stoim_do) {
$stoim_do = intval(str_replace(' ', '', $stoim_do));
$wherePart = $wherePart . " AND price <= " . $stoim_do . "";
}
if (isset($sq_all_ot) && $sq_all_ot) {
$wherePart = $wherePart . " AND space_total >= " . $sq_all_ot . "";
}
if (isset($sq_all_do) && $sq_all_do) {
$wherePart = $wherePart . " AND space_total <= " . $sq_all_do . "";
}
if (isset($flat_floor_ot) && $flat_floor_ot) {
$wherePart = $wherePart . " AND flat_floor >= " . $flat_floor_ot . "";
}
if (isset($flat_floor_do) && $flat_floor_do) {
$wherePart = $wherePart . " AND flat_floor <= " . $flat_floor_do . "";
}
if (isset($period) && $period > 0) {
$wherePart = $wherePart . " AND building_id in (select bds.id from buildings bds where UNIX_TIMESTAMP(bds.deadline) <=" . $period . ") ";
}
$rez = mysql_query($sql. $wherePart);
return mysql_result($rez, 0);
}
/**
* @param $blockId - Ид блока
* @param $roomTypeIds - типы квартир
* @param $from - начало пйджига
* @param $to - конец пейджига
* @return Apartment[]
*/
public static function search($blockId, $roomTypeIds, $decTypeId, $from, $to)
{
$sql = "SELECT * FROM apartments ";
$wherePart = "WHERE block_id = '$blockId' ";
if (isset($roomTypeId) && $roomTypeId) {
$wherePart = $wherePart . " AND room_type_id in (" . implode(",", $roomTypeIds) . ")";
}
if (isset($decTypeId) && $decTypeId) {
$wherePart = $wherePart . " AND decoration_id = " . $decTypeId . "";
}
$rez = mysql_query($sql . $wherePart. " LIMIT $from, $to");
$apartments = array();
if (mysql_num_rows($rez) > 0) {
while ($dbObject = mysql_fetch_assoc($rez)) {
$apartment = new Apartment();
$apartment->newRecord = false;
$apartment->id = $dbObject['id'];
$apartment->number = $dbObject['number'];
$apartment->block_id = $dbObject['block_id'];
$apartment->building_id = $dbObject['building_id'];
$apartment->room_type_id = $dbObject['room_type_id'];
$apartment->space_total = $dbObject['space_total'];
$apartment->space_room = $dbObject['space_room'];
$apartment->space_kitchen = $dbObject['space_kitchen'];
$apartment->space_balcony = $dbObject['space_balcony'];
$apartment->space_corridor = $dbObject['space_corridor'];
$apartment->space_watercloset = $dbObject['space_watercloset'];
$apartment->height = $dbObject['height'];
$apartment->section = $dbObject['section'];
$apartment->flat_type_id = $dbObject['flat_type_id'];
$apartment->decoration_id = $dbObject['decoration_id'];
$apartment->subsidy = $dbObject['subsidy'];
$apartment->creditend = $dbObject['creditend'];
$apartment->price = $dbObject['price'];
$apartment->discount_price = $dbObject['discount_price'];
$apartment->base_price = $dbObject['base_price'];
$apartment->meter_price = $dbObject['meter_price'];
$apartment->discount_meter_price = $dbObject['discount_meter_price'];
$apartment->flat_floor = $dbObject['flat_floor'];
$apartment->image = $dbObject['image'];
$apartment->image2 = $dbObject['image2'];
$apartment->user_id = $dbObject['user_id'];
$apartment->fee = $dbObject['fee'];
$apartment->created_at = $dbObject['created_at'];
$apartment->updated_at = $dbObject['updated_at'];
$apartments[] = $apartment;
}
}
return $apartments;
}
public function getAvailablePayments()
{
$res = [];
if (!$this->creditend) {
$res[] = 'Рассрочка';
}
if (!$this->subsidy) {
$res[] = 'Субсидии';
}
return $res;
}
}