PurchaseController.php 44 KB

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