PurchaseService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. namespace bizHd\purchase\services;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\ShopCapitalClass;
  5. use biz\shop\classes\ShopClass;
  6. use biz\shop\classes\ShopCouponClass;
  7. use biz\stat\classes\StatCgClass;
  8. use biz\stat\classes\StatCgGhsClass;
  9. use biz\stat\classes\StatOutClass;
  10. use bizGhs\custom\classes\CustomClass;
  11. use bizGhs\order\classes\OrderClass;
  12. use bizGhs\order\services\OrderService;
  13. use bizHd\base\services\BaseService;
  14. use bizHd\cg\classes\CgRefundClass;
  15. use bizHd\purchase\classes\PurchaseClass;
  16. use bizHd\purchase\classes\PurchaseItemClass;
  17. use bizHd\purchase\classes\PurchaseSnClass;
  18. use bizHd\shop\classes\MainClass;
  19. use common\components\dict;
  20. use common\components\imgUtil;
  21. use common\components\noticeUtil;
  22. use common\components\util;
  23. use Yii;
  24. class PurchaseService extends BaseService
  25. {
  26. public static $baseFile = '\bizHd\purchase\classes\PurchaseClass';
  27. public static function createPurchase($data, $ghs)
  28. {
  29. //零售采购单
  30. $respond = PurchaseClass::addPurchase($data, $ghs);
  31. //供货商销售单
  32. $ghsId = $data['ghsId'] ?? 0;
  33. $currentShopId = $ghs['shopId'] ?? 0;
  34. $currentSjId = $ghs['sjId'] ?? 0;
  35. $product = $data['product'] ?? [];
  36. $sendCost = $data['sendCost'] ?? 0;
  37. $packCost = $data['packCost'] ?? 0;
  38. $localOrder = $data['localOrder'] ?? 0;
  39. $customId = $ghs['customId'] ?? 0;
  40. $live = isset($ghs['live']) ? $ghs['live'] : 1;
  41. $custom = CustomClass::getById($customId);
  42. if (empty($custom)) {
  43. util::fail('没有找到客户');
  44. }
  45. //欠款超额,是否允许使用现金下单
  46. if ($custom['debtLimitPay'] == 1) {
  47. //欠款超额,不允许使用现金下单
  48. $debtLimit = $custom['debtLimit'] ?? 0;
  49. $debtAmount = $custom['debtAmount'] ?? 0;
  50. $actPrice = $respond->actPrice ?? 0;
  51. $finalDebtAmount = bcadd($debtAmount, $actPrice, 2);
  52. if ($finalDebtAmount > $debtLimit && $debtLimit > 0) {
  53. util::fail('请结清欠款再下单');
  54. }
  55. }
  56. $customName = $custom['name'] ?? '';
  57. $customNamePy = $custom['py'] ?? '';
  58. $customMobile = $custom['mobile'] ?? '';
  59. $purchaseId = $respond->id ?? 0;
  60. $shopAdminId = $data['shopAdminId'] ?? 0;
  61. $shopAdminName = $data['shopAdminName'] ?? '';
  62. $sendType = $data['sendType'] ?? 1;
  63. $transType = $data['transType'] ?? 0;
  64. $couponId = $data['couponId'] ?? 0;
  65. $wlName = $data['wlName'] ?? '';
  66. $discountType = $respond->discountType ?? dict::getDict('discountType', 'noDiscount');
  67. $discountAmount = $respond->discountAmount ?? 0;
  68. $discount = $respond->discount ?? 0;
  69. $remark = $data['remark'] ?? '';
  70. $xj = $data['xj'] ?? '';
  71. $sendTimeWant = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
  72. $orderType = 2;
  73. $book = $data['book'] ?? 0;
  74. $preSell = $data['presell'] ?? 0;
  75. $sendDistance = $data['sendDistance'] ?? 0;
  76. $mainId = $custom['ownMainId'] ?? 0;
  77. $fromType = dict::getDict('fromType', 'mall');
  78. $arr = [
  79. 'mainId' => $mainId,
  80. 'book' => $book,
  81. 'product' => $product,
  82. 'sjId' => $currentSjId,
  83. 'shopId' => $currentShopId,
  84. 'sendCost' => $sendCost,
  85. 'packCost' => $packCost,
  86. 'customId' => $customId,
  87. 'customName' => $customName,
  88. 'customNamePy' => $customNamePy,
  89. 'customMobile' => $customMobile,
  90. 'province' => $custom['province'] ?? '',
  91. 'city' => $custom['city'] ?? '',
  92. 'dist' => $custom['dist'] ?? '',
  93. 'address' => $custom['address'] ?? '',
  94. 'floor' => $custom['floor'] ?? '',
  95. 'fullAddress' => $custom['fullAddress'] ?? '',
  96. 'showAddress' => $custom['showAddress'] ?? '',
  97. 'long' => $custom['long'] ?? '',
  98. 'lat' => $custom['lat'] ?? '',
  99. 'purchaseId' => $purchaseId,
  100. 'sendType' => $sendType,
  101. 'customShopAdminId' => $shopAdminId,
  102. 'customShopAdminName' => $shopAdminName,
  103. 'ghsId' => $ghsId,
  104. 'cgStyle' => dict::getDict('cgStyle', 'hd'),
  105. 'couponId' => $couponId,
  106. 'discountType' => $discountType,
  107. 'discountAmount' => $discountAmount,
  108. 'remark' => $remark,
  109. 'xj' => $xj,
  110. 'sendTimeWant' => $sendTimeWant,
  111. 'discount' => $discount,
  112. 'orderType' => $orderType,
  113. 'fromType' => $fromType,
  114. 'presell' => $preSell,
  115. 'wlName' => $wlName,
  116. 'live' => $live,
  117. 'localOrder' => $localOrder,
  118. 'transType' => $transType,
  119. 'sendDistance' => $sendDistance,
  120. ];
  121. $result = OrderClass::addOrder($arr,$custom);
  122. $respond->saleId = $result['id'];
  123. $respond->save();
  124. return $respond;
  125. }
  126. //获取采购单信息 ssh 2021.1.24
  127. public static function getInfo($id, $showButton = false, $showProduct = false)
  128. {
  129. $purchase = PurchaseClass::getById($id, true);
  130. if (empty($purchase)) {
  131. util::fail('没有找到采购单');
  132. }
  133. $info = $purchase->attributes;
  134. $respond = PurchaseClass::groupPurchase([$info], $showButton, $showProduct);
  135. return current($respond);
  136. }
  137. //检查采购单
  138. public static function valid($info, $shopId)
  139. {
  140. if (empty($info)) {
  141. util::fail('没有找到采购单');
  142. }
  143. if (isset($info->shopId) && $info->shopId == $shopId) {
  144. return true;
  145. }
  146. util::fail('无法操作');
  147. }
  148. //采购记录 ssh 2021.1.24
  149. public static function getPurchaseList($where)
  150. {
  151. $data = PurchaseClass::getList('*', $where, 'addTime DESC');
  152. $data['list'] = self::groupInfo($data['list']);
  153. return $data;
  154. }
  155. //组合信息 ssh 2021.1.25
  156. public static function groupInfo($list)
  157. {
  158. if (empty($list)) {
  159. return $list;
  160. }
  161. foreach ($list as $key => $val) {
  162. $list[$key]['ghsAvatar'] = imgUtil::groupImg($val['ghsAvatar']);
  163. }
  164. return $list;
  165. }
  166. //所有的欠款采购单 ssh 2021.1.26
  167. public static function getDebtList($where)
  168. {
  169. $list = PurchaseClass::getAllList('*', $where, 'addTime DESC');
  170. $count = PurchaseClass::getCount($where);
  171. return ['list' => $list, 'debtNum' => $count];
  172. }
  173. //采购单失效 ssh 2021.5.14
  174. public static function expire($purchase)
  175. {
  176. if ($purchase->status != PurchaseClass::STATUS_UN_PAY) {
  177. util::fail('不能取消');
  178. }
  179. //采购单失效
  180. $id = $purchase->id;
  181. $purchase = PurchaseClass::cancel($id);
  182. $saleId = $purchase->saleId;
  183. //销售单失效
  184. OrderClass::cancel($saleId);
  185. $couponId = $purchase->hbId;
  186. if (!empty($couponId)) {
  187. $coupon = ShopCouponClass::getLockById($couponId);
  188. ShopCouponClass::rollback($coupon);
  189. }
  190. // OrderClass::cancel($saleId) 中已经清理了
  191. //清空限购缓存
  192. // $order = OrderClass::getById($saleId, true);
  193. // if (!empty($order)) {
  194. // $customId = $order->customId ?? 0;
  195. // $orderSn = $order->orderSn;
  196. // if (!empty($customId)) {
  197. // $orderItemList = OrderItemClass::getOrderItem($orderSn);
  198. // if (!empty($orderItemList)) {
  199. // foreach ($orderItemList as $item) {
  200. // $productId = $item['productId'] ?? 0;
  201. // $num = floor($item['num'] ?? 0);
  202. // if (!empty($productId) && $num > 0) {
  203. // ProductClass::baseClearLimitBuy($productId, $customId, $num);
  204. // }
  205. // }
  206. // }
  207. // }
  208. // }
  209. return $purchase;
  210. }
  211. //订单延期支付 ssh 2021.1.24
  212. public static function debt($info)
  213. {
  214. $payWay = dict::getDict('payWay', 'debtPay');
  215. $saleId = $info->saleId ?? 0;
  216. $order = OrderClass::getById($saleId, true);
  217. //批发店开的单,客户付款了,不需要打印也不需要微信通知
  218. if ($info->cgStyle == dict::getDict('cgStyle', 'ghs')) {
  219. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  220. $order->save();
  221. Yii::$app->params['noNeedWxNotice'] = 1;
  222. }
  223. self::payAfter($info, $payWay);
  224. OrderService::payAfter($order, $payWay, true);
  225. }
  226. //余额支付 ssh 2021.1.24
  227. public static function balancePay($info)
  228. {
  229. $payWay = dict::getDict('payWay', 'balancePay');
  230. self::payAfter($info, $payWay);
  231. $saleId = $info->saleId ?? 0;
  232. $order = OrderClass::getById($saleId, true);
  233. OrderService::payAfter($order, $payWay, true);
  234. }
  235. //付款码支付处理流程 ssh 20211231
  236. public static function codePay($payWay, $orderSn, $transactionId = '', $openId = '', $aliUserId = '')
  237. {
  238. //4秒内同个订单不允许重复请求
  239. $cacheKey = 'code_pay_f2f_' . $orderSn;
  240. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  241. if (!empty($has)) {
  242. Yii::info("付款码支付,重复查询支付结果:orderSn:{$orderSn}");
  243. noticeUtil::push("付款码支付,重复查询支付结果:orderSn:{$orderSn}", '15280215347');
  244. util::fail('请稍等...');
  245. }
  246. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
  247. $info = self::getByCondition(['orderSn' => $orderSn], true);
  248. if (empty($info)) {
  249. $msg = "没有找到订单 orderSn:{$orderSn}";
  250. Yii::info($msg);
  251. util::fail($msg);
  252. }
  253. //解决并发问题
  254. $id = $info->id;
  255. $info = PurchaseClass::getLockById($id);
  256. $info->onlinePay = dict::getDict('onlinePay', 'yes');
  257. $info->codePay = 1;
  258. $info->thirdNo = $transactionId;
  259. $info->payOpenId = $openId;
  260. $info->aliUserId = $aliUserId;
  261. //自取订单
  262. $info->getType = PurchaseClass::GET_TYPE_SELF_GET;
  263. $info->save();
  264. self::payAfter($info, $payWay);
  265. $saleId = $info->saleId ?? 0;
  266. $order = OrderClass::getById($saleId, true);
  267. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  268. $order->codePay = 1;
  269. //自取订单
  270. $order->sendType = OrderClass::SEND_TYPE_NO;
  271. //不需要小票
  272. $order->needPrint = dict::getDict('needPrint', 'noNeed');
  273. $order->save();
  274. OrderService::payAfter($order, $payWay, true);
  275. }
  276. //采购订单支付成功后回调处理流程
  277. public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId = '')
  278. {
  279. $info = self::getByCondition(['orderSn' => $orderSn], true);
  280. if (empty($info)) {
  281. $ghsName = '无供货商';
  282. preg_match('/\d+/', $orderSn, $m);
  283. if ($m[0]) {
  284. $cgSnId = $m[0];
  285. $snData = PurchaseSnClass::getById($cgSnId, true);
  286. $ghsId = $snData->ghsId ?? 0;
  287. if (!empty($ghsId)) {
  288. $ghs = \bizHd\ghs\classes\GhsClass::getById($ghsId, true);
  289. $ghsName = $ghs->name;
  290. }
  291. }
  292. noticeUtil::push('花店采购,付款成功,支付回调,单号:' . $orderSn . ',没有找到订单 ' . $ghsName . ',' . $attach, '15280215347');
  293. $msg = "没有找到订单 orderSn:{$orderSn}";
  294. util::fail($msg);
  295. }
  296. if ($info->actPrice != $totalFee) {
  297. noticeUtil::push("花店采购,付款成功,支付回调,单号:{$orderSn},订单金额与回调通知金额不一致{$info->actPrice} {$totalFee} {$attach}", '15280215347');
  298. $msg = "订单金额与回调通知金额不一致 orderSn:{$orderSn} {$info->actPrice} {$totalFee}";
  299. Yii::info($msg);
  300. util::fail($msg);
  301. }
  302. //临时解决微信一秒内通知二次问题
  303. $id = $info->id;
  304. $info = PurchaseClass::getLockById($id);
  305. //由批发店发起的采购单,零售付款时不需要微信通知
  306. if ($info->cgStyle == dict::getDict('cgStyle', 'ghs')) {
  307. Yii::$app->params['noNeedWxNotice'] = 1;
  308. }
  309. $info->onlinePay = dict::getDict('onlinePay', 'yes');
  310. $info->thirdNo = $transactionId;
  311. $info->save();
  312. self::payAfter($info, $payWay);
  313. $saleId = $info->saleId ?? 0;
  314. $order = OrderClass::getById($saleId, true);
  315. $order->onlinePay = dict::getDict('onlinePay', 'yes');
  316. $order->save();
  317. OrderService::payAfter($order, $payWay, true);
  318. }
  319. //第三方支付、余额支付和欠款支付后调用的流程 ssh 2021.4.27
  320. public static function payAfter($info, $payWay)
  321. {
  322. if (empty($info)) {
  323. util::fail('没有找到订单');
  324. }
  325. $deadline = $info->deadline;
  326. $book = $info->book ?? 0;
  327. $orderSn = $info->orderSn ?? '';
  328. $current = date("Y-m-d H:i:s");
  329. if (isset($info->status) == false) {
  330. noticeUtil::push('重点注意,采购单:' . $orderSn . ',客户付钱了但没有找到订单的状态', '15280215347');
  331. util::fail('没有找到订单状态');
  332. }
  333. if ($info->status == PurchaseClass::STATUS_CANCEL) {
  334. noticeUtil::push('注意花店采购单:' . $orderSn . ',客户付款了但订单已取消,关键词 cancel_get_money', '15280215347');
  335. $saleId = $info->saleId;
  336. $sale = OrderClass::getById($saleId, true);
  337. if (!empty($sale)) {
  338. $ghsShopId = $sale->shopId;
  339. $ghsShop = ShopClass::getById($ghsShopId, true);
  340. if (!empty($ghsShop)) {
  341. CgRefundClass::onlyRefundMoney($ghsShop, $info);
  342. }
  343. }
  344. util::stop("SUCCESS");
  345. }
  346. if (isset($info->payStatus) && $info->payStatus == 1) {
  347. util::fail('订单已经付过款了');
  348. }
  349. if ($deadline <= $current) {
  350. util::fail('订单已过期了哦');
  351. }
  352. //零售采购单状态变化
  353. $info->payWay = $payWay;
  354. $info->status = PurchaseClass::STATUS_UN_SEND;
  355. $info->payStatus = 1;
  356. $payTime = date("Y-m-d H:i:s");
  357. $info->payTime = $payTime;
  358. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  359. //欠款支付
  360. if (isset($info->actPrice) && $info->actPrice == 0) {
  361. $info->debt = 0;
  362. } else {
  363. $info->debt = 1;
  364. }
  365. $info->remainDebtPrice = $info->orderPrice ?? 0;
  366. $info->debtPrice = $info->orderPrice ?? 0;
  367. }
  368. $info->save();
  369. $orderSn = $info->orderSn ?? '';
  370. PurchaseItemClass::updateByCondition(['orderSn' => $orderSn], ['payTime' => $payTime]);
  371. //零售门店资产变化
  372. $shopId = $info['shopId'] ?? 0;
  373. $shop = ShopClass::getLockById($shopId);
  374. if (empty($shop)) {
  375. util::fail('没有找到门店63');
  376. }
  377. $mainId = $shop->mainId ?? 0;
  378. $main = MainClass::getLockById($mainId);
  379. if (empty($main)) {
  380. util::fail('没有main信息16');
  381. }
  382. $realPrice = $info->realPrice;
  383. $actPrice = $info->actPrice;
  384. //余额支付引起余额减少
  385. if ($payWay == dict::getDict('payWay', 'balancePay')) {
  386. ShopClass::purchaseReduceBalance($main, $shop, $realPrice, $info);
  387. }
  388. $main->totalPurchase = bcadd($main->totalPurchase, $actPrice, 2);
  389. $currentExpend = bcadd($main->totalExpend, $actPrice, 2);
  390. $main->totalExpend = $currentExpend;
  391. $main->cgUnPay -= 1;
  392. if ($book == 1) {
  393. $main->cgUnSend += 1;
  394. } else {
  395. $main->cgUnSend += 1;
  396. }
  397. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  398. //应付供货商款增加
  399. $currentMayPay = bcadd($main->mayPay, $realPrice, 2);
  400. $main->mayPay = $currentMayPay;
  401. }
  402. $main->save();
  403. //支出统计
  404. StatOutClass::updateOrInsert($main, $shop, $info->orderPrice);
  405. //支出流水
  406. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  407. $sjId = $info->sjId ?? 0;
  408. $shopId = $info->shopId ?? 0;
  409. $event = '采购';
  410. $capitalData = [
  411. 'capitalType' => $capitalType,
  412. 'io' => 0,
  413. 'totalExpend' => $currentExpend,
  414. 'payWay' => $payWay,
  415. 'amount' => $actPrice,
  416. 'sjId' => $sjId,
  417. 'shopId' => $shopId,
  418. 'event' => $event,
  419. 'mainId' => $mainId,
  420. ];
  421. ShopCapitalClass::addCapital($capitalData);
  422. //采购总量统计
  423. $cgNum = $info->bigNum ?? 0;
  424. StatCgClass::replace($main, $shop, $info->orderPrice, $cgNum);
  425. //按供货商分别统计采购量
  426. $num = $info->bigNum ?? 0;
  427. $amount = $info->orderPrice ?? 0;
  428. StatCgGhsClass::hdCgGhsReplace(true, $info, $amount, $num);
  429. //供货商资产变化
  430. $ghsId = $info->ghsId ?? 0;
  431. $ghs = GhsClass::getLockById($ghsId);
  432. if (empty($ghs)) {
  433. util::fail('没有找到供货商');
  434. }
  435. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  436. \bizHd\ghs\classes\GhsClass::cgDebtAmountAdd($ghs, $info);
  437. }
  438. $ghs->expendAmount = bcadd($ghs->expendAmount, $actPrice, 2);
  439. $ghs->expendNum += 1;
  440. $ghs->save();
  441. }
  442. }