修复一些错误

This commit is contained in:
liu
2026-08-14 21:28:55 +08:00
parent 398bb98356
commit 228212f8e3
20 changed files with 489 additions and 100 deletions
+209
View File
@@ -0,0 +1,209 @@
<?php
namespace Tests\Feature;
use App\Models\BillModel;
use App\Models\CustomerLevelModel;
use App\Models\PaymentModel;
use App\Models\ProductModel;
use App\Models\ProductPriceModel;
use App\Models\PurchaseOrderModel;
use App\Models\StoreModel;
use App\Models\StoreOrderModel;
use App\Models\UserModel;
/**
* 账单链路与支付:订单状态随业务链自动推进(采购单完成→配送中、生成账单→已完成);
* 账单支付后累加门店总采购金额(只统计商品金额,手动收款与支付审核双入口)
*/
class BillPaymentTest extends ProcurementTestCase
{
/**
* 完整业务链造账单:下单 → 接单 → 生成采购单 → 完成采购单 → 生成账单
* (商品 5.00 × 4 = 商品金额 20.00,配送费/筐/托盘均 0)
*
* @return array{0: StoreModel, 1: StoreOrderModel, 2: BillModel}
*/
private function makeBillViaChain(): array
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$product = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON]);
ProductPriceModel::factory()->create([
'product_id' => $product->id,
'level_id' => $level->id,
'price' => '5.00',
]);
$user = UserModel::factory()->forStore($store->id)->create();
$this->actingAsMiniUser($user);
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => 4]]])
->assertJsonPath('success', true);
$order = StoreOrderModel::where('store_id', $store->id)->first();
$this->actingAsSysUser();
// 接单
$this->putJson("/order/store/{$order->id}/status", ['status' => StoreOrderModel::STATUS_SUMMARIZED])
->assertJsonPath('success', true);
// 生成采购单 → 采购中
$this->postJson('/purchase/order/generate', ['purchase_date' => now()->toDateString()])
->assertJsonPath('success', true);
$purchase = PurchaseOrderModel::first();
// 完成采购单 → 配送中
$this->putJson("/purchase/order/{$purchase->id}/finish")->assertJsonPath('success', true);
// 生成账单 → 已完成
$this->postJson("/purchase/order/{$purchase->id}/bill", [
'stores' => [['store_id' => $store->id, 'delivery_fee' => 0, 'box_num' => 0, 'tray_num' => 0]],
])->assertJsonPath('success', true);
return [$store, $order->fresh(), BillModel::where('store_id', $store->id)->first()];
}
/** 造一张指定商品金额的未支付账单(总额=商品金额) */
private function makeBill(StoreModel $store, string $productAmount, array $attributes = []): BillModel
{
return BillModel::create(array_merge([
'bill_no' => 'ZD' . random_int(100000000000, 999999999999),
'purchase_id' => PurchaseOrderModel::factory()->create()->id,
'store_id' => $store->id,
'bill_date' => '2026-08-10',
'product_amount' => $productAmount,
'delivery_fee' => '0.00',
'box_num' => 0,
'tray_num' => 0,
'box_price' => '0.00',
'tray_price' => '0.00',
'added_amount' => '0.00',
'total_amount' => $productAmount,
'status' => BillModel::STATUS_UNPAID,
], $attributes));
}
/** 订单状态随业务链自动推进:接单→采购中→配送中→(生成账单)已完成 */
public function test_order_status_progresses_via_business_chain(): void
{
$level = CustomerLevelModel::factory()->create();
$store = StoreModel::factory()->create(['level_id' => $level->id]);
$product = ProductModel::factory()->create(['status' => ProductModel::STATUS_ON]);
ProductPriceModel::factory()->create(['product_id' => $product->id, 'level_id' => $level->id, 'price' => '5.00']);
$user = UserModel::factory()->forStore($store->id)->create();
$this->actingAsMiniUser($user);
$this->postJson('/mini/order', ['items' => [['product_id' => $product->id, 'quantity' => 4]]])
->assertJsonPath('success', true);
$order = StoreOrderModel::where('store_id', $store->id)->first();
$this->assertSame(StoreOrderModel::STATUS_PENDING, $order->status);
$this->actingAsSysUser();
// 接单 → 已接单
$this->putJson("/order/store/{$order->id}/status", ['status' => StoreOrderModel::STATUS_SUMMARIZED])
->assertJsonPath('success', true);
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $order->fresh()->status);
// 生成采购单 → 采购中
$this->postJson('/purchase/order/generate', ['purchase_date' => now()->toDateString()])
->assertJsonPath('success', true);
$purchase = PurchaseOrderModel::first();
$order->refresh();
$this->assertSame(StoreOrderModel::STATUS_DELIVERING, $order->status);
$this->assertSame($purchase->id, $order->purchase_id);
// 完成采购单 → 配送中
$this->putJson("/purchase/order/{$purchase->id}/finish")->assertJsonPath('success', true);
$this->assertSame(StoreOrderModel::STATUS_DISTRIBUTION, $order->fresh()->status);
// 生成账单 → 已完成 + 回写 bill_id
$this->postJson("/purchase/order/{$purchase->id}/bill", [
'stores' => [['store_id' => $store->id, 'delivery_fee' => 0, 'box_num' => 0, 'tray_num' => 0]],
])->assertJsonPath('success', true);
$order->refresh();
$bill = BillModel::where('store_id', $store->id)->first();
$this->assertNotNull($bill);
$this->assertSame(StoreOrderModel::STATUS_COMPLETED, $order->status);
$this->assertSame($bill->id, $order->bill_id);
$this->assertSame('20.00', (string) $bill->product_amount, '5.00 × 4');
}
/** 后台手动确认收款:累加门店总采购金额(只统计商品金额),重复收款拒绝不重复累加 */
public function test_manual_pay_accumulates_store_total_purchase_amount(): void
{
[$store, , $bill] = $this->makeBillViaChain();
$this->actingAsSysUser();
$this->putJson("/recon/bill/{$bill->id}/pay", ['pay_remark' => '线下现金'])
->assertJsonPath('success', true);
$store->refresh();
$this->assertSame('20.00', (string) $store->total_purchase_amount, '只累加商品金额 20.00');
// 第二笔账单收款后继续累加
$bill2 = $this->makeBill($store, '100.00');
$this->putJson("/recon/bill/{$bill2->id}/pay")->assertJsonPath('success', true);
$this->assertSame('120.00', (string) $store->fresh()->total_purchase_amount);
// 重复收款拒绝,金额不重复累加
$this->putJson("/recon/bill/{$bill->id}/pay")->assertJsonPath('success', false);
$this->assertSame('120.00', (string) $store->fresh()->total_purchase_amount);
}
/** 支付审核通过:关联账单置已支付并累加门店总采购金额 */
public function test_payment_audit_pass_accumulates_store_total_purchase_amount(): void
{
$store = StoreModel::factory()->create();
$user = UserModel::factory()->forStore($store->id)->create();
$payment = PaymentModel::create([
'payment_no' => 'ZF202608140001',
'store_id' => $store->id,
'user_id' => $user->id,
'amount' => '150.00',
'pay_method' => PaymentModel::METHOD_BANK,
'voucher_ids' => '',
'status' => PaymentModel::STATUS_PENDING,
]);
$bill1 = $this->makeBill($store, '100.00', ['payment_id' => $payment->id]);
$bill2 = $this->makeBill($store, '50.00', ['payment_id' => $payment->id]);
$this->actingAsSysUser();
$this->putJson("/recon/payment/{$payment->id}/audit", ['result' => 'pass'])
->assertJsonPath('success', true);
$this->assertSame(BillModel::STATUS_PAID, $bill1->fresh()->status);
$this->assertSame(BillModel::STATUS_PAID, $bill2->fresh()->status);
$this->assertSame('150.00', (string) $store->fresh()->total_purchase_amount);
// 重复审核拒绝,不重复累加
$this->putJson("/recon/payment/{$payment->id}/audit", ['result' => 'pass'])
->assertJsonPath('success', false);
$this->assertSame('150.00', (string) $store->fresh()->total_purchase_amount);
}
/** 审核拒绝:账单释放回待支付,不累加门店总采购金额 */
public function test_payment_audit_reject_does_not_accumulate(): void
{
$store = StoreModel::factory()->create();
$user = UserModel::factory()->forStore($store->id)->create();
$payment = PaymentModel::create([
'payment_no' => 'ZF202608140002',
'store_id' => $store->id,
'user_id' => $user->id,
'amount' => '100.00',
'pay_method' => PaymentModel::METHOD_WECHAT,
'voucher_ids' => '',
'status' => PaymentModel::STATUS_PENDING,
]);
$bill = $this->makeBill($store, '100.00', ['payment_id' => $payment->id]);
$this->actingAsSysUser();
$this->putJson("/recon/payment/{$payment->id}/audit", [
'result' => 'reject',
'audit_remark' => '凭证不清晰',
])->assertJsonPath('success', true);
$bill->refresh();
$this->assertSame(BillModel::STATUS_UNPAID, $bill->status);
$this->assertSame(0, $bill->payment_id, '账单释放可重新付款');
$this->assertSame('0.00', (string) $store->fresh()->total_purchase_amount);
}
}
+1 -32
View File
@@ -2,7 +2,6 @@
namespace Tests\Feature;
use App\Exports\PurchaseOrderExport;
use App\Models\CustomerLevelModel;
use App\Models\ProductCategoryModel;
use App\Models\ProductModel;
@@ -11,10 +10,9 @@ use App\Models\PurchaseOrderModel;
use App\Models\StoreModel;
use App\Models\StoreOrderModel;
use App\Models\UserModel;
use Maatwebsite\Excel\Facades\Excel;
/**
* 采购单导出(仅 Excel 表格):xlsx Content-Type / 蔬果分类过滤 / type 非法拒绝 /
* 采购单导出(仅 Excel 表格,全品类):xlsx Content-Type /
* 权限拦截 / 中文文件名 RFC 5987 编码
*/
class ExportTest extends ProcurementTestCase
@@ -71,35 +69,6 @@ class ExportTest extends ProcurementTestCase
$this->assertStringContainsString(rawurlencode('采购单'), $disposition);
}
/** 蔬果分类过滤:type=category 仅导出蔬菜/水果顶级分类商品 */
public function test_export_category_filters_to_vegetables(): void
{
$purchase = $this->buildPurchaseWithItems();
$this->actingAsSysUser();
Excel::fake();
$this->get("/purchase/order/{$purchase->id}/export?type=category")->assertOk();
Excel::assertDownloaded(
$purchase->purchase_no . '_采购单.xlsx',
static function (PurchaseOrderExport $export): bool {
$items = $export->collection();
// 仅蔬菜商品一行,肉禽被过滤
return $items->count() === 1;
}
);
}
/** type 参数非法 → 拒绝 */
public function test_export_invalid_type_rejected(): void
{
$purchase = $this->buildPurchaseWithItems();
$this->actingAsSysUser();
$this->get("/purchase/order/{$purchase->id}/export?type=doc")
->assertJsonPath('success', false);
}
/** 采购单不存在 → 拒绝 */
public function test_export_missing_purchase_rejected(): void
{
+127
View File
@@ -0,0 +1,127 @@
<?php
namespace Tests\Feature;
use App\Models\NoticeModel;
use App\Models\StoreModel;
use App\Models\UserModel;
/**
* 小程序消息通知:本人通知 + 全员广播;广播已读复制本人副本(data.broadcast_from 记来源),
* 列表排除已读广播。回归:列表曾报 Undefined property: stdClass::$data->broadcast_from
* pluck JSON 路径被 Query Builder 当作结果列名读取 stdClass 属性)
*/
class MiniNoticeTest extends ProcurementTestCase
{
/**
* 造一个绑定门店的小程序用户
*
* @return array{0: StoreModel, 1: UserModel}
*/
private function makeStoreWithUser(): array
{
$store = StoreModel::factory()->create();
return [$store, UserModel::factory()->forStore($store->id)->create()];
}
/** 通知列表:本人通知 + 未读广播,unread_count 正确 */
public function test_notice_list_includes_personal_and_broadcast(): void
{
[, $user] = $this->makeStoreWithUser();
$this->actingAsMiniUser($user);
NoticeModel::create([
'user_id' => NoticeModel::BROADCAST_USER_ID,
'type' => NoticeModel::TYPE_SYSTEM,
'title' => '系统公告',
'content' => '全员可见',
'is_read' => NoticeModel::UNREAD,
]);
NoticeModel::create([
'user_id' => $user->id,
'type' => NoticeModel::TYPE_ORDER,
'title' => '订单状态更新',
'content' => '您的订单已接单',
'is_read' => NoticeModel::UNREAD,
]);
$data = $this->getJson('/mini/notice')->assertOk()->json('data');
$this->assertSame(2, $data['total']);
$this->assertSame(2, $data['unread_count']);
}
/** 广播标记已读:复制本人已读副本,列表排除原广播(回归:列表不再报错) */
public function test_broadcast_read_creates_personal_copy(): void
{
[, $user] = $this->makeStoreWithUser();
$this->actingAsMiniUser($user);
$broadcast = NoticeModel::create([
'user_id' => NoticeModel::BROADCAST_USER_ID,
'type' => NoticeModel::TYPE_SYSTEM,
'title' => '系统公告',
'content' => '全员可见',
'data' => ['foo' => 'bar'],
'is_read' => NoticeModel::UNREAD,
]);
$this->putJson("/mini/notice/{$broadcast->id}/read")->assertJsonPath('success', true);
// 已读副本:本人记录、已读、broadcast_from 记来源且保留原 data
$copy = NoticeModel::where('user_id', $user->id)->first();
$this->assertNotNull($copy);
$this->assertSame(NoticeModel::READ, $copy->is_read);
$this->assertSame($broadcast->id, $copy->data['broadcast_from']);
$this->assertSame('bar', $copy->data['foo']);
// 列表:原广播被排除,仅剩已读副本,未读数归零
$data = $this->getJson('/mini/notice')->assertOk()->json('data');
$this->assertSame(1, $data['total']);
$this->assertSame(0, $data['unread_count']);
$this->assertSame($copy->id, $data['data'][0]['id']);
// 重复标记不重复复制
$this->putJson("/mini/notice/{$broadcast->id}/read")->assertJsonPath('success', true);
$this->assertSame(1, NoticeModel::where('user_id', $user->id)->count());
}
/** 个人通知标记已读:直接更新原记录,不产生副本 */
public function test_personal_notice_read_marks_directly(): void
{
[, $user] = $this->makeStoreWithUser();
$this->actingAsMiniUser($user);
$notice = NoticeModel::create([
'user_id' => $user->id,
'type' => NoticeModel::TYPE_ORDER,
'title' => '订单状态更新',
'content' => '您的订单已接单',
'is_read' => NoticeModel::UNREAD,
]);
$this->putJson("/mini/notice/{$notice->id}/read")->assertJsonPath('success', true);
$this->assertSame(NoticeModel::READ, $notice->fresh()->is_read);
$this->assertSame(1, NoticeModel::count(), '个人通知直接更新,不产生副本');
}
/** 他人通知不可标记(仅本人通知与广播可读) */
public function test_other_users_notice_not_found(): void
{
[, $user] = $this->makeStoreWithUser();
$this->actingAsMiniUser($user);
[, $other] = $this->makeStoreWithUser();
$notice = NoticeModel::create([
'user_id' => $other->id,
'type' => NoticeModel::TYPE_ORDER,
'title' => '订单状态更新',
'content' => '他人通知',
'is_read' => NoticeModel::UNREAD,
]);
$this->putJson("/mini/notice/{$notice->id}/read")->assertJsonPath('success', false);
$this->assertSame(NoticeModel::UNREAD, $notice->fresh()->is_read);
}
}
+64
View File
@@ -257,4 +257,68 @@ class StoreOrderTest extends ProcurementTestCase
$this->deleteJson("/order/store/{$order->id}")->assertOk()->assertJsonPath('success', true);
$this->deleteJson("/order/store/{$order->id}")->assertJsonPath('success', false);
}
/** 手动流转:仅保留接单与取消(待接单 → 已接单/已取消) */
public function test_manual_status_flow_only_accept_and_cancel(): void
{
$order = $this->makeOrderWithStatus(StoreOrderModel::STATUS_PENDING);
$this->actingAsSysUser();
$this->putJson("/order/store/{$order->id}/status", ['status' => StoreOrderModel::STATUS_SUMMARIZED])
->assertJsonPath('success', true);
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $order->fresh()->status);
$order2 = $this->makeOrderWithStatus(StoreOrderModel::STATUS_PENDING);
$this->actingAsSysUser();
$this->putJson("/order/store/{$order2->id}/status", ['status' => StoreOrderModel::STATUS_CANCELLED])
->assertJsonPath('success', true);
$this->assertSame(StoreOrderModel::STATUS_CANCELLED, $order2->fresh()->status);
}
/** 手动流转:配送中/已完成等目标状态被拒绝(由采购/账单链路自动推进) */
public function test_manual_status_flow_rejects_delivery_and_completion(): void
{
foreach ([StoreOrderModel::STATUS_DISTRIBUTION, StoreOrderModel::STATUS_COMPLETED] as $target) {
$order = $this->makeOrderWithStatus(StoreOrderModel::STATUS_DELIVERING);
$this->actingAsSysUser();
$this->putJson("/order/store/{$order->id}/status", ['status' => $target])
->assertJsonPath('success', false);
$this->assertSame(StoreOrderModel::STATUS_DELIVERING, $order->fresh()->status);
}
// 已接单不能手动转采购中(须经生成采购单)
$order = $this->makeOrderWithStatus(StoreOrderModel::STATUS_SUMMARIZED);
$this->actingAsSysUser();
$this->putJson("/order/store/{$order->id}/status", ['status' => StoreOrderModel::STATUS_DELIVERING])
->assertJsonPath('success', false);
}
/** 批量流转:仅接单/取消可选,配送/完成目标整批校验拒绝 */
public function test_batch_status_only_accept_and_cancel(): void
{
$pending1 = $this->makeOrderWithStatus(StoreOrderModel::STATUS_PENDING);
$pending2 = $this->makeOrderWithStatus(StoreOrderModel::STATUS_PENDING);
$this->actingAsSysUser();
// 批量接单
$this->putJson('/order/store/batchStatus', [
'ids' => [$pending1->id, $pending2->id],
'status' => StoreOrderModel::STATUS_SUMMARIZED,
])->assertJsonPath('success', true);
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $pending1->fresh()->status);
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $pending2->fresh()->status);
// 批量完成被拒绝(目标状态校验拦截)
$this->putJson('/order/store/batchStatus', [
'ids' => [$pending1->id],
'status' => StoreOrderModel::STATUS_COMPLETED,
])->assertJsonPath('success', false);
// 已接单订单不可批量取消(仅待接单可取消),整批中止
$this->putJson('/order/store/batchStatus', [
'ids' => [$pending1->id],
'status' => StoreOrderModel::STATUS_CANCELLED,
])->assertJsonPath('success', false);
$this->assertSame(StoreOrderModel::STATUS_SUMMARIZED, $pending1->fresh()->status);
}
}