PurchaseController.php 59 KB

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