| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- namespace bizHd\purchase\services;
- use biz\ghs\classes\GhsClass;
- use biz\ghs\models\Ghs;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopCouponClass;
- use biz\shop\models\Shop;
- use biz\stat\classes\StatCgClass;
- use biz\stat\classes\StatCgGhsClass;
- use biz\stat\classes\StatOutClass;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\order\classes\OrderClass;
- use bizGhs\order\services\OrderService;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizGhs\ws\services\WsService;
- use bizHd\admin\classes\ShopAdminClass;
- use bizHd\base\services\BaseService;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\classes\PurchaseItemClass;
- use bizHd\shop\classes\MainClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\util;
- use Yii;
- class PurchaseService extends BaseService
- {
- public static $baseFile = '\bizHd\purchase\classes\PurchaseClass';
- public static function createPurchase($data, $ghs)
- {
- //零售采购单
- $respond = PurchaseClass::addPurchase($data, $ghs);
- //供货商销售单
- $ghsId = $data['ghsId'] ?? 0;
- $currentShopId = $ghs['shopId'] ?? 0;
- $currentSjId = $ghs['sjId'] ?? 0;
- $product = $data['product'] ?? [];
- $sendCost = $data['sendCost'] ?? 0;
- $customId = $ghs['customId'] ?? 0;
- $custom = CustomClass::getById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- $customName = $custom['name'] ?? '';
- $customNamePy = $custom['py'] ?? '';
- $customMobile = $custom['mobile'] ?? '';
- $purchaseId = $respond->id ?? 0;
- $shopAdminId = $data['shopAdminId'] ?? 0;
- $shopAdminName = $data['shopAdminName'] ?? '';
- //客户采购时选了自取,则供货商的销售单就是自取
- $sendType = OrderClass::SEND_TYPE_UNKNOWN;
- $getType = $data['getType'] ?? PurchaseClass::GET_TYPE_SELF_GET;
- if ($getType == PurchaseClass::GET_TYPE_SELF_GET) {
- $sendType = OrderClass::SEND_TYPE_NO;
- }
- $couponId = $data['couponId'] ?? 0;
- $discountType = $respond->discountType ?? dict::getDict('discountType', 'noDiscount');
- $discountAmount = $respond->discountAmount ?? 0;
- $discount = $respond->discount ?? 0;
- $remark = $data['remark'] ?? '';
- $xj = $data['xj'] ?? '';
- $sendTimeWant = isset($data['sendTimeWant']) && !empty($data['sendTimeWant']) ? $data['sendTimeWant'] : date("Y-m-d");
- $orderType = 2;
- $book = $data['book'] ?? 0;
- $mainId = $custom['ownMainId'] ?? 0;
- $fromType = dict::getDict('fromType', 'mall');
- $arr = [
- 'mainId' => $mainId,
- 'book' => $book,
- 'product' => $product,
- 'sjId' => $currentSjId,
- 'shopId' => $currentShopId,
- 'sendCost' => $sendCost,
- 'customId' => $customId,
- 'customName' => $customName,
- 'customNamePy' => $customNamePy,
- 'customMobile' => $customMobile,
- 'province' => $custom['province'] ?? '',
- 'city' => $custom['city'] ?? '',
- 'dist' => $custom['dist'] ?? '',
- 'address' => $custom['address'] ?? '',
- 'floor' => $custom['floor'] ?? '',
- 'fullAddress' => $custom['fullAddress'] ?? '',
- 'showAddress' => $custom['showAddress'] ?? '',
- 'long' => $custom['long'] ?? '',
- 'lat' => $custom['lat'] ?? '',
- 'purchaseId' => $purchaseId,
- 'sendType' => $sendType,
- 'customShopAdminId' => $shopAdminId,
- 'customShopAdminName' => $shopAdminName,
- 'ghsId' => $ghsId,
- 'cgStyle' => dict::getDict('cgStyle', 'hd'),
- 'couponId' => $couponId,
- 'discountType' => $discountType,
- 'discountAmount' => $discountAmount,
- 'remark' => $remark,
- 'xj' => $xj,
- 'sendTimeWant' => $sendTimeWant,
- 'discount' => $discount,
- 'orderType' => $orderType,
- 'fromType' => $fromType,
- ];
- $result = OrderClass::addOrder($arr);
- $saleId = $result['id'] ?? 0;
- $respond->saleId = $saleId;
- $respond->save();
- return $respond;
- }
- //获取采购单信息 ssh 2021.1.24
- public static function getInfo($id, $showButton = false, $showProduct = false)
- {
- $purchase = PurchaseClass::getById($id, true);
- if (empty($purchase)) {
- util::fail('没有找到采购单');
- }
- $info = $purchase->attributes;
- $respond = PurchaseClass::groupPurchase([$info], $showButton, $showProduct);
- return current($respond);
- }
- //检查采购单
- public static function valid($info, $shopId)
- {
- if (empty($info)) {
- util::fail('没有找到采购单');
- }
- if (isset($info->shopId) && $info->shopId == $shopId) {
- return true;
- }
- util::fail('没有权限操作');
- }
- //采购记录 ssh 2021.1.24
- public static function getPurchaseList($where)
- {
- $data = PurchaseClass::getList('*', $where, 'addTime DESC');
- $data['list'] = self::groupInfo($data['list']);
- return $data;
- }
- //组合信息 ssh 2021.1.25
- public static function groupInfo($list)
- {
- if (empty($list)) {
- return $list;
- }
- foreach ($list as $key => $val) {
- $list[$key]['ghsAvatar'] = imgUtil::groupImg($val['ghsAvatar']);
- }
- return $list;
- }
- //所有的欠款采购单 ssh 2021.1.26
- public static function getDebtList($where)
- {
- $list = PurchaseClass::getAllList('*', $where, 'addTime DESC');
- $count = PurchaseClass::getCount($where);
- return ['list' => $list, 'debtNum' => $count];
- }
- //采购单失效 ssh 2021.5.14
- public static function expire($purchase)
- {
- if ($purchase->status != PurchaseClass::STATUS_UN_PAY) {
- util::fail('不是待付款订单哦');
- }
- //采购单失效
- $id = $purchase->id;
- $purchase = PurchaseClass::cancel($id);
- $saleId = $purchase->saleId;
- //销售单失效
- OrderClass::cancel($saleId);
- $couponId = $purchase->hbId;
- if (!empty($couponId)) {
- $coupon = ShopCouponClass::getLockById($couponId);
- ShopCouponClass::rollback($coupon);
- }
- return $purchase;
- }
- //订单延期支付 ssh 2021.1.24
- public static function debt($info)
- {
- $payWay = dict::getDict('payWay', 'debtPay');
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- //批发店开的单,客户付款了,不需要打印也不需要微信通知
- if ($info->cgStyle == dict::getDict('cgStyle', 'ghs')) {
- $order->needPrint = dict::getDict('needPrint', 'noNeed');
- $order->save();
- Yii::$app->params['noNeedWxNotice'] = 1;
- }
- self::payAfter($info, $payWay);
- OrderService::payAfter($order, $payWay, true);
- }
- //余额支付 ssh 2021.1.24
- public static function balancePay($info)
- {
- $payWay = dict::getDict('payWay', 'balancePay');
- self::payAfter($info, $payWay);
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- OrderService::payAfter($order, $payWay, true);
- }
- //付款码支付处理流程 ssh 20211231
- public static function codePay($payWay, $orderSn, $transactionId = '', $openId = '', $aliUserId = '')
- {
- //4秒内同个订单不允许重复请求
- $cacheKey = 'code_pay_f2f_' . $orderSn;
- $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (!empty($has)) {
- Yii::info("付款码支付,重复查询支付结果:orderSn:{$orderSn}");
- noticeUtil::push("付款码支付,重复查询支付结果:orderSn:{$orderSn}", '15280215347');
- util::fail('请稍等...');
- }
- Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 4, 'has']);
- $info = self::getByCondition(['orderSn' => $orderSn], true);
- if (empty($info)) {
- $msg = "没有找到订单 orderSn:{$orderSn}";
- Yii::info($msg);
- util::fail($msg);
- }
- //解决并发问题
- $id = $info->id;
- $info = PurchaseClass::getLockById($id);
- $info->onlinePay = dict::getDict('onlinePay', 'yes');
- $info->codePay = 1;
- $info->thirdNo = $transactionId;
- $info->payOpenId = $openId;
- $info->aliUserId = $aliUserId;
- //自取订单
- $info->getType = PurchaseClass::GET_TYPE_SELF_GET;
- $info->save();
- self::payAfter($info, $payWay);
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- $order->onlinePay = dict::getDict('onlinePay', 'yes');
- $order->codePay = 1;
- //自取订单
- $order->sendType = OrderClass::SEND_TYPE_NO;
- //不需要小票
- $order->needPrint = dict::getDict('needPrint', 'noNeed');
- $order->save();
- OrderService::payAfter($order, $payWay, true);
- }
- //采购订单支付成功后回调处理流程
- public static function thirdPay($payWay, $orderSn, $totalFee, $attach, $transactionId = '')
- {
- $info = self::getByCondition(['orderSn' => $orderSn], true);
- if (empty($info)) {
- $msg = "没有找到订单 orderSn:{$orderSn}";
- Yii::info($msg);
- util::fail($msg);
- }
- if ($info->actPrice != $totalFee) {
- $msg = "订单金额与回调通知金额不一致 orderSn:{$orderSn} {$info->actPrice} {$totalFee}";
- Yii::info($msg);
- util::fail($msg);
- }
- //临时解决微信一秒内通知二次问题
- $id = $info->id;
- $info = PurchaseClass::getLockById($id);
- //由批发店发起的采购单,零售付款时不需要微信通知
- if ($info->cgStyle == dict::getDict('cgStyle', 'ghs')) {
- Yii::$app->params['noNeedWxNotice'] = 1;
- }
- $info->onlinePay = dict::getDict('onlinePay', 'yes');
- $info->thirdNo = $transactionId;
- $info->save();
- self::payAfter($info, $payWay);
- $saleId = $info->saleId ?? 0;
- $order = OrderClass::getById($saleId, true);
- $order->onlinePay = dict::getDict('onlinePay', 'yes');
- $order->save();
- OrderService::payAfter($order, $payWay, true);
- }
- //第三方支付、余额支付和欠款支付后调用的流程 ssh 2021.4.27
- public static function payAfter($info, $payWay)
- {
- if (empty($info)) {
- util::fail('没有找到订单');
- }
- $deadline = $info->deadline;
- $book = $info->book ?? 0;
- $current = date("Y-m-d H:i:s");
- if ($deadline <= $current) {
- Yii::info($deadline . ' ' . $current);
- util::fail('订单已失效..');
- }
- if (isset($info->status) == false || $info->status != PurchaseClass::STATUS_UN_PAY) {
- util::fail('订单不是待付款状态');
- }
- //零售采购单状态变化
- $info->payWay = $payWay;
- $info->status = $book == 1 ? PurchaseClass::STATUS_UN_SEND : PurchaseClass::STATUS_COMPLETE;
- $info->payStatus = 1;
- $info->payTime = date("Y-m-d H:i:s");
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- //欠款支付
- $info->debt = 1;
- $info->remainDebtPrice = $info->orderPrice ?? 0;
- $info->debtPrice = $info->orderPrice ?? 0;
- }
- $info->save();
- //零售门店资产变化
- $shopId = $info['shopId'] ?? 0;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息');
- }
- $realPrice = $info->realPrice;
- $actPrice = $info->actPrice;
- //余额支付引起余额减少
- if ($payWay == dict::getDict('payWay', 'balancePay')) {
- ShopClass::purchaseReduceBalance($main, $shop, $realPrice, $info);
- }
- $main->totalPurchase = bcadd($main->totalPurchase, $actPrice, 2);
- $currentExpend = bcadd($main->totalExpend, $actPrice, 2);
- $main->totalExpend = $currentExpend;
- $main->cgUnPay -= 1;
- if ($book == 1) {
- $main->cgUnSend += 1;
- } else {
- $main->cgFinish += 1;
- //加库存 lqh 2021-05-10
- $orderSn = $info->orderSn;
- $purchaseItem = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, "*");
- foreach ($purchaseItem as $v) {
- $stockInfo = ProductClass::addStockByItemNum($v['productId'], $v['itemNum']);
- //记录库存流水
- $recordData = [];
- $recordData['sjId'] = $info->sjId;
- $recordData['shopId'] = $info->shopId;
- $recordData['mainId'] = $info->mainId;
- $recordData['itemId'] = $v['itemId'] ?? 0;
- $recordData['itemNum'] = $v['itemNum'];
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $v['productId'];
- $recordData['orderSn'] = $orderSn;
- $recordData['relateName'] = $info->ghsName ?? '';
- StockRecordClass::addPurchaseOrderRecord($recordData);
- }
- $info->stockChange = 1;
- $info->save();
- }
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- //应付供货商款增加
- $currentMayPay = bcadd($main->mayPay, $realPrice, 2);
- $main->mayPay = $currentMayPay;
- }
- $main->save();
- //支出统计
- StatOutClass::updateOrInsert($main, $shop, $info->orderPrice);
- //支出流水
- $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
- $sjId = $info->sjId ?? 0;
- $shopId = $info->shopId ?? 0;
- $event = '采购';
- $capitalData = [
- 'capitalType' => $capitalType,
- 'io' => 0,
- 'totalExpend' => $currentExpend,
- 'payWay' => $payWay,
- 'amount' => $actPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- //采购总量统计
- $cgNum = $info->bigNum ?? 0;
- StatCgClass::replace($main, $shop, $info->orderPrice, $cgNum);
- //按供货商分别统计采购量
- $num = $info->bigNum ?? 0;
- $amount = $info->orderPrice ?? 0;
- StatCgGhsClass::hdCgGhsReplace(true, $info, $amount, $num);
- //供货商资产变化
- $ghsId = $info->ghsId ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- \bizHd\ghs\classes\GhsClass::cgDebtAmountAdd($ghs, $info);
- }
- $ghs->expendAmount = bcadd($ghs->expendAmount, $actPrice, 2);
- $ghs->expendNum += 1;
- $ghs->save();
- //商家给开的采购单通知客户
- if (isset($info->ghsShopAdminId) && !empty($info->ghsShopAdminId)) {
- $noNeedWxNotice = isset(Yii::$app->params['noNeedWxNotice']) ? Yii::$app->params['noNeedWxNotice'] : 0;
- if ($noNeedWxNotice == 0) {
- WxMessageClass::generateCgOrder($shop, $info);
- }
- }
- }
- }
|