PurchaseController.php 58 KB

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