PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/std_func.php

https://github.com/nielsrune/saldi
PHP | 537 lines | 470 code | 39 blank | 28 comment | 138 complexity | 1b7e3571df146a3b282f4e4b7165ea56 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // -----------includes/std_func.php-------lap 3.4.1----2014-05-05-----
  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. // Dette program er udgivet med haab om at det vil vaere til gavn,
  14. // men UDEN NOGEN FORM FOR REKLAMATIONSRET ELLER GARANTI. Se
  15. // GNU General Public Licensen for flere detaljer.
  16. //
  17. // En dansk oversaettelse af licensen kan laeses her:
  18. // http://www.fundanemt.com/gpl_da.html
  19. //
  20. // Copyright (c) 2004-2014 DANOSOFT ApS
  21. // ----------------------------------------------------------------------
  22. //
  23. // 2013.02.10 Break ændret til break 1
  24. // Tastefejl rettet.
  25. // 2014.05.01 Funktion findtekst - teksten ignoreres nu hvis tekst="-"
  26. // 2014.05.05 Funktion findtekst insdat db_escape_string. (PHR - Danosoft) Søg 20140505
  27. if (!function_exists('nr_cast')) {
  28. function nr_cast($tekst)
  29. {
  30. global $db_type;
  31. if ($db_type=='mysql') $tmp = "CAST($tekst AS SIGNED)";
  32. else $tmp = "to_number(text($tekst),text(999999999999))";
  33. return $tmp;
  34. }
  35. }
  36. if (!function_exists('dkdecimal')) {
  37. function dkdecimal($tal)
  38. {
  39. if (is_numeric($tal)) {
  40. if ($tal>0) $tal=round($tal+0.0001,2);#Afrunding tilfoejet 2009.01.26 grundet diff i ordre 98 i saldi_104
  41. elseif ($tal<0) $tal=round($tal-0.0001,2);
  42. $tal=number_format($tal,2,",",".");
  43. }
  44. return $tal;
  45. }
  46. }
  47. if (!function_exists('dkdato')) {
  48. function dkdato($dato)
  49. {
  50. if ($dato) {
  51. list ($year, $month, $day) = explode('-', $dato);
  52. $month=$month*1;
  53. $day=$day*1;
  54. if ($month<10){$month='0'.$month;}
  55. if ($day<10){$day='0'.$day;}
  56. $dato = $day . "-" . $month . "-" . $year;
  57. return $dato;
  58. }
  59. }
  60. }
  61. if (!function_exists('if_isset')) {
  62. function if_isset(&$var)
  63. {
  64. return isset($var)? $var:NULL;
  65. }
  66. }
  67. if (!function_exists('usdate')) {
  68. function usdate($date)
  69. {
  70. global $regnaar;
  71. $day=NULL;$month=NULL;$year=NULL;
  72. $date=trim($date);
  73. if (!isset($date) || !$date) $date=date("dmY");
  74. $date=str_replace (".","-",$date);
  75. $date=str_replace (" ","-",$date);
  76. $date=str_replace ("/","-",$date);
  77. if (strpos($date,"-")) list ($day, $month, $year) = explode('-', $date);
  78. if ($year) $year=$year*1;
  79. if ($month) $month=$month*1;
  80. if ($day) $day=$day*1;
  81. if ($year && $year<10) $year='0'.$year;
  82. elseif (!$year) $year="";
  83. if ($month && $month<10) $month='0'.$month;
  84. elseif (!$month) $month="";
  85. if ($day && $day<10) $day='0'.$day;
  86. if ($day) $date=$day.$month.$year;
  87. if (strlen($date) <= 2) {
  88. $date=$date*1;
  89. if ($date<10) $date='0'.$date;
  90. $date=$date.date("m");
  91. }
  92. if (strlen($date) <= 4) {
  93. $g1=substr($date,0,2);
  94. $g2=substr($date,2,2);
  95. if ($r = db_fetch_array(db_select("select box1, box2, box3, box4 from grupper where art='RA' and kodenr='$regnaar'",__FILE__ . " linje " . __LINE__))){
  96. $startmaaned=trim($r['box1']);
  97. $startaar=trim($r['box2']);
  98. $slutmaaned=trim($r['box3']);
  99. $slutaar=trim($r['box4']);
  100. if ($startaar==$slutaar) $g3=$startaar;
  101. elseif ($g2>=$startmaaned) $g3=$startaar;
  102. else $g3=$slutaar;
  103. } else {
  104. $alerttekst='Regnskabs&aring;r ikke oprettet!';
  105. print "<BODY onLoad=\"javascript:alert('$alerttekst')\">";
  106. exit;
  107. }
  108. $date=$g1."-".$g2."-".$g3;
  109. } elseif (strlen($date) <= 6) {
  110. $g1=substr($date,0,2);
  111. $g2=substr($date,2,2);
  112. $g3=substr($date,4,2);
  113. $date=$g1."-".$g2."-".$g3;
  114. } else {
  115. $g1=substr($date,0,2);
  116. $g2=substr($date,2,2);
  117. $g3=substr($date,4,4);
  118. $date=$g1."-".$g2."-".$g3;
  119. }
  120. list ($day, $month, $year) = explode('-', $date);
  121. $year=$year*1;
  122. $month=$month*1;
  123. $day=$day*1;
  124. if ($year<10){$year='0'.$year;}
  125. if ($month<10){$month='0'.$month;}
  126. if ($day<10){$day='0'.$day;}
  127. if ($day>28) {
  128. while (!checkdate($month,$day,$year)){
  129. $day=$day-1;
  130. if ($day<28) break 1;
  131. }
  132. }
  133. if ($year < 80) {$year = "20".$year;}
  134. elseif ($year < 100) {$year = "19".$year;}
  135. if (checkdate($month, $day, $year)) {$date = $year . "-" . $month . "-" . $day;}
  136. else {$date=date("Y-m-d");}
  137. return $date;
  138. }
  139. }
  140. if (!function_exists('usdecimal')) {
  141. function usdecimal($tal)
  142. {
  143. if (!$tal){$tal="0,00";}
  144. $tal = str_replace(".","",$tal);
  145. $tal = str_replace(",",".",$tal);
  146. $tal=$tal*1;
  147. $tal=round($tal+0.0001,3);
  148. if (!$tal){$tal="0.00";}
  149. return $tal;
  150. }
  151. }
  152. if (!function_exists('findtekst')) {
  153. function findtekst($tekst_id,$sprog_id) {
  154. global $db_encode;
  155. GLOBAL $webservice;
  156. $ny_tekst=NULL;
  157. $tekst_id=$tekst_id*1;
  158. $sprog_id=$sprog_id*1;
  159. if (!$sprog_id) $sprog_id=1;
  160. if ($r = db_fetch_array(db_select("select tekst from tekster where tekst_id='$tekst_id' and sprog_id = '$sprog_id'",__FILE__ . " linje " . __LINE__))){
  161. $tekst=$r['tekst'];
  162. } elseif (file_exists("../importfiler/egnetekster.csv") ) {
  163. $fp=fopen("../importfiler/egnetekster.csv","r");
  164. if ($fp) {
  165. while (!feof($fp)) {
  166. if ($linje=trim(fgets($fp))) {
  167. list($tekst_nr,$tmp)=explode(chr(9),$linje);
  168. if ($tekst_id==$tekst_nr) {
  169. $ny_tekst=substr(stristr($linje,chr(9)),1);# Linjen efter 1. tab.
  170. for ($i=1;$i<=$sprog_id;$i++) $linje = substr(stristr($linje,chr(9)),1); # Start paa tekst med aktuel sprog id findes.
  171. list($ny_tekst,$tmp)=explode(chr(9),$linje); # Tekststrengen isoleres
  172. $tekst=$ny_tekst;
  173. }
  174. }
  175. }
  176. fclose($fp);
  177. }
  178. }
  179. if (!$tekst) {
  180. $fp=fopen("../importfiler/tekster.csv","r");
  181. if ($fp) {
  182. while (!feof($fp)) {
  183. if ($linje=trim(fgets($fp))) {
  184. list($tekst_nr,$tmp)=explode(chr(9),$linje);
  185. if ($tekst_id==$tekst_nr) {
  186. $ny_tekst=substr(stristr($linje,chr(9)),1);# Linjen efter 1. tab.
  187. for ($i=1;$i<=$sprog_id;$i++) $linje = substr(stristr($linje,chr(9)),1); # Start paa tekst med aktuel sprog id findes.
  188. list($ny_tekst,$tmp)=explode(chr(9),$linje); # Tekststrengen isoleres
  189. }
  190. }
  191. }
  192. fclose($fp);
  193. }
  194. }
  195. if ($ny_tekst) {
  196. if ($db_encode!="UTF8") $ny_tekst=utf8_decode($ny_tekst);
  197. $tmp=db_escape_string($ny_tekst); #20140505
  198. db_modify("insert into tekster(sprog_id,tekst_id,tekst) values ('$sprog_id','$tekst_id','$tmp')",__FILE__ . " linje " . __LINE__);
  199. $tekst=$ny_tekst;
  200. }
  201. if (!$tekst) $tekst="Tekst nr: $tekst_id";
  202. elseif ($tekst=="-") $tekst='';
  203. return ($tekst);
  204. }
  205. }
  206. if (!function_exists('javascript')) {
  207. function javascript() {
  208. }
  209. }
  210. if (!function_exists('afrund')) {
  211. function afrund($tal,$decimaler)
  212. {
  213. # Korrigerer afrundingsfejl i php
  214. $decimaler=$decimaler*1;
  215. $tmp=0.001;
  216. for ($x=1;$x<$decimaler ;$x++) {
  217. $tmp=$tmp/10;
  218. }
  219. if ($tal>0) $tal=round($tal+$tmp,$decimaler);
  220. elseif ($tal<0) $tal=round($tal-$tmp,$decimaler);
  221. return $tal;
  222. }
  223. }
  224. if (!function_exists('fjern_nul')) {
  225. function fjern_nul($tal)
  226. {
  227. #fjerner decimalnuller fra tal
  228. if (strpos($tal,",")) {
  229. list($a,$b)=explode(",",$tal);
  230. $b=$b*1;
  231. if ($b) $tal=$a.",".$b;
  232. else $tal=$a;
  233. }
  234. return $tal;
  235. }
  236. }
  237. if (!function_exists('bynavn')) {
  238. function bynavn($postnr) {
  239. global $db_encode;
  240. $fp=fopen("../importfiler/postnr.csv","r");
  241. if ($fp) {
  242. while ($linje=trim(fgets($fp))) {
  243. if ($db_encode=="UTF8") $linje=utf8_encode($linje);
  244. list($a,$b)=explode(chr(9),$linje);
  245. if ($a==$postnr) {
  246. $bynavn=str_replace('"','',$b);
  247. break 1;
  248. }
  249. }
  250. }
  251. fclose($fp);
  252. return("$bynavn");
  253. }
  254. }
  255. if (!function_exists('felt_fra_tekst')) {
  256. function felt_fra_tekst ($feltmatch, $tekstlinjer) {
  257. $matchende_linjer = preg_grep("/$feltmatch/", $tekstlinjer);
  258. foreach ($matchende_linjer as $linje) {
  259. $retur = str_replace($feltmatch, "", $linje);
  260. }
  261. return $retur;
  262. }
  263. }
  264. if (!function_exists('sidste_dag_i_maaned')) {
  265. function sidste_dag_i_maaned ($aar, $maaned) {
  266. $maaned++;
  267. $retur = date("d", mktime(12, 0, 0, $maaned, 0, $aar));
  268. return $retur;
  269. }
  270. }
  271. if (!function_exists('farvenuance')) {
  272. function farvenuance ($farve, $nuance) { # Notation for nuance: -33+33-33 eller -3+3-3
  273. global $bgcolor;
  274. $retur = $bgcolor;
  275. $farve = preg_replace("/[^0-9A-Fa-f]/", '', $farve);
  276. if ( strlen($farve) == 3 ) {
  277. $roed_farve=hexdec(str_repeat(substr($farve, 0, 1), 2));
  278. $groen_farve=hexdec(str_repeat(substr($farve, 1, 1), 2));
  279. $blaa_farve=hexdec(str_repeat(substr($farve, 2, 1), 2));
  280. } else {
  281. $roed_farve=hexdec(substr($farve, 0, 2));
  282. $groen_farve=hexdec(substr($farve, 2, 2));
  283. $blaa_farve=hexdec(substr($farve, 4, 2));
  284. }
  285. if ( strlen($nuance) == 6 ) {
  286. $roed_fortegn=substr($nuance, 0, 1)."1";
  287. $roed_nuance=$roed_fortegn*hexdec(str_repeat(substr($nuance, 1, 1), 2));
  288. $groen_fortegn=substr($nuance, 2, 1)."1";
  289. $groen_nuance=$groen_fortegn*hexdec(str_repeat(substr($nuance, 3, 1), 2));
  290. $blaa_fortegn=substr($nuance, 4, 1)."1";
  291. $blaa_nuance=$blaa_fortegn*hexdec(str_repeat(substr($nuance, 5, 1), 2));
  292. } else {
  293. $roed_fortegn=substr($nuance, 0, 1)."1";
  294. $roed_nuance=$roed_fortegn*hexdec(substr($nuance, 1, 2));
  295. $groen_fortegn=substr($nuance, 3, 1)."1";
  296. $groen_nuance=$groen_fortegn*hexdec(substr($nuance, 4, 2));
  297. $blaa_fortegn=substr($nuance, 6, 1)."1";
  298. $blaa_nuance=$blaa_fortegn*hexdec(substr($nuance, 7, 2));
  299. }
  300. $roed_farve=$roed_farve+$roed_nuance;
  301. if ($roed_farve < 0 ) $roed_farve = 0;
  302. if ($roed_farve > 255 ) $roed_farve = 255;
  303. $groen_farve=$groen_farve+$groen_nuance;
  304. if ($groen_farve < 0 ) $groen_farve = 0;
  305. if ($groen_farve > 255 ) $groen_farve = 255;
  306. $blaa_farve=$blaa_farve+$blaa_nuance;
  307. if ($blaa_farve < 0 ) $blaa_farve = 0;
  308. if ($blaa_farve > 255 ) $blaa_farve = 255;
  309. $roed_farve=str_pad(dechex($roed_farve), 2, STR_PAD_LEFT);
  310. $groen_farve=str_pad(dechex($groen_farve), 2, STR_PAD_LEFT);
  311. $blaa_farve=str_pad(dechex($blaa_farve), 2, STR_PAD_LEFT);
  312. $retur = "#".$roed_farve.$groen_farve.$blaa_farve;
  313. return $retur;
  314. }
  315. }
  316. if (!function_exists('linjefarve')) {
  317. #function linjefarve ($linjefarve, $ulige_bg, $lige_bg, $nuance = 0, $stdnuance = 0) {
  318. function linjefarve ($linjefarve, $ulige_bg, $lige_bg, $stdnuance = 0, $nuance = 0) {
  319. if ( $linjefarve === $ulige_bg || $linjefarve === farvenuance($ulige_bg, $stdnuance) ) {
  320. if ( $nuance ) {
  321. $retur = farvenuance($lige_bg, $nuance);
  322. } else {
  323. $retur = $lige_bg;
  324. }
  325. } else {
  326. if ( $nuance ) {
  327. $retur = farvenuance($ulige_bg, $nuance);
  328. } else {
  329. $retur = $ulige_bg;
  330. }
  331. }
  332. return $retur;
  333. }
  334. }
  335. if (!function_exists('copy_row')) {
  336. function copy_row($table,$id) {
  337. if (!$table || !$id) return('0');
  338. $r=0;$x=0;
  339. $fieldstring=NULL;
  340. $q_string="select * from $table where pris != '0' and m_rabat != '0' and rabat = '0' and id='$id'";
  341. $q=db_select("$q_string",__FILE__ . " linje " . __LINE__);
  342. while ($r < db_num_fields($q)) {
  343. if (db_field_name($q,$r) != 'id') {
  344. $x++;
  345. $fieldName[$x] = db_field_name($q,$r);
  346. $fieldType[$x] = db_field_type($q,$r);
  347. ($fieldstring)?$fieldstring.=",".$fieldName[$x]:$fieldstring=$fieldName[$x];
  348. }
  349. $r++;
  350. }
  351. $feltantal=$x;
  352. $ordre_id=NULL;$posnr=NULL;
  353. $x=0;
  354. $q=db_select("$q_string");
  355. if ($r = db_fetch_array($q)) {
  356. $fieldvalues=NULL;
  357. $selectstring=NULL;
  358. for ($y=1;$y<=$feltantal;$y++){
  359. $linjerabat=afrund($r['pris']/$r['m_rabat'],2);
  360. $feltnavn=$fieldName[$y];
  361. $felt[$y]=$r[$feltnavn];
  362. if ($fieldType[$y]=='varchar' || $fieldType[$y]=='text') $felt[$y]=addslashes($felt[$y]);
  363. if (substr($fieldType[$y],0,3)=='int' || $fieldType[$y]=='numeric') $felt[$y]*=1;
  364. if ($fieldName[$y]=='posnr') {
  365. $felt[$y]++;
  366. $posnr=$felt[$y];
  367. }
  368. if ($fieldName[$y]=='ordre_id') $ordre_id=$felt[$y];
  369. ($fieldvalues)?$fieldvalues.=",'".$felt[$y]."'":$fieldvalues="'".$felt[$y]."'";
  370. ($selectstring)?$selectstring.=" and ".$fieldName[$y]."='".$felt[$y]."'":$selectstring=$fieldName[$y]."='".$felt[$y]."'";
  371. }
  372. }
  373. if ($posnr && $ordre_id) db_modify("update $table set posnr=posnr+1 where ordre_id = '$ordre_id' and posnr >= '$posnr'",__FILE__ . " linje " . __LINE__);
  374. db_modify("insert into ordrelinjer ($fieldstring) values ($fieldvalues)",__FILE__ . " linje " . __LINE__);
  375. $r=db_fetch_array(db_select("select id from $table where $selectstring",__FILE__ . " linje " . __LINE__));
  376. $ny_id=$r['id'];
  377. return($ny_id);
  378. } # endfunc copy_row
  379. }
  380. if (!function_exists('reducer')) {
  381. function reducer($tal){
  382. while ((strpos($tal,".") || strpos($tal,",")) && ($tal && (substr($tal,-1,1)=='0' or substr($tal,-1,1)==',' or substr($tal,-1,1)=='.'))) {
  383. $tal=substr($tal,0,strlen($tal)-1);
  384. }
  385. return ($tal);
  386. }
  387. }
  388. if (!function_exists('transtjek')) {
  389. function transtjek () {
  390. global $db;
  391. $r=db_fetch_array(db_select("select sum(debet) as debet,sum(kredit) as kredit from transaktioner",__FILE__ . " linje " . __LINE__));
  392. $diff=abs(afrund($r['debet']-$r['kredit'],2));
  393. if ($diff >= 1) {
  394. $message=$db." | Ubalance i regnskab: kr: $diff";
  395. $headers = 'From: fejl@saldi.dk'."\r\n".'Reply-To: fejl@saldi.dk'."\r\n".'X-Mailer: PHP/' . phpversion();
  396. mail('fejl@saldi.dk', 'Ubalance i regnskab:'. $db, $message, $headers);
  397. }
  398. return($diff);
  399. }
  400. }
  401. if (!function_exists('cvrnr_omr')) {
  402. function cvrnr_omr($landekode) {
  403. $retur = "";
  404. if ( ! $landekode ) {
  405. $retur = "";
  406. } else {
  407. switch ( $landekode ) {
  408. case "dk": $retur = "DK"; break 1;
  409. case "at": $retur = "EU"; break 1;
  410. case "be": $retur = "EU"; break 1;
  411. case "cy": $retur = "EU"; break 1;
  412. case "cz": $retur = "EU"; break 1;
  413. case "de": $retur = "EU"; break 1;
  414. case "ee": $retur = "EU"; break 1;
  415. case "gr": $retur = "EU"; break 1;
  416. case "es": $retur = "EU"; break 1;
  417. case "fi": $retur = "EU"; break 1;
  418. case "fr": $retur = "EU"; break 1;
  419. case "gb": $retur = "EU"; break 1;
  420. case "hu": $retur = "EU"; break 1;
  421. case "ie": $retur = "EU"; break 1;
  422. case "it": $retur = "EU"; break 1;
  423. case "lt": $retur = "EU"; break 1;
  424. case "lu": $retur = "EU"; break 1;
  425. case "lv": $retur = "EU"; break 1;
  426. case "mt": $retur = "EU"; break 1;
  427. case "nl": $retur = "EU"; break 1;
  428. case "pl": $retur = "EU"; break 1;
  429. case "pt": $retur = "EU"; break 1;
  430. case "ro": $retur = "EU"; break 1;
  431. case "se": $retur = "EU"; break 1;
  432. case "si": $retur = "EU"; break 1;
  433. case "sk": $retur = "EU"; break 1;
  434. case "gl": $retur = "UD"; break 1;
  435. default: $retur = "UD"; break 1;
  436. }
  437. }
  438. return $retur;
  439. }
  440. }
  441. if (!function_exists('cvrnr_land')) {
  442. function cvrnr_land($cvrnr, $skat) {
  443. $retur = "";
  444. $cvrnr = strtoupper($cvrnr);
  445. if ( ! $cvrnr ) {
  446. $retur = "";
  447. } elseif ( is_numeric(substr($cvrnr, 0, 1)) ) {
  448. $retur = "dk";
  449. } else {
  450. $start_tegn=strtolower(substr($cvrnr, 0, 3));
  451. switch ( $start_tegn ) {
  452. case "ger": $start_tegn="gl"; break 1;
  453. default : break 1;
  454. }
  455. $start_tegn=substr($start_tegn, 0, 2);
  456. switch ( $start_tegn ) {
  457. case "el": $retur = "gr"; break 1;
  458. default: $retur = $start_tegn;
  459. }
  460. }
  461. return $retur;
  462. }
  463. }
  464. if (!function_exists('str2low')) {
  465. function str2low($string) {
  466. global $db_encode;
  467. $string=strtolower($string);
  468. if ($db_encode=='UTF8') {
  469. $string=str_replace(chr(195).chr(134),chr(195).chr(166),$string);
  470. $string=str_replace(chr(195).chr(152),chr(195).chr(184),$string);
  471. $string=str_replace(chr(195).chr(133),chr(195).chr(165),$string);
  472. } else {
  473. $string=str_replace(chr(198),chr(230),$string);
  474. $string=str_replace(chr(216),chr(248),$string);
  475. $string=str_replace(chr(197),chr(229),$string);
  476. }
  477. return ("$string");
  478. }
  479. }
  480. if (!function_exists('str2up')) {
  481. function str2up($string) {
  482. $string=strtoupper($string);
  483. if ($db_encode=='UTF8') {
  484. $string=str_replace(chr(195).chr(166),chr(195).chr(134),$string);
  485. $string=str_replace(chr(195).chr(184),chr(195).chr(152),$string);
  486. $string=str_replace(chr(195).chr(165),chr(195).chr(133),$string);
  487. } else {
  488. $string=str_replace(chr(230),chr(198),$string);
  489. $string=str_replace(chr(248),chr(216),$string);
  490. $string=str_replace(chr(229),chr(197),$string);
  491. }
  492. $string=str_replace('æ','Æ',$string);
  493. $string=str_replace('ø','Ø',$string);
  494. $string=str_replace('å','Å',$string);
  495. return ("$string");
  496. }
  497. }
  498. ?>