PurchaseController.php 64 KB

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