PurchaseController.php 58 KB

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