|
|
|
@@ -1,7 +1,9 @@
|
|
|
|
const state = {
|
|
|
|
const state = {
|
|
|
|
active: "home",
|
|
|
|
active: "home",
|
|
|
|
data: null,
|
|
|
|
data: null,
|
|
|
|
|
|
|
|
tenant: "科普·无界",
|
|
|
|
opFilter: "all",
|
|
|
|
opFilter: "all",
|
|
|
|
|
|
|
|
finFilter: "已签单",
|
|
|
|
projectView: null,
|
|
|
|
projectView: null,
|
|
|
|
chart: null,
|
|
|
|
chart: null,
|
|
|
|
chart2: null,
|
|
|
|
chart2: null,
|
|
|
|
@@ -48,7 +50,7 @@ function renderTable(headers, rows, rowClicks) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function load() {
|
|
|
|
async function load() {
|
|
|
|
state.data = await api("/api/bootstrap");
|
|
|
|
state.data = await api(`/api/bootstrap?tenant=${encodeURIComponent(state.tenant)}`);
|
|
|
|
render();
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -229,8 +231,42 @@ window.submitTaskForm = async (event, projectId) => {
|
|
|
|
alert("保存失败:" + error.message);
|
|
|
|
alert("保存失败:" + error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
window.createFinance = (event) => createResource(event, "finance");
|
|
|
|
window.createFinance = async (event) => {
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const form = event.currentTarget;
|
|
|
|
|
|
|
|
const data = Object.fromEntries(new FormData(form).entries());
|
|
|
|
|
|
|
|
data.tenant = state.tenant;
|
|
|
|
|
|
|
|
data.sign_amount = parseFloat(data.sign_amount) || 0;
|
|
|
|
|
|
|
|
for (const m of ["2026-06","2026-07","2026-08","2026-09"]) {
|
|
|
|
|
|
|
|
const k = m.replace("-","_");
|
|
|
|
|
|
|
|
data["rev_"+k] = parseFloat(data["rev_"+k]) || 0;
|
|
|
|
|
|
|
|
data["gross_"+k] = parseFloat(data["gross_"+k]) || 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const pfId = data.pf_id;
|
|
|
|
|
|
|
|
delete data.pf_id;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (pfId) {
|
|
|
|
|
|
|
|
await api("/api/projectFinances/" + pfId, { method: "PUT", body: JSON.stringify({ data }) });
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
await api("/api/projectFinances", { method: "POST", body: JSON.stringify({ data }) });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
form.reset();
|
|
|
|
|
|
|
|
document.querySelector("#pf-id-input").value = "";
|
|
|
|
|
|
|
|
document.querySelector("#financeModalTitle").textContent = "新增项目财务";
|
|
|
|
|
|
|
|
closeFinanceModal();
|
|
|
|
|
|
|
|
await load();
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
alert("保存失败:" + error.message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
window.switchTab = switchTab;
|
|
|
|
window.switchTab = switchTab;
|
|
|
|
|
|
|
|
window.switchTenant = (tenant) => {
|
|
|
|
|
|
|
|
state.tenant = tenant;
|
|
|
|
|
|
|
|
state.projectView = null;
|
|
|
|
|
|
|
|
const label = tenant.replace("·无界", "");
|
|
|
|
|
|
|
|
document.querySelector("#workspaceTitle").textContent = label + " OPC 工作台";
|
|
|
|
|
|
|
|
load();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function renderProjects() {
|
|
|
|
function renderProjects() {
|
|
|
|
// 二级页面:项目任务详情
|
|
|
|
// 二级页面:项目任务详情
|
|
|
|
@@ -247,18 +283,13 @@ function renderProjects() {
|
|
|
|
`<button class="btn btn-ghost btn-sm text-blue-600" onclick="event.stopPropagation(); state.projectView=${x.id}; renderProjects()"><i data-lucide="eye"></i>查看</button>`
|
|
|
|
`<button class="btn btn-ghost btn-sm text-blue-600" onclick="event.stopPropagation(); state.projectView=${x.id}; renderProjects()"><i data-lucide="eye"></i>查看</button>`
|
|
|
|
]);
|
|
|
|
]);
|
|
|
|
document.querySelector("#projects").innerHTML = `<div class="grid gap-4">
|
|
|
|
document.querySelector("#projects").innerHTML = `<div class="grid gap-4">
|
|
|
|
<div class="flex items-center justify-end">
|
|
|
|
<div id="project-form">
|
|
|
|
<button class="btn btn-primary" onclick="document.querySelector('#project-form').classList.toggle('hidden')">
|
|
|
|
|
|
|
|
<i data-lucide="plus"></i>新增项目
|
|
|
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div id="project-form" class="hidden">
|
|
|
|
|
|
|
|
${card(formHtml([
|
|
|
|
${card(formHtml([
|
|
|
|
{ label: "项目名称", input: `<input name="project_name" required>` },
|
|
|
|
{ label: "项目名称", input: `<input name="project_name" required>` },
|
|
|
|
{ label: "当前阶段", input: `<select name="current_stage"><option>商务洽谈</option><option>系统上线</option><option>团队分工</option><option>项目交付</option><option>上线推广</option><option>结项验收</option></select>` },
|
|
|
|
{ label: "当前阶段", input: `<select name="current_stage"><option>商务洽谈</option><option>系统上线</option><option>团队分工</option><option>项目交付</option><option>上线推广</option><option>结项验收</option></select>` },
|
|
|
|
{ label: "项目金额", input: `<input name="expected_contract_amount" type="number" step="0.01" placeholder="万元">` },
|
|
|
|
{ label: "项目金额", input: `<input name="expected_contract_amount" type="number" step="0.01" placeholder="万元">` },
|
|
|
|
{ label: "负责人", input: `<input name="owner">` },
|
|
|
|
{ label: "负责人", input: `<input name="owner">` },
|
|
|
|
], { handler: "createOperation", text: "确认新增" }), "p-4")}
|
|
|
|
], { handler: "createOperation", text: "新增项目" }), "p-4")}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
${renderTable(["项目", "项目说明", "当前阶段", "项目金额", "负责人", "进展"], rows, items.map((x) => ({ resource: "operations", id: x.id })))}
|
|
|
|
${renderTable(["项目", "项目说明", "当前阶段", "项目金额", "负责人", "进展"], rows, items.map((x) => ({ resource: "operations", id: x.id })))}
|
|
|
|
</div>`;
|
|
|
|
</div>`;
|
|
|
|
@@ -283,11 +314,11 @@ function renderProjectTasks(projectId) {
|
|
|
|
${phases.map((phase) => {
|
|
|
|
${phases.map((phase) => {
|
|
|
|
const pt = tasks.filter((t) => t.phase === phase);
|
|
|
|
const pt = tasks.filter((t) => t.phase === phase);
|
|
|
|
if (!pt.length) return "";
|
|
|
|
if (!pt.length) return "";
|
|
|
|
return `<div class="task-group"><div class="task-group-hd"><span class="task-group-icon"><i data-lucide="layers"></i></span><span class="task-group-label">${phase}</span><span class="task-group-n">${pt.length}</span></div><div class="task-group-list">${pt.map((t) => `<div class="task-row" data-id="${t.id}" onclick="openTaskForm(${projectId}, ${t.id})"><span class="task-dot"><i data-lucide="${t.status === 'done' ? 'check-circle' : 'circle'}"></i></span><div class="task-main"><span class="task-name">${t.task}</span>${t.notes ? `<span class="task-desc">${t.notes}</span>` : ""}${t.blockers ? `<span class="task-blocker">⚠ ${t.blockers}</span>` : ""}</div><span class="task-col">${t.owner || ""}</span><span class="task-col-badge">${t.due_date || ""}</span></div>`).join("")}</div></div>`;
|
|
|
|
return `<div class="task-group"><div class="task-group-hd"><span class="task-group-icon"><i data-lucide="layers"></i></span><span class="task-group-label">${phase}</span><span class="task-group-n">${pt.length}</span></div><div class="task-group-list" data-phase="${phase}" ondrop="handleTaskDrop(event, ${projectId}, '${phase}')" ondragover="event.preventDefault(); event.currentTarget.classList.add('drag-over')" ondragleave="event.currentTarget.classList.remove('drag-over')">${pt.map((t) => `<div class="task-row ${t.status === 'done' ? 'task-done' : ''}" data-id="${t.id}" draggable="true" ondragstart="handleTaskDragStart(event, ${t.id})" ondragend="event.currentTarget.classList.remove('dragging')"><span class="task-dot" onclick="event.stopPropagation(); toggleTaskDone(${t.id}, ${projectId})"><i data-lucide="${t.status === 'done' ? 'check-circle' : 'circle'}"></i></span><span class="task-grip"><i data-lucide="grip-vertical"></i></span><div class="task-main" onclick="openTaskForm(${projectId}, ${t.id})"><span class="task-name">${t.task}</span>${t.notes ? `<span class="task-desc">${t.notes}</span>` : ""}${t.blockers ? `<span class="task-blocker">⚠ ${t.blockers}</span>` : ""}</div><span class="task-col">${t.owner || ""}</span><span class="task-col-badge">${t.due_date || ""}</span></div>`).join("")}</div></div>`;
|
|
|
|
}).join("")}
|
|
|
|
}).join("")}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div id="task-drawer-${projectId}" class="task-drawer">
|
|
|
|
<div id="task-drawer-${projectId}" class="task-drawer">
|
|
|
|
<div class="task-drawer-hd"><span class="task-drawer-title">编辑任务</span><button class="task-close" onclick="closeTaskDrawer(${projectId})"><i data-lucide="x"></i></button></div>
|
|
|
|
<div class="task-drawer-hd"><span class="task-drawer-title">编辑任务</span><div class="flex items-center gap-2"><button type="button" class="btn btn-ghost btn-sm text-red-600 hover:bg-red-50" onclick="deleteTask(${projectId})"><i data-lucide="trash-2"></i>删除</button><button class="task-close" onclick="closeTaskDrawer(${projectId})"><i data-lucide="x"></i></button></div></div>
|
|
|
|
<form class="task-drawer-form" onsubmit="submitTaskForm(event, ${projectId})">
|
|
|
|
<form class="task-drawer-form" onsubmit="submitTaskForm(event, ${projectId})">
|
|
|
|
<input type="hidden" name="task_id" id="task-id-${projectId}" value="">
|
|
|
|
<input type="hidden" name="task_id" id="task-id-${projectId}" value="">
|
|
|
|
<label class="task-field"><span>任务名称</span><input name="task" required id="task-name-${projectId}"></label>
|
|
|
|
<label class="task-field"><span>任务名称</span><input name="task" required id="task-name-${projectId}"></label>
|
|
|
|
@@ -404,19 +435,107 @@ function renderProducts() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderFinance() {
|
|
|
|
function renderFinance() {
|
|
|
|
const rows = state.data.finance.map((x) => [x.month, badge(x.record_type === "revenue" ? "收入" : "成本/费用"), x.category, money(x.amount), x.occurred_date, text(x.notes)]);
|
|
|
|
const pfs = state.data.projectFinances || [];
|
|
|
|
|
|
|
|
const ops = state.data.operations || [];
|
|
|
|
|
|
|
|
const fmTypesByTenant = {
|
|
|
|
|
|
|
|
"科普·无界": ["科普音频","科普视频","科普文章","全品类科普"],
|
|
|
|
|
|
|
|
"科研·无界": ["真实世界研究","调研问卷","病例征集","患者招募"],
|
|
|
|
|
|
|
|
"医患·无界": ["医患运营","患者管理","患教会","创新支付","电商","其他"],
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const fmTypes = fmTypesByTenant[state.tenant] || fmTypesByTenant["科普·无界"];
|
|
|
|
|
|
|
|
const tenantOps = (state.data.operations || []).filter(o => (o.project_name || "").includes(state.tenant.replace("·无界","")) || o.tenant === state.tenant);
|
|
|
|
|
|
|
|
const months = ["2026-06","2026-07","2026-08","2026-09"];
|
|
|
|
|
|
|
|
const monthLabels = ["6月","7月","8月","9月"];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Aggregates
|
|
|
|
|
|
|
|
const signed = pfs.filter(x => x.status === "已签单");
|
|
|
|
|
|
|
|
const pending = pfs.filter(x => x.status !== "已签单");
|
|
|
|
|
|
|
|
const sumSign = signed.reduce((s,x) => s + (x.sign_amount||0), 0);
|
|
|
|
|
|
|
|
const sumPending = pending.reduce((s,x) => s + (x.sign_amount||0), 0);
|
|
|
|
|
|
|
|
const monthRev = months.map(m => pfs.reduce((s,x) => s + (x["rev_"+m.replace("-","_")]||0), 0));
|
|
|
|
|
|
|
|
const monthGross = months.map(m => pfs.reduce((s,x) => s + (x["gross_"+m.replace("-","_")]||0), 0));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const renderPfRow = (pf) => {
|
|
|
|
|
|
|
|
const mCols = months.map(m => {
|
|
|
|
|
|
|
|
const rev = pf["rev_"+m.replace("-","_")] || 0;
|
|
|
|
|
|
|
|
const gross = pf["gross_"+m.replace("-","_")] || 0;
|
|
|
|
|
|
|
|
return `<td class="p-2 text-right whitespace-nowrap"><span class="${rev ? 'text-blue-700 font-medium' : 'text-slate-300'}">${rev ? money(rev) : '—'}</span><br><span class="text-xs ${gross ? 'text-green-600' : 'text-slate-300'}">${gross ? money(gross) : '—'}</span></td>`;
|
|
|
|
|
|
|
|
}).join("");
|
|
|
|
|
|
|
|
const totalRev = months.reduce((s,m) => s + (pf["rev_"+m.replace("-","_")]||0), 0);
|
|
|
|
|
|
|
|
const totalGross = months.reduce((s,m) => s + (pf["gross_"+m.replace("-","_")]||0), 0);
|
|
|
|
|
|
|
|
const totalCol = `<td class="p-2 text-right whitespace-nowrap font-semibold"><span class="${totalRev ? 'text-blue-700' : 'text-slate-300'}">${totalRev ? money(totalRev) : '—'}</span><br><span class="text-xs ${totalGross ? 'text-green-600' : 'text-slate-300'}">${totalGross ? money(totalGross) : '—'}</span></td>`;
|
|
|
|
|
|
|
|
return `<tr class="border-b border-slate-100 hover:bg-slate-50 cursor-pointer" onclick="openPfEditModal(${pf.id})"><td class="p-2 text-sm font-medium">${pf.customer_name}</td><td class="p-2 text-sm">${pf.business_type}</td><td class="p-2 text-sm">${pf.status === "已签单" ? badge("已签") : badge(pf.status,"amber")}</td><td class="p-2 text-right text-sm">${money(pf.sign_amount)}</td>${mCols}${totalCol}<td class="p-2 text-sm text-slate-500">${pf.sales_person || ""}</td></tr>`;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
document.querySelector("#finance").innerHTML = `<div class="grid gap-4">
|
|
|
|
document.querySelector("#finance").innerHTML = `<div class="grid gap-4">
|
|
|
|
${card(`<h2 class="mb-4 text-lg font-bold">收入、毛利、成本/费用、净利月度曲线</h2><div style="position:relative;height:300px"><canvas id="financeChart2"></canvas></div>`, "p-5")}
|
|
|
|
<div class="grid grid-cols-6 gap-3">
|
|
|
|
${card(formHtml([
|
|
|
|
${[["已签项目","" + signed.length],["签约金额",money(sumSign)],["待签项目","" + pending.length],["待签金额",money(sumPending)],["本月确收",money(monthRev[0])],["本月毛利",money(monthGross[0])]].map(([l,v]) => `<div class="bg-white rounded-lg border border-slate-200 p-3 text-center"><p class="text-xs text-slate-500">${l}</p><p class="text-xl font-bold text-slate-800">${v}</p></div>`).join("")}
|
|
|
|
{ label: "月份", input: `<input name="month" required placeholder="YYYY-MM" pattern="\\d{4}-\\d{2}">` },
|
|
|
|
</div>
|
|
|
|
{ label: "类型", input: `<select name="record_type"><option value="revenue">收入</option><option value="cost_expense">成本/费用</option></select>` },
|
|
|
|
${card(`<div class="flex items-center justify-between cursor-pointer" onclick="toggleFinanceChart()"><h2 class="text-lg font-bold text-slate-700">月度趋势</h2><i data-lucide="chevron-down" class="transition-transform rotate-90" id="financeChartIcon"></i></div><div id="financeChartWrap" class="hidden mt-4"><div style="position:relative;height:300px"><canvas id="financeChart2"></canvas></div></div>`, "p-5")}
|
|
|
|
{ label: "分类", input: `<input name="category" required>` },
|
|
|
|
|
|
|
|
{ label: "金额/万", input: `<input name="amount" type="number" step="0.01" required>` },
|
|
|
|
<div class="flex justify-end"><button class="btn btn-primary btn-sm" onclick="openFinanceModal()"><i data-lucide="plus"></i>新增财务项目</button></div>
|
|
|
|
{ label: "发生日期", input: `<input name="occurred_date" type="date">` },
|
|
|
|
<div id="financeModal" class="hidden fixed inset-0 z-50 flex items-center justify-center bg-black/40" onclick="closeFinanceModal()"><div class="bg-white rounded-2xl shadow-2xl w-full max-w-5xl mx-4 max-h-[92vh] overflow-y-auto" onclick="event.stopPropagation()"><div class="sticky top-0 z-10 bg-white/95 backdrop-blur border-b border-slate-100 px-8 py-5 flex items-center justify-between"><div><h3 class="text-xl font-bold text-slate-800" id="financeModalTitle">新增项目财务</h3><p class="text-xs text-slate-400 mt-0.5">填写项目财务信息,标记月度确收与毛利预估</p></div><button class="btn btn-ghost btn-sm rounded-full w-8 h-8 p-0" onclick="closeFinanceModal()"><i data-lucide="x"></i></button></div><form onsubmit="createFinance(event)" class="p-8 grid gap-6"><input type="hidden" name="pf_id" id="pf-id-input" value=""><div class="grid grid-cols-2 gap-6"><div class="bg-slate-50 rounded-xl p-5"><h4 class="text-sm font-semibold text-slate-500 mb-4 flex items-center gap-2"><i data-lucide="briefcase-business"></i>基本信息</h4><div class="grid gap-4"><label class="block"><span class="text-xs font-medium text-slate-500">项目</span><select name="project_id" class="mt-1 block w-full rounded-lg border border-slate-200 px-3 py-2.5 text-sm bg-white">${tenantOps.map(o => `<option value="${o.id}">${o.project_name}</option>`).join("")}</select></label><label class="block"><span class="text-xs font-medium text-slate-500">业务类型</span><select name="business_type" class="mt-1 block w-full rounded-lg border border-slate-200 px-3 py-2.5 text-sm bg-white">${fmTypes.map(t => `<option>${t}</option>`).join("")}</select></label><label class="block"><span class="text-xs font-medium text-slate-500">客户名称</span><input name="customer_name" class="mt-1 block w-full rounded-lg border border-slate-200 px-3 py-2.5 text-sm"></label></div></div><div class="bg-slate-50 rounded-xl p-5"><h4 class="text-sm font-semibold text-slate-500 mb-4 flex items-center gap-2"><i data-lucide="banknote"></i>签约信息</h4><div class="grid gap-4"><label class="block"><span class="text-xs font-medium text-slate-500">签约金额(万元)</span><input name="sign_amount" type="number" step="0.01" class="mt-1 block w-full rounded-lg border border-slate-200 px-3 py-2.5 text-sm" placeholder="0.00"></label><label class="block"><span class="text-xs font-medium text-slate-500">签约月份</span><input name="sign_month" class="mt-1 block w-full rounded-lg border border-slate-200 px-3 py-2.5 text-sm" placeholder="如 2026-06"></label><label class="block"><span class="text-xs font-medium text-slate-500">项目状态</span><select name="status" class="mt-1 block w-full rounded-lg border border-slate-200 px-3 py-2.5 text-sm bg-white"><option>已签单</option><option>待签</option></select></label></div></div></div><div class="bg-slate-50 rounded-xl p-5"><h4 class="text-sm font-semibold text-slate-500 mb-4 flex items-center gap-2"><i data-lucide="calendar"></i>月度确收 & 毛利预估(万元)</h4><div class="grid grid-cols-4 gap-3">${["06","07","08","09"].map(m => `<div class="bg-white rounded-lg border border-slate-200 p-3 text-center"><p class="text-xs font-semibold text-slate-400 mb-2">${m}月</p><div class="grid grid-cols-2 gap-2"><div><p class="text-[10px] text-slate-400 mb-0.5">确收</p><input name="rev_2026_${m}" type="number" step="0.01" class="w-full rounded-md border border-slate-200 px-2 py-1.5 text-sm text-center" placeholder="—"></div><div><p class="text-[10px] text-slate-400 mb-0.5">毛利</p><input name="gross_2026_${m}" type="number" step="0.01" class="w-full rounded-md border border-slate-200 px-2 py-1.5 text-sm text-center" placeholder="—"></div></div></div>`).join("")}</div><div class="grid grid-cols-2 gap-3 mt-3"><div class="bg-white rounded-lg border border-slate-200 p-3 text-center"><p class="text-xs font-semibold text-slate-400 mb-1">总确收</p><input name="total_rev" id="total-rev-display" class="w-full rounded-md border border-slate-200 px-2 py-1.5 text-sm text-center font-bold text-blue-700" readonly placeholder="自动计算"></div><div class="bg-white rounded-lg border border-slate-200 p-3 text-center"><p class="text-xs font-semibold text-slate-400 mb-1">总毛利</p><input name="total_gross" id="total-gross-display" class="w-full rounded-md border border-slate-200 px-2 py-1.5 text-sm text-center font-bold text-green-600" readonly placeholder="自动计算"></div></div></div><div class="flex justify-end gap-3 pt-2"><button type="button" class="btn btn-ghost btn-sm px-6" onclick="closeFinanceModal()">取消</button><button type="submit" class="btn btn-primary btn-sm px-8">保存</button></div></form></div></div>
|
|
|
|
], { handler: "createFinance", text: "新增明细" }), "p-4")}
|
|
|
|
|
|
|
|
${renderTable(["月份", "类型", "分类", "金额", "发生日期", "备注"], rows)}
|
|
|
|
${card(`<h3 class="font-bold text-slate-700 mb-3">项目明细 <span class="text-slate-400 font-normal">(${pfs.length})</span></h3><div class="flex gap-2 mb-3">${[["已签单","已签"],["待签","待签"]].map(([k,v]) => `<button class="btn btn-sm ${state.finFilter === k ? 'btn-primary' : 'btn-ghost'}" onclick="state.finFilter='${k}';renderFinance()">${v} (${pfs.filter(x=>x.status===k).length})</button>`).join("")}</div><div class="overflow-x-auto"><table class="w-full text-sm"><thead><tr class="bg-slate-50 border-b border-slate-200"><th class="p-2 text-left font-semibold">客户</th><th class="p-2 text-left font-semibold">类型</th><th class="p-2 text-left font-semibold">状态</th><th class="p-2 text-right font-semibold">签约金额</th>${monthLabels.map(l => `<th class="p-2 text-center font-semibold">${l}<br><span class="text-xs text-slate-400">确收/毛利</span></th>`).join("")}<th class="p-2 text-center font-semibold">总计<br><span class="text-xs text-slate-400">确收/毛利</span></th><th class="p-2 text-left font-semibold">销售</th></tr></thead><tbody>${pfs.filter(x => x.status === state.finFilter).map(renderPfRow).join("")}</tbody></table></div>`, "p-4")}
|
|
|
|
</div>`;
|
|
|
|
</div>`;
|
|
|
|
renderChartOn("financeChart2", state.data.financeMonthly);
|
|
|
|
if (window.lucide) window.lucide.createIcons();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
window.openFinanceModal = () => {
|
|
|
|
|
|
|
|
const modal = document.querySelector("#financeModal");
|
|
|
|
|
|
|
|
modal.classList.remove("hidden");
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
window.closeFinanceModal = () => {
|
|
|
|
|
|
|
|
const modal = document.querySelector("#financeModal");
|
|
|
|
|
|
|
|
modal.classList.add("hidden");
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
window.openPfEditModal = (pfId) => {
|
|
|
|
|
|
|
|
const pf = (state.data.projectFinances || []).find(x => x.id === pfId);
|
|
|
|
|
|
|
|
if (!pf) return;
|
|
|
|
|
|
|
|
document.querySelector("#pf-id-input").value = pf.id;
|
|
|
|
|
|
|
|
document.querySelector("#financeModalTitle").textContent = "编辑项目财务";
|
|
|
|
|
|
|
|
const form = document.querySelector("#financeModal form");
|
|
|
|
|
|
|
|
form.querySelector('[name="project_id"]').value = pf.project_id || "";
|
|
|
|
|
|
|
|
form.querySelector('[name="business_type"]').value = pf.business_type || "";
|
|
|
|
|
|
|
|
form.querySelector('[name="customer_name"]').value = pf.customer_name || "";
|
|
|
|
|
|
|
|
form.querySelector('[name="sign_amount"]').value = pf.sign_amount || "";
|
|
|
|
|
|
|
|
form.querySelector('[name="sign_month"]').value = pf.sign_month || "";
|
|
|
|
|
|
|
|
form.querySelector('[name="status"]').value = pf.status || "已签单";
|
|
|
|
|
|
|
|
for (const m of ["06","07","08","09"]) {
|
|
|
|
|
|
|
|
form.querySelector('[name="rev_2026_' + m + '"]').value = pf["rev_2026_" + m] || "";
|
|
|
|
|
|
|
|
form.querySelector('[name="gross_2026_' + m + '"]').value = pf["gross_2026_" + m] || "";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate totals
|
|
|
|
|
|
|
|
const totalRev = (pf.rev_2026_06||0) + (pf.rev_2026_07||0) + (pf.rev_2026_08||0) + (pf.rev_2026_09||0);
|
|
|
|
|
|
|
|
const totalGross = (pf.gross_2026_06||0) + (pf.gross_2026_07||0) + (pf.gross_2026_08||0) + (pf.gross_2026_09||0);
|
|
|
|
|
|
|
|
document.querySelector("#total-rev-display").value = money(totalRev);
|
|
|
|
|
|
|
|
document.querySelector("#total-gross-display").value = money(totalGross);
|
|
|
|
|
|
|
|
openFinanceModal();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
window.toggleFinanceChart = () => {
|
|
|
|
|
|
|
|
const wrap = document.querySelector("#financeChartWrap");
|
|
|
|
|
|
|
|
const icon = document.querySelector("#financeChartIcon");
|
|
|
|
|
|
|
|
if (!wrap) return;
|
|
|
|
|
|
|
|
wrap.classList.toggle("hidden");
|
|
|
|
|
|
|
|
icon.classList.toggle("rotate-90");
|
|
|
|
|
|
|
|
if (!wrap.classList.contains("hidden") && !state.chart2) renderFinanceChart();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
function renderFinanceChart() {
|
|
|
|
|
|
|
|
const { financeMonthly } = state.data;
|
|
|
|
|
|
|
|
const canvas = document.querySelector("#financeChart2");
|
|
|
|
|
|
|
|
if (!canvas || !window.Chart) return;
|
|
|
|
|
|
|
|
if (state.chart2) state.chart2.destroy();
|
|
|
|
|
|
|
|
state.chart2 = new Chart(canvas, {
|
|
|
|
|
|
|
|
type: "line",
|
|
|
|
|
|
|
|
data: {
|
|
|
|
|
|
|
|
labels: financeMonthly.map((x) => x.month),
|
|
|
|
|
|
|
|
datasets: [
|
|
|
|
|
|
|
|
{ label: "月度确收", data: financeMonthly.map((x) => x.revenue), borderColor: "#2563eb", tension: 0.3 },
|
|
|
|
|
|
|
|
{ label: "月度毛利", data: financeMonthly.map((x) => x.net_profit), borderColor: "#059669", tension: 0.3 },
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { position: "bottom", labels: { boxWidth: 12, font: { size: 11 } } } }, scales: { x: { ticks: { font: { size: 11 } }, grid: { display: false } }, y: { ticks: { font: { size: 11 }, callback: (v) => v + "万" } } } },
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function renderChartOn(id, data) {
|
|
|
|
function renderChartOn(id, data) {
|
|
|
|
@@ -474,7 +593,7 @@ function openDrawer(resource, id) {
|
|
|
|
const multilineFields = ["customer_need", "current_deliverable", "risks", "next_action", "version_goal", "feature_list", "notes"];
|
|
|
|
const multilineFields = ["customer_need", "current_deliverable", "risks", "next_action", "version_goal", "feature_list", "notes"];
|
|
|
|
const followupTarget = resource === "sales" ? "sales" : resource === "proposals" ? "proposal" : resource === "operations" ? "operation" : resource === "products" ? "product" : "";
|
|
|
|
const followupTarget = resource === "sales" ? "sales" : resource === "proposals" ? "proposal" : resource === "operations" ? "operation" : resource === "products" ? "product" : "";
|
|
|
|
const title = item.target_customer || item.project_name || item.customer_or_project_name || item.product_name;
|
|
|
|
const title = item.target_customer || item.project_name || item.customer_or_project_name || item.product_name;
|
|
|
|
drawer.innerHTML = `<div class="drawer-panel"><div class="sticky top-0 z-10 flex items-center justify-between border-b border-slate-200 bg-white/95 px-5 py-3 backdrop-blur"><div><p class="text-[10px] font-semibold uppercase tracking-[0.18em] text-slate-400">Detail Drawer</p><div class="flex items-center gap-2"><h2 class="drawer-title text-[17px] font-semibold leading-6 text-slate-900">${title}</h2><span id="drawerSaveStatus" class="save-status"></span></div></div><div class="flex items-center gap-2"><button class="btn btn-ghost btn-sm text-red-600 hover:bg-red-50" onclick="deleteOperation(${id})"><i data-lucide="trash-2"></i>删除</button><button class="btn btn-ghost btn-sm" onclick="closeDrawer()">关闭</button></div></div><div class="grid gap-5 p-5">
|
|
|
|
drawer.innerHTML = `<div class="drawer-panel"><div class="sticky top-0 z-10 flex items-center justify-between border-b border-slate-200 bg-white/95 px-5 py-3 backdrop-blur"><div><p class="text-[10px] font-semibold uppercase tracking-[0.18em] text-slate-400">Detail Drawer</p><div class="flex items-center gap-2"><h2 class="drawer-title text-[17px] font-semibold leading-6 text-slate-900">${title}</h2><span id="drawerSaveStatus" class="save-status"></span></div></div><div class="flex items-center gap-2"><button class="btn btn-ghost btn-sm text-red-600 hover:bg-red-50" onclick="deleteDrawerItem('${resource}', ${id})"><i data-lucide="trash-2"></i>删除</button><button class="btn btn-ghost btn-sm" onclick="closeDrawer()">关闭</button></div></div><div class="grid gap-5 p-5">
|
|
|
|
<section>
|
|
|
|
<section>
|
|
|
|
<h3 class="drawer-section-title">属性</h3>
|
|
|
|
<h3 class="drawer-section-title">属性</h3>
|
|
|
|
<form id="drawerForm" class="drawer-fields">
|
|
|
|
<form id="drawerForm" class="drawer-fields">
|
|
|
|
@@ -576,16 +695,79 @@ function bindDrawerAutosave(resource, id, item) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
window.openDrawer = openDrawer;
|
|
|
|
window.openDrawer = openDrawer;
|
|
|
|
window.deleteOperation = async (id) => {
|
|
|
|
window.deleteDrawerItem = async (resource, id) => {
|
|
|
|
if (!confirm("确认删除该项目?此操作不可撤销。")) return;
|
|
|
|
if (!confirm("确认删除?此操作不可撤销。")) return;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
await api(`/api/operations/${id}`, { method: "DELETE" });
|
|
|
|
await api(`/api/${resource}/${id}`, { method: "DELETE" });
|
|
|
|
closeDrawer();
|
|
|
|
closeDrawer();
|
|
|
|
await load();
|
|
|
|
await load();
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
|
alert("删除失败:" + error.message);
|
|
|
|
alert("删除失败:" + error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
window.toggleTaskDone = async (taskId, projectId) => {
|
|
|
|
|
|
|
|
const task = (state.data.tasks || []).find((t) => t.id === taskId);
|
|
|
|
|
|
|
|
if (!task) return;
|
|
|
|
|
|
|
|
const newStatus = task.status === "done" ? "" : "done";
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
await api(`/api/tasks/${taskId}`, { method: "PUT", body: JSON.stringify({ data: { status: newStatus } }) });
|
|
|
|
|
|
|
|
await load();
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
alert("更新失败:" + error.message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
window.deleteTask = async (projectId) => {
|
|
|
|
|
|
|
|
const taskId = document.querySelector(`#task-id-${projectId}`).value;
|
|
|
|
|
|
|
|
if (!taskId) return;
|
|
|
|
|
|
|
|
if (!confirm("确认删除该任务?此操作不可撤销。")) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
await api(`/api/tasks/${taskId}`, { method: "DELETE" });
|
|
|
|
|
|
|
|
closeTaskDrawer(projectId);
|
|
|
|
|
|
|
|
await load();
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
alert("删除失败:" + error.message);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let dragTaskId = null;
|
|
|
|
|
|
|
|
window.handleTaskDragStart = (event, taskId) => {
|
|
|
|
|
|
|
|
dragTaskId = taskId;
|
|
|
|
|
|
|
|
event.currentTarget.classList.add("dragging");
|
|
|
|
|
|
|
|
event.dataTransfer.effectAllowed = "move";
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
window.handleTaskDrop = async (event, projectId, phase) => {
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
event.currentTarget.classList.remove("drag-over");
|
|
|
|
|
|
|
|
const target = event.currentTarget;
|
|
|
|
|
|
|
|
if (!dragTaskId) return;
|
|
|
|
|
|
|
|
// Find the dragged element and insert after the nearest task
|
|
|
|
|
|
|
|
const dragged = document.querySelector(`.task-row[data-id="${dragTaskId}"]`);
|
|
|
|
|
|
|
|
if (!dragged) return;
|
|
|
|
|
|
|
|
const afterElement = getDragAfterElement(target, event.clientY);
|
|
|
|
|
|
|
|
if (afterElement) {
|
|
|
|
|
|
|
|
target.insertBefore(dragged, afterElement);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
target.appendChild(dragged);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dragged.classList.remove("dragging");
|
|
|
|
|
|
|
|
// Update sort_order in DB
|
|
|
|
|
|
|
|
const rows = [...target.querySelectorAll(".task-row")];
|
|
|
|
|
|
|
|
const updates = rows.map((row, i) => ({ id: parseInt(row.dataset.id), sort_order: i }));
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
await api(`/api/tasks/batch-sort`, { method: "POST", body: JSON.stringify({ items: updates }) });
|
|
|
|
|
|
|
|
} catch (e) { /* non-critical */ }
|
|
|
|
|
|
|
|
dragTaskId = null;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
function getDragAfterElement(container, y) {
|
|
|
|
|
|
|
|
const elements = [...container.querySelectorAll(".task-row:not(.dragging)")];
|
|
|
|
|
|
|
|
return elements.reduce((closest, child) => {
|
|
|
|
|
|
|
|
const box = child.getBoundingClientRect();
|
|
|
|
|
|
|
|
const offset = y - box.top - box.height / 2;
|
|
|
|
|
|
|
|
if (offset < 0 && offset > closest.offset) {
|
|
|
|
|
|
|
|
return { offset, element: child };
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return closest;
|
|
|
|
|
|
|
|
}, { offset: Number.NEGATIVE_INFINITY }).element;
|
|
|
|
|
|
|
|
}
|
|
|
|
window.closeDrawer = () => document.querySelector("#drawer").classList.remove("open");
|
|
|
|
window.closeDrawer = () => document.querySelector("#drawer").classList.remove("open");
|
|
|
|
window.squireInstances = {};
|
|
|
|
window.squireInstances = {};
|
|
|
|
window.squireCmd = (cmd) => {
|
|
|
|
window.squireCmd = (cmd) => {
|
|
|
|
|