69 lines
3.3 KiB
PHP
69 lines
3.3 KiB
PHP
<?php
|
|
header("Content-Type: application/json");
|
|
header("Access-Control-Allow-Origin: *");
|
|
$action = $_GET["action"] ?? $_POST["action"] ?? "list";
|
|
$db_file = "/opt/weval-data/quotes.json";
|
|
if (!file_exists(dirname($db_file))) @mkdir(dirname($db_file), 0755, true);
|
|
if (!file_exists($db_file)) file_put_contents($db_file, "[]");
|
|
$quotes = json_decode(file_get_contents($db_file), true) ?: [];
|
|
|
|
switch ($action) {
|
|
case "list":
|
|
echo json_encode(["ok"=>true,"count"=>count($quotes),"quotes"=>$quotes]);
|
|
break;
|
|
|
|
case "create":
|
|
$q = [
|
|
"id" => "DEV-" . date("Ymd") . "-" . str_pad(count($quotes)+1, 3, "0", STR_PAD_LEFT),
|
|
"client" => $_POST["client"] ?? "",
|
|
"project" => $_POST["project"] ?? "",
|
|
"lines" => json_decode($_POST["lines"] ?? "[]", true) ?: [],
|
|
"total_ht" => floatval($_POST["total_ht"] ?? 0),
|
|
"tva" => floatval($_POST["total_ht"] ?? 0) * 0.20,
|
|
"total_ttc" => floatval($_POST["total_ht"] ?? 0) * 1.20,
|
|
"currency" => $_POST["currency"] ?? "MAD",
|
|
"validity_days" => intval($_POST["validity"] ?? 30),
|
|
"status" => "draft", // draft, sent, accepted, rejected, expired, invoiced
|
|
"created_at" => date("c"),
|
|
];
|
|
// IA: générer une estimation si demandé
|
|
if ($_POST["ai_estimate"] ?? false) {
|
|
$prompt = "Estime le coût d'un projet: " . $q["project"] . " pour " . $q["client"] .
|
|
". Retourne un JSON avec les lignes de devis (description, jours, tjm_mad).";
|
|
$ctx2 = stream_context_create(["http"=>["timeout"=>8,"method"=>"POST",
|
|
"header"=>"Content-Type: application/json",
|
|
"content"=>json_encode(["messages"=>[["role"=>"user","content"=>$prompt]],"max_tokens"=>300,"stream"=>false])]]);
|
|
$resp = @file_get_contents("http://127.0.0.1:4000/v1/chat/completions", false, $ctx2);
|
|
$ai = @json_decode($resp, true);
|
|
$q["ai_estimate"] = $ai["choices"][0]["message"]["content"] ?? "Sovereign indisponible";
|
|
}
|
|
$quotes[] = $q;
|
|
file_put_contents($db_file, json_encode($quotes, JSON_PRETTY_PRINT));
|
|
echo json_encode(["ok"=>true,"quote"=>$q]);
|
|
break;
|
|
|
|
case "convert_invoice":
|
|
$id = $_POST["id"] ?? "";
|
|
foreach ($quotes as &$q) {
|
|
if ($q["id"] === $id && $q["status"] === "accepted") {
|
|
$q["status"] = "invoiced";
|
|
// Créer la facture automatiquement
|
|
$inv_data = ["client"=>$q["client"],"subtotal_ht"=>$q["total_ht"],"currency"=>$q["currency"],"lines"=>json_encode($q["lines"])];
|
|
// POST vers invoice-api
|
|
echo json_encode(["ok"=>true,"message"=>"Devis converti, créer facture via /api/invoice-api.php?action=create"]);
|
|
break;
|
|
}
|
|
}
|
|
file_put_contents($db_file, json_encode($quotes, JSON_PRETTY_PRINT));
|
|
break;
|
|
|
|
case "stats":
|
|
echo json_encode(["ok"=>true,"total"=>count($quotes),
|
|
"by_status"=>array_count_values(array_column($quotes,"status")),
|
|
"pipeline_value"=>array_sum(array_map(fn($q)=>$q["status"]==="sent"?$q["total_ttc"]:0,$quotes))]);
|
|
break;
|
|
|
|
default:
|
|
echo json_encode(["ok"=>false,"actions"=>["list","create","convert_invoice","stats"]]);
|
|
}
|
|
?>
|