<?php
session_start();
require_once "main.php";
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
//require_once('tcpdf/config/lang/eng.php');
//ob_start();
require_once('tcpdf/tcpdf.php');
Logger::configure('log4php.xml');
$log = Logger::getLogger('balances');
ob_start();
$htm = '';
$totc = 0;
$tot3 = 0;
$tot6 = 0;
$tot = 0;
if ($_GET['submitType']=='posrec') {
$result = bill_get_cust_cred($_SESSION['comnum']);
$today = date("m.d.y");
ob_start();
$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum']);
$pdf = new TCPDF("P", "pt", "USLETTER", true, 'UTF-8', false);
$pdf->SetAutoPageBreak("true");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('WtrBill');
$pdf->SetTitle('Accounts with Credit Balances');
$pdf->SetSubject('credits');
$pdf->SetKeywords('WtrBill');
$style = array(
	'position' => '',
	'align' => 'L',
	'stretch' => false,
	'fitwidth' => false,
	'cellfitalign' => '',
	'border' => false,
	'hpadding' => 'auto',
	'vpadding' => 'auto',
	'fgcolor' => array(0,0,0),
	'bgcolor' => false, //array(255,255,255),
	'text' => false,
	'font' => 'helvetica',
	'fontsize' => 10,
	'stretchtext' => 4
);
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 6);
$resolution= array(792,612);//array(792, 612);
$pdf->AddPage("P", $resolution);
$rep1 = '<center><h2>Accounts Receivable with credit balances</h2></center>
    <center><h3>'.$today.'</h3></center>
    <table cellpadding="3" cellspacing="1" style="text-align:left;"><tr>
       <td>Account</td><td>Account Name</td><td>Service Address</td><td style="text-align:right;">Balance</td></tr>';
for ($i=0;$i<count($result);$i++) {       
$htm = $htm.'<tr><td>'. testx($result[$i]['account']).'</td>
            <td>'. testx($result[$i]['name']).'</td>
            <td>'. testx($result[$i]['maddr']).'</td>
            <td style="text-align:right;">$'. number_format(testx($result[$i]['abalance']),2).'</td>
        </tr>';
$tot = $tot + testx($result[$i]['abalance']);
}
$htm1 = '<tr>
        </tr>
        <tr>
            <td></td>
            <td>'.count($result).' Accounts</td>
            <td></td>
            <td style="text-align:right;">$'.number_format($tot,2).'</td>
        </tr></table>';

        $pdf->SetFont('helvetica', '', 10);
        $pdf->writeHTML($rep1.$htm.$htm1, true, false, true, false, '');
        //$pdf->writeHTML($rep2, true, false, true, false, '');
        //$pdf->writeHTMLcell(0,0,5,5,$rep1, false, true, false, false, false, '');//address
        $pdf->lastPage();
        ob_end_clean();
$pdf->Output('Creditbalance.pdf', 'I');
ob_end_flush();
} else if ($_GET['submitType']=='aging') {
$result = bill_get_cust_aging($_SESSION['comnum']);
$today = date("m.d.y");
$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum']);
$pdf = new TCPDF("P", "pt", "USLETTER", true, 'UTF-8', false);
$pdf->SetAutoPageBreak("true");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('WtrBill');
$pdf->SetTitle('Account Report');
$pdf->SetSubject('Aging');
$pdf->SetKeywords('WtrBill');
$style = array(
	'position' => '',
	'align' => 'L',
	'stretch' => false,
	'fitwidth' => false,
	'cellfitalign' => '',
	'border' => false,
	'hpadding' => 'auto',
	'vpadding' => 'auto',
	'fgcolor' => array(0,0,0),
	'bgcolor' => false, //array(255,255,255),
	'text' => false,
	'font' => 'helvetica',
	'fontsize' => 10,
	'stretchtext' => 4
);
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 6);
$resolution= array(792,612);//array(792, 612);
$pdf->AddPage("P", $resolution);
$b=0;
$k=0;
$td = idate('U',time());
//echo $td;
for ($j=0;$j<count($result);$j++){
      if (!($k==$result[$j]['account'])) {
          $k=$result[$j]['account'];
          $cbal = 0;
          $lbal = 0;
          $rlbal = 0;
          $b++;
          $fnx = 0;
      }
      //print_r($result[$j]);
      //echo '<Br />';
      if ($result[$j]['last']<>1) {
          $d = idate('U',strtotime($result[$j]['date']));
          //echo $d . ' -- ' . $td . '<br /r>';
          if ($d>($td-2592000)) {
              $cbal = $cbal + $result[$j]['bamount'];   
          } else if ($d>($td-5184000)) {
              $lbal = $lbal + $result[$j]['bamount'];
          } else if ($d>($td-7776000)) {
              $rlbal = $rlbal + $result[$j]['bamount'];
          }
      } else {
          if ($fnx==0) {
              $d = idate('U',strtotime($result[$j]['date']));
                //echo $d . ' -x- ' . $td . '<br /r>';
                if ($d>($td-2592000)) {
                    $cbal = $cbal + $result[$j]['bamount'] + $result[$j]['xbal'];
                } else if ($d>($td-5184000)) {
                    $lbal = $lbal + $result[$j]['bamount'] + $result[$j]['xbal'];
                } else if ($d>($td-7776000)) {
                    $rlbal = $rlbal + $result[$j]['bamount'] + $result[$j]['xbal'];
                }
               //$cbal = $cbal + $result[$j]['bamount'] + $result[$j]['x'];
               $fnx = 1;
          }
      }
      $res1[$b]['id'] = $k;
      $res1[$b]['name'] = $result[$j]['name'];
      $res1[$b]['account'] = $result[$j]['account'];
      $res1[$b]['cbal'] = $cbal;
      $res1[$b]['lbal'] = $lbal;
      $res1[$b]['rlbal'] = $rlbal;
      $res1[$b]['abal'] = $result[$j]['abalance'];
}
for ($b=0;$b<count($res1);$b++){
    $res2[$b]['id'] = $res1[$b+1]['id'];
    $res2[$b]['name'] = $res1[$b+1]['name'];
    $res2[$b]['account'] = $res1[$b+1]['account'];
    $res2[$b]['cbal'] = $res1[$b+1]['cbal'];
    $res2[$b]['lbal'] = $res1[$b+1]['lbal'];
    $res2[$b]['rlbal'] = $res1[$b+1]['rlbal'];
    $res2[$b]['abal'] = $res1[$b+1]['abal'];
}

$htm = '';
$rep1 = '<center><h2>Accounts Receivable Aging</h2></center>
    <center><h3>'.$today.'</h3></center>
    <table cellpadding="1" cellspacing="1" style="text-align:left;">
    <tr>
       <td><b>Account</b></td><td><b>Account Name</b></td><td style="text-align:right;"><b>Current</b></td><td style="text-align:right;"><b>30-59 Days</b></td><td style="text-align:right;"><b>60-89 Days</b></td><td style="text-align:right;"><b>Total</b></td></tr>';

for ($i=0;$i<count($res2);$i++) {  
$htm = $htm . '<tr><td>'. testx($res2[$i]['account']).'</td>
            <td>'. testx($res2[$i]['name']).'</td>
            <td style="text-align:right;">$'. number_format(floatval(testx($res2[$i]['cbal'])),2).'</td>
            <td style="text-align:right;">$'. number_format(floatval(testx($res2[$i]['lbal'])),2).'</td>
            <td style="text-align:right;">$'. number_format(floatval(testx($res2[$i]['rlbal'])),2).'</td>
            <td style="text-align:right;">$'. number_format(floatval(testx($res2[$i]['abal'])),2).'</td>
        </tr>';
$totc = $totc + floatval(testx($res2[$i]['cbal']));
$tot3 = $tot3 + floatval(testx($res2[$i]['lbal']));
$tot6 = $tot6 + floatval(testx($res2[$i]['rlbal']));
$tot = $tot + floatval(testx($res2[$i]['abal']));
} 
$htm1 = '<tr>
            <td><b>'.count($res2).' Accounts</b></td>
            <td></td>
            <td style="border-top: 1px solid black; text-align:right;"><b>$'.number_format(@$totc,2).'</b></td>
            <td style="border-top: 1px solid black; text-align:right;"><b>$'.number_format(@$tot3,2).'</b></td>
            <td style="border-top: 1px solid black; text-align:right;"><b>$'.number_format(@$tot6,2).'</b></td>
            <td style="border-top: 1px solid black; text-align:right;"><b>$'.number_format(@$tot,2).'</b></td>
        </tr></table>';

        $pdf->SetFont('helvetica', '', 10);
        $pdf->writeHTML($rep1.$htm.$htm1, true, false, true, false, '');
        //$pdf->writeHTML($rep2, true, false, true, false, '');
        //$pdf->writeHTMLcell(0,0,5,5,$rep1, false, true, false, false, false, '');//address
        $pdf->lastPage();
        //ob_end_clean();
$pdf->Output('AgingReport.pdf', 'I');
ob_end_flush();
} else if ($_GET['submitType']=='daily') {
$today = date('Y-m-d 00:00:00', strtotime($_GET['reportdate']));
$today1 = date('Y-m-d 23:59:59', strtotime($_GET['reportdate']));
$result = bill_get_receipts($_SESSION['comnum'], $today, $today1);
$zresult = bill_get_ptypes($_SESSION['comnum']);
$today = $_GET['reportdate'];
ob_end_clean();
$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum']);
$pdf = new TCPDF("P", "pt", "USLETTER", true, 'UTF-8', false);
$pdf->SetAutoPageBreak("true");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('WtrBill');
$pdf->SetTitle('Daily Reciepts');
$pdf->SetSubject('reciepts');
$pdf->SetKeywords('WtrBill');
$style = array(
	'position' => '',
	'align' => 'L',
	'stretch' => false,
	'fitwidth' => false,
	'cellfitalign' => '',
	'border' => false,
	'hpadding' => 'auto',
	'vpadding' => 'auto',
	'fgcolor' => array(0,0,0),
	'bgcolor' => false, //array(255,255,255),
	'text' => false,
	'font' => 'helvetica',
	'fontsize' => 10,
	'stretchtext' => 4
);
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 6);
$resolution= array(792,612);//array(792, 612);
$pdf->AddPage("P", $resolution);
$rep1 = '<center><h2>Daily Reciepts</h2></center>
    <center><h3>'.$today.'</h3></center>
    <table cellpadding="1" cellspacing="1"><tr>
       <th style="width=7%">Account</th><th style="width:24%">Name</th><th style="width=7%">Payment Type</th><th style="width=7%">Reference</th><th style="text-align:right">Amount</th></tr>';
$type1 = '';
$tzpe2 = 0;
$tot1 = 0;
$tot2 = 0;
$tot3 = 0;
$tot4 = 0;
$tot5 = 0;
$tot6 = 0;
$tot7 = 0;
$type3 = 0;
$cnt1 = 0;
$cnt2 = 0;
$cnt3 = 0;
$cnt4 = 0;
$cnt5 = 0;
$cnt6 = 0;

for ($i=0;$i<count($result);$i++) {
    $htm = $htm.'<tr><td>'. testx($result[$i]['masterid']).'</td>
            <td>'. testx($result[$i]['5']).'</td>
            <td>'. testx($result[$i]['name']).'</td>
            <td>'. testx($result[$i]['ref1']).'</td>
            <td style="text-align:right;">$'. testx($result[$i]['pamount']).'</td>
        </tr>';
            $tot = $tot + testx($result[$i]['pamount']);
}
$htm1 = '<tr>
            <td></td>
            <td>'.count($result).'</td>
            <td>Accounts</td>
            <td></td>
            <td style="text-align:right;">$'.number_format($tot,2).'</td>
        </tr></table>';
        $pdf->SetFont('helvetica', '', 10);
        $pdf->writeHTML($rep1.$htm.$htm1, true, false, true, false, '');
        //$pdf->writeHTML($rep2, true, false, true, false, '');
        //$pdf->writeHTMLcell(0,0,5,5,$rep1, false, true, false, false, false, '');//address
        $pdf->lastPage();
$pdf->Output('dailyreciepts.pdf', 'I');
}
?>

