PurchaseController.php 64 KB

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