PurchaseController.php 60 KB

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