6
0
Fork 0
jiqu-library-server/app/Admin/Controllers/ProductSkuController.php

163 lines
5.5 KiB
PHP

<?php
namespace App\Admin\Controllers;
use App\Admin\Renderable\ProductSkuTable;
use App\Admin\Repositories\ProductSku;
use App\Exceptions\BizException;
use App\Models\ProductBuynote;
use App\Models\ProductGroup;
use App\Models\ProductSku as ProductSkuModel;
use Carbon\Carbon;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Http\Request;
class ProductSkuController extends AdminController
{
/**
* Make a grid builder.
*
* @return Grid
*/
protected function grid()
{
return ProductSkuTable::grid();
}
/**
* Make a show builder.
*
* @param mixed $id
*
* @return Show
*/
protected function detail($id)
{
return Show::make($id, new ProductSku(), function (Show $show) {
$show->field('id');
$show->field('spu_id');
$show->field('name');
$show->field('subtitle');
$show->field('category_id');
$show->field('cover');
$show->field('images');
$show->field('description');
$show->field('sell_price');
$show->field('market_price');
$show->field('cost_price');
$show->field('vip_price');
$show->field('media');
$show->field('weight');
$show->field('attrs');
$show->field('specs');
$show->field('stock');
$show->field('sales');
$show->field('release_at');
$show->field('created_at');
$show->field('updated_at');
});
}
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
return Form::make(new ProductSku(), function (Form $form) {
$form->display('id');
$form->text('name')->required();
$form->text('subtitle');
$form->divider();
$form->image('cover')
->move('product-skus/cover/'.Carbon::now()->toDateString())
->saveFullUrl()
->removable(false)
->autoUpload();
$form->multipleImage('images')
->move('product-skus/'.Carbon::now()->toDateString())
->saveFullUrl()
->removable(false)
->autoUpload();
$form->file('media')->chunked()
->accept('mp4', 'mp4/*')
->move('prduct-sku-medias/'.Carbon::now()->toDateString())
->saveFullUrl()
->removable(false)
->autoUpload();
$form->editor('description');
$form->select('buynote_id')->options(ProductBuynote::all()->pluck('name', 'id'));
$form->number('weight')->min(0)->default(0);
$form->number('stock')->min(0)->default(0);
$form->divider();
$form->currency('sell_price')->symbol('¥')->default(0)->customFormat(function ($sellPrice) {
return bcdiv($sellPrice, 100, 2);
})->saving(function ($sellPrice) {
return bcmul($sellPrice, 100);
});
$form->currency('market_price')->symbol('¥')->default(0)->customFormat(function ($marketPrice) {
return bcdiv($marketPrice, 100, 2);
})->saving(function ($marketPrice) {
return bcmul($marketPrice, 100);
});
$form->currency('cost_price')->symbol('¥')->default(0)->customFormat(function ($costPrice) {
return bcdiv($costPrice, 100, 2);
})->saving(function ($costPrice) {
return bcmul($costPrice, 100);
});
$form->currency('vip_price')->symbol('¥')->customFormat(function ($vipPrice) {
if (is_null($vipPrice)) {
return null;
}
return bcdiv($vipPrice, 100, 2);
})->saving(function ($vipPrice) {
if (is_null($vipPrice)) {
return null;
}
return bcmul($vipPrice, 100);
});
$form->divider();
$form->select('attr_group')->options(ProductGroup::all()->pluck('name', 'id'));
$form->selectAttr('attrs')->listen('attr_group');
$form->hidden('verify_state');
$form->saving(function (Form $form) {
if (!(is_null($form->model()->release_at) || $form->model()->verify_state == 1)) {
return $form->response()->error('当前sku商品无法修改');
}
$form->verify_state = 0;
});
$form->ignore(['attr_group']);
$form->display('created_at');
$form->display('updated_at');
});
}
public function destroy($id)
{
$sku = ProductSkuModel::findOrFail($id);
if ($sku->release_at || $sku->verify_state == 1 || $sku->sales > 0) {//如果是上架中、审核中或者已售出的,无法删除
throw new BizException(__('product-sku.options.deny_message'));
}
return parent::destroy($id);
}
public function skus(Request $request)
{
$name = $request->input('q');
$query = ProductSkuModel::select('id', 'name as text');
if ($name) {
$query->where('name', 'like', "%$name%");
return $query->paginate(null);
}
return response()->json($query->get());
}
}