File size: 4,625 Bytes
dd6303a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""Per-tender editable context management."""

import json
import pathlib

BASE = pathlib.Path(__file__).parent.parent


def get_context_path(tender_id: str) -> pathlib.Path:
    return BASE / "input" / str(tender_id) / "context.json"


def get_firm_config() -> dict:
    path = pathlib.Path(__file__).parent / "firm_config.json"
    if path.exists():
        with open(path, encoding="utf-8") as f:
            return json.load(f)
    return {}


def load_context(tender_id: str) -> dict:
    path = get_context_path(tender_id)
    if path.exists():
        with open(path, encoding="utf-8") as f:
            return json.load(f)
    return create_default_context(tender_id)


def save_context(tender_id: str, data: dict) -> pathlib.Path:
    path = get_context_path(tender_id)
    path.parent.mkdir(parents=True, exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        json.dump(data, f, indent=2, ensure_ascii=False)
    return path


def _default_generate_docs() -> dict:
    return {
        "bg_hb": True,
        "bg_credit_line": True,
        "equipment_decl": True,
        "manpower_decl": True,
        "methodology": True,
        "work_plan": True,
        "boq_excel": True,
        "rate_check": True,
        "summary": True,
        "jv_deed": False,
        "jv_poa": False,
    }


def _default_work_months(firm: dict) -> list[str]:
    return firm.get("default_work_months", [
        "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    ])


def create_default_context(tender_id: str) -> dict:
    firm = get_firm_config()
    ctx = {
        "_info": "Edit these values before generation. They override PDF extraction.",
        "tender_id": str(tender_id),
        "zone": "A",
        "invitation_ref_no": "",
        "package_no": "",
        "work_name": "",
        "location": "",
        "procuring_entity": "",
        "executive_engineer": "",
        "pe_address": "",
        "start_date": "",
        "completion_date": "",
        "closing_date": "",
        "publication_date": "",
        "tender_security_amount": 0,
        "departmental_estimate": 0,
        "quoted_total": 0,
        "quoted_rate_percent": 0,
        "bg_date": "",
        "bg_validity_date": "",
        "bank_guarantee_no": "",
        "memo_no": firm.get("memo_no_prefix", "HB/") + "____",
        "document_date": "",
        "firm_name": firm.get("firm_name", ""),
        "firm_address": firm.get("firm_address", ""),
        "proprietor_name": firm.get("proprietor_name", ""),
        "egp_email": firm.get("egp_email", ""),
        "bank_name": firm.get("bank_name", ""),
        "bank_branch": firm.get("bank_branch", ""),
        "is_jv": False,
        "jv_name": "",
        "jv_date": "",
        "jv_partner_count": 0,
        "jv_share_text": "",
        "jv_office_address": "",
        "jv_phone": "",
        "lead_partner": "",
        "nominated_partner": "",
        "partner_in_charge_name": "",
        "partner_in_charge_firm": "",
        "work_months": _default_work_months(firm),
        "generate_docs": _default_generate_docs(),
    }
    for idx in range(1, 4):
        ctx.update({
            f"partner{idx}_code": "",
            f"partner{idx}_firm_name": "",
            f"partner{idx}_legal_type": "",
            f"partner{idx}_address": "",
            f"partner{idx}_signatory_name": "",
            f"partner{idx}_position": "",
            f"partner{idx}_share_percent": 0,
            f"partner{idx}_share_words": "",
        })
    save_context(tender_id, ctx)
    return ctx


def merge_context_into_firm_config(tender_id: str) -> dict:
    return {**get_firm_config(), **load_context(tender_id)}


def list_tenders() -> list:
    folder = BASE / "input"
    if not folder.exists():
        return []
    return sorted(d.name for d in folder.iterdir() if d.is_dir())


def get_tender_status(tender_id: str) -> dict:
    input_dir = BASE / "input" / str(tender_id)
    output_dir = BASE / "output" / str(tender_id)
    context = get_context_path(str(tender_id))
    pdf_files = list(input_dir.glob("*.pdf")) if input_dir.exists() else []
    output_files = list(output_dir.iterdir()) if output_dir.exists() else []
    return {
        "tender_id": str(tender_id),
        "input_exists": input_dir.exists(),
        "pdf_count": len(pdf_files),
        "pdf_files": [p.name for p in pdf_files],
        "context_exists": context.exists(),
        "output_exists": output_dir.exists(),
        "output_files": [p.name for p in output_files],
        "output_count": len(output_files),
    }