<?php
    $file_name = "입찰중리스트_".date("Y-m-d").".xls";
    Header("Content-type: charset=utf-8");
    Header("Content-type: application/octet-stream");
    header( "Content-type: application/vnd.ms-excel;charset=utf-8" );
    Header("Content-Disposition: attachment; filename=$file_name");
    Header("Pragma: no-cache");
    Header("Expires: 0");
    header( "Content-Description: PHP5 Generated Data" );

	include $_SERVER['DOCUMENT_ROOT'].'/common/lib/comm.php';
	include $_SERVER['DOCUMENT_ROOT'].ADMIN_PATH.'common/auth_chk.php';
    // echo '<table><tr><td>1</td>2</td></tr></table>';
    // exit;



	$sql = "where s.ds_delind='N'";
//	$orderby = "order by s.dt_insert desc";
	$orderby = "order by s.dt_update desc";		//20191207 디비에 업데이트 날짜 기준으로 리스트 정렬하기

	$searchtype = $_REQUEST['searchtype'];
	$searchword = $_REQUEST['searchword'];
	$ds_usertype = $_REQUEST['ds_usertype'];
	$ds_status = $_REQUEST['ds_status'];
	$dt_start = $_REQUEST['dt_start'];
	$dt_end = $_REQUEST['dt_end'];

	$ds_type = "A0";	//A0 : 매입
	$sql = $sql. "	and s.ds_type = '$ds_type' ";

	if($searchword != ""){
		if($searchtype=="" || $searchtype==null){
			$sql = $sql. "	and (s.nm_name like '$searchword%' or a.nm_number like '$searchword%') ";
		}else{
			$sql = $sql. "	and $searchtype like '$searchword%'";
		}
	}

	if($ds_usertype != ""){
		$sql = $sql. "	and s.ds_usertype = '$ds_usertype'";
	}

	if($dt_start != "" && $dt_end != ""){
		$sql = $sql. "	and s.dt_insert between '$dt_start' and '$dt_end' ";
	}

	$sql = $sql. "	and s.ds_status in ('E0','F0')";

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//관계사/지점 Setting
	$admin_ds_company_bp	= $_SESSION['admin_ds_company_bp'];	//관계사
	$admin_ds_branch		= $_SESSION['admin_ds_branch'];		//지점
	$admin_bp_ok			= ($admin_ds_company_bp != "" || $admin_ds_branch != "") ? true : false;	//admin_ds_company_bp 나 admin_ds_branch 값이 하나라도 있으면 관계사로 보고 권한 제한한다. $admin_bp_ok == true 이면 해당 관계사/지점 데이타만 확인 가능하다.

//관계사 일 경우 해당 관계사/지점 데이타만 확인 가능하다.
	if($admin_bp_ok){
		if($admin_ds_company_bp != "" && $admin_ds_branch != ""){
			$sql = $sql. "	and s.ds_company_bp = '$admin_ds_company_bp' and s.ds_branch = '$admin_ds_branch' ";
		}else{	//admin_ds_company_bp 나 admin_ds_branch 값이 하나라도 없으면 데이타 확인 안되게 한다.
			$sql = $sql. "	and 1 = 2 ";
		}
	}

    // $sql.= " and s.cd_sale = '3223'";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	$totalrows = @mysql_result(mysql_query("select count(*) from sale_master s inner join car_master a on (	s.cd_car = a.cd_car ) $sql"),0,0);

?>
<!DOCTYPE html>
<HTML lang="ko">
<head>
<meta charset="UTF-8" />
</head>
<body>
<table border="1">
<tr>
    <td>번호</td>
    <td>판매자</td>
    <td>차종</td>
    <td>모델</td>
    <td>등급</td>
    <td>차량번호</td>
    <td>주행거리</td>
    <td>지역</td>
    <td>차량등록일</td>
    <td>입찰업체</td>
    <td>입찰금액</td>
    <td>입찰담당자</td>
    <td>입찰일</td>
</tr>
<?
if($totalrows>0) {

    $r = mysql_query("SELECT
						a.*
						, b.nm_model
						, c.nm_grade
						, d.nm_brand
						, e.nm_model_sub
						, getCode('car_master','ds_type',a.ds_type) as nm_type
						, getCode('car_master','ds_open',a.ds_open) as nm_open
						, getCode('car_master','ds_sales',a.ds_sales) as nm_sales
						, getCode('car_master','ds_transmission',a.ds_transmission) as nm_transmission
						, getCode('member_master','ds_area',s.ds_area) as nm_area
						, getCode('sale_master','ds_status',s.ds_status) as nm_status
						, s.cd_sale
						, s.nm_name nm_name_sale
						, s.nm_recommend nm_recommend_sale
						, s.ds_status ds_status_sale
						, s.ds_status_contract ds_status_contract_sale
						, getRemainTimeByPhase(".LIMIT_TIME_TENDER.", ".LIMIT_TIME_SELL.", s.dt_approve, now()) as time_limit_sell
                        , (select count(*) from tender_master it where it.cd_sale = s.cd_sale) as tender_cnt
						FROM sale_master s
							inner join car_master a on (s.cd_car = a.cd_car)
							left outer join car_model b on (a.cd_model = b.cd_model)
							left outer join car_grade c on (a.cd_grade = c.cd_grade)
							left outer join car_brand d on (a.cd_brand = d.cd_brand)
							left outer join car_model_sub e on (a.cd_model_sub = e.cd_model_sub)
							$sql $orderby
        ");

    $i=1;
    while($col = mysql_fetch_array($r)) {
        // inner join tender_master t on (s.cd_sale = t.cd_sale)
        $tender_sql = "select t.*
                            , (select nm_company from dealer_master dm where dm.cd_dealer = t.cd_dealer_p)  as nm_company
                            from tender_master t
                            where cd_sale = '$col[cd_sale]'
                                ORDER BY cast(t.nm_price as decimal) desc";
        $tender_res = mysql_query($tender_sql);
        $tender_num = 0;
        while($tender = mysql_fetch_array($tender_res)) {
            if($tender_num == 0) {
?>
<tr>
    <td rowspan="<?=$col[tender_cnt]?>"><?=$i?></td>
    <td rowspan="<?=$col[tender_cnt]?>"><?=$col[nm_name_sale]?></td>
    <td rowspan="<?=$col[tender_cnt]?>"><?=$col[nm_brand]?></td>
    <td rowspan="<?=$col[tender_cnt]?>"><?=$col[nm_model]?></td>
    <td rowspan="<?=$col[tender_cnt]?>"><?=$col[nm_grade]?></td>
    <td rowspan="<?=$col[tender_cnt]?>"><?=$col[nm_number]?></td>
    <td rowspan="<?=$col[tender_cnt]?>"><?=$col[nm_mileage]?>km</td>
    <td rowspan="<?=$col[tender_cnt]?>"><?=$col[nm_area]?></td>
    <td rowspan="<?=$col[tender_cnt]?>"><?=$col[dt_insert]?></td>
    <td><?=$tender[nm_company]?></td>
    <td><?=number_format($tender[nm_price])?>만원</td>
    <td><?=$tender[nm_name]?></td>
    <td><?=$tender[dt_insert]?></td>
</tr>
<?
            }else {
?>
<tr>
    <td><?=$tender[nm_company]?></td>
    <td><?=number_format($tender[nm_price])?>만원</td>
    <td><?=$tender[nm_name]?></td>
    <td><?=$tender[dt_insert]?></td>
</tr>
<?
            }
            $tender_num++;
        }
        $i++;
    }
}else{
?>
<tr>
    <td colspan="13">등록된 정보가 없습니다.</td>
</tr>
<?
}
?>
</table>
</body>
</html>
