<?php
/*
 *   This file is part of NOALYSS.
 *
 *   NOALYSS is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   NOALYSS is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with NOALYSS; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// Copyright Author Dany De Bontridder danydb@aevalys.eu

/*!\file
 * \brief Called by impress->category, export in PDF the history of a category or balance
 * of card
 */
if ( ! defined ('ALLOWED') ) die('Appel direct ne sont pas permis');
// Security we check if user does exist and his privilege
require_once NOALYSS_INCLUDE.'/lib/ac_common.php';

/* Security */
$gDossier=dossier::id();
$cn=Dossier::connect();
$g_user->Check();
$g_user->check_dossier($gDossier);

$http=new HttpInput();

$cat = $http->get("cat");
$histo = $http->get("histo");

$name=$cn->get_value('select fd_label from fiche_def where fd_id=$1',array($cat));

$pdf=new PDF($cn);
$pdf->setDossierInfo("  Periode : ".$http->get('start')." - ".$http->get('end'));
$pdf->AliasNbPages();
$pdf->AddPage();

$pdf->SetTitle($name,1);
$pdf->SetAuthor('NOALYSS');
/* balance */
$allcard=(isset($_GET['allcard']))?1:0;
/*
 * Balance
 */
if ($histo == 4 || $histo==5)
{
    $fd=new Fiche_Def($cn,$http->request('cat'));
    if ($allcard==1 &&  $fd->hasAttribute(ATTR_DEF_ACCOUNT) == false )
    {
        $pdf->write_cell(0,10, _("Cette catégorie n'a pas de poste comptable"));
        //Save PDF to file
        $fDate=date('dmy-Hi');
        $pdf->Output("category-$fDate.pdf", 'D');
        exit;
    }
	// all card
	if ($allcard==1)
	{
		$afiche=$cn->get_array("select fd_id from vw_fiche_def where ad_id=".ATTR_DEF_ACCOUNT." order by fd_label asc");
	}
	else
	{
		$afiche[0]=array('fd_id'=>$http->request('cat'));
	}

	if ( $allcard==0 && empty($afiche))
    {
        $pdf->write_cell(0,10, "Aucune fiche trouvée");//Save PDF to file
        $fDate=date('dmy-Hi');
        $pdf->Output("category-$fDate.pdf", 'D');
        exit;
    }
	for ($e = 0; $e < count($afiche); $e++)
	{
    $aCard=$cn->get_array("select f_id,ad_value from fiche join fiche_Detail using (f_id)  where ad_id=1 and fd_id=$1 order by 2 ",array($afiche[$e]['fd_id']));
	$name=$cn->get_value('select fd_label from fiche_def where fd_id=$1',array($afiche[$e]['fd_id']));
	$pdf->SetFont('DejaVu','BI',14);
	$pdf->write_cell(0,8,$name,0,1,'C');
    $pdf->line_new();
	$pdf->SetFont('DejaVuCond','',7);
    $pdf->write_multi(30,3,'Quick Code',0,'L',0);
    $pdf->write_multi(80,3,'Libellé',0,'L',0);
    $pdf->write_cell(20,7,'Débit',0,0,'R',0);
    $pdf->write_cell(20,7,'Crédit',0,0,'R',0);
    $pdf->write_cell(20,7,'Solde',0,0,'R',0);
    $pdf->write_cell(20,7,'D/C',0,0,'C',0);
    $pdf->line_new();

	if (empty($aCard)) continue;


    $idx=0;$sum_deb=0;$sum_cred=0;bcscale(4);
    for ($i=0;$i < count($aCard);$i++)
    {
        if ( isDate($http->request('start')) == null || isDate ($http->request('end')) == null ) 	 exit;
        $filter= " (j_date >= to_date('".$http->request('start')."','DD.MM.YYYY') ".
                 " and  j_date <= to_date('".$http->request('end')."','DD.MM.YYYY')) ";
        $oCard=new Fiche($cn,$aCard[$i]['f_id']);
        $solde=$oCard->get_solde_detail($filter);
        if ( $solde['debit'] == 0 && $solde['credit']==0) continue;
	/* only not purged card */
	if ($histo == 5 && $solde['debit'] == $solde['credit']) continue;
    $fill=$pdf->is_fill($idx);
    $idx++;
	$side='';
	if(bcsub($solde['credit'],$solde['debit']) < 0) $side='Deb.';
	if(bcsub($solde['credit'],$solde['debit']) > 0) $side='Cred.';

        $sum_cred=bcadd($sum_cred,$solde['credit']);
        $sum_deb=bcadd($sum_deb,$solde['debit']);
        $sum_solde=bcsub($sum_deb,$sum_cred);


        $pdf->write_cell(30,7,$oCard->get_attribute(ATTR_DEF_QUICKCODE),0,0,'L',$fill);
        $pdf->LongLine(80,7,$oCard->get_attribute(ATTR_DEF_NAME)." (".$oCard->get_attribute(ATTR_DEF_ACCOUNT).")",0,'L',$fill);

        $pdf->write_cell(20,7,nbm($solde['debit']),0,0,'R',$fill);
        $pdf->write_cell(20,7,nbm($solde['credit']),0,0,'R',$fill);
        $pdf->write_cell(20,7,nbm(abs($solde['solde'])),0,0,'R',$fill);
        $pdf->write_cell(20,7,$side,0,0,'C',$fill);
        $pdf->line_new();
        }
        $fill=$pdf->is_fill($idx);

		$idx++;
        // Sum by category
        $pdf->write_cell(30,7,"",0,0,'L',$fill);
        $pdf->write_cell(80,7,_("Totaux"),0,0,'L',$fill);
        $pdf->write_cell(20,7,nbm($sum_deb),0,0,'R',$fill);
        $pdf->write_cell(20,7,nbm($sum_cred),0,0,'R',$fill);
        $pdf->write_cell(20,7,nbm(abs($sum_solde)),0,0,'R',$fill);
        $side=" = ";
        if ( $sum_solde > 0 )
        {
            $side='Deb.';
        }
        else if ( $sum_solde < 0)
        {
            $side='Cred.';
        }

        $pdf->write_cell(20,7,$side,0,0,'C',$fill);
        $pdf->line_new();
	}
}
else
{
	// History
	// all card
	if ($allcard == 1)
	{
		$afiche = $cn->get_array("select fd_id from vw_fiche_def where ad_id=" . ATTR_DEF_ACCOUNT . " order by fd_label asc");
	}
	else
	{
		$afiche[0] = array('fd_id' => $http->request('cat'));
	}
	$fic=new Fiche($cn);
	for ($e = 0; $e < count($afiche); $e++)
	{
		$array = Fiche::get_fiche_def($cn,$afiche[$e]['fd_id'] , 'name_asc');
		/*
		 * You show now the result
		 */
		if ($array == null)
		{
			continue;
		}
		$tab = array(12, 20, 20,68, 20, 20, 10, 20);
		$align = array('L', 'C', 'L', 'L', 'R','R', 'R', 'R');

		foreach ($array as $row_fiche)
		{
			$fic = new Fiche($cn, $row_fiche['f_id']);
			$letter = new Lettering_Card($cn);
			$letter->set_parameter('quick_code', $fic->get_attribute(ATTR_DEF_QUICKCODE));
			$letter->set_parameter('start',$http->request('start'));
			$letter->set_parameter('end',$http->request('end'));
			// all
			if ($histo == 0)
			{
				$letter->get_all();
			}

			// lettered
			if ($histo == 1)
			{
				$letter->get_letter();
			}
			// unlettered
			if ($histo == 2)
			{
				$letter->get_unletter();
			}
			if ($histo == 6)
			{
				$letter->get_letter_diff();
			}
			/* skip if nothing to display */
			if (count($letter->content) == 0)
				continue;
			$pdf->SetFont('DejaVuCond', '', 10);
			$fiche = new Fiche($cn, $row_fiche['f_id']);
			$pdf->write_cell(0, 7, $fiche->get_attribute(ATTR_DEF_NAME)." [".$fiche->get_attribute(ATTR_DEF_QUICKCODE).":".$fiche->get_attribute(ATTR_DEF_ACCOUNT)."]", 1, 'C');

			$pdf->SetFont('DejaVuCond', '', 7);
			$pdf->line_new();
			$pdf->write_cell($tab[0], 7, 'Date');
			$pdf->write_cell($tab[1], 7, 'ref');
			$pdf->write_cell($tab[2], 7, 'Internal');
			$pdf->write_cell($tab[3], 7, 'Comm');
			$pdf->write_cell($tab[4], 7, 'Montant', 0, 0, 'C');
			$pdf->write_cell($tab[5], 7, 'Prog.', 0, 0, 'R');
			$pdf->write_cell($tab[6], 7, 'Let.', 0, 0, 'R');
			$pdf->write_cell($tab[7], 7, 'Diff. Let.', 0, 0, 'R');
			$pdf->line_new();

			$amount_deb = 0;
			$amount_cred = 0;
			$prog=0;
			for ($i = 0; $i < count($letter->content); $i++)
			{
                $fill=$pdf->is_fill($i);
				$pdf->SetFont('DejaVuCond', '', 7);
				$row = $letter->content[$i];
				$str_date = shrink_date($row['j_date_fmt']);

				$pdf->write_multi($tab[0], 3, $str_date, 0, $align[0], $fill);
				$pdf->write_cell($tab[1], 4, $row['jr_pj_number'], 0, 0, $align[1], $fill);
				$pdf->write_multi($tab[2], 3, $row['jr_internal'], 0, $align[1], $fill);
				$pdf->write_multi($tab[3], 3, $row['jr_comment'], 0,  $align[2], $fill);
				if ($row['j_debit'] == 't')
				{
					$prog=bcadd($prog,$row['j_montant']);
					$pdf->write_cell($tab[4], 4, sprintf('%s D', nbm($row['j_montant'])), 0, 0, $align[4], $fill);
					$amount_deb+=$row['j_montant'];
					$str_prog=sprintf("%s %s",nbm(abs($prog)),$fic->get_amount_side($prog));
					$pdf->write_cell($tab[5], 4, $str_prog, 0, 0, $align[5], $fill);
				}
				else
				{
					$prog=bcsub($prog,$row['j_montant']);
					$pdf->write_cell($tab[4], 4, sprintf('%s C', nbm($row['j_montant'])), 0, 0, $align[4], $fill);
					$amount_cred+=$row['j_montant'];
					$str_prog=sprintf("%s %s",nbm(abs($prog)),$fic->get_amount_side($prog));
					$pdf->write_cell($tab[5], 4, $str_prog, 0, 0, $align[5], $fill);
				}
				if ($row['letter'] != -1)
				{
					$pdf->write_cell($tab[6], 4, strtoupper(base_convert($row['letter'],10,36)), 0, 0, $align[6], $fill);
					// get sum for this lettering

					$pdf->write_cell($tab[7], 4, sprintf('%s', nbm($row['letter_diff'])), '0', '0', $align[7], $fill);
				}
				else
					$pdf->write_cell($tab[6], 4, "", 0, 0, 'R', $fill);
				$pdf->line_new();
			}
			$pdf->fill_row(1);
			$pdf->SetFont('DejaVuCond', 'B', 8);
			$debit = sprintf('Debit  : %s', nbm($amount_deb));
			$credit = sprintf('Credit : %s', nbm($amount_cred));
			if ($amount_deb > $amount_cred)
				$s = 'solde débiteur';
			else
				$s = 'solde crediteur';
			$solde = sprintf('%s  : %s', $s, nbm(abs(round($amount_cred - $amount_deb, 2))));

			$pdf->write_cell(0, 6, $debit, 0, 0, 'R');
			$pdf->line_new(4);
			$pdf->write_cell(0, 6, $credit, 0, 0, 'R');
			$pdf->line_new(4);
			$pdf->write_cell(0, 6, $solde, 0, 0, 'R');
			$pdf->line_new(4);

			$pdf->line_new();
		}
	}
}
//Save PDF to file
$fDate=date('dmy-Hi');
$pdf->Output("category-$fDate.pdf", 'D');
exit;
