Joywork/parseCsvClients.php
2026-05-22 21:21:54 +03:00

157 lines
5.0 KiB
PHP

<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);
$file = fopen('klienty_assol.csv', 'r');
while (!feof($file)) {
$arr = fgetcsv($file, 2048, ';');
$j = count($arr);
if ($j>1) {
//$id = trim($arr[0]);
$clientDate = str_replace("+03:00", "", trim($arr[0]));
$clientDate = str_replace("T", " ", $clientDate);
$clientDate = preg_replace("/[^0-9.-: ]/", "", $clientDate);
var_dump($clientDate);
$clientDate = DateTime::createFromFormat('d.m.Y H:i:s', $clientDate);
$clientDate = $clientDate->format('Y-m-d H:i:s');
var_dump($clientDate);
$clientBDate = str_replace("+03:00", "", trim($arr[2]));
if ($clientBDate) {
$clientBDate = str_replace(",", ".", $clientBDate);
$clientBDate = preg_replace("/[^0-9.-: ]/", "", $clientBDate);
$clientBDate = DateTime::createFromFormat('d.m.y', $clientBDate);
$clientBDate = $clientBDate->format('Y-m-d H:i:s');
}
//$tag = trim($arr[3]);
$clientName = trim($arr[1]);
$clientType = trim($arr[2]);
$clientRole = trim($arr[3]);
$phone = trim($arr[4]);
$mail = trim($arr[5]);
$clientState = trim($arr[6]);
$managerPhone = trim($arr[7]);
if (strpos($phone, "+") === false) {
$phone = "+" . $phone;
}
if (strpos($managerPhone, "+") === false) {
$managerPhone = "+" . $managerPhone;
}
$source = trim($arr[8]);
$comment = trim($arr[9]);
var_dump($clientRole);
$sql = "select id from users where phone = '$managerPhone'";
$result = mysql_query($sql);
$m = mysql_fetch_row($result);
$managerId = $m[0];
if ($managerId == null) {
$sql = "select id from users where phone = '$managerPhone'";
$result = mysql_query($sql);
$m = mysql_fetch_row($result);
$managerId = $m[0];
}
if ($managerId != null) {
echo " manager:" . $managerId;
$sql = "select id from clients where phone = '$phone' and id_agent = $managerId";
$result = mysql_query($sql);
$m = mysql_fetch_row($result);
$addedId = $m[0];
if ($addedId != null) {
echo " already added: $addedId";
} else {
if ($source) {
$sql = "select id from advertising_sources where upper(source) = upper('$source')";
$result = mysql_query($sql);
$m = mysql_fetch_row($result);
if (!$m) {
$m = [];
$m[0] = 0;
}
} else {
$m = [];
$m[0] = 0;
}
$role = '';
$type = '';
if ($clientRole) {
$role = '[&quot;' . $clientRole .'&quot;]';
}
if ($clientType) {
$type = '[&quot;' . $clientType .'&quot;]';
}
$clients = new Clients();
$post = [];
$post['phone'] = $phone;
$post['email'] = $mail;
$post['opis'] = $comment;
$post['fio'] = $clientName;
$post['id_agent'] = $managerId;
$post['who_work'] = $managerId;
$post['funnel_id'] = 0;
$post['source'] = $m[0];
$post['confirm'] = 1;
$post['birthday'] = $clientBDate;
$post['role'] = $role;
$post['work_type'] = $type;
var_dump($post);
echo $clientState;
var_dump($clientDate);
//$sqlAct = "select id from activities where name = '$tag' and user_id=12331";
//$resultAct = mysql_query($sqlAct);
//$mAct = mysql_fetch_assoc($resultAct);
//if ($mAct['id']) {
//echo "tag=" . $mAct['id'];
//$sqlInsAct = "INSERT INTO `clients_activities` (`client_id`, `activity_id`) VALUES ('$idClient', '$mAct[id]')";
//mysql_query($sqlInsAct);
//}
$idClient = $clients->addEdit($post);
var_dump($idClient);
if ($clientState == "Закрыт") {
$sql = "UPDATE clients SET deleted = 1, who_delete = $managerId, id_agent = $managerId, date_add = '$clientDate' WHERE id = $idClient";
mysql_query($sql);
} else {
$sql = "UPDATE clients SET date_add = '$clientDate', id_agent = $managerId WHERE id = $idClient";
mysql_query($sql);
}
echo " added";
echo "<br>";
}
} else {
echo " manager not found";
}
echo "<br>";
}
}