/** * 导入单品Excel数据 **/ public function actionaddsuborder() { $ret = array('status' => 0, 'msg' => '', 'err_data' => array()); $brandid = isset($_SESSION['brandid']) ? $_SESSION['brandid'] : 0; $description = ""; //备注内容 $ordername = ""; //订单号 $orderid = 0; $type = ''; $uploadfile = ''; try { if (Yii::app()->request->getParam('description')) { $description = Yii::app()->request->getParam('description'); //备注 } if (Yii::app()->request->getParam('ordername')) { $ordername = Yii::app()->request->getParam('ordername'); //订单名 } if (Yii::app()->request->getParam('orderid')) { $orderid = Yii::app()->request->getParam('orderid'); //订单id } if (Yii::app()->request->getParam('type')) { $type = Yii::app()->request->getParam('type'); //表格类型 } //验证该订单是否上传过图片,如果传过禁止修改 if ($orderid > 0 && !empty($ordername)) { $array = array('orderid' => $orderid); $clothesorderclass = new clothesorderclass(); $orderclothes = $clothesorderclass->select_Single_clothes($brandid, $array); if (!empty($orderclothes)) { throw new Exception('不可对该订单做操作'); } } //echo $orderid."-----".$ordername."-----".$description; //exit(); //获取上传的文件名 $file = $_FILES['inputExcel']['name']; $file = md5($file) . '.xls'; if (!empty($file)) { //上传到服务器上的临时文件名 $filetempname = $_FILES['inputExcel']['tmp_name']; //自己设置的上传文件存放路径 $filePath = 'uploads/saveexcel/'; $uploadfile = $filePath . $file; //上传后的文件名地址 //如果上传文件成功,就执行导入excel操作 spl_autoload_unregister(array('YiiBase', 'autoload')); //取消YII自动加载 require_once 'protected/extensions/PHPExcel.php'; require_once 'protected/extensions/PHPExcel/IOFactory.php'; require_once 'protected/extensions/PHPExcel/Reader/Excel5.php'; spl_autoload_register(array('YiiBase', 'autoload')); // Enable Yii autoloader //move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。 $uploadfile = iconv("utf-8", "gb2312", $uploadfile); $result = move_uploaded_file($filetempname, $uploadfile); //假如上传到当前目录下 if (!$result) { throw new Exception('文件上传失败!'); } $objReader = PHPExcel_IOFactory::createReader('Excel5'); //use excel2007 for 2007 format $objPHPExcel = $objReader->load($uploadfile); $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumn = $sheet->getHighestColumn(); // 取得总列数 $srow = 2; //起始行 $str = ""; $brandnumber_array = array(); $brandnumber_str = ""; $code_array = array(); //序号 $code_str = ""; $category_str = ""; $category_array = array(); //循环读取excel文件,读取一条,插入一条 for ($j = 2; $j <= $highestRow; $j++) { for ($k = 'A'; $k <= $highestColumn; $k++) { $str .= $objPHPExcel->getSheet()->getCell("{$k}{$j}")->getValue() . '\\'; //读取单元格 if ($k == 'A') { $brandnumber_str .= trim($objPHPExcel->getSheet()->getCell("{$k}{$j}")->getValue()) . '\\'; } else { if ($k == 'B') { $category_str .= trim($objPHPExcel->getSheet()->getCell("{$k}{$j}")->getValue()) . '\\'; } else { if ($k == 'C') { $code_str .= trim($objPHPExcel->getSheet()->getCell("{$k}{$j}")->getValue()) . '\\'; } else { break; } } } } } $brandnumber_array1 = array(); $brandnumber_array1 = explode("\\", $brandnumber_str); //款色号数组 $code_array1 = array(); $code_array1 = explode("\\", $code_str); //款色号数组 foreach ($brandnumber_array1 as $value) { if (!empty($value)) { $brandnumber_array[] = $value; } } foreach ($code_array1 as $key => $value) { if (!empty($brandnumber_array1[$key])) { $code_array[] = $value; } } $category_array = explode("\\", $category_str); //防止重复字段 $u_strs = array_unique($brandnumber_array); if (count($brandnumber_array) > count($u_strs)) { @unlink($_SERVER['DOCUMENT_ROOT'] . '/' . $uploadfile); throw new Exception('本表中款色号不能有重复'); } if ($type != 'style') { //图片管理 导表分类管理 $brandnumber_string = implode("','", $brandnumber_array); $clothesorderclass = new clothesorderclass(); $clothesorder_obj = $clothesorderclass->selsectordernamebybrandnumber($brandid, $brandnumber_string, $orderid); $ret['repeat_data'] = $clothesorder_obj; $repeat_array = array(); foreach ($clothesorder_obj as $clothesorder_obj_value) { $repeat_array[] = $clothesorder_obj_value['brandnumber']; //将重复的加入数组 } $brandnumber_array = array_diff($brandnumber_array, $repeat_array); //将重复的款号删除 if (count($brandnumber_array) == 0) { $ret['status'] = 1; @unlink($_SERVER['DOCUMENT_ROOT'] . '/' . $uploadfile); throw new Exception('订单内容与之前订单重复'); } if (count($brandnumber_array) > 0 && count($repeat_array) > 0) { $ret['msg'] = "订单已创建,发现有其他订单下的款色号"; } if ($orderid > 0 && !empty($ordername)) { $orderclass = new orderclass(); //删除该订单下的所有款号 $orderclass->deletebrandnumberbyorderid($brandid, $orderid); $orderdir = $_SERVER['DOCUMENT_ROOT'] . '/orderlist/' . $ordername; $orderclass->delFolder($orderdir); } } } if ($type == 'style') { //图片管理 导表分类管理 if (count(array_filter($brandnumber_array)) == 0) { throw new Exception('款号为空!'); } if (count(array_filter($category_array)) == 0) { throw new Exception('分类为空!'); } //获取品牌 SKU 规则 $brand_ret = Brand::brandSelectById($_SESSION['brandid']); if (count($brand_ret) == 0) { throw new Exception(''); } $code_start = $brand_ret[0]['code_start']; $code_end = $brand_ret[0]['code_end']; //将SKC转为SKU $brandnumber_SKU_arr = array(); $category_SKU_array = array(); foreach ($brandnumber_array as $key => $value) { $brandnumber_SKU = $value; if ($code_start != 0 || $code_end != 0) { //设置了款号规则后 按规则提取款号 $brandnumber_SKU = $this->SKCToSKU($code_start, $code_end, $value); } $brandnumber_key = array_search($brandnumber_SKU, $brandnumber_SKU_arr); if ($brandnumber_key !== false) { $category_SKU_array[$brandnumber_key] = $category_array[$key]; } else { $brandnumber_SKU_arr[] = $brandnumber_SKU; $category_SKU_array[] = $category_array[$key]; } } //获取品牌的所有分类 $brandcategory = new brandcategoryclass(); $category_ret = $brandcategory->select_category($brandid); if (count($category_ret) == 0) { throw new Exception('品牌暂未设置分类!'); } //提取已有款号到数组 $category_data = array(); foreach ($category_ret as $value) { $category_data[$value->name] = $value->id; } $category_array_copy = array_unique($category_SKU_array); //清除重复的分类 $category_array_copy = array_filter($category_array_copy); //清除空元素 //不存在的分类 $no_category = Comm::array_diff_fast($category_array_copy, array_keys($category_data)); $no_category = array_values($no_category); //获取款号对应衣服 $clothesorder = new clothesorderclass(); $clothes_ret = $clothesorder->select_all_clothes($brandid, -1, array('sku' => $brandnumber_SKU_arr)); $no_brandnumber = array(); //不存在的款号 foreach ($clothes_ret['data'] as $value) { $brandnumber_copy_str = $value['sku']; $no_brandnumber[] = $brandnumber_copy_str; $brandnumber_key = array_search($brandnumber_copy_str, $brandnumber_SKU_arr); if (isset($category_data[$category_SKU_array[$brandnumber_key]])) { //当款号设置分类 已存在时才可以设置 $clothesorder->update_clothes($value['id'], array('brandcategoryid' => $category_data[$category_SKU_array[$brandnumber_key]])); } } $ret['no_brandnumber'] = Comm::array_diff_fast($brandnumber_SKU_arr, $no_brandnumber); $ret['no_brandnumber'] = array_values($ret['no_brandnumber']); $ret['no_category'] = $no_category; } else { $user36 = $this->createBrandCode($brandid); //品牌号生成5位编码 $order_id = 0; if (empty($ordername) && empty($orderid)) { $ordername = ''; $orderobj = Yii::app()->db->createCommand()->select('ordername')->from('erp_order')->where("brandid={$brandid}")->andwhere("ordername<>' '")->order("id desc")->queryRow(); if (!empty($orderobj)) { $ordername = $orderobj['ordername']; $ordername = substr($ordername, 5) + 1; if (strlen($ordername) == 1) { $ordername = $user36 . "00" . $ordername; } else { if (strlen($ordername) == 2) { $ordername = $user36 . "0" . $ordername; } } } else { $ordername = $user36 . '001'; } $erp_order = new erp_order(); $erp_order->ordername = $ordername; $erp_order->description = $description; $erp_order->barcodecount = count($brandnumber_array); $erp_order->addtime = date("Y-m-d H:i:s"); $erp_order->brandid = $brandid; $count = $erp_order->insert(); $order_id = Yii::app()->db->getLastInsertID(); if ($count > 0) { } else { throw new Exception('插入数据失败'); } } else { $order_id = $orderid; $update_arr = array(); $update_arr['description'] = $description; if (!empty($file)) { $update_arr['barcodecount'] = count($brandnumber_array); } if ($orderid > 0) { erp_order::model()->updateAll($update_arr, 'id=:textx', array(':textx' => $orderid)); } } $url = 'uploads/saveexcel/' . $brandid . '_' . $ordername . '.xls'; rename($_SERVER['DOCUMENT_ROOT'] . "/" . $uploadfile, $_SERVER['DOCUMENT_ROOT'] . "/" . $url); //将款号插入到订单对应的款号表中 foreach ($brandnumber_array as $key => $value) { $erp_brandnumber_order = new erp_brandnumber_order(); $erp_brandnumber_order->brandnumber = $value; $erp_brandnumber_order->orderid = $order_id; $erp_brandnumber_order->brandid = $brandid; $erp_brandnumber_order->snumber = $code_array[$key]; $count = $erp_brandnumber_order->insert(); } $orderclass = new orderclass(); $orderclass->create_order($brandid, $ordername, $brandnumber_array); //$bb=orderclass::create_order($ordername,$brandnumber_array); } $ret['status'] = 1; } catch (Exception $e) { $ret['msg'] = $e->getMessage(); } if (count($ret['err_data']) == 0) { $ret['err_data'] = ''; } echo '<script> parent.setsubclothes(' . json_encode($ret) . ');</script>'; }