PurchaseController.php 72 KB

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