账单导出

This commit is contained in:
2026-10-30 10:51:09 +08:00
parent 42a3fa00d4
commit 7366d88baf
178 changed files with 1166 additions and 898 deletions
+101 -2
View File
@@ -2,6 +2,7 @@
namespace Tests\Feature;
use App\Exports\BillBatchExport;
use App\Exports\BillExport;
use App\Models\BillModel;
use App\Models\ProductCategoryModel;
@@ -12,8 +13,8 @@ use App\Models\StoreOrderItemModel;
use Maatwebsite\Excel\Facades\Excel;
/**
* 门店账单单独导出:账单内按商品合并明细(加权平均单价)/ 一级分类过滤 /
* 费用行取账单自身字段 / 权限拦截 / 账单不存在 / 分类下无明细拒绝 / 小程序端本店口径
* 门店账单导出:单账单合并明细 / 批量按门店分工作表 / 一级分类过滤 /
* 费用行取账单自身字段 / 权限拦截 / 参数校验 / 小程序端本店口径
*/
class BillExportTest extends ProcurementTestCase
{
@@ -138,6 +139,68 @@ class BillExportTest extends ProcurementTestCase
);
}
/** 批量导出:同一门店多张账单合并在一张工作表,不同门店拆分工作表 */
public function test_batch_export_groups_bills_by_store(): void
{
$root = ProductCategoryModel::create(['name' => '蔬菜', 'parent_id' => 0, 'sort' => 0, 'status' => 1]);
$storeA = StoreModel::factory()->create(['name' => '门店A']);
$storeB = StoreModel::factory()->create(['name' => '门店B']);
$product = ProductModel::factory()->create(['category_id' => $root->id]);
$billA1 = $this->makeBill($storeA);
$billA2 = $this->makeBill($storeA);
$billB = $this->makeBill($storeB);
$this->makeItem($billA1, $product, 2, '2.00');
$this->makeItem($billA2, $product, 3, '3.00');
$this->makeItem($billB, $product, 4, '4.00');
$this->actingAsSysUser();
Excel::fake();
$query = http_build_query(['ids' => [$billB->id, $billA2->id, $billA1->id]]);
$this->get('/recon/bill/batch-export?' . $query)->assertOk();
Excel::assertDownloaded(
'门店账单_批量导出.xlsx',
static function (BillBatchExport $export) use ($billA1, $billA2, $billB): bool {
$sheets = collect($export->sheets());
if ($sheets->count() !== 2) {
return false;
}
$sheetA = $sheets->first(static fn (BillExport $sheet): bool => $sheet->title() === '门店A');
$sheetB = $sheets->first(static fn (BillExport $sheet): bool => $sheet->title() === '门店B');
if ($sheetA === null || $sheetB === null) {
return false;
}
$rowsA = $sheetA->collection()->values();
$billTitlesA = $rowsA->filter(static fn (array $row): bool => str_contains((string) $row[0], '门店账单 '));
if ($rowsA->count() !== 21 || $billTitlesA->count() !== 2) {
return false;
}
if (! str_contains((string) $rowsA[0][0], $billA1->bill_no)
|| ! str_contains((string) $rowsA[11][0], $billA2->bill_no)) {
return false;
}
$rowsB = $sheetB->collection()->values();
return $rowsB->count() === 10 && str_contains((string) $rowsB[0][0], $billB->bill_no);
}
);
}
/** 批量导出:必须选择已存在的账单,且一次最多 100 张 */
public function test_batch_export_validates_selected_ids(): void
{
$this->actingAsSysUser();
$this->getJson('/recon/bill/batch-export')->assertJsonPath('success', false);
$this->getJson('/recon/bill/batch-export?ids[]=99999')->assertJsonPath('success', false);
$this->getJson('/recon/bill/batch-export?' . http_build_query(['ids' => range(1, 101)]))
->assertJsonPath('success', false);
}
/** 一级分类过滤:子分类商品归入根分类,其他分类商品忽略;费用行仍全额计入 */
public function test_export_filters_by_root_category(): void
{
@@ -227,6 +290,42 @@ class BillExportTest extends ProcurementTestCase
->assertJsonPath('success', false);
}
/** 小程序端批量导出:本店多账单合并工作表,混入他店账单时整批拒绝 */
public function test_mini_batch_export_scoped_to_own_store(): void
{
$root = ProductCategoryModel::create(['name' => '蔬菜', 'parent_id' => 0, 'sort' => 0, 'status' => 1]);
$storeA = StoreModel::factory()->create(['name' => '门店A']);
$storeB = StoreModel::factory()->create(['name' => '门店B']);
$product = ProductModel::factory()->create(['category_id' => $root->id]);
$billA1 = $this->makeBill($storeA);
$billA2 = $this->makeBill($storeA);
$billB = $this->makeBill($storeB);
$this->makeItem($billA1, $product, 2, '2.00');
$this->makeItem($billA2, $product, 3, '3.00');
$this->actingAsMiniStore($storeA);
Excel::fake();
$query = http_build_query(['ids' => [$billA1->id, $billA2->id]]);
$this->get('/mini/bill/batch-export?' . $query)->assertOk();
Excel::assertDownloaded(
'门店账单_批量导出.xlsx',
static function (BillBatchExport $export): bool {
$sheets = $export->sheets();
return count($sheets) === 1
&& $sheets[0]->title() === '门店A'
&& $sheets[0]->collection()->count() === 21;
}
);
$otherStoreQuery = http_build_query(['ids' => [$billA1->id, $billB->id]]);
$this->get('/mini/bill/batch-export?' . $otherStoreQuery)
->assertJsonPath('success', false);
$this->get('/mini/bill/batch-export')->assertJsonPath('success', false);
}
/** 小程序端导出:强制本店过滤,他店账单拒绝 */
public function test_mini_export_scoped_to_own_store(): void
{