8e7cdc88b8c9bc90cd1273fc8b5586ed36648b22.php 14.3 KB
<?php

ini_set("precision", 15);

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
	$errorMsg = "";
	$successMsg = "";

	if($_FILES['file']['tmp_name'])
	{
		if($_FILES['file']['type'] == "application/vnd.ms-excel")
		{
			$fileUploadLimit = 52428800; // files size is in bytes
			if($_FILES['file']['size'] < $fileUploadLimit)
			{
				include_once(app_path().'/lib/phpexcel/PHPExcel.php');

				if ($_POST["uploadFileType"] == "customerInfo")
				{
					$kformlib=new \App\Jobs\KFormLib($wakka->HRCoreVars["HRFiledsStr"]);
					$kformlib->gthis=$wakka;
					$themehome=$wakka->GetThemePath('/');
					$updatetime=time();

					function mkdir_r ($dir)
					{
						if (strlen($dir) == 0)return 0;
						if (is_dir($dir))return 1;
						elseif (dirname($dir) == $dir)return 1;
						return (mkdir_r(dirname($dir)) and mkdir($dir,0777));
					}

					$clientlst=$wakka->GetBBBUserData("clientslist");
					$isadmin=$wakka->IsAdmin();
					$username=$wakka->GetUserName();
					$triggers=Input::get("triggers");
					$tmpstr=explode(",",$kformlib->HRFiledsStr);
					$success="";$message="";$successcnt=0;$duplicatecount=0;
					$inputFileType = "Excel5";

					$objReader = PHPExcel_IOFactory::createReader($inputFileType);
					$objPHPExcel = $objReader->load($_FILES['file']['tmp_name']);
					$objWorksheet = $objPHPExcel->getActiveSheet();
					$highestColumn = PHPExcel_Cell::columnIndexFromString($objPHPExcel->getActiveSheet()->getHighestColumn());
					$highestrow=$objPHPExcel->getActiveSheet()->getHighestRow();
					$excelarray=array();$keys=array();

					for($i=1;$i<=$highestrow;$i++)
					{
						$excelarray[$i]=array();
						for($head = 0; $head < $highestColumn; $head++)
						{
							if($i==1)
							{
								$keys[$head]=trim($objWorksheet->getCellByColumnAndRow($head,$i)->getValue());
							}else{
								$excelarray[$i][$keys[$head]]=trim($objWorksheet->getCellByColumnAndRow($head, $i)->getValue());
							}
						}
					}

					for($i=3;$i<=$highestrow;$i++)
					{
						if($excelarray[$i]["id"]!="")
						{
							if($excelarray[$i]["id"]=="Create")
							{
								$excelarray[$i]["id"]=$wakka->Query("insert into","","records",array('created'=>date('Y-m-d H:i:s')));
							}
							else $excelarray[$i]["id"]=intval($excelarray[$i]["id"]);

							// $lanq = DB::table('records')->select('id')->where('lan', '=', $excelarray[$i]["lan"])->first();
							// if($lanq){
							// 	$excelarray[$i]["id"]=intval($lanq->id);
							// }else{
							// 	$excelarray[$i]["id"]=$wakka->Query("insert into","","records",array('created'=>date('Y-m-d H:i:s')));
							// }

							if($wakka->getCount("records","id='".$excelarray[$i]["id"]."'")==1)
							{
								$empdata=$wakka->getPerson($excelarray[$i]["id"]);
								$ppldata=$empdata["peopledata"];
								$createdlog=$empdata['modifylog'];
								$fdirty=$empdata['dirty'];

								$createdlog[$updatetime]=$username."::";
								$createdlog["updated"]=$updatetime;
								
								$newdata=$ppldata;
								if($isadmin||strstr($clientlst,$ppldata['client']))
								{
									$editflag=0;
									foreach($excelarray[$i] as $key => $value)
									{
										$value = preg_replace('/[^A-Za-z0-9\., -]/', '', $value);

										if(isset($kformlib->HRFieldNames[$key]))
										{
											if($value!="")
											{
												if("A".$ppldata[$key]!="A".$value)//forcing string comparrision //MAGIC
												{
													$value=str_replace("'"," ",$value);
													if(strstr($createdlog[$updatetime],$key)==FALSE)$createdlog[$updatetime].="$key|".str_replace(array("|",",")," ",$ppldata[$key])."|".str_replace(array("|",",")," ",$value).",";

													$fdirty[$key]=1;$editflag=1;
														
													if($triggers=='Yes')
													{
														$kformlib->empAnalytics($excelarray[$i]["id"],$ppldata,$key,$value,$ppldata[$key]);
														$kformlib->empWorkflowTrigger($excelarray[$i]["id"],$ppldata['client'],$key,$value,$ppldata[$key],$ppldata,$newdata);
													}
													$newdata[$key]=$value;
												}
													//else $message.="same $excelarray[$i][id]:$key:$value, ";
											}
												//else $message.="empty $excelarray[$i][id]:$key, ";
										}else{
											// $message.="Invalid key :$key, ";
										}
									}
									if($editflag)
									{
										$empdata["peopledata"]=$newdata;
										$empdata['modifylog']=$createdlog;
										$empdata['dirty']=$fdirty;

										$wakka->setPerson($excelarray[$i]["id"],$empdata);
										$success.=$excelarray[$i]["id"].",";$successcnt++;
									}
								}else{
									$errorMsg.="No access to $excelarray[$i][id], ";
								}
							}else{
								//$errorMsg.="ID not found $excelarray[$i][id], ";
							}
						}else{
							$errorMsg.="Incorrect ID '$excelarray[$i][id]', ";
						}
					}
					$successMsg .= "Data Uploaded ($successcnt)."; // : $success.";
				}

				if ($_POST["uploadFileType"] == "trails")
				{
					$success="";$message="";$successcnt=0;$duplicatecount=0;
					$inputFileType = "Excel5";

					$objReader = PHPExcel_IOFactory::createReader($inputFileType);
					$objPHPExcel = $objReader->load($_FILES['file']['tmp_name']);
					$objWorksheet = $objPHPExcel->getActiveSheet();
					$highestColumn = PHPExcel_Cell::columnIndexFromString($objPHPExcel->getActiveSheet()->getHighestColumn());
					$highestrow=$objPHPExcel->getActiveSheet()->getHighestRow();
					$excelarray=array();$keys=array();

					for($i=1;$i<=$highestrow;$i++)
					{
						$excelarray[$i]=array();
						for($head = 0; $head < $highestColumn; $head++)
						{
							if($i==1)
							{
								$keys[$head]=trim($objWorksheet->getCellByColumnAndRow($head,$i)->getValue());
							}else{
								$excelarray[$i][$keys[$head]]=trim($objWorksheet->getCellByColumnAndRow($head, $i)->getValue());
							}
						}
					}

					$addCount = 0;$updateCount = 0;
					for($i=3;$i<=$highestrow;$i++)
					{
						if($excelarray[$i]["id"]!="")
						{
							if($excelarray[$i]["id"]=="Create")
							{
								$queryResult = $wakka->Query("insert into","","trails",array(
										'lan'=>$excelarray[$i]['lan'],
										'action_date'=> PHPExcel_Style_NumberFormat::toFormattedString($excelarray[$i]['actionDate'], 'yyyy-mm-dd'),
										'user_id'=>$excelarray[$i]['userId'],
										'action_code'=>$excelarray[$i]['actionCode'],
										'result_code'=>$excelarray[$i]['resultCode'],
										'next_action_code'=>$excelarray[$i]['nextActionCode'],
										'next_action_time'=>PHPExcel_Style_NumberFormat::toFormattedString($excelarray[$i]['nextActionDate'], 'yyyy-mm-dd hh:mm'),
										'rfd'=>$excelarray[$i]['rfd'],
										'remark'=>$excelarray[$i]['remark'],
										'bucket_code'=>$excelarray[$i]['bucketCode_field'],
										'special_code'=>$excelarray[$i]['specialCode']
									));
								$addResult .= $queryResult.', ';
								$addCount++;
							}else{
								$lanq = DB::table('trails')->select('id')->where('id', '=', $excelarray[$i]["id"])->first();
								if($lanq && $lanq->id != ""){
									DB::table('trails')->where('id',$lanq->id)->update([
										'lan'=>$excelarray[$i]['lan'],
										'action_date'=>PHPExcel_Style_NumberFormat::toFormattedString($excelarray[$i]['actionDate'], 'yyyy-mm-dd'),
										'user_id'=>$excelarray[$i]['userId'],
										'action_code'=>$excelarray[$i]['actionCode'],
										'result_code'=>$excelarray[$i]['resultCode'],
										'next_action_code'=>$excelarray[$i]['nextActionCode'],
										'next_action_time'=>PHPExcel_Style_NumberFormat::toFormattedString($excelarray[$i]['nextActionDate'], 'yyyy-mm-dd hh:mm'),
										'rfd'=>$excelarray[$i]['rfd'],
										'remark'=>$excelarray[$i]['remark'],
										'bucket_code'=>$excelarray[$i]['bucketCode_field'],
										'special_code'=>$excelarray[$i]['specialCode']
									]);
									$updateResult .= $lanq->id.', ';
									$updateCount++;
								}
							}
						}else{
							$errorMsg.="ID is blank, ";
						}
					}
					$successMsg .= "Data Uploaded ($addCount): $addResult. Data update($updateCount): $updateResult.";
				}

				if ($_POST["uploadFileType"] == "addressdata")
				{
					$success="";$message="";$successcnt=0;$duplicatecount=0;
					$inputFileType = "Excel5";

					$objReader = PHPExcel_IOFactory::createReader($inputFileType);
					$objPHPExcel = $objReader->load($_FILES['file']['tmp_name']);
					$objWorksheet = $objPHPExcel->getActiveSheet();
					$highestColumn = PHPExcel_Cell::columnIndexFromString($objPHPExcel->getActiveSheet()->getHighestColumn());
					$highestrow=$objPHPExcel->getActiveSheet()->getHighestRow();
					$excelarray=array();$keys=array();

					for($i=1;$i<=$highestrow;$i++)
					{
						$excelarray[$i]=array();
						for($head = 0; $head < $highestColumn; $head++)
						{
							if($i==1)
							{
								$keys[$head]=trim($objWorksheet->getCellByColumnAndRow($head,$i)->getValue());
							}else{
								$excelarray[$i][$keys[$head]]=trim($objWorksheet->getCellByColumnAndRow($head, $i)->getValue());
							}
						}
					}

					$addCount = 0;$updateCount = 0;
					for($i=3;$i<=$highestrow;$i++)
					{
						if($excelarray[$i]["id"]!="")
						{
							if($excelarray[$i]["id"]=="Create")
							{
								$queryResult = $wakka->Query("insert into","","address",array(
										'lan'=>$excelarray[$i]['lan'],
										'address_type'=>$excelarray[$i]['addresstype'],
										'contact_person'=>$excelarray[$i]['contactperson'],
										'address_1'=>$excelarray[$i]['address1'],
										'address_2'=>$excelarray[$i]['address2'],
										'address_3'=>$excelarray[$i]['address3'],
										'address_4'=>$excelarray[$i]['address4'],
										'area'=>$excelarray[$i]['area'],
										'landmark'=>$excelarray[$i]['landmark'],
										'city'=>$excelarray[$i]['city'],
										'zip'=>$excelarray[$i]['zip'],
										'state'=>$excelarray[$i]['state'],
										'mobile'=>$excelarray[$i]['mobileno'],
										'email'=>$excelarray[$i]['mailid']
									));
								$addResult .= $queryResult.', ';
								$addCount++;
							}else{
								$lanq = DB::table('trails')->select('id')->where('id', '=', $excelarray[$i]["id"])->first();
								if($lanq && $lanq->id != ""){
									DB::table('address')->where('id',$lanq->id)->update([
										'lan'=>$excelarray[$i]['lan'],
										'address_type'=>$excelarray[$i]['addresstype'],
										'contact_person'=>$excelarray[$i]['contactperson'],
										'address_1'=>$excelarray[$i]['address1'],
										'address_2'=>$excelarray[$i]['address2'],
										'address_3'=>$excelarray[$i]['address3'],
										'address_4'=>$excelarray[$i]['address4'],
										'area'=>$excelarray[$i]['area'],
										'landmark'=>$excelarray[$i]['landmark'],
										'city'=>$excelarray[$i]['city'],
										'zip'=>$excelarray[$i]['zip'],
										'state'=>$excelarray[$i]['state'],
										'mobile'=>$excelarray[$i]['mobileno'],
										'email'=>$excelarray[$i]['mailid']
									]);
									$updateResult .= $lanq->id.', ';
									$updateCount++;
								}
							}
						}else{
							$errorMsg.="ID is blank, ";
						}
					}
					$successMsg .= "Data Uploaded ($addCount): $addResult. Data update($updateCount): $updateResult.";
				}

			}else{
				$errorMsg .= "File size too large, Please check file size should be less then ".($fileUploadLimit / (1024*1024))."mb.";
			}
		}else{
			$errorMsg .= "File format not valid, Please attach Excel file (.xls) and try upload again.";
		}
		@unlink($_FILES['file']);
	}else{
		$errorMsg .= "File not found, Please attach file and try upload again.";
	}

	if(isset($successMsg) && $successMsg!="")echo "Success! ".$successMsg;
	if(isset($errorMsg) && $errorMsg!="")echo "Error! ".$errorMsg;
}else{
?>

<div class="layout-app">
	<div class="row">
		<div class="col-sm-12">
			<div class="col-separator col-separator-first box col-unscrollable">
				<div class="tab_generic" style="padding: 10px;">
					<h4>Customers Data Upload</h4>

					<hr style="margin-bottom: 10px;" />

					<p><span style="color:#f00;"><strong>Instruction</strong></span><br/>
						(Works only if you have 'Write access" to the records)<br/>
						Please create an Excel-2003 format file with header as field-keys and rows containing record data.<br/>
						Extream Care is should be taken during bulk upload as previous values will be overwritten and lost.</p>

					<div class="row">
						<div class="col-sm-4" style="border-right: 1px #eee solid;">
							<h5>Customer Info</h5>
							<div style="padding: 10px;">
								<p>Upload File</p>
								<form method="POST" enctype="multipart/form-data" target="resultArea" action="data?action=upload">
									<input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>" />
									<input type="hidden" name="uploadFileType" value="customerInfo"/>
									<p><input type="file" class="form-control" name="file"/></p>
									<button type="submit" class="btn btn-success pull-right"><i class="fa fa-upload"></i> Upload</button>
								</form>
							</div>
						</div>

						<div class="col-sm-4" style="border-right: 1px #eee solid;">
							<h5>Followup Trails</h5>
							<div style="padding: 10px;">
								<p>Upload File</p>
								<form method="POST" enctype="multipart/form-data" target="resultArea" action="data?action=upload">
									<input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>" />
									<input type="hidden" name="uploadFileType" value="trails"/>
									<p><input type="file" class="form-control" name="file"/></p>
									<button type="submit" class="btn btn-success pull-right"><i class="fa fa-upload"></i> Upload</button>
								</form>
							</div>
						</div>

						<div class="col-sm-4">
							<h5>Contact Details</h5>
							<div style="padding: 10px;">
								<p>Upload File</p>
								<form method="POST" enctype="multipart/form-data" target="resultArea" action="data?action=upload">
									<input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>" />
									<input type="hidden" name="uploadFileType" value="addressdata"/>
									<p><input type="file" class="form-control" name="file"/></p>
									<button type="submit" class="btn btn-success pull-right"><i class="fa fa-upload"></i> Upload</button>
								</form>
								
							</div>
						</div>
					</div>

					<div class="row" style="margin-top: 20px;">
						<div class="col-sm-12">
							<iframe name=resultArea id="resultArea" style="width: 100%;border:none;"></iframe>
						</div>
					</div>

				</div>
			</div>	
		</div>
	</div>
</div>

<?php
}
?>