PurchaseController.php 42 KB

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