PurchaseController.php 59 KB

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