PurchaseController.php 72 KB

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