PurchaseController.php 37 KB

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