PurchaseController.php 55 KB

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