<cfimport prefix="m" taglib="../lib"/>
<cfimport prefix="d" taglib="../lib/data"/>

<cfparam name="ATTRIBUTES.wz"/>
<cfparam name="ATTRIBUTES.crm_cache_base" default="https://elma-cache.adl.nubes.ru/contract_ls.cfm"/>
<cfparam name="ATTRIBUTES.out" default="qComponent"/>

<cfhttp method="get" 	
	url="#ATTRIBUTES.crm_cache_base#?wz=#ATTRIBUTES.wz#"
	timeout=10
	charset="utf-8"	
	result="res"
>	
</cfhttp>

<cfset local={}/>

<cfset company=deserializeJson(res.fileContent)/>
<!--- <cfdump var=#company#/> --->
<!--- *** это не слишком надежно, некорректно представленный NULL (обыкновенно представляемый пустой строкой) может все сломать. Да и корректный --->
<!--- мартышкин труд собирать этот запрос из JSON , может, лучше было динамически --->
<cfset qComponent = queryNew("
	 contractId
	,contractOutsideId

	,additionalAgreementId
	,additionalAgreementName

	,dealIndex
	,serviceId
	,serviceType
	,catalogName
	,serviceCode
	,serviceCustomName
	,serviceStart
	,serviceEnd
	,serviceDiscount
	,serviceQuantity
	,serviceHash

	,paramId	
	,componentCode	
	,component	
	,measureShort	
	,quantity	
	,priceWVat	
	,discount	
	,atomicCode
	,fullCode
")/>

<!--- this.nullSupport = true in Application.cfc ... no way --->
<cfloop array=#company.contracts# item="local.contract"><!--- имя массива писать в ##, иначе думает, что это строковый литерал --->
	<cfloop array=#local.contract.additionalAgreements# item="local.additionalAgreement">
		<cfif NOT local.additionalAgreement.isActual><cfcontinue/></cfif>
		<cfloop array=#local.additionalAgreement.services# item="local.service">
			<cfloop array=#local.service.components# item="local.component">
				<cfset queryAddRow(qComponent, [{
				contractId=local.contract.contractId,
				contractOutsideId=local.contract.contractOutsideId,
				
				additionalAgreementId=local.additionalAgreement.additionalAgreementId,
				additionalAgreementName=local.additionalAgreement.additionalAgreementName,
				
				dealIndex=local.service.dealIndex ,
				serviceId=local.service.serviceId ,
				serviceType=local.service.serviceType,
				catalogName=local.service.catalogName,
				serviceCode=local.service.serviceCode,
				serviceCustomName=local.service.serviceCustomName,
				serviceStart=parseDateTime(local.service.serviceStart.ts),
				serviceEnd=isDate(structGet("local.service.serviceEnd.ts"))? parseDateTime(local.service.serviceEnd.ts):javacast('null',''),
				serviceDiscount=local.service.discount,
				serviceQuantity=local.service.quantity,
				serviceHash=local.service.serviceHash,
				
				paramId=local.component.paramId,
				componentCode=local.component.componentCode,
				component=local.component.component,
				measureShort=local.component.measureShort,
				quantity=local.component.quantity,
				priceWVat=local.component.priceWVat,
				discount=local.component.discount,
				atomicCode=local.component.atomicCode,
				fullCode="#local.component.componentCode#-#listGetAt('i,f,m',local.service.serviceType)#"
				}])/>
			</cfloop>
		</cfloop>
	</cfloop>	
</cfloop>

<!--- <cfdump var=#qComponent#/> --->
<cfset "CALLER.#ATTRIBUTES.out#" = qComponent/>
<cfexit method="exittag"/>