OrderController.php 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. <?php
  2. namespace mall\controllers;
  3. use biz\product\classes\ProductClass;
  4. use biz\shop\classes\ShopClass;
  5. use biz\wx\classes\WxMessageClass;
  6. use bizHd\custom\classes\HdClass;
  7. use bizHd\distribution\classes\DistributionOrderClass;
  8. use bizHd\express\classes\HdDeliveryOrderClass;
  9. use bizHd\order\classes\OrderClass as HdOrderClass;
  10. use bizHd\order\classes\PsMethodClass;
  11. use bizHd\refund\classes\MallRefundApplyClass;
  12. use bizHd\hb\classes\HbClass;
  13. use bizHd\hb\classes\HbScopeClass;
  14. use bizHd\homePageConfig\classes\HomePageModuleClass;
  15. use bizHd\order\classes\ScanPayClass;
  16. use bizHd\wx\classes\WxOpenClass;
  17. use bizMall\custom\classes\CustomClass;
  18. use bizMall\goods\classes\GoodsClass;
  19. use bizMall\order\classes\OrderClass;
  20. use bizMall\order\classes\OrderForwardClass;
  21. use bizMall\order\classes\OrderItemClass;
  22. use bizMall\order\services\OrderService;
  23. use bizMall\promote\services\CouponService;
  24. use bizMall\saas\services\RegionService;
  25. use bizMall\shop\classes\ShopExtClass;
  26. use bizMall\user\services\UserService;
  27. use common\components\dateUtil;
  28. use common\components\delivery\util\DeliveryQuoteUtil;
  29. use common\components\dict;
  30. use common\components\imgUtil;
  31. use common\components\mapUtil;
  32. use common\components\noticeUtil;
  33. use common\components\orderSn;
  34. use common\components\stringUtil;
  35. use Yii;
  36. use common\components\util;
  37. use common\components\lakala\Lakala;
  38. use common\components\IntraCityExpress;
  39. class OrderController extends BaseController
  40. {
  41. //二维码收款使用
  42. public $guestAccess = ['order-relate', 'fast-pay'];
  43. public function actionInform()
  44. {
  45. $get = Yii::$app->request->get();
  46. $hdId = $get['hdId'] ?? 0;
  47. $account = $get['account'] ?? 0;
  48. $page = $get['page'] ?? '';
  49. $userId = $this->userId;
  50. noticeUtil::push("零售客户下单报错 hdId:{$hdId} account:{$account} userId:{$userId} page:{$page}", '15280215347');
  51. }
  52. //记账单列表 ssh 20250627
  53. public function actionDebtList()
  54. {
  55. $get = Yii::$app->request->get();
  56. $id = $get['id'] ?? 0;
  57. $where = [];
  58. $where['userId'] = $this->userId;
  59. if (!empty($id)) {
  60. $hd = \bizMall\hd\classes\HdClass::getById($id, true);
  61. if (empty($hd)) {
  62. util::fail('无效的花店');
  63. }
  64. $where['hdId'] = $id;
  65. }
  66. $where['debt'] = 1;
  67. $searchTime = $get['searchTime'] ?? '';
  68. if (!empty($searchTime)) {
  69. $startTime = $get['startTime'] ?? '';
  70. $endTime = $get['endTime'] ?? '';
  71. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  72. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  73. }
  74. $respond = OrderClass::getDebtList($where);
  75. util::success($respond);
  76. }
  77. //新计算运费
  78. public function actionNewFreight()
  79. {
  80. $post = Yii::$app->request->post();
  81. $hasMap = dict::getDict('hasMap');
  82. if ($hasMap == 0) {
  83. util::fail('暂不支持跑腿功能');
  84. }
  85. $mainData = $this->main;
  86. $wxStoreId = $mainData->wxStoreId ?? '';
  87. if (empty($wxStoreId)) {
  88. util::fail('商家还没有开通跑腿');
  89. }
  90. $cargoName = $post['cargo_name']; // 商品名称
  91. $cargo = [
  92. 'cargo_name' => $cargoName, // 商品名称
  93. 'cargo_weight' => intval($post['weight'] * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  94. 'cargo_price' => intval($post['price'] * 100), // 单位:分 -- 把元转换为分
  95. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  96. 'cargo_num' => intval($post['package_num'])
  97. ];
  98. $originalLng = doubleval($post['user_lng']);
  99. $originalLat = doubleval($post['user_lat']);
  100. $userName = !empty($post['user_name']) ? $post['user_name'] : '游客';
  101. $orderData = [
  102. 'wx_store_id' => $wxStoreId,
  103. 'user_name' => $userName,
  104. 'user_phone' => $post['user_phone'],
  105. 'user_lng' => $originalLng,
  106. 'user_lat' => $originalLat,
  107. 'user_address' => $post['user_address'],
  108. 'cargo' => $cargo,
  109. ];
  110. //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  111. $result = IntraCityExpress::preAddOrder($orderData);
  112. if ($result['errcode'] === 0) {
  113. util::success($result, "查询成功");
  114. } else {
  115. Yii::error("运费查询失败:" . json_encode($result));
  116. $errMsg = $result['errmsg'] ?? '';
  117. $errCode = $result['errcode'] ?? '';
  118. noticeUtil::push("运费查询失败,原因:" . $errMsg . ",编号:" . $errCode, '15280215347');
  119. util::fail('运费查询失败,请确认是否超过20公斤,编号896');
  120. }
  121. }
  122. //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
  123. public function actionFreight()
  124. {
  125. $get = Yii::$app->request->get();
  126. $shop = $this->shop;
  127. $shopLat = isset($shop->lat) ? $shop->lat : '';
  128. $shopLong = isset($shop->long) ? $shop->long : '';
  129. // 判断地图功能是否正常。0 表示地图定位不可用,全部经纬度数据失效
  130. if (dict::getDict('hasMap') == 0) {
  131. $shopLat = '';
  132. $shopLong = '';
  133. }
  134. if (empty($shopLat) || empty($shopLong)) {
  135. util::success([
  136. 'distance' => 0,
  137. 'showDistance' => 0,
  138. 'fee' => 0,
  139. 'hasMap' => dict::getDict('hasMap'),
  140. 'shop' => 'fail' //门店引起的失败
  141. ]);
  142. }
  143. $userLat = $get['lat'] ?? '';
  144. $userLong = $get['long'] ?? '';
  145. if (empty($userLat) || empty($userLong)) {
  146. util::success([
  147. 'distance' => 0,
  148. 'showDistance' => 0,
  149. 'fee' => 0,
  150. 'hasMap' => dict::getDict('hasMap'),
  151. 'client' => 'fail' //用户端定位失败
  152. ]);
  153. }
  154. $weight = $get['weight'] ?? 0;
  155. if (empty($weight)) {
  156. util::success([
  157. 'distance' => 0,
  158. 'showDistance' => 0,
  159. 'fee' => 0,
  160. 'hasMap' => dict::getDict('hasMap'),
  161. 'weight' => 'fail' //重量不对
  162. ]);
  163. }
  164. //data: {distance: "321", showDistance: "0.3", fee: "12.00"}
  165. $respond = OrderClass::getDistanceFee($userLat, $userLong, $shopLat, $shopLong, $weight);
  166. util::success($respond);
  167. }
  168. // 零售商的限购花材信息(与批发端 PurchaseController::actionLimitBuyInfo 对应,按花店客户与 hd_limit_buy Redis)
  169. public function actionLimitBuyInfo()
  170. {
  171. $post = Yii::$app->request->post();
  172. $list = $post['list'] ?? [];
  173. if (empty($list) || !is_array($list)) {
  174. util::fail('请选择花材');
  175. }
  176. $hd = $this->hd;
  177. if (empty($hd)) {
  178. util::fail('没有找到花店');
  179. }
  180. if ($hd->shopId != $this->shopId) {
  181. util::fail('不是你的花店');
  182. }
  183. $hdCustomId = $hd->customId ?? 0;
  184. if (empty($hdCustomId)) {
  185. util::fail('没有找到客户');
  186. }
  187. $respond = \bizHd\product\classes\ProductClass::getLimitBuyInfoByList($list, $hdCustomId);
  188. util::success($respond);
  189. }
  190. //购买花材 ssh 20220511
  191. public function actionBuyItem()
  192. {
  193. $post = Yii::$app->request->post();
  194. $post['sjId'] = $this->sjId;
  195. $post['shopId'] = $this->shopId;
  196. $post['payWay'] = 0;
  197. $post['mainId'] = $this->mainId ?? 0;
  198. $post['userId'] = $this->userId ?? 0;
  199. //避免重复提交
  200. $userId = $this->userId;
  201. util::checkRepeatCommit($userId, 3);
  202. $shop = $this->shop;
  203. $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
  204. if ($openShop == 0) {
  205. util::fail('已休店');
  206. }
  207. //普莲花艺、叶上花、丰行、小武鲜花、九江云朵、源花汇、紫荆不能在花卉宝下单,多处要同步修改,关键词ls_mall_not_open
  208. $mainId = $this->mainId;
  209. if (getenv('YII_ENV') == 'production') {
  210. if (in_array($mainId, [40057, 7779, 42940, 26374, 10536, 65381])) {
  211. util::fail('暂时无法访问');
  212. }
  213. } else {
  214. if (in_array($mainId, [0, 1])) {
  215. util::fail('暂时无法访问');
  216. }
  217. }
  218. $hdId = $post['hdId'] ?? 0;
  219. $hd = HdClass::getById($hdId, true);
  220. if (empty($hd)) {
  221. util::fail('没有找到花店');
  222. }
  223. if ($hd->shopId != $this->shopId) {
  224. util::fail('不是你的花店');
  225. }
  226. $post['hdName'] = $hd->name ?? '';
  227. $hdCustomId = $hd->customId ?? 0;
  228. $custom = CustomClass::getById($hdCustomId, true);
  229. if (empty($custom)) {
  230. util::fail('没有找到客户');
  231. }
  232. $customName = $custom->name ?? '';
  233. $post['customId'] = $hdCustomId;
  234. $post['customName'] = $customName;
  235. $post['customNamePy'] = stringUtil::py($customName);
  236. $now = time();
  237. //订单30分钟后过期
  238. $expireTime = $now + 1800;
  239. $post['deadline'] = $expireTime;
  240. //商城
  241. $post['fromType'] = dict::getDict('fromType', 'mall');
  242. $productJson = $post['product'] ?? '';
  243. if (empty($productJson) && empty($groupId)) {
  244. util::fail('请选择商品');
  245. }
  246. $productList = json_decode($productJson, true);
  247. if (empty($productList)) {
  248. util::fail('请选择商品');
  249. }
  250. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  251. $productInfoList = productClass::getByIds($ids, null, 'id');
  252. if (!empty($productInfoList)) {
  253. $presellData = [];
  254. $nowTime = strtotime(date("Y-m-d"));
  255. foreach ($productInfoList as $currentInfo) {
  256. if (!isset($currentInfo['mainId']) || $currentInfo['mainId'] != $this->mainId) {
  257. util::fail('只能选一家的商品,请清空重新选呢');
  258. }
  259. $presell = $currentInfo['presell'] ?? 0;
  260. $presellData[] = $presell;
  261. if ($presell == 1) {
  262. if (empty($post['reachDate'])) {
  263. util::fail('请选择配送或取货日期');
  264. }
  265. $sendTimeWant = $post['reachDate'];
  266. if (strtotime($sendTimeWant) < $nowTime) {
  267. util::fail('不能选择过去时间');
  268. }
  269. $hasDateStr = $currentInfo['presellDate'] ?? [];
  270. $hasDate = explode(',', trim($hasDateStr));
  271. if (empty($hasDate)) {
  272. util::fail('预售日期有问题,请提醒门店');
  273. }
  274. if (!in_array(strtotime($sendTimeWant), $hasDate)) {
  275. util::fail("有预售花材在{$sendTimeWant}没到货");
  276. }
  277. $post['presell'] = 1;
  278. }
  279. }
  280. if (count($productInfoList) != count($ids)) {
  281. util::fail('存在无效商品');
  282. }
  283. $presellData = array_unique($presellData);
  284. if (count($presellData) > 1) {
  285. util::fail('预售和非预售花材不能一起下单');
  286. }
  287. } else {
  288. util::fail('花材不存在');
  289. }
  290. $connection = Yii::$app->db;
  291. $transaction = $connection->beginTransaction();
  292. $transactionFinished = false;
  293. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  294. if ($transactionFinished) {
  295. return;
  296. }
  297. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  298. if ($transaction->isActive) {
  299. $transaction->rollBack();
  300. }
  301. });
  302. try {
  303. $orderValidTime = !getenv('ORDER_VALID_TIME') ? 600 : getenv('ORDER_VALID_TIME');
  304. $post['deadline'] = time() + $orderValidTime;
  305. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  306. $post['needPrint'] = dict::getDict('needPrint', 'need');
  307. $user = $this->user;
  308. $post['bookMobile'] = $user->mobile ?? '';
  309. $post['bookName'] = $user->name ?? '';
  310. $hasPay = 0;
  311. $modifyPrice = 0;
  312. //各个等级对应的price addPrice字段
  313. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  314. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  315. $totalWeight = 0;
  316. $totalNum = 0;
  317. $totalReachDiscount = 0;
  318. foreach ($productList as $eleKey => $element) {
  319. $level = 0;
  320. $productId = $element['productId'];
  321. $product = $productInfoList[$productId];
  322. if (empty($product)) {
  323. util::fail('有商品信息没有找到');
  324. }
  325. $num = $element['num'] ?? 0; // 此次购买数量
  326. $limitKey = \bizHd\product\classes\ProductClass::LIMIT_BUY_KEY . $productId;
  327. $hasBuyNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $hdCustomId]);
  328. $hasBuyNum = $num + ($hasBuyNum == null ? 0 : $hasBuyNum); // 已购买总数量
  329. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($product, $level, $priceMap, $addPriceMap, 0, $num, $hasBuyNum);
  330. $reachNum = $product['reachNum'] ?? 0;
  331. $reachNumDiscount = $product['reachNumDiscount'] ?? 0;
  332. if ($reachNum > 0 && $num >= $reachNum && $reachNumDiscount > 0) {
  333. $currentReachDiscount = bcmul($reachNumDiscount, $num, 2);
  334. $totalReachDiscount = bcadd($totalReachDiscount, $currentReachDiscount, 2);
  335. }
  336. $currentTotal = bcmul($price, $num, 2);
  337. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  338. $productList[$eleKey]['unitPrice'] = $price;
  339. $weight = $product['weight'] ?? 0;
  340. $currentWeight = bcmul($num, $weight, 2);
  341. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  342. $totalNum = bcadd($totalNum, $num);
  343. if ($product['limitBuy'] > 0) {
  344. $product['productId'] = $productId;
  345. \bizHd\product\classes\ProductClass::handleLimitBuy($product, $hdCustomId, floatval($num));
  346. }
  347. }
  348. $post['product'] = $productList;
  349. $post['reachDiscountPrice'] = $totalReachDiscount;
  350. //不能前端传运费过来,只能通过跑腿接口计算出来
  351. unset($post['sendCost']);
  352. $mapSet = ShopClass::hasIntraCity($this->shop);
  353. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  354. if (isset($post['sendType']) && $post['sendType'] == 2 && $openIntraCity != 0) {
  355. if ($openIntraCity == 2) {
  356. util::fail('不能使用跑腿');
  357. }
  358. $cargoName = '花材'; // 商品名称
  359. $cargo = [
  360. 'cargo_name' => $cargoName, // 商品名称
  361. 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  362. 'cargo_price' => intval($modifyPrice * 100), // 单位:分 -- 把元转换为分
  363. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  364. 'cargo_num' => $totalNum
  365. ];
  366. $customName = $custom->name;
  367. $customMobile = $custom->mobile;
  368. if (empty($post['long']) || empty($post['lat'])) {
  369. util::fail('位置错误');
  370. }
  371. // $originalLng = doubleval($post['long']);
  372. // $originalLat = doubleval($post['lat']);
  373. // $city = $post['city'] ?? '';
  374. // $dist = $post['dist'] ?? '';
  375. // $address = $post['address'] ?? '';
  376. // $showAddress = $post['showAddress'] ?? '';
  377. // $userAddress = $city . $dist . $address . "({$showAddress})";
  378. // $main = $this->main;
  379. $shop = $this->shop;
  380. $shopLat = $shop->lat ?? '';
  381. $shopLong = $shop->long ?? '';
  382. if (empty($shopLat) || empty($shopLong)) {
  383. util::fail('商家门店地址定位未设置,编号263');
  384. }
  385. // $wxStoreId = $main->wxStoreId ?? 0;
  386. // if (empty($wxStoreId)) {
  387. // util::fail('商家还没有开通跑腿功能');
  388. // }
  389. // $orderData = [
  390. // 'wx_store_id' => $wxStoreId,
  391. // 'user_name' => $customName,
  392. // 'user_phone' => $customMobile,
  393. // 'user_lng' => $originalLng,
  394. // 'user_lat' => $originalLat,
  395. // 'user_address' => $userAddress,
  396. // 'cargo' => $cargo,
  397. // ];
  398. // //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  399. // $result = IntraCityExpress::preAddOrder($orderData);
  400. // $errCode = $result['errcode'] ?? 1;
  401. // if ($errCode != 0) {
  402. // $errMsg = $result['errmsg'] ?? '';
  403. // util::fail('获取运费错误:' . $errMsg);
  404. // }
  405. // $fee = $result['est_fee'] ?? 0;
  406. // $distance = $result['distance'] ?? 0;
  407. // $sendCost = bcdiv($fee, 100, 2);
  408. $distance = 0;
  409. $sendCost = 0;
  410. //生成随机订单号,不要跟原来的混起来,跑腿-销售单-
  411. $prefix = 'PT-XSD-' . $this->mainId . '-';
  412. $orderSn = $prefix . round(microtime(true) * 1000);
  413. $ghsInfo = ['mainId' => $this->mainId, 'shopId' => $this->shopId];
  414. // 这儿要调用跑腿接口计算运费和距离
  415. try {
  416. if (empty($post['deliveryPlatform'])) {
  417. util::fail('请选择跑腿');
  418. }
  419. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  420. 'productList' => $productList,
  421. 'deliveryPlatform' => $post['deliveryPlatform'],
  422. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  423. 'ghsInfo' => $ghsInfo,
  424. 'custom' => $this->user, // $custom 数据有问题,要是 user
  425. 'order' => [
  426. 'orderSn' => $orderSn,
  427. 'goodsType' => 1, //商品类型:0花束 1花材
  428. 'itemTotalAmount' => $modifyPrice,
  429. 'remark' => $post['remark'] ?? '',
  430. ],
  431. 'mainId' => $this->mainId,
  432. 'productCount' => $totalNum,
  433. ]);
  434. $sendCost = $quoteResult['sendCost'];
  435. $distance = $quoteResult['sendDistance'];
  436. } catch (\Exception $e) {
  437. util::fail($e->getMessage());
  438. }
  439. $post['sendCost'] = $sendCost;
  440. $post['sendDistance'] = $distance;
  441. //noticeUtil::push("零售订单(花材),获取运费:{$sendCost} 重量:{$totalWeight} 距离:{$distance}m 姓名:{$customName} 手机号 {$customMobile} 金额:{$modifyPrice} 数量:{$totalNum}", '15280215347');
  442. $modifyPrice = bcadd($modifyPrice, $sendCost, 2);
  443. }
  444. //红包使用,多有处,关键词 use_hb_action
  445. $hbId = $post['hbId'] ?? 0;
  446. $hb = null;
  447. if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
  448. $hb = HbClass::getById($hbId, true);
  449. if (!empty($hb)) {
  450. if ($hb->customId != $hdCustomId) {
  451. util::fail('不是你的红包');
  452. }
  453. if ($hb->amount != $post['hbAmount']) {
  454. util::fail('红包金额不相等'); //红包数据可能被串改
  455. }
  456. if ($hb->status == 1) {
  457. util::fail('红包已使用');
  458. }
  459. if ($hb->status == -1) {
  460. util::fail('红包已作废');
  461. }
  462. if ($hb->endTime < time()) {
  463. $hb->status = -1;
  464. $hb->save();
  465. util::fail('红包已失效');
  466. }
  467. if ($hb->beginTime > time()) {
  468. util::fail('红包可使用期未到');
  469. }
  470. // 按适用范围计算适用商品合计是否达到最低消费
  471. HbScopeClass::assertMinConsume($hb, $productList ?? [], $modifyPrice);
  472. if ($this->shop->rechargeWeal == 3) {
  473. if (bccomp($custom->balance, $modifyPrice) == -1) {
  474. //不要限制提交
  475. //util::fail('此红包非余额支付不可用(您的余额不足)');
  476. }
  477. }
  478. $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);//扣减红包
  479. } else {
  480. Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
  481. util::fail('红包无效');
  482. }
  483. }
  484. $post['bigNum'] = $totalNum;
  485. $post['modifyPrice'] = $modifyPrice;
  486. //多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  487. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  488. if (!empty($hb)) {
  489. $hb->status = 1;
  490. $hb->orderId = $return->id;
  491. $hb->save();
  492. }
  493. if (isset($return->shopId)) {
  494. if ($return->shopId == 7610) {
  495. if ($return->actPrice < 200) {
  496. if ($return->sendType == 0) {
  497. util::fail('满200元支持送货上门');
  498. }
  499. }
  500. }
  501. }
  502. if ($return->sendType == 2) {
  503. $ext = ShopExtClass::getByCondition(['shopId' => $return->shopId], true, null, 'id,shopId,thirdSend');
  504. if ($ext->thirdSend == 1) {
  505. util::fail('本店暂不能使用跑腿呢');
  506. }
  507. }
  508. $transaction->commit();
  509. $transactionFinished = true;
  510. \bizHd\product\classes\ProductClass::clearLimitBuyRollbackSnapshot();
  511. $orderSn = $return->orderSn ?? '';
  512. $orderPrice = $return->orderPrice ?? 0;
  513. $id = $return->id ?? 0;
  514. $getPayType = dict::getDict('getPayType');
  515. util::success(['orderSn' => $orderSn, 'totalPrice' => $orderPrice, 'couponId' => 0, 'id' => $id, 'getPayType' => $getPayType]);
  516. } catch (\Exception $e) {
  517. $transaction->rollBack();
  518. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  519. $transactionFinished = true;
  520. Yii::error("失败原因:" . $e->getMessage());
  521. util::fail('下单失败');
  522. }
  523. }
  524. //商城下单操作 ssh 2019.12.3
  525. public function actionCreateOrder()
  526. {
  527. $post = Yii::$app->request->post();
  528. //避免重复提交
  529. $userId = $this->userId;
  530. util::checkRepeatCommit($userId, 3);
  531. $shop = $this->shop;
  532. $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
  533. if ($openShop == 0) {
  534. util::fail('已休店');
  535. }
  536. $goodsId = $post['goodsId'] ?? 0;
  537. $specGoodsId = (int)($post['specGoodsId'] ?? 0);
  538. $goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
  539. $sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
  540. if (empty($goodsId)) {
  541. util::fail('请选择商品');
  542. }
  543. $mainId = $this->mainId;
  544. $goodsInfo = GoodsClass::getById($goodsId, true);
  545. if (empty($goodsInfo)) {
  546. util::fail('没有找到商品');
  547. }
  548. if ($goodsInfo->mainId != $mainId) {
  549. util::fail('不是你的商品');
  550. }
  551. if ($specGoodsId > 0) {
  552. // 首规格落在主记录:specGoodsId 可以等于主商品 id,直接用主记录行售卖
  553. if ($specGoodsId != $goodsId) {
  554. $specGoods = GoodsClass::getById($specGoodsId, true);
  555. if (empty($specGoods) || $specGoods->mainId != $mainId || $specGoods->masterId != $goodsId || $specGoods->delStatus != 0 || $specGoods->status != 1) {
  556. util::fail('规格不可用');
  557. }
  558. $goodsInfo = $specGoods;
  559. }
  560. } elseif ((int)($goodsInfo->masterId ?? 0) === 0 && (int)($goodsInfo->goodsStyle ?? 0) === 1) {
  561. util::fail('请选择规格');
  562. }
  563. $stock = $goodsInfo->stock ?? 0;
  564. if ($stock <= 0) {
  565. util::fail('库存不足');
  566. }
  567. $saleGoodsId = $specGoodsId > 0 ? $specGoodsId : $goodsId;
  568. $activityType = strval($post['activityType'] ?? '');
  569. //事务处理
  570. $connection = Yii::$app->db;
  571. $transaction = $connection->beginTransaction();
  572. $transactionFinished = false;
  573. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  574. if ($transactionFinished) {
  575. return;
  576. }
  577. HomePageModuleClass::rollbackSeckillReservationSnapshot();
  578. if ($transaction->isActive) {
  579. $transaction->rollBack();
  580. }
  581. });
  582. try {
  583. $custom = $this->custom;
  584. if (empty($custom)) {
  585. util::fail('个人信息缺失');
  586. }
  587. $customId = $custom->id ?? 0;
  588. $hd = $this->hd;
  589. $post['customId'] = $customId;
  590. $customName = $custom->name ?? '';
  591. $post['customName'] = $customName;
  592. $post['customNamePy'] = stringUtil::py($customName);
  593. $post['hdId'] = $hd->id ?? 0;
  594. $post['hdName'] = $hd->name ?? '';
  595. $user = $this->user;
  596. $post['bookName'] = $user->name ?? '';
  597. $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
  598. $bookMobile = empty($bookMobile) && !empty($user->mobile) ? $user->mobile : $bookMobile;
  599. $post['bookMobile'] = $bookMobile;
  600. $post['payWay'] = 0;
  601. $shop = $this->shop;
  602. // 秒杀商品:不信任前端传的价格,独立回查秒杀配置核实活动状态/库存/限购后再核价
  603. $seckillRow = null;
  604. $seckillActivityGoodsId = 0;
  605. if ($activityType === 'seckill') {
  606. $seckillRow = HomePageModuleClass::getSeckillActiveRow($mainId, intval($this->shopId), $saleGoodsId);
  607. if (empty($seckillRow)) {
  608. util::fail('秒杀活动已结束或商品已下架,请重新选择');
  609. }
  610. $seckillActivityGoodsId = intval($seckillRow['id'] ?? ($seckillRow['activityGoodsId'] ?? 0));
  611. if ($seckillActivityGoodsId <= 0) {
  612. util::fail('秒杀商品版本无效,请重新选择');
  613. }
  614. $seckillStock = floatval($seckillRow['stock'] ?? 0);
  615. $soldCount = HomePageModuleClass::getSeckillSoldCount($mainId, $seckillActivityGoodsId);
  616. if (bcadd($soldCount, $goodsNum, 2) > $seckillStock) {
  617. util::fail('秒杀库存不足,请重新选择');
  618. }
  619. $seckillLimit = floatval($seckillRow['limit'] ?? 0);
  620. if ($seckillLimit > 0) {
  621. $boughtCount = HomePageModuleClass::getSeckillCustomBoughtCount($mainId, $seckillActivityGoodsId, $customId);
  622. if (bcadd($boughtCount, $goodsNum, 2) > $seckillLimit) {
  623. util::fail("秒杀商品每人限购{$seckillLimit}件,已超出可购买数量");
  624. }
  625. }
  626. }
  627. if ($seckillRow) {
  628. $goodsInfo->priceType = 1;
  629. $unitPrice = floatval($seckillRow['price'] ?? 0);
  630. if ($unitPrice <= 0) {
  631. util::fail('秒杀价格异常,不能下单');
  632. }
  633. // 通过校验后立即预占名额,失败/异常时随事务一起回滚(见 register_shutdown_function)
  634. HomePageModuleClass::reserveSeckillPurchase($mainId, $seckillActivityGoodsId, $customId, $goodsNum);
  635. } else {
  636. $params = [];
  637. $goodsData = $goodsInfo->attributes;
  638. $ret = \bizHd\goods\classes\GoodsClass::getFinalPrice($goodsData, $shop, $custom, $params);
  639. $goodsInfo->priceType = $ret['priceType'] ?? 0;
  640. if ($goodsInfo->priceType == 0) {
  641. util::fail('没有价格,不能下单哈');
  642. }
  643. $unitPrice = $ret['price'] ?? 0;
  644. if ($unitPrice <= 0) {
  645. util::fail('没有价格,不能下单呢');
  646. }
  647. }
  648. //计算总金额
  649. $goodsPrice = $unitPrice * $goodsNum;
  650. //计算运费
  651. $sendDistance = 0;
  652. $sendCost = 0;
  653. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  654. $receiveUserName = $post['receiveUserName'] ?? '';
  655. if (empty($receiveUserName)) {
  656. util::fail('请填写收花人名字');
  657. }
  658. $receiveMobile = $post['receiveMobile'] ?? '';
  659. if (empty($receiveMobile)) {
  660. util::fail('请填写收花人手机号');
  661. }
  662. if (!stringUtil::isMobile($receiveMobile)) {
  663. util::fail('收花人手机号错误');
  664. }
  665. }
  666. $mapSet = ShopClass::hasIntraCity($shop);
  667. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  668. if ($sendType == dict::getDict('sendType', 'thirdSend') && $openIntraCity != 0) {
  669. // if (dict::getDict('hasMap') == 0) {
  670. // util::fail('不能使用跑腿,请选其它方式');
  671. // }
  672. // $lat = $post['lat'] ?? '';
  673. // $lng = $post['long'] ?? '';
  674. // if ((empty($lat) || empty($lng))) { // 当 hasMap 为1时,经纬数据是必要的
  675. // util::fail('请填写正确地址');
  676. // }
  677. // $address = $post['address'] ?? '';
  678. // $floor = $post['floor'] ?? '';
  679. // $fullAddress = $address . $floor;
  680. // $post['fullAddress'] = $fullAddress;
  681. // $shopLat = $shop->lat ?? '';
  682. // $shopLong = $shop->long ?? '';
  683. // if (empty($shopLat) || empty($shopLong)) {
  684. // util::fail('商家门店地址定位未设置,编号8966');
  685. // }
  686. // $weight = $goodsInfo->weight ?? 0;
  687. // if ($weight <= 0) {
  688. // util::fail('商品没有重量');
  689. // }
  690. // $totalWeight = bcmul($weight, $goodsNum, 2);
  691. // $cargoName = '鲜花'; // 商品名称
  692. // $cargo = [
  693. // 'cargo_name' => $cargoName, // 商品名称
  694. // 'cargo_weight' => intval($totalWeight * 1000), // 单位:克 -- 把千克转换为克 ---- 重量(用户未知)-- 大概
  695. // 'cargo_price' => intval($goodsPrice * 100), // 单位:分 -- 把元转换为分
  696. // 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  697. // 'cargo_num' => $goodsNum
  698. // ];
  699. // $customName = $custom->name;
  700. // $customMobile = $custom->mobile;
  701. // if (empty($post['long']) || empty($post['lat'])) {
  702. // util::fail('位置错误');
  703. // }
  704. // $originalLng = doubleval($post['long']);
  705. // $originalLat = doubleval($post['lat']);
  706. // $city = $post['city'] ?? '';
  707. // $dist = $post['dist'] ?? '';
  708. // $address = $post['address'] ?? '';
  709. // $showAddress = $post['showAddress'] ?? '';
  710. // $userAddress = $city . $dist . $address . "({$showAddress})";
  711. // $main = $this->main;
  712. // $shopLat = $shop->lat ?? '';
  713. // $shopLong = $shop->long ?? '';
  714. // if (empty($shopLat) || empty($shopLong)) {
  715. // util::fail('商家门店地址定位未设置,编号6880');
  716. // }
  717. // $wxStoreId = $main->wxStoreId ?? 0;
  718. // if (empty($wxStoreId)) {
  719. // util::fail('商家还没有开通跑腿功能');
  720. // }
  721. // $orderData = [
  722. // 'wx_store_id' => $wxStoreId,
  723. // 'user_name' => $customName,
  724. // 'user_phone' => $customMobile,
  725. // 'user_lng' => $originalLng,
  726. // 'user_lat' => $originalLat,
  727. // 'user_address' => $userAddress,
  728. // 'cargo' => $cargo,
  729. // ];
  730. // //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  731. // $result = IntraCityExpress::preAddOrder($orderData);
  732. // $errCode = $result['errcode'] ?? 1;
  733. // if ($errCode != 0) {
  734. // $errMsg = $result['errmsg'] ?? '';
  735. // util::fail('获取运费错误:' . $errMsg);
  736. // }
  737. // $fee = $result['est_fee'] ?? 0;
  738. // $sendDistance = $result['distance'] ?? 0;
  739. // $sendCost = bcdiv($fee, 100, 2);
  740. // //免运费的不收运费
  741. // if ($goodsInfo->freightType == 1) {
  742. // $sendCost = 0;
  743. // }
  744. // noticeUtil::push("零售订单!获取运费:{$sendCost} 重量:{$totalWeight} 距离:{$sendDistance} 姓名:{$customName} 手机号 {$customMobile} 经纬 {$originalLng} {$originalLat} 金额:{$goodsPrice} 数量:{$goodsNum}", '15280215347');
  745. if ($openIntraCity == 2) {
  746. util::fail('不能使用跑腿');
  747. }
  748. $cutomInfo = [
  749. 'name' => $post['receiveUserName'],
  750. 'mobile' => $post['receiveMobile'],
  751. 'fullAddress' => $post['address'],
  752. 'floor' => $post['floor'],
  753. 'dist' => $post[''] ?? '',
  754. 'lat' => $post['lat'],
  755. 'long' => $post['long'],
  756. 'address' => $post['address'],
  757. 'city' => $post['city'],
  758. ];
  759. $cutomInfo = (object)$cutomInfo;
  760. // 生成随机花束订单号
  761. $prefix = 'KD_CS-' . $this->mainId . '-';
  762. $orderSn = $prefix . round(microtime(true) * 1000);
  763. // 使用 DeliveryQuoteUtil 获取配送报价
  764. try {
  765. if ($goodsInfo->weight > 5) {
  766. $goodsInfo->weight = 5;
  767. }
  768. if (empty($post['deliveryPlatform'])) {
  769. util::fail('请选择跑腿');
  770. }
  771. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  772. 'productList' => [['bigNum' => $goodsNum, 'weight' => $goodsInfo->weight]],
  773. 'deliveryPlatform' => $post['deliveryPlatform'],
  774. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  775. 'ghsInfo' => ['mainId' => $mainId, 'shopId' => $this->shopId],
  776. 'custom' => $cutomInfo,
  777. 'order' => [
  778. 'orderSn' => $orderSn,
  779. 'goodsType' => 0, //商品类型:0花束 1花材
  780. 'itemTotalAmount' => $goodsPrice,
  781. 'remark' => $post['remark'] ?? '',
  782. 'freightType' => $goodsInfo->freightType,
  783. ],
  784. 'mainId' => $this->mainId,
  785. 'productCount' => $goodsNum,
  786. ]);
  787. $sendCost = $quoteResult['sendCost'];
  788. $sendDistance = $quoteResult['sendDistance'];
  789. noticeUtil::push("零售订单(花束),获取运费:{$sendCost} 重量:{$goodsInfo->weight} 距离:{$sendDistance}m 姓名:{$customName} 金额:{$goodsPrice} 数量:{$goodsNum}");
  790. } catch (\Exception $e) {
  791. util::fail($e->getMessage());
  792. }
  793. }
  794. $post['sendDistance'] = $sendDistance;
  795. $post['sendCost'] = $sendCost;
  796. $post['sjId'] = $this->sjId;
  797. $post['shopId'] = $this->shopId;
  798. $post['mainId'] = $mainId;
  799. $post['userId'] = $this->userId;
  800. if ($goodsInfo->priceType == 0) {
  801. util::fail('没有价格,不能下单');
  802. }
  803. $modifyPrice = stringUtil::calcAdd($sendCost, $goodsPrice);
  804. //非门店订单
  805. $post['store'] = 0;
  806. //红包使用,多有处,关键词 use_hb_action
  807. $hbId = $post['hbId'] ?? 0;
  808. $hb = null;
  809. if ($hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
  810. $hb = HbClass::getById($hbId, true);
  811. if (!empty($hb)) {
  812. if ($hb->customId != $customId) {
  813. util::fail('不是你的红包');
  814. }
  815. if ($hb->amount != $post['hbAmount']) {
  816. util::fail('红包金额不相等'); //红包数据可能被串改
  817. }
  818. if ($hb->status == 1) {
  819. util::fail('红包已使用');
  820. }
  821. if ($hb->status == -1) {
  822. util::fail('红包已作废');
  823. }
  824. if ($hb->endTime < time()) {
  825. $hb->status = -1;
  826. $hb->save();
  827. util::fail('红包已失效');
  828. }
  829. if ($hb->beginTime > time()) {
  830. util::fail('红包可使用期未到');
  831. }
  832. // 单花束下单:按适用范围校验最低消费
  833. $hbGoods = [[
  834. 'productId' => $goodsInfo->id ?? 0,
  835. 'goodsId' => $goodsInfo->id ?? 0,
  836. 'unitPrice' => $goodsPrice ?? 0,
  837. 'num' => $goodsNum ?? 1,
  838. 'linePrice' => $goodsPrice ?? 0,
  839. ]];
  840. HbScopeClass::assertMinConsume($hb, $hbGoods, $modifyPrice);
  841. if ($this->shop->rechargeWeal == 3) {
  842. if ($custom->balance < $modifyPrice) {
  843. util::fail('此红包非余额支付不可用(您的余额不足)');
  844. }
  845. }
  846. $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);//扣减红包
  847. } else {
  848. Yii::error('没有找到红包,hbId', $hbId . ' ' . __METHOD__);
  849. util::fail('红包无效');
  850. }
  851. }
  852. $post['modifyPrice'] = $modifyPrice;
  853. //非自取订单考虑配送时间
  854. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  855. $sendType = $post['sendType'] ?? 0;
  856. if ($sendType != 1) {
  857. if (empty($post['reachPeriod'])) {
  858. util::fail('请选择配送时间哈');
  859. }
  860. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  861. }
  862. $now = time();
  863. $expireTime = $now + 1800;//订单30分钟后过期
  864. $post['deadline'] = $expireTime;
  865. $orderSn = orderSn::getOrderSn();
  866. $post['orderSn'] = $orderSn;
  867. $post['fromType'] = dict::getDict('fromType', 'mall');
  868. $post['cash'] = 0;
  869. $post['mainId'] = $mainId;
  870. $post['needPrint'] = dict::getDict('needPrint', 'need');
  871. $hasPay = 0;
  872. $product = [['productId' => $saleGoodsId, 'unitType' => 0, 'property' => 0, 'unitPrice' => $unitPrice, 'num' => $goodsNum, 'seckillGoodsId' => $seckillActivityGoodsId]];
  873. $post['product'] = $product;
  874. $post['hasSeckill'] = $seckillRow ? 1 : 0;
  875. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  876. //红包保存订单id
  877. if (!empty($hb)) {
  878. $hb->status = 1;
  879. $hb->orderId = $return->id;
  880. $hb->save();
  881. }
  882. $orderId = $return->id;
  883. $orderSn = $return->orderSn;
  884. $actPrice = $return->actPrice ?? 0;
  885. $transaction->commit();
  886. $transactionFinished = true;
  887. HomePageModuleClass::clearSeckillReservationSnapshot();
  888. util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => 0, 'id' => $orderId]);
  889. } catch (Exception $e) {
  890. $transaction->rollBack();
  891. HomePageModuleClass::rollbackSeckillReservationSnapshot();
  892. $transactionFinished = true;
  893. Yii::info("失败原因:" . $e->getMessage());
  894. util::fail('下单失败');
  895. }
  896. }
  897. /**
  898. * 花束+花材合并结算下单
  899. * 复用 OrderService::createHdOrder,不改动 buy-item / create-order
  900. */
  901. public function actionCreateMixOrder()
  902. {
  903. $post = Yii::$app->request->post();
  904. $post['sjId'] = $this->sjId;
  905. $post['shopId'] = $this->shopId;
  906. $post['payWay'] = 0;
  907. $post['mainId'] = $this->mainId ?? 0;
  908. $post['userId'] = $this->userId ?? 0;
  909. $userId = $this->userId;
  910. util::checkRepeatCommit($userId, 3);
  911. $mainId = $this->mainId;
  912. $shop = $this->shop;
  913. $openShop = \bizHd\shop\classes\ShopClass::getOpenShop($shop);
  914. if ($openShop == 0) {
  915. util::fail('已休店');
  916. }
  917. $hdId = $post['hdId'] ?? 0;
  918. $hd = HdClass::getById($hdId, true);
  919. if (empty($hd)) {
  920. util::fail('没有找到花店');
  921. }
  922. if ($hd->shopId != $this->shopId) {
  923. util::fail('不是你的花店');
  924. }
  925. $post['hdName'] = $hd->name ?? '';
  926. // 合并结算沿用花材购物车的客户身份(花店客户)
  927. $hdCustomId = $hd->customId ?? 0;
  928. $custom = CustomClass::getById($hdCustomId, true);
  929. if (empty($custom)) {
  930. util::fail('没有找到客户');
  931. }
  932. $customName = $custom->name;
  933. $post['customId'] = $hdCustomId;
  934. $post['customName'] = $customName;
  935. $post['customNamePy'] = stringUtil::py($customName);
  936. $productJson = $post['product'];
  937. if (empty($productJson)) {
  938. util::fail('请选择商品');
  939. }
  940. $productList = json_decode($productJson, true);
  941. if (empty($productList) || !is_array($productList)) {
  942. util::fail('请选择商品');
  943. }
  944. $itemRows = []; // 所有花材
  945. $goodsRows = []; // 所有花束
  946. foreach ($productList as $idx => $row) {
  947. $property = intval($row['property'] ?? -1);
  948. if ($property === 1) {
  949. $itemRows[$idx] = $row;
  950. } elseif ($property === 0) {
  951. $goodsRows[$idx] = $row;
  952. } else {
  953. util::fail('商品类型错误');
  954. }
  955. }
  956. if (empty($itemRows) && empty($goodsRows)) {
  957. util::fail('请选择商品');
  958. }
  959. // ---- 花材校验与定价 ----
  960. $productInfoList = [];
  961. if (!empty($itemRows)) {
  962. $ids = array_unique(array_filter(array_map(function ($r) {
  963. return intval($r['productId'] ?? 0);
  964. }, $itemRows)));
  965. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  966. if (empty($productInfoList)) {
  967. util::fail('花材不存在');
  968. }
  969. $presellData = [];
  970. $nowTime = strtotime(date("Y-m-d"));
  971. foreach ($productInfoList as $currentInfo) {
  972. if (!isset($currentInfo['mainId']) || $currentInfo['mainId'] != $this->mainId) {
  973. util::fail('只能选一家的商品,请清空重新选呢');
  974. }
  975. $presell = $currentInfo['presell'] ?? 0;
  976. $presellData[] = $presell;
  977. if ($presell == 1) {
  978. if (empty($post['reachDate'])) {
  979. util::fail('请选择配送或取货日期');
  980. }
  981. $sendTimeWant = $post['reachDate'];
  982. if (strtotime($sendTimeWant) < $nowTime) {
  983. util::fail('不能选择过去时间');
  984. }
  985. $hasDateStr = $currentInfo['presellDate'] ?? [];
  986. $hasDate = explode(',', trim($hasDateStr));
  987. if (empty($hasDate)) {
  988. util::fail('预售日期有问题,请提醒门店');
  989. }
  990. if (!in_array(strtotime($sendTimeWant), $hasDate)) {
  991. util::fail("有预售花材在{$sendTimeWant}没到货");
  992. }
  993. $post['presell'] = 1;
  994. }
  995. }
  996. if (count($productInfoList) != count($ids)) {
  997. util::fail('存在无效花材');
  998. }
  999. $presellData = array_unique($presellData);
  1000. if (count($presellData) > 1) {
  1001. util::fail('预售和非预售花材不能一起下单');
  1002. }
  1003. }
  1004. $connection = Yii::$app->db;
  1005. $transaction = $connection->beginTransaction();
  1006. $transactionFinished = false;
  1007. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  1008. if ($transactionFinished) {
  1009. return;
  1010. }
  1011. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  1012. HomePageModuleClass::rollbackSeckillReservationSnapshot();
  1013. if ($transaction->isActive) {
  1014. $transaction->rollBack();
  1015. }
  1016. });
  1017. try {
  1018. $orderValidTime = !getenv('ORDER_VALID_TIME') ? 600 : getenv('ORDER_VALID_TIME'); // TODO 找少华确认 -- 测试环境是 600
  1019. $post['deadline'] = time() + $orderValidTime;
  1020. $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
  1021. $post['needPrint'] = dict::getDict('needPrint', 'need');
  1022. $post['fromType'] = dict::getDict('fromType', 'mall');
  1023. $post['store'] = 0;
  1024. $user = $this->user;
  1025. $post['bookMobile'] = $user->mobile ?? '';
  1026. $post['bookName'] = $user->name ?? '';
  1027. $hasPay = 0;
  1028. $modifyPrice = 0;
  1029. $totalWeight = 0;
  1030. $totalNum = 0;
  1031. $itemBigNum = 0;
  1032. $totalReachDiscount = 0;
  1033. $resolvedProduct = [];
  1034. $hasSeckillGoods = false;
  1035. $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
  1036. $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
  1037. $post['orderType'] = 0;
  1038. foreach ($productList as $eleKey => $element) {
  1039. $property = $element['property']; // 1: 花材, 0: 花束
  1040. if ($property === 1) { // 花材
  1041. $level = 0;
  1042. $productId = $element['productId'];
  1043. $product = $productInfoList[$productId] ?? null;
  1044. if (empty($product)) {
  1045. util::fail('有花材信息没有找到');
  1046. }
  1047. $num = $element['num'] ?? 0;
  1048. $limitKey = \bizHd\product\classes\ProductClass::LIMIT_BUY_KEY . $productId;
  1049. $hasBuyNum = Yii::$app->redis->executeCommand('HGET', [$limitKey, $hdCustomId]);
  1050. $hasBuyNum = $num + ($hasBuyNum == null ? 0 : $hasBuyNum);
  1051. $price = \bizGhs\product\classes\ProductClass::getFinalPrice($product, $level, $priceMap, $addPriceMap, 0, $num, $hasBuyNum);
  1052. $reachNum = $product['reachNum'] ?? 0;
  1053. $reachNumDiscount = $product['reachNumDiscount'] ?? 0;
  1054. if ($reachNum > 0 && $num >= $reachNum && $reachNumDiscount > 0) {
  1055. $currentReachDiscount = bcmul($reachNumDiscount, $num, 2);
  1056. $totalReachDiscount = bcadd($totalReachDiscount, $currentReachDiscount, 2);
  1057. }
  1058. $currentTotal = bcmul($price, $num, 2);
  1059. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  1060. $weight = $product['weight'] ?? 0;
  1061. $currentWeight = bcmul($num, $weight, 2);
  1062. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  1063. $totalNum = bcadd($totalNum, $num);
  1064. $itemBigNum = bcadd($itemBigNum, $num);
  1065. if (($product['limitBuy'] ?? 0) > 0) {
  1066. $product['productId'] = $productId;
  1067. \bizHd\product\classes\ProductClass::handleLimitBuy($product, $hdCustomId, floatval($num));
  1068. }
  1069. $resolvedProduct[] = [
  1070. 'productId' => $productId,
  1071. 'num' => $num,
  1072. 'itemId' => $element['itemId'] ?? 0,
  1073. 'unitType' => $element['unitType'] ?? 0,
  1074. 'property' => 1,
  1075. 'unitPrice' => $price,
  1076. ];
  1077. } else if ($property === 0) { // 花束
  1078. $goodsId = $element['goodsId']; // 主商品ID
  1079. $specGoodsId = $element['specGoodsId']; // 规格ID
  1080. $saleGoodsId = $element['productId']; // 记录用于销售的商品ID,通常为具体的规格或产品ID
  1081. $goodsNum = isset($element['num']) && $element['num'] > 0 ? $element['num'] : 1;
  1082. if ($goodsId <= 0 && $saleGoodsId > 0) {
  1083. $probe = GoodsClass::getById($saleGoodsId, true);
  1084. if (!empty($probe)) {
  1085. $masterId = intval($probe->masterId);
  1086. if ($masterId > 0) {
  1087. $goodsId = $masterId;
  1088. $specGoodsId = $saleGoodsId;
  1089. } else {
  1090. $goodsId = $saleGoodsId;
  1091. }
  1092. }
  1093. }
  1094. if ($goodsId <= 0) {
  1095. util::fail('请选择花束商品');
  1096. }
  1097. $goodsInfo = GoodsClass::getById($goodsId, true);
  1098. if (empty($goodsInfo)) {
  1099. util::fail('没有找到花束商品');
  1100. }
  1101. if ($goodsInfo->mainId != $mainId) {
  1102. util::fail('不是你的商品');
  1103. }
  1104. if ($specGoodsId > 0) {
  1105. // 首规格落在主记录:specGoodsId 可以等于主商品 id,直接用主记录行售卖
  1106. if ($specGoodsId != $goodsId) {
  1107. $specGoods = GoodsClass::getById($specGoodsId, true);
  1108. if (empty($specGoods) || $specGoods->mainId != $mainId || $specGoods->masterId != $goodsId || $specGoods->delStatus != 0 || $specGoods->status != 1) {
  1109. util::fail('规格不可用');
  1110. }
  1111. $goodsInfo = $specGoods;
  1112. $saleGoodsId = $specGoodsId;
  1113. } else {
  1114. $saleGoodsId = $goodsId;
  1115. }
  1116. } else {
  1117. if ((int)($goodsInfo->masterId ?? 0) === 0 && (int)($goodsInfo->goodsStyle ?? 0) === 1) {
  1118. util::fail('请选择规格');
  1119. }
  1120. $saleGoodsId = $goodsId;
  1121. }
  1122. $stock = $goodsInfo->stock;
  1123. $stockSet = $goodsInfo->stockSet;
  1124. if ($stockSet == 1 && $stock <= 0) {
  1125. util::fail('花束库存不足');
  1126. }
  1127. // 秒杀商品:不信任前端传的价格,独立回查秒杀配置核实活动状态/库存/限购后再核价
  1128. $seckillRow = null;
  1129. $seckillActivityGoodsId = 0;
  1130. if (strval($element['activityType'] ?? '') === 'seckill') {
  1131. $seckillRow = HomePageModuleClass::getSeckillActiveRow($mainId, intval($this->shopId), $saleGoodsId);
  1132. if (empty($seckillRow)) {
  1133. util::fail('秒杀活动已结束或商品已下架,请重新选择');
  1134. }
  1135. $seckillActivityGoodsId = intval($seckillRow['id'] ?? ($seckillRow['activityGoodsId'] ?? 0));
  1136. if ($seckillActivityGoodsId <= 0) {
  1137. util::fail('秒杀商品版本无效,请重新选择');
  1138. }
  1139. $seckillStock = floatval($seckillRow['stock']);
  1140. $soldCount = HomePageModuleClass::getSeckillSoldCount($mainId, $seckillActivityGoodsId);
  1141. if (bcadd($soldCount, $goodsNum, 2) > $seckillStock) {
  1142. util::fail('秒杀库存不足,请重新选择');
  1143. }
  1144. $seckillLimit = floatval($seckillRow['limit'] ?? 0);
  1145. if ($seckillLimit > 0) {
  1146. $boughtCount = HomePageModuleClass::getSeckillCustomBoughtCount($mainId, $seckillActivityGoodsId, $hdCustomId); // TODO 同一个商品,如果缓存没有合理清除,就会一直限购
  1147. if (bcadd($boughtCount, $goodsNum, 2) > $seckillLimit) {
  1148. util::fail("秒杀商品每人限购{$seckillLimit}件,已超出可购买数量");
  1149. }
  1150. }
  1151. }
  1152. $goodsData = $goodsInfo->attributes;
  1153. if ($seckillRow) {
  1154. $unitPrice = floatval($seckillRow['price'] ?? 0);
  1155. if ($unitPrice <= 0) {
  1156. util::fail('秒杀价格异常,不能下单');
  1157. }
  1158. } else {
  1159. $ret = \bizHd\goods\classes\GoodsClass::getFinalPrice($goodsData, $shop, $custom, []);
  1160. $priceType = $ret['priceType'] ?? 0;
  1161. if ($priceType == 0) {
  1162. util::fail('花束没有价格,不能下单');
  1163. }
  1164. $unitPrice = $ret['price'] ?? 0;
  1165. if ($unitPrice <= 0) {
  1166. util::fail('花束没有价格,不能下单');
  1167. }
  1168. }
  1169. if ($seckillRow) {
  1170. // 通过校验后立即预占名额,失败/异常时随事务一起回滚(见 register_shutdown_function)
  1171. HomePageModuleClass::reserveSeckillPurchase($mainId, $seckillActivityGoodsId, $hdCustomId, $goodsNum);
  1172. $hasSeckillGoods = true;
  1173. }
  1174. $currentTotal = bcmul($unitPrice, $goodsNum, 2);
  1175. $modifyPrice = bcadd($modifyPrice, $currentTotal, 2);
  1176. $weight = $goodsInfo->weight ?? 0;
  1177. if ($weight > 5) {
  1178. $weight = 5;
  1179. }
  1180. $currentWeight = bcmul($weight, $goodsNum, 2);
  1181. $totalWeight = bcadd($totalWeight, $currentWeight, 2);
  1182. $totalNum = bcadd($totalNum, $goodsNum);
  1183. $resolvedProduct[] = [
  1184. 'productId' => $saleGoodsId,
  1185. 'unitType' => 0,
  1186. 'property' => 0,
  1187. // 花束是否包运费:供跑腿运费分支判断(1=包运费)
  1188. 'freightType' => (int)($goodsInfo->freightType ?? 0),
  1189. 'unitPrice' => $unitPrice,
  1190. 'num' => $goodsNum,
  1191. 'seckillGoodsId' => $seckillActivityGoodsId,
  1192. ];
  1193. } else {
  1194. util::fail('商品类型错误');
  1195. }
  1196. }
  1197. // 根据 $resolvedProduct 设置订单类型:1纯花束 2纯花材 3混合
  1198. $properties = array_unique(array_column($resolvedProduct, 'property'));
  1199. if (count($properties) === 1) {
  1200. $post['orderType'] = intval($properties[0]) === 0 ? 1 : 2;
  1201. } elseif (count($properties) > 1) {
  1202. $post['orderType'] = 3;
  1203. }
  1204. $post['product'] = $resolvedProduct;
  1205. $post['reachDiscountPrice'] = $totalReachDiscount;
  1206. $goodsOnlyPrice = $modifyPrice;
  1207. unset($post['sendCost'], $post['packCost'], $post['packingFee'], $post['unMeetSendCost']);
  1208. $sendType = intval($post['sendType'] ?? 0);
  1209. $customInfo = \bizHd\order\classes\PsMethodClass::buildCustomInfo($hd, $custom);
  1210. // 最低消费数量:花材扎数 + 花束份数(与结算页「还差 N 扎」同源)
  1211. $unMeetBigNum = $itemBigNum;
  1212. foreach ($resolvedProduct as $row) {
  1213. if ((int)($row['property'] ?? 1) === 0) {
  1214. $unMeetBigNum = bcadd($unMeetBigNum, $row['num'] ?? 0, 2);
  1215. }
  1216. }
  1217. $additionalCosts = \bizHd\order\classes\PsMethodClass::calcFeeForMallContext(
  1218. $this->shopId,
  1219. $this->mainId,
  1220. $sendType,
  1221. $goodsOnlyPrice,
  1222. $unMeetBigNum,
  1223. $hdCustomId,
  1224. $customInfo
  1225. );
  1226. $unMeetSendCost = $additionalCosts['sendCost'] ?? 0;
  1227. $packingFee = $additionalCosts['packCost'] ?? 0;
  1228. $mapSet = ShopClass::hasIntraCity($this->shop);
  1229. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  1230. $hasItem = !empty($itemRows);
  1231. // 含花材(含混合)按花材报价 goodsType=1;纯花束按 0
  1232. $quoteGoodsType = $hasItem ? 1 : 0;
  1233. // 跑腿 style=2:按包运费花束 / 不包运费花束 / 纯花材满减 + 跑腿或自定义(异常兜底)
  1234. if (isset($post['sendType']) && (int)$post['sendType'] === 2) {
  1235. if ((int)$openIntraCity === 2) {
  1236. util::fail('不能使用跑腿');
  1237. }
  1238. if (empty($post['long']) || empty($post['lat'])) {
  1239. util::fail('位置错误');
  1240. }
  1241. $shopLat = $shop->lat ?? '';
  1242. $shopLong = $shop->long ?? '';
  1243. if (empty($shopLat) || empty($shopLong)) {
  1244. util::fail('商家门店地址定位未设置,编号263');
  1245. }
  1246. $psConfig = PsMethodClass::getConfig($this->shopId, $this->mainId, 2);
  1247. if (empty($psConfig) || (int)($psConfig['status'] ?? 0) !== 1) {
  1248. util::fail('跑腿配送方式未启用');
  1249. }
  1250. $freightMode = PsMethodClass::resolveMixRunnerFreightMode($resolvedProduct);
  1251. $calcType = (int)($psConfig['calcType'] ?? 0);
  1252. $quoteCustom = (object)[
  1253. 'name' => $post['receiveUserName'] ?? '',
  1254. 'mobile' => $post['receiveMobile'] ?? '',
  1255. 'fullAddress' => $post['address'] ?? ($post['fullAddress'] ?? ''),
  1256. 'floor' => $post['floor'] ?? '',
  1257. 'dist' => $post['dist'] ?? '',
  1258. 'lat' => $post['lat'],
  1259. 'long' => $post['long'],
  1260. 'address' => $post['address'] ?? '',
  1261. 'city' => $post['city'] ?? '',
  1262. ];
  1263. $distanceRespond = mapUtil::calcShopDistanceByEleBike($quoteCustom, $shop, 0);
  1264. $distanceMeters = (float)($distanceRespond['distance'] ?? 0);
  1265. $distanceKm = (float)($distanceRespond['distanceKm'] ?? 0);
  1266. if ($distanceMeters <= 0 && $distanceKm <= 0) {
  1267. util::fail('算运费失败,请完善收货地址');
  1268. }
  1269. if ($distanceMeters <= 0 && $distanceKm > 0) {
  1270. $distanceMeters = $distanceKm * 1000;
  1271. }
  1272. $sendCost = 0;
  1273. $freightSource = '';
  1274. if ($freightMode === 'bouquetIncluded') {
  1275. // A:含包运费花束 → hsFreeKm / hsPerKmPrice
  1276. $sendCost = PsMethodClass::calcBouquetFreightByDistance($psConfig, $distanceMeters);
  1277. $freightSource = 'bouquetIncluded';
  1278. } elseif ($freightMode === 'flowerOnly'
  1279. && PsMethodClass::matchReduceRuleFree($psConfig, $distanceMeters, $itemBigNum, $goodsOnlyPrice)) {
  1280. // C:纯花材命中满减 → 免跑腿
  1281. $sendCost = 0;
  1282. $freightSource = 'reduceFree';
  1283. } else {
  1284. // B 或不免满减的 C:按 calcType 跑腿或自定义;跑腿异常最终自定义兜底
  1285. if ($calcType === 1) {
  1286. try {
  1287. $sendCost = PsMethodClass::calcCustomFreightByDistance($psConfig, $distanceKm > 0 ? $distanceKm : ($distanceMeters / 1000));
  1288. $freightSource = 'custom';
  1289. } catch (\Exception $e) {
  1290. util::fail($e->getMessage());
  1291. }
  1292. } else {
  1293. $runnerOk = false;
  1294. if ((int)$openIntraCity === 1 && !empty($post['deliveryPlatform'])) {
  1295. $prefix = 'PT-MIX-' . $this->mainId . '-';
  1296. $orderSn = $prefix . round(microtime(true) * 1000);
  1297. $ghsInfo = ['mainId' => $this->mainId, 'shopId' => $this->shopId];
  1298. try {
  1299. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  1300. 'productList' => $resolvedProduct,
  1301. 'deliveryPlatform' => $post['deliveryPlatform'],
  1302. 'deliveryBracketContent' => $post['deliveryBracketContent'] ?? '',
  1303. 'ghsInfo' => $ghsInfo,
  1304. 'custom' => $quoteCustom,
  1305. 'order' => [
  1306. 'orderSn' => $orderSn,
  1307. 'goodsType' => $quoteGoodsType,
  1308. 'itemTotalAmount' => $goodsOnlyPrice,
  1309. 'remark' => $post['remark'] ?? '',
  1310. ],
  1311. 'mainId' => $this->mainId,
  1312. 'productCount' => $totalNum,
  1313. 'shConfig' => $psConfig,
  1314. ]);
  1315. $sendCost = $quoteResult['sendCost'];
  1316. $distanceMeters = (float)($quoteResult['sendDistance'] ?? $distanceMeters);
  1317. $freightSource = 'platform';
  1318. $runnerOk = true;
  1319. } catch (\Exception $e) {
  1320. $runnerOk = false;
  1321. noticeUtil::push('商城混合单跑腿报价异常,改走自定义:' . $e->getMessage());
  1322. }
  1323. }
  1324. if (!$runnerOk) {
  1325. try {
  1326. $sendCost = PsMethodClass::calcCustomFreightByDistance(
  1327. $psConfig,
  1328. $distanceKm > 0 ? $distanceKm : ($distanceMeters / 1000)
  1329. );
  1330. $freightSource = 'customFallback';
  1331. } catch (\Exception $e) {
  1332. util::fail('跑腿计算异常且自定义计费不可用,请更换配送方式');
  1333. }
  1334. }
  1335. }
  1336. }
  1337. $post['sendCost'] = $sendCost;
  1338. $post['sendDistance'] = $distanceMeters;
  1339. $post['freightSource'] = $freightSource;
  1340. $modifyPrice = bcadd($modifyPrice, $sendCost, 2);
  1341. }
  1342. if ($unMeetSendCost > 0) {
  1343. $post['sendCost'] = bcadd($post['sendCost'] ?? 0, $unMeetSendCost, 2);
  1344. $modifyPrice = bcadd($modifyPrice, $unMeetSendCost, 2);
  1345. }
  1346. if ($packingFee > 0) {
  1347. $post['packingFee'] = $packingFee;
  1348. $modifyPrice = bcadd($modifyPrice, $packingFee, 2);
  1349. }
  1350. // 红包(归属花店客户,与 buy-item 一致)
  1351. $hbId = $post['hbId'] ?? 0;
  1352. $hb = null;
  1353. if ($hbId > 0) {
  1354. $hb = HbClass::getById($hbId, true);
  1355. if (!empty($hb)) {
  1356. if ($hb->customId != $hdCustomId) {
  1357. util::fail('不是你的红包');
  1358. }
  1359. if ($hb->amount != $post['hbAmount']) {
  1360. util::fail('红包金额不相等');
  1361. }
  1362. if ($hb->status == 1) {
  1363. util::fail('红包已使用');
  1364. }
  1365. if ($hb->status == -1) {
  1366. util::fail('红包已作废');
  1367. }
  1368. if ($hb->endTime < time()) {
  1369. $hb->status = -1;
  1370. $hb->save();
  1371. util::fail('红包已失效');
  1372. }
  1373. if ($hb->beginTime > time()) {
  1374. util::fail('红包可使用期未到');
  1375. }
  1376. // 混合结算:按适用范围用商品明细校验最低消费(不含运费包装费)
  1377. HbScopeClass::assertMinConsume($hb, $resolvedProduct ?? [], $goodsOnlyPrice ?? $modifyPrice);
  1378. $modifyPrice = bcsub($modifyPrice, $hb->amount, 2);
  1379. } else {
  1380. util::fail('红包无效');
  1381. }
  1382. }
  1383. $psMethodError = \bizHd\order\classes\PsMethodClass::checkLimit(
  1384. $this->shopId,
  1385. $this->mainId,
  1386. $sendType,
  1387. $goodsOnlyPrice,
  1388. $unMeetBigNum,
  1389. $packingFee,
  1390. $unMeetSendCost,
  1391. $hdCustomId,
  1392. $customInfo
  1393. );
  1394. if ($psMethodError !== '') {
  1395. util::fail($psMethodError);
  1396. }
  1397. // 商家配送(0)/跑腿(2)必填配送时间;到店自取(1)、物流(3)、快递(4)不强制(后两者由客服确认时效)
  1398. $sendType = intval($post['sendType'] ?? 0);
  1399. if (in_array($sendType, [0, 2], true)) {
  1400. if (empty($post['reachPeriod'])) {
  1401. util::fail('请选择配送时间哈');
  1402. }
  1403. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  1404. } elseif (!empty($post['reachPeriod'])) {
  1405. $post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
  1406. }
  1407. $post['bigNum'] = $totalNum;
  1408. $post['modifyPrice'] = $modifyPrice;
  1409. $post['hasSeckill'] = $hasSeckillGoods ? 1 : 0;
  1410. $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
  1411. $orderSn = $return->orderSn ?? '';
  1412. if (!empty($orderSn) && (float)$unMeetSendCost > 0) {
  1413. $unMeetSendKey = 'hd_order_unmeet_send:' . $orderSn;
  1414. Yii::$app->redis->executeCommand('SETEX', [$unMeetSendKey, 864000, (string)$unMeetSendCost]);
  1415. }
  1416. if (!empty($hb)) {
  1417. $hb->status = 1;
  1418. $hb->orderId = $return->id;
  1419. $hb->save();
  1420. }
  1421. if ($return->sendType == 2) {
  1422. $ext = ShopExtClass::getByCondition(['shopId' => $return->shopId], true, null, 'id,shopId,thirdSend');
  1423. if (!empty($ext) && $ext->thirdSend == 1) {
  1424. util::fail('本店暂不能使用跑腿呢');
  1425. }
  1426. }
  1427. $transaction->commit();
  1428. $transactionFinished = true;
  1429. \bizHd\product\classes\ProductClass::clearLimitBuyRollbackSnapshot();
  1430. HomePageModuleClass::clearSeckillReservationSnapshot();
  1431. $orderSn = $return->orderSn ?? '';
  1432. $orderPrice = $return->orderPrice ?? ($return->actPrice ?? 0);
  1433. $id = $return->id ?? 0;
  1434. $getPayType = dict::getDict('getPayType');
  1435. util::success([
  1436. 'orderSn' => $orderSn,
  1437. 'totalPrice' => $orderPrice,
  1438. 'couponId' => 0,
  1439. 'id' => $id,
  1440. 'getPayType' => $getPayType,
  1441. ]);
  1442. } catch (\Exception $e) {
  1443. $transaction->rollBack();
  1444. \bizHd\product\classes\ProductClass::rollbackLimitBuySnapshot();
  1445. HomePageModuleClass::rollbackSeckillReservationSnapshot();
  1446. $transactionFinished = true;
  1447. Yii::error("合并下单失败:" . $e->getMessage());
  1448. util::fail('下单失败');
  1449. }
  1450. }
  1451. //下单要用到的相关信息 ssh 2019.12.6
  1452. public function actionOrderRelate()
  1453. {
  1454. $regionTree = RegionService::tree();
  1455. //门店名称
  1456. $shop = !empty($this->shop) ? $this->shop->attributes : [];
  1457. $sjName = $shop['merchantName'] ?? '';
  1458. $shopName = $shop['shopName'] ?? '';
  1459. $shop['showName'] = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  1460. $avatar = $shop['avatar'] ?? '';
  1461. $smallAvatar = imgUtil::groupImg($avatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  1462. $shop['smallAvatar'] = $smallAvatar;
  1463. $freight = ['first' => 5, 'add' => 2];
  1464. $mapKey = 'OFWBZ-2NTHP-EHNDD-LKWQY-GANM7-PXBJH';
  1465. $out = ['freight' => $freight, 'shop' => $shop, 'region' => $regionTree, 'thirdMapKey' => $mapKey, 'merchant' => $shop];
  1466. util::success($out);
  1467. }
  1468. //余额支付 ssh 20250410
  1469. public function actionBalancePay()
  1470. {
  1471. $orderSn = Yii::$app->request->post('orderSn');
  1472. if (empty($orderSn)) {
  1473. util::fail('订单编号缺失');
  1474. }
  1475. //避免重复提交
  1476. util::checkRepeatCommit($orderSn, 5);
  1477. $connection = Yii::$app->db;
  1478. $transaction = $connection->beginTransaction();
  1479. try {
  1480. $fields = 'id,orderSn,shopId,sjId,mainId,customId,hdId,customName,shopAdminId,shopAdminName,status,payStatus,deadline,actPrice,realPrice,mainPay,cash,remainDebtPrice,onlinePay,fromType,sendType,showAddress,packingFee,groupBuyId,thirdNo,hbId,hbAmount';
  1481. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true, null, $fields);
  1482. if (empty($order)) {
  1483. util::fail('没有找到订单');
  1484. }
  1485. if ($order->shopId != $this->shopId) {
  1486. util::fail('不是你的订单');
  1487. }
  1488. //支付前验证订单有效性
  1489. OrderService::checkBeforePay($order);
  1490. $payWay = dict::getDict('payWay', 'balancePay');
  1491. \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
  1492. $transaction->commit();
  1493. //解决重复通知,重要
  1494. $cacheKey = 'mall_order_balance_pay_' . $orderSn;
  1495. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  1496. if (empty($has)) {
  1497. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  1498. //打印小票和语音播报
  1499. $fields = 'id,orderSn,shopId,mainId,customId,needPrint,payStatus,printNum,fromType,sendType,actPrice,addTime,sendNum,customName,customMobile,shopAdminName,bookName,bookMobile,receiveUserName,receiveMobile,fullAddress,showAddress,reachDate,reachPeriod,anonymity,cardInfo,remark,goodsPrice,sendCost,serviceFee,labourCost,prePrice,orderPrice,discountAmount,realPrice,refundPrice,debt,remainDebtPrice,thirdSn,goodsNum';
  1500. $order = \bizHd\order\classes\OrderClass::getByCondition(['orderSn' => $orderSn], true, null, $fields);
  1501. if (!empty($order)) {
  1502. \bizHd\order\classes\OrderClass::onlinePrint($order);
  1503. //付款成功了,通知发跑腿功能,关键词 hd_pay_after_call_pt,多处要同步修改
  1504. HdDeliveryOrderClass::payAfter($order);
  1505. $shopId = $order->shopId ?? 0;
  1506. $shop = ShopClass::getById($shopId, true);
  1507. if (in_array($order->fromType, [1, 3])) {
  1508. //您有新的收入提醒
  1509. WxMessageClass::gatheringIncomeInform($shop, $order);
  1510. }
  1511. if ($order->fromType == 2) {
  1512. //来自商城的新订单微信通知
  1513. WxMessageClass::hdNewOrderInform($shop, $order);
  1514. }
  1515. }
  1516. }
  1517. util::complete('支付成功');
  1518. } catch (\Exception $e) {
  1519. $transaction->rollBack();
  1520. Yii::error("支付原因:" . $e->getMessage());
  1521. util::fail('支付失败');
  1522. }
  1523. }
  1524. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  1525. public function actionWxPay()
  1526. {
  1527. ini_set('date.timezone', 'Asia/Shanghai');
  1528. $post = Yii::$app->request->post();
  1529. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  1530. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1531. $order = OrderService::getByOrderSn($orderSn);
  1532. $orderId = $order['id'];
  1533. //验证优惠券是否还有效
  1534. if (!empty($couponId)) {
  1535. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  1536. }
  1537. //支付前验证订单有效性
  1538. OrderService::checkBeforePay($order);
  1539. $name = !empty($order['orderName']) ? $order['orderName'] : '购买花材';
  1540. $shop = $this->shop;
  1541. if (empty($shop)) {
  1542. util::fail('没有找到门店信息');
  1543. }
  1544. if (isset($shop->shopName) && !empty($shop->shopName)) {
  1545. $name .= '(' . $shop->shopName . ')';
  1546. }
  1547. $totalFee = $order['mainPay'];
  1548. $openId = '';
  1549. if (!empty($post['miniOpenId'])) {
  1550. $openId = $post['miniOpenId'];
  1551. //noticeUtil::push('散客买花时,通过前端获取了openId:' . $openId, '15280215347');
  1552. }
  1553. if (empty($openId)) {
  1554. if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
  1555. $openId = $this->user['miniOpenId'];
  1556. //noticeUtil::push('散客买花时,通过数据库获取了openId:' . $openId, '15280215347');
  1557. }
  1558. }
  1559. if (empty($openId)) {
  1560. util::fail('没有找到openId');
  1561. }
  1562. $now = time();
  1563. $expireTime = $now + 1800;
  1564. $updateData = [];
  1565. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1566. $oldOrderSn = $orderSn;
  1567. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1568. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1569. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1570. $params = [
  1571. 'appid' => 'OP00002119',
  1572. 'serial_no' => '018b08cfddbd',
  1573. 'merchant_no' => $shop->lklSjNo,
  1574. 'term_no' => $shop->lklScanTermNo,
  1575. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1576. 'lklCertificatePath' => $lklCertificatePath,
  1577. ];
  1578. $laResource = new Lakala($params);
  1579. $closeParams = [
  1580. 'orderSn' => $oldOrderSn,
  1581. ];
  1582. $response = $laResource->close($closeParams);
  1583. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1584. $msg = $response['msg'] ?? '';
  1585. $lklSjNo = $shop->lklSjNo ?? '';
  1586. noticeUtil::push('重点注意,散客买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . '。商户号:' . $lklSjNo, '15280215347');
  1587. util::fail('出错了哦,请重新下一单');
  1588. }
  1589. $orderSn = orderSn::getOrderSn();
  1590. $updateData['orderSn'] = $orderSn;
  1591. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  1592. } else {
  1593. //已请求的不能再修改价格
  1594. $updateData['modPrice'] = 0;
  1595. }
  1596. if (empty($order['deadline'])) {
  1597. $updateData['deadline'] = $expireTime;
  1598. }
  1599. if (!empty($updateData)) {
  1600. OrderService::updateById($orderId, $updateData);
  1601. }
  1602. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1603. $sjExtend = WxOpenClass::getMallWxInfo();
  1604. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1605. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1606. $params = [
  1607. 'appid' => 'OP00002119',
  1608. 'serial_no' => '018b08cfddbd',
  1609. 'merchant_no' => $shop->lklSjNo,
  1610. 'term_no' => $shop->lklScanTermNo,
  1611. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1612. 'lklCertificatePath' => $lklCertificatePath,
  1613. ];
  1614. $laResource = new Lakala($params);
  1615. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  1616. $wxParams = [
  1617. 'orderSn' => $orderSn,
  1618. 'amount' => $totalFee,
  1619. 'capitalType' => $capitalType,
  1620. 'notifyUrl' => $notifyUrl,
  1621. 'wxAppId' => $sjExtend['miniAppId'],
  1622. 'openId' => $openId,
  1623. 'subject' => $name,
  1624. 'couponId' => $couponId,
  1625. ];
  1626. $response = $laResource->driveWxPay($wxParams);
  1627. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1628. $errMsg = $response['msg'] ?? '';
  1629. noticeUtil::push($errMsg, '15280215347');
  1630. util::fail('支付失败');
  1631. }
  1632. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1633. $appId = $newParams['app_id'] ?? '';
  1634. $nonceStr = $newParams['nonce_str'] ?? '';
  1635. $paySign = $newParams['pay_sign'] ?? '';
  1636. $package = $newParams['package'] ?? '';
  1637. $signType = $newParams['sign_type'] ?? '';
  1638. $timeStamp = $newParams['time_stamp'] ?? '';
  1639. $new = [
  1640. 'id' => $orderId,
  1641. 'appId' => $appId,
  1642. 'nonceStr' => $nonceStr,
  1643. 'paySign' => $paySign,
  1644. 'package' => $package,
  1645. 'signType' => $signType,
  1646. 'timeStamp' => $timeStamp,
  1647. 'orderSn' => $orderSn,
  1648. ];
  1649. util::success($new);
  1650. }
  1651. //快捷付款订单
  1652. public function actionFastPay()
  1653. {
  1654. ini_set('date.timezone', 'Asia/Shanghai');
  1655. $post = Yii::$app->request->post();
  1656. $payWay = $post['payWay'] ?? 0;
  1657. $shopId = $this->shopId;
  1658. $shop = $this->shop;
  1659. if (empty($shop)) {
  1660. util::fail('没有门店信息');
  1661. }
  1662. $customId = $this->customId;
  1663. $custom = $this->custom;
  1664. $hdId = $this->hdId;
  1665. $hd = $this->hd;
  1666. if (empty($customId) || empty($hdId)) {
  1667. //考虑没有登录时的付款
  1668. $defaultCustomId = $shop->defaultCustomId ?? 0;
  1669. if (empty($defaultCustomId)) {
  1670. util::fail('请设置快捷开单的客户');
  1671. }
  1672. $customId = $defaultCustomId;
  1673. $custom = CustomClass::getById($defaultCustomId, true);
  1674. if (empty($custom)) {
  1675. util::fail('无效的快捷客户');
  1676. }
  1677. $hdId = $custom->hdId;
  1678. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  1679. if (empty($hd)) {
  1680. util::fail('没有花店信息呢!');
  1681. }
  1682. }
  1683. $post['hdId'] = $hdId;
  1684. $post['hdName'] = $hd->name ?? '';
  1685. $customName = $custom->name ?? '';
  1686. $customPy = $custom->py ?? '';
  1687. $post['customName'] = $customName;
  1688. $post['customNamePy'] = $customPy;
  1689. $post['shopId'] = $shopId;
  1690. $post['mainId'] = $this->mainId;
  1691. $post['customId'] = $customId;
  1692. $prePrice = round($post['prePrice'], 2);
  1693. $actPrice = $prePrice;
  1694. if ($actPrice <= 0) {
  1695. util::fail('请填写付款金额');
  1696. }
  1697. $post['actPrice'] = $actPrice;
  1698. $post['userId'] = $this->userId;
  1699. $now = time();
  1700. $expireTime = $now + 300;//订单5分钟后过期
  1701. $post['createTime'] = date("Y-m-d H:i:s", $now);
  1702. $post['deadline'] = date("Y-m-d H:i:s", $expireTime);
  1703. $order = ScanPayClass::addOrder($post);
  1704. $orderId = $order->id ?? 0;
  1705. $orderSn = $order->orderSn ?? '';
  1706. $capitalType = dict::getDict('capitalType', 'scanPay', 'id');
  1707. if ($payWay == 0) {
  1708. //微信
  1709. $name = '购买鲜花,扫码付款';
  1710. $totalFee = $actPrice;
  1711. $user = UserService::getById($this->userId);
  1712. //小程序使用miniOpenId
  1713. $openId = $user['miniOpenId'];
  1714. if (empty($openId)) {
  1715. $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  1716. }
  1717. if (empty($openId)) {
  1718. util::fail('没有微信信息');
  1719. }
  1720. $sjExtend = WxOpenClass::getMallWxInfo();
  1721. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1722. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1723. $params = [
  1724. 'appid' => 'OP00002119',
  1725. 'serial_no' => '018b08cfddbd',
  1726. 'merchant_no' => $shop->lklSjNo,
  1727. 'term_no' => $shop->lklScanTermNo,
  1728. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1729. 'lklCertificatePath' => $lklCertificatePath,
  1730. ];
  1731. $laResource = new Lakala($params);
  1732. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  1733. $wxParams = [
  1734. 'orderSn' => $orderSn,
  1735. 'amount' => $totalFee,
  1736. 'capitalType' => $capitalType,
  1737. 'notifyUrl' => $notifyUrl,
  1738. 'wxAppId' => $sjExtend['miniAppId'],
  1739. 'openId' => $openId,
  1740. 'subject' => $name,
  1741. 'couponId' => 0,
  1742. ];
  1743. $response = $laResource->driveWxPay($wxParams);
  1744. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1745. $errMsg = $response['msg'] ?? '';
  1746. noticeUtil::push($errMsg, '15280215347');
  1747. util::fail('支付失败');
  1748. }
  1749. $newParams = $response['resp_data']['acc_resp_fields'] ?? [];
  1750. $appId = $newParams['app_id'] ?? '';
  1751. $nonceStr = $newParams['nonce_str'] ?? '';
  1752. $paySign = $newParams['pay_sign'] ?? '';
  1753. $package = $newParams['package'] ?? '';
  1754. $signType = $newParams['sign_type'] ?? '';
  1755. $timeStamp = $newParams['time_stamp'] ?? '';
  1756. $new = [
  1757. 'id' => $orderId,
  1758. 'appId' => $appId,
  1759. 'nonceStr' => $nonceStr,
  1760. 'paySign' => $paySign,
  1761. 'package' => $package,
  1762. 'signType' => $signType,
  1763. 'timeStamp' => $timeStamp,
  1764. 'orderSn' => $orderSn,
  1765. ];
  1766. util::success($new);
  1767. } elseif ($payWay == 1) {
  1768. //支付宝
  1769. $totalFee = $actPrice;
  1770. $subject = '购买鲜花,扫码付款';
  1771. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1772. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1773. $params = [
  1774. 'appid' => 'OP00002119',
  1775. 'serial_no' => '018b08cfddbd',
  1776. 'merchant_no' => $shop->lklSjNo,
  1777. 'term_no' => $shop->lklB2BTermNo,
  1778. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1779. 'lklCertificatePath' => $lklCertificatePath,
  1780. ];
  1781. $laResource = new Lakala($params);
  1782. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
  1783. $cashParams = [
  1784. 'orderSn' => $orderSn,
  1785. 'amount' => $totalFee,
  1786. 'capitalType' => $capitalType,
  1787. 'notifyUrl' => $notifyUrl,
  1788. 'subject' => $subject,
  1789. ];
  1790. $response = $laResource->cashierPay($cashParams);
  1791. if (!isset($response['code']) || $response['code'] != '000000') {
  1792. util::fail('支付失败了');
  1793. }
  1794. $url = $response['resp_data']['counter_url'] ?? '';
  1795. if (empty($url)) {
  1796. util::fail('支付失败');
  1797. }
  1798. util::success(['url' => $url]);
  1799. } elseif ($payWay == 2) {
  1800. //余额支付
  1801. util::fail('暂不支持');
  1802. } else {
  1803. util::fail('无效的支付方式');
  1804. }
  1805. }
  1806. //订单列表 ssh 2019.12.12
  1807. public function actionList()
  1808. {
  1809. $get = Yii::$app->request->get();
  1810. $status = $get['status'] ?? 0;
  1811. $hdId = $get['hdId'] ?? 0;
  1812. $userId = $this->userId;
  1813. $where = ['userId' => $userId];
  1814. if (!empty($status)) {
  1815. $where['status'] = $status;
  1816. }
  1817. if (!empty($hdId)) {
  1818. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  1819. if (empty($hd)) {
  1820. util::fail('无效的花店');
  1821. }
  1822. if ($hd['userId'] != $userId) {
  1823. util::fail('不是你的花店');
  1824. }
  1825. $where['hdId'] = $hdId;
  1826. }
  1827. $list = OrderService::getOrderList($where);
  1828. util::success($list);
  1829. }
  1830. //订单评价
  1831. public function actionComment()
  1832. {
  1833. $post = Yii::$app->request->post();
  1834. $id = $post['id'];
  1835. $order = OrderClass::getById($id);
  1836. if ($order['grade'] > 0) {
  1837. util::fail('您已经评过了');
  1838. }
  1839. OrderService::comment($post);
  1840. util::complete('提交成功');
  1841. }
  1842. //订单详情 ssh 2019.12.16
  1843. public function actionDetail()
  1844. {
  1845. $id = Yii::$app->request->get('id', 0);
  1846. $detail = OrderClass::getOrderById($id);
  1847. $userId = $this->userId;
  1848. if ($detail['userId'] != $userId) {
  1849. util::fail('不是你的单,不能查看');
  1850. }
  1851. $shOrderMap = [];
  1852. if ($detail['hasForward'] == 1) {
  1853. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  1854. }
  1855. $detail['shOrderMap'] = $shOrderMap;
  1856. //是否显示余额支付按钮 ssh 20260120
  1857. $hd = $this->hd;
  1858. $balance = $hd->balance ?? 0;
  1859. $showYePay = $balance > 0 ? 1 : 0;
  1860. $detail['showYePay'] = $showYePay;
  1861. $detail['balance'] = $balance;
  1862. $detail['rechargeWeal'] = $this->shop->rechargeWeal; //是否开启余额支付
  1863. // 与 RefundController::canApplyRefund 对齐,供详情页「申请售后」显隐
  1864. $detail['distOrderSettled'] = DistributionOrderClass::isDistOrderSettled((int)$id) ? 1 : 0;
  1865. $detail['canApplyRefund'] = $this->canApplyRefundForDetail($detail) ? 1 : 0;
  1866. // 配送方式名称:xhPsMethod.name(style = 订单 sendType)
  1867. $detail['psMethodName'] = $this->resolvePsMethodName($detail);
  1868. util::success($detail);
  1869. }
  1870. /**
  1871. * 解析订单配送方式名称(xhPsMethod.name,无记录时回退字典默认名)
  1872. * @param array $order 订单详情
  1873. * @return string
  1874. */
  1875. protected function resolvePsMethodName($order)
  1876. {
  1877. $mainId = (int)($order['mainId'] ?? 0);
  1878. $style = (int)($order['sendType'] ?? 0);
  1879. if ($mainId > 0) {
  1880. $psMethod = PsMethodClass::getByCondition([
  1881. 'mainId' => $mainId,
  1882. 'style' => $style,
  1883. ]);
  1884. if (!empty($psMethod['name'])) {
  1885. return (string)$psMethod['name'];
  1886. }
  1887. }
  1888. $default = dict::getDict('shMethod', $style);
  1889. return (string)($default['name'] ?? '');
  1890. }
  1891. /**
  1892. * 订单详情是否可申请售后(与 mall RefundController::canApplyRefund 规则一致)
  1893. * @param array $order 订单详情
  1894. * @return bool
  1895. */
  1896. protected function canApplyRefundForDetail($order)
  1897. {
  1898. if ((int)($order['payStatus'] ?? 0) !== 1) {
  1899. return false;
  1900. }
  1901. $status = (int)($order['status'] ?? 0);
  1902. if (in_array($status, [HdOrderClass::ORDER_STATUS_UN_PAY, HdOrderClass::ORDER_STATUS_CANCEL, 6], true)) {
  1903. return false;
  1904. }
  1905. if ((int)($order['forward'] ?? 0) === 1) {
  1906. return false;
  1907. }
  1908. $apply = MallRefundApplyClass::getLatestByOrderId((int)($order['id'] ?? 0));
  1909. if (!empty($apply) && (int)($apply['status'] ?? -1) === MallRefundApplyClass::STATUS_PENDING) {
  1910. return false;
  1911. }
  1912. if (DistributionOrderClass::isDistOrderSettled((int)($order['id'] ?? 0))) {
  1913. return false;
  1914. }
  1915. $could = bcsub((string)($order['orderPrice'] ?? 0), (string)($order['tkPrice'] ?? 0), 2);
  1916. if (bccomp($could, '0', 2) <= 0) {
  1917. return false;
  1918. }
  1919. return true;
  1920. }
  1921. //获取详情 ssh 20220512
  1922. public function actionInfo()
  1923. {
  1924. $sn = Yii::$app->request->get('orderSn', '');
  1925. $detail = OrderClass::getOrderBySn($sn);
  1926. util::success($detail);
  1927. }
  1928. }