PurchaseController.php 58 KB

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