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