PurchaseController.php 59 KB

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