PurchaseController.php 62 KB

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