PurchaseController.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. //如果已经算过一次打包费和运费了,则不再费了
  299. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  300. if ($needAdd == false) {
  301. $packCost = 0;
  302. }
  303. $post['packCost'] = $packCost;
  304. $transCost = dict::getDict('transCost');
  305. $sendCost = 0;
  306. if (!empty($transCost)) {
  307. foreach ($transCost as $trans) {
  308. $sign = $trans['sign'] ?? 0;
  309. if ($sign == $transType) {
  310. if ($sign == 2) {
  311. //冷链物流
  312. $llOption = $trans['option'] ?? [];
  313. if (!empty($llOption)) {
  314. $largeInfo = end($llOption);
  315. $largeNum = $largeInfo['num'] ?? 0;
  316. $largeAmount = $largeInfo['amount'] ?? 0;
  317. $bs = intval($totalItemNum / $largeNum);
  318. if ($bs > 0) {
  319. $addSendCost = bcmul($bs, $largeAmount, 2);
  320. $sendCost = bcadd($sendCost, $addSendCost, 2);
  321. $subNum = bcmul($bs, $largeNum, 2);
  322. $subNum = floatval($subNum);
  323. $totalItemNum = bcsub($totalItemNum, $subNum, 2);
  324. $totalItemNum = floatval($totalItemNum);
  325. }
  326. foreach ($llOption as $miniItem) {
  327. if ($miniItem['num'] >= $totalItemNum) {
  328. $sendCost = bcadd($sendCost, $miniItem['amount'], 2);
  329. break;
  330. }
  331. }
  332. }
  333. } else {
  334. //其它物流
  335. $perKiloCost = $trans['perKiloCost'] ?? 0;
  336. $sendCost = bcmul($perKiloCost, $totalWeight, 3);
  337. $sendCost = round($sendCost, 2);
  338. }
  339. }
  340. }
  341. }
  342. //如果已经算过一次打包费和运费了,则不再费了
  343. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  344. if ($needAdd == false) {
  345. $sendCost = 0;
  346. }
  347. $post['sendCost'] = $sendCost;
  348. //标记为昆明到地方订单
  349. $post['localOrder'] = 1;
  350. } else {
  351. //同城发货包装费和运费的计算
  352. $sendCost = 0;
  353. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  354. $weight = 0;
  355. foreach ($productList as $itemData) {
  356. $bigNum = $itemData['bigNum'] ?? 0;
  357. $thisWeight = $itemData['weight'] ?? 0;
  358. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  359. $weight = bcadd($currentWeight, $weight, 2);
  360. }
  361. $result = PurchaseClass::getDistanceFee($this->shop, $ghsShopInfo, $weight);
  362. $sendCost = $result['fee'] ?? 0;
  363. }
  364. $post['sendCost'] = $sendCost;
  365. //出车、发快递、发物流可以算包装费
  366. $packCost = 0;
  367. $itemTotalAmount = $post['itemTotalAmount'] ?? 0;
  368. unset($post['itemTotalAmount']);
  369. if ($sendType == dict::getDict('sendType', 'express')) {
  370. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $itemTotalAmount);
  371. if ($needAdd) {
  372. $packCost = $ghsShopInfo->packCost ?? 0;
  373. }
  374. }
  375. $post['packCost'] = $packCost;
  376. }
  377. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  378. if ($sendType == 0) {
  379. if (getenv('YII_ENV') == 'production') {
  380. //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
  381. if ($ghsShopId == 4608) {
  382. if ($respond->actPrice < 500) {
  383. $transaction->rollBack();
  384. util::fail('满500元支持免费配送');
  385. }
  386. }
  387. //叶上花,满500元市区免费配送
  388. if ($ghsShopId == 4804) {
  389. if ($respond->actPrice < 500) {
  390. $transaction->rollBack();
  391. util::fail('满500元支持免费配送');
  392. }
  393. }
  394. //花悠星 国恋 勇记暂时不支持免费配送
  395. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778) {
  396. util::fail('暂不支持送货上门,请选其它方式');
  397. }
  398. }
  399. }
  400. $transaction->commit();
  401. util::success($respond);
  402. } catch (Exception $e) {
  403. $transaction->rollBack();
  404. util::fail();
  405. }
  406. }
  407. //赊账 ssh 2021.1.24
  408. public function actionDebtPay()
  409. {
  410. $get = Yii::$app->request->get();
  411. $orderSn = $get['orderSn'] ?? 0;
  412. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  413. PurchaseService::valid($info, $this->shopId);
  414. if ($info->status == 5) {
  415. util::fail('订单已取消');
  416. }
  417. if ($info->status != 1) {
  418. util::fail('订单不是待付款状态');
  419. }
  420. if ($info->book == 1) {
  421. util::fail('预售不能记欠款');
  422. }
  423. $ghsId = $info->ghsId;
  424. $ghs = GhsClass::getGhsInfo($ghsId);
  425. $customId = $ghs['customId'] ?? 0;
  426. $custom = CustomClass::getCustom($customId);
  427. if (empty($custom)) {
  428. util::fail('没有找到客户');
  429. }
  430. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  431. util::fail('请先申请赊账权限');
  432. }
  433. $connection = Yii::$app->db;//事务处理
  434. $transaction = $connection->beginTransaction();
  435. try {
  436. //延期支付
  437. PurchaseService::debt($info);
  438. $transaction->commit();
  439. if (isset($info->localOrder) && $info->localOrder == 1) {
  440. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  441. $current = date("Y_m_d");
  442. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  443. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  444. } else {
  445. if (isset($info->sendType) && $info->sendType == 4) {
  446. //标记当天已收一次包装费,岭南批发市场用的功能
  447. $current = date("Y_m_d");
  448. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  449. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  450. }
  451. }
  452. //app新订单通知
  453. $saleId = $info->saleId ?? 0;
  454. $order = OrderClass::getById($saleId, true);
  455. if (!empty($order)) {
  456. $cgShopId = $info->shopId ?? 0;
  457. $cgShop = ShopClass::getById($cgShopId, true);
  458. if (!empty($cgShop)) {
  459. //采购成功小程序通知
  460. \bizHd\notice\classes\NoticeClass::cgSuccessMiniNotice($cgShop, $info);
  461. }
  462. $shopId = $order->shopId ?? 0;
  463. $shop = ShopClass::getById($shopId, true);
  464. if (!empty($shop)) {
  465. //微信通知供货商
  466. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  467. $noticeText = json_encode(['orderId' => $saleId]);
  468. $noticeKey = "hdCgNoticeGhs";
  469. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  470. }
  471. //订单生成时唤起在线打印
  472. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  473. //有几个地方要同步去修改
  474. if (getenv('YII_ENV') == 'production') {
  475. //源花汇不自动打开小票
  476. $mallOrderNoPrint = [10536];
  477. } else {
  478. $mallOrderNoPrint = [644];
  479. }
  480. if (in_array($order->mainId, $mallOrderNoPrint)) {
  481. } else {
  482. OrderClass::onlinePrint($order);
  483. $order->printNum += 1;
  484. $order->save();
  485. }
  486. }
  487. //花店采购供货商端收到APP通知
  488. //NoticeClass::ghsNewOrderNotice($order);
  489. }
  490. } catch (Exception $e) {
  491. $transaction->rollBack();
  492. util::fail('支付失败');
  493. }
  494. util::success($info->attributes);
  495. }
  496. //余额支付 ssh 2021.1.24
  497. public function actionBalancePay()
  498. {
  499. $shopAdmin = $this->shopAdmin;
  500. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  501. util::fail('超管才能操作');
  502. }
  503. $get = Yii::$app->request->get();
  504. $orderSn = $get['orderSn'] ?? 0;
  505. $password = $get['password'] ?? '';
  506. // 重新获取,不加密的用户数据
  507. $admin = AdminClass::getById($this->adminId, true);
  508. if (password_verify($password, $admin->payPassword) == false) {
  509. util::fail('密码错误');
  510. }
  511. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  512. if (empty($info)) {
  513. util::fail('没有找到采购单');
  514. }
  515. if ($info->status == 5) {
  516. util::fail('订单已取消');
  517. }
  518. if ($info->status != 1) {
  519. util::fail('订单不是待付款状态');
  520. }
  521. PurchaseService::valid($info, $this->shopId);
  522. $connection = Yii::$app->db;//事务处理
  523. $transaction = $connection->beginTransaction();
  524. try {
  525. //余额支付
  526. purchaseService::balancePay($info);
  527. $transaction->commit();
  528. } catch (Exception $e) {
  529. $transaction->rollBack();
  530. util::fail('支付失败');
  531. }
  532. util::success($info->attributes);
  533. }
  534. //采购记录 ssh 2021.1.24
  535. public function actionList()
  536. {
  537. $get = Yii::$app->request->get();
  538. $where = ['shopId' => $this->shopId];
  539. $status = $get['status'] ?? 0;
  540. if (!empty($status)) {
  541. $where['status'] = $status;
  542. }
  543. $respond = PurchaseService::getPurchaseList($where);
  544. $respond['shop'] = $this->shop->attributes;
  545. util::success($respond);
  546. }
  547. //采购详情 ssh 2021.01.25
  548. public function actionDetail()
  549. {
  550. $get = Yii::$app->request->get();
  551. $id = $get['id'] ?? 0;
  552. $info = PurchaseService::getInfo($id, true, true);
  553. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  554. //不能注释,好像分享付款功能要用,客户没有登录也能付款
  555. //util::fail('没有权限访问');
  556. }
  557. $customId = $info['customId'] ?? 0;
  558. $custom = CustomClass::getById($customId);
  559. $customName = $custom['name'] ?? '';
  560. $customMobile = $custom['mobile'] ?? '';
  561. $info['customName'] = $customName;
  562. $info['customMobile'] = $customMobile;
  563. $hasDebtPay = $custom['debt'] ?? 0;
  564. $book = $info['book'] ?? 0;
  565. $presell = $info['presell'] ?? 0;
  566. //预售不能记欠款
  567. if ($book == 1) {
  568. $hasDebtPay = 0;
  569. }
  570. if ($presell == 1) {
  571. $hasDebtPay = 0;
  572. }
  573. $info['hasDebtPay'] = $hasDebtPay;
  574. $info['balance'] = $this->shop->balance ?? 0;
  575. util::success($info);
  576. }
  577. //可用的支付方式 ssh 2021.1.25
  578. public function actionPayWay()
  579. {
  580. $button = [
  581. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  582. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  583. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  584. ];
  585. util::success(['button' => $button]);
  586. }
  587. //待结款明细 ssh 2021.1.26
  588. public function actionDebtList()
  589. {
  590. $get = Yii::$app->request->get();
  591. $id = $get['id'] ?? 0;
  592. $info = GhsClass::getGhsInfo($id);
  593. GhsClass::valid($info, $this->shopId);
  594. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  595. $searchTime = $get['searchTime'] ?? '';
  596. if (!empty($searchTime)) {
  597. $startTime = $get['startTime'] ?? '';
  598. $endTime = $get['endTime'] ?? '';
  599. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  600. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  601. }
  602. $respond = PurchaseService::getDebtList($where);
  603. util::success($respond);
  604. }
  605. //微信支付 ssh 2021.4.11
  606. public function actionWxPay()
  607. {
  608. ini_set('date.timezone', 'Asia/Shanghai');
  609. $post = Yii::$app->request->post();
  610. $couponId = $post['couponId'] ?? 0;
  611. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  612. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  613. if (empty($order)) {
  614. util::fail('订单号无效');
  615. }
  616. if ($order->status == 5) {
  617. util::fail('订单已取消');
  618. }
  619. if ($order->status != 1) {
  620. util::fail('订单不是待付款状态');
  621. }
  622. $id = $order->id;
  623. $order->changePrice = 2;
  624. $order->save();
  625. $deadline = $order->deadline;
  626. $current = time();
  627. if (($current + 50) > strtotime($deadline)) {
  628. util::fail('订单已失效,请重新下单');
  629. }
  630. $name = $orderSn;
  631. $totalFee = $order->realPrice;
  632. //强制使用小程序的miniOpenId
  633. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  634. if (empty($openId)) {
  635. $currentShopId = $order->shopId ?? 0;
  636. $currentShop = ShopClass::getById($currentShopId, true);
  637. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  638. $currentMobile = $currentShop->mobile ?? '';
  639. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  640. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  641. }
  642. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  643. //流水类型、优惠卷、微信支付(h5还是小程序支付)类型 带到回调
  644. $wxPayType = 0;
  645. if (httpUtil::isMiniProgram()) {
  646. $wxPayType = 1;
  647. }
  648. $attach = "couponId=" . $couponId . "&capitalType=" . $purchaseCapital . '&wxPayType=' . $wxPayType;
  649. //订单30分钟后过期
  650. $now = time();
  651. $expireTime = $now + 1800;
  652. $wx = Yii::getAlias("@vendor/weixin");
  653. require_once($wx . '/lib/WxPay.Api.php');
  654. require_once($wx . '/example/WxPay.JsApiPay.php');
  655. $input = new \WxPayUnifiedOrder();
  656. $input->SetBody($name);
  657. $input->SetOut_trade_no($orderSn);
  658. $input->SetTotal_fee($totalFee * 100);
  659. $input->SetTime_start(date("YmdHis", $now));
  660. //将流水类型、代金劵等传给微信再回传
  661. $input->SetAttach($attach);
  662. $input->SetTime_expire(date("YmdHis", $expireTime));
  663. $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
  664. $input->SetTrade_type("JSAPI");
  665. //花卉宝代为申请的微信支付
  666. $merchantExtend = WxOpenClass::getWxInfo();
  667. if (isset($merchantExtend['wxPayApply']) && $merchantExtend['wxPayApply'] == 1) {
  668. $input->SetSub_openid($openId);
  669. } else {
  670. $input->SetOpenid($openId);
  671. }
  672. //强制使用小程序的miniAppId
  673. $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
  674. $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
  675. $tools = new \JsApiPay();
  676. $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
  677. $newParams = json_decode($jsApiParameters, true);
  678. $newParams['id'] = $id;
  679. util::success($newParams);
  680. }
  681. //计算运费 ssh 20221003
  682. public function actionFreight()
  683. {
  684. $get = Yii::$app->request->get();
  685. $shop = $this->shop;
  686. $shopLat = isset($shop->lat) ? $shop->lat : '';
  687. $shopLong = isset($shop->long) ? $shop->long : '';
  688. if (empty($shopLat) || empty($shopLong)) {
  689. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  690. }
  691. $ghsId = $get['ghsId'] ?? 0;
  692. $ghs = GhsClass::getById($ghsId, true);
  693. if (empty($ghs)) {
  694. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  695. }
  696. $ghsShopId = $ghs->shopId ?? 0;
  697. $ghsShop = ShopClass::getById($ghsShopId, true);
  698. if (empty($ghsShop)) {
  699. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  700. }
  701. $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
  702. $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
  703. if (empty($ghsShopLat) || empty($ghsShopLong)) {
  704. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  705. }
  706. $weight = $get['weight'] ?? 0;
  707. if (empty($weight)) {
  708. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  709. }
  710. $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight);
  711. util::success($respond);
  712. }
  713. //运费计算 lqh 2021.4.12 暂时返回固定值
  714. public function actionFreight2()
  715. {
  716. //lqh 2021.6.25 运费固定返回0
  717. util::success(['sedCost' => 0]);
  718. $post = Yii::$app->request->post();
  719. $ghsId = $post['ghsId'] ?? 0;
  720. //供货商
  721. $ghsInfo = GhsClass::getById($ghsId);
  722. if (empty($ghsInfo)) {
  723. util::fail('没有找到供货商');
  724. }
  725. //花材信息
  726. $productJson = $post['product'] ?? '';
  727. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  728. if (empty($productJson)) {
  729. util::fail('请选择花材');
  730. }
  731. $productList = json_decode($productJson, true);
  732. if (empty($productList)) {
  733. util::fail('请选择花材');
  734. }
  735. $productList = PurchaseClass::mergeItemInfo($productList);
  736. $weight = 0;
  737. $price = 0;
  738. $productData = ProductClass::getProductMapData($productList);
  739. $bigNum = 0;
  740. foreach ($productList as $key => $val) {
  741. $productId = $val['productId'];
  742. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  743. $bigNum += $val['bigNum'];
  744. $weight = bcadd($w, $weight, 2);
  745. $ratio = $productData[$productId]['ratio'] ?? 0;
  746. //大小数量合并多少扎
  747. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  748. //售卖价格
  749. $itemPrice = $productData[$productId]['price'] ?? 0;
  750. //合计价格
  751. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  752. }
  753. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  754. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  755. $sendTime = $post['sendTime'] ?? '';
  756. if (!empty($sendTime)) {
  757. //配送时间不为空
  758. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  759. $sendTimeInt = strtotime($sendTime);
  760. $now = time();
  761. $diff = $sendTimeInt - $now;
  762. //因服务器写入时间,改为至少提前6分钟
  763. if ($diff <= 360) {
  764. util::fail('配送时间至少提前6分钟');
  765. }
  766. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  767. } else {
  768. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  769. }
  770. //找供货商的客户信息,用里面的配送相关地址信息
  771. $customId = $ghsInfo['customId'] ?? 0;
  772. $customInfo = CustomClass::getById($customId);
  773. if (empty($customInfo)) {
  774. util::fail('没有找到客户');
  775. }
  776. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  777. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  778. if (!$shopNo) {
  779. //使用自定义运费
  780. //util::fail('供货商暂未开通配送');
  781. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  782. util::success(['sedCost' => $cost]);
  783. }
  784. //组装订单信息
  785. $info = [];
  786. //发送放的商家信息,即供货商的相关信息
  787. $info['sjId'] = $ghsSjId;
  788. $info['shopId'] = $ghsShopId;
  789. $info['actPrice'] = $price;//价格
  790. $info['weight'] = $weight;
  791. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  792. $info['bigNum'] = $bigNum;
  793. //配送地址,当前零售端的门店地址
  794. $info['customName'] = $customInfo['name'] ?? '';
  795. $info['customMobile'] = $customInfo['mobile'] ?? '';
  796. $info['province'] = $customInfo['province'] ?? '';
  797. $info['city'] = $customInfo['city'] ?? '';;
  798. $info['address'] = $customInfo['address'] ?? '';;
  799. $info['floor'] = $customInfo['floor'] ?? '';;
  800. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  801. $info['lat'] = $customInfo['lat'] ?? '';;
  802. $info['long'] = $customInfo['long'] ?? '';;
  803. $info['sendTime'] = $sendTime;
  804. $res = DadaExpressServices::queryDeliverFee($info);
  805. util::success(['sedCost' => $res['fee']]);
  806. }
  807. }