exec("ATTACH DATABASE '{$dbFile}' AS encrypted KEY '{$key}'");
$db->exec("CREATE TABLE invoices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
projectname TEXT,
invoicedate TEXT,
movevalue TEXT,
invoiceid TEXT,
type TEXT,
totalvalue TEXT)");
// Detach the encrypted database
$db->exec("DETACH DATABASE encrypted");
} else {
// Connect to existing encrypted database
$db = new SQLite3($dbFile);
$db->exec("ATTACH DATABASE '{$dbFile}' AS encrypted KEY '{$key}'");
$db->close();
$pdoProjects = new PDO('sqlite:' . $dbFile);
$pdoProjects->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Query to count the number of rows in the projects table
$queryCount = 'SELECT COUNT(*) as count FROM invoices';
$statementCount = $pdoProjects->query($queryCount);
$resultCount = $statementCount->fetch(PDO::FETCH_ASSOC);
// Check if the count is zero
if ($resultCount['count'] == 0) {
// Connect to the SQLite database for invoices
$pdoInvoices = new PDO('sqlite:' . $dbFileInvoices);
$pdoInvoices->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Query to fetch invoices data
$queryInvoices = 'SELECT * FROM figures';
$statementInvoices = $pdoInvoices->query($queryInvoices);
$resultsInvoices = $statementInvoices->fetchAll(PDO::FETCH_ASSOC);
// Fetch project names
foreach ($resultsInvoices as $row) {
$rowID = $row['id'];
$projectname = $row['projectname'];
$statusofinvoice = $row['status'];
if ($statusofinvoice =="paid"){
// Check if the ID is already in the list
if (!in_array($rowID, $processedIDs)) {
// Add the ID to the list
$processedIDs[] = $rowID;
// Check if the project name contains 'render'
if (stripos($projectname, 'render') !== false) {
$projectnamesRenders[] = $projectname;
} else {
$projectnamesArchi[] = $projectname;
}
}
}
}
$processedIDsRenders = [];
$processedIDsArchi = [];
// Loop through each project name and fetch corresponding invoice totals
foreach ($projectnamesRenders as $name1) {
foreach ($resultsInvoices as $row2) {
$projectname1 = $row2['projectname'];
$rowIDRenders = $row2['id'];
$statusofinvoice = $row2['status'];
if ($statusofinvoice =="paid"){
if (stripos($projectname1, $name1) !== false) {
if (!in_array($rowIDRenders, $processedIDsRenders)) {
// Add the ID to the list
$processedIDsRenders[] = $rowIDRenders;
$createdDate1 = $row2['createdate'];
$createdID1 = $row2['id'];
$totalvalue1 = str_replace(' ', '', ltrim($row2['totalvalue'], 'R'));
$totalInvoicesRenders[] = [
'projectname1' => $name1,
'createdDate1' => $createdDate1,
'splitFigures1' => "NO",
'createdID1' => $createdID1,
'Type1' => "renders",
'totalvalue1' => $totalvalue1
];
$totalSumRenders += floatval($totalvalue1);
}
}
}
}
}
foreach ($projectnamesArchi as $name2) {
foreach ($resultsInvoices as $row3) {
$projectname2 = $row3['projectname'];
// Split the string by "-"
$parts = explode("-", $projectname2);
$firstPart = $parts[0];
$rowIDsArchi = $row3['id'];
$statusofinvoice = $row3['status'];
if ($statusofinvoice =="paid"){
if (stripos($projectname2, $firstPart) !== false) {
if (stripos($name2, $projectname2) !== false) {
if (!in_array($rowIDsArchi, $processedIDsArchi)) {
// Add the ID to the list
$processedIDsArchi[] = $rowIDsArchi;
$createdDate2 = $row3['createdate'];
$createdID2 = $row3['id'];
$totalvalue2 = str_replace(' ', '', ltrim($row3['totalvalue'], 'R'));
$totalInvoicesArchi[] = [
'projectname2' => $name2,
'createdDate2' => $createdDate2,
'splitFigures2' => "NO",
'createdID2' => $createdID2,
'Type2' => "architecture",
'totalvalue2' => $totalvalue2
];
$totalSumArchi += floatval($totalvalue2);
}
}
}
}
}
}
} else {
// echo 'The database has entries';
// Connect to the SQLite database for projects
$pdoProjects = new PDO('sqlite:' . $dbFile);
$pdoProjects->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Query to fetch projects data
$queryProjects = 'SELECT * FROM invoices';
$statementProjects = $pdoProjects->query($queryProjects);
$resultsProjects = $statementProjects->fetchAll(PDO::FETCH_ASSOC);
$uniqueInvoiceIds = array();
// Fetch project names
foreach ($resultsProjects as $row) {
$projectname = $row['projectname'];
// Check if the project name contains 'render'
if (stripos($projectname, 'render') !== false) {
$projectnamesRenders[] = $projectname;
} else {
$projectnamesArchi[] = $projectname;
}
$invoiceidNum = $row['invoiceid'];
// Check if the invoice ID is already in the array
if (!in_array($invoiceidNum, $uniqueInvoiceIds)) {
$uniqueInvoiceIds[] = $invoiceidNum;
}
}
$processedIDsRenders = [];
$processedIDsArchi = [];
// Connect to the SQLite database for invoices
$pdoInvoices = new PDO('sqlite:' . $dbFile);
$pdoInvoices->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Query to fetch invoices data
$queryInvoices = 'SELECT * FROM invoices';
$statementInvoices = $pdoInvoices->query($queryInvoices);
$resultsInvoices = $statementInvoices->fetchAll(PDO::FETCH_ASSOC);
// Loop through each project name and fetch corresponding invoice totals
foreach ($projectnamesRenders as $name1) {
foreach ($resultsInvoices as $row2) {
$projectname1 = $row2['projectname'];
$rowIDRenders = $row2['invoiceid'];
if (stripos($projectname1, $name1) !== false) {
if (!in_array($rowIDRenders, $processedIDsRenders)) {
// Add the ID to the list
$processedIDsRenders[] = $rowIDRenders;
$createdDate1 = $row2['invoicedate'];
$createdID1 = $row2['invoiceid'];
$movevalue1 = $row2['$movevalue'];
$idForEachRow1 = $row2['id'];
$type1 = $row2['type'];
$totalvalue1 = str_replace(' ', '', ltrim($row2['totalvalue'], 'R'));
$totalInvoicesRenders[] = [
'id1' => $idForEachRow1,
'projectname1' => $name1,
'createdDate1' => $createdDate1,
'splitFigures1' => $movevalue1,
'createdID1' => $createdID1,
'Type1' => "renders",
'totalvalue1' => $totalvalue1
];
$totalSumRenders += floatval($totalvalue1);
}
}
}
}
foreach ($projectnamesArchi as $name2) {
foreach ($resultsInvoices as $row3) {
$projectname2 = $row3['projectname'];
// Split the string by "-"
$parts = explode("-", $projectname2);
$firstPart = $parts[0];
$rowIDsArchi = $row3['invoiceid'];
if (stripos($projectname2, $firstPart) !== false) {
if (stripos($name2, $projectname2) !== false) {
if (!in_array($rowIDsArchi, $processedIDsArchi)) {
// Add the ID to the list
$processedIDsArchi[] = $rowIDsArchi;
$createdDate2 = $row3['invoicedate'];
$createdID2 = $row3['invoiceid'];
$movevalue2 = $row2['$movevalue'];
$idForEachRow2 = $row3['id'];
$type2 = $row3['type'];
$totalvalue2 = str_replace(' ', '', ltrim($row3['totalvalue'], 'R'));
$totalInvoicesArchi[] = [
'id2' => $idForEachRow2,
'projectname2' => $name2,
'createdDate2' => $createdDate2,
'splitFigures2' => $movevalue2,
'createdID2' => $createdID2,
'Type2' => "architecture",
'totalvalue2' => $totalvalue2
];
$totalSumArchi += floatval($totalvalue2);
}
}
}
}
}
/////// NOW CHECKING INVOICE
$uniqueIdsForNewInvoices = array();
// Connect to the SQLite database for invoices
$pdoInvoices = new PDO('sqlite:' . $dbFileInvoices);
$pdoInvoices->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Query to fetch invoices data
$queryInvoices = 'SELECT * FROM figures';
$statementInvoices = $pdoInvoices->query($queryInvoices);
$resultsInvoices2 = $statementInvoices->fetchAll(PDO::FETCH_ASSOC);
$projectnamesRenders2 = [];
$projectnamesArchi2 = [];
foreach ($resultsInvoices2 as $row2) {
$rowID = $row2['id'];
$projectname = $row2['projectname'];
$statusofinvoice = $row2['status'];
if ($statusofinvoice == "paid"){
if (stripos($projectname, 'render') !== false) {
$projectnamesRenders2[] = $projectname;
} else {
$projectnamesArchi2[] = $projectname;
}
if (!in_array($rowID, $uniqueInvoiceIds)) {
// Add the ID to the list
$uniqueInvoiceIds[] = $rowID;
$uniqueIdsForNewInvoices[] = $rowID;
}
}
}
foreach ($uniqueIdsForNewInvoices as $invoiceid) {
foreach ($resultsInvoices2 as $row2) {
$projectname = $row2['projectname'];
$rowID = $row2['id'];
$statusofinvoice = $row2['status'];
if ($statusofinvoice == "paid"){
if ($rowID == $invoiceid){
if (stripos($projectname, 'render') !== false) {
// $projectnamesRenders2[] = $projectname;
// echo "Invoice ID:" . $invoiceid . " - $projectname". "
";
$processedIDsRenders[] = $rowIDRenders;
$createdDate1 = $row2['createdate'];
$createdID1 = $row2['invoiceid'];
$movevalue1 = "NO";
$idForEachRow1 = $row2['id'];
$totalvalue1 = str_replace(' ', '', ltrim($row2['totalvalue'], 'R'));
$totalInvoicesRenders[] = [
'id1' => $idForEachRow1,
'projectname1' => $projectname,
'createdDate1' => $createdDate1,
'splitFigures1' => $movevalue1,
'createdID1' => $idForEachRow1,
'Type1' => "renders",
'totalvalue1' => $totalvalue1
];
$totalSumRenders += floatval($totalvalue1);
} else {
$processedIDsArchi[] = $rowIDsArchi;
$createdDate2 = $row2['createdate'];
$movevalue2 = "NO";
$idForEachRow2 = $row2['id'];
$totalvalue2 = str_replace(' ', '', ltrim($row2['totalvalue'], 'R'));
$totalInvoicesArchi[] = [
'id2' => $idForEachRow2,
'projectname2' => $projectname,
'createdDate2' => $createdDate2,
'splitFigures2' => $movevalue2,
'createdID2' => $idForEachRow2,
'Type2' => "architecture",
'totalvalue2' => $totalvalue2
];
$totalSumArchi += floatval($totalvalue2);
}
}
}
}
}
}
}
} catch (PDOException $e) {
echo 'Connection or query failed: ' . $e->getMessage();
}
// Get the current year
$currentYear = date('Y');
// Generate years for the dropdown
$years = [];
for ($i = 0; $i < 4; $i++) {
$years[] = $currentYear - $i;
}
?>
| Project Name | created Date | Total Invoice Value | |||
|---|---|---|---|---|---|
| NO | R |
||||
| Total Sum | N | N | N | N | R |
| Project Name | created Date | Total Invoice Value | |||
|---|---|---|---|---|---|
| NO | R |
||||
| Total Sum | N | N | N | N | R |