PurchaseController.php 58 KB

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