/*
* This file is part of PhpCompta.
*
* PhpCompta 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.
*
* PhpCompta 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 PhpCompta; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Revision$ */
// Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
require_once('class_jrn.php');
require_once('user_form_ven.php');
require_once('jrn.php');
require_once("class_document.php");
require_once("class_fiche.php");
require_once("check_priv.php");
/*!\file
* \brief the purpose off this file is to create invoices, to record them and to generate
* them, and of course to save them into the database
*
*/
// First we show the menu
// If nothing is asked the propose a blank form
// to enter a new invoice
if ( ! isset ($_REQUEST['p_jrn'])) {
// no journal are selected so we select the first one
$p_jrn=GetFirstJrnIdForJrnType($_SESSION['g_dossier'],'VEN');
} else
{
$p_jrn=$_REQUEST['p_jrn'];
}
// for the back button
$retour="";
$h_url="";
if ( isset ($_REQUEST['url']))
{
$retour=sprintf('',urldecode($_REQUEST['url']));
$h_url=sprintf('',urldecode($_REQUEST['url']));
}
$sub_action=(isset($_REQUEST['sa']))?$_REQUEST['sa']:"";
//-----------------------------------------------------
// If a list of invoice is asked
//
if ( $sub_action == "list")
{
// Check privilege
if ( CheckJrn($_SESSION['g_dossier'],$_SESSION['g_user'],$p_jrn) < 1 ) {
NoAccess();
exit -1;
}
// show the menu with the list item selected
echo '
';
// Ask to update payment
if ( isset ( $_GET['paid']))
{
// reset all the paid flag because the checkbox is post only
// when checked
foreach ($_GET as $name=>$paid)
{
list($ad) = sscanf($name,"set_jr_id%d");
if ( $ad == null ) continue;
$sql="update jrn set jr_rapt='' where jr_id=$ad";
$Res=ExecSql($cn,$sql);
}
// set a paid flag for the checked box
foreach ($_GET as $name=>$paid)
{
list ($id) = sscanf ($name,"rd_paid%d");
if ( $id == null ) continue;
// echo "Mise à jour $id";
$paid=($paid=='on')?'paid':'';
$sql="update jrn set jr_rapt='$paid' where jr_id=$id";
$Res=ExecSql($cn,$sql);
}
}
echo $retour;
echo '
';
//-----------------------------------------------------
// if we request to add an item
// the $_POST['add_item'] is set
// or if we ask to correct the invoice
if ( isset ($_POST['add_item']) || isset ($_POST["correct_new_invoice"]) )
{
if ( CheckJrn($_SESSION['g_dossier'],$_SESSION['g_user'],$p_jrn) != 2 ) {
NoAccess();
exit -1;
}
$nb_item=$_POST['nb_item'];
if ( isset ($_POST['add_item']))
$nb_item++;
$form=FormVenInput($cn,$p_jrn,$User->GetPeriode(),$_POST,false,$nb_item);
echo '
';
echo $form;
echo '
';
exit();
}
//-----------------------------------------------------
// we want to save the invoice and to generate a invoice
//
if ( isset($_POST['record_and_print_invoice']))
{
if ( CheckJrn($_SESSION['g_dossier'],$_SESSION['g_user'],$p_jrn) != 2 ) {
NoAccess();
exit -1;
}
// First we save the invoice, the internal code will be used to change the description
// and upload the file
list ($internal,$e)=RecordInvoice($cn,$_POST,$User,$p_jrn);
$form=FormVenteView($cn,$p_jrn,$User->GetPeriode(),$_POST,$_POST['nb_item'],'noform','');
echo '
';
echo '
Opération '.$internal.' enregistré
';
echo $form;
echo '';
// Show the details of the encoded invoice
// and the url of the invoice
if ( isset($_POST['gen_invoice']))
{
$doc=new Document($cn);
$doc->f_id=$_POST['e_client'];
$doc->md_id=$_POST['gen_doc'];
$doc->ag_id=0;
$str_file=$doc->Generate();
// Move the document to the jrn
$doc->MoveDocumentPj($internal);
// Update the comment with invoice number
$sql="update jrn set jr_comment='Facture ".$doc->d_number."' where jr_internal='$internal'";
ExecSql($cn,$sql);
echo $str_file;
}
echo '';
exit();
}
//-----------------------------------------------------
// we show the confirmation screen it is proposed here to generate the
// invoice
if ( isset ($_POST['view_invoice']) )
{
// Check privilege
if ( CheckJrn($_SESSION['g_dossier'],$_SESSION['g_user'],$p_jrn) < 1 ) {
NoAccess();
exit -1;
}
$nb_number=$_POST["nb_item"];
if ( form_verify_input($cn,$p_jrn,$User->GetPeriode(),$HTTP_POST_VARS,$nb_number) == true)
{
$form=FormVenteView($cn,$p_jrn,$User->GetPeriode(),$HTTP_POST_VARS,$nb_number);
} else {
// Check failed : invalid date or quantity
echo_error("Cannot validate ");
$form=FormVenInput($cn,$p_jrn,$User->GetPeriode(),$HTTP_POST_VARS,false,$nb_number);
}
echo '
';
echo $form;
echo '
';
exit();
}
//-----------------------------------------------------
// By default we add a new invoice
if ( $p_jrn != -1 )
{
if ( CheckJrn($_SESSION['g_dossier'],$_SESSION['g_user'],$p_jrn) != 2 ) {
exit -1;
}
$jrn=new jrn($cn, $p_jrn);
echo_debug('facture.inc.php.php',__LINE__,"Blank form");
// Show an empty form of invoice
$form=FormVenInput($cn,$p_jrn,$User->GetPeriode(),null,false,$jrn->GetDefLine());
echo '