diff --git a/admin/controllers/items/Cost.php b/admin/controllers/items/Cost.php index 6d355fcc..8a37164a 100644 --- a/admin/controllers/items/Cost.php +++ b/admin/controllers/items/Cost.php @@ -357,6 +357,94 @@ class Cost extends HD_Controller{ } + public function add_excel() + { + require_once COMMPATH . '/third_party/PHPExcel/IOFactory.php'; + $res = $this->upload(); + if (!$res['code']) { + return $this->show_json(0, $res['message']); + } + $file = $res['path']; + if ($res['file_ext'] == '.xls') { + $reader = \PHPExcel_IOFactory::createReader('Excel5'); // 读取 excel 文档 + } elseif ($res['file_ext'] == '.xlsx') { + $reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 读取 excel 文档 + } + $PHPExcel = $reader->load($file); // 文档名称 + $objWorksheet = $PHPExcel->getActiveSheet(); + $rowCnt = $objWorksheet->getHighestRow(); //获取总行数 + if ($rowCnt > 10000) { + @unlink($file); + return $this->show_json(0, '数据大于一万请拆分多个表格导入'); + } + $s = $f = $ignore_vin = $ignore_item = $ignore_cost = 0; + @unlink($file); + for ($_row = 2; $_row <= $rowCnt; $_row++) { //读取内容 + $vin = $objWorksheet->getCell('A' . $_row)->getValue(); //vin码 + if (!$vin){ + $ignore_vin++; + continue; + } + $item = $this->items_model->get(array('vin' => $vin)); + if (!$item){ + $ignore_item++; + continue; + } + $cost = $this->items_cost_model->get(array('item_id' => $item['id'])); + if (!$cost){ + $ignore_cost++; + continue; + } + + $id = $cost['id']; + $promotion_json = json_decode($cost['promotion_json'],true); + $cost_json = json_decode($cost['cost_json'],true); + $srv_json = json_decode($cost['srv_json'],true); + # 读取需要更新的列,生成需更新的数据 + # 参考 edit* 更新 + $buy_price = floatval($objWorksheet->getCell('B' . $_row)->getValue()); //采购成本 + $select_price = floatval($objWorksheet->getCell('C' . $_row)->getValue()); //选装成本 + $labor_price = floatval($objWorksheet->getCell('D' . $_row)->getValue()); //选装工时费 + $sale_price = floatval($objWorksheet->getCell('E' . $_row)->getValue()); //销售佣金 + $other = floatval($objWorksheet->getCell('F' . $_row)->getValue()); //其他促销成本 + $factory_price = floatval($objWorksheet->getCell('G' . $_row)->getValue()); //厂家补贴 + $other_in_price = floatval($objWorksheet->getCell('H' . $_row)->getValue()); //其他收入 + $other_out_price = floatval($objWorksheet->getCell('I' . $_row)->getValue()); //其他支出 + $res_item = 0; + if ($item['buy_price'] != $buy_price){ + $res_item = $this->items_model->update(array('buy_price'=>$buy_price),['id'=>$item['id']]); + } + $cost_json['select_price'] = $select_price ? $select_price : 0; + $cost_json['labor_price'] = $labor_price ? $labor_price : 0; + + $promotion_json['sale_price'] = $sale_price ? $sale_price : 0; + $promotion_json['other'] = $other ? $other : 0; + $promotion_json['factory_price'] = $factory_price ? $factory_price : 0; + + $srv_json['other_in_price'] = $other_in_price ? $other_in_price : 0; + $srv_json['other_out_price'] = $other_out_price ? $other_out_price : 0; + + $data = [ + 'cost_json' => json_encode($cost_json,JSON_UNESCAPED_UNICODE), + 'promotion_json' => json_encode($promotion_json,JSON_UNESCAPED_UNICODE), + 'srv_json' => json_encode($srv_json,JSON_UNESCAPED_UNICODE), + ]; + $res = $this->items_cost_model->update($data,['id'=>$cost['id']]); + if(is_numeric($res_item) || is_numeric($res)){ + $this->items_cost_model->update_total($cost['id']); + }else{ + $f++; + } + $s++; + } +// @unlink($file); + $ignore = $ignore_vin + $ignore_item + $ignore_cost; + $msg = "导入完成,导入总条数为:" . $s; + $f && $msg = $msg . '; 失败条数:'. $f; + $ignore && $msg = $msg . '; 忽略条数:'. $ignore; + return $this->show_json(SYS_CODE_SUCCESS, $msg); + } + public function export(){ $params = $this->input->get(); $params['page'] = 1; @@ -697,4 +785,25 @@ class Cost extends HD_Controller{ $this->load->library('excel'); $this->excel->out_csv($data, $indexs, $fileName . "_" . date('YmdHis')); } + + /** + * Notes:上传excel + * Created on: 2021/7/14 15:06 + * Created by: dengbw + * @return array + */ + private function upload() + { + $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/temp/'; + $config['allowed_types'] = '*';//xls|xlsx + $config['max_size'] = 5 * 1024; + $config['file_name'] = 'items_' . time() . rand(1, 99999); + $this->load->library('upload', $config); + if (!$this->upload->do_upload('file')) { + return array('code' => SYS_CODE_FAIL, 'message' => $this->upload->display_errors('', '')); + } else { + $data = $this->upload->data(); + return array('code' => SYS_CODE_SUCCESS, 'path' => $data['full_path'], 'file_ext' => $data['file_ext']); + } + } } \ No newline at end of file diff --git a/admin/views/items/cost/lists.php b/admin/views/items/cost/lists.php index 271f85b4..660d06fc 100644 --- a/admin/views/items/cost/lists.php +++ b/admin/views/items/cost/lists.php @@ -115,6 +115,9 @@