Joywork/ajax/getOneObjectAdvStats.php

296 lines
11 KiB
PHP
Raw Permalink Normal View History

2026-05-22 20:21:54 +02:00
<?php
$start = microtime(true);
require_once($_SERVER['DOCUMENT_ROOT'] . "/config.php");
global $PROMO_TARGETS;
global $ZIPAL_TARGETS;
/*ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);*/
$post = clearInputData($_POST);
if (isset($_POST['responsible'])) {
$usersArr = json_decode($_POST['responsible'], true);
} else {
$usersArr = array();
}
$cl = new Clients();
//список тех, кто входит в статистику.
$usersId = $cl->getUsersFilter($usersArr, $post['user_id'], $_SESSION['users_admin']);
$usersIdReal = array();
//пользователь, по которому смотрим объекты
$userId = $post['userId'];
$objectId = $post['objectId'];
// Определяем текущего пользователя
$user = new User;
$user->get($_SESSION['id']);
$user->checkPermissions();
$arrIdAddUsers = $arrIdWhoWork = $uniqueRecords = $records = $promo_records = [];
//если смотрим статистику по агентству, то смотрим по всем, кто в списке
if ($user->agency) {
foreach ($usersId as $usrId) {
$arrIdWhoWork[] = "who_work = $usrId";
$arrIdAddUsers[] = "id_add_user = $usrId";
}
$usersIdReal = $usersId;
} elseif ($user->manager) {
$arrIdWhoWork[] = "who_work = $userId";
$arrIdAddUsers[] = "id_add_user = $userId";
$usersIdReal[] = $userId;
$usersResult = mysql_query("SELECT id FROM users WHERE id_manager=" . $userId);
while ($usr = mysql_fetch_assoc($usersResult)) {
if (in_array($usr['id'], $usersId)) {
$arrIdWhoWork[] = "who_work = $usr[id]";
$arrIdAddUsers[] = "id_add_user = $usr[id]";
$usersIdReal[] = $usr['id'];
}
}
} else {
$arrIdWhoWork[] = "who_work = $userId";
$arrIdAddUsers[] = "id_add_user = $userId";
$usersIdReal[] = $userId;
}
$bd_usl_addUsers = " AND (" . implode(" OR ", $arrIdAddUsers) . ")";
$bd_usl_whoWork = " AND (" . implode(" OR ", $arrIdWhoWork) . ")";
$period = 'all';
$bd_piriod = '';
$bd_piriod_objects = '';
if (isset($post['period'])) {
$period = $post['period'];
if ($period == 'dates') {
$onDate = date('Y-m-d 00:00:00', strtotime($post['onDate']));
if (empty($onDate)) {
$onDate = '1970-01-01 00:00:00';
}
if (!empty($post['offDate'])) {
$offDate = date('Y-m-d 23:59:59', strtotime($_POST['offDate']));
} else {
$offDate = date('Y-m-d 23:59:59');
}
$bd_piriod .= " AND `date_add` between '{$onDate}' AND '{$offDate}'";
$bd_piriod_objects .= " AND `date_really_add` between '{$onDate}' AND '{$offDate}'";
$_SESSION['on_date'] = $post['onDate'];
$_SESSION['off_date'] = $post['offDate'];
} else {
unset($_SESSION['on_date']);
unset($_SESSION['off_date']);
$arrPeriod = ['today' => '-0 day', 'week' => '-6 day', 'month' => '-30 day', 'quarter' => '-120 day', 'year' => '-365'];
$days = date('Y-m-d', strtotime($arrPeriod[$post['period']]));
$bd_piriod .= " AND `date_add` > '{$days}'";
$bd_piriod_objects .= " AND `date_really_add` > '{$days}'";
}
}
$lastDay = '';
$startDate = '1970-01-01 00:00:00';
$endDate = '3000-12-31 23:59:59';
switch ($period) {
case 'month': {
$totalDays = cal_days_in_month(CAL_GREGORIAN, (int)date('m'), date('Y'));
$lastDay = $totalDays;
$startDate = date('Y') . "-" . date('m') . "-01 00:00:00";
$endDate = date('Y') . "-" . date('m') . "-" . $lastDay . " 23:59:59";
break;
}
case 'today':{
$startDate = date("Y-m-d 00:00:00");
$endDate = date("Y-m-d 23:59:59");
break;
}
case 'quarter' : {
$nowMonth = (int)date('m');
if ($nowMonth < 4) {
$startDate = date('Y') . "-01-01 00:00:00";
$endDate = date('Y') . "-03-31 23:59:59";
} else if ($nowMonth >= 4 && $nowMonth < 7) {
$startDate = date('Y') . "-04-01 00:00:00";
$endDate = date('Y') . "-06-30 23:59:59";
} else if ($nowMonth >= 7 && $nowMonth < 10) {
$startDate = date('Y') . "-07-01 00:00:00";
$endDate = date('Y') . "-09-30 23:59:59";
} else if ($nowMonth >= 10 && $nowMonth < 13) {
$startDate = date('Y') . "-10-01 00:00:00";
$endDate = date('Y') . "-12-31 23:59:59";
}
break;
}
case 'week': {
$startDate = date("Y-m-d 00:00:00", strtotime("monday this week"));
$endDate = date("Y-m-d 23:59:59", strtotime("sunday this week"));
break;
}
case '30days': {
$startDate = date('Y-m-d', strtotime('-30 day'))." 00:00:00";
$endDate = date("Y-m-d")." 23:59:59";
break;
}
case '90days': {
$startDate = date('Y-m-d', strtotime('-90 day'))." 00:00:00";
$endDate = date("Y-m-d")." 23:59:59";
break;
}
case 'dates' : {
$onDate = $_POST['onDate'];
//if(empty($onDate)) $onDate = '1970-01-01 00:00:00';
$offDate = $_POST['offDate'];
if (empty($offDate)) {
$offDate = date('Y-m-d 23:59:59');
}
$startDate = date("Y-m-d 00:00:00", strtotime($onDate));
$endDate = date("Y-m-d 23:59:59", strtotime($offDate));
break;
}
case 'year' : {
$startDate = date('Y') . "-01-01 00:00:00";
$endDate = date('Y') . "-12-31 23:59:59";
break;
}
}
$date_between = "between '" . $startDate . "' AND '" . $endDate . "'";
$usersWhere = $userId == $_SESSION['id'] ? "`user_owner_id` IN (" . implode(', ', $usersId) . ")" : "user_owner_id = '$userId'";
// Запрос статистики на все площадки
$sql = "SELECT *
FROM object_publish_statistic
WHERE `on_moderation` = 0 AND
$usersWhere AND
`destination` NOT IN (" . "'" . implode("', '", array_keys($PROMO_TARGETS)) . "'" . ") AND
(
`publish_start_date` $date_between
) AND
`object_id` = '$objectId'";
$rezDatesAndSumInPeriod = mysql_query($sql);
// Запрос статистики продвижения
$sql = "SELECT *
FROM object_publish_statistic
WHERE `on_moderation` = 0 AND
`user_owner_id` = '$userId' AND
`destination` IN (" . "'" . implode("', '", array_keys($PROMO_TARGETS)) . "'" . ") AND
(
`publish_start_date` $date_between
) AND
`object_id` = '$objectId'";
/*echo ($sql);*/
if ($query2 = mysql_query($sql)) {
while($row = mysql_fetch_assoc($query2)) {
$promo_records[] = [
'destination' => trim($row['destination']),
'price' => (float)$row['price']
];
}
}
if ($_SESSION['id'] == 7536) {
echo $sqlDatesAndPriceInPeriod;
}
if (mysql_num_rows($rezDatesAndSumInPeriod) > 0 || !empty($promo_records)) {
if (mysql_num_rows($rezDatesAndSumInPeriod) > 0) {
while ($statRec = mysql_fetch_assoc($rezDatesAndSumInPeriod)) {
$records[] = $statRec;
}
}
?>
<div class="crm__table" style="margin-bottom: 40px;">
<link rel="stylesheet" href="/css/bootstrap.css?v=3" type="text/css">
<link rel="stylesheet" href="/css/jquery.bootgrid.css" type="text/css">
<div class="grid_table">
<table id="object_adv_list_table" class="table table-condensed table-hover table-striped">
<thead>
<?php if (count($promo_records) > 0) : ?>
<tr>
<th data-column-id="id" data-first="true" data-rowspan="2" data-type="numeric">ID&nbsp;объекта</th>
<?php foreach ($records as $rec) {
if (in_array($rec['destination'], $uniqueRecords) === false) {
$uniqueRecords[] = $rec['destination'];
echo '<th data-column-id="' . $rec['destination'] . '" data-rowspan="2" data-first="true" data-header-css-class="text-center" data-identifier="true" data-css-class="text-center">' . $ZIPAL_TARGETS[$rec['destination']] . '</th>';
}
} ?>
<th data-column-id="promo" data-colspan="<?= count($promo_records) ?>" data-header-css-class="text-center" data-css-class="text-center">Продвижение</th>
</tr>
<tr>
<?php foreach ($promo_records as $rec) {
if (in_array($rec['destination'], $uniqueRecords) === false) {
$uniqueRecords[] = $rec['destination'];
echo '<th data-column-id="' . $rec['destination'] . '" data-podcheck="promo" data-identifier="true" data-first="false" data-header-css-class="text-center" data-css-class="text-center">' . $PROMO_TARGETS[$rec['destination']] . '</th>';
}
} ?>
</tr>
<?php else : ?>
<tr>
<th data-column-id="id" data-first="true" data-rowspan="2" data-type="numeric">ID&nbsp;объекта</th>
<?php foreach ($records as $rec) {
if (in_array($rec['destination'], $uniqueRecords) === false) {
$uniqueRecords[] = $rec['destination'];
echo '<th data-column-id="' . $rec['destination'] . '" data-rowspan="2" data-first="true" data-header-css-class="text-center" data-identifier="true" data-css-class="text-center">' . $ZIPAL_TARGETS[$rec['destination']] . '</th>';
}
} ?>
</tr>
<tr>
</tr>
<?php endif; ?>
</thead>
<tbody>
<tr>
<td><?= $objectId ?></td>
<?php foreach ($uniqueRecords as $dest) {
$sum = 0;
foreach ($records as $rec) {
if ($rec['destination'] == $dest) {
$sum = $sum + $rec['price'];
}
}
foreach ($promo_records as $rec) {
if ($rec['destination'] == $dest) {
$sum = $sum + $rec['price'];
}
}
echo '<td data-type="numeric">'.number_format($sum, 2, '.', ' ').'</td>';
} ?>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript" src="/js/bootstrap.js"></script>
<script type="text/javascript" src="/js/jquery.bootgrid.js"></script>
</div>
<script type="text/javascript">
$("#object_adv_list_table").bootgrid({
rowCount: 20,
sorting: false,
ajax: false,
templates: {
search: ""
},
formatters: {}
});
</script>
<?php } ?>