PurchaseController.php 58 KB

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