6
0
Fork 0

添加撤销线下去库存操作

release
vine_liutk 2022-02-14 11:50:41 +08:00
parent 98c9399e21
commit aa78727b0b
7 changed files with 103 additions and 2 deletions

View File

@ -31,6 +31,7 @@ class UserController extends Controller
'dealer'=> $dealer,
'dealer_wallet' => $user->dealerWallet?->balance,
'user_info' => UserInfoResource::make($user->userInfo),
'has_password' => (bool) $user->wallet?->password,
]);
}

View File

@ -73,7 +73,7 @@ class UserProductController extends Controller
'product_id'=> $input['product_id'],
'type' => DealerUserProductLog::TYPE_OFFLINE_OUT,
'qty'=>$input['num'],
'remark'=>$input['remark']??null,
'remark'=>$input['remark'] ?? null,
]);
DB::commit();
} catch (Throwable $th) {
@ -83,4 +83,53 @@ class UserProductController extends Controller
}
return response()->noContent();
}
/**
* 撤销线下去库存
*
*/
public function revokeQtyLog(DealerUserProductLog $log, Request $request)
{
//判断是否是自己的日志
if ($log->user_id != $request->user()->id) {
throw new BizException('日志未找到');
}
//是否可以撤销
if (!$log->canRevoke()) {
throw new BizException('该日志无法撤销');
}
$product = $request->user()->dealerProducts()
->where('product_id', $log->product_id)
->first();
if (!$product) {
throw new BizException('您还没有该商品');
}
try {
DB::beginTransaction();
$product->increment('stock', $log->qty);
$revokeLog = DealerUserProductLog::create([
'user_id'=> $request->user()->id,
'product_id'=> $log->product_id,
'type' => DealerUserProductLog::TYPE_REVOKE_IN,
'qty' => $log->qty,
'remark'=> '撤销回滚',
]);
$log->update([
'revoke_id' => $revokeLog->id,
]);
DB::commit();
} catch (Throwable $th) {
DB::rollBack();
report($th);
throw new BizException('系统繁忙,请稍后再试');
}
return response()->noContent();
}
}

View File

@ -16,10 +16,17 @@ class UserProductLogResource extends JsonResource
public function toArray($request)
{
return [
'id' => $this->id,
'product_name'=> $this->product?->name,
'type'=> $this->type,
'remark' => $this->remark,
'qty' => ($this->type == DealerUserProductLog::TYPE_ORDER_IN ? '+' : '-').$this->qty.$this->product?->unit,
'qty' => (in_array($this->type, [
DealerUserProductLog::TYPE_ORDER_IN,
DealerUserProductLog::TYPE_ADMIN_IN,
DealerUserProductLog::TYPE_REVOKE_IN,
]) ? '+' : '-').$this->qty.$this->product?->unit,
'created_at' => $this->created_at->toDateTimeString(),
'can_revoke' => $this->canRevoke(),
];
}
}

View File

@ -236,6 +236,7 @@ Route::group([
Route::get('user-products/{product}', [Dealer\UserProductController::class, 'show']);
Route::get('user-products-logs', [Dealer\UserProductController::class, 'logs']);
Route::post('user-products/offline-out', [Dealer\UserProductController::class, 'offlineOutQty']);
Route::post('user-products/offline-out-revoke/{log}', [Dealer\UserProductController::class, 'revokeQtyLog']);
//计算商品下单价格
Route::get('orders/total-amount', [Dealer\OrderController::class, 'totalAmount']);

View File

@ -18,6 +18,7 @@ class DealerUserProductLog extends Model
public const TYPE_OFFLINE_OUT = 3;//线下去库存
public const TYPE_ADMIN_IN = 4;//后台添加库存
public const TYPE_ADMIN_OUT = 5;//后台扣减库存
public const TYPE_REVOKE_IN = 9;//撤销线下去库存
protected $fillable = [
'user_id',
@ -25,10 +26,16 @@ class DealerUserProductLog extends Model
'type',
'qty',
'remark',
'revoke_id',
];
public function product()
{
return $this->belongsTo(DealerProduct::class, 'product_id');
}
public function canRevoke()
{
return ($this->type == static::TYPE_OFFLINE_OUT) && ($this->revoke_id == 0);
}
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddHasRevokeToDealerUserProductLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dealer_user_product_logs', function (Blueprint $table) {
//
$table->unsignedBigInteger('revoke_id')->default(0)->comment('撤销关联ID');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('dealer_user_product_logs', function (Blueprint $table) {
//
$table->dropColumn('revoke_id');
});
}
}

View File

@ -2,6 +2,7 @@
use App\Models\AfterSale;
use App\Models\Article;
use App\Models\DealerUserProductLog;
use App\Models\Order;
use App\Models\OrderProduct;
use App\Models\ProductSku;
@ -23,4 +24,5 @@ return [
AfterSale::class => '售后订单',
Article::class => '文章',
OrderPackage::class => '包裹',
DealerUserProductLog::class => '日志',
];