PurchaseController.php 49 KB

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