PurchaseService.php 17 KB

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