PurchaseController.php 35 KB

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