ApiBill.php 14.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
<? include $_SERVER["DOCUMENT_ROOT"] . "/common/classes/ApiBase.php" ;?>
<?

if (! class_exists("ApiBill"))
{

	class ApiBill extends ApiBase
	{

		function __construct($req)
		{
			parent::__construct($req);
		}

		//세부 사용량
		function getUsingData() {
		
			$no = $this->req["number"] ;
			
			$sql = "
				SELECT
					BC.electricity_usage_lm
					, BC.gas_usage_lm
					, BC.water_usage_lm
					, BC.electricity_usage
					, BC.gas_usage
					, BC.water_usage
				FROM tbl_bill_chunggu BC
				WHERE BC.no = '{$no}'
			";
			return $this->getRow($sql) ;
		
			
		}
		
		//건물이름 방이름 
		function getRoomName() {
			
			$group_fk = $this->appUser["group_fk"];
			$userNumber = $this->appUser["no"];
				
			$sql = "
			SELECT
				(SELECT `name` FROM tbl_room WHERE user_fk = '{$userNumber}' LIMIT 0, 1) AS roomName
				, (SELECT `name` FROM tbl_building WHERE group_fk = '{$group_fk}' LIMIT 0, 1) AS buildingName
			FROM DUAL
			";
			return $this->getRow($sql) ;
		}
		
		//공실율 이력
		function getVacantList() {
			
			$userNumber = $this->appUser["no"] ;
/* 
			$sql = "
				SELECT 
					D.date
					, R.*
				FROM (
					SELECT DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m') AS `date`
					UNION
					SELECT DATE_FORMAT(NOW() - INTERVAL 2 MONTH, '%Y-%m') AS `date`
					UNION
					SELECT DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%Y-%m') AS `date`
				) D
				LEFT JOIN 
				(
					SELECT
						DATE_FORMAT(history_month, '%Y-%m') AS `date`
						, (SELECT COUNT(*) FROM tbl_room_history WHERE building_fk=(SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}') ORDER BY pno DESC LIMIT 1) AS room_cnt
						, (SELECT COUNT(*) FROM tbl_room_history WHERE building_fk=(SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}') AND contract_status='RD' ORDER BY pno DESC LIMIT 1) AS resident_cnt
						, (SELECT COUNT(*) FROM tbl_room_history WHERE building_fk=(SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}') AND DATE_FORMAT(entering_due_date, '%Y-%m') = DATE_FORMAT(`history_month`, '%Y-%m') ORDER BY pno DESC LIMIT 1) AS entering_cnt
					FROM tbl_room_history
				) R
				ON D.date = R.date
				";
			 */
			$sql = "
				SELECT
					DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%m') AS `date3`
					, (SELECT COUNT(*) FROM tbl_room_history WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}') AND `status` = 'Y' AND DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%Y-%m') =  DATE_FORMAT(`history_month`, '%Y-%m')) AS room_cnt3
					, (SELECT COUNT(*) FROM tbl_room_history WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}') AND (contract_status='EP' OR contract_status='UC') AND `status` = 'Y' AND DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%Y-%m') =  DATE_FORMAT(`history_month`, '%Y-%m')) AS vacant_cnt3
					, DATE_FORMAT(NOW() - INTERVAL 2 MONTH, '%m') AS `date2`
					, (SELECT COUNT(*) FROM tbl_room_history WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}') AND `status` = 'Y' AND DATE_FORMAT(NOW() - INTERVAL 2 MONTH, '%Y-%m') =  DATE_FORMAT(`history_month`, '%Y-%m')) AS room_cnt2
					, (SELECT COUNT(*) FROM tbl_room_history WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}') AND (contract_status='EP' OR contract_status='UC') AND `status` = 'Y' AND DATE_FORMAT(NOW() - INTERVAL 2 MONTH, '%Y-%m') =  DATE_FORMAT(`history_month`, '%Y-%m')) AS vacant_cnt2
					, DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%m') AS `date1`
					, (SELECT COUNT(*) FROM tbl_room_history WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}') AND `status` = 'Y' AND DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m') =  DATE_FORMAT(`history_month`, '%Y-%m')) AS room_cnt1
					, (SELECT COUNT(*) FROM tbl_room_history WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}') AND (contract_status='EP' OR contract_status='UC') AND `status` = 'Y' AND DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m') =  DATE_FORMAT(`history_month`, '%Y-%m')) AS vacant_cnt1
				FROM DUAL			
				";			
			return $this->getRow($sql) ;
		}
		
		//특약 불러오기 
		function getSpecialContract() {
			
			$userNumber = $this->appUser["no"] ;
			
			$sql = "
				SELECT R.special_contract
				FROM tbl_room R
				WHERE user_fk = '{$userNumber}'
				";
			return $this->getRow($sql) ;
		}
		
		//호실리스트
		function getRoomList() {
			
			$userNumber = $this->appUser["no"] ;
			
			$sql = "
				SELECT 
					R.*
					, IFNULL((SELECT leaving_due_date FROM tbl_room WHERE `no`=R.no AND leaving_due_date BETWEEN DATE(NOW()) AND DATE(NOW() -INTERVAL -1 WEEK)), 0) AS isLeaving
					, IFNULL((SELECT entering_due_date FROM tbl_room WHERE `no`=R.no AND entering_due_date BETWEEN DATE(NOW()) AND DATE(NOW() -INTERVAL -1 WEEK)), 0) AS isEntering
				FROM tbl_room R
				WHERE R.building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$userNumber}' )
				AND R.status = 'Y'
				ORDER BY R.insert_date DESC 
				";
			$list = $this->getArray($sql) ;
			
			return $list ; 
		}
		
		//호실별 정보
		function getRoomData() {

			$no = $_REQUEST["no"] ;
			
			$sql = "
				SELECT 
					R.*
					, DATE(R.contract_date) AS contractDate
					, DATE(R.contract_expire_date) AS contractExpireDate
					, DATE(R.move_in_date) AS moveInDate
					, DATE(R.entering_due_date) As enteringDueDate
					, DATE(R.leaving_due_date) As leavingDueDate
				FROM tbl_room R WHERE no = '{$no}' AND status='Y'
				";
			$data = $this->getRow($sql) ;
			
			return $data ;
			
		}
		
		
		//비용 청구
		function getRequestData() {
			
			$no = $_REQUEST["no"] ;
			
			$sql = "
				SELECT 
					RC.* 
					, DATE(RC.request_date) AS requestDate
					, DATE(RC.handled_date) AS handledDate
				FROM tbl_request_charge RC
				WHERE RC.no = '{$no}'
				ORDER BY RC.	no DESC		
				";
			$data = $this->getRow($sql) ;
			
			return $data ;
			
		}
		
		//임대료 내역
		function getRentList() {
			
			$no = $this->appUser["no"] ;
			
			$sql = "
				SELECT 
					R.no
					, R.name
					, RC.* 
					, DATE(RC.month) AS monthDate
				FROM tbl_rent_chunggu RC
				JOIN tbl_room R ON R.no = RC.room_fk
 				WHERE R.building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$no}')
 				AND DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%Y%m%d') <= DATE_FORMAT(RC.month, '%Y%m%d')
				AND RC.status = 'Y'
				ORDER BY RC.NO DESC		
			";
			$rentList = $this->getArray($sql) ; 
			
			$sql = "
				SELECT
					R.name
					, R.payment_owner
					, RC.*
					, DATE(RC.request_date) AS requestDate
				FROM tbl_request_charge RC
				JOIN tbl_room R ON R.no = RC.room_fk
				WHERE R.building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$no}')
				AND DATE_FORMAT(NOW() - INTERVAL 3 MONTH, '%Y%m%d') <= DATE_FORMAT(RC.request_date, '%Y%m%d')
				AND RC.status = 'Y'
				ORDER BY RC.no DESC
			";
			$requestList = $this->getArray($sql) ; 
			
			$retArr = array(
					"rentList" => $rentList
					, "requestList" => $requestList
			) ;
			
			return $retArr ;
			
		}
		
		//건물정보
		function getBuildingInfo(){
			
			$no = $this->appUser["no"] ;

			if($this->IS_DEBUG)
			{
				$logData = "Api : getBuildingInfo // userNumber : {$no} ";
				LogUtil::writeFileLog($this->logPath, $logData);
			}
			
			$sql = "
				SELECT
					* 
					,  DATE(expire_date) AS expireDate
					, (SELECT COUNT(*) FROM tbl_room WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$no}' AND `status` = 'Y') AND `status` = 'Y') AS room_cnt
					, (SELECT COUNT(*) FROM tbl_room WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$no}' AND `status` = 'Y') AND (contract_status='RD' OR contract_status='CE') AND `status` = 'Y') AS resident_cnt
					, (SELECT COUNT(*) FROM tbl_room WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$no}' AND `status` = 'Y') AND (contract_status='EP' OR contract_status='UC') AND `status` = 'Y') AS vacant_cnt
					, (SELECT COUNT(*) FROM tbl_room WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$no}' AND `status` = 'Y') AND DATE_FORMAT(leaving_due_date, '%Y%m%d')>DATE_FORMAT(NOW(), '%Y%m%d') AND DATE_FORMAT(leaving_due_date, '%Y%m')=DATE_FORMAT(NOW(), '%Y%m') AND status='Y') AS leaving_cnt
					, (SELECT COUNT(*) FROM tbl_room WHERE building_fk = (SELECT `no` FROM tbl_building WHERE vip_fk = '{$no}' AND `status` = 'Y') AND DATE_FORMAT(entering_due_date, '%Y%m%d')>DATE_FORMAT(NOW(), '%Y%m%d') AND DATE_FORMAT(entering_due_date, '%Y%m')=DATE_FORMAT(NOW(), '%Y%m') AND status='Y') AS entering_cnt
				FROM tbl_building WHERE vip_fk = '{$no}' AND status='Y'
				";
			$data = $this->getRow($sql) ;
			
			return $data ;
			
		}
		
		//퇴실정산서 데이터
		function getCalculateData() {
			
			$userNumber = $this->appUser["no"];
			$group_fk = $this->appUser["group_fk"];
			
			$sql = "
				SELECT EC.* 
					, (SELECT balance FROM tbl_room WHERE user_fk = '{$userNumber}' LIMIT 0, 1) AS roomBalance
					, R.contractor_account_no	
					, R.contractor_account_bank
					, R.contractor_account_owner
					, R.contractor_name
					, DATE(R.leaving_due_date) AS leaving_due_date
					, (SELECT `name` FROM tbl_room WHERE user_fk = '{$userNumber}' LIMIT 0, 1) AS roomName
					, (SELECT `name` FROM tbl_building WHERE group_fk = '{$group_fk}' LIMIT 0, 1) AS buildingName
				FROM tbl_end_chunggu EC
				JOIN tbl_room_history R ON EC.room_fk = R.pno
				WHERE R.user_fk = '{$userNumber}'
				AND EC.status = 'Y'
				ORDER BY EC.no DESC
				";
			$data = $this->getRow($sql) ;
			
			$sql = "
					SELECT 
						MONTH(`month`) AS `month`
						, EC.electricity_usage_lm
						, EC.gas_usage_lm
						, EC.water_usage_lm
						, EC.electricity_usage
						, EC.gas_usage
						, EC.water_usage
					FROM tbl_end_chunggu EC
					WHERE user_fk = '{$userNumber}' 
					AND MONTH(`month`) IN(MONTH(NOW()), MONTH(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)))
					ORDER BY EC.no DESC					
					LIMIT 0, 1
				";
			$list = $this->getRow($sql) ;
			
			$retArr = array(
				"data" => $data,
				"list" => $list		
			);
			
			return $retArr ;
		}

		
		//월세 리스트
		function getMonthlyList() {
			
			$userNumber = $this->appUser["no"];
			$group_fk = $this->appUser["group_fk"];
			$year = $this->req["year"] == "" ? date("Y") : $this->req["year"] ;

			if($this->IS_DEBUG)
			{
				$logData = "Api : getMonthlyList // userNumber : {$userNumber} //group_fk : {$group_fk} // year : {$year}";
				LogUtil::writeFileLog($this->logPath, $logData);
			}
			
			$where = "" ;
			
			if($year != ""){
				$where = "AND YEAR(`month`) = '{$year}'" ;
			}
			
			$sql = "
				SELECT 
					R.name AS roomName
					, (SELECT `name` FROM tbl_building WHERE group_fk = '{$group_fk}' LIMIT 0, 1) AS buildingName
					, RC.* 
					, DATE_FORMAT(RC.month, '%Y.%m.%d') AS monthDate
				FROM tbl_rent_chunggu RC
				JOIN tbl_room_history R ON RC.room_fk = R.pno
				WHERE R.user_fk = '{$userNumber}' 
				AND RC.status = 'Y'
				AND rNO = (SELECT no FROM tbl_room WHERE user_fk='{$userNumber}')
				{$where}
				ORDER BY RC.NO DESC
				";
			$list = $this->getArray($sql) ;
			
			$sql = "
				SELECT
				(SELECT `name` FROM tbl_room WHERE user_fk = '{$userNumber}' LIMIT 0, 1) AS roomName
				, (SELECT `name` FROM tbl_building WHERE group_fk = '{$group_fk}' LIMIT 0, 1) AS buildingName
				FROM DUAL
			";
			$data = $this->getRow($sql) ;
			
			$retArr = array(
				"list" => $list 
				, "data" => $data	
			);
			
			return $retArr ;
			
		}
		
		//월세 상세
		function getMonthlyData() {
			
			$no = $_REQUEST["no"] ;

			$sql = "
				SELECT 
					R.name
					, R.payment_owner
					, (SELECT
							CASE WHEN B.is_paid = '0' THEN B.sum_after_payment ELSE 0 END AS sum_after_payment
						FROM
						(
							SELECT * FROM tbl_rent_chunggu
							WHERE rNo = (SELECT rNo FROM tbl_rent_chunggu RC WHERE RC.NO = '{$no}')
							ORDER BY NO DESC
						) B
						WHERE B.NO < '{$no}'
						ORDER BY B.NO DESC
						LIMIT 0, 1
						) AS sum_before_payment
					, RC.*
					, DATE_FORMAT(RC.month, '%Y.%m.%d') AS monthDate
					, B.month_account_no
					, B.month_account_bank
					, B.month_account_owner
				FROM tbl_rent_chunggu RC
				JOIN tbl_room_history R ON RC.room_fk = R.pno
				JOIN tbl_building B ON R.building_fk = B.no
				WHERE RC.`NO` = '{$no}'
				LIMIT 0, 1	
				";
			$data = $this->getRow($sql) ;

			return $data ;
		}
		
		//공과금 리스트
		function getUtilityList() {
			
			 $userNumber = $this->appUser["no"] ;
			 $group_fk = $this->appUser["group_fk"] ;
			 $year = $this->req["year"] == "" ? date("Y") : $this->req["year"] ;
			 
			 $where = "" ;
			 	
			 if($year != ""){
			 	$where = "AND YEAR(`month`) = '{$year}'" ;
			 }
			 
			 $sql = "
			 		SELECT BC.*
						, DATE_FORMAT(BC.month, '%Y.%m.%d') AS monthDate 
					FROM tbl_bill_chunggu BC
					JOIN tbl_room_history R ON R.pno = BC.room_fk
					WHERE R.user_fk = '{$userNumber}'
					AND BC.status = 'Y'
					AND rNO = (SELECT no FROM tbl_room WHERE user_fk='{$userNumber}')
					{$where}
					ORDER BY BC.no DESC
				";
			 $list = $this->getArray($sql) ;
			 
			 $sql = "
			 SELECT
			 (SELECT `name` FROM tbl_room WHERE user_fk = '{$userNumber}' LIMIT 0, 1) AS roomName
			 , (SELECT `name` FROM tbl_building WHERE group_fk = '{$group_fk}' LIMIT 0, 1) AS buildingName
			 FROM DUAL
			 ";
			 $data = $this->getRow($sql) ;
			 	
			 $retArr = array(
			 		"list" => $list
			 		, "data" => $data
			 );
			 	
			 return $retArr ;
			 
		}
		
		//공과금 상세
		function getUtilityData() {
				
			$no = $_REQUEST["no"] ;

			$sql = "
				SELECT
					R.name
					, (SELECT
							CASE WHEN B.is_paid = '0' THEN B.current_month_charge ELSE 0 END AS sum_after_payment
						FROM
						(
							SELECT * FROM tbl_bill_chunggu
							WHERE rNo = (SELECT rNo FROM tbl_bill_chunggu RC WHERE RC.NO = '{$no}')
							ORDER BY NO DESC
						) B
						WHERE B.NO < '{$no}'
						ORDER BY B.NO DESC
						LIMIT 0, 1
						) AS sum_before_payment
					, BC.*
					, DATE_FORMAT(BC.month, '%Y.%m.%d') AS monthDate
					, B.util_account_no
					, B.util_account_bank
					, B.util_account_owner
				FROM tbl_bill_chunggu BC
				JOIN tbl_room_history R ON BC.room_fk = R.pno
				JOIN tbl_building B ON R.building_fk = B.no
				WHERE BC.`NO` = '{$no}'
			";
			$data = $this->getRow($sql) ;
		
			return $data ;
				
		}
		
		
		
	} // 클래스 종료
}
?>