PurchaseController.php 57 KB

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