PurchaseController.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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 (isset($ghsInfo['home'])) {
  583. if ($ghsInfo['home'] == 0) {
  584. util::fail('暂不支持送货上门,请选其它配送方式');
  585. }
  586. if (!empty($ghsInfo['homeAmount'])) {
  587. $homeType = $ghsInfo['homeType'] ?? 0;
  588. $homeAmount = floatval($ghsInfo['homeAmount']);
  589. if ($homeType == 0) {
  590. if ($homeAmount > $respond->actPrice) {
  591. util::fail("满{$homeAmount}元 可送货上门");
  592. }
  593. }
  594. if ($homeType == 1) {
  595. if ($homeAmount > $respond->bigNum) {
  596. util::fail("满{$homeAmount}扎 可送货上门");
  597. }
  598. }
  599. }
  600. }
  601. }
  602. if ($sendType == 2) {
  603. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,thirdSend');
  604. if ($ext->thirdSend == 1) {
  605. util::fail('本店暂不能使用跑腿哦');
  606. }
  607. }
  608. if (getenv('YII_ENV') == 'production') {
  609. //昆明花落谁家
  610. if ($ghsShopId == 13398) {
  611. if ($respond->bigNum < 15) {
  612. $transaction->rollBack();
  613. util::fail('不足15扎,请联系店长');
  614. }
  615. }
  616. //盛丰最少要20扎
  617. if ($ghsShopId == 44556) {
  618. if ($respond->bigNum < 20) {
  619. $transaction->rollBack();
  620. util::fail('最少要20扎');
  621. }
  622. }
  623. }
  624. $transaction->commit();
  625. util::success($respond);
  626. } catch (Exception $e) {
  627. $transaction->rollBack();
  628. util::fail();
  629. }
  630. }
  631. //赊账 ssh 2021.1.24
  632. public function actionDebtPay()
  633. {
  634. $get = Yii::$app->request->get();
  635. $orderSn = $get['orderSn'] ?? 0;
  636. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  637. PurchaseService::valid($info, $this->shopId);
  638. //解决重复和并发提交
  639. $cacheKey = 'hd_debt_pay_' . $orderSn;
  640. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  641. if (!empty($has)) {
  642. util::fail('请稍等...');
  643. }
  644. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  645. if ($info->status == 5) {
  646. util::fail('订单已取消');
  647. }
  648. if ($info->status != 1) {
  649. util::fail('订单不是待付款状态');
  650. }
  651. if ($info->book == 1) {
  652. util::fail('预售不能记欠款');
  653. }
  654. $ghsId = $info->ghsId;
  655. $ghs = GhsClass::getGhsInfo($ghsId);
  656. $customId = $ghs['customId'] ?? 0;
  657. $custom = CustomClass::getCustom($customId);
  658. if (empty($custom)) {
  659. util::fail('没有找到客户');
  660. }
  661. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  662. util::fail('请先申请赊账权限');
  663. }
  664. if (getenv('YII_ENV') == 'production') {
  665. //源花汇不允许客户自己下欠款单
  666. if ($ghs['mainId'] == 10536) {
  667. util::fail('暂未开通');
  668. }
  669. }
  670. $connection = Yii::$app->db;
  671. $transaction = $connection->beginTransaction();
  672. try {
  673. //延期支付
  674. PurchaseService::debt($info);
  675. $transaction->commit();
  676. if (isset($info->localOrder) && $info->localOrder == 1) {
  677. if ($info->transType != 5 && $info->transType != 4) {
  678. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  679. $current = date("Y_m_d");
  680. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  681. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  682. }
  683. } else {
  684. if (isset($info->sendType) && $info->sendType == 4) {
  685. //标记当天已收一次包装费,岭南批发市场用的功能
  686. $current = date("Y_m_d");
  687. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  688. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  689. }
  690. }
  691. //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  692. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  693. if (!empty($cg)) {
  694. if (isset($cg->book) && $cg->book == 0) {
  695. $ghsShopId = $ghs['shopId'] ?? 0;
  696. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  697. if ($ext->orderFlow == 0) {
  698. PurchaseClass::confirmTake($cg);
  699. }
  700. }
  701. }
  702. //app新订单通知
  703. $saleId = $info->saleId ?? 0;
  704. $order = OrderClass::getById($saleId, true);
  705. if (!empty($order)) {
  706. $shopId = $order->shopId ?? 0;
  707. $shop = ShopClass::getById($shopId, true);
  708. if (!empty($shop)) {
  709. //微信通知供货商
  710. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  711. $noticeText = json_encode(['orderId' => $saleId]);
  712. $noticeKey = "hdCgNoticeGhs";
  713. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  714. //向供货商APP发通知
  715. $allDevices = \bizGhs\device\classes\GhsDeviceClass::getAllByCondition(['shopId'=>$shopId]);
  716. $cids = array_column($allDevices, 'clientId');
  717. $title = '你有新的订单';
  718. $content = $shop->shopName . ' ¥' . $order->actPrice;
  719. $payload = [
  720. "page" => "pagesOrder/detail",
  721. "params" => ["id" => $order->id]
  722. ];
  723. $push = new push('ghs', push::MSG_TYPE_ORDER);
  724. $push->pushByCloud($cids, $title, $content, $payload);
  725. }
  726. //订单生成时唤起在线打印
  727. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  728. //有几个地方要同步去修改
  729. if (getenv('YII_ENV') == 'production') {
  730. //源花汇、盛丰不自动打小票,关键词mall_order_no_auto_print
  731. $mallOrderNoPrint = [10536, 44282, 26374];
  732. } else {
  733. $mallOrderNoPrint = [];
  734. }
  735. if (in_array($order->mainId, $mallOrderNoPrint)) {
  736. } else {
  737. OrderClass::onlinePrint($order);
  738. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  739. if (isset($ext->printSn) && !empty($ext->printSn)) {
  740. $order->printNum += 1;
  741. $order->save();
  742. }
  743. //xxx 打二次小票,有多处要修改搜索关键词tow_print
  744. if (in_array($order->mainId, [0])) {
  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. }
  752. }
  753. }
  754. //更新最后下单时间
  755. $customId = $order->customId ?? 0;
  756. $custom = CustomClass::getById($customId, true);
  757. $date = date("Y-m-d H:i:s");
  758. if (!empty($custom)) {
  759. $custom->recentExpend = $date;
  760. $custom->save();
  761. }
  762. $ghsId = $order->ghsId ?? 0;
  763. $ghs = GhsClass::getById($ghsId, true);
  764. if (!empty($ghs)) {
  765. $ghs->recentExpend = $date;
  766. $ghs->save();
  767. }
  768. }
  769. } catch (Exception $e) {
  770. $transaction->rollBack();
  771. util::fail('支付失败');
  772. }
  773. util::success($info->attributes);
  774. }
  775. //余额支付 ssh 2021.1.24
  776. public function actionBalancePay()
  777. {
  778. $shopAdmin = $this->shopAdmin;
  779. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  780. util::fail('超管才能操作');
  781. }
  782. $get = Yii::$app->request->get();
  783. $orderSn = $get['orderSn'] ?? 0;
  784. $password = $get['password'] ?? '';
  785. // 重新获取,不加密的用户数据
  786. $admin = AdminClass::getById($this->adminId, true);
  787. if (password_verify($password, $admin->payPassword) == false) {
  788. util::fail('密码错误');
  789. }
  790. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  791. if (empty($info)) {
  792. util::fail('没有找到采购单');
  793. }
  794. if ($info->status == 5) {
  795. util::fail('订单已取消');
  796. }
  797. if ($info->status != 1) {
  798. util::fail('订单不是待付款状态');
  799. }
  800. PurchaseService::valid($info, $this->shopId);
  801. $connection = Yii::$app->db;
  802. $transaction = $connection->beginTransaction();
  803. try {
  804. //余额支付
  805. purchaseService::balancePay($info);
  806. $transaction->commit();
  807. } catch (Exception $e) {
  808. $transaction->rollBack();
  809. util::fail('支付失败');
  810. }
  811. util::success($info->attributes);
  812. }
  813. //采购记录 ssh 2021.1.24
  814. public function actionList()
  815. {
  816. $get = Yii::$app->request->get();
  817. $where = ['shopId' => $this->shopId];
  818. $searchTime = $get['searchTime'] ?? '';
  819. if (!empty($searchTime)) {
  820. $startTime = $get['startTime'] ?? '';
  821. $endTime = $get['endTime'] ?? '';
  822. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  823. $where['payTime'] = ['between', [$period['startTime'], $period['endTime']]];
  824. }
  825. $ghsId = $get['ghsId'] ?? 0;
  826. if (!empty($ghsId)) {
  827. $where['ghsId'] = $ghsId;
  828. }
  829. $status = $get['status'] ?? 0;
  830. if (!empty($status)) {
  831. $where['status'] = $status;
  832. }
  833. $respond = PurchaseService::getPurchaseList($where);
  834. $respond['shop'] = $this->shop->attributes;
  835. util::success($respond);
  836. }
  837. //采购详情 ssh 2021.01.25
  838. public function actionDetail()
  839. {
  840. $get = Yii::$app->request->get();
  841. $id = $get['id'] ?? 0;
  842. $info = PurchaseService::getInfo($id, true, true);
  843. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  844. //不能注释,分享付款功能要用,客户没有登录也能付款
  845. //util::fail('无法访问');
  846. }
  847. $customId = $info['customId'] ?? 0;
  848. $custom = CustomClass::getById($customId);
  849. $customName = $custom['name'] ?? '';
  850. $customMobile = $custom['mobile'] ?? '';
  851. $info['customName'] = $customName;
  852. $info['customMobile'] = $customMobile;
  853. $hasDebtPay = $custom['debt'] ?? 0;
  854. $book = $info['book'] ?? 0;
  855. $presell = $info['presell'] ?? 0;
  856. //预售不能记欠款
  857. if ($book == 1) {
  858. $hasDebtPay = 0;
  859. }
  860. if ($presell == 1) {
  861. $hasDebtPay = 0;
  862. }
  863. $shop = $this->shop;
  864. $info['pfShopId'] = $shop->pfShopId ?? 0;
  865. $info['hasDebtPay'] = $hasDebtPay;
  866. $info['balance'] = $shop->balance ?? 0;
  867. $getPayType = dict::getDict('getPayType');
  868. $info['getPayType'] = $getPayType;
  869. //获取售后条件
  870. $ghsId = $info['ghsId'] ?? 0;
  871. $ghsInfo = GhsClass::getById($ghsId, true);
  872. if (empty($ghsInfo)) {
  873. util::fail('没有找到供货商');
  874. }
  875. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  876. //重要,客户看到的电话修改成门店的联系电话
  877. $ghsShopId = $ghsInfo->shopId ?? 0;
  878. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  879. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  880. $info['ghsMobile2'] = $ghsShopInfo->telephone2 ?? '';
  881. $info['hasRenew'] = 1;
  882. util::success($info);
  883. }
  884. //可用的支付方式 ssh 2021.1.25
  885. public function actionPayWay()
  886. {
  887. $button = [
  888. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  889. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  890. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  891. ];
  892. util::success(['button' => $button]);
  893. }
  894. //待结款明细 ssh 2021.1.26
  895. public function actionDebtList()
  896. {
  897. $get = Yii::$app->request->get();
  898. $id = $get['id'] ?? 0;
  899. $info = GhsClass::getGhsInfo($id);
  900. GhsClass::valid($info, $this->shopId);
  901. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  902. $searchTime = $get['searchTime'] ?? '';
  903. if (!empty($searchTime)) {
  904. $startTime = $get['startTime'] ?? '';
  905. $endTime = $get['endTime'] ?? '';
  906. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  907. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  908. }
  909. $respond = PurchaseService::getDebtList($where);
  910. util::success($respond);
  911. }
  912. //支付宝支付 ssh 2021.4.11
  913. public function actionAliPay()
  914. {
  915. ini_set('date.timezone', 'Asia/Shanghai');
  916. $post = Yii::$app->request->post();
  917. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  918. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  919. if (empty($order)) {
  920. util::fail('订单号无效');
  921. }
  922. if ($order->status == 5) {
  923. util::fail('订单已取消');
  924. }
  925. if ($order->status != 1) {
  926. util::fail('订单不是待付款状态');
  927. }
  928. $deadline = $order->deadline;
  929. $current = time();
  930. if (($current + 50) > strtotime($deadline)) {
  931. util::fail('订单已失效,请重新下单');
  932. }
  933. $ghsId = $order->ghsId ?? 0;
  934. $ghs = GhsClass::getById($ghsId, true);
  935. if (empty($ghs)) {
  936. util::fail('没有供货商信息');
  937. }
  938. $ghsShopId = $ghs->shopId ?? 0;
  939. $ghsShop = ShopClass::getById($ghsShopId, true);
  940. if (empty($ghsShop)) {
  941. util::fail('没有供货商门店信息');
  942. }
  943. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  944. util::fail('商家支付功能未开通');
  945. }
  946. //避免重复提交
  947. util::checkRepeatCommit($orderSn, 8);
  948. //已经唤起过支付了,根据拉卡拉的规则,需要生成新的订单号
  949. if ($order->changePrice == 2) {
  950. $oldOrderSn = $orderSn;
  951. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  952. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  953. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  954. $params = [
  955. 'appid' => 'OP00002119',
  956. 'serial_no' => '018b08cfddbd',
  957. 'merchant_no' => $ghsShop->lklSjNo,
  958. 'term_no' => $ghsShop->lklScanTermNo,
  959. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  960. 'lklCertificatePath' => $lklCertificatePath,
  961. ];
  962. $laResource = new Lakala($params);
  963. $closeParams = [
  964. 'orderSn' => $oldOrderSn,
  965. ];
  966. $response = $laResource->cashClose($closeParams);
  967. if (isset($response['code']) == false || $response['code'] != '000000') {
  968. $msg = $response['msg'] ?? '';
  969. $code = $response['code'] ?? 0;
  970. if (!in_array($code, ['000091'])) {
  971. noticeUtil::push('注意花店买花更换单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  972. }
  973. }
  974. $shopId = $order->shopId ?? 0;
  975. $mainId = $order->mainId ?? 0;
  976. $customId = $order->customId ?? 0;
  977. $ghsId = $order->ghsId ?? 0;
  978. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  979. $newOrderSn = orderSn::getPurchaseSn($snData);
  980. $orderSn = $newOrderSn;
  981. $order->orderSn = $newOrderSn;
  982. $order->save();
  983. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  984. }
  985. $order->changePrice = 2;
  986. $order->save();
  987. $ghsShopName = $ghsShop->shopName ?? '';
  988. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  989. $name = $ghsShopName . " 花材 " . $orderSn;
  990. $totalFee = $order->realPrice;
  991. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  992. $ghsId = $order->ghsId ?? 0;
  993. $ghs = GhsClass::getById($ghsId, true);
  994. if (empty($ghs)) {
  995. util::fail('没有供货商信息');
  996. }
  997. $ghsShopId = $ghs->shopId ?? 0;
  998. $ghsShop = ShopClass::getById($ghsShopId, true);
  999. if (empty($ghsShop)) {
  1000. util::fail('没有供货商门店信息');
  1001. }
  1002. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1003. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1004. $params = [
  1005. 'appid' => 'OP00002119',
  1006. 'serial_no' => '018b08cfddbd',
  1007. 'merchant_no' => $ghsShop->lklSjNo,
  1008. 'term_no' => $ghsShop->lklB2BTermNo,
  1009. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1010. 'lklCertificatePath' => $lklCertificatePath,
  1011. ];
  1012. $laResource = new Lakala($params);
  1013. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  1014. $wxParams = [
  1015. 'orderSn' => $orderSn,
  1016. 'amount' => $totalFee,
  1017. 'capitalType' => $purchaseCapital,
  1018. 'notifyUrl' => $notifyUrl,
  1019. 'subject' => $name,
  1020. ];
  1021. $response = $laResource->cashierPay($wxParams);
  1022. if (isset($response['code']) == false || $response['code'] != '000000') {
  1023. $errMsg = $response['msg'] ?? '';
  1024. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  1025. util::fail('发起支付失败');
  1026. }
  1027. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  1028. $hasRenew = $ghsShop->hasRenew ?? 0;
  1029. $new = [
  1030. 'payUrl' => $counter_url,
  1031. 'orderSn' => $orderSn,
  1032. 'hasRenew' => 1,
  1033. ];
  1034. util::success($new);
  1035. }
  1036. //微信支付 ssh 2021.4.11
  1037. public function actionWxPay()
  1038. {
  1039. ini_set('date.timezone', 'Asia/Shanghai');
  1040. $post = Yii::$app->request->post();
  1041. $couponId = $post['couponId'] ?? 0;
  1042. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1043. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1044. if (empty($order)) {
  1045. util::fail('订单号无效');
  1046. }
  1047. if ($order->status == 5) {
  1048. util::fail('订单已取消');
  1049. }
  1050. if ($order->status != 1) {
  1051. util::fail('订单不是待付款状态');
  1052. }
  1053. $deadline = $order->deadline;
  1054. $current = time();
  1055. if (($current + 50) > strtotime($deadline)) {
  1056. util::fail('订单已失效,请重新下单');
  1057. }
  1058. //避免重复提交
  1059. util::checkRepeatCommit($orderSn, 8);
  1060. $ghsId = $order->ghsId ?? 0;
  1061. $ghs = GhsClass::getById($ghsId, true);
  1062. if (empty($ghs)) {
  1063. util::fail('没有供货商信息');
  1064. }
  1065. $ghsShopId = $ghs->shopId ?? 0;
  1066. $ghsShop = ShopClass::getById($ghsShopId, true);
  1067. if (empty($ghsShop)) {
  1068. util::fail('没有供货商门店信息');
  1069. }
  1070. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1071. util::fail('商家支付功能未开通');
  1072. }
  1073. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  1074. if ($order->changePrice == 2) {
  1075. $oldOrderSn = $orderSn;
  1076. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1077. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1078. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1079. $params = [
  1080. 'appid' => 'OP00002119',
  1081. 'serial_no' => '018b08cfddbd',
  1082. 'merchant_no' => $ghsShop->lklSjNo,
  1083. 'term_no' => $ghsShop->lklScanTermNo,
  1084. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1085. 'lklCertificatePath' => $lklCertificatePath,
  1086. ];
  1087. $laResource = new Lakala($params);
  1088. $closeParams = [
  1089. 'orderSn' => $oldOrderSn,
  1090. ];
  1091. $response = $laResource->close($closeParams);
  1092. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  1093. $msg = $response['msg'] ?? '';
  1094. $code = $response['code'] ?? 0;
  1095. if (!in_array($code, ['BBS11114'])) {
  1096. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . $code, '15280215347');
  1097. util::fail('出错了,请重新下单');
  1098. }
  1099. }
  1100. $shopId = $order->shopId ?? 0;
  1101. $mainId = $order->mainId ?? 0;
  1102. $customId = $order->customId ?? 0;
  1103. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1104. $newOrderSn = orderSn::getPurchaseSn($snData);
  1105. $orderSn = $newOrderSn;
  1106. $order->orderSn = $newOrderSn;
  1107. $order->save();
  1108. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1109. }
  1110. $id = $order->id;
  1111. $order->changePrice = 2;
  1112. $order->save();
  1113. $ghsShopName = $ghsShop->shopName ?? '';
  1114. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1115. $name = $ghsShopName . " 花材 " . $orderSn;
  1116. $totalFee = $order->realPrice;
  1117. $openId = '';
  1118. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  1119. $openId = $post['currentMiniOpenId'];
  1120. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  1121. }
  1122. if (empty($openId)) {
  1123. if (isset($this->admin) && !empty($this->admin)) {
  1124. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  1125. $openId = $this->admin->miniOpenId;
  1126. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  1127. }
  1128. }
  1129. }
  1130. if (empty($openId)) {
  1131. $currentShopId = $order->shopId ?? 0;
  1132. $currentShop = ShopClass::getById($currentShopId, true);
  1133. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  1134. $currentMobile = $currentShop->mobile ?? '';
  1135. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  1136. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  1137. }
  1138. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1139. //花卉宝代为申请的微信支付
  1140. $merchantExtend = WxOpenClass::getWxInfo();
  1141. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1142. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1143. $params = [
  1144. 'appid' => 'OP00002119',
  1145. 'serial_no' => '018b08cfddbd',
  1146. 'merchant_no' => $ghsShop->lklSjNo,
  1147. 'term_no' => $ghsShop->lklScanTermNo,
  1148. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1149. 'lklCertificatePath' => $lklCertificatePath,
  1150. ];
  1151. $laResource = new Lakala($params);
  1152. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  1153. $wxParams = [
  1154. 'orderSn' => $orderSn,
  1155. 'amount' => $totalFee,
  1156. 'capitalType' => $purchaseCapital,
  1157. 'notifyUrl' => $notifyUrl,
  1158. 'wxAppId' => $merchantExtend['miniAppId'],
  1159. 'openId' => $openId,
  1160. 'subject' => $name,
  1161. 'couponId' => $couponId,
  1162. ];
  1163. $response = $laResource->driveWxPay($wxParams);
  1164. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  1165. $errMsg = $response['msg'] ?? '';
  1166. noticeUtil::push('编号129680931:' . $errMsg, '15280215347');
  1167. util::fail('支付失败');
  1168. }
  1169. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1170. $appId = $newParams['app_id'] ?? '';
  1171. $nonceStr = $newParams['nonce_str'] ?? '';
  1172. $paySign = $newParams['pay_sign'] ?? '';
  1173. $package = $newParams['package'] ?? '';
  1174. $signType = $newParams['sign_type'] ?? '';
  1175. $timeStamp = $newParams['time_stamp'] ?? '';
  1176. $new = [
  1177. 'id' => $id,
  1178. 'appId' => $appId,
  1179. 'nonceStr' => $nonceStr,
  1180. 'paySign' => $paySign,
  1181. 'package' => $package,
  1182. 'signType' => $signType,
  1183. 'timeStamp' => $timeStamp,
  1184. 'orderSn' => $orderSn,
  1185. ];
  1186. util::success($new);
  1187. }
  1188. //计算运费 ssh 20221003
  1189. public function actionFreight()
  1190. {
  1191. $get = Yii::$app->request->get();
  1192. $shop = $this->shop;
  1193. $ghsId = $get['ghsId'] ?? 0;
  1194. $ghs = GhsClass::getById($ghsId, true);
  1195. if (empty($ghs)) {
  1196. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1197. }
  1198. $ghsShopId = $ghs->shopId ?? 0;
  1199. $ghsShop = ShopClass::getById($ghsShopId, true);
  1200. if (empty($ghsShop)) {
  1201. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1202. }
  1203. $customId = $ghs->customId ?? 0;
  1204. $custom = CustomClass::getById($customId, true);
  1205. if (empty($custom)) {
  1206. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1207. }
  1208. $weight = $get['weight'] ?? 0;
  1209. if (empty($weight)) {
  1210. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1211. }
  1212. $respond = PurchaseClass::getDistanceFee($shop, $ghsShop, $weight, $custom);
  1213. util::success($respond);
  1214. }
  1215. //运费计算 lqh 2021.4.12 暂时返回固定值
  1216. public function actionFreight2()
  1217. {
  1218. //lqh 2021.6.25 运费固定返回0
  1219. util::success(['sedCost' => 0]);
  1220. }
  1221. }