PurchaseController.php 80 KB

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