PurchaseController.php 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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\AccountMoneyClass;
  9. use bizGhs\custom\classes\CustomClass;
  10. use bizGhs\express\classes\GhsDeliveryOrderClass;
  11. use bizGhs\order\classes\OrderClass;
  12. use bizGhs\order\classes\ShMethodClass;
  13. use bizHd\cg\classes\CgMergeSnClass;
  14. use bizHd\purchase\classes\PurchaseClass;
  15. use bizHd\purchase\classes\PurchaseItemClass;
  16. use bizHd\purchase\services\PurchaseService;
  17. use bizGhs\product\classes\ProductClass;
  18. use bizHd\wx\classes\WxOpenClass;
  19. use common\components\dateUtil;
  20. use common\components\delivery\util\DeliveryQuoteUtil;
  21. use common\components\dict;
  22. use common\components\imgUtil;
  23. use common\components\noticeUtil;
  24. use common\components\orderSn;
  25. use Yii;
  26. use common\components\util;
  27. use bizGhs\order\services\OrderService;
  28. use common\components\lakala\Lakala;
  29. use common\components\qrCodeUtil;
  30. use common\components\push;
  31. class PurchaseController extends BaseController
  32. {
  33. public $guestAccess = ['detail', 'ali-pay', 'get-ali-pay-code', 'wx-pay', 'merge-order-list', 'debt-list'];
  34. //取消采购单 ssh 20250710
  35. public function actionCancel()
  36. {
  37. $get = Yii::$app->request->get();
  38. $id = $get['id'] ?? '';
  39. $info = PurchaseClass::getById($id, true);
  40. if (empty($info)) {
  41. util::fail('没有找到采购单');
  42. }
  43. if (!isset($info->mainId) || $info->mainId != $this->mainId) {
  44. util::fail('访问错误');
  45. }
  46. if ($info->status == 5) {
  47. util::fail('已取消');
  48. }
  49. if ($info->status != 1) {
  50. util::fail('不能取消');
  51. }
  52. PurchaseService::expire($info);
  53. util::complete('已取消');
  54. }
  55. //获取支付宝付款码 ssh 20240713
  56. public function actionGetAliPayCode()
  57. {
  58. $get = Yii::$app->request->get();
  59. $orderSn = $get['orderSn'] ?? '';
  60. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  61. if (empty($info)) {
  62. util::fail('没有找到采购单');
  63. }
  64. if (!isset($info->mainId) || $info->mainId != $this->mainId) {
  65. //重要,这个不能限制,因为开单分享给客户付款的,要显示支付宝二维码,限制了会不显示!!!!!! shish 20260326
  66. //util::fail('无法访问!');
  67. }
  68. if (getenv('YII_ENV') == 'production') {
  69. $url = "https://mall.huahb.cn/#/admin/cg/alipay?orderSn={$orderSn}";
  70. } else {
  71. $url = "https://mall.huaml.com/#/admin/cg/alipay?orderSn={$orderSn}";
  72. }
  73. $unique = "order_ali_pay_" . $orderSn;
  74. $imgUrl = qrCodeUtil::generateRechargeQrCode($url, $unique);
  75. $imageUrl = imgUtil::groupImg($imgUrl);
  76. util::success(['imgUrl' => $imageUrl]);
  77. }
  78. //确认收货 ssh 20231119
  79. public function actionConfirmTake()
  80. {
  81. $get = Yii::$app->request->get();
  82. $id = $get['id'] ?? 0;
  83. $info = PurchaseClass::getById($id, true);
  84. if (empty($info)) {
  85. util::fail('没有找到采购单');
  86. }
  87. if (isset($info->mainId) == false || $info->mainId != $this->mainId) {
  88. util::fail('无法访问!!');
  89. }
  90. PurchaseClass::confirmTake($info);
  91. util::complete('确认成功');
  92. }
  93. //这个接口只能用于
  94. public function actionGetFullInfo()
  95. {
  96. $get = Yii::$app->request->get();
  97. $id = $get['id'] ?? 0;
  98. $info = PurchaseClass::getById($id);
  99. if (empty($info)) {
  100. util::fail('没有找到采购单');
  101. }
  102. if (!isset($info['mainId']) || $info['mainId'] != $this->mainId) {
  103. util::fail('无法访问。。。');
  104. }
  105. $orderSn = $info['orderSn'] ?? '';
  106. $ghsShopId = $info['ghsShopId'] ?? 0;
  107. if (empty($ghsShopId)) {
  108. $ghsId = $info['ghsId'] ?? 0;
  109. if (!empty($ghsId)) {
  110. $ghs = GhsClass::getById($ghsId, true);
  111. $ghsShopId = $ghs->shopId ?? 0;
  112. PurchaseClass::updateById($id, ['ghsShopId' => $ghsShopId]);
  113. }
  114. }
  115. //申请售后 0禁止 1允许
  116. $afterSale = 1;
  117. //下单后多少小时内可以售后,0不限制
  118. $saleHour = 12;
  119. //是否可以仅退款 1支持 0不支持
  120. $refundOnlyPrice = 1;
  121. //售后是否要求上传照片 0不要求 1必须
  122. $refundMustImg = 0;
  123. if (!empty($ghsShopId)) {
  124. $shop = ShopClass::getByCondition(['id' => $ghsShopId], false, false, 'id,afterSale,saleHour,refundOnlyPrice,refundMustImg');
  125. $afterSale = $shop['afterSale'] ?? 1;
  126. $saleHour = $shop['saleHour'] ?? 12;
  127. $refundOnlyPrice = $shop['refundOnlyPrice'] ?? 1;
  128. $refundMustImg = $shop['refundMustImg'] ?? 0;
  129. }
  130. //售后规则
  131. $info['afterSaleRule'] = ['afterSale' => $afterSale, 'saleHour' => $saleHour, 'refundOnlyPrice' => $refundOnlyPrice, 'refundMustImg' => $refundMustImg];
  132. $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  133. if (!empty($list)) {
  134. foreach ($list as $key => $item) {
  135. $shortCover = $item['cover'] ?? '';
  136. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  137. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  138. $list[$key]['cover'] = $cover;
  139. $list[$key]['bigCover'] = $bigCover;
  140. $list[$key]['shortCover'] = $shortCover;
  141. }
  142. }
  143. $info['product'] = $list;
  144. util::success($info);
  145. }
  146. //虚拟供货商的采购 ssh 20230308
  147. public function actionAddVirtualCg()
  148. {
  149. $post = Yii::$app->request->post();
  150. $ghsId = $post['ghsId'] ?? 0;
  151. $payMode = $post['payMode'] ?? 0;
  152. $payWay = $post['payWay'] ?? 0;
  153. $ghsInfo = GhsClass::getById($ghsId);
  154. if (empty($ghsInfo)) {
  155. util::fail('没有找到供货商');
  156. }
  157. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  158. util::fail('不能向自己的店买花');
  159. }
  160. $connection = Yii::$app->db;
  161. $transaction = $connection->beginTransaction();
  162. $transactionFinished = false;
  163. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  164. if ($transactionFinished) {
  165. return;
  166. }
  167. ProductClass::rollbackLimitBuySnapshot();
  168. if ($transaction->isActive) {
  169. $transaction->rollBack();
  170. }
  171. });
  172. try {
  173. $post['book'] = $post['book'] ?? 0;
  174. $post['sjId'] = $this->sjId;
  175. $post['shopId'] = $this->shopId;
  176. $post['shopAdminId'] = $this->shopAdminId;
  177. $post['ghsId'] = $ghsId;
  178. $post['ghsName'] = $ghsInfo['name'] ?? '';
  179. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  180. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  181. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  182. $customId = $ghsInfo['customId'] ?? 0;
  183. $post['customId'] = $customId;
  184. $shopAdmin = $this->shopAdmin ?? null;
  185. $name = $shopAdmin->name ?? '';
  186. $post['shopAdminName'] = $name;
  187. $post['mainId'] = $this->mainId;
  188. $product = $post['product'] ?? '';
  189. $productList = json_decode($product, true);
  190. //判断product数据是不是同个供货商的
  191. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  192. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  193. if (!empty($productInfoList)) {
  194. foreach ($productInfoList as $currentInfo) {
  195. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  196. util::fail('只能选一家的商品,请清空重选。');
  197. }
  198. }
  199. if (count($productInfoList) != count($ids)) {
  200. util::fail('存在无效商品');
  201. }
  202. } else {
  203. util::fail('花材不存在');
  204. }
  205. //供货商预订单最低公斤数要求和每公斤服务费
  206. $ghsInfo['kiloFee'] = 0;
  207. $ghsInfo['miniKilo'] = 0;
  208. $post['product'] = $productList;
  209. $packCost = 0;
  210. $post['packCost'] = $packCost;
  211. $post['sendCost'] = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  212. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  213. if ($payMode == 0) {
  214. //欠款
  215. PurchaseService::debt($respond);
  216. } else {
  217. //线下支付
  218. PurchaseService::payAfter($respond, $payWay);
  219. $saleId = $respond->saleId ?? 0;
  220. $order = OrderClass::getById($saleId, true);
  221. if (empty($order)) {
  222. util::fail('没有找到订单');
  223. }
  224. OrderService::payAfter($order, $payWay, true);
  225. }
  226. $transaction->commit();
  227. $transactionFinished = true;
  228. ProductClass::clearLimitBuyRollbackSnapshot();
  229. util::success($respond);
  230. } catch (\Throwable $e) {
  231. if ($transaction->isActive) {
  232. $transaction->rollBack();
  233. }
  234. ProductClass::rollbackLimitBuySnapshot();
  235. $transactionFinished = true;
  236. util::fail();
  237. }
  238. }
  239. // 批发商的限购花材信息
  240. public function actionLimitBuyInfo()
  241. {
  242. $post = Yii::$app->request->post();
  243. $ghsId = $post['ghsId'] ?? 0;
  244. $list = $post['list'] ?? [];
  245. if (empty($ghsId)) {
  246. util::fail('缺少供货商');
  247. }
  248. if (empty($list) || !is_array($list)) {
  249. util::fail('请选择花材');
  250. }
  251. $ghsInfo = GhsClass::getById($ghsId);
  252. if (empty($ghsInfo)) {
  253. util::fail('没有找到供货商');
  254. }
  255. $customId = $ghsInfo['customId'] ?? 0;
  256. if (empty($customId)) {
  257. util::fail('没有找到客户');
  258. }
  259. $respond = ProductClass::getLimitBuyInfoByList($list, $customId);
  260. util::success($respond);
  261. }
  262. //生成采购单 ssh 2021.1.17
  263. public function actionCreateOrder()
  264. {
  265. $post = Yii::$app->request->post();
  266. $version = $post['version'] ?? 0;
  267. $direct = $post['direct'] ?? 0;
  268. //避免重复提交
  269. $adminId = $this->adminId;
  270. util::checkRepeatCommit($adminId, 3);
  271. //多颜色数据整理
  272. $colorItem = $post['xj'] ?? [];
  273. $newXj = [];
  274. if (!empty($colorItem)) {
  275. $colorData = json_decode($colorItem, true);
  276. if (!empty($colorData) && is_array($colorData)) {
  277. foreach ($colorData as $colorInfo) {
  278. $ptItemId = $colorInfo['ptItemId'] ?? 0;
  279. $colorList = $colorInfo['list'] ?? [];
  280. if (!empty($colorList)) {
  281. foreach ($colorList as $colorItemKey => $colorItem) {
  282. $newXj[$ptItemId][] = $colorItem;
  283. }
  284. }
  285. }
  286. }
  287. }
  288. $post['xj'] = $newXj;
  289. $ghsId = $post['ghsId'] ?? 0;
  290. //供货商
  291. $ghsInfo = GhsClass::getById($ghsId);
  292. if (empty($ghsInfo)) {
  293. util::fail('没有找到供货商');
  294. }
  295. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  296. if (!empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  297. util::fail('不能跟自己的店买花');
  298. }
  299. $connection = Yii::$app->db;
  300. $transaction = $connection->beginTransaction();
  301. $transactionFinished = false;
  302. register_shutdown_function(function () use (&$transactionFinished, $transaction) {
  303. if ($transactionFinished) {
  304. return;
  305. }
  306. ProductClass::rollbackLimitBuySnapshot();
  307. if ($transaction->isActive) {
  308. $transaction->rollBack();
  309. }
  310. });
  311. try {
  312. $post['book'] = $post['book'] ?? 0;
  313. $post['sjId'] = $this->sjId;
  314. $post['shopId'] = $this->shopId;
  315. $post['shopAdminId'] = $this->shopAdminId;
  316. $post['ghsId'] = $ghsId;
  317. $post['ghsName'] = $ghsInfo['name'] ?? '';
  318. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  319. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  320. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  321. $customId = $ghsInfo['customId'] ?? 0;
  322. $custom = CustomClass::getById($customId, true);
  323. if (empty($custom)) {
  324. util::fail('用户信息缺失');
  325. }
  326. $post['customId'] = $customId;
  327. $shopAdmin = $this->shopAdmin ?? null;
  328. $name = $shopAdmin->name ?? '';
  329. $post['shopAdminName'] = $name;
  330. $post['mainId'] = $this->mainId;
  331. $product = $post['product'] ?? '';
  332. $productList = json_decode($product, true);
  333. //判断product数据是不是同个供货商的
  334. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  335. $ghsMainId = $ghsShopInfo->mainId ?? 0;
  336. $isOpen = ShopClass::isOpen($ghsShopInfo);
  337. $book = $post['book'] ?? 0;
  338. if ($isOpen == 0 && $book == 0) {
  339. util::fail('本店已休息,请选择其它门店');
  340. }
  341. if (isset($post['transType']) && $post['transType'] == 4) {
  342. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  343. $notSameCity = PurchaseClass::notSameCity($this->shop, $ghsShopInfo);
  344. if (!$notSameCity) {
  345. util::fail('距离太远,不能选择同城配送');
  346. }
  347. }
  348. }
  349. if ($book == 1) {
  350. $sendTimeWant = isset($post['sendTimeWant']) && !empty($post['sendTimeWant']) ? $post['sendTimeWant'] : date("Y-m-d");
  351. $future = date("Y-m-d", strtotime('+2 day'));
  352. if (strtotime($sendTimeWant) < strtotime($future)) {
  353. //util::fail('请选择时间');
  354. }
  355. $nowTime = strtotime(date("Y-m-d"));
  356. if (strtotime($sendTimeWant) < $nowTime) {
  357. util::fail('不能选择过去时间');
  358. }
  359. }
  360. //收集客户要下单的花材数量
  361. $orderNum = [];
  362. $productCount = 0;
  363. foreach ($productList as $key => $product) {
  364. $bigNum = $product['bigNum'] ?? 0;
  365. $smallNum = $product['smallNum'] ?? 0;
  366. $pName = $product['name'] ?? '';
  367. if ($smallNum > 0) {
  368. util::fail($pName . '不能选小单位');
  369. }
  370. $productId = $product['productId'] ?? 0;
  371. $orderNum[$productId] = ['num' => $bigNum];
  372. $productCount += $bigNum;
  373. }
  374. $lackList = [];
  375. $changeList = [];
  376. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  377. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  378. if (!empty($productInfoList)) {
  379. $presellData = [];
  380. $nowTime = strtotime(date("Y-m-d"));
  381. foreach ($productInfoList as $currentInfo) {
  382. if (!isset($currentInfo['mainId']) || $currentInfo['mainId'] != $ghsMainId) {
  383. util::fail('只能选一家的商品,请清空重新选。');
  384. }
  385. $currentName = $currentInfo['name'] ?? '';
  386. if (isset($currentInfo['frontHide']) && $currentInfo['frontHide'] == 1) {
  387. //珑松珠海店,隐藏的商品,分享出去了也要能下单。644 是测试账号的mainId
  388. if (isset($currentInfo['mainId']) && in_array($currentInfo['mainId'], [14116, 644])) {
  389. //无操作
  390. } else {
  391. util::fail($currentName . ' 没有库存,请删除');
  392. }
  393. }
  394. $presell = $currentInfo['presell'] ?? 0;
  395. $presellData[] = $presell;
  396. if ($presell == 1) {
  397. if ($book == 1) {
  398. util::fail('预订时不能选择预售花材');
  399. } else {
  400. if (empty($post['sendTimeWant'])) {
  401. util::fail('请选择配送日期');
  402. }
  403. $sendTimeWant = $post['sendTimeWant'];
  404. if (strtotime($sendTimeWant) < $nowTime) {
  405. util::fail('不能选择过去时间');
  406. }
  407. $hasDateStr = $currentInfo['presellDate'] ?? [];
  408. $hasDate = explode(',', trim($hasDateStr));
  409. if (empty($hasDate)) {
  410. util::fail('预售日期有问题,请提醒门店');
  411. }
  412. if (!in_array(strtotime($sendTimeWant), $hasDate)) {
  413. util::fail("有预售花材在{$sendTimeWant}没到货");
  414. }
  415. $post['presell'] = 1;
  416. }
  417. }
  418. if ($version >= 10) {
  419. $stock = $currentInfo['stock'] ?? 0;
  420. $stock = floatval($stock);
  421. //会有0.5扎和2.5扎问题,所以要转成整数,不然会出严重问题 ssh 20250503
  422. $stock = intval($stock);
  423. $productId = $currentInfo['id'] ?? 0;
  424. $productName = $currentInfo['name'] ?? '';
  425. $num = $orderNum[$productId] && $orderNum[$productId]['num'] ? $orderNum[$productId]['num'] : 0;
  426. $num = floatval($num);
  427. if ($num > $stock) {
  428. $lackList[] = ['name' => $productName, 'stock' => $stock];
  429. $changeList[$productId] = $stock;
  430. }
  431. }
  432. }
  433. if (count($productInfoList) != count($ids)) {
  434. util::fail('存在无效商品');
  435. }
  436. $presellData = array_unique($presellData);
  437. if (count($presellData) > 1) {
  438. util::fail('预售和非预售花材不能一起下单');
  439. }
  440. if ($version >= 10) {
  441. if ($direct == 1) {
  442. //直接更换库存并提交
  443. $allNoStock = true;
  444. foreach ($productList as $pKey => $pVal) {
  445. $productId = $pVal['productId'] ?? 0;
  446. if (isset($changeList[$productId])) {
  447. $changeStock = $changeList[$productId];
  448. $productList[$pKey]['bigNum'] = $changeStock;
  449. }
  450. if ($productList[$pKey]['bigNum'] > 0) {
  451. $allNoStock = false;
  452. }
  453. if ($productList[$pKey]['bigNum'] <= 0) {
  454. unset($productList[$pKey]);
  455. }
  456. }
  457. if ($allNoStock) {
  458. util::fail('全部没库存了');
  459. }
  460. } else {
  461. //前台提示库存不足
  462. if (!empty($lackList)) {
  463. util::success(['lackList' => $lackList, 'hasLackError' => 1]);
  464. }
  465. }
  466. }
  467. } else {
  468. util::fail('花材不存在');
  469. }
  470. //供货商预订单最低公斤数要求和每公斤服务费
  471. $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
  472. $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
  473. $post['product'] = $productList;
  474. // xhShMethod 门店下单前保留花材合计,供后续 packCost 计算(pfLevel==0 分支会 unset itemTotalAmount)
  475. $shMethodItemAmount = $post['itemTotalAmount'] ?? 0;
  476. $sendType = $post['sendType'] ?? 0;
  477. $transType = $post['transType'] ?? 0;
  478. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  479. $shopExt = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,thirdSend,hcMap,hcFreeKm');
  480. if ($shopExt->thirdSend == 1) {
  481. util::fail('本店暂不能使用跑腿哦');
  482. }
  483. }
  484. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  485. //昆明发货包装费和运费的计算
  486. $totalWeight = 0;
  487. $totalItemNum = 0;
  488. foreach ($productList as $itemData) {
  489. $bigNum = $itemData['bigNum'] ?? 0;
  490. $thisWeight = $itemData['weight'] ?? 0;
  491. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  492. $totalWeight = bcadd($currentWeight, $totalWeight, 2);
  493. $totalItemNum = bcadd($totalItemNum, $bigNum, 2);
  494. }
  495. $totalItemNum = floatval($totalItemNum);
  496. $kmPackCost = PurchaseClass::getKmPackCost($ghsMainId);
  497. $packCost = 0;
  498. if (!empty($kmPackCost)) {
  499. foreach ($kmPackCost as $itemPack) {
  500. $kiloNum = $itemPack['num'] ?? 0;
  501. if ($kiloNum >= $totalWeight) {
  502. $packCost = $itemPack['amount'] ?? 0;
  503. break;
  504. }
  505. }
  506. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  507. noticeUtil::push("总重量" . $totalWeight, '15280215347');
  508. }
  509. }
  510. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  511. noticeUtil::push("走的pfLevel=1", '15280215347');
  512. }
  513. //同城配送和到店自取免打包费
  514. if ($transType == 4 || $transType == 5) {
  515. $packCost = 0;
  516. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  517. noticeUtil::push("没有计算打包费原因:transType = 4 =5", '15280215347');
  518. }
  519. }
  520. //如果已经算过一次打包费和运费了,则不再费了
  521. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $sendType);
  522. if (!$needAdd) {
  523. $packCost = 0;
  524. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  525. noticeUtil::push("没有计算打包费原因:已经算过一次了", '15280215347');
  526. }
  527. }
  528. $post['packCost'] = $packCost;
  529. $transCost = dict::getDict('transCost', null, null, $ghsShopInfo->mainId);
  530. $sendCost = 0;
  531. if (!empty($transCost)) {
  532. foreach ($transCost as $trans) {
  533. $sign = $trans['sign'] ?? 0;
  534. if ($sign == $transType) {
  535. if ($sign == 2) {
  536. //冷链物流
  537. $llOption = $trans['option'] ?? [];
  538. if (!empty($llOption)) {
  539. $largeInfo = end($llOption);
  540. $largeNum = $largeInfo['num'] ?? 0;
  541. $largeAmount = $largeInfo['amount'] ?? 0;
  542. $bs = intval($totalItemNum / $largeNum);
  543. if ($bs > 0) {
  544. $addSendCost = bcmul($bs, $largeAmount, 2);
  545. $sendCost = bcadd($sendCost, $addSendCost, 2);
  546. $subNum = bcmul($bs, $largeNum, 2);
  547. $subNum = floatval($subNum);
  548. $totalItemNum = bcsub($totalItemNum, $subNum, 2);
  549. $totalItemNum = floatval($totalItemNum);
  550. }
  551. foreach ($llOption as $miniItem) {
  552. if ($miniItem['num'] >= $totalItemNum) {
  553. $sendCost = bcadd($sendCost, $miniItem['amount'], 2);
  554. break;
  555. }
  556. }
  557. }
  558. } else {
  559. //其它物流
  560. $perKiloCost = $trans['perKiloCost'] ?? 0;
  561. $sendCost = bcmul($perKiloCost, $totalWeight, 3);
  562. $sendCost = round($sendCost, 2);
  563. }
  564. }
  565. }
  566. }
  567. //如果已经算过一次打包费和运费了,则不再费了
  568. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $sendType);
  569. if ($needAdd == false) {
  570. $sendCost = 0;
  571. }
  572. $post['sendCost'] = $sendCost;
  573. //标记为昆明到地方订单
  574. $post['localOrder'] = 1;
  575. } else {
  576. // xhShMethod 门店:下单前按配置写入不满最低消费时的附加包装费/运费,关键词 sh_method_area
  577. $newShMethod = dict::getNewMethodShop();
  578. if (in_array($ghsShopId, $newShMethod)) {
  579. $shMethodBigNum = 0;
  580. foreach ($productList as $itemData) {
  581. $shMethodBigNum += floatval($itemData['bigNum'] ?? 0);
  582. }
  583. $additionalCosts = ShMethodClass::calcFee(
  584. $ghsShopId,
  585. $ghsMainId,
  586. $sendType,
  587. $shMethodItemAmount,
  588. $shMethodBigNum,
  589. $customId,
  590. $ghsInfo
  591. );
  592. // 如果产生了附加运费,与前面可能产生的 sendCost 进行累加或覆盖
  593. if ($additionalCosts['sendCost'] > 0) {
  594. $post['sendCost'] = bcadd($post['sendCost'] ?? 0, $additionalCosts['sendCost'], 2);
  595. }
  596. // 如果产生了附加包装费,覆盖前面赋值的 packCost
  597. if ($additionalCosts['packCost'] > 0) {
  598. $post['packCost'] = $additionalCosts['packCost'];
  599. }
  600. }else{
  601. //同城发货包装费和运费的计算
  602. $sendCost = 0;
  603. $sendDistance = 0;
  604. //特别注意,这边取供货商的shop信息
  605. $mapSet = ShopClass::hasIntraCity($ghsShopInfo);
  606. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  607. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  608. if ($openIntraCity == 2) {
  609. util::fail('不能使用跑腿');
  610. }
  611. if ($openIntraCity == 1) {
  612. // 生成随机订单号,不要跟原来的混起来,跑腿-销售单-
  613. $prefix = 'PT-XSD-' . $this->mainId . '-';
  614. $orderSn = $prefix . round(microtime(true) * 1000);
  615. // 使用 DeliveryQuoteUtil 获取配送报价
  616. try {
  617. if (empty($post['deliveryPlatform'])) {
  618. util::fail('请选择跑腿');
  619. }
  620. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  621. 'productList' => $productList,
  622. 'deliveryPlatform' => $post['deliveryPlatform'],
  623. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  624. 'ghsInfo' => $ghsInfo,
  625. 'custom' => $custom,
  626. 'order' => [
  627. 'orderSn' => $orderSn,
  628. 'goodsType' => 1, //商品类型:0花束 1花材
  629. 'itemTotalAmount' => $post['itemTotalAmount'] ?? 0,
  630. 'remark' => $post['remark'] ?? '',
  631. ],
  632. 'mainId' => $this->mainId,
  633. 'productCount' => $productCount,
  634. ]);
  635. $sendCost = $quoteResult['sendCost'];
  636. $sendDistance = $quoteResult['sendDistance'];
  637. } catch (\Exception $e) {
  638. util::fail($e->getMessage());
  639. }
  640. }
  641. }
  642. $post['sendCost'] = $sendCost;
  643. $post['sendDistance'] = $sendDistance;
  644. // 出车(0)、发快递(4)、发物流可以算包装费
  645. $packCost = 0;
  646. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  647. noticeUtil::push("走的pfLevel=0", '15280215347');
  648. }
  649. $itemTotalAmount = $post['itemTotalAmount'] ?? 0;
  650. unset($post['itemTotalAmount']);
  651. // sendType == 4 要收包装费
  652. if ($sendType == 4) {
  653. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $sendType, $itemTotalAmount);
  654. if ($needAdd) {
  655. $packCost = $ghsShopInfo->packCost ?? 0;
  656. }
  657. }
  658. $post['packCost'] = $packCost;
  659. }
  660. }
  661. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  662. $newShMethod = dict::getNewMethodShop();
  663. if (in_array($ghsShopId, $newShMethod)) {
  664. // 使用 xhShMethod 配置校验配送限制,替代旧版门店硬编码规则,关键词 sh_method_area
  665. $shMethodError = ShMethodClass::checkLimit(
  666. $ghsShopId,
  667. $ghsMainId,
  668. $sendType,
  669. $respond->actPrice ?? 0,
  670. $respond->bigNum ?? 0,
  671. $post['packCost'] ?? 0,
  672. $post['sendCost'] ?? 0,
  673. $post['wlName'] ?? '',
  674. $customId,
  675. $ghsInfo
  676. );
  677. if ($shMethodError !== '') {
  678. $transaction->rollBack();
  679. util::fail($shMethodError);
  680. }
  681. } else {
  682. if ($sendType == 0) {
  683. if (getenv('YII_ENV') == 'production') {
  684. //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
  685. if ($ghsShopId == 4608) {
  686. if ($respond->actPrice < 500) {
  687. $transaction->rollBack();
  688. util::fail('满500元支持免费配送');
  689. }
  690. }
  691. //叶上花,满500元市区免费配送
  692. if ($ghsShopId == 4804) {
  693. if ($respond->actPrice < 500) {
  694. $transaction->rollBack();
  695. util::fail('满500元支持免费配送');
  696. }
  697. }
  698. //花悠星 国恋 勇记 万丽 陆丰暂时不支持免费配送
  699. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003 || $ghsShopId == 38128) {
  700. util::fail('暂不支持送货上门,请选其它配送方式!');
  701. }
  702. //广州鑫源花卉,选择送货上门必须选必选商品
  703. if ($ghsShopId == 12439) {
  704. $hasMust = array_intersect($ids, [3446945, 3446970, 3446973, 3446979, 3446995, 3447010, 3447023, 3459983, 4333184, 4317745, 1585738]);
  705. if (empty($hasMust)) {
  706. util::fail('请返回选择 必选商品');
  707. }
  708. }
  709. //情意花卉,必选品
  710. if ($ghsShopId == 100883) {
  711. $hasMust = array_intersect($ids, [5017957,5036067,5036089,5036099,5017958,5036063,5017948,5035980,5036073,5036075,5036076,5036117,5036118,5017949,5035979,5035976,5035928,5035930,5036061,5035879]);
  712. if (empty($hasMust)) {
  713. util::fail('请返回选择 必选商品');
  714. }
  715. }
  716. //三明易批花
  717. if ($ghsShopId == 82200) {
  718. if ($respond->actPrice < 100 || $respond->bigNum < 15) {
  719. $hasMust = array_intersect($ids, [4283478]);
  720. if (empty($hasMust)) {
  721. util::fail('没满100元且15扎,请返回选运费');
  722. }
  723. }
  724. // 按当天计时,把 sendTimeWant 要限制在两天的时间范围之内
  725. $future = date("Y-m-d", strtotime('+2 day'));
  726. if (strtotime($respond->sendTimeWant) > strtotime($future)) {
  727. util::fail('配送日期只能选最近二天');
  728. }
  729. }
  730. if ($ghsShopId == 77703) {
  731. //天天鲜花思明店,暂时关闭送货上门
  732. if ($respond->sendType == 0) {
  733. //util::fail('节日暂停送货上门,请选到店自取');
  734. }
  735. }
  736. if ($ghsShopId == 86726) {
  737. //藏花林
  738. if ($respond->transType != 4 && $respond->transType != 5) {
  739. util::fail('请选同城配送或到店自取');
  740. }
  741. }
  742. }
  743. if (isset($ghsInfo['home'])) {
  744. if ($ghsInfo['home'] == 0) {
  745. util::fail('暂不支持送货上门,请选其它配送方式');
  746. }
  747. $homeAmount = $ghsInfo['homeAmount'] ? floatval($ghsInfo['homeAmount']) : 0;
  748. $homeNum = $ghsInfo['homeNum'] ? floatval($ghsInfo['homeNum']) : 0;
  749. if ($homeAmount > 0 && $homeAmount > $respond->actPrice) {
  750. util::fail("满{$homeAmount}元 可送货上门");
  751. }
  752. if ($homeNum > 0 && $homeNum > $respond->bigNum) {
  753. util::fail("满{$homeNum}扎 可送货上门");
  754. }
  755. }
  756. }
  757. if ($sendType == 2) {
  758. //温州易批花,跑腿,不满100,要选10元的补运费商品
  759. if ($ghsShopId == 91115) {
  760. if ($respond->actPrice < 100) {
  761. $transaction->rollBack();
  762. $hasMust = array_intersect($ids, [4449812, 4449811]);
  763. if (empty($hasMust)) {
  764. util::fail('不满100,必选商品 补运费10元');
  765. }
  766. }
  767. }
  768. }
  769. if ($sendType == 4) {
  770. //温州易批花,快递,不满100,要选10元的补运费商品
  771. if ($ghsShopId == 91115) {
  772. if ($respond->actPrice < 100) {
  773. $transaction->rollBack();
  774. $hasMust = array_intersect($ids, [4449812, 4449811]);
  775. if (empty($hasMust)) {
  776. util::fail('不满100,必选商品 补运费10元');
  777. }
  778. }
  779. }
  780. }
  781. if ($sendType == 3) {
  782. //温州易批花,物流,不满200,要选10元的补运费商品
  783. if ($ghsShopId == 91115) {
  784. if ($respond->actPrice < 200) {
  785. $transaction->rollBack();
  786. $hasMust = array_intersect($ids, [4449812, 4449811]);
  787. if (empty($hasMust)) {
  788. util::fail('不满200,必选商品 补运费10元');
  789. }
  790. }
  791. }
  792. }
  793. }
  794. $transaction->commit();
  795. $transactionFinished = true;
  796. ProductClass::clearLimitBuyRollbackSnapshot();
  797. util::success($respond);
  798. } catch (\Exception $e) {
  799. if ($transaction->isActive) {
  800. $transaction->rollBack();
  801. }
  802. ProductClass::rollbackLimitBuySnapshot();
  803. $transactionFinished = true;
  804. Yii::error($e->getMessage());
  805. util::fail();
  806. }
  807. }
  808. /**
  809. * 赊账支付
  810. * 职责:处理采购单延期支付(记欠款/赊账),更新采购单状态,并触发后续的自动收货、通知、在线打印、跑腿呼叫等业务流
  811. * 入参:GET 传参 orderSn (采购单号)
  812. * 返回:JSON 格式的采购单属性
  813. * 副作用:更新采购单、订单状态,增减花材库存并记录库存流水,更新客户及供货商的最后下单时间,发送外部通知和打印请求
  814. * 关键边界:
  815. * 1. 校验采购单及客户的赊账权限
  816. * 2. 优化:复用采购单实例,避免重复查询
  817. * 3. 优化:将自动确认收货(confirmTake)包裹在独立事务中,合并写操作,减少磁盘 I/O
  818. * 4. 优化:合并打印逻辑中的 ShopExt 查询及订单保存,减少数据库交互
  819. * 5. 优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE
  820. */
  821. public function actionDebtPay()
  822. {
  823. $get = Yii::$app->request->get();
  824. $orderSn = $get['orderSn'] ?? 0;
  825. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  826. PurchaseService::valid($info, $this->shopId);
  827. //解决重复和并发提交
  828. $cacheKey = 'hd_debt_pay_' . $orderSn;
  829. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  830. if (!empty($has)) {
  831. util::fail('请稍等...');
  832. }
  833. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  834. if ($info->status == 5) {
  835. util::fail('订单已取消');
  836. }
  837. if ($info->status != 1) {
  838. util::fail('订单不是待付款状态');
  839. }
  840. if ($info->book == 1) {
  841. util::fail('预售不能记欠款');
  842. }
  843. $ghsId = $info->ghsId;
  844. $ghs = GhsClass::getGhsInfo($ghsId);
  845. $customId = $ghs['customId'] ?? 0;
  846. $custom = CustomClass::getCustom($customId);
  847. if (empty($custom)) {
  848. util::fail('没有找到客户');
  849. }
  850. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  851. util::fail('请先申请赊账权限');
  852. }
  853. if (getenv('YII_ENV') == 'production') {
  854. //源花汇不允许客户自己下欠款单
  855. if ($ghs['mainId'] == 10536) {
  856. util::fail('暂未开通');
  857. }
  858. }
  859. $connection = Yii::$app->db;
  860. $transaction = $connection->beginTransaction();
  861. try {
  862. //延期支付
  863. PurchaseService::debt($info);
  864. $transaction->commit();
  865. if (isset($info->localOrder) && $info->localOrder == 1) {
  866. if ($info->transType != 5 && $info->transType != 4) {
  867. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  868. $current = date("Y_m_d");
  869. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  870. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  871. }
  872. } else {
  873. //标记当配送方式已收一次包装费/运费,岭南批发市场用的功能,关键词 sh_method_area,只要有成功单就标记,不再要求金额必须>0
  874. $current = date("Y_m_d");
  875. if (isset($info->sendType) && in_array($info->sendType, [0, 4])) {
  876. $key = 'ghs_custom_today_has_get_pack_cost_' . $info->sendType . '_' . $current . '_' . $customId;
  877. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  878. // 兼容快递收运费的情况
  879. $sendKey = 'ghs_custom_today_has_get_send_cost_' . $info->sendType . '_' . $current . '_' . $customId;
  880. Yii::$app->redis->executeCommand('SETEX', [$sendKey, 86400, '1']);
  881. }
  882. }
  883. // 复杂分支/关键逻辑:不是预订单,且供货商配置为直接完成流程,则自动确认收货。
  884. // 优化:复用已有的 $info 实例,避免重复查询;同时将自动确认收货包裹在独立事务中,合并批量写操作,减少磁盘 I/O。
  885. if (!empty($info)) {
  886. if (isset($info->book) && $info->book == 0) {
  887. $ghsShopId = $ghs['shopId'] ?? 0;
  888. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  889. if (!empty($ext) && intval($ext->orderFlow ?? 0) === 0) {
  890. $takeTransaction = Yii::$app->db->beginTransaction();
  891. try {
  892. PurchaseClass::confirmTake($info);
  893. $takeTransaction->commit();
  894. } catch (\Exception $takeEx) {
  895. $takeTransaction->rollBack();
  896. Yii::error("自动确认收货失败: " . $takeEx->getMessage());
  897. throw $takeEx;
  898. }
  899. }
  900. }
  901. }
  902. //app新订单通知
  903. $saleId = $info->saleId ?? 0;
  904. $order = OrderClass::getById($saleId, true);
  905. if (!empty($order)) {
  906. $shopId = $order->shopId ?? 0;
  907. $shop = ShopClass::getById($shopId, true);
  908. if (!empty($shop)) {
  909. //通知供货商
  910. GhsNotifyClass::newOrderNotify($saleId);
  911. }
  912. //订单生成时唤起在线打印
  913. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  914. //有几个地方要同步去修改
  915. if (getenv('YII_ENV') == 'production') {
  916. //源花汇、盛丰不自动打小票,关键词mall_order_no_auto_print
  917. $mallOrderNoPrint = [10536, 44282, 26374];
  918. } else {
  919. $mallOrderNoPrint = [];
  920. }
  921. if (!in_array($order->mainId, $mallOrderNoPrint)) {
  922. // 复杂分支/关键逻辑:优化:只查询一次 $ext,合并 $order->save(),减少数据库交互
  923. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  924. $hasPrintSn = isset($ext->printSn) && !empty($ext->printSn);
  925. OrderClass::onlinePrint($order);
  926. if ($hasPrintSn) {
  927. $order->printNum += 1;
  928. }
  929. //花镜 打二次小票,有多处要修改搜索关键词tow_print
  930. if (in_array($order->mainId, [76796])) {
  931. OrderClass::onlinePrint($order);
  932. if ($hasPrintSn) {
  933. $order->printNum += 1;
  934. }
  935. }
  936. if ($hasPrintSn) {
  937. $order->save();
  938. }
  939. }
  940. }
  941. //付款成功之后呼叫跑腿,关键词 pay_after_call_pt,多处要同步修改
  942. GhsDeliveryOrderClass::payAfter($order);
  943. // 复杂分支/关键逻辑:优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE 的性能开销
  944. $customId = $order->customId ?? 0;
  945. $ghsId = $order->ghsId ?? 0;
  946. $date = date("Y-m-d H:i:s");
  947. if ($customId > 0) {
  948. CustomClass::updateByCondition(['id' => $customId], ['recentExpend' => $date]);
  949. }
  950. if ($ghsId > 0) {
  951. GhsClass::updateByCondition(['id' => $ghsId], ['recentExpend' => $date]);
  952. }
  953. }
  954. } catch (\Exception $e) {
  955. $transaction->rollBack();
  956. Yii::error("赊账支付失败原因:" . $e->getMessage());
  957. util::fail('支付失败');
  958. }
  959. util::success($info->attributes);
  960. }
  961. /**
  962. * 采购待支付页:客户 xhGhsCustom 净余额支付(并发重试 + 行锁)。
  963. */
  964. public function actionCustomBalancePay()
  965. {
  966. $post = Yii::$app->request->post();
  967. $orderSn = $post['orderSn'] ?? '';
  968. if (empty($orderSn)) {
  969. util::fail('订单号无效');
  970. }
  971. $cacheKey = 'hd_custom_balance_pay_' . $orderSn;
  972. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  973. if (!empty($has)) {
  974. util::fail('请稍等...');
  975. }
  976. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 8, 'has']);
  977. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  978. if (empty($info)) {
  979. util::fail('没有找到采购单');
  980. }
  981. PurchaseService::valid($info, $this->shopId);
  982. $deadline = $info->deadline ?? '';
  983. if (!empty($deadline) && time() > strtotime($deadline)) {
  984. util::fail('订单已失效了哦');
  985. }
  986. $ghsId = intval($info->ghsId ?? 0);
  987. $ghs = GhsClass::getGhsInfo($ghsId);
  988. try {
  989. $result = util::runWithDbConcurrencyRetry(function () use ($orderSn) {
  990. $connection = Yii::$app->db;
  991. $transaction = $connection->beginTransaction();
  992. try {
  993. $purchase = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  994. $respond = PurchaseService::payByGhsCustomBalance($purchase);
  995. $transaction->commit();
  996. return $respond;
  997. } catch (\Exception $exception) {
  998. if ($transaction->isActive) {
  999. $transaction->rollBack();
  1000. }
  1001. throw $exception;
  1002. }
  1003. });
  1004. $info = $result['purchase'];
  1005. $order = $result['saleOrder'] ?? null;
  1006. if (!empty($order)) {
  1007. if (isset($info->book) && $info->book == 0 && !empty($ghs)) {
  1008. $ghsShopId = $ghs['shopId'] ?? 0;
  1009. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  1010. if (!empty($ext) && intval($ext->orderFlow ?? 0) === 0) {
  1011. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1012. if (!empty($cg)) {
  1013. PurchaseClass::confirmTake($cg);
  1014. }
  1015. }
  1016. }
  1017. $shopId = $order->shopId ?? 0;
  1018. $shop = ShopClass::getById($shopId, true);
  1019. if (!empty($shop)) {
  1020. GhsNotifyClass::newOrderNotify($order->id ?? 0);
  1021. }
  1022. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  1023. OrderClass::onlinePrint($order);
  1024. }
  1025. GhsDeliveryOrderClass::payAfter($order);
  1026. $customId = $order->customId ?? 0;
  1027. $custom = CustomClass::getById($customId, true);
  1028. $date = date('Y-m-d H:i:s');
  1029. if (!empty($custom)) {
  1030. $custom->recentExpend = $date;
  1031. $custom->save();
  1032. }
  1033. $orderGhsId = $order->ghsId ?? 0;
  1034. $orderGhs = GhsClass::getById($orderGhsId, true);
  1035. if (!empty($orderGhs)) {
  1036. $orderGhs->recentExpend = $date;
  1037. $orderGhs->save();
  1038. }
  1039. }
  1040. util::success($info->attributes);
  1041. } catch (\Exception $e) {
  1042. Yii::info('客户余额支付失败:' . $e->getMessage());
  1043. util::fail($e->getMessage() ?: '支付失败');
  1044. }
  1045. }
  1046. //余额支付 ssh 2021.1.24
  1047. public function actionBalancePay()
  1048. {
  1049. $shopAdmin = $this->shopAdmin;
  1050. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1051. util::fail('超管才能操作');
  1052. }
  1053. $get = Yii::$app->request->get();
  1054. $orderSn = $get['orderSn'] ?? 0;
  1055. $password = $get['password'] ?? '';
  1056. // 重新获取,不加密的用户数据
  1057. $admin = AdminClass::getById($this->adminId, true);
  1058. if (password_verify($password, $admin->payPassword) == false) {
  1059. util::fail('密码错误');
  1060. }
  1061. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1062. if (empty($info)) {
  1063. util::fail('没有找到采购单');
  1064. }
  1065. if ($info->status == 5) {
  1066. util::fail('订单已取消');
  1067. }
  1068. if ($info->status != 1) {
  1069. util::fail('订单不是待付款状态');
  1070. }
  1071. PurchaseService::valid($info, $this->shopId);
  1072. $connection = Yii::$app->db;
  1073. $transaction = $connection->beginTransaction();
  1074. try {
  1075. //余额支付
  1076. purchaseService::balancePay($info);
  1077. $transaction->commit();
  1078. } catch (\Exception $e) {
  1079. $transaction->rollBack();
  1080. util::fail('支付失败');
  1081. }
  1082. util::success($info->attributes);
  1083. }
  1084. //采购记录 ssh 2021.1.24
  1085. public function actionList()
  1086. {
  1087. $get = Yii::$app->request->get();
  1088. $where = ['shopId' => $this->shopId];
  1089. $searchTime = $get['searchTime'] ?? '';
  1090. if (!empty($searchTime)) {
  1091. $startTime = $get['startTime'] ?? '';
  1092. $endTime = $get['endTime'] ?? '';
  1093. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1094. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1095. }
  1096. $ghsId = $get['ghsId'] ?? 0;
  1097. if (!empty($ghsId)) {
  1098. $where['ghsId'] = (int)$ghsId;
  1099. }
  1100. $status = $get['status'] ?? 0;
  1101. if (!empty($status)) {
  1102. $where['status'] = $status;
  1103. }
  1104. $respond = PurchaseService::getPurchaseList($where);
  1105. $respond['shop'] = $this->shop->attributes;
  1106. util::success($respond);
  1107. }
  1108. //获取合并采购单列表 ssh 20260519
  1109. public function actionMergeOrderList()
  1110. {
  1111. Yii::$app->params['pageSize'] = 9999;
  1112. $get = Yii::$app->request->get();
  1113. $mergeId = $get['mergeId'] ?? 0;
  1114. if (empty($mergeId)) {
  1115. util::fail('请选择采购单');
  1116. }
  1117. $mergeSn = CgMergeSnClass::getById($mergeId, true);
  1118. if (empty($mergeSn)) {
  1119. util::fail('请选择合并采购单');
  1120. }
  1121. $salt = $get['salt'] ?? '';
  1122. if (!empty($salt)) {
  1123. if (empty($mergeSn->salt) || $mergeSn->salt != $salt) {
  1124. util::fail('此单无法查看哦');
  1125. }
  1126. } else {
  1127. if (empty($this->shopId)) {
  1128. util::fail('暂时不能查看');
  1129. }
  1130. if ($mergeSn->mainId != $this->mainId) {
  1131. util::fail('此单无法查看哈');
  1132. }
  1133. }
  1134. $ghsId = $mergeSn->ghsId ?? 0;
  1135. $where = [
  1136. 'ghsId' => $ghsId,
  1137. 'mergeId' => $mergeId,
  1138. ];
  1139. $respond = PurchaseService::getPurchaseList($where);
  1140. $totalPrice = 0;
  1141. if (!empty($respond['list'])) {
  1142. foreach ($respond['list'] as $one) {
  1143. $actPrice = $one['actPrice'] ?? 0;
  1144. $totalPrice = bcadd($totalPrice, $actPrice, 2);
  1145. }
  1146. }
  1147. $totalPrice = floatval($totalPrice);
  1148. $respond['totalPrice'] = $totalPrice;
  1149. $respond['shop'] = !empty($this->shop) ? $this->shop->attributes : [];
  1150. util::success($respond);
  1151. }
  1152. //采购详情 ssh 2021.01.25
  1153. public function actionDetail()
  1154. {
  1155. $get = Yii::$app->request->get();
  1156. $id = $get['id'] ?? 0;
  1157. $info = PurchaseService::getInfo($id, true, true);
  1158. if (empty($info)) {
  1159. util::fail('没有找到采购单');
  1160. }
  1161. $salt = $get['salt'] ?? '';
  1162. if (!empty($salt)) {
  1163. if (!isset($info['salt']) || $info['salt'] != $salt) {
  1164. util::fail('无效采购单呢!');
  1165. }
  1166. } else {
  1167. if (!isset($info['mainId']) || $info['mainId'] != $this->mainId) {
  1168. //util::fail('采购单不能访问');
  1169. }
  1170. }
  1171. $customId = $info['customId'] ?? 0;
  1172. $custom = CustomClass::getById($customId);
  1173. $customName = $custom['name'] ?? '';
  1174. $customMobile = $custom['mobile'] ?? '';
  1175. $info['customName'] = $customName;
  1176. $info['customMobile'] = $customMobile;
  1177. $hasDebtPay = $custom['debt'] ?? 0;
  1178. $book = $info['book'] ?? 0;
  1179. $presell = $info['presell'] ?? 0;
  1180. //预售不能记欠款
  1181. if ($book == 1) {
  1182. $hasDebtPay = 0;
  1183. }
  1184. if ($presell == 1) {
  1185. $hasDebtPay = 0;
  1186. }
  1187. $shop = $this->shop;
  1188. $info['pfShopId'] = $shop->pfShopId ?? 0;
  1189. $info['hasDebtPay'] = $hasDebtPay;
  1190. // 待支付页展示 xhGhsCustom 净余额(正数=可支付余额),不再使用 main.balance
  1191. $info['balance'] = 0;
  1192. $info['customNetBalance'] = '0.00';
  1193. $info['showBalancePay'] = 0;
  1194. $info['ghsSalt'] = '';
  1195. $ghsId = intval($info['ghsId'] ?? 0);
  1196. if ($ghsId > 0) {
  1197. $ghsRow = GhsClass::getById($ghsId, true);
  1198. if (!empty($ghsRow)) {
  1199. $info['ghsSalt'] = $ghsRow->salt ?? '';
  1200. $ghsCustomId = intval($ghsRow->customId ?? 0);
  1201. if ($ghsCustomId > 0) {
  1202. $ghsCustom = CustomClass::getById($ghsCustomId, true);
  1203. if (!empty($ghsCustom)) {
  1204. $net = AccountMoneyClass::getNetBalanceFromRow($ghsCustom);
  1205. $info['customNetBalance'] = $net;
  1206. if (bccomp($net, '0', 2) > 0) {
  1207. $info['balance'] = floatval($net);
  1208. $info['showBalancePay'] = 1;
  1209. }
  1210. }
  1211. }
  1212. }
  1213. }
  1214. $getPayType = dict::getDict('getPayType');
  1215. $info['getPayType'] = $getPayType;
  1216. //获取售后条件
  1217. $ghsId = $info['ghsId'] ?? 0;
  1218. $ghsInfo = GhsClass::getById($ghsId, true);
  1219. if (empty($ghsInfo)) {
  1220. util::fail('没有找到供货商');
  1221. }
  1222. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  1223. //重要,客户看到的电话修改成门店的联系电话
  1224. $ghsShopId = $ghsInfo->shopId ?? 0;
  1225. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  1226. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  1227. $info['ghsMobile2'] = $ghsShopInfo->telephone2 ?? '';
  1228. $info['hasRenew'] = 1;
  1229. //如果是快捷开单,则不需要显示累计待结,因为商家经常把快捷开单的订单发给客户结账
  1230. $showForPay = 1;
  1231. if ($ghsShopInfo->skCustomId == $info['customId']) {
  1232. $showForPay = 0;
  1233. }
  1234. $info['showForPay'] = $showForPay;
  1235. util::success($info);
  1236. }
  1237. //可用的支付方式 ssh 2021.1.25
  1238. public function actionPayWay()
  1239. {
  1240. $button = [
  1241. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  1242. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  1243. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  1244. ];
  1245. util::success(['button' => $button]);
  1246. }
  1247. //待结款明细 ssh 2021.1.26
  1248. public function actionDebtList()
  1249. {
  1250. $get = Yii::$app->request->get();
  1251. $id = $get['id'] ?? 0;
  1252. $salt = $get['salt'] ?? '';
  1253. $info = GhsClass::getGhsInfo($id);
  1254. if (!empty($salt)) {
  1255. if (!isset($info['salt']) || $info['salt'] != $salt) {
  1256. util::fail('供货商出错了');
  1257. }
  1258. } else {
  1259. if (!isset($info['ownShopId']) || $info['ownShopId'] != $this->shopId) {
  1260. util::fail('错误的供货商.');
  1261. }
  1262. }
  1263. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  1264. $searchTime = $get['searchTime'] ?? '';
  1265. if (!empty($searchTime)) {
  1266. $startTime = $get['startTime'] ?? '';
  1267. $endTime = $get['endTime'] ?? '';
  1268. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1269. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1270. }
  1271. $respond = PurchaseService::getDebtList($where);
  1272. util::success($respond);
  1273. }
  1274. //支付宝支付 ssh 2021.4.11
  1275. public function actionAliPay()
  1276. {
  1277. ini_set('date.timezone', 'Asia/Shanghai');
  1278. $post = Yii::$app->request->post();
  1279. //默认是支付宝支付
  1280. $payWay = $post['payWay'] ?? 1;
  1281. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1282. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1283. if (empty($order)) {
  1284. util::fail('订单号无效');
  1285. }
  1286. if ($order->status == 5) {
  1287. util::fail('订单已取消');
  1288. }
  1289. if ($order->status != 1) {
  1290. util::fail('订单不是待付款状态');
  1291. }
  1292. $deadline = $order->deadline;
  1293. $current = time();
  1294. if (($current + 50) > strtotime($deadline)) {
  1295. util::fail('订单已失效,请重新下单');
  1296. }
  1297. $ghsId = $order->ghsId ?? 0;
  1298. $ghs = GhsClass::getById($ghsId, true);
  1299. if (empty($ghs)) {
  1300. util::fail('没有供货商信息');
  1301. }
  1302. $ghsShopId = $ghs->shopId ?? 0;
  1303. $ghsShop = ShopClass::getById($ghsShopId, true);
  1304. if (empty($ghsShop)) {
  1305. util::fail('没有供货商门店信息');
  1306. }
  1307. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1308. util::fail('商家支付功能未开通');
  1309. }
  1310. //避免重复提交
  1311. util::checkRepeatCommit($orderSn, 8);
  1312. //已经唤起过支付了,根据拉卡拉的规则,需要生成新的订单号
  1313. if ($order->changePrice == 2) {
  1314. $oldOrderSn = $orderSn;
  1315. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1316. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1317. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1318. $params = [
  1319. 'appid' => 'OP00002119',
  1320. 'serial_no' => '018b08cfddbd',
  1321. 'merchant_no' => $ghsShop->lklSjNo,
  1322. 'term_no' => $ghsShop->lklScanTermNo,
  1323. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1324. 'lklCertificatePath' => $lklCertificatePath,
  1325. ];
  1326. $laResource = new Lakala($params);
  1327. $closeParams = [
  1328. 'orderSn' => $oldOrderSn,
  1329. ];
  1330. $response = $laResource->cashClose($closeParams);
  1331. if (isset($response['code']) == false || $response['code'] != '000000') {
  1332. $msg = $response['msg'] ?? '';
  1333. $code = $response['code'] ?? 0;
  1334. if (!in_array($code, ['000091'])) {
  1335. noticeUtil::push('注意花店买花更换单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  1336. }
  1337. }
  1338. $shopId = $order->shopId ?? 0;
  1339. $mainId = $order->mainId ?? 0;
  1340. $customId = $order->customId ?? 0;
  1341. $ghsId = $order->ghsId ?? 0;
  1342. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1343. $newOrderSn = orderSn::getPurchaseSn($snData);
  1344. $orderSn = $newOrderSn;
  1345. $order->orderSn = $newOrderSn;
  1346. $order->save();
  1347. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1348. }
  1349. $order->changePrice = 2;
  1350. $order->save();
  1351. $ghsShopName = $ghsShop->shopName ?? '';
  1352. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1353. $name = $ghsShopName . " 花材 " . $orderSn;
  1354. $totalFee = $order->realPrice;
  1355. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1356. $ghsId = $order->ghsId ?? 0;
  1357. $ghs = GhsClass::getById($ghsId, true);
  1358. if (empty($ghs)) {
  1359. util::fail('没有供货商信息');
  1360. }
  1361. $ghsShopId = $ghs->shopId ?? 0;
  1362. $ghsShop = ShopClass::getById($ghsShopId, true);
  1363. if (empty($ghsShop)) {
  1364. util::fail('没有供货商门店信息');
  1365. }
  1366. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1367. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1368. $params = [
  1369. 'appid' => 'OP00002119',
  1370. 'serial_no' => '018b08cfddbd',
  1371. 'merchant_no' => $ghsShop->lklSjNo,
  1372. 'term_no' => $ghsShop->lklB2BTermNo,
  1373. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1374. 'lklCertificatePath' => $lklCertificatePath,
  1375. ];
  1376. $laResource = new Lakala($params);
  1377. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  1378. $wxParams = [
  1379. 'orderSn' => $orderSn,
  1380. 'amount' => $totalFee,
  1381. 'capitalType' => $purchaseCapital,
  1382. 'notifyUrl' => $notifyUrl,
  1383. 'subject' => $name,
  1384. 'payWay' => $payWay,
  1385. ];
  1386. $response = $laResource->cashierPay($wxParams);
  1387. if (isset($response['code']) == false || $response['code'] != '000000') {
  1388. $errMsg = $response['msg'] ?? '';
  1389. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  1390. util::fail('发起支付失败');
  1391. }
  1392. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  1393. $hasRenew = $ghsShop->hasRenew ?? 0;
  1394. $new = [
  1395. 'payUrl' => $counter_url,
  1396. 'orderSn' => $orderSn,
  1397. 'hasRenew' => 1,
  1398. ];
  1399. util::success($new);
  1400. }
  1401. //微信支付 ssh 2021.4.11
  1402. public function actionWxPay()
  1403. {
  1404. ini_set('date.timezone', 'Asia/Shanghai');
  1405. $post = Yii::$app->request->post();
  1406. $couponId = $post['couponId'] ?? 0;
  1407. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1408. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1409. if (empty($order)) {
  1410. util::fail('订单号无效');
  1411. }
  1412. if ($order->status == 5) {
  1413. util::fail('订单已取消');
  1414. }
  1415. if ($order->status != 1) {
  1416. util::fail('订单不是待付款状态');
  1417. }
  1418. $deadline = $order->deadline;
  1419. $current = time();
  1420. if (($current + 50) > strtotime($deadline)) {
  1421. util::fail('订单已失效,请重新下单');
  1422. }
  1423. //避免重复提交
  1424. util::checkRepeatCommit($orderSn, 8);
  1425. $ghsId = $order->ghsId ?? 0;
  1426. $ghs = GhsClass::getById($ghsId, true);
  1427. if (empty($ghs)) {
  1428. util::fail('没有供货商信息');
  1429. }
  1430. $ghsShopId = $ghs->shopId ?? 0;
  1431. $ghsShop = ShopClass::getById($ghsShopId, true);
  1432. if (empty($ghsShop)) {
  1433. util::fail('没有供货商门店信息');
  1434. }
  1435. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1436. util::fail('商家支付功能未开通');
  1437. }
  1438. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  1439. if ($order->changePrice == 2) {
  1440. $oldOrderSn = $orderSn;
  1441. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1442. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1443. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1444. $params = [
  1445. 'appid' => 'OP00002119',
  1446. 'serial_no' => '018b08cfddbd',
  1447. 'merchant_no' => $ghsShop->lklSjNo,
  1448. 'term_no' => $ghsShop->lklScanTermNo,
  1449. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1450. 'lklCertificatePath' => $lklCertificatePath,
  1451. ];
  1452. $laResource = new Lakala($params);
  1453. $closeParams = [
  1454. 'orderSn' => $oldOrderSn,
  1455. ];
  1456. $response = $laResource->close($closeParams);
  1457. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  1458. $msg = $response['msg'] ?? '';
  1459. $code = $response['code'] ?? 0;
  1460. if (!in_array($code, ['BBS11114'])) {
  1461. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . $code, '15280215347');
  1462. util::fail('出错了,请重新下单');
  1463. }
  1464. }
  1465. $shopId = $order->shopId ?? 0;
  1466. $mainId = $order->mainId ?? 0;
  1467. $customId = $order->customId ?? 0;
  1468. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1469. $newOrderSn = orderSn::getPurchaseSn($snData);
  1470. $orderSn = $newOrderSn;
  1471. $order->orderSn = $newOrderSn;
  1472. $order->save();
  1473. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1474. }
  1475. $id = $order->id;
  1476. $order->changePrice = 2;
  1477. $order->save();
  1478. $ghsShopName = $ghsShop->shopName ?? '';
  1479. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1480. $name = $ghsShopName . " 花材 " . $orderSn;
  1481. $totalFee = $order->realPrice;
  1482. $openId = '';
  1483. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  1484. $openId = $post['currentMiniOpenId'];
  1485. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  1486. }
  1487. if (empty($openId)) {
  1488. if (isset($this->admin) && !empty($this->admin)) {
  1489. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  1490. $openId = $this->admin->miniOpenId;
  1491. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  1492. }
  1493. }
  1494. }
  1495. if (empty($openId)) {
  1496. $currentShopId = $order->shopId ?? 0;
  1497. $currentShop = ShopClass::getById($currentShopId, true);
  1498. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  1499. $currentMobile = $currentShop->mobile ?? '';
  1500. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  1501. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  1502. }
  1503. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1504. //花卉宝代为申请的微信支付
  1505. $merchantExtend = WxOpenClass::getWxInfo();
  1506. if (empty($ghsShop->lklSjNo)) {
  1507. noticeUtil::push('编号31505:单号:' . $orderSn . ' 商家没有开通收款功能:' . $ghsShopName, '15280215347');
  1508. util::fail('商家未开通支付功能');
  1509. }
  1510. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1511. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1512. $params = [
  1513. 'appid' => 'OP00002119',
  1514. 'serial_no' => '018b08cfddbd',
  1515. 'merchant_no' => $ghsShop->lklSjNo,
  1516. 'term_no' => $ghsShop->lklScanTermNo,
  1517. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1518. 'lklCertificatePath' => $lklCertificatePath,
  1519. ];
  1520. $laResource = new Lakala($params);
  1521. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  1522. $wxParams = [
  1523. 'orderSn' => $orderSn,
  1524. 'amount' => $totalFee,
  1525. 'capitalType' => $purchaseCapital,
  1526. 'notifyUrl' => $notifyUrl,
  1527. 'wxAppId' => $merchantExtend['miniAppId'],
  1528. 'openId' => $openId,
  1529. 'subject' => $name,
  1530. 'couponId' => $couponId,
  1531. ];
  1532. $response = $laResource->driveWxPay($wxParams);
  1533. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1534. $errMsg = $response['msg'] ?? '';
  1535. noticeUtil::push('编号129680931:' . $errMsg . ' 单号:' . $orderSn . ' ' . $ghsShopName, '15280215347');
  1536. util::fail('支付失败');
  1537. }
  1538. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1539. $appId = $newParams['app_id'] ?? '';
  1540. $nonceStr = $newParams['nonce_str'] ?? '';
  1541. $paySign = $newParams['pay_sign'] ?? '';
  1542. $package = $newParams['package'] ?? '';
  1543. $signType = $newParams['sign_type'] ?? '';
  1544. $timeStamp = $newParams['time_stamp'] ?? '';
  1545. $new = [
  1546. 'id' => $id,
  1547. 'appId' => $appId,
  1548. 'nonceStr' => $nonceStr,
  1549. 'paySign' => $paySign,
  1550. 'package' => $package,
  1551. 'signType' => $signType,
  1552. 'timeStamp' => $timeStamp,
  1553. 'orderSn' => $orderSn,
  1554. ];
  1555. util::success($new);
  1556. }
  1557. //计算运费 ssh 20221003
  1558. public function actionFreight()
  1559. {
  1560. $get = Yii::$app->request->get();
  1561. $shop = $this->shop;
  1562. $ghsId = $get['ghsId'] ?? 0;
  1563. $ghs = GhsClass::getById($ghsId, true);
  1564. if (empty($ghs)) {
  1565. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1566. }
  1567. $ghsShopId = $ghs->shopId ?? 0;
  1568. $ghsShop = ShopClass::getById($ghsShopId, true);
  1569. if (empty($ghsShop)) {
  1570. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1571. }
  1572. $customId = $ghs->customId ?? 0;
  1573. $custom = CustomClass::getById($customId, true);
  1574. if (empty($custom)) {
  1575. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1576. }
  1577. $weight = $get['weight'] ?? 0;
  1578. if (empty($weight)) {
  1579. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1580. }
  1581. $respond = PurchaseClass::getDistanceFee($shop, $ghsShop, $weight, $custom);
  1582. util::success($respond);
  1583. }
  1584. //运费计算 lqh 2021.4.12 暂时返回固定值
  1585. public function actionFreight2()
  1586. {
  1587. //lqh 2021.6.25 运费固定返回0
  1588. util::success(['sedCost' => 0]);
  1589. }
  1590. }