PurchaseController.php 55 KB

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