PurchaseController.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\ghs\classes\GhsClass;
  5. use biz\notice\classes\NoticeClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExpressClass;
  8. use biz\shop\classes\ShopExtClass;
  9. use bizGhs\custom\classes\CustomClass;
  10. use bizGhs\express\services\DadaExpressServices;
  11. use bizGhs\order\classes\OrderClass;
  12. use bizGhs\order\classes\OrderItemClass;
  13. use bizHd\purchase\classes\PurchaseClass;
  14. use bizHd\purchase\classes\PurchaseItemClass;
  15. use bizHd\purchase\services\PurchaseService;
  16. use bizGhs\product\classes\ProductClass;
  17. use bizHd\wx\classes\WxOpenClass;
  18. use common\components\dateUtil;
  19. use common\components\dict;
  20. use common\components\freight;
  21. use common\components\httpUtil;
  22. use common\components\imgUtil;
  23. use common\components\noticeUtil;
  24. use common\components\orderSn;
  25. use Yii;
  26. use common\components\util;
  27. use biz\wx\classes\WxMessageClass;
  28. use bizGhs\order\services\OrderService;
  29. use common\components\lakala\Lakala;
  30. use common\components\qrCodeUtil;
  31. class PurchaseController extends BaseController
  32. {
  33. public $guestAccess = ['detail', 'ali-pay', 'get-ali-pay-code'];
  34. //获取支付宝付款码 ssh 20240713
  35. public function actionGetAliPayCode()
  36. {
  37. $get = Yii::$app->request->get();
  38. $orderSn = $get['orderSn'] ?? '';
  39. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  40. if (empty($info)) {
  41. util::fail('没有找到采购单');
  42. }
  43. if (isset($info->mainId) == false || $info->mainId != $this->mainId) {
  44. util::fail('无法访问');
  45. }
  46. if (getenv('YII_ENV') == 'production') {
  47. $url = "https://mall.huahb.cn/#/admin/cg/alipay?orderSn={$orderSn}";
  48. } else {
  49. $url = "https://mall.huaml.com/#/admin/cg/alipay?orderSn={$orderSn}";
  50. }
  51. $unique = "order_ali_pay_" . $orderSn;
  52. $imgUrl = qrCodeUtil::generateRechargeQrCode($url, $unique);
  53. $imageUrl = imgUtil::groupImg($imgUrl);
  54. util::success(['imgUrl' => $imageUrl]);
  55. }
  56. //确认收货 ssh 20231119
  57. public function actionConfirmTake()
  58. {
  59. $get = Yii::$app->request->get();
  60. $id = $get['id'] ?? 0;
  61. $info = PurchaseClass::getById($id, true);
  62. if (empty($info)) {
  63. util::fail('没有找到采购单');
  64. }
  65. if (isset($info->mainId) == false || $info->mainId != $this->mainId) {
  66. util::fail('无法访问');
  67. }
  68. PurchaseClass::confirmTake($info);
  69. util::complete('确认成功');
  70. }
  71. public function actionGetFullInfo()
  72. {
  73. $get = Yii::$app->request->get();
  74. $id = $get['id'] ?? 0;
  75. $info = PurchaseClass::getById($id);
  76. if (empty($info)) {
  77. util::fail('没有找到采购单');
  78. }
  79. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  80. util::fail('无法访问');
  81. }
  82. $orderSn = $info['orderSn'] ?? '';
  83. $ghsId = $info['ghsId'] ?? 0;
  84. $info['ghsShopId'] = 0;
  85. if (!empty($ghsId)) {
  86. $ghs = GhsClass::getById($ghsId, true);
  87. $info['ghsShopId'] = $ghs->shopId ?? 0;
  88. }
  89. $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  90. if (!empty($list)) {
  91. foreach ($list as $key => $item) {
  92. $shortCover = $item['cover'] ?? '';
  93. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  94. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  95. $list[$key]['cover'] = $cover;
  96. $list[$key]['bigCover'] = $bigCover;
  97. $list[$key]['shortCover'] = $shortCover;
  98. }
  99. }
  100. $info['product'] = $list;
  101. util::success($info);
  102. }
  103. //虚拟供货商的采购 ssh 20230308
  104. public function actionAddVirtualCg()
  105. {
  106. $post = Yii::$app->request->post();
  107. $ghsId = $post['ghsId'] ?? 0;
  108. $payMode = $post['payMode'] ?? 0;
  109. $payWay = $post['payWay'] ?? 0;
  110. $ghsInfo = GhsClass::getById($ghsId);
  111. if (empty($ghsInfo)) {
  112. util::fail('没有找到供货商');
  113. }
  114. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  115. util::fail('不能向自己的店买花');
  116. }
  117. $connection = Yii::$app->db;
  118. $transaction = $connection->beginTransaction();
  119. try {
  120. $post['book'] = $post['book'] ?? 0;
  121. $post['sjId'] = $this->sjId;
  122. $post['shopId'] = $this->shopId;
  123. $post['shopAdminId'] = $this->shopAdminId;
  124. $post['ghsId'] = $ghsId;
  125. $post['ghsName'] = $ghsInfo['name'] ?? '';
  126. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  127. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  128. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  129. $customId = $ghsInfo['customId'] ?? 0;
  130. $post['customId'] = $customId;
  131. $shopAdmin = $this->shopAdmin ?? null;
  132. $name = $shopAdmin->name ?? '';
  133. $post['shopAdminName'] = $name;
  134. $post['mainId'] = $this->mainId;
  135. $product = $post['product'] ?? '';
  136. $productList = json_decode($product, true);
  137. //判断product数据是不是同个供货商的
  138. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  139. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  140. if (!empty($productInfoList)) {
  141. foreach ($productInfoList as $currentInfo) {
  142. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  143. util::fail('只能选择同一家的商品哦');
  144. }
  145. }
  146. if (count($productInfoList) != count($ids)) {
  147. util::fail('存在无效商品');
  148. }
  149. } else {
  150. util::fail('花材不存在');
  151. }
  152. //供货商预订单最低公斤数要求和每公斤服务费
  153. $ghsInfo['kiloFee'] = 0;
  154. $ghsInfo['miniKilo'] = 0;
  155. $post['product'] = $productList;
  156. $packCost = 0;
  157. $post['packCost'] = $packCost;
  158. $post['sendCost'] = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  159. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  160. if ($payMode == 0) {
  161. //欠款
  162. PurchaseService::debt($respond);
  163. } else {
  164. //线下支付
  165. PurchaseService::payAfter($respond, $payWay);
  166. $saleId = $respond->saleId ?? 0;
  167. $order = OrderClass::getById($saleId, true);
  168. if (empty($order)) {
  169. util::fail('没有找到订单');
  170. }
  171. OrderService::payAfter($order, $payWay, true);
  172. }
  173. $transaction->commit();
  174. util::success($respond);
  175. } catch (Exception $e) {
  176. $transaction->rollBack();
  177. util::fail();
  178. }
  179. }
  180. //生成采购单 ssh 2021.1.17
  181. public function actionCreateOrder()
  182. {
  183. $post = Yii::$app->request->post();
  184. //多颜色数据整理
  185. $colorItem = $post['xj'] ?? [];
  186. $newXj = [];
  187. if (!empty($colorItem)) {
  188. $colorData = json_decode($colorItem, true);
  189. if (!empty($colorData) && is_array($colorData)) {
  190. foreach ($colorData as $colorInfo) {
  191. $ptItemId = $colorInfo['ptItemId'] ?? 0;
  192. $colorList = $colorInfo['list'] ?? [];
  193. if (!empty($colorList)) {
  194. foreach ($colorList as $colorItemKey => $colorItem) {
  195. $newXj[$ptItemId][] = $colorItem;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. $post['xj'] = $newXj;
  202. $ghsId = $post['ghsId'] ?? 0;
  203. //供货商
  204. $ghsInfo = GhsClass::getById($ghsId);
  205. if (empty($ghsInfo)) {
  206. util::fail('没有找到供货商');
  207. }
  208. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  209. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  210. util::fail('不能跟自己的店买花');
  211. }
  212. $connection = Yii::$app->db;
  213. $transaction = $connection->beginTransaction();
  214. try {
  215. $post['book'] = $post['book'] ?? 0;
  216. $post['sjId'] = $this->sjId;
  217. $post['shopId'] = $this->shopId;
  218. $post['shopAdminId'] = $this->shopAdminId;
  219. $post['ghsId'] = $ghsId;
  220. $post['ghsName'] = $ghsInfo['name'] ?? '';
  221. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  222. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  223. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  224. $customId = $ghsInfo['customId'] ?? 0;
  225. $post['customId'] = $customId;
  226. $shopAdmin = $this->shopAdmin ?? null;
  227. $name = $shopAdmin->name ?? '';
  228. $post['shopAdminName'] = $name;
  229. $post['mainId'] = $this->mainId;
  230. $product = $post['product'] ?? '';
  231. $productList = json_decode($product, true);
  232. //判断product数据是不是同个供货商的
  233. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  234. $ghsMainId = $ghsShopInfo->mainId ?? 0;
  235. $isOpen = ShopClass::isOpen($ghsShopInfo);
  236. $book = $post['book'] ?? 0;
  237. if ($isOpen == 0 && $book == 0) {
  238. util::fail('本店已休息,请选择其它门店');
  239. }
  240. if (isset($post['transType']) && $post['transType'] == 4) {
  241. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  242. $notSameCity = PurchaseClass::notSameCity($this->shop, $ghsShopInfo);
  243. if ($notSameCity == false) {
  244. util::fail('距离太远,不能选择同城配送');
  245. }
  246. }
  247. }
  248. if ($book == 1) {
  249. $sendTimeWant = isset($post['sendTimeWant']) && !empty($post['sendTimeWant']) ? $post['sendTimeWant'] : date("Y-m-d");
  250. $future = date("Y-m-d", strtotime('+2 day'));
  251. if (strtotime($sendTimeWant) < strtotime($future)) {
  252. //util::fail('请选择时间');
  253. }
  254. $nowTime = strtotime(date("Y-m-d"));
  255. if (strtotime($sendTimeWant) < $nowTime) {
  256. util::fail('不能选择过去时间');
  257. }
  258. }
  259. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  260. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  261. if (!empty($productInfoList)) {
  262. $presellData = [];
  263. $nowTime = strtotime(date("Y-m-d"));
  264. foreach ($productInfoList as $currentInfo) {
  265. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $ghsMainId) {
  266. util::fail('只能选择同一家的商品哦');
  267. }
  268. $currentName = $currentInfo['name'] ?? '';
  269. if (isset($currentInfo['frontHide']) && $currentInfo['frontHide'] == 1) {
  270. util::fail($currentName . '没有库存,请删除');
  271. }
  272. $presell = $currentInfo['presell'] ?? 0;
  273. $presellData[] = $presell;
  274. if ($presell == 1) {
  275. if ($book == 1) {
  276. util::fail('预订时不能选择预售花材');
  277. } else {
  278. if (isset($post['sendTimeWant']) == false || empty($post['sendTimeWant'])) {
  279. util::fail('请选择配送日期');
  280. }
  281. $sendTimeWant = $post['sendTimeWant'];
  282. if (strtotime($sendTimeWant) < $nowTime) {
  283. util::fail('不能选择过去时间');
  284. }
  285. $hasDateStr = $currentInfo['presellDate'] ?? [];
  286. $hasDate = explode(',', trim($hasDateStr));
  287. if (empty($hasDate)) {
  288. util::fail('预售日期有问题,请提醒门店');
  289. }
  290. if (in_array(strtotime($sendTimeWant), $hasDate) == false) {
  291. util::fail("有预售花材在{$sendTimeWant}没到货");
  292. }
  293. $post['presell'] = 1;
  294. }
  295. }
  296. }
  297. if (count($productInfoList) != count($ids)) {
  298. util::fail('存在无效商品');
  299. }
  300. $presellData = array_unique($presellData);
  301. if (count($presellData) > 1) {
  302. util::fail('预售和非预售花材不能一起下单');
  303. }
  304. } else {
  305. util::fail('花材不存在');
  306. }
  307. //供货商预订单最低公斤数要求和每公斤服务费
  308. $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
  309. $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
  310. //旭海银柳的临时解决办法
  311. if (getenv('YII_ENV') == 'production') {
  312. if($ghsShopInfo->mainId == 41121){
  313. $mer = [1903693,1903712,1903789,1903796,1903816,1903841,1903855,1903859,1903860,1903864];
  314. $getIds = array_column($productList, 'productId');
  315. $hasYl = false;
  316. foreach($productList as $ylVal){
  317. $ylProductId = $ylVal['productId'];
  318. if(in_array($ylProductId, $mer)){
  319. $hasYl = true;
  320. }
  321. }
  322. if($hasYl){
  323. $myInfoList = ProductClass::getAllByCondition(['id'=>['in',$getIds]],null,'*','id');
  324. $currentMap = [1903693=>220,1903712=>150,1903789=>100,1903796=>60,1903816=>45,1903841=>80,1903855=>55,1903859=>40,1903860=>25,1903864=>20];
  325. foreach($productList as $ylKey => $ylValue){
  326. $ylProductId = $ylValue['productId'];
  327. $num = $ylValue['bigNum']??0;
  328. if(!isset($currentMap[$ylProductId])){
  329. util::fail('花材有问题');
  330. }
  331. $mustBeNum = $currentMap[$ylProductId];
  332. $myInfo = $myInfoList[$ylProductId]??[];
  333. if(empty($myInfo)){
  334. util::fail('花材有问题哦');
  335. }
  336. $myName = $myInfo['name']??'';
  337. if($num>=$mustBeNum){
  338. $curVal = $num/$mustBeNum;
  339. if($curVal != intval($curVal)){
  340. util::fail($myName.' 要'.$mustBeNum.'捆或'.$mustBeNum.'的倍数');
  341. }
  342. }else{
  343. util::fail($myName.' 数量要'.$mustBeNum.'捆');
  344. }
  345. }
  346. }
  347. }
  348. }else{
  349. if($ghsShopInfo->mainId == 644){
  350. $mer = [85378,85956,86036,86062,86039,86042,86211,86743,25846,34238];
  351. $getIds = array_column($productList, 'productId');
  352. $hasYl = false;
  353. foreach($productList as $ylVal){
  354. $ylProductId = $ylVal['productId'];
  355. if(in_array($ylProductId, $mer)){
  356. $hasYl = true;
  357. }
  358. }
  359. if($hasYl){
  360. $myInfoList = ProductClass::getAllByCondition(['id'=>['in',$getIds]],null,'*','id');
  361. $currentMap = [85378=>2,85956=>3,86036=>4,86062=>5,86039=>6,86042=>3,86211=>4,86743=>5,25846=>6,34238=>7];
  362. foreach($productList as $ylKey => $ylValue){
  363. $ylProductId = $ylValue['productId'];
  364. $num = $ylValue['bigNum']??0;
  365. if(!isset($currentMap[$ylProductId])){
  366. util::fail('花材有问题');
  367. }
  368. $mustBeNum = $currentMap[$ylProductId];
  369. $myInfo = $myInfoList[$ylProductId]??[];
  370. if(empty($myInfo)){
  371. util::fail('花材有问题哦');
  372. }
  373. $myName = $myInfo['name']??'';
  374. if($num>=$mustBeNum){
  375. $curVal = $num/$mustBeNum;
  376. if($curVal != intval($curVal)){
  377. util::fail($myName.' 要'.$mustBeNum.'捆或'.$mustBeNum.'的倍数');
  378. }
  379. }else{
  380. util::fail($myName.' 数量要'.$mustBeNum.'捆');
  381. }
  382. }
  383. }
  384. }
  385. }
  386. $post['product'] = $productList;
  387. $sendType = $post['sendType'] ?? 0;
  388. $transType = $post['transType'] ?? 0;
  389. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  390. //昆明发货包装费和运费的计算
  391. $totalWeight = 0;
  392. $totalItemNum = 0;
  393. foreach ($productList as $itemData) {
  394. $bigNum = $itemData['bigNum'] ?? 0;
  395. $thisWeight = $itemData['weight'] ?? 0;
  396. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  397. $totalWeight = bcadd($currentWeight, $totalWeight, 2);
  398. $totalItemNum = bcadd($totalItemNum, $bigNum, 2);
  399. }
  400. $totalItemNum = floatval($totalItemNum);
  401. $kmPackCost = PurchaseClass::getKmPackCost($ghsMainId);
  402. $packCost = 0;
  403. if (!empty($kmPackCost)) {
  404. foreach ($kmPackCost as $itemPack) {
  405. $kiloNum = $itemPack['num'] ?? 0;
  406. if ($kiloNum >= $totalWeight) {
  407. $packCost = $itemPack['amount'] ?? 0;
  408. break;
  409. }
  410. }
  411. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  412. noticeUtil::push("总重量" . $totalWeight, '15280215347');
  413. }
  414. }
  415. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  416. noticeUtil::push("走的pfLevel=1", '15280215347');
  417. }
  418. //同城配送和到店自取免打包费
  419. if ($transType == 4 || $transType == 5) {
  420. $packCost = 0;
  421. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  422. noticeUtil::push("没有计算打包费原因:transType = 4 =5", '15280215347');
  423. }
  424. }
  425. //如果已经算过一次打包费和运费了,则不再费了
  426. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  427. if ($needAdd == false) {
  428. $packCost = 0;
  429. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  430. noticeUtil::push("没有计算打包费原因:已经算过一次了", '15280215347');
  431. }
  432. }
  433. $post['packCost'] = $packCost;
  434. $transCost = dict::getDict('transCost', null, null, $ghsShopInfo->mainId);
  435. $sendCost = 0;
  436. if (!empty($transCost)) {
  437. foreach ($transCost as $trans) {
  438. $sign = $trans['sign'] ?? 0;
  439. if ($sign == $transType) {
  440. if ($sign == 2) {
  441. //冷链物流
  442. $llOption = $trans['option'] ?? [];
  443. if (!empty($llOption)) {
  444. $largeInfo = end($llOption);
  445. $largeNum = $largeInfo['num'] ?? 0;
  446. $largeAmount = $largeInfo['amount'] ?? 0;
  447. $bs = intval($totalItemNum / $largeNum);
  448. if ($bs > 0) {
  449. $addSendCost = bcmul($bs, $largeAmount, 2);
  450. $sendCost = bcadd($sendCost, $addSendCost, 2);
  451. $subNum = bcmul($bs, $largeNum, 2);
  452. $subNum = floatval($subNum);
  453. $totalItemNum = bcsub($totalItemNum, $subNum, 2);
  454. $totalItemNum = floatval($totalItemNum);
  455. }
  456. foreach ($llOption as $miniItem) {
  457. if ($miniItem['num'] >= $totalItemNum) {
  458. $sendCost = bcadd($sendCost, $miniItem['amount'], 2);
  459. break;
  460. }
  461. }
  462. }
  463. } else {
  464. //其它物流
  465. $perKiloCost = $trans['perKiloCost'] ?? 0;
  466. $sendCost = bcmul($perKiloCost, $totalWeight, 3);
  467. $sendCost = round($sendCost, 2);
  468. }
  469. }
  470. }
  471. }
  472. //如果已经算过一次打包费和运费了,则不再费了
  473. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  474. if ($needAdd == false) {
  475. $sendCost = 0;
  476. }
  477. $post['sendCost'] = $sendCost;
  478. //标记为昆明到地方订单
  479. $post['localOrder'] = 1;
  480. } else {
  481. //同城发货包装费和运费的计算
  482. $sendCost = 0;
  483. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  484. $weight = 0;
  485. foreach ($productList as $itemData) {
  486. $bigNum = $itemData['bigNum'] ?? 0;
  487. $thisWeight = $itemData['weight'] ?? 0;
  488. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  489. $weight = bcadd($currentWeight, $weight, 2);
  490. }
  491. $result = PurchaseClass::getDistanceFee($this->shop, $ghsShopInfo, $weight);
  492. $sendCost = $result['fee'] ?? 0;
  493. }
  494. $post['sendCost'] = $sendCost;
  495. //出车、发快递、发物流可以算包装费
  496. $packCost = 0;
  497. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  498. noticeUtil::push("走的pfLevel=0", '15280215347');
  499. }
  500. $itemTotalAmount = $post['itemTotalAmount'] ?? 0;
  501. unset($post['itemTotalAmount']);
  502. if ($sendType == dict::getDict('sendType', 'express')) {
  503. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $itemTotalAmount);
  504. if ($needAdd) {
  505. $packCost = $ghsShopInfo->packCost ?? 0;
  506. }
  507. }
  508. $post['packCost'] = $packCost;
  509. }
  510. if (isset($ghsInfo['mainId']) && $ghsInfo['mainId'] == 14499) {
  511. noticeUtil::push("包装费:" . $packCost, '15280215347');
  512. }
  513. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  514. if ($sendType == 0) {
  515. if (getenv('YII_ENV') == 'production') {
  516. //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
  517. if ($ghsShopId == 4608) {
  518. if ($respond->actPrice < 500) {
  519. $transaction->rollBack();
  520. util::fail('满500元支持免费配送');
  521. }
  522. }
  523. //叶上花,满500元市区免费配送
  524. if ($ghsShopId == 4804) {
  525. if ($respond->actPrice < 500) {
  526. $transaction->rollBack();
  527. util::fail('满500元支持免费配送');
  528. }
  529. }
  530. //花悠星 国恋 勇记 易宝暂时不支持免费配送
  531. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778 || $ghsShopId == 12003) {
  532. util::fail('暂不支持送货上门,请选其它方式');
  533. }
  534. }
  535. }
  536. if ($sendType == 2) {
  537. if (in_array($ghsShopId, [520, 1489, 13628, 3, 7855, 7687])) {
  538. util::fail('暂时不能使用跑腿');
  539. }
  540. }
  541. if (getenv('YII_ENV') == 'production') {
  542. //昆明花落谁家
  543. if ($ghsShopId == 13398) {
  544. if ($respond->bigNum < 15) {
  545. $transaction->rollBack();
  546. util::fail('不足15扎,请联系店长');
  547. }
  548. }
  549. }
  550. $transaction->commit();
  551. util::success($respond);
  552. } catch (Exception $e) {
  553. $transaction->rollBack();
  554. util::fail();
  555. }
  556. }
  557. //赊账 ssh 2021.1.24
  558. public function actionDebtPay()
  559. {
  560. $get = Yii::$app->request->get();
  561. $orderSn = $get['orderSn'] ?? 0;
  562. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  563. PurchaseService::valid($info, $this->shopId);
  564. //解决重复和并发提交
  565. $cacheKey = 'hd_debt_pay_' . $orderSn;
  566. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  567. if (!empty($has)) {
  568. util::fail('请稍等...');
  569. }
  570. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 5, 'has']);
  571. if ($info->status == 5) {
  572. util::fail('订单已取消');
  573. }
  574. if ($info->status != 1) {
  575. util::fail('订单不是待付款状态');
  576. }
  577. if ($info->book == 1) {
  578. util::fail('预售不能记欠款');
  579. }
  580. $ghsId = $info->ghsId;
  581. $ghs = GhsClass::getGhsInfo($ghsId);
  582. $customId = $ghs['customId'] ?? 0;
  583. $custom = CustomClass::getCustom($customId);
  584. if (empty($custom)) {
  585. util::fail('没有找到客户');
  586. }
  587. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  588. util::fail('请先申请赊账权限');
  589. }
  590. $connection = Yii::$app->db;
  591. $transaction = $connection->beginTransaction();
  592. try {
  593. //延期支付
  594. PurchaseService::debt($info);
  595. $transaction->commit();
  596. if (isset($info->localOrder) && $info->localOrder == 1) {
  597. if ($info->transType != 5 && $info->transType != 4) {
  598. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  599. $current = date("Y_m_d");
  600. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  601. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  602. }
  603. } else {
  604. if (isset($info->sendType) && $info->sendType == 4) {
  605. //标记当天已收一次包装费,岭南批发市场用的功能
  606. $current = date("Y_m_d");
  607. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  608. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  609. }
  610. }
  611. //不是预订单,并且采购人没有开批发店,供货商不是源花汇、小明鲜花、淄博花超、云漫梦金鹏,则订单直接完成掉。是预订单,则确认发货时直接完成掉。有多个地方要修改,请搜索关键词zjFinish
  612. $cg = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  613. if (!empty($cg)) {
  614. if (isset($cg->book) && $cg->book == 0) {
  615. $ghsShopId = $ghs['shopId'] ?? 0;
  616. if (getenv('YII_ENV') == 'production') {
  617. $specialList = [10649, 8042, 16454, 21319, 33668, 43274];
  618. } else {
  619. $specialList = [36523];
  620. }
  621. if (!in_array($ghsShopId, $specialList)) {
  622. $cgShopId = $cg->shopId ?? 0;
  623. $cgShop = ShopClass::getById($cgShopId, true);
  624. if (!empty($cgShop)) {
  625. if (isset($cgShop->pfShopId) && $cgShop->pfShopId == 0) {
  626. PurchaseClass::confirmTake($cg);
  627. }
  628. }
  629. }
  630. }
  631. }
  632. //app新订单通知
  633. $saleId = $info->saleId ?? 0;
  634. $order = OrderClass::getById($saleId, true);
  635. if (!empty($order)) {
  636. $shopId = $order->shopId ?? 0;
  637. $shop = ShopClass::getById($shopId, true);
  638. if (!empty($shop)) {
  639. //微信通知供货商
  640. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  641. $noticeText = json_encode(['orderId' => $saleId]);
  642. $noticeKey = "hdCgNoticeGhs";
  643. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  644. }
  645. //订单生成时唤起在线打印
  646. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  647. //有几个地方要同步去修改
  648. if (getenv('YII_ENV') == 'production') {
  649. //源花汇不自动打开小票
  650. $mallOrderNoPrint = [10536];
  651. } else {
  652. $mallOrderNoPrint = [];
  653. }
  654. if (in_array($order->mainId, $mallOrderNoPrint)) {
  655. } else {
  656. OrderClass::onlinePrint($order);
  657. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  658. if (isset($ext->printSn) && !empty($ext->printSn)) {
  659. $order->printNum += 1;
  660. $order->save();
  661. }
  662. //小齐鲜花打二次小票,有多处要修改搜索关键词tow_print
  663. if (in_array($order->mainId, [40624])) {
  664. OrderClass::onlinePrint($order);
  665. $ext = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  666. if (isset($ext->printSn) && !empty($ext->printSn)) {
  667. $order->printNum += 1;
  668. $order->save();
  669. }
  670. }
  671. }
  672. }
  673. //更新最后下单时间
  674. $customId = $order->customId ?? 0;
  675. $custom = CustomClass::getById($customId, true);
  676. $date = date("Y-m-d H:i:s");
  677. if (!empty($custom)) {
  678. $custom->recentExpend = $date;
  679. $custom->save();
  680. }
  681. $ghsId = $order->ghsId ?? 0;
  682. $ghs = GhsClass::getById($ghsId, true);
  683. if (!empty($ghs)) {
  684. $ghs->recentExpend = $date;
  685. $ghs->save();
  686. }
  687. }
  688. } catch (Exception $e) {
  689. $transaction->rollBack();
  690. util::fail('支付失败');
  691. }
  692. util::success($info->attributes);
  693. }
  694. //余额支付 ssh 2021.1.24
  695. public function actionBalancePay()
  696. {
  697. $shopAdmin = $this->shopAdmin;
  698. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  699. util::fail('超管才能操作');
  700. }
  701. $get = Yii::$app->request->get();
  702. $orderSn = $get['orderSn'] ?? 0;
  703. $password = $get['password'] ?? '';
  704. // 重新获取,不加密的用户数据
  705. $admin = AdminClass::getById($this->adminId, true);
  706. if (password_verify($password, $admin->payPassword) == false) {
  707. util::fail('密码错误');
  708. }
  709. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  710. if (empty($info)) {
  711. util::fail('没有找到采购单');
  712. }
  713. if ($info->status == 5) {
  714. util::fail('订单已取消');
  715. }
  716. if ($info->status != 1) {
  717. util::fail('订单不是待付款状态');
  718. }
  719. PurchaseService::valid($info, $this->shopId);
  720. $connection = Yii::$app->db;
  721. $transaction = $connection->beginTransaction();
  722. try {
  723. //余额支付
  724. purchaseService::balancePay($info);
  725. $transaction->commit();
  726. } catch (Exception $e) {
  727. $transaction->rollBack();
  728. util::fail('支付失败');
  729. }
  730. util::success($info->attributes);
  731. }
  732. //采购记录 ssh 2021.1.24
  733. public function actionList()
  734. {
  735. $get = Yii::$app->request->get();
  736. $where = ['shopId' => $this->shopId];
  737. $status = $get['status'] ?? 0;
  738. if (!empty($status)) {
  739. $where['status'] = $status;
  740. }
  741. $respond = PurchaseService::getPurchaseList($where);
  742. $respond['shop'] = $this->shop->attributes;
  743. util::success($respond);
  744. }
  745. //采购详情 ssh 2021.01.25
  746. public function actionDetail()
  747. {
  748. $get = Yii::$app->request->get();
  749. $id = $get['id'] ?? 0;
  750. $info = PurchaseService::getInfo($id, true, true);
  751. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  752. //不能注释,分享付款功能要用,客户没有登录也能付款
  753. //util::fail('无法访问');
  754. }
  755. $customId = $info['customId'] ?? 0;
  756. $custom = CustomClass::getById($customId);
  757. $customName = $custom['name'] ?? '';
  758. $customMobile = $custom['mobile'] ?? '';
  759. $info['customName'] = $customName;
  760. $info['customMobile'] = $customMobile;
  761. $hasDebtPay = $custom['debt'] ?? 0;
  762. $book = $info['book'] ?? 0;
  763. $presell = $info['presell'] ?? 0;
  764. //预售不能记欠款
  765. if ($book == 1) {
  766. $hasDebtPay = 0;
  767. }
  768. if ($presell == 1) {
  769. $hasDebtPay = 0;
  770. }
  771. $shop = $this->shop;
  772. $info['pfShopId'] = $shop->pfShopId ?? 0;
  773. $info['hasDebtPay'] = $hasDebtPay;
  774. $info['balance'] = $shop->balance ?? 0;
  775. $getPayType = dict::getDict('getPayType');
  776. $info['getPayType'] = $getPayType;
  777. //获取售后条件
  778. $ghsId = $info['ghsId'] ?? 0;
  779. $ghsInfo = GhsClass::getById($ghsId, true);
  780. if (empty($ghsInfo)) {
  781. util::fail('没有找到供货商');
  782. }
  783. $info['afterSale'] = $ghsInfo->afterSale ?? 1;
  784. //重要,客户看到的电话修改成门店的联系电话
  785. $ghsShopId = $ghsInfo->shopId ?? 0;
  786. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  787. $info['ghsMobile'] = $ghsShopInfo->telephone ?? '';
  788. $info['hasRenew'] = 1;
  789. util::success($info);
  790. }
  791. //可用的支付方式 ssh 2021.1.25
  792. public function actionPayWay()
  793. {
  794. $button = [
  795. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  796. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  797. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  798. ];
  799. util::success(['button' => $button]);
  800. }
  801. //待结款明细 ssh 2021.1.26
  802. public function actionDebtList()
  803. {
  804. $get = Yii::$app->request->get();
  805. $id = $get['id'] ?? 0;
  806. $info = GhsClass::getGhsInfo($id);
  807. GhsClass::valid($info, $this->shopId);
  808. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  809. $searchTime = $get['searchTime'] ?? '';
  810. if (!empty($searchTime)) {
  811. $startTime = $get['startTime'] ?? '';
  812. $endTime = $get['endTime'] ?? '';
  813. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  814. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  815. }
  816. $respond = PurchaseService::getDebtList($where);
  817. util::success($respond);
  818. }
  819. //支付宝支付 ssh 2021.4.11
  820. public function actionAliPay()
  821. {
  822. ini_set('date.timezone', 'Asia/Shanghai');
  823. $post = Yii::$app->request->post();
  824. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  825. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  826. if (empty($order)) {
  827. util::fail('订单号无效');
  828. }
  829. if ($order->status == 5) {
  830. util::fail('订单已取消');
  831. }
  832. if ($order->status != 1) {
  833. util::fail('订单不是待付款状态');
  834. }
  835. $deadline = $order->deadline;
  836. $current = time();
  837. if (($current + 50) > strtotime($deadline)) {
  838. util::fail('订单已失效,请重新下单');
  839. }
  840. $ghsId = $order->ghsId ?? 0;
  841. $ghs = GhsClass::getById($ghsId, true);
  842. if (empty($ghs)) {
  843. util::fail('没有供货商信息');
  844. }
  845. $ghsShopId = $ghs->shopId ?? 0;
  846. $ghsShop = ShopClass::getById($ghsShopId, true);
  847. if (empty($ghsShop)) {
  848. util::fail('没有供货商门店信息');
  849. }
  850. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  851. util::fail('商家支付功能未开通');
  852. }
  853. //已经唤起过支付了,根据拉卡拉的规则,需要生成新的订单号
  854. if ($order->changePrice == 2) {
  855. $oldOrderSn = $orderSn;
  856. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  857. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  858. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  859. $params = [
  860. 'appid' => 'OP00002119',
  861. 'serial_no' => '018b08cfddbd',
  862. 'merchant_no' => $ghsShop->lklSjNo,
  863. 'term_no' => $ghsShop->lklScanTermNo,
  864. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  865. 'lklCertificatePath' => $lklCertificatePath,
  866. ];
  867. $laResource = new Lakala($params);
  868. $closeParams = [
  869. 'orderSn' => $oldOrderSn,
  870. ];
  871. $response = $laResource->cashClose($closeParams);
  872. if (isset($response['code']) == false || $response['code'] != '000000') {
  873. $msg = $response['msg'] ?? '';
  874. $code = $response['code'] ?? 0;
  875. if (!in_array($code, ['000091'])) {
  876. noticeUtil::push('注意花店买花更换单号:' . $oldOrderSn . ',关单没有成功(收银台-支付宝),' . $msg, '15280215347');
  877. }
  878. }
  879. $newOrderSn = orderSn::getPurchaseSn();
  880. $newGhsOrderSn = orderSn::getGhsOrderSn();
  881. $orderSn = $newOrderSn;
  882. $order->orderSn = $newOrderSn;
  883. $order->save();
  884. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  885. $saleId = $order->saleId ?? 0;
  886. $sale = OrderClass::getById($saleId, true);
  887. if (!empty($sale)) {
  888. $oldSaleOrderSn = $sale->orderSn ?? '';
  889. $sale->orderSn = $newGhsOrderSn;
  890. $sale->save();
  891. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  892. }
  893. }
  894. $order->changePrice = 2;
  895. $order->save();
  896. $name = $orderSn;
  897. $totalFee = $order->realPrice;
  898. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  899. $ghsId = $order->ghsId ?? 0;
  900. $ghs = GhsClass::getById($ghsId, true);
  901. if (empty($ghs)) {
  902. util::fail('没有供货商信息');
  903. }
  904. $ghsShopId = $ghs->shopId ?? 0;
  905. $ghsShop = ShopClass::getById($ghsShopId, true);
  906. if (empty($ghsShop)) {
  907. util::fail('没有供货商门店信息');
  908. }
  909. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  910. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  911. $params = [
  912. 'appid' => 'OP00002119',
  913. 'serial_no' => '018b08cfddbd',
  914. 'merchant_no' => $ghsShop->lklSjNo,
  915. 'term_no' => $ghsShop->lklB2BTermNo,
  916. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  917. 'lklCertificatePath' => $lklCertificatePath,
  918. ];
  919. $laResource = new Lakala($params);
  920. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/cash-pay-callback";
  921. $wxParams = [
  922. 'orderSn' => $orderSn,
  923. 'amount' => $totalFee,
  924. 'capitalType' => $purchaseCapital,
  925. 'notifyUrl' => $notifyUrl,
  926. 'subject' => $name,
  927. ];
  928. $response = $laResource->cashierPay($wxParams);
  929. if (isset($response['code']) == false || $response['code'] != '000000') {
  930. $errMsg = $response['msg'] ?? '';
  931. noticeUtil::push('编号AA90:' . $errMsg, '15280215347');
  932. util::fail('发起支付失败');
  933. }
  934. $counter_url = $response['resp_data']['counter_url'] ? $response['resp_data']['counter_url'] : '';
  935. $hasRenew = $ghsShop->hasRenew ?? 0;
  936. $new = [
  937. 'payUrl' => $counter_url,
  938. 'orderSn' => $orderSn,
  939. 'hasRenew' => 1,
  940. ];
  941. util::success($new);
  942. }
  943. //微信支付 ssh 2021.4.11
  944. public function actionWxPay()
  945. {
  946. ini_set('date.timezone', 'Asia/Shanghai');
  947. $post = Yii::$app->request->post();
  948. $couponId = $post['couponId'] ?? 0;
  949. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  950. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  951. if (empty($order)) {
  952. util::fail('订单号无效');
  953. }
  954. if ($order->status == 5) {
  955. util::fail('订单已取消');
  956. }
  957. if ($order->status != 1) {
  958. util::fail('订单不是待付款状态');
  959. }
  960. $deadline = $order->deadline;
  961. $current = time();
  962. if (($current + 50) > strtotime($deadline)) {
  963. util::fail('订单已失效,请重新下单');
  964. }
  965. $ghsId = $order->ghsId ?? 0;
  966. $ghs = GhsClass::getById($ghsId, true);
  967. if (empty($ghs)) {
  968. util::fail('没有供货商信息');
  969. }
  970. $ghsShopId = $ghs->shopId ?? 0;
  971. $ghsShop = ShopClass::getById($ghsShopId, true);
  972. if (empty($ghsShop)) {
  973. util::fail('没有供货商门店信息');
  974. }
  975. if (isset($ghsShop->lklSjNo) && empty($ghsShop->lklSjNo)) {
  976. util::fail('商家支付功能未开通');
  977. }
  978. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  979. if ($order->changePrice == 2) {
  980. $oldOrderSn = $orderSn;
  981. //老单号需要关单,否则有付款成功的风险,老单号关闭成功了,才能生成新单号
  982. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  983. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  984. $params = [
  985. 'appid' => 'OP00002119',
  986. 'serial_no' => '018b08cfddbd',
  987. 'merchant_no' => $ghsShop->lklSjNo,
  988. 'term_no' => $ghsShop->lklScanTermNo,
  989. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  990. 'lklCertificatePath' => $lklCertificatePath,
  991. ];
  992. $laResource = new Lakala($params);
  993. $closeParams = [
  994. 'orderSn' => $oldOrderSn,
  995. ];
  996. $response = $laResource->close($closeParams);
  997. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  998. $msg = $response['msg'] ?? '';
  999. $code = $response['code'] ?? 0;
  1000. if (!in_array($code, ['BBS11114'])) {
  1001. noticeUtil::push('重点注意,花店买花更换单号没有成功,单号:' . $oldOrderSn . ',关单没有成功,' . $msg . $code, '15280215347');
  1002. util::fail('出错了,请重新下单');
  1003. }
  1004. }
  1005. $newOrderSn = orderSn::getPurchaseSn();
  1006. $newGhsOrderSn = orderSn::getGhsOrderSn();
  1007. $orderSn = $newOrderSn;
  1008. $order->orderSn = $newOrderSn;
  1009. $order->save();
  1010. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  1011. $saleId = $order->saleId ?? 0;
  1012. $sale = OrderClass::getById($saleId, true);
  1013. if (!empty($sale)) {
  1014. $oldSaleOrderSn = $sale->orderSn ?? '';
  1015. $sale->orderSn = $newGhsOrderSn;
  1016. $sale->save();
  1017. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  1018. }
  1019. }
  1020. $id = $order->id;
  1021. $order->changePrice = 2;
  1022. $order->save();
  1023. $name = $orderSn;
  1024. $totalFee = $order->realPrice;
  1025. $openId = '';
  1026. if (isset($post['currentMiniOpenId']) && !empty($post['currentMiniOpenId'])) {
  1027. $openId = $post['currentMiniOpenId'];
  1028. //noticeUtil::push('花店采购下单时,通过前端获取了openId:' . $openId, '15280215347');
  1029. }
  1030. if (empty($openId)) {
  1031. if (isset($this->admin) && !empty($this->admin)) {
  1032. if (isset($this->admin->miniOpenId) && !empty($this->admin->miniOpenId)) {
  1033. $openId = $this->admin->miniOpenId;
  1034. noticeUtil::push('花店采购下单时,通过数据库获取了openId:' . $openId, '15280215347');
  1035. }
  1036. }
  1037. }
  1038. if (empty($openId)) {
  1039. $currentShopId = $order->shopId ?? 0;
  1040. $currentShop = ShopClass::getById($currentShopId, true);
  1041. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  1042. $currentMobile = $currentShop->mobile ?? '';
  1043. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  1044. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  1045. }
  1046. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  1047. //花卉宝代为申请的微信支付
  1048. $merchantExtend = WxOpenClass::getWxInfo();
  1049. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  1050. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  1051. $params = [
  1052. 'appid' => 'OP00002119',
  1053. 'serial_no' => '018b08cfddbd',
  1054. 'merchant_no' => $ghsShop->lklSjNo,
  1055. 'term_no' => $ghsShop->lklScanTermNo,
  1056. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  1057. 'lklCertificatePath' => $lklCertificatePath,
  1058. ];
  1059. $laResource = new Lakala($params);
  1060. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  1061. $wxParams = [
  1062. 'orderSn' => $orderSn,
  1063. 'amount' => $totalFee,
  1064. 'capitalType' => $purchaseCapital,
  1065. 'notifyUrl' => $notifyUrl,
  1066. 'wxAppId' => $merchantExtend['miniAppId'],
  1067. 'openId' => $openId,
  1068. 'subject' => $name,
  1069. 'couponId' => $couponId,
  1070. ];
  1071. $response = $laResource->driveWxPay($wxParams);
  1072. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  1073. $errMsg = $response['msg'] ?? '';
  1074. noticeUtil::push('编号129680931:' . $errMsg, '15280215347');
  1075. util::fail('支付失败');
  1076. }
  1077. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  1078. $appId = $newParams['app_id'] ?? '';
  1079. $nonceStr = $newParams['nonce_str'] ?? '';
  1080. $paySign = $newParams['pay_sign'] ?? '';
  1081. $package = $newParams['package'] ?? '';
  1082. $signType = $newParams['sign_type'] ?? '';
  1083. $timeStamp = $newParams['time_stamp'] ?? '';
  1084. $new = [
  1085. 'id' => $id,
  1086. 'appId' => $appId,
  1087. 'nonceStr' => $nonceStr,
  1088. 'paySign' => $paySign,
  1089. 'package' => $package,
  1090. 'signType' => $signType,
  1091. 'timeStamp' => $timeStamp,
  1092. 'orderSn' => $orderSn,
  1093. ];
  1094. util::success($new);
  1095. }
  1096. //计算运费 ssh 20221003
  1097. public function actionFreight()
  1098. {
  1099. $get = Yii::$app->request->get();
  1100. $shop = $this->shop;
  1101. $shopLat = isset($shop->lat) ? $shop->lat : '';
  1102. $shopLong = isset($shop->long) ? $shop->long : '';
  1103. if (empty($shopLat) || empty($shopLong)) {
  1104. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1105. }
  1106. $ghsId = $get['ghsId'] ?? 0;
  1107. $ghs = GhsClass::getById($ghsId, true);
  1108. if (empty($ghs)) {
  1109. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1110. }
  1111. $ghsShopId = $ghs->shopId ?? 0;
  1112. $ghsShop = ShopClass::getById($ghsShopId, true);
  1113. if (empty($ghsShop)) {
  1114. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1115. }
  1116. $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
  1117. $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
  1118. if (empty($ghsShopLat) || empty($ghsShopLong)) {
  1119. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1120. }
  1121. $weight = $get['weight'] ?? 0;
  1122. if (empty($weight)) {
  1123. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  1124. }
  1125. $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight);
  1126. util::success($respond);
  1127. }
  1128. //运费计算 lqh 2021.4.12 暂时返回固定值
  1129. public function actionFreight2()
  1130. {
  1131. //lqh 2021.6.25 运费固定返回0
  1132. util::success(['sedCost' => 0]);
  1133. $post = Yii::$app->request->post();
  1134. $ghsId = $post['ghsId'] ?? 0;
  1135. //供货商
  1136. $ghsInfo = GhsClass::getById($ghsId);
  1137. if (empty($ghsInfo)) {
  1138. util::fail('没有找到供货商');
  1139. }
  1140. //花材信息
  1141. $productJson = $post['product'] ?? '';
  1142. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  1143. if (empty($productJson)) {
  1144. util::fail('请选择花材');
  1145. }
  1146. $productList = json_decode($productJson, true);
  1147. if (empty($productList)) {
  1148. util::fail('请选择花材25');
  1149. }
  1150. $productList = PurchaseClass::mergeItemInfo($productList);
  1151. $weight = 0;
  1152. $price = 0;
  1153. $productData = ProductClass::getProductMapData($productList);
  1154. $bigNum = 0;
  1155. foreach ($productList as $key => $val) {
  1156. $productId = $val['productId'];
  1157. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  1158. $bigNum += $val['bigNum'];
  1159. $weight = bcadd($w, $weight, 2);
  1160. $ratio = $productData[$productId]['ratio'] ?? 0;
  1161. //大小数量合并多少扎
  1162. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  1163. //售卖价格
  1164. $itemPrice = $productData[$productId]['price'] ?? 0;
  1165. //合计价格
  1166. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  1167. }
  1168. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  1169. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  1170. $sendTime = $post['sendTime'] ?? '';
  1171. if (!empty($sendTime)) {
  1172. //配送时间不为空
  1173. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  1174. $sendTimeInt = strtotime($sendTime);
  1175. $now = time();
  1176. $diff = $sendTimeInt - $now;
  1177. //因服务器写入时间,改为至少提前6分钟
  1178. if ($diff <= 360) {
  1179. util::fail('配送时间至少提前6分钟');
  1180. }
  1181. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  1182. } else {
  1183. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  1184. }
  1185. //找供货商的客户信息,用里面的配送相关地址信息
  1186. $customId = $ghsInfo['customId'] ?? 0;
  1187. $customInfo = CustomClass::getById($customId);
  1188. if (empty($customInfo)) {
  1189. util::fail('没有找到客户');
  1190. }
  1191. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  1192. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  1193. if (!$shopNo) {
  1194. //使用自定义运费
  1195. //util::fail('供货商暂未开通配送');
  1196. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  1197. util::success(['sedCost' => $cost]);
  1198. }
  1199. //组装订单信息
  1200. $info = [];
  1201. //发送放的商家信息,即供货商的相关信息
  1202. $info['sjId'] = $ghsSjId;
  1203. $info['shopId'] = $ghsShopId;
  1204. $info['actPrice'] = $price;//价格
  1205. $info['weight'] = $weight;
  1206. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  1207. $info['bigNum'] = $bigNum;
  1208. //配送地址,当前零售端的门店地址
  1209. $info['customName'] = $customInfo['name'] ?? '';
  1210. $info['customMobile'] = $customInfo['mobile'] ?? '';
  1211. $info['province'] = $customInfo['province'] ?? '';
  1212. $info['city'] = $customInfo['city'] ?? '';;
  1213. $info['address'] = $customInfo['address'] ?? '';;
  1214. $info['floor'] = $customInfo['floor'] ?? '';;
  1215. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  1216. $info['lat'] = $customInfo['lat'] ?? '';;
  1217. $info['long'] = $customInfo['long'] ?? '';;
  1218. $info['sendTime'] = $sendTime;
  1219. $res = DadaExpressServices::queryDeliverFee($info);
  1220. util::success(['sedCost' => $res['fee']]);
  1221. }
  1222. }