PurchaseController.php 44 KB

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