388 lines
14 KiB
PHP
388 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Http\ViewComposers;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\View\View;
|
|
|
|
class WorkstationComposer
|
|
{
|
|
public function compose(View $view)
|
|
{
|
|
$userEmail = Auth::user()->email;
|
|
|
|
$equipmentsTodo = DB::select("
|
|
WITH equipments_multiple_rows AS (
|
|
SELECT
|
|
e.equipment_id,
|
|
e.equipment_tag,
|
|
COALESCE(MAX(oet.elemental_tasks_id), MAX(oet.further_tasks_id)) AS equipment_all_tasks
|
|
FROM
|
|
users u
|
|
JOIN
|
|
construction_workstations cw ON u.user_name = cw.name_workstations
|
|
JOIN
|
|
workstations_association_tasks wat ON cw.id_workstations = wat.id_workstations
|
|
JOIN
|
|
equipments e ON wat.company_projects_id = e.company_projects_id
|
|
JOIN
|
|
ordered_equipment_tasks oet ON e.equipment_id = oet.equipment_id
|
|
WHERE
|
|
u.email = :email1
|
|
AND
|
|
oet.execution_order = 1
|
|
GROUP BY
|
|
e.equipment_id,
|
|
e.equipment_tag
|
|
),
|
|
|
|
workstations_multiple_rows AS (
|
|
SELECT
|
|
cw.id_workstations,
|
|
cw.name_workstations,
|
|
COALESCE(wat.elemental_tasks_id, wat.further_tasks_id) AS all_tasks
|
|
FROM
|
|
users u
|
|
JOIN
|
|
construction_workstations cw ON u.user_name = cw.name_workstations
|
|
JOIN
|
|
workstations_association_tasks wat ON cw.id_workstations = wat.id_workstations
|
|
WHERE
|
|
u.email = :email2
|
|
),
|
|
|
|
workstations_control_all_tasks AS (
|
|
SELECT
|
|
cew.control_equipment_workstation_id,
|
|
cew.equipment_id,
|
|
CASE
|
|
WHEN cew.id_workstations IS NULL THEN NULL
|
|
WHEN cew.id_workstations IN (
|
|
SELECT wat.id_workstations
|
|
FROM workstations_association_tasks wat
|
|
JOIN construction_workstations cw ON wat.company_projects_id = cw.company_projects_id
|
|
JOIN users u ON u.user_name = cw.name_workstations
|
|
WHERE u.email = :email3
|
|
) THEN cew.id_workstations
|
|
ELSE NULL
|
|
END AS control_id_workstations,
|
|
COALESCE(cew.elemental_tasks_id, cew.further_tasks_id) AS control_workstation_all_tasks,
|
|
cew.equipment_comments_id,
|
|
cew.entry_date,
|
|
cew.departure_date
|
|
FROM
|
|
control_equipment_workstation cew
|
|
WHERE
|
|
cew.id_workstations IS NULL
|
|
OR cew.id_workstations IN (
|
|
SELECT wat.id_workstations
|
|
FROM workstations_association_tasks wat
|
|
JOIN construction_workstations cw ON wat.company_projects_id = cw.company_projects_id
|
|
JOIN users u ON u.user_name = cw.name_workstations
|
|
WHERE u.email = :email4
|
|
)
|
|
)
|
|
|
|
SELECT
|
|
emr.equipment_id,
|
|
emr.equipment_tag
|
|
FROM
|
|
equipments_multiple_rows emr
|
|
WHERE
|
|
(
|
|
emr.equipment_all_tasks IN (SELECT all_tasks FROM workstations_multiple_rows)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM workstations_control_all_tasks wc
|
|
WHERE wc.equipment_id = emr.equipment_id
|
|
AND wc.control_id_workstations IN (SELECT id_workstations FROM workstations_multiple_rows)
|
|
)
|
|
)
|
|
OR
|
|
(
|
|
emr.equipment_id IN (
|
|
SELECT equipment_id
|
|
FROM workstations_control_all_tasks
|
|
WHERE control_id_workstations IS NULL
|
|
AND entry_date IS NULL
|
|
AND departure_date IS NULL
|
|
AND control_workstation_all_tasks IN (SELECT all_tasks FROM workstations_multiple_rows)
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM workstations_control_all_tasks wc
|
|
WHERE wc.equipment_id = emr.equipment_id
|
|
AND wc.control_id_workstations IS NOT NULL
|
|
GROUP BY wc.equipment_id
|
|
HAVING COUNT(DISTINCT wc.control_id_workstations) > 20
|
|
)
|
|
)
|
|
GROUP BY
|
|
emr.equipment_id,
|
|
emr.equipment_tag
|
|
ORDER BY
|
|
emr.equipment_id ASC;
|
|
", [
|
|
'email1' => $userEmail,
|
|
'email2' => $userEmail,
|
|
'email3' => $userEmail,
|
|
'email4' => $userEmail
|
|
]);
|
|
|
|
|
|
$equipmentsReturned = DB::select("
|
|
WITH equipments_multiple_rows AS (
|
|
SELECT
|
|
e.equipment_id,
|
|
e.equipment_tag,
|
|
COALESCE(MAX(oet.elemental_tasks_id), MAX(oet.further_tasks_id)) AS equipment_all_tasks
|
|
FROM
|
|
users u
|
|
JOIN
|
|
construction_workstations cw ON u.user_name = cw.name_workstations
|
|
JOIN
|
|
workstations_association_tasks wat ON cw.id_workstations = wat.id_workstations
|
|
JOIN
|
|
equipments e ON wat.company_projects_id = e.company_projects_id
|
|
JOIN
|
|
ordered_equipment_tasks oet ON e.equipment_id = oet.equipment_id
|
|
WHERE
|
|
u.email = :email1
|
|
AND
|
|
oet.execution_order = 1
|
|
GROUP BY
|
|
e.equipment_id,
|
|
e.equipment_tag
|
|
),
|
|
|
|
|
|
|
|
workstations_multiple_rows AS (
|
|
SELECT
|
|
cw.id_workstations,
|
|
cw.name_workstations,
|
|
COALESCE(wat.elemental_tasks_id, wat.further_tasks_id) AS all_tasks
|
|
FROM
|
|
users u
|
|
JOIN
|
|
construction_workstations cw ON u.user_name = cw.name_workstations
|
|
JOIN
|
|
workstations_association_tasks wat ON cw.id_workstations = wat.id_workstations
|
|
WHERE
|
|
u.email = :email2
|
|
),
|
|
|
|
|
|
|
|
workstations_control_all_tasks AS (
|
|
SELECT
|
|
cew.control_equipment_workstation_id,
|
|
cew.equipment_id,
|
|
CASE
|
|
WHEN cew.id_workstations IS NULL THEN NULL
|
|
WHEN cew.id_workstations IN (
|
|
SELECT wat.id_workstations
|
|
FROM workstations_association_tasks wat
|
|
JOIN construction_workstations cw ON wat.company_projects_id = cw.company_projects_id
|
|
JOIN users u ON u.user_name = cw.name_workstations
|
|
WHERE u.email = :email3
|
|
) THEN cew.id_workstations
|
|
ELSE NULL
|
|
END AS control_id_workstations,
|
|
COALESCE(cew.elemental_tasks_id, cew.further_tasks_id) AS control_workstation_all_tasks,
|
|
cew.equipment_comments_id,
|
|
cew.entry_date,
|
|
cew.departure_date
|
|
FROM
|
|
control_equipment_workstation cew
|
|
WHERE
|
|
cew.id_workstations IS NULL
|
|
OR cew.id_workstations IN (
|
|
SELECT wat.id_workstations
|
|
FROM workstations_association_tasks wat
|
|
JOIN construction_workstations cw ON wat.company_projects_id = cw.company_projects_id
|
|
JOIN users u ON u.user_name = cw.name_workstations
|
|
WHERE u.email = :email4
|
|
)
|
|
)
|
|
|
|
|
|
|
|
SELECT
|
|
emr.equipment_id,
|
|
emr.equipment_tag
|
|
FROM
|
|
equipments_multiple_rows emr
|
|
WHERE
|
|
emr.equipment_id IN (
|
|
SELECT equipment_id
|
|
FROM workstations_control_all_tasks
|
|
WHERE control_id_workstations IS NULL
|
|
AND entry_date IS NULL
|
|
AND departure_date IS NULL
|
|
AND control_workstation_all_tasks IN (SELECT all_tasks FROM workstations_multiple_rows)
|
|
AND EXISTS (
|
|
SELECT 1
|
|
FROM workstations_control_all_tasks wc
|
|
WHERE wc.equipment_id = emr.equipment_id
|
|
AND wc.control_id_workstations IS NOT NULL
|
|
GROUP BY wc.equipment_id
|
|
HAVING COUNT(DISTINCT wc.control_id_workstations) >= 20
|
|
|
|
)
|
|
)
|
|
GROUP BY
|
|
emr.equipment_id,
|
|
emr.equipment_tag
|
|
ORDER BY
|
|
emr.equipment_id ASC;
|
|
|
|
", [
|
|
|
|
'email1' => $userEmail,
|
|
'email2' => $userEmail,
|
|
'email3' => $userEmail,
|
|
'email4' => $userEmail
|
|
|
|
]);
|
|
|
|
|
|
$equipmentsDone = DB::select("
|
|
WITH equipments_multiple_rows AS (
|
|
SELECT
|
|
e.equipment_id,
|
|
e.equipment_tag,
|
|
COALESCE(MAX(oet.elemental_tasks_id), MAX(oet.further_tasks_id)) AS equipment_all_tasks
|
|
FROM
|
|
users u
|
|
JOIN
|
|
construction_workstations cw ON u.user_name = cw.name_workstations
|
|
JOIN
|
|
workstations_association_tasks wat ON cw.id_workstations = wat.id_workstations
|
|
JOIN
|
|
equipments e ON wat.company_projects_id = e.company_projects_id
|
|
JOIN
|
|
ordered_equipment_tasks oet ON e.equipment_id = oet.equipment_id
|
|
WHERE
|
|
u.email = :email1
|
|
AND
|
|
oet.execution_order = 1
|
|
GROUP BY
|
|
e.equipment_id,
|
|
e.equipment_tag
|
|
),
|
|
|
|
workstations_multiple_rows AS (
|
|
SELECT
|
|
cw.id_workstations,
|
|
cw.name_workstations,
|
|
COALESCE(wat.elemental_tasks_id, wat.further_tasks_id) AS all_tasks
|
|
FROM
|
|
users u
|
|
JOIN
|
|
construction_workstations cw ON u.user_name = cw.name_workstations
|
|
JOIN
|
|
workstations_association_tasks wat ON cw.id_workstations = wat.id_workstations
|
|
WHERE
|
|
u.email = :email2
|
|
),
|
|
|
|
workstations_control_all_tasks AS (
|
|
SELECT
|
|
cew.control_equipment_workstation_id,
|
|
cew.equipment_id,
|
|
CASE
|
|
WHEN cew.id_workstations IS NULL THEN NULL
|
|
WHEN cew.id_workstations IN (
|
|
SELECT wat.id_workstations
|
|
FROM workstations_association_tasks wat
|
|
JOIN construction_workstations cw ON wat.company_projects_id = cw.company_projects_id
|
|
JOIN users u ON u.user_name = cw.name_workstations
|
|
WHERE u.email = :email3
|
|
) THEN cew.id_workstations
|
|
ELSE NULL
|
|
END AS control_id_workstations,
|
|
COALESCE(cew.elemental_tasks_id, cew.further_tasks_id) AS control_workstation_all_tasks,
|
|
cew.equipment_comments_id,
|
|
cew.entry_date,
|
|
cew.departure_date
|
|
FROM
|
|
control_equipment_workstation cew
|
|
WHERE
|
|
cew.id_workstations IS NULL
|
|
OR cew.id_workstations IN (
|
|
SELECT wat.id_workstations
|
|
FROM workstations_association_tasks wat
|
|
JOIN construction_workstations cw ON wat.company_projects_id = cw.company_projects_id
|
|
JOIN users u ON u.user_name = cw.name_workstations
|
|
WHERE u.email = :email4
|
|
)
|
|
)
|
|
|
|
SELECT
|
|
emr.equipment_id,
|
|
emr.equipment_tag
|
|
FROM
|
|
equipments_multiple_rows emr
|
|
WHERE
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM workstations_control_all_tasks wc
|
|
JOIN workstations_multiple_rows wmr ON wc.control_id_workstations = wmr.id_workstations
|
|
WHERE wc.equipment_id = emr.equipment_id
|
|
AND wc.control_workstation_all_tasks = wmr.all_tasks
|
|
AND wc.entry_date IS NOT NULL
|
|
AND wc.departure_date IS NOT NULL
|
|
AND wc.control_id_workstations IS NOT NULL
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1
|
|
FROM workstations_control_all_tasks wc
|
|
WHERE wc.equipment_id = emr.equipment_id
|
|
AND wc.control_workstation_all_tasks IN (SELECT all_tasks FROM workstations_multiple_rows)
|
|
AND wc.control_id_workstations IS NULL
|
|
)
|
|
GROUP BY
|
|
emr.equipment_id,
|
|
emr.equipment_tag
|
|
ORDER BY
|
|
emr.equipment_id ASC;
|
|
", [
|
|
'email1' => $userEmail,
|
|
'email2' => $userEmail,
|
|
'email3' => $userEmail,
|
|
'email4' => $userEmail
|
|
]);
|
|
|
|
$workstationNameQuery = DB::select("
|
|
SELECT construction_workstations.nomenclature_workstation
|
|
FROM construction_workstations
|
|
JOIN users
|
|
WHERE construction_workstations.name_workstations = users.user_name
|
|
AND users.email = :email;
|
|
", ['email' => $userEmail]);
|
|
|
|
$workstationName = $workstationNameQuery[0]->nomenclature_workstation;
|
|
|
|
|
|
//Counting of equipments
|
|
$equipmentsTodoCount = count($equipmentsTodo);
|
|
$equipmentsDoneCount = count($equipmentsDone);
|
|
$equipmentsReturnedCount = count($equipmentsReturned);
|
|
|
|
|
|
//Returning values of the queries to workstations layout
|
|
$view->with([
|
|
'equipmentsTodo' => $equipmentsTodo,
|
|
'equipmentsTodoCount' => $equipmentsTodoCount,
|
|
'equipmentsDone' => $equipmentsDone,
|
|
'equipmentsDoneCount' => $equipmentsDoneCount,
|
|
'equipmentsReturned' => $equipmentsReturned,
|
|
'equipmentsReturnedCount' => $equipmentsReturnedCount,
|
|
'workstationName' => $workstationName
|
|
]);
|
|
}
|
|
}
|