PurchaseController.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\common\classes\GhsNotifyClass;
  5. use biz\ghs\classes\GhsClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExtClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizGhs\order\classes\OrderClass;
  10. use bizHd\purchase\classes\PurchaseClass;
  11. use bizHd\purchase\classes\PurchaseItemClass;
  12. use bizHd\purchase\services\PurchaseService;
  13. use bizGhs\product\classes\ProductClass;
  14. use bizHd\wx\classes\WxOpenClass;
  15. use common\components\dateUtil;
  16. use common\components\delivery\services\DispatchService;
  17. use common\components\delivery\util\DeliveryQuoteUtil;
  18. use common\components\dict;
  19. use common\components\imgUtil;
  20. use common\components\noticeUtil;
  21. use common\components\orderSn;
  22. use Yii;
  23. use common\components\util;
  24. use bizGhs\order\services\OrderService;
  25. use common\components\lakala\Lakala;
  26. use common\components\qrCodeUtil;
  27. use common\components\push;
  28. class PurchaseController extends BaseController
  29. {
  30. public $guestAccess = ['detail', 'ali-pay', 'get-ali-pay-code', 'wx-pay'];
  31. //取消采购单 ssh 20250710
  32. public function actionCancel()
  33. {
  34. $get = Yii::$app->request->get();
  35. $id = $get['id'] ?? '';
  36. $info = PurchaseClass::getById($id, true);
  37. if (empty($info)) {
  38. util::fail('没有找到采购单');
  39. }
  40. if (!isset($info->mainId) || $info->mainId != $this->mainId) {
  41. util::fail('访问错误');
  42. }
  43. if ($info->status == 5) {
  44. util::fail('已取消');
  45. }
  46. if ($info->status != 1) {
  47. util::fail('不能取消');
  48. }
  49. PurchaseService::expire($info);
  50. util::complete('已取消');
  51. }
  52. //获取支付宝付款码 ssh 20240713
  53. public function actionGetAliPayCode()
  54. {
  55. $get = Yii::$app->request->get();
  56. $orderSn = $get['orderSn'] ?? '';
  57. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  58. if (empty($info)) {
  59. util::fail('没有找到采购单');
  60. }
  61. if (isset($info->mainId) == false || $info->mainId != $this->mainId) {
  62. util::fail('无法访问');
  63. }
  64. if (getenv('YII_ENV') == 'production') {
  65. $url = "https://mall.huahb.cn/#/admin/cg/alipay?orderSn={$orderSn}";
  66. } else {
  67. $url = "https://mall.huaml.com/#/admin/cg/alipay?orderSn={$orderSn}";
  68. }
  69. $unique = "order_ali_pay_" . $orderSn;
  70. $imgUrl = qrCodeUtil::generateRechargeQrCode($url, $unique);
  71. $imageUrl = imgUtil::groupImg($imgUrl);
  72. util::success(['imgUrl' => $imageUrl]);
  73. }
  74. //确认收货 ssh 20231119
  75. public function actionConfirmTake()
  76. {
  77. $get = Yii::$app->request->get();
  78. $id = $get['id'] ?? 0;
  79. $info = PurchaseClass::getById($id, true);
  80. if (empty($info)) {
  81. util::fail('没有找到采购单');
  82. }
  83. if (isset($info->mainId) == false || $info->mainId != $this->mainId) {
  84. util::fail('无法访问');
  85. }
  86. PurchaseClass::confirmTake($info);
  87. util::complete('确认成功');
  88. }
  89. public function actionGetFullInfo()
  90. {
  91. $get = Yii::$app->request->get();
  92. $id = $get['id'] ?? 0;
  93. $info = PurchaseClass::getById($id);
  94. if (empty($info)) {
  95. util::fail('没有找到采购单');
  96. }
  97. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  98. util::fail('无法访问');
  99. }
  100. $orderSn = $info['orderSn'] ?? '';
  101. $ghsId = $info['ghsId'] ?? 0;
  102. $info['ghsShopId'] = 0;
  103. if (!empty($ghsId)) {
  104. $ghs = GhsClass::getById($ghsId, true);
  105. $info['ghsShopId'] = $ghs->shopId ?? 0;
  106. }
  107. $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  108. if (!empty($list)) {
  109. foreach ($list as $key => $item) {
  110. $shortCover = $item['cover'] ?? '';
  111. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  112. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  113. $list[$key]['cover'] = $cover;
  114. $list[$key]['bigCover'] = $bigCover;
  115. $list[$key]['shortCover'] = $shortCover;
  116. }
  117. }
  118. $info['product'] = $list;
  119. util::success($info);
  120. }
  121. //虚拟供货商的采购 ssh 20230308
  122. public function actionAddVirtualCg()
  123. {
  124. $post = Yii::$app->request->post();
  125. $ghsId = $post['ghsId'] ?? 0;
  126. $payMode = $post['payMode'] ?? 0;
  127. $payWay = $post['payWay'] ?? 0;
  128. $ghsInfo = GhsClass::getById($ghsId);
  129. if (empty($ghsInfo)) {
  130. util::fail('没有找到供货商');
  131. }
  132. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  133. util::fail('不能向自己的店买花');
  134. }
  135. $connection = Yii::$app->db;
  136. $transaction = $connection->beginTransaction();
  137. try {
  138. $post['book'] = $post['book'] ?? 0;
  139. $post['sjId'] = $this->sjId;
  140. $post['shopId'] = $this->shopId;
  141. $post['shopAdminId'] = $this->shopAdminId;
  142. $post['ghsId'] = $ghsId;
  143. $post['ghsName'] = $ghsInfo['name'] ?? '';
  144. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  145. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  146. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  147. $customId = $ghsInfo['customId'] ?? 0;
  148. $post['customId'] = $customId;
  149. $shopAdmin = $this->shopAdmin ?? null;
  150. $name = $shopAdmin->name ?? '';
  151. $post['shopAdminName'] = $name;
  152. $post['mainId'] = $this->mainId;
  153. $product = $post['product'] ?? '';
  154. $productList = json_decode($product, true);
  155. //判断product数据是不是同个供货商的
  156. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  157. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  158. if (!empty($productInfoList)) {
  159. foreach ($productInfoList as $currentInfo) {
  160. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  161. util::fail('只能选择同一家的商品哦');
  162. }
  163. }
  164. if (count($productInfoList) != count($ids)) {
  165. util::fail('存在无效商品');
  166. }
  167. } else {
  168. util::fail('花材不存在');
  169. }
  170. //供货商预订单最低公斤数要求和每公斤服务费
  171. $ghsInfo['kiloFee'] = 0;
  172. $ghsInfo['miniKilo'] = 0;
  173. $post['product'] = $productList;
  174. $packCost = 0;
  175. $post['packCost'] = $packCost;
  176. $post['sendCost'] = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  177. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  178. if ($payMode == 0) {
  179. //欠款
  180. PurchaseService::debt($respond);
  181. } else {
  182. //线下支付
  183. PurchaseService::payAfter($respond, $payWay);
  184. $saleId = $respond->saleId ?? 0;
  185. $order = OrderClass::getById($saleId, true);
  186. if (empty($order)) {
  187. util::fail('没有找到订单');
  188. }
  189. OrderService::payAfter($order, $payWay, true);
  190. }
  191. $transaction->commit();
  192. util::success($respond);
  193. } catch (Exception $e) {
  194. $transaction->rollBack();
  195. util::fail();
  196. }
  197. }
  198. //生成采购单 ssh 2021.1.17
  199. public function actionCreateOrder()
  200. {
  201. $post = Yii::$app->request->post();
  202. $version = $post['version'] ?? 0;
  203. $direct = $post['direct'] ?? 0;
  204. //多颜色数据整理
  205. $colorItem = $post['xj'] ?? [];
  206. $newXj = [];
  207. if (!empty($colorItem)) {
  208. $colorData = json_decode($colorItem, true);
  209. if (!empty($colorData) && is_array($colorData)) {
  210. foreach ($colorData as $colorInfo) {
  211. $ptItemId = $colorInfo['ptItemId'] ?? 0;
  212. $colorList = $colorInfo['list'] ?? [];
  213. if (!empty($colorList)) {
  214. foreach ($colorList as $colorItemKey => $colorItem) {
  215. $newXj[$ptItemId][] = $colorItem;
  216. }
  217. }
  218. }
  219. }
  220. }
  221. $post['xj'] = $newXj;
  222. $ghsId = $post['ghsId'] ?? 0;
  223. //供货商
  224. $ghsInfo = GhsClass::getById($ghsId);
  225. if (empty($ghsInfo)) {
  226. util::fail('没有找到供货商');
  227. }
  228. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  229. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  230. util::fail('不能跟自己的店买花');
  231. }
  232. $connection = Yii::$app->db;
  233. $transaction = $connection->beginTransaction();
  234. try {
  235. $post['book'] = $post['book'] ?? 0;
  236. $post['sjId'] = $this->sjId;
  237. $post['shopId'] = $this->shopId;
  238. $post['shopAdminId'] = $this->shopAdminId;
  239. $post['ghsId'] = $ghsId;
  240. $post['ghsName'] = $ghsInfo['name'] ?? '';
  241. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  242. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  243. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  244. $customId = $ghsInfo['customId'] ?? 0;
  245. $custom = CustomClass::getById($customId, true);
  246. if (empty($custom)) {
  247. util::fail('用户信息缺失');
  248. }
  249. $post['customId'] = $customId;
  250. $shopAdmin = $this->shopAdmin ?? null;
  251. $name = $shopAdmin->name ?? '';
  252. $post['shopAdminName'] = $name;
  253. $post['mainId'] = $this->mainId;
  254. $product = $post['product'] ?? '';
  255. $productList = json_decode($product, true);
  256. //判断product数据是不是同个供货商的
  257. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  258. $ghsMainId = $ghsShopInfo->mainId ?? 0;
  259. $isOpen = ShopClass::isOpen($ghsShopInfo);
  260. $book = $post['book'] ?? 0;
  261. if ($isOpen == 0 && $book == 0) {
  262. util::fail('本店已休息,请选择其它门店');
  263. }
  264. if (isset($post['transType']) && $post['transType'] == 4) {
  265. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  266. $notSameCity = PurchaseClass::notSameCity($this->shop, $ghsShopInfo);
  267. if ($notSameCity == false) {
  268. util::fail('距离太远,不能选择同城配送');
  269. }
  270. }
  271. }
  272. if ($book == 1) {
  273. $sendTimeWant = isset($post['sendTimeWant']) && !empty($post['sendTimeWant']) ? $post['sendTimeWant'] : date("Y-m-d");
  274. $future = date("Y-m-d", strtotime('+2 day'));
  275. if (strtotime($sendTimeWant) < strtotime($future)) {
  276. //util::fail('请选择时间');
  277. }
  278. $nowTime = strtotime(date("Y-m-d"));
  279. if (strtotime($sendTimeWant) < $nowTime) {
  280. util::fail('不能选择过去时间');
  281. }
  282. }
  283. //收集客户要下单的花材数量
  284. $orderNum = [];
  285. $productCount = 0;
  286. foreach ($productList as $key => $product) {
  287. $bigNum = $product['bigNum'] ?? 0;
  288. $smallNum = $product['smallNum'] ?? 0;
  289. $pName = $product['name'] ?? '';
  290. if ($smallNum > 0) {
  291. util::fail($pName . '不能选小单位');
  292. }
  293. $productId = $product['productId'] ?? 0;
  294. $orderNum[$productId] = ['num' => $bigNum];
  295. $productCount += $bigNum;
  296. }
  297. $lackList = [];
  298. $changeList = [];
  299. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  300. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  301. if (!empty($productInfoList)) {
  302. $presellData = [];
  303. $nowTime = strtotime(date("Y-m-d"));
  304. foreach ($productInfoList as $currentInfo) {
  305. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $ghsMainId) {
  306. util::fail('只能选择同一家的商品哦');
  307. }
  308. $currentName = $currentInfo['name'] ?? '';
  309. if (isset($currentInfo['frontHide']) && $currentInfo['frontHide'] == 1) {
  310. //珑松珠海店,隐藏的商品,分享出去了也要能下单。644 是测试账号的mainId
  311. if (isset($currentInfo['mainId']) && in_array($currentInfo['mainId'], [14116, 644])) {
  312. //无操作
  313. } else {
  314. util::fail($currentName . ' 没有库存,请删除');
  315. }
  316. }
  317. $presell = $currentInfo['presell'] ?? 0;
  318. $presellData[] = $presell;
  319. if ($presell == 1) {
  320. if ($book == 1) {
  321. util::fail('预订时不能选择预售花材');
  322. } else {
  323. if (isset($post['sendTimeWant']) == false || empty($post['sendTimeWant'])) {
  324. util::fail('请选择配送日期');
  325. }
  326. $sendTimeWant = $post['sendTimeWant'];
  327. if (strtotime($sendTimeWant) < $nowTime) {
  328. util::fail('不能选择过去时间');
  329. }
  330. $hasDateStr = $currentInfo['presellDate'] ?? [];
  331. $hasDate = explode(',', trim($hasDateStr));
  332. if (empty($hasDate)) {
  333. util::fail('预售日期有问题,请提醒门店');
  334. }
  335. if (in_array(strtotime($sendTimeWant), $hasDate) == false) {
  336. util::fail("有预售花材在{$sendTimeWant}没到货");
  337. }
  338. $post['presell'] = 1;
  339. }
  340. }
  341. if ($version >= 10) {
  342. $stock = $currentInfo['stock'] ?? 0;
  343. $stock = floatval($stock);
  344. //会有0.5扎和2.5扎问题,所以要转成整数,不然会出严重问题 ssh 20250503
  345. $stock = intval($stock);
  346. $productId = $currentInfo['id'] ?? 0;
  347. $productName = $currentInfo['name'] ?? '';
  348. $num = $orderNum[$productId] && $orderNum[$productId]['num'] ? $orderNum[$productId]['num'] : 0;
  349. $num = floatval($num);
  350. if ($num > $stock) {
  351. $lackList[] = ['name' => $productName, 'stock' => $stock];
  352. $changeList[$productId] = $stock;
  353. }
  354. }
  355. }
  356. if (count($productInfoList) != count($ids)) {
  357. util::fail('存在无效商品');
  358. }
  359. $presellData = array_unique($presellData);
  360. if (count($presellData) > 1) {
  361. util::fail('预售和非预售花材不能一起下单');
  362. }
  363. if ($version >= 10) {
  364. if ($direct == 1) {
  365. //直接更换库存并提交
  366. $allNoStock = true;
  367. foreach ($productList as $pKey => $pVal) {
  368. $productId = $pVal['productId'] ?? 0;
  369. if (isset($changeList[$productId])) {
  370. $changeStock = $changeList[$productId];
  371. $productList[$pKey]['bigNum'] = $changeStock;
  372. }
  373. if ($productList[$pKey]['bigNum'] > 0) {
  374. $allNoStock = false;
  375. }
  376. if ($productList[$pKey]['bigNum'] <= 0) {
  377. unset($productList[$pKey]);
  378. }
  379. }
  380. if ($allNoStock) {
  381. util::fail('全部没库存了');
  382. }
  383. } else {
  384. //前台提示库存不足
  385. if (!empty($lackList)) {
  386. util::success(['lackList' => $lackList, 'hasLackError' => 1]);
  387. }
  388. }
  389. }
  390. } else {
  391. util::fail('花材不存在');
  392. }
  393. //供货商预订单最低公斤数要求和每公斤服务费
  394. $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
  395. $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
  396. //旭海银柳的临时解决办法
  397. if (getenv('YII_ENV') == 'production') {
  398. if ($ghsShopInfo->mainId == 41121) {
  399. $mer = [1903693, 1903712, 1903789, 1903796, 1903816, 1903841, 1903855, 1903859, 1903860, 1903864];
  400. $getIds = array_column($productList, 'productId');
  401. $hasYl = false;
  402. foreach ($productList as $ylVal) {
  403. $ylProductId = $ylVal['productId'];
  404. if (in_array($ylProductId, $mer)) {
  405. $hasYl = true;
  406. }
  407. }
  408. if ($hasYl) {
  409. $myInfoList = ProductClass::getAllByCondition(['id' => ['in', $getIds]], null, '*', 'id');
  410. $currentMap = [1903693 => 220, 1903712 => 150, 1903789 => 100, 1903796 => 60, 1903816 => 45, 1903841 => 80, 1903855 => 55, 1903859 => 40, 1903860 => 25, 1903864 => 20];
  411. foreach ($productList as $ylKey => $ylValue) {
  412. $ylProductId = $ylValue['productId'];
  413. $num = $ylValue['bigNum'] ?? 0;
  414. if (!isset($currentMap[$ylProductId])) {
  415. util::fail('花材有问题');
  416. }
  417. $mustBeNum = $currentMap[$ylProductId];
  418. $myInfo = $myInfoList[$ylProductId] ?? [];
  419. if (empty($myInfo)) {
  420. util::fail('花材有问题哦');
  421. }
  422. $myName = $myInfo['name'] ?? '';
  423. if ($num >= $mustBeNum) {
  424. $curVal = $num / $mustBeNum;
  425. if ($curVal != intval($curVal)) {
  426. util::fail($myName . ' 要' . $mustBeNum . '捆或' . $mustBeNum . '的倍数');
  427. }
  428. } else {
  429. util::fail($myName . ' 数量要' . $mustBeNum . '捆');
  430. }
  431. }
  432. }
  433. }
  434. }
  435. $post['product'] = $productList;
  436. $sendType = $post['sendType'] ?? 0;
  437. $transType = $post['transType'] ?? 0;
  438. $shopExt = null;
  439. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  440. $shopExt = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,thirdSend,hcMap,hcFreeKm');
  441. if ($shopExt->thirdSend == 1) {
  442. util::fail('本店暂不能使用跑腿哦');
  443. }
  444. }
  445. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  446. //昆明发货包装费和运费的计算
  447. $totalWeight = 0;
  448. $totalItemNum = 0;
  449. foreach ($productList as $itemData) {
  450. $bigNum = $itemData['bigNum'] ?? 0;
  451. $thisWeight = $itemData['weight'] ?? 0;
  452. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  453. $totalWeight = bcadd($currentWeight, $totalWeight, 2);
  454. $totalItemNum = bcadd($totalItemNum, $bigNum, 2);
  455. }
  456. $totalItemNum = floatval($totalItemNum);
  457. $kmPackCost = PurchaseClass::getKmPackCost($ghsMainId);
  458. $packCost = 0;
  459. if (!empty($kmPackCost)) {
  460. foreach ($kmPackCost as $itemPack) {
  461. $kiloNum = $itemPack['num'] ?? 0;
  462. if ($kiloNum >= $totalWeight) {
  463. $packCost = $itemPack['amount'] ?? 0;
  464. break;
  465. }
  466. }
  467. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  468. noticeUtil::push("总重量" . $totalWeight, '15280215347');
  469. }
  470. }
  471. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  472. noticeUtil::push("走的pfLevel=1", '15280215347');
  473. }
  474. //同城配送和到店自取免打包费
  475. if ($transType == 4 || $transType == 5) {
  476. $packCost = 0;
  477. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  478. noticeUtil::push("没有计算打包费原因:transType = 4 =5", '15280215347');
  479. }
  480. }
  481. //如果已经算过一次打包费和运费了,则不再费了
  482. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  483. if ($needAdd == false) {
  484. $packCost = 0;
  485. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  486. noticeUtil::push("没有计算打包费原因:已经算过一次了", '15280215347');
  487. }
  488. }
  489. $post['packCost'] = $packCost;
  490. $transCost = dict::getDict('transCost', null, null, $ghsShopInfo->mainId);
  491. $sendCost = 0;
  492. if (!empty($transCost)) {
  493. foreach ($transCost as $trans) {
  494. $sign = $trans['sign'] ?? 0;
  495. if ($sign == $transType) {
  496. if ($sign == 2) {
  497. //冷链物流
  498. $llOption = $trans['option'] ?? [];
  499. if (!empty($llOption)) {
  500. $largeInfo = end($llOption);
  501. $largeNum = $largeInfo['num'] ?? 0;
  502. $largeAmount = $largeInfo['amount'] ?? 0;
  503. $bs = intval($totalItemNum / $largeNum);
  504. if ($bs > 0) {
  505. $addSendCost = bcmul($bs, $largeAmount, 2);
  506. $sendCost = bcadd($sendCost, $addSendCost, 2);
  507. $subNum = bcmul($bs, $largeNum, 2);
  508. $subNum = floatval($subNum);
  509. $totalItemNum = bcsub($totalItemNum, $subNum, 2);
  510. $totalItemNum = floatval($totalItemNum);
  511. }
  512. foreach ($llOption as $miniItem) {
  513. if ($miniItem['num'] >= $totalItemNum) {
  514. $sendCost = bcadd($sendCost, $miniItem['amount'], 2);
  515. break;
  516. }
  517. }
  518. }
  519. } else {
  520. //其它物流
  521. $perKiloCost = $trans['perKiloCost'] ?? 0;
  522. $sendCost = bcmul($perKiloCost, $totalWeight, 3);
  523. $sendCost = round($sendCost, 2);
  524. }
  525. }
  526. }
  527. }
  528. //如果已经算过一次打包费和运费了,则不再费了
  529. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  530. if ($needAdd == false) {
  531. $sendCost = 0;
  532. }
  533. $post['sendCost'] = $sendCost;
  534. //标记为昆明到地方订单
  535. $post['localOrder'] = 1;
  536. } else {
  537. //同城发货包装费和运费的计算
  538. $sendCost = 0;
  539. $sendDistance = 0;
  540. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  541. // 生成随机订单号
  542. $prefix = 'XSD_CS-' . $this->mainId . '-';
  543. $orderSn = $prefix . round(microtime(true) * 1000);
  544. // 使用 DeliveryQuoteUtil 获取配送报价
  545. try {
  546. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  547. 'productList' => $productList,
  548. 'deliveryPlatform' => $post['deliveryPlatform'],
  549. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  550. 'ghsInfo' => $ghsInfo,
  551. 'custom' => $custom,
  552. 'order' => [
  553. 'orderSn' => $orderSn,
  554. 'itemTotalAmount' => $post['itemTotalAmount'] ?? 0,
  555. 'remark' => $post['remark'] ?? '',
  556. ],
  557. 'mainId' => $this->mainId,
  558. 'productCount' => $productCount,
  559. ]);
  560. $sendCost = $quoteResult['sendCost'];
  561. $sendDistance = $quoteResult['sendDistance'];
  562. } catch (\Exception $e) {
  563. util::fail($e->getMessage());
  564. }
  565. }
  566. $post['sendCost'] = $sendCost;
  567. $post['sendDistance'] = $sendDistance;
  568. //出车、发快递、发物流可以算包装费
  569. $packCost = 0;
  570. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  571. noticeUtil::push("走的pfLevel=0", '15280215347');
  572. }
  573. $itemTotalAmount = $post['itemTotalAmount'] ?? 0;
  574. unset($post['itemTotalAmount']);
  575. if ($sendType == dict::getDict('sendType', 'express')) {
  576. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $itemTotalAmount);
  577. if ($needAdd) {
  578. $packCost = $ghsShopInfo->packCost ?? 0;
  579. }
  580. }
  581. $post['packCost'] = $packCost;
  582. }
  583. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  584. noticeUtil::push("包装费:" . $packCost, '15280215347');
  585. }
  586. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  587. if ($sendType == 0) {
  588. if (getenv('YII_ENV') == 'production') {
  589. //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
  590. if ($ghsShopId == 4608) {
  591. if ($respond->actPrice < 500) {
  592. $transaction->rollBack();
  593. util::fail('满500元支持免费配送');
  594. }
  595. }
  596. //叶上花,满500元市区免费配送
  597. if ($ghsShopId == 4804) {
  598. if ($respond->actPrice < 500) {
  599. $transaction->rollBack();
  600. util::fail('满500元支持免费配送');
  601. }
  602. }
  603. //花悠星 国恋 勇记 万丽 陆丰暂时不支持免费配送
  604. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003 || $ghsShopId == 38128) {
  605. util::fail('暂不支持送货上门,请选其它配送方式!');
  606. }
  607. //广州鑫源花卉,选择送货上门必须选必选商品
  608. if ($ghsShopId == 12439) {
  609. $hasMust = array_intersect($ids, [3446945, 3446970, 3446973, 3446979, 3446995, 3447010, 3447023, 3459983]);
  610. if (empty($hasMust)) {
  611. util::fail('请返回选择 必选商品');
  612. }
  613. }
  614. } else {
  615. //好运鲜花批发,选择送货上门必须选必选商品
  616. if ($ghsShopId == 36523) {
  617. $hasMust = array_intersect($ids, [25846, 34238, 86042]);
  618. if (empty($hasMust)) {
  619. //util::fail('请返回选择 必选商品');
  620. }
  621. }
  622. }
  623. if (isset($ghsInfo['home'])) {
  624. if ($ghsInfo['home'] == 0) {
  625. util::fail('暂不支持送货上门,请选其它配送方式');
  626. }
  627. if (!empty($ghsInfo['homeAmount'])) {
  628. $homeType = $ghsInfo['homeType'] ?? 0;
  629. $homeAmount = floatval($ghsInfo['homeAmount']);
  630. if ($homeType == 0) {
  631. if ($homeAmount > $respond->actPrice) {
  632. util::fail("满{$homeAmount}元 可送货上门");
  633. }
  634. }
  635. if ($homeType == 1) {
  636. if ($homeAmount > $respond->bigNum) {
  637. util::fail("满{$homeAmount}扎 可送货上门");
  638. }
  639. }
  640. }
  641. }
  642. }
  643. if (getenv('YII_ENV') == 'production') {
  644. //昆明花落谁家
  645. if ($ghsShopId == 13398) {
  646. if ($respond->bigNum < 15) {
  647. $transaction->rollBack();
  648. util::fail('不足15扎,请联系店长');
  649. }
  650. }
  651. //盛丰最少要20扎
  652. if ($ghsShopId == 44556) {
  653. if ($respond->bigNum < 20) {
  654. $transaction->rollBack();
  655. util::fail('最少要20扎');
  656. }
  657. }
  658. }
  659. $transaction->commit();
  660. util::success($respond);
  661. } catch (Exception $e) {
  662. $transaction->rollBack();
  663. util::fail();
  664. }
  665. }
  666. //赊账 ssh 2021.1.24
  667. public function actionDebtPay()
  668. {
  669. $get = Yii::$app->request->get();
  670. $orderSn = $get['orderSn'] ?? 0;
  671. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  672. PurchaseService::valid($info, $this->shopId);
  673. //解决重复和并发提交
  674. $cacheKey = 'hd_debt_pay_' . $orderSn;
  675. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  676. if (!empty($has)) {
  677. util::fail('请稍等...');
  678. }
  679. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  680. if ($info->status == 5) {
  681. util::fail('订单已取消');
  682. }
  683. if ($info->status != 1) {
  684. util::fail('订单不是待付款状态');
  685. }
  686. if ($info->book == 1) {
  687. util::fail('预售不能记欠款');
  688. }
  689. $ghsId = $info->ghsId;
  690. $ghs = GhsClass::getGhsInfo($ghsId);
  691. $customId = $ghs['customId'] ?? 0;
  692. $custom = CustomClass::getCustom($customId);
  693. if (empty($custom)) {
  694. util::fail('没有找到客户');
  695. }
  696. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  697. util::fail('请先申请赊账权限');
  698. }
  699. if (getenv('YII_ENV') == 'production') {
  700. //源花汇不允许客户自己下欠款单
  701. if ($ghs['mainId'] == 10536) {
  702. util::fail('暂未开通');
  703. }
  704. }
  705. $connection = Yii::$app->db;
  706. $transaction = $connection->beginTransaction();
  707. try {
  708. //延期支付
  709. PurchaseService::debt($info);
  710. $transaction->commit();
  711. if (isset($info->localOrder) && $info->localOrder == 1) {
  712. if ($info->transType != 5 && $info->transType != 4) {
  713. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  714. $current = date("Y_m_d");
  715. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  716. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  717. }
  718. } else {
  719. if (isset($info->sendType) && $info->sendType == 4) {
  720. //标记当天已收一次包装费,岭南批发市场用的功能
  721. $current = date("Y_m_d");
  722. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  723. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  724. }
  725. }
  726. //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  727. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  728. if (!empty($cg)) {
  729. if (isset($cg->book) && $cg->book == 0) {
  730. $ghsShopId = $ghs['shopId'] ?? 0;
  731. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  732. if ($ext->orderFlow == 0) {
  733. PurchaseClass::confirmTake($cg);
  734. }
  735. }
  736. }
  737. //app新订单通知
  738. $saleId = $info->saleId ?? 0;
  739. $order = OrderClass::getById($saleId, true);
  740. if (!empty($order)) {
  741. $shopId = $order->shopId ?? 0;
  742. $shop = ShopClass::getById($shopId, true);
  743. if (!empty($shop)) {
  744. //微信通知供货商
  745. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  746. // $noticeText = json_encode(['orderId' => $saleId]);
  747. // $noticeKey = "hdCgNoticeGhs";
  748. // Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  749. GhsNotifyClass::newOrderNotify($saleId);
  750. //向供货商APP发通知
  751. // $cgShop = ShopClass::getById($cg->shopId, true, 'shopName, merchantName');
  752. // $push = new push('ghs', push::MSG_TYPE_ORDER);
  753. // $push->ghsOrderMessage($shop, $cgShop, $order);
  754. }
  755. //订单生成时唤起在线打印
  756. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  757. //有几个地方要同步去修改
  758. if (getenv('YII_ENV') == 'production') {
  759. //源花汇、盛丰不自动打小票,关键词mall_order_no_auto_print
  760. $mallOrderNoPrint = [10536, 44282, 26374];
  761. } else {
  762. $mallOrderNoPrint = [];
  763. }
  764. if (in_array($order->mainId, $mallOrderNoPrint)) {
  765. } else {
  766. OrderClass::onlinePrint($order);
  767. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  768. if (isset($ext->printSn) && !empty($ext->printSn)) {
  769. $order->printNum += 1;
  770. $order->save();
  771. }
  772. //花镜 打二次小票,有多处要修改搜索关键词tow_print
  773. if (in_array($order->mainId, [76796])) {
  774. OrderClass::onlinePrint($order);
  775. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  776. if (isset($ext->printSn) && !empty($ext->printSn)) {
  777. $order->printNum += 1;
  778. $order->save();
  779. }
  780. }
  781. }
  782. }
  783. //更新最后下单时间
  784. $customId = $order->customId ?? 0;
  785. $custom = CustomClass::getById($customId, true);
  786. $date = date("Y-m-d H:i:s");
  787. if (!empty($custom)) {
  788. $custom->recentExpend = $date;
  789. $custom->save();
  790. }
  791. $ghsId = $order->ghsId ?? 0;
  792. $ghs = GhsClass::getById($ghsId, true);
  793. if (!empty($ghs)) {
  794. $ghs->recentExpend = $date;
  795. $ghs->save();
  796. }
  797. }
  798. } catch (Exception $e) {
  799. $transaction->rollBack();
  800. util::fail('支付失败');
  801. }
  802. util::success($info->attributes);
  803. }
  804. //余额支付 ssh 2021.1.24
  805. public function actionBalancePay()
  806. {
  807. $shopAdmin = $this->shopAdmin;
  808. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  809. util::fail('超管才能操作');
  810. }
  811. $get = Yii::$app->request->get();
  812. $orderSn = $get['orderSn'] ?? 0;
  813. $password = $get['password'] ?? '';
  814. // 重新获取,不加密的用户数据
  815. $admin = AdminClass::getById($this->adminId, true);
  816. if (password_verify($password, $admin->payPassword) == false) {
  817. util::fail('密码错误');
  818. }
  819. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  820. if (empty($info)) {
  821. util::fail('没有找到采购单');
  822. }
  823. if ($info->status == 5) {
  824. util::fail('订单已取消');
  825. }
  826. if ($info->status != 1) {
  827. util::fail('订单不是待付款状态');
  828. }
  829. PurchaseService::valid($info, $this->shopId);
  830. $connection = Yii::$app->db;
  831. $transaction = $connection->beginTransaction();
  832. try {
  833. //余额支付
  834. purchaseService::balancePay($info);
  835. $transaction->commit();
  836. } catch (Exception $e) {
  837. $transaction->rollBack();
  838. util::fail('支付失败');
  839. }
  840. util::success($info->attributes);
  841. }
  842. //采购记录 ssh 2021.1.24
  843. public function actionList()
  844. {
  845. $get = Yii::$app->request->get();
  846. $where = ['shopId' => $this->shopId];
  847. $searchTime = $get['searchTime'] ?? '';
  848. if (!empty($searchTime)) {
  849. $startTime = $get['startTime'] ?? '';
  850. $endTime = $get['endTime'] ?? '';
  851. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  852. $where['payTime'] = ['between', [$period['startTime'], $period['endTime']]];
  853. }
  854. $ghsId = $get['ghsId'] ?? 0;
  855. if (!empty($ghsId)) {
  856. $where['ghsId'] = $ghsId;
  857. }
  858. $status = $get['status'] ?? 0;
  859. if (!empty($status)) {
  860. $where['status'] = $status;
  861. }
  862. $respond = PurchaseService::getPurchaseList($where);
  863. $respond['shop'] = $this->shop->attributes;
  864. util::success($respond);
  865. }
  866. //采购详情 ssh 2021.01.25
  867. public function actionDetail()
  868. {
  869. $get = Yii::$app->request->get();
  870. $id = $get['id'] ?? 0;
  871. $info = PurchaseService::getInfo($id, true, true);
  872. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  873. //不能注释,分享付款功能要用,客户没有登录也能付款
  874. //util::fail('无法访问');
  875. }
  876. $customId = $info['customId'] ?? 0;
  877. $custom = CustomClass::getById($customId);
  878. $customName = $custom['name'] ?? '';
  879. $customMobile = $custom['mobile'] ?? '';
  880. $info['customName'] = $customName;
  881. $info['customMobile'] = $customMobile;
  882. $hasDebtPay = $custom['debt'] ?? 0;
  883. $book = $info['book'] ?? 0;
  884. $presell = $info['presell'] ?? 0;
  885. //预售不能记欠款
  886. if ($book == 1) {
  887. $hasDebtPay = 0;
  888. }
  889. if ($presell == 1) {
  890. $hasDebtPay = 0;
  891. }
  892. $shop = $this->shop;
  893. $info['pfShopId'] = $shop->pfShopId ?? 0;
  894. $info['hasDebtPay'] = $hasDebtPay;
  895. $info['balance'] = $shop->balance ?? 0;
  896. $getPayType = dict::getDict('getPayType');
  897. $info['getPayType'] = $getPayType;
  898. //获取售后条件
  899. $ghsId = $info['ghsId'] ?? 0;
  900. $ghsInfo = GhsClass::getById($ghsId, true);
  901. if (empty($ghsInfo)) {
  902. util::fail('没有找到供货商');
  903. }
  904. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  905. //重要,客户看到的电话修改成门店的联系电话
  906. $ghsShopId = $ghsInfo->shopId ?? 0;
  907. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  908. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  909. $info['ghsMobile2'] = $ghsShopInfo->telephone2 ?? '';
  910. $info['hasRenew'] = 1;
  911. util::success($info);
  912. }
  913. //可用的支付方式 ssh 2021.1.25
  914. public function actionPayWay()
  915. {
  916. $button = [
  917. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  918. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  919. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  920. ];
  921. util::success(['button' => $button]);
  922. }
  923. //待结款明细 ssh 2021.1.26
  924. public function actionDebtList()
  925. {
  926. $get = Yii::$app->request->get();
  927. $id = $get['id'] ?? 0;
  928. $info = GhsClass::getGhsInfo($id);
  929. GhsClass::valid($info, $this->shopId);
  930. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  931. $searchTime = $get['searchTime'] ?? '';
  932. if (!empty($searchTime)) {
  933. $startTime = $get['startTime'] ?? '';
  934. $endTime = $get['endTime'] ?? '';
  935. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  936. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  937. }
  938. $respond = PurchaseService::getDebtList($where);
  939. util::success($respond);
  940. }
  941. //支付宝支付 ssh 2021.4.11
  942. public function actionAliPay()
  943. {
  944. ini_set('date.timezone', 'Asia/Shanghai');
  945. $post = Yii::$app->request->post();
  946. //默认是支付宝支付
  947. $payWay = $post['payWay'] ?? 1;
  948. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  949. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  950. if (empty($order)) {
  951. util::fail('订单号无效');
  952. }
  953. if ($order->status == 5) {
  954. util::fail('订单已取消');
  955. }
  956. if ($order->status != 1) {
  957. util::fail('订单不是待付款状态');
  958. }
  959. $deadline = $order->deadline;
  960. $current = time();
  961. if (($current + 50) > strtotime($deadline)) {
  962. util::fail('订单已失效,请重新下单');
  963. }
  964. $ghsId = $order->ghsId ?? 0;
  965. $ghs = GhsClass::getById($ghsId, true);
  966. if (empty($ghs)) {
  967. util::fail('没有供货商信息');
  968. }
  969. $ghsShopId = $ghs->shopId ?? 0;
  970. $ghsShop = ShopClass::getById($ghsShopId, true);
  971. if (empty($ghsShop)) {
  972. util::fail('没有供货商门店信息');
  973. }
  974. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  975. util::fail('商家支付功能未开通');
  976. }
  977. //避免重复提交
  978. util::checkRepeatCommit($orderSn, 8);
  979. //已经唤起过支付了,根据拉卡拉的规则,需要生成新的订单号
  980. if ($order->changePrice == 2) {
  981. $oldOrderSn = $orderSn;
  982. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  983. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  984. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  985. $params = [
  986. 'appid' => 'OP00002119',
  987. 'serial_no' => '018b08cfddbd',
  988. 'merchant_no' => $ghsShop->lklSjNo,
  989. 'term_no' => $ghsShop->lklScanTermNo,
  990. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  991. 'lklCertificatePath' => $lklCertificatePath,
  992. ];
  993. $laResource = new Lakala($params);
  994. $closeParams = [
  995. 'orderSn' => $oldOrderSn,
  996. ];
  997. $response = $laResource->cashClose($closeParams);
  998. if (isset($response['code']) == false || $response['code'] != '000000') {
  999. $msg = $response['msg'] ?? '';
  1000. $code = $response['code'] ?? 0;
  1001. if (!in_array($code, ['000091'])) {
  1002. noticeUtil::push('注意花店买花更换单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  1003. }
  1004. }
  1005. $shopId = $order->shopId ?? 0;
  1006. $mainId = $order->mainId ?? 0;
  1007. $customId = $order->customId ?? 0;
  1008. $ghsId = $order->ghsId ?? 0;
  1009. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1010. $newOrderSn = orderSn::getPurchaseSn($snData);
  1011. $orderSn = $newOrderSn;
  1012. $order->orderSn = $newOrderSn;
  1013. $order->save();
  1014. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1015. }
  1016. $order->changePrice = 2;
  1017. $order->save();
  1018. $ghsShopName = $ghsShop->shopName ?? '';
  1019. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1020. $name = $ghsShopName . " 花材 " . $orderSn;
  1021. $totalFee = $order->realPrice;
  1022. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1023. $ghsId = $order->ghsId ?? 0;
  1024. $ghs = GhsClass::getById($ghsId, true);
  1025. if (empty($ghs)) {
  1026. util::fail('没有供货商信息');
  1027. }
  1028. $ghsShopId = $ghs->shopId ?? 0;
  1029. $ghsShop = ShopClass::getById($ghsShopId, true);
  1030. if (empty($ghsShop)) {
  1031. util::fail('没有供货商门店信息');
  1032. }
  1033. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1034. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1035. $params = [
  1036. 'appid' => 'OP00002119',
  1037. 'serial_no' => '018b08cfddbd',
  1038. 'merchant_no' => $ghsShop->lklSjNo,
  1039. 'term_no' => $ghsShop->lklB2BTermNo,
  1040. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1041. 'lklCertificatePath' => $lklCertificatePath,
  1042. ];
  1043. $laResource = new Lakala($params);
  1044. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  1045. $wxParams = [
  1046. 'orderSn' => $orderSn,
  1047. 'amount' => $totalFee,
  1048. 'capitalType' => $purchaseCapital,
  1049. 'notifyUrl' => $notifyUrl,
  1050. 'subject' => $name,
  1051. 'payWay' => $payWay,
  1052. ];
  1053. $response = $laResource->cashierPay($wxParams);
  1054. if (isset($response['code']) == false || $response['code'] != '000000') {
  1055. $errMsg = $response['msg'] ?? '';
  1056. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  1057. util::fail('发起支付失败');
  1058. }
  1059. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  1060. $hasRenew = $ghsShop->hasRenew ?? 0;
  1061. $new = [
  1062. 'payUrl' => $counter_url,
  1063. 'orderSn' => $orderSn,
  1064. 'hasRenew' => 1,
  1065. ];
  1066. util::success($new);
  1067. }
  1068. //微信支付 ssh 2021.4.11
  1069. public function actionWxPay()
  1070. {
  1071. ini_set('date.timezone', 'Asia/Shanghai');
  1072. $post = Yii::$app->request->post();
  1073. $couponId = $post['couponId'] ?? 0;
  1074. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1075. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1076. if (empty($order)) {
  1077. util::fail('订单号无效');
  1078. }
  1079. if ($order->status == 5) {
  1080. util::fail('订单已取消');
  1081. }
  1082. if ($order->status != 1) {
  1083. util::fail('订单不是待付款状态');
  1084. }
  1085. $deadline = $order->deadline;
  1086. $current = time();
  1087. if (($current + 50) > strtotime($deadline)) {
  1088. util::fail('订单已失效,请重新下单');
  1089. }
  1090. //避免重复提交
  1091. util::checkRepeatCommit($orderSn, 8);
  1092. $ghsId = $order->ghsId ?? 0;
  1093. $ghs = GhsClass::getById($ghsId, true);
  1094. if (empty($ghs)) {
  1095. util::fail('没有供货商信息');
  1096. }
  1097. $ghsShopId = $ghs->shopId ?? 0;
  1098. $ghsShop = ShopClass::getById($ghsShopId, true);
  1099. if (empty($ghsShop)) {
  1100. util::fail('没有供货商门店信息');
  1101. }
  1102. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1103. util::fail('商家支付功能未开通');
  1104. }
  1105. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  1106. if ($order->changePrice == 2) {
  1107. $oldOrderSn = $orderSn;
  1108. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1109. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1110. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1111. $params = [
  1112. 'appid' => 'OP00002119',
  1113. 'serial_no' => '018b08cfddbd',
  1114. 'merchant_no' => $ghsShop->lklSjNo,
  1115. 'term_no' => $ghsShop->lklScanTermNo,
  1116. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1117. 'lklCertificatePath' => $lklCertificatePath,
  1118. ];
  1119. $laResource = new Lakala($params);
  1120. $closeParams = [
  1121. 'orderSn' => $oldOrderSn,
  1122. ];
  1123. $response = $laResource->close($closeParams);
  1124. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  1125. $msg = $response['msg'] ?? '';
  1126. $code = $response['code'] ?? 0;
  1127. if (!in_array($code, ['BBS11114'])) {
  1128. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . $code, '15280215347');
  1129. util::fail('出错了,请重新下单');
  1130. }
  1131. }
  1132. $shopId = $order->shopId ?? 0;
  1133. $mainId = $order->mainId ?? 0;
  1134. $customId = $order->customId ?? 0;
  1135. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1136. $newOrderSn = orderSn::getPurchaseSn($snData);
  1137. $orderSn = $newOrderSn;
  1138. $order->orderSn = $newOrderSn;
  1139. $order->save();
  1140. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1141. }
  1142. $id = $order->id;
  1143. $order->changePrice = 2;
  1144. $order->save();
  1145. $ghsShopName = $ghsShop->shopName ?? '';
  1146. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1147. $name = $ghsShopName . " 花材 " . $orderSn;
  1148. $totalFee = $order->realPrice;
  1149. $openId = '';
  1150. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  1151. $openId = $post['currentMiniOpenId'];
  1152. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  1153. }
  1154. if (empty($openId)) {
  1155. if (isset($this->admin) && !empty($this->admin)) {
  1156. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  1157. $openId = $this->admin->miniOpenId;
  1158. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  1159. }
  1160. }
  1161. }
  1162. if (empty($openId)) {
  1163. $currentShopId = $order->shopId ?? 0;
  1164. $currentShop = ShopClass::getById($currentShopId, true);
  1165. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  1166. $currentMobile = $currentShop->mobile ?? '';
  1167. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  1168. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  1169. }
  1170. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1171. //花卉宝代为申请的微信支付
  1172. $merchantExtend = WxOpenClass::getWxInfo();
  1173. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1174. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1175. $params = [
  1176. 'appid' => 'OP00002119',
  1177. 'serial_no' => '018b08cfddbd',
  1178. 'merchant_no' => $ghsShop->lklSjNo,
  1179. 'term_no' => $ghsShop->lklScanTermNo,
  1180. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1181. 'lklCertificatePath' => $lklCertificatePath,
  1182. ];
  1183. $laResource = new Lakala($params);
  1184. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  1185. $wxParams = [
  1186. 'orderSn' => $orderSn,
  1187. 'amount' => $totalFee,
  1188. 'capitalType' => $purchaseCapital,
  1189. 'notifyUrl' => $notifyUrl,
  1190. 'wxAppId' => $merchantExtend['miniAppId'],
  1191. 'openId' => $openId,
  1192. 'subject' => $name,
  1193. 'couponId' => $couponId,
  1194. ];
  1195. $response = $laResource->driveWxPay($wxParams);
  1196. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  1197. $errMsg = $response['msg'] ?? '';
  1198. noticeUtil::push('编号129680931:' . $errMsg, '15280215347');
  1199. util::fail('支付失败');
  1200. }
  1201. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1202. $appId = $newParams['app_id'] ?? '';
  1203. $nonceStr = $newParams['nonce_str'] ?? '';
  1204. $paySign = $newParams['pay_sign'] ?? '';
  1205. $package = $newParams['package'] ?? '';
  1206. $signType = $newParams['sign_type'] ?? '';
  1207. $timeStamp = $newParams['time_stamp'] ?? '';
  1208. $new = [
  1209. 'id' => $id,
  1210. 'appId' => $appId,
  1211. 'nonceStr' => $nonceStr,
  1212. 'paySign' => $paySign,
  1213. 'package' => $package,
  1214. 'signType' => $signType,
  1215. 'timeStamp' => $timeStamp,
  1216. 'orderSn' => $orderSn,
  1217. ];
  1218. util::success($new);
  1219. }
  1220. //计算运费 ssh 20221003
  1221. public function actionFreight()
  1222. {
  1223. $get = Yii::$app->request->get();
  1224. $shop = $this->shop;
  1225. $ghsId = $get['ghsId'] ?? 0;
  1226. $ghs = GhsClass::getById($ghsId, true);
  1227. if (empty($ghs)) {
  1228. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1229. }
  1230. $ghsShopId = $ghs->shopId ?? 0;
  1231. $ghsShop = ShopClass::getById($ghsShopId, true);
  1232. if (empty($ghsShop)) {
  1233. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1234. }
  1235. $customId = $ghs->customId ?? 0;
  1236. $custom = CustomClass::getById($customId, true);
  1237. if (empty($custom)) {
  1238. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1239. }
  1240. $weight = $get['weight'] ?? 0;
  1241. if (empty($weight)) {
  1242. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1243. }
  1244. $respond = PurchaseClass::getDistanceFee($shop, $ghsShop, $weight, $custom);
  1245. util::success($respond);
  1246. }
  1247. //运费计算 lqh 2021.4.12 暂时返回固定值
  1248. public function actionFreight2()
  1249. {
  1250. //lqh 2021.6.25 运费固定返回0
  1251. util::success(['sedCost' => 0]);
  1252. }
  1253. }