<!doctype html>
<html lang="fa" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#10283e">
<title>محاسبه گر فروش اقساطی ایران اورجینال</title>
<style>
:root{
font-family:Vazirmatn,"Segoe UI",Tahoma,sans-serif;
color:#173044; background:#f3f6f8; font-synthesis:none;
--ink:#122d41;--sub:#708291;--line:#e3ebef;--teal:#08766f;
--surface:#fff;--amber:#fff9e9;
}
*{box-sizing:border-box}
body{margin:0; min-height:100vh; background:linear-gradient(180deg,#eaf3f5 0,#f6f8fa 360px)}
button,input{font:inherit}
button{cursor:pointer}
.container{max-width:1150px; margin:0 auto; padding:30px 22px 60px}
header{display:flex;align-items:center;justify-content:space-between;gap:24px;margin-bottom:26px}
.brand{display:flex;align-items:center;gap:14px}
.mark{width:51px;height:51px;background:#102e40;color:#fefefe;border-radius:15px;display:grid;place-items:center;font-size:26px;font-weight:800;box-shadow:0 9px 20px #102e401f}
h1{margin:0 0 3px;font-size:clamp(21px,2.4vw,30px);font-weight:850;letter-spacing:-.7px}
.subhead{margin:0;font-size:13px;color:var(--sub)}
.actions{display:flex;gap:9px;flex-wrap:wrap}
.action{border:1px solid #cad7dc;background:white;border-radius:11px;padding:10px 15px;color:#24485b;font-size:12px;font-weight:750}
.action:hover{background:#edf8f6}
.action.primary{color:white;background:#103a49;border-color:#103a49}
.banner{display:flex;align-items:center;gap:11px;background:#e7f4f2;border:1px solid #cfebe6;padding:13px 17px;border-radius:13px;color:#326e6b;font-size:13px;margin-bottom:22px}
.banner b{color:#086962}
.dot{width:8px;height:8px;flex-shrink:0;border-radius:50%;background:#2fa890}
.section{background:var(--surface);border:1px solid #e5ecef;box-shadow:0 7px 35px #12354d0c;border-radius:22px;padding:25px;margin-bottom:22px}
.sectiontitle{display:flex;align-items:center;justify-content:space-between;gap:12px;margin:0 0 20px}
.sectionname{display:flex;align-items:center;gap:10px}
.index{background:#e3f4ef;color:#08776c;width:37px;height:37px;display:grid;place-items:center;border-radius:11px;font-weight:850;font-size:14px}
h2{font-size:18px;letter-spacing:-.3px;margin:0;font-weight:850}
.caption{font-size:12px;color:var(--sub);margin-top:4px}
.pill{padding:8px 11px;background:#eff7f8;color:#3e7075;border-radius:40px;font-size:11px;font-weight:750;white-space:nowrap}
.pill.strong{color:#09634d;background:#ddf5ea}
.fields{display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-bottom:16px}
.field{border:1px solid #dce6eb;background:#fbfcfd;border-radius:14px;padding:13px 15px 12px}
.field label{display:block;font-size:12px;color:#5f7283;font-weight:750;margin-bottom:8px}
.valuefield{position:relative}
.valuefield input{width:100%;font-family:Tahoma,Arial,sans-serif;direction:ltr;text-align:right;outline:0; border:0;background:transparent;color:#12384a;font-weight:850;font-size:21px;letter-spacing:.1px;padding:0 0 2px 57px;font-variant-numeric:tabular-nums}
.valuefield input:focus{color:#08766f}
.valuefield .unit{position:absolute;left:0;bottom:5px;font-size:12px;color:#8a9ca9}
.field:focus-within{border-color:#0f9489;box-shadow:0 0 0 3px #0f948918}
.field input::placeholder{font-weight:500;color:#b4c2cb}
.summary{display:grid;grid-template-columns:repeat(3,1fr);gap:12px;margin:13px 0 19px}
.summarybox{border:1px solid #e5edf1;border-radius:13px;padding:14px 15px;background:#f7fafb;min-width:0}
.summarybox span{display:block;color:#738797;font-size:11px;margin-bottom:9px}
.summarybox strong{font-size:17px;font-family:Tahoma,Arial,sans-serif;font-variant-numeric:tabular-nums;overflow-wrap:anywhere}
.summarybox.highlight{border-color:#b6e9d6;background:#e5f7ee}
.summarybox.highlight strong{color:#066b56;font-size:22px}
.tablewrap{overflow:auto;border:1px solid #e4ecf0;border-radius:13px}
table{width:100%;border-collapse:collapse;text-align:right;table-layout:auto;min-width:630px}
thead{background:#f0f5f7}
th{font-size:11px;padding:13px 15px;color:#617688;white-space:nowrap}
td{padding:13px 15px;font-size:12px;border-top:1px solid #e9eff2;color:#284459}
td.num{font-family:Tahoma,Arial,sans-serif;font-size:14px;font-weight:750;direction:ltr;text-align:right;white-space:nowrap;font-variant-numeric:tabular-nums}
tbody tr:hover{background:#f7fbfb}
tbody td:first-child{font-weight:850;color:#193d4d;white-space:nowrap}
.footerrow{display:flex;align-items:center;gap:6px;font-size:11px;color:#82939f;margin-top:12px}
.minbar{margin:-3px 0 15px;display:flex;align-items:center;gap:12px;flex-wrap:wrap}
.track{height:7px;flex:1;min-width:120px;background:#e5eff0;border-radius:20px;overflow:hidden}
.track > div{height:100%;background:linear-gradient(90deg,#15a895,#08766f);border-radius:20px;transition:width .2s}
.mintext{color:#5c7683;font-size:12px}
.mintext b{color:#08766f}
.helper{display:flex;align-items:center;gap:8px;background:#fff8e7;color:#795825;border:1px solid #f4e5bb;border-radius:12px;padding:12px 14px;margin-bottom:14px;font-size:12px;line-height:1.8}
.warning{display:none;color:#af5632;font-size:11px;margin-top:8px}
.warning.show{display:block}
.lower-note{font-size:12px;color:#728593;margin:19px auto 0;text-align:center;max-width:750px;line-height:1.9}
.print-value,.print-percent{display:none}
.customer-grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:12px;margin-bottom:12px}
.customer-grid .field{min-width:0;padding:10px 13px}
.customer-grid .address-field{grid-column:span 2}
.plain-input{width:100%;min-height:30px;background:transparent;border:0;outline:0;color:var(--ink);font-size:14px;text-align:right}
.plain-input::placeholder{color:#a9bbc5}
.plain-input.numeric-input{direction:ltr;text-align:right;font-variant-numeric:tabular-nums;font-family:Tahoma,Arial,sans-serif;font-weight:700}
.customer-footer{display:flex;align-items:center;justify-content:space-between;gap:10px;flex-wrap:wrap;color:#738797;font-size:11px}
.copy-actions{display:flex;gap:7px;flex-wrap:wrap}
.mini-btn{border-radius:9px;padding:7px 10px;background:#edf6f6;border:1px solid #cbe5e1;color:#155e5b;font-weight:750;font-size:11px}
.mini-btn:hover{background:#d9f2ec}
.check-fields{grid-template-columns:1fr 1fr 1fr}
.percent-wrap{display:flex;gap:8px;align-items:center}
.percent-wrap input{border:0;outline:0;background:transparent;color:#08766f;width:100%;font-size:22px;font-weight:850;font-family:Tahoma,Arial,sans-serif;direction:ltr;text-align:right}
.percent-wrap span{font-size:17px;color:#679085}
.pct-note{font-size:10px;color:#83979a;margin-top:6px}
@media(max-width:950px){.customer-grid{grid-template-columns:repeat(2,minmax(0,1fr))}.check-fields{grid-template-columns:repeat(2,minmax(0,1fr))}.percentage-field{grid-column:span 2}}
@media(max-width:760px){
.container{padding:22px 12px 35px}header{flex-direction:column;align-items:stretch;gap:17px}
.section{padding:17px 14px;border-radius:17px}
.fields{grid-template-columns:1fr}
.customer-grid{grid-template-columns:1fr 1fr}
.check-fields{grid-template-columns:1fr}
.percentage-field{grid-column:auto}
.summary{grid-template-columns:1fr 1fr}
.summarybox.highlight{grid-column:1/-1}
.sectiontitle{align-items:flex-start}
.pill{font-size:10px}
h2{font-size:16px}
}
@media(max-width:430px){
.summarybox strong{font-size:14px}
.customer-grid{grid-template-columns:1fr}
.customer-grid .address-field{grid-column:auto}
.sectionname{align-items:flex-start}
.pill{padding:7px}
}
@media print{
@page{size:A4 portrait;margin:8mm 8.5mm}
*,*::before,*::after{-webkit-print-color-adjust:exact;print-color-adjust:exact}
html,body{width:100%;height:auto;min-height:0;margin:0;padding:0;background:#fff!important;color:#142c38}
.container{width:100%;max-width:none;margin:0;padding:0}
.actions,.banner,.customer-footer,.footerrow,.lower-note,.helper,.minbar,.warning,.pct-note,.caption,.pill{display:none!important}
header{margin:0 0 3.1mm;gap:0;align-items:center;display:flex}
.brand{gap:7px}
.mark{width:29px;height:29px;border-radius:7px;font-size:15px;box-shadow:none}
h1{font-size:17px;line-height:1.35;letter-spacing:0;margin:0}
.subhead{font-size:9px;margin:1px 0 0;color:#60717c}
.section{padding:3.6mm 3.6mm 3.6mm;margin:0 0 3mm;border-radius:8px;border:1px solid #cad8dd;box-shadow:none;break-inside:avoid;page-break-inside:avoid}
.section:last-of-type{margin-bottom:0}
.sectiontitle{margin:0 0 2mm;gap:8px}
.sectionname{gap:6px}
.index{width:23px;height:23px;border-radius:5px;font-size:11px}
h2{font-size:13.5px;margin:0;line-height:1.4}
.customer-grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:1.6mm;margin:0}
.customer-grid .address-field{grid-column:span 2}
.customer-grid .field{padding:1.9mm 1.9mm 1.8mm;min-height:13mm}
.customer-grid .plain-input{display:none!important}
.customer-grid .print-value{display:block;font:11px Tahoma,Arial,sans-serif;color:#152f3c;white-space:pre-wrap;overflow-wrap:anywhere;line-height:1.55;min-height:3.5mm}
.customer-grid [data-print-for="productPrice"],.customer-grid [data-print-for="nationalId"],.customer-grid [data-print-for="customerPhone"]{direction:ltr;text-align:right}
.field{padding:2.1mm 2mm;border-radius:6px}
.field label{font-size:10px;margin:0 0 1mm;line-height:1.4}
.fields{grid-template-columns:repeat(2,minmax(0,1fr));gap:1.8mm;margin:0 0 2mm}
.check-fields{grid-template-columns:repeat(3,minmax(0,1fr))}
.percentage-field{grid-column:auto}
.valuefield input{font-size:14px;padding-bottom:0;line-height:1.45;padding-left:38px}
.valuefield .unit{font-size:9px;bottom:1px}
.percent-wrap input,.percent-wrap .print-percent{font-size:14px;line-height:1.45}
.percent-wrap span{font-size:11px}
.percent-wrap input,.percent-wrap span:not(.print-percent){display:none!important}
.percent-wrap .print-percent{display:block;color:#08766f;font-weight:850;direction:ltr;font-family:Tahoma,Arial,sans-serif;text-align:right;width:100%}
.summary{grid-template-columns:repeat(3,minmax(0,1fr));gap:1.6mm;margin:0 0 2mm}
.summarybox{min-width:0;border-radius:6px;padding:1.5mm 2mm}
.summarybox span{font-size:9.5px;margin-bottom:1mm;line-height:1.4}
.summarybox strong,.summarybox.highlight strong{font-size:13px;line-height:1.5}
.summarybox.highlight{grid-column:auto}
.tablewrap{overflow:visible;border-radius:6px}
table{width:100%;min-width:0;table-layout:auto;border-collapse:collapse}
th{font-size:10px;font-weight:700;padding:2.1mm 2.2mm;white-space:nowrap}
td{padding:2.2mm 2.2mm;font-size:10px;line-height:1.4}
td.num{font-size:11.5px;font-weight:700}
tbody tr{break-inside:avoid;page-break-inside:avoid}
.tablewrap th,.tablewrap td{border-color:#e1e8eb}
input{outline:0!important;box-shadow:none!important}
input::placeholder{color:transparent!important}
}
</style>
</head>
<body>
<div class="container">
<header>
<div class="brand">
<div class="mark">٪</div>
<div><h1>محاسبه گر فروش اقساطی ایران اورجینال</h1><p class="subhead">فرم مشتری و دو روش مستقل فروش اقساطی • همهٔ مبالغ به تومان</p></div>
</div>
<div class="actions">
<button class="action primary" type="button" onclick="window.print()">چاپ / ذخیره PDF</button>
<button class="action" type="button" id="resetBtn">پاککردن ورودیها</button>
</div>
</header>
<div class="banner"><span class="dot"></span><span>اطلاعات مشتری را ثبت کنید و برای محاسبهٔ هر روش، <b>مبلغ کالا</b> و <b>پیشپرداخت</b> همان روش را وارد کنید. دو روش از هم مستقلاند.</span></div>
<section class="section customer-section" aria-labelledby="customerTitle">
<div class="sectiontitle"><div class="sectionname"><span class="index">✎</span><div><h2 id="customerTitle">اطلاعات مشتری و محصول</h2><div class="caption">ثبت مشخصات جهت چاپ یا ذخیرهٔ برگهٔ فروش</div></div></div><span class="pill">فرم اطلاعات</span></div>
<div class="customer-grid">
<div class="field"><label for="saleDate">تاریخ</label><input id="saleDate" class="plain-input" type="text" placeholder="مثلاً ۱۴۰۵/۰۷/۱۷" autocomplete="off" /><span class="print-value" data-print-for="saleDate">—</span></div>
<div class="field"><label for="productModel">مدل محصول</label><input id="productModel" class="plain-input" type="text" placeholder="مدل یا مشخصات دستگاه" autocomplete="off" /><span class="print-value" data-print-for="productModel">—</span></div>
<div class="field"><label for="productPrice">قیمت محصول (تومان)</label><input id="productPrice" class="plain-input numeric-input" type="text" inputmode="numeric" placeholder="مبلغ به تومان" autocomplete="off" /><span class="print-value" data-print-for="productPrice">—</span></div>
<div class="field"><label for="customerFullName">نام و نام خانوادگی</label><input id="customerFullName" class="plain-input" type="text" placeholder="نام و نام خانوادگی مشتری" autocomplete="off" /><span class="print-value" data-print-for="customerFullName">—</span></div>
<div class="field"><label for="nationalId">کد ملی</label><input id="nationalId" class="plain-input" type="text" inputmode="numeric" maxlength="10" placeholder="۱۰ رقم" autocomplete="off" /><span class="print-value" data-print-for="nationalId">—</span></div>
<div class="field"><label for="customerPhone">تلفن</label><input id="customerPhone" class="plain-input" type="tel" inputmode="tel" placeholder="شماره تماس" autocomplete="off" /><span class="print-value" data-print-for="customerPhone">—</span></div>
<div class="field address-field"><label for="customerAddress">آدرس</label><input id="customerAddress" class="plain-input" type="text" placeholder="آدرس کامل مشتری" autocomplete="off" /><span class="print-value" data-print-for="customerAddress">—</span></div>
</div>
<div class="customer-footer"><span>قیمت این فرم برای ثبت اطلاعات است؛ قیمت هر روش فروش مستقل باقی میماند.</span><div class="copy-actions"><button type="button" class="mini-btn" id="copyLoanPrice">انتقال قیمت به تسهیلات</button><button type="button" class="mini-btn" id="copyCheckPrice">انتقال قیمت به اقساط با چک</button></div></div>
</section>
<section class="section" aria-labelledby="finTitle">
<div class="sectiontitle">
<div class="sectionname"><span class="index">۰۱</span><div><h2 id="finTitle">اقساط با تسهیلات</h2><div class="caption">پرداخت ماهانه در ۱۲، ۱۸، ۲۴ یا ۳۶ ماه</div></div></div>
<span class="pill">مستقل از جدول چک</span>
</div>
<div class="fields">
<div class="field"><label for="loanPrice">مبلغ کالا</label><div class="valuefield"><input id="loanPrice" type="text" inputmode="numeric" autocomplete="off" aria-label="مبلغ کالای تسهیلات" placeholder="قیمت کالا"><span class="unit">تومان</span></div></div>
<div class="field"><label for="loanAdvance">پیشپرداخت</label><div class="valuefield"><input id="loanAdvance" type="text" inputmode="numeric" autocomplete="off" aria-label="پیش پرداخت تسهیلات" placeholder="مبلغ پیشپرداخت"><span class="unit">تومان</span></div></div>
</div>
<div class="summary">
<div class="summarybox"><span>ارزش افزوده (۶٪)</span><strong id="loanVat">—</strong></div>
<div class="summarybox"><span>قیمت کالا با ارزش افزوده</span><strong id="loanGross">—</strong></div>
<div class="summarybox highlight"><span>مبلغ تسهیلات نهایی</span><strong id="loanBalance">—</strong></div>
</div>
<div class="tablewrap"><table><thead><tr><th>مدت اقساط</th><th>مبلغ هر قسط</th><th>چک ضمانت</th></tr></thead><tbody id="loanRows"></tbody></table></div>
<div class="footerrow">نرخ اقساط و شرایط ضمانت مطابق فایل اکسل اولیه محاسبه شدهاند.</div>
</section>
<section class="section" aria-labelledby="checkTitle">
<div class="sectiontitle">
<div class="sectionname"><span class="index">۰۲</span><div><h2 id="checkTitle">اقساط با چک</h2><div class="caption">محاسبه چکها برای بازههای ۲ تا ۱۰ ماه</div></div></div>
<span class="pill strong">حداقل پیشپرداخت ۴۰٪</span>
</div>
<div class="fields check-fields">
<div class="field"><label for="checkPrice">مبلغ کالا</label><div class="valuefield"><input id="checkPrice" type="text" inputmode="numeric" autocomplete="off" aria-label="مبلغ کالای فروش با چک" placeholder="قیمت کالا"><span class="unit">تومان</span></div></div>
<div class="field"><label for="checkAdvance">پیشپرداخت (حداقل ۴۰٪ مبلغ کالا)</label><div class="valuefield"><input id="checkAdvance" type="text" inputmode="numeric" autocomplete="off" aria-label="پیش پرداخت فروش با چک" placeholder="حداقل ۴۰٪ قیمت"><span class="unit">تومان</span></div><div class="warning" id="advanceWarning" role="status">مبلغ کمتر از ۴۰٪ است و هنگام خروج از کادر به حداقل مجاز اصلاح میشود.</div></div>
<div class="field percentage-field"><label for="checkPercent">درصد پیشپرداخت (از ۴۰٪ تا ۱۰۰٪)</label><div class="percent-wrap"><input id="checkPercent" type="number" min="40" max="100" step="1" value="40" inputmode="decimal" aria-label="درصد پیشپرداخت"><span>٪</span><span class="print-percent" id="printPercent">۴۰٪</span></div><div class="pct-note">با تغییر درصد، مبلغ پیشپرداخت خودکار محاسبه میشود.</div></div>
</div>
<div class="minbar"><div class="mintext">حداقل مجاز: <b id="checkMinimum">—</b> تومان</div><div class="track"><div id="downProgress" style="width:40%"></div></div><div class="mintext">درصد فعلی: <b id="downPct">۴۰٪</b></div></div>
<div class="helper">پیشپرداخت کمتر از ۴۰٪ پذیرفته نمیشود؛ اگر مبلغ کالا تغییر کند، حداقل جدید بهطور خودکار اعمال خواهد شد.</div>
<div class="summary">
<div class="summarybox"><span>ارزش افزوده (۶٪)</span><strong id="checkVat">—</strong></div>
<div class="summarybox"><span>پیشپرداخت اعمالشده</span><strong id="checkApplied">—</strong></div>
<div class="summarybox highlight"><span>مبلغ مبنای چکها</span><strong id="checkBalance">—</strong></div>
</div>
<div class="tablewrap"><table><thead><tr><th>مدت</th><th>مبلغ کل چکها</th><th>مبلغ هر چک</th><th>تعداد چک</th><th>شرایط پرداخت</th></tr></thead><tbody id="checkRows"></tbody></table></div>
<div class="footerrow">برای مدت ۴ماهه نیز مبلغ چک بهصورت فرمولی محاسبه میشود.</div>
</section>
<p class="lower-note">این صفحه آفلاین اجرا میشود و اطلاعات واردشده را برای سرور ارسال نمیکند. دکمهٔ «چاپ / ذخیره PDF» خروجی ثابت و قابل ارائه ایجاد میکند؛ محاسبههای زنده در همین صفحه انجام میشوند.</p>
</div>
<script>
"use strict";
const TAX=0.06, MIN_DOWN=0.40;
const LOAN_TERMS=[
{months:12,perMillion:120000,guarantee:"هر ۱۰۰ میلیون، یک چک ضمانت"},
{months:18,perMillion:90000,guarantee:"هر ۱۰۰ میلیون، یک چک ضمانت"},
{months:24,perMillion:75000,guarantee:"هر ۲۰۰ میلیون، یک چک ضمانت"},
{months:36,perMillion:60000,guarantee:"هر ۲۵۰ میلیون، یک چک ضمانت"}
];
const CHECK_TERMS=[
{months:2,factor:1.11,count:2,kind:"هر ماه یک چک",terms:"چک اول ۲۵ روزه"},
{months:4,factor:1.22,count:2,kind:"دو فقره چک هر دو ماه",terms:"الباقی هر ۲ ماه یک چک"},
{months:6,factor:1.33,count:3,kind:"سه فقره چک هر دو ماه",terms:"الباقی هر ۲ ماه یک چک"},
{months:8,factor:1.44,count:4,kind:"چهار فقره چک هر دو ماه",terms:"الباقی هر ۲ ماه یک چک"},
{months:10,factor:1.55,count:5,kind:"پنج فقره چک هر دو ماه",terms:"الباقی هر ۲ ماه یک چک"}
];
const $=id=>document.getElementById(id);
const numberFmt=new Intl.NumberFormat("en-US",{maximumFractionDigits:0});
const faFmt=new Intl.NumberFormat("fa-IR",{maximumFractionDigits:0});
const digits=val=>String(val??"").replace(/[۰-۹]/g,c=>String(c.charCodeAt(0)-1776)).replace(/[٠-٩]/g,c=>String(c.charCodeAt(0)-1632));
const parse=val=>{const cleaned=digits(val).replace(/[,\u066C\s]/g,"").replace(/[^\d.-]/g,"");return cleaned===""?null:Number(cleaned)};
const cash=n=>Number.isFinite(n)?numberFmt.format(Math.round(n)):"—";
const show=(id,n)=>$(id).textContent=cash(n);
const safe=(n)=>Math.max(0,Number.isFinite(n)?n:0);
const strToInput=n=>Number.isFinite(n)?cash(n):"";
let loan={price:null,advance:null}, checks={price:null,advance:null,percent:40};
const inputIds={loanPrice:["loan","price"],loanAdvance:["loan","advance"],checkPrice:["checks","price"],checkAdvance:["checks","advance"]};
function setInputs(){
for(const [id,[type,key]] of Object.entries(inputIds)){
$(id).value=strToInput((type==="loan"?loan:checks)[key]);
}
$("checkPercent").value=Number.isFinite(checks.percent)?Number(checks.percent.toFixed(2)):40;
}
function renderLoan(){
const price=loan.price, advance=loan.advance;
const ready=price!==null&&price>0;
const vat=ready?price*TAX:null;
const gross=ready?price+vat:null;
const balance=ready?Math.max(0,gross-safe(advance)):null;
show("loanVat",vat);show("loanGross",gross);show("loanBalance",balance);
$("loanRows").innerHTML=LOAN_TERMS.map(t=>`<tr><td>${faFmt.format(t.months)} ماهه</td><td class="num">${cash(balance===null?null:balance/1000000*t.perMillion)}</td><td>${t.guarantee}</td></tr>`).join("");
}
const constrainedPct = p => Math.min(100,Math.max(40,Number.isFinite(p)?p:40));
function renderChecks(){
const price=checks.price;
const ready=price!==null&&price>0;
const minimum=ready?Math.round(price*MIN_DOWN):null;
const entered=safe(checks.advance);
const applied=ready?Math.max(minimum,Math.min(price,entered)):null;
const vat=ready?price*TAX:null;
const balance=ready?Math.max(0,price+vat-applied):null;
const pct=ready?Math.round((applied/price)*100):constrainedPct(checks.percent);
show("checkMinimum",minimum);show("checkVat",vat);show("checkApplied",applied);show("checkBalance",balance);
$("downPct").textContent=faFmt.format(pct)+"٪";
$("downProgress").style.width=pct+"%";
$("advanceWarning").classList.toggle("show",ready&&checks.advance!==null&&checks.advance<minimum);
$("checkRows").innerHTML=CHECK_TERMS.map(t=>{
const total=balance===null?null:balance*t.factor;
return `<tr><td>${faFmt.format(t.months)} ماهه</td><td class="num">${cash(total)}</td><td class="num">${cash(total===null?null:total/t.count)}</td><td>${t.kind}</td><td>${t.terms}</td></tr>`;
}).join("");
}
function clampChecks(){
checks.percent=constrainedPct(checks.percent);
if(checks.price===null||checks.price<=0){checks.advance=null;return;}
checks.advance=Math.round(checks.price*checks.percent/100);
}
for(const [id,[which,key]] of Object.entries(inputIds)){
const el=$(id);
el.addEventListener("input",()=>{
const state=which==="loan"?loan:checks;
let n=parse(el.value);
if(n!==null&&(!Number.isFinite(n)||n<0))n=0;
state[key]=n;
if(which==="checks"&&key==="price"){
clampChecks();$("checkAdvance").value=strToInput(checks.advance);
}
if(which==="checks"&&key==="advance"&&checks.price>0){
checks.percent=(n===null?40:100*n/checks.price);
$("checkPercent").value=Number(constrainedPct(checks.percent).toFixed(2));
}
renderLoan();renderChecks();
});
el.addEventListener("blur",()=>{
if(which==="checks")clampChecks();
if(which==="loan"&&loan.price!==null){
loan.advance=Math.max(0,Math.min(safe(loan.advance),loan.price*(1+TAX)));
}
setInputs();renderLoan();renderChecks();
});
}
$("checkPercent").addEventListener("input",()=>{
const value=parse($("checkPercent").value);
checks.percent=value===null?40:value;
if(checks.price>0) checks.advance=Math.round(checks.price*constrainedPct(checks.percent)/100);
else checks.advance=null;
$("checkAdvance").value=strToInput(checks.advance);
renderChecks();
});
$("checkPercent").addEventListener("blur",()=>{
clampChecks();setInputs();renderChecks();
});
$("productPrice").addEventListener("blur",()=>{
const val=parse($("productPrice").value);
$("productPrice").value=val!==null&&Number.isFinite(val)&&val>=0?strToInput(val):"";
});
for(const [buttonId,mode] of [["copyLoanPrice","loan"],["copyCheckPrice","checks"]]){
$(buttonId).addEventListener("click",()=>{
const price=parse($("productPrice").value);
if(price===null||!Number.isFinite(price)||price<=0){$("productPrice").focus();return;}
if(mode==="loan")loan.price=price;
else {checks.price=price;clampChecks();}
setInputs();renderLoan();renderChecks();
$(mode==="loan"?"loanPrice":"checkPrice").focus();
});
}
$("resetBtn").addEventListener("click",()=>{
loan={price:null,advance:null}; checks={price:null,advance:null,percent:40};
for(const id of ["saleDate","productModel","productPrice","customerFullName","nationalId","customerAddress","customerPhone"])$(id).value="";
setInputs();renderLoan();renderChecks();
$("saleDate").focus();
});
function syncPrintFields(){
$("printPercent").textContent=faFmt.format(constrainedPct(checks.percent))+"٪";
for(const id of ["saleDate","productModel","productPrice","customerFullName","nationalId","customerPhone","customerAddress"]){
const input=$(id);
const display=document.querySelector('[data-print-for="'+id+'"]');
if(!display)continue;
let value=input.value.trim();
if(id==="productPrice"&&value){const n=parse(value);if(n!==null&&Number.isFinite(n)&&n>=0)value=cash(n);}
display.textContent=value||"—";
}
}
if(typeof window!=="undefined"){
window.addEventListener("beforeprint",()=>{
if(document.activeElement&&typeof document.activeElement.blur==="function")document.activeElement.blur();
syncPrintFields();
});
}
setInputs();renderLoan();renderChecks();
</script>
</body>
</html>