优化财务流水写入

This commit is contained in:
luofei 2024-01-30 13:56:27 +08:00
parent f5aaee07e1
commit 38157d4bbb
4 changed files with 107 additions and 49 deletions

View File

@ -20,32 +20,29 @@ class CommissionDao
/**
* 活动首单商户佣金 (支付成功后调用)
* @param $order
* @param $finance
* @param $financeSn
* @param $financeDao
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function firstOrderCommission($order, $finance, $financeSn, $financeIndex)
public function firstOrderCommission($order, $financeDao)
{
$consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find();
if (empty($consumption)) {
return $finance;
return $financeDao;
}
$storeConsumptionDao = new StoreConsumptionUserDao();
$isFirstOrder = $storeConsumptionDao->isFirstOrder($order['uid'], $consumption['start_time'], $consumption['end_time']);
if (!$isFirstOrder) {
return $finance;
return $financeDao;
}
$financeDao = new FinancialDao();
$commission = bcmul($order['pay_price'], 0.01, 2);
if ($commission > 0 && $order['order_type'] == 1) {
// 订单为自提且佣金大于0
$financeDao->user = $order->user;
$financeDao->order = $order;
$finance[] = $financeDao->platformOut($commission, 'first_order_commission', $financeSn, $financeIndex);
$financeIndex++;
$financeDao->platformOut($commission, 'first_order_commission');
app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission);
}
// 给镇合伙人、村合伙人、小组服务团队、店铺分佣,仅直推
@ -58,7 +55,7 @@ class CommissionDao
$commission = bcmul($order['pay_price'], 0.03, 2);
$financeDao->user = $user;
$financeDao->order = $order;
$finance[] = $financeDao->platformOut($commission, 'first_order_commission', $financeSn, $financeIndex);
$financeDao->platformOut($commission, 'first_order_commission');
app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission);
$redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2);
if ($redPack > 0) {
@ -72,7 +69,7 @@ class CommissionDao
$this->sendCommission($order, $promotionCode);
}
}
return $finance;
return $financeDao;
}
/**

View File

@ -16,6 +16,7 @@ namespace app\common\dao\system\financial;
use app\common\dao\BaseDao;
use app\common\model\system\financial\Financial;
use app\common\model\system\merchant\FinancialRecord;
use app\common\repositories\system\merchant\FinancialRecordRepository;
class FinancialDao extends BaseDao
@ -23,6 +24,9 @@ class FinancialDao extends BaseDao
public $order;
public $user;
public $index = 0;
public $financeSn;
public $list = [];
protected function getModel(): string
{
@ -87,65 +91,53 @@ class FinancialDao extends BaseDao
* 平台出账财务流水
* @param $number
* @param $financialType
* @param $financeSn
* @param $financeIndex
* @param $merId
* @return array
*/
public function platformOut($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '')
public function platformOut($number, $financialType, $merId = '')
{
return $this->setData($number, $financialType, 0, 2, $financeSn, $financeIndex, $merId);
$this->setData($number, $financialType, 0, 2, $merId);
}
/**
* 平台入账财务流水
* @param $number
* @param $financialType
* @param $financeSn
* @param $financeIndex
* @param $merId
* @return array
*/
public function platformIn($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '')
public function platformIn($number, $financialType, $merId = '')
{
return $this->setData($number, $financialType, 1, 2, $financeSn, $financeIndex, $merId);
$this->setData($number, $financialType, 1, 2, $merId);
}
/**
* 公共入账财务流水
* @param $number
* @param $financialType
* @param $financeSn
* @param $financeIndex
* @param $merId
* @return array
*/
public function publicOut($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '')
public function publicOut($number, $financialType, $merId = '')
{
return $this->setData($number, $financialType, 0, 1, $financeSn, $financeIndex, $merId);
$this->setData($number, $financialType, 0, 1, $merId);
}
/**
* 公共入账财务流水
* @param $number
* @param $financialType
* @param $financeSn
* @param $financeIndex
* @param $merId
* @return array
*/
public function publicIn($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '')
public function publicIn($number, $financialType, $merId = '')
{
return $this->setData($number, $financialType, 1, 1, $financeSn, $financeIndex, $merId);
$this->setData($number, $financialType, 1, 1, $merId);
}
public function setData($number, $financialType, $pm, $type = 2, $financeSn = '', $financeIndex = '', $merId = '')
public function setData($number, $financialType, $pm, $type = 2, $merId = '')
{
if (empty($financeSn)) {
if (empty($this->financeSn)) {
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
$financeSn = $financialRecordRepository->getSn();
$this->financeSn = $financialRecordRepository->getSn();
}
return [
$this->list[] = [
'order_id' => $this->order->order_id,
'order_sn' => $this->order->order_sn,
'user_info' => $this->user['nickname'],
@ -155,8 +147,15 @@ class FinancialDao extends BaseDao
'type' => $type,
'number' => $number,
'mer_id' => !empty($merId) ? $merId : $this->order->mer_id,
'financial_record_sn' => $financeSn . $financeIndex
'financial_record_sn' => $this->financeSn . ($this->index++)
];
}
public function save()
{
if (count($this->list) > 0) {
(new FinancialRecord())->insertAll($this->list);
}
}
}

View File

@ -216,10 +216,7 @@ class StoreOrderRepository extends BaseRepository
$orderStatus = [];
$groupOrder->append(['orderList.orderProduct']);
$flag = true;
$finance = [];
$profitsharing = [];
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
$financeSn = $financialRecordRepository->getSn();
$userMerchantRepository = app()->make(UserMerchantRepository::class);
$storeOrderProfitsharingRepository = app()->make(StoreOrderProfitsharingRepository::class);
$uid = $groupOrder->uid;
@ -308,25 +305,25 @@ class StoreOrderRepository extends BaseRepository
$financeDao->user = $groupOrder->user;
$financialType = $presell ? 'order_presell' : 'order';
// 商户流水账单数据
$finance[] = $financeDao->setData($order->pay_price, $financialType, 1, $presell ? 2 : 1, $financeSn, $i++);
$financeDao->setData($order->pay_price, $financialType, 1, $presell ? 2 : 1);
if ($order->source == 103) {
$_payPrice = $order->procure_price;
$finance[] = $financeDao->publicOut($_payPrice, 'supply_chain', $financeSn, $i++);
$financeDao->publicOut($_payPrice, 'supply_chain');
//市级供应链
$product_mer_id = Db::name('store_order_product')->where('order_id', $order->order_id)->value('product_mer_id');
if ($product_mer_id) {
$finance[] = $financeDao->publicOut($_payPrice, 'order', $financeSn, $i++, $product_mer_id);
$financeDao->publicOut($_payPrice, 'order', $product_mer_id);
//市级供应链押金计算
if ($_payPrice > 0) {
/** @var MerchantRepository $merchantRepo */
$merchantRepo = app()->make(MerchantRepository::class);
$merchantRepo->merId = $product_mer_id;
$merchantRepo->forceMargin = false;
[$_payCityPrice, $finance, $increase] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++);
[$_payCityPrice, $financeDao] = $merchantRepo->deductDeposit($_payPrice, $order, $financeDao);
}
if (isset($_payCityPrice)) {
$finance[] = $financeDao->platformOut($_payCityPrice, 'order_true', $financeSn, $i++, $product_mer_id);
$financeDao->platformOut($_payCityPrice, 'order_true', $product_mer_id);
if (!$is_combine) {
app()->make(MerchantRepository::class)->addLockMoney($product_mer_id, 'order', $order->order_id, $_payCityPrice);
}
@ -376,7 +373,7 @@ class StoreOrderRepository extends BaseRepository
if (!$presell) {
if ($order['commission_rate'] > 0 || $order->source == 103) {
//支出手续费
$finance[] = $financeDao->publicOut($_order_rate, 'order_charge', $financeSn, $i++);
$financeDao->publicOut($_order_rate, 'order_charge');
}
//押金计算
if ($_payPrice > 0) {
@ -384,12 +381,12 @@ class StoreOrderRepository extends BaseRepository
$merchantRepo = app()->make(MerchantRepository::class);
$merchantRepo->merId = $order->mer_id;
$merchantRepo->forceMargin = false;
[$_payPrice, $finance, $increase] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++);
[$_payCityPrice, $financeDao] = $merchantRepo->deductDeposit($_payPrice, $order, $financeDao);
}
if ($order->source == 103) {
$finance[] = $financeDao->publicOut($_payPrice, 'commission_to_cloud_warehouse', $financeSn, $i++);
$financeDao->publicOut($_payPrice, 'commission_to_cloud_warehouse');
} else {
$finance[] = $financeDao->platformOut($_payPrice, 'order_true', $financeSn, $i++);
$financeDao->platformOut($_payPrice, 'order_true');
}
@ -436,7 +433,7 @@ class StoreOrderRepository extends BaseRepository
$this->autoPrinter($order->order_id, $order->mer_id);
if ($order['pay_price'] > 0 && $order['source'] == 103) {
// "惠农供销,谱写数字新篇章"活动首单分润,商户和村、小组合伙人
$finance = (new CommissionDao())->firstOrderCommission($order, $finance, $financeSn, $i++);
$financeDao = (new CommissionDao())->firstOrderCommission($order, $financeDao);
}
}
//分销判断
@ -453,7 +450,7 @@ class StoreOrderRepository extends BaseRepository
if (count($profitsharing)) {
$storeOrderProfitsharingRepository->insertAll($profitsharing);
}
$financialRecordRepository->insertAll($finance);
$financeDao->save();
$storeOrderStatusRepository->batchCreateLog($orderStatus);
if (count($groupOrder['give_coupon_ids']) > 0)
$groupOrder['give_coupon_ids'] = app()->make(StoreCouponRepository::class)->getGiveCoupon($groupOrder['give_coupon_ids'])->column('coupon_id');

View File

@ -14,6 +14,7 @@
namespace app\common\repositories\system\merchant;
use app\common\dao\system\financial\FinancialDao;
use app\common\dao\system\merchant\MerchantDao;
use app\common\dao\system\serve\ServeOrderDao;
use app\common\model\store\order\StoreGroupOrder;
@ -676,6 +677,7 @@ class MerchantRepository extends BaseRepository
}
/**
* @deprecated 用下面的 deductDeposit
* 自动扣除押金
* @param $income
* @param $order
@ -748,4 +750,67 @@ class MerchantRepository extends BaseRepository
return [bcsub($income,$margin,2), $finance, true];
}
/**
* 自动扣除押金
* @param $income
* @param $order
* @param FinancialDao $financeDao
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function deductDeposit($income, $order, FinancialDao $financeDao)
{
$merchant = Merchant::find($this->merId);
// $margin_type = Db::name('MerchantType')->where('mer_type_id', $merchant['type_id'])->value('margin');
//商户押金大于支付押金 或者forceMargin==false 直接返回 不计算押金
if ($merchant['paid_margin']>= $merchant['margin']|| ($this->forceMargin === false && $merchant['auto_margin_rate'] == 0)) {
return [$income, $financeDao];
}
$rate = $this->forceMargin ? 100 : $merchant['auto_margin_rate'];
// //商户押金未完全缴纳且设置了自动扣除比例
$margin= bcmul($income, bcdiv($rate, 100,2), 2);
// $margin = min(bcsub($margin, $merchant['paid_margin'], 2), $margin);
// $income = max(bcsub($income, $margin, 2), 0);
// if ($margin <= 0) {
// return [$income, $financeDao];
// }
$financeDao->publicOut($margin, 'auto_margin', $this->merId);
if(bcadd($merchant['paid_margin'],$margin)>=$merchant['margin']){
$is_margin=10;
}else{
$is_margin=1;
}
$orderInfo = [
'type_id' => $merchant['type_id'],
'is_margin' => $is_margin,
'margin' => $margin,
];
$values = [
'status' => 1,
'is_del' => 0,
'mer_id' => $merchant['mer_id'],
'type' => ServeOrderRepository::TYPE_MARGIN,
'meal_id'=> $merchant['type_id'],
'pay_type' => ServeOrderRepository::PAY_TYPE_BALANCE,
'order_info' => json_encode($orderInfo,JSON_UNESCAPED_UNICODE),
'pay_price' => $margin,
'store_order_id' => $order['order_id'],
];
$values['order_sn'] = app()->make(StoreOrderRepository::class)->getNewOrderId('cs');
$values['pay_time'] = date('y_m-d H:i:s', time());
if (!app()->make(ServeOrderDao::class)->create($values)) {
throw new \Exception('serve_order 保存出错', 500);
}
$merchant->paid_margin = bcadd($margin, $merchant->paid_margin, 2);
$merchant->ot_margin = $merchant->paid_margin;
$merchant->is_margin = $is_margin;
if ($merchant->save() === false) {
throw new \Exception('merchant 保存出错', 500);
}
return [bcsub($income,$margin,2), $financeDao];
}
}