payment.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:18k
- <?php
- // +-------------------------------------------------------------+
- // | DeskPRO v [2.0.1 Production]
- // | Copyright (C) 2001 - 2004 Headstart Solutions Limited
- // | Supplied by WTN-WDYL
- // | Nullified by WTN-WDYL
- // | Distribution via WebForum, ForumRU and associated file dumps
- // +-------------------------------------------------------------+
- // | DESKPRO IS NOT FREE SOFTWARE
- // +-------------------------------------------------------------+
- // | License ID : Full Enterprise License =) ...
- // | License Owner : WTN-WDYL Team
- // +-------------------------------------------------------------+
- // | $RCSfile: payment.php,v $
- // | $Date: 2004/02/10 01:34:25 $
- // | $Revision: 1.31 $
- // +-------------------------------------------------------------+
- // | File Details:
- // | - Billables/payables report generator (administration interface)
- // +-------------------------------------------------------------+
- error_reporting(E_ALL & ~E_NOTICE);
- require_once('./global.php');
- //Nullify WTN-WDYL Team
- include('./settings_include.php');
- feature_check('payments');
- ############################### UPDATE SETTINGS ###############################
- if ($_REQUEST['do'] == "update") {
- update_settings();
- $_REQUEST['do'] = "settings";
- }
- ############################### DISPLAY SETTINGS ###############################
- if ($_REQUEST['do'] == "settings") {
- admin_header('Payment Functions', 'Payment Settings');
- show_settings('payment.php', array('Payment Settings'), false);
- exit;
- }
- ############################### RUN PAYMENT REPORTS ###############################
- admin_header('Payment Functions', 'Run Payment Reports');
- if ($_REQUEST['submit'] AND $_REQUEST['ids']) {
- $ids = explode(',',$_REQUEST['ids']);
- $queries = 0;
- if (is_array($_REQUEST['isbillable'])) {
- $sql_billable = array_intersect($ids, $_REQUEST['isbillable']);
- $sql_unbillable = array_diff($ids, $_REQUEST['isbillable']);
- } else {
- $sql_unbillable = $ids;
- }
- if (is_array($_REQUEST['ispaid'])) {
- $sql_paid = array_intersect($ids, $_REQUEST['ispaid']);
- $sql_unpaid = array_diff($ids, $_REQUEST['ispaid']);
- } else {
- $sql_unpaid = $ids;
- }
- if (is_array($sql_billable)) {
- if (count($sql_billable)) {
- $db->query('UPDATE user_bill SET billable = 1 WHERE id IN ' . array2sql($sql_billable));
- $queries++;
- }
- }
- if (is_array($sql_unbillable)) {
- if (count($sql_unbillable)) {
- $db->query('UPDATE user_bill SET billable = 0 WHERE id IN ' . array2sql($sql_unbillable));
- $queries++;
- }
- }
- if (is_array($sql_paid)) {
- if (count($sql_paid)) {
- $db->query('UPDATE user_bill SET paid = 1 WHERE id IN ' . array2sql($sql_paid));
- $queries++;
- }
- }
- if (is_array($sql_unpaid)) {
- if (count($sql_unpaid)) {
- $db->query('UPDATE user_bill SET paid = 0 WHERE id IN ' . array2sql($sql_unpaid));
- $queries++;
- }
- }
- }
- $ids = array();
- $ticketid = (int)$_REQUEST['ticketid'];
- $paid = (int)$_REQUEST['paid'];
- if ($_REQUEST['bytech']) {
- $group = 'techname';
- } else {
- $group = 'username';
- }
- if (!(int)$_REQUEST['billable']) {
- $billable = 1;
- } else {
- $billable = (int)$_REQUEST['billable'];
- }
- $detail = (int)$_REQUEST['detail'];
- if ($ticketid) { // If a specific ticket is requested, we're doing a detailed view
- $detail = 1;
- }
- $ystart = (int)$_REQUEST['ystart'];
- $mstart = (int)$_REQUEST['mstart'];
- $dstart = (int)$_REQUEST['dstart'];
- $yend = (int)$_REQUEST['yend'];
- $mend = (int)$_REQUEST['mend'];
- $dend = (int)$_REQUEST['dend'];
- $start = validate_ymd("$ystart-$mstart-$dstart");
- $end = validate_ymd("$yend-$mend-$dend");
- $stamp = time();
- $now = split('-', date('Y-m-d', $stamp));
- if (!$start) { // Default to starting at the beginning of the current month
- $start = strtotime("$now[0]-$now[1]-01");
- } else {
- $start = strtotime($start);
- }
- if (!$end OR $_REQUEST['alldates']) { // Default to ending right now
- $end = $stamp;
- } else {
- $end = strtotime($end) + ((60 * 60 * 24) - 1);
- }
- if ($_REQUEST['alldates']) { // If we've been asked to do everything, start should be zero
- $start = 0;
- }
- if ($settings['log_to_billing']) {
- $notes[] = '<B>Note:</B> Automatic billing is <I>not</I> enabled; results may be incomplete.';
- }
- if ($settings['payments']) {
- $notes[] = '<B>Warning:</B> Payments are not currently enabled; results may be incomplete.';
- }
- if (is_array($notes)) {
- $notes = join('<BR>', $notes);
- }
- if (is_array($_REQUEST['userid'])) {
- if (!in_array(0, $_REQUEST['userid'])) {
- $db->query('SELECT id FROM user WHERE id IN ' . array2sql($_REQUEST['userid']));
- while ($res = $db->row_array()) {
- $validusers[] = $res['id'];
- }
- $invalidusers = array_diff($_REQUEST['userid'], $validusers);
- if (count($invalidusers)) {
- $warnings[] = 'User(s) not valid: ' . join(', ', $invalidusers);
- }
- $terms[] = 'user_bill.userid in ' . array2sql($_REQUEST['userid']);
- if (count($validuser)) {
- $criteria[] = 'user ID(s) ' . join(', ', $validusers);
- }
- }
- }
- if (is_array($_REQUEST['techid'])) {
- if (!in_array(0, $_REQUEST['techid'])) {
- $db->query('SELECT id FROM tech WHERE id IN ' . array2sql($_REQUEST['techid']));
- while ($res = $db->row_array()) {
- $validtechs[] = $res['id'];
- }
- $invalidtechs = array_diff($_REQUEST['techid'], $validtechs);
- if (count($invalidtechs)) {
- $warnings[] = 'Tech(s) not valid: ' . join(', ', $invalidtechs);
- }
- $terms[] = 'user_bill.techid in ' . array2sql($_REQUEST['techid']);
- $criteria[] = 'technician ID(s) ' . join(', ', $validtechs);
- }
- }
- if ($ticketid) {
- $db->query("SELECT id FROM ticket WHERE id = $ticketid");
- if ($db->num_rows()) {
- $terms[] = "user_bill.ticketid = $ticketid";
- $criteria[] = "ticket #$ticketid";
- } else {
- $warnings[] = "Ticket $ticketid couldn't be found.";
- }
- }
- if ($billable == 1) { // Billable = 0, show non-billable. Billable = 1, show billable. Billable = anything else, show both
- $terms[] = 'billable';
- $criteria[] = 'billable';
- } elseif ($billable == 0) {
- $terms[] = '!billable';
- $criteria[] = 'non-billable';
- } else {
- $billable = 2;
- $criteria[] = 'billable and non-billable';
- }
- if ($paid == 1) { // Paid = 0, show unpaid. Paid = 1, show paid. Paid = anything else, show all
- $terms[] = 'paid';
- $criteria[] = 'paid';
- } elseif ($paid == 0) {
- $terms[] = '!paid';
- $criteria[] = 'unpaid';
- } else {
- $paid = 2;
- $criteria[] = 'paid and unpaid';
- }
- $terms[] = "stamp >= $start";
- $terms[] = "stamp <= $end";
- if ($start) {
- $criteria[] = date('F jS Y', $start) . ' to ' . date('F jS Y', $end);
- } else {
- $criteria[] = 'all dates';
- }
- if (is_array($terms)) {
- $where = join(' AND ', $terms);
- }
- if ($detail) {
- $criteria[] = '<I>detail view</I>';
- $query = "SELECT user_bill.id, time, charge, user.username, user.id AS userid, tech.username AS techname, tech.id AS techid, ticket.id as ticketid, stamp, paid, billable
- FROM user_bill
- LEFT JOIN user ON user_bill.userid = user.id
- LEFT JOIN ticket ON user_bill.ticketid = ticket.id
- LEFT JOIN tech ON user_bill.techid = tech.id
- WHERE $where
- ORDER BY username, stamp";
- } else {
- $criteria[] = '<I>summary view</I>';
- $query = "SELECT SUM(time) AS time, SUM(charge) AS charge, user.username, user.id AS userid, tech.username AS techname, tech.id AS techid, ticket.id as ticketid, stamp, paid, billable, ticketid
- FROM user_bill
- LEFT JOIN user ON user_bill.userid = user.id
- LEFT JOIN ticket ON user_bill.ticketid = ticket.id
- LEFT JOIN tech ON user_bill.techid = tech.id
- WHERE $where
- GROUP BY $group
- ORDER BY username, stamp";
- }
- $db->query($query);
- $total = 0;
- if ($detail) {
- if ($db->num_rows()) {
- $paid_js = ' (<A HREF="javascript:checkall(document.forms['res'].elements['ispaid[]']);">All</A> <A HREF="javascript:uncheckall(document.res.elements['ispaid[]']);">None</A>)';
- $bill_js = ' (<A HREF="javascript:checkall(document.forms['res'].elements['isbillable[]']);">All</A> <A HREF="javascript:uncheckall(document.res.elements['isbillable[]']);">None</A>)';
- } else {
- $paid_js = '';
- $bill_js = '';
- $no_update = 1;
- }
- $cols = array(
- 'User',
- 'Technician',
- 'Ticket ID',
- 'Time',
- 'Charge',
- "Paid $paid_js",
- "Billable $bill_js",
- 'Date/Time'
- );
- } else {
- $cols = array(
- iff($_REQUEST['bytech'], 'Technician', 'User'),
- 'Time',
- 'Charge'
- );
- }
- $total_users = array();
- $total_techs = array();
- $total_tickets = array();
- $total_time = 0;
- $total_charge = 0;
- $total_billable = 0;
- $total_unbillable = 0;
- $total_paid = 0;
- $total_unpaid = 0;
- while ($res = $db->row_array()) {
- if ($_REQUEST['daily_tech']) {
- $daily_tech[date('Y-m-d', $res['stamp'])][$res['techid']]['charge'] += $res['charge'];
- $daily_tech[date('Y-m-d', $res['stamp'])][$res['techid']]['time'] += $res['time'];
- }
- if ($_REQUEST['daily_user']) {
- $daily_user[date('Y-m-d', $res['stamp'])][$res['userid']]['charge'] += $res['charge'];
- $daily_user[date('Y-m-d', $res['stamp'])][$res['userid']]['time'] += $res['time'];
- }
- if ($detail) {
- $rows[] = array(
- $res['username'],
- $res['techname'],
- $res['ticketid'],
- clean_time($res['time']),
- sprintf('%01.2f', $res['charge']),
- form_checkbox_single('ispaid[]', $res['id'], iff($res['paid'], 1, 0)),
- form_checkbox_single('isbillable[]', $res['id'], iff($res['billable'], 1, 0)),
- date('r', $res['stamp'])
- );
- $ids[] = $res['id'];
- } else {
- $rows[] = array(
- iff($_REQUEST['bytech'], $res['techname'], $res['username']),
- clean_time($res['time']),
- sprintf('%01.2f', $res['charge'])
- );
- }
- $total_users[$res['userid']]++;
- $total_techs[$res['techid']]++;
- $total_tickets[$res['ticketid']]++;
- $total_time += $res['time'];
- $total_charge += $res['charge'];
- $total_billable += (iff($res['billable'], 1, 0));
- $total_unbillable += (iff(!$res['billable'], 1, 0));
- $total_paid += (iff($res['paid'], 1, 0));
- $total_unpaid += (iff(!$res['paid'], 1, 0));
- if ($total_earliest) {
- $total_earliest = (iff(($res['stamp'] < $total_earliest), $res['stamp'], $total_earliest));
- } else {
- $total_earliest = $res['stamp'];
- }
- $total_latest = (iff(($res['stamp'] > $total_latest), $res['stamp'], $total_latest));
- }
- if (count($rows) > 0) {
- if ($detail) {
- $rows[] = array(
- '<HR><B>' . count($total_users) . '</B> user(s)<BR> ',
- '<HR><B>' . count($total_techs) . '</B> tech(s)<BR> ',
- '<HR><B>' . count($total_tickets) . '</B> ticket(s)<BR> ',
- '<HR><B>' . clean_time($total_time) . '</B><BR> ',
- '<HR><B>' . sprintf('%01.2f', $total_charge) . '</B><BR> ',
- "<HR><B>$total_paid</B> paid<BR><B>$total_unpaid</B> unpaid",
- "<HR><B>$total_billable</B> billable<BR><B>$total_unbillable</B> unbillable",
- '<HR><B>' . date('D M jS Y g:i:sa', $total_earliest) . '</B> to<BR><B>' . date('D M jS Y g:i:sa', $total_latest) . '</B>'
- );
- } else {
- $rows[] = array(
- '<HR><B>' . count($total_users) . '</B>' . iff($_REQUEST['bytech'], ' tech(s)', ' user(s)') . '<BR> ',
- '<HR><B>' . clean_time($total_time) . '</B><BR> ',
- '<HR><B>' . sprintf('%01.2f', $total_charge) . '</B><BR> '
- );
- }
- } else {
- $rows[] = array('<B>No items found.</B>');
- }
- $db->query("SELECT username, id FROM tech ORDER BY username");
- $techs[0] = 'Include All Techs';
- while ($result = $db->row_array()) {
- $techs[$result[id]] = $result[username];
- }
- $db->query("SELECT username, id FROM user ORDER BY username");
- $users[0] = 'Include All Users';
- while ($result = $db->row_array()) {
- $users[$result[id]] = $result[username];
- }
- $form[] = array('<FORM METHOD="post" ACTION="payment.php"><B>Limit report to:</B>',
- '<TABLE><TR><TD>Techs:</TD><TD>Users:</TD></TR>
- <TR><TD>'. form_select('techid', $techs, '', $_REQUEST['techid'], 0, '', 4) . '</TD><TD>' .
- form_select('userid', $users, '', $_REQUEST['userid'], 0, '', 4) . '</TD></TR></TABLE>');
- $form[] = array('<B>Show Items Occuring in Range:</B>',
- form_date('start', '', '', '', '', date('Y-m-d', $start)) . ' to ' .
- form_date('end', '', '', '', '', date('Y-m-d', $end)));
- $form[] = array('<B>Show Detailed View:</B>',
- form_checkbox_single('detail', 1, $detail) . 'Detail View');
- $form[] = array('<B>Search Entry Types:</B>',
- '<TABLE><TR><TD><B>Billable</B>' .
- form_radio_single('billable', '0', iff(($billable == 0), 1, 0)) . 'Non-billable only<BR>' .
- form_radio_single('billable', '1', iff(($billable == 1), 1, 0)) . 'Billable only<BR>' .
- form_radio_single('billable', '2', iff(($billable == 2), 1, 0)) . 'Both' .
- '</TD><TD><B>Paid</B>' .
- form_radio_single('paid', '0', iff(($paid == 0), 1, 0)) . 'Unpaid only<BR>' .
- form_radio_single('paid', '1', iff(($paid == 1), 1, 0)) . 'Paid only<BR>' .
- form_radio_single('paid', '2', iff(($paid == 2), 1, 0)) . 'Both' .
- '</TD></TR></TABLE>'
- );
- $form[] = array('<B>Show Details for Ticket:</B>',
- 'Ticket # ' . form_input('ticketid', iff($ticketid, $ticketid, ''), 10)
- );
- $form[] = array('<B>Summarize By:</B><BR><I>Summary views only; ignored in detail views</I>',
- form_radio_single('bytech', '0', iff($_REQUEST['bytech'], 0, 1)) . ' by User ' .
- form_radio_single('bytech', '1', iff($_REQUEST['bytech'], 1, 0)) . ' by Technician '
- );
- $form[] = array('<B>Show Daily Totals by Tech</B>',
- form_radio_yn('daily_tech', '', $_REQUEST['daily_tech']));
- $form[] = array('<B>Show Daily Totals by User</B>',
- form_radio_yn('daily_user', '', $_REQUEST['daily_user']));
- $form[] = array('<B>Update View</B>', form_submit('Update'));
- $this_month_start = strtotime("$now[0]-$now[1]-01");
- $last_month_start = explode('-', date('Y-m', strtotime(date('Y-m-d', $this_month_start) . ' -1 month')));
- $last_month_end = explode('-', date('Y-m-d', strtotime(date('Y-m-d', $this_month_start) . ' -1 second')));
- $summaries_bu[] = '<A HREF="payment.php?billable=1">All Billable (this month)</A>';
- $summaries_bu[] = '<A HREF="payment.php?paid=0&billable=1">Unpaid Billable (this month)</A>';
- $summaries_bu[] = "<A HREF="payment.php?paid=0&billable=1&ystart=$last_month_start[0]&mstart=$last_month_start[1]&dstart=1¥d=$last_month_end[0]&mend=$last_month_end[1]&dend=$last_month_end[2]">Unpaid Billable (last month)</A>";
- $summaries_bu[] = '<A HREF="payment.php?paid=0&billable=1&alldates=1">Unpaid Billable (all)</A>';
- $summaries_bt[] = '<A HREF="payment.php?bytech=1&billable=1">All Billable (this month)</A>';
- $summaries_bt[] = '<A HREF="payment.php?paid=0&billable=1&bytech=1">Unpaid Billable (this month)</A>';
- $summaries_bt[] = "<A HREF="payment.php?paid=0&billable=1&bytech=1&ystart=$last_month_start[0]&mstart=$last_month_start[1]&dstart=1¥d=$last_month_end[0]&mend=$last_month_end[1]&dend=$last_month_end[2]">Unpaid Billable (last month)</A>";
- $summaries_bt[] = '<A HREF="payment.php?paid=0&billable=1&alldates=1&bytech=1">Unpaid Billable (all)</A>';
- $details[] = '<A HREF="payment.php?billable=1&detail=1">All Billable (this month)</A>';
- $details[] = '<A HREF="payment.php?paid=0&billable=1&detail=1">Unpaid Billable (this month)</A>';
- $details[] = "<A HREF="payment.php?paid=0&billable=1&detail=1&ystart=$last_month_start[0]&mstart=$last_month_start[1]&dstart=1¥d=$last_month_end[0]&mend=$last_month_end[1]&dend=$last_month_end[2]">Unpaid Billable (last month)</A>";
- $details[] = '<A HREF="payment.php?paid=0&billable=1&detail=1&alldates=1">Unpaid Billable (all)</A>';
- $prebuilds = array(
- '<TABLE><TR><TD><B><U>Summaries (by User):</U></B><BR>' . join('<BR>', $summaries_bu) . '</TD><TD> </TD>' .
- '<TD><B><U>Summaries (by Technician):</U></B><BR>' . join('<BR>', $summaries_bt) . '</TD><TD> </TD>' .
- '<TD><B><U>Detail Views:</U></B><BR>' . join('<BR>', $details) . '</TD></TR></TABLE>'
- );
- if ($queries) {
- print '<B>Item(s) updated.</B><BR>';
- }
- print '<B>Showing: </B>' . join('; ', $criteria) . '<BR>';
- if ($warnings) {
- print '<B>Warnings: </B>' . join('; ', $warnings) . '<BR>';
- }
- print '<BR>';
- ?>
- <?
- if ($daily_tech) {
- $d_rows = array();
- $techs = $db->query_return_array_id("SELECT username, id FROM tech", 'username');
- foreach ($daily_tech AS $date => $val) {
- $d_rows[] = array('<B>'.our_date(strtotime($date), 'day').'</B>');
- foreach ($val AS $tech => $data) {
- $d_rows[] = array($techs[$tech], sprintf('%01.2f', $data['charge']), clean_time($data['time']));
- }
- }
- $d_cols = array('Technician', 'Charge', 'Time');
- table_header('Daily Totals by Technician');
- table_content($d_cols, $d_rows);
- table_footer();
- }
- if ($daily_user) {
- $d_rows = array();
- $users = $db->query_return_array_id("SELECT username, id FROM user", 'username');
- foreach ($daily_user AS $date => $val) {
- $d_rows[] = array('<B>'.our_date(strtotime($date), 'day').'</B>');
- foreach ($val AS $usr => $data) {
- $d_rows[] = array($users[$usr], sprintf('%01.2f', $data['charge']), clean_time($data['time']));
- }
- }
- $d_cols = array('User', 'Charge', 'Time');
- table_header('Daily Totals by User');
- table_content($d_cols, $d_rows);
- table_footer();
- }
- print "</FORM>";
- print '<FORM METHOD="POST" ACTION="payment.php" NAME="res" ID="res">';
- table_header('Results');
- table_content($cols, $rows);
- table_footer();
- print form_hidden('techid', $_REQUEST['techid']);
- print form_hidden('userid', $_REQUEST['userid']);
- print form_hidden('ystart', $_REQUEST['ystart']);
- print form_hidden('mstart', $_REQUEST['mstart']);
- print form_hidden('dstart', $_REQUEST['dstart']);
- print form_hidden('yend', $_REQUEST['yend']);
- print form_hidden('mend', $_REQUEST['mend']);
- print form_hidden('dend', $_REQUEST['dend']);
- print form_hidden('detail', $_REQUEST['detail']);
- print form_hidden('billable', $_REQUEST['billable']);
- print form_hidden('paid', $_REQUEST['paid']);
- print form_hidden('ticketid', $_REQUEST['ticketid']);
- print form_hidden('bytech', $_REQUEST['bytech']);
- if (is_array($ids)) {
- print form_hidden('ids', join(',', $ids));
- }
- if (!$no_update) {
- print form_submit('Update Items') . '</FORM><BR>';
- }
- table_header('Quick Reports');
- table_content('', $prebuilds);
- table_footer();
- table_header('Report Options');
- table_content('', $form);
- table_footer();
- ?>
- </FORM>
- <SCRIPT LANGUAGE="JavaScript">
- function checkall(field) {
- field.checked = true;
- for (var i = 0; i < field.length; i++)
- field[i].checked = true ;
- }
- function uncheckall(field) {
- field.checked = false;
- for (var i = 0; i < field.length; i++)
- field[i].checked = false;
- }
- </SCRIPT>