multi-store/app/common/logic/CapitalFlowLogic.php

170 lines
5.4 KiB
PHP

<?php
namespace app\common\logic;
use app\common\enum\OrderEnum;
use app\common\model\finance\CapitalFlow;
class CapitalFlowLogic extends BaseLogic
{
public $user;
public $store;
public function __construct($obj, $type = 'user')
{
if (isset($type) && $type == 'user') {
$this->user = $obj;
} else {
$this->store = $obj;
}
}
/**
* 用户收入
* @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['id'];
$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 $store_id
* @param $linkType
* @param $linkId
* @param $amount
* @param $mark
* @return mixed
*/
public function userExpense($category, $linkType, $linkId, $amount, $mark = '',$payType=0,$store_id=0)
{
$model = new CapitalFlow();
$model->uid = $this->user['id'];
$model->category = $category;
$model->store_id = $store_id;
$model->link_type = $linkType;
$model->link_id = $linkId;
$model->amount = $amount;
if($payType == OrderEnum::BALANCE_PAYMEN_NO){
$model->before_balance = $this->user['now_money'];
$model->balance = $this->user['now_money'];
}else{
$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 storeIncome($category, $linkType, $linkId, $amount, $mark = '')
{
$model = new CapitalFlow();
$model->store_id = $this->store['id'];
$model->category = $category;
$model->link_type = $linkType;
$model->link_id = $linkId;
$model->amount = $amount;
$model->before_balance = $this->store['balance'];
$model->balance = bcadd($this->store['balance'], $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();
$this->store->update(['balance' => $model->balance], ['id' => $this->store['id']]);
return $model->id;
}
/**
* 商户支出
* @param $category
* @param $linkType
* @param $linkId
* @param $amount
* @param $mark
* @return mixed
*/
public function storeExpense($category, $linkType, $linkId, $amount, $mark = '')
{
$model = new CapitalFlow();
$model->store_id = $this->store['store_id'];
$model->uid = 0;
$model->category = $category;
$model->link_type = $linkType;
$model->link_id = $linkId;
$model->amount = $amount;
$model->before_balance = $this->store['balance'];
$model->balance = bcsub($this->store['balance'], $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();
$this->store->update(['balance' => $model->balance], ['id' => $this->store['id']]);
return $model->id;
}
public function getTitle($category, $amount)
{
switch ($category) {
case 'user_balance_recharge':
return "用户充值{$amount}";
case 'store_margin':
return "店铺自动扣除保证金{$amount}";
case 'store_order_income':
return "店铺订单收入{$amount}";
case 'user_order_refund':
return "用户订单退款{$amount}";
case 'store_order_refund':
return "店铺订单退款{$amount}";
case 'store_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}";
}
}
}