PurchaseController.php 79 KB

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