<?php
session_start();
require_once "main.php";
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
Logger::configure('log4php.xml');
$log = Logger::getLogger('balances');
if ($_GET['submitType']=='posrec') {
$result = bill_get_cust_cred($_SESSION['comnum']);
$today = date("m.d.y");
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('ProTekBill');
$pdf->SetTitle('Account Report');
$pdf->SetSubject('credits');
$pdf->SetKeywords('ProTekBill');
$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();
$pdf->Output('Creditbal.pdf', 'I');
} else if ($_GET['submitType']=='aging') {
$result = bill_get_cust_aging($_SESSION['comnum']);
$today = date("m.d.y");
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('ProTekBill');
$pdf->SetTitle('Account Report');
$pdf->SetSubject('Aging');
$pdf->SetKeywords('ProTekBill');
$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('z',time());
//echo $td;
for ($j=0;$j<count($result);$j++){
      if (!($k==$result[$j]['cus_id'])) {
          $k=$result[$j]['cus_id'];
          $cbal = 0;
          $lbal = 0;
          $rlbal = 0;
          $b++;
          $fnx = 0;
      }
      //print_r($result[$j]);
      //echo '<Br />';
      if ($result[$j]['last']<>1) {
          $d = idate('z',strtotime($result[$j]['date']));
          //echo $d . ' -- ' . $td . '<br /r>';
          if ($d>($td-30)) {
              $cbal = $cbal + $result[$j]['bamount'];   
          } else if ($d>($td-60)) {
              $lbal = $lbal + $result[$j]['bamount'];
          } else if ($d>($td-90)) {
              $rlbal = $rlbal + $result[$j]['bamount'];
          }
      } else {
          if ($fnx==0) {
              $d = idate('z',strtotime($result[$j]['date']));
                //echo $d . ' -x- ' . $td . '<br /r>';
                if ($d>($td-30)) {
                    $cbal = $cbal + $result[$j]['bamount'] + $result[$j]['xbal']; 
                } else if ($d>($td-60)) {
                    $lbal = $lbal + $result[$j]['bamount'] + $result[$j]['xbal'];
                } else if ($d>($td-90)) {
                    $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 Days</b></td><td style="text-align:right;"><b>60+ 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(testx($res2[$i]['cbal']),2).'</td>
            <td style="text-align:right;">$'. number_format(testx($res2[$i]['lbal']),2).'</td>
            <td style="text-align:right;">$'. number_format(testx($res2[$i]['rlbal']),2).'</td>
            <td style="text-align:right;">$'. number_format(testx($res2[$i]['abal']),2).'</td>
        </tr>';
$totc = $totc + testx($res2[$i]['cbal']);
$tot3 = $tot3 + testx($res2[$i]['lbal']);
$tot6 = $tot6 + testx($res2[$i]['rlbal']);
$tot = $tot + 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();
$pdf->Output('Creditbal.pdf', 'I'); 
print_r($rep1.$htm.$htm1);
}

?>

