PageRenderTime 307ms CodeModel.GetById 121ms app.highlight 76ms RepoModel.GetById 105ms app.codeStats 0ms

/soapklient/saldi_update.php

https://github.com/nielsrune/saldi
PHP | 290 lines | 246 code | 10 blank | 34 comment | 68 complexity | cfec6965bd2df45b73a5ea1f0c235aa9 MD5 | raw file
  1<?php
  2// #----------------- saldi_update.php -----ver 3.2.6---- 2012.01.10 ----------
  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// Programmet 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) 2003-2012 DANOSOFT ApS
 22// ----------------------------------------------------------------------
 23
 24
 25if (!function_exists('saldi_update')) {
 26	function saldi_update($insert_id) {
 27	global $url;
 28
 29		$debitorgruppe=1; #Den debitorgruppe som webdebitorer tilhører.
 30
 31		if (!$insert_id) return('missing insert_id');
 32		$q=mysql_query("select * from orders where orders_id = '$insert_id'");
 33		if ($r=mysql_fetch_array($q)) {
 34			$customers_id=$r['customers_id'];
 35			$customers_name=$r['customers_name'];
 36			$customers_company=$r['customers_company'];
 37			$customers_street_address=$r['customers_street_address'];
 38			$customers_city=$r['customers_city'];
 39			$customers_postcode=$r['customers_postcode'];
 40			$customers_country=$r['customers_country'];
 41			$customers_telephone=$r['customers_telephone'];
 42			$customers_email_address=$r['customers_email_address'];
 43			$delivery_name=$r['delivery_name'];
 44			$delivery_company=$r['delivery_company'];
 45			$delivery_street_address=$r['delivery_street_address'];
 46			$delivery_city=$r['delivery_city'];
 47			$delivery_postcode=$r['delivery_postcode'];
 48			$delivery_country=$r['delivery_country'];
 49			$billing_name=$r['billing_name'];
 50			$billing_company=$r['billing_company'];
 51			$billing_street_address=$r['billing_street_address'];
 52			$billing_city=$r['billing_city'];
 53			$billing_postcode=$r['billing_postcode'];
 54			$billing_country=$r['billing_country'];
 55			$date_purchased=$r['date_purchased'];
 56			$payment_method=$r['payment_method'];
 57			$currency=$r['currency'];
 58			$currency_value=$r['currency_value'];
 59		}	else return('order not found');
 60		$x=0;
 61		$q=mysql_query("select * from orders_products where orders_id = '$insert_id'");
 62		while ($r=mysql_fetch_array($q)) {
 63			$x++;
 64			$orders_products_id[$x]=$r['orders_products_id'];
 65			$products_id[$x]=$r['products_id'];
 66			$products_model[$x]=$r['products_model'];
 67			$products_name[$x]=$r['products_name'];
 68			$products_price[$x]=$r['products_price'];
 69			$products_tax[$x]=$r['products_tax'];
 70			$products_quantity[$x]=$r['products_quantity'];
 71			$stregkode[$x]=$products_model[$x];
 72		}
 73		$item_quantity=$x;
 74		if (!$item_quantity) return('no items in order');
 75
 76		if (file_exists("soapklient/soapfunc.php")) $filnavn="soapklient/soapfunc.log";
 77		else $filnavn="soapfunc.log";
 78		$fp=fopen($filnavn,'a');
 79		fwrite($fp,"------------".date("Y-m-d H:i:s")."------------\n");
 80
 81		#		medshop hack.
 82		if ($billing_country=='Denmark') $debitorgruppe="1";
 83		elseif ($billing_country=='Norway') $debitorgruppe="3";
 84		elseif ($billing_company) $debitorgruppe="4";
 85		else $debitorgruppe="2";
 86
 87		for ($x=1;$x<=$item_quantity;$x++) {
 88			$y=0;
 89			$select="products_options.products_options_id as products_options_id,products_options_values.products_options_values_id as products_options_values_id";
 90			$from="orders_products_attributes,products_options,products_options_values";
 91			$where="orders_products_attributes.orders_id = '$insert_id' and orders_products_attributes.orders_products_id='$orders_products_id[$x]' ";
 92			$where.="and products_options.products_options_name=orders_products_attributes.products_options and ";
 93			$where.="products_options_values.products_options_values_name=orders_products_attributes.products_options_values";
 94			fwrite($fp,__LINE__." - SELECT $select FROM $from WHERE $where\n");
 95			$q=mysql_query("SELECT $select FROM $from WHERE $where");
 96			while ($r=mysql_fetch_array($q)) {
 97				$y++;
 98				$products_options_id[$x][$y]=$r['products_options_id'];
 99				$products_options_values_id[$x][$y]=$r['products_options_values_id'];
100				fwrite($fp,__LINE__.$products_options_id[$x][$y]." -:-".$products_options_values_id[$x][$y]."\n");
101			}
102		}
103		if (file_exists("soapklient/soapfunc.php")) include("soapklient/soapfunc.php");
104		else include("soapfunc.php");
105		if (file_exists("soapklient/saldi_connect.php")) include("soapklient/saldi_connect.php");
106		else include("saldi_connect.php");
107		fwrite($fp,__LINE__."$url logon($regnskab,$brugernavn,$adgangskode)\n");
108		list($fejl,$svar)=explode(chr(9),logon($regnskab,$brugernavn,$adgangskode));
109		fwrite($fp,__LINE__."fejl $fejl - svar $svar\n");
110		$fejl*=1;
111		if ($fejl) return ($fejl.chr(9).$svar);
112		else $s_id=$svar;
113		fwrite($fp,__LINE__." - saldi_id from shop_adresser where shop_id='$customers_id'\n");
114		list($fejl,$svar)=explode(chr(9),singleselect($s_id,"saldi_id from shop_adresser where shop_id='$customers_id'"));
115		fwrite($fp,__LINE__." - svar $svar\n");
116		if ($fejl) return ($svar);
117		elseif ($svar) {
118			$konto_id=$svar;
119			fwrite($fp,__LINE__." - kontonr from adresser where id='$konto_id'\n");
120			list($fejl,$svar)=explode(chr(9),singleselect($s_id,"kontonr from adresser where id='$konto_id'"));
121			fwrite($fp,__LINE__." - svar $svar\n");
122			if ($fejl) return ($svar);
123			else $kontonr=$svar;
124
125		} else {
126			if ($customers_company) {
127				$kontotype='Erhverv';
128				$fornavn=NULL;
129				$efternavn=NULL;
130				$kontakt=$customers_name;
131			} else {
132				$kontotype='Privat';
133				$customers_company=$customers_name;
134				$delivery_company=$delivery_name;
135				$billing_company=$billing_name;
136
137				list($fornavn,$tmp)=explode(" ",$customers_name);
138				$efternavn=trim(substr($customers_name,strlen($fornavn)));
139				$kontakt='';
140			}
141			fwrite($fp,__LINE__." - id,kontonr,firmanavn,addr1 from adresser where art='D' and tlf='$customers_telephone'\n");
142			# tjekker om der er en kunde i saldi med samme telefonnummer.
143			list($fejl,$svar)=explode(chr(9),singleselect($s_id,"id,kontonr,firmanavn,addr1 from adresser where art='D' and tlf='$customers_telephone'"));
144			fwrite($fp,__LINE__." - svar $svar\n");
145			if ($fejl) return ($svar);
146			else list($konto_id,$kontonr,$firmanavn,$addr1)=explode(',',$svar);
147			if ($kontonr && strtolower($customers_company)==strtolower($firmanavn) && strtolower($customers_street_address)==strtolower($addr1)) {
148				return("0".chr(9).$id);
149			} elseif ($customers_telephone) {
150				fwrite($fp,__LINE__." - id,kontonr,firmanavn,addr1 from adresser where art='D' and kontonr='$customers_telephone'\n");
151				# tjekker om der er en kunde i saldi med samme kontonummer som kundens telefonnummer.
152				list($fejl,$svar)=explode(chr(9),singleselect($s_id,"id,kontonr,firmanavn,addr1 from adresser where art='D' and kontonr='$customers_telephone'"));
153				fwrite($fp,__LINE__." - svar $fejl - $svar\n");
154				if ($fejl) return ($svar);
155				else list($konto_id,$kontonr,$firmanavn,$addr1)=explode(',',$svar);
156#				if ($kontonr && strtolower($customers_company)==strtolower($firmanavn) && strtolower($customers_street_address)==strtolower($addr1)) {
157#				return("0".chr(9).$id);
158			} elseif (!$customers_telephone) {
159				fwrite($fp,__LINE__." - max(kontonr) as kontonr from adresser where art='D'\n");
160				list($fejl,$svar)=explode(chr(9),singleselect($s_id,"max(kontonr) as kontonr from adresser where art='D'"));
161				fwrite($fp,__LINE__." - svar $svar\n");
162				if ($fejl) return ('1'.chr(9).$svar);
163				else $kontonr=$svar+1;
164			} 
165			if (!$kontonr) $kontonr=str_replace("+","",$customers_telephone);
166			fwrite($fp,__LINE__."- $kontonr=".str_replace("z","",$customers_telephone)." - \n");
167			$kontonr=str_replace(" ","",$kontonr);
168			fwrite($fp,__LINE__."- $kontonr - \n");
169			$kontonr*=1;
170			fwrite($fp,__LINE__." -$kontonr - \n");
171			if (!$konto_id) { 
172				fwrite($fp,__LINE__." - adresser (kontonr,firmanavn,fornavn,efternavn,kontakt,addr1,bynavn,postnr,land,tlf,email,lev_kontakt,lev_firmanavn,lev_addr1,lev_bynavn,lev_postnr,lev_land,lev_tlf,art,kontotype,gruppe) values ('$kontonr','$customers_company','$fornavn','$efternavn','$kontakt','$customers_street_address','$customers_city','$customers_postcode','$customers_country','$customers_telephone','$customers_email_address','$delivery_name','$delivery_company','$delivery_street_address','$delivery_city','$delivery_postcode','$delivery_country','$delivery_telephone','D','$kontotype','$debitorgruppe')\n");
173				list($fejl,$svar)=explode(chr(9),singleinsert($s_id,"adresser (kontonr,firmanavn,fornavn,efternavn,kontakt,addr1,bynavn,postnr,land,tlf,email,lev_kontakt,lev_firmanavn,lev_addr1,lev_bynavn,lev_postnr,lev_land,lev_tlf,art,kontotype,gruppe) values ('$kontonr','$customers_company','$fornavn','$efternavn','$kontakt','$customers_street_address','$customers_city','$customers_postcode','$customers_country','$customers_telephone','$customers_email_address','$delivery_name','$delivery_company','$delivery_street_address','$delivery_city','$delivery_postcode','$delivery_country','$delivery_telephone','D','$kontotype','$debitorgruppe')"));
174				fwrite($fp,__LINE__." - svar $svar\n");
175				if ($fejl) return ($fejl.chr(9).$svar);
176				else $konto_id=$svar;
177				fwrite($fp,__LINE__." - shop_adresser (shop_id,saldi_id) values ('$customers_id','$konto_id')\n");
178				list($fejl,$svar)=explode(chr(9),singleinsert($s_id,"shop_adresser (shop_id,saldi_id) values ('$customers_id','$konto_id')"));
179				fwrite($fp,__LINE__." - svar $svar\n");
180				if ($fejl) return('1'.chr(9).$svar);
181				if ($kontotype=='Erhverv') { 
182					fwrite($fp,__LINE__." - ansatte (navn,konto_id) values ('$customers_name','$konto_id')\n");
183					list($fejl,$svar)=explode(chr(9),singleinsert($s_id,"ansatte (navn,konto_id) values ('$customers_name','$konto_id')"));
184					fwrite($fp,__LINE__." - svar $svar\n");
185					if ($fejl) return ($fejl.chr(9).$svar);
186				}
187			}
188		}
189		fwrite($fp,__LINE__." - saldi_id from shop_ordrer where shop_id='$insert_id'\n");
190		list($fejl,$svar)=explode(chr(9),singleselect($s_id,"saldi_id from shop_ordrer where shop_id='$insert_id'"));
191		fwrite($fp,__LINE__." - svar $svar\n");
192		if ($fejl) return ($svar);
193		elseif ($svar) $ordre_id=$svar;
194		else {
195			fwrite($fp,__LINE__." - max(ordrenr) as ordrenr from ordrer where art='DO'\n");
196			list($fejl,$svar)=explode(chr(9),singleselect($s_id,"max(ordrenr) as ordrenr from ordrer where art='DO'"));
197			fwrite($fp,__LINE__." - fejl $fejl, svar $svar\n");
198			if ($fejl) return ($fejl.chr(9).$svar);
199			else $ordrenr=$svar+1;
200			$ordredate=date("Y-m-d");
201			if(!$billing_company){
202				$billing_company=$billing_name;
203				$billing_name='';
204			}
205			if(!$delivery_company){
206				$delivery_company=$delivery_name;
207				$delivery_name='';
208			}
209#cho "ordrer (konto_id,kontonr,firmanavn,kontakt,addr1,bynavn,postnr,land,email,lev_kontakt,lev_navn,lev_addr1,lev_bynavn,lev_postnr,art) values ('$konto_id','$kontonr','$billing_company','$billing_name','$billing_street_address','$billing_city','$billing_postcode','$billing_country','$customers_email_address','$delivery_name','$delivery_company','$delivery_street_address','$delivery_city','$delivery_postcode','DO')";
210			list($fejl,$svar)=explode(chr(9),singleselect($s_id,"box2 as momssats from grupper where art='SM' and kodenr='$debitorgruppe'"));
211			fwrite($fp,__LINE__." - fejl $fejl, svar $svar\n");
212			if ($fejl) return ($fejl.chr(9).$svar);
213			else $momssats=$svar*1;
214			fwrite($fp,__LINE__." - ordrer (ordrenr,konto_id,kontonr,firmanavn,kontakt,addr1,bynavn,postnr,land,email,lev_kontakt,lev_navn,lev_addr1,lev_bynavn,lev_postnr,art,momssats,kundeordnr) values ('$ordrenr','$ordredate','$konto_id','$kontonr','$billing_company','$billing_name','$billing_street_address','$billing_city','$billing_postcode','$billing_country','$customers_email_address','$delivery_name','$delivery_company','$delivery_street_address','$delivery_city','$delivery_postcode','DO','$momssats','$insert_id')\n");
215			list($fejl,$svar)=explode(chr(9),singleinsert($s_id,"ordrer (ordrenr,ordredate,konto_id,kontonr,firmanavn,kontakt,addr1,bynavn,postnr,land,email,lev_kontakt,lev_navn,lev_addr1,lev_bynavn,lev_postnr,art,status,momssats,kundeordnr) values ('$ordrenr','$ordredate','$konto_id','$kontonr','$billing_company','$billing_name','$billing_street_address','$billing_city','$billing_postcode','$billing_country','$customers_email_address','$delivery_name','$delivery_company','$delivery_street_address','$delivery_city','$delivery_postcode','DO','2','$momssats','$insert_id')"));
216			fwrite($fp,__LINE__." - svar $svar\n");
217			if ($fejl) return ($fejl.chr(9).$svar);
218			else $ordre_id=$svar;
219			fwrite($fp,__LINE__." - shop_ordrer (shop_id,saldi_id) values ('$insert_id','$ordre_id')\n");
220			list($fejl,$svar)=explode(chr(9),singleinsert($s_id,"shop_ordrer (shop_id,saldi_id) values ('$insert_id','$ordre_id')"));
221			fwrite($fp,__LINE__." - svar $svar\n");
222			if ($fejl) return('1'.chr(9).$svar);
223		}
224		# Tilføjer ordrelinjer
225		for ($x=1;$x<=$item_quantity;$x++) {
226			# Tjekker om varerelation eksisterer i Saldi
227			fwrite($fp,__LINE__." - saldi_id from shop_varer where shop_id='$products_id[$x]'\n");
228			list($fejl,$svar)=explode(chr(9),singleselect($s_id,"saldi_id from shop_varer where shop_id='$products_id[$x]'"));
229			fwrite($fp,"svar $svar\n");
230			if ($fejl) return ($svar);
231			elseif ($svar) $vare_id[$x]=$svar;
232			else {
233				# Tjekker om varenummeret eksisterer i Saldi
234				fwrite($fp,__LINE__." - id from varer where varenr='$products_model[$x]'\n");
235				list($fejl,$svar)=explode(chr(9),singleselect($s_id,"id from varer where varenr='$products_model[$x]'"));
236				fwrite($fp,__LINE__." - svar $svar\n");
237				if ($fejl) return ($svar);
238				elseif ($svar) $vare_id[$x]=$svar;
239				else {
240					#Hvis varen ikke eksisterer i Saldi oprettes den.
241					fwrite($fp,__LINE__." - varer (varenr,beskrivelse,salgspris,gruppe) values ('$products_model[$x]','$products_name[$x]','$products_price[$x]','1')\n");
242					list($fejl,$svar)=explode(chr(9),singleinsert($s_id,"varer (varenr,beskrivelse,salgspris,gruppe) values ('$products_model[$x]','$products_name[$x]','$products_price[$x]','1')"));
243					fwrite($fp,__LINE__." - svar $svar\n");
244					if ($fejl) return ('1'.chr(9).$svar);
245					else $vare_id=$svar;
246				}
247				# Og der oprettes relation mellem Saldi & osc vare ID
248				fwrite($fp,__LINE__." - shop_varer (shop_id,saldi_id) values ('$products_id[$x]','$ordre_id')\n");
249				list($fejl,$svar)=explode(chr(9),singleinsert($s_id,"shop_varer (shop_id,saldi_id) values ('$products_id[$x]','$ordre_id')"));
250				fwrite($fp,__LINE__." - svar $svar\n");
251				if ($fejl) return('1'.chr(9).$svar);
252			}
253			fwrite($fp,__LINE__." - Tjekker om varen indeholder varianter\n");
254			#Tjekker om varen indeholder varianter.
255			if (count($products_options_values_id[$x])) {
256				$tmp=count($products_options_values_id[$x]);
257				for ($y=1;$y<=$tmp;$y++) {
258				fwrite($fp,__LINE__." - products_options_values_id: ".$products_options_values_id[$x][$y]."\n");
259					if ($products_options_values_id[$x][$y]) {
260						fwrite($fp,__LINE__." - id from variant_typer where shop_id='".$products_options_values_id[$x][$y]."'\n");
261						list($fejl,$svar)=explode(chr(9),singleselect($s_id,"id from variant_typer where shop_id='".$products_options_values_id[$x][$y]."'"));
262						fwrite($fp,__LINE__." - svar $svar\n");
263						if ($fejl) return('1'.chr(9).$svar);
264						else $variant_type_id[$x][$y]=$svar;
265					}
266				}
267				sort($variant_type_id[$x]);
268				$tmp=count($variant_type_id[$x]);
269				$variant[$x]=NULL;
270				for ($y=0;$y<$tmp;$y++) {
271					($variant[$x])?$variant[$x].=chr(9).$variant_type_id[$x][$y]:$variant[$x]=$variant_type_id[$x][$y];
272				}
273				fwrite($fp,__LINE__." - variant_stregkode from variant_varer where variant_type='$variant[$x]'\n");
274
275				list($fejl,$svar)=explode(chr(9),singleselect($s_id,"variant_stregkode from variant_varer where variant_type='$variant[$x]'"),2);
276				fwrite($fp,__LINE__." - stregkode $svar\n");
277				if ($fejl) return('1'.chr(9).$svar);
278				else $stregkode[$x]=$svar;
279			}
280			# Ordrelinjen operettes.
281			fwrite($fp,__LINE__." - \"$ordre_id\",\"$stregkode[$x]\",\"$products_name[$x]\",\"$products_quantity[$x]\",\"$products_price[$x]\",\"$products_tax[$x]\",\"$x\"\n");
282			list($fejl,$svar)=explode(chr(9),addorderline("$s_id","$ordre_id","$stregkode[$x]","$products_name[$x]","$products_quantity[$x]","$products_price[$x]","$products_tax[$x]","$x"));
283			fwrite($fp,__LINE__." - svar $svar\n");
284			if ($fejl) return('1'.chr(9).$svar);
285		}
286		return('0'.chr(9).$konto_id.chr(9).$ordre_id);
287		fclose($fp);
288	}
289}
290?>