PurchaseController.php 60 KB

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