PurchaseController.php 61 KB

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