158 lines
4.7 KiB
PHP
158 lines
4.7 KiB
PHP
<?php
|
|
|
|
class Reports
|
|
{
|
|
private PDO $pdo;
|
|
private int $companyId;
|
|
|
|
public function __construct(PDO $pdo, int $companyId)
|
|
{
|
|
$this->pdo = $pdo;
|
|
$this->companyId = $companyId;
|
|
}
|
|
|
|
/**
|
|
* Master Data Summary:
|
|
* - Total Categories: Count of all categories in md_product_category for the company.
|
|
* - Active Categories: Count of categories where status = 1.
|
|
* - Inactive Categories: Count of categories where status = 0.
|
|
*/
|
|
public function getTotalCategory(): int
|
|
{
|
|
$sql = "SELECT COUNT(*)
|
|
FROM md_product_category
|
|
WHERE company_id = :company_id";
|
|
return (int) $this->fetchScalar($sql);
|
|
}
|
|
|
|
public function getActiveCategory(): int
|
|
{
|
|
$sql = "SELECT COUNT(*)
|
|
FROM md_product_category
|
|
WHERE company_id = :company_id
|
|
AND `status` = 1";
|
|
return (int) $this->fetchScalar($sql);
|
|
}
|
|
|
|
public function getInactiveCategory(): int
|
|
{
|
|
$sql = "SELECT COUNT(*)
|
|
FROM md_product_category
|
|
WHERE company_id = :company_id
|
|
AND `status` = 0";
|
|
return (int) $this->fetchScalar($sql);
|
|
}
|
|
|
|
public function getTotalWarehouse(): int
|
|
{
|
|
$sql = "SELECT COUNT(*)
|
|
FROM md_warehouse
|
|
WHERE company_id = :company_id
|
|
AND `status` = 1";
|
|
return (int) $this->fetchScalar($sql);
|
|
}
|
|
|
|
public function getTotalLocation(): int
|
|
{
|
|
$sql = "SELECT COUNT(DISTINCT `location`)
|
|
FROM md_warehouse
|
|
WHERE company_id = :company_id
|
|
AND `status` = 1";
|
|
return (int) $this->fetchScalar($sql);
|
|
}
|
|
|
|
/**
|
|
* Total capacity = total count of active racks across all warehouses.
|
|
* One rack = one storage position (holds one product unit).
|
|
*/
|
|
public function getTotalCapacity(): int
|
|
{
|
|
$sql = "SELECT COUNT(*)
|
|
FROM md_rack
|
|
WHERE company_id = :company_id";
|
|
return (int) $this->fetchScalar($sql);
|
|
}
|
|
|
|
/**
|
|
* Space Used = count of racks currently occupied (product_sku IS NOT NULL).
|
|
*/
|
|
public function getSpaceUsed(): int
|
|
{
|
|
$sql = "SELECT COUNT(*)
|
|
FROM md_rack
|
|
WHERE company_id = :company_id
|
|
AND product_sku IS NOT NULL";
|
|
return (int) $this->fetchScalar($sql);
|
|
}
|
|
|
|
/**
|
|
* Product Count = total count of distinct products in the md_product table for the company.
|
|
* Low Stock Count = count of products where current stock balance (total_in - total_out) is less than the min_stock defined in md_product.
|
|
*/
|
|
public function getTotalProduct(): int
|
|
{
|
|
$sql = "SELECT COUNT(*)
|
|
FROM md_product
|
|
WHERE company_id = :company_id";
|
|
return (int) $this->fetchScalar($sql);
|
|
}
|
|
|
|
public function getTotalProductInStock(): int
|
|
{
|
|
$sql = "SELECT COUNT(DISTINCT product_sku)
|
|
FROM warehouse_balance
|
|
WHERE company_id = :company_id
|
|
AND (total_in - total_out) > 0";
|
|
return (int) $this->fetchScalar($sql);
|
|
}
|
|
|
|
public function getLowStockCount(): int
|
|
{
|
|
$products = $this->getStockBalance();
|
|
$count = 0;
|
|
foreach ($products as $product) {
|
|
$balance = (float) $product["total_in"] - (float) $product["total_out"];
|
|
if ($balance < (float) $product["min_stock"]) {
|
|
$count++;
|
|
}
|
|
}
|
|
return $count;
|
|
}
|
|
|
|
public function getStockBalance(): array
|
|
{
|
|
$sql = "SELECT
|
|
a.product_sku,
|
|
SUM(a.total_in) AS total_in,
|
|
SUM(a.total_out) AS total_out,
|
|
b.min_stock
|
|
FROM warehouse_balance a
|
|
LEFT JOIN md_product b
|
|
ON a.company_id = b.company_id
|
|
AND a.product_sku = b.sku
|
|
WHERE a.company_id = :company_id
|
|
GROUP BY a.product_sku, b.min_stock";
|
|
return $this->fetchAll($sql);
|
|
}
|
|
|
|
// FETCH HELPERS
|
|
// These helper methods abstract away the common pattern of preparing, executing, and fetching results from the database.
|
|
private function fetchScalar(string $sql)
|
|
{
|
|
$sth = $this->pdo->prepare($sql);
|
|
$sth->execute([":company_id" => $this->companyId]);
|
|
return $sth->fetchColumn();
|
|
}
|
|
|
|
private function fetchAll(string $sql): array
|
|
{
|
|
$sth = $this->pdo->prepare($sql);
|
|
$sth->execute([":company_id" => $this->companyId]);
|
|
return $sth->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|