Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(work-order): add WorkOrder #73

Merged
merged 8 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions docker/database/start-scripts/0-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ create table incidences (
--TODO: tasks, routes and works
create table work_orders (
id int auto_increment primary key,
name varchar(255),
contract_id int,
created_at timestamp default current_timestamp,
deleted_at timestamp,
updated_at timestamp,
deleted_at timestamp
foreign key (contract_id) references contracts(id)
);

create table parts (
Expand All @@ -136,34 +137,38 @@ create table routes (
);

create table tasks (
id int auto_increment primary key,
task_name varchar(255),
id int auto_increment primary key,
work_order_id int,
description varchar(255),
element_id int,
machine_id int,
route_id int,
part_id int,
history_id int,
created_at timestamp default current_timestamp,
updated_at timestamp,
notes varchar(255),
created_at timestamp,
deleted_at timestamp,
foreign key (work_order_id) references work_orders(id),
foreign key (element_id) references elements(id),
foreign key (machine_id) references machines(id),
foreign key (route_id) references routes(id),
foreign key (part_id) references parts(id)
foreign key (work_order_id) references work_orders(id)
);

create table task_workers (
create table tasks_workers (
id int auto_increment primary key,
task_id int,
worker_id int,
created_at timestamp default current_timestamp,
foreign key (task_id) references tasks(id),
foreign key (worker_id) references workers(id)
);

create table tasks_zones (
id int auto_increment primary key,
task_id int,
zone_id int,
foreign key (task_id) references tasks(id),
foreign key (zone_id) references zones(id)
);

create table tasks_tasktypes (
id int auto_increment primary key,
task_id int,
tasktype_id int,
foreign key (task_id) references tasks(id),
foreign key (tasktype_id) references task_types(id)
);

--* FUTURE: sensors and sensor history
-- create table sensors (
-- id int auto_increment primary key,
Expand Down
193 changes: 72 additions & 121 deletions docker/database/start-scripts/1-seed.sql
Original file line number Diff line number Diff line change
@@ -1,125 +1,76 @@
--* Roles, users, contracts and machines
INSERT INTO roles (name)
VALUES ('Administrador'),
('Gerente'),
('Trabajador');
INSERT INTO workers (
company,
name,
dni,
password,
email,
role_id
)
VALUES (
'TechCorp',
'Carlos García',
'12345678A',
'hashedpassword1',
'[email protected]',
1
),
(
'InnovaTech',
'Ana Martínez',
'23456789B',
'hashedpassword2',
'[email protected]',
2
),
(
'DesignWorks',
'José Rodríguez',
'34567890C',
'hashedpassword3',
'[email protected]',
3
);
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
);
INSERT INTO machines (name, max_basket_size)
VALUES ('Cesta elevadora', 200.00),
('Plataforma elevadora', 300.00),
('Tijera elevadora', 400.00);
INSERT INTO roles (name) VALUES
('Administrador'),
('Gerente'),
('Trabajador');
INSERT INTO workers (company, name, dni, password, email, role_id, created_at, updated_at, deleted_at) VALUES
('TechCorp', 'Carlos García', '12345678A', 'hashedpassword1', '[email protected]', 1, NOW(), NOW(), NULL),
('InnovaTech', 'Ana Martínez', '23456789B', 'hashedpassword2', '[email protected]', 2, NOW(), NOW(), NULL),
('DesignWorks', 'José Rodríguez', '34567890C', 'hashedpassword3', '[email protected]', 3, NOW(), NOW(), NULL);
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);
INSERT INTO machines (name, max_basket_size) VALUES
('Cesta elevadora', 200.00),
('Plataforma elevadora', 300.00),
('Tijera elevadora', 400.00);
--* Tree, task and pruning types
INSERT INTO tree_types (family, genus, species)
VALUES ('Fagaceae', 'Quercus', 'Quercus robur'),
('Pinaceae', 'Pinus', 'Pinus sylvestris'),
('Sapindaceae', 'Acer', 'Acer campestre');
INSERT INTO task_types (name)
VALUES ('Abono arbustos'),
('Podar setos'),
('Abono setos');
INSERT INTO pruning_types (name, description)
VALUES (
'A',
'Poda de mantenimiento en árbol tipo A, caduco, de p.c. entre 41/80 cm.'
),
(
'B',
'Poda de mantenimiento en árbol tipo B, caduco, de p.c. mayor de 81 cm.'
),
(
'C',
'Poda de mantenimiento en árbol tipo C, perenne, de p.c. entre 41/60 cm.'
);
INSERT INTO tree_types (family, genus, species) VALUES
('Fagaceae', 'Quercus', 'Quercus robur'),
('Pinaceae', 'Pinus', 'Pinus sylvestris'),
('Sapindaceae', 'Acer', 'Acer campestre');
INSERT INTO task_types (name) VALUES
('Abono arbustos'),
('Podar setos'),
('Abono setos');
INSERT INTO pruning_types (name, description) VALUES
('A', 'Poda de mantenimiento en árbol tipo A, caduco, de p.c. entre 41/80 cm.'),
('B', 'Poda de mantenimiento en árbol tipo B, caduco, de p.c. mayor de 81 cm.'),
('C', 'Poda de mantenimiento en árbol tipo C, perenne, de p.c. entre 41/60 cm.');
--* Points and zones
INSERT INTO points (latitude, longitude)
VALUES (40.416775, -3.703790),
(40.416776, -3.703795),
(40.416777, -3.703800);
INSERT INTO zones (name, postal_code, point_id)
VALUES ('Zona 1', '46001', 1),
('Zona 2', '46002', 2),
('Zona 3', '46003', 3);
INSERT INTO points (latitude, longitude) VALUES
(40.416775, -3.703790),
(40.416776, -3.703795),
(40.416777, -3.703800);
INSERT INTO zones (name, postal_code, point_id) VALUES
('Zona 1', '46001', 1),
('Zona 2', '46002', 2),
('Zona 3', '46003', 3);
--* Elements and incidences
INSERT INTO elements (name, zone_id, point_id, tree_type_id)
VALUES ('Árbol 1', 1, 1, 1),
('Árbol 2', 2, 2, 2),
('Árbol 3', 3, 3, 3);
INSERT INTO incidences (name, element_id, description)
VALUES ('Rama caída', 1, 'Rama caída en el suelo'),
('Banco roto', 2, 'Banco roto en el parque'),
(
'Fuente sin agua',
3,
'Fuente sin agua en el parque'
),
(
'Árbol enfermo',
1,
'Árbol con signos de enfermedad'
),
('Banco pintado', 2, 'Banco pintado con grafitis'),
('Fuente con fuga', 3, 'Fuente con fuga de agua');
--TODO: tasks, routes and works
INSERT INTO elements (name, zone_id, point_id, tree_type_id) VALUES
('Árbol 1', 1, 1, 1),
('Árbol 2', 2, 2, 2),
('Árbol 3', 3, 3, 3);
INSERT INTO incidences (name, element_id, description) VALUES
('Rama caída', 1, 'Rama caída en el suelo'),
('Banco roto', 2, 'Banco roto en el parque'),
('Fuente sin agua', 3, 'Fuente sin agua en el parque'),
('Árbol enfermo', 1, 'Árbol con signos de enfermedad'),
('Banco pintado', 2, 'Banco pintado con grafitis'),
('Fuente con fuga', 3, 'Fuente con fuga de agua');
--TODO: tasks, routes and works
INSERT INTO work_orders (contract_id) VALUES
(1),
(2),
(3);

INSERT INTO tasks (work_order_id, notes) VALUES
(1, 'Poda de mantenimiento en árbol tipo A, caduco, de p.c. entre 41/80 cm.'),
(2, 'Poda de mantenimiento en árbol tipo B, caduco, de p.c. entre 50/100 cm.'),
(3, 'Poda de mantenimiento en árbol tipo C, caduco, de p.c. entre 60/120 cm.');

INSERT INTO tasks_zones (task_id, zone_id) VALUES
(1, 1),
(2, 2),
(3, 3);

INSERT INTO tasks_tasktypes (task_id, tasktype_id) VALUES
(1, 1),
(2, 2),
(3, 3);

INSERT INTO tasks_workers (task_id, worker_id) VALUES
(1, 1),
(2, 2),
(3, 3);
60 changes: 60 additions & 0 deletions src/app/Controllers/CWorkOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace App\Controllers;

use App\Core\View;
use App\Models\MWorkOrder;
class CWorkOrder
{
public function findAll()
{
$orders = MWorkOrder::findAll();
View::render([
"view" => "order/index",
"title" => "Order",
"layout" => "MainLayout",
"data" => ["orders" => $orders]
]);
}

public function find()
{
$id = $_GET['id'];
$order = MWorkOrder::find($id);
View::render([
"view" => "order",
"title" => "Order",
"layout" => "MainLayout",
"data" => ["order" => $order]
]);
}

public function indexCreate()
{
$orders = MWorkOrder::findAll();
View::render([
"view" => "order/create",
"title" => "Create Order",
"layout" => "MainLayout",
"data" => ["orders" => $orders]
]);
}

public function save()
{
$order = new MWorkOrder();

$order->save();
}

public function delete()
{
$id = $_GET['id'];
$order = MWorkOrder::find($id);
$order->delete();
}

public function update()
{

}
}
21 changes: 20 additions & 1 deletion src/app/Core/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ abstract class BaseModel
protected int $id;
protected $created_at;

// Get the related model in a one-to-one relationship
// One-to-one relationship
public function belongsTo($relatedModel, $foreignKey, $ownerKey = 'id')
{
$relatedTable = $relatedModel::getTableName();
Expand All @@ -19,6 +19,25 @@ public function belongsTo($relatedModel, $foreignKey, $ownerKey = 'id')
return !empty($results) ? $relatedModel::mapDataToModel($results[0]) : null;
}


// Many-to-Many relationship
public function belongsToMany($relatedModel, $pivotTable, $foreignKey, $relatedKey, $ownerKey = 'id', $relatedOwnerKey = 'id')
{
$relatedTable = $relatedModel::getTableName();
$localKeyValue = $this->{$ownerKey};

$query = "
SELECT $relatedTable.*
FROM $relatedTable
INNER JOIN $pivotTable ON $pivotTable.$relatedKey = $relatedTable.$relatedOwnerKey
WHERE $pivotTable.$foreignKey = :localKeyValue
";

$results = Database::prepareAndExecute($query, ['localKeyValue' => $localKeyValue]);

return array_map(fn($row) => $relatedModel::mapDataToModel($row), $results);
}

public function delete()
{
$table = static::getTableName();
Expand Down
Loading