*************************************************************************************************************************
* adauga corespondente intre partener si fiecare tip din lista de id-uri tip partener
* Date : 04/10/2006
* author : marius.mutu
******************************************************* ADAUGA_CORESP_TIP_PART ******************************************
PROCEDURE ADAUGA_CORESP_TIP_PART
	LPARAMETERS tnIdPart, tcTipuriPartener
	&& tcTipuriParteneri: lista de id-uri tipuri parteneri delimitata prin ','
	*!* ADAUG TIPURI DE PARTENER - PARTENER DACA NU EXISTA

	LOCAL lcTipPart, lcSql, lnSucces, i
	IF EMPTY(tcTipuriPartener) OR EMPTY(tnIdPart)
		RETURN
	ENDIF
	FOR i = 1 TO GETWORDCOUNT(tcTipuriPartener,[,;])
		lcTipPart = GETWORDNUM(tcTipuriPartener,i,[,;])
		lcSql = [begin PACK_DEF.adauga_coresp_tip_part(] + ALLTRIM(TRANSFORM(tnIdPart)) + [,] + lcTipPart + [); END;]
		lnSucces = goExecutor.oExecute(lcSql)
		IF lnSucces < 0
			AMESSAGEBOX(goExecutor.cEroare,0+16,'Eroare')
		ENDIF
	ENDFOR
ENDPROC && ADAUGA_CORESP_TIP_PART
******************************************************* ADAUGA_CORESP_DELEGAT ******************************************
Procedure ADAUGA_CORESP_DELEGAT
Lparameters tnIdDelegat, tnIdPart
Local lcSql,lnSucces
lcSql = [begin pack_def.adauga_coresp_delegat(] + Alltrim(Str(tnIdDelegat)) + [,] + Alltrim(Str(tnIdPart)) + [); end;]
lnSucces = goExecutor.oExecute(lcSql)
If lnSucces < 0
	AMESSAGEBOX(goExecutor.cEroare,0+16,'Eroare')
Endif
EndProc
*************************************************************************************************************************
Procedure Adauga_Delegat_Partener
Lparameters tnIdDelegat, tnIdPartener
Local lcTipPart, lcSql, lnSucces
If Empty(tnIdDelegat) Or Empty(tnIdPartener)
	Return
Endif
lcSql = [begin pack_def.adauga_coresp_tip_part(]+Alltrim(Str(tnIdDelegat))+[,?gnIdTipDelegati);] + ;
		[pack_def.actualizeaza_coresp_delegati(] + Alltrim(Str(tnIdDelegat)) + [,'] + ;
		Alltrim(Str(tnIdPartener)) + [;'); END;]
lnSucces = goExecutor.oExecute(lcSql)
If lnSucces < 0
	AMESSAGEBOX(goExecutor.cEroare,0+16,'Eroare')
Endif
Endproc && Adauga_Delegat_Partener


PROCEDURE Adauga_partener
	LPARAMETERS toPartener

	Local lcAdresa, lcApart, lcBanca, lcBloc, lcCod_fiscal, lcCodpostal, lcCont_Banca, lcCorespDel
	Local lcDA_apare, lcDenumire, lcDenumire_adresa, lcEmail, lcEtaj, lcFax, lcIdPart, lcIdString
	Local lcId_Judet, lcId_categ_ent, lcId_loc, lcId_loc_inreg, lcId_tara, lcId_util, lcItem, lcJudet
	Local lcJudetBucuresti, lcLocalitate, lcMesaj, lcMotiv_inactiv, lcNumar, lcNume, lcPrefix, lcPrenume
	Local lcPrincipala, lcReg_comert, lcScara, lcSql, lcSqlInsert, lcStrada, lcSufix, lcTara, lcTelefon1
	Local lcTelefon2, lcTip_persoana, lcWeb, lcinactiv, llSucces, lnIdJudet, lnIdJudetBucuresti
	Local lnIdLocalitateBucuresti, lnIdTaraRO, lnItem, lnItems, lnSucces
	*:Global pnIdAdresa, pnIdJudet

	lcDenumire = Nvl(Strtran(Alltrim(Upper(toPartener.denumire)), ['], ['']), "")
	lcNume = ''
	IF TYPE('toPartener.nume') = 'C'
		lcNume = Nvl(Strtran(Alltrim(Upper(toPartener.nume)), ['], ['']), "")
	ENDIF 	
	If Empty(m.lcNume)
		lcNume = m.lcDenumire
		m.lcPrenume = ''
	Endif

	lcPrenume = ''
	IF TYPE('toPartener.prenume') = 'C'
		lcPrenume = Nvl(Strtran(Alltrim(Upper(toPartener.prenume)), ['], ['']), "")
	ENDIF 	

	lcCod_fiscal = ''
	IF TYPE('toPartener.cod_fiscal') = 'C'
		lcCod_fiscal = Upper(Alltrim(toPartener.cod_fiscal))
	ENDIF 	

	lcReg_comert = ''
	IF TYPE('toPartener.reg_com') = 'C'
		lcReg_comert = Nvl(Alltrim(Upper(toPartener.reg_com)), "")
	ENDIF 	

	lcId_loc_inreg = 'NULL'
	lcId_categ_ent = 'NULL'
	lcPrefix = ""
	lcSufix = ""
	lcTip_persoana = IIF(!EMPTY(m.lcCod_fiscal) and LEN(m.lcCod_fiscal) < 13, "1", "2")
	lcBanca = ""
	lcCont_Banca = ""
	lcinactiv = "0"
	lcMotiv_inactiv = ""
	lcTip = 'CLIENTI'
	IF TYPE('toPartener.tip') = 'C'
		lcTip = UPPER(ALLTRIM(toPartener.tip))
	ENDIF 	
	lcIdString = IIF(m.lcTip = 'CLIENTI', "16", "17") && 16 = CLIENTI, 17 = FURNIZORI
	lcCorespDel = ""
	lcId_util = "-3"
	lcSqlInsert = [begin pack_def.adauga_partener('] + lcDenumire + [','] + lcNume + [','] + lcPrenume + [','] + lcCod_fiscal + [','] + ;
		lcReg_comert + [',] + lcId_loc_inreg + [,] + lcId_categ_ent + [,'] + lcPrefix + [','] + lcSufix + [',] + ;
		lcTip_persoana + [,'] + lcBanca + [','] + lcCont_Banca + [',] + lcinactiv + [,'] + lcMotiv_inactiv + [',] + ;
		lcId_util + [,'] + lcIdString + [','] + lcCorespDel + [',?@pnIdPart); end;]

	llSucces = goExecutor.oExecuta(m.lcSqlInsert)

	***********************************
	* Adresa partener
	***********************************

	lcIdPart = Alltrim(Str(m.pnIdPart))
	
	lcAdresa = 	Nvl(Strtran(Alltrim(Upper(toPartener.adresa)), ['], ['']), "")

	lcDenumire_adresa = ""
	lcDA_apare = "0"
	lcStrada = ""
	lcNumar = ""

	lcBloc = ""
	lcScara = ""
	lcApart = ""
	lcEtaj = ""
	lcId_loc = "NULL"
	lcLocalitate = ""
	lcId_Judet = "NULL"
	lcCodpostal = "NULL"
	lcId_tara =  "NULL"
	lcTelefon1 = ""
	lcTelefon2 = ""
	lcFax = ""
	lcEmail = ""
	lcWeb = ""
	lcPrincipala = Iif(m.pnNrAdrese = 0, "1", "0")
	lcinactiv = "0"
	lcId_util = "-3"
	
	lnItems = Getwordcount(m.lcAdresa, ',')
	For lnItem = 1 To m.lnItems
		lcItem = Alltrim(Getwordnum(m.lcAdresa, m.lnItem, ','))
		Do Case
			Case m.lnItem = 1 && strada
				lcStrada = LEFT(Alltrim(m.lcItem), 150)
			Case 'NR.' $ m.lcItem && nr strada
				lcNumar = LEFT(Alltrim(Strtran(m.lcItem, 'NR.', '')), 10)
			Case 'BL.' $ m.lcItem
				lcBloc = LEFT(Alltrim(Strtran(m.lcItem, 'BL.', '')), 30)
			Case 'SC.' $ m.lcItem
				lcScara = LEFT(Alltrim(Strtran(m.lcItem, 'SC.', '')), 10)
			Case 'ET.' $ m.lcItem
				lcEtaj = LEFT(Alltrim(Strtran(m.lcItem, 'ET.', '')), 20)
			Case 'AP.' $ m.lcItem
				lcApart = LEFT(Alltrim(Strtran(m.lcItem, 'AP.', '')), 10)
			Case 'COD POSTAL' $ m.lcItem
				lcCodpostal = ALLTRIM(STR(INT(VAL(LEFT(Alltrim(Strtran(m.lcItem, 'COD POSTAL', '')), 20)))))
			Case 'LOC.' $ m.lcItem
				lcLocalitate = Alltrim(Strtran(m.lcItem, 'LOC.', ''))
			Case 'JUD.' $ m.lcItem
				lcJudet = Alltrim(Strtran(m.lcItem, 'JUD.', ''))
			Case m.lnItem = m.lnItems - 1 And Empty(m.lcLocalitate)
				lcLocalitate = m.lcItem
			Case m.lnItem = m.lnItems
				lcTara = m.lcItem
		Endcase
	Endfor

	lnIdJudetBucuresti = 10
	lcJudetBucuresti = "BUCURESTI"
	lnIdLocalitateBucuresti = 1759
	lnIdTaraRO = 1
	
	If m.lcLocalitate = 'BUCURESTI'
		m.lcLocalitate = 'BUCURESTI SECTORUL 1'
	Endif
	If Empty(m.lcLocalitate)
		lcLocalitate = 'BUCURESTI SECTORUL 1'
	Endif
	If Empty(m.lcJudet)
		lcJudet = m.lcJudetBucuresti
	ENDIF
	
	* caut adresa dupa localitate. daca nu o gasesc, o adaug
	SELECT cAdrese
	LOCATE FOR id_part = m.pnIdPart AND localitate = m.lcLocalitate
	IF !FOUND()
		pnIdJudet = 0
		lnSucces = goExecutor.oSelect2Value([select j.id_judet, j.id_tara from syn_nom_judete j where j.judet = '] + m.lcJudet + [' and j.inactiv = 0 and j.sters = 0], @pnIdJudet)
	
		If m.lnSucces > 0
			IF EMPTY(NVL(m.pnIdJudet,0))
				lnSucces = goExecutor.oSelect2Value([select j.id_judet, j.id_tara from syn_nom_judete j where j.judet like '%] + m.lcJudetBucuresti + [%' and j.inactiv = 0 and j.sters = 0], @pnIdJudet)
				IF m.lnSucces > 0
					lnIdJudet = m.pnIdJudet
				ELSE
					lnIdJudet = m.lnIdJudetBucuresti	 										
				ENDIF 
			ELSE 
				lnIdJudet = m.pnIdJudet	
			ENDIF
		ELSE 
			This.Trace(goExecutor.cEroare)
			lnIdJudet = m.lnIdJudetBucuresti
		ENDIF 
		
		lcSql = [select l.id_loc, l.id_judet, j.id_tara from syn_nom_localitati l left join syn_nom_judete j on l.id_judet = j.id_judet where l.id_judet = ] + ALLTRIM(STR(m.lnIdJudet)) + [ and l.localitate = '] + m.lcLocalitate + [' and l.inactiv = 0 and l.sters = 0 order by l.localitate]
		lnSucces = goExecutor.oExecute(m.lcSql, 'cLocalitateTemp')
		If m.lnSucces > 0
			IF RECCOUNT('cLocalitateTemp') > 0
				Select cLocalitateTemp
				Go Top
				lcId_loc = Alltrim(Str(id_loc))
				lcId_Judet = Alltrim(Str(id_judet))
				lcId_tara = Alltrim(Str(id_tara))
				Use In (Select('cLocalitateTemp'))					
			ELSE
				Use In (Select('cLocalitateTemp'))					
				lcSql = [select l.id_loc, l.id_judet, j.id_tara from syn_nom_localitati l left join syn_nom_judete j on l.id_judet = j.id_judet where l.id_judet = ] + ALLTRIM(STR(m.lnIdJudet)) + [ and l.inactiv = 0 and l.sters = 0 order by l.localitate]
				lnSucces = goExecutor.oExecute(m.lcSql, 'cLocalitateTemp')
				If m.lnSucces > 0
					Select cLocalitateTemp
					Go Top
					lcId_loc = Alltrim(Str(id_loc))
					lcId_Judet = Alltrim(Str(id_judet))
					lcId_tara = Alltrim(Str(id_tara))					
				ELSE 
					This.Trace(goExecutor.cEroare)						
				ENDIF 
				Use In (Select('cLocalitateTemp'))
			ENDIF 
		ELSE
			This.Trace(goExecutor.cEroare)
		ENDIF 	

		IF EMPTY(NVL(m.lcId_loc, ''))
			lcId_loc = Alltrim(Str(m.lnIdLocalitateBucuresti))
			lcId_Judet = Alltrim(Str(m.lnIdJudetBucuresti))
			lcId_tara = Alltrim(Str(m.lnIdTaraRO))				
		Endif && lnSucces

		If m.lcId_loc <> 'NULL'
			pnIdAdresa = 0
*!*					* caut adresa dupa localitate. daca nu o gasesc, o adaug
*!*					lcSql = [SELECT id_adresa FROM adrese_parteneri where sters = 0 and inactiv = 0 and id_loc = ] + m.lcId_loc + [ AND id_part = ] + m.lcIdPart
*!*					lnSucces = goExecutor.oSelect2Value(m.lcSql, @pnIdAdresa)
*!*					If m.lnSucces < 0
*!*						lcMesaj = goExecutor.cEroare
*!*						Thisform.trace(m.lcMesaj)
*!*						AMessagebox(m.lcMesaj, 0 + 48, _Screen.Caption )
*!*						Exit
*!*					Endif

			If Empty(NVL(m.pnIdAdresa,0))
				lcSql = [begin pack_def.adauga_adresa_partener2(] + lcIdPart + [,'] + lcDenumire_adresa + [',] + lcDA_apare + [,] + ;
					['] + lcStrada + [','] + lcNumar + [','] + ;
					lcBloc + [','] + lcScara + [','] + lcApart + [','] + lcEtaj + [',] + lcId_loc + [,'] + lcLocalitate + [',] + lcId_Judet + [,] + lcCodpostal + [,] + lcId_tara + [,'] + ;
					lcTelefon1 + [','] + lcTelefon2 + [','] + lcFax + [','] + lcEmail + [','] + lcWeb + [',] + ;
					lcPrincipala + [,] + lcinactiv + [,] + lcId_util + [,?@pnIdAdresa); end;]

				lnSucces = goExecutor.oExecute(m.lcSql)
				If m.lnSucces < 0
					lcMesaj = goExecutor.cEroare
					Thisform.trace(m.lcMesaj)
					AMessagebox(m.lcMesaj, 0 + 48, _Screen.Caption )
					Exit
				Endif
			Endif && empty(m.pnIdAdresa)

		Endif && m.lcId_loc <> 'NULL'
	ENDIF && !found()	

ENDPROC && Adauga_partener
 