getTimestamp())."'";
$who_work = 0;
if(isset($_POST['who_work']) && (int)$_POST['who_work'] > 0){
$who_work = (int)$_POST['who_work'];
}
$req_id = 0;
if(isset($_POST['req_id']) && $_POST['req_id'] > 0){
$req_id = (int)$_POST['req_id'];
}
$sql="SELECT * FROM clients WHERE id=$post[client_id]";
if($req_id > 0){
$sql="SELECT * FROM requisitions WHERE id=$req_id";
}
$rez=mysql_query($sql);
$client = mysql_fetch_assoc($rez);
$idUser = $_SESSION['id'];
if ($client['who_work'] > 0 && $client['doer_clients'] == 0) {
// тому событие, чей клиент
$idUser = $client['who_work'];
}
if($post['from_id']){
$idUser = $post['from_id'];
if($post['from_id'] == 'not') $idUser = $_SESSION['id'];
}
$post['sum'] = str_replace(' ', '', $post['sum']);
if($who_work > 0){
$idUser = $who_work;
}
$from_id = $_SESSION['id'];
$calendar_view = 0;
if ($post[noEvent])
$calendar_view = 1;
// Формируем интерактивный документ
$document_id = null;
$document = Docs::fillInnerDocument($post['blank_id'], $post['client_id'], $post['object_id'], $post['clients_ids']);
if (isset($document['doc_id']))
$document_id = $document['doc_id'];
$client_id = $post['client_id'];
$funnel_id = 0;
$step_id = 0;
if($client['funnel_id']) $funnel_id = (int)$client['funnel_id'];
if($client['step_id']) $step_id = (int)$client['step_id'];
$sql = "INSERT INTO user_client_events(user_id, client_id, type, comment, sum, schedule_date, from_id, calendar_view, document_id, funnel_id, step_id) ";
$sql .= " VALUES($idUser, $client_id, 'deal', '$post[comment]', '$post[sum]', $scheduleDate, $from_id, $calendar_view, '$document_id', $funnel_id, $step_id)";
if($req_id > 0){
$sql = "INSERT INTO user_client_events(user_id, req_id, type, comment, sum, schedule_date, from_id, calendar_view, document_id, funnel_id, step_id) ";
$sql .= " VALUES($idUser, $req_id, 'deal', '$post[comment]', '$post[sum]', $scheduleDate, $from_id, $calendar_view, '$document_id', $funnel_id, $step_id)";
}
if (isset($post['object_id'])) {
$sql2 = "INSERT INTO user_object_events(user_id, object_id, type, comment, sum, schedule_date, document_id) ";
$sql2 .= " VALUES($idUser, $post[object_id], 'deal', '$post[comment]', '$post[sum]', $scheduleDate, '$document_id')";
}
if (isset($post['clients_ids'])) {
if(is_array($post['clients_ids'])){
foreach($post['clients_ids'] as $clientId){
if($client_id != $clientId){
$sql21 = "INSERT INTO user_client_events(user_id, client_id, type, comment, sum, schedule_date, from_id, calendar_view, document_id) ";
$sql21 .= " VALUES($idUser, $clientId, 'deal', '$post[comment]', '$post[sum]', $scheduleDate, $from_id, $calendar_view, '$document_id')";
mysql_query($sql21);
}
}
}
}
// Событие о сделке в Историю клиента
$date = DateTime::createFromFormat('d.m.Y H:i', $post['schedule_date']);
$dop_text = "Запланирована сделка на " . date("Y-m-d H:i:s", $date->getTimestamp());
/*$client_id = $post['client_id'];
if ($req_id > 0)
$client_id = $req_id;
if (isset($post['object_id'])) {
$sql_obj = "SELECT id, nazv, adres FROM `objects` WHERE `id` = '$post[object_id]' LIMIT 1";
$rez_obj = mysql_query($sql_obj);
if ($obj = mysql_fetch_assoc($rez_obj)) {
$dop_text .= " по объекту " . ''.$obj['nazv'].' ('.$obj['adres'].')';
}
}
if (isset($post['sum']))
$dop_text .= "
Сумма сделки: " . $post['sum'] ." руб.";
if (isset($post['sum']))
$dop_text .= "
Комментарий: " . $post['comment'];*/
if ($document) {
$dop_text .= ", сформирован документ: " . '
' . $document['doc_name'] . '
.';
} else {
$dop_text .= ".";
}
$dop_text .= "
От ";
$sql3 = "INSERT INTO `events_clients` (
user_id,
client_id,
from_id,
event,
`read`,
`dop_text`
) VALUES (
'".$idUser."',
'".$client_id."',
'".$from_id."',
'add_deal',
'1',
'".$dop_text."'
)";
if (!mysql_query($sql) || !((!empty($sql2)) ? mysql_query($sql2) : true) || !mysql_query($sql3))
echo mysql_error();
echo 'true';
}