Skip to content

Commit

Permalink
fix(contract): update changes and add seed samples
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1026 committed Nov 16, 2024
1 parent 444ff2c commit b2fdde2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 136 deletions.
2 changes: 1 addition & 1 deletion docker/database/start-scripts/0-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ CREATE TABLE contracts (
invoice_paid float,
created_at timestamp default current_timestamp,
deleted_at timestamp,
updated_at timestamp,
updated_at timestamp
);

create table work_orders (
Expand Down
5 changes: 5 additions & 0 deletions docker/database/start-scripts/1-seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ INSERT INTO tree_types (family, genus, species) VALUES
('Pinaceae', 'Pinus', 'Pinus sylvestris'),
('Sapindaceae', 'Acer', 'Acer campestre');

-- Insert sample contracts
INSERT INTO contracts (name, start_date, end_date, invoice_proposed, invoice_agreed, invoice_paid) VALUES
('Ayuntamiento de Valencia', '2021-01-01', '2021-12-31', 1000.00, 900.00, 900.00),
('Administración General del Estado', '2021-01-01', '2021-12-31', 2000.00, 1800.00, 1800.00),
('Ayuntamiento de Carlet', '2021-01-01', '2021-12-31', 3000.00, 2700.00, 2700.00);
11 changes: 8 additions & 3 deletions src/app/Controllers/ContractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@

namespace App\Controllers;

use App\Core\BaseController;
use App\Core\View;
use App\Models\Contract;

class ContractController
class ContractController implements BaseController
{
public function index()
public function get()
{
$contracts = Contract::getAll();
$contracts = Contract::findAll();
View::render([
"view" => "Contracts",
"title" => "Contracts",
"layout" => "MainLayout",
"data" => ["contracts" => $contracts]
]);
}

public function post() {}
public function put() {}
public function delete() {}
}
151 changes: 26 additions & 125 deletions src/app/Models/Contract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,132 +2,33 @@

namespace App\Models;

use App\Core\Database;
use PDO;
use PDOException;
use App\Core\BaseModel;

class Contract
class Contract extends BaseModel
{
private $id;
private $contract_type;
private $start_date;
private $end_date;
private $salary;

// Constructor per inicialitzar les propietats
public function __construct($data = [])
{
foreach ($data as $key => $value) {
$this->$key = $value;
}
}

// Mètode per obtenir tots els contractes
public static function getAll()
{
$query = "SELECT * FROM contracts";
$results = Database::prepareAndExecute($query);
foreach ($results as $key => $value) {
$results[$key] = new self($value);
}
return $results;
}

// Mètode per obtenir un contracte per ID
public static function findById($id)
{
$query = "SELECT * FROM contracts WHERE id = :id";
$results = Database::prepareAndExecute($query, ['id' => $id]);

return $results ? new self($results[0]) : null;
}

// Mètode per guardar un nou contracte
public function save()
{
$query = "INSERT INTO contracts (contract_type, start_date, end_date, salary)
VALUES (:contract_type, :start_date, :end_date, :salary)";
$params = [
'contract_type' => $this->contract_type,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'salary' => $this->salary,
];

return Database::prepareAndExecute($query, $params);
}

// Mètode per actualitzar un contracte existent
public function update()
{
$query = "UPDATE contracts SET contract_type = :contract_type, start_date = :start_date,
end_date = :end_date, salary = :salary WHERE id = :id";
$params = [
'contract_type' => $this->contract_type,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'salary' => $this->salary,
'id' => $this->id,
];

return Database::prepareAndExecute($query, $params);
}

// Mètode per esborrar un contracte
public function delete()
{
$query = "DELETE FROM contracts WHERE id = :id";
return Database::prepareAndExecute($query, ['id' => $this->id]);
}

// Getters i setters
public function getId()
{
return $this->id;
}

public function getContractType()
{
return $this->contract_type;
}

public function getStartDate()
{
return $this->start_date;
}

public function getEndDate()
{
return $this->end_date;
}

public function getSalary()
{
return $this->salary;
}

public function setId($id)
{
$this->id = $id;
}

public function setContractType($contract_type)
{
$this->contract_type = $contract_type;
}

public function setStartDate($start_date)
{
$this->start_date = $start_date;
}

public function setEndDate($end_date)
{
$this->end_date = $end_date;
}

public function setSalary($salary)
{
$this->salary = $salary;
public $name;
public $start_date;
public $end_date;
public $invoice_proposed;
public $invoice_agreed;
public $invoice_paid;
public $created_at;

protected static function getTableName()
{
return 'contracts';
}

protected static function mapDataToModel($data) {
$contract = new Contract();
$contract->id = $data['id'];
$contract->name = $data['name'];
$contract->start_date = $data['start_date'];
$contract->end_date = $data['end_date'];
$contract->invoice_proposed = $data['invoice_proposed'];
$contract->invoice_agreed = $data['invoice_agreed'];
$contract->invoice_paid = $data['invoice_paid'];
$contract->created_at = $data['created_at'];
return $contract;
}
}
2 changes: 1 addition & 1 deletion src/app/Models/TreeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TreeType extends BaseModel
public string $genus;
public string $species;

protected static function getTableName(): string
protected static function getTableName()
{
return 'tree_types';
}
Expand Down
18 changes: 12 additions & 6 deletions src/app/Views/Contracts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
<thead>
<tr class="bg-gray-100 text-left">
<th class="px-4 py-2 border-b">ID</th>
<th class="px-4 py-2 border-b">Contract Type</th>
<th class="px-4 py-2 border-b">Name</th>
<th class="px-4 py-2 border-b">Start Date</th>
<th class="px-4 py-2 border-b">End Date</th>
<th class="px-4 py-2 border-b">Salary</th>
<th class="px-4 py-2 border-b">Invoice proposed</th>
<th class="px-4 py-2 border-b">Invoice agreed</th>
<th class="px-4 py-2 border-b">Invoice paid</th>
<th class="px-4 py-2 border-b">Created at</th>
</tr>
</thead>
<tbody>
<?php foreach ($contracts as $contract): ?>
<tr class="hover:bg-gray-50">
<td class="px-4 py-2 border-b"><?php echo $contract->getId(); ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->getContractType(); ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->getStartDate(); ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->getEndDate(); ?></td>
<td class="px-4 py-2 border-b"><?php echo number_format($contract->getSalary(), 2); ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->name; ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->start_date; ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->end_date; ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->invoice_proposed; ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->invoice_agreed; ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->invoice_paid; ?></td>
<td class="px-4 py-2 border-b"><?php echo $contract->created_at; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down

0 comments on commit b2fdde2

Please sign in to comment.