默认重量

This commit is contained in:
liu
2026-09-05 14:37:22 +08:00
parent 364471a086
commit 12cbed411b
10 changed files with 205 additions and 8 deletions
@@ -12,6 +12,7 @@ use App\Models\StoreOrderItemModel;
use App\Models\StoreOrderModel;
use App\Services\BillNumberService;
use App\Services\ItemImageResolver;
use App\Services\WeightEstimator;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@@ -72,6 +73,7 @@ class OrderController extends BaseMiniController
$totalQuantity = '0';
$totalAmount = '0';
$totalWeight = '0';
$now = now();
$rows = [];
foreach ($items as $row) {
@@ -85,8 +87,11 @@ class OrderController extends BaseMiniController
$price = CustomerLevelModel::calcLevelPrice($product->cost_price, $level->percent);
$quantity = (string) $row['quantity'];
$amount = bcmul($price, $quantity, 2);
// 参考重量 = 订货量 × 规格折算(仅作参考,实际称重以采购录入为准)
$weight = WeightEstimator::estimate((string) $product->spec, (string) $product->unit, $quantity);
$totalQuantity = bcadd($totalQuantity, $quantity, 2);
$totalAmount = bcadd($totalAmount, $amount, 2);
$totalWeight = bcadd($totalWeight, $weight, 3);
$rows[] = [
'store_id' => $store->id,
@@ -102,7 +107,7 @@ class OrderController extends BaseMiniController
'content' => (string) $product->content,
'shelf_life' => (int) $product->shelf_life,
'quantity' => $quantity,
'weight' => 0,
'weight' => $weight,
'amount' => $amount,
'cost_price' => (string) $product->cost_price,
'remark' => '',
@@ -116,7 +121,7 @@ class OrderController extends BaseMiniController
'store_id' => $store->id,
'order_date' => $now->toDateString(),
'total_quantity' => $totalQuantity,
'total_weight' => 0,
'total_weight' => $totalWeight,
'total_amount' => $totalAmount,
'status' => StoreOrderModel::STATUS_PENDING,
'remark' => $remark,
@@ -51,7 +51,7 @@ class ProductController extends BaseController
$params,
ProductModel::query()->with(['category:id,name', 'supplier:id,name'])
)
->orderBy('sort', 'desc')
->orderBy('sort')
->orderBy('id', 'desc')
->paginate($pageSize);
$data->getCollection()->makeVisible('cost_price');
@@ -22,6 +22,7 @@ use App\Services\BillGenerateService;
use App\Services\ItemImageResolver;
use App\Services\PurchaseGenerateService;
use App\Services\PurchaseItemService;
use App\Services\WeightEstimator;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
@@ -481,7 +482,10 @@ class PurchaseOrderController extends BaseController
'content' => (string) $product->content,
'shelf_life' => (int) $product->shelf_life,
'quantity' => $quantity,
'weight' => bcadd((string) ($validated['weight'] ?? 0), '0', 3),
// 未传称重时按订货量 × 规格预填参考重量
'weight' => isset($validated['weight'])
? bcadd((string) $validated['weight'], '0', 3)
: WeightEstimator::estimate((string) $product->spec, (string) $product->unit, (string) $quantity),
'amount' => bcmul($price, (string) $quantity, 2),
'cost_price' => (string) $product->cost_price,
'remark' => '',
+6 -1
View File
@@ -63,13 +63,18 @@ readonly class PurchaseGenerateService
static fn (string $carry, $item): string => bcadd($carry, bcmul($item->quantity, $item->cost_price, 2), 2),
'0'
);
// 参考总重量 = Σ 明细参考重量(下单时按订货量 × 规格预填)
$totalWeight = $items->reduce(
static fn (string $carry, $item): string => bcadd($carry, (string) $item->weight, 3),
'0'
);
$purchase = PurchaseOrderModel::create([
'purchase_no' => $this->billNumberService->make('PO'),
'purchase_date' => $date,
'status' => PurchaseOrderModel::STATUS_PENDING,
'total_quantity' => $totalQuantity,
'total_weight' => 0,
'total_weight' => $totalWeight,
'estimate_amount' => $estimateAmount,
'actual_amount' => 0,
'operator_id' => $operatorId,
+60
View File
@@ -0,0 +1,60 @@
<?php
namespace App\Services;
/**
* 参考重量估算:订货量 × 单品规格折算(斤),仅作参考,实际称重以人工录入为准
*/
class WeightEstimator
{
/**
* 估算参考重量(斤,3 位小数)
*
* 计价单位本身是重量单位时订货量即重量;否则从规格(如「10斤/箱」「500g/袋」)
* 解析每件重量再乘订货量;规格无法解析时计 0
*/
public static function estimate(string $spec, string $unit, string $quantity): string
{
return bcmul($quantity === '' ? '0' : $quantity, self::perUnitJin($spec, $unit), 3);
}
/**
* 单个计价单位折算重量(斤)
*/
private static function perUnitJin(string $spec, string $unit): string
{
// 按重量计价的单位:订货量本身就是重量
$unitWeight = self::toJin('1', $unit);
if ($unitWeight !== null) {
return $unitWeight;
}
// 从规格解析每件重量(如「10斤/箱」「500g/袋」「1.5kg/箱」)
if (preg_match('/(\d+(?:\.\d+)?)\s*(公斤|千克|kg|克|斤|g)/iu', $spec, $matches) === 1) {
$parsed = self::toJin($matches[1], $matches[2]);
if ($parsed !== null) {
return $parsed;
}
}
// 规格中的裸数字按斤计(如「10/箱」);无法解析计 0
if (preg_match('/\d+(?:\.\d+)?/', $spec, $matches) === 1) {
return $matches[0];
}
return '0';
}
/**
* 数值按单位折算为斤;非重量单位返回 null(1斤=500g1公斤/千克/kg=2斤)
*/
private static function toJin(string $value, string $unit): ?string
{
return match (mb_strtolower(trim($unit))) {
'斤' => $value,
'公斤', '千克', 'kg' => bcmul($value, '2', 3),
'克', 'g' => bcdiv($value, '500', 3),
default => null,
};
}
}