Browse Source

分账

master
lemon 4 years ago
parent
commit
6ea6a34ed6
  1. 163
      app/Http/Controllers/Api/VerificationController.php
  2. 2
      app/Models/Order.php
  3. 5
      app/Models/Statement.php
  4. 5
      app/Models/Withdrawal.php
  5. 30
      app/Service/WithdrawalService.php
  6. 18
      app/Traits/StatementTraits.php
  7. 32
      database/migrations/2021_09_16_151024_update_statement_table.php

163
app/Http/Controllers/Api/VerificationController.php

@ -14,8 +14,10 @@ use App\Models\OrderProductItem;
use App\Models\User;
use App\Common\OrderStatus;
use App\Service\SmsService;
use App\Service\WithdrawalService;
use App\Traits\DemandTraits;
use App\Traits\SmsTraits;
use App\Traits\StatementTraits;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
@ -40,12 +42,12 @@ class VerificationController extends Controller
return $this->error('订单不存在或订单状态不允许核销');
}
//$mobile = User::where('id', $this->user_id)->value('mobile');
//
//$checkMobile = Product::query()->whereIn('id', explode(',', $order->product_ids))->where('verify_mobile', $mobile)->doesntExist();
//if ($checkMobile) {
// return $this->error('对不起,你没有核销权限,请联系管理员');
//}
$mobile = User::where('id', $this->user_id)->value('mobile');
$checkMobile = Product::query()->whereIn('id', explode(',', $order->product_ids))->where('verify_mobile', $mobile)->doesntExist();
if ($checkMobile) {
return $this->error('对不起,你没有核销权限,请联系管理员');
}
$order->status = OrderStatus::SUCCESS;
if ($order->save()) {
@ -72,6 +74,7 @@ class VerificationController extends Controller
public function fund($order)
{
$service = new WithdrawalService();
DB::beginTransaction();
try {
//最后批量插入
@ -82,13 +85,14 @@ class VerificationController extends Controller
$guidePrice = $order->guide_price;
$cost = bcadd($cost, $order->guide_price, 6);
//成本价 加上地接价格
$statementCreate[] = [
'price' => $order->guide_price,
'type' => StatementType::ORDER,
'user_id' => $order->guide->id,
'user_type' => DemandTraits::$col[2],
'order_id' => $order->id
];
$statementCreate[] = $service->createByOrder(
$order->guide_price,
StatementType::ORDER,
$order->guide->id,
DemandTraits::$col[2],
$order->id,
StatementTraits::$type[0]
);
//抽成
if ($order->guide->rate > 0) {
//计算抽成金额
@ -96,21 +100,22 @@ class VerificationController extends Controller
$cutPrice = $guideCut > 0 ? bcdiv($guideCut, 100, 6) : 0;
//总后台抽成流水
if ($cutPrice > 0) {
$adminCreate[] = [
'price' => $cutPrice,
'type' => StatementType::CUT,
'cut_user_id' => $order->guide->id,
'cut_user_type' => DemandTraits::$col[2],
'order_id' => $order->id
];
$adminCreate[] = $service->createByOrderFormAdmin(
$cutPrice,
StatementType::CUT,
$order->guide->id,
DemandTraits::$col[2],
$order->id,
);
//地接被抽成流水
$statementCreate[] = [
'price' => bcmul($cutPrice, -1, 2),
'type' => StatementType::CUT,
'user_id' => $order->guide->id,
'user_type' => DemandTraits::$col[2],
'order_id' => $order->id
];
$statementCreate[] = $service->createByOrder(
bcmul($cutPrice, -1, 2),
StatementType::CUT,
$order->guide->id,
DemandTraits::$col[2],
$order->id,
StatementTraits::$type[0]
);
$guidePrice = bcsub($order->guide_price, $cutPrice, 6);
$guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
$guide->balance = bcadd($guide->balance, $guidePrice, 6);
@ -131,13 +136,14 @@ class VerificationController extends Controller
$cost = bcadd($cost, $v->sum_price, 6);
$supplierPrice = $v->sum_price;
$statementCreate[] = [
'price' => $v->sum_price,
'type' => StatementType::ORDER,
'user_id' => $v->supplier_id,
'user_type' => DemandTraits::$col[1],
'order_id' => $order->id
];
$statementCreate[] = $service->createByOrder(
$v->sum_price,
StatementType::ORDER,
$v->supplier_id,
DemandTraits::$col[1],
$order->id,
StatementTraits::$type[0]
);
if ($v->supplier->rate > 0) {
//计算抽成金额
@ -145,21 +151,22 @@ class VerificationController extends Controller
$cutPrice = $supplierCut > 0 ? bcdiv($supplierCut, 100, 6) : 0;
if ($cutPrice > 0) {
//总后台抽成流水
$adminCreate[] = [
'price' => $cutPrice,
'type' => StatementType::CUT,
'cut_user_id' => $v->supplier_id,
'cut_user_type' => DemandTraits::$col[1],
'order_id' => $order->id
];
$adminCreate[] = $service->createByOrderFormAdmin(
$cutPrice,
StatementType::CUT,
$v->supplier_id,
DemandTraits::$col[1],
$order->id,
);
//供应商被抽成流水
$statementCreate[] = [
'price' => bcmul($cutPrice, -1, 6),
'type' => StatementType::CUT,
'user_id' => $v->supplier_id,
'user_type' => DemandTraits::$col[1],
'order_id' => $order->id
];
$statementCreate[] = $service->createByOrder(
bcmul($cutPrice, -1, 6),
StatementType::CUT,
$v->supplier_id,
DemandTraits::$col[1],
$order->id,
StatementTraits::$type[0]
);
$supplierPrice = bcsub($supplierPrice, $cutPrice, 6);
}
@ -174,13 +181,14 @@ class VerificationController extends Controller
$agentPrice = bcsub($order->price, $cost, 2);
if ($agentPrice > 0) {
$statementCreate[] = [
'price' => $agentPrice,
'type' => StatementType::ORDER,
'user_id' => $order->agent_id,
'user_type' => DemandTraits::$col[0],
'order_id' => $order->id
];
$statementCreate[] = $service->createByOrder(
$agentPrice,
StatementType::ORDER,
$order->agent_id,
DemandTraits::$col[0],
$order->id,
StatementTraits::$type[0]
);
//抽成
if ($order->agent->rate > 0) {
@ -190,40 +198,41 @@ class VerificationController extends Controller
//总后台抽成流水
if ($cutPrice > 0) {
$adminCreate[] = [
'price' => $cutPrice,
'type' => StatementType::CUT,
'cut_user_id' => $order->agent->id,
'cut_user_type' => DemandTraits::$col[0],
'order_id' => $order->id
];
$adminCreate[] = $service->createByOrderFormAdmin(
$cutPrice,
StatementType::CUT,
$order->agent->id,
DemandTraits::$col[0],
$order->id,
);
//代理商被抽成流水
$statementCreate[] = [
'price' => bcmul($cutPrice, -1, 6),
'type' => StatementType::CUT,
'user_id' => $order->agent->id,
'user_type' => DemandTraits::$col[0],
'order_id' => $order->id
];
$statementCreate[] = $service->createByOrder(
bcmul($cutPrice, -1, 6),
StatementType::CUT,
$order->agent->id,
DemandTraits::$col[0],
$order->id,
StatementTraits::$type[0]
);
$agentPrice = bcsub($agentPrice, $cutPrice, 6);
}
}
//扣除微信支付手续费
$chargePrice = bcmul($order->price, 0.006, 6);
$statementCreate[] = [
'price' => bcmul($chargePrice, -1, 6),
'type' => StatementType::CHARGE,
'user_id' => $order->agent_id,
'user_type' => DemandTraits::$col[0],
'order_id' => $order->id
];
$statementCreate[] = $service->createByOrder(
bcmul($chargePrice, -1, 6),
StatementType::CHARGE,
$order->agent_id,
DemandTraits::$col[0],
$order->id,
StatementTraits::$type[0]
);
$agentPrice = bcsub($agentPrice, $chargePrice, 6);
$agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
$agent->balance = bcadd($agent->balance, $agentPrice, 6);
$agent->save();
}
//dd($adminCreate,$guideCreate);
if (!empty($adminCreate)) {
$order->statementAdmin()->createMany($adminCreate);
}

2
app/Models/Order.php

@ -131,6 +131,6 @@ class Order extends BaseModel
public function statement()
{
return $this->hasMany(Statement::class,'order_id','id');
return $this->morphMany(Statement::class,'access');
}
}

5
app/Models/Statement.php

@ -17,4 +17,9 @@ class Statement extends Model
{
return $this->morphTo();
}
public function access()
{
return $this->morphTo();
}
}

5
app/Models/Withdrawal.php

@ -22,4 +22,9 @@ class Withdrawal extends Model
{
return $this->morphTo();
}
public function statement()
{
return $this->morphMany(Statement::class,'access');
}
}

30
app/Service/WithdrawalService.php

@ -10,14 +10,40 @@ use App\Traits\DemandTraits;
class WithdrawalService
{
public function create($price,$type,$userId,$userType,$orderId = '')
//提现流水
public function create($price,$type,$userId,$userType,$accessId,$accessType)
{
Statement::query()->create([
'price' => $price,
'type' => $type,
'user_id' => $userId,
'user_type' => $userType,
'order_id' => $orderId ?? ''
'access_id' => $accessId,
'access_type' => $accessType,
]);
}
//订单流水 暂时放一起
public function createByOrder($price,$type,$userId,$userType,$accessId,$accessType)
{
return [
'price' => $price,
'type' => $type,
'user_id' => $userId,
'user_type' => $userType,
'access_id' => $accessId,
'access_type' => $accessType,
];
}
public function createByOrderFormAdmin($price,$type,$userId,$userType,$orderId)
{
return [
'price' => $price,
'type' => $type,
'cut_user_id' => $userId,
'cut_user_type' => $userType,
'order_id' => $orderId,
];
}
}

18
app/Traits/StatementTraits.php

@ -0,0 +1,18 @@
<?php
namespace App\Traits;
trait StatementTraits
{
public static $type = [
'App\Models\Order',
'App\Models\Withdrawal',
];
public static $TypeText = [
'App\Models\Order' => '订单',
'App\Models\Withdrawal' => '提现',
];
}

32
database/migrations/2021_09_16_151024_update_statement_table.php

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateStatementTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('statement', function (Blueprint $table) {
$table->string('access_type')->index()->default('')->comment('关联类型 目前有订单 和提现')->after('user_type');
$table->integer('access_id')->index()->default(0)->comment('关联')->after('user_type');
$table->dropColumn('order_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Loading…
Cancel
Save