<?php

require_once(dirname(dirname(__FILE__)) . '/app.php');
need_manager();


if($_GET){
    
    if(isset($_GET['act']) && ( $_GET['act']=='export_pay' || $_GET['act']=='export_all')){
        if($_GET['act']=='export_pay'){
            $condition = "and order_info.state='pay'";
            $xls = "order_pay";
        }else{
            $xls = "order_all";
        }
        $sql = "SELECT user.username,order_info.*,provider.name as provider_name,provider.channel,provider.brand,fare.name as express_name
                FROM `user`,`order_info`,`provider`,`fare`
                WHERE fare.id=order_info.express_id and user.id = order_info.user_id and order_info.provider_id=provider.id ".$condition."
                ORDER BY order_info.pay_time DESC";
        $order_info = DB::GetQueryResult($sql,false);
        header('Content-Disposition: attachment; filename="'.$xls.'.xls"');
        orderHtml1();
        exit;
    
    }elseif(isset($_GET['act']) && ( $_GET['act']=='export_pay_print' || $_GET['act']=='export_all_print')){
        if($_GET['act']=='export_pay_print'){
            $condition = ' and b.state="pay" ';
            $xls = "order_pay_print";
        }else{
            $xls = "order_all_print";
        }
        (mceil(1.1)==2)? $ceil = "ceil" : $ceil = "";
        
        $sql = 'SELECT b.order_number, c.username, a.item_number, a.size, a.quantity, d.channel, f.season, a.origin, a.discount,
                    '.$ceil.'( a.origin * a.discount ) * a.quantity AS price, a.order_number, b.fare, e.name AS express_name,
                    concat( b.realname, "，", b.mobile, "，，", b.province, b.address, "，", b.zipcode ) AS address, b.remark, b.express_no,
                    g.cost_discount, a.origin * g.cost_discount AS cost_price, a.origin * g.cost_discount * a.quantity AS sum_cost_price
                FROM order_item AS a, order_info AS b, user AS c, provider AS d, fare AS e, item AS f, category AS g
                WHERE a.order_number = b.order_number
                AND b.user_id = c.id
                AND b.provider_id = d.id
                AND b.express_id = e.id
                AND a.item_id = f.id
                AND a.category_id = g.id'.$condition.'
                ORDER BY a.order_number';
        $order_info = DB::GetQueryResult($sql,false);
        header('Content-Disposition: attachment; filename="'.$xls.'.xls"');
        orderHtml2();
        exit;
    }
}

;echo '
';  function orderHtml1(){global $order_info;;echo '<html><meta http-equiv=content-type content="text/html; charset=UTF-8"><body><table border=\'1\'>
        <thead><tr><th>订单号</th><th>会员名</th><th>商品列表</th><th>提供商</th><th>付款日期</th>
            <th>收货人</th><th>手机</th><th>地区</th><th>固话</th><th>邮编</th><th>商品重量</th><th>配送方式</th>
            <th>配送费用</th><th>快递单号</th><th>商品总价</th><th>总金额</th></tr></thead>
        '; if(is_array($order_info)){foreach($order_info AS $index=>$one) {;echo '        <tr>
            <td><a href="'; echo WEB_ROOT."/manage/order_item.php?order_number=".$one['order_number']; ;echo '">'; echo $one['order_number']; ;echo '</a></td>
            <td><a href="#">'; echo $one['username']; ;echo '</a></td>
            <td>'; echo str_replace("<br />","",OOrder::getItemList($one['order_number'])); ;echo '</td>
            <td>'; echo $one['provider_name']."-".$one['brand']."-".$one['channel'] ;echo '</td>
            <td>'; echo date('Y-m-d H:i:s',$one['pay_time']) ;echo '</td>
            <td>'; echo $one['realname'] ;echo '</td>
            <td>'; echo $one['mobile'] ;echo '</td>
            <td>'; echo $one['province'] ;echo '</td>
            <td>'; echo $one['phone'] ;echo '</td>
            <td>'; echo $one['zipcode'] ;echo '</td>
            <td>'; echo $one['weight'] ;echo '</td>
            <td>'; echo $one['express_name'] ;echo '</td>
            <td>'; echo $one['fare'] ;echo '</td>
            <td>'; if($xls=='order_all') ;echo '&nbsp;'; echo strval($one['express_no']);;echo '            </td>
            <td>'; echo $one['sum']-$one['fare']; ;echo '</td>
            <td>'; echo $one['sum'] ;echo '</td>

        </tr>
        '; }} ;echo '    </table></body></html>
'; } ;echo '';
function orderHtml2(){
    global $order_info;
    $out = "";
    ob_start();

;echo '

<html><meta http-equiv=content-type content="text/html; charset=UTF-8"><body><table border=\'1\'>
        <thead><tr><th>订单号</th><th>代理名称</th><th>款号</th><th>尺码</th><th>数量</th><th>仓库点</th>
            <th>发布季</th><th>吊牌价</th><th>折扣</th><th>售价</th><th>邮费</th><th>运送方式</th><th>收货地址</th>
            <th>订单备注</th><th>快递单号</th><th>成本折扣</th><th>成本单价</th><th>成本总价</th></tr></thead>
        ';
        
        $order_number_prev = "new";
        $order_number = "new";
        $rowspan = 1;
        if(is_array($order_info)){foreach($order_info AS $index=>$one) {

            ($order_number_prev != $one['order_number'])? $order_number = $one['order_number'] : $order_number="";
            if($order_number==""){
                $rowspan++;
                $fare="";
                $address = "";
                $express_no = "";
                $express_name = "";
                $remark = "";
            }else{
                if($order_number_prev != "new"){
                    $out .= ob_get_contents();
                    $out = str_replace("name=".$order_number_prev, "rowspan=".$rowspan, $out);
                    ob_clean();
                }
                $rowspan = 1;
                $fare = $one['fare'];
                $address = $one['address'];
                $express_no = $one['express_no'];
                $express_name = $one['express_name'];
                $remark = $one['remark'];
            }

            $order_number_prev = $one['order_number'];
        ;echo '        <tr>
            '; if($order_number!=""){ ;echo '<td name='; echo $order_number; ;echo '>'; echo $order_number; ;echo '</td>'; } ;echo '            <td>'; echo $one['username']; ;echo '</td>
            <td>'; echo $one['item_number']; ;echo '</td>
            <td>'; echo $one['size'] ;echo '</td>
            <td>'; echo $one['quantity'] ;echo '</td>
            <td>'; echo $one['channel'] ;echo '</td>
            <td>'; echo $one['season'] ;echo '</td>
            <td>'; echo $one['origin'] ;echo '</td>
            <td>'; echo $one['discount'] ;echo '</td>
            <td>'; echo $one['price'] ;echo '</td>
            '; if($order_number!=""){ ;echo '<td name='; echo $order_number; ;echo '>'; echo $fare; ;echo '</td>'; } ;echo '            '; if($order_number!=""){ ;echo '<td name='; echo $order_number; ;echo '>'; echo $express_name ;echo '</td>'; } ;echo '            '; if($order_number!=""){ ;echo '<td name='; echo $order_number; ;echo '>'; echo $address; ;echo '</td>'; } ;echo '            '; if($order_number!=""){ ;echo '<td name='; echo $order_number; ;echo '>'; echo $remark ;echo '</td>'; } ;echo '            '; if($order_number!=""){ ;echo '<td name='; echo $order_number; ;echo '>&nbsp;'; echo $express_no ;echo '</td>'; } ;echo '            <td>'; echo $one['cost_discount'] ;echo '</td>
            <td>'; echo $one['cost_price'] ;echo '</td>
            <td>'; echo $one['sum_cost_price'] ;echo '</td>
        </tr>
        '; }} ;echo '    </table></body></html>
';
$out .= ob_get_contents();
$out = str_replace("name=".$order_number_prev, "rowspan=".$rowspan, $out);
ob_end_clean();
echo $out;
} ?>
