Spaces:
Sleeping

Muhasebe / frontend /src /lib /orders.js
Caner Giden
Cursor
fix(orders): net tutar ve net kar hesabini siparis - kesintiler ile hizala
b2864a2
Raw History Blame Contribute Delete
2.11 kB
/** Sipariş satırı doğrulama ve net tutar gösterim mantığı. */
import { ORDER_MARKETPLACE_FEE_KEYS } from "../constants/app.js";
export function orderRowFeeDeductionsAbs(orderRow) {
return ORDER_MARKETPLACE_FEE_KEYS.reduce((s, k) => {
const v = Number(orderRow[k] ?? 0);
return s + (Number.isFinite(v) ? Math.abs(v) : 0);
}, 0);
}
function roundMoney2(x) {
return Math.round(x * 100) / 100;
}
/** Siparis tutari varsa siparis - |kesintiler|; yoksa null. */
export function computedNetPlatformAmount(orderRow) {
const ot = Number(orderRow.order_total ?? 0);
if (!Number.isFinite(ot) || ot === 0) return null;
return roundMoney2(ot - orderRowFeeDeductionsAbs(orderRow));
}
/** Liste: sipariş tutarı varsa net tutar daima sipariş − |kesintiler| (Excel’deki “Net Tutar” sütunu burada kullanılmaz). */
export function effectiveNetPlatformDisplay(orderRow) {
const c = computedNetPlatformAmount(orderRow);
if (c != null) return { value: c, computed: true };
const raw = orderRow.net_platform_amount;
if (raw != null && raw !== "") {
const n = Number(raw);
if (Number.isFinite(n)) return { value: n, computed: false };
}
return { value: null, computed: false };
}
export function validateOrderEntryForm(form) {
const num = (v) => (v === "" || v === undefined || v === null ? 0 : Number(v));
const errs = {};
if (!(form.order_date || "").trim()) errs.order_date = true;
if (!(form.order_no || "").trim()) errs.order_no = true;
const qty = num(form.quantity ?? 1);
if (!Number.isFinite(qty) || qty < 1) errs.quantity = true;
const orderTotal = num(form.order_total);
const netFilled = form.net_platform_amount !== "" && form.net_platform_amount != null;
const hasMarketplace = orderTotal !== 0 || netFilled;
const pn = (form.product_name || "").trim();
const usp = num(form.unit_sale_price);
const legacyOk = pn !== "" && usp !== 0;
if (!hasMarketplace && !legacyOk) {
errs.order_total = true;
errs.net_platform_amount = true;
errs.product_name = true;
errs.unit_sale_price = true;
}
return errs;
}