<?php
/* Copyright (C) 2007-2008 Jeremie Ollivier    <jeremie.o@laposte.net>
 * Copyright (C) 2011      Laurent Destailleur <eldy@users.sourceforge.net>
 * Copyright (C) 2012      Marcos García       <marcosgdf@gmail.com>
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
 */

// Protection to avoid direct call of template
if (empty($langs) || ! is_object($langs))
{
	print "Error, template page can't be called as URL";
	exit;
}


include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
include_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';

$langs->load("main");
$langs->load('cashdesk');

top_httphead('text/html');

$facid=GETPOST('facid','int');

$object=new Facture($db);
$object->fetch($facid);

//on verifie si le client a tout payé
$sqlastfact = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture ORDER BY rowid DESC LIMIT 1";
$reslast = $db->query($sqlastfact);
        $idlastfact = $reslast->fetch_row();
        
$sqlfac = "SELECT f.total as total, pf.amount as amount, f.paye as paye FROM ".MAIN_DB_PREFIX."facture f"
          ." LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON pf.fk_facture = f.rowid"
        . " WHERE f.rowid=".$idlastfact[0];

        $result = $db->query($sqlfac);
        $factmontant = $result->fetch_row();
        
if (!empty($_SESSION["CASHDESK_ID_WAREHOUSE"]) && ! empty($conf->stock->enabled))
{
        $soc = new Societe($db);
        $sql = "SELECT nom FROM ".MAIN_DB_PREFIX."societe WHERE `rowid` =". $_SESSION["CASHDESK_ID_THIRDPARTY"];
        $result = $db->query($sql);
        $nomclient = $result->fetch_row();
}
?>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="<?php echo DOL_URL_ROOT;?>/cashdesk/css/ticket.css">
       
    </head>

<body>

<div class="entete">
    <div class="logo">
        <?php print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;file='.urlencode('/thumbs/'.$mysoc->logo_small).'">'; ?>

        <?php print '<img class="nam" src="http://localhost/novesio/doc/images/nam.jpg">'; ?>
    </div>
    <div class="infos">
        <p class="address"><?php echo $mysoc->name; ?><br>
        <?php print dol_nl2br(dol_format_address($mysoc)); ?><br>
        </p>

        <p class="date_heure"><?php
        // Recuperation et affichage de la date et de l'heure
        $now = dol_now();
        print dol_print_date($now,'dayhourtext').'<br>';
        print $object->ref;
        ?></p>
    </div>
</div>

    <span class="client">Nom du client: <?php echo strtoupper ($nomclient[0])?></span>
<table >
	<tr class="titres">
            <th><?php print $langs->trans("Code"); ?></th>
            <th><?php print $langs->trans("Label"); ?></th>
            <th><?php print $langs->trans("Qty"); ?></th>
            <th><?php print "prix unitaire"; ?></th>
            <th><?php print $langs->trans("TotalHT"); ?></th>
	</tr>
    <tbody>
    <?php

    $tab=array();
    $tab = $_SESSION['poscart'];

    $tab_size=count($tab);
    for($i=0;$i < $tab_size;$i++)
    {
        $remise = $tab[$i]['remise'];
    ?>
    <tr>
        <td><?php echo $tab[$i]['ref'];?></td>
        <td><?php echo $tab[$i]['label'];?></td>
        <td><?php echo $tab[$i]['qte'];?></td>
        <td class="totalleft"><?php echo $tab[$i]['price']." Fcfa";?></td>
        <td class="totalleft"><?php echo $tab[$i]['total_ht']." Fcfa";?></td>
    </tr>
    <?php
    }
    ?>
    </tbody>
</table>

<table class="sommestotal">
    <tr style="margin-left: 95px;">
        <th class="nowrap"><?php echo "Total";?></th>
        <td class="nowrap totalleft"><?php echo price(price2num($obj_facturation->prixTotalHt(),'MT'),'',$langs,0,0,0,"F cfa")."\n";?></td>
    </tr>
    
    <?php if($factmontant[2]==0) : ?>
        <tr>
            <th class="nowrap"> Montant versé </th> 
            <td class="nowrap totalleft"> <?php echo price(price2num($factmontant[1],'MT'),'',$langs,0,0,0,"F cfa");?> </td>
        </tr>
        <tr>
            <th class="nowrap"> Montant restant</th>
            <td class="nowrap  totalleft couleur"> <?php echo price(price2num($factmontant[0]-$factmontant[1],'MT'),'',$langs,0,0,0,"F cfa");?> </td>
        </tr>
    <?php endif ?>
</table>
    <footer>
         <?php print '<img class="footer-nam" src="'.DOL_DOCUMENT_ROOT.'/doc/images/novesio_footer.jpg">'; ?>
    </footer>
<script type="text/javascript">
    window.print();
</script>
</body>
</html>
