PurchaseController.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  412. if ($sendType == 0) {
  413. if (getenv('YII_ENV') == 'production') {
  414. //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
  415. if ($ghsShopId == 4608) {
  416. if ($respond->actPrice < 500) {
  417. $transaction->rollBack();
  418. util::fail('满500元支持免费配送');
  419. }
  420. }
  421. //叶上花,满500元市区免费配送
  422. if ($ghsShopId == 4804) {
  423. if ($respond->actPrice < 500) {
  424. $transaction->rollBack();
  425. util::fail('满500元支持免费配送');
  426. }
  427. }
  428. //花悠星 国恋 勇记 易宝暂时不支持免费配送
  429. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003) {
  430. util::fail('暂不支持送货上门,请选其它方式');
  431. }
  432. }
  433. }
  434. if ($sendType == 2) {
  435. if (in_array($ghsShopId, [520, 1489, 13628, 3, 7855, 7687])) {
  436. util::fail('暂时不能使用跑腿');
  437. }
  438. }
  439. if (getenv('YII_ENV') == 'production') {
  440. //昆明花落谁家
  441. if ($ghsShopId == 13398) {
  442. if ($respond->bigNum < 15) {
  443. $transaction->rollBack();
  444. util::fail('不足15扎,请联系店长');
  445. }
  446. }
  447. }
  448. $transaction->commit();
  449. util::success($respond);
  450. } catch (Exception $e) {
  451. $transaction->rollBack();
  452. util::fail();
  453. }
  454. }
  455. //赊账 ssh 2021.1.24
  456. public function actionDebtPay()
  457. {
  458. $get = Yii::$app->request->get();
  459. $orderSn = $get['orderSn'] ?? 0;
  460. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  461. PurchaseService::valid($info, $this->shopId);
  462. //解决重复和并发提交
  463. $cacheKey = 'hd_debt_pay_' . $orderSn;
  464. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  465. if (!empty($has)) {
  466. util::fail('请稍等...');
  467. }
  468. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  469. if ($info->status == 5) {
  470. util::fail('订单已取消');
  471. }
  472. if ($info->status != 1) {
  473. util::fail('订单不是待付款状态');
  474. }
  475. if ($info->book == 1) {
  476. util::fail('预售不能记欠款');
  477. }
  478. $ghsId = $info->ghsId;
  479. $ghs = GhsClass::getGhsInfo($ghsId);
  480. $customId = $ghs['customId'] ?? 0;
  481. $custom = CustomClass::getCustom($customId);
  482. if (empty($custom)) {
  483. util::fail('没有找到客户');
  484. }
  485. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  486. util::fail('请先申请赊账权限');
  487. }
  488. $connection = Yii::$app->db;//事务处理
  489. $transaction = $connection->beginTransaction();
  490. try {
  491. //延期支付
  492. PurchaseService::debt($info);
  493. $transaction->commit();
  494. if (isset($info->localOrder) && $info->localOrder == 1) {
  495. if ($info->transType != 5 && $info->transType != 4) {
  496. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  497. $current = date("Y_m_d");
  498. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  499. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  500. }
  501. } else {
  502. if (isset($info->sendType) && $info->sendType == 4) {
  503. //标记当天已收一次包装费,岭南批发市场用的功能
  504. $current = date("Y_m_d");
  505. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  506. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  507. }
  508. }
  509. //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  510. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  511. if (!empty($cg)) {
  512. if (isset($cg->book) && $cg->book == 0) {
  513. $ghsShopId = $ghs['shopId'] ?? 0;
  514. if (getenv('YII_ENV') == 'production') {
  515. $specialList = [10649, 8042, 16454, 21319];
  516. } else {
  517. $specialList = [36523];
  518. }
  519. if (!in_array($ghsShopId, $specialList)) {
  520. $cgShopId = $cg->shopId ?? 0;
  521. $cgShop = ShopClass::getById($cgShopId, true);
  522. if (!empty($cgShop)) {
  523. if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
  524. PurchaseClass::confirmTake($cg);
  525. }
  526. }
  527. }
  528. }
  529. }
  530. //app新订单通知
  531. $saleId = $info->saleId ?? 0;
  532. $order = OrderClass::getById($saleId, true);
  533. if (!empty($order)) {
  534. $shopId = $order->shopId ?? 0;
  535. $shop = ShopClass::getById($shopId, true);
  536. if (!empty($shop)) {
  537. //微信通知供货商
  538. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  539. $noticeText = json_encode(['orderId' => $saleId]);
  540. $noticeKey = "hdCgNoticeGhs";
  541. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  542. }
  543. //订单生成时唤起在线打印
  544. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  545. //有几个地方要同步去修改
  546. if (getenv('YII_ENV') == 'production') {
  547. //源花汇不自动打开小票
  548. $mallOrderNoPrint = [10536];
  549. } else {
  550. $mallOrderNoPrint = [];
  551. }
  552. if (in_array($order->mainId, $mallOrderNoPrint)) {
  553. } else {
  554. OrderClass::onlinePrint($order);
  555. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  556. if (isset($ext->printSn) && !empty($ext->printSn)) {
  557. $order->printNum += 1;
  558. $order->save();
  559. }
  560. }
  561. }
  562. }
  563. } catch (Exception $e) {
  564. $transaction->rollBack();
  565. util::fail('支付失败');
  566. }
  567. util::success($info->attributes);
  568. }
  569. //余额支付 ssh 2021.1.24
  570. public function actionBalancePay()
  571. {
  572. $shopAdmin = $this->shopAdmin;
  573. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  574. util::fail('超管才能操作');
  575. }
  576. $get = Yii::$app->request->get();
  577. $orderSn = $get['orderSn'] ?? 0;
  578. $password = $get['password'] ?? '';
  579. // 重新获取,不加密的用户数据
  580. $admin = AdminClass::getById($this->adminId, true);
  581. if (password_verify($password, $admin->payPassword) == false) {
  582. util::fail('密码错误');
  583. }
  584. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  585. if (empty($info)) {
  586. util::fail('没有找到采购单');
  587. }
  588. if ($info->status == 5) {
  589. util::fail('订单已取消');
  590. }
  591. if ($info->status != 1) {
  592. util::fail('订单不是待付款状态');
  593. }
  594. PurchaseService::valid($info, $this->shopId);
  595. $connection = Yii::$app->db;//事务处理
  596. $transaction = $connection->beginTransaction();
  597. try {
  598. //余额支付
  599. purchaseService::balancePay($info);
  600. $transaction->commit();
  601. } catch (Exception $e) {
  602. $transaction->rollBack();
  603. util::fail('支付失败');
  604. }
  605. util::success($info->attributes);
  606. }
  607. //采购记录 ssh 2021.1.24
  608. public function actionList()
  609. {
  610. $get = Yii::$app->request->get();
  611. $where = ['shopId' => $this->shopId];
  612. $status = $get['status'] ?? 0;
  613. if (!empty($status)) {
  614. $where['status'] = $status;
  615. }
  616. $respond = PurchaseService::getPurchaseList($where);
  617. $respond['shop'] = $this->shop->attributes;
  618. util::success($respond);
  619. }
  620. //采购详情 ssh 2021.01.25
  621. public function actionDetail()
  622. {
  623. $get = Yii::$app->request->get();
  624. $id = $get['id'] ?? 0;
  625. $info = PurchaseService::getInfo($id, true, true);
  626. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  627. //不能注释,分享付款功能要用,客户没有登录也能付款
  628. //util::fail('无法访问');
  629. }
  630. $customId = $info['customId'] ?? 0;
  631. $custom = CustomClass::getById($customId);
  632. $customName = $custom['name'] ?? '';
  633. $customMobile = $custom['mobile'] ?? '';
  634. $info['customName'] = $customName;
  635. $info['customMobile'] = $customMobile;
  636. $hasDebtPay = $custom['debt'] ?? 0;
  637. $book = $info['book'] ?? 0;
  638. $presell = $info['presell'] ?? 0;
  639. //预售不能记欠款
  640. if ($book == 1) {
  641. $hasDebtPay = 0;
  642. }
  643. if ($presell == 1) {
  644. $hasDebtPay = 0;
  645. }
  646. $shop = $this->shop;
  647. $info['pfShopId'] = $shop->pfShopId ?? 0;
  648. $info['hasDebtPay'] = $hasDebtPay;
  649. $info['balance'] = $shop->balance ?? 0;
  650. $getPayType = dict::getDict('getPayType');
  651. $info['getPayType'] = $getPayType;
  652. //获取售后条件
  653. $ghsId = $info['ghsId'] ?? 0;
  654. $ghsInfo = GhsClass::getById($ghsId, true);
  655. if (empty($ghsInfo)) {
  656. util::fail('没有找到供货商');
  657. }
  658. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  659. //重要,客户看到的电话修改成门店的联系电话
  660. $ghsShopId = $ghsInfo->shopId ?? 0;
  661. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  662. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  663. util::success($info);
  664. }
  665. //可用的支付方式 ssh 2021.1.25
  666. public function actionPayWay()
  667. {
  668. $button = [
  669. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  670. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  671. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  672. ];
  673. util::success(['button' => $button]);
  674. }
  675. //待结款明细 ssh 2021.1.26
  676. public function actionDebtList()
  677. {
  678. $get = Yii::$app->request->get();
  679. $id = $get['id'] ?? 0;
  680. $info = GhsClass::getGhsInfo($id);
  681. GhsClass::valid($info, $this->shopId);
  682. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  683. $searchTime = $get['searchTime'] ?? '';
  684. if (!empty($searchTime)) {
  685. $startTime = $get['startTime'] ?? '';
  686. $endTime = $get['endTime'] ?? '';
  687. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  688. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  689. }
  690. $respond = PurchaseService::getDebtList($where);
  691. util::success($respond);
  692. }
  693. //微信支付 ssh 2021.4.11
  694. public function actionAliPay()
  695. {
  696. ini_set('date.timezone', 'Asia/Shanghai');
  697. $post = Yii::$app->request->post();
  698. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  699. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  700. if (empty($order)) {
  701. util::fail('订单号无效');
  702. }
  703. if ($order->status == 5) {
  704. util::fail('订单已取消');
  705. }
  706. if ($order->status != 1) {
  707. util::fail('订单不是待付款状态');
  708. }
  709. $deadline = $order->deadline;
  710. $current = time();
  711. if (($current + 50) > strtotime($deadline)) {
  712. util::fail('订单已失效,请重新下单');
  713. }
  714. $ghsId = $order->ghsId ?? 0;
  715. $ghs = GhsClass::getById($ghsId, true);
  716. if (empty($ghs)) {
  717. util::fail('没有供货商信息');
  718. }
  719. $ghsShopId = $ghs->shopId ?? 0;
  720. $ghsShop = ShopClass::getById($ghsShopId, true);
  721. if (empty($ghsShop)) {
  722. util::fail('没有供货商门店信息');
  723. }
  724. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  725. util::fail('商家支付功能未开通');
  726. }
  727. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  728. if ($order->changePrice == 2) {
  729. $oldOrderSn = $orderSn;
  730. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  731. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  732. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  733. $params = [
  734. 'appid' => 'OP00002119',
  735. 'serial_no' => '018b08cfddbd',
  736. 'merchant_no' => $ghsShop->lklSjNo,
  737. 'term_no' => $ghsShop->lklScanTermNo,
  738. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  739. 'lklCertificatePath' => $lklCertificatePath,
  740. ];
  741. $laResource = new Lakala($params);
  742. $closeParams = [
  743. 'orderSn' => $oldOrderSn,
  744. ];
  745. $response = $laResource->cashClose($closeParams);
  746. if (isset($response['code']) == false || $response['code'] != '000000') {
  747. $msg = $response['msg'] ?? '';
  748. noticeUtil::push('注意花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  749. }
  750. $newOrderSn = orderSn::getPurchaseSn();
  751. $newGhsOrderSn = orderSn::getGhsOrderSn();
  752. $orderSn = $newOrderSn;
  753. $order->orderSn = $newOrderSn;
  754. $order->save();
  755. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  756. $saleId = $order->saleId ?? 0;
  757. $sale = OrderClass::getById($saleId, true);
  758. if (!empty($sale)) {
  759. $oldSaleOrderSn = $sale->orderSn ?? '';
  760. $sale->orderSn = $newGhsOrderSn;
  761. $sale->save();
  762. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  763. }
  764. }
  765. $order->changePrice = 2;
  766. $order->save();
  767. $name = $orderSn;
  768. $totalFee = $order->realPrice;
  769. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  770. $ghsId = $order->ghsId ?? 0;
  771. $ghs = GhsClass::getById($ghsId, true);
  772. if (empty($ghs)) {
  773. util::fail('没有供货商信息');
  774. }
  775. $ghsShopId = $ghs->shopId ?? 0;
  776. $ghsShop = ShopClass::getById($ghsShopId, true);
  777. if (empty($ghsShop)) {
  778. util::fail('没有供货商门店信息');
  779. }
  780. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  781. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  782. $params = [
  783. 'appid' => 'OP00002119',
  784. 'serial_no' => '018b08cfddbd',
  785. 'merchant_no' => $ghsShop->lklSjNo,
  786. 'term_no' => $ghsShop->lklB2BTermNo,
  787. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  788. 'lklCertificatePath' => $lklCertificatePath,
  789. ];
  790. $laResource = new Lakala($params);
  791. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  792. $wxParams = [
  793. 'orderSn' => $orderSn,
  794. 'amount' => $totalFee,
  795. 'capitalType' => $purchaseCapital,
  796. 'notifyUrl' => $notifyUrl,
  797. 'subject' => $name,
  798. ];
  799. $response = $laResource->cashierPay($wxParams);
  800. if (isset($response['code']) == false || $response['code'] != '000000') {
  801. $errMsg = $response['msg'] ?? '';
  802. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  803. util::fail('发起支付失败');
  804. }
  805. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  806. $new = [
  807. 'payUrl' => $counter_url,
  808. 'orderSn' => $orderSn,
  809. ];
  810. util::success($new);
  811. }
  812. //微信支付 ssh 2021.4.11
  813. public function actionWxPay()
  814. {
  815. ini_set('date.timezone', 'Asia/Shanghai');
  816. $post = Yii::$app->request->post();
  817. $couponId = $post['couponId'] ?? 0;
  818. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  819. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  820. if (empty($order)) {
  821. util::fail('订单号无效');
  822. }
  823. if ($order->status == 5) {
  824. util::fail('订单已取消');
  825. }
  826. if ($order->status != 1) {
  827. util::fail('订单不是待付款状态');
  828. }
  829. $deadline = $order->deadline;
  830. $current = time();
  831. if (($current + 50) > strtotime($deadline)) {
  832. util::fail('订单已失效,请重新下单');
  833. }
  834. $ghsId = $order->ghsId ?? 0;
  835. $ghs = GhsClass::getById($ghsId, true);
  836. if (empty($ghs)) {
  837. util::fail('没有供货商信息');
  838. }
  839. $ghsShopId = $ghs->shopId ?? 0;
  840. $ghsShop = ShopClass::getById($ghsShopId, true);
  841. if (empty($ghsShop)) {
  842. util::fail('没有供货商门店信息');
  843. }
  844. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  845. util::fail('商家支付功能未开通');
  846. }
  847. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  848. if ($order->changePrice == 2) {
  849. $oldOrderSn = $orderSn;
  850. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  851. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  852. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  853. $params = [
  854. 'appid' => 'OP00002119',
  855. 'serial_no' => '018b08cfddbd',
  856. 'merchant_no' => $ghsShop->lklSjNo,
  857. 'term_no' => $ghsShop->lklScanTermNo,
  858. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  859. 'lklCertificatePath' => $lklCertificatePath,
  860. ];
  861. $laResource = new Lakala($params);
  862. $closeParams = [
  863. 'orderSn' => $oldOrderSn,
  864. ];
  865. $response = $laResource->close($closeParams);
  866. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  867. $msg = $response['msg'] ?? '';
  868. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg, '15280215347');
  869. util::fail('出错了,请重新下一单');
  870. }
  871. $newOrderSn = orderSn::getPurchaseSn();
  872. $newGhsOrderSn = orderSn::getGhsOrderSn();
  873. $orderSn = $newOrderSn;
  874. $order->orderSn = $newOrderSn;
  875. $order->save();
  876. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  877. $saleId = $order->saleId ?? 0;
  878. $sale = OrderClass::getById($saleId, true);
  879. if (!empty($sale)) {
  880. $oldSaleOrderSn = $sale->orderSn ?? '';
  881. $sale->orderSn = $newGhsOrderSn;
  882. $sale->save();
  883. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  884. }
  885. }
  886. $id = $order->id;
  887. $order->changePrice = 2;
  888. $order->save();
  889. $name = $orderSn;
  890. $totalFee = $order->realPrice;
  891. $openId = '';
  892. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  893. $openId = $post['currentMiniOpenId'];
  894. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  895. }
  896. if (empty($openId)) {
  897. if (isset($this->admin) && !empty($this->admin)) {
  898. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  899. $openId = $this->admin->miniOpenId;
  900. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  901. }
  902. }
  903. }
  904. if (empty($openId)) {
  905. $currentShopId = $order->shopId ?? 0;
  906. $currentShop = ShopClass::getById($currentShopId, true);
  907. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  908. $currentMobile = $currentShop->mobile ?? '';
  909. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  910. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  911. }
  912. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  913. //花卉宝代为申请的微信支付
  914. $merchantExtend = WxOpenClass::getWxInfo();
  915. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  916. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  917. $params = [
  918. 'appid' => 'OP00002119',
  919. 'serial_no' => '018b08cfddbd',
  920. 'merchant_no' => $ghsShop->lklSjNo,
  921. 'term_no' => $ghsShop->lklScanTermNo,
  922. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  923. 'lklCertificatePath' => $lklCertificatePath,
  924. ];
  925. $laResource = new Lakala($params);
  926. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  927. $wxParams = [
  928. 'orderSn' => $orderSn,
  929. 'amount' => $totalFee,
  930. 'capitalType' => $purchaseCapital,
  931. 'notifyUrl' => $notifyUrl,
  932. 'wxAppId' => $merchantExtend['miniAppId'],
  933. 'openId' => $openId,
  934. 'subject' => $name,
  935. 'couponId' => $couponId,
  936. ];
  937. $response = $laResource->driveWxPay($wxParams);
  938. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  939. $errMsg = $response['msg'] ?? '';
  940. noticeUtil::push('编号99891:' . $errMsg, '15280215347');
  941. util::fail('支付失败');
  942. }
  943. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  944. $appId = $newParams['app_id'] ?? '';
  945. $nonceStr = $newParams['nonce_str'] ?? '';
  946. $paySign = $newParams['pay_sign'] ?? '';
  947. $package = $newParams['package'] ?? '';
  948. $signType = $newParams['sign_type'] ?? '';
  949. $timeStamp = $newParams['time_stamp'] ?? '';
  950. $new = [
  951. 'id' => $id,
  952. 'appId' => $appId,
  953. 'nonceStr' => $nonceStr,
  954. 'paySign' => $paySign,
  955. 'package' => $package,
  956. 'signType' => $signType,
  957. 'timeStamp' => $timeStamp,
  958. 'orderSn' => $orderSn,
  959. ];
  960. util::success($new);
  961. }
  962. //计算运费 ssh 20221003
  963. public function actionFreight()
  964. {
  965. $get = Yii::$app->request->get();
  966. $shop = $this->shop;
  967. $shopLat = isset($shop->lat) ? $shop->lat : '';
  968. $shopLong = isset($shop->long) ? $shop->long : '';
  969. if (empty($shopLat) || empty($shopLong)) {
  970. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  971. }
  972. $ghsId = $get['ghsId'] ?? 0;
  973. $ghs = GhsClass::getById($ghsId, true);
  974. if (empty($ghs)) {
  975. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  976. }
  977. $ghsShopId = $ghs->shopId ?? 0;
  978. $ghsShop = ShopClass::getById($ghsShopId, true);
  979. if (empty($ghsShop)) {
  980. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  981. }
  982. $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
  983. $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
  984. if (empty($ghsShopLat) || empty($ghsShopLong)) {
  985. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  986. }
  987. $weight = $get['weight'] ?? 0;
  988. if (empty($weight)) {
  989. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  990. }
  991. $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight);
  992. util::success($respond);
  993. }
  994. //运费计算 lqh 2021.4.12 暂时返回固定值
  995. public function actionFreight2()
  996. {
  997. //lqh 2021.6.25 运费固定返回0
  998. util::success(['sedCost' => 0]);
  999. $post = Yii::$app->request->post();
  1000. $ghsId = $post['ghsId'] ?? 0;
  1001. //供货商
  1002. $ghsInfo = GhsClass::getById($ghsId);
  1003. if (empty($ghsInfo)) {
  1004. util::fail('没有找到供货商');
  1005. }
  1006. //花材信息
  1007. $productJson = $post['product'] ?? '';
  1008. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  1009. if (empty($productJson)) {
  1010. util::fail('请选择花材');
  1011. }
  1012. $productList = json_decode($productJson, true);
  1013. if (empty($productList)) {
  1014. util::fail('请选择花材');
  1015. }
  1016. $productList = PurchaseClass::mergeItemInfo($productList);
  1017. $weight = 0;
  1018. $price = 0;
  1019. $productData = ProductClass::getProductMapData($productList);
  1020. $bigNum = 0;
  1021. foreach ($productList as $key => $val) {
  1022. $productId = $val['productId'];
  1023. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  1024. $bigNum += $val['bigNum'];
  1025. $weight = bcadd($w, $weight, 2);
  1026. $ratio = $productData[$productId]['ratio'] ?? 0;
  1027. //大小数量合并多少扎
  1028. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  1029. //售卖价格
  1030. $itemPrice = $productData[$productId]['price'] ?? 0;
  1031. //合计价格
  1032. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  1033. }
  1034. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  1035. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  1036. $sendTime = $post['sendTime'] ?? '';
  1037. if (!empty($sendTime)) {
  1038. //配送时间不为空
  1039. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  1040. $sendTimeInt = strtotime($sendTime);
  1041. $now = time();
  1042. $diff = $sendTimeInt - $now;
  1043. //因服务器写入时间,改为至少提前6分钟
  1044. if ($diff <= 360) {
  1045. util::fail('配送时间至少提前6分钟');
  1046. }
  1047. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  1048. } else {
  1049. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  1050. }
  1051. //找供货商的客户信息,用里面的配送相关地址信息
  1052. $customId = $ghsInfo['customId'] ?? 0;
  1053. $customInfo = CustomClass::getById($customId);
  1054. if (empty($customInfo)) {
  1055. util::fail('没有找到客户');
  1056. }
  1057. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  1058. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  1059. if (!$shopNo) {
  1060. //使用自定义运费
  1061. //util::fail('供货商暂未开通配送');
  1062. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  1063. util::success(['sedCost' => $cost]);
  1064. }
  1065. //组装订单信息
  1066. $info = [];
  1067. //发送放的商家信息,即供货商的相关信息
  1068. $info['sjId'] = $ghsSjId;
  1069. $info['shopId'] = $ghsShopId;
  1070. $info['actPrice'] = $price;//价格
  1071. $info['weight'] = $weight;
  1072. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  1073. $info['bigNum'] = $bigNum;
  1074. //配送地址,当前零售端的门店地址
  1075. $info['customName'] = $customInfo['name'] ?? '';
  1076. $info['customMobile'] = $customInfo['mobile'] ?? '';
  1077. $info['province'] = $customInfo['province'] ?? '';
  1078. $info['city'] = $customInfo['city'] ?? '';;
  1079. $info['address'] = $customInfo['address'] ?? '';;
  1080. $info['floor'] = $customInfo['floor'] ?? '';;
  1081. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  1082. $info['lat'] = $customInfo['lat'] ?? '';;
  1083. $info['long'] = $customInfo['long'] ?? '';;
  1084. $info['sendTime'] = $sendTime;
  1085. $res = DadaExpressServices::queryDeliverFee($info);
  1086. util::success(['sedCost' => $res['fee']]);
  1087. }
  1088. }