PurchaseController.php 61 KB

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