PurchaseController.php 57 KB

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