PurchaseController.php 58 KB

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