public function get($f3) { global $smarty; $expressService = new ExpressService(); $expressArray = $expressService->fetchExpressArray(); $smarty->assign('expressArray', $expressArray); $smarty->display('misc_express.tpl'); }
/** * 批量上传快递单号,必须上传配货单 * * @param $f3 */ public function Upload($f3) { $recIdColumnIndex = 5; // 子订单 ID 列号 $shippingIdColumnIndex = 23; // 快递公司 ID 列号 $shippingNoColumnIndex = 24; // 快递单号列 if (empty($_FILES) || !array_key_exists('uploadfile', $_FILES)) { $this->addFlashMessage('没有上传文件'); goto out; } if ($_FILES['uploadfile']['error'] > 0) { $this->addFlashMessage('上传文件错误:' . $_FILES['uploadfile']['error']); goto out; } // 解析上传的文件名 $pathInfoArray = pathinfo($_FILES['uploadfile']['name']); $fileExt = strtolower($pathInfoArray['extension']); if ('xls' != $fileExt) { $this->addFlashMessage('文件格式错误,必须是 Excel xls 文件'); goto out; } $targetFile = $f3->get('TEMP') . time() . $fileExt; move_uploaded_file($_FILES['uploadfile']['tmp_name'], $targetFile); require_once PROTECTED_PATH . '/Vendor/PHPExcel/Settings.php'; // 设置Excel缓存,防止数据太多拖死了程序 \PHPExcel_Settings::setCacheStorageMethod(\PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp); try { $objPHPExcel = \PHPExcel_IOFactory::load($targetFile); } catch (\Exception $e) { $this->addFlashMessage('上传的文件格式错误,请注意不要修改批量下载订单文件的格式'); goto out; } // 取得快递公司列表 $expressService = new ExpressService(); $expressArray = $expressService->fetchExpressArray(); // 构建 shipping_id --> express 的反查表 $shippingIdExpressArray = array(); foreach ($expressArray as $expressItem) { $shippingIdExpressArray[$expressItem['meta_id']] = $expressItem; } unset($expressArray); unset($expressService); // 释放内存 $activeSheet = $objPHPExcel->setActiveSheetIndex(0); $maxRow = $activeSheet->getHighestRow(); $expressSetCount = 0; // 成功设置计数 // 当前登录用户 $authSupplierUser = AuthHelper::getAuthUser(); $orderBasicService = new OrderBasicService(); // 一行一行的读取数据 for ($currentRow = 1; $currentRow <= $maxRow; $currentRow++) { // 取得子订单 ID $recIdStr = trim($activeSheet->getCellByColumnAndRow($recIdColumnIndex, $currentRow)->getValue()); if (!ctype_digit($recIdStr)) { // 如果不全是数字,说明这列不对 continue; } $orderGoods = $orderBasicService->loadOrderGoodsById(intval($recIdStr)); if ($orderGoods->isEmpty() || OrderGoodsService::OGS_PAY != $orderGoods->order_goods_status || $orderGoods['suppliers_id'] != $authSupplierUser['suppliers_id']) { $this->addFlashMessage('子订单[' . $recIdStr . ']非法'); continue; } //取得快递公司 ID 设置 $shippingIdStr = trim($activeSheet->getCellByColumnAndRow($shippingIdColumnIndex, $currentRow)->getValue()); if (!ctype_digit($shippingIdStr) || intval($shippingIdStr) <= 0) { $this->addFlashMessage('子订单[' . $recIdStr . '] 对应的 快递ID 错误'); continue; } $shipping_id = intval($shippingIdStr); if (!isset($shippingIdExpressArray[$shipping_id])) { $this->addFlashMessage('子订单[' . $recIdStr . '] 对应的 快递ID[' . $shipping_id . '] 非法'); continue; } if ($orderGoods->shipping_id > 0) { $this->addFlashMessage('子订单[' . $recIdStr . '] 覆盖了之前已有的快递信息 [' . $orderGoods->shipping_name . ':' . $orderGoods->shipping_no . ']'); } //取得快递单号 $shippingNoStr = trim($activeSheet->getCellByColumnAndRow($shippingNoColumnIndex, $currentRow)->getValue()); //设置快递信息 $orderGoods->shipping_id = $shipping_id; $orderGoods->shipping_name = $shippingIdExpressArray[$shipping_id]['meta_name']; $orderGoods->shipping_no = $shippingNoStr; $orderGoods->save(); $expressSetCount++; // 更新 order_info 的 update_time 字段 $orderInfo = $orderBasicService->loadOrderInfoById($orderGoods['order_id'], 1); //缓存1秒 $orderInfo->update_time = Time::gmTime(); $orderInfo->save(); // 添加订单操作日志 $action_note = '' . $shipping_id . ',' . $shippingIdExpressArray[$shipping_id]['meta_name'] . ',' . $shippingNoStr; $orderActionService = new OrderActionService(); $orderActionService->logOrderAction($orderGoods['order_id'], $orderGoods['rec_id'], $orderInfo['order_status'], $orderInfo['pay_status'], $orderGoods['order_goods_status'], $action_note, '供货商:[' . $authSupplierUser['suppliers_id'] . ']' . $authSupplierUser['suppliers_name'], 0, $orderInfo['shipping_status']); } $this->addFlashMessage('一共更新了 ' . $expressSetCount . ' 个快递信息'); out: // 删除上传文件 if (!empty($targetFile)) { @unlink($targetFile); } // 回到批量下载界面 RouteHelper::reRoute($this, '/Order/Excel'); }