diff --git a/app/api/logic/order/OrderLogic.php b/app/api/logic/order/OrderLogic.php index a52eafcd..6d004e1b 100644 --- a/app/api/logic/order/OrderLogic.php +++ b/app/api/logic/order/OrderLogic.php @@ -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) { diff --git a/app/common/logic/CapitalFlowLogic.php b/app/common/logic/CapitalFlowLogic.php new file mode 100644 index 00000000..cbe68c08 --- /dev/null +++ b/app/common/logic/CapitalFlowLogic.php @@ -0,0 +1,154 @@ +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}元"; + } + } + +} diff --git a/app/common/logic/PayNotifyLogLogic.php b/app/common/logic/PayNotifyLogLogic.php new file mode 100644 index 00000000..99c2aa79 --- /dev/null +++ b/app/common/logic/PayNotifyLogLogic.php @@ -0,0 +1,42 @@ +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; + } + +} diff --git a/app/common/logic/PayNotifyLogic.php b/app/common/logic/PayNotifyLogic.php index 66da6756..e73a84df 100644 --- a/app/common/logic/PayNotifyLogic.php +++ b/app/common/logic/PayNotifyLogic.php @@ -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) { diff --git a/app/common/logic/StoreFinanceFlowLogic.php b/app/common/logic/StoreFinanceFlowLogic.php new file mode 100644 index 00000000..d1682a30 --- /dev/null +++ b/app/common/logic/StoreFinanceFlowLogic.php @@ -0,0 +1,75 @@ +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)); + } + +} diff --git a/app/common/model/finance/CapitalFlow.php b/app/common/model/finance/CapitalFlow.php new file mode 100644 index 00000000..08d68441 --- /dev/null +++ b/app/common/model/finance/CapitalFlow.php @@ -0,0 +1,10 @@ +