IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Imports/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Imports/MarksImport.php

<?php

namespace App\Imports;

use App\MarksheetRoyal;
use App\AdmEntry;
use App\ClassM;
use App\Section;
use App\Subject;
use DB;
use Maatwebsite\Excel\Concerns\ToModel;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\WithStartRow;

class MarksImport implements ToModel, WithStartRow
{
    private $data;

    private $mapper;

    private $uid;

    public function __construct($data,$uid)
    {
        $this->data = $data;
        $this->mapper = [];
        $this->uid = $uid;

    }
    public function model(array $row)
    {

        $arr = [];
        $tableParams = [
            'PT1','PT2','Preboard1','Preboard2','WPT1','WPT2','WPreboard1','WPreboard2'
        ];
        //check class
        $idClass =  ClassM::where('className',$row[2])->where('idSchool',$this->data['idSchool'])->first();
        if($idClass == null){
                //if no subject found


                if(in_array($this->mapper[$row[0]]['idClass'],[240])){
                    $tableParams = [
                        'PT1','PT2','PT3','WPT','Pro','MA','SE','Annual'
                    ];
                }else if(in_array($this->mapper[$row[0]]['idClass'],[232,233])){
                    $cy = ['C1','C2','C3','C4','C5','C6','C7','C8'];
                    $tb = ['R','W','S','H','L','S'];
                    $tableParams = [
                        'E1','E2','E3','E4'
                    ];
                    foreach($cy as $k=>$v){
                        foreach($tb as $m=>$n){
                            array_push($tableParams,$v.$n);
                        }
                    }
                }else if(in_array($this->mapper[$row[0]]['idClass'],[234,235,236,237,238,239])){
                    $tableParams = [
                        'PT1','PT1T','NB1','SE1','HF','PT2','PT2T','NB2','SE2','AE'
                    ];
                }
                
                if($this->data['idType'] != "S"){
                    $tableParams = [];
                    if(in_array($this->mapper[$row[0]]['idClass'],[234,235,236,237,238,239]))
                        $fetchParam = DB::table('exam_subject_mapping')->where('idClass',234)->get();
                    else $fetchParam = DB::table('exam_subject_mapping')->where('idClass',$this->mapper[$row[0]]['idClass'])->get();
                    if($fetchParam != null){
                            foreach($fetchParam as $params){
                                array_push($tableParams,$params->headerName.'('.$params->subjectName.')');
                            }
                    }
                }
                

                $indx = 5;
                foreach($tableParams as $k => $v){
                    $arr[$v] = $row[$indx] ?? null;
                    $indx++;
                }

                return new MarksheetRoyal([
                    'idSchool' => $this->data['idSchool'],
                    'idFinancialYear' => $this->data['idFinancialYear'],
                    'idClass'      => $this->mapper[$row[0]]['idClass'],
                    'idSection'      => $this->mapper[$row[0]]['idSection'],
                    'subjectName'      => $row[4],
                    'idStudent'      => $this->mapper[$row[0]]['idStudent'],
                    'created_by' => $this->data['created_by'],
                    'scholasticData' => json_encode($arr)
                ]);
        }else{
            $idSection = Section::where('sectionName',$row[3])->where('idClass',$idClass->idClass)->where('idSchool',$this->data['idSchool'])->first();
            $idStudent = AdmEntry::where('ecNo',$row[0])->where('idFinancialYear',$this->data['idFinancialYear'])
            ->where('idSection',$idSection->idSection)->where('idClass',$idClass->idClass)->where('idSchool',$this->data['idSchool'])->first();
                //if no subject found
                MarksheetRoyal::where('id','<',$this->uid)->where('subjectName',$row[4])->where('idSchool',$this->data['idSchool'])->where('idFinancialYear',$this->data['idFinancialYear'])->where('idStudent',$idStudent->idStudent)->delete();
                $this->mapper[$row[0]]= [
                    'idSchool' => $this->data['idSchool'],
                    'idFinancialYear' => $this->data['idFinancialYear'],
                    'created_by' => $this->data['created_by'],
                    'idClass'      => $idClass->idClass,
                    'idSection'      => $idSection->idSection,
                    'idStudent'      => $idStudent->idStudent,
                ];

                if(in_array($idClass->idClass,[240])){
                    $tableParams = [
                        'PT1','PT2','PT3','WPT','Pro','MA','SE','Annual'
                    ];
                }else if(in_array($idClass->idClass,[232,233])){
                    $cy = ['C1','C2','C3','C4','C5','C6','C7','C8'];
                    $tb = ['R','W','S','H','L','S'];
                    $tableParams = [
                        'E1','E2','E3','E4'
                    ];
                    foreach($cy as $k=>$v){
                        foreach($tb as $m=>$n){
                            array_push($tableParams,$v.$n);
                        }
                    }
                }else if(in_array($idClass->idClass,[234,235,236,237,238,239])){
                    $tableParams = [
                        'PT1','PT1T','NB1','SE1','HF','PT2','PT2T','NB2','SE2','AE'
                    ];
                }

                if($this->data['idType'] != "S"){
                    $tableParams = [];
                    if(in_array($idClass->idClass,[234,235,236,237,238,239]))
                    $fetchParam = DB::table('exam_subject_mapping')->where('idClass',234)->get();
                    else $fetchParam = DB::table('exam_subject_mapping')->where('idClass',$idClass->idClass)->get();
                    if($fetchParam != null){
                        foreach($fetchParam as $params){
                            array_push($tableParams,$params->headerName.'('.$params->subjectName.')');
                        }
                    }
                }

                $indx = 5;
                foreach($tableParams as $k => $v){
                    $arr[$v] = $row[$indx] ?? null;
                    $indx++;
                }

                return new MarksheetRoyal([
                    'idSchool' => $this->data['idSchool'],
                    'idFinancialYear' => $this->data['idFinancialYear'],
                    'idClass'      => $idClass->idClass,
                    'subjectName'      => $row[4],
                    'idSection'      => $idSection->idSection,
                    'idStudent'      => $idStudent->idStudent,
                    'scholasticData' => json_encode($arr),
                    'created_by' => $this->data['created_by']
                ]);
            
        }
        //check type of excel
        
    }

    /**
     * @return int
     */
    public function startRow(): int
    {
        return 3;
    }
    
}


Copyright © 2021 - 2025 IMMREX7