/** * 更新或者新建一个快递公司 * * @param $f3 */ public function Edit($f3) { // 权限检查 $this->requirePrivilege('manage_misc_express_edit'); global $smarty; // 参数验证 $validator = new Validator($f3->get('GET')); $meta_id = $validator->digits()->validate('meta_id'); $meta_id = $meta_id ?: 0; // 加载 快递信息 $expressService = new ExpressService(); $expressInfo = $expressService->loadMetaById($meta_id); if (Request::isRequestGet()) { goto out_assign; } // 安全性检查 if ($meta_id > 0) { if ($expressInfo->isEmpty() || ExpressService::META_TYPE != $expressInfo->meta_type) { $this->addFlashMessage('非法ID[' . $meta_id . ']'); goto out; } } unset($validator); $validator = new Validator($f3->get('POST')); $inputArray = array(); $inputArray['meta_type'] = ExpressService::META_TYPE; $inputArray['meta_name'] = $validator->required()->validate('meta_name'); $inputArray['meta_ename'] = $validator->required()->validate('meta_ename'); $inputArray['meta_sort_order'] = $validator->digits()->validate('meta_sort_order'); $inputArray['meta_status'] = $validator->digits()->validate('meta_status'); $inputArray['meta_desc'] = $validator->validate('meta_desc'); if (!$this->validate($validator)) { goto out; } // 保存 $expressInfo->copyFrom($inputArray); $expressInfo->save(); $this->addFlashMessage('快递信息保存成功'); // POST 成功从这里退出 RouteHelper::reRoute($this, RouteHelper::makeUrl('/Misc/Express/Edit', array('meta_id' => $expressInfo->meta_id), true)); return; out_assign: $smarty->assign($expressInfo->toArray()); out: $smarty->display('misc_express_edit.tpl'); }
public function ListExpress($f3) { // 检查缓存 $cacheKey = md5(__NAMESPACE__ . '\\' . __CLASS__ . '\\' . __METHOD__); $expressArray = $f3->get($cacheKey); if (!empty($expressArray)) { goto out; } $expressService = new ExpressService(); $expressArray = $expressService->fetchAvailableExpressArray(); $f3->set($cacheKey, $expressArray, 300); //缓存 5 分钟 out: $f3->expire(60); // 客户端缓存 1 分钟 Ajax::header(); echo Ajax::buildResult(null, null, $expressArray); }
/** * 设置订单的信息,注意:这个方法里面糅合了很多功能,通过 action="xxxx" 来区分 * * @param $f3 */ public function Update($f3) { // 验证 action $validator = new Validator($f3->get('GET')); $action = $validator->required()->oneOf(array('set_extra_discount', 'set_suppliers_price', 'set_shipping_no', 'set_memo', 'set_refund', 'set_extra_refund'), '非法操作')->validate('action'); if (!$this->validate($validator)) { goto out; } // 验证提交上来的参数 $validator = new Validator($f3->get('POST')); $updateValueArray = array(); $rec_id = $validator->required()->digits()->min(1)->validate('rec_id'); // 针对不同的 action 做不同的验证 switch ($action) { case 'set_extra_discount': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_extra_discount'); $updateValueArray['extra_discount'] = Money::toStorage($validator->validate('extra_discount')); $updateValueArray['extra_discount_note'] = $validator->required()->validate('extra_discount_note'); break; case 'set_suppliers_price': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_suppliers_price'); $updateValueArray['suppliers_price'] = Money::toStorage($validator->validate('suppliers_price')); $updateValueArray['suppliers_shipping_fee'] = Money::toStorage($validator->validate('suppliers_shipping_fee')); break; case 'set_shipping_no': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_shipping_no'); $updateValueArray['shipping_id'] = $validator->digits()->min(1)->validate('shipping_id'); $updateValueArray['shipping_no'] = $validator->validate('shipping_no'); break; case 'set_memo': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_memo'); $updateValueArray['memo'] = $validator->validate('memo'); break; case 'set_refund': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_refund'); $updateValueArray['refund'] = Money::toStorage($validator->validate('refund')); $updateValueArray['refund_note'] = $validator->required()->validate('refund_note'); $updateValueArray['refund_time'] = Time::gmTime(); $updateValueArray['suppliers_refund'] = Money::toStorage($validator->validate('suppliers_refund')); $updateValueArray['suppliers_refund_note'] = $validator->required()->validate('suppliers_refund_note'); break; case 'set_extra_refund': // 权限检查 $this->requirePrivilege('manage_order_goods_update_set_extra_refund'); $updateValueArray['extra_refund'] = Money::toStorage($validator->validate('extra_refund')); $updateValueArray['extra_refund_note'] = $validator->required()->validate('extra_refund_note'); $updateValueArray['extra_refund_time'] = Time::gmTime(); break; default: // 非法的 action goto out; } if (!$this->validate($validator)) { goto out; } // 取 order_goods $orderBasicService = new OrderBasicService(); $orderGoods = $orderBasicService->loadOrderGoodsById($rec_id); if ($orderGoods->isEmpty()) { $this->addFlashMessage('非法订单'); goto out_fail; } // 取得 orderInfo $orderInfo = $orderBasicService->loadOrderInfoById($orderGoods['order_id']); // 针对不同的 action 做额外不同的工作 $action_note = ''; switch ($action) { case 'set_extra_discount': // 商品只有是未付款状态才可以设置额外优惠 if (OrderGoodsService::OGS_UNPAY != $orderGoods['order_goods_status']) { $this->addFlashMessage('只有未付款订单才可以给予额外优惠'); goto out; } // 额外优惠允许的最大金额 $allowExtraDiscount = $orderGoods['goods_price'] + $orderGoods['shipping_fee'] - $orderGoods['discount']; $maxExtraDiscount = intval($allowExtraDiscount * $f3->get('sysConfig[max_order_goods_extra_discount_rate]')); $maxExtraDiscount = max($maxExtraDiscount, $f3->get('sysConfig[max_order_goods_extra_discount_value]')); $maxExtraDiscount = min($maxExtraDiscount, $allowExtraDiscount); // 额外优惠不能超过商品本身的金额 if ($updateValueArray['extra_discount'] > $maxExtraDiscount) { $this->addFlashMessage('额外优惠不能超过商品总金额 ' . $maxExtraDiscount); goto out; } // 设置额外余额,需要重新计算 order_info 中的值 $diffDiscount = 0; if ($orderGoods->extra_discount != $updateValueArray['extra_discount']) { $diffDiscount = $updateValueArray['extra_discount'] - $orderGoods->extra_discount; } $orderInfo->extra_discount += $diffDiscount; $orderInfo->order_amount -= $diffDiscount; $action_note .= '额外优惠:' . Money::toSmartyDisplay($updateValueArray['extra_discount']) . ","; $action_note .= '优惠说明:' . $updateValueArray['extra_discount_note'] . "\n"; break; case 'set_suppliers_price': $action_note .= '供货价:' . Money::toSmartyDisplay($updateValueArray['suppliers_price']) . ","; $action_note .= '供货快递费:' . Money::toSmartyDisplay($updateValueArray['suppliers_shipping_fee']) . "\n"; break; case 'set_shipping_no': if ($updateValueArray['shipping_id'] > 0) { //取得快递信息 $expressService = new ExpressService(); $expressInfo = $expressService->loadMetaById($updateValueArray['shipping_id']); if ($expressInfo->isEmpty() || ExpressService::META_TYPE != $expressInfo['meta_type']) { $this->addFlashMessage('快递ID非法'); goto out; } $updateValueArray['shipping_name'] = $expressInfo['meta_name']; } else { $updateValueArray['shipping_name'] = null; } $action_note .= '快递公司:' . $updateValueArray['shipping_name'] . "\n"; $action_note .= '快递单号:' . $updateValueArray['shipping_no'] . "\n"; break; case 'set_memo': $action_note .= '客服备注:' . $updateValueArray['memo'] . "\n"; break; case 'set_refund': // 检查订单状态 if (!in_array($orderGoods['order_goods_status'], array(OrderGoodsService::OGS_PAY, OrderGoodsService::OGS_ASKREFUND))) { $this->addFlashMessage('订单状态非法,不能退款'); goto out; } if ($orderGoods['settle_id'] > 0) { $this->addFlashMessage('已经结算的订单不能退款'); goto out; } // 订单设置为 申请退款 $updateValueArray['order_goods_status'] = OrderGoodsService::OGS_ASKREFUND; // 同步更新 order_info 中的 refund 字段 $diffRefund = 0; if ($orderGoods->refund != $updateValueArray['refund']) { $diffRefund = $updateValueArray['refund'] - $orderGoods->refund; } $orderInfo->refund += $diffRefund; // 检查金额,对一些常见错误提出警告 if (0 == $updateValueArray['refund']) { $this->addFlashMessage('警告:你确定给顾客退款金额设置为 ' . Money::toSmartyDisplay($updateValueArray['refund']) . ' ?'); } if (0 == $updateValueArray['suppliers_refund']) { $this->addFlashMessage('警告:你确定供货商给我们退款金额为 ' . Money::toSmartyDisplay($updateValueArray['refund']) . ' ?'); } if ($updateValueArray['refund'] <= $updateValueArray['suppliers_refund']) { $this->addFlashMessage('警告:给顾客退款金额 <= 供货商给我们的退款金额'); } // 日志信息记录 $action_note .= '申请退款' . "\n"; $action_note .= '顾客金额:' . Money::toSmartyDisplay($updateValueArray['refund']) . ","; $action_note .= '顾客说明:' . $updateValueArray['refund_note'] . "\n"; $action_note .= '供货商金额:' . Money::toSmartyDisplay($updateValueArray['suppliers_refund']) . ","; $action_note .= '供货商说明:' . $updateValueArray['suppliers_refund_note'] . "\n"; break; case 'set_extra_refund': // 检查订单状态 if (OrderGoodsService::OGS_UNPAY == $orderGoods['order_goods_status']) { $this->addFlashMessage('订单状态非法,不能退款'); goto out; } $action_note .= '额外退款:' . Money::toSmartyDisplay($updateValueArray['extra_refund']) . ","; $action_note .= '退款说明:' . $updateValueArray['extra_refund_note'] . "\n"; break; default: // 非法的 action goto out; } // 更新订单信息 $orderGoods->copyFrom($updateValueArray); $orderGoods->update_time = Time::gmTime(); $orderGoods->save(); // 更新 order_info 的 update_time 字段 $orderInfo->update_time = Time::gmTime(); $orderInfo->save(); // 添加订单操作日志 $authAdminUser = AuthHelper::getAuthUser(); $orderActionService = new OrderActionService(); $orderActionService->logOrderAction($orderGoods['order_id'], $orderGoods['rec_id'], $orderInfo['order_status'], $orderInfo['pay_status'], $orderGoods['order_goods_status'], $action_note, $authAdminUser['user_name'], 0, $orderInfo['shipping_status']); $this->addFlashMessage('订单信息保存成功'); out: RouteHelper::reRoute($this, RouteHelper::makeUrl('/Order/Goods/Detail', array('rec_id' => $rec_id), true)); return; out_fail: // 失败从这里退出 RouteHelper::reRoute($this, '/Order/Goods/Search', false); }
/** * 批量上传快递单号,必须上传配货单 * * @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'); }
/** * 设置订单的快递信息 * * @param $f3 */ public function ajaxUpdate($f3) { // 参数验证 $validator = new Validator($f3->get('POST')); $rec_id = $validator->required('子订单ID不能为空')->digits('子订单ID必须是数字')->min(1)->validate('rec_id'); $shipping_id = $validator->digits('快递ID必须是数字')->min(1)->validate('shipping_id'); $shipping_no = $validator->validate('shipping_no'); if (!$this->validate($validator)) { goto out_fail; } // 取 order_goods $orderBasicService = new OrderBasicService(); $orderGoods = $orderBasicService->loadOrderGoodsById($rec_id); if ($orderGoods->isEmpty()) { $this->addFlashMessage('非法订单'); goto out_fail; } // 权限检查 $authSupplierUser = AuthHelper::getAuthUser(); if ($orderGoods['suppliers_id'] !== $authSupplierUser['suppliers_id']) { $this->addFlashMessage('非法订单'); goto out_fail; } // 取得快递名 if ($shipping_id > 0) { //取得快递信息 $expressService = new ExpressService(); $expressInfo = $expressService->loadMetaById($shipping_id); if ($expressInfo->isEmpty() || ExpressService::META_TYPE != $expressInfo['meta_type']) { $this->addFlashMessage('快递ID非法'); goto out_fail; } $shipping_name = $expressInfo['meta_name']; } else { $shipping_name = null; } // 更新快递信息 $orderGoods->shipping_id = $shipping_id; $orderGoods->shipping_name = $shipping_name; $orderGoods->shipping_no = $shipping_no; $orderGoods->save(); // 更新 order_info 的 update_time 字段 $orderInfo = $orderBasicService->loadOrderInfoById($orderGoods['order_id']); $orderInfo->update_time = Time::gmTime(); $orderInfo->save(); // 添加订单操作日志 if ($shipping_id > 0) { $action_note = '' . $shipping_id . ',' . $shipping_name . ',' . $shipping_no; } else { $action_note = '删除快递信息'; } $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']); Ajax::header(); echo Ajax::buildResult(null, null, null); return; // 成功从这里返回 out_fail: // 失败从这里退出 Ajax::header(); $errorMessage = ''; foreach ($this->flashMessageArray as $messageItem) { $errorMessage .= $messageItem . ','; } echo Ajax::buildResult(-1, $errorMessage, null); }