request->post(); $ghsId = $post['ghsId'] ?? 0; $payMode = $post['payMode'] ?? 0; $payWay = $post['payWay'] ?? 0; $ghsInfo = GhsClass::getById($ghsId); if (empty($ghsInfo)) { util::fail('没有找到供货商'); } if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) { 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数据是不是同个供货商的 $ids = array_unique(array_filter(array_column($productList, 'productId'))); $productInfoList = ProductClass::getByIds($ids, null, 'id'); if (!empty($productInfoList)) { foreach ($productInfoList as $currentInfo) { if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) { util::fail('只能选择同一家的商品哦'); } } if (count($productInfoList) != count($ids)) { util::fail('存在无效商品'); } } else { util::fail('花材不存在'); } //供货商预订单最低公斤数要求和每公斤服务费 $ghsInfo['kiloFee'] = 0; $ghsInfo['miniKilo'] = 0; $post['product'] = $productList; $packCost = 0; $post['packCost'] = $packCost; $post['sendCost'] = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0; $respond = PurchaseService::createPurchase($post, $ghsInfo); if ($payMode == 0) { //欠款 PurchaseService::debt($respond); } else { //线下支付 PurchaseService::payAfter($respond, $payWay); $saleId = $respond->saleId ?? 0; $order = OrderClass::getById($saleId, true); if (empty($order)) { util::fail('没有找到订单'); } OrderService::payAfter($order, $payWay, true); } $transaction->commit(); util::success($respond); } catch (Exception $e) { $transaction->rollBack(); util::fail(); } } //生成采购单 ssh 2021.1.17 public function actionCreateOrder() { $post = Yii::$app->request->post(); //多颜色花材数据结转换 $colorItem = $post['xj'] ?? []; $newXj = []; if (!empty($colorItem)) { $colorData = json_decode($colorItem, true); if (!empty($colorData) && is_array($colorData)) { foreach ($colorData as $colorList) { if (!empty($colorList)) { foreach ($colorList as $color) { $ptItemId = $color['ptItemId']; $newXj[$ptItemId][] = $color; } } } } } $post['xj'] = $newXj; $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('这个是你自己的批发店'); } $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); $book = $post['book'] ?? 0; if ($isOpen == 0 && $book == 0) { util::fail('本店已休息,请选择其它门店'); } if ($book == 1) { $sendTimeWant = isset($post['sendTimeWant']) && !empty($post['sendTimeWant']) ? $post['sendTimeWant'] : date("Y-m-d"); $future = date("Y-m-d", strtotime('+2 day')); if (strtotime($sendTimeWant) < strtotime($future)) { util::fail('请选择未来二天或更后时间'); } $nowTime = strtotime(date("Y-m-d")); if (strtotime($sendTimeWant) < $nowTime) { util::fail('不能选择过去时间'); } } $ids = array_unique(array_filter(array_column($productList, 'productId'))); $productInfoList = ProductClass::getByIds($ids, null, 'id'); if (!empty($productInfoList)) { $presellData = []; $nowTime = strtotime(date("Y-m-d")); foreach ($productInfoList as $currentInfo) { if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $ghsMainId) { util::fail('只能选择同一家的商品哦'); } $currentName = $currentInfo['name'] ?? ''; $presell = $currentInfo['presell'] ?? 0; $presellData[] = $presell; if ($presell == 1) { if ($book == 1) { util::fail('预订时不能选择预售花材'); } else { if (isset($post['sendTimeWant']) == false || empty($post['sendTimeWant'])) { util::fail('请选择配送日期'); } $sendTimeWant = $post['sendTimeWant']; if (strtotime($sendTimeWant) < $nowTime) { util::fail('不能选择过去时间'); } $hasDateStr = $currentInfo['presellDate'] ?? []; $hasDate = explode(',', trim($hasDateStr)); if (empty($hasDate)) { util::fail('预售日期有问题,请提醒门店'); } if (in_array(strtotime($sendTimeWant), $hasDate) == false) { util::fail("有预售花材在{$sendTimeWant}没到货"); } $post['presell'] = 1; } } } if (count($productInfoList) != count($ids)) { util::fail('存在无效商品'); } $presellData = array_unique($presellData); if (count($presellData) > 1) { util::fail('预售和非预售花材不能一起下单'); } } else { util::fail('花材不存在'); } //供货商预订单最低公斤数要求和每公斤服务费 $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0; $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0; $post['product'] = $productList; $sendType = $post['sendType'] ?? 0; $sendCost = 0; if ($sendType == dict::getDict('sendType', 'thirdSend')) { $weight = 0; foreach ($productList as $itemData) { $currentWeight = $itemData['weight'] ?? 0; $weight = bcadd($currentWeight, $weight, 2); } $result = PurchaseClass::getDistanceFee($this->shop, $ghsShopInfo, $weight); $sendCost = $result['fee'] ?? 0; } $post['sendCost'] = $sendCost; //出车和发快递可以算打包费 $packCost = 0; if ($sendType == dict::getDict('sendType', 'thirdSend') || $sendType == dict::getDict('sendType', 'carGet')) { $packCost = $ghsShopInfo->packCost ?? 0; } $post['packCost'] = $packCost; $respond = PurchaseService::createPurchase($post, $ghsInfo); if ($sendType == 0) { if (getenv('YII_ENV') == 'production') { //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送 if ($ghsShopId == 4608) { if ($respond->actPrice < 500) { $transaction->rollBack(); util::fail('满500元支持免费配送'); } } //叶上花,满500元市区免费配送 if ($ghsShopId == 4804) { if ($respond->actPrice < 500) { $transaction->rollBack(); util::fail('满500元支持免费配送'); } } } } $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('订单不是待付款状态'); } if ($info->book == 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); if (!empty($order)) { $cgShopId = $info->shopId ?? 0; $cgShop = ShopClass::getById($cgShopId, true); if (!empty($cgShop)) { //采购成功小程序通知 \bizHd\notice\classes\NoticeClass::cgSuccessMiniNotice($cgShop, $info); } $shopId = $order->shopId ?? 0; $shop = ShopClass::getById($shopId, true); if (!empty($shop)) { //微信通知供货商 WxMessageClass::ghsHasNewOrderInform($shop, $order); } //订单生成时唤起在线打印 if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) { OrderClass::onlinePrint($order); $order->printNum += 1; $order->save(); } //花店采购供货商端收到APP通知 //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); $customName = $custom['name'] ?? ''; $customMobile = $custom['mobile'] ?? ''; $info['customName'] = $customName; $info['customMobile'] = $customMobile; $hasDebtPay = $custom['debt'] ?? 0; $book = $info['book'] ?? 0; $presell = $info['presell'] ?? 0; //预售不能记欠款 if ($book == 1) { $hasDebtPay = 0; } if ($presell == 1) { $hasDebtPay = 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)) { $currentShopId = $order->shopId ?? 0; $currentShop = ShopClass::getById($currentShopId, true); $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName; $currentMobile = $currentShop->mobile ?? ''; noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347'); util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]); } $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); } //计算运费 ssh 20221003 public function actionFreight() { $get = Yii::$app->request->get(); $shop = $this->shop; $shopLat = isset($shop->lat) ? $shop->lat : ''; $shopLong = isset($shop->long) ? $shop->long : ''; if (empty($shopLat) || empty($shopLong)) { util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]); } $ghsId = $get['ghsId'] ?? 0; $ghs = GhsClass::getById($ghsId, true); if (empty($ghs)) { util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]); } $ghsShopId = $ghs->shopId ?? 0; $ghsShop = ShopClass::getById($ghsShopId, true); if (empty($ghsShop)) { util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]); } $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : ''; $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : ''; if (empty($ghsShopLat) || empty($ghsShopLong)) { util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]); } $weight = $get['weight'] ?? 0; if (empty($weight)) { util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]); } $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight); util::success($respond); } //运费计算 lqh 2021.4.12 暂时返回固定值 public function actionFreight2() { //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']]); } }