修复BUG
This commit is contained in:
@@ -130,7 +130,7 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
'product_spec' => $spec,
|
||||
'unit' => (string) ($snapshot->unit ?? $product->unit),
|
||||
'cost_price' => (float) ($snapshot->cost_price ?? $product->cost_price),
|
||||
// 参考零售价 = 加权平均售价 ÷ 包规数值(无订货行无售价数据,留空)
|
||||
// 单价 = 加权平均售价 ÷ 包规数值(无订货行无售价数据,留空)
|
||||
'retail_price' => $group !== null && (float) $quantity > 0
|
||||
? $this->unitRefPrice((float) bcdiv($amount, $quantity, 4), $spec)
|
||||
: null,
|
||||
@@ -173,7 +173,7 @@ class PurchaseOrderExport implements FromCollection, WithStrictNullComparison, W
|
||||
|
||||
// 列头
|
||||
$rows[] = array_merge(
|
||||
['序号', '分类', '品名', '供应商', '市场', '包规', '单位', '成本', '参考零售价', '数量', '实际称重', '金额'],
|
||||
['序号', '分类', '品名', '供应商', '市场', '包规', '单位', '成本', '单价', '数量', '实际称重', '金额'],
|
||||
array_values($this->storeNames),
|
||||
);
|
||||
$this->specialRows[++$rowIndex] = 'header';
|
||||
|
||||
@@ -6,11 +6,13 @@ use App\Exceptions\RepositoryException;
|
||||
use App\Models\PurchaseOrderModel;
|
||||
use App\Models\StoreOrderItemModel;
|
||||
use App\Models\SupplierModel;
|
||||
use App\Services\PurchaseItemService;
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
|
||||
/**
|
||||
* 供应商采购明细导出:多供应商合并为一个 XLSX(每供应商一个工作表,工作表名=供应商名称);
|
||||
* 构造传入 supplierId 时仅导出该供应商(单工作表)
|
||||
* 供应商采购明细导出:多供应商合并为一个 XLSX,按「供应商 × 市场」拆分工作表
|
||||
* (工作表名=供应商·市场,市场取商品档案,未设置市场归入「未设置」);
|
||||
* 构造传入 supplierId 时仅导出该供应商
|
||||
*/
|
||||
class PurchaseSupplierExport implements WithMultipleSheets
|
||||
{
|
||||
@@ -51,14 +53,23 @@ class PurchaseSupplierExport implements WithMultipleSheets
|
||||
->orderBy('id')
|
||||
->get(['id', 'name']);
|
||||
|
||||
$service = app(PurchaseItemService::class);
|
||||
$usedNames = [];
|
||||
$sheets = [];
|
||||
foreach ($suppliers as $supplier) {
|
||||
$sheets[] = new PurchaseSupplierSheet(
|
||||
$this->purchase,
|
||||
$supplier,
|
||||
SheetName::make((string) $supplier->name, (int) $supplier->id, $usedNames),
|
||||
);
|
||||
// 按市场拆分工作表
|
||||
$matrix = $service->supplierMarketMatrix($this->purchase->id, (int) $supplier->id);
|
||||
foreach ($matrix['markets'] as $market => $rows) {
|
||||
$marketLabel = $market === '' ? '未设置' : $market;
|
||||
$sheets[] = new PurchaseSupplierSheet(
|
||||
$this->purchase,
|
||||
$supplier,
|
||||
$marketLabel,
|
||||
$rows,
|
||||
$matrix['stores'],
|
||||
SheetName::make($supplier->name . '·' . $marketLabel, (int) $supplier->id, $usedNames),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $sheets;
|
||||
|
||||
@@ -4,20 +4,25 @@ namespace App\Exports;
|
||||
|
||||
use App\Models\PurchaseOrderModel;
|
||||
use App\Models\SupplierModel;
|
||||
use App\Services\PurchaseItemService;
|
||||
use Illuminate\Support\Collection;
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
|
||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
||||
/**
|
||||
* 供应商采购明细导出 · 单供应商工作表(成本口径:金额=Σ数量×成本价)
|
||||
* 供应商采购明细导出 · 单市场工作表
|
||||
* 模板列序:品名、汇总、市场、各门店明细(门店列=该供应商有明细的门店);
|
||||
* 有数据的单元格(品名/汇总/门店数量)填充突出颜色并加粗
|
||||
*/
|
||||
class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison, WithStyles, WithTitle
|
||||
{
|
||||
/** 有数据单元格填充色(浅黄,突出显示) */
|
||||
private const string DATA_FILL = 'FFFFFF99';
|
||||
|
||||
private ?Collection $rows = null;
|
||||
|
||||
/** @var array<int, string> 特殊行索引(1 起)=> 类型(title/header/summary) */
|
||||
@@ -26,15 +31,29 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
/** 列头所在行索引 */
|
||||
private int $headerRow = 1;
|
||||
|
||||
/** @var array<int, array<int, int>> 有数据的单元格:行索引(1 起)=> 列索引(1 起)列表 */
|
||||
private array $dataCells = [];
|
||||
|
||||
/**
|
||||
* @param PurchaseOrderModel $purchase 采购单
|
||||
* @param SupplierModel $supplier 供应商
|
||||
* @param string $marketLabel 市场名(空市场已转为「未设置」)
|
||||
* @param array<int, array<string, mixed>> $productRows 商品行(品名/汇总数量/各门店数量)
|
||||
* @param array<int, string> $stores 门店列(门店ID => 名称)
|
||||
* @param string $sheetTitle 工作表名
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly PurchaseOrderModel $purchase,
|
||||
private readonly SupplierModel $supplier,
|
||||
private readonly string $marketLabel,
|
||||
private readonly array $productRows,
|
||||
private readonly array $stores,
|
||||
private readonly string $sheetTitle,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出行:标题/空行/列头/明细/合计
|
||||
* 导出行:标题/空行/列头(品名、汇总、市场、各门店)/明细/合计
|
||||
*/
|
||||
public function collection(): Collection
|
||||
{
|
||||
@@ -42,48 +61,52 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
return $this->rows;
|
||||
}
|
||||
|
||||
$items = app(PurchaseItemService::class)->supplierRows($this->purchase->id, $this->supplier->id);
|
||||
$storeIds = array_map('intval', array_keys($this->stores));
|
||||
|
||||
$rows = [];
|
||||
$rowIndex = 0;
|
||||
|
||||
// 标题行
|
||||
$rows[] = [$this->supplier->name . ' · 采购单 ' . $this->purchase->purchase_no];
|
||||
$rows[] = [$this->supplier->name . ' · ' . $this->marketLabel . ' · 采购单 ' . $this->purchase->purchase_no];
|
||||
$this->specialRows[++$rowIndex] = 'title';
|
||||
|
||||
// 空行
|
||||
$rows[] = [''];
|
||||
$rowIndex++;
|
||||
|
||||
// 列头
|
||||
$rows[] = ['序号', '品名', '市场', '包规', '单位', '成本价', '数量', '重量(斤)', '金额'];
|
||||
// 列头:品名、汇总、市场、各门店明细
|
||||
$rows[] = array_merge(['品名', '汇总', '市场'], array_values($this->stores));
|
||||
$this->specialRows[++$rowIndex] = 'header';
|
||||
$this->headerRow = $rowIndex;
|
||||
|
||||
// 明细行
|
||||
// 明细行(有数据的单元格记录到 dataCells:品名/汇总/门店数量)
|
||||
$totalQuantity = 0;
|
||||
$totalWeight = '0';
|
||||
$totalAmount = '0';
|
||||
foreach (array_values($items) as $sort => $item) {
|
||||
$rows[] = [
|
||||
$sort + 1,
|
||||
$item['product_name'],
|
||||
$item['market'],
|
||||
$item['product_spec'],
|
||||
$item['unit'],
|
||||
(float) $item['cost_price'],
|
||||
(int) $item['quantity'],
|
||||
(float) $item['weight'],
|
||||
(float) $item['amount'],
|
||||
$storeTotals = array_fill_keys($storeIds, 0);
|
||||
foreach ($this->productRows as $productRow) {
|
||||
$line = [
|
||||
$productRow['product_name'],
|
||||
(int) $productRow['quantity'],
|
||||
$this->marketLabel,
|
||||
];
|
||||
$rowIndex++;
|
||||
$totalQuantity += (int) $item['quantity'];
|
||||
$totalWeight = bcadd($totalWeight, (string) $item['weight'], 3);
|
||||
$totalAmount = bcadd($totalAmount, (string) $item['amount'], 2);
|
||||
$filled = [1, 2, 3];
|
||||
foreach ($storeIds as $position => $storeId) {
|
||||
$quantity = (int) ($productRow['store_quantities'][$storeId] ?? 0);
|
||||
$line[] = $quantity > 0 ? $quantity : '';
|
||||
if ($quantity > 0) {
|
||||
$filled[] = 4 + $position;
|
||||
$storeTotals[$storeId] += $quantity;
|
||||
}
|
||||
}
|
||||
$rows[] = $line;
|
||||
$this->dataCells[++$rowIndex] = $filled;
|
||||
$totalQuantity += (int) $productRow['quantity'];
|
||||
}
|
||||
|
||||
// 合计行
|
||||
$rows[] = ['', '合计', '', '', '', '', $totalQuantity, (float) $totalWeight, (float) $totalAmount];
|
||||
$rows[] = array_merge(
|
||||
['合计', $totalQuantity, ''],
|
||||
array_map(static fn (int $storeId): int => $storeTotals[$storeId], $storeIds),
|
||||
);
|
||||
$this->specialRows[++$rowIndex] = 'summary';
|
||||
|
||||
return $this->rows = collect($rows);
|
||||
@@ -95,17 +118,33 @@ class PurchaseSupplierSheet implements FromCollection, WithStrictNullComparison,
|
||||
}
|
||||
|
||||
/**
|
||||
* 标题/列头/合计加粗,冻结列头
|
||||
* 标题/列头/合计加粗,有数据的单元格填充突出颜色,冻结列头
|
||||
*/
|
||||
public function styles(Worksheet $sheet): array
|
||||
{
|
||||
$this->collection();
|
||||
|
||||
$sheet->freezePane('A' . ($this->headerRow + 1));
|
||||
$widths = [6, 24, 12, 14, 8, 10, 10, 12, 12];
|
||||
|
||||
$widths = [24, 10, 12];
|
||||
foreach ($widths as $index => $width) {
|
||||
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex($index + 1))->setWidth($width);
|
||||
}
|
||||
$storeCount = count($this->stores);
|
||||
for ($i = 0; $i < $storeCount; $i++) {
|
||||
$sheet->getColumnDimension(Coordinate::stringFromColumnIndex(4 + $i))->setWidth(12);
|
||||
}
|
||||
|
||||
// 有数据的单元格:填充突出颜色并加粗(含品名/汇总/门店数量)
|
||||
foreach ($this->dataCells as $rowIndex => $columns) {
|
||||
foreach ($columns as $columnIndex) {
|
||||
$style = $sheet->getStyle(Coordinate::stringFromColumnIndex($columnIndex) . $rowIndex);
|
||||
$style->getFill()
|
||||
->setFillType(Fill::FILL_SOLID)
|
||||
->getStartColor()->setARGB(self::DATA_FILL);
|
||||
$style->getFont()->setBold(true);
|
||||
}
|
||||
}
|
||||
|
||||
$styles = [];
|
||||
foreach ($this->specialRows as $row => $type) {
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Mini;
|
||||
|
||||
use App\Exceptions\RepositoryException;
|
||||
use App\Http\Requests\Mini\MiniOrderRequest;
|
||||
use App\Models\CartModel;
|
||||
use App\Models\CustomerLevelModel;
|
||||
use App\Models\ProductModel;
|
||||
use App\Models\StoreOrderItemModel;
|
||||
@@ -104,6 +105,12 @@ class OrderController extends BaseMiniController
|
||||
}
|
||||
StoreOrderItemModel::insert($rows);
|
||||
|
||||
// 下单成功后自动清空购物车中已下单的商品
|
||||
CartModel::query()
|
||||
->where('store_id', $store->id)
|
||||
->whereIn('product_id', $productIds)
|
||||
->delete();
|
||||
|
||||
return $order;
|
||||
});
|
||||
|
||||
|
||||
@@ -52,6 +52,15 @@ class StoreOrderController extends BaseController
|
||||
});
|
||||
}
|
||||
|
||||
// 按采购单号模糊搜索(关联采购单)
|
||||
$purchaseNo = trim((string) ($params['purchase_no'] ?? ''));
|
||||
if ($purchaseNo !== '') {
|
||||
$keyword = '%' . str_replace('%', '\%', $purchaseNo) . '%';
|
||||
$query->whereHas('purchase', static function ($purchaseQuery) use ($keyword) {
|
||||
$purchaseQuery->where('purchase_no', 'like', $keyword);
|
||||
});
|
||||
}
|
||||
|
||||
$data = $this->buildSearch($params, $query)
|
||||
->orderBy('order_date', 'desc')
|
||||
->orderBy('id', 'desc')
|
||||
|
||||
@@ -105,7 +105,7 @@ class PurchaseOrderController extends BaseController
|
||||
$quantity = 0;
|
||||
// 采购总重量
|
||||
$weight = '0';
|
||||
// 采购总金额(Σ明细 amount,参考零售价按 金额÷数量 加权)
|
||||
// 采购总金额(Σ明细 amount,单价按 金额÷数量 加权)
|
||||
$amount = '0';
|
||||
// 门店明细
|
||||
$cellsMap = [];
|
||||
|
||||
@@ -26,6 +26,7 @@ class ProductFormRequest extends BaseFormRequest
|
||||
'name' => 'required|string|max:100',
|
||||
'spec' => 'nullable|string|max:100',
|
||||
'unit' => 'nullable|string|max:20',
|
||||
'price_unit' => 'nullable|string|max:20',
|
||||
'image_ids' => 'nullable|array|max:255',
|
||||
'image_ids.*' => ['integer', new Exists(SysFileModel::class, 'id')],
|
||||
'content' => 'nullable|string',
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace App\Http\Requests\Purchase;
|
||||
use Modules\Common\Http\Requests\BaseFormRequest;
|
||||
|
||||
/**
|
||||
* 采购单生成账单 验证(按门店提交配送费/周转筐/托盘数量,金额由系统汇总不可修改;
|
||||
* 采购单生成账单 验证(按门店提交配送费/周转筐/托盘数量与售后/备注,金额由系统汇总不可修改;
|
||||
* 筐/托盘数量正数=压筐附加金额,负数=回筐抵扣金额)
|
||||
*/
|
||||
class PurchaseBillGenerateRequest extends BaseFormRequest
|
||||
@@ -20,6 +20,8 @@ class PurchaseBillGenerateRequest extends BaseFormRequest
|
||||
'stores.*.delivery_fee' => 'required|numeric|min:0',
|
||||
'stores.*.box_num' => 'required|integer',
|
||||
'stores.*.tray_num' => 'required|integer',
|
||||
'stores.*.after_sale' => 'nullable|string|max:255',
|
||||
'stores.*.remark' => 'nullable|string|max:255',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -39,6 +41,8 @@ class PurchaseBillGenerateRequest extends BaseFormRequest
|
||||
'stores.*.box_num.integer' => '周转筐数量必须为整数(正数=压筐,负数=回筐)',
|
||||
'stores.*.tray_num.required' => '周转托盘数量不能为空',
|
||||
'stores.*.tray_num.integer' => '周转托盘数量必须为整数(正数=压筐,负数=回筐)',
|
||||
'stores.*.after_sale.max' => '售后说明最长 255 个字符',
|
||||
'stores.*.remark.max' => '备注最长 255 个字符',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,6 +75,7 @@ class BillModel extends Model
|
||||
'paid_operator_id',
|
||||
'operator_id',
|
||||
'remark',
|
||||
'after_sale',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
|
||||
@@ -31,6 +31,7 @@ class ProductModel extends Model
|
||||
'name',
|
||||
'spec',
|
||||
'unit',
|
||||
'price_unit',
|
||||
'image_ids',
|
||||
'content',
|
||||
'sort',
|
||||
@@ -67,7 +68,7 @@ class ProductModel extends Model
|
||||
public function imageIds(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn ($value) => explode(',', $value),
|
||||
get: fn ($value) => $value ? explode(',', $value) : [],
|
||||
set: fn ($value) => is_array($value) ? implode(',', $value) : $value,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ readonly class BillGenerateService
|
||||
|
||||
/**
|
||||
* @param PurchaseOrderModel $purchase 已完成采购单
|
||||
* @param array<int, array{store_id: int, delivery_fee: string, box_num: int, tray_num: int}> $stores 按门店提交的配送费/周转筐/托盘数量
|
||||
* @param array<int, array{store_id: int, delivery_fee: string, box_num: int, tray_num: int, after_sale?: string, remark?: string}> $stores 按门店提交的配送费/周转筐/托盘数量与售后/备注
|
||||
* @param int $operatorId 生成人(后台系统用户ID)
|
||||
* @return BillModel[] 生成的账单列表
|
||||
* @throws Throwable
|
||||
@@ -111,6 +111,8 @@ readonly class BillGenerateService
|
||||
'added_amount' => $addedAmount,
|
||||
'total_amount' => $totalAmount,
|
||||
'operator_id' => $operatorId,
|
||||
'after_sale' => (string) ($row['after_sale'] ?? ''),
|
||||
'remark' => (string) ($row['remark'] ?? ''),
|
||||
]);
|
||||
|
||||
// 关联该门店在采购单中的全部订单与订单明细到账单,订单转入「已完成」
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Services;
|
||||
|
||||
use App\Models\ProductModel;
|
||||
use App\Models\PurchaseOrderModel;
|
||||
use App\Models\StoreModel;
|
||||
use App\Models\StoreOrderItemModel;
|
||||
use App\Models\StoreOrderModel;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
@@ -138,6 +139,60 @@ class PurchaseItemService
|
||||
return $this->sortRows($rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* 供应商采购明细矩阵(导出用):该供应商在采购单内的商品行按市场分组,
|
||||
* 行附「汇总数量 + 各门店数量」;门店列=本采购单内有该供应商明细的门店(按ID排序)
|
||||
*
|
||||
* @return array{
|
||||
* stores: array<int, string>,
|
||||
* markets: array<string, array<int, array<string, mixed>>>
|
||||
* }
|
||||
*/
|
||||
public function supplierMarketMatrix(int $purchaseId, int $supplierId): array
|
||||
{
|
||||
$items = $this->purchaseItems($purchaseId)
|
||||
->where('supplier_id', $supplierId)
|
||||
->sortBy('id');
|
||||
|
||||
$storeIds = $items->pluck('store_id')
|
||||
->map(static fn ($id): int => (int) $id)
|
||||
->unique()->sort()->values()->all();
|
||||
$stores = StoreModel::withTrashed()
|
||||
->whereIn('id', $storeIds)
|
||||
->orderBy('id')
|
||||
->pluck('name', 'id')
|
||||
->toArray();
|
||||
|
||||
$rows = [];
|
||||
foreach ($items->groupBy('product_id') as $productId => $group) {
|
||||
$first = $group->first();
|
||||
$quantity = 0;
|
||||
$storeQuantities = array_fill_keys(array_map('intval', array_keys($stores)), 0);
|
||||
foreach ($group as $item) {
|
||||
$quantity += (int) $item->quantity;
|
||||
$storeQuantities[(int) $item->store_id] += (int) $item->quantity;
|
||||
}
|
||||
|
||||
$rows[] = [
|
||||
'product_id' => (int) $productId,
|
||||
'product_name' => $first->product_name,
|
||||
'market' => (string) data_get($first, 'market', ''),
|
||||
'quantity' => $quantity,
|
||||
'store_quantities' => $storeQuantities,
|
||||
'category_sort' => (int) data_get($first, 'category_sort', 9999),
|
||||
'product_sort' => (int) data_get($first, 'product_sort', 9999),
|
||||
];
|
||||
}
|
||||
|
||||
// 排序后按市场分组(组间/组内均保持 分类sort → 商品sort 顺序)
|
||||
$markets = [];
|
||||
foreach ($this->sortRows($rows) as $row) {
|
||||
$markets[$row['market']][] = $row;
|
||||
}
|
||||
|
||||
return ['stores' => $stores, 'markets' => $markets];
|
||||
}
|
||||
|
||||
/**
|
||||
* 采购单订货明细(关联订单过滤软删、成本价可见、附分类/商品排序键)
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user