PurchaseController.php 56 KB

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