PurchaseController.php 56 KB

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