fix some bugs from accounting

This commit is contained in:
2026-07-28 17:44:24 +03:30
parent 91f6b24da4
commit d81cea3d4f
7 changed files with 995 additions and 247 deletions
@@ -353,6 +353,7 @@ class CartController extends Controller
/**
* ثبت سبد در دفترکل داخلی + حسابداری بیرونی (مستقل از is_store؛ فقط accounting_provider).
* ته‌حساب/زرین: per-line تا دستهٔ هر محصول درست برود.
*/
private function syncCartOrderAccounting(Order $order, Cart $cart): void
{
@@ -364,43 +365,62 @@ class CartController extends Controller
report($e);
}
$primaryProduct = Product::find($cart->items->first()?->product_id);
$metalId = max(1, (int) ($primaryProduct->metal_id ?? 1));
if ($config->isTahesabAccounting()) {
try {
app(TahesabController::class)->storeTrade([
'customer_id' => $order->customer_id,
'amount' => $order->amount,
'direction' => (int) ($order->direction ?? 1),
'price' => $order->price,
'total_price' => $order->total_price,
'pay_at' => $order->pay_at ?? $order->created_at,
'product' => ['metal_id' => $metalId],
'order' => ['id' => $order->id],
]);
} catch (\Throwable $e) {
report($e);
$lineIndex = 0;
foreach ($cart->items as $item) {
$product = Product::find($item->product_id);
if (!$product) {
continue;
}
}
if ($config->isZarinAccounting()) {
try {
app(\App\Http\Controllers\Admin\ZarinController::class)->storeTrade([
'customer_id' => $order->customer_id,
'amount' => $order->amount,
'direction' => (int) ($order->direction ?? 1),
'price' => $order->price,
'total_price' => $order->total_price,
'pay_at' => $order->pay_at ?? $order->created_at,
'product' => [
'metal_id' => $metalId,
'id' => $primaryProduct?->id,
],
'order' => ['id' => $order->id],
]);
} catch (\Throwable $e) {
report($e);
$qty = max(1, (int) ($item->quantity ?? 1));
$unitWeight = (float) ($product->amount ?? 0);
$lineWeight = $unitWeight > 0 ? $unitWeight * $qty : (float) $qty;
$unitPrice = (float) ($item->unit_price ?? 0);
if ($unitPrice <= 0) {
$unitPrice = (float) ($product->price ?? 0) + (float) ($product->price_profit ?? 0);
}
$lineTotal = (float) ($item->total_price ?? ($unitPrice * $qty));
$payload = [
'customer_id' => $order->customer_id,
'amount' => $lineWeight,
'number' => $qty,
'direction' => (int) ($order->direction ?? 1),
'price' => $unitPrice,
'total_price' => $lineTotal,
'pay_at' => $order->pay_at ?? $order->created_at,
'product_id' => (int) $product->id,
'product' => [
'id' => $product->id,
'title' => $product->title,
'group_type' => $product->group_type,
'metal_id' => $product->metal_id,
'amount' => $product->amount,
'unit' => $product->unit,
'trade_by_quantity' => $product->trade_by_quantity,
],
'line' => $lineIndex + 1,
'order' => [
'id' => (int) $order->id,
'product_id' => (int) $product->id,
],
];
$lineIndex++;
if ($config->isTahesabAccounting()) {
try {
app(TahesabController::class)->storeTrade($payload);
} catch (\Throwable $e) {
report($e);
}
}
if ($config->isZarinAccounting()) {
try {
app(\App\Http\Controllers\Admin\ZarinController::class)->storeTrade($payload);
} catch (\Throwable $e) {
report($e);
}
}
}