PurchaseController.php 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\common\classes\GhsNotifyClass;
  5. use biz\ghs\classes\GhsClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExtClass;
  8. use bizGhs\custom\classes\AccountMoneyClass;
  9. use bizGhs\custom\classes\CustomClass;
  10. use bizGhs\express\classes\GhsDeliveryOrderClass;
  11. use bizGhs\order\classes\OrderClass;
  12. use bizGhs\order\classes\ShMethodClass;
  13. use bizHd\cg\classes\CgMergeSnClass;
  14. use bizHd\purchase\classes\PurchaseClass;
  15. use bizHd\purchase\classes\PurchaseItemClass;
  16. use bizHd\purchase\services\PurchaseService;
  17. use bizGhs\product\classes\ProductClass;
  18. use bizHd\wx\classes\WxOpenClass;
  19. use common\components\dateUtil;
  20. use common\components\delivery\util\DeliveryQuoteUtil;
  21. use common\components\dict;
  22. use common\components\imgUtil;
  23. use common\components\noticeUtil;
  24. use common\components\orderSn;
  25. use Yii;
  26. use common\components\util;
  27. use bizGhs\order\services\OrderService;
  28. use common\components\lakala\Lakala;
  29. use common\components\qrCodeUtil;
  30. use common\components\push;
  31. class PurchaseController extends BaseController
  32. {
  33. public $guestAccess = ['detail', 'ali-pay', 'get-ali-pay-code', 'wx-pay', 'merge-order-list', 'debt-list'];
  34. //取消采购单 ssh 20250710
  35. public function actionCancel()
  36. {
  37. $get = Yii::$app->request->get();
  38. $id = $get['id'] ?? '';
  39. $info = PurchaseClass::getById($id, true);
  40. if (empty($info)) {
  41. util::fail('没有找到采购单');
  42. }
  43. if (!isset($info->mainId) || $info->mainId != $this->mainId) {
  44. util::fail('访问错误');
  45. }
  46. if ($info->status == 5) {
  47. util::fail('已取消');
  48. }
  49. if ($info->status != 1) {
  50. util::fail('不能取消');
  51. }
  52. PurchaseService::expire($info);
  53. util::complete('已取消');
  54. }
  55. //获取支付宝付款码 ssh 20240713
  56. public function actionGetAliPayCode()
  57. {
  58. $get = Yii::$app->request->get();
  59. $orderSn = $get['orderSn'] ?? '';
  60. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  61. if (empty($info)) {
  62. util::fail('没有找到采购单');
  63. }
  64. if (!isset($info->mainId) || $info->mainId != $this->mainId) {
  65. //重要,这个不能限制,因为开单分享给客户付款的,要显示支付宝二维码,限制了会不显示!!!!!! shish 20260326
  66. //util::fail('无法访问!');
  67. }
  68. if (getenv('YII_ENV') == 'production') {
  69. $url = "https://mall.huahb.cn/#/admin/cg/alipay?orderSn={$orderSn}";
  70. } else {
  71. $url = "https://mall.huaml.com/#/admin/cg/alipay?orderSn={$orderSn}";
  72. }
  73. $unique = "order_ali_pay_" . $orderSn;
  74. $imgUrl = qrCodeUtil::generateRechargeQrCode($url, $unique);
  75. $imageUrl = imgUtil::groupImg($imgUrl);
  76. util::success(['imgUrl' => $imageUrl]);
  77. }
  78. //确认收货 ssh 20231119
  79. public function actionConfirmTake()
  80. {
  81. $get = Yii::$app->request->get();
  82. $id = $get['id'] ?? 0;
  83. $info = PurchaseClass::getById($id, true);
  84. if (empty($info)) {
  85. util::fail('没有找到采购单');
  86. }
  87. if (isset($info->mainId) == false || $info->mainId != $this->mainId) {
  88. util::fail('无法访问!!');
  89. }
  90. PurchaseClass::confirmTake($info);
  91. util::complete('确认成功');
  92. }
  93. //这个接口只能用于
  94. public function actionGetFullInfo()
  95. {
  96. $get = Yii::$app->request->get();
  97. $id = $get['id'] ?? 0;
  98. $info = PurchaseClass::getById($id);
  99. if (empty($info)) {
  100. util::fail('没有找到采购单');
  101. }
  102. if (!isset($info['mainId']) || $info['mainId'] != $this->mainId) {
  103. util::fail('无法访问。。。');
  104. }
  105. $orderSn = $info['orderSn'] ?? '';
  106. $ghsShopId = $info['ghsShopId'] ?? 0;
  107. if (empty($ghsShopId)) {
  108. $ghsId = $info['ghsId'] ?? 0;
  109. if (!empty($ghsId)) {
  110. $ghs = GhsClass::getById($ghsId, true);
  111. $ghsShopId = $ghs->shopId ?? 0;
  112. PurchaseClass::updateById($id, ['ghsShopId' => $ghsShopId]);
  113. }
  114. }
  115. //申请售后 0禁止 1允许
  116. $afterSale = 1;
  117. //下单后多少小时内可以售后,0不限制
  118. $saleHour = 12;
  119. //是否可以仅退款 1支持 0不支持
  120. $refundOnlyPrice = 1;
  121. //售后是否要求上传照片 0不要求 1必须
  122. $refundMustImg = 0;
  123. if (!empty($ghsShopId)) {
  124. $shop = ShopClass::getByCondition(['id' => $ghsShopId], false, false, 'id,afterSale,saleHour,refundOnlyPrice,refundMustImg');
  125. $afterSale = $shop['afterSale'] ?? 1;
  126. $saleHour = $shop['saleHour'] ?? 12;
  127. $refundOnlyPrice = $shop['refundOnlyPrice'] ?? 1;
  128. $refundMustImg = $shop['refundMustImg'] ?? 0;
  129. }
  130. //售后规则
  131. $info['afterSaleRule'] = ['afterSale' => $afterSale, 'saleHour' => $saleHour, 'refundOnlyPrice' => $refundOnlyPrice, 'refundMustImg' => $refundMustImg];
  132. $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  133. if (!empty($list)) {
  134. foreach ($list as $key => $item) {
  135. $shortCover = $item['cover'] ?? '';
  136. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  137. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  138. $list[$key]['cover'] = $cover;
  139. $list[$key]['bigCover'] = $bigCover;
  140. $list[$key]['shortCover'] = $shortCover;
  141. }
  142. }
  143. $info['product'] = $list;
  144. util::success($info);
  145. }
  146. //虚拟供货商的采购 ssh 20230308
  147. public function actionAddVirtualCg()
  148. {
  149. $post = Yii::$app->request->post();
  150. $ghsId = $post['ghsId'] ?? 0;
  151. $payMode = $post['payMode'] ?? 0;
  152. $payWay = $post['payWay'] ?? 0;
  153. $ghsInfo = GhsClass::getById($ghsId);
  154. if (empty($ghsInfo)) {
  155. util::fail('没有找到供货商');
  156. }
  157. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  158. util::fail('不能向自己的店买花');
  159. }
  160. $connection = Yii::$app->db;
  161. $transaction = $connection->beginTransaction();
  162. $transactionFinished = false;
  163. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  164. if ($transactionFinished) {
  165. return;
  166. }
  167. ProductClass::rollbackLimitBuySnapshot();
  168. if ($transaction->isActive) {
  169. $transaction->rollBack();
  170. }
  171. });
  172. try {
  173. $post['book'] = $post['book'] ?? 0;
  174. $post['sjId'] = $this->sjId;
  175. $post['shopId'] = $this->shopId;
  176. $post['shopAdminId'] = $this->shopAdminId;
  177. $post['ghsId'] = $ghsId;
  178. $post['ghsName'] = $ghsInfo['name'] ?? '';
  179. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  180. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  181. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  182. $customId = $ghsInfo['customId'] ?? 0;
  183. $post['customId'] = $customId;
  184. $shopAdmin = $this->shopAdmin ?? null;
  185. $name = $shopAdmin->name ?? '';
  186. $post['shopAdminName'] = $name;
  187. $post['mainId'] = $this->mainId;
  188. $product = $post['product'] ?? '';
  189. $productList = json_decode($product, true);
  190. //判断product数据是不是同个供货商的
  191. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  192. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  193. if (!empty($productInfoList)) {
  194. foreach ($productInfoList as $currentInfo) {
  195. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  196. util::fail('只能选一家的商品,请清空重选。');
  197. }
  198. }
  199. if (count($productInfoList) != count($ids)) {
  200. util::fail('存在无效商品');
  201. }
  202. } else {
  203. util::fail('花材不存在');
  204. }
  205. //供货商预订单最低公斤数要求和每公斤服务费
  206. $ghsInfo['kiloFee'] = 0;
  207. $ghsInfo['miniKilo'] = 0;
  208. $post['product'] = $productList;
  209. $packCost = 0;
  210. $post['packCost'] = $packCost;
  211. $post['sendCost'] = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  212. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  213. if ($payMode == 0) {
  214. //欠款
  215. PurchaseService::debt($respond);
  216. } else {
  217. //线下支付
  218. PurchaseService::payAfter($respond, $payWay);
  219. $saleId = $respond->saleId ?? 0;
  220. $order = OrderClass::getById($saleId, true);
  221. if (empty($order)) {
  222. util::fail('没有找到订单');
  223. }
  224. OrderService::payAfter($order, $payWay, true);
  225. }
  226. $transaction->commit();
  227. $transactionFinished = true;
  228. ProductClass::clearLimitBuyRollbackSnapshot();
  229. util::success($respond);
  230. } catch (\Throwable $e) {
  231. if ($transaction->isActive) {
  232. $transaction->rollBack();
  233. }
  234. ProductClass::rollbackLimitBuySnapshot();
  235. $transactionFinished = true;
  236. util::fail();
  237. }
  238. }
  239. // 批发商的限购花材信息
  240. public function actionLimitBuyInfo()
  241. {
  242. $post = Yii::$app->request->post();
  243. $ghsId = $post['ghsId'] ?? 0;
  244. $list = $post['list'] ?? [];
  245. if (empty($ghsId)) {
  246. util::fail('缺少供货商');
  247. }
  248. if (empty($list) || !is_array($list)) {
  249. util::fail('请选择花材');
  250. }
  251. $ghsInfo = GhsClass::getById($ghsId);
  252. if (empty($ghsInfo)) {
  253. util::fail('没有找到供货商');
  254. }
  255. $customId = $ghsInfo['customId'] ?? 0;
  256. if (empty($customId)) {
  257. util::fail('没有找到客户');
  258. }
  259. $respond = ProductClass::getLimitBuyInfoByList($list, $customId);
  260. util::success($respond);
  261. }
  262. //生成采购单 ssh 2021.1.17
  263. public function actionCreateOrder()
  264. {
  265. $post = Yii::$app->request->post();
  266. $version = $post['version'] ?? 0;
  267. $direct = $post['direct'] ?? 0;
  268. //避免重复提交
  269. $adminId = $this->adminId;
  270. util::checkRepeatCommit($adminId, 3);
  271. //多颜色数据整理
  272. $colorItem = $post['xj'] ?? [];
  273. $newXj = [];
  274. if (!empty($colorItem)) {
  275. $colorData = json_decode($colorItem, true);
  276. if (!empty($colorData) && is_array($colorData)) {
  277. foreach ($colorData as $colorInfo) {
  278. $ptItemId = $colorInfo['ptItemId'] ?? 0;
  279. $colorList = $colorInfo['list'] ?? [];
  280. if (!empty($colorList)) {
  281. foreach ($colorList as $colorItemKey => $colorItem) {
  282. $newXj[$ptItemId][] = $colorItem;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. $post['xj'] = $newXj;
  289. $ghsId = $post['ghsId'] ?? 0;
  290. //供货商
  291. $ghsInfo = GhsClass::getById($ghsId);
  292. if (empty($ghsInfo)) {
  293. util::fail('没有找到供货商');
  294. }
  295. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  296. if (!empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  297. util::fail('不能跟自己的店买花');
  298. }
  299. $connection = Yii::$app->db;
  300. $transaction = $connection->beginTransaction();
  301. $transactionFinished = false;
  302. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  303. if ($transactionFinished) {
  304. return;
  305. }
  306. ProductClass::rollbackLimitBuySnapshot();
  307. if ($transaction->isActive) {
  308. $transaction->rollBack();
  309. }
  310. });
  311. try {
  312. $post['book'] = $post['book'] ?? 0;
  313. $post['sjId'] = $this->sjId;
  314. $post['shopId'] = $this->shopId;
  315. $post['shopAdminId'] = $this->shopAdminId;
  316. $post['ghsId'] = $ghsId;
  317. $post['ghsName'] = $ghsInfo['name'] ?? '';
  318. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  319. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  320. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  321. $customId = $ghsInfo['customId'] ?? 0;
  322. $custom = CustomClass::getById($customId, true);
  323. if (empty($custom)) {
  324. util::fail('用户信息缺失');
  325. }
  326. $post['customId'] = $customId;
  327. $shopAdmin = $this->shopAdmin ?? null;
  328. $name = $shopAdmin->name ?? '';
  329. $post['shopAdminName'] = $name;
  330. $post['mainId'] = $this->mainId;
  331. $product = $post['product'] ?? '';
  332. $productList = json_decode($product, true);
  333. //判断product数据是不是同个供货商的
  334. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  335. $ghsMainId = $ghsShopInfo->mainId ?? 0;
  336. $isOpen = ShopClass::isOpen($ghsShopInfo);
  337. $book = $post['book'] ?? 0;
  338. if ($isOpen == 0 && $book == 0) {
  339. util::fail('本店已休息,请选择其它门店');
  340. }
  341. if (isset($post['transType']) && $post['transType'] == 4) {
  342. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  343. $notSameCity = PurchaseClass::notSameCity($this->shop, $ghsShopInfo);
  344. if (!$notSameCity) {
  345. util::fail('距离太远,不能选择同城配送');
  346. }
  347. }
  348. }
  349. if ($book == 1) {
  350. $sendTimeWant = isset($post['sendTimeWant']) && !empty($post['sendTimeWant']) ? $post['sendTimeWant'] : date("Y-m-d");
  351. $future = date("Y-m-d", strtotime('+2 day'));
  352. if (strtotime($sendTimeWant) < strtotime($future)) {
  353. //util::fail('请选择时间');
  354. }
  355. $nowTime = strtotime(date("Y-m-d"));
  356. if (strtotime($sendTimeWant) < $nowTime) {
  357. util::fail('不能选择过去时间');
  358. }
  359. }
  360. //收集客户要下单的花材数量
  361. $orderNum = [];
  362. $productCount = 0;
  363. foreach ($productList as $key => $product) {
  364. $bigNum = $product['bigNum'] ?? 0;
  365. $smallNum = $product['smallNum'] ?? 0;
  366. $pName = $product['name'] ?? '';
  367. if ($smallNum > 0) {
  368. util::fail($pName . '不能选小单位');
  369. }
  370. $productId = $product['productId'] ?? 0;
  371. $orderNum[$productId] = ['num' => $bigNum];
  372. $productCount += $bigNum;
  373. }
  374. $lackList = [];
  375. $changeList = [];
  376. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  377. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  378. if (!empty($productInfoList)) {
  379. $presellData = [];
  380. $nowTime = strtotime(date("Y-m-d"));
  381. foreach ($productInfoList as $currentInfo) {
  382. if (!isset($currentInfo['mainId']) || $currentInfo['mainId'] != $ghsMainId) {
  383. util::fail('只能选一家的商品,请清空重新选。');
  384. }
  385. $currentName = $currentInfo['name'] ?? '';
  386. if (isset($currentInfo['frontHide']) && $currentInfo['frontHide'] == 1) {
  387. //珑松珠海店,隐藏的商品,分享出去了也要能下单。644 是测试账号的mainId
  388. if (isset($currentInfo['mainId']) && in_array($currentInfo['mainId'], [14116, 644])) {
  389. //无操作
  390. } else {
  391. util::fail($currentName . ' 没有库存,请删除');
  392. }
  393. }
  394. $presell = $currentInfo['presell'] ?? 0;
  395. $presellData[] = $presell;
  396. if ($presell == 1) {
  397. if ($book == 1) {
  398. util::fail('预订时不能选择预售花材');
  399. } else {
  400. if (empty($post['sendTimeWant'])) {
  401. util::fail('请选择配送日期');
  402. }
  403. $sendTimeWant = $post['sendTimeWant'];
  404. if (strtotime($sendTimeWant) < $nowTime) {
  405. util::fail('不能选择过去时间');
  406. }
  407. $hasDateStr = $currentInfo['presellDate'] ?? [];
  408. $hasDate = explode(',', trim($hasDateStr));
  409. if (empty($hasDate)) {
  410. util::fail('预售日期有问题,请提醒门店');
  411. }
  412. if (!in_array(strtotime($sendTimeWant), $hasDate)) {
  413. util::fail("有预售花材在{$sendTimeWant}没到货");
  414. }
  415. $post['presell'] = 1;
  416. }
  417. }
  418. if ($version >= 10) {
  419. $stock = $currentInfo['stock'] ?? 0;
  420. $stock = floatval($stock);
  421. //会有0.5扎和2.5扎问题,所以要转成整数,不然会出严重问题 ssh 20250503
  422. $stock = intval($stock);
  423. $productId = $currentInfo['id'] ?? 0;
  424. $productName = $currentInfo['name'] ?? '';
  425. $num = $orderNum[$productId] && $orderNum[$productId]['num'] ? $orderNum[$productId]['num'] : 0;
  426. $num = floatval($num);
  427. if ($num > $stock) {
  428. $lackList[] = ['name' => $productName, 'stock' => $stock];
  429. $changeList[$productId] = $stock;
  430. }
  431. }
  432. }
  433. if (count($productInfoList) != count($ids)) {
  434. util::fail('存在无效商品');
  435. }
  436. $presellData = array_unique($presellData);
  437. if (count($presellData) > 1) {
  438. util::fail('预售和非预售花材不能一起下单');
  439. }
  440. if ($version >= 10) {
  441. if ($direct == 1) {
  442. //直接更换库存并提交
  443. $allNoStock = true;
  444. foreach ($productList as $pKey => $pVal) {
  445. $productId = $pVal['productId'] ?? 0;
  446. if (isset($changeList[$productId])) {
  447. $changeStock = $changeList[$productId];
  448. $productList[$pKey]['bigNum'] = $changeStock;
  449. }
  450. if ($productList[$pKey]['bigNum'] > 0) {
  451. $allNoStock = false;
  452. }
  453. if ($productList[$pKey]['bigNum'] <= 0) {
  454. unset($productList[$pKey]);
  455. }
  456. }
  457. if ($allNoStock) {
  458. util::fail('全部没库存了');
  459. }
  460. } else {
  461. //前台提示库存不足
  462. if (!empty($lackList)) {
  463. util::success(['lackList' => $lackList, 'hasLackError' => 1]);
  464. }
  465. }
  466. }
  467. // 已经有了 -- 在 replaceItem()
  468. // foreach ($productList as $itemData) {
  469. // $productId = $itemData['productId'] ?? 0;
  470. // $product = $productInfoList[$productId] ?? [];
  471. // if (empty($product)) {
  472. // util::fail('有商品信息没有找到');
  473. // }
  474. // $bigNum = $itemData['bigNum'] ?? 0;
  475. // if (($product['limitBuy'] ?? 0) > 0) {
  476. // $product['productId'] = $productId;
  477. // ProductClass::handleLimitBuy($product, $customId, floatval($bigNum));
  478. // }
  479. // }
  480. } else {
  481. util::fail('花材不存在');
  482. }
  483. //供货商预订单最低公斤数要求和每公斤服务费
  484. $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
  485. $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
  486. $post['product'] = $productList;
  487. // xhShMethod 门店下单前保留花材合计,供后续 packCost 计算(pfLevel==0 分支会 unset itemTotalAmount)
  488. $shMethodItemAmount = $post['itemTotalAmount'] ?? 0;
  489. $sendType = $post['sendType'] ?? 0;
  490. $transType = $post['transType'] ?? 0;
  491. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  492. $shopExt = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,thirdSend,hcMap,hcFreeKm');
  493. if ($shopExt->thirdSend == 1) {
  494. util::fail('本店暂不能使用跑腿哦');
  495. }
  496. }
  497. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  498. //昆明发货包装费和运费的计算
  499. $totalWeight = 0;
  500. $totalItemNum = 0;
  501. foreach ($productList as $itemData) {
  502. $bigNum = $itemData['bigNum'] ?? 0;
  503. $thisWeight = $itemData['weight'] ?? 0;
  504. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  505. $totalWeight = bcadd($currentWeight, $totalWeight, 2);
  506. $totalItemNum = bcadd($totalItemNum, $bigNum, 2);
  507. }
  508. $totalItemNum = floatval($totalItemNum);
  509. $kmPackCost = PurchaseClass::getKmPackCost($ghsMainId);
  510. $packCost = 0;
  511. if (!empty($kmPackCost)) {
  512. foreach ($kmPackCost as $itemPack) {
  513. $kiloNum = $itemPack['num'] ?? 0;
  514. if ($kiloNum >= $totalWeight) {
  515. $packCost = $itemPack['amount'] ?? 0;
  516. break;
  517. }
  518. }
  519. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  520. noticeUtil::push("总重量" . $totalWeight, '15280215347');
  521. }
  522. }
  523. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  524. noticeUtil::push("走的pfLevel=1", '15280215347');
  525. }
  526. //同城配送和到店自取免打包费
  527. if ($transType == 4 || $transType == 5) {
  528. $packCost = 0;
  529. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  530. noticeUtil::push("没有计算打包费原因:transType = 4 =5", '15280215347');
  531. }
  532. }
  533. //如果已经算过一次打包费和运费了,则不再费了
  534. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  535. if (!$needAdd) {
  536. $packCost = 0;
  537. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  538. noticeUtil::push("没有计算打包费原因:已经算过一次了", '15280215347');
  539. }
  540. }
  541. $post['packCost'] = $packCost;
  542. $transCost = dict::getDict('transCost', null, null, $ghsShopInfo->mainId);
  543. $sendCost = 0;
  544. if (!empty($transCost)) {
  545. foreach ($transCost as $trans) {
  546. $sign = $trans['sign'] ?? 0;
  547. if ($sign == $transType) {
  548. if ($sign == 2) {
  549. //冷链物流
  550. $llOption = $trans['option'] ?? [];
  551. if (!empty($llOption)) {
  552. $largeInfo = end($llOption);
  553. $largeNum = $largeInfo['num'] ?? 0;
  554. $largeAmount = $largeInfo['amount'] ?? 0;
  555. $bs = intval($totalItemNum / $largeNum);
  556. if ($bs > 0) {
  557. $addSendCost = bcmul($bs, $largeAmount, 2);
  558. $sendCost = bcadd($sendCost, $addSendCost, 2);
  559. $subNum = bcmul($bs, $largeNum, 2);
  560. $subNum = floatval($subNum);
  561. $totalItemNum = bcsub($totalItemNum, $subNum, 2);
  562. $totalItemNum = floatval($totalItemNum);
  563. }
  564. foreach ($llOption as $miniItem) {
  565. if ($miniItem['num'] >= $totalItemNum) {
  566. $sendCost = bcadd($sendCost, $miniItem['amount'], 2);
  567. break;
  568. }
  569. }
  570. }
  571. } else {
  572. //其它物流
  573. $perKiloCost = $trans['perKiloCost'] ?? 0;
  574. $sendCost = bcmul($perKiloCost, $totalWeight, 3);
  575. $sendCost = round($sendCost, 2);
  576. }
  577. }
  578. }
  579. }
  580. //如果已经算过一次打包费和运费了,则不再费了
  581. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  582. if ($needAdd == false) {
  583. $sendCost = 0;
  584. }
  585. $post['sendCost'] = $sendCost;
  586. //标记为昆明到地方订单
  587. $post['localOrder'] = 1;
  588. } else {
  589. //同城发货包装费和运费的计算
  590. $sendCost = 0;
  591. $sendDistance = 0;
  592. //特别注意,这边取供货商的shop信息
  593. $mapSet = ShopClass::hasIntraCity($ghsShopInfo);
  594. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  595. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  596. if ($openIntraCity == 2) {
  597. util::fail('不能使用跑腿');
  598. }
  599. if ($openIntraCity == 1) {
  600. // 生成随机订单号,不要跟原来的混起来,跑腿-销售单-
  601. $prefix = 'PT-XSD-' . $this->mainId . '-';
  602. $orderSn = $prefix . round(microtime(true) * 1000);
  603. // 使用 DeliveryQuoteUtil 获取配送报价
  604. try {
  605. if (empty($post['deliveryPlatform'])) {
  606. util::fail('请选择跑腿');
  607. }
  608. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  609. 'productList' => $productList,
  610. 'deliveryPlatform' => $post['deliveryPlatform'],
  611. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  612. 'ghsInfo' => $ghsInfo,
  613. 'custom' => $custom,
  614. 'order' => [
  615. 'orderSn' => $orderSn,
  616. 'goodsType' => 1, //商品类型:0花束 1花材
  617. 'itemTotalAmount' => $post['itemTotalAmount'] ?? 0,
  618. 'remark' => $post['remark'] ?? '',
  619. ],
  620. 'mainId' => $this->mainId,
  621. 'productCount' => $productCount,
  622. ]);
  623. $sendCost = $quoteResult['sendCost'];
  624. $sendDistance = $quoteResult['sendDistance'];
  625. } catch (\Exception $e) {
  626. util::fail($e->getMessage());
  627. }
  628. }
  629. }
  630. $post['sendCost'] = $sendCost;
  631. $post['sendDistance'] = $sendDistance;
  632. //出车、发快递、发物流可以算包装费
  633. $packCost = 0;
  634. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  635. noticeUtil::push("走的pfLevel=0", '15280215347');
  636. }
  637. $itemTotalAmount = $post['itemTotalAmount'] ?? 0;
  638. unset($post['itemTotalAmount']);
  639. if ($sendType == dict::getDict('sendType', 'express')) {
  640. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $itemTotalAmount);
  641. if ($needAdd) {
  642. $packCost = $ghsShopInfo->packCost ?? 0;
  643. }
  644. }
  645. $post['packCost'] = $packCost;
  646. }
  647. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  648. noticeUtil::push("包装费:" . $packCost, '15280215347');
  649. }
  650. // xhShMethod 门店:下单前按配置写入不满最低消费时的附加包装费/运费
  651. if (in_array($ghsShopId, [36523, 0])) {
  652. $shMethodBigNum = 0;
  653. foreach ($productList as $itemData) {
  654. $shMethodBigNum += floatval($itemData['bigNum'] ?? 0);
  655. }
  656. $post['packCost'] = ShMethodClass::resolvePurchasePackCost(
  657. $ghsShopId,
  658. $ghsMainId,
  659. $sendType,
  660. $shMethodItemAmount,
  661. $shMethodBigNum
  662. );
  663. }
  664. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  665. if (in_array($ghsShopId, [36523, 0])) {
  666. // 使用 xhShMethod 配置校验配送限制,替代旧版门店硬编码规则
  667. $shMethodError = ShMethodClass::validatePurchaseOrder(
  668. $ghsShopId,
  669. $ghsMainId,
  670. $sendType,
  671. $respond->actPrice ?? 0,
  672. $respond->bigNum ?? 0,
  673. $respond->packCost ?? 0,
  674. $post['wlName'] ?? ''
  675. );
  676. if ($shMethodError !== '') {
  677. $transaction->rollBack();
  678. util::fail($shMethodError);
  679. }
  680. } else {
  681. if ($sendType == 0) {
  682. if (getenv('YII_ENV') == 'production') {
  683. //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
  684. if ($ghsShopId == 4608) {
  685. if ($respond->actPrice < 500) {
  686. $transaction->rollBack();
  687. util::fail('满500元支持免费配送');
  688. }
  689. }
  690. //叶上花,满500元市区免费配送
  691. if ($ghsShopId == 4804) {
  692. if ($respond->actPrice < 500) {
  693. $transaction->rollBack();
  694. util::fail('满500元支持免费配送');
  695. }
  696. }
  697. //花悠星 国恋 勇记 万丽 陆丰暂时不支持免费配送
  698. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003 || $ghsShopId == 38128) {
  699. util::fail('暂不支持送货上门,请选其它配送方式!');
  700. }
  701. //广州鑫源花卉,选择送货上门必须选必选商品
  702. if ($ghsShopId == 12439) {
  703. $hasMust = array_intersect($ids, [3446945, 3446970, 3446973, 3446979, 3446995, 3447010, 3447023, 3459983, 4333184, 4317745, 1585738]);
  704. if (empty($hasMust)) {
  705. util::fail('请返回选择 必选商品');
  706. }
  707. }
  708. //三明易批花
  709. if ($ghsShopId == 82200) {
  710. if ($respond->actPrice < 100 || $respond->bigNum < 15) {
  711. $hasMust = array_intersect($ids, [4283478]);
  712. if (empty($hasMust)) {
  713. util::fail('没满100元且15扎,请返回选运费');
  714. }
  715. }
  716. // 按当天计时,把 sendTimeWant 要限制在两天的时间范围之内
  717. $future = date("Y-m-d", strtotime('+2 day'));
  718. if (strtotime($respond->sendTimeWant) > strtotime($future)) {
  719. util::fail('配送日期只能选最近二天');
  720. }
  721. }
  722. if ($ghsShopId == 77703) {
  723. //天天鲜花思明店,暂时关闭送货上门
  724. if ($respond->sendType == 0) {
  725. //util::fail('节日暂停送货上门,请选到店自取');
  726. }
  727. }
  728. if ($ghsShopId == 86726) {
  729. //藏花林
  730. if ($respond->transType != 4 && $respond->transType != 5) {
  731. util::fail('请选同城配送或到店自取');
  732. }
  733. }
  734. }
  735. if (isset($ghsInfo['home'])) {
  736. if ($ghsInfo['home'] == 0) {
  737. util::fail('暂不支持送货上门,请选其它配送方式');
  738. }
  739. $homeAmount = $ghsInfo['homeAmount'] ? floatval($ghsInfo['homeAmount']) : 0;
  740. $homeNum = $ghsInfo['homeNum'] ? floatval($ghsInfo['homeNum']) : 0;
  741. if ($homeAmount > 0 && $homeAmount > $respond->actPrice) {
  742. util::fail("满{$homeAmount}元 可送货上门");
  743. }
  744. if ($homeNum > 0 && $homeNum > $respond->bigNum) {
  745. util::fail("满{$homeNum}扎 可送货上门");
  746. }
  747. }
  748. }
  749. if ($sendType == 2) {
  750. //温州易批花,跑腿,不满100,要选10元的补运费商品
  751. if ($ghsShopId == 91115) {
  752. if ($respond->actPrice < 100) {
  753. $transaction->rollBack();
  754. $hasMust = array_intersect($ids, [4449812, 4449811]);
  755. if (empty($hasMust)) {
  756. util::fail('不满100,必选商品 补运费10元');
  757. }
  758. }
  759. }
  760. }
  761. if ($sendType == 4) {
  762. //温州易批花,快递,不满100,要选10元的补运费商品
  763. if ($ghsShopId == 91115) {
  764. if ($respond->actPrice < 100) {
  765. $transaction->rollBack();
  766. $hasMust = array_intersect($ids, [4449812, 4449811]);
  767. if (empty($hasMust)) {
  768. util::fail('不满100,必选商品 补运费10元');
  769. }
  770. }
  771. }
  772. }
  773. if ($sendType == 3) {
  774. //温州易批花,物流,不满200,要选10元的补运费商品
  775. if ($ghsShopId == 91115) {
  776. if ($respond->actPrice < 200) {
  777. $transaction->rollBack();
  778. $hasMust = array_intersect($ids, [4449812, 4449811]);
  779. if (empty($hasMust)) {
  780. util::fail('不满200,必选商品 补运费10元');
  781. }
  782. }
  783. }
  784. }
  785. }
  786. $transaction->commit();
  787. $transactionFinished = true;
  788. ProductClass::clearLimitBuyRollbackSnapshot();
  789. util::success($respond);
  790. } catch (\Exception $e) {
  791. if ($transaction->isActive) {
  792. $transaction->rollBack();
  793. }
  794. ProductClass::rollbackLimitBuySnapshot();
  795. $transactionFinished = true;
  796. Yii::error($e->getMessage());
  797. util::fail();
  798. }
  799. }
  800. /**
  801. * 赊账支付
  802. * 职责:处理采购单延期支付(记欠款/赊账),更新采购单状态,并触发后续的自动收货、通知、在线打印、跑腿呼叫等业务流
  803. * 入参:GET 传参 orderSn (采购单号)
  804. * 返回:JSON 格式的采购单属性
  805. * 副作用:更新采购单、订单状态,增减花材库存并记录库存流水,更新客户及供货商的最后下单时间,发送外部通知和打印请求
  806. * 关键边界:
  807. * 1. 校验采购单及客户的赊账权限
  808. * 2. 优化:复用采购单实例,避免重复查询
  809. * 3. 优化:将自动确认收货(confirmTake)包裹在独立事务中,合并写操作,减少磁盘 I/O
  810. * 4. 优化:合并打印逻辑中的 ShopExt 查询及订单保存,减少数据库交互
  811. * 5. 优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE
  812. */
  813. public function actionDebtPay()
  814. {
  815. $get = Yii::$app->request->get();
  816. $orderSn = $get['orderSn'] ?? 0;
  817. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  818. PurchaseService::valid($info, $this->shopId);
  819. //解决重复和并发提交
  820. $cacheKey = 'hd_debt_pay_' . $orderSn;
  821. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  822. if (!empty($has)) {
  823. util::fail('请稍等...');
  824. }
  825. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  826. if ($info->status == 5) {
  827. util::fail('订单已取消');
  828. }
  829. if ($info->status != 1) {
  830. util::fail('订单不是待付款状态');
  831. }
  832. if ($info->book == 1) {
  833. util::fail('预售不能记欠款');
  834. }
  835. $ghsId = $info->ghsId;
  836. $ghs = GhsClass::getGhsInfo($ghsId);
  837. $customId = $ghs['customId'] ?? 0;
  838. $custom = CustomClass::getCustom($customId);
  839. if (empty($custom)) {
  840. util::fail('没有找到客户');
  841. }
  842. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  843. util::fail('请先申请赊账权限');
  844. }
  845. if (getenv('YII_ENV') == 'production') {
  846. //源花汇不允许客户自己下欠款单
  847. if ($ghs['mainId'] == 10536) {
  848. util::fail('暂未开通');
  849. }
  850. }
  851. $connection = Yii::$app->db;
  852. $transaction = $connection->beginTransaction();
  853. try {
  854. //延期支付
  855. PurchaseService::debt($info);
  856. $transaction->commit();
  857. if (isset($info->localOrder) && $info->localOrder == 1) {
  858. if ($info->transType != 5 && $info->transType != 4) {
  859. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  860. $current = date("Y_m_d");
  861. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  862. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  863. }
  864. } else {
  865. if (isset($info->sendType) && $info->sendType == 4) {
  866. //标记当天已收一次包装费,岭南批发市场用的功能
  867. $current = date("Y_m_d");
  868. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  869. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  870. }
  871. }
  872. // 复杂分支/关键逻辑:不是预订单,且供货商配置为直接完成流程,则自动确认收货。
  873. // 优化:复用已有的 $info 实例,避免重复查询;同时将自动确认收货包裹在独立事务中,合并批量写操作,减少磁盘 I/O。
  874. if (!empty($info)) {
  875. if (isset($info->book) && $info->book == 0) {
  876. $ghsShopId = $ghs['shopId'] ?? 0;
  877. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  878. if (!empty($ext) && intval($ext->orderFlow ?? 0) === 0) {
  879. $takeTransaction = Yii::$app->db->beginTransaction();
  880. try {
  881. PurchaseClass::confirmTake($info);
  882. $takeTransaction->commit();
  883. } catch (\Exception $takeEx) {
  884. $takeTransaction->rollBack();
  885. Yii::error("自动确认收货失败: " . $takeEx->getMessage());
  886. throw $takeEx;
  887. }
  888. }
  889. }
  890. }
  891. //app新订单通知
  892. $saleId = $info->saleId ?? 0;
  893. $order = OrderClass::getById($saleId, true);
  894. if (!empty($order)) {
  895. $shopId = $order->shopId ?? 0;
  896. $shop = ShopClass::getById($shopId, true);
  897. if (!empty($shop)) {
  898. //通知供货商
  899. GhsNotifyClass::newOrderNotify($saleId);
  900. }
  901. //订单生成时唤起在线打印
  902. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  903. //有几个地方要同步去修改
  904. if (getenv('YII_ENV') == 'production') {
  905. //源花汇、盛丰不自动打小票,关键词mall_order_no_auto_print
  906. $mallOrderNoPrint = [10536, 44282, 26374];
  907. } else {
  908. $mallOrderNoPrint = [];
  909. }
  910. if (!in_array($order->mainId, $mallOrderNoPrint)) {
  911. // 复杂分支/关键逻辑:优化:只查询一次 $ext,合并 $order->save(),减少数据库交互
  912. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  913. $hasPrintSn = isset($ext->printSn) && !empty($ext->printSn);
  914. OrderClass::onlinePrint($order);
  915. if ($hasPrintSn) {
  916. $order->printNum += 1;
  917. }
  918. //花镜 打二次小票,有多处要修改搜索关键词tow_print
  919. if (in_array($order->mainId, [76796])) {
  920. OrderClass::onlinePrint($order);
  921. if ($hasPrintSn) {
  922. $order->printNum += 1;
  923. }
  924. }
  925. if ($hasPrintSn) {
  926. $order->save();
  927. }
  928. }
  929. }
  930. //付款成功之后呼叫跑腿,关键词 pay_after_call_pt,多处要同步修改
  931. GhsDeliveryOrderClass::payAfter($order);
  932. // 复杂分支/关键逻辑:优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE 的性能开销
  933. $customId = $order->customId ?? 0;
  934. $ghsId = $order->ghsId ?? 0;
  935. $date = date("Y-m-d H:i:s");
  936. if ($customId > 0) {
  937. CustomClass::updateByCondition(['id' => $customId], ['recentExpend' => $date]);
  938. }
  939. if ($ghsId > 0) {
  940. GhsClass::updateByCondition(['id' => $ghsId], ['recentExpend' => $date]);
  941. }
  942. }
  943. } catch (\Exception $e) {
  944. $transaction->rollBack();
  945. Yii::error("赊账支付失败原因:" . $e->getMessage());
  946. util::fail('支付失败');
  947. }
  948. util::success($info->attributes);
  949. }
  950. /**
  951. * 采购待支付页:客户 xhGhsCustom 净余额支付(并发重试 + 行锁)。
  952. */
  953. public function actionCustomBalancePay()
  954. {
  955. $post = Yii::$app->request->post();
  956. $orderSn = $post['orderSn'] ?? '';
  957. if (empty($orderSn)) {
  958. util::fail('订单号无效');
  959. }
  960. $cacheKey = 'hd_custom_balance_pay_' . $orderSn;
  961. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  962. if (!empty($has)) {
  963. util::fail('请稍等...');
  964. }
  965. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 8, 'has']);
  966. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  967. if (empty($info)) {
  968. util::fail('没有找到采购单');
  969. }
  970. PurchaseService::valid($info, $this->shopId);
  971. $deadline = $info->deadline ?? '';
  972. if (!empty($deadline) && time() > strtotime($deadline)) {
  973. util::fail('订单已失效了哦');
  974. }
  975. $ghsId = intval($info->ghsId ?? 0);
  976. $ghs = GhsClass::getGhsInfo($ghsId);
  977. try {
  978. $result = util::runWithDbConcurrencyRetry(function () use ($orderSn) {
  979. $connection = Yii::$app->db;
  980. $transaction = $connection->beginTransaction();
  981. try {
  982. $purchase = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  983. $respond = PurchaseService::payByGhsCustomBalance($purchase);
  984. $transaction->commit();
  985. return $respond;
  986. } catch (\Exception $exception) {
  987. if ($transaction->isActive) {
  988. $transaction->rollBack();
  989. }
  990. throw $exception;
  991. }
  992. });
  993. $info = $result['purchase'];
  994. $order = $result['saleOrder'] ?? null;
  995. if (!empty($order)) {
  996. if (isset($info->book) && $info->book == 0 && !empty($ghs)) {
  997. $ghsShopId = $ghs['shopId'] ?? 0;
  998. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  999. if (!empty($ext) && intval($ext->orderFlow ?? 0) === 0) {
  1000. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1001. if (!empty($cg)) {
  1002. PurchaseClass::confirmTake($cg);
  1003. }
  1004. }
  1005. }
  1006. $shopId = $order->shopId ?? 0;
  1007. $shop = ShopClass::getById($shopId, true);
  1008. if (!empty($shop)) {
  1009. GhsNotifyClass::newOrderNotify($order->id ?? 0);
  1010. }
  1011. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  1012. OrderClass::onlinePrint($order);
  1013. }
  1014. GhsDeliveryOrderClass::payAfter($order);
  1015. $customId = $order->customId ?? 0;
  1016. $custom = CustomClass::getById($customId, true);
  1017. $date = date('Y-m-d H:i:s');
  1018. if (!empty($custom)) {
  1019. $custom->recentExpend = $date;
  1020. $custom->save();
  1021. }
  1022. $orderGhsId = $order->ghsId ?? 0;
  1023. $orderGhs = GhsClass::getById($orderGhsId, true);
  1024. if (!empty($orderGhs)) {
  1025. $orderGhs->recentExpend = $date;
  1026. $orderGhs->save();
  1027. }
  1028. }
  1029. util::success($info->attributes);
  1030. } catch (\Exception $e) {
  1031. Yii::info('客户余额支付失败:' . $e->getMessage());
  1032. util::fail($e->getMessage() ?: '支付失败');
  1033. }
  1034. }
  1035. //余额支付 ssh 2021.1.24
  1036. public function actionBalancePay()
  1037. {
  1038. $shopAdmin = $this->shopAdmin;
  1039. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1040. util::fail('超管才能操作');
  1041. }
  1042. $get = Yii::$app->request->get();
  1043. $orderSn = $get['orderSn'] ?? 0;
  1044. $password = $get['password'] ?? '';
  1045. // 重新获取,不加密的用户数据
  1046. $admin = AdminClass::getById($this->adminId, true);
  1047. if (password_verify($password, $admin->payPassword) == false) {
  1048. util::fail('密码错误');
  1049. }
  1050. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1051. if (empty($info)) {
  1052. util::fail('没有找到采购单');
  1053. }
  1054. if ($info->status == 5) {
  1055. util::fail('订单已取消');
  1056. }
  1057. if ($info->status != 1) {
  1058. util::fail('订单不是待付款状态');
  1059. }
  1060. PurchaseService::valid($info, $this->shopId);
  1061. $connection = Yii::$app->db;
  1062. $transaction = $connection->beginTransaction();
  1063. try {
  1064. //余额支付
  1065. purchaseService::balancePay($info);
  1066. $transaction->commit();
  1067. } catch (\Exception $e) {
  1068. $transaction->rollBack();
  1069. util::fail('支付失败');
  1070. }
  1071. util::success($info->attributes);
  1072. }
  1073. //采购记录 ssh 2021.1.24
  1074. public function actionList()
  1075. {
  1076. $get = Yii::$app->request->get();
  1077. $where = ['shopId' => $this->shopId];
  1078. $searchTime = $get['searchTime'] ?? '';
  1079. if (!empty($searchTime)) {
  1080. $startTime = $get['startTime'] ?? '';
  1081. $endTime = $get['endTime'] ?? '';
  1082. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1083. $where['payTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1084. }
  1085. $ghsId = $get['ghsId'] ?? 0;
  1086. if (!empty($ghsId)) {
  1087. $where['ghsId'] = (int)$ghsId;
  1088. }
  1089. $status = $get['status'] ?? 0;
  1090. if (!empty($status)) {
  1091. $where['status'] = $status;
  1092. }
  1093. $respond = PurchaseService::getPurchaseList($where);
  1094. $respond['shop'] = $this->shop->attributes;
  1095. util::success($respond);
  1096. }
  1097. //获取合并采购单列表 ssh 20260519
  1098. public function actionMergeOrderList()
  1099. {
  1100. Yii::$app->params['pageSize'] = 9999;
  1101. $get = Yii::$app->request->get();
  1102. $mergeId = $get['mergeId'] ?? 0;
  1103. if (empty($mergeId)) {
  1104. util::fail('请选择采购单');
  1105. }
  1106. $mergeSn = CgMergeSnClass::getById($mergeId, true);
  1107. if (empty($mergeSn)) {
  1108. util::fail('请选择合并采购单');
  1109. }
  1110. $salt = $get['salt'] ?? '';
  1111. if (!empty($salt)) {
  1112. if (empty($mergeSn->salt) || $mergeSn->salt != $salt) {
  1113. util::fail('此单无法查看哦');
  1114. }
  1115. } else {
  1116. if (empty($this->shopId)) {
  1117. util::fail('暂时不能查看');
  1118. }
  1119. if ($mergeSn->mainId != $this->mainId) {
  1120. util::fail('此单无法查看哈');
  1121. }
  1122. }
  1123. $ghsId = $mergeSn->ghsId ?? 0;
  1124. $where = [
  1125. 'ghsId' => $ghsId,
  1126. 'mergeId' => $mergeId,
  1127. ];
  1128. $respond = PurchaseService::getPurchaseList($where);
  1129. $totalPrice = 0;
  1130. if (!empty($respond['list'])) {
  1131. foreach ($respond['list'] as $one) {
  1132. $actPrice = $one['actPrice'] ?? 0;
  1133. $totalPrice = bcadd($totalPrice, $actPrice, 2);
  1134. }
  1135. }
  1136. $totalPrice = floatval($totalPrice);
  1137. $respond['totalPrice'] = $totalPrice;
  1138. $respond['shop'] = !empty($this->shop) ? $this->shop->attributes : [];
  1139. util::success($respond);
  1140. }
  1141. //采购详情 ssh 2021.01.25
  1142. public function actionDetail()
  1143. {
  1144. $get = Yii::$app->request->get();
  1145. $id = $get['id'] ?? 0;
  1146. $info = PurchaseService::getInfo($id, true, true);
  1147. if (empty($info)) {
  1148. util::fail('没有找到采购单');
  1149. }
  1150. $salt = $get['salt'] ?? '';
  1151. if (!empty($salt)) {
  1152. if (!isset($info['salt']) || $info['salt'] != $salt) {
  1153. util::fail('无效采购单呢!');
  1154. }
  1155. } else {
  1156. if (!isset($info['mainId']) || $info['mainId'] != $this->mainId) {
  1157. //util::fail('采购单不能访问');
  1158. }
  1159. }
  1160. $customId = $info['customId'] ?? 0;
  1161. $custom = CustomClass::getById($customId);
  1162. $customName = $custom['name'] ?? '';
  1163. $customMobile = $custom['mobile'] ?? '';
  1164. $info['customName'] = $customName;
  1165. $info['customMobile'] = $customMobile;
  1166. $hasDebtPay = $custom['debt'] ?? 0;
  1167. $book = $info['book'] ?? 0;
  1168. $presell = $info['presell'] ?? 0;
  1169. //预售不能记欠款
  1170. if ($book == 1) {
  1171. $hasDebtPay = 0;
  1172. }
  1173. if ($presell == 1) {
  1174. $hasDebtPay = 0;
  1175. }
  1176. $shop = $this->shop;
  1177. $info['pfShopId'] = $shop->pfShopId ?? 0;
  1178. $info['hasDebtPay'] = $hasDebtPay;
  1179. // 待支付页展示 xhGhsCustom 净余额(正数=可支付余额),不再使用 main.balance
  1180. $info['balance'] = 0;
  1181. $info['customNetBalance'] = '0.00';
  1182. $info['showBalancePay'] = 0;
  1183. $info['ghsSalt'] = '';
  1184. $ghsId = intval($info['ghsId'] ?? 0);
  1185. if ($ghsId > 0) {
  1186. $ghsRow = GhsClass::getById($ghsId, true);
  1187. if (!empty($ghsRow)) {
  1188. $info['ghsSalt'] = $ghsRow->salt ?? '';
  1189. $ghsCustomId = intval($ghsRow->customId ?? 0);
  1190. if ($ghsCustomId > 0) {
  1191. $ghsCustom = CustomClass::getById($ghsCustomId, true);
  1192. if (!empty($ghsCustom)) {
  1193. $net = AccountMoneyClass::getNetBalanceFromRow($ghsCustom);
  1194. $info['customNetBalance'] = $net;
  1195. if (bccomp($net, '0', 2) > 0) {
  1196. $info['balance'] = floatval($net);
  1197. $info['showBalancePay'] = 1;
  1198. }
  1199. }
  1200. }
  1201. }
  1202. }
  1203. $getPayType = dict::getDict('getPayType');
  1204. $info['getPayType'] = $getPayType;
  1205. //获取售后条件
  1206. $ghsId = $info['ghsId'] ?? 0;
  1207. $ghsInfo = GhsClass::getById($ghsId, true);
  1208. if (empty($ghsInfo)) {
  1209. util::fail('没有找到供货商');
  1210. }
  1211. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  1212. //重要,客户看到的电话修改成门店的联系电话
  1213. $ghsShopId = $ghsInfo->shopId ?? 0;
  1214. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  1215. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  1216. $info['ghsMobile2'] = $ghsShopInfo->telephone2 ?? '';
  1217. $info['hasRenew'] = 1;
  1218. //如果是快捷开单,则不需要显示累计待结,因为商家经常把快捷开单的订单发给客户结账
  1219. $showForPay = 1;
  1220. if ($ghsShopInfo->skCustomId == $info['customId']) {
  1221. $showForPay = 0;
  1222. }
  1223. $info['showForPay'] = $showForPay;
  1224. util::success($info);
  1225. }
  1226. //可用的支付方式 ssh 2021.1.25
  1227. public function actionPayWay()
  1228. {
  1229. $button = [
  1230. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  1231. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  1232. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  1233. ];
  1234. util::success(['button' => $button]);
  1235. }
  1236. //待结款明细 ssh 2021.1.26
  1237. public function actionDebtList()
  1238. {
  1239. $get = Yii::$app->request->get();
  1240. $id = $get['id'] ?? 0;
  1241. $salt = $get['salt'] ?? '';
  1242. $info = GhsClass::getGhsInfo($id);
  1243. if (!empty($salt)) {
  1244. if (!isset($info['salt']) || $info['salt'] != $salt) {
  1245. util::fail('供货商出错了');
  1246. }
  1247. } else {
  1248. if (!isset($info['ownShopId']) || $info['ownShopId'] != $this->shopId) {
  1249. util::fail('错误的供货商.');
  1250. }
  1251. }
  1252. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  1253. $searchTime = $get['searchTime'] ?? '';
  1254. if (!empty($searchTime)) {
  1255. $startTime = $get['startTime'] ?? '';
  1256. $endTime = $get['endTime'] ?? '';
  1257. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1258. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1259. }
  1260. $respond = PurchaseService::getDebtList($where);
  1261. util::success($respond);
  1262. }
  1263. //支付宝支付 ssh 2021.4.11
  1264. public function actionAliPay()
  1265. {
  1266. ini_set('date.timezone', 'Asia/Shanghai');
  1267. $post = Yii::$app->request->post();
  1268. //默认是支付宝支付
  1269. $payWay = $post['payWay'] ?? 1;
  1270. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1271. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1272. if (empty($order)) {
  1273. util::fail('订单号无效');
  1274. }
  1275. if ($order->status == 5) {
  1276. util::fail('订单已取消');
  1277. }
  1278. if ($order->status != 1) {
  1279. util::fail('订单不是待付款状态');
  1280. }
  1281. $deadline = $order->deadline;
  1282. $current = time();
  1283. if (($current + 50) > strtotime($deadline)) {
  1284. util::fail('订单已失效,请重新下单');
  1285. }
  1286. $ghsId = $order->ghsId ?? 0;
  1287. $ghs = GhsClass::getById($ghsId, true);
  1288. if (empty($ghs)) {
  1289. util::fail('没有供货商信息');
  1290. }
  1291. $ghsShopId = $ghs->shopId ?? 0;
  1292. $ghsShop = ShopClass::getById($ghsShopId, true);
  1293. if (empty($ghsShop)) {
  1294. util::fail('没有供货商门店信息');
  1295. }
  1296. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1297. util::fail('商家支付功能未开通');
  1298. }
  1299. //避免重复提交
  1300. util::checkRepeatCommit($orderSn, 8);
  1301. //已经唤起过支付了,根据拉卡拉的规则,需要生成新的订单号
  1302. if ($order->changePrice == 2) {
  1303. $oldOrderSn = $orderSn;
  1304. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1305. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1306. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1307. $params = [
  1308. 'appid' => 'OP00002119',
  1309. 'serial_no' => '018b08cfddbd',
  1310. 'merchant_no' => $ghsShop->lklSjNo,
  1311. 'term_no' => $ghsShop->lklScanTermNo,
  1312. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1313. 'lklCertificatePath' => $lklCertificatePath,
  1314. ];
  1315. $laResource = new Lakala($params);
  1316. $closeParams = [
  1317. 'orderSn' => $oldOrderSn,
  1318. ];
  1319. $response = $laResource->cashClose($closeParams);
  1320. if (isset($response['code']) == false || $response['code'] != '000000') {
  1321. $msg = $response['msg'] ?? '';
  1322. $code = $response['code'] ?? 0;
  1323. if (!in_array($code, ['000091'])) {
  1324. noticeUtil::push('注意花店买花更换单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  1325. }
  1326. }
  1327. $shopId = $order->shopId ?? 0;
  1328. $mainId = $order->mainId ?? 0;
  1329. $customId = $order->customId ?? 0;
  1330. $ghsId = $order->ghsId ?? 0;
  1331. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1332. $newOrderSn = orderSn::getPurchaseSn($snData);
  1333. $orderSn = $newOrderSn;
  1334. $order->orderSn = $newOrderSn;
  1335. $order->save();
  1336. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1337. }
  1338. $order->changePrice = 2;
  1339. $order->save();
  1340. $ghsShopName = $ghsShop->shopName ?? '';
  1341. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1342. $name = $ghsShopName . " 花材 " . $orderSn;
  1343. $totalFee = $order->realPrice;
  1344. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1345. $ghsId = $order->ghsId ?? 0;
  1346. $ghs = GhsClass::getById($ghsId, true);
  1347. if (empty($ghs)) {
  1348. util::fail('没有供货商信息');
  1349. }
  1350. $ghsShopId = $ghs->shopId ?? 0;
  1351. $ghsShop = ShopClass::getById($ghsShopId, true);
  1352. if (empty($ghsShop)) {
  1353. util::fail('没有供货商门店信息');
  1354. }
  1355. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1356. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1357. $params = [
  1358. 'appid' => 'OP00002119',
  1359. 'serial_no' => '018b08cfddbd',
  1360. 'merchant_no' => $ghsShop->lklSjNo,
  1361. 'term_no' => $ghsShop->lklB2BTermNo,
  1362. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1363. 'lklCertificatePath' => $lklCertificatePath,
  1364. ];
  1365. $laResource = new Lakala($params);
  1366. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  1367. $wxParams = [
  1368. 'orderSn' => $orderSn,
  1369. 'amount' => $totalFee,
  1370. 'capitalType' => $purchaseCapital,
  1371. 'notifyUrl' => $notifyUrl,
  1372. 'subject' => $name,
  1373. 'payWay' => $payWay,
  1374. ];
  1375. $response = $laResource->cashierPay($wxParams);
  1376. if (isset($response['code']) == false || $response['code'] != '000000') {
  1377. $errMsg = $response['msg'] ?? '';
  1378. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  1379. util::fail('发起支付失败');
  1380. }
  1381. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  1382. $hasRenew = $ghsShop->hasRenew ?? 0;
  1383. $new = [
  1384. 'payUrl' => $counter_url,
  1385. 'orderSn' => $orderSn,
  1386. 'hasRenew' => 1,
  1387. ];
  1388. util::success($new);
  1389. }
  1390. //微信支付 ssh 2021.4.11
  1391. public function actionWxPay()
  1392. {
  1393. ini_set('date.timezone', 'Asia/Shanghai');
  1394. $post = Yii::$app->request->post();
  1395. $couponId = $post['couponId'] ?? 0;
  1396. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1397. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1398. if (empty($order)) {
  1399. util::fail('订单号无效');
  1400. }
  1401. if ($order->status == 5) {
  1402. util::fail('订单已取消');
  1403. }
  1404. if ($order->status != 1) {
  1405. util::fail('订单不是待付款状态');
  1406. }
  1407. $deadline = $order->deadline;
  1408. $current = time();
  1409. if (($current + 50) > strtotime($deadline)) {
  1410. util::fail('订单已失效,请重新下单');
  1411. }
  1412. //避免重复提交
  1413. util::checkRepeatCommit($orderSn, 8);
  1414. $ghsId = $order->ghsId ?? 0;
  1415. $ghs = GhsClass::getById($ghsId, true);
  1416. if (empty($ghs)) {
  1417. util::fail('没有供货商信息');
  1418. }
  1419. $ghsShopId = $ghs->shopId ?? 0;
  1420. $ghsShop = ShopClass::getById($ghsShopId, true);
  1421. if (empty($ghsShop)) {
  1422. util::fail('没有供货商门店信息');
  1423. }
  1424. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1425. util::fail('商家支付功能未开通');
  1426. }
  1427. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  1428. if ($order->changePrice == 2) {
  1429. $oldOrderSn = $orderSn;
  1430. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1431. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1432. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1433. $params = [
  1434. 'appid' => 'OP00002119',
  1435. 'serial_no' => '018b08cfddbd',
  1436. 'merchant_no' => $ghsShop->lklSjNo,
  1437. 'term_no' => $ghsShop->lklScanTermNo,
  1438. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1439. 'lklCertificatePath' => $lklCertificatePath,
  1440. ];
  1441. $laResource = new Lakala($params);
  1442. $closeParams = [
  1443. 'orderSn' => $oldOrderSn,
  1444. ];
  1445. $response = $laResource->close($closeParams);
  1446. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  1447. $msg = $response['msg'] ?? '';
  1448. $code = $response['code'] ?? 0;
  1449. if (!in_array($code, ['BBS11114'])) {
  1450. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . $code, '15280215347');
  1451. util::fail('出错了,请重新下单');
  1452. }
  1453. }
  1454. $shopId = $order->shopId ?? 0;
  1455. $mainId = $order->mainId ?? 0;
  1456. $customId = $order->customId ?? 0;
  1457. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1458. $newOrderSn = orderSn::getPurchaseSn($snData);
  1459. $orderSn = $newOrderSn;
  1460. $order->orderSn = $newOrderSn;
  1461. $order->save();
  1462. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1463. }
  1464. $id = $order->id;
  1465. $order->changePrice = 2;
  1466. $order->save();
  1467. $ghsShopName = $ghsShop->shopName ?? '';
  1468. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1469. $name = $ghsShopName . " 花材 " . $orderSn;
  1470. $totalFee = $order->realPrice;
  1471. $openId = '';
  1472. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  1473. $openId = $post['currentMiniOpenId'];
  1474. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  1475. }
  1476. if (empty($openId)) {
  1477. if (isset($this->admin) && !empty($this->admin)) {
  1478. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  1479. $openId = $this->admin->miniOpenId;
  1480. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  1481. }
  1482. }
  1483. }
  1484. if (empty($openId)) {
  1485. $currentShopId = $order->shopId ?? 0;
  1486. $currentShop = ShopClass::getById($currentShopId, true);
  1487. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  1488. $currentMobile = $currentShop->mobile ?? '';
  1489. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  1490. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  1491. }
  1492. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1493. //花卉宝代为申请的微信支付
  1494. $merchantExtend = WxOpenClass::getWxInfo();
  1495. if (empty($ghsShop->lklSjNo)) {
  1496. noticeUtil::push('编号31505:单号:' . $orderSn . ' 商家没有开通收款功能:' . $ghsShopName, '15280215347');
  1497. util::fail('商家未开通支付功能');
  1498. }
  1499. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1500. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1501. $params = [
  1502. 'appid' => 'OP00002119',
  1503. 'serial_no' => '018b08cfddbd',
  1504. 'merchant_no' => $ghsShop->lklSjNo,
  1505. 'term_no' => $ghsShop->lklScanTermNo,
  1506. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1507. 'lklCertificatePath' => $lklCertificatePath,
  1508. ];
  1509. $laResource = new Lakala($params);
  1510. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  1511. $wxParams = [
  1512. 'orderSn' => $orderSn,
  1513. 'amount' => $totalFee,
  1514. 'capitalType' => $purchaseCapital,
  1515. 'notifyUrl' => $notifyUrl,
  1516. 'wxAppId' => $merchantExtend['miniAppId'],
  1517. 'openId' => $openId,
  1518. 'subject' => $name,
  1519. 'couponId' => $couponId,
  1520. ];
  1521. $response = $laResource->driveWxPay($wxParams);
  1522. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1523. $errMsg = $response['msg'] ?? '';
  1524. noticeUtil::push('编号129680931:' . $errMsg . ' 单号:' . $orderSn . ' ' . $ghsShopName, '15280215347');
  1525. util::fail('支付失败');
  1526. }
  1527. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1528. $appId = $newParams['app_id'] ?? '';
  1529. $nonceStr = $newParams['nonce_str'] ?? '';
  1530. $paySign = $newParams['pay_sign'] ?? '';
  1531. $package = $newParams['package'] ?? '';
  1532. $signType = $newParams['sign_type'] ?? '';
  1533. $timeStamp = $newParams['time_stamp'] ?? '';
  1534. $new = [
  1535. 'id' => $id,
  1536. 'appId' => $appId,
  1537. 'nonceStr' => $nonceStr,
  1538. 'paySign' => $paySign,
  1539. 'package' => $package,
  1540. 'signType' => $signType,
  1541. 'timeStamp' => $timeStamp,
  1542. 'orderSn' => $orderSn,
  1543. ];
  1544. util::success($new);
  1545. }
  1546. //计算运费 ssh 20221003
  1547. public function actionFreight()
  1548. {
  1549. $get = Yii::$app->request->get();
  1550. $shop = $this->shop;
  1551. $ghsId = $get['ghsId'] ?? 0;
  1552. $ghs = GhsClass::getById($ghsId, true);
  1553. if (empty($ghs)) {
  1554. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1555. }
  1556. $ghsShopId = $ghs->shopId ?? 0;
  1557. $ghsShop = ShopClass::getById($ghsShopId, true);
  1558. if (empty($ghsShop)) {
  1559. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1560. }
  1561. $customId = $ghs->customId ?? 0;
  1562. $custom = CustomClass::getById($customId, true);
  1563. if (empty($custom)) {
  1564. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1565. }
  1566. $weight = $get['weight'] ?? 0;
  1567. if (empty($weight)) {
  1568. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1569. }
  1570. $respond = PurchaseClass::getDistanceFee($shop, $ghsShop, $weight, $custom);
  1571. util::success($respond);
  1572. }
  1573. //运费计算 lqh 2021.4.12 暂时返回固定值
  1574. public function actionFreight2()
  1575. {
  1576. //lqh 2021.6.25 运费固定返回0
  1577. util::success(['sedCost' => 0]);
  1578. }
  1579. }