<?php
// +-------------------------------------------------+
// � 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: encaissement.php,v 1.14 2015-04-03 11:16:23 jpermanne Exp $

//Liste des trabsactions d'un compte
$base_path="..";

$current_alert="circ";

require_once("../includes/init.inc.php");
require_once("$base_path/classes/comptes.class.php");
require_once($class_path."/transaction/transaction.class.php");

$cpte=new comptes($id_compte);
if ($cpte->error) {
	print pmb_bidi($cpte->error_message);
	exit;
} 

function back_to_list() {
	global $show_transactions, $date_debut,$id_compte;
	
	print "<script type='text/javascript' >document.location=\"list_transactions.php?show_transactions=$show_transactions&date_debut=".rawurlencode(stripslashes($date_debut))."&id_compte=$id_compte\";</script>";
}

function back_to_main() {
		global $show_transactions, $date_debut,$id_compte,$cpte;
	
		print "<script type='text/javascript' >parent.document.location=\"../circ.php?categ=pret&sub=compte&id=".$cpte->get_empr_from_compte_id()."&typ_compte=".$cpte->typ_compte."&show_transactions=$show_transactions&date_debut=".rawurlencode(stripslashes($date_debut))."\";</script>";
}

function encaisse_form($with_validated=false, $transacash_num=0) {
	global $id_compte,$solde,$date_debut,$val_transactions,$somme,$cpte,$charset;
	global $pmb_gestion_devise,$msg,$charset;
	
	$solde=$cpte->get_solde();
	if ($solde<0) {
		print "<table>";
		print "<tr><td style='text-align:right'>".$msg["finance_enc_montant_valide"]." : </td><td style='text-align:right'>".comptes::format($somme*(-1))."</td></tr>";
		if ($solde<=0) print "<tr class='erreur'><td style='text-align:right'>".$msg["finance_enc_montant_a_enc"]." : </td><td style='text-align:right'>"; else if ($solde>0) print "<td>".$msg["finance_enc_compte_cred"]." : </td><td style='text-align:right'>";
		print comptes::format($solde*(-1));		
		print "</td></tr></table>";
		print "<script type='text/javascript' >function check_somme(f) {
			message='';
			if (isNaN(f.somme.value)) {
				message='".addslashes($msg["finance_enc_nan"])."';
			} else {
				if (f.somme.value<=0)
					message='".addslashes($msg["finance_enc_mnt_neg"])."';
			}
			if (message) {
				alert(message);
				return false;
			} else return true;
		}
		</script>";
		print "<form name='form_encaissement' action='encaissement.php?id_compte=$id_compte&show_transactions=$show_transactions&date_debut=".rawurlencode(stripslashes($date_debut))."' method='post'>
		<input type='hidden' name='act' value='enc'/>
		<input type='hidden' name='transacash_num' value='$transacash_num'/>
		<input type='hidden' name='val_transactions' value=\"".htmlentities($val_transactions,ENT_QUOTES,$charset)."\"/>".
		htmlentities($msg['finance_mnt_percu'], ENT_QUOTES, $charset)."&nbsp;<input type='text' value='".$solde*(-1)."' name='somme' class='saisie-5em' style='text-align:right'>&nbsp;".$pmb_gestion_devise."
		<input type='submit' value='".$msg["finance_but_enc"]."' class='bouton' onClick=\"return check_somme(this.form)\"/>&nbsp;<input type='button' value='".$msg["76"]."' class='bouton' onClick=\"document.form_encaissement.act.value=''; document.form_encaissement.submit();\"/>
		</form>
		";
	} else {
		back_to_main();
	}
}

function special_form() {
	global $id_compte,$solde,$date_debut,$val_transactions,$somme,$cpte,$charset,$msg, $pmb_gestion_devise;
	print "<h3>".$msg["finance_but_cred"]."</h3>";
	print "<script type='text/javascript' >function check_somme(f) {
		message='';
		if (isNaN(f.somme.value)) {
			message='".addslashes($msg["finance_enc_nan"])."';
		} else {
			if (f.somme.value<=0)
				message='".addslashes($msg["finance_enc_mnt_neg"])."';
		}
		if (message) {
			alert(message);
			return false;
		} else return true;
	}
	</script>";
	print "<form name='form_special' action='encaissement.php?id_compte=$id_compte&show_transactions=$show_transactions&date_debut=".rawurlencode(stripslashes($date_debut))."' method='post'>
		<input type='hidden' name='act' value='enc_special'/>
		".$msg["finance_montant"]." <input type='text' value='' name='somme' class='saisie-5em' style='text-align:right'>&nbsp;".$pmb_gestion_devise."<br />
		<input type='radio' value='1' name='typ_special' id='typ_special_1' checked>&nbsp;<label for='typ_special_1'>".$msg["finance_enc_spe_crediter"]."&nbsp;<input type='checkbox' name='credit_perte' value='1'>&nbsp;".$msg["finance_enc_spe_perte"]."</label><br /><input type='radio' value='2' name='typ_special' id='typ_special_2'>&nbsp;<label for='typ_special_2'>".$msg["finance_enc_debiter"]."</label><br />
		<input type='radio' value='3' name='typ_special' id='typ_special_3'>&nbsp;<label for='typ_special_3'>".$msg["finance_enc_crediter_enc"]."</label><br /><input type='radio' value='4' name='typ_special' id='typ_special_4'>&nbsp;<label for='typ_special_4'>".$msg["finance_enc_debiter_enc"]." <input type='checkbox' name='dec_perte' value='1'>&nbsp;".$msg["finance_enc_spe_perte"]."</label><br />
		".$msg["finance_enc_raison"]."<br />
		<textarea cols='80' rows='2' wrap='virtual' name='commentaire'></textarea><br />
		<input type='submit' value='".$msg["finance_enc_valider"]."' class='bouton' onClick=\"return check_somme(this.form)\"/>&nbsp;<input type='button' value='".$msg["76"]."' class='bouton' onClick=\"document.form_special.act.value=''; document.form_special.submit();\"/>
		</form>
		";
}

if($pmb_printer_name) {
$print_script="
<div id='printer_script'></div>
<script type='text/javascript'>
	
	function printer_get_jzebra() {
		if(!document.jzebra) {
			var req = new http_request();
			req.request('$base_path/ajax.php?module=circ&categ=zebra_print_pret&sub=get_script');
			document.getElementById('printer_script').innerHTML=req.get_text();
			return false;
		}
	}

	function printer_jzebra_send_ticket(text,printer,encoding) {
		console.log(text);
		console.log(printer);
		console.log(encoding);	
		var applet = document.jzebra;
		var found=false;
		if(applet!=null) {
			applet.findPrinter(printer);
			while (!applet.isDoneFinding()) {}
			if(printer == applet.getPrinter()) {
				found = true; 
				if(encoding) {
					applet.setEncoding(encoding);
				}
				applet.append(text);
				applet.print();
			}
		}
		if(!found) {		     	     				
       		alert('".$msg['printer_not_found']."');
       	}
    } 
				
	function printer_jzebra_print_ticket(url) {
		printer_get_jzebra();
		var req = new http_request();
		if(req.request(url)){
			// Il y a une erreur. 
			alert ( req.get_text() );			
		}else { 
			alert ( req.get_text() );	
			printer_jzebra_send_ticket(req.get_text(),'".$pmb_printer_name."','850');
			return 1;	
		}
	}
	printer_jzebra_print_ticket('$base_path/ajax.php?module=circ&categ=zebra_print_pret&sub=transacash_ticket&transacash_id=!!transacash_id!!');
</script>	";
}

switch ($act) {
	case "valenc":
		//Validation de ce qui n'est pas valide
		$t=$cpte->get_transactions("","",0,0);
		$somme=0;
		$val_transactions="";
		for ($i=0; $i<count($t); $i++) {
			if ($cpte->validate_transaction($t[$i]->id_transaction)) {
				$somme+=$t[$i]->montant*$t[$i]->sens;
				$val_transactions.=" #".$t[$i]->id_transaction."#";
			}
		}
		if(count($t)){
			$transacash_num=$cpte->cashdesk_memo_transactions($t);					
		}
		if ($val_transactions!="") $val_transactions=$msg["finance_enc_tr_lib_valider"]." : ".$val_transactions."\n";
		$solde_avant=$cpte->get_solde();
		if ($solde_avant!=0) $val_transactions.=$msg["finance_enc_tr_lib_etat_compte"]." : ".$solde_avant;
		$cpte->update_solde();
		encaisse_form(true,$transacash_num);
		break;
	case "enc":
		if ($somme*1>0) {
			//Generation de la transaction
			if ($id_transaction=$cpte->record_transaction("",$somme,1,$val_transactions,1)) {
				$cpte->validate_transaction($id_transaction);
				$cpte->update_solde();
				if(!$transacash_num){					
					$req="select MAX(transacash_num) from transactions where compte_id=".$cpte->id_compte."";
					$resultat=pmb_mysql_query($req);
					if ($transacash_num=pmb_mysql_result($resultat,0,0)){
						$req="update transactions set transacash_num = $transacash_num where compte_id=".$cpte->id_compte." and transacash_num=0";
						pmb_mysql_query($req);						
					}
				}
				$cpte->cashdesk_memo_encaissement($id_transaction,$transacash_num,$somme);		
			}
//			print str_replace("!!transacash_id!!",$transacash_num,$print_script);			
			back_to_main();
		} else {
			back_to_list();
		}
		break;
	case "transac_add":			
		if ($transactype_total*1>0) {
			$transactype=new transactype($transactype_id);	
			if($quantity) $aff_quantity=" ($quantity)";
			//Generation de la transaction
			if ($id_transaction=$cpte->record_transaction("",$transactype_total,-1,$transactype->name.$aff_quantity,0,$transactype_id)) {
				//$cpte->validate_transaction($id_transaction);
				//$cpte->update_solde();
			}
			back_to_main();
		} else {
			back_to_list();
		}
		break;
	case "val":
		if (!isset($trans)) $trans=array();
		$i=0;
		foreach ($trans as $key=>$value){
			$cpte->validate_transaction($key);
			$t[$i]->id_transaction=$key;			
			$i++;
		}
		if(count($t)){
			$transacash_num=$cpte->cashdesk_memo_transactions($t);					
		}
		$cpte->update_solde();
		back_to_main();
		break;
	case "supr":
		if (!isset($trans)) $trans=array();
		foreach ($trans as $key=>$value){
			$cpte->delete_transaction($key);
		}
		$cpte->update_solde();
		back_to_main();
		break;
	case "encnoval":
		$solde=$cpte->get_solde();
		$val_transactions.=$msg["finance_enc_tr_lib_etat_compte"]." : ".$solde;
		encaisse_form();
		
		break;
	case "special":
		special_form();
		break;
	case "enc_special":
		if ($somme*1>0) {
			switch ($typ_special) {
				case "1":
					//Crediter
					$signe=1;
					$caisse=0;
					break;
				case "2":
					//Debiter
					$signe=-1;
					$caisse=0;
					break;
				case "3":
					//Crediter et encaisser
					$signe=1;
					$caisse=1;
					break;
				case "4":
					//Debiter et decaisser
					$signe=-1;
					$caisse=1;
					break;
			}
			if ($id_transaction=$cpte->record_transaction("",$somme,$signe,stripslashes($commentaire),$caisse)) {
				$cpte->validate_transaction($id_transaction);
				//Traitement du passage en perte
				//Credit
				if (($typ_special==1)&&($credit_perte)) {
					//Ajout d'un transaction debit pour le compte 0
					$requete="insert into transactions (compte_id,user_id,user_name,machine,date_enrgt,date_prevue,date_effective,montant,sens,realisee,commentaire,encaissement) values(0,$PMBuserid,'".$PMBusername."','".$_SERVER["REMOTE_ADDR"]."', now(), now(), now(), ".($somme*1).", -1, 1,'#".$id_transaction."# : ".$commentaire."',0)";
					pmb_mysql_query($requete);
				}
				//Decaissement
				if (($typ_special==4)&&($dec_perte)) {
					//Credit sur le compte du lecteur
					if ($id_transaction_1=$cpte->record_transaction("",$somme,1,sprintf($msg["finance_enc_tr_lib_lost"]," #".$id_transaction."#")." : ".stripslashes($commentaire),0)) {
						$cpte->validate_transaction($id_transaction_1);
						//Debit sur le compte 0
						$requete="insert into transactions (compte_id,user_id,user_name,machine,date_enrgt,date_prevue,date_effective,montant,sens,realisee,commentaire,encaissement) values(0,$PMBuserid,'".$PMBusername."','".$_SERVER["REMOTE_ADDR"]."', now(), now(), now(), ".($somme*1).", -1, 1,'#".$id_transaction_1."# : ".sprintf($msg["finance_enc_tr_lib_lost"],"#".$id_transaction."#")." : ".stripslashes($commentaire)."',0)";
						pmb_mysql_query($requete);
					}
				}
				$cpte->update_solde();
			}
			back_to_main();
		} else {
			back_to_list();
		}
	default:
		back_to_list();
		break;
}

print "<script type='text/javascript'> parent.document.getElementById('selector_transaction_list').style.visibility='hidden';
parent.document.getElementById('buttons_transaction_list').style.visibility='hidden';
</script>";
?>