PurchaseController.php 37 KB

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