PurchaseController.php 58 KB

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