actPrice ?? 0; $finalDebtAmount = bcadd($debtAmount, $actPrice, 2); if ($finalDebtAmount > $debtLimit && $debtLimit > 0) { util::fail('请结清欠款再下单'); } } $customName = $custom['name'] ?? ''; $customNamePy = $custom['py'] ?? ''; $customMobile = $custom['mobile'] ?? ''; $purchaseId = $respond->id ?? 0; $shopAdminId = $data['shopAdminId'] ?? 0; $shopAdminName = $data['shopAdminName'] ?? ''; $sendType = $data['sendType'] ?? 1; $transType = $data['transType'] ?? 0; $couponId = $data['couponId'] ?? 0; $wlName = $data['wlName'] ?? ''; $discountType = $respond->discountType ?? dict::getDict('discountType', 'noDiscount'); $discountAmount = $respond->discountAmount ?? 0; $discount = $respond->discount ?? 0; $remark = $data['remark'] ?? ''; $xj = $data['xj'] ?? ''; $sendTimeWant = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d"); $orderType = 2; $book = $data['book'] ?? 0; $preSell = $data['presell'] ?? 0; $sendDistance = $data['sendDistance'] ?? 0; $mainId = $custom['ownMainId'] ?? 0; $fromType = dict::getDict('fromType', 'mall'); $arr = [ 'mainId' => $mainId, 'book' => $book, 'product' => $product, 'sjId' => $currentSjId, 'shopId' => $currentShopId, 'sendCost' => $sendCost, 'packCost' => $packCost, 'customId' => $customId, 'customName' => $customName, 'customNamePy' => $customNamePy, 'customMobile' => $customMobile, 'province' => $custom['province'] ?? '', 'city' => $custom['city'] ?? '', 'dist' => $custom['dist'] ?? '', 'address' => $custom['address'] ?? '', 'floor' => $custom['floor'] ?? '', 'fullAddress' => $custom['fullAddress'] ?? '', 'showAddress' => $custom['showAddress'] ?? '', 'long' => $custom['long'] ?? '', 'lat' => $custom['lat'] ?? '', 'purchaseId' => $purchaseId, 'sendType' => $sendType, 'customShopAdminId' => $shopAdminId, 'customShopAdminName' => $shopAdminName, 'ghsId' => $ghsId, 'cgStyle' => dict::getDict('cgStyle', 'hd'), 'couponId' => $couponId, 'discountType' => $discountType, 'discountAmount' => $discountAmount, 'remark' => $remark, 'xj' => $xj, 'sendTimeWant' => $sendTimeWant, 'discount' => $discount, 'orderType' => $orderType, 'fromType' => $fromType, 'presell' => $preSell, 'wlName' => $wlName, 'live' => $live, 'localOrder' => $localOrder, 'transType' => $transType, 'sendDistance' => $sendDistance, ]; $result = OrderClass::addOrder($arr,$custom); $respond->saleId = $result['id']; $respond->save(); return $respond; } //获取采购单信息 ssh 2021.1.24 public static function getInfo($id, $showButton = false, $showProduct = false) { $purchase = PurchaseClass::getById($id, true); if (empty($purchase)) { util::fail('没有找到采购单'); } $info = $purchase->attributes; $respond = PurchaseClass::groupPurchase([$info], $showButton, $showProduct); return current($respond); } //检查采购单 public static function valid($info, $shopId) { if (empty($info)) { util::fail('没有找到采购单'); } if (isset($info->shopId) && $info->shopId == $shopId) { return true; } util::fail('无法操作'); } //采购记录 ssh 2021.1.24 public static function getPurchaseList($where) { $data = PurchaseClass::getList('*', $where, 'addTime DESC'); $data['list'] = self::groupInfo($data['list']); return $data; } //组合信息 ssh 2021.1.25 public static function groupInfo($list) { if (empty($list)) { return $list; } foreach ($list as $key => $val) { $list[$key]['ghsAvatar'] = imgUtil::groupImg($val['ghsAvatar']); } return $list; } //所有的欠款采购单 ssh 2021.1.26 public static function getDebtList($where) { $list = PurchaseClass::getAllList('*', $where, 'addTime DESC'); $count = PurchaseClass::getCount($where); return ['list' => $list, 'debtNum' => $count]; } //采购单失效 ssh 2021.5.14 public static function expire($purchase) { if ($purchase->status != PurchaseClass::STATUS_UN_PAY) { util::fail('不能取消'); } //采购单失效 $id = $purchase->id; $purchase = PurchaseClass::cancel($id); $saleId = $purchase->saleId; //销售单失效 OrderClass::cancel($saleId); $couponId = $purchase->hbId; if (!empty($couponId)) { $coupon = ShopCouponClass::getLockById($couponId); ShopCouponClass::rollback($coupon); } // OrderClass::cancel($saleId) 中已经清理了 //清空限购缓存 // $order = OrderClass::getById($saleId, true); // if (!empty($order)) { // $customId = $order->customId ?? 0; // $orderSn = $order->orderSn; // if (!empty($customId)) { // $orderItemList = OrderItemClass::getOrderItem($orderSn); // if (!empty($orderItemList)) { // foreach ($orderItemList as $item) { // $productId = $item['productId'] ?? 0; // $num = floor($item['num'] ?? 0); // if (!empty($productId) && $num > 0) { // ProductClass::baseClearLimitBuy($productId, $customId, $num); // } // } // } // } // } return $purchase; } //订单延期支付 ssh 2021.1.24 public static function debt($info) { $payWay = dict::getDict('payWay', 'debtPay'); $saleId = $info->saleId ?? 0; $order = OrderClass::getById($saleId, true); //批发店开的单,客户付款了,不需要打印也不需要微信通知 if ($info->cgStyle == dict::getDict('cgStyle', 'ghs')) { $order->needPrint = dict::getDict('needPrint', 'noNeed'); $order->save(); Yii::$app->params['noNeedWxNotice'] = 1; } self::payAfter($info, $payWay); OrderService::payAfter($order, $payWay, true); } //余额支付 ssh 2021.1.24 public static function balancePay($info) { $payWay = dict::getDict('payWay', 'balancePay'); self::payAfter($info, $payWay); $saleId = $info->saleId ?? 0; $order = OrderClass::getById($saleId, true); OrderService::payAfter($order, $payWay, true); } //付款码支付处理流程 ssh 20211231 public static function codePay($payWay, $orderSn, $transactionId = '', $openId = '', $aliUserId = '') { //4秒内同个订单不允许重复请求 $cacheKey = 'code_pay_f2f_' . $orderSn; $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]); if (!empty($has)) { Yii::info("付款码支付,重复查询支付结果:orderSn:{$orderSn}"); noticeUtil::push("付款码支付,重复查询支付结果:orderSn:{$orderSn}", '15280215347'); util::fail('请稍等...'); } Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']); $info = self::getByCondition(['orderSn' => $orderSn], true); if (empty($info)) { $msg = "没有找到订单 orderSn:{$orderSn}"; Yii::info($msg); util::fail($msg); } //解决并发问题 $id = $info->id; $info = PurchaseClass::getLockById($id); $info->onlinePay = dict::getDict('onlinePay', 'yes'); $info->codePay = 1; $info->thirdNo = $transactionId; $info->payOpenId = $openId; $info->aliUserId = $aliUserId; //自取订单 $info->getType = PurchaseClass::GET_TYPE_SELF_GET; $info->save(); self::payAfter($info, $payWay); $saleId = $info->saleId ?? 0; $order = OrderClass::getById($saleId, true); $order->onlinePay = dict::getDict('onlinePay', 'yes'); $order->codePay = 1; //自取订单 $order->sendType = OrderClass::SEND_TYPE_NO; //不需要小票 $order->needPrint = dict::getDict('needPrint', 'noNeed'); $order->save(); OrderService::payAfter($order, $payWay, true); } //采购订单支付成功后回调处理流程 public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId = '') { $info = self::getByCondition(['orderSn' => $orderSn], true); if (empty($info)) { $ghsName = '无供货商'; preg_match('/\d+/', $orderSn, $m); if ($m[0]) { $cgSnId = $m[0]; $snData = PurchaseSnClass::getById($cgSnId, true); $ghsId = $snData->ghsId ?? 0; if (!empty($ghsId)) { $ghs = \bizHd\ghs\classes\GhsClass::getById($ghsId, true); $ghsName = $ghs->name; } } noticeUtil::push('花店采购,付款成功,支付回调,单号:' . $orderSn . ',没有找到订单 ' . $ghsName . ',' . $attach, '15280215347'); $msg = "没有找到订单 orderSn:{$orderSn}"; util::fail($msg); } if ($info->actPrice != $totalFee) { noticeUtil::push("花店采购,付款成功,支付回调,单号:{$orderSn},订单金额与回调通知金额不一致{$info->actPrice} {$totalFee} {$attach}", '15280215347'); $msg = "订单金额与回调通知金额不一致 orderSn:{$orderSn} {$info->actPrice} {$totalFee}"; Yii::info($msg); util::fail($msg); } //临时解决微信一秒内通知二次问题 $id = $info->id; $info = PurchaseClass::getLockById($id); //由批发店发起的采购单,零售付款时不需要微信通知 if ($info->cgStyle == dict::getDict('cgStyle', 'ghs')) { Yii::$app->params['noNeedWxNotice'] = 1; } $info->onlinePay = dict::getDict('onlinePay', 'yes'); $info->thirdNo = $transactionId; $info->save(); self::payAfter($info, $payWay); $saleId = $info->saleId ?? 0; $order = OrderClass::getById($saleId, true); $order->onlinePay = dict::getDict('onlinePay', 'yes'); $order->save(); OrderService::payAfter($order, $payWay, true); } //第三方支付、余额支付和欠款支付后调用的流程 ssh 2021.4.27 public static function payAfter($info, $payWay) { if (empty($info)) { util::fail('没有找到订单'); } $deadline = $info->deadline; $book = $info->book ?? 0; $orderSn = $info->orderSn ?? ''; $current = date("Y-m-d H:i:s"); if (isset($info->status) == false) { noticeUtil::push('重点注意,采购单:' . $orderSn . ',客户付钱了但没有找到订单的状态', '15280215347'); util::fail('没有找到订单状态'); } if ($info->status == PurchaseClass::STATUS_CANCEL) { noticeUtil::push('注意花店采购单:' . $orderSn . ',客户付款了但订单已取消,关键词 cancel_get_money', '15280215347'); $saleId = $info->saleId; $sale = OrderClass::getById($saleId, true); if (!empty($sale)) { $ghsShopId = $sale->shopId; $ghsShop = ShopClass::getById($ghsShopId, true); if (!empty($ghsShop)) { CgRefundClass::onlyRefundMoney($ghsShop, $info); } } util::stop("SUCCESS"); } if (isset($info->payStatus) && $info->payStatus == 1) { util::fail('订单已经付过款了'); } if ($deadline <= $current) { util::fail('订单已过期了哦'); } //零售采购单状态变化 $info->payWay = $payWay; $info->status = PurchaseClass::STATUS_UN_SEND; $info->payStatus = 1; $payTime = date("Y-m-d H:i:s"); $info->payTime = $payTime; if ($payWay == dict::getDict('payWay', 'debtPay')) { //欠款支付 if (isset($info->actPrice) && $info->actPrice == 0) { $info->debt = 0; } else { $info->debt = 1; } $info->remainDebtPrice = $info->orderPrice ?? 0; $info->debtPrice = $info->orderPrice ?? 0; } $info->save(); $orderSn = $info->orderSn ?? ''; PurchaseItemClass::updateByCondition(['orderSn' => $orderSn], ['payTime' => $payTime]); //零售门店资产变化 $shopId = $info['shopId'] ?? 0; $shop = ShopClass::getLockById($shopId); if (empty($shop)) { util::fail('没有找到门店63'); } $mainId = $shop->mainId ?? 0; $main = MainClass::getLockById($mainId); if (empty($main)) { util::fail('没有main信息16'); } $realPrice = $info->realPrice; $actPrice = $info->actPrice; //余额支付引起余额减少 if ($payWay == dict::getDict('payWay', 'balancePay')) { ShopClass::purchaseReduceBalance($main, $shop, $realPrice, $info); } $main->totalPurchase = bcadd($main->totalPurchase, $actPrice, 2); $currentExpend = bcadd($main->totalExpend, $actPrice, 2); $main->totalExpend = $currentExpend; $main->cgUnPay -= 1; if ($book == 1) { $main->cgUnSend += 1; } else { $main->cgUnSend += 1; } if ($payWay == dict::getDict('payWay', 'debtPay')) { //应付供货商款增加 $currentMayPay = bcadd($main->mayPay, $realPrice, 2); $main->mayPay = $currentMayPay; } $main->save(); //支出统计 StatOutClass::updateOrInsert($main, $shop, $info->orderPrice); //支出流水 $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id'); $sjId = $info->sjId ?? 0; $shopId = $info->shopId ?? 0; $event = '采购'; $capitalData = [ 'capitalType' => $capitalType, 'io' => 0, 'totalExpend' => $currentExpend, 'payWay' => $payWay, 'amount' => $actPrice, 'sjId' => $sjId, 'shopId' => $shopId, 'event' => $event, 'mainId' => $mainId, ]; ShopCapitalClass::addCapital($capitalData); //采购总量统计 $cgNum = $info->bigNum ?? 0; StatCgClass::replace($main, $shop, $info->orderPrice, $cgNum); //按供货商分别统计采购量 $num = $info->bigNum ?? 0; $amount = $info->orderPrice ?? 0; StatCgGhsClass::hdCgGhsReplace(true, $info, $amount, $num); //供货商资产变化 $ghsId = $info->ghsId ?? 0; $ghs = GhsClass::getLockById($ghsId); if (empty($ghs)) { util::fail('没有找到供货商'); } if ($payWay == dict::getDict('payWay', 'debtPay')) { \bizHd\ghs\classes\GhsClass::cgDebtAmountAdd($ghs, $info); } $ghs->expendAmount = bcadd($ghs->expendAmount, $actPrice, 2); $ghs->expendNum += 1; $ghs->save(); } }