PurchaseController.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. <?php
  2. namespace hd\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\ghs\classes\GhsClass;
  5. use biz\notice\classes\NoticeClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExpressClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use bizGhs\express\services\DadaExpressServices;
  10. use bizGhs\order\classes\OrderClass;
  11. use bizGhs\order\classes\OrderItemClass;
  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\dict;
  19. use common\components\freight;
  20. use common\components\httpUtil;
  21. use common\components\imgUtil;
  22. use common\components\noticeUtil;
  23. use common\components\orderSn;
  24. use Yii;
  25. use common\components\util;
  26. use biz\wx\classes\WxMessageClass;
  27. use bizGhs\order\services\OrderService;
  28. use common\components\lakala\Lakala;
  29. class PurchaseController extends BaseController
  30. {
  31. public $guestAccess = ['detail'];
  32. public function actionGetFullInfo()
  33. {
  34. $get = Yii::$app->request->get();
  35. $id = $get['id'] ?? 0;
  36. $info = PurchaseClass::getById($id);
  37. if (empty($info)) {
  38. util::fail('没有找到采购单');
  39. }
  40. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  41. util::fail('没有权限访问');
  42. }
  43. $orderSn = $info['orderSn'] ?? '';
  44. $list = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  45. if (!empty($list)) {
  46. foreach ($list as $key => $item) {
  47. $shortCover = $item['cover'] ?? '';
  48. $cover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  49. $bigCover = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  50. $list[$key]['cover'] = $cover;
  51. $list[$key]['bigCover'] = $bigCover;
  52. $list[$key]['shortCover'] = $shortCover;
  53. }
  54. }
  55. $info['product'] = $list;
  56. util::success($info);
  57. }
  58. //虚拟供货商的采购 ssh 20230308
  59. public function actionAddVirtualCg()
  60. {
  61. $post = Yii::$app->request->post();
  62. $ghsId = $post['ghsId'] ?? 0;
  63. $payMode = $post['payMode'] ?? 0;
  64. $payWay = $post['payWay'] ?? 0;
  65. $ghsInfo = GhsClass::getById($ghsId);
  66. if (empty($ghsInfo)) {
  67. util::fail('没有找到供货商');
  68. }
  69. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  70. util::fail('这个是你自己的批发店');
  71. }
  72. $connection = Yii::$app->db;
  73. $transaction = $connection->beginTransaction();
  74. try {
  75. $post['book'] = $post['book'] ?? 0;
  76. $post['sjId'] = $this->sjId;
  77. $post['shopId'] = $this->shopId;
  78. $post['shopAdminId'] = $this->shopAdminId;
  79. $post['ghsId'] = $ghsId;
  80. $post['ghsName'] = $ghsInfo['name'] ?? '';
  81. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  82. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  83. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  84. $customId = $ghsInfo['customId'] ?? 0;
  85. $post['customId'] = $customId;
  86. $shopAdmin = $this->shopAdmin ?? null;
  87. $name = $shopAdmin->name ?? '';
  88. $post['shopAdminName'] = $name;
  89. $post['mainId'] = $this->mainId;
  90. $product = $post['product'] ?? '';
  91. $productList = json_decode($product, true);
  92. //判断product数据是不是同个供货商的
  93. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  94. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  95. if (!empty($productInfoList)) {
  96. foreach ($productInfoList as $currentInfo) {
  97. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $this->mainId) {
  98. util::fail('只能选择同一家的商品哦');
  99. }
  100. }
  101. if (count($productInfoList) != count($ids)) {
  102. util::fail('存在无效商品');
  103. }
  104. } else {
  105. util::fail('花材不存在');
  106. }
  107. //供货商预订单最低公斤数要求和每公斤服务费
  108. $ghsInfo['kiloFee'] = 0;
  109. $ghsInfo['miniKilo'] = 0;
  110. $post['product'] = $productList;
  111. $packCost = 0;
  112. $post['packCost'] = $packCost;
  113. $post['sendCost'] = isset($post['sendCost']) && $post['sendCost'] > 0 ? $post['sendCost'] : 0;
  114. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  115. if ($payMode == 0) {
  116. //欠款
  117. PurchaseService::debt($respond);
  118. } else {
  119. //线下支付
  120. PurchaseService::payAfter($respond, $payWay);
  121. $saleId = $respond->saleId ?? 0;
  122. $order = OrderClass::getById($saleId, true);
  123. if (empty($order)) {
  124. util::fail('没有找到订单');
  125. }
  126. OrderService::payAfter($order, $payWay, true);
  127. }
  128. $transaction->commit();
  129. util::success($respond);
  130. } catch (Exception $e) {
  131. $transaction->rollBack();
  132. util::fail();
  133. }
  134. }
  135. //生成采购单 ssh 2021.1.17
  136. public function actionCreateOrder()
  137. {
  138. $post = Yii::$app->request->post();
  139. $shop = $this->shop;
  140. $pfShopId = $shop->pfShopId ?? 0;
  141. $mainId = $shop->mainId ?? 0;
  142. if ($mainId != 1553 && $mainId != 52) {
  143. //除了花样年华、惠雅鲜花、天天鲜花和美天鲜花,其他人有开批发,暂不支持直接向别人采购
  144. if (!empty($pfShopId)) {
  145. util::fail('你有开批发店,暂不支持直接向别人采购');
  146. }
  147. }
  148. //多颜色数据整理
  149. $colorItem = $post['xj'] ?? [];
  150. $newXj = [];
  151. if (!empty($colorItem)) {
  152. $colorData = json_decode($colorItem, true);
  153. if (!empty($colorData) && is_array($colorData)) {
  154. foreach ($colorData as $colorInfo) {
  155. $ptItemId = $colorInfo['ptItemId'] ?? 0;
  156. $colorList = $colorInfo['list'] ?? [];
  157. if (!empty($colorList)) {
  158. foreach ($colorList as $colorItemKey => $colorItem) {
  159. $newXj[$ptItemId][] = $colorItem;
  160. }
  161. }
  162. }
  163. }
  164. }
  165. $post['xj'] = $newXj;
  166. $ghsId = $post['ghsId'] ?? 0;
  167. //供货商
  168. $ghsInfo = GhsClass::getById($ghsId);
  169. if (empty($ghsInfo)) {
  170. util::fail('没有找到供货商');
  171. }
  172. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  173. if (isset($ghsInfo['mainId']) && !empty($ghsInfo['mainId']) && $ghsInfo['mainId'] == $ghsInfo['ownMainId']) {
  174. util::fail('这个是你自己的批发店');
  175. }
  176. $connection = Yii::$app->db;//事务处理
  177. $transaction = $connection->beginTransaction();
  178. try {
  179. $post['book'] = $post['book'] ?? 0;
  180. $post['sjId'] = $this->sjId;
  181. $post['shopId'] = $this->shopId;
  182. $post['shopAdminId'] = $this->shopAdminId;
  183. $post['ghsId'] = $ghsId;
  184. $post['ghsName'] = $ghsInfo['name'] ?? '';
  185. $post['ghsMobile'] = $ghsInfo['mobile'] ?? '';
  186. $post['ghsAvatar'] = $ghsInfo['avatar'] ?? '';
  187. $post['ghsFullAddress'] = $ghsInfo['fullAddress'] ?? '';
  188. $customId = $ghsInfo['customId'] ?? 0;
  189. $post['customId'] = $customId;
  190. $shopAdmin = $this->shopAdmin ?? null;
  191. $name = $shopAdmin->name ?? '';
  192. $post['shopAdminName'] = $name;
  193. $post['mainId'] = $this->mainId;
  194. $product = $post['product'] ?? '';
  195. $productList = json_decode($product, true);
  196. //判断product数据是不是同个供货商的
  197. $ghsShopInfo = ShopClass::getById($ghsShopId, true);
  198. $ghsMainId = $ghsShopInfo->mainId ?? 0;
  199. $isOpen = ShopClass::isOpen($ghsShopInfo);
  200. $book = $post['book'] ?? 0;
  201. if ($isOpen == 0 && $book == 0) {
  202. util::fail('本店已休息,请选择其它门店');
  203. }
  204. if (isset($post['transType']) && $post['transType'] == 4) {
  205. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  206. $notSameCity = PurchaseClass::notSameCity($this->shop, $ghsShopInfo);
  207. if ($notSameCity == false) {
  208. util::fail('距离太远,不能选择同城配送');
  209. }
  210. }
  211. }
  212. if ($book == 1) {
  213. $sendTimeWant = isset($post['sendTimeWant']) && !empty($post['sendTimeWant']) ? $post['sendTimeWant'] : date("Y-m-d");
  214. $future = date("Y-m-d", strtotime('+2 day'));
  215. if (strtotime($sendTimeWant) < strtotime($future)) {
  216. //util::fail('请选择时间');
  217. }
  218. $nowTime = strtotime(date("Y-m-d"));
  219. if (strtotime($sendTimeWant) < $nowTime) {
  220. util::fail('不能选择过去时间');
  221. }
  222. }
  223. $ids = array_unique(array_filter(array_column($productList, 'productId')));
  224. $productInfoList = ProductClass::getByIds($ids, null, 'id');
  225. if (!empty($productInfoList)) {
  226. $presellData = [];
  227. $nowTime = strtotime(date("Y-m-d"));
  228. foreach ($productInfoList as $currentInfo) {
  229. if (isset($currentInfo['mainId']) == false || $currentInfo['mainId'] != $ghsMainId) {
  230. util::fail('只能选择同一家的商品哦');
  231. }
  232. $currentName = $currentInfo['name'] ?? '';
  233. $presell = $currentInfo['presell'] ?? 0;
  234. $presellData[] = $presell;
  235. if ($presell == 1) {
  236. if ($book == 1) {
  237. util::fail('预订时不能选择预售花材');
  238. } else {
  239. if (isset($post['sendTimeWant']) == false || empty($post['sendTimeWant'])) {
  240. util::fail('请选择配送日期');
  241. }
  242. $sendTimeWant = $post['sendTimeWant'];
  243. if (strtotime($sendTimeWant) < $nowTime) {
  244. util::fail('不能选择过去时间');
  245. }
  246. $hasDateStr = $currentInfo['presellDate'] ?? [];
  247. $hasDate = explode(',', trim($hasDateStr));
  248. if (empty($hasDate)) {
  249. util::fail('预售日期有问题,请提醒门店');
  250. }
  251. if (in_array(strtotime($sendTimeWant), $hasDate) == false) {
  252. util::fail("有预售花材在{$sendTimeWant}没到货");
  253. }
  254. $post['presell'] = 1;
  255. }
  256. }
  257. }
  258. if (count($productInfoList) != count($ids)) {
  259. util::fail('存在无效商品');
  260. }
  261. $presellData = array_unique($presellData);
  262. if (count($presellData) > 1) {
  263. util::fail('预售和非预售花材不能一起下单');
  264. }
  265. } else {
  266. util::fail('花材不存在');
  267. }
  268. //供货商预订单最低公斤数要求和每公斤服务费
  269. $ghsInfo['kiloFee'] = $ghsShopInfo->kiloFee ?? 0;
  270. $ghsInfo['miniKilo'] = $ghsShopInfo->miniKilo ?? 0;
  271. $post['product'] = $productList;
  272. $sendType = $post['sendType'] ?? 0;
  273. $transType = $post['transType'] ?? 0;
  274. if (isset($ghsShopInfo->pfLevel) && $ghsShopInfo->pfLevel == 1) {
  275. //昆明发货包装费和运费的计算
  276. $totalWeight = 0;
  277. $totalItemNum = 0;
  278. foreach ($productList as $itemData) {
  279. $bigNum = $itemData['bigNum'] ?? 0;
  280. $thisWeight = $itemData['weight'] ?? 0;
  281. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  282. $totalWeight = bcadd($currentWeight, $totalWeight, 2);
  283. $totalItemNum = bcadd($totalItemNum, $bigNum, 2);
  284. }
  285. $totalItemNum = floatval($totalItemNum);
  286. $kmPackCost = dict::getDict('kmPackCost');
  287. $packCost = 0;
  288. if (!empty($kmPackCost)) {
  289. foreach ($kmPackCost as $itemPack) {
  290. $kiloNum = $itemPack['num'] ?? 0;
  291. if ($kiloNum >= $totalWeight) {
  292. $packCost = $itemPack['amount'] ?? 0;
  293. break;
  294. }
  295. }
  296. }
  297. //同城配送和到店自取免打包费
  298. if ($transType == 4 || $transType == 5) {
  299. $packCost = 0;
  300. }
  301. //如果已经算过一次打包费和运费了,则不再费了
  302. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  303. if ($needAdd == false) {
  304. $packCost = 0;
  305. }
  306. $post['packCost'] = $packCost;
  307. $transCost = dict::getDict('transCost');
  308. $sendCost = 0;
  309. if (!empty($transCost)) {
  310. foreach ($transCost as $trans) {
  311. $sign = $trans['sign'] ?? 0;
  312. if ($sign == $transType) {
  313. if ($sign == 2) {
  314. //冷链物流
  315. $llOption = $trans['option'] ?? [];
  316. if (!empty($llOption)) {
  317. $largeInfo = end($llOption);
  318. $largeNum = $largeInfo['num'] ?? 0;
  319. $largeAmount = $largeInfo['amount'] ?? 0;
  320. $bs = intval($totalItemNum / $largeNum);
  321. if ($bs > 0) {
  322. $addSendCost = bcmul($bs, $largeAmount, 2);
  323. $sendCost = bcadd($sendCost, $addSendCost, 2);
  324. $subNum = bcmul($bs, $largeNum, 2);
  325. $subNum = floatval($subNum);
  326. $totalItemNum = bcsub($totalItemNum, $subNum, 2);
  327. $totalItemNum = floatval($totalItemNum);
  328. }
  329. foreach ($llOption as $miniItem) {
  330. if ($miniItem['num'] >= $totalItemNum) {
  331. $sendCost = bcadd($sendCost, $miniItem['amount'], 2);
  332. break;
  333. }
  334. }
  335. }
  336. } else {
  337. //其它物流
  338. $perKiloCost = $trans['perKiloCost'] ?? 0;
  339. $sendCost = bcmul($perKiloCost, $totalWeight, 3);
  340. $sendCost = round($sendCost, 2);
  341. }
  342. }
  343. }
  344. }
  345. //如果已经算过一次打包费和运费了,则不再费了
  346. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId);
  347. if ($needAdd == false) {
  348. $sendCost = 0;
  349. }
  350. $post['sendCost'] = $sendCost;
  351. //标记为昆明到地方订单
  352. $post['localOrder'] = 1;
  353. } else {
  354. //同城发货包装费和运费的计算
  355. $sendCost = 0;
  356. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  357. $weight = 0;
  358. foreach ($productList as $itemData) {
  359. $bigNum = $itemData['bigNum'] ?? 0;
  360. $thisWeight = $itemData['weight'] ?? 0;
  361. $currentWeight = bcmul($thisWeight, $bigNum, 2);
  362. $weight = bcadd($currentWeight, $weight, 2);
  363. }
  364. $result = PurchaseClass::getDistanceFee($this->shop, $ghsShopInfo, $weight);
  365. $sendCost = $result['fee'] ?? 0;
  366. }
  367. $post['sendCost'] = $sendCost;
  368. //出车、发快递、发物流可以算包装费
  369. $packCost = 0;
  370. $itemTotalAmount = $post['itemTotalAmount'] ?? 0;
  371. unset($post['itemTotalAmount']);
  372. if ($sendType == dict::getDict('sendType', 'express')) {
  373. $needAdd = \bizHd\shop\classes\ShopClass::needAddPackCost($ghsShopInfo, $customId, $itemTotalAmount);
  374. if ($needAdd) {
  375. $packCost = $ghsShopInfo->packCost ?? 0;
  376. }
  377. }
  378. $post['packCost'] = $packCost;
  379. }
  380. $respond = PurchaseService::createPurchase($post, $ghsInfo);
  381. if ($sendType == 0) {
  382. if (getenv('YII_ENV') == 'production') {
  383. //杭州斗南鲜花批发部,满500,10公里内免费配送,满1000,20公里免费配送
  384. if ($ghsShopId == 4608) {
  385. if ($respond->actPrice < 500) {
  386. $transaction->rollBack();
  387. util::fail('满500元支持免费配送');
  388. }
  389. }
  390. //叶上花,满500元市区免费配送
  391. if ($ghsShopId == 4804) {
  392. if ($respond->actPrice < 500) {
  393. $transaction->rollBack();
  394. util::fail('满500元支持免费配送');
  395. }
  396. }
  397. //花悠星 国恋 勇记暂时不支持免费配送
  398. if ($ghsShopId == 7687 || $ghsShopId == 7831 || $ghsShopId == 7778) {
  399. util::fail('暂不支持送货上门,请选其它方式');
  400. }
  401. }
  402. }
  403. $transaction->commit();
  404. util::success($respond);
  405. } catch (Exception $e) {
  406. $transaction->rollBack();
  407. util::fail();
  408. }
  409. }
  410. //赊账 ssh 2021.1.24
  411. public function actionDebtPay()
  412. {
  413. $get = Yii::$app->request->get();
  414. $orderSn = $get['orderSn'] ?? 0;
  415. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  416. PurchaseService::valid($info, $this->shopId);
  417. if ($info->status == 5) {
  418. util::fail('订单已取消');
  419. }
  420. if ($info->status != 1) {
  421. util::fail('订单不是待付款状态');
  422. }
  423. if ($info->book == 1) {
  424. util::fail('预售不能记欠款');
  425. }
  426. $ghsId = $info->ghsId;
  427. $ghs = GhsClass::getGhsInfo($ghsId);
  428. $customId = $ghs['customId'] ?? 0;
  429. $custom = CustomClass::getCustom($customId);
  430. if (empty($custom)) {
  431. util::fail('没有找到客户');
  432. }
  433. if (isset($custom['debt']) == false || $custom['debt'] == CustomClass::IS_DEBT_NO) {
  434. util::fail('请先申请赊账权限');
  435. }
  436. $connection = Yii::$app->db;//事务处理
  437. $transaction = $connection->beginTransaction();
  438. try {
  439. //延期支付
  440. PurchaseService::debt($info);
  441. $transaction->commit();
  442. if (isset($info->localOrder) && $info->localOrder == 1) {
  443. if ($info->transType != 5 && $info->transType != 4) {
  444. //昆明发货,客户算了一次打包费和运费,第二次就不再算了
  445. $current = date("Y_m_d");
  446. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  447. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  448. }
  449. } else {
  450. if (isset($info->sendType) && $info->sendType == 4) {
  451. //标记当天已收一次包装费,岭南批发市场用的功能
  452. $current = date("Y_m_d");
  453. $key = 'ghs_custom_today_has_get_pack_cost_' . $current . '_' . $customId;
  454. Yii::$app->redis->executeCommand('SETEX', [$key, 86400, '1']);
  455. }
  456. }
  457. //app新订单通知
  458. $saleId = $info->saleId ?? 0;
  459. $order = OrderClass::getById($saleId, true);
  460. if (!empty($order)) {
  461. $cgShopId = $info->shopId ?? 0;
  462. $cgShop = ShopClass::getById($cgShopId, true);
  463. if (!empty($cgShop)) {
  464. //采购成功小程序通知
  465. \bizHd\notice\classes\NoticeClass::cgSuccessMiniNotice($cgShop, $info);
  466. }
  467. $shopId = $order->shopId ?? 0;
  468. $shop = ShopClass::getById($shopId, true);
  469. if (!empty($shop)) {
  470. //微信通知供货商
  471. //WxMessageClass::ghsHasNewOrderInform($shop, $order);
  472. $noticeText = json_encode(['orderId' => $saleId]);
  473. $noticeKey = "hdCgNoticeGhs";
  474. Yii::$app->redis->executeCommand('LPUSH', [$noticeKey, $noticeText]);
  475. }
  476. //订单生成时唤起在线打印
  477. if (isset($order->needPrint) && $order->needPrint == dict::getDict('needPrint', 'need')) {
  478. //有几个地方要同步去修改
  479. if (getenv('YII_ENV') == 'production') {
  480. //源花汇不自动打开小票
  481. $mallOrderNoPrint = [10536];
  482. } else {
  483. $mallOrderNoPrint = [644];
  484. }
  485. if (in_array($order->mainId, $mallOrderNoPrint)) {
  486. } else {
  487. OrderClass::onlinePrint($order);
  488. $order->printNum += 1;
  489. $order->save();
  490. }
  491. }
  492. //花店采购供货商端收到APP通知
  493. //NoticeClass::ghsNewOrderNotice($order);
  494. }
  495. } catch (Exception $e) {
  496. $transaction->rollBack();
  497. util::fail('支付失败');
  498. }
  499. util::success($info->attributes);
  500. }
  501. //余额支付 ssh 2021.1.24
  502. public function actionBalancePay()
  503. {
  504. $shopAdmin = $this->shopAdmin;
  505. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  506. util::fail('超管才能操作');
  507. }
  508. $get = Yii::$app->request->get();
  509. $orderSn = $get['orderSn'] ?? 0;
  510. $password = $get['password'] ?? '';
  511. // 重新获取,不加密的用户数据
  512. $admin = AdminClass::getById($this->adminId, true);
  513. if (password_verify($password, $admin->payPassword) == false) {
  514. util::fail('密码错误');
  515. }
  516. $info = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  517. if (empty($info)) {
  518. util::fail('没有找到采购单');
  519. }
  520. if ($info->status == 5) {
  521. util::fail('订单已取消');
  522. }
  523. if ($info->status != 1) {
  524. util::fail('订单不是待付款状态');
  525. }
  526. PurchaseService::valid($info, $this->shopId);
  527. $connection = Yii::$app->db;//事务处理
  528. $transaction = $connection->beginTransaction();
  529. try {
  530. //余额支付
  531. purchaseService::balancePay($info);
  532. $transaction->commit();
  533. } catch (Exception $e) {
  534. $transaction->rollBack();
  535. util::fail('支付失败');
  536. }
  537. util::success($info->attributes);
  538. }
  539. //采购记录 ssh 2021.1.24
  540. public function actionList()
  541. {
  542. $get = Yii::$app->request->get();
  543. $where = ['shopId' => $this->shopId];
  544. $status = $get['status'] ?? 0;
  545. if (!empty($status)) {
  546. $where['status'] = $status;
  547. }
  548. $respond = PurchaseService::getPurchaseList($where);
  549. $respond['shop'] = $this->shop->attributes;
  550. util::success($respond);
  551. }
  552. //采购详情 ssh 2021.01.25
  553. public function actionDetail()
  554. {
  555. $get = Yii::$app->request->get();
  556. $id = $get['id'] ?? 0;
  557. $info = PurchaseService::getInfo($id, true, true);
  558. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  559. //不能注释,好像分享付款功能要用,客户没有登录也能付款
  560. //util::fail('没有权限访问');
  561. }
  562. $customId = $info['customId'] ?? 0;
  563. $custom = CustomClass::getById($customId);
  564. $customName = $custom['name'] ?? '';
  565. $customMobile = $custom['mobile'] ?? '';
  566. $info['customName'] = $customName;
  567. $info['customMobile'] = $customMobile;
  568. $hasDebtPay = $custom['debt'] ?? 0;
  569. $book = $info['book'] ?? 0;
  570. $presell = $info['presell'] ?? 0;
  571. //预售不能记欠款
  572. if ($book == 1) {
  573. $hasDebtPay = 0;
  574. }
  575. if ($presell == 1) {
  576. $hasDebtPay = 0;
  577. }
  578. $info['hasDebtPay'] = $hasDebtPay;
  579. $info['balance'] = $this->shop->balance ?? 0;
  580. util::success($info);
  581. }
  582. //可用的支付方式 ssh 2021.1.25
  583. public function actionPayWay()
  584. {
  585. $button = [
  586. ['type' => 'wxPay', 'show' => 1, 'disable' => 1],
  587. ['type' => 'debtPay', 'show' => 1, 'disable' => 0],
  588. ['type' => 'balancePay', 'show' => 1, 'disable' => 0],
  589. ];
  590. util::success(['button' => $button]);
  591. }
  592. //待结款明细 ssh 2021.1.26
  593. public function actionDebtList()
  594. {
  595. $get = Yii::$app->request->get();
  596. $id = $get['id'] ?? 0;
  597. $info = GhsClass::getGhsInfo($id);
  598. GhsClass::valid($info, $this->shopId);
  599. $where = ['ghsId' => $id, 'debt' => PurchaseClass::DEBT_YES];
  600. $searchTime = $get['searchTime'] ?? '';
  601. if (!empty($searchTime)) {
  602. $startTime = $get['startTime'] ?? '';
  603. $endTime = $get['endTime'] ?? '';
  604. $period = dateUtil::formatTime($searchTime, $startTime, $endTime);
  605. $where['addTime'] = ['between', [$period['startTime'], $period['endTime']]];
  606. }
  607. $respond = PurchaseService::getDebtList($where);
  608. util::success($respond);
  609. }
  610. //微信支付 ssh 2021.4.11
  611. public function actionWxPay()
  612. {
  613. ini_set('date.timezone', 'Asia/Shanghai');
  614. $post = Yii::$app->request->post();
  615. $couponId = $post['couponId'] ?? 0;
  616. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  617. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  618. if (empty($order)) {
  619. util::fail('订单号无效');
  620. }
  621. if ($order->status == 5) {
  622. util::fail('订单已取消');
  623. }
  624. if ($order->status != 1) {
  625. util::fail('订单不是待付款状态');
  626. }
  627. $deadline = $order->deadline;
  628. $current = time();
  629. if (($current + 50) > strtotime($deadline)) {
  630. util::fail('订单已失效,请重新下单');
  631. }
  632. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  633. if ($order->changePrice == 2) {
  634. $oldOrderSn = $orderSn;
  635. $newOrderSn = orderSn::getPurchaseSn();
  636. $newGhsOrderSn = orderSn::getGhsOrderSn();
  637. $orderSn = $newOrderSn;
  638. $order->orderSn = $newOrderSn;
  639. $order->save();
  640. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  641. $saleId = $order->saleId ?? 0;
  642. $sale = OrderClass::getById($saleId, true);
  643. if (!empty($sale)) {
  644. $oldSaleOrderSn = $sale->orderSn ?? '';
  645. $sale->orderSn = $newGhsOrderSn;
  646. $sale->save();
  647. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  648. }
  649. }
  650. $id = $order->id;
  651. $order->changePrice = 2;
  652. $order->save();
  653. $name = $orderSn;
  654. $totalFee = $order->realPrice;
  655. //强制使用小程序的miniOpenId
  656. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  657. if (empty($openId)) {
  658. $currentShopId = $order->shopId ?? 0;
  659. $currentShop = ShopClass::getById($currentShopId, true);
  660. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  661. $currentMobile = $currentShop->mobile ?? '';
  662. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  663. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  664. }
  665. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  666. //花卉宝代为申请的微信支付
  667. $merchantExtend = WxOpenClass::getWxInfo();
  668. $ghsId = $order->ghsId ?? 0;
  669. $ghs = GhsClass::getById($ghsId, true);
  670. if (empty($ghs)) {
  671. util::fail('没有供货商信息');
  672. }
  673. $ghsShopId = $ghs->shopId ?? 0;
  674. $ghsShop = ShopClass::getById($ghsShopId, true);
  675. if (empty($ghsShop)) {
  676. noticeUtil::push('没有供货商门店信息' . $ghsShopId, '15280215347');
  677. util::fail('没有供货商门店信息');
  678. }
  679. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  680. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  681. $params = [
  682. 'appid' => 'OP00002119',
  683. 'serial_no' => '018b08cfddbd',
  684. 'merchant_no' => $ghsShop->lklSjNo,
  685. 'term_no' => $ghsShop->lklScanTermNo,
  686. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  687. 'lklCertificatePath' => $lklCertificatePath,
  688. ];
  689. $laResource = new Lakala($params);
  690. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  691. $wxParams = [
  692. 'orderSn' => $orderSn,
  693. 'amount' => $totalFee,
  694. 'capitalType' => $purchaseCapital,
  695. 'notifyUrl' => $notifyUrl,
  696. 'wxAppId' => $merchantExtend['miniAppId'],
  697. 'openId' => $openId,
  698. 'subject' => $name,
  699. 'couponId' => $couponId,
  700. ];
  701. $response = $laResource->cashierPay($wxParams);
  702. print_r($response);
  703. die;
  704. if (isset($response['code']) == false || $response['code'] != '000000') {
  705. $errMsg = $response['msg'] ?? '';
  706. noticeUtil::push($errMsg, '15280215347');
  707. util::fail('支付失败');
  708. }
  709. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  710. $appId = $newParams['app_id'] ?? '';
  711. $nonceStr = $newParams['nonce_str'] ?? '';
  712. $paySign = $newParams['pay_sign'] ?? '';
  713. $package = $newParams['package'] ?? '';
  714. $signType = $newParams['sign_type'] ?? '';
  715. $timeStamp = $newParams['time_stamp'] ?? '';
  716. $new = [
  717. 'id' => $id,
  718. 'appId' => $appId,
  719. 'nonceStr' => $nonceStr,
  720. 'paySign' => $paySign,
  721. 'package' => $package,
  722. 'signType' => $signType,
  723. 'timeStamp' => $timeStamp,
  724. 'orderSn' => $orderSn,
  725. ];
  726. util::success($new);
  727. }
  728. //微信支付 ssh 2021.4.11
  729. public function actionWxPay2()
  730. {
  731. ini_set('date.timezone', 'Asia/Shanghai');
  732. $post = Yii::$app->request->post();
  733. $couponId = $post['couponId'] ?? 0;
  734. $orderSn = isset($post['orderSn']) ? $post['orderSn'] : 0;
  735. $order = PurchaseClass::getByCondition(['orderSn' => $orderSn], true);
  736. if (empty($order)) {
  737. util::fail('订单号无效');
  738. }
  739. if ($order->status == 5) {
  740. util::fail('订单已取消');
  741. }
  742. if ($order->status != 1) {
  743. util::fail('订单不是待付款状态');
  744. }
  745. $deadline = $order->deadline;
  746. $current = time();
  747. if (($current + 50) > strtotime($deadline)) {
  748. util::fail('订单已失效,请重新下单');
  749. }
  750. //已经唤起过微信支付了,根据拉卡拉的规则,需要生成新的订单号
  751. if ($order->changePrice == 2) {
  752. $oldOrderSn = $orderSn;
  753. $newOrderSn = orderSn::getPurchaseSn();
  754. $newGhsOrderSn = orderSn::getGhsOrderSn();
  755. $orderSn = $newOrderSn;
  756. $order->orderSn = $newOrderSn;
  757. $order->save();
  758. PurchaseItemClass::updateByCondition(['orderSn' => $oldOrderSn], ['orderSn' => $newOrderSn]);
  759. $saleId = $order->saleId ?? 0;
  760. $sale = OrderClass::getById($saleId, true);
  761. if (!empty($sale)) {
  762. $oldSaleOrderSn = $sale->orderSn ?? '';
  763. $sale->orderSn = $newGhsOrderSn;
  764. $sale->save();
  765. OrderItemClass::updateByCondition(['orderSn' => $oldSaleOrderSn], ['orderSn' => $newGhsOrderSn]);
  766. }
  767. }
  768. $id = $order->id;
  769. $order->changePrice = 2;
  770. $order->save();
  771. $name = $orderSn;
  772. $totalFee = $order->realPrice;
  773. //强制使用小程序的miniOpenId
  774. $openId = isset($this->admin) && !empty($this->admin) ? $this->admin->miniOpenId : '';
  775. if (empty($openId)) {
  776. $currentShopId = $order->shopId ?? 0;
  777. $currentShop = ShopClass::getById($currentShopId, true);
  778. $currentShopName = $currentShop->merchantName . ' ' . $currentShop->shopName;
  779. $currentMobile = $currentShop->mobile ?? '';
  780. noticeUtil::push("花店采购时,发现没有openId,请跟进是否采购成功。{$currentShopName} {$currentMobile}", '15280215347');
  781. util::success(['hasError' => 1, 'hasNoMiniOpenId' => 1]);
  782. }
  783. $purchaseCapital = dict::getDict('capitalType', 'xhPurchase', 'id');
  784. //花卉宝代为申请的微信支付
  785. $merchantExtend = WxOpenClass::getWxInfo();
  786. $ghsId = $order->ghsId ?? 0;
  787. $ghs = GhsClass::getById($ghsId, true);
  788. if (empty($ghs)) {
  789. util::fail('没有供货商信息');
  790. }
  791. $ghsShopId = $ghs->shopId ?? 0;
  792. $ghsShop = ShopClass::getById($ghsShopId, true);
  793. if (empty($ghsShop)) {
  794. util::fail('没有供货商门店信息');
  795. }
  796. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  797. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  798. $params = [
  799. 'appid' => 'OP00002119',
  800. 'serial_no' => '018b08cfddbd',
  801. 'merchant_no' => $ghsShop->lklSjNo,
  802. 'term_no' => $ghsShop->lklScanTermNo,
  803. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  804. 'lklCertificatePath' => $lklCertificatePath,
  805. ];
  806. $laResource = new Lakala($params);
  807. $notifyUrl = Yii::$app->params['hdHost'] . "/notice/pay-callback";
  808. $wxParams = [
  809. 'orderSn' => $orderSn,
  810. 'amount' => $totalFee,
  811. 'capitalType' => $purchaseCapital,
  812. 'notifyUrl' => $notifyUrl,
  813. 'wxAppId' => $merchantExtend['miniAppId'],
  814. 'openId' => $openId,
  815. 'subject' => $name,
  816. 'couponId' => $couponId,
  817. ];
  818. $response = $laResource->driveWxPay($wxParams);
  819. if (isset($response['code']) == false || $response['code'] != 'BBS00000') {
  820. $errMsg = $response['msg'] ?? '';
  821. noticeUtil::push($errMsg, '15280215347');
  822. util::fail('支付失败');
  823. }
  824. $newParams = isset($response['resp_data']['acc_resp_fields']) ? $response['resp_data']['acc_resp_fields'] : [];
  825. $appId = $newParams['app_id'] ?? '';
  826. $nonceStr = $newParams['nonce_str'] ?? '';
  827. $paySign = $newParams['pay_sign'] ?? '';
  828. $package = $newParams['package'] ?? '';
  829. $signType = $newParams['sign_type'] ?? '';
  830. $timeStamp = $newParams['time_stamp'] ?? '';
  831. $new = [
  832. 'id' => $id,
  833. 'appId' => $appId,
  834. 'nonceStr' => $nonceStr,
  835. 'paySign' => $paySign,
  836. 'package' => $package,
  837. 'signType' => $signType,
  838. 'timeStamp' => $timeStamp,
  839. 'orderSn' => $orderSn,
  840. ];
  841. util::success($new);
  842. }
  843. //计算运费 ssh 20221003
  844. public function actionFreight()
  845. {
  846. $get = Yii::$app->request->get();
  847. $shop = $this->shop;
  848. $shopLat = isset($shop->lat) ? $shop->lat : '';
  849. $shopLong = isset($shop->long) ? $shop->long : '';
  850. if (empty($shopLat) || empty($shopLong)) {
  851. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  852. }
  853. $ghsId = $get['ghsId'] ?? 0;
  854. $ghs = GhsClass::getById($ghsId, true);
  855. if (empty($ghs)) {
  856. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  857. }
  858. $ghsShopId = $ghs->shopId ?? 0;
  859. $ghsShop = ShopClass::getById($ghsShopId, true);
  860. if (empty($ghsShop)) {
  861. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  862. }
  863. $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
  864. $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
  865. if (empty($ghsShopLat) || empty($ghsShopLong)) {
  866. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  867. }
  868. $weight = $get['weight'] ?? 0;
  869. if (empty($weight)) {
  870. util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
  871. }
  872. $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight);
  873. util::success($respond);
  874. }
  875. //运费计算 lqh 2021.4.12 暂时返回固定值
  876. public function actionFreight2()
  877. {
  878. //lqh 2021.6.25 运费固定返回0
  879. util::success(['sedCost' => 0]);
  880. $post = Yii::$app->request->post();
  881. $ghsId = $post['ghsId'] ?? 0;
  882. //供货商
  883. $ghsInfo = GhsClass::getById($ghsId);
  884. if (empty($ghsInfo)) {
  885. util::fail('没有找到供货商');
  886. }
  887. //花材信息
  888. $productJson = $post['product'] ?? '';
  889. // $productJson = '[{"productId":31993,"bigNum":1,"smallNum":0}]';
  890. if (empty($productJson)) {
  891. util::fail('请选择花材');
  892. }
  893. $productList = json_decode($productJson, true);
  894. if (empty($productList)) {
  895. util::fail('请选择花材');
  896. }
  897. $productList = PurchaseClass::mergeItemInfo($productList);
  898. $weight = 0;
  899. $price = 0;
  900. $productData = ProductClass::getProductMapData($productList);
  901. $bigNum = 0;
  902. foreach ($productList as $key => $val) {
  903. $productId = $val['productId'];
  904. $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
  905. $bigNum += $val['bigNum'];
  906. $weight = bcadd($w, $weight, 2);
  907. $ratio = $productData[$productId]['ratio'] ?? 0;
  908. //大小数量合并多少扎
  909. $itemNum = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
  910. //售卖价格
  911. $itemPrice = $productData[$productId]['price'] ?? 0;
  912. //合计价格
  913. $price = bcadd($price, bcmul($itemNum, $itemPrice, 2), 2);
  914. }
  915. $ghsShopId = $ghsInfo['shopId'] ?? 0;
  916. $ghsSjId = $ghsInfo['sjId'] ?? 0;
  917. $sendTime = $post['sendTime'] ?? '';
  918. if (!empty($sendTime)) {
  919. //配送时间不为空
  920. // 预约发单时间(预约时间unix时间戳(10位),精确到分;整分钟为间隔,并且需要至少提前5分钟预约
  921. $sendTimeInt = strtotime($sendTime);
  922. $now = time();
  923. $diff = $sendTimeInt - $now;
  924. //因服务器写入时间,改为至少提前6分钟
  925. if ($diff <= 360) {
  926. util::fail('配送时间至少提前6分钟');
  927. }
  928. $sendTime = date('Y-m-d H:i', strtotime($sendTime));
  929. } else {
  930. $sendTime = date('Y-m-d H:i', strtotime("+30 minute"));
  931. }
  932. //找供货商的客户信息,用里面的配送相关地址信息
  933. $customId = $ghsInfo['customId'] ?? 0;
  934. $customInfo = CustomClass::getById($customId);
  935. if (empty($customInfo)) {
  936. util::fail('没有找到客户');
  937. }
  938. //判断商家是否开启达达,若未开启,则使用自定义运费计算
  939. $shopNo = ShopExpressClass::getShopNo($ghsSjId, $ghsShopId);
  940. if (!$shopNo) {
  941. //使用自定义运费
  942. //util::fail('供货商暂未开通配送');
  943. $cost = freight::getCost($ghsInfo['lat'], $ghsInfo['long'], $customInfo['lat'], $customInfo['long'], $weight);
  944. util::success(['sedCost' => $cost]);
  945. }
  946. //组装订单信息
  947. $info = [];
  948. //发送放的商家信息,即供货商的相关信息
  949. $info['sjId'] = $ghsSjId;
  950. $info['shopId'] = $ghsShopId;
  951. $info['actPrice'] = $price;//价格
  952. $info['weight'] = $weight;
  953. $info['orderSn'] = \common\components\orderSn::getGhsOrderSn();
  954. $info['bigNum'] = $bigNum;
  955. //配送地址,当前零售端的门店地址
  956. $info['customName'] = $customInfo['name'] ?? '';
  957. $info['customMobile'] = $customInfo['mobile'] ?? '';
  958. $info['province'] = $customInfo['province'] ?? '';
  959. $info['city'] = $customInfo['city'] ?? '';;
  960. $info['address'] = $customInfo['address'] ?? '';;
  961. $info['floor'] = $customInfo['floor'] ?? '';;
  962. $info['fullAddress'] = $customInfo['fullAddress'] ?? '';;
  963. $info['lat'] = $customInfo['lat'] ?? '';;
  964. $info['long'] = $customInfo['long'] ?? '';;
  965. $info['sendTime'] = $sendTime;
  966. $res = DadaExpressServices::queryDeliverFee($info);
  967. util::success(['sedCost' => $res['fee']]);
  968. }
  969. }