133 lines
5.1 KiB
PHP
133 lines
5.1 KiB
PHP
<?php
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Exceptions\RepositoryException;
|
||
use App\Models\ProductCategoryModel;
|
||
use App\Models\ProductModel;
|
||
use App\Models\ReconciliationItemModel;
|
||
use App\Models\ReconciliationModel;
|
||
use App\Models\StoreOrderItemModel;
|
||
use App\Models\StoreOrderModel;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* 对账明细构建(D1 品类 / D2 供应商筛选)
|
||
*
|
||
* 流程(事务内,可重复 build:先清后建):
|
||
* 1. 按周期 + 品类(含子孙分类)+ 供应商拉取「已完成」门店订单的订货明细
|
||
* 2. 每条订货明细 → 一条对账明细:
|
||
* publish_amount = 订货金额(order_item.amount)
|
||
* actual_amount = 采购成本(称重>0 ? 称重×单价 : 数量×单价,单价 = 成本/包规)
|
||
* diff = publish − actual,冗余 product_name / store_id
|
||
* 3. 汇总写回头的 publish/actual/diff_amount,status → 对账中
|
||
*/
|
||
class ReconciliationBuildService
|
||
{
|
||
/**
|
||
* @return int 生成的对账明细数
|
||
*/
|
||
public function build(ReconciliationModel $recon): int
|
||
{
|
||
return DB::transaction(function () use ($recon) {
|
||
// 1. 按周期 + 品类 + 供应商拉取已完成订单的订货明细
|
||
$itemQuery = StoreOrderItemModel::query()
|
||
->join('store_order', 'store_order.id', '=', 'store_order_item.order_id')
|
||
->where('store_order.status', StoreOrderModel::STATUS_COMPLETED)
|
||
->whereNull('store_order.deleted_at')
|
||
->whereDate('store_order.order_date', '>=', $recon->period_start)
|
||
->whereDate('store_order.order_date', '<=', $recon->period_end)
|
||
->select('store_order_item.*');
|
||
|
||
if ((int) $recon->supplier_id > 0) {
|
||
$itemQuery->where('store_order_item.supplier_id', $recon->supplier_id);
|
||
}
|
||
if ((int) $recon->category_id > 0) {
|
||
$itemQuery->whereIn(
|
||
'store_order_item.category_id',
|
||
$this->descendantCategoryIds((int) $recon->category_id)
|
||
);
|
||
}
|
||
|
||
$orderItems = $itemQuery->get()->makeVisible('cost_price');
|
||
if ($orderItems->isEmpty()) {
|
||
throw new RepositoryException('周期内无符合筛选条件的已完成订单数据,无法生成对账明细');
|
||
}
|
||
|
||
// 2. 先清后建(幂等)
|
||
ReconciliationItemModel::where('recon_id', $recon->id)->delete();
|
||
|
||
$publishTotal = '0';
|
||
$actualTotal = '0';
|
||
$rows = [];
|
||
$sort = 1;
|
||
$now = now();
|
||
foreach ($orderItems as $orderItem) {
|
||
$publish = (string) $orderItem->amount;
|
||
$unitCost = PurchaseEditService::unitCost(
|
||
(string) ($orderItem->cost_price ?? '0'),
|
||
(string) $orderItem->product_spec
|
||
);
|
||
$actual = PurchaseEditService::costAmount(
|
||
(string) $orderItem->quantity,
|
||
(string) $orderItem->weight,
|
||
$unitCost
|
||
);
|
||
$publishTotal = bcadd($publishTotal, $publish, 2);
|
||
$actualTotal = bcadd($actualTotal, $actual, 2);
|
||
|
||
$rows[] = [
|
||
'recon_id' => $recon->id,
|
||
'store_id' => $orderItem->store_id,
|
||
'order_item_id' => $orderItem->id,
|
||
'product_id' => $orderItem->product_id,
|
||
'product_name' => $orderItem->product_name,
|
||
'quantity' => $orderItem->quantity,
|
||
'weight' => $orderItem->weight,
|
||
'publish_amount' => $publish,
|
||
'actual_amount' => $actual,
|
||
'diff_amount' => bcsub($publish, $actual, 2),
|
||
'is_reconciled' => ReconciliationItemModel::NOT_RECONCILED,
|
||
'store_remark' => '',
|
||
'sort' => $sort++,
|
||
'created_at' => $now,
|
||
'updated_at' => $now,
|
||
];
|
||
}
|
||
ReconciliationItemModel::insert($rows);
|
||
|
||
// 3. 汇总写回头 + 状态流转
|
||
$recon->publish_amount = $publishTotal;
|
||
$recon->actual_amount = $actualTotal;
|
||
$recon->diff_amount = bcsub($publishTotal, $actualTotal, 2);
|
||
$recon->status = ReconciliationModel::STATUS_WORKING;
|
||
$recon->save();
|
||
|
||
return count($rows);
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 分类自身 + 全部子孙分类ID(多级分类下按顶级分类筛选)
|
||
*
|
||
* @return array<int, int>
|
||
*/
|
||
private function descendantCategoryIds(int $categoryId): array
|
||
{
|
||
$parentMap = ProductCategoryModel::pluck('parent_id', 'id');
|
||
$ids = [$categoryId];
|
||
$queue = [$categoryId];
|
||
while ($queue !== []) {
|
||
$current = array_shift($queue);
|
||
foreach ($parentMap as $id => $parentId) {
|
||
if ((int) $parentId === $current && ! in_array((int) $id, $ids, true)) {
|
||
$ids[] = (int) $id;
|
||
$queue[] = (int) $id;
|
||
}
|
||
}
|
||
}
|
||
|
||
return $ids;
|
||
}
|
||
}
|