PurchaseController.php 57 KB

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