Untitled
else if ($action == 'getPurchaseRequest'){ $query->leftJoin('departments', 'departments.id', '=', 'purchase_requests.department_id'); $query->leftJoin('employees', 'employees.department_id', '=', 'departments.id'); $query->leftJoin('users', 'users.employee_id', '=', 'employees.id'); $query->leftJoin('warehouses', 'warehouses.id', '=', 'purchase_requests.warehouse_id'); $query->leftJoin('users as created_user', 'created_user.id', '=', 'purchase_requests.created_by'); $query->leftJoin('users as updated_user', 'updated_user.id', '=', 'purchase_requests.updated_by'); $query->leftJoin('users as deleted_user', 'deleted_user.id', '=', 'purchase_requests.deleted_by'); $query->leftJoin('users as approved_user_1', 'approved_user_1.id', '=', 'purchase_requests.approved_1_by'); $query->leftJoin('users as approved_user_2', 'approved_user_2.id', '=', 'purchase_requests.approved_2_by'); $query->leftJoin('users as canceled_user', 'canceled_user.id', '=', 'purchase_requests.canceled_by'); // $query->where('users.id', auth()->id()); // Cek apakah user memiliki akses approval $user_groups = $this->authController->getUserGroups(); $canApprove = false; $menu_access = false; foreach ($user_groups as $user_group) { $haystack = strtolower($user_group->user_group_name); if (str_contains($haystack, 'department manager') || str_contains($haystack, 'general manager') || str_contains($haystack, 'admin') || str_contains($haystack, 'all')) { $canApprove = true; break; } } // Filter berdasarkan user ID jika user tidak memiliki akses approval if ($canApprove) { $query->where('users.id', auth()->id()); } $query->select( 'purchase_requests.id as id', DB::raw("'<input type=checkbox name=select value=' || purchase_requests.id || '>' as is_checked"), 'purchase_requests.pr_number as pr_number', 'purchase_requests.needs as needs', 'purchase_requests.remark_pr as remark_pr', 'purchase_requests.is_approve as is_approve', 'purchase_requests.approved_1_status as approved_1_status', 'purchase_requests.approved_2_status as approved_2_status', 'purchase_requests.created_at as created_at', 'purchase_requests.updated_at as updated_at', 'purchase_requests.deleted_at as deleted_at', 'purchase_requests.is_close', 'departments.id as department_id', 'departments.code as department_code', 'departments.name as department_name', 'warehouses.id as warehouse_id', 'warehouses.code as warehouse_code', 'warehouses.name as warehouse_name', 'created_user.name AS created_by', 'updated_user.name AS updated_by', 'deleted_user.name AS deleted_by', DB::raw("CASE WHEN purchase_requests.is_approve = TRUE THEN 'Approved 2' WHEN purchase_requests.approved_1_status = 1 THEN 'Approved 1' WHEN purchase_requests.approved_2_status = 1 THEN 'Approved 1' ELSE 'Not Approved' END AS approval_status") ); } else if ($action == 'getPurchaseRequestApproval') { $query->leftJoin('departments', 'departments.id', '=', 'purchase_requests.department_id'); $query->leftJoin('warehouses', 'warehouses.id', '=', 'purchase_requests.warehouse_id'); $query->leftJoin('users as created_user', 'created_user.id', '=', 'purchase_requests.created_by'); $query->leftJoin('users as updated_user', 'updated_user.id', '=', 'purchase_requests.updated_by'); $query->leftJoin('users as deleted_user', 'deleted_user.id', '=', 'purchase_requests.deleted_by'); $query->leftJoin('users as approved_user_1', 'approved_user_1.id', '=', 'purchase_requests.approved_1_by'); $query->leftJoin('users as approved_user_2', 'approved_user_2.id', '=', 'purchase_requests.approved_2_by'); $query->leftJoin('users as canceled_user', 'canceled_user.id', '=', 'purchase_requests.canceled_by'); $query->select( 'purchase_requests.id as id', DB::raw("'<input type=checkbox name=select value=' || purchase_requests.id || '>' as is_checked"), 'purchase_requests.pr_number as pr_number', 'purchase_requests.needs as needs', 'purchase_requests.remark_pr as remark_pr', 'purchase_requests.is_approve as is_approve', 'purchase_requests.approved_1_status as approved_1_status', 'purchase_requests.approved_2_status as approved_2_status', 'purchase_requests.created_at as created_at', 'purchase_requests.updated_at as updated_at', 'purchase_requests.deleted_at as deleted_at', 'purchase_requests.is_close', 'departments.id as department_id', 'departments.code as department_code', 'departments.name as department_name', 'warehouses.id as warehouse_id', 'warehouses.code as warehouse_code', 'warehouses.name as warehouse_name', 'created_user.name AS created_by', 'updated_user.name AS updated_by', 'deleted_user.name AS deleted_by', DB::raw("CASE WHEN purchase_requests.is_approve = TRUE THEN 'Approved 2' WHEN purchase_requests.approved_1_status = 1 THEN 'Approved 1' WHEN purchase_requests.approved_2_status = 1 THEN 'Approved 1' ELSE 'Not Approved' END AS approval_status") ); if ($action == 'getPurchaseRequestApproval') { $user_groups = $this->authController->getUserGroups(); foreach ($user_groups as $user_group) { $haystack = strtolower($user_group->user_group_name); $isMatched = str_contains($haystack, 'department manager'); if ($isMatched) { $query->where('approved_1_status', 0); break; } $isMatched = str_contains($haystack, 'general manager'); if ($isMatched) { $query->where('approved_2_status', 0); break; } $isMatched = str_contains($haystack, 'admin'); if ($isMatched) { $query->where('approved_1_status', 0); $query->orWhere('approved_2_status', 0); break; } } } }
Leave a Comment