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