PurchaseController.php 58 KB

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