Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
68797e4fb5 | ||
|
|
af4ae1cbc3 | ||
|
|
c42abb05da | ||
|
|
4d1dc3b355 |
@@ -316,12 +316,12 @@ def attach_common(conn, resource, items):
|
|||||||
return items
|
return items
|
||||||
|
|
||||||
|
|
||||||
def monthly_finance(conn):
|
def monthly_finance(conn, tenant="科普·无界"):
|
||||||
data = []
|
data = []
|
||||||
for item in rows(conn, "SELECT DISTINCT month FROM finance_records ORDER BY month"):
|
for item in rows(conn, "SELECT DISTINCT month FROM finance_records WHERE tenant=? ORDER BY month", (tenant,)):
|
||||||
month = item["month"]
|
month = item["month"]
|
||||||
revenue = one(conn, "SELECT COALESCE(SUM(amount),0) AS v FROM finance_records WHERE month=? AND record_type='revenue'", (month,))["v"]
|
revenue = one(conn, "SELECT COALESCE(SUM(amount),0) AS v FROM finance_records WHERE month=? AND record_type='revenue' AND tenant=?", (month, tenant))["v"]
|
||||||
cost = one(conn, "SELECT COALESCE(SUM(amount),0) AS v FROM finance_records WHERE month=? AND record_type='cost_expense'", (month,))["v"]
|
cost = one(conn, "SELECT COALESCE(SUM(amount),0) AS v FROM finance_records WHERE month=? AND record_type='cost_expense' AND tenant=?", (month, tenant))["v"]
|
||||||
data.append({"month": month, "revenue": revenue, "gross_profit": revenue - cost, "cost_expense": cost, "net_profit": revenue - cost})
|
data.append({"month": month, "revenue": revenue, "gross_profit": revenue - cost, "cost_expense": cost, "net_profit": revenue - cost})
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@@ -333,13 +333,17 @@ def index():
|
|||||||
|
|
||||||
@app.route("/api/bootstrap")
|
@app.route("/api/bootstrap")
|
||||||
def bootstrap():
|
def bootstrap():
|
||||||
|
tenant = request.args.get("tenant", "科普·无界")
|
||||||
conn = db()
|
conn = db()
|
||||||
try:
|
try:
|
||||||
sales = attach_common(conn, "sales", rows(conn, "SELECT * FROM sales_leads ORDER BY id DESC"))
|
def q(sql, *args):
|
||||||
proposals = attach_common(conn, "proposals", rows(conn, "SELECT * FROM business_proposals ORDER BY id DESC"))
|
return rows(conn, sql, args)
|
||||||
operations = attach_common(conn, "operations", rows(conn, "SELECT * FROM operation_projects ORDER BY id DESC"))
|
sales = attach_common(conn, "sales", q("SELECT * FROM sales_leads WHERE tenant=? ORDER BY id DESC", tenant))
|
||||||
products = attach_common(conn, "products", rows(conn, "SELECT * FROM product_versions ORDER BY id DESC"))
|
proposals = attach_common(conn, "proposals", q("SELECT * FROM business_proposals WHERE tenant=? ORDER BY id DESC", tenant))
|
||||||
finance = rows(conn, "SELECT * FROM finance_records ORDER BY month DESC, id DESC")
|
operations = attach_common(conn, "operations", q("SELECT * FROM operation_projects WHERE tenant=? ORDER BY id DESC", tenant))
|
||||||
|
products = attach_common(conn, "products", q("SELECT * FROM product_versions WHERE tenant=? ORDER BY id DESC", tenant))
|
||||||
|
finance = q("SELECT * FROM finance_records WHERE tenant=? ORDER BY month DESC, id DESC", tenant)
|
||||||
|
tasks = q("SELECT * FROM project_tasks WHERE tenant=? ORDER BY phase, sort_order, id", tenant)
|
||||||
current_month = "2026-06"
|
current_month = "2026-06"
|
||||||
# Finance aggregates
|
# Finance aggregates
|
||||||
def sum_finance(months, rtype):
|
def sum_finance(months, rtype):
|
||||||
@@ -392,21 +396,21 @@ def bootstrap():
|
|||||||
"gross_q2": revenue_q2 - cost_q2,
|
"gross_q2": revenue_q2 - cost_q2,
|
||||||
"signed_not_executed": signed_not_executed,
|
"signed_not_executed": signed_not_executed,
|
||||||
},
|
},
|
||||||
"recent": rows(conn, "SELECT * FROM follow_up_records ORDER BY id DESC LIMIT 8"),
|
"recent": q("SELECT * FROM follow_up_records WHERE tenant=? ORDER BY id DESC LIMIT 8", tenant),
|
||||||
"risks": [{"title": "执行提醒", "content": x["next_action"]} for x in operations if x["next_action"]][:5],
|
"risks": [{"title": "执行提醒", "content": x["next_action"]} for x in operations if x["next_action"]][:5],
|
||||||
}
|
}
|
||||||
return jsonify({"summary": summary, "sales": sales, "proposals": proposals, "operations": operations, "products": products, "finance": finance, "financeMonthly": monthly_finance(conn), "tasks": rows(conn, "SELECT * FROM project_tasks ORDER BY phase, sort_order, id")})
|
return jsonify({"summary": summary, "sales": sales, "proposals": proposals, "operations": operations, "products": products, "finance": finance, "financeMonthly": monthly_finance(conn, tenant), "tasks": tasks, "tenant": tenant, "tenants": ["科普·无界","科研·无界","医患·无界"]})
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
TABLES = {
|
TABLES = {
|
||||||
"sales": ("sales_leads", ["target_customer", "priority", "status"]),
|
"sales": ("sales_leads", ["target_customer", "priority", "status", "tenant"]),
|
||||||
"proposals": ("business_proposals", ["customer_or_project_name", "version", "description", "status", "created_date"]),
|
"proposals": ("business_proposals", ["customer_or_project_name", "version", "description", "status", "created_date", "tenant"]),
|
||||||
"operations": ("operation_projects", ["project_name", "project_version", "project_type", "project_status", "current_stage", "owner", "target_customer", "customer_need", "expected_contract_amount", "expected_sign_date", "sign_probability", "next_action", "sop_stage", "execution_progress", "current_deliverable", "risks", "notes"]),
|
"operations": ("operation_projects", ["project_name", "project_version", "project_type", "project_status", "current_stage", "owner", "target_customer", "customer_need", "expected_contract_amount", "expected_sign_date", "sign_probability", "next_action", "sop_stage", "execution_progress", "current_deliverable", "risks", "notes", "tenant"]),
|
||||||
"products": ("product_versions", ["product_name", "version", "version_goal", "feature_list", "launch_date", "status", "platform", "notes"]),
|
"products": ("product_versions", ["product_name", "version", "version_goal", "feature_list", "launch_date", "status", "platform", "notes", "tenant"]),
|
||||||
"finance": ("finance_records", ["month", "project_name", "record_type", "category", "amount", "occurred_date", "notes"]),
|
"finance": ("finance_records", ["month", "project_name", "record_type", "category", "amount", "occurred_date", "notes", "tenant"]),
|
||||||
"tasks": ("project_tasks", ["project_id", "phase", "milestone", "task", "owner", "due_date", "blockers", "notes", "status", "sort_order"]),
|
"tasks": ("project_tasks", ["project_id", "phase", "milestone", "task", "owner", "due_date", "blockers", "notes", "status", "sort_order", "tenant"]),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const state = {
|
const state = {
|
||||||
active: "home",
|
active: "home",
|
||||||
data: null,
|
data: null,
|
||||||
|
tenant: "科普·无界",
|
||||||
opFilter: "all",
|
opFilter: "all",
|
||||||
projectView: null,
|
projectView: null,
|
||||||
chart: null,
|
chart: null,
|
||||||
@@ -48,7 +49,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 +230,27 @@ window.submitTaskForm = async (event, projectId) => {
|
|||||||
alert("保存失败:" + error.message);
|
alert("保存失败:" + error.message);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
window.createFinance = (event) => createResource(event, "finance");
|
window.createFinance = async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
const data = Object.fromEntries(new FormData(event.currentTarget).entries());
|
||||||
|
// Map form: month(date)→month(YYYY-MM), category→category, amount, tenant
|
||||||
|
data.month = data.month.substring(0, 7);
|
||||||
|
data.project_name = state.tenant;
|
||||||
|
data.tenant = state.tenant;
|
||||||
|
data.record_type = "revenue"; // default, will be adjusted by category
|
||||||
|
try {
|
||||||
|
await api("/api/finance", { method: "POST", body: JSON.stringify({ data }) });
|
||||||
|
await load();
|
||||||
|
} catch (error) {
|
||||||
|
alert("新增失败:" + error.message);
|
||||||
|
}
|
||||||
|
};
|
||||||
window.switchTab = switchTab;
|
window.switchTab = switchTab;
|
||||||
|
window.switchTenant = (tenant) => {
|
||||||
|
state.tenant = tenant;
|
||||||
|
state.projectView = null;
|
||||||
|
load();
|
||||||
|
};
|
||||||
|
|
||||||
function renderProjects() {
|
function renderProjects() {
|
||||||
// 二级页面:项目任务详情
|
// 二级页面:项目任务详情
|
||||||
@@ -247,18 +267,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>`;
|
||||||
@@ -404,17 +419,15 @@ 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 rows = state.data.finance.map((x) => [x.month, x.category, money(x.amount), text(x.notes)]);
|
||||||
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")}
|
${card(`<h2 class="mb-4 text-lg font-bold">收入、毛利、成本/费用、净利月度曲线</h2><div style="position:relative;height:300px"><canvas id="financeChart2"></canvas></div>`, "p-5")}
|
||||||
${card(formHtml([
|
${card(formHtml([
|
||||||
{ label: "月份", input: `<input name="month" required placeholder="YYYY-MM" pattern="\\d{4}-\\d{2}">` },
|
{ label: "日期", input: `<input name="month" type="date" required>` },
|
||||||
{ label: "类型", input: `<select name="record_type"><option value="revenue">收入</option><option value="cost_expense">成本/费用</option></select>` },
|
{ label: "类型", input: `<select name="category"><option>签单</option><option>确认收入</option><option>人力成本</option><option>费用</option><option>外部采购</option></select>` },
|
||||||
{ label: "分类", input: `<input name="category" required>` },
|
|
||||||
{ label: "金额/万", input: `<input name="amount" type="number" step="0.01" required>` },
|
{ label: "金额/万", input: `<input name="amount" type="number" step="0.01" required>` },
|
||||||
{ label: "发生日期", input: `<input name="occurred_date" type="date">` },
|
|
||||||
], { handler: "createFinance", text: "新增明细" }), "p-4")}
|
], { handler: "createFinance", text: "新增明细" }), "p-4")}
|
||||||
${renderTable(["月份", "类型", "分类", "金额", "发生日期", "备注"], rows)}
|
${renderTable(["日期", "类型", "金额", "备注"], rows)}
|
||||||
</div>`;
|
</div>`;
|
||||||
renderChartOn("financeChart2", state.data.financeMonthly);
|
renderChartOn("financeChart2", state.data.financeMonthly);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,9 +27,18 @@
|
|||||||
</head>
|
</head>
|
||||||
<body class="min-h-screen bg-slate-50 text-slate-950">
|
<body class="min-h-screen bg-slate-50 text-slate-950">
|
||||||
<header class="topbar border-b border-slate-200 bg-white px-8 py-5">
|
<header class="topbar border-b border-slate-200 bg-white px-8 py-5">
|
||||||
<div>
|
<div class="flex items-center gap-3">
|
||||||
<p class="eyebrow text-xs font-semibold uppercase tracking-[0.18em] text-blue-700">OPC Manager · 单用户 · 单项目</p>
|
<div>
|
||||||
<h1 class="mt-1 text-2xl font-semibold">科普(慰心斋)OPC 工作台</h1>
|
<p class="eyebrow text-xs font-semibold uppercase tracking-[0.18em] text-blue-700">OPC Manager</p>
|
||||||
|
<div class="flex items-center gap-3 mt-1">
|
||||||
|
<h1 class="text-2xl font-semibold">无界 OPC 工作台</h1>
|
||||||
|
<select id="tenantSelect" class="rounded-md border border-slate-200 bg-white px-3 py-1.5 text-sm font-medium text-slate-700 outline-none focus:border-blue-500" onchange="switchTenant(this.value)">
|
||||||
|
<option value="科普·无界">科普·无界</option>
|
||||||
|
<option value="科研·无界">科研·无界</option>
|
||||||
|
<option value="医患·无界">医患·无界</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button id="refreshBtn" class="rounded-md border border-slate-200 bg-white px-3 py-2 text-sm font-medium hover:bg-slate-50" type="button"><i data-lucide="refresh-cw"></i>刷新</button>
|
<button id="refreshBtn" class="rounded-md border border-slate-200 bg-white px-3 py-2 text-sm font-medium hover:bg-slate-50" type="button"><i data-lucide="refresh-cw"></i>刷新</button>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
Reference in New Issue
Block a user