예제 #1
0
 /**
  *  订单导出
  */
 public function do_export($id = 0)
 {
     // 初始化返回数据
     $return_data = array();
     //请求结构体
     $request_data = array();
     try {
         //权限验证
         role::check('order_export');
         if ($id < 1) {
             throw new MyRuntimeException(Kohana::lang('o_global.data_load_error'), 403);
         }
         if (!$id) {
             throw new MyRuntimeException(Kohana::lang('o_global.data_load_error'), 403);
         }
         if ($_POST) {
             /* 验证是否选择了订单 */
             if (!isset($_POST['order_ids'])) {
                 throw new MyRuntimeException(Kohana::lang('o_order.select_order_export'), 403);
             }
             $order_ids = $this->input->post('order_ids');
             //array(1,2);
             /* 得到当前的导出配置 */
             $data = Myorder_export::instance($id)->get();
             $output_field_ids = unserialize($data['export_ids']);
             /* 导出格式错误 */
             if (!is_array($output_field_ids) || count($output_field_ids) < 1) {
                 throw new MyRuntimeException(Kohana::lang('o_order.order_export_config_error'), 403);
             }
             $xls = export::instance();
             //$xls->debug(true);//开测试模式
             $xls->set_output_field_ids($output_field_ids);
             /* 订单状态,支付状态,物流状态 */
             $order_status = Kohana::config('order.order_status');
             $pay_status = Kohana::config('order.pay_status');
             $ship_status = Kohana::config('order.ship_status');
             $result = array();
             foreach ($order_ids as $order_id) {
                 $order = Myorder::instance($order_id)->get();
                 $shipping_country_name = Mycountry::instance()->get_name_by_iso_code($order['shipping_country']);
                 $billing_country_name = Mycountry::instance()->get_name_by_iso_code($order['billing_country']);
                 $order_products = Myorder_product::instance()->order_product_details(array('order_id' => $order['id']));
                 $order['shipping_country_name'] = $shipping_country_name;
                 $order['billing_country_name'] = $billing_country_name;
                 $order['pay_status_name'] = $pay_status[$order['pay_status']]['name'];
                 $order['ship_status_name'] = $ship_status[$order['ship_status']]['name'];
                 $order['order_status_name'] = $order_status[$order['order_status']]['name'];
                 $order['ip'] = long2ip($order['ip']);
                 if (is_array($order_products) && count($order_products) > 0) {
                     foreach ($order_products as $key => $value) {
                         $order_products[$key]['total'] = round($value['discount_price'] * $value['quantity'] * $order['conversion_rate'], 2);
                     }
                 }
                 $order['product'] = $order_products;
                 $xls->set_order_line($order);
             }
             $xls->output();
             exit;
         }
     } catch (MyRuntimeException $ex) {
         $return_struct['status'] = 0;
         $return_struct['code'] = $ex->getCode();
         $return_struct['msg'] = $ex->getMessage();
         var_dump($return_struct);
         exit;
         //TODO 异常处理
         //throw $ex;
         if ($this->is_ajax_request()) {
             $this->template = new View('layout/empty_html');
             $this->template->content = $return_struct['msg'];
         } else {
             $this->template->return_struct = $return_struct;
             $content = new View('info');
             $this->template->content = $content;
             /* 请求结构数据绑定 */
             $this->template->content->request_data = $request_data;
             /* 返回结构体绑定 */
             $this->template->content->return_struct = $return_struct;
         }
     }
 }