1. 程式人生 > >tp5中訂單列表多條件篩選及excel匯出

tp5中訂單列表多條件篩選及excel匯出

檢視層:



<div class="page-content">
<!-- <a class="btn btn-success" href="{:url('order/addorder')}">新增商品</a> -->
<!-- /section:settings.box -->
<div class="page-content-area">

<a href="{:url('order/order',array('s'=>''))}" class="btn btn-white btn-info btn-bold">

全部
</a>


<a href="{:url('order/order',array('s'=>'5'))}" class="btn btn-white btn-info btn-bold">
未付款
</a>


<a href="{:url('order/order',array('s'=>'1'))}" class="btn btn-white btn-info btn-bold">
已付款
</a>
<a href="{:url('order/order',array('s'=>'3'))}" class="btn btn-white btn-info btn-bold">
已發貨
</a>

<a href="{:url('order/order',array('s'=>'4'))}" class="btn btn-white btn-info btn-bold">
已收貨
</a>
<a href="{:url('order/Excelout',array('start'=>"$start",'end'=>"$end",'s'=>"$s"))}" class="btn btn-white btn-info btn-bold">
Excel匯出
</a>
<form action="{:url('order/order')}" method="post">
<input type="hidden" name="s" value="{$s}"/>
<div class="col-xs-12 col-sm-3">
<!-- <label for="id-date-picker-1">時間段選擇日期表單</label> -->
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-calendar bigger-110"></i>
</span>
<input class="form-control" type="text" name="date-range-picker" id="id-date-range-picker-1" value=""/>
</div>

</div>
<input type="submit" class="btn btn-success" value="搜尋">
</form>
<div class="row">
<div class="col-xs-12">
<!-- PAGE CONTENT BEGINS -->
<div class="row">
<div class="col-xs-12">
<table id="sample-table-1" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>下單時間</th>
<th>支付時間</th>
<th>訂單號</th>
<th>商品名稱</th>
<th>使用者暱稱</th>
<th>支付金額</th>
<th>訂單狀態</th>
<th>操作</th>
</tr>
</thead>


<tbody>
{foreach $data as $vo} 
<tr>
<td>

<?php echo date("Y-m-d",$vo['o_cratetime']); ?>

</td>
<td>
{if condition="$vo['o_paytime']"}
<?php echo date("Y-m-d",$vo['o_paytime']); ?>
{else/}
未支付
{/if}
</td>
<td>{$vo.o_number}</td>
<td>{$vo.g_name}</td>
<td>

{$vo.u_nickname}

</td>
<td>
{if condition="$vo.o_status>=1&&$vo.o_status!=10"}
{$vo.o_price}
{/if}
</td>


<td>
{if condition="$vo.o_status==5"}
<button class="btn-info">未付款</button>
{elseif condition="$vo.o_status==1"/}
<button class="btn-danger">已付款</button>
{elseif condition="$vo.o_status==2"/}
<button class="btn-warning">未發貨</button>
{elseif condition="$vo.o_status==3"/}
<button class="btn-success">已發貨</button>
{else/}
<button class="btn-info">已收貨</button>
{/if}
</td>

<td>
<div class="hidden-sm hidden-xs btn-group">
{if condition="$vo.o_status==1"}
<a class="btn btn-xs btn-info" href="{:url('order/express',array('id'=>$vo.o_id))}">
發貨
</a>
{/if}
<a class="btn btn-xs btn-info" href="{:url('order/xiangqing',array('id'=>$vo.o_id))}">
詳情
</a>

<a class="btn btn-xs btn-danger" href="{:url('order/delOrder',array('id'=>$vo.o_id))}">
刪除
<!-- <i class="ace-icon fa fa-trash-o bigger-120"></i> -->
</a>
</div>
</td>
</tr>
{/foreach}
</tbody>
</table>
{$pagefoot}
</div><!-- /.span -->
</div><!-- /.row -->
<div class="col-sm-6" style="text-align:center;">



<div>
<ul class="pagination">
<!-- <li class="disabled">
<a href="#">首頁</a>
</li> -->
<!-- <li class="disabled">
<a href="#">
<i class="ace-icon fa fa-angle-double-left"></i>
</a>
</li> -->



<!-- 
<li>
<a href="#">
<i class="ace-icon fa fa-angle-double-right"></i>
</a>
</li>
<li class="">
<a href="#">尾頁</a>
</li> -->
</ul>
</div>

</div>
</div><!-- /.col -->
</div><!-- /.row -->
</div><!-- /.page-content-area -->

</div>


<script src="__PUBLIC__/static/admin/js/bootstrap.min.js"></script>


<script src="__PUBLIC__/static/admin/js/jquery.dataTables.min.js"></script>
<script src="__PUBLIC__/static/admin/js/jquery.dataTables.bootstrap.js"></script>




<script src="__PUBLIC__/static/admin/js/jquery-ui.custom.min.js"></script>
<script src="__PUBLIC__/static/admin/js/jquery.ui.touch-punch.min.js"></script>
<script src="__PUBLIC__/static/admin/js/chosen.jquery.min.js"></script>
<script src="__PUBLIC__/static/admin/js/fuelux/fuelux.spinner.min.js"></script>
<script src="__PUBLIC__/static/admin/js/date-time/bootstrap-datepicker.min.js"></script>
<script src="__PUBLIC__/static/admin/js/date-time/bootstrap-timepicker.min.js"></script>
<script src="__PUBLIC__/static/admin/js/date-time/moment.min.js"></script>
<script src="__PUBLIC__/static/admin/js/date-time/daterangepicker.min.js"></script>
<script src="__PUBLIC__/static/admin/js/date-time/bootstrap-datetimepicker.min.js"></script>
<script src="__PUBLIC__/static/admin/js/bootstrap-colorpicker.min.js"></script>
<script src="__PUBLIC__/static/admin/js/jquery.knob.min.js"></script>
<script src="__PUBLIC__/static/admin/js/jquery.autosize.min.js"></script>
<script src="__PUBLIC__/static/admin/js/jquery.inputlimiter.1.3.1.min.js"></script>
<script src="__PUBLIC__/static/admin/js/jquery.maskedinput.min.js"></script>
<script src="__PUBLIC__/static/admin/js/bootstrap-tag.min.js"></script>
<script src="__PUBLIC__/static/admin/js/typeahead.jquery.min.js"></script>
<script src="__PUBLIC__/static/admin/js/ace-extra.min.js"></script>
<!-- ace scripts -->
<script src="__PUBLIC__/static/admin/js/ace-elements.min.js"></script>

<script src="__PUBLIC__/static/admin/js/ace.min.js"></script>


控制器:

// 訂單列表
public function order(){
$start='1';
$end='2';
if(input("date-range-picker")){
$time = input("date-range-picker");
$time = explode('-',$time);
$time[0] = str_replace("/","-",$time[0]);
$time[0] = explode('-',$time[0]);
$start = trim($time[0][2])."-".trim($time[0][0])."-".trim($time[0][1])." 00:00:01";
$time[1] = str_replace("/","-",$time[1]);
$time[1] = explode('-',$time[1]);
$end = trim($time[1][2])."-".trim($time[1][0])."-".trim($time[1][1])." 23:59:59";
$start = strtotime($start);
$end = strtotime($end);
$where['o_cratetime']  = ['between',[$start,$end]];
$size = Config("size");
$s = input("s")?input("s"):null;
if(request()->isGet('search')){
            $search=input('search');                
        }else{ 
            $search = "";
        }
        if(is_null($s)){
        $where['o_delete'] = 0;
}else{
        $where['o_delete'] = 0;
    $where['o_status'] = $s;
        }
        $orderdata=Db::table("dnjk_order")->alias('O')->field("O.*,U.*,G.*,H.*")
->join("user U","O.o_u_id=U.u_id","LEFT")
->join("goods G","O.o_g_id=G.g_id","LEFT")
->join("user_healthy H","O.o_id=H.h_oid","LEFT")
->where("o_number","like","%".$search."%")
->where($where)
->order("o_id desc")
->paginate($size,false,[
'type'     => 'Bootstrap',
'var_page' => 'page',
]);
        $pagefoot=$orderdata->render();
        $this->assign(["data"=>$orderdata,"pagefoot"=>$pagefoot,"search"=>$search,"s"=>$s,"start"=>$start,"end"=>$end]);
return $this->fetch();
}else{
$size = Config("size");
$s = input("s")?input("s"):null;
if(request()->isGet('search')){
            $search=input('search');                
        }else{ 
            $search = "";
        }
        if(is_null($s)){
        $where['o_delete'] = 0;
}else{
        $where['o_delete'] = 0;
        $where['o_status'] = $s;
   
        }
        $orderdata=Db::table("dnjk_order")->alias('O')->field("O.*,U.*,G.*,H.*")
->join("user U","O.o_u_id=U.u_id","LEFT")
->join("goods G","O.o_g_id=G.g_id","LEFT")
->join("user_healthy H","O.o_id=H.h_oid","LEFT")
->where("o_number","like","%".$search."%")
->where($where)
->order("o_id desc")
->paginate($size,false,[
'type'     => 'Bootstrap',
'var_page' => 'page',
]);
        $pagefoot=$orderdata->render();
        $this->assign(["data"=>$orderdata,"pagefoot"=>$pagefoot,"search"=>$search,"s"=>$s,"start"=>$start,"end"=>$end]);
return $this->fetch();
}

}


//Excel匯出
public function Excelout(){
$start=input("start")?input("start"):1;
$end=input("end")?input("end"):2;
if($start!=1 && $end!=2){
$where['o_cratetime']  = ['between',[$start,$end]];
}
$size = Config("size");
$s = input("s")?input("s"):null;
if(request()->isGet('search')){
            $search=input('search');                
        }else{ 
            $search = "";
        }
        if(is_null($s)){
        $where['o_delete'] = 0;
}else{
        $where['o_delete'] = 0;
    $where['o_status'] = $s;
        }
        $res=Db::table("dnjk_order")
        ->alias('O')
        ->field("O.*,U.*,G.*,H.*")
        ->join("user U","O.o_u_id=U.u_id","LEFT")
->join("goods G","O.o_g_id=G.g_id","LEFT")
->join("user_healthy H","O.o_id=H.h_oid","LEFT")
->where("o_number","like","%".$search."%")
->where($where)
->order("o_id desc")
->select();
        foreach ($res as $key => $value) {
        $w3['h_oid'] = $value['h_oid'];
        $res[$key]['healty'] = Db::table("dnjk_user_healthy")->where($w3)->find();
        }
import('PHPExcel.PHPExcel', EXTEND_PATH);
        $phpexcel=new \PHPExcel();//例項化PHPExcel類物件,方便操作即將生成的excel表格
        $phpexcel->setActiveSheetIndex(0);//選中我們生成的excel檔案的第一張工作表
        $sheet=$phpexcel->getActiveSheet();//獲取到選中的工作表,方面後面資料插入操作
        //此處設定的是生成的excel表的第一行標題
        $arr=[
            'o_cratetime'=>'下單時間',
            'o_paytime'=>'支付時間',
            'o_number'=>'訂單號',
            'g_name'=>'商品名稱',
            'u_nickname'=>'使用者暱稱',
            'o_price'=>'支付金額',
            'o_status'=>'訂單狀態',
            'o_username'=>'收貨人姓名',
            'o_telphone'=>'收貨人手機號',
            'o_address'=>'收貨人所在地區',
            'o_addressinfo'=>'收貨人詳細地址',
            'h_username'=>'使用者姓名',
            'h_address'=>'使用者所在地',
            'h_physical'=>'持卡人身體狀況',
            'h_telephone'=>'聯絡電話',
            'h_IDcard'=>'卡主身份證',
            'h_weight'=>'體重',
            'h_height'=>'身高',
            'h_blood_type'=>'血型',
            'h_surgery'=>'手術史',
            'h_allergy'=>'藥物過敏史',
            'h_inherit'=>'遺傳病家族史',
            'h_deformity'=>'殘疾情況',
        ];
         array_unshift($res,$arr);
         //將我們上面手動設定的標題資訊放到陣列中,作為第一行寫入資料表
        $currow=0;//因為我們生成的excel表格的行數是從1開始,所以我們預先設定好一個變數,供下面foreach迴圈的$k使用
        $sheet->setCellValue('A1',$arr['o_cratetime'])
                  ->setCellValue('B1',$arr['o_paytime'])
                  ->setCellValue('C1',$arr['o_number'])
                  ->setCellValue('D1',$arr['g_name'])
              ->setCellValue('E1',$arr['u_nickname'])
                  ->setCellValue('F1',$arr['o_price'])
                  ->setCellValue('G1',$arr['o_status'])
              ->setCellValue('H1',$arr['o_username'])
                  ->setCellValue('I1',$arr['o_telphone'])
                  ->setCellValue('J1',$arr['o_address'])
              ->setCellValue('K1',$arr['o_addressinfo'])
              ->setCellValue('L1',$arr['h_username'])
                  ->setCellValue('M1',$arr['h_address'])
              ->setCellValue('N1',$arr['h_physical'])
                  ->setCellValue('O1',$arr['h_telephone'])
                  ->setCellValue('P1',$arr['h_IDcard'])
              ->setCellValue('Q1',$arr['h_weight'])
                  ->setCellValue('R1',$arr['h_height'])
                  ->setCellValue('S1',$arr['h_blood_type'])
              ->setCellValue('T1',$arr['h_surgery'])
                  ->setCellValue('U1',$arr['h_allergy'])
                  ->setCellValue('V1',$arr['h_inherit'])
              ->setCellValue('W1',$arr['h_deformity'])
            ;
        foreach ($res as $k => $v) {
        if($k==0){
        continue;
        }
        if($v['o_paytime']==""){
                  $v['o_paytime']="未支付";
                  }else{
                  $v['o_paytime']=date("Y-m-d H:i:s",$v['o_paytime']);
                  }
            if($v['o_status']==5){
            $v['o_status']="未付款";
            }elseif($v['o_status']==1){
            $v['o_status']="已付款";
            }
            elseif($v['o_status']==2){
            $v['o_status']="未發貨";
            }
            elseif($v['o_status']==3){
            $v['o_status']="已發貨";
            }
            elseif($v['o_status']==4){
            $v['o_status']="已收貨";
            }else{
            $v['o_status']="已取消";
            }
            if($v['healty']['h_allergy']==0){
            $v['healty']['h_allergy']="無";
            }elseif($v['healty']['h_allergy']==1){
            $v['healty']['h_allergy']="青黴素過敏";
            }elseif($v['healty']['h_allergy']==2){
            $v['healty']['h_allergy']="磺胺";
            }else{
            $v['healty']['h_allergy']="鏈黴素";
            }
            if($v['healty']['h_inherit']==0){
            $v['healty']['h_inherit']="高血壓";
            }elseif($v['healty']['h_inherit']==1){
            $v['healty']['h_inherit']="糖尿病";
            }
            elseif($v['healty']['h_inherit']==2){
            $v['healty']['h_inherit']="慢性阻塞性肺疾病";
            }
            elseif($v['healty']['h_inherit']==3){
            $v['healty']['h_inherit']="冠心病";
            }
            elseif($v['healty']['h_inherit']==4){
            $v['healty']['h_inherit']="結核病";
            }
            elseif($v['healty']['h_inherit']==5){
            $v['healty']['h_inherit']="肝炎";
            }
            elseif($v['healty']['h_inherit']==6){
            $v['healty']['h_inherit']="腦卒中";
            }
            elseif($v['healty']['h_inherit']==7){
            $v['healty']['h_inherit']="重性精神疾病";
            }
            elseif($v['healty']['h_inherit']==8){
            $v['healty']['h_inherit']="噁心腫瘤";
            }
            elseif($v['healty']['h_inherit']==9){
            $v['healty']['h_inherit']="其他法定傳染病";
            }
            elseif($v['healty']['h_inherit']==10){
            $v['healty']['h_inherit']="其他";
            }else{
            $v['healty']['h_inherit']="無";
            }
            if($v['healty']['h_deformity']==0){
            $v['healty']['h_deformity']="無疾病";
            }elseif($v['healty']['h_deformity']==1){
            $v['healty']['h_deformity']="視力殘疾";
            }
            elseif($v['healty']['h_deformity']==2){
            $v['healty']['h_deformity']="聽力殘疾";
            }
            elseif($v['healty']['h_deformity']==3){
            $v['healty']['h_deformity']="語言殘疾";
            }
            elseif($v['healty']['h_deformity']==4){
            $v['healty']['h_deformity']="肢體殘疾";
            }
            elseif($v['healty']['h_deformity']==5){
            $v['healty']['h_deformity']="智力殘疾";
            }
            elseif($v['healty']['h_deformity']==6){
            $v['healty']['h_deformity']="精神殘疾";
            }else{
            $v['healty']['h_deformity']="其他殘疾";
            }
            $currow=$k+1;//表示從生成的excel表格的第一行開始插入資料
            $sheet->setCellValue('A'.$currow,date("Y-m-d H:i:s",$v['o_cratetime']))
                  ->setCellValue('B'.$currow,$v['o_paytime'])
                  ->setCellValue('C'.$currow,$v['o_number'])
                  ->setCellValue('D'.$currow,$v['g_name'])
              ->setCellValue('E'.$currow,$v['u_nickname'])
                  ->setCellValue('F'.$currow,$v['o_price'])
                  ->setCellValue('G'.$currow,$v['o_status'])
              ->setCellValue('H'.$currow,$v['o_username'])
                  ->setCellValue('I'.$currow,$v['o_telphone'])
                  ->setCellValue('J'.$currow,$v['o_address'])
              ->setCellValue('K'.$currow,$v['o_addressinfo'])
                  ->setCellValue('L'.$currow,$v['healty']['h_username'])
                  ->setCellValue('M'.$currow,$v['healty']['h_address'])
              ->setCellValue('N'.$currow,$v['healty']['h_physical'])
                  ->setCellValue('O'.$currow,$v['healty']['h_telephone'])
                  ->setCellValue('P'.$currow,$v['healty']['h_IDcard'])
              ->setCellValue('Q'.$currow,$v['healty']['h_weight'].'kg')
                  ->setCellValue('R'.$currow,$v['healty']['h_height'].'cm')
                  ->setCellValue('S'.$currow,$v['healty']['h_blood_type'].'型')
              ->setCellValue('T'.$currow,$v['healty']['h_surgery'])
                  ->setCellValue('U'.$currow,$v['healty']['h_allergy'])
                  ->setCellValue('V'.$currow,$v['healty']['h_inherit'])
              ->setCellValue('W'.$currow,$v['healty']['h_deformity'])
            ;
        }
        header('Content-Type:application/octet-stream');
        header('Accept-Ranges:bytes');
        header('Content-Type: application/vnd.ms-excel');//設定下載前的頭資訊
        header('Content-Disposition: attachment;filename="ceshi.xlsx"');
        header('ache-Control: max-age=0');
        header('Pragma: no-cache');
        header('Expires: 0');
        $phpwriter=new \PHPExcel_Writer_Excel2007($phpexcel);
        //此處的2007代表的是高版本的excel表格
        $phpwriter->save('php://output');//生成並下載excel表格
        return;


}