From faf721a99a8e4de1d5b0c1e675c2f57a12545993 Mon Sep 17 00:00:00 2001 From: 24Victor Date: Sat, 16 Nov 2024 17:40:33 +0100 Subject: [PATCH 1/4] feat(work-order): add Model and Controller --- src/app/Controllers/OrderController.php | 24 ++++++++++++++++ src/app/Models/Order.php | 38 +++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/app/Controllers/OrderController.php create mode 100644 src/app/Models/Order.php diff --git a/src/app/Controllers/OrderController.php b/src/app/Controllers/OrderController.php new file mode 100644 index 00000000..d17dcec2 --- /dev/null +++ b/src/app/Controllers/OrderController.php @@ -0,0 +1,24 @@ + "order/index", + "title" => "Order", + "layout" => "MainLayout", + "data" => ["orders" => $orders] + ]); + } + + public function post() {} + public function put() {} + public function delete() {} + +} \ No newline at end of file diff --git a/src/app/Models/Order.php b/src/app/Models/Order.php new file mode 100644 index 00000000..e78acc91 --- /dev/null +++ b/src/app/Models/Order.php @@ -0,0 +1,38 @@ +id = $data['id']; + $order->name = $data['name']; + $order->contract_id = $data['contract_id']; + $order->notes = $data['notes']; + $order->created_at = $data['created_at']; + + return $order; + } + + public function getCreatedAt(){ + return $this->created_at; + } + + public function contract(){ + return $this->belongsTo(Contract::class, 'contract_id', 'id'); + } +} \ No newline at end of file From 2c4943e4517e8bd758c12d9df8220de08cf55fd1 Mon Sep 17 00:00:00 2001 From: 0x1026 <69076992+0x1026@users.noreply.github.com> Date: Sun, 17 Nov 2024 18:08:41 +0100 Subject: [PATCH 2/4] feat(BaseModel): add belongsToMany method for many-to-many relationships --- src/app/Core/BaseModel.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/app/Core/BaseModel.php b/src/app/Core/BaseModel.php index 697037f7..642a1d5f 100644 --- a/src/app/Core/BaseModel.php +++ b/src/app/Core/BaseModel.php @@ -6,7 +6,7 @@ abstract class BaseModel { protected int $id; - // Get the related model in a one-to-one relationship + // One-to-one relationship public function belongsTo($relatedModel, $foreignKey, $ownerKey = 'id') { $relatedTable = $relatedModel::getTableName(); @@ -18,6 +18,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(); From 9d096784e81bc9e6ad1c63ba765cd2baad4e1f6a Mon Sep 17 00:00:00 2001 From: 24Victor Date: Sun, 17 Nov 2024 19:37:45 +0100 Subject: [PATCH 3/4] Wip --- docker/database/start-scripts/0-init.sql | 66 ++++++++++++---------- docker/database/start-scripts/1-seed.sql | 16 ++++++ src/app/Controllers/OrderController.php | 4 +- src/app/Models/Order.php | 9 ++- src/app/Models/Task.php | 47 ++++++++++++++++ src/app/Views/order/create.php | 31 ++++++++++ src/app/Views/order/index.php | 72 ++++++++++++++++++++++++ src/app/routes.php | 3 + 8 files changed, 212 insertions(+), 36 deletions(-) create mode 100644 src/app/Models/Task.php create mode 100644 src/app/Views/order/create.php create mode 100644 src/app/Views/order/index.php diff --git a/docker/database/start-scripts/0-init.sql b/docker/database/start-scripts/0-init.sql index ce9221d7..c3e115fb 100644 --- a/docker/database/start-scripts/0-init.sql +++ b/docker/database/start-scripts/0-init.sql @@ -5,6 +5,11 @@ create table points ( constraint UC_Point unique (latitude, longitude) ); +create table task_types ( + id int auto_increment primary key, + name varchar(255) unique +); + create table zones ( id int auto_increment primary key, name varchar(255) not null, @@ -48,17 +53,17 @@ create table incidences ( create table roles ( id int auto_increment primary key, - name varchar(255) unique + name varchar(255) ); create table workers ( id int auto_increment primary key, company varchar(255), name varchar(255), - dni varchar(255) unique, + dni varchar(255) unique, password varchar(255), - email varchar(255), - role_id int, + email varchar(255), + role_id int, created_at timestamp, deleted_at timestamp, updated_at timestamp, @@ -81,10 +86,11 @@ CREATE TABLE contracts ( create table work_orders ( id int auto_increment primary key, - name varchar(255), - created_at timestamp, + contract_id int, + created_at timestamp default current_timestamp, deleted_at timestamp, - updated_at timestamp + updated_at timestamp, + foreign key (contract_id) references contracts(id) ); create table machines ( @@ -103,33 +109,22 @@ create table parts ( create table routes ( id int auto_increment primary key, - distance float, - point_id int, - travel_time int, + distance float, + point_id int, + travel_time int, foreign key (point_id) references points(id) ); 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, - status BIT, - part_id int, - history_id int, + 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 worker_tasks ( +create table tasks_workers ( id int auto_increment primary key, task_id int, worker_id int, @@ -137,6 +132,22 @@ create table worker_tasks ( 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) +); + create table sensors ( id int auto_increment primary key, entidad_vegetal int, @@ -163,8 +174,3 @@ create table pruning_types ( name varchar(20) unique, description varchar(255) ); - -create table task_types ( - id int auto_increment primary key, - name varchar(255) unique -); diff --git a/docker/database/start-scripts/1-seed.sql b/docker/database/start-scripts/1-seed.sql index 30a4e040..c1ebfa9e 100644 --- a/docker/database/start-scripts/1-seed.sql +++ b/docker/database/start-scripts/1-seed.sql @@ -60,3 +60,19 @@ INSERT INTO incidences (name, element_id, description) VALUES ('Árbol enfermo', 1, 'Árbol con signos de enfermedad'), ('Banco pintado', 2, 'Banco pintado con grafitis'), ('Fuente con fuga', 3, 'Fuente con fuga de agua'); + +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.'); + + + + + + diff --git a/src/app/Controllers/OrderController.php b/src/app/Controllers/OrderController.php index d17dcec2..bc2423ee 100644 --- a/src/app/Controllers/OrderController.php +++ b/src/app/Controllers/OrderController.php @@ -17,7 +17,9 @@ public function get() ]); } - public function post() {} + public function post() { + + } public function put() {} public function delete() {} diff --git a/src/app/Models/Order.php b/src/app/Models/Order.php index e78acc91..aefdaeae 100644 --- a/src/app/Models/Order.php +++ b/src/app/Models/Order.php @@ -2,13 +2,10 @@ namespace App\Models; use App\Core\BaseModel; -use App\Core\Database; class Order extends BaseModel { - public string $name; public int $contract_id; - public string $notes; protected $created_at; protected static function getTableName() @@ -20,9 +17,7 @@ protected static function mapDataToModel($data) { $order = new Order(); $order->id = $data['id']; - $order->name = $data['name']; $order->contract_id = $data['contract_id']; - $order->notes = $data['notes']; $order->created_at = $data['created_at']; return $order; @@ -35,4 +30,8 @@ public function getCreatedAt(){ public function contract(){ return $this->belongsTo(Contract::class, 'contract_id', 'id'); } + + public function tasks(){ + return $this->hasMany(Task::class, 'work_order_id', 'id'); + } } \ No newline at end of file diff --git a/src/app/Models/Task.php b/src/app/Models/Task.php new file mode 100644 index 00000000..4c96c479 --- /dev/null +++ b/src/app/Models/Task.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(Order::class, 'work_order_id', 'id'); + } + + // Many-to-Many relationship with Post + public function zones() + { + return $this->belongsToMany(Zone::class, 'tasks_zones', 'task_id', 'zone_id'); + } + + public function workers() + { + return $this->belongsToMany(Worker::class, 'tasks_workers', 'task_id', 'worker_id'); + } + + public function taskTypes() + { + return $this->belongsToMany(TaskType::class, 'tasks_tasktypes', 'task_id', 'tasktype_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..c4bdcee2 --- /dev/null +++ b/src/app/Views/order/index.php @@ -0,0 +1,72 @@ + + + + + + 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 diff --git a/src/app/routes.php b/src/app/routes.php index da816284..2309b17e 100644 --- a/src/app/routes.php +++ b/src/app/routes.php @@ -12,8 +12,11 @@ use App\Controllers\HomeController; use App\Controllers\AuthController; +use App\Controllers\OrderController; return $routes = [ "/" => HomeController::class, "/login" => AuthController::class, + "/order" => OrderController::class, + "/order/create" => OrderController::class, ]; From 891b69e874a581eafbd98bda0583b686112348b0 Mon Sep 17 00:00:00 2001 From: 24Victor Date: Sun, 17 Nov 2024 22:22:41 +0100 Subject: [PATCH 4/4] feat: MVC --- docker/database/start-scripts/1-seed.sql | 15 ++++ src/app/Controllers/CWorkOrder.php | 60 ++++++++++++++ src/app/Controllers/OrderController.php | 26 ------ src/app/Models/{Task.php => MTask.php} | 16 ++-- src/app/Models/{Order.php => MWorkOrder.php} | 21 +++-- src/app/Views/order/index.php | 83 ++++++++++---------- 6 files changed, 134 insertions(+), 87 deletions(-) create mode 100644 src/app/Controllers/CWorkOrder.php delete mode 100644 src/app/Controllers/OrderController.php rename src/app/Models/{Task.php => MTask.php} (57%) rename src/app/Models/{Order.php => MWorkOrder.php} (52%) diff --git a/docker/database/start-scripts/1-seed.sql b/docker/database/start-scripts/1-seed.sql index 53947d31..d18efacf 100644 --- a/docker/database/start-scripts/1-seed.sql +++ b/docker/database/start-scripts/1-seed.sql @@ -59,3 +59,18 @@ 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/Controllers/OrderController.php b/src/app/Controllers/OrderController.php deleted file mode 100644 index bc2423ee..00000000 --- a/src/app/Controllers/OrderController.php +++ /dev/null @@ -1,26 +0,0 @@ - "order/index", - "title" => "Order", - "layout" => "MainLayout", - "data" => ["orders" => $orders] - ]); - } - - public function post() { - - } - public function put() {} - public function delete() {} - -} \ No newline at end of file diff --git a/src/app/Models/Task.php b/src/app/Models/MTask.php similarity index 57% rename from src/app/Models/Task.php rename to src/app/Models/MTask.php index 4c96c479..53e4f694 100644 --- a/src/app/Models/Task.php +++ b/src/app/Models/MTask.php @@ -3,11 +3,11 @@ namespace App\Models; use App\Core\BaseModel; -use App\Models\Order; +use App\Models\MWorkOrder; -class Task extends BaseModel +class MTask extends BaseModel { - public ?string$notes; + public ?string $notes; public $work_order_id; protected static function getTableName() @@ -17,7 +17,7 @@ protected static function getTableName() protected static function mapDataToModel($data) { - $task = new Task(); + $task = new MTask(); $task->id = $data['id']; $task->notes = $data['notes']; $task->work_order_id = $data['work_order_id']; @@ -26,22 +26,22 @@ protected static function mapDataToModel($data) } public function Order() { - return $this->belongsTo(Order::class, 'work_order_id', 'id'); + return $this->belongsTo(MWorkOrder::class, 'work_order_id', 'id'); } // Many-to-Many relationship with Post public function zones() { - return $this->belongsToMany(Zone::class, 'tasks_zones', 'task_id', 'zone_id'); + return $this->belongsToMany(MZone::class, 'tasks_zones', 'task_id', 'zone_id'); } public function workers() { - return $this->belongsToMany(Worker::class, 'tasks_workers', 'task_id', 'worker_id'); + return $this->belongsToMany(MWorker::class, 'tasks_workers', 'task_id', 'worker_id'); } public function taskTypes() { - return $this->belongsToMany(TaskType::class, 'tasks_tasktypes', 'task_id', 'tasktype_id'); + return $this->belongsToMany(MTaskType::class, 'tasks_tasktypes', 'task_id', 'tasktype_id'); } } \ No newline at end of file diff --git a/src/app/Models/Order.php b/src/app/Models/MWorkOrder.php similarity index 52% rename from src/app/Models/Order.php rename to src/app/Models/MWorkOrder.php index aefdaeae..96978da0 100644 --- a/src/app/Models/Order.php +++ b/src/app/Models/MWorkOrder.php @@ -3,10 +3,9 @@ use App\Core\BaseModel; -class Order extends BaseModel -{ +class MWorkOrder extends BaseModel +{ public int $contract_id; - protected $created_at; protected static function getTableName() { @@ -15,7 +14,7 @@ protected static function getTableName() protected static function mapDataToModel($data) { - $order = new Order(); + $order = new MWorkOrder(); $order->id = $data['id']; $order->contract_id = $data['contract_id']; $order->created_at = $data['created_at']; @@ -23,15 +22,13 @@ protected static function mapDataToModel($data) return $order; } - public function getCreatedAt(){ - return $this->created_at; - } - - public function contract(){ - return $this->belongsTo(Contract::class, 'contract_id', 'id'); + public function contract() + { + return $this->belongsTo(MContract::class, 'contract_id', 'id'); } - public function tasks(){ - return $this->hasMany(Task::class, 'work_order_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/index.php b/src/app/Views/order/index.php index c4bdcee2..ab28424c 100644 --- a/src/app/Views/order/index.php +++ b/src/app/Views/order/index.php @@ -1,32 +1,35 @@ - + + Llista d'Ordres - - -

Ordre de Treball

- - - - - - - - - - - - - - - - - - tasks() as $task): ?> + + + +

Ordre de Treball

+ + +
ContracteDataZonesTasquesOperarisNotesEstatOpcions
+ + + + + + + + + + + + + + + tasks() as $task): ?> @@ -48,25 +51,23 @@ - - -
ContracteDataZonesTasquesOperarisNotesEstatOpcions
contract()->name; ?> getCreatedAt(); ?>notes; ?> - + - +
+ +
- - + + + + + \ No newline at end of file