dolibarr/htdocs/cashdesk/validation_verif.php
2009-09-27 21:26:36 +00:00

544 lines
12 KiB
PHP
Raw Blame History

<?php
/* Copyright (C) 2007-2008 Jeremie Ollivier <jeremie.o@laposte.net>
* Copyright (C) 2008-2009 Laurent Destailleur <eldy@uers.sourceforge.net>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
require ('../master.inc.php');
require (DOL_DOCUMENT_ROOT.'/cashdesk/include/environnement.php');
require (DOL_DOCUMENT_ROOT.'/cashdesk/classes/Facturation.class.php');
require (DOL_DOCUMENT_ROOT.'/Facture.class.php');
$obj_facturation = unserialize ($_SESSION['serObjFacturation']);
unset ($_SESSION['serObjFacturation']);
switch ( $_GET['action'] )
{
default:
$redirection = DOL_URL_ROOT.'/cashdesk/affIndex.php?menu=validation';
break;
case 'valide_achat':
$company=new Societe($db);
$company->fetch($conf->global->CASHDESK_ID_THIRDPARTY);
$invoice=new Facture($db);
$invoice->date=dol_now('tzserver');
$invoice->type=0;
$num=$invoice->getNextNumRef($company);
$obj_facturation->num_facture($num);
$obj_facturation->mode_reglement ($_POST['hdnChoix']);
// Si paiement autre qu'en esp<73>ces, montant encaiss<73> = prix total
$mode_reglement = $obj_facturation->mode_reglement();
if ( $mode_reglement != 'ESP' ) {
$montant = $obj_facturation->prix_total_ttc();
} else {
$montant = $_POST['txtEncaisse'];
}
if ( $mode_reglement != 'DIF') {
$obj_facturation->montant_encaisse ($montant);
//D<>termination de la somme rendue
$total = $obj_facturation->prix_total_ttc ();
$encaisse = $obj_facturation->montant_encaisse();
$obj_facturation->montant_rendu ( $encaisse - $total );
} else {
$obj_facturation->paiement_le ($_POST['txtDatePaiement']);
}
$redirection = 'affIndex.php?menu=validation';
break;
case 'retour':
$redirection = 'affIndex.php?menu=facturation';
break;
case 'valide_facture':
// Recuperation de la date et de l'heure
$date = date ('Y-m-d');
$heure = date ('H:i:s');
// Recuperation du mode de reglement et cr<63>ation de la note privee ...
$note = '';
switch ( $obj_facturation->mode_reglement() ) {
case 'ESP':
$mode_reglement = 4;
$note .= 'Reglement en especes'."\n";
$note .= 'Somme encaissee : '.$obj_facturation->montant_encaisse()." euro\n";
$note .= 'Somme rendue : '.$obj_facturation->montant_rendu()." euro\n";
$note .= "\n";
$note .= '--------------------------------------'."\n\n";
break;
case 'CB':
$mode_reglement = 6;
break;
case 'CHQ':
$mode_reglement = 7;
break;
}
// ... on termine la note
$note .= addslashes ($_POST['txtaNotes']);
// TODO Add records into database using the Dolibarr business classes
// instead of direct access.
// Also add a transaction and error management
// Si paiement differe ...
if ( $obj_facturation->mode_reglement() == 'DIF' )
{
// ... ajout d'une facture sans mode de reglement, avec la date d'<27>ch<63>ance
$sql->query (
"INSERT INTO ".MAIN_DB_PREFIX."facture (
facnumber,
type,
ref_client,
increment,
fk_soc,
datec,
datef,
date_valid,
paye,
amount,
remise_percent,
remise_absolue,
remise,
close_code,
close_note,
tva,
total,
total_ttc,
fk_statut,
fk_user_author,
fk_user_valid,
fk_facture_source,
fk_projet,
fk_cond_reglement,
fk_mode_reglement,
date_lim_reglement,
note,
model_pdf
) VALUES (
'".$obj_facturation->num_facture()."',
0,
NULL,
NULL,
".$conf_fksoc.",
'".$date." ".$heure."',
'".$date."',
NULL,
0,
".$obj_facturation->prix_total_ht().",
0,
0,
0,
NULL,
NULL,
".$obj_facturation->montant_tva().",
".$obj_facturation->prix_total_ht().",
".$obj_facturation->prix_total_ttc().",
1,
".$_SESSION['uid'].",
".$_SESSION['uid'].",
NULL,
NULL,
0,
255,
'".$obj_facturation->paiement_le()."',
'".$note."',
'crabe'
);");
// Recuperation de l'id de la facture nouvellement creee
$resql=$sql->query ("SELECT rowid
FROM ".MAIN_DB_PREFIX."facture
WHERE 1
ORDER BY rowid DESC");
$ret=array();
$tab = $sql->fetch_array($resql);
foreach ( $tab as $cle => $valeur )
{
$ret[$cle] = $valeur;
}
$tab_id_facture=$ret;
$id = $tab_id_facture['rowid'];
// Sinon ...
}
else
{
// ... ajout d'une facture et d'un paiement
$sql->query ("INSERT INTO ".MAIN_DB_PREFIX."facture (
facnumber,
type,
ref_client,
increment,
fk_soc,
datec,
datef,
date_valid,
paye,
amount,
remise_percent,
remise_absolue,
remise,
close_code,
close_note,
tva,
total,
total_ttc,
fk_statut,
fk_user_author,
fk_user_valid,
fk_facture_source,
fk_projet,
fk_cond_reglement,
fk_mode_reglement,
date_lim_reglement,
note,
note_public)
VALUES (
'".$obj_facturation->num_facture()."',
0,
NULL,
NULL,
".$conf_fksoc.",
'".$date." ".$heure."',
'".$date."',
NULL,
1,
".$obj_facturation->prix_total_ht().",
0,
0,
0,
NULL,
NULL,
".$obj_facturation->montant_tva().",
".$obj_facturation->prix_total_ht().",
".$obj_facturation->prix_total_ttc().",
2,
".$_SESSION['uid'].",
".$_SESSION['uid'].",
NULL,
NULL,
1,
".$mode_reglement.",
'".$date."',
'".$note."',
NULL)
");
// Recuperation de l'id de la facture nouvellement creee
$resql=$sql->query (
"SELECT rowid
FROM ".MAIN_DB_PREFIX."facture
WHERE 1
ORDER BY rowid DESC");
$ret=array();
$tab = $sql->fetch_array($resql);
foreach ( $tab as $cle => $valeur )
{
$ret[$cle] = $valeur;
}
$tab_id_facture = $tab;
$id = $tab_id_facture['rowid'];
// Ajout d'une operation sur le compte de caisse, uniquement si le paiement est en especes
if ( $obj_facturation->mode_reglement() == 'ESP' )
{
$sql->query (
"INSERT INTO ".MAIN_DB_PREFIX."bank (
datec,
datev,
dateo,
amount,
label,
fk_account,
fk_user_author,
fk_type,
rappro,
fk_bordereau
)
VALUES (
'".$date." ".$heure."',
'".$date."',
'".$date."',
".$obj_facturation->prix_total_ttc().",
'Paiement caisse facture ".$obj_facturation->num_facture()."',
".$conf_fkaccount.",
".$_SESSION['uid'].",
'ESP',
0,
0
)
");
}
// Recuperation de l'id de l'operation nouvellement creee
$resql=$sql->query (
"SELECT rowid
FROM ".MAIN_DB_PREFIX."bank
WHERE 1
ORDER BY rowid DESC");
$ret=array();
$tab = $sql->fetch_array($resql);
foreach ( $tab as $cle => $valeur )
{
$ret[$cle] = $valeur;
}
$tab_id_operation = $tab;
$id_op = $tab_id_operation['rowid'];
// Ajout d'un nouveau paiement
$sql->query (
"INSERT INTO ".MAIN_DB_PREFIX."paiement (
fk_facture,
datec,
datep,
amount,
fk_paiement,
num_paiement,
note,
fk_bank,
fk_user_creat,
fk_user_modif,
statut,
fk_export_compta
)
VALUES (
".$id.",
'".$date." ".$heure."',
'".$date." 12:00:00',
".$obj_facturation->prix_total_ttc().",
".$mode_reglement.",
NULL,
NULL,
$id_op,
".$_SESSION['uid'].",
NULL,
1,
0
)");
// Recuperation de l'id du paiement nouvellement cr<63>
$resql=$sql->query (
"SELECT rowid
FROM ".MAIN_DB_PREFIX."paiement
WHERE 1
ORDER BY rowid DESC");
$ret=array();
$tab = $sql->fetch_array($resql);
foreach ( $tab as $cle => $valeur )
{
$ret[$cle] = $valeur;
}
$tab_id_paiement = $tab;
$id_paiement = $tab_id_paiement['rowid'];
$sql->query (
"INSERT INTO ".MAIN_DB_PREFIX."paiement_facture (
fk_paiement,
fk_facture,
amount
)
VALUES (
".$id_paiement.",
".$id.",
".$obj_facturation->prix_total_ttc()."
)
");
}
// Recuperation de la liste des articles du panier
$res=$sql->query ('
SELECT fk_article, qte, fk_tva, remise_percent, remise, total_ht, total_ttc, reel
FROM '.MAIN_DB_PREFIX.'tmp_caisse
LEFT JOIN '.MAIN_DB_PREFIX.'product_stock ON '.MAIN_DB_PREFIX.'tmp_caisse.fk_article = '.MAIN_DB_PREFIX.'product_stock.fk_product
WHERE 1');
$ret=array(); $i=0;
while ( $tab = $sql->fetch_array($res) )
{
foreach ( $tab as $cle => $valeur )
{
$ret[$i][$cle] = $valeur;
}
$i++;
}
$tab_liste = $ret;
// Loop on each product
for ($i = 0; $i < count ($tab_liste); $i++)
{
// Recuperation de l'article
$res = $sql->query (
'SELECT label, tva_tx, price
FROM '.MAIN_DB_PREFIX.'product
WHERE rowid = '.$tab_liste[$i]['fk_article']);
$ret=array();
$tab = $sql->fetch_array($res);
foreach ( $tab as $cle => $valeur )
{
$ret[$cle] = $valeur;
}
$tab_article = $ret;
$res = $sql->query (
'SELECT taux
FROM '.MAIN_DB_PREFIX.'c_tva
WHERE rowid = '.$tab_liste[$i]['fk_tva']);
$ret=array();
$tab = $sql->fetch_array($res);
foreach ( $tab as $cle => $valeur )
{
$ret[$cle] = $valeur;
}
$tab_tva = $ret;
// Calcul du montant de la TVA
$montant_tva = $tab_liste[$i]['total_ttc'] - $tab_liste[$i]['total_ht'];
// Calcul de la position de l'article dans la liste
$position = $i + 1;
$reel = $tab_liste[$i]['reel'];
$qte = $tab_liste[$i]['qte'];
$stock = $reel - $qte;
// Ajout d'une entree dans le detail de la facture
$sql->query (
'INSERT INTO '.MAIN_DB_PREFIX.'facturedet (
fk_facture,
fk_product,
description,
tva_taux,
qty,
remise_percent,
remise,
fk_remise_except,
subprice,
price,
total_ht,
total_tva,
total_ttc,
date_start,
date_end,
info_bits,
fk_code_ventilation,
fk_export_compta,
rang
)
VALUES (
'.$id.",
".$tab_liste[$i]['fk_article'].",
'".$tab_article['label']."',
".$tab_tva['taux'].",
".$tab_liste[$i]['qte'].",
".$tab_liste[$i]['remise_percent'].",
".$tab_liste[$i]['remise'].",
0,
".$tab_article['price'].",
".$tab_article['price'].",
".$tab_liste[$i]['total_ht'].",
".$montant_tva.",
".$tab_liste[$i]['total_ttc'].",
NULL,
NULL,
0,
0,
0,
".$position.")");
}
$redirection = 'affIndex.php?menu=validation_ok&facid='.$id; // Ajout de l'id de la facture, pour l'inclure dans un lien pointant directement vers celle-ci dans Dolibarr
break;
// End of case: valide_facture
}
$_SESSION['serObjFacturation'] = serialize ($obj_facturation);
header ('Location: '.$redirection);
?>