PurchaseController.php 78 KB

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