PurchaseController.php 59 KB

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