PurchaseController.php 59 KB

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