/
home
/
sjslayjy
/
public_html
/
cropnet
/
app
/
Imports
/
Upload File
HOME
<?php namespace App\Imports; use Illuminate\Support\Collection; use Maatwebsite\Excel\Concerns\ToCollection; use Maatwebsite\Excel\Concerns\WithHeadingRow; class ImportMpp implements ToCollection,WithHeadingRow { /** * @param Collection $collection */ private $mppNotFound = array(); public $user_id; function __construct($data = null) { $this->user_id = $data; } public function mppNotFound() { return $this->mppNotFound; } public function collection(Collection $collection) { //delete already assinged mpp foreach ($collection as $key1=>$sheet1) { if(count($sheet1) > 0){ $assinged_data = \DB::table('assign_users')->where('user_id', $this->user_id)->where('mcc_code', $sheet1['mcc'])->first(); if(!is_null($assinged_data)) { \DB::table('assign_users')->where('user_id', $this->user_id)->where('mcc_code', $sheet1['mcc'])->delete(); } } } foreach ($collection as $key=>$sheet) { if(count($sheet) > 0){ $mpp_details = \DB::table('mpp')->where('mcc_code', $sheet['mcc'])->where('code',$sheet['mpp'])->first(); if(!is_null($mpp_details)) { $assinged_data = \DB::table('assign_users')->where('user_id', $this->user_id)->where('mcc_code', $sheet['mcc'])->first(); if(is_null($assinged_data)) { \DB::table('assign_users')->insert([ 'user_id' => $this->user_id, 'mcc_code' => $sheet['mcc'], 'mpp_code' => $sheet['mpp'], ]); } else { $mppArr = explode(',', $assinged_data->mpp_code); if(!in_array($sheet['mpp'], $mppArr)) { \DB::table('assign_users')->where('user_id', $this->user_id)->where('mcc_code', $sheet['mcc'])->update([ 'user_id' => $this->user_id, 'mcc_code' => $sheet['mcc'], 'mpp_code' => $assinged_data->mpp_code.','.$sheet['mpp'], ]); } } } else { array_push($this->mppNotFound, $sheet['mpp']); } } } } }