Added Tax Inquiry in Banking and General Ledger tab.
[fa-stable.git] / gl / inquiry / tax_inquiry.php
1 <?php
2
3 $page_security = 8;
4 $path_to_root="../..";
5 include_once($path_to_root . "/includes/session.inc");
6
7
8 include_once($path_to_root . "/includes/date_functions.inc");
9 include_once($path_to_root . "/includes/ui.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
11
12 include_once($path_to_root . "/gl/includes/gl_db.inc");
13
14 $js = '';
15 set_focus('account');
16 if ($use_popup_windows)
17         $js .= get_js_open_window(800, 500);
18 if ($use_date_picker)
19         $js .= get_js_date_picker();
20
21 page(_("Tax Inquiry"), false, false, '', $js);
22
23 //----------------------------------------------------------------------------------------------------
24 // Ajax updates
25 //
26 if (get_post('Show')) 
27 {
28         $Ajax->activate('trans_tbl');
29 }
30
31 if (get_post('TransFromDate') == "" && get_post('TransToDate') == "")
32 {
33         $date = Today();
34         $row = get_company_prefs();
35         $edate = add_months($date, -$row['tax_last']);
36         $edate = end_month($edate);
37         $bdate = add_months($edate, -$row['tax_prd'] + 1);
38         $_POST["TransFromDate"] = begin_month($bdate);
39         $_POST["TransToDate"] = $edate;
40 }       
41
42 //----------------------------------------------------------------------------------------------------
43
44 function get_tax_types()
45 {
46         $sql = "SELECT * FROM ".TB_PREF."tax_types ORDER BY id";
47     return db_query($sql,"No transactions were returned");
48 }
49
50 function tax_inquiry_controls()
51 {
52         global $table_style2;
53
54     start_form();
55
56     //start_table($table_style2);
57     start_table("class='tablestyle_noborder'");
58         start_row();
59
60         date_cells(_("from:"), 'TransFromDate', '', null, -30);
61         date_cells(_("to:"), 'TransToDate');
62         submit_cells('Show',_("Show"),'','', true);
63
64     end_row();
65
66         end_table();
67
68     end_form();
69 }
70
71 //----------------------------------------------------------------------------------------------------
72
73 function show_results()
74 {
75         global $path_to_root, $table_style;
76
77         $taxes = get_tax_types();
78
79     /*Now get the transactions  */
80         div_start('trans_tbl');
81         start_table($table_style);
82
83         $th = array(_("Type"), _("Description"), _("Amount"));
84         table_header($th);
85         $k = 0;
86         $total = 0;
87         $bdate = date2sql($_POST['TransFromDate']);
88         $edate  = date2sql($_POST['TransToDate']);
89         while ($tx = db_fetch($taxes))
90         {
91                 if ($tx['sales_gl_code'] == $tx['purchasing_gl_code'])
92                 {
93                         $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) AS payable, SUM(IF(amount < 0, -amount, 0)) AS collectible
94                                 FROM ".TB_PREF."gl_trans WHERE account = '".$tx['sales_gl_code']."' AND tran_date >= '$bdate' AND tran_date <= '$edate'";
95                         $result = db_query($sql, "Error retrieving tax inquiry");
96                         $row = db_fetch($result);
97                         $payable = -$row['payable'];
98                         $collectible.= -$row['collectible'];
99                 }
100                 else
101                 {
102                         $sql = "SELECT SUM(amount) AS collectible
103                                 FROM ".TB_PREF."gl_trans WHERE account = '".$tx['sales_gl_code']."' AND tran_date >= '$bdate' AND tran_date <= '$edate'";
104                         $result = db_query($sql, "Error retrieving tax inquiry");
105                         $row = db_fetch($result);
106                         $collectible = -$row['collectible'];
107                         $sql = "SELECT SUM(amount) AS payable
108                                 FROM ".TB_PREF."gl_trans WHERE account = '".$tx['purchasing_gl_code']."' AND tran_date >= '$bdate' AND tran_date <= '$edate'";
109                         $result = db_query($sql, "Error retrieving tax inquiry");
110                         $row = db_fetch($result);
111                         $payable = -$row['payable'];
112                 }
113                 $net = $collectible + $payable;
114                 $total += $net;
115                 alt_table_row_color($k);
116                 label_cell($tx['name'] . " " . $tx['rate'] . "%");
117                 label_cell(_("Charged on sales") . " (" . _("Output Tax")."):");
118                 amount_cell($collectible);
119                 end_row();
120                 alt_table_row_color($k);
121                 label_cell($tx['name'] . " " . $tx['rate'] . "%");
122                 label_cell(_("Paid on purchases") . " (" . _("Input Tax")."):");
123                 amount_cell($payable);
124                 end_row();
125                 alt_table_row_color($k);
126                 label_cell($tx['name'] . " " . $tx['rate'] . "%");
127                 label_cell(_("Net payable or collectible") . ":");
128                 amount_cell($net, true);
129                 end_row();
130         }       
131         alt_table_row_color($k);
132         label_cell("");
133         label_cell("<b>"._("Total payable or refund") . ":</b>");
134         amount_cell($total, true);
135         end_row();
136
137         end_table(2);
138         div_end();
139 }
140
141 //----------------------------------------------------------------------------------------------------
142
143 tax_inquiry_controls();
144
145 show_results();
146
147 //----------------------------------------------------------------------------------------------------
148
149 end_page();
150
151 ?>