优化财务流水写入
This commit is contained in:
parent
ccbffc730e
commit
f5aaee07e1
@ -2,6 +2,7 @@
|
||||
|
||||
namespace app\common\dao\store\consumption;
|
||||
|
||||
use app\common\dao\system\financial\FinancialDao;
|
||||
use app\common\model\store\consumption\StoreConsumption;
|
||||
use app\common\model\store\consumption\StoreConsumptionUser;
|
||||
use app\common\model\store\order\StoreOrder;
|
||||
@ -37,21 +38,13 @@ class CommissionDao
|
||||
if (!$isFirstOrder) {
|
||||
return $finance;
|
||||
}
|
||||
$financeDao = new FinancialDao();
|
||||
$commission = bcmul($order['pay_price'], 0.01, 2);
|
||||
if ($commission > 0 && $order['order_type'] == 1) {
|
||||
// 订单为自提,且佣金大于0
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $order->user->nickname,
|
||||
'user_id' => $order['uid'],
|
||||
'financial_type' => 'first_order_commission', // TODO 这个类型应该是长期的
|
||||
'financial_pm' => 0,
|
||||
'type' => 2,
|
||||
'number' => $commission,
|
||||
'mer_id' => $order->mer_id,
|
||||
'financial_record_sn' => $financeSn . $financeIndex
|
||||
];
|
||||
$financeDao->user = $order->user;
|
||||
$financeDao->order = $order;
|
||||
$finance[] = $financeDao->platformOut($commission, 'first_order_commission', $financeSn, $financeIndex);
|
||||
$financeIndex++;
|
||||
app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission);
|
||||
}
|
||||
@ -63,18 +56,9 @@ class CommissionDao
|
||||
$userId = Merchant::where('mer_id', $merchantId)->value('uid');
|
||||
$user = User::where('uid', $userId)->find();
|
||||
$commission = bcmul($order['pay_price'], 0.03, 2);
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $user['nickname'],
|
||||
'user_id' => $user['uid'],
|
||||
'financial_type' => 'first_order_commission',
|
||||
'financial_pm' => 0,
|
||||
'type' => 2,
|
||||
'number' => $commission,
|
||||
'mer_id' => $order->mer_id,
|
||||
'financial_record_sn' => $financeSn . $financeIndex
|
||||
];
|
||||
$financeDao->user = $user;
|
||||
$financeDao->order = $order;
|
||||
$finance[] = $financeDao->platformOut($commission, 'first_order_commission', $financeSn, $financeIndex);
|
||||
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) {
|
||||
@ -114,21 +98,14 @@ class CommissionDao
|
||||
}
|
||||
$finance = [];
|
||||
$result = [];
|
||||
$financeDao = new FinancialDao();
|
||||
foreach ($users as $k => $user) {
|
||||
$commission = bcdiv($user['user_profit'], 100, 2);
|
||||
if ($commission > 0) {
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $user['nickname'],
|
||||
'user_id' => $user['uid'],
|
||||
'financial_type' => $user['type'] == 3 ? 'order_commission' : 'first_order_commission', // TODO 配送员的佣金类型需要调整
|
||||
'financial_pm' => 0,
|
||||
'type' => 2,
|
||||
'number' => $commission,
|
||||
'mer_id' => $order['mer_id'],
|
||||
'financial_record_sn' => $financeSn . ($k + 1)
|
||||
];
|
||||
$financeDao->user = $user;
|
||||
$financeDao->order = $order;
|
||||
$financialType = $user['type'] == 3 ? 'order_commission' : 'first_order_commission'; // TODO 配送员的佣金类型需要调整
|
||||
$finance[] = $financeDao->platformOut($commission, $financialType, $financeSn, $k + 1);
|
||||
$result[] = $user;
|
||||
}
|
||||
$redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2);
|
||||
@ -186,25 +163,6 @@ class CommissionDao
|
||||
*/
|
||||
public function refundByOrder($refundOrder)
|
||||
{
|
||||
$commission = bcmul($refundOrder->order['pay_price'], 0.01, 2);
|
||||
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
|
||||
$financeSn = $financialRecordRepository->getSn();
|
||||
if ($commission > 0 && $refundOrder->order['order_type'] == 1) {
|
||||
// 订单为自提,且佣金大于0
|
||||
$finance[] = [
|
||||
'order_id' => $refundOrder->order->order_id,
|
||||
'order_sn' => $refundOrder->order->order_sn,
|
||||
'user_info' => $refundOrder->order->user->nickname,
|
||||
'user_id' => $refundOrder->order['uid'],
|
||||
'financial_type' => 'first_order_commission_refund',
|
||||
'financial_pm' => 1,
|
||||
'type' => 2,
|
||||
'number' => $commission,
|
||||
'mer_id' => $refundOrder->order->mer_id,
|
||||
'financial_record_sn' => $financeSn
|
||||
];
|
||||
app()->make(MerchantRepository::class)->subLockMoney($refundOrder->order['mer_id'], 'order', $refundOrder->order['order_id'], $commission);
|
||||
}
|
||||
// 是否已经退过佣金
|
||||
$refunded = Financial::where('order_id', $refundOrder->order['order_id'])
|
||||
->whereIn('financial_type', ['order_commission_refund', 'first_order_commission_refund'])
|
||||
@ -212,27 +170,29 @@ class CommissionDao
|
||||
if ($refunded > 0) {
|
||||
return;
|
||||
}
|
||||
$commission = bcmul($refundOrder->order['pay_price'], 0.01, 2);
|
||||
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
|
||||
$financeSn = $financialRecordRepository->getSn();
|
||||
$financeDao = new FinancialDao();
|
||||
$finance = [];
|
||||
if ($commission > 0 && $refundOrder->order['order_type'] == 1) {
|
||||
// 订单为自提,且佣金大于0,下单的店铺退佣金
|
||||
$financeDao->user = $refundOrder->order->user;
|
||||
$financeDao->order = $refundOrder->order;
|
||||
$finance[] = $financeDao->platformIn($commission, 'first_order_commission_refund', $financeSn, 0);
|
||||
app()->make(MerchantRepository::class)->subLockMoney($refundOrder->order['mer_id'], 'order', $refundOrder->order['order_id'], $commission);
|
||||
}
|
||||
|
||||
// 退佣金和红包
|
||||
$finance = [];
|
||||
$financeRecord = Financial::where('order_id', $refundOrder->order['order_id'])
|
||||
->whereIn('financial_type', ['order_commission', 'first_order_commission'])
|
||||
->select()->toArray();
|
||||
->field('user_id uid,user_info nickname')->select()->toArray();
|
||||
$redPack = bcmul($refundOrder->order['pay_price'], 0.07, 2);
|
||||
(new StoreConsumptionUserDao())->refundByCommission($refundOrder->order['uid'], $refundOrder->order->order_id, $redPack);
|
||||
foreach ($financeRecord as $k => $item) {
|
||||
$finance[] = [
|
||||
'order_id' => $refundOrder->order->order_id,
|
||||
'order_sn' => $refundOrder->order->order_sn,
|
||||
'user_info' => $item['user_info'],
|
||||
'user_id' => $item['user_id'],
|
||||
'financial_type' => $item['financial_type'] . '_refund',
|
||||
'financial_pm' => 1,
|
||||
'type' => 2,
|
||||
'number' => $item['number'],
|
||||
'mer_id' => $item['mer_id'],
|
||||
'financial_record_sn' => $financeSn . ($k + 1)
|
||||
];
|
||||
$financeDao->user = $item;
|
||||
$financeDao->order = $refundOrder->order;
|
||||
$finance[] = $financeDao->platformIn($item['number'], $item['financial_type'] . '_refund', $financeSn, $k + 1);
|
||||
}
|
||||
if (count($finance) > 0) {
|
||||
$financialRecordRepository->insertAll($finance);
|
||||
@ -275,21 +235,14 @@ class CommissionDao
|
||||
}
|
||||
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
|
||||
$financeSn = $financialRecordRepository->getSn();
|
||||
$financeDao = new FinancialDao();
|
||||
foreach ($users as $k => $user) {
|
||||
$commission = bcdiv($user['user_profit'], 100, 2);
|
||||
if ($commission > 0) {
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $user['nickname'],
|
||||
'user_id' => $user['uid'],
|
||||
'financial_type' => ($user['type'] == 3 ? 'order_commission' : 'first_order_commission') . '_refund',
|
||||
'financial_pm' => 1,
|
||||
'type' => 2,
|
||||
'number' => $commission,
|
||||
'mer_id' => $order['mer_id'],
|
||||
'financial_record_sn' => $financeSn . ($k + 1)
|
||||
];
|
||||
$financeDao->user = $user;
|
||||
$financeDao->order = $order;
|
||||
$financialType = ($user['type'] == 3 ? 'order_commission' : 'first_order_commission') . '_refund';
|
||||
$finance[] = $financeDao->platformIn($commission, $financialType, $financeSn, $k);
|
||||
$result[] = $user;
|
||||
}
|
||||
$redPack = bcmul($order['pay_price'], 0.07, 2);
|
||||
|
@ -16,10 +16,14 @@ namespace app\common\dao\system\financial;
|
||||
|
||||
use app\common\dao\BaseDao;
|
||||
use app\common\model\system\financial\Financial;
|
||||
use app\common\repositories\system\merchant\FinancialRecordRepository;
|
||||
|
||||
class FinancialDao extends BaseDao
|
||||
{
|
||||
|
||||
public $order;
|
||||
public $user;
|
||||
|
||||
protected function getModel(): string
|
||||
{
|
||||
return Financial::class;
|
||||
@ -79,4 +83,80 @@ class FinancialDao extends BaseDao
|
||||
return $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* 平台出账财务流水
|
||||
* @param $number
|
||||
* @param $financialType
|
||||
* @param $financeSn
|
||||
* @param $financeIndex
|
||||
* @param $merId
|
||||
* @return array
|
||||
*/
|
||||
public function platformOut($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '')
|
||||
{
|
||||
return $this->setData($number, $financialType, 0, 2, $financeSn, $financeIndex, $merId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 平台入账财务流水
|
||||
* @param $number
|
||||
* @param $financialType
|
||||
* @param $financeSn
|
||||
* @param $financeIndex
|
||||
* @param $merId
|
||||
* @return array
|
||||
*/
|
||||
public function platformIn($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '')
|
||||
{
|
||||
return $this->setData($number, $financialType, 1, 2, $financeSn, $financeIndex, $merId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 公共入账财务流水
|
||||
* @param $number
|
||||
* @param $financialType
|
||||
* @param $financeSn
|
||||
* @param $financeIndex
|
||||
* @param $merId
|
||||
* @return array
|
||||
*/
|
||||
public function publicOut($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '')
|
||||
{
|
||||
return $this->setData($number, $financialType, 0, 1, $financeSn, $financeIndex, $merId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 公共入账财务流水
|
||||
* @param $number
|
||||
* @param $financialType
|
||||
* @param $financeSn
|
||||
* @param $financeIndex
|
||||
* @param $merId
|
||||
* @return array
|
||||
*/
|
||||
public function publicIn($number, $financialType, $financeSn = '', $financeIndex = '', $merId = '')
|
||||
{
|
||||
return $this->setData($number, $financialType, 1, 1, $financeSn, $financeIndex, $merId);
|
||||
}
|
||||
|
||||
public function setData($number, $financialType, $pm, $type = 2, $financeSn = '', $financeIndex = '', $merId = '')
|
||||
{
|
||||
if (empty($financeSn)) {
|
||||
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
|
||||
$financeSn = $financialRecordRepository->getSn();
|
||||
}
|
||||
return [
|
||||
'order_id' => $this->order->order_id,
|
||||
'order_sn' => $this->order->order_sn,
|
||||
'user_info' => $this->user['nickname'],
|
||||
'user_id' => $this->user['uid'],
|
||||
'financial_type' => $financialType,
|
||||
'financial_pm' => $pm,
|
||||
'type' => $type,
|
||||
'number' => $number,
|
||||
'mer_id' => !empty($merId) ? $merId : $this->order->mer_id,
|
||||
'financial_record_sn' => $financeSn . $financeIndex
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ use app\common\dao\store\consumption\StoreConsumptionUserDao;
|
||||
use app\common\dao\store\order\StoreCartDao;
|
||||
use app\common\dao\store\order\StoreOrderDao;
|
||||
use app\common\dao\store\StoreActivityDao;
|
||||
use app\common\dao\system\financial\FinancialDao;
|
||||
use app\common\model\store\order\StoreGroupOrder;
|
||||
use app\common\model\store\order\StoreOrder;
|
||||
use app\common\model\store\order\StoreOrderInterest;
|
||||
@ -222,11 +223,12 @@ class StoreOrderRepository extends BaseRepository
|
||||
$userMerchantRepository = app()->make(UserMerchantRepository::class);
|
||||
$storeOrderProfitsharingRepository = app()->make(StoreOrderProfitsharingRepository::class);
|
||||
$uid = $groupOrder->uid;
|
||||
$i = 1;
|
||||
$i = 0;
|
||||
// $isVipCoupon = app()->make(StoreGroupOrderRepository::class)->isVipCoupon($groupOrder);
|
||||
//订单记录
|
||||
$storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
|
||||
$svipDiscount = 0;
|
||||
$financeDao = new FinancialDao();
|
||||
foreach ($groupOrder->orderList as $_k => $order) {
|
||||
(new StoreActivityDao())->saveOrderProduct(2, $order);
|
||||
$order->paid = 1;
|
||||
@ -302,49 +304,19 @@ class StoreOrderRepository extends BaseRepository
|
||||
Queue::push(SendGoodsCodeJob::class, $order);
|
||||
}
|
||||
|
||||
$financeDao->order = $order;
|
||||
$financeDao->user = $groupOrder->user;
|
||||
$financialType = $presell ? 'order_presell' : 'order';
|
||||
// 商户流水账单数据
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $groupOrder->user->nickname,
|
||||
'user_id' => $uid,
|
||||
'financial_type' => $presell ? 'order_presell' : 'order',
|
||||
'financial_pm' => 1,
|
||||
'type' => $presell ? 2 : 1,
|
||||
'number' => $order->pay_price,
|
||||
'mer_id' => $order->mer_id,
|
||||
'financial_record_sn' => $financeSn . ($i++)
|
||||
];
|
||||
$finance[] = $financeDao->setData($order->pay_price, $financialType, 1, $presell ? 2 : 1, $financeSn, $i++);
|
||||
|
||||
if ($order->source == 103) {
|
||||
$_payPrice = $order->procure_price;
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $groupOrder->user->nickname,
|
||||
'user_id' => $uid,
|
||||
'financial_type' => 'supply_chain',
|
||||
'financial_pm' => 0,
|
||||
'type' => 1,
|
||||
'number' => $_payPrice,
|
||||
'mer_id' => $order->mer_id,
|
||||
'financial_record_sn' => $financeSn . ($i++)
|
||||
];
|
||||
$finance[] = $financeDao->publicOut($_payPrice, 'supply_chain', $financeSn, $i++);
|
||||
//市级供应链
|
||||
$product_mer_id = Db::name('store_order_product')->where('order_id', $order->order_id)->value('product_mer_id');
|
||||
if ($product_mer_id) {
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $groupOrder->user->nickname,
|
||||
'user_id' => $uid,
|
||||
'financial_type' => 'order',
|
||||
'financial_pm' => 0,
|
||||
'type' => 1,
|
||||
'number' => $_payPrice,
|
||||
'mer_id' => $product_mer_id,
|
||||
'financial_record_sn' => $financeSn . (($i++)+1)
|
||||
];
|
||||
$finance[] = $financeDao->publicOut($_payPrice, 'order', $financeSn, $i++, $product_mer_id);
|
||||
//市级供应链押金计算
|
||||
if ($_payPrice > 0) {
|
||||
/** @var MerchantRepository $merchantRepo */
|
||||
@ -354,18 +326,7 @@ class StoreOrderRepository extends BaseRepository
|
||||
[$_payCityPrice, $finance, $increase] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++);
|
||||
}
|
||||
if (isset($_payCityPrice)) {
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $groupOrder->user->nickname,
|
||||
'user_id' => $uid,
|
||||
'financial_type' => 'order_true',
|
||||
'financial_pm' => 0,
|
||||
'type' => 2,
|
||||
'number' => $_payCityPrice,
|
||||
'mer_id' => $product_mer_id,
|
||||
'financial_record_sn' => $financeSn . (($i++)+2)
|
||||
];
|
||||
$finance[] = $financeDao->platformOut($_payCityPrice, 'order_true', $financeSn, $i++, $product_mer_id);
|
||||
if (!$is_combine) {
|
||||
app()->make(MerchantRepository::class)->addLockMoney($product_mer_id, 'order', $order->order_id, $_payCityPrice);
|
||||
}
|
||||
@ -415,18 +376,7 @@ class StoreOrderRepository extends BaseRepository
|
||||
if (!$presell) {
|
||||
if ($order['commission_rate'] > 0 || $order->source == 103) {
|
||||
//支出手续费
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $groupOrder->user->nickname,
|
||||
'user_id' => $uid,
|
||||
'financial_type' => 'order_charge',
|
||||
'financial_pm' => 0,
|
||||
'type' => 1,
|
||||
'number' => $_order_rate,
|
||||
'mer_id' => $order->mer_id,
|
||||
'financial_record_sn' => $financeSn . ($i++)
|
||||
];
|
||||
$finance[] = $financeDao->publicOut($_order_rate, 'order_charge', $financeSn, $i++);
|
||||
}
|
||||
//押金计算
|
||||
if ($_payPrice > 0) {
|
||||
@ -437,31 +387,9 @@ class StoreOrderRepository extends BaseRepository
|
||||
[$_payPrice, $finance, $increase] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++);
|
||||
}
|
||||
if ($order->source == 103) {
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $groupOrder->user->nickname,
|
||||
'user_id' => $uid,
|
||||
'financial_type' => 'commission_to_cloud_warehouse',
|
||||
'financial_pm' => 0,
|
||||
'type' => 1,
|
||||
'number' => $_payPrice,
|
||||
'mer_id' => $order->mer_id,
|
||||
'financial_record_sn' => $financeSn . ($i++)
|
||||
];
|
||||
$finance[] = $financeDao->publicOut($_payPrice, 'commission_to_cloud_warehouse', $financeSn, $i++);
|
||||
} else {
|
||||
$finance[] = [
|
||||
'order_id' => $order->order_id,
|
||||
'order_sn' => $order->order_sn,
|
||||
'user_info' => $groupOrder->user->nickname,
|
||||
'user_id' => $uid,
|
||||
'financial_type' => 'order_true',
|
||||
'financial_pm' => 0,
|
||||
'type' => 2,
|
||||
'number' => $_payPrice,
|
||||
'mer_id' => $order->mer_id,
|
||||
'financial_record_sn' => $financeSn . ($i++)
|
||||
];
|
||||
$finance[] = $financeDao->platformOut($_payPrice, 'order_true', $financeSn, $i++);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user