添加财务流水、支付回调、资金流水
This commit is contained in:
parent
7e4e845e44
commit
34cad62c8e
@ -6,6 +6,7 @@ use app\common\enum\OrderEnum;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\enum\YesNoEnum;
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\logic\CapitalFlowLogic;
|
||||
use app\common\model\merchant\Merchant;
|
||||
use app\common\model\order\Cart;
|
||||
use app\common\model\store_branch_product\StoreBranchProduct;
|
||||
@ -291,6 +292,8 @@ class OrderLogic extends BaseLogic
|
||||
try {
|
||||
$user->user_money = bcsub($user->user_money, $order['actual'], 2);
|
||||
$user->save();
|
||||
$capitalFlowDao = new CapitalFlowLogic($user);
|
||||
$capitalFlowDao->userExpense('user_order_pay', 'order', $order['id'], $order['actual']);
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
|
154
app/common/logic/CapitalFlowLogic.php
Normal file
154
app/common/logic/CapitalFlowLogic.php
Normal file
@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\model\finance\CapitalFlow;
|
||||
|
||||
class CapitalFlowLogic extends BaseLogic
|
||||
{
|
||||
|
||||
public $user;
|
||||
|
||||
public function __construct($user)
|
||||
{
|
||||
$this->user = $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户收入
|
||||
* @param $category
|
||||
* @param $linkType
|
||||
* @param $linkId
|
||||
* @param $amount
|
||||
* @param $mark
|
||||
* @return mixed
|
||||
*/
|
||||
public function userIncome($category, $linkType, $linkId, $amount, $mark = '')
|
||||
{
|
||||
$model = new CapitalFlow();
|
||||
$model->uid = $this->user['uid'];
|
||||
$model->category = $category;
|
||||
$model->link_type = $linkType;
|
||||
$model->link_id = $linkId;
|
||||
$model->amount = $amount;
|
||||
$model->before_balance = $this->user['now_money'];
|
||||
$model->balance = bcadd($this->user['now_money'], $amount, 2);
|
||||
$model->create_time = date('Y-m-d H:i:s');
|
||||
$model->type = 'in';
|
||||
$model->title = $this->getTitle($category, $amount);
|
||||
$model->mark = empty($mark) ? $model->title : $mark;
|
||||
$model->save();
|
||||
return $model->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户支出
|
||||
* @param $category
|
||||
* @param $linkType
|
||||
* @param $linkId
|
||||
* @param $amount
|
||||
* @param $mark
|
||||
* @return mixed
|
||||
*/
|
||||
public function userExpense($category, $linkType, $linkId, $amount, $mark = '')
|
||||
{
|
||||
$model = new CapitalFlow();
|
||||
$model->uid = $this->user['uid'];
|
||||
$model->category = $category;
|
||||
$model->link_type = $linkType;
|
||||
$model->link_id = $linkId;
|
||||
$model->amount = $amount;
|
||||
$model->before_balance = $this->user['now_money'];
|
||||
$model->balance = bcsub($this->user['now_money'], $amount, 2);
|
||||
$model->create_time = date('Y-m-d H:i:s');
|
||||
$model->type = 'out';
|
||||
$model->title = $this->getTitle($category, $amount);
|
||||
$model->mark = empty($mark) ? $model->title : $mark;
|
||||
$model->save();
|
||||
return $model->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 商户收入
|
||||
* @param $category
|
||||
* @param $linkType
|
||||
* @param $linkId
|
||||
* @param $amount
|
||||
* @param $mark
|
||||
* @return mixed
|
||||
*/
|
||||
public function merchantIncome($category, $linkType, $linkId, $amount, $mark = '')
|
||||
{
|
||||
$model = new CapitalFlow();
|
||||
$model->mer_id = $this->user['mer_id'];
|
||||
$model->category = $category;
|
||||
$model->link_type = $linkType;
|
||||
$model->link_id = $linkId;
|
||||
$model->amount = $amount;
|
||||
$model->before_balance = $this->user['mer_money'];
|
||||
$model->balance = bcadd($this->user['mer_money'], $amount, 2);
|
||||
$model->create_time = date('Y-m-d H:i:s');
|
||||
$model->type = 'in';
|
||||
$model->title = $this->getTitle($category, $amount);
|
||||
$model->mark = empty($mark) ? $model->title : $mark;
|
||||
$model->save();
|
||||
return $model->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 商户支出
|
||||
* @param $category
|
||||
* @param $linkType
|
||||
* @param $linkId
|
||||
* @param $amount
|
||||
* @param $mark
|
||||
* @return mixed
|
||||
*/
|
||||
public function merchantExpense($category, $linkType, $linkId, $amount, $mark = '')
|
||||
{
|
||||
$model = new CapitalFlow();
|
||||
$model->mer_id = $this->user['mer_id'];
|
||||
$model->uid = $this->user['uid'];
|
||||
$model->category = $category;
|
||||
$model->link_type = $linkType;
|
||||
$model->link_id = $linkId;
|
||||
$model->amount = $amount;
|
||||
$model->before_balance = $this->user['mer_money'];
|
||||
$model->balance = bcsub($this->user['mer_money'], $amount, 2);
|
||||
$model->create_time = date('Y-m-d H:i:s');
|
||||
$model->type = 'out';
|
||||
$model->title = $this->getTitle($category, $amount);
|
||||
$model->mark = empty($mark) ? $model->title : $mark;
|
||||
$model->save();
|
||||
return $model->id;
|
||||
}
|
||||
|
||||
public function getTitle($category, $amount)
|
||||
{
|
||||
switch ($category) {
|
||||
case 'user_balance_recharge':
|
||||
return "用户充值{$amount}元";
|
||||
case 'merchant_margin':
|
||||
return "店铺自动扣除保证金{$amount}元";
|
||||
case 'merchant_order_income':
|
||||
return "店铺订单收入{$amount}元";
|
||||
case 'user_order_refund':
|
||||
return "用户订单退款{$amount}元";
|
||||
case 'merchant_order_refund':
|
||||
return "店铺订单退款{$amount}元";
|
||||
case 'merchant_margin_refund':
|
||||
return "店铺退还保证金{$amount}元";
|
||||
case 'user_order_promotion':
|
||||
return "订单推广佣金{$amount}元";
|
||||
case 'user_order_promotion_refund':
|
||||
return "退还订单推广佣金{$amount}元";
|
||||
case 'system_balance_add':
|
||||
return "系统增加余额{$amount}元";
|
||||
case 'system_balance_reduce':
|
||||
return "系统减少余额{$amount}元";
|
||||
default:
|
||||
return "订单支付{$amount}元";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
42
app/common/logic/PayNotifyLogLogic.php
Normal file
42
app/common/logic/PayNotifyLogLogic.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\model\finance\PayNotifyLog;
|
||||
use support\Log;
|
||||
|
||||
class PayNotifyLogLogic extends BaseLogic
|
||||
{
|
||||
|
||||
public function insert($payType, $data, $type = PayNotifyLog::TYPE_ORDER)
|
||||
{
|
||||
try {
|
||||
$exist = PayNotifyLog::where('order_sn', $data['out_trade_no'])->where('pay_type', $payType)->where('type', $type)->count();
|
||||
if ($exist) {
|
||||
return false;
|
||||
}
|
||||
$model = new PayNotifyLog();
|
||||
$model->pay_type = $payType;
|
||||
$model->order_sn = $data['out_trade_no'];
|
||||
$model->type = $type;
|
||||
$model->amount = $payType == 'wechat_common' ? $data['amount']['payer_total'] : $data['buyer_pay_amount'];
|
||||
$model->out_trade_no = $payType == 'wechat_common' ? $data['transaction_id'] : $data['trade_no'];
|
||||
$model->transaction_id = $data['transaction_id'];
|
||||
$model->attach = $data['attach'] ?? '';
|
||||
if (isset($data['pay_time'])) {
|
||||
$model->create_time = $data['pay_time'];
|
||||
}
|
||||
if (isset($data['success_time'])) {
|
||||
$model->create_time = $data['success_time'];
|
||||
}
|
||||
$model->create_time = $model->create_time ?: date('Y-m-d H:i:s');
|
||||
$model->save();
|
||||
return $model->id;
|
||||
} catch (\Exception $e) {
|
||||
$message = 'pay notify log insert error: ' . $e->getMessage();
|
||||
Log::error($message . ', trace: ' . $e->getTraceAsString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -23,6 +23,10 @@ class PayNotifyLogic extends BaseLogic
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
if ($action != 'cash_pay') {
|
||||
$payNotifyLogLogic = new PayNotifyLogLogic();
|
||||
$payNotifyLogLogic->insert($action, $extra);
|
||||
}
|
||||
self::$action($orderSn, $extra);
|
||||
Db::commit();
|
||||
return true;
|
||||
@ -54,53 +58,25 @@ class PayNotifyLogic extends BaseLogic
|
||||
if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
$financial_type = OrderEnum::USER_ORDER_PAY;
|
||||
$financial_type2 = OrderEnum::MERCHANT_ORDER_OBTAINS;
|
||||
if ($order->pay_type != 10) {
|
||||
$order->pay_price = bcdiv($extra['amount']['payer_total'], 100, 2);
|
||||
$order->paid = 1;
|
||||
$order->status = 1;
|
||||
$order->save();
|
||||
} else {
|
||||
$financial_type2 = OrderEnum::CASHIER_CASH_ORDER_PAY;
|
||||
$extra['transaction_id'] = time();
|
||||
}
|
||||
if ($order->pay_type == 9) {
|
||||
$order->status = 2;
|
||||
$financial_type2 = OrderEnum::CASHIER_ORDER_PAY;
|
||||
}
|
||||
$financeLogic = new StoreFinanceFlowLogic();
|
||||
$financeLogic->order = $order;
|
||||
$financeLogic->user = ['uid' => $order['uid']];
|
||||
if ($order->pay_type != 9 || $order->pay_type != 10) {
|
||||
//用户支出流水
|
||||
$record[] = [
|
||||
'financial_record_sn' => $extra['transaction_id'],
|
||||
'order_id' => $order['id'],
|
||||
'order_sn' => $order['order_id'],
|
||||
'user_id' => $order['uid'],
|
||||
'financial_type' => $financial_type,
|
||||
'financial_pm' => OrderEnum::EXPENDITURE,
|
||||
'number' => $order['pay_price'],
|
||||
'status' => 1,
|
||||
'type' => OrderEnum::USER,
|
||||
'store_id' => $order['store_id'],
|
||||
'staff_id' => $order['staff_id'],
|
||||
];
|
||||
$financeLogic->in($order['pay_price'], OrderEnum::USER_ORDER_PAY);
|
||||
}
|
||||
//商户获得流水
|
||||
$record[] = [
|
||||
'financial_record_sn' => $extra['transaction_id'],
|
||||
'order_id' => $order['id'],
|
||||
'order_sn' => $order['order_id'],
|
||||
'user_id' => $order['uid'],
|
||||
'financial_type' => $financial_type2,
|
||||
'financial_pm' => OrderEnum::INCOME,
|
||||
'number' => $order['pay_price'],
|
||||
'status' => 0,
|
||||
'type' => OrderEnum::MERCHANT,
|
||||
'store_id' => $order['store_id'],
|
||||
'staff_id' => $order['staff_id'],
|
||||
];
|
||||
(new StoreFinanceFlow())->saveAll($record);
|
||||
|
||||
$financeLogic->out($order['pay_price'], OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0);
|
||||
$financeLogic->save();
|
||||
|
||||
if ($order->pay_type == 9) {
|
||||
$extra['create_time'] = $order['create_time'];
|
||||
@ -115,7 +91,7 @@ class PayNotifyLogic extends BaseLogic
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 现金支付
|
||||
*/
|
||||
@ -160,50 +136,25 @@ class PayNotifyLogic extends BaseLogic
|
||||
if ($order->isEmpty() || $order->paid == PayEnum::ISPAID) {
|
||||
return true;
|
||||
}
|
||||
$financial_type = OrderEnum::USER_ORDER_PAY;
|
||||
$financial_type2 = OrderEnum::MERCHANT_ORDER_OBTAINS;
|
||||
if ($order->pay_type != 10) {
|
||||
$order->money = $extra['buyer_pay_amount'];
|
||||
$order->paid = 1;
|
||||
$order->status = 1;
|
||||
$order->save();
|
||||
} else {
|
||||
$financial_type2 = OrderEnum::CASHIER_CASH_ORDER_PAY;
|
||||
$extra['transaction_id'] = time();
|
||||
}
|
||||
if ($order->pay_type == 9) {
|
||||
$order->status = 2;
|
||||
$financial_type2 = OrderEnum::CASHIER_ORDER_PAY;
|
||||
}
|
||||
$financeLogic = new StoreFinanceFlowLogic();
|
||||
$financeLogic->order = $order;
|
||||
$financeLogic->user = ['uid' => $order['uid']];
|
||||
if ($order->pay_type != 9 || $order->pay_type != 10) {
|
||||
//用户支出流水
|
||||
$record[] = [
|
||||
'financial_record_sn' => $extra['trade_no'],
|
||||
'order_id' => $order['id'],
|
||||
'order_sn' => $order['order_id'],
|
||||
'user_id' => $order['uid'],
|
||||
'financial_type' => $financial_type,
|
||||
'financial_pm' => OrderEnum::EXPENDITURE,
|
||||
'number' => $order['actual'],
|
||||
'status' => 1,
|
||||
'type' => OrderEnum::USER,
|
||||
'mer_id' => $order['merchant'],
|
||||
];
|
||||
$financeLogic->in($order['pay_price'], OrderEnum::USER_ORDER_PAY);
|
||||
}
|
||||
//商户获得流水
|
||||
$record[] = [
|
||||
'financial_record_sn' => $extra['trade_no'],
|
||||
'order_id' => $order['id'],
|
||||
'order_sn' => $order['order_id'],
|
||||
'user_id' => $order['uid'],
|
||||
'financial_type' => $financial_type2,
|
||||
'financial_pm' => OrderEnum::INCOME,
|
||||
'number' => $order['actual'],
|
||||
'status' => 0,
|
||||
'type' => OrderEnum::MERCHANT,
|
||||
'mer_id' => $order['merchant'],
|
||||
];
|
||||
(new StoreFinanceFlow())->saveAll($record);
|
||||
$financeLogic->out($order['pay_price'], OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], $order['staff_id'], 0);
|
||||
$financeLogic->save();
|
||||
|
||||
|
||||
if ($order->pay_type == 9) {
|
||||
|
75
app/common/logic/StoreFinanceFlowLogic.php
Normal file
75
app/common/logic/StoreFinanceFlowLogic.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\model\store_finance_flow\StoreFinanceFlow;
|
||||
|
||||
class StoreFinanceFlowLogic extends BaseLogic
|
||||
{
|
||||
|
||||
public $order;
|
||||
public $user;
|
||||
public $index = 0;
|
||||
public $financeSn;
|
||||
public $list = [];
|
||||
|
||||
/**
|
||||
* 支出财务流水
|
||||
* @param $number
|
||||
* @param $financialType
|
||||
* @param $storeId
|
||||
* @param $staffId
|
||||
* @param $status
|
||||
*/
|
||||
public function out($number, $financialType, $storeId = 0, $staffId = 0, $status = 1)
|
||||
{
|
||||
$this->setData($number, $financialType, 0, $storeId, $staffId, $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收入财务流水
|
||||
* @param $number
|
||||
* @param $financialType
|
||||
* @param $storeId
|
||||
* @param $staffId
|
||||
* @param $status
|
||||
*/
|
||||
public function in($number, $financialType, $storeId = 0, $staffId = 0, $status = 1)
|
||||
{
|
||||
$this->setData($number, $financialType, 1, $storeId, $staffId, $status);
|
||||
}
|
||||
|
||||
public function setData($number, $financialType, $pm, $storeId, $staffId, $status)
|
||||
{
|
||||
if (empty($this->financeSn)) {
|
||||
$this->financeSn = $this->getSn();
|
||||
}
|
||||
$this->list[] = [
|
||||
'order_id' => $this->order['id'],
|
||||
'order_sn' => $this->order['order_id'],
|
||||
'user_id' => $this->user['uid'],
|
||||
'financial_type' => $financialType,
|
||||
'financial_pm' => $pm,
|
||||
'number' => $number,
|
||||
'status' => $status,
|
||||
'store_id' => $storeId !== '' ? $storeId : $this->order['store_id'],
|
||||
'staff_id' => $staffId !== '' ? $staffId : $this->order['staff_id'],
|
||||
'financial_record_sn' => $this->financeSn . ($this->index++)
|
||||
];
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
if (count($this->list) > 0) {
|
||||
(new StoreFinanceFlow())->insertAll($this->list);
|
||||
}
|
||||
}
|
||||
|
||||
public function getSn()
|
||||
{
|
||||
list($msec, $sec) = explode(' ', microtime());
|
||||
$msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
|
||||
return 'fn' . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
|
||||
}
|
||||
|
||||
}
|
10
app/common/model/finance/CapitalFlow.php
Normal file
10
app/common/model/finance/CapitalFlow.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\finance;
|
||||
|
||||
use app\common\model\BaseModel;
|
||||
|
||||
class CapitalFlow extends BaseModel
|
||||
{
|
||||
|
||||
}
|
13
app/common/model/finance/PayNotifyLog.php
Normal file
13
app/common/model/finance/PayNotifyLog.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\finance;
|
||||
|
||||
use app\common\model\BaseModel;
|
||||
|
||||
class PayNotifyLog extends BaseModel
|
||||
{
|
||||
|
||||
const TYPE_ORDER = 1;
|
||||
const TYPE_REFUND = 2;
|
||||
|
||||
}
|
@ -3,8 +3,8 @@
|
||||
namespace app\store\controller\store_order;
|
||||
|
||||
|
||||
use app\admin\lists\store_order\StoreOrderLists;
|
||||
use app\store\controller\BaseAdminController;
|
||||
use app\store\lists\store_order\StoreOrderLists;
|
||||
use app\store\logic\store_order\StoreOrderLogic;
|
||||
use app\store\validate\store_order\StoreOrderValidate;
|
||||
use hg\apidoc\annotation as ApiDoc;
|
||||
|
Loading…
x
Reference in New Issue
Block a user