ьмрепргмепргмтепргщтбмепщргбмеп. Проектирование и создание информационной системы для торговой организации
![]()
|
«./components/pages/reports.php» - файл содержащий PHP-код раздела статистики. /* Получаем данные для построения графиков */ $chart = []; $chart_sum = []; $chart_count = []; $q = $db->query("SELECT `add_time`,`goods_sum` FROM `orders` WHERE `add_time`>?i AND `status`='2' AND `del` IS NULL ORDER BY `id` DESC",strtotime('-100 days')); while($ls = $db->fetch($q)) { $ls['add_time'] = strtotime(date('Y-m-d',$ls['add_time'])); if(!isset($chart[$ls['add_time']])) { $chart[$ls['add_time']] = ['count' => 0, 'sum' => 0]; } $chart[$ls['add_time']]['count']++; $chart[$ls['add_time']]['sum'] += $ls['goods_sum']; } 99 foreach($chart as $key => $val) { $key = time2date($key,2); $val['sum'] = round($val['sum']); $chart_sum[] = "['{$key}', {$val['sum']}]"; $chart_count[] = "['{$key}', {$val['count']}]"; } $chart_sum = implode(',',array_reverse($chart_sum)); $chart_count = implode(',',array_reverse($chart_count)); /* Составление таблиц */ $table_all = ['count' => 0, 'sum' => 0, 'sum_middle' => 0, 'cancel' => 0, 'user' => [], 'client' => 0]; $tables = [ '1month' => ['count' => 0, 'sum' => 0, 'sum_middle' => 0, 'cancel' => 0, 'user' => [], 'client' => 0, 'date' => null], '2month' => ['count' => 0, 'sum' => 0, 'sum_middle' => 0, 'cancel' => 0, 'user' => [], 'client' => 0, 'date' => null], '3month' => ['count' => 0, 'sum' => 0, 'sum_middle' => 0, 'cancel' => 0, 'user' => [], 'client' => 0, 'date' => null] ]; $q = $db->query("SELECT `add_time`,`goods_sum`,`user`,`status` FROM `orders` WHERE `add_time`>=?i AND (`status`='2' OR `status`='3') AND `del` IS NULL ORDER BY `id`",strtotime('-2 month',strtotime(date('Y-m-01',time())))); $table_month = null; while($ls = $db->fetch($q)) { $add_time_month = strtotime(date('Y-m-01',$ls['add_time'])); 100 switch($add_time_month) { case strtotime('-1 month',strtotime(date('Y-m-01',time()))): $table_month = &$tables['2month']; break; case strtotime('-2 month',strtotime(date('Y-m-01',time()))): $table_month = &$tables['3month']; break; default: $table_month = &$tables['1month']; } if(!isset($table_month['date'])) { $table_month['date'] = strtotime(date('Y-m-01',$ls['add_time'])); } if($ls['status'] == 3) { $table_month['cancel']++; continue;} $table_month['count']++; $table_month['sum'] += $ls['goods_sum']; if(isset($table_month['user'][$ls['user']])) { $table_month['user'][$ls['user']]++; } else { $table_month['user'][$ls['user']] = 1; } } foreach($tables as $month_name => &$month) { $month['sum_middle'] = ($month['sum'] == 0 ? 0 : round($month['sum']/$month['count'])); $month['client'] = $db->getOne("SELECT COUNT(`id`) FROM `clients` WHERE `add_time`>=?i AND `add_time`$month['date'], strtotime('+1 month',$month['date'])); $table_all['count'] += $month['count']; 101 $table_all['sum'] += $month['sum']; $table_all['sum_middle'] += $month['sum_middle']; $table_all['cancel'] += $month['cancel']; $table_all['client'] = $db->getOne("SELECT COUNT(`id`) FROM `clients` WHERE `add_time`>=?i AND `add_time`<=?i AND `del` IS NULL", strtotime('-2 month',strtotime(date('Y-m-01',time()))), time()); foreach($month['user'] as $user_id => $user_count) { if(!isset($table_all['user'][$user_id])) { $table_all['user'][$user_id] = $user_count; } else { $table_all['user'][$user_id] += $user_count; } } } $table_all['sum_middle'] = round($table_all['sum']/$table_all['count']); $full_table = ' Продажи | '.$table_all['count'].' | '.$tables['1month' | ]['count'].' '.$tables['2month']['count'].' | '.$tables['3month']['count | '].' '; foreach($table_all['user'] as $user_id => $user_count) { $user_name = $db->getOne("SELECT `name` FROM `users` WHERE `id`=?i LIMIT 1",$user_id); $full_table .= ' | '.$user_name.' '.$user_count.' | '.(isset($tables['1month']['user'][$ | user_id]) ? $tables['1month']['user'][$user_id] : 0).' '.(isset($tables['2month']['user'][$user_id]) | ? $tables['2month']['user'][$user_id] : 0).' '.(isset($tables['3month']['user'][$user_id]) | ? $tables['3month']['user'][$user_id] : 0).' } 102 $full_table .= ' Оборот | '.$table_all['sum'].' р. | '.$tables['1month']['sum'].' р. | '.$tables['2month']['sum'].' р. | '.$tables['3month']['sum'].' р. | Средний чек | '.$table_all['sum_middle'].' р. | '.$tables['1month']['sum_middle'].' р. | '.$tables['2month']['sum_middle'].' р. | '.$tables['3month']['sum_middle'].' р. | Отменённые заказы | '.$table_all['cancel'].' | '.$tables['1month']['cancel'].' | '.$tables['2month']['cancel'].' | '.$tables['3month']['cancel'].' | Новых клиентов | '.$table_all['client'].' | '.$tables['1month']['client'].' | '.$tables['2month']['client'].' | '.$tables['3month']['client'].' | '; /* Добавляем в head страницы скрипт Google Charts */ $isys_page['head'] = ''; /* Выбор даты для детального отчёта */ $select_date = null; $q = $db->query("SELECT DATE_FORMAT(FROM_UNIXTIME(`add_time`), '%m-%Y') AS 'month_year' FROM `orders` WHERE `status`='2' AND `del` IS NULL GROUP BY `month_year` DESC"); while($ls = $db->fetch($q)) { $select_date .= ''; 103 } /* Если выбрана дата детальной статистики */ $month_table = null; $month_caption = null; $month_summ = 0; $month_count = 0; $month_cancel = 0; $month_start = 0; $month_end = 0; if(!empty($_POST['select_date'])) { $month_start = strtotime('01-'.$_POST['select_date']); $month_end = strtotime(date('t',$month_start).'-'.$_POST['select_date']); $month_caption = time2date($month_start,6); $isys_page['head'] .= ''; $q = $db->query("SELECT `id`,`add_time`,`status`,`goods_sum`,`pay`,`delivery`,(SELECT `name` FROM `clients` WHERE `id`=`client` LIMIT 1) as 'client',(SELECT `name` FROM `users` WHERE `id`=`user` LIMIT 1) as 'user' FROM `orders` WHERE `add_time`>=?i AND `add_time`<=?i AND (`status`='2' OR `status`='3') AND `del` IS NULL ORDER BY `add_time` ASC, `client`", $month_start, $month_end); while($ls = $db->fetch($q)) { if($ls['status'] == 3) { $month_cancel++; continue; } $month_count++; $month_summ += $ls['goods_sum']; 104 $month_table .= ' '.$ls['id'].' | '.time2date($ls['add_time'],9).' | '.$ls['cl ient'].' | '.$ls['goods_sum'].' р. | '.$isys_lang['orders']['pay'][$ls['pay']].' | '.$isys_lang['orders'][' | delivery'][$ls['delivery']].' '.$ls['user'].' | } } /* Передаём данные в шаблон страницы статистики */ $isys_page['content'] = data2template('reports', ['chart_count' => $chart_count, 'chart_sum' => $chart_sum, 'table' => $full_table, '1month' => time2date($tables['1month']['date'],6), '2month' => time2date($tables['2month']['date'],6), '3month' => time2date($tables['3month']['date'],6), 'select_date' => $select_date, 'month_table' => $month_table, 'month_caption' => $month_caption, 'month_summ' => $month_summ, 'month_count' => $month_count, 'month_cancel' => $month_cancel ]); ?> |