public function ListOrderGoodsSupplierIdName($f3) { // 参数验证 $validator = new Validator($f3->get('GET')); //表单查询 $formQuery = array(); // 是否这个列表是用于供货商订单结算 $supplierForSettle = $validator->validate('supplier_for_settle'); //付款时间 $payTimeStartStr = $validator->required()->validate('pay_time_start'); $payTimeStart = Time::gmStrToTime($payTimeStartStr) ?: null; $payTimeEndStr = $validator->required()->validate('pay_time_end'); $payTimeEnd = Time::gmStrToTime($payTimeEndStr) ?: null; $formQuery['pay_time'] = array($payTimeStart, $payTimeEnd); //额外退款时间 $extraRefundTimeStartStr = $validator->required()->validate('extra_refund_time_start'); $extraRefundTimeStart = Time::gmStrToTime($extraRefundTimeStartStr) ?: null; $extraRefundTimeEndStr = $validator->required()->validate('extra_refund_time_end'); $extraRefundTimeEnd = Time::gmStrToTime($extraRefundTimeEndStr) ?: null; $formQuery['extra_refund_time'] = array($extraRefundTimeStart, $extraRefundTimeEnd); if (!($payTimeStart && $payTimeEnd) && !($extraRefundTimeStart && $extraRefundTimeEnd)) { goto out_fail; } // 取得供货商 id $condArray = array(); $condArray[] = array('oi.order_id = og.order_id'); // 这个列表是用于供货商订单结算,只取得需要结算的供货商 if (!empty($supplierForSettle)) { //只有付款了订单才显示 $condArray[] = array('order_goods_status > 0'); //只有发货了订单才需要结算 $condArray[] = array('og.shipping_id > 0'); //商家结算过了就不要显示出来了 $condArray[] = array('settle_id = 0'); } // 表单查询 $condArray = array_merge($condArray, QueryBuilder::buildQueryCondArray($formQuery)); $orderGoodsService = new OrderGoodsService(); $queryArray = $orderGoodsService->_fetchArray(array('order_info' => 'oi', 'order_goods' => 'og'), 'distinct(og.suppliers_id)', $condArray, array('order' => 'suppliers_id desc'), 0, $f3->get('sysConfig[max_query_record_count]'), 0); //最多限制 max_query_record_count 条记录 // 取得供货商 id 列表 $supplierIdArray = array(); foreach ($queryArray as $queryItem) { $supplierIdArray[] = $queryItem['suppliers_id']; } if (empty($supplierIdArray)) { // 没有数据,退出 goto out_fail; } // 取得供货商信息 $userSupplierService = new UserSupplierService(); $queryArray = $userSupplierService->fetchSupplierArrayBySupplierIdArray($supplierIdArray); $supplierArray = array(); foreach ($queryArray as $queryItem) { $supplierItem = array(); $supplierItem['suppliers_id'] = $queryItem['suppliers_id']; $supplierItem['suppliers_account'] = $queryItem['suppliers_account']; $supplierItem['suppliers_name'] = $queryItem['suppliers_name']; $supplierArray[] = $supplierItem; } // 正常从这里返回 Ajax::header(); echo Ajax::buildResult(null, null, $supplierArray); return; out_fail: Ajax::header(); echo Ajax::buildResult(null, null, array()); }
/** * * 批量下载订单 * */ public function Download($f3) { // 权限检查 $this->requirePrivilege('manage_order_excel_download'); // 参数验证 $validator = new Validator($f3->get('GET')); //表单查询 $formQuery = array(); $formQuery['suppliers_id'] = $validator->digits()->min(1)->filter('ValidatorIntValue')->validate('suppliers_id'); $formQuery['goods_id'] = $validator->digits()->min(1)->filter('ValidatorIntValue')->validate('goods_id'); if (empty($formQuery['suppliers_id']) && empty($formQuery['goods_id'])) { $this->addFlashMessage('供货商ID 和 商品ID 不能同时为空'); goto out_fail; } //付款时间 $payTimeStartStr = $validator->validate('pay_time_start'); $payTimeStart = Time::gmStrToTime($payTimeStartStr) ?: null; $payTimeEndStr = $validator->validate('pay_time_end'); $payTimeEnd = Time::gmStrToTime($payTimeEndStr) ?: null; $formQuery['pay_time'] = array($payTimeStart, $payTimeEnd); //额外退款,时间 $extraRefundTimeStartStr = $validator->validate('extra_refund_time_start'); $extraRefundTimeStart = Time::gmStrToTime($extraRefundTimeStartStr) ?: null; $extraRefundTimeEndStr = $validator->validate('extra_refund_time_end'); $extraRefundTimeEnd = Time::gmStrToTime($extraRefundTimeEndStr) ?: null; $formQuery['extra_refund_time'] = array($extraRefundTimeStart, $extraRefundTimeEnd); if (!$this->validate($validator)) { goto out_fail; } if (Utils::isBlank($formQuery['suppliers_id']) || (Utils::isBlank($payTimeStart) || Utils::isBlank($payTimeEnd)) && (Utils::isBlank($extraRefundTimeStart) || Utils::isBlank($extraRefundTimeEnd))) { $this->addFlashMessage('按照供货商下载订单必须提供时间段参数'); goto out_fail; } $condArray = array(); $condArray[] = array('oi.order_id = og.order_id'); $condArray[] = array('order_goods_status > 0'); // 表单查询 $condArray = array_merge($condArray, QueryBuilder::buildQueryCondArray($formQuery)); $orderGoodsService = new OrderGoodsService(); $orderGoodsArray = $orderGoodsService->_fetchArray(array('order_info' => 'oi', 'order_goods' => 'og'), 'og.*, oi.add_time, oi.pay_time, oi.consignee, oi.mobile, oi.tel, oi.zipcode, oi.postscript, oi.address', $condArray, array('order' => 'oi.order_id asc, og.rec_id asc, og.goods_id asc'), 0, $f3->get('sysConfig[max_query_record_count]'), 0); //最多限制 max_query_record_count 条记录 // 转换显示格式 foreach ($orderGoodsArray as &$orderGoodsItem) { $orderGoodsItem['add_time'] = Time::gmTimeToLocalTimeStr($orderGoodsItem['add_time'], 'Y-m-d H:i:s'); $orderGoodsItem['pay_time'] = Time::gmTimeToLocalTimeStr($orderGoodsItem['pay_time'], 'Y-m-d H:i:s'); $orderGoodsItem['extra_refund_time'] = Time::gmTimeToLocalTimeStr($orderGoodsItem['extra_refund_time'], 'Y-m-d H:i:s'); $orderGoodsItem['suppliers_total_price'] = $orderGoodsItem['suppliers_price'] * $orderGoodsItem['goods_number']; $orderGoodsItem['order_goods_status_desc'] = OrderGoodsService::$orderGoodsStatusDesc[$orderGoodsItem['order_goods_status']]; } unset($orderGoodsItem); require_once PROTECTED_PATH . '/Vendor/PHPExcel/Settings.php'; // 设置Excel缓存,防止数据太多拖死了程序 \PHPExcel_Settings::setCacheStorageMethod(\PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp); // 导出为 Excel 格式 $objPHPExcel = new \PHPExcel(); // 设置工作 sheet $objPHPExcel->setActiveSheetIndex(0); $activeSheet = $objPHPExcel->getActiveSheet(); // 取得快递公司列表 $expressService = new ExpressService(); $expressArray = $expressService->fetchExpressArray(); //输出快递公司参考表 $this->outputExpressArray($activeSheet, $expressArray); //释放内存 unset($expressArray); unset($expressService); // 格式化数据 $rowIndex = 1; $lastOrderId = 0; $orderGoodsArraySize = count($orderGoodsArray); // 输出头部信息 $this->outputHeaderRow($activeSheet, $rowIndex); $rowIndex++; // 换行 for ($orderGoodsIndex = 0; $orderGoodsIndex < $orderGoodsArraySize; $orderGoodsIndex++) { // 取得这行数据 $orderGoodsItem = $orderGoodsArray[$orderGoodsIndex]; if ($lastOrderId != $orderGoodsItem['order_id']) { $lastOrderId = $orderGoodsItem['order_id']; // 不同的订单,需要特殊处理 $rowIndex += 2; // 跳过 2 行 } // 输出数据 //$this->outputDataRow($activeSheet, $rowIndex, $orderGoodsItem, array('goods_name', 'goods_id')); $this->outputDataRow($activeSheet, $rowIndex, $orderGoodsItem); $rowIndex++; // 换行 } $fileName = '订单下载_' . $formQuery['goods_id'] . '_' . Time::gmTimeToLocalTimeStr($payTimeStart, 'Y-m-d_H-i-s') . '__' . Time::gmTimeToLocalTimeStr($payTimeEnd, 'Y-m-d_H-i-s'); // 输出为 Excel5 格式 $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); header('Content-Type: application/vnd.ms-excel'); if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE")) { header('Content-Disposition: attachment; filename="' . urlencode($fileName) . '.xls"'); } else { header('Content-Disposition: attachment; filename="' . $fileName . '.xls"'); } header('Cache-Control: max-age=0'); $objWriter->save('php://output'); //输出到浏览器 die; out_fail: // 失败,打印错误消息 $flashMessageArray = $this->flashMessageArray; foreach ($flashMessageArray as $flashMessage) { echo $flashMessage . '<br />'; } }
public function post($f3) { // 权限检查 $this->requirePrivilege('manage_order_settle'); // 参数验证 $validator = new Validator($f3->get('POST')); $payTimeStart = $validator->required()->digits()->validate('payTimeStart'); $payTimeEnd = $validator->required()->digits()->validate('payTimeEnd'); $suppliers_id = $validator->required()->digits()->validate('suppliers_id'); if (!$this->validate($validator)) { goto out; } $orderGoodsIdArray = $validator->validate('orderGoodsIdArray'); $memo = $validator->validate('memo'); if (empty($orderGoodsIdArray) || !is_array($orderGoodsIdArray)) { $this->addFlashMessage('没有订单需要结算'); goto out; } // 取得供货商的信息 $userSupplierService = new UserSupplierService(); $supplier = $userSupplierService->loadSupplierById($suppliers_id); if ($supplier->isEmpty()) { $this->addFlashMessage('供货商不存在'); goto out; } // 取得所有 order_goods 记录 $orderGoodsService = new OrderGoodsService(); $orderGoodsArray = $orderGoodsService->_fetchArray('order_goods', 'rec_id, order_goods_status, goods_number,suppliers_id, suppliers_price, suppliers_shipping_fee, suppliers_refund, shipping_id', array(array(QueryBuilder::buildInCondition('rec_id', $orderGoodsIdArray))), array('order' => 'rec_id asc'), 0, $f3->get('sysConfig[max_query_record_count]'), 0); //最多限制 max_query_record_count 条记录 if (empty($orderGoodsArray)) { $this->addFlashMessage('没有订单需要结算'); goto out; } // 检查订单,计算订单结算金额 $totalGoodsPrice = 0; $totalShippingFee = 0; $totalRefund = 0; $totalOrderGoodsCount = 0; //剔除非法的 orderGoodsId $invalidOrderGoodsIdArray = array(); foreach ($orderGoodsArray as $orderGoodsItem) { if (OrderGoodsService::OGS_UNPAY == $orderGoodsItem['order_goods_status'] || $orderGoodsItem['suppliers_id'] != $suppliers_id || $orderGoodsItem['shipping_id'] <= 0) { // 非法订单,剔除掉 $invalidOrderGoodsIdArray[] = $orderGoodsItem['rec_id']; continue; } $totalGoodsPrice += $orderGoodsItem['goods_number'] * $orderGoodsItem['suppliers_price']; $totalShippingFee += $orderGoodsItem['suppliers_shipping_fee']; $totalRefund += $orderGoodsItem['suppliers_refund']; $totalOrderGoodsCount++; } //剔除非法的 orderGoodsId $orderGoodsIdArray = array_diff($orderGoodsIdArray, $invalidOrderGoodsIdArray); if (empty($orderGoodsIdArray)) { $this->addFlashMessage('没有订单需要结算'); goto out; } // 取得当前结算的管理员 $authAdminUser = AuthHelper::getAuthUser(); $dbEngine = DataMapper::getDbEngine(); try { // 我们这里需要事务保障 $dbEngine->begin(); //创建 order_settle 记录 $orderSettleService = new OrderSettleService(); $orderSettle = $orderSettleService->loadOrderSettleBySettleId(0); $orderSettle->user_id = $authAdminUser['user_id']; $orderSettle->user_name = $authAdminUser['user_name']; $orderSettle->settle_start_time = $payTimeStart; $orderSettle->settle_end_time = $payTimeEnd; $orderSettle->suppliers_id = $suppliers_id; $orderSettle->suppliers_name = $supplier['suppliers_name']; $orderSettle->suppliers_goods_price = $totalGoodsPrice; $orderSettle->suppliers_shipping_fee = $totalShippingFee; $orderSettle->suppliers_refund = $totalRefund; $orderSettle->create_time = Time::gmTime(); $orderSettle->memo = $memo; $orderSettle->save(); // 更新 order_goods ,设置上 settle_id $sql = "update " . DataMapper::tableName('order_goods') . ' set settle_id = ? where ' . QueryBuilder::buildInCondition('rec_id', $orderGoodsIdArray); $dbEngine->exec($sql, $orderSettle->settle_id); $dbEngine->commit(); $this->addFlashMessage('成功创建结算记录'); } catch (\Exception $e) { $dbEngine->rollback(); $this->addFlashMessage('数据库读写错误'); } out: // 回到结算页面 RouteHelper::reRoute($this, RouteHelper::getRefer(), false); }