column('id', 'ID')->sortable();
$grid->column('item_id', '商品ID')->display(function ($value) {
return "{$value}";
});
$grid->column('quantity', '当前库存')->display(function ($value) {
return number_format($value);
});
$grid->column('total_buy_quantity', '累计买入数量')->display(function ($value) {
return number_format($value);
});
$grid->column('total_sell_quantity', '累计卖出数量')->display(function ($value) {
return number_format($value);
});
$grid->column('total_buy_amount', '累计买入金额')->display(function ($value) {
return number_format($value, 5);
});
$grid->column('total_sell_amount', '累计卖出金额')->display(function ($value) {
return number_format($value, 5);
});
$grid->column('average_buy_price', '平均买入价')->display(function () {
if ($this->total_buy_quantity > 0) {
$avgPrice = bcdiv($this->total_buy_amount, $this->total_buy_quantity, 5);
return number_format($avgPrice, 5);
}
return '-';
});
$grid->column('average_sell_price', '平均卖出价')->display(function () {
if ($this->total_sell_quantity > 0) {
$avgPrice = bcdiv($this->total_sell_amount, $this->total_sell_quantity, 5);
return number_format($avgPrice, 5);
}
return '-';
});
$grid->column('last_transaction_at', '最后交易时间');
// 禁用新增、编辑和删除
$grid->disableCreateButton();
$grid->disableEditButton();
$grid->disableDeleteButton();
// 筛选器
$grid->filter(function (Grid\Filter $filter) {
$filter->equal('id', 'ID');
$filter->equal('item_id', '商品ID');
$filter->between('quantity', '库存范围');
$filter->between('total_buy_quantity', '累计买入数量范围');
$filter->between('total_sell_quantity', '累计卖出数量范围');
$filter->between('total_buy_amount', '累计买入金额范围');
$filter->between('total_sell_amount', '累计卖出金额范围');
$filter->between('last_transaction_at', '最后交易时间')->datetime();
});
// 默认排序
$grid->model()->orderBy('quantity', 'desc');
// 工具栏
$grid->tools(function (Grid\Tools $tools) {
$tools->append('
');
});
// 行操作
$grid->actions(function (Grid\Displayers\Actions $actions) {
// 可以添加查看详细统计的操作
$actions->append('
统计
');
});
});
}
/**
* 详情页面
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
return Show::make($id, new MexWarehouseRepository(), function (Show $show) {
$show->field('id', 'ID');
$show->field('item_id', '商品ID');
$show->divider('库存信息');
$show->field('quantity', '当前库存');
$show->field('total_buy_quantity', '累计买入数量');
$show->field('total_sell_quantity', '累计卖出数量');
$show->field('net_quantity', '净买入数量')->as(function () {
return $this->total_buy_quantity - $this->total_sell_quantity;
});
$show->divider('金额信息');
$show->field('total_buy_amount', '累计买入金额');
$show->field('total_sell_amount', '累计卖出金额');
$show->field('net_amount', '净买入金额')->as(function () {
return bcsub($this->total_buy_amount, $this->total_sell_amount, 5);
});
$show->divider('价格信息');
$show->field('average_buy_price', '平均买入价')->as(function () {
if ($this->total_buy_quantity > 0) {
return bcdiv($this->total_buy_amount, $this->total_buy_quantity, 5);
}
return '0.00000';
});
$show->field('average_sell_price', '平均卖出价')->as(function () {
if ($this->total_sell_quantity > 0) {
return bcdiv($this->total_sell_amount, $this->total_sell_quantity, 5);
}
return '0.00000';
});
$show->field('last_transaction_at', '最后交易时间');
// 禁用编辑和删除
$show->disableEditButton();
$show->disableDeleteButton();
});
}
/**
* 表单页面(仅用于查看,不允许编辑)
*
* @return Form
*/
protected function form()
{
return Form::make(new MexWarehouseRepository(), function (Form $form) {
$form->display('id', 'ID');
$form->display('item_id', '商品ID');
$form->display('quantity', '当前库存');
$form->display('total_buy_quantity', '累计买入数量');
$form->display('total_sell_quantity', '累计卖出数量');
$form->display('total_buy_amount', '累计买入金额');
$form->display('total_sell_amount', '累计卖出金额');
$form->display('last_transaction_at', '最后交易时间');
// 禁用保存按钮
$form->disableSubmitButton();
$form->disableResetButton();
});
}
}