deviation_process

PHOTO EMBED

Thu Jul 31 2025 06:56:58 GMT+0000 (Coordinated Universal Time)

Saved by @mohinibhojane #php

  private function processNotificationData(&$NotificationData)
    {
        $machineIds = collect($NotificationData)->pluck('machine_id')->unique()->filter()->toArray();
        $operatorIds = collect($NotificationData)->pluck('operator_id')->unique()->filter()->toArray();
        $techsheetHeaderIds = collect($NotificationData)->pluck('techsheet_header_id')->unique()->filter()->toArray();
        $poSrTechOpns = collect($NotificationData)->map(function ($data) {
            return [
                'machine_id' => $data->machine_id,
                'po_sr_no' => $data->po_sr_no,
                'tech_opn_no' => $data->tech_opn_no,
            ];
        });

        // Bulk load data
        $machines = MachineMaster::whereIn('id', $machineIds)->get()->keyBy('id');
        $users = UserMaster::whereIn('id', $operatorIds)->get()->keyBy('id');

        // Bulk fetch queue data
        $queueData = DB::table('tech_operation_queue_dataset')
            ->whereIn('machine_id', $machineIds)
            ->whereIn('po_sr_no', $NotificationData->pluck('po_sr_no')->unique())
            ->get()
            ->groupBy(fn($item) => $item->machine_id . '_' . $item->po_sr_no . '_' . $item->tech_opn_no);

        // Bulk fetch techsheet_data
        $techData = DB::table('techsheet_data')
            ->whereIn('fk_techsheet_header_id', $techsheetHeaderIds)
            ->get()
            ->groupBy(fn($item) => $item->fk_techsheet_header_id . '_' . $item->opn_no);

        // Update each row
        foreach ($NotificationData as $data) {
            $data->sah = $data->estimated_mrr + $data->estimated_nva;

            $machine = $machines[$data->machine_id] ?? null;
            $data->machine_shop_name = $machine->machine_shop_name ?? '';
            $data->fk_area_master_id = $machine->fk_area_master_id ?? null;

            $user = $users[$data->operator_id] ?? null;
            $data->reported_by = $user->profile_name ?? '';

            $queueKey = $data->machine_id . '_' . $data->po_sr_no . '_' . $data->tech_opn_no;
            $queue = $queueData[$queueKey][0] ?? null;
            $data->actual_time_taken = $data->sah + ($queue->duration ?? 0);
            $data->shift_date = $queue->shift_date ?? '';

            // Handling multiple opn_no's
            $opnArray = collect(explode(',', $data->tech_opn_no))
                ->map(fn($val) => (int) trim($val))
                ->filter()
                ->unique()
                ->toArray();

            foreach ($opnArray as $opn) {
                $key = $data->techsheet_header_id . '_' . $opn;
                $tech = $techData[$key][0] ?? null;

                if ($tech) {
                    $data->m_c_no = $tech->m_c_no ?? '';
                    $data->actual_m_c_no = $tech->actual_m_c_no ?? '';
                    break;
                }
            }
        }
    }
content_copyCOPY