Script php
This commit is contained in:
1
script.log
Normal file
1
script.log
Normal file
@@ -0,0 +1 @@
|
|||||||
|
2025-12-12T06:34:25+00:00 | Daniel | amount=250 | vat=47.5 | total=297.5
|
||||||
45
script_php.php
Executable file
45
script_php.php
Executable file
@@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/env php
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rulare:
|
||||||
|
* php script.php --name=Daniel --amount=125.50
|
||||||
|
*/
|
||||||
|
|
||||||
|
function usage(): void {
|
||||||
|
echo "Usage: php script.php --name=NAME --amount=NUMBER\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$options = getopt("", ["name:", "amount:"]);
|
||||||
|
$name = $options["name"] ?? null;
|
||||||
|
$amount = $options["amount"] ?? null;
|
||||||
|
|
||||||
|
if ($name === null || $amount === null) {
|
||||||
|
usage();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_numeric($amount)) {
|
||||||
|
fwrite(STDERR, "Error: --amount must be numeric.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$amount = (float)$amount;
|
||||||
|
$vatRate = 0.19; // TVA 19%
|
||||||
|
$vat = round($amount * $vatRate, 2);
|
||||||
|
$total = round($amount + $vat, 2);
|
||||||
|
|
||||||
|
$result = [
|
||||||
|
"name" => $name,
|
||||||
|
"amount" => $amount,
|
||||||
|
"vat" => $vat,
|
||||||
|
"total" => $total,
|
||||||
|
"time" => date("c"),
|
||||||
|
];
|
||||||
|
|
||||||
|
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
|
||||||
|
|
||||||
|
// scrie și un log local
|
||||||
|
$logLine = $result["time"] . " | {$name} | amount={$amount} | vat={$vat} | total={$total}\n";
|
||||||
|
file_put_contents(__DIR__ . "/script.log", $logLine, FILE_APPEND);
|
||||||
Reference in New Issue
Block a user