PurchaseController.php 79 KB

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