PurchaseController.php 55 KB

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