<?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 this class extends PDF and let you export the detailled printing
 *  of any ledgers
 */

/*!
 * \class Print_Ledger_Detail
 * \brief this class extends PDF and let you export the detailled printing
 *  of any ledgers
 */


class Print_Ledger_Detail extends Print_Ledger
{
    public function __construct (Database $p_cn , Acc_Ledger $ledger,$p_from,$p_to)
    {

        parent::__construct($p_cn,'P', 'mm', 'A4',$ledger,$p_from,$p_to,'all');
        
    }

    function setDossierInfo($dossier = "n/a")
    {
        $this->dossier = dossier::name()." ".$dossier;
    }

    function Header()
    {
        //Arial bold 12
        $this->SetFont('DejaVu', 'B', 12);
        //Title
        $this->Cell(0,10,$this->dossier, 'B', 0, 'C');
        //Line break
        $this->Ln(20);
    }
    function Footer()
    {
        //Position at 2 cm from bottom
        $this->SetY(-20);
        //Arial italic 8
        $this->SetFont('Arial', 'I', 8);
        //Page number
        $this->Cell(0,8,'Date '.$this->date." - Page ".$this->PageNo().'/{nb}',0,0,'L');
        // Created by NOALYSS
        $this->Cell(0,8,'Created by NOALYSS, online on https://www.noalyss.eu',0,0,'R',false,'https://www.noalyss.eu');

    }
   

    function export()
    {
        
        // detailled printing
        $rap_deb=0;
        $rap_cred=0;
        // take all operations from jrn
        $array=$this->get_ledger()->get_operation($this->get_from(),$this->get_to());

        $this->SetFont('DejaVu','BI',7);
        $this->write_cell(155,7,'report Débit',0,0,'R');
        $this->write_cell(30,7,nbm($rap_deb),0,0,'R');
        $this->line_new(4);
        $this->write_cell(155,7,'report Crédit',0,0,'R');
        $this->write_cell(30,7,nbm($rap_cred),0,0,'R');
        $this->line_new(10);

        // print all operation
        for ($i=0;$i< count($array);$i++)
        {
            $this->SetFont('DejaVuCond','B',7);
            $row=$array[$i];

            $this->write_multi(20, 4,$row['pj'],'T');
            $this->write_cell(15,4,$row['date_fmt'],'T');
            $this->write_cell(20,4,$row['internal'],'T');
            $this->write_multi(100,4,$row['comment'],'T');
            $this->write_cell(40,4,nbm($row['montant']),'T',0,'R');
            

            $this->line_new(7);
            // get the entries
            $aEntry=$this->cn->get_array("select j_id,j_poste,j_qcode,j_montant,j_debit, j_text,".
										 " case when j_text='' or j_text is null then pcm_lib else j_text end as desc,".
                                         " pcm_lib ".
                                         " from jrnx join tmp_pcmn on (j_poste=pcm_val) where j_grpt = $1".
                                         " order by j_debit desc,j_id",
                                         array($row['jr_grpt_id']));
            for ($j=0;$j<count($aEntry);$j++)
            {
                $this->SetFont('DejaVuCond','',7);
                $entry=$aEntry[$j];
                // $this->write_cell(15,6,$entry['j_id'],0,0,'R');
                $this->write_cell(32,6,$entry['j_qcode'],0,0,'R');
                $this->write_cell(23,6,$entry['j_poste'],0,0,'R');

                // if j_qcode is not empty retrieve name
                if ( $entry['j_text'] =='' && $entry['j_qcode'] != '')
                {
                    $f_id=$this->cn->get_value('select f_id from vw_poste_qcode where j_qcode=$1',array($entry['j_qcode']));
                    if ($f_id != '')
                        $name=$this->cn->get_value('select ad_value from fiche_detail where f_id=$1 and ad_id=1',
                                                   array($f_id));
                    else
                        $name=$entry['pcm_lib'];
                }
                else
                    $name=$entry['desc'];
                $this->write_cell(100,6,$name,0,0,'L');

                // print amount
                $str_amount=nbm($entry['j_montant']);
                if ( $entry['j_debit']=='t')
                {
                    $this->write_cell(20,6,$str_amount,0,0,'R');
                    $this->write_cell(20,6,'',0,0,'R');
                    
                }
                else
                {
                    $this->write_cell(20,6,'',0,0,'R');
                    $this->write_cell(20,6,$str_amount,0,0,'R');
                }
                $this->line_new(4);
            }
            $this->line_new(3);
        }
    }
}
