PurchaseController.php 44 KB

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