PurchaseController.php 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  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. util::fail();
  761. }
  762. }
  763. //赊账 ssh 2021.1.24
  764. public function actionDebtPay()
  765. {
  766. $get = Yii::$app->request->get();
  767. $orderSn = $get['orderSn'] ?? 0;
  768. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  769. PurchaseService::valid($info, $this->shopId);
  770. //解决重复和并发提交
  771. $cacheKey = 'hd_debt_pay_' . $orderSn;
  772. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  773. if (!empty($has)) {
  774. util::fail('请稍等...');
  775. }
  776. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  777. if ($info->status == 5) {
  778. util::fail('订单已取消');
  779. }
  780. if ($info->status != 1) {
  781. util::fail('订单不是待付款状态');
  782. }
  783. if ($info->book == 1) {
  784. util::fail('预售不能记欠款');
  785. }
  786. $ghsId = $info->ghsId;
  787. $ghs = GhsClass::getGhsInfo($ghsId);
  788. $customId = $ghs['customId'] ?? 0;
  789. $custom = CustomClass::getCustom($customId);
  790. if (empty($custom)) {
  791. util::fail('没有找到客户');
  792. }
  793. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  794. util::fail('请先申请赊账权限');
  795. }
  796. if (getenv('YII_ENV') == 'production') {
  797. //源花汇不允许客户自己下欠款单
  798. if ($ghs['mainId'] == 10536) {
  799. util::fail('暂未开通');
  800. }
  801. }
  802. $connection = Yii::$app->db;
  803. $transaction = $connection->beginTransaction();
  804. try {
  805. //延期支付
  806. PurchaseService::debt($info);
  807. $transaction->commit();
  808. if (isset($info->localOrder) && $info->localOrder == 1) {
  809. if ($info->transType != 5 && $info->transType != 4) {
  810. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  811. $current = date("Y_m_d");
  812. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  813. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  814. }
  815. } else {
  816. if (isset($info->sendType) && $info->sendType == 4) {
  817. //标记当天已收一次包装费,岭南批发市场用的功能
  818. $current = date("Y_m_d");
  819. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  820. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  821. }
  822. }
  823. //不是预订单,并且供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏、海翔,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  824. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  825. if (!empty($cg)) {
  826. if (isset($cg->book) && $cg->book == 0) {
  827. $ghsShopId = $ghs['shopId'] ?? 0;
  828. $ext = ShopExtClass::getByCondition(['shopId' => $ghsShopId], true, null, 'id,shopId,orderFlow');
  829. if ($ext->orderFlow == 0) {
  830. PurchaseClass::confirmTake($cg);
  831. }
  832. }
  833. }
  834. //app新订单通知
  835. $saleId = $info->saleId ?? 0;
  836. $order = OrderClass::getById($saleId, true);
  837. if (!empty($order)) {
  838. $shopId = $order->shopId ?? 0;
  839. $shop = ShopClass::getById($shopId, true);
  840. if (!empty($shop)) {
  841. //通知供货商
  842. GhsNotifyClass::newOrderNotify($saleId);
  843. }
  844. //订单生成时唤起在线打印
  845. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  846. //有几个地方要同步去修改
  847. if (getenv('YII_ENV') == 'production') {
  848. //源花汇、盛丰不自动打小票,关键词mall_order_no_auto_print
  849. $mallOrderNoPrint = [10536, 44282, 26374];
  850. } else {
  851. $mallOrderNoPrint = [];
  852. }
  853. if (in_array($order->mainId, $mallOrderNoPrint)) {
  854. } else {
  855. OrderClass::onlinePrint($order);
  856. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  857. if (isset($ext->printSn) && !empty($ext->printSn)) {
  858. $order->printNum += 1;
  859. $order->save();
  860. }
  861. //花镜 打二次小票,有多处要修改搜索关键词tow_print
  862. if (in_array($order->mainId, [76796])) {
  863. OrderClass::onlinePrint($order);
  864. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  865. if (isset($ext->printSn) && !empty($ext->printSn)) {
  866. $order->printNum += 1;
  867. $order->save();
  868. }
  869. }
  870. }
  871. }
  872. //付款成功之后呼叫跑腿,关键词 pay_after_call_pt,多处要同步修改
  873. GhsDeliveryOrderClass::payAfter($order);
  874. //更新最后下单时间
  875. $customId = $order->customId ?? 0;
  876. $custom = CustomClass::getById($customId, true);
  877. $date = date("Y-m-d H:i:s");
  878. if (!empty($custom)) {
  879. $custom->recentExpend = $date;
  880. $custom->save();
  881. }
  882. $ghsId = $order->ghsId ?? 0;
  883. $ghs = GhsClass::getById($ghsId, true);
  884. if (!empty($ghs)) {
  885. $ghs->recentExpend = $date;
  886. $ghs->save();
  887. }
  888. }
  889. } catch (\Exception $e) {
  890. $transaction->rollBack();
  891. util::fail('支付失败');
  892. }
  893. util::success($info->attributes);
  894. }
  895. //余额支付 ssh 2021.1.24
  896. public function actionBalancePay()
  897. {
  898. $shopAdmin = $this->shopAdmin;
  899. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  900. util::fail('超管才能操作');
  901. }
  902. $get = Yii::$app->request->get();
  903. $orderSn = $get['orderSn'] ?? 0;
  904. $password = $get['password'] ?? '';
  905. // 重新获取,不加密的用户数据
  906. $admin = AdminClass::getById($this->adminId, true);
  907. if (password_verify($password, $admin->payPassword) == false) {
  908. util::fail('密码错误');
  909. }
  910. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  911. if (empty($info)) {
  912. util::fail('没有找到采购单');
  913. }
  914. if ($info->status == 5) {
  915. util::fail('订单已取消');
  916. }
  917. if ($info->status != 1) {
  918. util::fail('订单不是待付款状态');
  919. }
  920. PurchaseService::valid($info, $this->shopId);
  921. $connection = Yii::$app->db;
  922. $transaction = $connection->beginTransaction();
  923. try {
  924. //余额支付
  925. purchaseService::balancePay($info);
  926. $transaction->commit();
  927. } catch (\Exception $e) {
  928. $transaction->rollBack();
  929. util::fail('支付失败');
  930. }
  931. util::success($info->attributes);
  932. }
  933. //采购记录 ssh 2021.1.24
  934. public function actionList()
  935. {
  936. $get = Yii::$app->request->get();
  937. $where = ['shopId' => $this->shopId];
  938. $searchTime = $get['searchTime'] ?? '';
  939. if (!empty($searchTime)) {
  940. $startTime = $get['startTime'] ?? '';
  941. $endTime = $get['endTime'] ?? '';
  942. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  943. $where['payTime'] = ['between', [$period['startTime'], $period['endTime']]];
  944. }
  945. $ghsId = $get['ghsId'] ?? 0;
  946. if (!empty($ghsId)) {
  947. $where['ghsId'] = $ghsId;
  948. }
  949. $status = $get['status'] ?? 0;
  950. if (!empty($status)) {
  951. $where['status'] = $status;
  952. }
  953. $respond = PurchaseService::getPurchaseList($where);
  954. $respond['shop'] = $this->shop->attributes;
  955. util::success($respond);
  956. }
  957. //获取合并采购单列表 ssh 20260519
  958. public function actionMergeOrderList()
  959. {
  960. Yii::$app->params['pageSize'] = 9999;
  961. $get = Yii::$app->request->get();
  962. $mergeId = $get['mergeId'] ?? 0;
  963. if (empty($mergeId)) {
  964. util::fail('请选择采购单');
  965. }
  966. $mergeSn = CgMergeSnClass::getById($mergeId, true);
  967. if (empty($mergeSn)) {
  968. util::fail('请选择合并采购单');
  969. }
  970. $salt = $get['salt'] ?? '';
  971. if (empty($this->shopId)) {
  972. if (empty($salt)) {
  973. util::fail('请求参数错误');
  974. }
  975. if (empty($mergeSn->salt) || $mergeSn->salt != $salt) {
  976. util::fail('找不到订单');
  977. }
  978. } else {
  979. if ($mergeSn->mainId != $this->mainId) {
  980. util::fail('不是你的订单');
  981. }
  982. }
  983. $ghsId = $mergeSn->ghsId ?? 0;
  984. $where = [
  985. 'ghsId' => $ghsId,
  986. 'mergeId' => $mergeId,
  987. ];
  988. $respond = PurchaseService::getPurchaseList($where);
  989. $totalPrice = 0;
  990. if (!empty($respond['list'])) {
  991. foreach ($respond['list'] as $one) {
  992. $actPrice = $one['actPrice'] ?? 0;
  993. $totalPrice = bcadd($totalPrice, $actPrice, 2);
  994. }
  995. }
  996. $totalPrice = floatval($totalPrice);
  997. $respond['totalPrice'] = $totalPrice;
  998. $respond['shop'] = !empty($this->shop) ? $this->shop->attributes : [];
  999. util::success($respond);
  1000. }
  1001. //采购详情 ssh 2021.01.25
  1002. public function actionDetail()
  1003. {
  1004. $get = Yii::$app->request->get();
  1005. $id = $get['id'] ?? 0;
  1006. $info = PurchaseService::getInfo($id, true, true);
  1007. if (empty($info)) {
  1008. util::fail('没有找到采购单');
  1009. }
  1010. if (empty($this->shopId)) {
  1011. $salt = $get['salt'] ?? '';
  1012. if (empty($salt)) {
  1013. util::fail('请求参数错误');
  1014. }
  1015. if (empty($info['salt']) || $info['salt'] != $salt) {
  1016. util::fail('没有找到采购单');
  1017. }
  1018. }
  1019. if (empty($info['mainId']) || $info['mainId'] != $this->mainId) {
  1020. //不能注释,分享付款功能要用,客户没有登录也能付款
  1021. //util::fail('无法访问');
  1022. }
  1023. $customId = $info['customId'] ?? 0;
  1024. $custom = CustomClass::getById($customId);
  1025. $customName = $custom['name'] ?? '';
  1026. $customMobile = $custom['mobile'] ?? '';
  1027. $info['customName'] = $customName;
  1028. $info['customMobile'] = $customMobile;
  1029. $hasDebtPay = $custom['debt'] ?? 0;
  1030. $book = $info['book'] ?? 0;
  1031. $presell = $info['presell'] ?? 0;
  1032. //预售不能记欠款
  1033. if ($book == 1) {
  1034. $hasDebtPay = 0;
  1035. }
  1036. if ($presell == 1) {
  1037. $hasDebtPay = 0;
  1038. }
  1039. $shop = $this->shop;
  1040. $info['pfShopId'] = $shop->pfShopId ?? 0;
  1041. $info['hasDebtPay'] = $hasDebtPay;
  1042. $info['balance'] = $shop->balance ?? 0;
  1043. $getPayType = dict::getDict('getPayType');
  1044. $info['getPayType'] = $getPayType;
  1045. //获取售后条件
  1046. $ghsId = $info['ghsId'] ?? 0;
  1047. $ghsInfo = GhsClass::getById($ghsId, true);
  1048. if (empty($ghsInfo)) {
  1049. util::fail('没有找到供货商');
  1050. }
  1051. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  1052. //重要,客户看到的电话修改成门店的联系电话
  1053. $ghsShopId = $ghsInfo->shopId ?? 0;
  1054. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  1055. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  1056. $info['ghsMobile2'] = $ghsShopInfo->telephone2 ?? '';
  1057. $info['hasRenew'] = 1;
  1058. //如果是快捷开单,则不需要显示累计待结,因为商家经常把快捷开单的订单发给客户结账
  1059. $showForPay = 1;
  1060. if ($ghsShopInfo->skCustomId == $info['customId']) {
  1061. $showForPay = 0;
  1062. }
  1063. $info['showForPay'] = $showForPay;
  1064. util::success($info);
  1065. }
  1066. //可用的支付方式 ssh 2021.1.25
  1067. public function actionPayWay()
  1068. {
  1069. $button = [
  1070. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  1071. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  1072. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  1073. ];
  1074. util::success(['button' => $button]);
  1075. }
  1076. //待结款明细 ssh 2021.1.26
  1077. public function actionDebtList()
  1078. {
  1079. $get = Yii::$app->request->get();
  1080. $id = $get['id'] ?? 0;
  1081. $info = GhsClass::getGhsInfo($id);
  1082. GhsClass::valid($info, $this->shopId);
  1083. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  1084. $searchTime = $get['searchTime'] ?? '';
  1085. if (!empty($searchTime)) {
  1086. $startTime = $get['startTime'] ?? '';
  1087. $endTime = $get['endTime'] ?? '';
  1088. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  1089. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  1090. }
  1091. $respond = PurchaseService::getDebtList($where);
  1092. util::success($respond);
  1093. }
  1094. //支付宝支付 ssh 2021.4.11
  1095. public function actionAliPay()
  1096. {
  1097. ini_set('date.timezone', 'Asia/Shanghai');
  1098. $post = Yii::$app->request->post();
  1099. //默认是支付宝支付
  1100. $payWay = $post['payWay'] ?? 1;
  1101. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1102. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1103. if (empty($order)) {
  1104. util::fail('订单号无效');
  1105. }
  1106. if ($order->status == 5) {
  1107. util::fail('订单已取消');
  1108. }
  1109. if ($order->status != 1) {
  1110. util::fail('订单不是待付款状态');
  1111. }
  1112. $deadline = $order->deadline;
  1113. $current = time();
  1114. if (($current + 50) > strtotime($deadline)) {
  1115. util::fail('订单已失效,请重新下单');
  1116. }
  1117. $ghsId = $order->ghsId ?? 0;
  1118. $ghs = GhsClass::getById($ghsId, true);
  1119. if (empty($ghs)) {
  1120. util::fail('没有供货商信息');
  1121. }
  1122. $ghsShopId = $ghs->shopId ?? 0;
  1123. $ghsShop = ShopClass::getById($ghsShopId, true);
  1124. if (empty($ghsShop)) {
  1125. util::fail('没有供货商门店信息');
  1126. }
  1127. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1128. util::fail('商家支付功能未开通');
  1129. }
  1130. //避免重复提交
  1131. util::checkRepeatCommit($orderSn, 8);
  1132. //已经唤起过支付了,根据拉卡拉的规则,需要生成新的订单号
  1133. if ($order->changePrice == 2) {
  1134. $oldOrderSn = $orderSn;
  1135. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1136. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1137. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1138. $params = [
  1139. 'appid' => 'OP00002119',
  1140. 'serial_no' => '018b08cfddbd',
  1141. 'merchant_no' => $ghsShop->lklSjNo,
  1142. 'term_no' => $ghsShop->lklScanTermNo,
  1143. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1144. 'lklCertificatePath' => $lklCertificatePath,
  1145. ];
  1146. $laResource = new Lakala($params);
  1147. $closeParams = [
  1148. 'orderSn' => $oldOrderSn,
  1149. ];
  1150. $response = $laResource->cashClose($closeParams);
  1151. if (isset($response['code']) == false || $response['code'] != '000000') {
  1152. $msg = $response['msg'] ?? '';
  1153. $code = $response['code'] ?? 0;
  1154. if (!in_array($code, ['000091'])) {
  1155. noticeUtil::push('注意花店买花更换单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  1156. }
  1157. }
  1158. $shopId = $order->shopId ?? 0;
  1159. $mainId = $order->mainId ?? 0;
  1160. $customId = $order->customId ?? 0;
  1161. $ghsId = $order->ghsId ?? 0;
  1162. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1163. $newOrderSn = orderSn::getPurchaseSn($snData);
  1164. $orderSn = $newOrderSn;
  1165. $order->orderSn = $newOrderSn;
  1166. $order->save();
  1167. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1168. }
  1169. $order->changePrice = 2;
  1170. $order->save();
  1171. $ghsShopName = $ghsShop->shopName ?? '';
  1172. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1173. $name = $ghsShopName . " 花材 " . $orderSn;
  1174. $totalFee = $order->realPrice;
  1175. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1176. $ghsId = $order->ghsId ?? 0;
  1177. $ghs = GhsClass::getById($ghsId, true);
  1178. if (empty($ghs)) {
  1179. util::fail('没有供货商信息');
  1180. }
  1181. $ghsShopId = $ghs->shopId ?? 0;
  1182. $ghsShop = ShopClass::getById($ghsShopId, true);
  1183. if (empty($ghsShop)) {
  1184. util::fail('没有供货商门店信息');
  1185. }
  1186. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1187. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1188. $params = [
  1189. 'appid' => 'OP00002119',
  1190. 'serial_no' => '018b08cfddbd',
  1191. 'merchant_no' => $ghsShop->lklSjNo,
  1192. 'term_no' => $ghsShop->lklB2BTermNo,
  1193. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1194. 'lklCertificatePath' => $lklCertificatePath,
  1195. ];
  1196. $laResource = new Lakala($params);
  1197. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  1198. $wxParams = [
  1199. 'orderSn' => $orderSn,
  1200. 'amount' => $totalFee,
  1201. 'capitalType' => $purchaseCapital,
  1202. 'notifyUrl' => $notifyUrl,
  1203. 'subject' => $name,
  1204. 'payWay' => $payWay,
  1205. ];
  1206. $response = $laResource->cashierPay($wxParams);
  1207. if (isset($response['code']) == false || $response['code'] != '000000') {
  1208. $errMsg = $response['msg'] ?? '';
  1209. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  1210. util::fail('发起支付失败');
  1211. }
  1212. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  1213. $hasRenew = $ghsShop->hasRenew ?? 0;
  1214. $new = [
  1215. 'payUrl' => $counter_url,
  1216. 'orderSn' => $orderSn,
  1217. 'hasRenew' => 1,
  1218. ];
  1219. util::success($new);
  1220. }
  1221. //微信支付 ssh 2021.4.11
  1222. public function actionWxPay()
  1223. {
  1224. ini_set('date.timezone', 'Asia/Shanghai');
  1225. $post = Yii::$app->request->post();
  1226. $couponId = $post['couponId'] ?? 0;
  1227. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  1228. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  1229. if (empty($order)) {
  1230. util::fail('订单号无效');
  1231. }
  1232. if ($order->status == 5) {
  1233. util::fail('订单已取消');
  1234. }
  1235. if ($order->status != 1) {
  1236. util::fail('订单不是待付款状态');
  1237. }
  1238. $deadline = $order->deadline;
  1239. $current = time();
  1240. if (($current + 50) > strtotime($deadline)) {
  1241. util::fail('订单已失效,请重新下单');
  1242. }
  1243. //避免重复提交
  1244. util::checkRepeatCommit($orderSn, 8);
  1245. $ghsId = $order->ghsId ?? 0;
  1246. $ghs = GhsClass::getById($ghsId, true);
  1247. if (empty($ghs)) {
  1248. util::fail('没有供货商信息');
  1249. }
  1250. $ghsShopId = $ghs->shopId ?? 0;
  1251. $ghsShop = ShopClass::getById($ghsShopId, true);
  1252. if (empty($ghsShop)) {
  1253. util::fail('没有供货商门店信息');
  1254. }
  1255. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  1256. util::fail('商家支付功能未开通');
  1257. }
  1258. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  1259. if ($order->changePrice == 2) {
  1260. $oldOrderSn = $orderSn;
  1261. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  1262. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1263. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1264. $params = [
  1265. 'appid' => 'OP00002119',
  1266. 'serial_no' => '018b08cfddbd',
  1267. 'merchant_no' => $ghsShop->lklSjNo,
  1268. 'term_no' => $ghsShop->lklScanTermNo,
  1269. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1270. 'lklCertificatePath' => $lklCertificatePath,
  1271. ];
  1272. $laResource = new Lakala($params);
  1273. $closeParams = [
  1274. 'orderSn' => $oldOrderSn,
  1275. ];
  1276. $response = $laResource->close($closeParams);
  1277. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  1278. $msg = $response['msg'] ?? '';
  1279. $code = $response['code'] ?? 0;
  1280. if (!in_array($code, ['BBS11114'])) {
  1281. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . $code, '15280215347');
  1282. util::fail('出错了,请重新下单');
  1283. }
  1284. }
  1285. $shopId = $order->shopId ?? 0;
  1286. $mainId = $order->mainId ?? 0;
  1287. $customId = $order->customId ?? 0;
  1288. $snData = ['shopId' => $shopId, 'mainId' => $mainId, 'customId' => $customId, 'ghsId' => $ghsId];
  1289. $newOrderSn = orderSn::getPurchaseSn($snData);
  1290. $orderSn = $newOrderSn;
  1291. $order->orderSn = $newOrderSn;
  1292. $order->save();
  1293. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1294. }
  1295. $id = $order->id;
  1296. $order->changePrice = 2;
  1297. $order->save();
  1298. $ghsShopName = $ghsShop->shopName ?? '';
  1299. //要带上哪个店的,这样才知道客户下的单是哪个店的,客户很多有多家店
  1300. $name = $ghsShopName . " 花材 " . $orderSn;
  1301. $totalFee = $order->realPrice;
  1302. $openId = '';
  1303. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  1304. $openId = $post['currentMiniOpenId'];
  1305. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  1306. }
  1307. if (empty($openId)) {
  1308. if (isset($this->admin) && !empty($this->admin)) {
  1309. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  1310. $openId = $this->admin->miniOpenId;
  1311. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  1312. }
  1313. }
  1314. }
  1315. if (empty($openId)) {
  1316. $currentShopId = $order->shopId ?? 0;
  1317. $currentShop = ShopClass::getById($currentShopId, true);
  1318. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  1319. $currentMobile = $currentShop->mobile ?? '';
  1320. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  1321. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  1322. }
  1323. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1324. //花卉宝代为申请的微信支付
  1325. $merchantExtend = WxOpenClass::getWxInfo();
  1326. if (empty($ghsShop->lklSjNo)) {
  1327. noticeUtil::push('编号31505:单号:' . $orderSn . ' 商家没有开通收款功能:' . $ghsShopName, '15280215347');
  1328. util::fail('商家未开通支付功能');
  1329. }
  1330. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1331. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1332. $params = [
  1333. 'appid' => 'OP00002119',
  1334. 'serial_no' => '018b08cfddbd',
  1335. 'merchant_no' => $ghsShop->lklSjNo,
  1336. 'term_no' => $ghsShop->lklScanTermNo,
  1337. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1338. 'lklCertificatePath' => $lklCertificatePath,
  1339. ];
  1340. $laResource = new Lakala($params);
  1341. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  1342. $wxParams = [
  1343. 'orderSn' => $orderSn,
  1344. 'amount' => $totalFee,
  1345. 'capitalType' => $purchaseCapital,
  1346. 'notifyUrl' => $notifyUrl,
  1347. 'wxAppId' => $merchantExtend['miniAppId'],
  1348. 'openId' => $openId,
  1349. 'subject' => $name,
  1350. 'couponId' => $couponId,
  1351. ];
  1352. $response = $laResource->driveWxPay($wxParams);
  1353. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  1354. $errMsg = $response['msg'] ?? '';
  1355. noticeUtil::push('编号129680931:' . $errMsg . ' 单号:' . $orderSn . ' ' . $ghsShopName, '15280215347');
  1356. util::fail('支付失败');
  1357. }
  1358. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1359. $appId = $newParams['app_id'] ?? '';
  1360. $nonceStr = $newParams['nonce_str'] ?? '';
  1361. $paySign = $newParams['pay_sign'] ?? '';
  1362. $package = $newParams['package'] ?? '';
  1363. $signType = $newParams['sign_type'] ?? '';
  1364. $timeStamp = $newParams['time_stamp'] ?? '';
  1365. $new = [
  1366. 'id' => $id,
  1367. 'appId' => $appId,
  1368. 'nonceStr' => $nonceStr,
  1369. 'paySign' => $paySign,
  1370. 'package' => $package,
  1371. 'signType' => $signType,
  1372. 'timeStamp' => $timeStamp,
  1373. 'orderSn' => $orderSn,
  1374. ];
  1375. util::success($new);
  1376. }
  1377. //计算运费 ssh 20221003
  1378. public function actionFreight()
  1379. {
  1380. $get = Yii::$app->request->get();
  1381. $shop = $this->shop;
  1382. $ghsId = $get['ghsId'] ?? 0;
  1383. $ghs = GhsClass::getById($ghsId, true);
  1384. if (empty($ghs)) {
  1385. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1386. }
  1387. $ghsShopId = $ghs->shopId ?? 0;
  1388. $ghsShop = ShopClass::getById($ghsShopId, true);
  1389. if (empty($ghsShop)) {
  1390. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1391. }
  1392. $customId = $ghs->customId ?? 0;
  1393. $custom = CustomClass::getById($customId, true);
  1394. if (empty($custom)) {
  1395. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1396. }
  1397. $weight = $get['weight'] ?? 0;
  1398. if (empty($weight)) {
  1399. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1400. }
  1401. $respond = PurchaseClass::getDistanceFee($shop, $ghsShop, $weight, $custom);
  1402. util::success($respond);
  1403. }
  1404. //运费计算 lqh 2021.4.12 暂时返回固定值
  1405. public function actionFreight2()
  1406. {
  1407. //lqh 2021.6.25 运费固定返回0
  1408. util::success(['sedCost' => 0]);
  1409. }
  1410. }