90 lines
4.4 KiB
PHP
90 lines
4.4 KiB
PHP
<?php
|
|
header("Content-Type: application/json");
|
|
header("Access-Control-Allow-Origin: *");
|
|
$action = $_GET["action"] ?? $_POST["action"] ?? "list";
|
|
$db_file = "/opt/weval-data/contracts.json";
|
|
if (!file_exists(dirname($db_file))) @mkdir(dirname($db_file), 0755, true);
|
|
if (!file_exists($db_file)) file_put_contents($db_file, "[]");
|
|
$contracts = json_decode(file_get_contents($db_file), true) ?: [];
|
|
|
|
switch ($action) {
|
|
case "list":
|
|
echo json_encode(["ok"=>true,"count"=>count($contracts),"contracts"=>$contracts]);
|
|
break;
|
|
|
|
case "create":
|
|
$c = [
|
|
"id" => "CTR-" . date("Ymd") . "-" . str_pad(count($contracts)+1, 3, "0", STR_PAD_LEFT),
|
|
"type" => $_POST["type"] ?? "consulting", // nda, sla, consulting, loi, amendment
|
|
"client" => $_POST["client"] ?? "",
|
|
"title" => $_POST["title"] ?? "",
|
|
"value_mad" => floatval($_POST["value_mad"] ?? 0),
|
|
"currency" => $_POST["currency"] ?? "MAD",
|
|
"start_date" => $_POST["start_date"] ?? date("Y-m-d"),
|
|
"end_date" => $_POST["end_date"] ?? "",
|
|
"status" => "draft", // draft, sent, signed, active, expired, terminated
|
|
"entity" => $_POST["entity"] ?? "WEVAL Consulting SARL",
|
|
"clauses" => [],
|
|
"versions" => [["v"=>1,"date"=>date("c"),"author"=>"system"]],
|
|
"created_at" => date("c"),
|
|
"updated_at" => date("c"),
|
|
];
|
|
// IA: générer les clauses via Sovereign
|
|
if ($_POST["ai_generate"] ?? false) {
|
|
$prompt = "Génère les clauses principales pour un contrat de type " . $c["type"] .
|
|
" entre WEVAL Consulting SARL (Casablanca, Maroc) et " . $c["client"] .
|
|
". Valeur: " . $c["value_mad"] . " " . $c["currency"] .
|
|
". Retourne un JSON array de clauses avec titre et contenu.";
|
|
$ctx2 = stream_context_create(["http"=>["timeout"=>8,"method"=>"POST",
|
|
"header"=>"Content-Type: application/json",
|
|
"content"=>json_encode(["messages"=>[["role"=>"user","content"=>$prompt]],"max_tokens"=>500,"stream"=>false])]]);
|
|
$resp = @file_get_contents("http://127.0.0.1:4000/v1/chat/completions", false, $ctx2);
|
|
$ai = @json_decode($resp, true);
|
|
$c["clauses_ai"] = $ai["choices"][0]["message"]["content"] ?? "Sovereign indisponible";
|
|
}
|
|
$contracts[] = $c;
|
|
file_put_contents($db_file, json_encode($contracts, JSON_PRETTY_PRINT));
|
|
echo json_encode(["ok"=>true,"contract"=>$c]);
|
|
break;
|
|
|
|
case "get":
|
|
$id = $_GET["id"] ?? "";
|
|
$found = array_filter($contracts, fn($c) => $c["id"] === $id);
|
|
echo json_encode(["ok"=>!empty($found),"contract"=>array_values($found)[0] ?? null]);
|
|
break;
|
|
|
|
case "update_status":
|
|
$id = $_POST["id"] ?? "";
|
|
$status = $_POST["status"] ?? "";
|
|
foreach ($contracts as &$c) {
|
|
if ($c["id"] === $id) { $c["status"] = $status; $c["updated_at"] = date("c"); }
|
|
}
|
|
file_put_contents($db_file, json_encode($contracts, JSON_PRETTY_PRINT));
|
|
echo json_encode(["ok"=>true]);
|
|
break;
|
|
|
|
case "templates":
|
|
echo json_encode(["ok"=>true,"templates"=>[
|
|
["type"=>"nda","name"=>"NDA Standard","desc"=>"Accord de non-divulgation bilatéral"],
|
|
["type"=>"consulting","name"=>"Contrat Consulting","desc"=>"Prestation de services IT/IA"],
|
|
["type"=>"sla","name"=>"SLA","desc"=>"Service Level Agreement avec KPIs"],
|
|
["type"=>"loi","name"=>"Lettre d'intention","desc"=>"LOI avant contrat définitif"],
|
|
["type"=>"amendment","name"=>"Avenant","desc"=>"Modification contrat existant"],
|
|
["type"=>"partnership","name"=>"Partenariat","desc"=>"Accord de partenariat commercial"],
|
|
]]);
|
|
break;
|
|
|
|
case "stats":
|
|
$by_status = [];
|
|
$total_value = 0;
|
|
foreach ($contracts as $c) {
|
|
$by_status[$c["status"]] = ($by_status[$c["status"]] ?? 0) + 1;
|
|
if ($c["status"] === "active") $total_value += $c["value_mad"];
|
|
}
|
|
echo json_encode(["ok"=>true,"total"=>count($contracts),"by_status"=>$by_status,"total_value_mad"=>$total_value]);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(["ok"=>false,"error"=>"Unknown action","actions"=>["list","create","get","update_status","templates","stats"]]);
|
|
}
|
|
?>
|