PurchaseController.php 59 KB

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