PurchaseController.php 73 KB

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