PurchaseController.php 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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. // 已经有了 -- 在 replaceItem()
  468. // foreach ($productList as $itemData) {
  469. // $productId = $itemData['productId'] ?? 0;
  470. // $product = $productInfoList[$productId] ?? [];
  471. // if (empty($product)) {
  472. // util::fail('有商品信息没有找到');
  473. // }
  474. // $bigNum = $itemData['bigNum'] ?? 0;
  475. // if (($product['limitBuy'] ?? 0) > 0) {
  476. // $product['productId'] = $productId;
  477. // ProductClass::handleLimitBuy($product, $customId, floatval($bigNum));
  478. // }
  479. // }
  480. } else {
  481. util::fail('花材不存在');
  482. }
  483. //供货商预订单最低公斤数要求和每公斤服务费
  484. $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
  485. $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
  486. $post['product'] = $productList;
  487. // xhShMethod 门店下单前保留花材合计,供后续 packCost 计算(pfLevel==0 分支会 unset itemTotalAmount)
  488. $shMethodItemAmount = $post['itemTotalAmount'] ?? 0;
  489. $sendType = $post['sendType'] ?? 0;
  490. $transType = $post['transType'] ?? 0;
  491. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  492. $shopExt = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,thirdSend,hcMap,hcFreeKm');
  493. if ($shopExt->thirdSend == 1) {
  494. util::fail('本店暂不能使用跑腿哦');
  495. }
  496. }
  497. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  498. //昆明发货包装费和运费的计算
  499. $totalWeight = 0;
  500. $totalItemNum = 0;
  501. foreach ($productList as $itemData) {
  502. $bigNum = $itemData['bigNum'] ?? 0;
  503. $thisWeight = $itemData['weight'] ?? 0;
  504. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  505. $totalWeight = bcadd($currentWeight, $totalWeight, 2);
  506. $totalItemNum = bcadd($totalItemNum, $bigNum, 2);
  507. }
  508. $totalItemNum = floatval($totalItemNum);
  509. $kmPackCost = PurchaseClass::getKmPackCost($ghsMainId);
  510. $packCost = 0;
  511. if (!empty($kmPackCost)) {
  512. foreach ($kmPackCost as $itemPack) {
  513. $kiloNum = $itemPack['num'] ?? 0;
  514. if ($kiloNum >= $totalWeight) {
  515. $packCost = $itemPack['amount'] ?? 0;
  516. break;
  517. }
  518. }
  519. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  520. noticeUtil::push("总重量" . $totalWeight, '15280215347');
  521. }
  522. }
  523. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  524. noticeUtil::push("走的pfLevel=1", '15280215347');
  525. }
  526. //同城配送和到店自取免打包费
  527. if ($transType == 4 || $transType == 5) {
  528. $packCost = 0;
  529. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  530. noticeUtil::push("没有计算打包费原因:transType = 4 =5", '15280215347');
  531. }
  532. }
  533. //如果已经算过一次打包费和运费了,则不再费了
  534. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $sendType);
  535. if (!$needAdd) {
  536. $packCost = 0;
  537. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  538. noticeUtil::push("没有计算打包费原因:已经算过一次了", '15280215347');
  539. }
  540. }
  541. $post['packCost'] = $packCost;
  542. $transCost = dict::getDict('transCost', null, null, $ghsShopInfo->mainId);
  543. $sendCost = 0;
  544. if (!empty($transCost)) {
  545. foreach ($transCost as $trans) {
  546. $sign = $trans['sign'] ?? 0;
  547. if ($sign == $transType) {
  548. if ($sign == 2) {
  549. //冷链物流
  550. $llOption = $trans['option'] ?? [];
  551. if (!empty($llOption)) {
  552. $largeInfo = end($llOption);
  553. $largeNum = $largeInfo['num'] ?? 0;
  554. $largeAmount = $largeInfo['amount'] ?? 0;
  555. $bs = intval($totalItemNum / $largeNum);
  556. if ($bs > 0) {
  557. $addSendCost = bcmul($bs, $largeAmount, 2);
  558. $sendCost = bcadd($sendCost, $addSendCost, 2);
  559. $subNum = bcmul($bs, $largeNum, 2);
  560. $subNum = floatval($subNum);
  561. $totalItemNum = bcsub($totalItemNum, $subNum, 2);
  562. $totalItemNum = floatval($totalItemNum);
  563. }
  564. foreach ($llOption as $miniItem) {
  565. if ($miniItem['num'] >= $totalItemNum) {
  566. $sendCost = bcadd($sendCost, $miniItem['amount'], 2);
  567. break;
  568. }
  569. }
  570. }
  571. } else {
  572. //其它物流
  573. $perKiloCost = $trans['perKiloCost'] ?? 0;
  574. $sendCost = bcmul($perKiloCost, $totalWeight, 3);
  575. $sendCost = round($sendCost, 2);
  576. }
  577. }
  578. }
  579. }
  580. //如果已经算过一次打包费和运费了,则不再费了
  581. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $sendType);
  582. if ($needAdd == false) {
  583. $sendCost = 0;
  584. }
  585. $post['sendCost'] = $sendCost;
  586. //标记为昆明到地方订单
  587. $post['localOrder'] = 1;
  588. } else {
  589. // xhShMethod 门店:下单前按配置写入不满最低消费时的附加包装费/运费,关键词 sh_method_area
  590. $newShMethod = dict::getDict('newShMethod');
  591. if (in_array($ghsShopId, $newShMethod)) {
  592. $shMethodBigNum = 0;
  593. foreach ($productList as $itemData) {
  594. $shMethodBigNum += floatval($itemData['bigNum'] ?? 0);
  595. }
  596. $additionalCosts = ShMethodClass::calcFee(
  597. $ghsShopId,
  598. $ghsMainId,
  599. $sendType,
  600. $shMethodItemAmount,
  601. $shMethodBigNum,
  602. $customId,
  603. $ghsInfo
  604. );
  605. // 如果产生了附加运费,与前面可能产生的 sendCost 进行累加或覆盖
  606. if ($additionalCosts['sendCost'] > 0) {
  607. $post['sendCost'] = bcadd($post['sendCost'] ?? 0, $additionalCosts['sendCost'], 2);
  608. }
  609. // 如果产生了附加包装费,覆盖前面赋值的 packCost
  610. if ($additionalCosts['packCost'] > 0) {
  611. $post['packCost'] = $additionalCosts['packCost'];
  612. }
  613. }else{
  614. //同城发货包装费和运费的计算
  615. $sendCost = 0;
  616. $sendDistance = 0;
  617. //特别注意,这边取供货商的shop信息
  618. $mapSet = ShopClass::hasIntraCity($ghsShopInfo);
  619. $openIntraCity = $mapSet['openIntraCity'] ?? 0;
  620. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  621. if ($openIntraCity == 2) {
  622. util::fail('不能使用跑腿');
  623. }
  624. if ($openIntraCity == 1) {
  625. // 生成随机订单号,不要跟原来的混起来,跑腿-销售单-
  626. $prefix = 'PT-XSD-' . $this->mainId . '-';
  627. $orderSn = $prefix . round(microtime(true) * 1000);
  628. // 使用 DeliveryQuoteUtil 获取配送报价
  629. try {
  630. if (empty($post['deliveryPlatform'])) {
  631. util::fail('请选择跑腿');
  632. }
  633. $quoteResult = DeliveryQuoteUtil::getDeliveryQuote([
  634. 'productList' => $productList,
  635. 'deliveryPlatform' => $post['deliveryPlatform'],
  636. 'deliveryBracketContent' => $post['deliveryBracketContent'], // 平台多报价识别id
  637. 'ghsInfo' => $ghsInfo,
  638. 'custom' => $custom,
  639. 'order' => [
  640. 'orderSn' => $orderSn,
  641. 'goodsType' => 1, //商品类型:0花束 1花材
  642. 'itemTotalAmount' => $post['itemTotalAmount'] ?? 0,
  643. 'remark' => $post['remark'] ?? '',
  644. ],
  645. 'mainId' => $this->mainId,
  646. 'productCount' => $productCount,
  647. ]);
  648. $sendCost = $quoteResult['sendCost'];
  649. $sendDistance = $quoteResult['sendDistance'];
  650. } catch (\Exception $e) {
  651. util::fail($e->getMessage());
  652. }
  653. }
  654. }
  655. $post['sendCost'] = $sendCost;
  656. $post['sendDistance'] = $sendDistance;
  657. // 出车(0)、发快递(4)、发物流可以算包装费
  658. $packCost = 0;
  659. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  660. noticeUtil::push("走的pfLevel=0", '15280215347');
  661. }
  662. $itemTotalAmount = $post['itemTotalAmount'] ?? 0;
  663. unset($post['itemTotalAmount']);
  664. // sendType == 0 或 4 都要收包装费
  665. if (in_array($sendType, [0, 4])) {
  666. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $sendType, $itemTotalAmount);
  667. if ($needAdd) {
  668. $packCost = $ghsShopInfo->packCost ?? 0;
  669. }
  670. }
  671. $post['packCost'] = $packCost;
  672. }
  673. }
  674. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  675. $newShMethod = dict::getDict('newShMethod');
  676. if (in_array($ghsShopId, $newShMethod)) {
  677. // 使用 xhShMethod 配置校验配送限制,替代旧版门店硬编码规则,关键词 sh_method_area
  678. $shMethodError = ShMethodClass::checkLimit(
  679. $ghsShopId,
  680. $ghsMainId,
  681. $sendType,
  682. $respond->actPrice ?? 0,
  683. $respond->bigNum ?? 0,
  684. $post['packCost'] ?? 0,
  685. $post['sendCost'] ?? 0,
  686. $post['wlName'] ?? '',
  687. $customId,
  688. $ghsInfo
  689. );
  690. if ($shMethodError !== '') {
  691. $transaction->rollBack();
  692. util::fail($shMethodError);
  693. }
  694. } else {
  695. if ($sendType == 0) {
  696. if (getenv('YII_ENV') == 'production') {
  697. //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
  698. if ($ghsShopId == 4608) {
  699. if ($respond->actPrice < 500) {
  700. $transaction->rollBack();
  701. util::fail('满500元支持免费配送');
  702. }
  703. }
  704. //叶上花,满500元市区免费配送
  705. if ($ghsShopId == 4804) {
  706. if ($respond->actPrice < 500) {
  707. $transaction->rollBack();
  708. util::fail('满500元支持免费配送');
  709. }
  710. }
  711. //花悠星 国恋 勇记 万丽 陆丰暂时不支持免费配送
  712. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003 || $ghsShopId == 38128) {
  713. util::fail('暂不支持送货上门,请选其它配送方式!');
  714. }
  715. //广州鑫源花卉,选择送货上门必须选必选商品
  716. if ($ghsShopId == 12439) {
  717. $hasMust = array_intersect($ids, [3446945, 3446970, 3446973, 3446979, 3446995, 3447010, 3447023, 3459983, 4333184, 4317745, 1585738]);
  718. if (empty($hasMust)) {
  719. util::fail('请返回选择 必选商品');
  720. }
  721. }
  722. //情意花卉,必选品
  723. if ($ghsShopId == 100883) {
  724. $hasMust = array_intersect($ids, [5017957,5036067,5036089,5036099,5017958,5036063,5017948,5035980,5036073,5036075,5036076,5036117,5036118,5017949,5035979,5035976,5035928,5035930,5036061,5035879]);
  725. if (empty($hasMust)) {
  726. util::fail('请返回选择 必选商品');
  727. }
  728. }
  729. //三明易批花
  730. if ($ghsShopId == 82200) {
  731. if ($respond->actPrice < 100 || $respond->bigNum < 15) {
  732. $hasMust = array_intersect($ids, [4283478]);
  733. if (empty($hasMust)) {
  734. util::fail('没满100元且15扎,请返回选运费');
  735. }
  736. }
  737. // 按当天计时,把 sendTimeWant 要限制在两天的时间范围之内
  738. $future = date("Y-m-d", strtotime('+2 day'));
  739. if (strtotime($respond->sendTimeWant) > strtotime($future)) {
  740. util::fail('配送日期只能选最近二天');
  741. }
  742. }
  743. if ($ghsShopId == 77703) {
  744. //天天鲜花思明店,暂时关闭送货上门
  745. if ($respond->sendType == 0) {
  746. //util::fail('节日暂停送货上门,请选到店自取');
  747. }
  748. }
  749. if ($ghsShopId == 86726) {
  750. //藏花林
  751. if ($respond->transType != 4 && $respond->transType != 5) {
  752. util::fail('请选同城配送或到店自取');
  753. }
  754. }
  755. }
  756. if (isset($ghsInfo['home'])) {
  757. if ($ghsInfo['home'] == 0) {
  758. util::fail('暂不支持送货上门,请选其它配送方式');
  759. }
  760. $homeAmount = $ghsInfo['homeAmount'] ? floatval($ghsInfo['homeAmount']) : 0;
  761. $homeNum = $ghsInfo['homeNum'] ? floatval($ghsInfo['homeNum']) : 0;
  762. if ($homeAmount > 0 && $homeAmount > $respond->actPrice) {
  763. util::fail("满{$homeAmount}元 可送货上门");
  764. }
  765. if ($homeNum > 0 && $homeNum > $respond->bigNum) {
  766. util::fail("满{$homeNum}扎 可送货上门");
  767. }
  768. }
  769. }
  770. if ($sendType == 2) {
  771. //温州易批花,跑腿,不满100,要选10元的补运费商品
  772. if ($ghsShopId == 91115) {
  773. if ($respond->actPrice < 100) {
  774. $transaction->rollBack();
  775. $hasMust = array_intersect($ids, [4449812, 4449811]);
  776. if (empty($hasMust)) {
  777. util::fail('不满100,必选商品 补运费10元');
  778. }
  779. }
  780. }
  781. }
  782. if ($sendType == 4) {
  783. //温州易批花,快递,不满100,要选10元的补运费商品
  784. if ($ghsShopId == 91115) {
  785. if ($respond->actPrice < 100) {
  786. $transaction->rollBack();
  787. $hasMust = array_intersect($ids, [4449812, 4449811]);
  788. if (empty($hasMust)) {
  789. util::fail('不满100,必选商品 补运费10元');
  790. }
  791. }
  792. }
  793. }
  794. if ($sendType == 3) {
  795. //温州易批花,物流,不满200,要选10元的补运费商品
  796. if ($ghsShopId == 91115) {
  797. if ($respond->actPrice < 200) {
  798. $transaction->rollBack();
  799. $hasMust = array_intersect($ids, [4449812, 4449811]);
  800. if (empty($hasMust)) {
  801. util::fail('不满200,必选商品 补运费10元');
  802. }
  803. }
  804. }
  805. }
  806. }
  807. $transaction->commit();
  808. $transactionFinished = true;
  809. ProductClass::clearLimitBuyRollbackSnapshot();
  810. util::success($respond);
  811. } catch (\Exception $e) {
  812. if ($transaction->isActive) {
  813. $transaction->rollBack();
  814. }
  815. ProductClass::rollbackLimitBuySnapshot();
  816. $transactionFinished = true;
  817. Yii::error($e->getMessage());
  818. util::fail();
  819. }
  820. }
  821. /**
  822. * 赊账支付
  823. * 职责:处理采购单延期支付(记欠款/赊账),更新采购单状态,并触发后续的自动收货、通知、在线打印、跑腿呼叫等业务流
  824. * 入参:GET 传参 orderSn (采购单号)
  825. * 返回:JSON 格式的采购单属性
  826. * 副作用:更新采购单、订单状态,增减花材库存并记录库存流水,更新客户及供货商的最后下单时间,发送外部通知和打印请求
  827. * 关键边界:
  828. * 1. 校验采购单及客户的赊账权限
  829. * 2. 优化:复用采购单实例,避免重复查询
  830. * 3. 优化:将自动确认收货(confirmTake)包裹在独立事务中,合并写操作,减少磁盘 I/O
  831. * 4. 优化:合并打印逻辑中的 ShopExt 查询及订单保存,减少数据库交互
  832. * 5. 优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE
  833. */
  834. public function actionDebtPay()
  835. {
  836. $get = Yii::$app->request->get();
  837. $orderSn = $get['orderSn'] ?? 0;
  838. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  839. PurchaseService::valid($info, $this->shopId);
  840. //解决重复和并发提交
  841. $cacheKey = 'hd_debt_pay_' . $orderSn;
  842. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  843. if (!empty($has)) {
  844. util::fail('请稍等...');
  845. }
  846. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  847. if ($info->status == 5) {
  848. util::fail('订单已取消');
  849. }
  850. if ($info->status != 1) {
  851. util::fail('订单不是待付款状态');
  852. }
  853. if ($info->book == 1) {
  854. util::fail('预售不能记欠款');
  855. }
  856. $ghsId = $info->ghsId;
  857. $ghs = GhsClass::getGhsInfo($ghsId);
  858. $customId = $ghs['customId'] ?? 0;
  859. $custom = CustomClass::getCustom($customId);
  860. if (empty($custom)) {
  861. util::fail('没有找到客户');
  862. }
  863. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  864. util::fail('请先申请赊账权限');
  865. }
  866. if (getenv('YII_ENV') == 'production') {
  867. //源花汇不允许客户自己下欠款单
  868. if ($ghs['mainId'] == 10536) {
  869. util::fail('暂未开通');
  870. }
  871. }
  872. $connection = Yii::$app->db;
  873. $transaction = $connection->beginTransaction();
  874. try {
  875. //延期支付
  876. PurchaseService::debt($info);
  877. $transaction->commit();
  878. if (isset($info->localOrder) && $info->localOrder == 1) {
  879. if ($info->transType != 5 && $info->transType != 4) {
  880. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  881. $current = date("Y_m_d");
  882. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  883. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  884. }
  885. } else {
  886. //标记当天已收一次包装费/运费,岭南批发市场用的功能,只要有成功订单就标记,不再要求金额必须>0
  887. $current = date("Y_m_d");
  888. if (isset($info->sendType) && $info->sendType == 4) {
  889. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  890. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  891. }
  892. noticeUtil::push("运费:" . $info->sendCost . ' sendType:' . $info->sendType, '15280215347');
  893. if (isset($info->sendType) && $info->sendType == 0) {
  894. noticeUtil::push("执行到了", '15280215347');
  895. $sendKey = 'ghs_custom_today_has_get_send_cost_' . $current . '_' . $customId;
  896. Yii::$app->redis->executeCommand('SETEX', [$sendKey, 86400, '1']);
  897. }
  898. }
  899. // 复杂分支/关键逻辑:不是预订单,且供货商配置为直接完成流程,则自动确认收货。
  900. // 优化:复用已有的 $info 实例,避免重复查询;同时将自动确认收货包裹在独立事务中,合并批量写操作,减少磁盘 I/O。
  901. if (!empty($info)) {
  902. if (isset($info->book) && $info->book == 0) {
  903. $ghsShopId = $ghs['shopId'] ?? 0;
  904. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  905. if (!empty($ext) && intval($ext->orderFlow ?? 0) === 0) {
  906. $takeTransaction = Yii::$app->db->beginTransaction();
  907. try {
  908. PurchaseClass::confirmTake($info);
  909. $takeTransaction->commit();
  910. } catch (\Exception $takeEx) {
  911. $takeTransaction->rollBack();
  912. Yii::error("自动确认收货失败: " . $takeEx->getMessage());
  913. throw $takeEx;
  914. }
  915. }
  916. }
  917. }
  918. //app新订单通知
  919. $saleId = $info->saleId ?? 0;
  920. $order = OrderClass::getById($saleId, true);
  921. if (!empty($order)) {
  922. $shopId = $order->shopId ?? 0;
  923. $shop = ShopClass::getById($shopId, true);
  924. if (!empty($shop)) {
  925. //通知供货商
  926. GhsNotifyClass::newOrderNotify($saleId);
  927. }
  928. //订单生成时唤起在线打印
  929. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  930. //有几个地方要同步去修改
  931. if (getenv('YII_ENV') == 'production') {
  932. //源花汇、盛丰不自动打小票,关键词mall_order_no_auto_print
  933. $mallOrderNoPrint = [10536, 44282, 26374];
  934. } else {
  935. $mallOrderNoPrint = [];
  936. }
  937. if (!in_array($order->mainId, $mallOrderNoPrint)) {
  938. // 复杂分支/关键逻辑:优化:只查询一次 $ext,合并 $order->save(),减少数据库交互
  939. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  940. $hasPrintSn = isset($ext->printSn) && !empty($ext->printSn);
  941. OrderClass::onlinePrint($order);
  942. if ($hasPrintSn) {
  943. $order->printNum += 1;
  944. }
  945. //花镜 打二次小票,有多处要修改搜索关键词tow_print
  946. if (in_array($order->mainId, [76796])) {
  947. OrderClass::onlinePrint($order);
  948. if ($hasPrintSn) {
  949. $order->printNum += 1;
  950. }
  951. }
  952. if ($hasPrintSn) {
  953. $order->save();
  954. }
  955. }
  956. }
  957. //付款成功之后呼叫跑腿,关键词 pay_after_call_pt,多处要同步修改
  958. GhsDeliveryOrderClass::payAfter($order);
  959. // 复杂分支/关键逻辑:优化:使用 updateByCondition 直接更新最后下单时间,避免先 SELECT 再 UPDATE 的性能开销
  960. $customId = $order->customId ?? 0;
  961. $ghsId = $order->ghsId ?? 0;
  962. $date = date("Y-m-d H:i:s");
  963. if ($customId > 0) {
  964. CustomClass::updateByCondition(['id' => $customId], ['recentExpend' => $date]);
  965. }
  966. if ($ghsId > 0) {
  967. GhsClass::updateByCondition(['id' => $ghsId], ['recentExpend' => $date]);
  968. }
  969. }
  970. } catch (\Exception $e) {
  971. $transaction->rollBack();
  972. Yii::error("赊账支付失败原因:" . $e->getMessage());
  973. util::fail('支付失败');
  974. }
  975. util::success($info->attributes);
  976. }
  977. /**
  978. * 采购待支付页:客户 xhGhsCustom 净余额支付(并发重试 + 行锁)。
  979. */
  980. public function actionCustomBalancePay()
  981. {
  982. $post = Yii::$app->request->post();
  983. $orderSn = $post['orderSn'] ?? '';
  984. if (empty($orderSn)) {
  985. util::fail('订单号无效');
  986. }
  987. $cacheKey = 'hd_custom_balance_pay_' . $orderSn;
  988. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  989. if (!empty($has)) {
  990. util::fail('请稍等...');
  991. }
  992. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 8, 'has']);
  993. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  994. if (empty($info)) {
  995. util::fail('没有找到采购单');
  996. }
  997. PurchaseService::valid($info, $this->shopId);
  998. $deadline = $info->deadline ?? '';
  999. if (!empty($deadline) && time() > strtotime($deadline)) {
  1000. util::fail('订单已失效了哦');
  1001. }
  1002. $ghsId = intval($info->ghsId ?? 0);
  1003. $ghs = GhsClass::getGhsInfo($ghsId);
  1004. try {
  1005. $result = util::runWithDbConcurrencyRetry(function () use ($orderSn) {
  1006. $connection = Yii::$app->db;
  1007. $transaction = $connection->beginTransaction();
  1008. try {
  1009. $purchase = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1010. $respond = PurchaseService::payByGhsCustomBalance($purchase);
  1011. $transaction->commit();
  1012. return $respond;
  1013. } catch (\Exception $exception) {
  1014. if ($transaction->isActive) {
  1015. $transaction->rollBack();
  1016. }
  1017. throw $exception;
  1018. }
  1019. });
  1020. $info = $result['purchase'];
  1021. $order = $result['saleOrder'] ?? null;
  1022. if (!empty($order)) {
  1023. if (isset($info->book) && $info->book == 0 && !empty($ghs)) {
  1024. $ghsShopId = $ghs['shopId'] ?? 0;
  1025. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  1026. if (!empty($ext) && intval($ext->orderFlow ?? 0) === 0) {
  1027. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1028. if (!empty($cg)) {
  1029. PurchaseClass::confirmTake($cg);
  1030. }
  1031. }
  1032. }
  1033. $shopId = $order->shopId ?? 0;
  1034. $shop = ShopClass::getById($shopId, true);
  1035. if (!empty($shop)) {
  1036. GhsNotifyClass::newOrderNotify($order->id ?? 0);
  1037. }
  1038. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  1039. OrderClass::onlinePrint($order);
  1040. }
  1041. GhsDeliveryOrderClass::payAfter($order);
  1042. $customId = $order->customId ?? 0;
  1043. $custom = CustomClass::getById($customId, true);
  1044. $date = date('Y-m-d H:i:s');
  1045. if (!empty($custom)) {
  1046. $custom->recentExpend = $date;
  1047. $custom->save();
  1048. }
  1049. $orderGhsId = $order->ghsId ?? 0;
  1050. $orderGhs = GhsClass::getById($orderGhsId, true);
  1051. if (!empty($orderGhs)) {
  1052. $orderGhs->recentExpend = $date;
  1053. $orderGhs->save();
  1054. }
  1055. }
  1056. util::success($info->attributes);
  1057. } catch (\Exception $e) {
  1058. Yii::info('客户余额支付失败:' . $e->getMessage());
  1059. util::fail($e->getMessage() ?: '支付失败');
  1060. }
  1061. }
  1062. //余额支付 ssh 2021.1.24
  1063. public function actionBalancePay()
  1064. {
  1065. $shopAdmin = $this->shopAdmin;
  1066. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  1067. util::fail('超管才能操作');
  1068. }
  1069. $get = Yii::$app->request->get();
  1070. $orderSn = $get['orderSn'] ?? 0;
  1071. $password = $get['password'] ?? '';
  1072. // 重新获取,不加密的用户数据
  1073. $admin = AdminClass::getById($this->adminId, true);
  1074. if (password_verify($password, $admin->payPassword) == false) {
  1075. util::fail('密码错误');
  1076. }
  1077. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1078. if (empty($info)) {
  1079. util::fail('没有找到采购单');
  1080. }
  1081. if ($info->status == 5) {
  1082. util::fail('订单已取消');
  1083. }
  1084. if ($info->status != 1) {
  1085. util::fail('订单不是待付款状态');
  1086. }
  1087. PurchaseService::valid($info, $this->shopId);
  1088. $connection = Yii::$app->db;
  1089. $transaction = $connection->beginTransaction();
  1090. try {
  1091. //余额支付
  1092. purchaseService::balancePay($info);
  1093. $transaction->commit();
  1094. } catch (\Exception $e) {
  1095. $transaction->rollBack();
  1096. util::fail('支付失败');
  1097. }
  1098. util::success($info->attributes);
  1099. }
  1100. //采购记录 ssh 2021.1.24
  1101. public function actionList()
  1102. {
  1103. $get = Yii::$app->request->get();
  1104. $where = ['shopId' => $this->shopId];
  1105. $searchTime = $get['searchTime'] ?? '';
  1106. if (!empty($searchTime)) {
  1107. $startTime = $get['startTime'] ?? '';
  1108. $endTime = $get['endTime'] ?? '';
  1109. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1110. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1111. }
  1112. $ghsId = $get['ghsId'] ?? 0;
  1113. if (!empty($ghsId)) {
  1114. $where['ghsId'] = (int)$ghsId;
  1115. }
  1116. $status = $get['status'] ?? 0;
  1117. if (!empty($status)) {
  1118. $where['status'] = $status;
  1119. }
  1120. $respond = PurchaseService::getPurchaseList($where);
  1121. $respond['shop'] = $this->shop->attributes;
  1122. util::success($respond);
  1123. }
  1124. //获取合并采购单列表 ssh 20260519
  1125. public function actionMergeOrderList()
  1126. {
  1127. Yii::$app->params['pageSize'] = 9999;
  1128. $get = Yii::$app->request->get();
  1129. $mergeId = $get['mergeId'] ?? 0;
  1130. if (empty($mergeId)) {
  1131. util::fail('请选择采购单');
  1132. }
  1133. $mergeSn = CgMergeSnClass::getById($mergeId, true);
  1134. if (empty($mergeSn)) {
  1135. util::fail('请选择合并采购单');
  1136. }
  1137. $salt = $get['salt'] ?? '';
  1138. if (!empty($salt)) {
  1139. if (empty($mergeSn->salt) || $mergeSn->salt != $salt) {
  1140. util::fail('此单无法查看哦');
  1141. }
  1142. } else {
  1143. if (empty($this->shopId)) {
  1144. util::fail('暂时不能查看');
  1145. }
  1146. if ($mergeSn->mainId != $this->mainId) {
  1147. util::fail('此单无法查看哈');
  1148. }
  1149. }
  1150. $ghsId = $mergeSn->ghsId ?? 0;
  1151. $where = [
  1152. 'ghsId' => $ghsId,
  1153. 'mergeId' => $mergeId,
  1154. ];
  1155. $respond = PurchaseService::getPurchaseList($where);
  1156. $totalPrice = 0;
  1157. if (!empty($respond['list'])) {
  1158. foreach ($respond['list'] as $one) {
  1159. $actPrice = $one['actPrice'] ?? 0;
  1160. $totalPrice = bcadd($totalPrice, $actPrice, 2);
  1161. }
  1162. }
  1163. $totalPrice = floatval($totalPrice);
  1164. $respond['totalPrice'] = $totalPrice;
  1165. $respond['shop'] = !empty($this->shop) ? $this->shop->attributes : [];
  1166. util::success($respond);
  1167. }
  1168. //采购详情 ssh 2021.01.25
  1169. public function actionDetail()
  1170. {
  1171. $get = Yii::$app->request->get();
  1172. $id = $get['id'] ?? 0;
  1173. $info = PurchaseService::getInfo($id, true, true);
  1174. if (empty($info)) {
  1175. util::fail('没有找到采购单');
  1176. }
  1177. $salt = $get['salt'] ?? '';
  1178. if (!empty($salt)) {
  1179. if (!isset($info['salt']) || $info['salt'] != $salt) {
  1180. util::fail('无效采购单呢!');
  1181. }
  1182. } else {
  1183. if (!isset($info['mainId']) || $info['mainId'] != $this->mainId) {
  1184. //util::fail('采购单不能访问');
  1185. }
  1186. }
  1187. $customId = $info['customId'] ?? 0;
  1188. $custom = CustomClass::getById($customId);
  1189. $customName = $custom['name'] ?? '';
  1190. $customMobile = $custom['mobile'] ?? '';
  1191. $info['customName'] = $customName;
  1192. $info['customMobile'] = $customMobile;
  1193. $hasDebtPay = $custom['debt'] ?? 0;
  1194. $book = $info['book'] ?? 0;
  1195. $presell = $info['presell'] ?? 0;
  1196. //预售不能记欠款
  1197. if ($book == 1) {
  1198. $hasDebtPay = 0;
  1199. }
  1200. if ($presell == 1) {
  1201. $hasDebtPay = 0;
  1202. }
  1203. $shop = $this->shop;
  1204. $info['pfShopId'] = $shop->pfShopId ?? 0;
  1205. $info['hasDebtPay'] = $hasDebtPay;
  1206. // 待支付页展示 xhGhsCustom 净余额(正数=可支付余额),不再使用 main.balance
  1207. $info['balance'] = 0;
  1208. $info['customNetBalance'] = '0.00';
  1209. $info['showBalancePay'] = 0;
  1210. $info['ghsSalt'] = '';
  1211. $ghsId = intval($info['ghsId'] ?? 0);
  1212. if ($ghsId > 0) {
  1213. $ghsRow = GhsClass::getById($ghsId, true);
  1214. if (!empty($ghsRow)) {
  1215. $info['ghsSalt'] = $ghsRow->salt ?? '';
  1216. $ghsCustomId = intval($ghsRow->customId ?? 0);
  1217. if ($ghsCustomId > 0) {
  1218. $ghsCustom = CustomClass::getById($ghsCustomId, true);
  1219. if (!empty($ghsCustom)) {
  1220. $net = AccountMoneyClass::getNetBalanceFromRow($ghsCustom);
  1221. $info['customNetBalance'] = $net;
  1222. if (bccomp($net, '0', 2) > 0) {
  1223. $info['balance'] = floatval($net);
  1224. $info['showBalancePay'] = 1;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. }
  1230. $getPayType = dict::getDict('getPayType');
  1231. $info['getPayType'] = $getPayType;
  1232. //获取售后条件
  1233. $ghsId = $info['ghsId'] ?? 0;
  1234. $ghsInfo = GhsClass::getById($ghsId, true);
  1235. if (empty($ghsInfo)) {
  1236. util::fail('没有找到供货商');
  1237. }
  1238. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  1239. //重要,客户看到的电话修改成门店的联系电话
  1240. $ghsShopId = $ghsInfo->shopId ?? 0;
  1241. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  1242. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  1243. $info['ghsMobile2'] = $ghsShopInfo->telephone2 ?? '';
  1244. $info['hasRenew'] = 1;
  1245. //如果是快捷开单,则不需要显示累计待结,因为商家经常把快捷开单的订单发给客户结账
  1246. $showForPay = 1;
  1247. if ($ghsShopInfo->skCustomId == $info['customId']) {
  1248. $showForPay = 0;
  1249. }
  1250. $info['showForPay'] = $showForPay;
  1251. util::success($info);
  1252. }
  1253. //可用的支付方式 ssh 2021.1.25
  1254. public function actionPayWay()
  1255. {
  1256. $button = [
  1257. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  1258. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  1259. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  1260. ];
  1261. util::success(['button' => $button]);
  1262. }
  1263. //待结款明细 ssh 2021.1.26
  1264. public function actionDebtList()
  1265. {
  1266. $get = Yii::$app->request->get();
  1267. $id = $get['id'] ?? 0;
  1268. $salt = $get['salt'] ?? '';
  1269. $info = GhsClass::getGhsInfo($id);
  1270. if (!empty($salt)) {
  1271. if (!isset($info['salt']) || $info['salt'] != $salt) {
  1272. util::fail('供货商出错了');
  1273. }
  1274. } else {
  1275. if (!isset($info['ownShopId']) || $info['ownShopId'] != $this->shopId) {
  1276. util::fail('错误的供货商.');
  1277. }
  1278. }
  1279. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  1280. $searchTime = $get['searchTime'] ?? '';
  1281. if (!empty($searchTime)) {
  1282. $startTime = $get['startTime'] ?? '';
  1283. $endTime = $get['endTime'] ?? '';
  1284. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1285. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1286. }
  1287. $respond = PurchaseService::getDebtList($where);
  1288. util::success($respond);
  1289. }
  1290. //支付宝支付 ssh 2021.4.11
  1291. public function actionAliPay()
  1292. {
  1293. ini_set('date.timezone', 'Asia/Shanghai');
  1294. $post = Yii::$app->request->post();
  1295. //默认是支付宝支付
  1296. $payWay = $post['payWay'] ?? 1;
  1297. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1298. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1299. if (empty($order)) {
  1300. util::fail('订单号无效');
  1301. }
  1302. if ($order->status == 5) {
  1303. util::fail('订单已取消');
  1304. }
  1305. if ($order->status != 1) {
  1306. util::fail('订单不是待付款状态');
  1307. }
  1308. $deadline = $order->deadline;
  1309. $current = time();
  1310. if (($current + 50) > strtotime($deadline)) {
  1311. util::fail('订单已失效,请重新下单');
  1312. }
  1313. $ghsId = $order->ghsId ?? 0;
  1314. $ghs = GhsClass::getById($ghsId, true);
  1315. if (empty($ghs)) {
  1316. util::fail('没有供货商信息');
  1317. }
  1318. $ghsShopId = $ghs->shopId ?? 0;
  1319. $ghsShop = ShopClass::getById($ghsShopId, true);
  1320. if (empty($ghsShop)) {
  1321. util::fail('没有供货商门店信息');
  1322. }
  1323. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1324. util::fail('商家支付功能未开通');
  1325. }
  1326. //避免重复提交
  1327. util::checkRepeatCommit($orderSn, 8);
  1328. //已经唤起过支付了,根据拉卡拉的规则,需要生成新的订单号
  1329. if ($order->changePrice == 2) {
  1330. $oldOrderSn = $orderSn;
  1331. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1332. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1333. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1334. $params = [
  1335. 'appid' => 'OP00002119',
  1336. 'serial_no' => '018b08cfddbd',
  1337. 'merchant_no' => $ghsShop->lklSjNo,
  1338. 'term_no' => $ghsShop->lklScanTermNo,
  1339. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1340. 'lklCertificatePath' => $lklCertificatePath,
  1341. ];
  1342. $laResource = new Lakala($params);
  1343. $closeParams = [
  1344. 'orderSn' => $oldOrderSn,
  1345. ];
  1346. $response = $laResource->cashClose($closeParams);
  1347. if (isset($response['code']) == false || $response['code'] != '000000') {
  1348. $msg = $response['msg'] ?? '';
  1349. $code = $response['code'] ?? 0;
  1350. if (!in_array($code, ['000091'])) {
  1351. noticeUtil::push('注意花店买花更换单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  1352. }
  1353. }
  1354. $shopId = $order->shopId ?? 0;
  1355. $mainId = $order->mainId ?? 0;
  1356. $customId = $order->customId ?? 0;
  1357. $ghsId = $order->ghsId ?? 0;
  1358. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1359. $newOrderSn = orderSn::getPurchaseSn($snData);
  1360. $orderSn = $newOrderSn;
  1361. $order->orderSn = $newOrderSn;
  1362. $order->save();
  1363. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1364. }
  1365. $order->changePrice = 2;
  1366. $order->save();
  1367. $ghsShopName = $ghsShop->shopName ?? '';
  1368. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1369. $name = $ghsShopName . " 花材 " . $orderSn;
  1370. $totalFee = $order->realPrice;
  1371. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1372. $ghsId = $order->ghsId ?? 0;
  1373. $ghs = GhsClass::getById($ghsId, true);
  1374. if (empty($ghs)) {
  1375. util::fail('没有供货商信息');
  1376. }
  1377. $ghsShopId = $ghs->shopId ?? 0;
  1378. $ghsShop = ShopClass::getById($ghsShopId, true);
  1379. if (empty($ghsShop)) {
  1380. util::fail('没有供货商门店信息');
  1381. }
  1382. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1383. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1384. $params = [
  1385. 'appid' => 'OP00002119',
  1386. 'serial_no' => '018b08cfddbd',
  1387. 'merchant_no' => $ghsShop->lklSjNo,
  1388. 'term_no' => $ghsShop->lklB2BTermNo,
  1389. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1390. 'lklCertificatePath' => $lklCertificatePath,
  1391. ];
  1392. $laResource = new Lakala($params);
  1393. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  1394. $wxParams = [
  1395. 'orderSn' => $orderSn,
  1396. 'amount' => $totalFee,
  1397. 'capitalType' => $purchaseCapital,
  1398. 'notifyUrl' => $notifyUrl,
  1399. 'subject' => $name,
  1400. 'payWay' => $payWay,
  1401. ];
  1402. $response = $laResource->cashierPay($wxParams);
  1403. if (isset($response['code']) == false || $response['code'] != '000000') {
  1404. $errMsg = $response['msg'] ?? '';
  1405. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  1406. util::fail('发起支付失败');
  1407. }
  1408. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  1409. $hasRenew = $ghsShop->hasRenew ?? 0;
  1410. $new = [
  1411. 'payUrl' => $counter_url,
  1412. 'orderSn' => $orderSn,
  1413. 'hasRenew' => 1,
  1414. ];
  1415. util::success($new);
  1416. }
  1417. //微信支付 ssh 2021.4.11
  1418. public function actionWxPay()
  1419. {
  1420. ini_set('date.timezone', 'Asia/Shanghai');
  1421. $post = Yii::$app->request->post();
  1422. $couponId = $post['couponId'] ?? 0;
  1423. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1424. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1425. if (empty($order)) {
  1426. util::fail('订单号无效');
  1427. }
  1428. if ($order->status == 5) {
  1429. util::fail('订单已取消');
  1430. }
  1431. if ($order->status != 1) {
  1432. util::fail('订单不是待付款状态');
  1433. }
  1434. $deadline = $order->deadline;
  1435. $current = time();
  1436. if (($current + 50) > strtotime($deadline)) {
  1437. util::fail('订单已失效,请重新下单');
  1438. }
  1439. //避免重复提交
  1440. util::checkRepeatCommit($orderSn, 8);
  1441. $ghsId = $order->ghsId ?? 0;
  1442. $ghs = GhsClass::getById($ghsId, true);
  1443. if (empty($ghs)) {
  1444. util::fail('没有供货商信息');
  1445. }
  1446. $ghsShopId = $ghs->shopId ?? 0;
  1447. $ghsShop = ShopClass::getById($ghsShopId, true);
  1448. if (empty($ghsShop)) {
  1449. util::fail('没有供货商门店信息');
  1450. }
  1451. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1452. util::fail('商家支付功能未开通');
  1453. }
  1454. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  1455. if ($order->changePrice == 2) {
  1456. $oldOrderSn = $orderSn;
  1457. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1458. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1459. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1460. $params = [
  1461. 'appid' => 'OP00002119',
  1462. 'serial_no' => '018b08cfddbd',
  1463. 'merchant_no' => $ghsShop->lklSjNo,
  1464. 'term_no' => $ghsShop->lklScanTermNo,
  1465. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1466. 'lklCertificatePath' => $lklCertificatePath,
  1467. ];
  1468. $laResource = new Lakala($params);
  1469. $closeParams = [
  1470. 'orderSn' => $oldOrderSn,
  1471. ];
  1472. $response = $laResource->close($closeParams);
  1473. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  1474. $msg = $response['msg'] ?? '';
  1475. $code = $response['code'] ?? 0;
  1476. if (!in_array($code, ['BBS11114'])) {
  1477. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . $code, '15280215347');
  1478. util::fail('出错了,请重新下单');
  1479. }
  1480. }
  1481. $shopId = $order->shopId ?? 0;
  1482. $mainId = $order->mainId ?? 0;
  1483. $customId = $order->customId ?? 0;
  1484. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1485. $newOrderSn = orderSn::getPurchaseSn($snData);
  1486. $orderSn = $newOrderSn;
  1487. $order->orderSn = $newOrderSn;
  1488. $order->save();
  1489. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1490. }
  1491. $id = $order->id;
  1492. $order->changePrice = 2;
  1493. $order->save();
  1494. $ghsShopName = $ghsShop->shopName ?? '';
  1495. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1496. $name = $ghsShopName . " 花材 " . $orderSn;
  1497. $totalFee = $order->realPrice;
  1498. $openId = '';
  1499. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  1500. $openId = $post['currentMiniOpenId'];
  1501. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  1502. }
  1503. if (empty($openId)) {
  1504. if (isset($this->admin) && !empty($this->admin)) {
  1505. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  1506. $openId = $this->admin->miniOpenId;
  1507. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  1508. }
  1509. }
  1510. }
  1511. if (empty($openId)) {
  1512. $currentShopId = $order->shopId ?? 0;
  1513. $currentShop = ShopClass::getById($currentShopId, true);
  1514. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  1515. $currentMobile = $currentShop->mobile ?? '';
  1516. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  1517. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  1518. }
  1519. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1520. //花卉宝代为申请的微信支付
  1521. $merchantExtend = WxOpenClass::getWxInfo();
  1522. if (empty($ghsShop->lklSjNo)) {
  1523. noticeUtil::push('编号31505:单号:' . $orderSn . ' 商家没有开通收款功能:' . $ghsShopName, '15280215347');
  1524. util::fail('商家未开通支付功能');
  1525. }
  1526. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1527. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1528. $params = [
  1529. 'appid' => 'OP00002119',
  1530. 'serial_no' => '018b08cfddbd',
  1531. 'merchant_no' => $ghsShop->lklSjNo,
  1532. 'term_no' => $ghsShop->lklScanTermNo,
  1533. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1534. 'lklCertificatePath' => $lklCertificatePath,
  1535. ];
  1536. $laResource = new Lakala($params);
  1537. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  1538. $wxParams = [
  1539. 'orderSn' => $orderSn,
  1540. 'amount' => $totalFee,
  1541. 'capitalType' => $purchaseCapital,
  1542. 'notifyUrl' => $notifyUrl,
  1543. 'wxAppId' => $merchantExtend['miniAppId'],
  1544. 'openId' => $openId,
  1545. 'subject' => $name,
  1546. 'couponId' => $couponId,
  1547. ];
  1548. $response = $laResource->driveWxPay($wxParams);
  1549. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1550. $errMsg = $response['msg'] ?? '';
  1551. noticeUtil::push('编号129680931:' . $errMsg . ' 单号:' . $orderSn . ' ' . $ghsShopName, '15280215347');
  1552. util::fail('支付失败');
  1553. }
  1554. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1555. $appId = $newParams['app_id'] ?? '';
  1556. $nonceStr = $newParams['nonce_str'] ?? '';
  1557. $paySign = $newParams['pay_sign'] ?? '';
  1558. $package = $newParams['package'] ?? '';
  1559. $signType = $newParams['sign_type'] ?? '';
  1560. $timeStamp = $newParams['time_stamp'] ?? '';
  1561. $new = [
  1562. 'id' => $id,
  1563. 'appId' => $appId,
  1564. 'nonceStr' => $nonceStr,
  1565. 'paySign' => $paySign,
  1566. 'package' => $package,
  1567. 'signType' => $signType,
  1568. 'timeStamp' => $timeStamp,
  1569. 'orderSn' => $orderSn,
  1570. ];
  1571. util::success($new);
  1572. }
  1573. //计算运费 ssh 20221003
  1574. public function actionFreight()
  1575. {
  1576. $get = Yii::$app->request->get();
  1577. $shop = $this->shop;
  1578. $ghsId = $get['ghsId'] ?? 0;
  1579. $ghs = GhsClass::getById($ghsId, true);
  1580. if (empty($ghs)) {
  1581. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1582. }
  1583. $ghsShopId = $ghs->shopId ?? 0;
  1584. $ghsShop = ShopClass::getById($ghsShopId, true);
  1585. if (empty($ghsShop)) {
  1586. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1587. }
  1588. $customId = $ghs->customId ?? 0;
  1589. $custom = CustomClass::getById($customId, true);
  1590. if (empty($custom)) {
  1591. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1592. }
  1593. $weight = $get['weight'] ?? 0;
  1594. if (empty($weight)) {
  1595. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1596. }
  1597. $respond = PurchaseClass::getDistanceFee($shop, $ghsShop, $weight, $custom);
  1598. util::success($respond);
  1599. }
  1600. //运费计算 lqh 2021.4.12 暂时返回固定值
  1601. public function actionFreight2()
  1602. {
  1603. //lqh 2021.6.25 运费固定返回0
  1604. util::success(['sedCost' => 0]);
  1605. }
  1606. }