get($currentUserId); if (!isset($user_view->agencyId)) { $user_view->agencyId = $currentUserId; $user_view->agencyName = $user_view->fio; } $agency_id = (int) $user_view->agencyId; $tableId = (int) $_POST['table_id']; $tableDBO = mysql_query("SELECT * FROM `stat_tables` WHERE `id`=$tableId"); $table; while ($row = mysql_fetch_assoc($tableDBO)) { $table = $row; $table['json_funnel'] = json_decode(htmlspecialchars_decode($table['json_funnel']), true); for ($j = 0; $j < count($table['json_funnel']); $j++) { for ($i = 0; $i < count($table['json_funnel'][$j]['steps']); $i++) { $stepId = (int) $table['json_funnel'][$j]['steps'][$i]['id']; $stepDBO = mysql_query("SELECT name FROM `funnel_steps` WHERE `id`=$stepId"); while ($row2 = mysql_fetch_assoc($stepDBO)) { $table['json_funnel'][$j]['steps'][$i]['name'] = $row2['name']; break; } } } $table['json_add'] = json_decode(htmlspecialchars_decode($table['json_add']), true); } $clients = array(); for ($i = 0; $i < count($table['json_funnel']); $i++) { $funnelId = (int) $table['json_funnel'][$i]['id']; $sqlDBO = "SELECT * FROM `clients` WHERE `funnel_id`=$funnelId"; if(isset($table['json_add']['sources']) && !empty($table['json_add']['sources'])){ $sqlDBO .= " AND source in (".implode(', ', $table['json_add']['sources']).")"; } $clientsDBO = mysql_query($sqlDBO); while ($row = mysql_fetch_assoc($clientsDBO)) { array_push($clients, $row); } } for ($i = 0; $i < count($clients); $i++) { $eventsDBO = mysql_query("SELECT id, comment,create_date FROM user_client_events WHERE client_id=" . $clients[$i]['id']); $events = array(); while ($row = mysql_fetch_assoc($eventsDBO)) { array_push($events, $row); } $clients[$i]['events'] = $events; $clients[$i]['objects'] = json_decode(htmlspecialchars_decode($clients[$i]['objects']), true); $objectsId = ''; for ($j = 1; $j < count($clients[$i]['objects']); $j++) { if(isset($clients[$i]['objects'][$j])){ $objectsId .= ' or id = ' . $clients[$i]['objects'][$j]; } } $objects = array(); if($clients[$i]['objects'][0] . $objectsId != ''){ $objectsDBO = mysql_query("SELECT adres, nazv, stoim FROM objects WHERE id=" . $clients[$i]['objects'][0] . $objectsId); while ($row = mysql_fetch_assoc($objectsDBO)) { array_push($objects, $row); } } $clients[$i]['objectsInfo'] = $objects; $clients[$i]['agent'] = $clients[$i]['agent_main'] = mysql_fetch_assoc(mysql_query("SELECT fio FROM users WHERE id=" . $clients[$i]['who_work']))['fio']; if($clients[$i]['doer_clients'] == 1){ $doers = json_decode(html_entity_decode($clients[$i]['doers']), true); foreach($doers as $user => $doer){ if($doer == 1){ $clients[$i]['agent'] .= ", ".mysql_fetch_assoc(mysql_query("SELECT fio FROM users WHERE id=" . $user))['fio']; } } } if (((int) $clients[$i]['source']) > 0) { $source = mysql_fetch_assoc(mysql_query("SELECT source FROM advertising_sources WHERE id=" . $clients[$i]['source'])); $clients[$i]['source'] = $source['source']; } else { $clients[$i]['source'] = ''; } $eventsClientsDBO = mysql_query("SELECT * FROM events_clients where event='stageedit' AND client_id=" . $clients[$i]['id']); $eventsClients = array(); while ($row = mysql_fetch_assoc($eventsClientsDBO)) { $row['user'] = mysql_fetch_assoc(mysql_query("SELECT fio FROM users WHERE id=" . $row['user_id']))['fio']; array_push($eventsClients, $row); } $doersEventDBO = mysql_query("SELECT * FROM doers_emp_client where client_id=" . $clients[$i]['id']." AND confirm = 1 group by step_id, client_id, doer_id"); $doersEvent = array(); while ($row = mysql_fetch_assoc($doersEventDBO)) { $row['doer'] = mysql_fetch_assoc(mysql_query("SELECT fio FROM users WHERE id=" . $row['doer_id']))['fio']; $row['user_work'] = mysql_fetch_assoc(mysql_query("SELECT fio FROM users WHERE id=" . $row['who_work']))['fio']; array_push($doersEvent, $row); } $clients[$i]['doersEvent'] = $doersEvent; $clients[$i]['eventsClients'] = $eventsClients; $clients[$i]['agentadd'] = mysql_fetch_assoc(mysql_query("SELECT fio FROM users WHERE id=" . $clients[$i]['id_agent']))['fio']; } $tableData = array(); $tableData['clients'] = $clients; $tableData['table'] = $table; echo (json_encode($tableData));