PurchaseController.php 59 KB

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