request->post(); $ghsId = $post['ghsId'] ?? 0; //供应商 $ghsInfo = GhsClass::getById($ghsId); if (empty($ghsInfo)) { util::fail('没有找到供应商'); } $ghsShopId = $ghsInfo['shopId'] ?? 0; if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) { util::fail('不能向自己的批发店采购'); } $shop = $this->shop; $pfShopId = $shop->pfShopId ?? 0; if (!empty($pfShopId)) { util::fail('请在批发店进行采购'); } $connection = Yii::$app->db;//事务处理 $transaction = $connection->beginTransaction(); try { $post['book'] = $post['book'] ?? 0; $post['sjId'] = $this->sjId; $post['shopId'] = $this->shopId; $post['shopAdminId'] = $this->shopAdminId; $post['ghsId'] = $ghsId; $post['ghsName'] = $ghsInfo['name'] ?? ''; $post['ghsMobile'] = $ghsInfo['mobile'] ?? ''; $post['ghsAvatar'] = $ghsInfo['avatar'] ?? ''; $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? ''; $customId = $ghsInfo['customId'] ?? 0; $post['customId'] = $customId; $shopAdmin = $this->shopAdmin ?? null; $name = $shopAdmin->name ?? ''; $post['shopAdminName'] = $name; $post['mainId'] = $this->mainId; $product = $post['product'] ?? ''; $productList = json_decode($product, true); //判断product数据是不是同个供应商的 $ghsShopInfo = ShopClass::getById($ghsShopId, true); $ghsMainId = $ghsShopInfo->mainId ?? 0; $isOpen = ShopClass::isOpen($ghsShopInfo); if ($isOpen == 0 && $post['book'] == 0) { util::fail('商家已关店休息了'); } ProductClass::valid($productList, $ghsMainId); //供应商预订单最低公斤数要求和每公斤服务费 $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0; $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0; $post['product'] = $productList; $post['sendCost'] = isset($post['sendCost']) && is_numeric($post['sendCost']) ? $post['sendCost'] : 0; $respond = PurchaseService::createPurchase($post, $ghsInfo); $transaction->commit(); util::success($respond); } catch (Exception $e) { $transaction->rollBack(); util::fail(); } } //延期支付 ssh 2021.1.24 public function actionDebtPay() { $get = Yii::$app->request->get(); $orderSn = $get['orderSn'] ?? 0; $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true); PurchaseService::valid($info, $this->shopId); if ($info->status == 5) { util::fail('订单已取消'); } if ($info->status != 1) { util::fail('订单不是待付款状态'); } $ghsId = $info->ghsId; $ghs = GhsClass::getGhsInfo($ghsId); $customId = $ghs['customId'] ?? 0; $custom = CustomClass::getCustom($customId); if (empty($custom)) { util::fail('没有找到客户'); } if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) { util::fail('您没有延期付款权限'); } $connection = Yii::$app->db;//事务处理 $transaction = $connection->beginTransaction(); try { //延期支付 PurchaseService::debt($info); $transaction->commit(); //app新订单通知 $saleId = $info->saleId ?? 0; $order = OrderClass::getById($saleId, true); NoticeClass::ghsNewOrderNotice($order); } catch (Exception $e) { $transaction->rollBack(); util::fail('支付失败'); } util::success($info->attributes); } //余额支付 ssh 2021.1.24 public function actionBalancePay() { $shopAdmin = $this->shopAdmin; if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) { util::fail('超管才能操作'); } $get = Yii::$app->request->get(); $orderSn = $get['orderSn'] ?? 0; $password = $get['password'] ?? ''; // 重新获取,不加密的用户数据 $admin = AdminClass::getById($this->adminId, true); if (password_verify($password, $admin->payPassword) == false) { util::fail('密码错误'); } $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true); if (empty($info)) { util::fail('没有找到采购单'); } if ($info->status == 5) { util::fail('订单已取消'); } if ($info->status != 1) { util::fail('订单不是待付款状态'); } PurchaseService::valid($info, $this->shopId); $connection = Yii::$app->db;//事务处理 $transaction = $connection->beginTransaction(); try { //余额支付 purchaseService::balancePay($info); $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); util::fail('支付失败'); } util::success($info->attributes); } //采购记录 ssh 2021.1.24 public function actionList() { $get = Yii::$app->request->get(); $where = ['shopId' => $this->shopId]; $status = $get['status'] ?? 0; if (!empty($status)) { $where['status'] = $status; } $respond = PurchaseService::getPurchaseList($where); $respond['shop'] = $this->shop->attributes; util::success($respond); } //采购详情 ssh 2021.01.25 public function actionDetail() { $get = Yii::$app->request->get(); $id = $get['id'] ?? 0; $info = PurchaseService::getInfo($id, true, true); if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) { //util::fail('没有权限访问'); } $customId = $info['customId'] ?? 0; $custom = CustomClass::getById($customId); $hasDebtPay = $custom['debt'] ?? 0; $info['hasDebtPay'] = $hasDebtPay; $info['balance'] = $this->shop->balance ?? 0; util::success($info); } //可用的支付方式 ssh 2021.1.25 public function actionPayWay() { $button = [ ['type' => 'wxPay', 'show' => 1, 'disable' => 1], ['type' => 'debtPay', 'show' => 1, 'disable' => 0], ['type' => 'balancePay', 'show' => 1, 'disable' => 0], ]; util::success(['button' => $button]); } //待结款明细 ssh 2021.1.26 public function actionDebtList() { $get = Yii::$app->request->get(); $id = $get['id'] ?? 0; $info = GhsClass::getGhsInfo($id); GhsClass::valid($info, $this->shopId); $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES]; $searchTime = $get['searchTime'] ?? ''; if (!empty($searchTime)) { $startTime = $get['startTime'] ?? ''; $endTime = $get['endTime'] ?? ''; $period = dateUtil::formatTime($searchTime, $startTime, $endTime); $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]]; } $respond = PurchaseService::getDebtList($where); util::success($respond); } //微信支付 ssh 2021.4.11 public function actionWxPay() { ini_set('date.timezone', 'Asia/Shanghai'); $post = Yii::$app->request->post(); $couponId = $post['couponId'] ?? 0; $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0; $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true); if (empty($order)) { util::fail('订单号无效'); } if ($order->status == 5) { util::fail('订单已取消'); } if ($order->status != 1) { util::fail('订单不是待付款状态'); } $id = $order->id; $order->changePrice = 2; $order->save(); $deadline = $order->deadline; $current = time(); if (($current + 50) > strtotime($deadline)) { util::fail('订单已失效,请重新下单'); } $name = $orderSn; $totalFee = $order->realPrice; //强制使用小程序的miniOpenId $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : ''; if (empty($openId)) { util::fail('没有miniOpenId...'); } $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id'); //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调 $wxPayType = 0; if (httpUtil::isMiniProgram()) { $wxPayType = 1; } $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType; //订单30分钟后过期 $now = time(); $expireTime = $now + 1800; $wx = Yii::getAlias("@vendor/weixin"); require_once($wx . '/lib/WxPay.Api.php'); require_once($wx . '/example/WxPay.JsApiPay.php'); $input = new \WxPayUnifiedOrder(); $input->SetBody($name); $input->SetOut_trade_no($orderSn); $input->SetTotal_fee($totalFee * 100); $input->SetTime_start(date("YmdHis", $now)); //将流水类型、代金劵等传给微信再回传 $input->SetAttach($attach); $input->SetTime_expire(date("YmdHis", $expireTime)); $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/'); $input->SetTrade_type("JSAPI"); //花卉宝代为申请的微信支付 $merchantExtend = WxOpenClass::getWxInfo(); if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) { $input->SetSub_openid($openId); } else { $input->SetOpenid($openId); } //强制使用小程序的miniAppId $merchantExtend['wxAppId'] = $merchantExtend['miniAppId']; $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend); $tools = new \JsApiPay(); $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend); $newParams = json_decode($jsApiParameters, true); $newParams['id'] = $id; util::success($newParams); } //运费计算 lqh 2021.4.12 暂时返回固定值 public function actionFreight() { //lqh 2021.6.25 运费固定返回0 util::success(['sedCost' => 0]); $post = Yii::$app->request->post(); $ghsId = $post['ghsId'] ?? 0; //供应商 $ghsInfo = GhsClass::getById($ghsId); if (empty($ghsInfo)) { util::fail('没有找到供应商'); } //花材信息 $productJson = $post['product'] ?? ''; // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]'; if (empty($productJson)) { util::fail('请选择花材'); } $productList = json_decode($productJson, true); if (empty($productList)) { util::fail('请选择花材'); } $productList = PurchaseClass::mergeItemInfo($productList); $weight = 0; $price = 0; $productData = ProductClass::getProductMapData($productList); $bigNum = 0; foreach ($productList as $key => $val) { $productId = $val['productId']; $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']); $bigNum += $val['bigNum']; $weight = bcadd($w, $weight, 2); $ratio = $productData[$productId]['ratio'] ?? 0; //大小数量合并多少扎 $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio); //售卖价格 $itemPrice = $productData[$productId]['price'] ?? 0; //合计价格 $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2); } $ghsShopId = $ghsInfo['shopId'] ?? 0; $ghsSjId = $ghsInfo['sjId'] ?? 0; $sendTime = $post['sendTime'] ?? ''; if (!empty($sendTime)) { //配送时间不为空 // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约 $sendTimeInt = strtotime($sendTime); $now = time(); $diff = $sendTimeInt - $now; //因服务器写入时间,改为至少提前6分钟 if ($diff <= 360) { util::fail('配送时间至少提前6分钟'); } $sendTime = date('Y-m-d H:i', strtotime($sendTime)); } else { $sendTime = date('Y-m-d H:i', strtotime("+30 minute")); } //找供应商的客户信息,用里面的配送相关地址信息 $customId = $ghsInfo['customId'] ?? 0; $customInfo = CustomClass::getById($customId); if (empty($customInfo)) { util::fail('没有找到客户'); } //判断商家是否开启达达,若未开启,则使用自定义运费计算 $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId); if (!$shopNo) { //使用自定义运费 //util::fail('供应商暂未开通配送'); $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight); util::success(['sedCost' => $cost]); } //组装订单信息 $info = []; //发送放的商家信息,即供应商的相关信息 $info['sjId'] = $ghsSjId; $info['shopId'] = $ghsShopId; $info['actPrice'] = $price;//价格 $info['weight'] = $weight; $info['orderSn'] = \common\components\orderSn::getGhsOrderSn(); $info['bigNum'] = $bigNum; //配送地址,当前零售端的门店地址 $info['customName'] = $customInfo['name'] ?? ''; $info['customMobile'] = $customInfo['mobile'] ?? ''; $info['province'] = $customInfo['province'] ?? ''; $info['city'] = $customInfo['city'] ?? '';; $info['address'] = $customInfo['address'] ?? '';; $info['floor'] = $customInfo['floor'] ?? '';; $info['fullAddress'] = $customInfo['fullAddress'] ?? '';; $info['lat'] = $customInfo['lat'] ?? '';; $info['long'] = $customInfo['long'] ?? '';; $info['sendTime'] = $sendTime; $res = DadaExpressServices::queryDeliverFee($info); util::success(['sedCost' => $res['fee']]); } }