PageRenderTime 708ms CodeModel.GetById 101ms app.highlight 359ms RepoModel.GetById 154ms app.codeStats 1ms

/debitor/pos_ordre.php

https://github.com/nielsrune/saldi
PHP | 1575 lines | 1362 code | 88 blank | 125 comment | 427 complexity | 3b4ede9849323771f1a8a8866f53e63d MD5 | raw file
   1<?php
   2// ------------- debitor/pos_ordre.php ---------- lap 3.2.3----2013.05.07-------
   3// LICENS
   4//
   5// Dette program er fri software. Du kan gendistribuere det og / eller
   6// modificere det under betingelserne i GNU General Public License (GPL)
   7// som er udgivet af The Free Software Foundation; enten i version 2
   8// af denne licens eller en senere version efter eget valg
   9// Fra og med version 3.2.2 dog under iagttagelse af følgende:
  10// 
  11// Programmet må ikke uden forudgående skriftlig aftale anvendes
  12// i konkurrence med DANOSOFT ApS eller anden rettighedshaver til programmet.
  13//
  14// Dette program er udgivet med haab om at det vil vaere til gavn,
  15// men UDEN NOGEN FORM FOR REKLAMATIONSRET ELLER GARANTI. Se
  16// GNU General Public Licensen for flere detaljer.
  17//
  18// En dansk oversaettelse af licensen kan laeses her:
  19// http://www.fundanemt.com/gpl_da.html
  20//
  21// Copyright (c) 2004-2013 DANOSOFT ApS
  22// ----------------------------------------------------------------------
  23//
  24// 2013-03-10. Tilføjet mulighed for at give rabat på varer uden pris ved at skrive "r" efter prisen. Søg 20130310
  25// 2013.05.07 - Tilføjet visning af kostpris v. mus over pris.
  26
  27@session_start();
  28$s_id=session_id();
  29ob_start();
  30
  31$modulnr=5;
  32$title="POS_ordre";
  33$css="../css/pos.css";
  34$betaling=NULL; $betaling2=NULL; $konto_id=NULL; $next_varenr=NULL;
  35$fokus="varenr_ny";
  36$valuta='DKK';$valutakurs='100';
  37
  38
  39include("../includes/connect.php");
  40include("../includes/online.php");
  41include("../includes/std_func.php");
  42include("../includes/ordrefunc.php");
  43
  44
  45$projekt=NULL;
  46$tid=date("H:i");
  47$r=db_fetch_array(db_select("select box9 from grupper where art='POSBUT' and (box7 < box8) and (box7<'$tid' and box8>'$tid')",__FILE__ . " linje " . __LINE__));
  48$projekt=$r['box9'];
  49if (!$projekt) {
  50	$r=db_fetch_array(db_select("select box9 from grupper where art='POSBUT' and (box7 > box8) and ((box7>'$tid' and box8>'$tid') or (box7<'$tid' and box8<'$tid'))",__FILE__ . " linje " . __LINE__));
  51	$projekt=$r['box9'];
  52}
  53
  54$returside=(if_isset($_GET['returside']));
  55if (!$returside) {
  56	if ($popup) $returside="../includes/luk.php";
  57	else $returside="../index/menu.php";
  58}
  59print "<script language=\"javascript\" type=\"text/javascript\" src=\"../javascript/confirmclose.js\"></script>";
  60
  61$kasse = if_isset($_GET['kasse']);
  62$menu_id = if_isset($_GET['menu_id']);
  63$kassebeholdning = if_isset($_GET['kassebeholdning']);
  64if ($kasse && $kassebeholdning) kassebeholdning($kasse);
  65if (!$kasse || $kasse == "?") $kasse=find_kasse($kasse);
  66elseif ($kasse=="opdat") {
  67	$kasse=$_POST['kasse'];
  68	setcookie("saldi_pos",$kasse,time()+60*60*24*30);
  69}
  70ob_end_flush();
  71$bon = trim(strtoupper(if_isset($_POST['bon'])));
  72if ($bon=='S') {
  73	$r=db_fetch_array(db_select("select max(id) as id from ordrer where felt_5='$kasse'",__FILE__ . " linje " . __LINE__));
  74	if ($id=$r['id'])	$r=db_fetch_array(db_select("select fakturanr from ordrer where id='$id'",__FILE__ . " linje " . __LINE__));
  75	$bon=trim($r['fakturanr']);
  76} elseif ($bon) {
  77	$r=db_fetch_array(db_select("select id from ordrer where fakturanr = '$bon'",__FILE__ . " linje " . __LINE__));
  78	$id=$r['id'];
  79} else $id = if_isset($_GET['id'])*1;
  80$vare_id = if_isset($_GET['vare_id'])*1;
  81$vare_id_ny = if_isset($_GET['vare_id_ny'])*1;
  82if ($vare_id_ny && !$vare_id) {
  83	$vare_id=$vare_id_ny;
  84} elseif ($vare_id_ny && $vare_id) {
  85	if (!$id) $id=opret_ordre(NULL,$kasse);
  86	$r=db_fetch_array(db_select("select varenr, salgspris from varer where id = '$vare_id'",__FILE__ . " linje " . __LINE__));
  87#cho "A opret_ordrelinje($id,$r[varenr],1,'',$r[pris_ny],0,'PO','','','0','on','','','0')<br>";
  88	$linje_id=opret_ordrelinje($id,$r['varenr'],1,'',$r['pris_ny'],0,'PO','','','0','on','','','0');
  89	$vare_id=$vare_id_ny;
  90}
  91$funktion = if_isset($_GET['funktion']);
  92if ($funktion) {
  93	$sort = if_isset($_GET['sort'])*1;
  94	$funktion ('PO',$sort,$fokus, $id,"","","");
  95}
  96$spec_func = if_isset($_GET['spec_func']);
  97if ($spec_func) {
  98	$kode = if_isset($_POST['kode']);
  99	include("../includes/spec_func.php");
 100	$svar=$spec_func('xx',$id,$kode);
 101	if (!is_numeric($svar)) {
 102		print "<BODY onLoad=\"javascript:alert('$svar')\">";
 103	}	else $konto_id=$svar;
 104}
 105$kontonr = if_isset($_POST['kontonr'])*1;
 106if (!$konto_id) $konto_id = if_isset($_GET['konto_id'])*1;
 107if ($konto_id || $kontonr) {
 108	$id=opdater_konto($konto_id,$kontonr,$id);
 109	$r=db_fetch_array(db_select("select momssats,sum,betalt,betalingsbet from ordrer where id = '$id'",__FILE__ . " linje " . __LINE__));
 110	$betalingsbet=$r['betalingsbet'];
 111	$momssats=$r['momssats']*1;
 112	if ($betalingsbet!='Kontant') $modtaget=$r['betalt']*1;
 113	$sum=$r['sum']*1;
 114	$betaling='ukendt';
 115#	if ($modtaget <= $sum) $id=afslut($id,'konto',$modtaget);
 116#	else $betaling='ukendt';
 117}
 118
 119if ($vare_id) {
 120	$r=db_fetch_array(db_select("select varenr from varer where id = '$vare_id'",__FILE__ . " linje " . __LINE__));
 121	$varenr_ny=$r['varenr'];
 122} elseif (sizeof($_POST)>1) {
 123	$afslut=if_isset($_POST['afslut']);
 124	$ny_bruger=if_isset($_POST['ny_bruger']);
 125	$kode=if_isset($_POST['kode']);
 126	$indbetaling=if_isset($_POST['indbetaling']);
 127	$sum=if_isset($_POST['sum']);
 128	$betaling=if_isset($_POST['betaling']);
 129	$betaling2=if_isset($_POST['betaling2']);
 130	$kontonr=if_isset($_POST['kontonr']);
 131	$modtaget=if_isset($_POST['modtaget']);
 132	$modtaget2=if_isset($_POST['modtaget2']);
 133	$kundeordnr=if_isset($_POST['kundeordnr']);
 134	$fokus=if_isset($_POST['fokus']);
 135 	$pris_ny=if_isset($_POST['pris_ny']);
 136 	$antal_ny=strtolower(trim(if_isset($_POST['antal_ny'])));
 137	$varenr_ny=db_escape_string(trim(if_isset($_POST['varenr_ny'])));
 138	$momssats=(if_isset($_POST['momssats']));
 139	$rabat_ny=if_isset($_POST['rabat_ny']);
 140
 141	if (substr($betaling,0,9) == "Kontant p") {
 142		$antal_ny=1;
 143		if ($id && $varenr_ny) {
 144			$r=db_fetch_array(db_select("select salgspris from varer where varenr = '$varenr_ny'",__FILE__ . " linje " . __LINE__));
 145#cho "B opret_ordrelinje($id,$varenr_ny,1,'',$r[pris_ny],0,'PO','','','0','on','','','0')<br>";
 146			$linje_id=opret_ordrelinje($id,$varenr_ny,1,'',$r[pris_ny],0,'PO','','','0','on','','','0');
 147			$varenr_ny=NULL;
 148		}
 149	}
 150	if (strtolower($antal_ny)=='a') {
 151		$antal_ny=1;
 152		$afslut=NULL;
 153	}
 154	$sum*=1;
 155	#cho "update ordrer set kundeordnr = '$kundeordnr',sum='$sum', betalt='$betalt',felt_1='$betaling',felt_2='$modtaget',felt_3='$betaling2',felt_4='$modtaget2',felt_5='$kasse' where id='$id'<br>";
 156	if ($kundeordnr && $id) db_modify("update ordrer set kundeordnr = '$kundeordnr' where id='$id'",__FILE__ . " linje " . __LINE__);
 157
 158#cho "betalt=$betalt fok $fokus<br>";
 159	if (strstr($pris_ny,",")) { #Skaerer orebelob ned til 2 cifre.
 160		list($kr,$ore)=explode(",",$pris_ny);
 161		$ore=substr($ore,0,2);
 162		$pris_ny=$kr.",".$ore;
 163	}
 164	if(isset($_POST['ny']) && $_POST['ny'] == "Ny kunde") {
 165		$id=0;
 166		$kontonr=0;
 167		$menu_id=NULL;
 168		$bon=NULL;
 169	}
 170	if(isset($_POST['udskriv']) && $_POST['udskriv'] == "Udskriv") {
 171		$momssats=$momssats*1;
 172#		include("../includes/formfunk.php");
 173		pos_txt_print($id,$betaling,$modtaget,$indbetaling);
 174	}
 175	if(isset($_POST['krediter'])) {
 176		$ny_id=krediter_pos($id);
 177	  print "<meta http-equiv=\"refresh\" content=\"0;URL=pos_ordre.php?id=$ny_id\">";
 178	}
 179
 180	if ($fokus=="antal_ny" && $antal_ny!='0' && !$pris_ny) $antal_ny.="p";
 181	if ($fokus=="pris_ny" && $pris_ny!='f' && substr($pris_ny,-1)!='r') $fokus="antal_ny"; #20130310 tilføjet: "&& substr($pris_ny,-1)!='r'" samt 2 næste linjer
 182	if ($fokus=="pris_ny" && $pris_ny!='f' && substr($pris_ny,-1)=='r') { 
 183		$pris_ny=str_replace("r","",$pris_ny);
 184		$fokus='rabat_ny';
 185	} elseif ($fokus=="rabat_ny" && $pris_ny!='f') $fokus="antal_ny";
 186	if ($fokus=="antal_ny" && (substr($antal_ny,-1)=='p' || substr($antal_ny,-1)=='r')) {
 187		if (substr($antal_ny,-1)=='p') $fokus='pris_ny';
 188		else $fokus='rabat_ny';
 189		if (strlen($antal_ny)>1) $antal_ny=substr($antal_ny,0,strlen($antal_ny)-1);
 190		else $antal_ny=1;
 191	} elseif ($fokus=="varenr_ny" && $varenr_ny=='v') {
 192		vareopslag('PO',"",$fokus, $id,"","","");
 193	} elseif ($fokus=="pris_ny" && substr($pris_ny,-1)=='r') {
 194		$pris_ny=substr($pris_ny,0,strlen($pris_ny)-1);
 195		$fokus="rabat_ny";
 196	} elseif (isset($_POST['forfra']) && $id) {
 197		$r=db_fetch_array(db_select("select status from ordrer where id = '$id'",__FILE__ . " linje " . __LINE__));
 198		if ($r['status']<3) {
 199			db_modify ("update ordrer set konto_id='0', kontonr='',firmanavn='',addr1='',addr2='',postnr='',bynavn='',land='',betalingsdage='0',betalingsbet='Kontant',
 200				cvrnr='',ean='',institution='',email='',kontakt='',art='PO',valuta='DKK',valutakurs='100',kundeordnr='' where id = '$id'",__FILE__ . " linje " . __LINE__);
 201			db_modify("delete from ordrelinjer where ordre_id='$id'",__FILE__ . " linje " . __LINE__);
 202			$varenr_ny=''; $antal_ny=''; $modtaget=''; $betaling=''; $indbetaling=''; $fokus="varenr_ny";
 203		}
 204	} elseif (substr($modtaget,-1)=='t') $betaling="";
 205#	elseif (substr($modtaget,-1)=='d' && !$betaling) $betaling="creditcard";
 206	elseif (substr($modtaget,-1)=='c' && !$betaling) $betaling="kontant";
 207	elseif (substr($modtaget,-1)=='k' || $betaling == "konto") {
 208		if (substr($modtaget,0,1)=='+') $modtaget=$sum+usdecimal(substr($modtaget,1,strlen($modtaget)-1));
 209		elseif (!is_numeric(substr($modtaget,-1))) $modtaget=substr($modtaget,0,strlen($modtaget)-1);
 210		if (!$modtaget || !$kontonr) kontoopslag('PO',"",$fokus, $id,"","","");
 211	} elseif (isset($_POST['kontoopslag'])) {
 212		kontoopslag('PO',"","varenr_ny",$id,"","","","","","","");
 213	}
 214	if ($indbetaling) {
 215			$indbetaling=str_replace("a","",$indbetaling);
 216			$tmp=trim(str_replace(".","",$indbetaling));
 217			$tmp=str_replace(",",".",$tmp);
 218#cho "$tmp $indbetaling $modtaget<br>"; 
 219			if (is_numeric($tmp)) {
 220				$indbetaling=usdecimal($indbetaling);
 221				$modtaget=usdecimal($modtaget);
 222			if ($indbetaling>$modtaget) {
 223				print "<BODY onLoad=\"javascript:alert('Indbetaling kan ikke v&aelig;re større end beløbet der modtages')\">";
 224				$indbetaling=$modtaget;
 225			}
 226		}
 227#cho "$tmp $indbetaling $modtaget<br>"; 
 228#exit;
 229	}	elseif ($betaling) {
 230		if (substr($modtaget,0,1)=='+') $modtaget=$sum+usdecimal(substr($modtaget,1,strlen($modtaget)-1));
 231		elseif (!is_numeric(substr($modtaget,-1))) $modtaget=usdecimal(substr($modtaget,0,strlen($modtaget)-1));
 232		else $modtaget=usdecimal($modtaget);
 233		$modtaget=$modtaget*1;
 234		if (!$modtaget) $modtaget=$sum;
 235		if (substr($modtaget2,0,1)=='+') $modtaget2=$sum+usdecimal(substr($modtaget2,1,strlen($modtaget2)-1));
 236		elseif (!is_numeric(substr($modtaget2,-1))) $modtaget2=usdecimal(substr($modtaget2,0,strlen($modtaget2)-1));
 237		else $modtaget2=usdecimal($modtaget2);
 238		$modtaget2=$modtaget2*1;
 239#		if (!$modtaget2) $modtaget2=$sum;
 240	}
 241	$betalt=$modtaget+$modtaget2;
 242	if (($betalt && is_numeric($betalt))||(!$sum && $afslut=="Afslut")) {
 243		if (!$sum && $afslut=="Afslut"){
 244			$betaling="kontant";
 245		}
 246		$afslut="OK";
 247		if (!is_numeric($sum)) $afslut=NULL;
 248		if ($betalt < $sum && !$indbetaling) $afslut=NULL;
 249		if (!$betaling)  $afslut=NULL;
 250		if (substr($betaling,0,9)=="Kontant p") $afslut=NULL;
 251		if ($betaling=="ukendt") $afslut=NULL;
 252		if ($betaling2 && $betaling2=="ukendt") $afslut=NULL;
 253		if ($modtaget2 && (!$betaling2 || $betaling2=="ukendt")) $afslut=NULL;
 254		if ($afslut=="OK") {
 255			 $svar=afslut($id,$betaling,$betaling2,$modtaget,$modtaget2,$indbetaling);
 256			if ($svar) print "<BODY onLoad=\"javascript:alert('$svar')\">";
 257 			else {
 258			  print "<meta http-equiv=\"refresh\" content=\"0;URL=$php_self\">";
 259			}
 260		}
 261	} else {
 262		$tmp=str_replace(",",".",$antal_ny);
 263		if ($varenr_ny == "a") {
 264			$betaling="ukendt";
 265			$varenr_ny=NULL;
 266		} elseif ($antal_ny == "a") {
 267			$betaling="ukendt";
 268			$antal_ny=1;
 269		} elseif ($antal_ny && !is_numeric($tmp) || $tmp>99999) { # Sᅵ er der skannet et varenummer ind som antal
 270				$next_varenr=$antal_ny;
 271				$antal_ny=1;
 272		} elseif ($fokus=="antal_ny") {
 273			if ($antal_ny=="0") $varenr_ny = NULL;
 274			elseif (!strlen($antal_ny)) $antal_ny=1;
 275			else $antal_ny=usdecimal($antal_ny);
 276		}
 277 		if ($varenr_ny && $antal_ny && $fokus!="pris_ny" && $fokus!="rabat_ny") {
 278			if (!$id) $id=opret_ordre(NULL,$kasse);
 279			if ($id && !is_numeric($id)) {
 280				print "<BODY onLoad=\"javascript:alert('$id')\">";
 281			} else {
 282#cho "264  $id,$varenr_ny,$antal_ny,'',usdecimal($pris_ny),$rabat_ny<br>";
 283 				$linje_id=opret_ordrelinje($id,$varenr_ny,$antal_ny,'',usdecimal($pris_ny),$rabat_ny,'PO','','','0','on','','','0');
 284				if ($linje_id && !is_numeric($linje_id)) {
 285					print "<BODY onLoad=\"javascript:alert('$linje_id')\">";
 286					$fokus="pris_ny";
 287				} else {
 288					$varenr_ny=$next_varenr;
 289					$antal_ny=NULL;
 290		#			$sum=0;
 291				}
 292			}
 293		} elseif ($varenr_ny) $sum=find_pris($varenr_ny);
 294#		else $sum=0;
 295	}
 296}
 297
 298############################
 299$x=0;
 300if (!$id) {
 301	$dd=date("Y-m-d");
 302	$r=db_fetch_array(db_select("select max(id) as id from ordrer where status < '3' and art = 'PO' and ref = '$brugernavn' and ordredate = '$dd'",__FILE__ . " linje " . __LINE__));
 303	$id=$r['id']*1;
 304}
 305if ($ny_bruger && $ny_bruger!=$brugernavn) skift_bruger($ny_bruger,$kode);
 306if (!isset($momssats)) $momssats=find_momssats($id,$kasse);
 307print "<table width=\"100%\" height=\"100%\" border=\"1\" cellpadding=\"0\" cellspacing=\"0\"><tbody>\n";
 308print "<tr><td valign=\"top\" width=50%><table width=\"100%\"><tbody>\n";
 309print "<form name=pos_ordre action=pos_ordre.php?id=$id&menu_id=$menu_id method=post>\n";
 310if ($id && $betaling) $sum=betaling($id,$momssats,$betaling,$betaling2,$modtaget,$modtaget2);
 311elseif (!$indbetaling) list($varenr_ny,$pris_ny,$status)=explode(chr(9),varescan($id,$momssats,$varenr_ny,$antal_ny,$pris_ny));
 312else indbetaling($id,$indbetaling,$modtaget);
 313if (substr($betaling,0,9) == "Kontant p") {
 314	$betaling='Kontant';
 315	$modtaget=$sum;
 316	$svar=afslut($id,$betaling,NULL,$modtaget,0,NULL);
 317	if ($svar) print "<BODY onLoad=\"javascript:alert('$svar')\">";
 318	else print "<meta http-equiv=\"refresh\" content=\"0;URL=pos_ordre.php?id=$id\">";
 319}
 320if ($varenr_ny=='fejl') fejl($id,"$status");
 321if (!$id) print "<tr><td height=\"50%\" colspan=\"4\" align=\"center\" valign=\"middle\"><b><div style=\"font-size:100mm;color:$bgcolor2;\">$kasse</div></b></td></tr>";
 322print "</tbody></table></td>\n";
 323print "<td valign=\"top\"><table width=\"100%\" border=\"0\"><tbody><td align=\"center\">\n";
 324print "<tr><td colspan=\"2\" valign=\"top\" height=\"1%\"><table width=\"100%\" border=\"0\"><tbody>\n";
 325hoved($kasse);
 326# kassebeholdning($kasse);
 327print "</tbody></table></td></tr>\n";
 328#print "</FORM><form name=tastatur action=pos_ordre.php?id=$id method=post>\n";
 329tastatur($status);
 330print "</FORM>";
 331print "</tbody></table></td></tr>\n";
 332print "<tr><td colspan=\"2\" valign=\"top\" height=\"1%\" align=\"center\"><table width=\"100%\" border=\"0\"><tbody>\n";
 333if ($status<3) menubuttons($id,$menu_id,$vare_id);
 334print "</td></tbody></table></td></tr>\n";
 335print "</td></tbody></table></td></tr>\n";
 336#print "<tr><td colspan=2 width=\"100%\" height=\"1%\"><table width=\"100%\" height=\"100%\" border=\"0\"><tbody>\n";
 337#footer ($kasse);
 338#print "</tbody></table></td></tr>\n";
 339# print "</tbody></table></td>\n";
 340
 341
 342function afslut ($id,$betaling,$betaling2,$modtaget,$modtaget2,$indbetaling) {
 343# "afslut($id,$betaling,$modtaget,$indbetaling)";
 344#exit;
 345
 346	global $charset;
 347	global $bruger_id;
 348	global $kasse;
 349	global $regnaar;
 350	
 351#	include("../includes/formfunk.php");
 352	$projekt=NULL;
 353	$tid=date("H:i");
 354	$r=db_fetch_array(db_select("select box9 from grupper where art='POSBUT' and (box7 < box8) and (box7<'$tid' and box8>'$tid')",__FILE__ . " linje " . __LINE__));
 355	$projekt=$r['box9'];
 356	if (!$projekt) {
 357		$r=db_fetch_array(db_select("select box9 from grupper where art='POSBUT' and (box7 > box8) and ((box7>'$tid' and box8>'$tid') or (box7<'$tid' and box8<'$tid'))",__FILE__ . " linje " . __LINE__));
 358		$projekt=$r['box9'];
 359	}
 360	$hurtigfakt='on';
 361	$moms=0;
 362	$dd=date("Y-m-d");
 363	$r=db_fetch_array(db_select("select konto_id,status,fakturanr,momssats,betalingsbet from ordrer where id = '$id'",__FILE__ . " linje " . __LINE__));
 364	$momssats=$r['momssats'];
 365	$status=$r['status'];
 366	$konto_id=$r['konto_id'];
 367	$betalingsbet=$r['betalingsbet'];
 368	$x=0;
 369
 370	if ($status<3) {
 371		$r=db_fetch_array(db_select("select box8 from grupper where art = 'POS' and kodenr = '1'",__FILE__ . " linje " . __LINE__));
 372		$rabatvareid=$r['box8'];
 373		$q=db_select("select * from ordrelinjer where ordre_id = '$id' order by rabatgruppe, id desc ",__FILE__ . " linje " . __LINE__);
 374		while($r=db_fetch_array($q)) {
 375			$x++;
 376			$linje_id[$x]=$r['id'];
 377			$pris[$x]=$r['pris'];
 378			$antal[$x]=$r['antal'];
 379			$momsfri[$x]=$r['momsfri'];
 380			$varemomssats[$x]=$r['momssats'];
 381			$folgevare[$x]=$r['folgevare'];
 382			$rabat[$x]=$r['rabat'];
 383			$rabatart[$x]=$r['rabatart'];
 384			$rabatgruppe[$x]=$r['rabatgruppe'];
 385			if ($rabatgruppe[$x]) {
 386				if ($rabatgruppe[$x]==$rabatgruppe[$x-1]) {
 387					$rabatantal[$x]=$antal[$x]+$rabatantal[$x-1];
 388					$rabatantal[$x-1]=0;
 389				} else $rabatantal[$x]=$antal[$x];
 390			} else $rabatantal[$x]=0;
 391			$m_rabat[$x]=$r['m_rabat']*-1;
 392		}
 393		$linjeantal=$x;
 394		$pos=0;
 395
 396		for($x=1;$x<=$linjeantal;$x++) {
 397			$pos++;
 398			db_modify("update ordrelinjer set posnr='$pos',projekt='$projekt' where id='$linje_id[$x]'",__FILE__ . " linje " . __LINE__);
 399			if ($rabatart[$x]=='amount') {
 400				$tmp=afrund(($pris[$x]-$rabat[$x])*$antal[$x],2);
 401			} else $tmp=afrund($pris[$x]*$antal[$x]-($pris[$x]*$antal[$x]/100*$rabat[$x]),2);
 402			$sum+=$tmp;
 403			if (!$momsfri[$x]) $moms+=$tmp/100*$varemomssats[$x];
 404########################################################################
 405			if ($folgevare[$x]) {
 406				$pos++;
 407				$r=db_fetch_array(db_select("select varenr,beskrivelse,salgspris,gruppe from varer where id = '$folgevare[$x]'",__FILE__ . " linje " . __LINE__));
 408				$r2 = db_fetch_array(db_select("select box4, box7 from grupper where art = 'VG' and kodenr = '$r[gruppe]'",__FILE__ . " linje " . __LINE__));
 409				$f_bogfkto=$r2['box4'];
 410				$f_momsfri=$r2['box7'];
 411				if (!$f_momsfri){
 412					$r2 = db_fetch_array(db_select("select moms from kontoplan where kontonr = '$f_bogfkto' and regnskabsaar = '$regnaar'",__FILE__ . " linje " . __LINE__));
 413					$kodenr=substr($r['moms'],1);
 414					$r2 = db_fetch_array(db_select("select box2 from grupper where kodenr = '$kodenr' and art = 'SM'",__FILE__ . " linje " . __LINE__));
 415					$f_momssats=$r2['box2']*1;
 416				}
 417#cho "insert into ordrelinjer (ordre_id,vare_id,varenr,beskrivelse,antal,m_rabat,pris,kostpris,momssats,momsfri,posnr,projekt) values ('$id','$folgevare[$x]', '$r[varenr]', '$r[beskrivelse]', '$antal[$x]','0','$r[salgspris]','0','$f_momssats','$f_momsfri','$pos','0')<br>";
 418				db_modify("insert into ordrelinjer (ordre_id,vare_id,varenr,beskrivelse,antal,m_rabat,pris,kostpris,momssats,momsfri,posnr,projekt) values ('$id','$folgevare[$x]', '$r[varenr]', '$r[beskrivelse]', '$antal[$x]','0','$r[salgspris]','0','$f_momssats','$f_momsfri','$pos','$projekt')",__FILE__ . " linje " . __LINE__);
 419#				print "<tr><td>$r[varenr]</td><td align=\"right\">".dkdecimal($antal[$x])."</td><td>$r[beskrivelse]</td><td align=\"right\">".dkdecimal($r['salgspris'])."</td><td align=\"right\">".dkdecimal($antal[$x]*$r['salgspris'])."</td>";
 420				$sum+=afrund($antal[$x]*$r['salgspris'],2);
 421			}
 422			if ($rabatantal[$x]) {
 423				list($grupperabat,$rabattype)=explode(";",grupperabat($rabatantal[$x],$rabatgruppe[$x]));
 424					if ($grupperabat) {
 425						$pos++;
 426						$r=db_fetch_array(db_select("select varenr,beskrivelse,salgspris,gruppe from varer where id = '$rabatvareid'",__FILE__ . " linje " . __LINE__));
 427						$r2 = db_fetch_array(db_select("select box6, box7 from grupper where art = 'VG' and kodenr = '$r[gruppe]'",__FILE__ . " linje " . __LINE__));
 428						$momsfri = $r2['box7'];
 429						db_modify("insert into ordrelinjer (ordre_id,vare_id,varenr,beskrivelse,antal,m_rabat,pris,kostpris,momsfri,posnr,projekt) values ('$id','$rabatvareid', '$r[varenr]', '$r[beskrivelse]', '$rabatantal[$x]','0','$grupperabat','0','$r[momsfri]','$pos','$projekt')",__FILE__ . " linje " . __LINE__);
 430						$sum+=afrund($grupperabat*$rabatantal[$x],2);
 431					}
 432				} elseif ($m_rabat[$x] && !$rabatgruppe[$x]) {
 433					$pos++;
 434					if ($rabatvareid && $r=db_fetch_array(db_select("select id,varenr,beskrivelse,salgspris,gruppe from varer where id = '$rabatvareid'",__FILE__ . " linje " . __LINE__))) {
 435						$r2 = db_fetch_array(db_select("select box6, box7 from grupper where art = 'VG' and kodenr = '$r[gruppe]'",__FILE__ . " linje " . __LINE__));
 436						$momsfri = $r2['box7'];
 437						db_modify("insert into ordrelinjer (ordre_id,vare_id,varenr,beskrivelse,antal,m_rabat,pris,kostpris,momsfri,posnr,projekt) values ('$id','$rabatvareid', '$r[varenr]', '$r[beskrivelse]', '$antal[$x]','0','$m_rabat[$x]','0','$r[momsfri]','$pos','$projekt')",__FILE__ . " linje " . __LINE__);
 438						$sum+=afrund($m_rabat[$x]*$antal[$x],2);
 439					} else print "<BODY onLoad=\"javascript:alert('Manglende varenr for rabat')\">";
 440				}
 441#			}
 442		}
 443
 444		$fakturanr=1;
 445		$q=db_select("select fakturanr from ordrer where art = 'PO'",__FILE__ . " linje " . __LINE__);
 446			while ($r=db_fetch_array($q)) {
 447			$tmp=$r['fakturanr']*1;
 448			if ($tmp>$fakturanr) $fakturanr=$tmp;
 449		}
 450		$fakturanr++;
 451		$sum*=1; $moms*=1;
 452		$betalt=$modtaget+$modtaget2;
 453		
 454		if ($konto_id && ($betalingsbet!='Kontant' || $indbetaling)) {
 455			$saldo=0;
 456			$q=db_select("select * from openpost where konto_id = '$konto_id'",__FILE__ . " linje " . __LINE__);
 457			while ($r=db_fetch_array($q)) {
 458				$saldo=$saldo+$r['amount'];
 459			}
 460			$betaling2=$saldo;
 461			if ($indbetaling) {
 462			$modtaget2=$saldo-$indbetaling;
 463			$sum=$indbetaling;
 464			$moms='0';
 465		} else $modtaget2=$saldo+$sum;
 466	}
 467	if ($betaling=='Kontant' && !$betaling2) { 
 468#cho "select box2 from grupper where art='OreDif'<br>";
 469		$r=db_fetch_array(db_select("select box2 from grupper where art='OreDif'",__FILE__ . " linje " . __LINE__));
 470		if ($difkto=$r['box2']){
 471#cho "D $difkto<br>";
 472			$afrundet=pos_afrund($sum);
 473#cho "E $afrundet<br>";
 474			if ($modtaget==$sum) {
 475				$modtaget=$afrundet;
 476#				$sum=$afrundet;
 477			}
 478		}
 479	}
 480#cho "B $betaling - $modtaget - $sum - $afrundet<br>";
 481#xit;
 482
 483$modtaget=afrund($modtaget,2);
 484$modtaget2=afrund($modtaget2,2);
 485
 486$tidspkt=date("Y-m-d H:i");
 487#cho "update ordrer set levdate = '$dd',fakturadate = '$dd',fakturanr = '$fakturanr',sum='$sum', moms='$moms', betalt='$betalt',status='2',felt_1='$betaling',felt_2='$modtaget',felt_3='$betaling2',felt_4='$modtaget2',felt_5='$kasse',tidspkt='$tidspkt',projekt='$projekt' where id='$id'<br>"; 
 488db_modify("update ordrer set levdate = '$dd',fakturadate = '$dd',fakturanr = '$fakturanr',sum='$sum', moms='$moms', betalt='$betalt',status='2',felt_1='$betaling',felt_2='$modtaget',felt_3='$betaling2',felt_4='$modtaget2',felt_5='$kasse',tidspkt='$tidspkt',projekt='$projekt' where id='$id'",__FILE__ . " linje " . __LINE__);
 489		if (!$indbetaling) {
 490			$svar=levering($id,'on','','');
 491#cho "Svar1 $svar<br>";
 492				if ($svar != 'OK') return ($svar);
 493			$svar=bogfor($id,'');
 494#cho "Svar2 $svar<br>";
 495			if ($svar != 'OK') return ($svar);
 496		} else {
 497			$svar=bogfor_indbetaling($id,'');
 498			if ($svar != 'OK') return ($svar);
 499		}
 500
 501	}
 502	if (db_fetch_array(db_select("select id from grupper where art = 'POS' and kodenr = '1' and box10='on'",__FILE__ . " linje " . __LINE__))) {
 503		pos_txt_print($id,$betaling,$betaling2,$modtaget,$modtaget2,$indbetaling);
 504	} else {
 505		$pfnavn="../temp/".$db."/".$bruger_id.".txt";
 506		$fp=fopen("$pfnavn","w");
 507		fclose($fp);
 508		$tmp="/temp/".$db."/".$bruger_id.".txt";
 509		$url="://".$_SERVER['SERVER_NAME'].=$_SERVER['PHP_SELF'];
 510		$url=str_replace("/debitor/pos_ordre.php","",$url);
 511		if ($_SERVER[HTTPS]) $url="s".$url;
 512		$url="http".$url;
 513		print "<BODY onLoad=\"JavaScript:window.open('http://localhost/saldiprint.php?printfil=$tmp&url=$url&bruger_id=$bruger_id&bonantal=1' , '' , '$jsvars');\">";
 514	}
 515	return(NULL);
 516}
 517
 518function betaling($id,$momssats,$betaling,$betaling2,$modtaget,$modtaget2) {
 519	global $fokus;
 520	global $kontonr;
 521
 522	$fokus="modtaget";
 523	if ($id) {
 524		$r=db_fetch_array(db_select("select * from ordrer where id = '$id'",__FILE__ . " linje " . __LINE__));
 525		$konto_id=$r['konto_id']*1;
 526		$kontonr=$r['kontonr'];
 527		$firmanavn=$r['firmanavn'];
 528		$addr1=$r['addr1'];
 529		$post_by=$r['postnr']." ".$r['bynavn'];
 530		$kundeordnr=$r['kundeordnr'];
 531		$status=$r['status'];
 532		$betalingsbet=$r['betalingsbet'];
 533		if ($r['lukket']) $betalingsbet='Kontant';
 534		$ref=$r['ref'];
 535		if ($konto_id) {
 536			print "<tr><td><b>$kontonr</b>";
 537			if ($kundeordnr) print "&nbsp;&nbsp;&nbsp; Rekv.nr: $kundeordnr";
 538			print "</td></tr>\n";
 539			print "<tr><td colspan=\"2\"><b>D $firmanavn</b></td></tr>\n";
 540			if ($betalingsbet=='Kontant')print "<tr><td colspan=\"2\"><b>Ingen kredit</b></td>"; 
 541		}
 542		print "<tr><td width=\"50%\"><table width=\"100%\"><tbody>";
 543		print "<tr><td>Varenummer</td><td align=\"right\">Antal</td><td>Varenavn</td><td align=\"right\">Pris</td><td align=\"right\">Sum</td></tr>\n";
 544		print "<tr><td colspan=\"5\"><hr></td></tr>\n";
 545		$sum=vis_pos_linjer($id,$momssats,$status);
 546		if ($kontonr && $betalingsbet!='Kontant') $modtaget=$sum;
 547		if ($modtaget) $retur=$modtaget-$sum;
 548	}
 549	print "<input type=\"hidden\" name = \"fokus\" value=\"$fokus\">";
 550	print "<input type=\"hidden\" name = \"betaling\" value=\"$betaling\">";
 551	print "<input type=\"hidden\" name = \"sum\" value=\"$sum\">";
 552	if ($modtaget) $tmp=dkdecimal($modtaget);
 553	else $tmp="";
 554#cho "$kontonr && $betalingsbet!='Kontant'<br>";
 555#exit;
 556	if ($kontonr && $betalingsbet!='Kontant') print "<input type=\"hidden\" name=\"modtaget\" value=\"$tmp\">";
 557	elseif(substr($betaling,0,9)!='Kontant p') {
 558		print "<tr><td>$betaling</td><td colspan= \"4\" align=right><input class=\"inputbox\" type=\"text\" size=\"15\" style=\"text-align:right\" name = \"modtaget\" value=\"$tmp\"></td></tr>\n";
 559		if ($betaling != "ukendt" && ($retur<0 || $modtaget2)) {$color="color: rgb(255, 0, 0);";
 560			if ($modtaget2) $tmp=dkdecimal($modtaget2);
 561			else $tmp="";
 562			if (!$betaling2) $betaling2="ukendt";
 563			$fokus="modtaget2";
 564			$retur=$retur+$modtaget2;
 565			print "<tr><td>$betaling2</td><td colspan= \"4\" align=right><input class=\"inputbox\" type=\"text\" size=\"15\" style=\"text-align:right\" name = \"modtaget2\" value=\"$tmp\"></td></tr>\n";
 566		} else $color="color: rgb(0, 0, 0);";
 567		print "<tr><td>Retur</td><td colspan= \"4\" align=right><span style=\"$color\">".dkdecimal($retur)."</span></td></tr>\n";
 568	}
 569	print "<td colspan=\"5\"><input STYLE=\"width: 100%;height: 0.01em;\" type=submit name=\"OK\" value=\"\"></td></tr>\n";
 570	print "</tbody></table>";
 571#cho "SUM $sum<br>";
 572	return($sum);
 573}
 574
 575function skift_bruger($ny_bruger,$kode) {
 576	global $brugernavn;
 577	global $s_id;
 578	global $db;
 579
 580	$kode=md5($kode);
 581	if ($r=db_fetch_array(db_select("select id from brugere where brugernavn ='$ny_bruger' and kode = '$kode'",__FILE__ . " linje " . __LINE__))) {
 582		include("../includes/connect.php");
 583		db_modify("update online set brugernavn='$ny_bruger' where session_id='$s_id' and db = '$db'",__FILE__ . " linje " . __LINE__);
 584		$brugernavn=$ny_bruger;
 585		print "<input type=\"hidden\" name=\"brugernavn\" value=\"$brugernavn\">";
 586		include("../includes/online.php");
 587	} else print "<BODY onLoad=\"javascript:alert('Forkert adgangskode')\">";
 588}
 589
 590function varescan ($id,$momssats,$varenr_ny,$antal_ny,$pris_ny) {
 591	global $fokus;
 592	global $kontonr;
 593	global $sum;
 594
 595	if ($id) {
 596		$r=db_fetch_array(db_select("select * from ordrer where id = '$id'",__FILE__ . " linje " . __LINE__));
 597		$konto_id=$r['konto_id'];
 598		$kontonr=$r['kontonr'];
 599		$firmanavn=$r['firmanavn'];
 600		$addr1=$r['addr1'];
 601		$post_by=$r['postnr']." ".$r['bynavn'];
 602		$status=$r['status'];
 603		$kundeordnr=$r['kundeordnr'];
 604		$betalingsbet=$r['betalingsbet'];
 605		if (!$r['firmanavn']) $betalingsbet='Kontant';
 606		if ($status >= 3) {
 607			$fakturanr=$r['fakturanr'];
 608			$kasse=$r['felt_5'];
 609			$fakturadato=dkdato(substr($r['fakturadate'],0,10));
 610			$tidspkt=substr($r['tidspkt'],-5);
 611			if (!$tidspkt) {
 612				$r2=db_fetch_array(db_select("select logtime from transaktioner where ordre_id = '$id'",__FILE__ . " linje " . __LINE__));
 613				$tidspkt=substr($r2['logtime'],0,5);
 614				$tmp=$r['fakturadate']." ".$tidspkt;
 615#cho "update ordrer set tidspkt = '$tmp' where id = '$id'<br>";
 616				db_modify("update ordrer set tidspkt = '$tmp' where id = '$id'",__FILE__ . " linje " . __LINE__);
 617			}
 618		}
 619		if ($status >= 3) {
 620			$betaling=$r['felt_1'];
 621			$modtaget=$r['felt_2'];
 622			$betaling2=$r['felt_3'];
 623			$modtaget2=$r['felt_4'];
 624		} else {
 625			$fakturanr=NULL;
 626			$fakturadato=NULL;
 627			$kasse=NULL;
 628			$tidspkt=NULL;
 629		}
 630		($r['ref'])?$ref=$r['ref']:$ref=$brugernavn;
 631
 632		if ($ref) {
 633			if ($r=db_fetch_array(db_select("select ansat_id from brugere where brugernavn = '$ref'",__FILE__ . " linje " . __LINE__))) {
 634				$ansat_id=$r['ansat_id']*1;
 635				if ($r=db_fetch_array(db_select("select navn from ansatte where id = '$ansat_id'",__FILE__ . " linje " . __LINE__))) $ref=$r['navn'];
 636			}
 637		}
 638	}
 639	if ($kontonr && $betalingsbet!='Kontant') {
 640		$r=db_fetch_array(db_select("select kreditmax from adresser where id = '$konto_id'",__FILE__ . " linje " . __LINE__));
 641		$kreditmax=$r['kreditmax'];
 642		$r=db_fetch_array(db_select("select sum(amount) as saldo from openpost where konto_id = '$konto_id'",__FILE__ . " linje " . __LINE__));
 643		$saldo=$r['saldo'];
 644	}
 645
 646	if ($varenr_ny) {
 647		$varenr_ny=db_escape_string($varenr_ny);
 648		$varenr_low=strtolower($varenr_ny);
 649		$varenr_up=strtoupper($varenr_ny);
 650#cho "SELECT id,vare_id,variant_type FROM variant_varer WHERE upper(variant_stregkode) = '$varenr_up'<br>";
 651		if ($r=db_fetch_array(db_select("SELECT id,vare_id,variant_type FROM variant_varer WHERE upper(variant_stregkode) = '$varenr_up'",__FILE__ . " linje " . __LINE__))) {
 652			$vare_id=$r['vare_id'];
 653			$variant_type=$r['variant_type'];
 654			$variant_id=$r['id'];
 655		} else {
 656			$variant_id=0;
 657			$variant_type='';
 658		}
 659		if ($vare_id) $string="select * from varer where id='$vare_id'";
 660		else $string="select * from varer where lower(varenr) = '$varenr_low' or upper(varenr) = '$varenr_up' or varenr LIKE '$varenr_ny' or lower(stregkode) = '$varenr_low' or upper(stregkode) = '$varenr_up' or stregkode LIKE '$varenr_ny'";
 661#cho "streng ".$string."<br>";
 662		if ($r=db_fetch_array(db_select("$string",__FILE__ . " linje " . __LINE__))) {
 663#		  $varenr_ny=db_escape_string($r['varenr']);
 664		  $beskrivelse[0]=$r['beskrivelse'];
 665		  $kostpris[0]=$r['kostpris'];
 666			$pris[0]=find_pris($r['varenr'])*1;
 667		  if ($pris[0]) $pris[0]=dkdecimal($pris[0]);
 668			else $pris[0]="";
 669
 670			if ($fokus!="pris_ny" && $fokus!="rabat_ny") $fokus="antal_ny";
 671		} else return ("fejl".chr(9)."".chr(9)."Varenr: $varenr_ny eksisterer ikke");
 672		if ($variant_type) {
 673			$varianter=explode(chr(9),$variant_type);
 674			for ($y=0;$y<count($varianter);$y++) {
 675				$r1=db_fetch_array(db_select("select variant_typer.beskrivelse as vt_besk,varianter.beskrivelse as var_besk from variant_typer,varianter where variant_typer.id = '$varianter[$y]' and variant_typer.variant_id=varianter.id",__FILE__ . " linje " . __LINE__));
 676				$beskrivelse[0].=", ".$r1['var_besk'].":".$r1['vt_besk'];
 677			}
 678		}
 679	} else $fokus="varenr_ny";
 680	if ($kontonr) {
 681		print "<tr><td><b>$kontonr</b></td><td colspan=\"2\">";
 682		if ($status<3) print "Rekv.nr:&nbsp; <input type=\"text\" size=\"15\" name=\"kundeordnr\" value=\"$kundeordnr\">";
 683		elseif ($kundeordnr) print "&nbsp; Rekv.nr:&nbsp; $kundeordnr</td>";
 684		if ($status>=3) print "</td><td colspan=\"2\" align=\"right\">Ekspedient: $ref | Bon: $fakturanr</td>";
 685		print "</tr>\n<tr><td colspan=\"2\"><b>$firmanavn</b></td>";
 686		if ($status>=3) print "<td colspan=\"4\" align=\"right\">Kasse: $kasse | $fakturadato kl. $tidspkt</td></tr>";
 687		if ($betalingsbet=='Kontant')print "<tr><td colspan=\"2\"><b>Ingen kredit</b></td>"; 
 688	} else {
 689		print "<tr><td colspan=\"5\" align=\"right\">Ekspedient: $ref | Bon: $fakturanr</td></tr>";
 690		print "<tr><td colspan=\"5\" align=\"right\">Kasse: $kasse | $fakturadato kl. $tidspkt</td></tr>";
 691	}
 692	print "<tr><td width=\"10%\" height=\"25px\" valign=\"bottom\">Varenummer</td><td width=\"2%\" valign=\"bottom\">Antal</td><td valign=\"bottom\">Varenavn</td><td align=\"right\" valign=\"bottom\">Pris</td>\n";
 693 	if ($fokus=="rabat_ny") print "<td align=\"right\" valign=\"bottom\">Rabat</td></tr>";
 694 	else print "<td align=\"right\" valign=\"bottom\">Sum</td></tr>";
 695	print "<tr><td colspan=\"5\"><hr></td></tr>\n";
 696	if ($status < 3) {
 697		print "<input type=\"hidden\" name = \"fokus\" value=\"$fokus\">";
 698		#print "<input type=\"hidden\" name = \"vare_id\" value=\"$vare_id[0]\">";
 699		print "<input type=\"hidden\" name = \"momssats\" value=\"$momssats\">";
 700		print "<tr><td width=\"30px\"><input class=\"inputbox\" type=\"text\" size=\"15\" name = \"varenr_ny\" value=\"$varenr_ny\"></td>";
 701		if ($varenr_ny) {
 702			print "<td width=\"7px\"><input class=\"inputbox\" type=\"text\" style=\"text-align:right\" size=\"2\" name = \"antal_ny\" value=\"$antal_ny\"></td><td>".$beskrivelse[0]."</td>";
 703			if ($antal_ny) print "<td align=\"right\" title=\"Kostpris ex. moms: ".dkdecimal($kostpris[0])."\"><input class=\"inputbox\" type=\"text\" style=\"text-align:right\" size=\"4\" name = \"pris_ny\" value=\"$pris[0]\"></td>";
 704			else {
 705				print "<input type=hidden name=\"pris_ny\" value=\"$pris[0]\">";
 706				print "<td align=\"right\" title=\"Kostpris ex. moms: ".dkdecimal($kostpris[0])."\">$pris[0]</td>";
 707			}
 708			if ($pris_ny && $fokus=="rabat_ny") {
 709				$r=db_fetch_array(db_select("select box8 from grupper where art = 'POS' and kodenr = '1'",__FILE__ . " linje " . __LINE__));
 710				$rabatvareid=$r['box8']*1;
 711				if (db_fetch_array(db_select("select varenr from varer where id = '$rabatvareid'",__FILE__ . " linje " . __LINE__))) {
 712					print "<td align=\"right\"><input class=\"inputbox\" type=\"text\" style=\"text-align:right\" size=\"4\" name = \"rabat_ny\">%</td>";
 713				} else {
 714					$txt="Manglende varenr til rabat";
 715					print "<BODY onLoad=\"javascript:alert('$txt')\">";
 716					return($txt);
 717				}
 718			} else {
 719					print "<input type=hidden name=\"rabat_ny\" value=\"$rabat[0]\">";
 720					print "<td align=\"right\">$rabat[0]</td>";
 721			}
 722
 723		}
 724		print "</tr>";
 725	}
 726	$sum=vis_pos_linjer($id,$momssats,$status);
 727	if ($konto_id && $kreditmax && $sum > $kreditmax - $saldo) {
 728		$ny_saldo=$saldo+$sum;
 729		$txt = "Kreditmax: ".dkdecimal($kreditmax)."\\nGl. saldo :  ".dkdecimal($saldo)."\\nNy saldo :  ".dkdecimal($ny_saldo);
 730		print "<BODY onLoad=\"javascript:alert('$txt')\">";
 731
 732	}
 733	print "<input type=\"hidden\" name = \"sum\" value = \"$sum\">";
 734	if ($status >= 3 && $sum) {
 735		$tmp=dkdecimal($modtaget);
 736		print "<tr><td>$betaling</td><td colspan=\"4\" align=\"right\">$tmp</td></tr>";
 737		if ($betalt<$sum && $betaling != "Konto") {
 738		$tmp=dkdecimal($modtaget2);
 739		print "<tr><td>$betaling2</td><td colspan=\"4\" align=\"right\">$tmp</td></tr>";
 740		}
 741		$tmp=dkdecimal($modtaget+$modtaget2-$sum);
 742		if ($betaling != "Konto" || $betalingsbet=='Kontant') print "<tr><td>Retur</td><td colspan=\"4\" align=\"right\"><b>$tmp</b></td></tr>";
 743	} elseif ($status >= 3) {
 744		$r=db_fetch_array($q = db_select("select * from ordrer where id='$id'",__FILE__ . " linje " . __LINE__));
 745		print "<tr><td>Saldo</td><td colspan=\"4\" align=\"right\">".dkdecimal($r[felt_3])."</td></tr>\n";
 746		$indbetaling=($r['felt_4']-$r['felt_3'])*-1;
 747		print "<tr><td>Indbetaling</td><td colspan=\"4\" align=\"right\">".dkdecimal($indbetaling)."</td></tr>\n";
 748		print "<tr><td>$r[felt_1]</td><td colspan=\"4\" align=\"right\">".dkdecimal($r['felt_2'])."</td></tr>\n";
 749		$ny_saldo=$r['felt_4'];
 750		print "<tr><td>Ny saldo</td><td colspan=\"4\" align=\"right\">".dkdecimal($ny_saldo)."</td></tr>\n";
 751		$retur=$r['felt_2']+$r['felt_4']-$r['felt_3'];
 752		print "<tr><td>Retur</td><td  colspan=\"4\" align=\"right\">".dkdecimal($retur)."</td></tr>\n";
 753	}
 754	print "<tr><td colspan=\"5\" align=\"right\"><input  STYLE=\"width: 100%;height: 0.01em;\" type=submit name=\"OK\" value=\"\"></td></tr>\n";
 755	if ($konto_id && $status<3 && $betalingsbet!='Kontant') {
 756		print "<tr><td>Gl. saldo</td><td colspan=\"4\" align=\"right\">".dkdecimal($saldo)."</td></tr>";
 757		$ny_saldo=$saldo+$sum;
 758		print "<tr><td>Ny saldo</td><td colspan=\"4\" align=\"right\">".dkdecimal($ny_saldo)."</td></tr>";
 759		if ($kreditmax) {
 760			print "<tr><td>Kreditmax</td><td colspan=\"4\" align=\"right\">".dkdecimal($kreditmax	)."</td></tr>";
 761		}
 762	}
 763	return ($varenr_ny.chr(9).$pris_ny.chr(9).$status);
 764}
 765
 766function opret_ordre($konto_id,$kasse){
 767	global $brugernavn;
 768	if ($r=db_fetch_array($q = db_select("select ordrenr from ordrer where art='PO' order by ordrenr desc",__FILE__ . " linje " . __LINE__))) {
 769		$ordrenr=$r['ordrenr']+1;
 770	} else $ordrenr=1;
 771	$ordredate=date("Y-m-d");
 772	$tidspkt=date("U");
 773	$r=db_fetch_array(db_select("select * from grupper where art = 'POS' and kodenr = '1'",__FILE__ . " linje " . __LINE__));
 774	$id=$r['id'];
 775	$kasseantal=$r['box1']*1;
 776	$moms=explode(chr(9),$r['box7']);
 777	$x=$kasse-1;
 778	if ($moms[$x]){
 779		$r=db_fetch_array(db_select("select * from grupper where art = 'SM' and kodenr = '$moms[$x]'",__FILE__ . " linje " . __LINE__));
 780		$momssats=$r['box2'];
 781	} else $momssats='0';
 782	db_modify ("insert into ordrer
 783		(ordrenr,konto_id, kontonr,firmanavn,addr1,addr2,postnr,bynavn,land,betalingsdage,betalingsbet,cvrnr,ean,institution,email,mail_fakt,notes,art,ordredate,momssats,hvem,tidspkt,ref,valuta,sprog,kontakt,pbs,status)
 784			values
 785		('$ordrenr','0','$kontonr','$firmanavn','','','','','','0','Kontant','','','','','','$notes','PO','$ordredate','$momssats','$brugernavn','$tidspkt','$brugernavn','DKK','','','','0')",__FILE__ . " linje " . __LINE__);
 786
 787	$r=db_fetch_array(db_select("select id from ordrer where hvem='$brugernavn' and tidspkt='$tidspkt' order by id desc",__FILE__ . " linje " . __LINE__));
 788	$id=$r['id'];
 789	return($id);
 790} # endfunc opret_ordre()
 791
 792function indbetaling($id,$indbetaling,$modtaget,$modtaget2,$betaling) {
 793
 794	global $fokus;
 795	global $status;
 796
 797	$fokus="indbetaling";
 798	$saldo=0;
 799	$r=db_fetch_array(db_select("select * from ordrer where id = '$id'",__FILE__ . " linje " . __LINE__));
 800	$konto_id=$r['konto_id'];
 801	$status=$r['status'];
 802	$kontonr=$r['kontonr'];
 803	$firmanavn=$r['firmanavn'];
 804	$addr1=$r['addr1'];
 805	$addr2=$r['addr2'];
 806	$postnr_by=$r['postnr']." ".$r['bynavn'];
 807	if ($status<3) {
 808		$q=db_select("select * from openpost where konto_id = '$konto_id'",__FILE__ . " linje " . __LINE__);
 809		while ($r=db_fetch_array($q)) {
 810			$saldo=$saldo+$r['amount'];
 811		}
 812		list($a,$b)=explode(",",$indbetaling);
 813		if (!$indbetaling || !is_numeric($indbetaling)) {
 814			$indbetaling=$saldo;
 815			$modtaget='';
 816			$modtaget2='';
 817		}
 818		if ($modtaget+$modtaget2-$indbetaling>0) $retur=dkdecimal($modtaget+$modtaget2-$indbetaling);
 819		else $retur="0,00";
 820	} else {
 821		$saldo=$r['felt_3'];
 822		$indbetaling=$r['sum'];
 823		$retur=$r['felt_2']-$indbetaling;
 824	}
 825	$ny_saldo=dkdecimal($saldo-$indbetaling);
 826	$saldo=dkdecimal($saldo);
 827	$indbetaling=dkdecimal($indbetaling);
 828	if ($modtaget) {
 829		$modtaget=dkdecimal($modtaget);
 830		$fokus="modtaget";
 831	}
 832	if ($modtaget2) {
 833		$modtaget2=dkdecimal($modtaget2);
 834		$fokus="modtaget";
 835	}
 836	print "<input type=\"hidden\" name=\"id\" value=\"$id\">";
 837	print "<tr><td><b>$kontonr</b></td></tr>\n";
 838	print "<tr><td><b>C $firmanavn</b></td></tr>\n";
 839	print "<tr><td><b>$addr1</b></td></tr>\n";
 840	print "<tr><td><b>$addr2</b></td></tr>\n";
 841	print "<tr><td><b>$postnr_by</b></td></tr>\n";
 842	print "<tr><td colspan=2 width=400px><hr></td></tr>\n";
 843#	while (strlen($saldo) < 10) $saldo=" ".$saldo;
 844	print "<tr><td>Saldo</td><td align=\"right\">$saldo</td></tr>\n";
 845	print "<tr><td>Indbetaling</td>";
 846	if ($status<3) print "<td align=\"right\"><input class=\"inputbox\" type=text size=8 style=\"text-align:right\" name=\"indbetaling\" value=\"$indbetaling\"></td></tr>\n";
 847	else print "<td align=\"right\">$indbetaling</td></tr>\n";
 848	if ($status<3) print "<tr><td>Betalt</td><td align=\"right\"><input class=\"inputbox\" type=text size=8 style=\"text-align:right\" name=\"modtaget\" value=\"$modtaget\"></td></tr>\n";
 849	else print "<tr><td>Betalt</td><td align=\"right\">$modtaget</td></tr>\n";
 850	print "<tr><td>Ny saldo</td><td align=\"right\">$ny_saldo</td></tr>\n";
 851	print "<tr><td>Retur</td><td align=\"right\">$retur</td></tr>\n";
 852  print "<td colspan=\"5\"><input STYLE=\"width: 100%;height: 0.01em;\" type=submit name=\"OK\" value=\"\"></td></tr>\n";
 853}
 854
 855function vis_pos_linjer($id,$momssats,$status) {
 856
 857	global $varelinjer;
 858	global $bgcolor;
 859	global $bgcolor5;
 860
 861	$linjebg=$bgcolor;
 862
 863	$id=$id*1;
 864	$q=db_select("select * from ordrelinjer where ordre_id = '$id' and posnr >= 0 order by rabatgruppe, id desc",__FILE__ . " linje " . __LINE__);
 865		while($r=db_fetch_array($q)) {
 866		$x++;
 867		$vare_id[$x]=$r['id'];
 868		$posnr[$x]=$r['posnr'];
 869		$varenr[$x]=$r['varenr'];
 870		$beskrivelse[$x]=stripslashes($r['beskrivelse']);
 871		$pris[$x]=$r['pris'];
 872		$kostpris[$x]=$r['kostpris'];
 873		$antal[$x]=$r['antal'];
 874		$folgevare[$x]=$r['folgevare'];
 875		$rabatgruppe[$x]=$r['rabatgruppe'];
 876		$rabat[$x]=$r['rabat']*1;
 877		$rabatart[$x]=$r['rabatart'];
 878		$m_rabat[$x]=$r['m_rabat']*-1;
 879		$momsfri[$x]=trim($r['momsfri']);
 880		$varemomssats[$x]=trim($r['momssats']);
 881		if ($rabatgruppe[$x]) {
 882			if ($rabatgruppe[$x]==$rabatgruppe[$x-1]) {
 883				$rabatantal[$x]=$antal[$x]+$rabatantal[$x-1];
 884				$rabatantal[$x-1]=0;
 885			} else $rabatantal[$x]=$antal[$x];
 886		} else $rabatantal[$x]=0;
 887		if ($varemomssats[$x] & $momsfri[$x]!='on') {
 888			$pris[$x]=$pris[$x]+$pris[$x]/100*$varemomssats[$x];
 889			if ($m_rabat[$x]) $m_rabat[$x]=$m_rabat[$x]+$m_rabat[$x]/100*$varemomssats[$x];
 890		}
 891	}
 892	$varelinjer=$x;
 893
 894	for ($x=1;$x<=$varelinjer;$x++) {
 895		($linjebg!=$bgcolor5)?$linjebg=$bgcolor5:$linjebg=$bgcolor;
 896		if ($posnr[$x]) print "<tr bgcolor=\"$linjebg\"><td>$varenr[$x]</td><td align=\"right\">".dkdecimal($antal[$x])."</td><td>$beskrivelse[$x]</td><td align=\"right\" title=\"Kostpris ex. moms: ".dkdecimal($kostpris[$x])."\">".dkdecimal($pris[$x])."</td><td align=\"right\">".dkdecimal($pris[$x]*$antal[$x])."</td>";
 897		$sum+=afrund($pris[$x]*$antal[$x],2);
 898		if ($rabat[$x]) {
 899			($linjebg!=$bgcolor5)?$linjebg=$bgcolor5:$linjebg=$bgcolor;
 900			if ($rabatart[$x]=="amount") {
 901				if ($varemomssats[$x] & $momsfri[$x]!='on') $tmp=afrund($rabat[$x]+$rabat[$x]/100*$varemomssats[$x],2)*-1;
 902				else $tmp=afrund($rabat[$x],2)*-1;
 903				if ($posnr[$x]) print "<tr bgcolor=\"$linjebg\"><td>rabat</td><td align=\"right\">".dkdecimal($antal[$x])."</td><td>Rabat</td><td align=\"right\">".dkdecimal($tmp)."</td><td align=\"right\">".dkdecimal($tmp*$antal[$x])."</td>";
 904				$sum+=afrund($tmp*$antal[$x],2);
 905			} else {
 906				$tmp=afrund($pris[$x]*$rabat[$x]/-100,2);
 907				if ($posnr[$x]) print "<tr bgcolor=\"$linjebg\"><td>rabat</td><td align=\"right\">".dkdecimal($antal[$x])."</td><td>$rabat[$x]% rabat</td><td align=\"right\">".dkdecimal($tmp)."</td><td align=\"right\">".dkdecimal($tmp*$antal[$x])."</td>";
 908				$sum+=afrund($tmp*$antal[$x],2);
 909			}
 910		}
 911		if ($status < 3) {
 912			if ($folgevare[$x]) {
 913				($linjebg!=$bgcolor5)?$linjebg=$bgcolor5:$linjebg=$bgcolor;
 914				$r=db_fetch_array(db_select("select varenr,beskrivelse,salgspris,gruppe from varer where id = '$folgevare[$x]'",__FILE__ . " linje " . __LINE__));
 915				$r2 = db_fetch_array(db_select("select box4, box7 from grupper where art = 'VG' and kodenr = '$r[gruppe]'",__FILE__ . " linje " . __LINE__));
 916				$f_bogfkto=$r2['box4'];
 917				$f_momsfri=$r2['box7'];
 918				if ($f_momsfri){
 919					$f_momssats=0;
 920					$f_pris=$r['salgspris'];
 921					} else {
 922				#cho "select moms from kontoplan where kontonr = '$f_bogfkto' and regnskabsaar = '$regnaar'<br>";
 923					$r2 = db_fetch_array(db_select("select moms from kontoplan where kontonr = '$f_bogfkto' and regnskabsaar = '$regnaar'",__FILE__ . " linje " . __LINE__));
 924					$kodenr=substr($r2['moms'],1);
 925#cho "select box2 from grupper where kodenr = '$kodenr' and art = 'SM'<br>";
 926					$r2 = db_fetch_array(db_select("select box2 from grupper where kodenr = '$kodenr' and art = 'SM'",__FILE__ . " linje " . __LINE__));
 927					$f_momssats=$r2['box2']*1;
 928					$f_pris=$r['salgspris']+$r['salgspris']*$f_momssats/100;
 929				}
 930				if ($posnr[$x]) print "<tr bgcolor=\"$linjebg\"><td>$r[varenr]</td><td align=\"right\">".dkdecimal($antal[$x])."</td><td>".stripslashes($r['beskrivelse'])."</td><td align=\"right\">".dkdecimal($r['salgspris'])."</td><td align=\"right\">".dkdecimal($antal[$x]*$r['salgspris'])."</td>";
 931				$sum+=afrund($antal[$x]*$r['salgspris'],2);
 932			}
 933			if ($rabatantal[$x]) {
 934				($linjebg!=$bgcolor5)?$linjebg=$bgcolor5:$linjebg=$bgcolor;
 935				list($grupperabat,$rabattype)=explode(";",grupperabat($rabatantal[$x],$rabatgruppe[$x]));
 936				if ($grupperabat) {
 937					if ($posnr[$x]) print "<tr bgcolor=\"$linjebg\"><td>rabat</td><td align=\"right\">".dkdecimal($rabatantal[$x])."</td><td>Rabat</td><td align=\"right\">".dkdecimal($grupperabat)."</td><td align=\"right\">".dkdecimal($grupperabat*$rabatantal[$x])."</td>";
 938					$sum+=afrund($grupperabat*$rabatantal[$x],2);
 939				}
 940			} elseif ($m_rabat[$x] && !$rabatgruppe[$x]) {
 941				if ($posnr[$x]) print "<tr bgcolor=\"$linjebg\"><td>$r[varenr]</td><td align=\"right\">".dkdecimal($antal[$x])."</td><td>".stripslashes($r['beskrivelse'])."</td><td align=\"right\">".dkdecimal($f_pris)."</td><td align=\"right\">".dkdecimal($antal[$x]*$f_pris)."</td>";
 942				$sum+=afrund($m_rabat[$x]*$antal[$x],2);
 943			}
 944		}		
 945	}
 946	$r=db_fetch_array(db_select("select box2 from grupper where art='OreDif'",__FILE__ . " linje " . __LINE__));
 947	($difkto=$r['box2'])?$afrundet=pos_afrund($sum):$afrundet=0;
 948	print "<tr><td colspan=\"5\"><hr></td></tr>\n";
 949	if ($sum) print "<tr><td>I alt</td><td align=\"right\"></td><td></td><td align=\"right\"></td><td align=\"right\">".dkdecimal($sum)."</td></tr>\n";
 950	if ($afrundet) print "<tr><td>Afrundet</td><td align=\"right\"></td><td></td><td align=\"right\"></td><td align=\"right\">".dkdecimal($afrundet)."</td></tr>\n";
 951return($sum);
 952
 953}
 954
 955function pos_txt_print($id,$betaling,$betaling2,$modtaget,$modtaget2,$indbetaling) {
 956
 957#cho "$id,$betaling,$betaling2,$modtaget,$modtaget2,$indbetaling<br>";
 958
 959	global $db;
 960	global $db_id;
 961	global $brugernavn;
 962	global $bruger_id;
 963	global $momssats;
 964	global $db_encode;
 965
 966#	$udskriv_bon=1;
 967 	include("../includes/ConvertCharset.class.php");
 968	if ($db_encode=="UTF8") $FromCharset = "UTF-8";
 969	else $FromCharset = "iso-8859-15";
 970	$ToCharset = "cp865";
 971	$convert = new ConvertCharset();
 972
 973	$pfnavn="../temp/".$db."/".$bruger_id.".txt";
 974	$fp=fopen("$pfnavn","w");
 975	$r=db_fetch_array(db_select("select * from adresser where art = 'S'",__FILE__ . " linje " . __LINE__));
 976	$firmanavn=$r['firmanavn'];
 977	$addr1=$r['addr1'];
 978	$addr2=$r['addr2'];
 979	$postnr=$r['postnr'];
 980	$bynavn=$r['bynavn'];
 981	$tlf=$r['tlf'];
 982	$cvrnr=$r['cvrnr'];
 983
 984	$belob="beløb";
 985	if ($firmanavn) $firmanavn = $convert ->Convert($firmanavn, $FromCharset, $ToCharset);
 986	if ($addr1) $addr1 = $convert ->Convert($addr1, $FromCharset, $ToCharset);
 987	if ($addr2) $addr2 = $convert ->Convert($addr2, $FromCharset, $ToCharset);
 988	if ($bynavn) $bynavn = $convert ->Convert($bynavn, $FromCharset, $ToCharset);
 989	if ($tlf) $tlf = $convert ->Convert($tlf, $FromCharset, $ToCharset);
 990	if ($cvrnr) $cvrnr = $convert ->Convert($cvrnr, $FromCharset, $ToCharset);
 991
 992	if ($belob) $belob = $convert ->Convert($belob, $FromCharset, $ToCharset);
 993
 994	$r=db_fetch_array(db_select("select * from ordrer where id = '$id'",__FILE__ . " linje " . __LINE__));
 995	$konto_id=$r['konto_id'];
 996	$kontonr=$r['kontonr'];
 997	$kundenavn=$r['firmanavn'];
 998	$kundeaddr1=$r['addr1'];
 999	$kundepostnr=$r['postnr'];
1000	$kundeby=$r['bynavn'];
1001	$kundeordnr=$r['kundeordnr'];
1002	$fakturadate=$r['fakturadate'];
1003	$fakturanr=$r['fakturanr'];
1004	$betalingsbet=$r['betalingsbet'];
1005	$fakturadato=dkdato($r['fakturadate']);
1006	$sum=$r['sum'];
1007	$moms=$r['moms'];
1008	$momssats=$r['momssats'];
1009	$betaling=$r['felt_1'];
1010	$modtaget=$r['felt_2']*1;
1011	$betaling2=$r['felt_3'];
1012	$modtaget2=$r['felt_4']*1;
1013	$betalt=$modtaget+$modtaget2;
1014	$ref=$r['ref'];
1015	$kasse=$r['felt_5'];
1016	$tidspkt=$r['tidspkt'];
1017	$dkdato=dkdato(substr($tidspkt,0,10));
1018	$tid=substr($tidspkt,-5);
1019	if (!$tid) $tid=date("H:i");
1020	if (!$betaling) $betaling="Betalt";
1021	if ($ref) {
1022		if ($r=db_fetch_array(db_select("select ansat_id from brugere where brugernavn = '$ref'",__FILE__ . " linje " . __LINE__))) {
1023			$ansat_id=$r['ansat_id']*1;
1024			if ($r=db_fetch_array(db_select("select navn from ansatte where id = '$ansat_id'",__FILE__ . " linje " . __LINE__))) $ref=$r['navn'];
1025	  }
1026	}
1027	if ($kundenavn) $kundenavn = $convert ->Convert($kundenavn, $FromCharset, $ToCharset);
1028	if ($kundeaddr1) $kundeaddr1 = $convert ->Convert($kundeaddr1, $FromCharset, $ToCharset);
1029	if ($kundeby) $kundeby = $convert ->Convert($kundeby, $FromCharset, $ToCharset);
1030	if ($ref) $ref = $convert ->Convert($ref, $FromCharset, $ToCharset);
1031
1032	$x=0;
1033	$q=db_select("select * from ordrelinjer where ordre_id = '$id' and posnr > 0 order by posnr",__FILE__ . " linje " . __LINE__);
1034	while($r=db_fetch_array($q)) {
1035		$x++;
1036		if ($r['momsfri']!='on') $pris=$r['pris']+$r['pris']/100*$momssats;
1037		else $pris=$r['pris'];
1038		if (strtoupper($r['varenr'])=='INDBETALING') {
1039			$pris=$pris*-1;
1040			$sum=$sum*-1;
1041		}
1042		$rabat[$x]=$r['rabat']*1;
1043		$rabatart[$x]=$r['rabatart'];
1044		$beskrivelse[$x]=$r['beskrivelse'];
1045		if ($beskrivelse[$x]) $beskrivelse[$x]= $convert ->Convert($beskrivelse[$x], $FromCharset, $ToCharset);
1046		$antal[$x]=$r['antal']*1;
1047		$dkkpris[$x]=dkdecimal($pris*$antal[$x]);
1048		while(strlen($dkkpris[$x])<9){
1049			$dkkpris[$x]=" ".$dkkpris[$x];
1050		}
1051		while(strlen($antal[$x])<3){
1052			$antal[$x]=" ".$antal[$x];
1053		}
1054		if (strlen($beskrivelse[$x])>26) $beskrivelse[$x]=substr($beskrivelse[$x],0,25);
1055		while(strlen($beskrivelse[$x])<26){
1056			$beskrivelse[$x]=$beskrivelse[$x]." ";
1057		}
1058		if ($rabat[$x]) {
1059			$y=$x;
1060			$x++;
1061			$antal[$x]=$antal[$y];
1062			if ($rabatart[$y]=='amount') {
1063				$beskrivelse[$x]="Rabat";
1064				$pris=$rabat[$y]*-1;
1065			} else {
1066				$beskrivelse[$x]="Rabat ".$rabat[$y]."%";
1067				$pris=$r['pris']/100*$rabat[$y]*-1;
1068			}
1069			if ($r['momsfri']!='on') $pris+=$pris/100*$momssats;
1070			$dkkpris[$x]=dkdecimal($pris*$r['antal']);
1071			while(strlen($dkkpris[$x])<9){
1072				$dkkpris[$x]=" ".$dkkpris[$x];
1073			}
1074			while(strlen($antal[$x])<3){
1075				$antal[$x]=" ".$antal[$x];
1076			}
1077			if (strlen($beskrivelse[$x])>26) $beskrivelse[$x]=substr($beskrivelse[$x],0,25);
1078			while(strlen($beskrivelse[$x])<26){
1079				$beskrivelse[$x]=$beskrivelse[$x]." ";
1080			}
1081		}
1082		$linjeantal=$x;
1083	}
1084	$sum+=$moms;
1085	if ($konto_id) {
1086		if (!$x) $indbetaling=$sum;
1087		$gl_saldo=dkdecimal($betaling2);
1088		$ny_saldo=dkdecimal($modtaget2);
1089	}
1090	if ($indbetaling) $retur=$modtaget-$indbetaling;
1091	else $retur=$betalt-$sum;
1092	$dkksum=dkdecimal($sum);
1093	while(strlen($dkksum)<9){
1094		$dkksum=" ".$dkksum;
1095	}
1096	$dkkretur=dkdecimal($retur);
1097	while(strlen($dkkretur)<9){
1098		$dkkretur=" ".$dkkretur;
1099	}
1100	$betalt=dkdecimal($betalt);
1101	while(strlen($betalt)<9){
1102		$betalt=" ".$betalt;
1103	}
1104	while(strlen($betaling)<19){
1105		$betaling=$betaling." ";
1106	}
1107	while(strlen($betaling2)<19){
1108		$betaling2=$betaling2." ";
1109	}
1110	$dkkmodtaget=dkdecimal($modtaget);
1111	while(strlen($dkkmodtaget)<9){
1112		$dkkmodtaget=" ".$dkkmodtaget;
1113	}
1114	if ($modtaget2) {
1115		$dkkmodtaget2=dkdecimal($dkkmodtaget2);
1116		while(strlen($dkkmodtaget2)<9){
1117			$dkkmodtaget2=" ".$dkkmodtaget2;
1118		}
1119	}
1120	$dkksum=dkdecimal($sum);
1121	while(strlen($dkksum)<9){
1122		$dkksum=" ".$dkksum;
1123	}
1124	$dkkmoms=dkdecimal($moms);
1125	while(strlen($dkkmoms)<9){
1126		$dkkmoms=" ".$dkkmoms;
1127	}
1128	$filnavn="pos_print/pos_print_".$db_id.".php";
1129	if (file_exists("$filnavn")) include("$filnavn");
1130	else include("pos_print/pos_print.php");
1131	fclose($fp);
1132}
1133
1134function opdater_konto($konto_id,$kontonr,$id) {
1135#Opdaterer kontoinformation på ordren
1136	global $kasse;
1137	global $kundeordnr;
1138	if (!$id) $id=opret_ordre(0,$kasse);
1139	$konto_id*=1;
1140	$kontonr*=1;
1141	$r=db_fetch_array(db_select("select status from ordrer where id = '$id'",__FILE__ . " linje " . __LINE__));
1142	$status=$r['status'];
1143	if ($status < 3 && ($konto_id || $kontonr)) {
1144		if ($konto_id) $r=db_fetch_array(db_select("select * from adresser where id = '$konto_id'",__FILE__ . " linje " . __LINE__));
1145		else $r=db_fetch_array(db_select("select * from adresser where kontonr = '$kontonr' and art = 'D'",__FILE__ . " linje " . __LINE__));
1146		if ($konto_id=$r['id']) {
1147			if ($r['lukket']) {
1148				$betalingsbet='Kontant';
1149				$betalingsdage='0';
1150			} else {
1151				($r['betalingsbet'])?$betalingsbet=$r['betalingsbet']:$betalingsbet='Kontant';
1152				$betalingsdage=$r['betalingsdage']*1;
1153			}
1154		  db_modify ("update ordrer set konto_id='$konto_id', kontonr='$r[kontonr]',firmanavn='$r[firmanavn]',addr1='$r[addr1]',addr2='$r[addr2]',
1155					postnr='$r[postnr]',bynavn='$r[bynavn]',land='$r[land]',betalingsdage='$betalingsdage',betalingsbet='$betalingsbet',cvrnr='$r[cvrnr]',
1156			ean='$r[ean]',institution='$r[institution]',email='$r[email]',kontakt='$r[kontakt]',art='PO',valuta='DKK',valutakurs='100' where id = '$id'",__FILE__ . " linje " . __LINE__);
1157	  }
1158	}
1159	return($id);
1160} # endfunc opdater_konto()
1161
1162
1163function hoved($kasse) {
1164	global $regnskab;
1165	global $brugernavn;
1166	global $bruger_id;
1167	global $id;
1168	global $db;
1169	global $db_id;
1170	global $bon;
1171	global $sum;
1172	global $returside;
1173
1174	if ($kasse=="?") find_kasse($kasse);
1175	$x=0;
1176	$q=db_select("select brugernavn from brugere order by brugernavn",__FILE__ . " linje " . __LINE__);
1177	while ($r=db_fetch_array($q)) {
1178		$x++;
1179		$loginnavn[$x]=$r['brugernavn'];
1180	}
1181	$brugerantal=$x;
1182	print "<tr>";
1183	print "<td width=\"96%\" height=\"25px\" valign=\"bottom\"><b>$regnskab</b> Kasse: <a href=pos_ordre.php?id=$id&kasse=?>$kasse</a> | ";
1184	print "<a href=pos_ordre.php?id=$id&kasse=$kasse&kassebeholdning=on>Kasseopt&aelig;lling</a><br>";
1185	print "Ekspedient<select class=\"inputbox\" NAME=\"ny_bruger\">";
1186	print "<option>$brugernavn</option>";
1187	for ($x=1;$x<=$brugerantal;$x++) {
1188		if ($loginnavn[$x] != $brugernavn) print "<option>$loginnavn[$x]</option>";
1189	}
1190	print "</option>";
1191	if ($status>=3 && !$bon && $id) {
1192		$r=db_fetch_array($q=db_select("select fakturanr from ordrer where id = '$id'",__FILE__ . " linje " . __LINE__));
1193		$bon=$r['fakturanr'];
1194	}
1195
1196	print "<input class=\"inputbox\" type=\"password\" size=\"10\" name=\"kode\" value=\"        \">";
1197	print "<span title=\"Skriv bon nummeret på den bon som skal genkaldes elles 'S' for sidste bon fra denne kasse\">  | Bon <input class=\"inputbox\" type=\"text\" name=\"bon\" size=\"6\" value=\"$bon\"></span><br><hr></td>";
1198	print "<td width=\"4%\" align=\"right\" valign=\"top\"><a href='$returside'><div class=\"luk\"></div></a></td></tr>\n";
1199	print "</tr>\n";
1200}
1201
1202/*
1203	function hoved($kasse) {
1204	global $regnskab;
1205	global $brugernavn;
1206	global $id;
1207	global $db;
1208	global $db_id;
1209
1210	if ($kasse=="?") find_kasse($kasse);
1211	print "<tr>";
1212	print "<td width=\"96%\" height=\"25px\" valign=\"bottom\"><b>$regnskab</b> Kasse: <a href=pos_ordre.php?id=$id&kasse=?>$kasse</a> | ";
1213	print "Ekspedient: <a href=../includes/relogin.php?regnskab=$regnskab&bruger_id=$bruger_id&db_$db&db_id=$db_id>$brugernavn</a> | ";
1214	print "<a href=pos_ordre.php?id=$id&kasse=$kasse&kassebeholdning=on>Kasseopt&aelig;lling</a><br><hr></td>";
1215	print "<td width=\"4%\" align=\"right\" valign=\"top\"><a href=../includes/luk.php><div class=\"luk\"></div></a></td></tr>\n";
1216
1217
1218print "</tr>\n";
1219
1220}
1221*/
1222function find_kasse($kasse) {
1223
1224      if ($kasse!="?" && isset($_COOKIE['saldi_pos'])) {
1225		return(stripslashes($_COOKIE['saldi_pos']));
1226	} else {
1227		print "<form name=pos_ordre action=pos_ordre.php?kasse=opdat method=post>";
1228		$r=db_fetch_array(db_select("select * from grupper where art = 'POS'",__FILE__ . " linje " . __LINE__));
1229		$kasseantal=$r['box1']*1;
1230		print "V&aelig;lg kasse<SELECT NAME=kasse>";
1231		for($x=1;$x<=$kasseantal;$x++) {
1232			print	"<option value=\"$x\">$x</option>";
1233		}
1234		print "</SELECT></td>";;
1235		print "<INPUT TYPE=\"submit\" NAME=\"submit\"VALUE=\"OK\">";
1236		print "</form>";
1237	}
1238	exit;
1239}
1240
1241function tastatur($status) {
1242
1243	global $id;
1244	global $bon;
1245	global $fokus;
1246	global $sum;
1247	global $modtaget;
1248	global $modtaget2;
1249	global $kontonr;
1250	global $varelinjer;
1251	global $varenr_ny;
1252	global $indbetaling;
1253	global $betalingsbet;
1254
1255	$sum=afrund($sum,2);
1256	$modtaget=afrund($modtaget,2);
1257	$modtaget2=afrund($modtaget2,2);
1258
1259	$r = db_fetch_array(db_select("select * from grupper where art = 'POS' and kodenr = '1'",__FILE__ . " linje " . __LINE__));
1260	$kortantal=$r['box4']*1;
1261	$korttyper=explode(chr(9),$r['box5']);
1262	$vis_kontoopslag=$r['box11'];
1263	$vis_hurtigknap=$r['box12'];
1264	$vis_indbetaling=$r['box14'];
1265	$timeout=$r['box13']*1;
1266
1267	print "<input type=hidden name=\"sum\" value=\"$sum\">";
1268	print "<input type=hidden name=\"kontonr\" value=\"$kontonr\">";
1269
1270	print "<TR><TD height=\"100%\" valign=\"top\"  align=\"center\"><TABLE BORDER=\"0\" CELLPADDING=\"4\" CELLSPACING=\"4\"><TBODY>";
1271	print "<TR>";
1272	if ($status < 3) {
1273		$stil="STYLE=\"width: 4.5em;height: 2em;font-size:150%;\"";
1274		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"one\"   VALUE=\"1\" OnClick=\"pos_ordre.$fokus.value += '1';pos_ordre.$fokus.focus();\"></TD>";
1275		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"two\"   VALUE=\"2\" OnCLick=\"pos_ordre.$fokus.value += '2';pos_ordre.$fokus.focus();\"></TD>";
1276		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"three\" VALUE=\"3\" OnClick=\"pos_ordre.$fokus.value += '3';pos_ordre.$fokus.focus();\"></TD>";
1277		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"plus\"  VALUE=\"+\" OnClick=\"pos_ordre.$fokus.value += '+';pos_ordre.$fokus.focus();\"></TD>";
1278		print "</TR><TR>";
1279		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"four\"  VALUE=\"4\" OnClick=\"pos_ordre.$fokus.value += '4';pos_ordre.$fokus.focus();\"></TD>";
1280		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"five\"  VALUE=\"5\" OnCLick=\"pos_ordre.$fokus.value += '5';pos_ordre.$fokus.focus();\"></TD>";
1281		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"six\"   VALUE=\"6\" OnClick=\"pos_ordre.$fokus.value += '6';pos_ordre.$fokus.focus();\"></TD>";
1282		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"minus\" VALUE=\"-\" OnClick=\"pos_ordre.$fokus.value += '-';pos_ordre.$fokus.focus();\"></TD>";
1283		print "</TR><TR>";
1284		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"seven\" VALUE=\"7\" OnClick=\"pos_ordre.$fokus.value += '7';pos_ordre.$fokus.focus();\"></TD>";
1285		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"eight\" VALUE=\"8\" OnCLick=\"pos_ordre.$fokus.value += '8';pos_ordre.$fokus.focus();\"></TD>";
1286		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"nine\"  VALUE=\"9\" OnClick=\"pos_ordre.$fokus.value += '9';pos_ordre.$fokus.focus();\"></TD>";
1287		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"times\" VALUE=\"x\" OnClick=\"pos_ordre.$fokus.value += '*'\"></TD>";
1288		print "</TR><TR>";
1289		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"zero\"  VALUE=\",\" OnClick=\"pos_ordre.$fokus.value += ',';pos_ordre.$fokus.focus();\"></TD>";
1290		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"zero\"  VALUE=\"0\" OnClick=\"pos_ordre.$fokus.value += '0';pos_ordre.$fokus.focus();\"></TD>";
1291		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"DoIt\"  VALUE=\"=\" OnClick=\"pos_ordre.$fokus.value = eval(pos_ordre.$fokus.value);pos_ordre.$fokus.focus();\"></TD>";
1292		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"div\"   VALUE=\"/\" OnClick=\"pos_ordre.$fokus.value += '/';pos_ordre.$fokus.focus();\"></TD>";
1293		print "</TR><TR>";
1294		print "<TD><INPUT TYPE=\"button\" $stil NAME=\"clear\" VALUE=\"Ryd\" OnClick=\"pos_ordre.$fokus.value = '';pos_ordre.$fokus.focus();\"></TD>";
1295		if ($id) {
1296			print "<TD><INPUT TYPE=\"submit\" $stil NAME=\"afslut\"VALUE=\"Afslut\" OnClick=\"pos_ordre.$fokus.value += 'a';pos_ordre.$fokus.focus();\"></TD>";
1297			print "<TD onclick=\"return confirm('Slet alt og start forfra')\"><INPUT TYPE=\"submit\" $stil NAME=\"forfra\"VALUE=\"Forfra\" OnClick=\"pos_ordre.$fokus.value += 'f';pos_ordre.$fokus.focus();\"></TD>";
1298		} else print "<TD COLSPAN=\"2\"></TD>";
1299		if ($fokus=='modtaget') {
1300			print "<TD onclick=\"return confirm('Tilbage til varescanning')\"><INPUT TYPE=\"submit\" $stil NAME=\"tilbage\"VALUE=\"Tilbage\" OnClick=\"pos_ordre.$fokus.value += 't';pos_ordre.$fokus.focus();\"></TD>";
1301			print "</TR><TR>";
1302			print "<TD COLSPAN=\"3\"></TD>";
1303		}
1304		print "<TR><TD COLSPAN=\"4\"><HR></TD></tr>\n";
1305		$stil2="STYLE=\"width: 9.5em;height: 2em;font-size:150%;\"";
1306		print "<TR>";
1307#cho "$fokus=='modtaget' && $modtaget>=$sum && !$indbetaling<br>";
1308		if ($fokus=='varenr_ny') print "<TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"varer\"VALUE=\"Varer\" OnClick=\"pos_ordre.$fokus.value += 'v';pos_ordre.$fokus.focus();\"></TD>";
1309		elseif ($fokus=='antal_ny' || $fokus=='pris_ny') { #20130310 Tilføjet: || $fokus=='pris_ny' 
1310			if ($fokus=='antal_ny') print "<TD COLSPAN=\"1\"><INPUT TYPE=\"submit\" $stil NAME=\"pris\"VALUE=\"Pris\" OnClick=\"pos_ordre.$fokus.value += 'p';pos_ordre.$fokus.focus();\"></TD>";
1311			else print "<TD COLSPAN=\"1\"></TD>";
1312			print "<TD COLSPAN=\"1\"><INPUT TYPE=\"submit\" $stil NAME=\"rabat\"VALUE=\"Rabat\" OnClick=\"pos_ordre.$fokus.value += 'r';pos_ordre.$fokus.focus();\"></TD>";
1313		} elseif ($fokus=='modtaget' && $modtaget>=$sum && !$indbetaling && $betalingsbet != 'Kontant') {
1314			print "<TR><TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"betaling\" VALUE=\"Konto\" OnClick=\"pos_ordre.$fokus.value += 'k';pos_ordre.$fokus.focus();\"></TD>";
1315		} elseif ($fokus=='modtaget2' && $modtaget+$modtaget2>=$sum && !$indbetaling) {
1316			print "<TR><TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"betaling2\" VALUE=\"Konto\" OnClick=\"pos_ordre.$fokus.value += 'k';pos_ordre.$fokus.focus();\"></TD>";
1317		}	elseif ($indbetaling && $modtaget >= $indbetaling) {
1318			print "<TR><TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"betaling\" VALUE=\"Kontant\" OnClick=\"pos_ordre.$fokus.value += 'c';pos_ordre.$fokus.focus();\"></TD>";
1319		} else print "<TD colspan=2></TD>";
1320		print "<TD colspan=2><INPUT TYPE=\"submit\" $stil2 NAME=\"OK\"  VALUE=\"Enter\"></TD></tr>\n";
1321		if ($vis_hurtigknap && $fokus=='antal_ny') print "<TR><TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"betaling\" VALUE=\"Kontant p&aring; bel&oslash;b\" OnClick=\"pos_ordre.$fokus.value += 'c';pos_ordre.$fokus.focus();\"></TD>";
1322		if ($vis_kontoopslag && !$varenr_ny && !$indbetaling) print "<TR><TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"kontoopslag\" VALUE=\"Kontoopslag\"></TD></tr>\n";
1323		if ((($fokus=='modtaget' || $fokus=='modtaget2') && (!$kontonr || $betalingsbet=='Kontant')) || ($indbetaling && $modtaget>=$indbetaling && $kontonr)) {
1324			for($x=0;$x<$kortantal;$x++) {
1325				($fokus=='modtaget2')?$tmp="betaling2":$tmp="betaling";
1326#cho "beta $tmp<br>";
1327				print "<TR><TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=$tmp VALUE=\"$korttyper[$x]\" OnClick=\"pos_ordre.$fokus.value += 'd';pos_ordre.$fokus.focus();\"></TD></tr>\n";
1328			}
1329			if (!$indbetaling) {
1330				if ($fokus=='modtaget2') $tmp="betaling2";
1331				else $tmp="betaling";
1332				print "<TR><TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=$tmp VALUE=\"Kontant\" OnClick=\"pos_ordre.$fokus.value += 'c';pos_ordre.$fokus.focus();\"></TD></tr>\n";
1333			}
1334#			print "<TR><TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"betaling\" VALUE=\"Konto\" OnClick=\"pos_ordre.$fokus.value += 'k';pos_ordre.$fokus.focus();\"></TD></tr>\n";
1335		} elseif ($id && $kontonr && !$varelinjer && !$indbetaling)
1336		if ($vis_indbetaling) print "<TR><TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"indbetaling\" VALUE=\"Indbetaling\" OnClick=\"pos_ordre.$fokus.value += 'i';pos_ordre.$fokus.focus();\"></TD>";
1337	} else {
1338#		print "<input type=\"hidden\" name=\"bon\" value = \"\">";
1339		$stil2="STYLE=\"width: 9.5em;height: 2em;font-size:150%;\"";
1340		print "<TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"udskriv\"VALUE=\"Udskriv\"></TD>";
1341		print "<TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"ny\"VALUE=\"Ny kunde\"></TD></TR>";
1342		print "<TD COLSPAN=\"4\"><br></TD></TR>";
1343		print "<TD COLSPAN=\"4\"><br></TD></TR>";
1344		print "<TD COLSPAN=\"4\" align=\"center\"><INPUT TYPE=\"submit\" $stil2 NAME=\"krediter\"VALUE=\"Korrektion\"></TD>";
1345#		print "<TD COLSPAN=\"2\"><INPUT TYPE=\"submit\" $stil2 NAME=\"ny\"VALUE=\"Ny kunde\"></TD>";
1346		if ($timeout && !$bon) print "<meta http-equiv=\"refresh\" content=\"$timeout;URL=pos_ordre.php?id=0\">";
1347	}
1348	print "</tr>\n";
1349	print "</TBODY></TABLE></TD></tr>\n";
1350}
1351
1352function menubuttons($id,$menu_id,$vare_id) {
1353	global $bgcolor2;
1354	global $fokus;
1355	global $pris_ny;
1356	global $varenr_ny;
1357
1358	$tid=date("H:i");
1359	if (!$menu_id) {
1360	$r=db_fetch_array(db_select("select kodenr from grupper where art='POSBUT' and (box7 < box8) and (box7<='$tid' and box8>='$tid')",__FILE__ . " linje " . __LINE__));
1361		$menu_id=$r['kodenr'];
1362	if (!$menu_id) { #her tages højde for at slut tidspkt kan være mindre en starttidspkt
1363		$r=db_fetch_array(db_select("select kodenr from grupper where art='POSBUT' and (box7 > box8) and ((box7>='$tid' and box8>='$tid') or (box7<='$tid' and box8<='$tid'))",__FILE__ . " linje " . __LINE__));
1364		$menu_id=$r['kodenr'];
1365	}
1366	}
1367
1368	$q=db_select("select * from grupper where art='POSBUT' and kodenr='$menu_id'",__FILE__ . " linje " . __LINE__);
1369	$r = db_fetch_array($q);
1370	$menuid=$r['kodenr'];
1371	$beskrivelse=$r['box1'];
1372	$cols=$r['box2'];
1373	$rows=$r['box3'];
1374	$height=$r['box4'];
1375	$width=$r['box5'];
1376	$fontsize=$r['box10'];
1377	if (!$fontsize) $fontsize=$height*$width/200;
1378/*
1379	print "<style type=\"text/css\">";
1380	print "table a {display:block;width:100%;height:100%;}";
1381	print "</style>";
1382*/
1383#print "
1384#";
1385	print "<table border=\"0\" cellspacing=\"5\" cellpadding=\"1\"><tbody>"; # table 1 ->
1386	print "<tr><td colspan=\"$cols\" align=\"center\" bgcolor=\"$bgcolor2\">$beskrivelse</td></tr>";
1387
1388for ($x=1;$x<=$rows;$x++) {
1389	print "<tr>";
1390	for ($y=1;$y<=$cols;$y++) {
1391#		menu_id,row,col,beskrivelse,color,funktion,vare_id,colspan,rowspan
1392		$r=db_fetch_array(db_select("select * from pos_buttons where menu_id=$menuid and row='$x' and col='$y'",__FILE__ . " linje " . __LINE__));
1393		$a=$r['beskrivelse'];
1394		$b=$r['color'];
1395		$c=$r['vare_id']*1;
1396		$d=$r['funktion']*1;
1397		if ($a) {
1398			$knap="<input type=\"button\" style=\"width:".$width."px;height:".$height."px;text-align:center;font-size:".$fontsize."px; background-color:#$b;\" value= \"$a\">";
1399			print "<td>";
1400#			($fokus='antal')?$vnr=varenr_ny=$varenr_ny:$vnr=varenr_ny=$varenr_ny;
1401			if (!$d || $d==1) print "<a style=\"text-decoration:none\" href=pos_ordre.php?id=$id&menu_id=$menu_id&vare_id=$vare_id&vare_id_ny=$c&varenr_ny=$varenr_ny&pris_ny=$pris_ny&fokus=$fokus>$knap</a>";
1402			elseif ($d==2) print "<a style=\"text-decoration:none\" href=pos_ordre.php?id=$id&vare_id=$vare_id&menu_id=$c&varenr_ny=$varenr_ny&pris_ny=$pris_ny&fokus=$fokus>$knap</a>";
1403			elseif ($d==3) print "<a style=\"text-decoration:none\" href=pos_ordre.php?id=$id&konto_id=$c&varenr_ny=$varenr_ny&pris_ny=$pris_ny&fokus=$fokus>$knap</a>";
1404			elseif ($d==4) print "<a style=\"text-decoration:none\" href=pos_ordre.php?id=$id&spec_func=spec_$c&varenr_ny=$varenr_ny&pris_ny=$pris_ny&fokus=$fokus>$knap</a>";
1405			print "</td>";
1406		}
1407	}
1408	print "</tr>";
1409}
1410print "</tbody></table>"; # <- table 1
1411} # function menubuttons
1412
1413function fejl ($id,$fejltekst) {
1414  print "<BODY onLoad=\"javascript:alert('$fejltekst')\">";
1415  print "<meta http-equiv=\"refresh\" content=\"0;URL=$php_self\">";
1416
1417}
1418
1419function posbogfor ($kasse) {
1420
1421	$x=0;
1422#cho "select distinct(fakturadate) as fakturadate from ordrer where felt_5='$kasse' and konto_id= '0' and art = 'PO' and status='3' order by fakturadate<br>";
1423	$q=db_select("select distinct(fakturadate) as fakturadate from ordrer where felt_5='$kasse' and (konto_id='0' or betalingsbet='Kontant') and art = 'PO' and status='3' order by fakturadate",__FILE__ . " linje " . __LINE__);
1424	while ($r=db_fetch_array($q)) {
1425		if ($r['fakturadate']) {
1426			$x++;
1427			$fakturadate[$x]=$r['fakturadate'];
1428		}
1429	}
1430	$x=0;
1431#cho "select distinct(felt_1) as betaling from ordrer where felt_5='$kasse' and konto_id= '0' and art = 'PO' and status='3' order by felt_1<br>";
1432	$q=db_select("select distinct(felt_1) as betaling from ordrer where felt_5='$kasse' and (konto_id='0' or betalingsbet='Kontant') and art = 'PO' and status='3' order by felt_1",__FILE__ . " linje " . __LINE__);
1433	while ($r=db_fetch_array($q)) {
1434		if ($r['betaling']) {
1435			$x++;
1436			$betaling[$x]=$r['betaling'];
1437		}
1438	}
1439	$x=0;
1440#cho "select distinct(felt_3) as betaling2 from ordrer where felt_5='$kasse' and konto_id= '0' and art = 'PO' and status='3' order by felt_3<br>";
1441	$q=db_select("select distinct(felt_3) as betaling2 from ordrer where felt_5='$kasse' and (konto_id='0' or betalingsbet='Kontant') and art = 'PO' and status='3' order by felt_3",__FILE__ . " linje " . __LINE__);
1442	while ($r=db_fetch_array($q)) {
1443		if ($r['betaling2']) {
1444			$x++;
1445			$betaling2[$x]=$r['betaling2'];
1446		}
1447	}
1448	for ($x=1;$x<=count($fakturadate);$x++) {
1449#cho "X $x<br>";
1450		for ($y=0;$y<=count($betaling);$y++) {
1451#cho "Y $y<br>";
1452			for ($z=0;$z<=count($betaling2);$z++) {
1453#cho "Z $z<br>";
1454				$id=NULL;
1455#cho "select id from ordrer where felt_5='$kasse' and fakturadate='$fakturadate[$x]' and felt_1='$betaling[$y]' and felt_3='$betaling2[$z]' and konto_id= '0' and art = 'PO' and status='3'<br>";
1456				$q=db_select("select id from ordrer where felt_5='$kasse' and fakturadate='$fakturadate[$x]' and felt_1='$betaling[$y]' and felt_3='$betaling2[$z]' and (konto_id='0' or betalingsbet='Kontant') and art = 'PO' and status='3'",__FILE__ . " linje " . __LINE__);
1457				while ($r=db_fetch_array($q)) {
1458					if($id) $id.=",".$r['id'];
1459					else $id=$r['id'];
1460				}
1461				$r = db_fetch_array(db_select("select box9 from grupper where art='POS' and kodenr='1'",__FILE__ . " linje " . __LINE__));
1462				if($r['box9'] && $id) {
1463					transaktion('begin');
1464					bogfor_nu("$id","");
1465					transaktion('commit');
1466				}
1467			}
1468		}
1469	}
1470}
1471
1472function kassebeholdning ($kasse) {
1473	global $bruger_id;
1474	global $db;
1475	global $db_encode;
1476	$dd=date("Y-m-d");
1477	$tid=date("H:m");
1478
1479	posbogfor($kasse);
1480
1481 	include("../includes/ConvertCharset.class.php");
1482	if ($db_encode=="UTF8") $FromCharset = "UTF-8";
1483	else $FromCharset = "iso-8859-15";
1484	$ToCharset = "cp865";
1485	$convert = new ConvertCharset();
1486
1487	$pfnavn="../temp/".$db."/".$bruger_id.".txt";
1488	$fp=fopen("$pfnavn","w");
1489	$kassopgorelse="KASSEOPGØRELSE";
1490	$tmp = $convert ->Convert($kassopgorelse, $FromCharset, $ToCharset);
1491
1492	fwrite($fp,"\n\n$tmp\n\n");
1493	fwrite($fp,"Den $dd kl. $tid\n");
1494	fwrite($fp,"Kasse nr: $kasse\n");
1495
1496	$r = db_fetch_array(db_select("select * from grupper where art = 'POS' and kodenr = '1'",__FILE__ . " linje " . __LINE__));
1497	$kassekonti=explode(chr(9),$r['box2']);
1498	$kortantal=$r['box4']*1;
1499	$kortnavn=explode(chr(9),$r['box5']);
1500	$kortkonto=explode(chr(9),$r['box6']);
1501	$straksbogfor=$r['box9'];
1502
1503	$kasse--;
1504	$r=db_fetch_array(db_select("select sum(debet) as debet,sum(kredit) as kredit from transaktioner where transdate < '$dd' and kontonr = '$kassekonti[$kasse]'",__FILE__ . " linje " . __LINE__));
1505	$byttepenge=$r['debet']-$r['kredit'];
1506	$r = db_fetch_array(db_select("select sum(debet) as debet,sum(kredit) as kredit from transaktioner where transdate = '$dd' and kontonr = '$kassekonti[$kasse]'",__FILE__ . " linje " . __LINE__));
1507	$tilgang=$r['debet']-$r['kredit'];
1508
1509	if (!$straksbogfor) {
1510#		$r=db_fetch_array(db_select("select sum(ordrelinjer.pris) as pris from ordrelinjer,ordrer where  ordrer.status = '3' and ordrer.art = 'PO' and ordrer.fakturadate < '$dd' and ordrelinjer.ordre_id=ordrer.id and ordrelinjer.bogf_konto = '$kassekonti[$kasse]'",__FILE__ . " linje " . __LINE__));
1511		$r=db_fetch_array(db_select("select sum(sum+moms) as sum from ordrer where  status = '3' and art = 'PO' and fakturadate < '$dd'",__FILE__ . " linje " . __LINE__));
1512		$byttepenge+=$r['pris'];
1513#		$r=db_fetch_array(db_select("select sum(ordrelinjer.pris) as pris from ordrelinjer,ordrer where  ordrer.status = '3' and ordrer.art = 'PO' and ordrer.fakturadate = '$dd' and ordrelinjer.ordre_id=ordrer.id and ordrelinjer.bogf_konto = '$kassekonti[$kasse]'",__FILE__ . " linje " . __LINE__));
1514		$r=db_fetch_array(db_select("select sum(sum+moms) as sum from ordrer where  status = '3' and art = 'PO' and fakturadate = '$dd'",__FILE__ . " linje " . __LINE__));
1515		$tilgang+=$r['sum'];
1516	}
1517
1518	if ($kortantal) {
1519#		$kortsum[]=0;
1520		fwrite($fp,"\n\nSalg paa kort\n\n");
1521		for ($x=0;$x<$kortantal;$x++) {
1522			if ($kortkonto[$x]) {
1523				$r = db_fetch_array(db_select("select sum(debet) as debet,sum(kredit) as kredit from transaktioner where transdate = '$dd' and kontonr = '$kortkonto[$x]'",__FILE__ . " linje " . __LINE__));
1524				$kortsum[$x]=dkdecimal($r['debet']-$r['kredit']);
1525				if (!$straksbogfor) {
1526#cho "select sum(ordrelinjer.pris) as pris from ordrelinjer,ordrer where  ordrer.status = '3' and ordrer.art = 'PO' and ordrer.fakturadate < '$dd' and ordrelinjer.ordre_id=ordrer.id and ordrelinjer.bogf_konto = '$kortkonto[$x]'<br>";
1527#					$r=db_fetch_array(db_select("select sum(sum+moms) as sum from ordrer where  status = '3' and art = 'PO' and fakturadate = '$dd'",__FILE__ . " linje " . __LINE__));
1528#					$totalsum+=$r['pris'];
1529					$q=db_select("select felt_2 from ordrer where  status = '3' and art = 'PO' and fakturadate < '$dd' and felt_1='$kortnavn[$x]'",__FILE__ . " linje " . __LINE__);
1530					while ($r=db_fetch_array($q)) $byttepenge-=$r['felt_2'];
1531					$q=db_select("select felt_4 from ordrer where  status = '3' and art = 'PO' and fakturadate < '$dd' and felt_3='$kortnavn[$x]'",__FILE__ . " linje " . __LINE__);
1532					while ($r=db_fetch_array($q)) $byttepenge-=$r['felt_4'];
1533					$q=db_select("select felt_2 from ordrer where  status = '3' and art = 'PO' and fakturadate = '$dd' and felt_1='$kortnavn[$x]'",__FILE__ . " linje " . __LINE__);
1534					while ($r=db_fetch_array($q)) $kortsum[$x]+=$r['felt_2'];
1535					$q=db_select("select felt_4 from ordrer where  status = '3' and art = 'PO' and fakturadate = '$dd' and felt_3='$kortnavn[$x]'",__FILE__ . " linje " . __LINE__);
1536					while ($r=db_fetch_array($q)) $kortsum[$x]+=$r['felt_4'];
1537					$tilgang-=$kortsum[$x];
1538				}
1539#				fwrite($fp,"$kortnavn[$x]: $tmp\n\n");
1540			}
1541		}
1542	}
1543	$kassesum=dkdecimal($byttepenge+$tilgang);
1544	$byttepenge=dkdecimal($byttepenge);
1545	$tilgang=dkdecimal($tilgang);
1546
1547	fwrite($fp,"Beholdning primo: $byttepenge\n\n");
1548	fwrite($fp,"Dagens indbetalinger $tilgang\n\n");
1549	fwrite($fp,"Beholdning ultimo $kassesum\n\n");
1550	if ($kortantal) {
1551		for ($x=0;$x<$kortantal;$x++) {
1552			if ($kortkonto[$x]) {
1553				fwrite($fp,"\n\nSalg paa kort\n\n");
1554				fwrite($fp,"$kortnavn[$x]: $kortsum[$x]\n\n");
1555			}
1556		}
1557	}
1558	fwrite($fp,"\n\n\n");
1559
1560	fclose($fp);
1561	$tmp="/temp/".$db."/".$bruger_id.".txt";
1562	$url="://".$_SERVER['SERVER_NAME'].=$_SERVER['PHP_SELF'];
1563	$url=str_replace("/debitor/pos_ordre.php","",$url);
1564	if ($_SERVER[HTTPS]) $url="s".$url;
1565	$url="http".$url;
1566	print "<BODY onLoad=\"JavaScript:window.open('http://localhost/saldiprint.php?printfil=$tmp&url=$url&bruger_id=$bruger_id&bonantal=1' , '' , '$jsvars');\">";
1567
1568}
1569if (!$varenr_ny && $fokus!='modtaget' && $fokus!='modtaget2' && $fokus!='indbetaling') $fokus="varenr_ny";
1570#cho "fokus $fokus<br>";
1571?>
1572</body></html>
1573<script language="javascript">
1574document.pos_ordre.<?php echo $fokus?>.focus();
1575</script>