<?php

/***************************************************************************
 *   copyright            : (C) 2012 PCRepairTracker.com
 *   email                : info@pcrepairtracker.com
 *   This program is a copyrighted work.
 *   Please see the license.txt file for details
 *
 ***************************************************************************/

if (array_key_exists('func',$_REQUEST)) {
$func = $_REQUEST['func'];
} else {
$func = "";
}

function taxreport() {

require("deps.php");
require_once("common.php");
perm_boot("5");

if (function_exists('date_default_timezone_set')) {
date_default_timezone_set("$pcrt_timezone");
}


if (array_key_exists('year',$_REQUEST)) {
if ($_REQUEST['year'] != "") {
$year = $_REQUEST['year'];
} else {
$year = date("Y");
}
} else {
$year = date("Y");
}

if (array_key_exists('month',$_REQUEST)) {
if ($_REQUEST['month'] != "") {
$qmonth = $_REQUEST['month'];
} else {
$qmonth = date("M");
}
} else {
$qmonth = date("M");
}



if (array_key_exists('fiscaldate',$_REQUEST)) {
$fiscaldate = $_REQUEST['fiscaldate'];
} else {
$fiscaldate = 0;
}



if(!isset($_REQUEST['printable'])) {
require_once("header.php");
} else {
if (!$fiscaldate) {
echo printableheader(pcrtlang("Yearly Sales/Service Tax Report for")." $year");
} else {
echo printableheader(pcrtlang("Yearly Sales/Service Tax Report for Fiscal")." $year");
}
}

if(!isset($_REQUEST['printable'])) {
start_box();
}

echo "<table width=100%><tr><td width=75% align=left>";
if (!$fiscaldate) {
echo "<span class=sizeme20>".pcrtlang("Yearly Sales/Service Tax Report for")." $year</span><br><br>";
} else {
echo "<span class=sizeme20>".pcrtlang("Yearly Sales/Service Tax Report for Fiscal")." $year</span><br><br>";
}

if($fiscaldate) {
echo "<span class=sizeme12>".pcrtlang("Fiscal Year Starting")." $year-$qmonth-01</span><br><br>";
}

echo "</td><td width=25% align=right>";
if(!isset($_REQUEST['printable'])) {
echo "<a href=reports.php?func=taxreportnew&year=$year&printable=yes&month=$qmonth&fiscaldate=$fiscaldate class=linkbutton><img src=images/print.png border=0 align=absmiddle> ".pcrtlang("Printable")."</a>";
echo "<br><a href=reports.php?func=taxreportnew_csv&year=$year&month=$qmonth&fiscaldate=$fiscaldate class=linkbutton><img src=images/csv.png border=0 align=absmiddle> ".pcrtlang("CSV")."</a>";
}


echo "</td></tr></table>";




if(!isset($_REQUEST['printable'])) {
if(!$fiscaldate) {
echo "<span class=sizeme20>".pcrtlang("Year Total for")." $year &nbsp;</span><br><br>";
} else {
echo "<span class=sizeme20>".pcrtlang("Year Total for Fiscal")." $year &nbsp;</span><br><br>";
}
} else {
if(!$fiscaldate) {
echo "<span class=sizeme20>".pcrtlang("Year Total for")." $year &nbsp;</span><br><br>";
} else {
echo "<span class=sizeme20>".pcrtlang("Year Total for Fiscal")." $year &nbsp;</span><br><br>";
}
}


echo "<table class=\"standard lastalignright\">";
$rs_find_cart_tax = "SELECT * FROM taxes WHERE isgrouprate != '1'";
$rs_result_tax = mysqli_query($rs_connect, $rs_find_cart_tax);
while($rs_result_tax_q = mysqli_fetch_object($rs_result_tax)) {
$rs_taxid = "$rs_result_tax_q->taxid";
$rs_taxname = "$rs_result_tax_q->taxname";
$rs_taxenabled = "$rs_result_tax_q->taxenabled";


$groupsbelong = array();
$groupsbelong[] = "$rs_taxid";
$rs_ql_isingroup = "SELECT * FROM taxes WHERE isgrouprate = '1'";
$rs_result1_isingroup = mysqli_query($rs_connect, $rs_ql_isingroup);
while($rs_result_q1_isingroup = mysqli_fetch_object($rs_result1_isingroup)) {
$compositerate = "$rs_result_q1_isingroup->compositerate";
$gtaxid = "$rs_result_q1_isingroup->taxid";
$subarray = unserialize($compositerate);
if (in_array($rs_taxid,$subarray)) {
$groupsbelong[] = $gtaxid;
}
}



if ($rs_taxenabled != "1") {
$ycolor="<span class=italme>";
} else {
$ycolor="<span>";
}

$taxids = implode(',',$groupsbelong);  


if(!$fiscaldate) {
$rs_find_cart_purt_purchase = "SELECT SUM(sold_items.sold_price) AS total FROM sold_items,receipts WHERE YEAR(sold_items.date_sold) = '$year' 
AND sold_items.sold_type = 'purchase' AND sold_items.taxex IN ($taxids) AND receipts.invoice_id = ''";
$rs_find_invoice_purt_purchase = "SELECT SUM(sold_items.sold_price) AS total FROM sold_items,receipts WHERE YEAR(sold_items.date_sold) = '$year'
AND sold_items.sold_type = 'purchase' AND sold_items.taxex IN ($taxids) AND receipts.invoice_id = ''";


$rs_find_cart_purt_labor = "SELECT SUM(sold_items.sold_price) AS total FROM sold_items,receipts WHERE YEAR(sold_items.date_sold) = '$year' 
AND sold_items.sold_type = 'labor' AND sold_items.taxex IN ($taxids) AND receipts.invoice_id = ''";


$rs_find_cart_purt_refund = "SELECT SUM(sold_price) AS total FROM sold_items WHERE YEAR(date_sold) = '$year' AND sold_type = 'refund' AND taxex IN ($taxids)";
$rs_find_cart_purt_refundlabor = "SELECT SUM(sold_price) AS total FROM sold_items WHERE YEAR(date_sold) = '$year' AND sold_type = 'refundlabor' AND taxex IN ($taxids)";
} else {
$rs_find_cart_purt_purchase = "SELECT SUM(sold_items.sold_price) AS total FROM sold_items,receipts WHERE sold_items.date_sold >= '$year-$qmonth-01' AND sold_items.date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 1 YEAR)) AND sold_items.sold_type = 'purchase' AND sold_items.taxex IN ($taxids) AND receipts.invoice_id = ''";
$rs_find_cart_purt_labor = "SELECT SUM(sold_items.sold_price) AS total FROM sold_items,receipts WHERE sold_items.date_sold >= '$year-$qmonth-01' AND sold_items.date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 1 YEAR)) AND sold_items.sold_type = 'labor' AND sold_items.taxex IN ($taxids) AND receipts.invoice_id = ''";
$rs_find_cart_purt_refund = "SELECT SUM(sold_price) AS total FROM sold_items WHERE date_sold >= '$year-$qmonth-01' AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 1 YEAR)) AND sold_type = 'refund' AND taxex IN ($taxids)";
$rs_find_cart_purt_refundlabor = "SELECT SUM(sold_price) AS total FROM sold_items WHERE date_sold >= '$year-$qmonth-01' AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 1 YEAR)) AND sold_type = 'refundlabor' AND taxex IN ($taxids)";
}


$rs_result_purt_purchase = mysqli_query($rs_connect, $rs_find_cart_purt_purchase);
$rs_result_pur_purchase_qt = mysqli_fetch_object($rs_result_purt_purchase);
$rs_totalsales_purchase = "$rs_result_pur_purchase_qt->total";
$salestaxrate = getsalestaxrate($rs_taxid);
$rs_totaltax_purchase = $rs_totalsales_purchase * $salestaxrate;

$rs_result_purt_labor = mysqli_query($rs_connect, $rs_find_cart_purt_labor);
$rs_result_pur_labor_qt = mysqli_fetch_object($rs_result_purt_labor);
$rs_totalsales_labor = "$rs_result_pur_labor_qt->total";
$servicetaxrate = getservicetaxrate($rs_taxid);
$rs_totaltax_labor = $rs_totalsales_labor * $servicetaxrate;

$rs_result_purt_refund = mysqli_query($rs_connect, $rs_find_cart_purt_refund);
$rs_result_pur_refund_qt = mysqli_fetch_object($rs_result_purt_refund);
$rs_totalsales_refund = "$rs_result_pur_refund_qt->total";
$rs_totaltax_refund = $rs_totalsales_refund * $salestaxrate;

$rs_result_purt_refundlabor = mysqli_query($rs_connect, $rs_find_cart_purt_refundlabor);
$rs_result_pur_refundlabor_qt = mysqli_fetch_object($rs_result_purt_refundlabor);
$rs_totalsales_refundlabor = "$rs_result_pur_refundlabor_qt->total";
$rs_totaltax_refundlabor = $rs_totalsales_refundlabor * $servicetaxrate;


$rs_taxtotal = $rs_totaltax_purchase + $rs_totaltax_labor;
$rs_taxtotalrefund = $rs_totaltax_refund + $rs_totaltax_refundlabor;


echo "<tr><td>$ycolor$rs_taxname ".pcrtlang("Tax Collected").":</span> </td><td>$ycolor$money".mf("$rs_taxtotal")."</span></td></tr>";
echo "<tr><td>$ycolor$rs_taxname ".pcrtlang("Tax Refunded").":</span> </td><td>$ycolor$money".mf("$rs_taxtotalrefund")."</span></td></tr>";

$rs_taxtotalcolminusrefund = $rs_taxtotal - $rs_taxtotalrefund;

echo "<tr><td>$ycolor$rs_taxname ".pcrtlang("Tax").":</span> </td><td>$ycolor$money".mf("$rs_taxtotalcolminusrefund")."</span></td></tr>";
echo "<tr><td>$ycolor$rs_taxname ".pcrtlang("Tax Basis")." (".pcrtlang("purchases")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_purchase")."</span></td></tr>";
echo "<tr><td>$ycolor$rs_taxname ".pcrtlang("Tax Basis")." (".pcrtlang("labor")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_labor")."</span></td></tr>";
echo "<tr><td>$ycolor$rs_taxname ".pcrtlang("Tax Basis")." (".pcrtlang("sales refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_refund")."</span></td></tr>";
echo "<tr><td>$ycolor$rs_taxname ".pcrtlang("Tax Basis")." (".pcrtlang("labor refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_refundlabor")."</span></td></tr>";


$rs_totalpur = $rs_totalsales_purchase - $rs_totalsales_refund;

echo "<tr><td>$ycolor$rs_taxname ".pcrtlang("Tax Basis")." (".pcrtlang("purchases")."-".pcrtlang("sales refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totalpur")."</span></td></tr>";

$rs_totallabpur = (($rs_totalsales_purchase + $rs_totalsales_labor) - ($rs_totalsales_refund + $rs_totalsales_refundlabor));
echo "<tr><td>$ycolor$rs_taxname ".pcrtlang("Tax Basis")." (".pcrtlang("labor")."+".pcrtlang("purchases")."-".pcrtlang("sales refunds")."+".pcrtlang("labor refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totallabpur")."</span></td></tr>";
echo "<tr><td colspan=2>&nbsp;</td></tr>";

}
echo "</table>";

###quarter

if(!isset($_REQUEST['printable'])) {
if(!$fiscaldate) {
echo "<br><br><span class=sizeme20>".pcrtlang("Quarter Total for")." $year &nbsp;</span><br><br>";
} else {
echo "<br><br><span class=sizeme20>".pcrtlang("Quarter Total for Fiscal")." $year &nbsp;</span><br><br>";
}
} else {
if(!$fiscaldate) {
echo "<br><br><span class=sizeme20>".pcrtlang("Quarter Total for")." $year &nbsp;</span><br><br>";
} else {
echo "<br><br><span class=sizeme20>".pcrtlang("Quarter Total for Fiscal")." $year &nbsp;</span><br><br>";
}
}


echo "<table class=\"standard lastalignright\">";

$fiscalquarter[1] = "date_sold >= '$year-$qmonth-01' AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 3 MONTH))";
$fiscalquarter[2] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 3 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 6 MONTH))";
$fiscalquarter[3] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 6 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 9 MONTH))";
$fiscalquarter[4] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 9 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 1 YEAR))";


$qrtr = 1;

while ($qrtr < 5) {

$rs_find_cart_tax2 = "SELECT * FROM taxes WHERE isgrouprate != '1'";
$rs_result_tax2 = mysqli_query($rs_connect, $rs_find_cart_tax2);

while($rs_result_tax_q2 = mysqli_fetch_object($rs_result_tax2)) {
$rs_taxid2 = "$rs_result_tax_q2->taxid";
$rs_taxname2 = "$rs_result_tax_q2->taxname";
$rs_taxenabled2 = "$rs_result_tax_q2->taxenabled";


$groupsbelong = array();
$groupsbelong[] = "$rs_taxid2";
$rs_ql_isingroup = "SELECT * FROM taxes WHERE isgrouprate = '1'";
$rs_result1_isingroup = mysqli_query($rs_connect, $rs_ql_isingroup);
while($rs_result_q1_isingroup = mysqli_fetch_object($rs_result1_isingroup)) {
$compositerate = "$rs_result_q1_isingroup->compositerate";
$gtaxid = "$rs_result_q1_isingroup->taxid";
$subarray = unserialize($compositerate);
if (in_array($rs_taxid2,$subarray)) {
$groupsbelong[] = $gtaxid;
}
}

if ($rs_taxenabled2 != '1') {
$ycolor2="<span class=italme>";
} else {
$ycolor2="<span>";
}

$taxids = implode(',',$groupsbelong);

if(!$fiscaldate) {
$rs_find_cart_purt_purchase = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE QUARTER(date_sold) = '$qrtr' AND YEAR(date_sold) = '$year' AND sold_type = 'purchase' AND taxex IN ($taxids)";
$rs_find_cart_purt_labor = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE QUARTER(date_sold) = '$qrtr' AND YEAR(date_sold) = '$year' AND sold_type = 'labor' AND taxex IN ($taxids)";
$rs_find_cart_purt_refund = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE QUARTER(date_sold) = '$qrtr' AND YEAR(date_sold) = '$year' AND sold_type = 'refund' AND taxex IN ($taxids)";
$rs_find_cart_purt_refundlabor = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE QUARTER(date_sold) = '$qrtr' AND YEAR(date_sold) = '$year' AND sold_type = 'refundlabor' AND taxex IN ($taxids)";
} else {
$rs_find_cart_purt_purchase = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE $fiscalquarter[$qrtr] AND sold_type = 'purchase' AND taxex IN ($taxids)";
$rs_find_cart_purt_labor = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE $fiscalquarter[$qrtr] AND sold_type = 'labor' AND taxex IN ($taxids)";
$rs_find_cart_purt_refund = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE $fiscalquarter[$qrtr] AND sold_type = 'refund' AND taxex IN ($taxids)";
$rs_find_cart_purt_refundlabor = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE $fiscalquarter[$qrtr] AND sold_type = 'refundlabor' AND taxex IN ($taxids)";
}


$rs_result_purt_purchase = mysqli_query($rs_connect, $rs_find_cart_purt_purchase);
$rs_result_pur_purchase_qt = mysqli_fetch_object($rs_result_purt_purchase);
$rs_totalsales_purchase = "$rs_result_pur_purchase_qt->total";
$salestaxrate = getsalestaxrate($rs_taxid2);
$rs_totaltax_purchase = $rs_totalsales_purchase * $salestaxrate;

$rs_result_purt_labor = mysqli_query($rs_connect, $rs_find_cart_purt_labor);
$rs_result_pur_labor_qt = mysqli_fetch_object($rs_result_purt_labor);
$rs_totalsales_labor = "$rs_result_pur_labor_qt->total";
$servicetaxrate = getservicetaxrate($rs_taxid2);
$rs_totaltax_labor = $rs_totalsales_labor * $servicetaxrate;

$rs_result_purt_refund = mysqli_query($rs_connect, $rs_find_cart_purt_refund);
$rs_result_pur_refund_qt = mysqli_fetch_object($rs_result_purt_refund);
$rs_totalsales_refund = "$rs_result_pur_refund_qt->total";
$rs_totaltax_refund = $rs_totalsales_refund * $salestaxrate;

$rs_result_purt_refundlabor = mysqli_query($rs_connect, $rs_find_cart_purt_refundlabor);
$rs_result_pur_refundlabor_qt = mysqli_fetch_object($rs_result_purt_refundlabor);
$rs_totalsales_refundlabor = "$rs_result_pur_refundlabor_qt->total";
$rs_totaltax_refundlabor = $rs_totalsales_refundlabor * $servicetaxrate;



$rs_taxtotal = $rs_totaltax_purchase + $rs_totaltax_labor;
$rs_taxtotalrefund = $rs_totaltax_refund + $rs_totaltax_refundlabor;

echo "<tr><td>$ycolor".pcrtlang("Quarter")." #$qrtr $rs_taxname2 ".pcrtlang("Tax Collected").":</span> </td><td>$ycolor$money".mf("$rs_taxtotal")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Quarter")." #$qrtr $rs_taxname2 ".pcrtlang("Tax Refunded").":</span> </td><td>$ycolor$money".mf("$rs_taxtotalrefund")."</span></td></tr>";

$rs_taxtotalcolminusrefund = $rs_taxtotal - $rs_taxtotalrefund;
echo "<tr><td>$ycolor".pcrtlang("Quarter")." #$qrtr $rs_taxname2 ".pcrtlang("Tax").":</span> </td><td>$ycolor$money".mf("$rs_taxtotalcolminusrefund")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Quarter")." #$qrtr $rs_taxname2 ".pcrtlang("Tax Basis")." (".pcrtlang("purchases")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_purchase")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Quarter")." #$qrtr $rs_taxname2 ".pcrtlang("Tax Basis")." (".pcrtlang("labor")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_labor")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Quarter")." #$qrtr $rs_taxname2 ".pcrtlang("Tax Basis")." (".pcrtlang("sales refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_refund")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Quarter")." #$qrtr $rs_taxname2 ".pcrtlang("Tax Basis")." (".pcrtlang("labor refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_refundlabor")."</span></td></tr>";

$rs_totalpur = $rs_totalsales_purchase - $rs_totalsales_refund;
echo "<tr><td>$ycolor".pcrtlang("Quarter")." #$qrtr $rs_taxname2 ".pcrtlang("Tax Basis")." (".pcrtlang("purchases")."-".pcrtlang("refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totalpur")."</span></td></tr>";

$rs_totallabpur = (($rs_totalsales_purchase + $rs_totalsales_labor) - ($rs_totalsales_refund + $rs_totalsales_refundlabor));
echo "<tr><td>$ycolor".pcrtlang("Quarter")." #$qrtr $rs_taxname2 ".pcrtlang("Tax Basis")." (".pcrtlang("labor")."+".pcrtlang("purchases")."-".pcrtlang("sales refunds")."+".pcrtlang("labor refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totallabpur")."</span></td></tr>";
echo "<tr><td colspan=2>&nbsp;</td></tr>";



}
echo "<tr><td colspan=2>&nbsp;</td></tr>";
$qrtr++;

}
echo "</table>";





####month 

if(!isset($_REQUEST['printable'])) {
if(!$fiscaldate) {
echo "<br><br><span class=sizeme20>".pcrtlang("Monthly Totals for")." $year &nbsp;</span><br><br>";
} else {
echo "<br><br><span class=sizeme20>".pcrtlang("Monthly Totals for Fiscal")." $year &nbsp;</span><br><br>";
}
} else {
if(!$fiscaldate) {
echo "<br><br><span class=sizeme20>".pcrtlang("Monthly Totals for")." $year &nbsp;</span><br><br>";
} else {
echo "<br><br><span class=sizeme20>".pcrtlang("Monthly Totals for Fiscal")." $year &nbsp;</span><br><br>";
}
}


echo "<table class=\"standard lastalignright\">";

$fiscalmonth[1] = "date_sold >= '$year-$qmonth-01' AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 1 MONTH))";
$fiscalmonth[2] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 1 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 2 MONTH))";
$fiscalmonth[3] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 2 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 3 MONTH))";
$fiscalmonth[4] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 3 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 4 MONTH))";
$fiscalmonth[5] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 4 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 5 MONTH))";
$fiscalmonth[6] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 5 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 6 MONTH))";
$fiscalmonth[7] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 6 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 7 MONTH))";
$fiscalmonth[8] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 7 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 8 MONTH))";
$fiscalmonth[9] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 8 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 9 MONTH))";
$fiscalmonth[10] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 9 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 10 MONTH))";
$fiscalmonth[11] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 10 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 11 MONTH))";
$fiscalmonth[12] = "date_sold >= (DATE_ADD('$year-$qmonth-01', INTERVAL 11 MONTH)) AND date_sold < (DATE_ADD('$year-$qmonth-01', INTERVAL 12 MONTH))";

$month = 1;

while ($month < 13) {

$rs_find_cart_tax3 = "SELECT * FROM taxes WHERE isgrouprate != '1'";
$rs_result_tax3 = mysqli_query($rs_connect, $rs_find_cart_tax3);

while($rs_result_tax_q3 = mysqli_fetch_object($rs_result_tax3)) {
$rs_taxid3 = "$rs_result_tax_q3->taxid";
$rs_taxname3 = "$rs_result_tax_q3->taxname";
$rs_taxenabled3 = "$rs_result_tax_q3->taxenabled";

$groupsbelong = array();
$groupsbelong[] = "$rs_taxid3";
$rs_ql_isingroup = "SELECT * FROM taxes WHERE isgrouprate = '1'";
$rs_result1_isingroup = mysqli_query($rs_connect, $rs_ql_isingroup);
while($rs_result_q1_isingroup = mysqli_fetch_object($rs_result1_isingroup)) {
$compositerate = "$rs_result_q1_isingroup->compositerate";
$gtaxid = "$rs_result_q1_isingroup->taxid";
$subarray = unserialize($compositerate);
if (in_array($rs_taxid3,$subarray)) {
$groupsbelong[] = $gtaxid;
}
}

$taxids = implode(',',$groupsbelong);

if ($rs_taxenabled3 != '1') {
$ycolor3 = "<span class=italme>";
} else {
$ycolor3 = "<span>";
}

if(!$fiscaldate) {
$rs_find_cart_purt_purchase = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE MONTH(date_sold) = '$month' AND YEAR(date_sold) = '$year' AND sold_type = 'purchase' AND taxex IN ($taxids)";
$rs_find_cart_purt_labor = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE MONTH(date_sold) = '$month' AND YEAR(date_sold) = '$year' AND sold_type = 'labor' AND taxex IN ($taxids)";
$rs_find_cart_purt_refund = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE MONTH(date_sold) = '$month' AND YEAR(date_sold) = '$year' AND sold_type = 'refund' AND taxex IN ($taxids)";
$rs_find_cart_purt_refundlabor = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE MONTH(date_sold) = '$month' AND YEAR(date_sold) = '$year' AND sold_type = 'refundlabor' AND taxex IN ($taxids)";
} else {
$rs_find_cart_purt_purchase = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE $fiscalmonth[$month] AND taxex = '$rs_taxid3' AND sold_type = 'purchase' AND taxex IN ($taxids)";
$rs_find_cart_purt_labor = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE $fiscalmonth[$month] AND taxex = '$rs_taxid3' AND sold_type = 'labor' AND taxex IN ($taxids)";
$rs_find_cart_purt_refund = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE $fiscalmonth[$month] AND taxex = '$rs_taxid3' AND sold_type = 'refund' AND taxex IN ($taxids)";
$rs_find_cart_purt_refundlabor = "SELECT SUM(sold_price) AS total FROM `sold_items` WHERE $fiscalmonth[$month] AND taxex = '$rs_taxid3' AND sold_type = 'refundlabor' AND taxex IN ($taxids)";
}

$rs_result_purt_purchase = mysqli_query($rs_connect, $rs_find_cart_purt_purchase);
$rs_result_pur_purchase_qt = mysqli_fetch_object($rs_result_purt_purchase);
$rs_totalsales_purchase = "$rs_result_pur_purchase_qt->total";
$salestaxrate = getsalestaxrate($rs_taxid3);
$rs_totaltax_purchase = $rs_totalsales_purchase * $salestaxrate;

$rs_result_purt_labor = mysqli_query($rs_connect, $rs_find_cart_purt_labor);
$rs_result_pur_labor_qt = mysqli_fetch_object($rs_result_purt_labor);
$rs_totalsales_labor = "$rs_result_pur_labor_qt->total";
$servicetaxrate = getservicetaxrate($rs_taxid3);
$rs_totaltax_labor = $rs_totalsales_labor * $servicetaxrate;

$rs_result_purt_refund = mysqli_query($rs_connect, $rs_find_cart_purt_refund);
$rs_result_pur_refund_qt = mysqli_fetch_object($rs_result_purt_refund);
$rs_totalsales_refund = "$rs_result_pur_refund_qt->total";
$rs_totaltax_refund = $rs_totalsales_refund * $salestaxrate;

$rs_result_purt_refundlabor = mysqli_query($rs_connect, $rs_find_cart_purt_refundlabor);
$rs_result_pur_refundlabor_qt = mysqli_fetch_object($rs_result_purt_refundlabor);
$rs_totalsales_refundlabor = "$rs_result_pur_refundlabor_qt->total";
$rs_totaltax_refundlabor = $rs_totalsales_refundlabor * $servicetaxrate;

$rs_taxtotal = $rs_totaltax_purchase + $rs_totaltax_labor;
$rs_taxtotalrefund = $rs_totaltax_refund + $rs_totaltax_refundlabor;

echo "<tr><td>$ycolor".pcrtlang("Month")." #$month $rs_taxname3 ".pcrtlang("Tax Collected").":</span> </td><td>$ycolor$money".mf("$rs_taxtotal")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Month")." #$month $rs_taxname3 ".pcrtlang("Tax Refunded").":</span> </td><td>$ycolor$money".mf("$rs_taxtotalrefund")."</span></td></tr>";

$rs_taxtotalcolminusrefund = $rs_taxtotal - $rs_taxtotalrefund;


echo "<tr><td>$ycolor".pcrtlang("Month")." #$month $rs_taxname3 ".pcrtlang("Tax").":</span> </td><td>$ycolor$money".mf("$rs_taxtotalcolminusrefund")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Month")." #$month $rs_taxname3 ".pcrtlang("Tax Basis")." (".pcrtlang("purchases")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_purchase")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Month")." #$month $rs_taxname3 ".pcrtlang("Tax Basis")." (".pcrtlang("labor")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_labor")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Month")." #$month $rs_taxname3 ".pcrtlang("Tax Basis")." (".pcrtlang("sales refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_refund")."</span></td></tr>";
echo "<tr><td>$ycolor".pcrtlang("Month")." #$month $rs_taxname3 ".pcrtlang("Tax Basis")." (".pcrtlang("labor refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totalsales_refundlabor")."</span></td></tr>";

$rs_totalpur = $rs_totalsales_purchase - $rs_totalsales_refund;
echo "<tr><td>$ycolor".pcrtlang("Month")." #$month $rs_taxname3 ".pcrtlang("Tax Basis")." (".pcrtlang("purchases")."-".pcrtlang("refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totalpur")."</span></td></tr>";

$rs_totallabpur = (($rs_totalsales_purchase + $rs_totalsales_labor) - ($rs_totalsales_refund + $rs_totalsales_refundlabor));
echo "<tr><td>$ycolor".pcrtlang("Month")." #$month $rs_taxname3 ".pcrtlang("Tax Basis")." (".pcrtlang("labor")."+".pcrtlang("purchases")."-".pcrtlang("sales refunds")."+".pcrtlang("labor refunds")."):</span> </td><td>$ycolor$money".mf("$rs_totallabpur")."</span></td></tr>";
echo "<tr><td colspan=2>&nbsp;</td></tr>";




}
echo "<tr><td colspan=2>&nbsp;</td></tr>";
$month++;

}
echo "</table>";

if(!isset($_REQUEST['printable'])) {
stop_box();
}

if(!isset($_REQUEST['printable'])) {
require("footer.php");
} else {
printablefooter();
}



}



######

switch($func) {

 case "taxreport":
    taxreport();
    break;

}
