Files
arrr-erp/app/modules/clients/templates/clients/business_structure.html
T
2026-09-03 15:57:05 +05:30

257 lines
21 KiB
HTML

{% extends "ui/templates/base/layout.html" %}
{% block content %}
<div class="space-y-6">
<div class="flex flex-wrap items-center justify-between gap-3">
<div><h2 class="text-xl font-semibold text-slate-900">{{ client.client_name }} — Business Structure</h2>
<p class="text-sm text-slate-500">Maintain Business Units, Client Branches and statutory registrations under one PAN/legal client.</p></div>
<a href="/clients/{{ client.id }}" class="rounded-xl border border-slate-300 px-4 py-2 text-sm">Back to Client</a>
</div>
{% if request.query_params.get('message') %}
<div class="rounded-xl border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm text-emerald-800">{{ request.query_params.get('message') }}</div>
{% endif %}
{% if request.query_params.get('error') %}
<div class="rounded-xl border border-rose-200 bg-rose-50 px-4 py-3 text-sm text-rose-800">{{ request.query_params.get('error') }}</div>
{% endif %}
{% if can_edit %}
<div class="grid gap-5 xl:grid-cols-3">
<form id="add-business" data-quick-action="business" method="post" action="/clients/{{ client.id }}/business-units" class="rounded-2xl bg-white p-5 shadow-soft space-y-3">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<h3 class="font-semibold text-slate-900">Add Business Unit</h3>
<input name="business_code" required placeholder="Business code" class="w-full rounded-xl border px-3 py-2 text-sm">
<input name="business_name" required placeholder="Business unit name" class="w-full rounded-xl border px-3 py-2 text-sm">
<input name="trade_name" placeholder="Trade name" class="w-full rounded-xl border px-3 py-2 text-sm">
<input name="nature_of_business" placeholder="Nature of business" class="w-full rounded-xl border px-3 py-2 text-sm">
<label class="flex gap-2 text-sm"><input type="checkbox" name="is_primary"> Primary Business Unit</label>
<button class="rounded-xl bg-brand-600 px-4 py-2 text-sm text-white">Add Business Unit</button>
</form>
<form id="add-branch" data-quick-action="branch" method="post" action="/clients/{{ client.id }}/branches" class="rounded-2xl bg-white p-5 shadow-soft space-y-3">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<h3 class="font-semibold text-slate-900">Add Client Branch</h3>
<select name="business_unit_id" required class="w-full rounded-xl border px-3 py-2 text-sm">
<option value="">Select Business Unit</option>
{% for row in businesses if row.is_active %}<option value="{{ row.id }}" {% if request.query_params.get('business_unit_id') == row.id|string %}selected{% endif %}>{{ row.business_name }}</option>{% endfor %}
</select>
<input name="branch_code" required placeholder="Branch code" class="w-full rounded-xl border px-3 py-2 text-sm">
<input name="branch_name" required placeholder="Client Branch name" class="w-full rounded-xl border px-3 py-2 text-sm">
<select name="branch_type" class="w-full rounded-xl border px-3 py-2 text-sm">
<option value="head_office">Head Office</option><option value="branch">Branch</option>
<option value="warehouse">Warehouse</option><option value="unit">Unit</option>
</select>
<div class="grid grid-cols-3 gap-2"><input name="city" placeholder="City" class="rounded-xl border px-3 py-2 text-sm"><input name="state" placeholder="State" class="rounded-xl border px-3 py-2 text-sm"><input name="pincode" placeholder="Pincode" class="rounded-xl border px-3 py-2 text-sm"></div>
<label class="flex gap-2 text-sm"><input type="checkbox" name="is_primary"> Primary Branch</label>
<button class="rounded-xl bg-brand-600 px-4 py-2 text-sm text-white">Add Client Branch</button>
</form>
<form id="add-registration" data-quick-action="registration" method="post" action="/clients/{{ client.id }}/registrations" class="rounded-2xl bg-white p-5 shadow-soft space-y-3">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<h3 class="font-semibold text-slate-900">Add Registration</h3>
<select name="business_unit_id" class="w-full rounded-xl border px-3 py-2 text-sm"><option value="">Client level / derive from Branch</option>{% for row in businesses if row.is_active %}<option value="{{ row.id }}" {% if request.query_params.get('business_unit_id') == row.id|string %}selected{% endif %}>{{ row.business_name }}</option>{% endfor %}</select>
<select name="client_branch_id" class="w-full rounded-xl border px-3 py-2 text-sm"><option value="">No Client Branch</option>{% for row in branches if row.is_active %}<option value="{{ row.id }}" {% if request.query_params.get('client_branch_id') == row.id|string %}selected{% endif %}>{{ row.branch_name }}</option>{% endfor %}</select>
<select name="registration_type_id" required class="w-full rounded-xl border px-3 py-2 text-sm">
<option value="">Select Registration Type</option>
{% for row in registration_types if row.is_active %}<option value="{{ row.id }}">{{ row.name }} ({{ row.code }})</option>{% endfor %}
</select>
<input name="registration_number" required placeholder="Registration number" class="w-full rounded-xl border px-3 py-2 text-sm">
<input name="legal_name" placeholder="Legal name" class="w-full rounded-xl border px-3 py-2 text-sm">
<input name="trade_name" placeholder="Trade / deductor unit name" class="w-full rounded-xl border px-3 py-2 text-sm">
<input name="state" placeholder="State" class="w-full rounded-xl border px-3 py-2 text-sm">
<button class="rounded-xl bg-brand-600 px-4 py-2 text-sm text-white">Add Registration</button>
</form>
</div>
{% endif %}
{% if can_edit and request.query_params.get('edit_business') %}
{% for edit_row in businesses if edit_row.id|string == request.query_params.get('edit_business') %}
<section id="edit-business" class="rounded-2xl border border-brand-200 bg-white p-5 shadow-soft">
<div class="mb-4 flex items-center justify-between gap-3">
<div><h3 class="font-semibold text-slate-900">Edit Business Unit</h3><p class="text-xs text-slate-500">Updates this existing Business Unit; linked records remain attached.</p></div>
<a href="/clients/{{ client.id }}/business-structure#business-units" class="rounded-lg border px-3 py-1.5 text-xs">Cancel</a>
</div>
<form method="post" action="/clients/{{ client.id }}/business-units/{{ edit_row.id }}/edit" class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<input name="business_code" value="{{ edit_row.business_code }}" required placeholder="Business code" class="rounded-xl border px-3 py-2 text-sm">
<input name="business_name" value="{{ edit_row.business_name }}" required placeholder="Business unit name" class="rounded-xl border px-3 py-2 text-sm">
<input name="trade_name" value="{{ edit_row.trade_name or '' }}" placeholder="Trade name" class="rounded-xl border px-3 py-2 text-sm">
<input name="nature_of_business" value="{{ edit_row.nature_of_business or '' }}" placeholder="Nature of business" class="rounded-xl border px-3 py-2 text-sm">
<label class="flex items-center gap-2 text-sm"><input type="checkbox" name="is_primary" {% if edit_row.is_primary %}checked{% endif %}> Primary Business Unit</label>
<div class="md:col-span-2 xl:col-span-3"><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white">Save Business Changes</button></div>
</form>
</section>
{% endfor %}
{% endif %}
{% if can_edit and request.query_params.get('edit_branch') %}
{% for edit_row in branches if edit_row.id|string == request.query_params.get('edit_branch') %}
<section id="edit-branch" class="rounded-2xl border border-brand-200 bg-white p-5 shadow-soft">
<div class="mb-4 flex items-center justify-between gap-3">
<div><h3 class="font-semibold text-slate-900">Edit Client Branch</h3><p class="text-xs text-slate-500">Updates this existing branch without changing its record ID.</p></div>
<a href="/clients/{{ client.id }}/business-structure#client-branches" class="rounded-lg border px-3 py-1.5 text-xs">Cancel</a>
</div>
<form method="post" action="/clients/{{ client.id }}/branches/{{ edit_row.id }}/edit" class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<select name="business_unit_id" required class="rounded-xl border px-3 py-2 text-sm">
{% for business in businesses %}<option value="{{ business.id }}" {% if business.id == edit_row.business_unit_id %}selected{% endif %}>{{ business.business_name }}{% if not business.is_active %} (Inactive){% endif %}</option>{% endfor %}
</select>
<input name="branch_code" value="{{ edit_row.branch_code }}" required placeholder="Branch code" class="rounded-xl border px-3 py-2 text-sm">
<input name="branch_name" value="{{ edit_row.branch_name }}" required placeholder="Client Branch name" class="rounded-xl border px-3 py-2 text-sm">
<select name="branch_type" class="rounded-xl border px-3 py-2 text-sm">
{% for code, label in [('head_office','Head Office'),('branch','Branch'),('warehouse','Warehouse'),('unit','Unit')] %}<option value="{{ code }}" {% if edit_row.branch_type == code %}selected{% endif %}>{{ label }}</option>{% endfor %}
</select>
<input name="city" value="{{ edit_row.city or '' }}" placeholder="City" class="rounded-xl border px-3 py-2 text-sm">
<input name="state" value="{{ edit_row.state or '' }}" placeholder="State" class="rounded-xl border px-3 py-2 text-sm">
<input name="pincode" value="{{ edit_row.pincode or '' }}" placeholder="Pincode" class="rounded-xl border px-3 py-2 text-sm">
<label class="flex items-center gap-2 text-sm"><input type="checkbox" name="is_primary" {% if edit_row.is_primary %}checked{% endif %}> Primary Branch</label>
<div class="md:col-span-2 xl:col-span-4"><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white">Save Branch Changes</button></div>
</form>
</section>
{% endfor %}
{% endif %}
{% if can_edit and request.query_params.get('edit_registration') %}
{% for edit_row in registrations if edit_row.id|string == request.query_params.get('edit_registration') %}
<section id="edit-registration" class="rounded-2xl border border-brand-200 bg-white p-5 shadow-soft">
<div class="mb-4 flex items-center justify-between gap-3">
<div><h3 class="font-semibold text-slate-900">Edit Registration</h3><p class="text-xs text-slate-500">The existing registration ID and linked credentials remain unchanged.</p></div>
<a href="/clients/{{ client.id }}/business-structure#registrations" class="rounded-lg border px-3 py-1.5 text-xs">Cancel</a>
</div>
<form method="post" action="/clients/{{ client.id }}/registrations/{{ edit_row.id }}/edit" class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<select name="business_unit_id" class="rounded-xl border px-3 py-2 text-sm">
<option value="">Client level / derive from Branch</option>
{% for business in businesses %}<option value="{{ business.id }}" {% if business.id == edit_row.business_unit_id %}selected{% endif %}>{{ business.business_name }}{% if not business.is_active %} (Inactive){% endif %}</option>{% endfor %}
</select>
<select name="client_branch_id" class="rounded-xl border px-3 py-2 text-sm">
<option value="">No Client Branch</option>
{% for branch in branches %}<option value="{{ branch.id }}" {% if branch.id == edit_row.client_branch_id %}selected{% endif %}>{{ branch.branch_name }}{% if not branch.is_active %} (Inactive){% endif %}</option>{% endfor %}
</select>
<select name="registration_type_id" required class="rounded-xl border px-3 py-2 text-sm">
{% for reg_type in registration_types if reg_type.is_active or reg_type.id == edit_row.registration_type_id %}<option value="{{ reg_type.id }}" {% if reg_type.id == edit_row.registration_type_id %}selected{% endif %}>{{ reg_type.name }} ({{ reg_type.code }}){% if not reg_type.is_active %} — Inactive{% endif %}</option>{% endfor %}
</select>
<input name="registration_number" value="{{ edit_row.registration_number }}" required placeholder="Registration number" class="rounded-xl border px-3 py-2 text-sm">
<input name="legal_name" value="{{ edit_row.legal_name or '' }}" placeholder="Legal name" class="rounded-xl border px-3 py-2 text-sm">
<input name="trade_name" value="{{ edit_row.trade_name or '' }}" placeholder="Trade / deductor unit name" class="rounded-xl border px-3 py-2 text-sm">
<input name="state" value="{{ edit_row.state or '' }}" placeholder="State" class="rounded-xl border px-3 py-2 text-sm">
<div><button class="rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white">Save Registration Changes</button></div>
</form>
</section>
{% endfor %}
{% endif %}
<section id="business-units" class="rounded-2xl bg-white shadow-soft overflow-hidden">
<div class="border-b p-4 font-semibold">Business Units</div>
<table class="min-w-full text-sm"><thead class="bg-slate-50"><tr><th class="p-3 text-left">Code</th><th class="p-3 text-left">Business Unit</th><th class="p-3 text-left">Trade Name</th><th class="p-3 text-left">Nature</th><th class="p-3">Status</th><th></th></tr></thead>
<tbody>{% for row in businesses %}<tr class="border-t"><td class="p-3">{{ row.business_code }}</td><td class="p-3 font-medium">{{ row.business_name }}{% if row.is_primary %} <span class="text-xs text-brand-700">Primary</span>{% endif %}</td><td class="p-3">{{ row.trade_name or '-' }}</td><td class="p-3">{{ row.nature_of_business or '-' }}</td><td class="p-3 text-center">{{ 'Active' if row.is_active else 'Inactive' }}</td><td class="p-3">{% if can_edit %}<div class="flex items-center gap-3"><a href="/clients/{{ client.id }}/business-structure?edit_business={{ row.id }}#edit-business" class="text-xs font-semibold text-brand-700">Edit</a><form method="post" action="/clients/{{ client.id }}/business-structure/business/{{ row.id }}/toggle"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><button class="text-xs text-brand-700">{{ 'Deactivate' if row.is_active else 'Activate' }}</button></form></div>{% endif %}</td></tr>{% else %}<tr><td colspan="6" class="p-6 text-center text-slate-500">No Business Units added.</td></tr>{% endfor %}</tbody></table>
</section>
<section id="client-branches" class="rounded-2xl bg-white shadow-soft overflow-hidden">
<div class="border-b p-4 font-semibold">Client Branches</div>
<table class="min-w-full text-sm"><thead class="bg-slate-50"><tr><th class="p-3 text-left">Code</th><th class="p-3 text-left">Client Branch</th><th class="p-3 text-left">Type</th><th class="p-3 text-left">Location</th><th class="p-3">Status</th><th></th></tr></thead>
<tbody>{% for row in branches %}<tr class="border-t"><td class="p-3">{{ row.branch_code }}</td><td class="p-3 font-medium">{{ row.branch_name }}{% if row.is_primary %} <span class="text-xs text-brand-700">Primary</span>{% endif %}</td><td class="p-3">{{ row.branch_type|replace('_',' ')|title }}</td><td class="p-3">{{ row.city or '' }}{% if row.city and row.state %}, {% endif %}{{ row.state or '-' }}</td><td class="p-3 text-center">{{ 'Active' if row.is_active else 'Inactive' }}</td><td class="p-3">{% if can_edit %}<div class="flex items-center gap-3"><a href="/clients/{{ client.id }}/business-structure?edit_branch={{ row.id }}#edit-branch" class="text-xs font-semibold text-brand-700">Edit</a><form method="post" action="/clients/{{ client.id }}/business-structure/branch/{{ row.id }}/toggle"><input type="hidden" name="csrf_token" value="{{ csrf_token }}"><button class="text-xs text-brand-700">{{ 'Deactivate' if row.is_active else 'Activate' }}</button></form></div>{% endif %}</td></tr>{% else %}<tr><td colspan="6" class="p-6 text-center text-slate-500">No Client Branches added.</td></tr>{% endfor %}</tbody></table>
</section>
<section id="registrations" class="rounded-2xl bg-white shadow-soft overflow-hidden">
<div class="flex flex-wrap items-center justify-between gap-2 border-b p-4">
<div>
<div class="font-semibold">Registrations</div>
<div class="mt-0.5 text-xs font-normal text-slate-500">Portal credentials linked to each registration are available here without exposing encrypted values.</div>
</div>
</div>
<div class="overflow-x-auto">
<table class="min-w-full text-sm">
<thead class="bg-slate-50">
<tr>
<th class="p-3 text-left">Type</th>
<th class="p-3 text-left">Number</th>
<th class="p-3 text-left">Trade / Unit Name</th>
<th class="p-3 text-left">State</th>
<th class="p-3 text-center">Status</th>
<th class="p-3 text-left">Credentials</th>
<th class="p-3 text-left">Actions</th>
</tr>
</thead>
<tbody>
{% for row in registrations %}
{% set linked_credentials = registration_credentials.get(row.id, []) %}
<tr class="border-t align-top">
<td class="p-3">{{ registration_type_codes.get(row.id, "-") }}</td>
<td class="p-3 font-medium">{{ row.registration_number }}</td>
<td class="p-3">{{ row.trade_name or row.legal_name or '-' }}</td>
<td class="p-3">{{ row.state or '-' }}</td>
<td class="p-3 text-center">{{ row.status|replace('_',' ')|title }}</td>
<td class="p-3 min-w-[260px]">
{% if can_open_credentials %}
{% if linked_credentials %}
<details class="group">
<summary class="cursor-pointer list-none inline-flex items-center gap-2 rounded-lg border border-violet-200 bg-violet-50 px-2.5 py-1.5 text-xs font-semibold text-violet-700">
{{ linked_credentials|length }} credential{{ '' if linked_credentials|length == 1 else 's' }}
<span class="text-[10px] group-open:rotate-180">▼</span>
</summary>
<div class="mt-2 space-y-2">
{% for credential in linked_credentials %}
<div class="rounded-lg border border-slate-200 bg-slate-50 p-2.5">
<div class="flex flex-wrap items-start justify-between gap-2">
<div>
<div class="font-medium text-slate-800">{{ credential.title }}</div>
<div class="text-xs text-slate-500">{{ credential_category_labels.get(credential.category, credential.category|replace('_',' ')|title) }}</div>
</div>
<span class="rounded-full px-2 py-0.5 text-[11px] font-medium {% if credential.status == 'active' %}bg-emerald-100 text-emerald-700{% else %}bg-slate-200 text-slate-600{% endif %}">{{ credential.status|replace('_',' ')|title }}</span>
</div>
<a href="/credential-vault/{{ credential.id }}" class="mt-1.5 inline-block text-xs font-semibold text-brand-700">View credential</a>
</div>
{% endfor %}
</div>
</details>
{% else %}
<span class="text-xs text-slate-400">No credentials added</span>
{% endif %}
{% else %}
<span class="text-xs text-slate-400">Restricted</span>
{% endif %}
</td>
<td class="p-3 min-w-[170px]">
<div class="flex flex-wrap items-center gap-x-3 gap-y-2">
{% if can_edit %}
<a href="/clients/{{ client.id }}/business-structure?edit_registration={{ row.id }}#edit-registration" class="text-xs font-semibold text-brand-700">Edit</a>
{% endif %}
{% if can_manage_credentials %}
<a href="/credential-vault/new?client_id={{ client.id }}&registration_id={{ row.id }}" class="text-xs font-semibold text-violet-700">{{ '+ Add Another Credential' if linked_credentials else '+ Add Credential' }}</a>
{% endif %}
{% if can_edit %}
<form method="post" action="/clients/{{ client.id }}/business-structure/registration/{{ row.id }}/toggle">
<input type="hidden" name="csrf_token" value="{{ csrf_token }}">
<button class="text-xs text-brand-700">{{ 'Deactivate' if row.status in ['active','valid','registered'] else 'Activate' }}</button>
</form>
{% endif %}
</div>
</td>
</tr>
{% else %}
<tr><td colspan="7" class="p-6 text-center text-slate-500">No registrations added.</td></tr>
{% endfor %}
</tbody>
</table>
</div>
</section>
</div>
<script>
(function () {
const action = new URLSearchParams(window.location.search).get('action');
if (!action) return;
const target = document.querySelector('[data-quick-action="' + action + '"]');
if (!target) return;
target.classList.add('ring-2', 'ring-brand-400', 'ring-offset-2');
window.setTimeout(function () {
target.scrollIntoView({behavior: 'smooth', block: 'center'});
const first = target.querySelector('input:not([type="hidden"]), select, textarea');
if (first) first.focus({preventScroll: true});
}, 80);
})();
</script>
{% endblock %}