diff --git a/docker/database/start-scripts/0-init.sql b/docker/database/start-scripts/0-init.sql index 0889690e..ac84e639 100644 --- a/docker/database/start-scripts/0-init.sql +++ b/docker/database/start-scripts/0-init.sql @@ -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 ( @@ -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, diff --git a/docker/database/start-scripts/1-seed.sql b/docker/database/start-scripts/1-seed.sql index 52689a81..d18efacf 100644 --- a/docker/database/start-scripts/1-seed.sql +++ b/docker/database/start-scripts/1-seed.sql @@ -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', - 'carlos.garcia@example.com', - 1 - ), - ( - 'InnovaTech', - 'Ana Martínez', - '23456789B', - 'hashedpassword2', - 'ana.martinez@example.com', - 2 - ), - ( - 'DesignWorks', - 'José Rodríguez', - '34567890C', - 'hashedpassword3', - 'jose.rodriguez@example.com', - 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', 'carlos.garcia@example.com', 1, NOW(), NOW(), NULL), +('InnovaTech', 'Ana Martínez', '23456789B', 'hashedpassword2', 'ana.martinez@example.com', 2, NOW(), NOW(), NULL), +('DesignWorks', 'José Rodríguez', '34567890C', 'hashedpassword3', 'jose.rodriguez@example.com', 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 \ No newline at end of file +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); diff --git a/src/app/Controllers/CWorkOrder.php b/src/app/Controllers/CWorkOrder.php new file mode 100644 index 00000000..6bf06e08 --- /dev/null +++ b/src/app/Controllers/CWorkOrder.php @@ -0,0 +1,60 @@ + "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() + { + + } +} \ No newline at end of file diff --git a/src/app/Core/BaseModel.php b/src/app/Core/BaseModel.php index 4deff38c..7cfee611 100644 --- a/src/app/Core/BaseModel.php +++ b/src/app/Core/BaseModel.php @@ -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(); @@ -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(); diff --git a/src/app/Models/MTask.php b/src/app/Models/MTask.php new file mode 100644 index 00000000..53e4f694 --- /dev/null +++ b/src/app/Models/MTask.php @@ -0,0 +1,47 @@ +id = $data['id']; + $task->notes = $data['notes']; + $task->work_order_id = $data['work_order_id']; + $task->created_at = $data['created_at']; + return $task; + } + + public function Order() { + return $this->belongsTo(MWorkOrder::class, 'work_order_id', 'id'); + } + + // Many-to-Many relationship with Post + public function zones() + { + return $this->belongsToMany(MZone::class, 'tasks_zones', 'task_id', 'zone_id'); + } + + public function workers() + { + return $this->belongsToMany(MWorker::class, 'tasks_workers', 'task_id', 'worker_id'); + } + + public function taskTypes() + { + return $this->belongsToMany(MTaskType::class, 'tasks_tasktypes', 'task_id', 'tasktype_id'); + } +} \ No newline at end of file diff --git a/src/app/Models/MWorkOrder.php b/src/app/Models/MWorkOrder.php new file mode 100644 index 00000000..96978da0 --- /dev/null +++ b/src/app/Models/MWorkOrder.php @@ -0,0 +1,34 @@ +id = $data['id']; + $order->contract_id = $data['contract_id']; + $order->created_at = $data['created_at']; + + return $order; + } + + public function contract() + { + return $this->belongsTo(MContract::class, 'contract_id', 'id'); + } + + public function tasks() + { + return $this->hasMany(MTask::class, 'work_order_id', 'id'); + } +} \ No newline at end of file diff --git a/src/app/Views/order/create.php b/src/app/Views/order/create.php new file mode 100644 index 00000000..970605b5 --- /dev/null +++ b/src/app/Views/order/create.php @@ -0,0 +1,31 @@ + + + + + + Crear Ordres + + +

Crear Ordre de Treball

+ +
+ + + + + + + + + + +
+ + \ No newline at end of file diff --git a/src/app/Views/order/index.php b/src/app/Views/order/index.php new file mode 100644 index 00000000..ab28424c --- /dev/null +++ b/src/app/Views/order/index.php @@ -0,0 +1,73 @@ + + + + + + + Llista d'Ordres + + + +

Ordre de Treball

+ + + + + + + + + + + + + + + + + + tasks() as $task): ?> + + + + + + + + + + + + + +
ContracteDataZonesTasquesOperarisNotesEstatOpcions
contract()->name; ?>getCreatedAt(); ?> + zones() as $zone): ?> + name; ?>, + + + taskTypes() as $task_type): ?> + name; ?>, + + + workers() as $worker): ?> + name; ?>, + + notes; ?> + + +
+ +
+
+ + + \ No newline at end of file