PurchaseController.php 80 KB

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