OrderController.php 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  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. $post = Yii::$app->request->post();
  1472. $orderSn = $post['orderSn'] ?? '';
  1473. //避免重复提交
  1474. util::checkRepeatCommit($orderSn, 5);
  1475. $connection = Yii::$app->db;
  1476. $transaction = $connection->beginTransaction();
  1477. try {
  1478. $order = OrderClass::getByCondition(['orderSn' => $orderSn], true);
  1479. if (empty($order)) {
  1480. util::fail('没有找到订单');
  1481. }
  1482. if ($order->shopId != $this->shopId) {
  1483. util::fail('不是你的订单');
  1484. }
  1485. //支付前验证订单有效性
  1486. OrderService::checkBeforePay($order);
  1487. $payWay = dict::getDict('payWay', 'balancePay');
  1488. \bizHd\order\classes\OrderClass::payAfter($order, $payWay);
  1489. $transaction->commit();
  1490. //解决重复通知,重要
  1491. $cacheKey = 'mall_order_balance_pay_' . $orderSn;
  1492. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  1493. if (empty($has)) {
  1494. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 864000, 'has']);
  1495. //打印小票和语音播报
  1496. $order = \bizHd\order\classes\OrderClass::getByCondition(['orderSn' => $orderSn], true);
  1497. if (!empty($order)) {
  1498. \bizHd\order\classes\OrderClass::onlinePrint($order);
  1499. //付款成功了,通知发跑腿功能,关键词 hd_pay_after_call_pt,多处要同步修改
  1500. HdDeliveryOrderClass::payAfter($order);
  1501. $shopId = $order->shopId ?? 0;
  1502. $shop = ShopClass::getById($shopId, true);
  1503. if (in_array($order->fromType, [1, 3])) {
  1504. //您有新的收入提醒
  1505. WxMessageClass::gatheringIncomeInform($shop, $order);
  1506. }
  1507. if ($order->fromType == 2) {
  1508. //来自商城的新订单微信通知
  1509. WxMessageClass::hdNewOrderInform($shop, $order);
  1510. }
  1511. }
  1512. }
  1513. util::complete('支付成功');
  1514. } catch (\Exception $e) {
  1515. $transaction->rollBack();
  1516. Yii::error("支付原因:" . $e->getMessage());
  1517. util::fail('支付失败');
  1518. }
  1519. }
  1520. //获取商城下单要用的微信支付和小程序支付参数 ssh 2019.21.3
  1521. public function actionWxPay()
  1522. {
  1523. ini_set('date.timezone', 'Asia/Shanghai');
  1524. $post = Yii::$app->request->post();
  1525. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;
  1526. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1527. $order = OrderService::getByOrderSn($orderSn);
  1528. $orderId = $order['id'];
  1529. //验证优惠券是否还有效
  1530. if (!empty($couponId)) {
  1531. CouponService::checkBeforeUse($couponId, $order['prePrice'], $order['userId']);
  1532. }
  1533. //支付前验证订单有效性
  1534. OrderService::checkBeforePay($order);
  1535. $name = !empty($order['orderName']) ? $order['orderName'] : '购买花材';
  1536. $shop = $this->shop;
  1537. if (empty($shop)) {
  1538. util::fail('没有找到门店信息');
  1539. }
  1540. if (isset($shop->shopName) && !empty($shop->shopName)) {
  1541. $name .= '(' . $shop->shopName . ')';
  1542. }
  1543. $totalFee = $order['mainPay'];
  1544. $openId = '';
  1545. if (!empty($post['miniOpenId'])) {
  1546. $openId = $post['miniOpenId'];
  1547. //noticeUtil::push('散客买花时,通过前端获取了openId:' . $openId, '15280215347');
  1548. }
  1549. if (empty($openId)) {
  1550. if (isset($this->user['miniOpenId']) && !empty($this->user['miniOpenId'])) {
  1551. $openId = $this->user['miniOpenId'];
  1552. //noticeUtil::push('散客买花时,通过数据库获取了openId:' . $openId, '15280215347');
  1553. }
  1554. }
  1555. if (empty($openId)) {
  1556. util::fail('没有找到openId');
  1557. }
  1558. $now = time();
  1559. $expireTime = $now + 1800;
  1560. $updateData = [];
  1561. if (isset($order['modPrice']) && $order['modPrice'] == 0) {
  1562. $oldOrderSn = $orderSn;
  1563. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1564. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1565. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1566. $params = [
  1567. 'appid' => 'OP00002119',
  1568. 'serial_no' => '018b08cfddbd',
  1569. 'merchant_no' => $shop->lklSjNo,
  1570. 'term_no' => $shop->lklScanTermNo,
  1571. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1572. 'lklCertificatePath' => $lklCertificatePath,
  1573. ];
  1574. $laResource = new Lakala($params);
  1575. $closeParams = [
  1576. 'orderSn' => $oldOrderSn,
  1577. ];
  1578. $response = $laResource->close($closeParams);
  1579. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1580. $msg = $response['msg'] ?? '';
  1581. $lklSjNo = $shop->lklSjNo ?? '';
  1582. noticeUtil::push('重点注意,散客买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . '。商户号:' . $lklSjNo, '15280215347');
  1583. util::fail('出错了哦,请重新下一单');
  1584. }
  1585. $orderSn = orderSn::getOrderSn();
  1586. $updateData['orderSn'] = $orderSn;
  1587. OrderItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $orderSn]);
  1588. } else {
  1589. //已请求的不能再修改价格
  1590. $updateData['modPrice'] = 0;
  1591. }
  1592. if (empty($order['deadline'])) {
  1593. $updateData['deadline'] = $expireTime;
  1594. }
  1595. if (!empty($updateData)) {
  1596. OrderService::updateById($orderId, $updateData);
  1597. }
  1598. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  1599. $sjExtend = WxOpenClass::getMallWxInfo();
  1600. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1601. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1602. $params = [
  1603. 'appid' => 'OP00002119',
  1604. 'serial_no' => '018b08cfddbd',
  1605. 'merchant_no' => $shop->lklSjNo,
  1606. 'term_no' => $shop->lklScanTermNo,
  1607. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1608. 'lklCertificatePath' => $lklCertificatePath,
  1609. ];
  1610. $laResource = new Lakala($params);
  1611. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  1612. $wxParams = [
  1613. 'orderSn' => $orderSn,
  1614. 'amount' => $totalFee,
  1615. 'capitalType' => $capitalType,
  1616. 'notifyUrl' => $notifyUrl,
  1617. 'wxAppId' => $sjExtend['miniAppId'],
  1618. 'openId' => $openId,
  1619. 'subject' => $name,
  1620. 'couponId' => $couponId,
  1621. ];
  1622. $response = $laResource->driveWxPay($wxParams);
  1623. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1624. $errMsg = $response['msg'] ?? '';
  1625. noticeUtil::push($errMsg, '15280215347');
  1626. util::fail('支付失败');
  1627. }
  1628. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1629. $appId = $newParams['app_id'] ?? '';
  1630. $nonceStr = $newParams['nonce_str'] ?? '';
  1631. $paySign = $newParams['pay_sign'] ?? '';
  1632. $package = $newParams['package'] ?? '';
  1633. $signType = $newParams['sign_type'] ?? '';
  1634. $timeStamp = $newParams['time_stamp'] ?? '';
  1635. $new = [
  1636. 'id' => $orderId,
  1637. 'appId' => $appId,
  1638. 'nonceStr' => $nonceStr,
  1639. 'paySign' => $paySign,
  1640. 'package' => $package,
  1641. 'signType' => $signType,
  1642. 'timeStamp' => $timeStamp,
  1643. 'orderSn' => $orderSn,
  1644. ];
  1645. util::success($new);
  1646. }
  1647. //快捷付款订单
  1648. public function actionFastPay()
  1649. {
  1650. ini_set('date.timezone', 'Asia/Shanghai');
  1651. $post = Yii::$app->request->post();
  1652. $payWay = $post['payWay'] ?? 0;
  1653. $shopId = $this->shopId;
  1654. $shop = $this->shop;
  1655. if (empty($shop)) {
  1656. util::fail('没有门店信息');
  1657. }
  1658. $customId = $this->customId;
  1659. $custom = $this->custom;
  1660. $hdId = $this->hdId;
  1661. $hd = $this->hd;
  1662. if (empty($customId) || empty($hdId)) {
  1663. //考虑没有登录时的付款
  1664. $defaultCustomId = $shop->defaultCustomId ?? 0;
  1665. if (empty($defaultCustomId)) {
  1666. util::fail('请设置快捷开单的客户');
  1667. }
  1668. $customId = $defaultCustomId;
  1669. $custom = CustomClass::getById($defaultCustomId, true);
  1670. if (empty($custom)) {
  1671. util::fail('无效的快捷客户');
  1672. }
  1673. $hdId = $custom->hdId;
  1674. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  1675. if (empty($hd)) {
  1676. util::fail('没有花店信息呢!');
  1677. }
  1678. }
  1679. $post['hdId'] = $hdId;
  1680. $post['hdName'] = $hd->name ?? '';
  1681. $customName = $custom->name ?? '';
  1682. $customPy = $custom->py ?? '';
  1683. $post['customName'] = $customName;
  1684. $post['customNamePy'] = $customPy;
  1685. $post['shopId'] = $shopId;
  1686. $post['mainId'] = $this->mainId;
  1687. $post['customId'] = $customId;
  1688. $prePrice = round($post['prePrice'], 2);
  1689. $actPrice = $prePrice;
  1690. if ($actPrice <= 0) {
  1691. util::fail('请填写付款金额');
  1692. }
  1693. $post['actPrice'] = $actPrice;
  1694. $post['userId'] = $this->userId;
  1695. $now = time();
  1696. $expireTime = $now + 300;//订单5分钟后过期
  1697. $post['createTime'] = date("Y-m-d H:i:s", $now);
  1698. $post['deadline'] = date("Y-m-d H:i:s", $expireTime);
  1699. $order = ScanPayClass::addOrder($post);
  1700. $orderId = $order->id ?? 0;
  1701. $orderSn = $order->orderSn ?? '';
  1702. $capitalType = dict::getDict('capitalType', 'scanPay', 'id');
  1703. if ($payWay == 0) {
  1704. //微信
  1705. $name = '购买鲜花,扫码付款';
  1706. $totalFee = $actPrice;
  1707. $user = UserService::getById($this->userId);
  1708. //小程序使用miniOpenId
  1709. $openId = $user['miniOpenId'];
  1710. if (empty($openId)) {
  1711. $openId = !empty($post['miniOpenId']) ? $post['miniOpenId'] : '';
  1712. }
  1713. if (empty($openId)) {
  1714. util::fail('没有微信信息');
  1715. }
  1716. $sjExtend = WxOpenClass::getMallWxInfo();
  1717. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1718. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1719. $params = [
  1720. 'appid' => 'OP00002119',
  1721. 'serial_no' => '018b08cfddbd',
  1722. 'merchant_no' => $shop->lklSjNo,
  1723. 'term_no' => $shop->lklScanTermNo,
  1724. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1725. 'lklCertificatePath' => $lklCertificatePath,
  1726. ];
  1727. $laResource = new Lakala($params);
  1728. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/pay-callback";
  1729. $wxParams = [
  1730. 'orderSn' => $orderSn,
  1731. 'amount' => $totalFee,
  1732. 'capitalType' => $capitalType,
  1733. 'notifyUrl' => $notifyUrl,
  1734. 'wxAppId' => $sjExtend['miniAppId'],
  1735. 'openId' => $openId,
  1736. 'subject' => $name,
  1737. 'couponId' => 0,
  1738. ];
  1739. $response = $laResource->driveWxPay($wxParams);
  1740. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1741. $errMsg = $response['msg'] ?? '';
  1742. noticeUtil::push($errMsg, '15280215347');
  1743. util::fail('支付失败');
  1744. }
  1745. $newParams = $response['resp_data']['acc_resp_fields'] ?? [];
  1746. $appId = $newParams['app_id'] ?? '';
  1747. $nonceStr = $newParams['nonce_str'] ?? '';
  1748. $paySign = $newParams['pay_sign'] ?? '';
  1749. $package = $newParams['package'] ?? '';
  1750. $signType = $newParams['sign_type'] ?? '';
  1751. $timeStamp = $newParams['time_stamp'] ?? '';
  1752. $new = [
  1753. 'id' => $orderId,
  1754. 'appId' => $appId,
  1755. 'nonceStr' => $nonceStr,
  1756. 'paySign' => $paySign,
  1757. 'package' => $package,
  1758. 'signType' => $signType,
  1759. 'timeStamp' => $timeStamp,
  1760. 'orderSn' => $orderSn,
  1761. ];
  1762. util::success($new);
  1763. } elseif ($payWay == 1) {
  1764. //支付宝
  1765. $totalFee = $actPrice;
  1766. $subject = '购买鲜花,扫码付款';
  1767. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1768. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1769. $params = [
  1770. 'appid' => 'OP00002119',
  1771. 'serial_no' => '018b08cfddbd',
  1772. 'merchant_no' => $shop->lklSjNo,
  1773. 'term_no' => $shop->lklB2BTermNo,
  1774. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1775. 'lklCertificatePath' => $lklCertificatePath,
  1776. ];
  1777. $laResource = new Lakala($params);
  1778. $notifyUrl = Yii::$app->params['mallHost'] . "/notice/cash-pay-callback";
  1779. $cashParams = [
  1780. 'orderSn' => $orderSn,
  1781. 'amount' => $totalFee,
  1782. 'capitalType' => $capitalType,
  1783. 'notifyUrl' => $notifyUrl,
  1784. 'subject' => $subject,
  1785. ];
  1786. $response = $laResource->cashierPay($cashParams);
  1787. if (!isset($response['code']) || $response['code'] != '000000') {
  1788. util::fail('支付失败了');
  1789. }
  1790. $url = $response['resp_data']['counter_url'] ?? '';
  1791. if (empty($url)) {
  1792. util::fail('支付失败');
  1793. }
  1794. util::success(['url' => $url]);
  1795. } elseif ($payWay == 2) {
  1796. //余额支付
  1797. util::fail('暂不支持');
  1798. } else {
  1799. util::fail('无效的支付方式');
  1800. }
  1801. }
  1802. //订单列表 ssh 2019.12.12
  1803. public function actionList()
  1804. {
  1805. $get = Yii::$app->request->get();
  1806. $status = $get['status'] ?? 0;
  1807. $hdId = $get['hdId'] ?? 0;
  1808. $userId = $this->userId;
  1809. $where = ['userId' => $userId];
  1810. if (!empty($status)) {
  1811. $where['status'] = $status;
  1812. }
  1813. if (!empty($hdId)) {
  1814. $hd = \bizMall\hd\classes\HdClass::getById($hdId, true);
  1815. if (empty($hd)) {
  1816. util::fail('无效的花店');
  1817. }
  1818. if ($hd['userId'] != $userId) {
  1819. util::fail('不是你的花店');
  1820. }
  1821. $where['hdId'] = $hdId;
  1822. }
  1823. $list = OrderService::getOrderList($where);
  1824. util::success($list);
  1825. }
  1826. //订单评价
  1827. public function actionComment()
  1828. {
  1829. $post = Yii::$app->request->post();
  1830. $id = $post['id'];
  1831. $order = OrderClass::getById($id);
  1832. if ($order['grade'] > 0) {
  1833. util::fail('您已经评过了');
  1834. }
  1835. OrderService::comment($post);
  1836. util::complete('提交成功');
  1837. }
  1838. //订单详情 ssh 2019.12.16
  1839. public function actionDetail()
  1840. {
  1841. $id = Yii::$app->request->get('id', 0);
  1842. $detail = OrderClass::getOrderById($id);
  1843. $userId = $this->userId;
  1844. if ($detail['userId'] != $userId) {
  1845. util::fail('不是你的单,不能查看');
  1846. }
  1847. $shOrderMap = [];
  1848. if ($detail['hasForward'] == 1) {
  1849. $shOrderMap = OrderForwardClass::getAllByCondition(['orderId' => $id], null, '*');
  1850. }
  1851. $detail['shOrderMap'] = $shOrderMap;
  1852. //是否显示余额支付按钮 ssh 20260120
  1853. $hd = $this->hd;
  1854. $balance = $hd->balance ?? 0;
  1855. $showYePay = $balance > 0 ? 1 : 0;
  1856. $detail['showYePay'] = $showYePay;
  1857. $detail['balance'] = $balance;
  1858. $detail['rechargeWeal'] = $this->shop->rechargeWeal; //是否开启余额支付
  1859. // 与 RefundController::canApplyRefund 对齐,供详情页「申请售后」显隐
  1860. $detail['distOrderSettled'] = DistributionOrderClass::isDistOrderSettled((int)$id) ? 1 : 0;
  1861. $detail['canApplyRefund'] = $this->canApplyRefundForDetail($detail) ? 1 : 0;
  1862. // 配送方式名称:xhPsMethod.name(style = 订单 sendType)
  1863. $detail['psMethodName'] = $this->resolvePsMethodName($detail);
  1864. util::success($detail);
  1865. }
  1866. /**
  1867. * 解析订单配送方式名称(xhPsMethod.name,无记录时回退字典默认名)
  1868. * @param array $order 订单详情
  1869. * @return string
  1870. */
  1871. protected function resolvePsMethodName($order)
  1872. {
  1873. $mainId = (int)($order['mainId'] ?? 0);
  1874. $style = (int)($order['sendType'] ?? 0);
  1875. if ($mainId > 0) {
  1876. $psMethod = PsMethodClass::getByCondition([
  1877. 'mainId' => $mainId,
  1878. 'style' => $style,
  1879. ]);
  1880. if (!empty($psMethod['name'])) {
  1881. return (string)$psMethod['name'];
  1882. }
  1883. }
  1884. $default = dict::getDict('shMethod', $style);
  1885. return (string)($default['name'] ?? '');
  1886. }
  1887. /**
  1888. * 订单详情是否可申请售后(与 mall RefundController::canApplyRefund 规则一致)
  1889. * @param array $order 订单详情
  1890. * @return bool
  1891. */
  1892. protected function canApplyRefundForDetail($order)
  1893. {
  1894. if ((int)($order['payStatus'] ?? 0) !== 1) {
  1895. return false;
  1896. }
  1897. $status = (int)($order['status'] ?? 0);
  1898. if (in_array($status, [HdOrderClass::ORDER_STATUS_UN_PAY, HdOrderClass::ORDER_STATUS_CANCEL, 6], true)) {
  1899. return false;
  1900. }
  1901. if ((int)($order['forward'] ?? 0) === 1) {
  1902. return false;
  1903. }
  1904. $apply = MallRefundApplyClass::getLatestByOrderId((int)($order['id'] ?? 0));
  1905. if (!empty($apply) && (int)($apply['status'] ?? -1) === MallRefundApplyClass::STATUS_PENDING) {
  1906. return false;
  1907. }
  1908. if (DistributionOrderClass::isDistOrderSettled((int)($order['id'] ?? 0))) {
  1909. return false;
  1910. }
  1911. $could = bcsub((string)($order['orderPrice'] ?? 0), (string)($order['tkPrice'] ?? 0), 2);
  1912. if (bccomp($could, '0', 2) <= 0) {
  1913. return false;
  1914. }
  1915. return true;
  1916. }
  1917. //获取详情 ssh 20220512
  1918. public function actionInfo()
  1919. {
  1920. $sn = Yii::$app->request->get('orderSn', '');
  1921. $detail = OrderClass::getOrderBySn($sn);
  1922. util::success($detail);
  1923. }
  1924. }