<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends salebase {

    public function action_rukuindex()
    {
        $model = parent::model('purchase', 'sa');
        list($total, $data) = $model->endPurchase(array('page'=>$this->page, 'pagesize'=>$this->pagesize));
        unset($model);
        $model = parent::model('supplier', 'sa');
        foreach ($data as $key => $value) {
            $supplier_name = $model->get_data($value['supplier_id']);
            $data[$key]['supplier_name'] = $supplier_name['supplier_name'];
        }
        unset($model);
        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$this->page+1,
            'prepage'=>$this->page-1,
            'pagecount'=>ceil($total/$this->pagesize),
            'pagesize'=>$this->pagesize,
            'total'=>$total,
            'showpage'=>XPage::sale($total, $this->pagesize, $this->page, $url, 10),
            'urlitem'=>$this->_urlitem,
            'comeurl'=>$this->_comeurl,
            'tid'=>$this->tid,
            'sname'=>$this->sname,
            'storage'=>$data,
            'category_select'=>$category_select,
            'modtitle'=>$this->modtitle,
            'sname'=>$sname,
            'scatid'=>$scatid,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'report/rukuindex.tpl');
    }

    public function action_ruku()
    {
        require_once BASE_ROOT.'source/core/PHPExcel.php';
        $model = parent::model('purchase', 'sa');
        list($total, $data) = $model->endPurchase(array('page'=>$this->page, 'pagesize'=>$this->pagesize));
        unset($model);
        $model = parent::model('supplier', 'sa');
        foreach ($data as $key => $value) {
            $supplier_name = $model->get_data($value['supplier_id']);
            $data[$key]['supplier_name'] = $supplier_name['supplier_name'];
        }
        unset($model);


        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("作者简庆旺")
                    ->setLastModifiedBy("最后更改者")
                    ->setTitle("文档标题")
                    ->setSubject("文档主题")
                    ->setDescription("文档的描述信息")
                    ->setKeywords("设置文档关键词")
                    ->setCategory("设置文档的分类");

        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '订单号');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '订单数量');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '产品名称');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', '采购单发起人');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', '供应商名');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F1', '预计到货时间');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G1', '实际到货时间');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('H1', '订单创建时间');

        //根据excel坐标，添加数据
        foreach ($data as $key => $value) {
            $a = $key+2;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$a, $value['purchase_name']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$a, $value['pruchase_number']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$a, $value['productname']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$a, $value['create_user_name']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$a, $value['supplier_name']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F'.$a, $value['delivery_time']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G'.$a, date("Y-m-d H:i:s",$value['receiptTime']));
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('H'.$a, date("Y-m-d H:i:s",$value['add_time']));

        }

        // 重命名工作sheet
        $objPHPExcel->getActiveSheet()->setTitle('入库统计');

        // 设置第一个sheet为工作的sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // 保存Excel 2007格式文件，保存路径为当前路径，名字为export.xlsx
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $name = time().'export.xlsx';
        $objWriter->save($name);

        ob_end_clean();
        ob_start();
 
        $dir ="/";  //相对于网站根目录的下载目录路径
        $down_host = $_SERVER['HTTP_HOST'].'/'; //当前域名


        if (! file_exists ($name )) {    
            header('HTTP/1.1 404 NOT FOUND');  
        } else {    
            //以只读和二进制模式打开文件   
            $file = fopen ($name, "rb" ); 

            //告诉浏览器这是一个文件流格式的文件    
            Header ( "Content-type: application/octet-stream" ); 
            //请求范围的度量单位  
            Header ( "Accept-Ranges: bytes" );  
            //Content-Length是指定包含于请求或响应中数据的字节长度    
            Header ( "Accept-Length: " . filesize ( $name ) );  
            //用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值。
            Header ( "Content-Disposition: attachment; filename=" . $name );    

            //读取文件内容并直接输出到浏览器    
            echo fread ( $file, filesize ( $name ) );    
            fclose ( $file );    
            exit ();    
        }
    }

    public function action_chukuindex()
    {
        $searchsql = " AND status not in ('待付款')";
        $model = parent::model('order', 'am');
        list($total, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
        $url = XRequest::getPhpSelf();
        $url .= '?c=htmllabel';
        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$this->page+1,
            'prepage'=>$this->page-1,
            'pagecount'=>ceil($total/$this->pagesize),
            'pagesize'=>$this->pagesize,
            'total'=>$total,
            'showpage'=>XPage::admin($total, $this->pagesize, $this->page, $url, 10),
            'product'=>$data,
        );
        unset($model);
        TPL::assign($var_array);
        TPL::display($this->cptpl.'report/chukuindex.tpl');

    }

    public function action_chuku()
    {
        require_once BASE_ROOT.'source/core/PHPExcel.php';
        $searchsql = " AND status not in ('待付款')";
        $model = parent::model('order', 'am');
        list($total, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
       
        unset($model);

         $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setCreator("作者简庆旺")
                    ->setLastModifiedBy("最后更改者")
                    ->setTitle("文档标题")
                    ->setSubject("文档主题")
                    ->setDescription("文档的描述信息")
                    ->setKeywords("设置文档关键词")
                    ->setCategory("设置文档的分类");

        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '会员名称');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', '订单编号');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C1', '商品名称');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D1', '商品数量');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E1', '订单价格');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F1', '状态');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G1', '下单时间');
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('H1', '订单创建时间');

        //根据excel坐标，添加数据
        foreach ($data as $key => $value) {
            $a = $key+2;
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$a, $value['username']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$a, $value['ordernum']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$a, $value['goodsname']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('D'.$a, $value['goodsnum']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('E'.$a, $value['orderprice']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('F'.$a, $value['status']);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('G'.$a, date("Y-m-d H:i:s",$value['time']));
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue('H'.$a, date("Y-m-d H:i:s",$value['time']));

        }

         // 重命名工作sheet
        $objPHPExcel->getActiveSheet()->setTitle('出库统计');

        // 设置第一个sheet为工作的sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // 保存Excel 2007格式文件，保存路径为当前路径，名字为export.xlsx
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $name = time().'export.xlsx';
        $objWriter->save($name);

        ob_end_clean();
        ob_start();
 
        $dir ="/";  //相对于网站根目录的下载目录路径
        $down_host = $_SERVER['HTTP_HOST'].'/'; //当前域名


        if (! file_exists ($name )) {    
            header('HTTP/1.1 404 NOT FOUND');  
        } else {    
            //以只读和二进制模式打开文件   
            $file = fopen ($name, "rb" ); 

            //告诉浏览器这是一个文件流格式的文件    
            Header ( "Content-type: application/octet-stream" ); 
            //请求范围的度量单位  
            Header ( "Accept-Ranges: bytes" );  
            //Content-Length是指定包含于请求或响应中数据的字节长度    
            Header ( "Accept-Length: " . filesize ( $name ) );  
            //用来告诉浏览器，文件是可以当做附件被下载，下载后的文件名称为$file_name该变量的值。
            Header ( "Content-Disposition: attachment; filename=" . $name );    

            //读取文件内容并直接输出到浏览器    
            echo fread ( $file, filesize ( $name ) );    
            fclose ( $file );    
            exit ();    
        } 

    }


 
}