PurchaseController.php 55 KB

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