PurchaseController.php 60 KB

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