FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:bp
:TCMOD:tmm
:TFLNO:100
:FUNC:START
function void fill.bptmm100(
		domain	tcmcs.long	i.type,
		domain	tcmcs.long	i.seqn,
		domain	bcstr22		i.rgdt,
		domain	bcstr22		i.trdt,			|#147118.n
		domain	tisfc.prtm	i.hrea,
		domain	tccwtt		i.cwtt,			|#200-100940.n
		domain	bcstr3		i.chlt,
		domain	tisfc.prtm	i.hrma,
		domain	bcstr22		i.stdt,
		domain	bcstr22		i.endt,
		domain	tcprcg		i.perc,
		domain	tctxtn		i.txta)
{
|	bptmm100.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm100.year = tihra100.year		|* FILLED IN RULE
|	bptmm100.peri = tihra100.week		|* FILLED IN RULE
	on case i.type
	case GENERAL_TASK:
		var.bptmm100.type = target(bptmm.type.general)
		break
	case PCS:
		var.bptmm100.type = target(bptmm.type.pcs.project)
		break
	case PRODUCTION:
		var.bptmm100.type = target(bptmm.type.prod.order)
		break
	case SERVICE:
		var.bptmm100.type = target(bptmm.type.serv.order)
		break
	endcase
	var.bptmm100.seqn = i.seqn
								|#147118.so
|	bptmm100.trdt = utc(tihra100.trdt, 0)	|* FILLED IN RULE
								|#147118.eo
	var.bptmm100.trdt = i.trdt				|#147118.n
|	bptmm100.htst = tihra100.htst		|* FILLED IN RULE
	var.bptmm100.rgdt = i.rgdt
|	bptmm100.cwoc = tihra100.cwoc		|* FILLED IN RULE
|	bptmm100.wtsc = tihra100.cwtt				|#200-100940.o
	var.bptmm100.wtsc = i.cwtt				|#200-100940.n
|	bptmm100.cuni = bcsbp000.dsbt		|* FILLED IN RULE
	var.bptmm100.hrea = i.hrea
	var.bptmm100.chlt = i.chlt
	var.bptmm100.hrma = i.hrma
	var.bptmm100.stdt = i.stdt
|	bptmm100.stup = target(tcyesno.no)	|* FILLED IN RULE
	var.bptmm100.endt = i.endt
	var.bptmm100.perc = i.perc
|	bptmm100.cnfd = target(tcyesno.no)	|* FILLED IN RULE
|	bptmm100.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm100.cgrp = empty()		|* FILLED IN RULE
|	bptmm100.hrdv = target(tcyesno.no)	|* FILLED IN RULE
|	bptmm100.proc = target(tcyesno.no)	|* FILLED IN RULE
|	bptmm100.logn = logname$		|* FILLED IN RULE
|	bptmm100.agui = empty()			|* FILLED IN RULE
|	bptmm100.drpr = tihra100.crpr		|* FILLED IN RULE
|	bptmm100.crpr = tihra100.srpr		|* FILLED IN RULE
	var.bptmm100.txta = i.txta
							|#GEN-0205.sn
	var.bptmm100.emdp = 
		get.employee.department(tihra100.emno)
							|#GEN-0205.en
	dump 100
}

function void fill.bptmm111()
{
	domain	tisfc.prtm	day.hrea
	domain	tctxtn		day.txta
	domain	tcmcs.long	day.seq.nr

	domain	bcstr22		start.date
	domain	bcstr22		reg.date

|	bptmm111.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm111.year = tihra100.year			|* FILLED IN RULE
|	bptmm111.peri = tihra100.week			|* FILLED IN RULE
|	bptmm111.type = target(bptmm.type.general)	|* FILLED IN RULE
|	bptmm111.seqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm111.cgrp = empty()				|* FILLED IN RULE
|	bptmm111.cprj = empty()				|* FILLED IN RULE
|	bptmm111.cspa = empty()				|* FILLED IN RULE
|	bptmm111.cact = empty()				|* FILLED IN RULE
|	bptmm111.cstl = empty()				|* FILLED IN RULE
|	bptmm111.swct = target(bpmdm.swct.labor)	|* FILLED IN RULE
|	bptmm111.ccun = empty()				|* FILLED IN RULE

|	bptmm111.seqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration
	var.bptmm111.seqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				GENERAL_TASK)		
								|#158562.en
				
	ret = get.general.task(	bcsbp.orig.ti,
				str$(tihra100.tano),
				0,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm111.gtsk = bcsbp004.ntsk
		var.bptmm111.dsca = bcsbp004.ntsd
	else
		var.bptmm111.gtsk = empty()
		var.bptmm111.dsca = empty()
	endif
|	bptmm111.expn = empty()				|* FILLED IN RULE
	var.bptmm111.cpcp = bcsbp000.cpgh
|	bptmm111.cwoc = tihra100.cwoc			|* FILLED IN RULE
	set.mem(var.bptmm111.hrea, 0.0)
	day.number = convert.dayname.to.daynumber(tihra100.stdy)|#PERF.n
								|#PERF.so
|	day.number = itihra0001.convert.dayname.to.daynumber(tihra100.stdy)
								|#PERF.eo
	var.bptmm111.hrea(day.number) = tihra100.hrea
	var.bptmm111.hrea(8) = tihra100.hrea
	ret = get.labor.type(	bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm111.chlt = bcsbp002.nhlt
	else
		var.bptmm111.chlt = empty()
	endif
|	bptmm111.rgdt = empty()				|* FILLED IN RULE
								|#147118.so
|	bptmm111.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm111.trdt = get.transaction.date(tihra100.trdt)	|#147118.n
|	bptmm111.quan = empty()				|* FILLED IN RULE
|	bptmm111.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm111.pric = empty()				|* FILLED IN RULE
|	bptmm111.ccur = empty()				|* FILLED IN RULE
|	bptmm111.amnt = empty()				|* FILLED IN RULE
|	bptmm111.rdat = empty()				|* FILLED IN RULE
|	bptmm111.rate = empty()				|* FILLED IN RULE
|	bptmm111.rate(2) = empty()			|* FILLED IN RULE
|	bptmm111.rate(3) = empty()			|* FILLED IN RULE
|	bptmm111.ratf = empty()				|* FILLED IN RULE
|	bptmm111.ratf(2) = empty()			|* FILLED IN RULE
|	bptmm111.ratf(3) = empty()			|* FILLED IN RULE
|	bptmm111.rtyp = empty()				|* FILLED IN RULE
|	bptmm111.cnfd = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm111.proc = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm111.hrdv = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm111.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm111.logn = logname$			|* FILLED IN RULE
|	bptmm111.blbl = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm111.agui = empty()				|* FILLED IN RULE
	day.txta = fill.text.fields(day.number)

	dump 111

	if day.number <> 0 then
		day.hrea = var.bptmm111.hrea(day.number)
|		day.seq.nr = (tihra100.wsrn * 10) + day.number		|#158562.o
		day.seq.nr = (var.bptmm111.seqn * 10) + day.number	|#158562.n
	else
		day.hrea = var.bptmm111.hrea(8)
|		day.seq.nr = (tihra100.wsrn * 10) + 8			|#158562.o
		day.seq.nr = (var.bptmm111.seqn * 10) + 8		|#158562.n
	endif

	start.date = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				1200)
	|* Time part of start date for General Tasks is always 12:00:00.

	fill.bptmm150(
		day.seq.nr,
		start.date,
		var.bptmm111.gtsk,
		var.bptmm111.dsca,
		var.bptmm111.cpcp)

	reg.date = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
	|* The registration date in bptmm100 is always the date part of
	|* the start date.

	fill.bptmm100(
		GENERAL_TASK,
		day.seq.nr,
		reg.date,
		var.bptmm111.trdt,				|#147118.n
		day.hrea,
		"",		|* Working Time Schedule Code	|#200-100940.n
		var.bptmm111.chlt,
		0.0,		|* Machine Time
		start.date,
		utc(0, 0),	|* End Date
		0.0,		|* Percentage Completed
		day.txta)
}

function void fill.bptmm120()
{
|	bptmm120.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm120.year = tihra100.year			|* FILLED IN RULE
|	bptmm120.peri = tihra100.week			|* FILLED IN RULE
|	bptmm120.seqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm120.seqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm120.seqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				PRODUCTION)		
								|#158562.en
	var.bptmm120.stdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	ret = get.labor.type(	bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm120.chlt = bcsbp002.nhlt
	else
		var.bptmm120.chlt = ""
	endif
	var.bptmm120.wtsc = get.wts.code(tihra100.cwtt,		|#200-100940.sn
					 tihra100.stdy,
					 tihra100.sttm,
					 tihra100.entm,
					 var.bptmm120.chlt)	|#200-100940.en
|	if not isspace(tihra100.cwtt) then			|#200-100940.o
	if not isspace(var.bptmm120.wtsc) then			|#200-100940.n
		var.bptmm120.endt = convert.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm120.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm120.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm120.trdt = get.transaction.date(tihra100.trdt)	|#147118.n

	var.bptmm120.rgdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm120.htst = tihra100.htst			|* FILLED IN RULE
|	bptmm120.cgrp = empty()				|* FILLED IN RULE
|	bptmm120.wtsc = tihra100.cwtt				|#200-100940.o
|	bptmm120.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm120.hrea = tihra100.hrea			|* FILLED IN RULE
|	bptmm120.hrma = tihra100.hrma			|* FILLED IN RULE
|	bptmm120.stup = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm120.orno = convert.number(bcckon.act.sfc, tihra100.pdno)	|* FILLED IN RULE
|	bptmm120.opno = tihra100.opno			|* FILLED IN RULE
|	bptmm120.tano = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm120.mcno = tihra100.mcno			|* FILLED IN RULE
|	bptmm120.perc = tihra100.pccp			|* FILLED IN RULE
|	bptmm120.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm120.cprj = get.ti.project(tihra100.cprj)	|* FILLED IN RULE
|	bptmm120.cact = tihra100.cact			|* FILLED IN RULE
|	bptmm120.hrdv = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm120.cnfd = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm120.proc = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm120.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm120.logn = logname$			|* FILLED IN RULE
|	bptmm120.agui = empty()				|* FILLED IN RULE
|	bptmm120.txta = tihra100.txta			|* FILLED IN RULE

	dump 120

	fill.bptmm100(
		PRODUCTION,
|		tihra100.wsrn,					|#158562.o
		var.bptmm120.seqn,				|#158562.n
		var.bptmm120.rgdt,
		var.bptmm120.trdt,				|#147118.n
		tihra100.hrea,
		var.bptmm120.wtsc,				|#200-100940.n
		var.bptmm120.chlt,
		tihra100.hrma,
		var.bptmm120.stdt,
		var.bptmm120.endt,
		tihra100.pccp,
		tihra100.txta)
}

function void fill.bptmm130()
{
|	bptmm130.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm130.year = tihra100.year			|* FILLED IN RULE
|	bptmm130.peri = tihra100.week			|* FILLED IN RULE
|	bptmm130.seqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm130.seqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm130.seqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				SERVICE)		
								|#158562.en

	var.bptmm130.stdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	ret = get.labor.type(	bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm130.chlt = bcsbp002.nhlt
	else
		var.bptmm130.chlt = ""
	endif
	var.bptmm130.wtsc = get.wts.code(tihra100.cwtt,		|#200-100940.sn
					 tihra100.stdy,
					 tihra100.sttm,
					 tihra100.entm,
					 var.bptmm130.chlt)	|#200-100940.en
|	if not isspace(tihra100.cwtt) then			|#200-100940.o
	if not isspace(var.bptmm130.wtsc) then			|#200-100940.n
		var.bptmm130.endt = convert.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm130.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm130.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm130.trdt = get.transaction.date(tihra100.trdt)	|#147118.n

	var.bptmm130.rgdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm130.cgrp = empty()				|* FILLED IN RULE
|	bptmm130.wtsc = tihra100.cwtt				|#200-100940.o
|	bptmm130.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm130.hrea = tihra100.hrea			|* FILLED IN RULE
|	bptmm130.orno = convert.number(bcckon.sma.order, tihra100.pdno)	|* FILLED IN RULE
|	bptmm130.stsk = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm130.acln = empty()				|* FILLED IN RULE
|	bptmm130.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm130.lbtv = target(bptmm.serl.labo)		|* FILLED IN RULE
|	bptmm130.lbln = empty()				|* FILLED IN RULE
|	bptmm130.tvln = empty()				|* FILLED IN RULE
	var.bptmm130.cpcp = get.cost.component.from.ts(
					tihra100.pdno,
					tihra100.tano)
|	bptmm130.hrdv = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm130.cnfd = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm130.proc = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm130.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm130.logn = logname$			|* FILLED IN RULE
|	bptmm130.agui = empty()				|* FILLED IN RULE
|	bptmm130.txta = tihra100.txta			|* FILLED IN RULE

	dump 130

	fill.bptmm100(
		SERVICE,
|		tihra100.wsrn,					|#158562.o
		var.bptmm130.seqn,				|#158562.n
		var.bptmm130.rgdt,
		var.bptmm130.trdt,				|#147118.n
		tihra100.hrea,
		var.bptmm130.wtsc,				|#200-100940.n
		var.bptmm130.chlt,
		0.0,		|* Machine Time
		var.bptmm130.stdt,
		var.bptmm130.endt,
		0.0,		|* Percentage Completed
		tihra100.txta)
}

function void fill.bptmm150(
		domain	tcmcs.long	i.seqn,
		domain	bcstr22		i.stdt,
		domain	bcstr8		i.gtsk,
		domain	tcdsca		i.dsca,
		domain	bcstr8		i.cpcp)
{
|	bptmm150.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm150.year = tihra100.year			|* FILLED IN RULE
|	bptmm150.peri = tihra100.week			|* FILLED IN RULE
	var.bptmm150.seqn = i.seqn
|	bptmm150.cgrp = empty()			|* FILLED IN RULE
	var.bptmm150.stdt = i.stdt
	var.bptmm150.gtsk = i.gtsk
|	bptmm150.expn = empty()			|* FILLED IN RULE
	var.bptmm150.dsca = i.dsca
|	bptmm150.cwoc = tihra100.cwoc		|* FILLED IN RULE
	var.bptmm150.cpcp = i.cpcp
|	bptmm150.quan = empty()			|* FILLED IN RULE
|	bptmm150.cuni = bcsbp000.dsbt		|* FILLED IN RULE
|	bptmm150.pric = empty()			|* FILLED IN RULE
|	bptmm150.ccur = empty()			|* FILLED IN RULE
|	bptmm150.amnt = empty()			|* FILLED IN RULE
|	bptmm150.rdat = empty()			|* FILLED IN RULE
|	bptmm150.rate(1) = empty()		|* FILLED IN RULE
|	bptmm150.rate(2) = empty()		|* FILLED IN RULE
|	bptmm150.rate(3) = empty()		|* FILLED IN RULE
|	bptmm150.ratf(1) = empty()		|* FILLED IN RULE
|	bptmm150.rate(2) = empty()		|* FILLED IN RULE
|	bptmm150.rate(3) = empty()		|* FILLED IN RULE
|	bptmm150.rtyp = empty()			|* FILLED IN RULE

	dump 150
}

function void fill.bptmm170()
{
|	bptmm170.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm170.year = tihra100.year			|* FILLED IN RULE
|	bptmm170.peri = tihra100.week			|* FILLED IN RULE
|	bptmm170.seqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm170.seqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm170.seqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				PCS)		
								|#158562.en

	var.bptmm170.stdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	ret = get.labor.type(	bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm170.chlt = bcsbp002.nhlt
	else
		var.bptmm170.chlt = ""
	endif
	var.bptmm170.wtsc = get.wts.code(tihra100.cwtt,		|#200-100940.sn
					 tihra100.stdy,
					 tihra100.sttm,
					 tihra100.entm,
					 var.bptmm170.chlt)	|#200-100940.en
|	if not isspace(tihra100.cwtt) then			|#200-100940.o
	if not isspace(var.bptmm170.wtsc) then			|#200-100940.n
		var.bptmm170.endt = convert.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm170.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm170.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm170.trdt = get.transaction.date(tihra100.trdt)	|#147118.n

	var.bptmm170.rgdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm170.htst = tihra100.htst			|* FILLED IN RULE
|	bptmm170.cgrp = empty()				|* FILLED IN RULE
|	bptmm170.wtsc = tihra100.cwtt				|#200-100940.o
|	bptmm170.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm170.hrea = tihra100.hrea			|* FILLED IN RULE
|	bptmm170.hrma = tihra100.hrma			|* FILLED IN RULE
|	bptmm170.cprj = get.ti.project(tihra100.cprj)	|* FILLED IN RULE
|	bptmm170.cact = tihra100.cact			|* FILLED IN RULE
|	bptmm170.tano = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm170.mcno = tihra100.mcno			|* FILLED IN RULE
|	bptmm170.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm170.hrdv = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm170.cnfd = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm170.proc = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm170.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm170.logn = logname$			|* FILLED IN RULE
|	bptmm170.agui = empty()				|* FILLED IN RULE
|	bptmm170.txta = tihra100.txta			|* FILLED IN RULE

	dump 170

	fill.bptmm100(
		PCS,
|		tihra100.wsrn,					|#158562.o
		var.bptmm170.seqn,				|#158562.n
		var.bptmm170.rgdt,
		var.bptmm170.trdt,				|#147118.n
		tihra100.hrea,
		var.bptmm170.wtsc,				|#200-100940.n
		var.bptmm170.chlt,
		tihra100.hrma,
		var.bptmm170.stdt,
		var.bptmm170.endt,
		0.0,		|* Percentage Completed
		tihra100.txta)
}

function string convert.day.to.utc.date(
		domain	tcyrno		i.year,
		domain	tcweek		i.week,
		domain	tcdynm		i.dayname,
		domain	tctmhs		i.time)
{
|	Dllusage
|	Expl:	This function calculates the utc-date based on an input day and
|		an input time.
|	Input:
|		i.year - Year
|		i.week - Week Number
|		i.dayname - Enumerate (Monday, Tuesday, etc.)
|		i.time - Time (interpretation depends on tihra100.cltm)
|	Enddllusage

		domain	tcmcs.long	l.day.number
		domain	tcmcs.long	l.utc.date.part
		domain	tcmcs.long	l.utc.time.part
		domain	tcmcs.long	l.number.of.hours

	l.day.number = convert.dayname.to.daynumber(i.dayname)	|#PERF.sn
	l.utc.date.part = week.to.num(	i.week,
					i.year,
					l.day.number)		|#PERF.en
								|#PERF.so
|	l.utc.date.part = itihra0001.convert.dayname.to.date(	i.year,
|								i.week,
|								i.dayname)
								|#PERF.eo
	if i.time <> 0 then
		read.hra.parameters()

		if tihra000.cltm = tccltm.minutes then
		|* 850 is to be interpreted as 08:50.
			l.number.of.hours = i.time/100
			l.utc.time.part = (l.number.of.hours * 60 * 60) +
				((i.time - l.number.of.hours * 100) * 60)
		else
		|* 850 is to be interpreted as 08:30.
			l.number.of.hours = i.time/100
			l.utc.time.part = (l.number.of.hours * 60 * 60) +
				((i.time - l.number.of.hours * 100) *
							tihra000.perl * 60)
		endif
	else
		l.utc.time.part = 0
		return(utc(l.utc.date.part, l.utc.time.part, 2)) |#225709.n
	endif

	return(utc(l.utc.date.part, l.utc.time.part))
}

function domain tcmcs.long convert.dayname.to.daynumber(	|#PERF.sn
		domain	tcdynm		i.day.name)
{
		domain	tcmcs.long	l.ret
		string			l.dummy
	static	domain	tcncmp		s.company
	static	domain	tcmcs.long	s.first.day.of.week
		domain	tcmcs.long	l.day.nr

	if s.company <> ext.lng.comp then
		l.ret = tt.company(	ext.lng.comp,
					l.dummy,
					l.dummy,
					s.first.day.of.week)
		s.company = ext.lng.comp
	endif

	if i.day.name = empty then
		i.day.name = ltoe(s.first.day.of.week)
	endif

	on case i.day.name
		case tcdynm.sunday:
			l.day.nr = 1
			break
		case tcdynm.monday:
			l.day.nr = 2
			break
		case tcdynm.tuesday:
			l.day.nr = 3
			break
		case tcdynm.wednesday:
			l.day.nr = 4
			break
		case tcdynm.thursday:
			l.day.nr = 5
			break
		case tcdynm.friday:
			l.day.nr = 6
			break
		case tcdynm.saturday:
			l.day.nr = 7
			break
		default:
			l.day.nr = 0
			break
	endcase

	if l.day.nr <> 0 then
		|* Make correction for first day of the week
		l.day.nr = l.day.nr + 1 + (7 - s.first.day.of.week)
		if l.day.nr > 7 then
			l.day.nr = l.day.nr - 7
		endif
	endif

	return(l.day.nr)
}								|#PERF.en

function domain tccwtt get.wts.code(				|#200-100940.sn
		domain	tccwtt		i.working.time.table,
		domain	tcdynm		i.day.name,
		domain	tctmhs		i.start.time,
		domain	tctmhs		i.end.time,
		domain	tcckow		i.labor.type)
{
	|* In BaanIVc4 the labor type of the working time table serves as
	|* default value for the hours record.
	|* In BaanERP6.1a the labor type of the hours record must be according
	|* to the labor type as it is present in the working time schedule.
	|* The function below checks if the labor type of the hours record is
	|* according to the working time table. Is this is not the case, then
	|* the working time table is set to empty (code in selectempty). In this
	|* way the BaanVI DAL checks is working fine. This solution is
	|* acceptable, because after having saved an hours record, the working
	|* time table has not any functionality anymore.

		domain	tctmhs		wtt.start.time
		domain	tctmhs		wtt.end.time

	if isspace(i.working.time.table) or
	   isspace(i.labor.type) then
		return("")
	endif

	select	tihra120.sttm :wtt.start.time,
		tihra120.entm :wtt.end.time
	from	tihra120
	where	tihra120._index1 = {	:i.working.time.table,
					:i.day.name}
	and	tihra120.ckow = :i.labor.type
	selectdo
		if i.start.time >= wtt.start.time and
		   i.end.time <= wtt.end.time then
			return(i.working.time.table)
		endif
	selectempty
		return("")
	endselect

	return("")
}								|#200-100940.en

function domain	tctxtn fill.text.fields(
		domain	tcmcs.long	day.number)
{
	var.bptmm111.txta = 0

	var.bptmm111.txtb = 0
	var.bptmm111.txtc = 0
	var.bptmm111.txtd = 0
	var.bptmm111.txte = 0
	var.bptmm111.txtf = 0
	var.bptmm111.txtg = 0

	on case day.number
	case 0:
	case 1:
		var.bptmm111.txta = tihra100.txta

		return(var.bptmm111.txta)
		break
	case 2:
		var.bptmm111.txtb = tihra100.txta
		return(var.bptmm111.txtb)
		break
	case 3:
		var.bptmm111.txtc = tihra100.txta
		return(var.bptmm111.txtc)
		break
	case 4:
		var.bptmm111.txtd = tihra100.txta
		return(var.bptmm111.txtd)
		break
	case 5:
		var.bptmm111.txte = tihra100.txta
		return(var.bptmm111.txte)
		break
	case 6:
		var.bptmm111.txtf = tihra100.txta
		return(var.bptmm111.txtf)
		break
	case 7:
		var.bptmm111.txtg = tihra100.txta
		return(var.bptmm111.txtg)
		break
	default:
		return(0)
	endcase

	return(0)
}

function void read.hra.parameters()
{
	static	domain	tcbool		s.hra.parameters.read

	if s.hra.parameters.read then
		return
	endif

	select	tihra000.cltm, tihra000.perl
	from	tihra000
	where	tihra000._index1 = {1}
	as set with 1 rows
	selectdo
		s.hra.parameters.read = true
	endselect
}

function domain tccpcp get.cost.component.from.ts(
		domain	tcpdno		i.orno,
		domain	tctano		i.task)
{
		domain	tccuno		l.customer
		domain	tccpcp		l.cost.component
		domain	tcitem		l.item
		domain	tcpric		o.dummy.pric
		domain	tssma.mess	o.dummy.mess

	l.customer = read.customer.from.service.order(i.orno)

	|if itssma0011.search.rate.from.task(		|#MCR.so
	|				l.customer,
	|				i.task,
	|				l.item,		|* Output
	|				o.dummy.pric,
	|				o.dummy.mess) then
							|#MCR.eo
	if search.rate.from.task(			|#MCR.sn
				l.customer,
				i.task,
				l.item,			|* Output
				o.dummy.pric,
				o.dummy.mess) then	|#MCR.en
		l.cost.component = read.cost.component.from.item(l.item)
		get.cost.components(
				bcorig.ticpr,
				l.cost.component,
				"",
				dummy.err.mess)
		return(bcstc006.cost)
	else
		l.cost.component = bcsbp000.cpcp
		return(l.cost.component)
	endif
}

function domain tccuno read.customer.from.service.order(
		domain	tcpdno		i.orno)
{
	static	domain	tcpdno		s.orno
	static	domain	tccuno		s.cuno

	if s.orno = i.orno then
		return(s.cuno)
	endif

	select	tssma301.cuno :s.cuno
	from	tssma301
	where	tssma301._index1 = {:i.orno}
	as set with 1 rows
	selectdo
		s.orno = i.orno
	endselect

	return(s.cuno)
}

function domain tccpcp read.cost.component.from.item(
		domain	tcitem		i.item)
{
	static	domain	tcitem		s.item
	static	domain	tccpcp		s.cpcp

	if s.item = i.item then
		return(s.cpcp)
	endif


	select	tiitm001.cpcp :s.cpcp
	from	tiitm001
	where	tiitm001._index1 = {:i.item}
	as set with 1 rows
	selectdo
		s.item = i.item
	endselect

	return(s.cpcp)
}

function domain	bcstr22 get.transaction.date(			|#147118.sn
		domain	tidate		i.transaction.date)
{
	if i.transaction.date = 0 then
		|* Transaction Date not filled; fill with
		|* current time stamp.
		return( utc(date.num(), time.num()) )
	endif
		
	return( utc(i.transaction.date, 0) )
}								|#147118.en

								|#158562.sn
function domain tcmcs.long get.sequence.number(
		domain	tcyrno		i.year,
		domain	tcweek		i.week,
		domain	tcemno		i.emno,
		domain	tcmcs.long	i.type)
{
|	Dllusage
|	Expl:	This function determines the sequence number. 
|		The sequence number is not taken anymore from BaanIV because
|		there were too large numbers present which did not fit in the domain
|		present in LN (maximum of domain bpmdm.serd is 9999; maximum
|		of domain tihra.sern is 99999).
|		Now tihra100 is read with order by on year, week, emno, koht. Then
|		we can determine the sequence number in the right order to be used
|		in LN.
|	Input:
|		i.year - Year
|		i.week - Week Number
|		i.emno - Employee
|		i.type - Indicates type of hours
|	Enddllusage

	static	domain	tcyrno		hold.year
	static	domain	tcweek		hold.week
	static	domain	tcemno		hold.emno
	static	domain	tcmcs.long	hold.type
	static	domain	tcmcs.long	hold.sequence

	if hold.year = i.year and
	   hold.week = i.week and
	   hold.emno = i.emno and
	   hold.type = i.type then
		hold.sequence = hold.sequence + 1
		return(hold.sequence)
	endif

	hold.year = i.year
	hold.week = i.week
	hold.emno = i.emno
	hold.type = i.type
	hold.sequence = 1
	return(hold.sequence)
}								|#158562.en

								|#GEN-0205.sn
function domain tccwoc get.employee.department(
		domain	tcemno		i.employee)
{
	static	domain	tcemno		s.employee
	static	domain	tccwoc		s.employee.department	fixed

	if s.employee = i.employee then
		return(s.employee.department)
	endif

	s.employee = i.employee
	
	select	tccom001.cwoc:s.employee.department
	from	tccom001
	where	tccom001._index1 = {:i.employee}
	as set with 1 rows
	selectdo
		break
	selectempty
		s.employee.department = ""
	endselect
	
	return(s.employee.department)
}
								|#GEN-0205.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:bp
:TCMOD:tmm
:TFLNO:200
:FUNC:START
function void fill.and.dump.bptmm200(
		domain	tcemno		i.original.employee.code, |#GEN-0205.n
		domain	bcstr6		i.emno,
		domain	tcyrno		i.year,
		domain	tcpern		i.peri,
		domain	tcmcs.long	i.type,
		domain	tcmcs.long	i.seqn,
		domain	bcstr6		i.cwoc,
		domain	bcstr3		i.wtsc,
		domain	tisfc.prtm	i.hrea,
		domain	tisfc.prtm	i.hrma,
		domain	bcstr3		i.chlt,
		domain	bcstr22		i.stdt,
		domain	bcstr22		i.endt,
		domain	tcprcg		i.perc,
		domain	bcstr22		i.rgdt,
|		domain	tcamnt		i.prlc,			|#MCR.so
|		domain	tcamnt		i.prmc,
|		domain	tcamnt		i.proc,			|#MCR.eo
	ref	domain	tcamnt		i.prlc(),		|#MCR.sn
	ref	domain	tcamnt		i.prmc(),
	ref	domain	tcamnt		i.proc(),		|#MCR.en
		domain	bcstr22		i.trdt,
		domain	tclogn		i.logn,
		domain	tctxtn		i.txta)
{
	var.bptmm200.emno = fill.field.string(i.emno)
|	var.bptmm200.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
	var.bptmm200.year = fill.field.long(i.year)
	var.bptmm200.peri = fill.field.long(i.peri)
	on case i.type
	case GENERAL_TASK:
		var.bptmm200.type = 1
		break
	case PROJECT:
		var.bptmm200.type = 2
		break
	case PCS:
		var.bptmm200.type = 3
		break
	case PRODUCTION:
		var.bptmm200.type = 4
		break
	case SERVICE_TI:
	case SERVICE_TP:
		var.bptmm200.type = 6
		break
	endcase
	var.bptmm200.psqn = fill.field.long(i.seqn)
	var.bptmm200.cwoc = fill.field.string(i.cwoc)
	var.bptmm200.wtsc = fill.field.string(i.wtsc)
|	bptmm200.cuni = bcsbp000.dsbt			|* FILLED IN RULE
	var.bptmm200.hrea = fill.field.double(i.hrea)
	var.bptmm200.hrma = fill.field.double(i.hrma)
	var.bptmm200.chlt = fill.field.string(i.chlt)
|	bptmm200.stup = target(tcyesno.no)		|* FILLED IN RULE
	var.bptmm200.stdt = fill.field.string(i.stdt)
	var.bptmm200.endt = fill.field.string(i.endt)
	var.bptmm200.perc = fill.field.double(i.perc)
|	bptmm200.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
	var.bptmm200.rgdt = fill.field.string(i.rgdt)

|	var.bptmm200.prlc = fill.field.double(i.prlc)		|#MCR.so
|	var.bptmm200.prmc = fill.field.double(i.prmc)
|	var.bptmm200.proc = fill.field.double(i.proc)		|#MCR.eo

	var.bptmm200.prlc(1) = fill.field.double(i.prlc(1))	|#MCR.sn
	var.bptmm200.prlc(2) = fill.field.double(i.prlc(2))
	var.bptmm200.prlc(3) = fill.field.double(i.prlc(3))
	var.bptmm200.prmc(1) = fill.field.double(i.prmc(1))
	var.bptmm200.prmc(2) = fill.field.double(i.prmc(2))
	var.bptmm200.prmc(3) = fill.field.double(i.prmc(3))
	var.bptmm200.proc(1) = fill.field.double(i.proc(1))
	var.bptmm200.proc(2) = fill.field.double(i.proc(2))
	var.bptmm200.proc(3) = fill.field.double(i.proc(3))	|#MCR.en

	var.bptmm200.trdt = fill.field.string(i.trdt)
|	bptmm200.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm200.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm200.logn = fill.field.string(i.logn)
	var.bptmm200.txta = fill.field.long(i.txta)
							|#GEN-0205.sn
	var.bptmm200.emdp = 
		get.employee.department(i.original.employee.code)
							|#GEN-0205.en
	dump 200
}

function void fill.and.dump.bptmm210(
		domain	tcmcs.long	i.psqn,
		domain	bcstr22		i.stdt,
		domain	tcmcs.long	i.swct,
		domain	bcstr8		i.cpcp)
{
|	bptmm210.emno = format.emno(tphrs100.emno)	|* FILLED IN RULE
|	bptmm210.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
|	bptmm210.year = tphrs100.year			|* FILLED IN RULE
|	bptmm210.peri = tphrs100.peri			|* FILLED IN RULE
	var.bptmm210.psqn = i.psqn
	var.bptmm210.stdt = i.stdt
|	bptmm210.cprj = get.tp.project(tphrs100.cprj)	|* FILLED IN RULE
	var.bptmm210.swct = i.swct
|	bptmm210.cspa = tphrs100.cspa			|* FILLED IN RULE
|	bptmm210.cact = tphrs100.cact			|* FILLED IN RULE
|	bptmm210.cstl = tphrs100.cstl			|* FILLED IN RULE
	var.bptmm210.cpcp = i.cpcp
|	bptmm210.ccun = tphrs100.ccun			|* FILLED IN RULE
	var.bptmm210.dsca = get.cost.object.description(
				tphrs100.cotp,
				tphrs100.ccun)
|	bptmm210.quan = empty()				|* FILLED IN RULE
|	bptmm210.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm210.pric = empty()				|* FILLED IN RULE
|	bptmm210.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
|	bptmm210.amnt = tphrs100.amnt			|* FILLED IN RULE
	if tphrs100.amnt = 0.0 then
		|* Hours record
		var.bptmm210.rdat = utc(0, 0)
		var.bptmm210.rate = empty()
		var.bptmm210.ratf = empty()
		var.bptmm210.rtyp = empty()
	else
		var.bptmm210.rdat = i.stdt
		var.bptmm210.rate = 1.0
		var.bptmm210.ratf = 1
		var.bptmm210.rtyp = get.internal.exchange.rate.type()
	endif
|	bptmm210.rate(2) = empty()			|* FILLED IN RULE
|	bptmm210.rate(3) = empty()			|* FILLED IN RULE
|	bptmm210.ratf(2) = empty()			|* FILLED IN RULE
|	bptmm210.ratf(3) = empty()			|* FILLED IN RULE

	dump 210
}

function void fill.and.dump.bptmm211.from.tihra100()
{
	domain	tcmcs.long	l.day.number
	domain	tcmcs.long	l.day.seq.nr			|#158562.n
	domain	tctxtn		l.day.txta

	domain	bcstr22		l.start.date
	domain	bcstr22		l.reg.date

	domain	tcmcs.long	l.ret				|#MCR.sn
	domain	tcamnt		var.211.prlc(3)
	domain	tcamnt		dummy.var.prmc(3)
	domain	tcamnt		dummy.var.proc(3)		|#MCR.en


	var.bptmm211.emno = format.emno(tihra100.emno)
|	bptmm211.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
	var.bptmm211.year = tihra100.year
	var.bptmm211.peri = tihra100.week
	var.bptmm211.type = target(bptmm.type.general)
|	var.bptmm211.psqn = tihra100.wsrn			|#158562.o
	var.bptmm211.psqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				GENERAL_TASK)		
								|#158562.en

	var.bptmm211.cprj = empty()
	var.bptmm211.cspa = empty()
	var.bptmm211.cact = empty()
	var.bptmm211.cstl = empty()
	var.bptmm211.swct = target(bpmdm.swct.labor)
	var.bptmm211.ccun = empty()
	ret.long = get.general.task(
				bcsbp.orig.ti,
				str$(tihra100.tano),
				0,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm211.gtsk = bcsbp004.ntsk
		var.bptmm211.dsca = bcsbp004.ntsd
	else
		var.bptmm211.gtsk = empty()
		var.bptmm211.dsca = empty()
	endif
	var.bptmm211.expn = empty()
	var.bptmm211.cpcp = bcsbp000.cpgh |remark 800-137648
	var.bptmm211.cwoc = tihra100.cwoc
	set.mem(var.bptmm211.hrea, 0.0)
								|#PERF.so
|	l.day.number = itihra0001.convert.dayname.to.daynumber(tihra100.stdy)
								|#PERF.eo
								|#PERF.sn
	l.day.number = convert.dayname.to.daynumber(tihra100.stdy)
								|#PERF.en
	var.bptmm211.hrea(l.day.number) = tihra100.hrea
	var.bptmm211.hrea(8) = tihra100.hrea
	ret.long = get.labor.type(
				bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm211.chlt = bcsbp002.nhlt
	else
		var.bptmm211.chlt = empty()
	endif
|	var.bptmm211.rgdt = utc(0, 0)				|#225709.o
	var.bptmm211.rgdt = utc(0, 0, 2)			|#225709.n
|	var.bptmm211.trdt = utc(tihra100.trdt, 0)		|#147118.o
	var.bptmm211.trdt = get.transaction.date(tihra100.trdt, 0) |#147118.n

|	bptmm211.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm211.quan = empty()				|* FILLED IN RULE
|	bptmm211.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm211.pric = empty()				|* FILLED IN RULE
|	bptmm211.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
	var.bptmm211.amnt = empty()
	fill.labor.costs.from.ti(l.day.number)
|	bptmm211.prls = empty()				|* FILLED IN RULE
|	bptmm211.rdat = empty()				|* FILLED IN RULE
	var.bptmm211.rdat = utc(0, 0)
|	bptmm211.rate(1) = empty()			|* FILLED IN RULE
|	bptmm211.rate(2) = empty()			|* FILLED IN RULE
|	bptmm211.rate(3) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(1) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(2) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(3) = empty()			|* FILLED IN RULE
|	bptmm211.rtyp = empty()				|* FILLED IN RULE
|	bptmm211.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm211.logn = logname$
	var.bptmm211.potf = tcyesno.yes
	l.day.txta = fill.text.fields(l.day.number)

	dump 211

|	var.bptmm211.psqn = tihra100.wsrn * 10 + l.day.number		|#158562.o
	l.day.seq.nr =  var.bptmm211.psqn * 10 + l.day.number		|#158562.n

	|* Time part of start date for General Tasks is always 12:00:00.
	l.start.date = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				1200)
	l.reg.date = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)

	fill.and.dump.bptmm250(
		var.bptmm211.emno,
		var.bptmm211.year,
		var.bptmm211.peri,
|		var.bptmm211.psqn,					|#158562.o
		l.day.seq.nr,						|#158562.n
		l.start.date,
		var.bptmm211.gtsk,
		"",	|* Expense
		var.bptmm211.dsca,
		var.bptmm211.cwoc,
		var.bptmm211.cpcp,
		0.0,	|* Amount
		var.bptmm211.potf)

	var.211.prlc(1) = var.bptmm211.prlc(22)		|#MCR.sn
	var.211.prlc(2) = var.bptmm211.prlc(23)
	var.211.prlc(3) = var.bptmm211.prlc(24)

	l.ret = set.mem(dummy.var.prmc, 0.0, 3)
	l.ret = set.mem(dummy.var.proc, 0.0, 3)		|#MCR.en

	fill.and.dump.bptmm200(
		tihra100.emno,						|#GEN-0205.n
		var.bptmm211.emno,
		var.bptmm211.year,
		var.bptmm211.peri,
		GENERAL_TASK,
|		var.bptmm211.psqn,					|#158562.o
		l.day.seq.nr,						|#158562.n
		var.bptmm211.cwoc,
		"",			|* Working Time Schedule Code
		var.bptmm211.hrea(l.day.number),
		0.0,
		var.bptmm211.chlt,
		l.start.date,
		utc(0, 0),		|* End Date
		0.0,
		l.reg.date,
								|#MCR.so
|		var.bptmm211.prlc(22),	|* Total amount (always for 1 day)
|		0.0,			|* Machine Costs
|		0.0,			|* Overhead Costs	|#MCR.eo
		var.211.prlc,					|#MCR.sn
		dummy.var.prmc,
		dummy.var.proc,					|#MCR.en
		var.bptmm211.trdt,
		var.bptmm211.logn,
		l.day.txta)
}

function void fill.and.dump.bptmm211.from.tphrs100()
{
		domain	tcmcs.long	l.day.number
		domain	bcstr22		l.start.date
		domain	bcstr22		l.reg.date

		domain	tcmcs.long	l.dummy.long

	l.dummy.long = 0

	var.bptmm211.emno = format.emno(tphrs100.emno)
	var.bptmm211.year = tphrs100.year
	var.bptmm211.peri = tphrs100.peri

	on case tphrs100.splt
	case tphrs.splt.project:
		var.bptmm211.type = target(bptmm.type.project)
		var.bptmm211.gtsk = empty()
		var.bptmm211.expn = empty()
		var.bptmm211.dsca = get.cost.object.description(
						tphrs100.cotp,
						tphrs100.ccun)
		var.bptmm211.cwoc = ""
		var.bptmm211.cpcp = get.cost.component.from.tpppc(
						tphrs100.ccco,
						tphrs100.cprj,
						tphrs100.cotp,
						tphrs100.year,
						tphrs100.peri,
						tphrs100.empr,
						tphrs100.emno,
						tphrs100.serc)
		var.bptmm211.potf = tcyesno.no
		break
	case tphrs.splt.finance:
	case tphrs.splt.no:
		var.bptmm211.type = target(bptmm.type.general)
		if tphrs100.amnt = 0.0 then
			ret.long = get.general.task(
					bcsbp.orig.tp,
					str$(tphrs100.wgco),
					tphrs100.empr,
					dummy.err.mess)
			if ret.long = 0 then
				var.bptmm211.gtsk = bcsbp004.ntsk
				var.bptmm211.dsca = bcsbp004.ntsd
			else
				var.bptmm211.gtsk = empty()
				var.bptmm211.dsca = empty()
			endif
			var.bptmm211.expn = empty()
		else
			var.bptmm211.expn = tphrs100.wgco
			get.expense.description(tphrs100.empr,
						tphrs100.wgco,
						var.bptmm211.dsca)
			var.bptmm211.gtsk = empty()
		endif
		var.bptmm211.cwoc = get.department.from.employee(tphrs100.emno)
		var.bptmm211.cpcp = bcsbp000.cpgh	|remark 800-137648
		if tphrs100.splt = tphrs.splt.finance then
			var.bptmm211.potf = tcyesno.yes
		else
			var.bptmm211.potf = tcyesno.no
		endif
		break
	default:
		|* For Service records this function will not be entered.
		break
	endcase





	var.bptmm211.cprj = get.tp.project(tphrs100.cprj)
	var.bptmm211.cspa = tphrs100.cspa
	var.bptmm211.cact = tphrs100.cact
	var.bptmm211.cstl = tphrs100.cstl
	var.bptmm211.ccun = tphrs100.ccun
	if tphrs100.cotp = tppdm.cotp.tasks then
		var.bptmm211.swct = target(bpmdm.swct.labor)
	else
		var.bptmm211.swct = target(bpmdm.swct.indirect)
	endif
	if tphrs100.amnt = 0.0 then
		|* Hours record
		get.hours.sequence.number.tp(
					"bptmm211",
					var.bptmm211.emno,
					var.bptmm211.year,
					var.bptmm211.peri,
					l.dummy.long,
					l.dummy.long,
					l.dummy.long,
					var.bptmm211.psqn)
		var.bptmm211.rgdt = utc(0, 0)
		var.bptmm211.chlt = get.labor.type.from.tp(	tphrs100.empr,
								tphrs100.wgco)
		var.bptmm211.amnt = empty()
		var.bptmm211.rdat = utc(0, 0)
		var.bptmm211.rate = empty()
		var.bptmm211.ratf = empty()
		var.bptmm211.rtyp = empty()
	else
		|* Expense record
|		var.bptmm211.psqn = tphrs100.serc + 900
		get.expense.sequence.number.tp(
					"bptmm211",
					var.bptmm211.emno,
					var.bptmm211.year,
					var.bptmm211.peri,
					0,	|* Sequence no, only applicable
						|* if day number is filled.
					var.bptmm211.psqn)
		var.bptmm211.rgdt = utc(tphrs100.rgdt, 0)
		var.bptmm211.chlt = empty()
		var.bptmm211.amnt = tphrs100.amnt
		var.bptmm211.rdat = utc(tphrs100.rgdt, TWELVE_HOURS)
		var.bptmm211.rate = 1.0
		var.bptmm211.ratf = 1
		var.bptmm211.rtyp = get.internal.exchange.rate.type()
	endif
	fill.hours.from.tp()
|	var.bptmm211.trdt = utc(tphrs100.ltdt, tphrs100.lttm)	|#147118.o
	var.bptmm211.trdt = get.transaction.date(		|#147118.sn
					tphrs100.ltdt, 
					tphrs100.lttm) 		|#147118.en
|	bptmm211.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm211.quan = empty()				|* FILLED IN RULE
	|* Quantity field is only applicable for expenses. There is no quantity
	|* field for expenses in BaanIVc4.
|	bptmm211.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm211.pric = empty()				|* FILLED IN RULE
|	bptmm211.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
	set.mem(var.bptmm211.prlc, 0.0)
|	bptmm211.prls = empty()				|* FILLED IN RULE
|	bptmm211.rate(2) = empty()			|* FILLED IN RULE
|	bptmm211.rate(3) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(2) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(3) = empty()			|* FILLED IN RULE
|	bptmm211.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm211.logn = tphrs100.loco
|	bptmm211.txta = empty()				|* FILLED IN RULE
|	bptmm211.txtb = empty()				|* FILLED IN RULE
|	bptmm211.txtc = empty()				|* FILLED IN RULE
|	bptmm211.txtd = empty()				|* FILLED IN RULE
|	bptmm211.txte = empty()				|* FILLED IN RULE
|	bptmm211.txtf = empty()				|* FILLED IN RULE
|	bptmm211.txtg = empty()				|* FILLED IN RULE

	dump 211

	if tphrs100.amnt = 0.0 then
		for l.day.number = 1 to 7
			if tphrs100.nhrs(l.day.number) <> 0.0 then
				l.start.date =
					utc(tphrs100.rgdt + l.day.number - 1,
					TWELVE_HOURS)
				l.reg.date =
					utc(tphrs100.rgdt + l.day.number - 1,
					0)
				dump.bptmm210.or.bptmm250.and.bptmm200.from.tp(
						tphrs100.splt,
						l.day.number,
						tphrs100.emno,	|#GEN-0205.n
						var.bptmm211.emno,
						var.bptmm211.year,
						var.bptmm211.peri,
						var.bptmm211.psqn,
						l.start.date,
						l.reg.date,
						var.bptmm211.cprj,
						var.bptmm211.swct,
						var.bptmm211.cspa,
						var.bptmm211.cact,
						var.bptmm211.cstl,
						var.bptmm211.cpcp,
						var.bptmm211.ccun,
						var.bptmm211.dsca,
						var.bptmm211.hrea(l.day.number),
						var.bptmm211.amnt,
						var.bptmm211.gtsk,
						"",		|* Expense
						var.bptmm211.potf,
						var.bptmm211.cwoc,
						var.bptmm211.chlt,
						var.bptmm211.trdt,
						var.bptmm211.logn,
						var.bptmm211.txta)
			endif
		endfor
	else
		|* Expense Record.
		l.start.date = utc(tphrs100.rgdt, TWELVE_HOURS)
		l.reg.date = utc(tphrs100.rgdt, 0)

		dump.bptmm210.or.bptmm250.and.bptmm200.from.tp(
				tphrs100.splt,
				l.day.number,
				tphrs100.emno,			|#GEN-0205.n
				var.bptmm211.emno,
				var.bptmm211.year,
				var.bptmm211.peri,
				var.bptmm211.psqn,
				l.start.date,
				l.reg.date,
				var.bptmm211.cprj,
				var.bptmm211.swct,
				var.bptmm211.cspa,
				var.bptmm211.cact,
				var.bptmm211.cstl,
				var.bptmm211.cpcp,
				var.bptmm211.ccun,
				var.bptmm211.dsca,
				var.bptmm211.hrea(1),	|0.0
				var.bptmm211.amnt,
				"",		|* General Task
				var.bptmm211.expn,
				var.bptmm211.potf,
				var.bptmm211.cwoc,
				var.bptmm211.chlt,
				var.bptmm211.trdt,
				var.bptmm211.logn,
				var.bptmm211.txta)
	endif
}

function void fill.and.dump.bptmm220()
{
		domain	tcamnt	var.220.prlc(3)			|#MCR.sn
		domain	tcamnt	var.220.prmc(3)
		domain	tcamnt	var.220.proc(3)			|#MCR.en

|	bptmm220.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm220.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
|	bptmm220.year = tihra100.year			|* FILLED IN RULE
|	bptmm220.peri = tihra100.week			|* FILLED IN RULE
|	bptmm220.psqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm220.psqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm220.psqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				PRODUCTION)		
								|#158562.en

	var.bptmm220.stdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,

				tihra100.stdy,
				tihra100.sttm)
	if not isspace(tihra100.cwtt) then
		var.bptmm220.endt = convert.hra.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm220.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm220.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm220.trdt = get.transaction.date(tihra100.trdt, 0) |#147118.n

	var.bptmm220.rgdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm220.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm220.wtsc = tihra100.cwtt			|* FILLED IN RULE
|	bptmm220.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm220.hrea = tihra100.hrea			|* FILLED IN RULE
	ret.long = get.labor.type(
				bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm220.chlt = bcsbp002.nhlt
	else
		var.bptmm220.chlt = empty()
	endif
|	bptmm220.hrma = tihra100.hrma			|* FILLED IN RULE
|	bptmm220.stup = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm220.orno = convert.number(bcckon.act.sfc, tihra100.pdno)	|* FILLED IN RULE
|	bptmm220.opno = tihra100.opno			|* FILLED IN RULE
|	bptmm220.tano = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm220.mcno = tihra100.mcno			|* FILLED IN RULE
	var.bptmm220.perc = correct.percentage.completed(tihra100.pccp)
|	bptmm220.perc = tihra100.pccp			|* FILLED IN RULE
|	bptmm220.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm220.cprj = get.ti.project(tihra100.cprj)	|* FILLED IN RULE
|	bptmm220.cact = tihra100.cact			|* FILLED IN RULE
|	bptmm220.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
|	bptmm220.prlc(1) = tihra100.wgca		|* FILLED IN RULE
|	bptmm220.prlc(2) = empty()			|* FILLED IN RULE
|	bptmm220.prlc(3) = empty()			|* FILLED IN RULE
|	bptmm220.prmc(1) = tihra100.mcca		|* FILLED IN RULE
|	bptmm220.prmc(2) = empty()			|* FILLED IN RULE
|	bptmm220.prmc(3) = empty()			|* FILLED IN RULE
|	bptmm220.proc(1) = tihra100.ohca		|* FILLED IN RULE
|	bptmm220.proc(2) = empty()			|* FILLED IN RULE
|	bptmm220.proc(3) = empty()			|* FILLED IN RULE
|	bptmm220.prls(1) = empty()			|* FILLED IN RULE
|	bptmm220.prls(2) = empty()			|* FILLED IN RULE
|	bptmm220.prls(3) = empty()			|* FILLED IN RULE
|	bptmm220.prms(1) = empty()			|* FILLED IN RULE

|	bptmm220.prms(2) = empty()			|* FILLED IN RULE
|	bptmm220.prms(3) = empty()			|* FILLED IN RULE
|	bptmm220.pros(1) = empty()			|* FILLED IN RULE
|	bptmm220.pros(2) = empty()			|* FILLED IN RULE
|	bptmm220.pros(3) = empty()			|* FILLED IN RULE
|	bptmm220.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm220.logn = logname$			|* FILLED IN RULE
|	bptmm220.txta = tihra100.txta			|* FILLED IN RULE

	for element = 1 to 3					|#MCR.sn
		var.220.prlc(element) = fill.mc.field.double(
							tihra100.wgca,
							element,
							0.0)
		var.220.prmc(element) = fill.mc.field.double(
							tihra100.mcca,
							element,
							0.0)
		var.220.proc(element) = fill.mc.field.double(
							tihra100.ohca,
							element,
							0.0)
	endfor							|#MCR.en



	dump 220


	fill.and.dump.bptmm200(
		tihra100.emno,					|#GEN-0205.n
		format.emno(tihra100.emno),
		tihra100.year,
		tihra100.week,
		PRODUCTION,
|		tihra100.wsrn,					|#158562.o
		var.bptmm220.psqn,				|#158562.n
		tihra100.cwoc,
		tihra100.cwtt,
		tihra100.hrea,
		tihra100.hrma,
		var.bptmm220.chlt,
		var.bptmm220.stdt,
		var.bptmm220.endt,
		var.bptmm220.perc,
		var.bptmm220.rgdt,
|		tihra100.wgca,					|#MCR.so
|		tihra100.mcca,
|		tihra100.ohca,					|#MCR.eo
		var.220.prlc,					|#MCR.sn
		var.220.prmc,
		var.220.proc,					|#MCR.en
|		utc(tihra100.trdt, 0),				|#147118.o
		var.bptmm220.trdt,				|#147118.n
		logname$,
		tihra100.txta)
}

function void fill.and.dump.bptmm230.from.tihra100()
{
	domain	tcmcs.long	l.ret				|#MCR.n
	domain	tcamnt		dummy.var.prmc(3)		|#MCR.n

	var.bptmm230.emno = format.emno(tihra100.emno)
|	bptmm230.cpdt = var.bptmm2xx.cpdt			|* FILLED IN RULE
	var.bptmm230.year = tihra100.year
	var.bptmm230.peri = tihra100.week
|	var.bptmm230.psqn = tihra100.wsrn			|#158562.o
								|#158562.sn
	var.bptmm230.psqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				SERVICE_TI)		
								|#158562.en

	var.bptmm230.stdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	if not isspace(tihra100.cwtt) then
		var.bptmm230.endt = convert.hra.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm230.endt = utc(0, 0)
	endif
|	var.bptmm230.trdt = utc(tihra100.trdt, 0)		|#147118.o
	var.bptmm230.trdt = get.transaction.date(tihra100.trdt, 0) |#147118.n

	var.bptmm230.rgdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm230.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
	var.bptmm230.wtsc = tihra100.cwtt
|	bptmm230.cuni = bcsbp000.dsbt			|* FILLED IN RULE
	var.bptmm230.hrea = tihra100.hrea
	ret.long = get.labor.type(
				bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm230.chlt = bcsbp002.nhlt
	else
		var.bptmm230.chlt = empty()
	endif
	var.bptmm230.orno = convert.number(bcckon.sma.order, tihra100.pdno)
	var.bptmm230.stsk = format.tano(tihra100.tano)
|	bptmm230.acln = empty()				|* FILLED IN RULE
	var.bptmm230.cwoc = tihra100.cwoc
|	bptmm230.lbtv = target(bptmm.serl.labo)		|* FILLED IN RULE
|	bptmm230.lbln = empty()				|* FILLED IN RULE
|	bptmm230.tvln = empty()				|* FILLED IN RULE
	var.bptmm230.cpcp = get.cost.component.from.ts(
					tihra100.pdno,
					tihra100.tano)
|	bptmm230.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE

|	var.bptmm230.prlc = tihra100.wgca			|#MCR.o

	for element = 1 to 3					|#MCR.sn
		var.bptmm230.prlc(element) = fill.mc.field.double(tihra100.wgca,
								  element,
							   	  0.0)
	endfor							|#MCR.en

|	bptmm230.prls = empty()				|* FILLED IN RULE

|	var.bptmm230.proc = tihra100.ohca			|#MCR.o

	for element = 1 to 3					|#MCR.sn
		var.bptmm230.proc(element) = fill.mc.field.double(tihra100.ohca,
								  element,
							   	  0.0)
	endfor							|#MCR.en
	l.ret = set.mem(dummy.var.prmc, 0.0, 3)			|#MCR.n

|	bptmm230.pros = empty()				|* FILLED IN RULE
|	bptmm230.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm230.logn = logname$
	var.bptmm230.txta = tihra100.txta

	dump 230


	fill.and.dump.bptmm200(
		tihra100.emno,					|#GEN-0205.n
		var.bptmm230.emno,
		var.bptmm230.year,
		var.bptmm230.peri,
		SERVICE_TI,
		var.bptmm230.psqn,
		var.bptmm230.cwoc,
		var.bptmm230.wtsc,
		var.bptmm230.hrea,
		0.0,		|* Machine Hours
		var.bptmm230.chlt,
		var.bptmm230.stdt,
		var.bptmm230.endt,
		0.0,		|* Percentage Completed
		var.bptmm230.rgdt,
|		var.bptmm230.prlc,				|#MCR.so
|		0.0,		|* Machine Costs
|		var.bptmm230.proc,				|#MCR.eo
		var.bptmm230.prlc,				|#MCR.sn
		dummy.var.prmc,
		var.bptmm230.proc,				|#MCR.en
		var.bptmm230.trdt,
		var.bptmm230.logn,
		var.bptmm230.txta)
}

function void fill.and.dump.bptmm230.from.tphrs100()
{
	static	domain	tcmcs.long	s.counter
		domain	tcmcs.long	l.day.number

		domain	tcmcs.long	l.ret			|#MCR.sn
		domain	tcamnt		dummy.var.prlc(3)
		domain	tcamnt		dummy.var.prmc(3)
		domain	tcamnt		dummy.var.proc(3)	|#MCR.en

	if tphrs100.amnt <> 0.0 then
		|* Service expenses are not supported in BaanVI.
		return
	endif

	var.bptmm230.emno = format.emno(tphrs100.emno)
|	bptmm230.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
	var.bptmm230.year = tphrs100.year
	var.bptmm230.peri = tphrs100.peri
	var.bptmm230.endt = utc(0, 0)
	|* endt is only applicable if the Working Time Schedule code is filled.
	|* This field is not migrated.
|	var.bptmm230.trdt = utc(tphrs100.ltdt, tphrs100.lttm)	|#147118.o
	var.bptmm230.trdt = get.transaction.date(		|#147118.sn
					tphrs100.ltdt, 
					tphrs100.lttm) 		|#147118.en

|	bptmm230.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
	var.bptmm230.wtsc = empty()
|	bptmm230.cuni = bcsbp000.dsbt			|* FILLED IN RULE

	var.bptmm230.chlt = get.labor.type.from.tp(	tphrs100.empr,
							tphrs100.wgco)
	var.bptmm230.orno = convert.number(bcckon.sma.order, tphrs100.orno)


	var.bptmm230.stsk = format.tano(tphrs100.tano)
|	bptmm230.acln = empty()				|* FILLED IN RULE
	var.bptmm230.cwoc = get.service.department(tphrs100.orno)
|	bptmm230.lbtv = target(bptmm.serl.labo)		|* FILLED IN RULE
|	bptmm230.lbln = empty()				|* FILLED IN RULE
|	bptmm230.tvln = empty()				|* FILLED IN RULE
	var.bptmm230.cpcp = get.cost.component.from.ts(	tphrs100.orno,
							tphrs100.tano)
|	bptmm230.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE

|	var.bptmm230.prlc = empty()				|#MCR.o
	l.ret = set.mem(var.bptmm230.prlc, 0.0, 3)		|#MCR.n

|	bptmm230.prls = empty()				|* FILLED IN RULE

|	var.bptmm230.proc = empty()				|#MCR.o
	l.ret = set.mem(var.bptmm230.proc, 0.0, 3)		|#MCR.n

|	bptmm230.pros = empty()				|* FILLED IN RULE
|	bptmm230.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm230.logn = tphrs100.loco
	var.bptmm230.txta = empty()

	s.counter = 1
	for l.day.number = 1 to 7
		if tphrs100.nhrs(l.day.number) <> 0.0 then
			get.hours.sequence.number.tp(
						"bptmm230",
						var.bptmm230.emno,
						var.bptmm230.year,
						var.bptmm230.peri,
						l.day.number,
						0,
						s.counter,
						var.bptmm230.psqn)
			var.bptmm230.stdt =
					utc(tphrs100.rgdt + l.day.number - 1,
					TWELVE_HOURS)
			var.bptmm230.rgdt =
					utc(tphrs100.rgdt + l.day.number - 1, 0)
			var.bptmm230.hrea = tphrs100.nhrs(l.day.number)

			l.ret = set.mem(dummy.var.prlc, 0.0, 3)	|#MCR.sn
			l.ret = set.mem(dummy.var.prmc, 0.0, 3)
			l.ret = set.mem(dummy.var.proc, 0.0, 3)	|#MCR.en

			dump 230

			fill.and.dump.bptmm200(
					tphrs100.emno,			|#GEN-0205.n
					var.bptmm230.emno,
					var.bptmm230.year,
					var.bptmm230.peri,
					SERVICE_TP,
					var.bptmm230.psqn,
					var.bptmm230.cwoc,
					var.bptmm230.wtsc,
					var.bptmm230.hrea,
					0.0,		|* Machine Hours
					var.bptmm230.chlt,
					var.bptmm230.stdt,
					var.bptmm230.endt,
					0.0,		|* Percentage Completed
					var.bptmm230.rgdt,
|					0.0,	|* Labor Costs		|#MCR.so
|					0.0,	|* Machine Costs
|					0.0,	|* Overhead Costs	|#MCR.eo
					dummy.var.prlc,			|#MCR.sn
					dummy.var.prmc,
					dummy.var.proc,			|#MCR.en
					var.bptmm230.trdt,
					var.bptmm230.logn,
					var.bptmm230.txta)
			s.counter = s.counter + 1
		endif
	endfor
}

function void fill.and.dump.bptmm250(
		domain	bcstr6		i.emno,
		domain	tppdm.year	i.year,
		domain	tcpern		i.peri,
		domain	tcmcs.long	i.psqn,
		domain	bcstr22		i.stdt,
		domain	bcstr8		i.gtsk,
		domain	bcstr8		i.expn,
		domain	tcdsca		i.dsca,
		domain	bcstr6		i.cwoc,
		domain	bcstr8		i.cpcp,
		domain	tcamnt		i.amnt,
		domain	tcyesno		i.potf)
{
	var.bptmm250.emno = fill.field.string(i.emno)
|	var.bptmm250.cpdt = var.bptmm2xx.cpdt	|* FILLED IN RULE
	var.bptmm250.year = fill.field.long(i.year)
	var.bptmm250.peri = fill.field.long(i.peri)
	var.bptmm250.psqn = fill.field.long(i.psqn)

	var.bptmm250.stdt = fill.field.string(i.stdt)
	var.bptmm250.gtsk = fill.field.string(i.gtsk)
	var.bptmm250.expn = fill.field.string(i.expn)
	var.bptmm250.dsca = fill.field.string(i.dsca)
	var.bptmm250.cwoc = fill.field.string(i.cwoc)
	var.bptmm250.cpcp = fill.field.string(i.cpcp)
|	bptmm250.quan = empty()			|* FILLED IN RULE
|	bptmm250.cuni = bcsbp000.dsbt		|* FILLED IN RULE

|	bptmm250.pric = empty()			|* FILLED IN RULE
|	bptmm250.ccur = var.bptmm2xx.ccur	|* FILLED IN RULE
	var.bptmm250.amnt = fill.field.double(i.amnt)
	if i.amnt = 0.0 then
		var.bptmm250.rdat = utc(0, 0)
		var.bptmm250.rate = empty()
		var.bptmm250.ratf = empty()
		var.bptmm250.rtyp = empty()
	else
		var.bptmm250.rdat = fill.field.string(i.stdt)
		var.bptmm250.rate = 1.0
		var.bptmm250.ratf = 1
		var.bptmm250.rtyp = get.internal.exchange.rate.type()
	endif
|	bptmm250.rate(2) = empty()		|* FILLED IN RULE
|	bptmm250.rate(3) = empty()		|* FILLED IN RULE
|	bptmm250.ratf(2) = empty()		|* FILLED IN RULE
|	bptmm250.ratf(3) = empty()		|* FILLED IN RULE
	var.bptmm250.potf = i.potf

	dump 250
}

function void fill.and.dump.bptmm270()
{
		domain	tcamnt	var.270.prlc(3)			|#MCR.sn
		domain	tcamnt	var.270.prmc(3)
		domain	tcamnt	var.270.proc(3)			|#MCR.en

|	bptmm270.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm270.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
|	bptmm270.year = tihra100.year			|* FILLED IN RULE

|	bptmm270.peri = tihra100.week			|* FILLED IN RULE
|	bptmm270.psqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm270.psqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm270.psqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				PCS)		
								|#158562.en

	var.bptmm270.stdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	if not isspace(tihra100.cwtt) then
		var.bptmm270.endt = convert.hra.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm270.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm270.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm270.trdt = get.transaction.date(tihra100.trdt, 0) |#147118.n

	var.bptmm270.rgdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm270.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm270.wtsc = tihra100.cwtt			|* FILLED IN RULE



|	bptmm270.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm270.hrea = tihra100.hrea			|* FILLED IN RULE
	ret.long = get.labor.type(
				bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm270.chlt = bcsbp002.nhlt
	else
		var.bptmm270.chlt = empty()
	endif
|	bptmm270.hrma = tihra100.hrma			|* FILLED IN RULE
|	bptmm270.cprj = get.ti.project(tihra100.cprj)	|* FILLED IN RULE
|	bptmm270.cact = tihra100.cact			|* FILLED IN RULE
|	bptmm270.tano = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm270.mcno = tihra100.mcno			|* FILLED IN RULE
|	bptmm270.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm270.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
|	bptmm270.prlc = tihra100.wgca			|* FILLED IN RULE
|	bptmm270.prmc =	tihra100.mcca			|* FILLED IN RULE
|	bptmm270.proc = tihra100.ohca			|* FILLED IN RULE
|	bptmm270.prls = empty()				|* FILLED IN RULE
|	bptmm270.prms = empty()				|* FILLED IN RULE
|	bptmm270.pros = empty()				|* FILLED IN RULE
|	bptmm270.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm270.logn = logname$			|* FILLED IN RULE
|	bptmm270.txta = tihra100.txta			|* FILLED IN RULE

	for element = 1 to 3					|#MCR.sn
		var.270.prlc(element) = fill.mc.field.double(
							tihra100.wgca,
							element,
							0.0)
		var.270.prmc(element) = fill.mc.field.double(
							tihra100.mcca,
							element,
							0.0)
		var.270.proc(element) = fill.mc.field.double(
							tihra100.ohca,
							element,
							0.0)
	endfor							|#MCR.en


	dump 270


	fill.and.dump.bptmm200(
		tihra100.emno,					|#GEN-0205.n
		format.emno(tihra100.emno),
		tihra100.year,
		tihra100.week,
		PCS,
|		tihra100.wsrn,					|#158562.o
		var.bptmm270.psqn,				|#158562.n
		tihra100.cwoc,
		tihra100.cwtt,
		tihra100.hrea,
		tihra100.hrma,
		var.bptmm270.chlt,
		var.bptmm270.stdt,
		var.bptmm270.endt,
		tihra100.pccp,
		var.bptmm270.rgdt,
|		tihra100.wgca,					|#MCR.so
|		tihra100.mcca,
|		tihra100.ohca,					|#MCR.eo
		var.270.prlc,					|#MCR.sn
		var.270.prmc,
		var.270.proc,					|#MCR.en
|		utc(tihra100.trdt, 0),				|#147118.o
		var.bptmm270.trdt,				|#147118.n
		logname$,
		tihra100.txta)
}

function void get.period.table.code.for.ti(
	ref	domain	bcstp.cpdt	o.period.table.code)
{
	get.period.table.code(	"tihra100",
				bcsbp000.prtc,
				"",
				dummy.err.mess)
	o.period.table.code = bcstc029.tptc
}

function void get.period.table.code.for.tp(
	ref	domain	bcstp.cpdt	o.period.table.code)
{
	|******************************************************
	|* If HRA records are present OR if both HRA and HRS
	|* records are present then the HRA periods are used.
	|* If only HRS records are present then the HRS periods
	|* are used.
	|******************************************************

	get.period.table.code(	"tpppc001",
				bcstc000.pfpp,
				bcstp000.cpth,
				dummy.err.mess)
	o.period.table.code = bcstc029.tptc

	if hra.period.definition = true then
	|* Variable is set at the moment an tiHRA record is selected to migrate.
		if periods.are.equal() then
			get.period.table.code(	"tihra100",
						bcsbp000.prtc,
						"",
						dummy.err.mess)
			o.period.table.code = bcstc029.tptc	|bcsbp000.prtc
		endif
	endif
}

function domain tcbool periods.are.equal()
{
		domain	tppdm.date	l.period.date

	select	tpppc001.*
	from	tpppc001
	order by tpppc001._index1
	selectdo
		l.period.date = week.to.num(
					tpppc001.peri,	|* Week number
					tpppc001.year,	|* Year number
					1)		|* First day of week
		if l.period.date = tpppc001.date then
			continue
			|* Check next record...
		else
			return(false)
		endif
	endselect

	return(true)
}

function domain tcbool expense.record.with.zero.amount(
		domain	tphrs.amnt	i.amnt,
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco)
{
	|**********************************************************************

	|* In BaanIV it is possible to register an expense with no amount. These
	|* records are not supported in BaanVI. Therefore these records must be

	|* skipped during migration.
	|**********************************************************************

		domain	tppdm.wgct	o.wgct

	if i.amnt <> 0.0 then
		|* Early return if the amount is filled.
		return(false)
	endif

	read.wage.component.type.from.wage.component(
					i.empr,
					i.wgco,
					o.wgct)

	if o.wgct = tppdm.wgct.amount then
		return(true)
	endif

	return(false)
}

function domain tcprcg correct.percentage.completed(
		domain	tcprcg	i.perc.completed)
{
	if i.perc.completed > 100.0 then
		return(100.0)
	endif

	if i.perc.completed < 0.0 then
		return(0.0)
	endif

	return(i.perc.completed)
}

function void read.wage.component.type.from.wage.component(
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco,
	ref	domain	tppdm.wgct	o.wgct)
{
	static	domain	tphrs.empr	s.empr
	static	domain	tphrs.wgco	s.wgco	fixed
	static	domain	tppdm.wgct	s.wgct

	if s.empr = i.empr and
	   s.wgco = i.wgco then
		o.wgct = s.wgct
		return
	endif

	select	tphrs010.wgct :o.wgct
	from	tphrs010
	where	tphrs010._index1 = {	:i.empr,
					:i.wgco}
	as set with 1 rows

	selectdo
		s.empr = i.empr
		s.wgco = i.wgco
		s.wgct = o.wgct
	endselect
}

function void read.labor.type.from.wage.component(
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco,
	ref	domain	tpppc.cwgt	o.cwgt)
{
	static	domain	tphrs.empr	s.empr
	static	domain	tphrs.wgco	s.wgco	fixed
	static	domain	tpppc.cwgt	s.cwgt

	if s.empr = i.empr and
	   s.wgco = i.wgco then
		o.cwgt = s.cwgt
		return
	endif


	select	tphrs010.cwgt :o.cwgt
	from	tphrs010
	where	tphrs010._index1 = {	:i.empr,
					:i.wgco}
	as set with 1 rows
	selectdo
		s.empr = i.empr
		s.wgco = i.wgco
		s.cwgt = o.cwgt
	endselect




}

function void dump.bptmm210.or.bptmm250.and.bptmm200.from.tp(
		domain	tphrs.splt	i.splt,
		domain	tcmcs.long	i.dayn,
		domain	tcemno		i.original.employee.code, |#GEN-0205.n
		domain	bcstr6		i.emno,
		domain	tppdm.year	i.year,
		domain	tppdm.peri	i.peri,
		domain	tcmcs.long	i.serc,
		domain	bcstr22		i.stdt,
		domain	bcstr22		i.rgdt,
		domain	tccprj		i.cprj,
		domain	tcmcs.long	i.swct,
		domain	bcstr8		i.cspa,
		domain	bcstr8		i.cact,
		domain	bcstr4		i.cstl,
		domain	bcstr8		i.cpcp,
		domain	bcstr8		i.ccun,
		domain	tcdsca		i.dsca,
		domain	tisfc.prtm	i.hrea,
		domain	tcamnt		i.amnt,
		domain	bcstr8		i.gtsk,
		domain	bcstr8		i.expn,
		domain	tcyesno		i.potf,
		domain	bcstr6		i.cwoc,
		domain	bcstr3		i.chlt,
		domain	bcstr22		i.trdt,
		domain	tclogn		i.logn,
		domain	tctxtn		i.txta)
{
		domain	tcbool		l.hours.record
		domain	tcmcs.long	l.dummy.long


		domain	tcmcs.long	l.ret			|#MCR.sn
		domain	tcamnt		dummy.var.prlc(3)
		domain	tcamnt		dummy.var.prmc(3)
		domain	tcamnt		dummy.var.proc(3)	|#MCR.en

	l.hours.record = false
	l.dummy.long = 0

	if i.amnt = 0.0 then
		l.hours.record = true
	endif

	if i.splt = tphrs.splt.project then
		if l.hours.record = true then
			get.hours.sequence.number.tp(
						"bptmm210",
						i.emno,
						i.year,
						i.peri,
						i.dayn,
						i.serc,
						l.dummy.long,
						var.bptmm210.psqn)
		else
			get.expense.sequence.number.tp(
						"bptmm210",
						i.emno,
						i.year,
						i.peri,
						i.serc,
						var.bptmm210.psqn)
		endif
		fill.and.dump.bptmm210(
			var.bptmm210.psqn,
			i.stdt,
			i.swct,
			i.cpcp)
	else
		if l.hours.record = true then
			get.hours.sequence.number.tp(
						"bptmm250",
						i.emno,
						i.year,
						i.peri,
						i.dayn,
						i.serc,
						l.dummy.long,
						var.bptmm250.psqn)
		else
			get.expense.sequence.number.tp(
						"bptmm250",
						i.emno,
						i.year,
						i.peri,
						i.serc,
						var.bptmm250.psqn)
		endif
		fill.and.dump.bptmm250(
			i.emno,
			i.year,
			i.peri,
			var.bptmm250.psqn,
			i.stdt,
			i.gtsk,
			i.expn,
			i.dsca,
			i.cwoc,
			i.cpcp,
			i.amnt,
			i.potf)
	endif

	if i.splt = tphrs.splt.project then

		var.bptmm200.type = PROJECT
		var.bptmm200.psqn = var.bptmm210.psqn
	else
		var.bptmm200.type = GENERAL_TASK
		var.bptmm200.psqn = var.bptmm250.psqn
	endif

	l.ret = set.mem(dummy.var.prlc, 0.0, 3)		|#MCR.sn
	l.ret = set.mem(dummy.var.prmc, 0.0, 3)
	l.ret = set.mem(dummy.var.proc, 0.0, 3)		|#MCR.en


	fill.and.dump.bptmm200(
		i.original.employee.code,			|#GEN-0205.n
		i.emno,
		i.year,
		i.peri,
		var.bptmm200.type,
		var.bptmm200.psqn,
		i.cwoc,
		"",		|* Working Time Schedule Code
		i.hrea,
		0.0,		|* Machine Hours
		i.chlt,
		i.stdt,

		utc(0, 0),	|* End Date
		0.0,		|* Percentage Completed
		i.rgdt,
|		0.0,		|* Labor Costs			|#MCR.so
|		0.0,		|* Machine Costs
|		0.0,		|* Overhead Costs		|#MCR.eo
		dummy.var.prlc,					|#MCR.sn
		dummy.var.prmc,
		dummy.var.proc,					|#MCR.en
		i.trdt,
		i.logn,
		i.txta)
}

function void get.hours.sequence.number.tp(
		domain	bcstr8		i.target.table,
		domain	bcstr6		i.emno	fixed,
		domain	tppdm.year	i.year,
		domain	tppdm.peri	i.peri,
		domain	tcmcs.long	i.dayn,
		domain	tcmcs.long	i.seqn,
		domain	tcmcs.long	i.counter,	|* Only applicable for
							|* Service
	ref	domain	tcmcs.long	o.seqn)
{
	|* Note: i.seqn is only applicable if i.dayn is filled. The value of
	|* i.seqn must be the result of a previous call of this function (and
	|* may not be retrieved from tphrs100).

	static	domain	bcstr6		s.emno	fixed
	static	domain	tppdm.year	s.year
	static	domain	tppdm.peri	s.peri
	static	domain	tcmcs.long	s.seqn

	on case i.target.table
	case "bptmm211":
		if s.emno = i.emno and
		   s.year = i.year and
		   s.peri = i.peri then
			s.seqn = s.seqn + 1
		else
			s.seqn = 1
			s.emno = i.emno
			s.year = i.year
			s.peri = i.peri
		endif
		o.seqn = s.seqn
		break
	case "bptmm210":
	case "bptmm250":
		o.seqn = i.seqn * 10 + i.dayn
		break
	case "bptmm230":
		if i.counter = 1 then
			if s.emno = i.emno and
			   s.year = i.year and
			   s.peri = i.peri then
				s.seqn = s.seqn + 1
			else
				s.seqn = 1
				s.emno = i.emno
				s.year = i.year
				s.peri = i.peri
			endif
		endif
		o.seqn = s.seqn * 10 + i.dayn
		break
	endcase
}

function void get.expense.sequence.number.tp(
		domain	bcstr8		i.target.table,
		domain	bcstr6		i.emno	fixed,
		domain	tppdm.year	i.year,
		domain	tppdm.peri	i.peri,
		domain	tcmcs.long	i.seqn,
	ref	domain	tcmcs.long	o.seqn)
{
	|* Note: i.seqn is only applicable if i.dayn is filled. The value of
	|* i.seqn must be the result of a previous call of this function (and
	|* may not be retrieved from tphrs100).

	static	domain	bcstr6		s.emno	fixed
	static	domain	tppdm.year	s.year
	static	domain	tppdm.peri	s.peri
	static	domain	tcmcs.long	s.seqn


	on case i.target.table
	case "bptmm211":
		if s.emno = i.emno and
		   s.year = i.year and
		   s.peri = i.peri then
			s.seqn = s.seqn + 1
		else
			s.seqn = 900
			s.emno = i.emno
			s.year = i.year
			s.peri = i.peri
		endif
		o.seqn = s.seqn
		break
	case "bptmm210":
	case "bptmm250":
		|* The sequence number for expense records start from 900;
		|* In table bptmm210 / bptmm250 they must start with 9000.
		o.seqn = 9000 + (i.seqn - 900)
		break
	case "bptmm230":
		|* Service expenses are not supported in BaanVI.
		break
	endcase
}


function domain	bcrtyp get.internal.exchange.rate.type()
{

	static	domain	bcrtyp		s.internal.rate.type

	if isspace(s.internal.rate.type) then
		get.exchange.rate.type(	"EU",
					dummy.err.mess)
		s.internal.rate.type = bcstc011.brtp
	endif

	return(s.internal.rate.type)
}

function string convert.hra.day.to.utc.date(
		domain	tcyrno		i.year,
		domain	tcweek		i.week,
		domain	tcdynm		i.dayname,
		domain	tctmhs		i.time)
{
|	Dllusage
|	Expl:	This function calculates the utc-date based on an input day and
|		an input time.
|	Input:
|		i.year - Year
|		i.week - Week Number
|		i.dayname - Enumerate (Monday, Tuesday, etc.)
|		i.time - Time (interpretation depends on tihra100.cltm)
|	Enddllusage

|		domain	tcmcs.long	l.ret			|#PERF.o
|		string			l.dummy			|#PERF.o
		domain	tcmcs.long	l.day.number		|#PERF.n
		domain	tcmcs.long	l.utc.date.part
		domain	tcmcs.long	l.utc.time.part
		domain	tcmcs.long	l.number.of.hours

|	if i.dayname = empty then				|#PERF.so
|		l.ret = tt.company(	get.compnr(),
|					l.dummy,
|					l.dummy,
|					l.first.day.of.week)
|		i.dayname = ltoe(l.first.day.of.week)
|	endif							|#PERF.eo

	l.day.number = convert.dayname.to.daynumber(i.dayname)	|#PERF.sn
	l.utc.date.part = week.to.num(	i.week,
					i.year,
					l.day.number)		|#PERF.en
								|#PERF.so
|	l.utc.date.part = itihra0001.convert.dayname.to.date(	i.year,
|								i.week,
|								i.dayname)
								|#PERF.eo
	if i.time <> 0 then
		read.hra.parameters()

		if tihra000.cltm = tccltm.minutes then
		|* 850 is to be interpreted as 08:50.
			l.number.of.hours = i.time/100
			l.utc.time.part = (l.number.of.hours * 60 * 60) +
				((i.time - l.number.of.hours * 100) * 60)
		else
		|* 850 is to be interpreted as 08:30.
			l.number.of.hours = i.time/100
			l.utc.time.part = (l.number.of.hours * 60 * 60) +
				((i.time - l.number.of.hours * 100) *
							tihra000.perl * 60)
		endif
	else
		l.utc.time.part = 0
		return(utc(l.utc.date.part, l.utc.time.part, 2)) |#225709.n
	endif

	return(utc(l.utc.date.part, l.utc.time.part))
}

function domain tcmcs.long convert.dayname.to.daynumber(	|#PERF.sn
		domain	tcdynm		i.day.name)
{
		domain	tcmcs.long	l.ret
		string			l.dummy
	static	domain	tcncmp		s.company
	static	domain	tcmcs.long	s.first.day.of.week
		domain	tcmcs.long	l.day.nr

	if s.company <> ext.lng.comp then
		l.ret = tt.company(	ext.lng.comp,
					l.dummy,
					l.dummy,
					s.first.day.of.week)
		s.company = ext.lng.comp
	endif

	if i.day.name = empty then
		i.day.name = ltoe(s.first.day.of.week)
	endif

	on case i.day.name
		case tcdynm.sunday:
			l.day.nr = 1
			break
		case tcdynm.monday:
			l.day.nr = 2
			break
		case tcdynm.tuesday:
			l.day.nr = 3
			break
		case tcdynm.wednesday:
			l.day.nr = 4
			break
		case tcdynm.thursday:
			l.day.nr = 5
			break
		case tcdynm.friday:
			l.day.nr = 6
			break
		case tcdynm.saturday:
			l.day.nr = 7
			break
		default:
			l.day.nr = 0
			break
	endcase

	if l.day.nr <> 0 then
		|* Make correction for first day of the week
		l.day.nr = l.day.nr + 1 + (7 - s.first.day.of.week)
		if l.day.nr > 7 then
			l.day.nr = l.day.nr - 7
		endif
	endif

	return(l.day.nr)
}								|#PERF.en



function domain	tctxtn fill.text.fields(
		domain	tcmcs.long	i.day.number)
{
	var.bptmm211.txta = 0
	var.bptmm211.txtb = 0
	var.bptmm211.txtc = 0
	var.bptmm211.txtd = 0
	var.bptmm211.txte = 0
	var.bptmm211.txtf = 0
	var.bptmm211.txtg = 0

	on case i.day.number

	case 0:
	case 1:
		var.bptmm211.txta = tihra100.txta
		return(var.bptmm211.txta)
	case 2:
		var.bptmm211.txtb = tihra100.txta
		return(var.bptmm211.txtb)
	case 3:
		var.bptmm211.txtc = tihra100.txta
		return(var.bptmm211.txtc)
	case 4:
		var.bptmm211.txtd = tihra100.txta
		return(var.bptmm211.txtd)
	case 5:
		var.bptmm211.txte = tihra100.txta
		return(var.bptmm211.txte)
	case 6:
		var.bptmm211.txtf = tihra100.txta

		return(var.bptmm211.txtf)
	case 7:
		var.bptmm211.txtg = tihra100.txta
		return(var.bptmm211.txtg)
	default:
		return(0)
	endcase

	return(0)
}

function domain bccost get.cost.component.from.tpppc(
		domain	tppdm.ccco	i.ccco,
		domain	tppdm.cprj	i.cprj,
		domain	tppdm.cotp	i.cost.type,
		domain	tppdm.year	i.year,
		domain	tppdm.peri	i.peri,
		domain	tphrs.empr	i.empr,
		domain	tcemno		i.emno,
		domain	tppdm.serc	i.serc)
{
		domain	bccost		l.cost.component

	if isspace(i.ccco) then
		|* If no cost components are defined in tphrs100, then cost
		|* components are not allowed due to the cost control settings
		|* in session tppdm6102s000.
		return("")

	endif

	l.cost.component = ""

	if i.cost.type = tppdm.cotp.tasks then
		select	tpppc231.ccco :l.cost.component
		from	tpppc231
		where	tpppc231._index9 = {	:i.year,
						:i.peri,
						:i.empr,
						:i.emno,
						:i.serc}
		as set with 1 rows
		selectdo
		selectempty
			|* Record is present in history tpppc235. The cost
			|* component cannot be retrieved from tpppc235, because
			|* there is no hours link with tpppc235. The 'best' cost
			|* component is the one of tphrs100.
			l.cost.component = i.ccco
		endselect
	else
		select	tpppc291.ccco :l.cost.component
		from	tpppc291
		where	tpppc291._index7 = {	:i.year,
						:i.peri,
						:i.empr,
						:i.emno,
						:i.serc}
		as set with 1 rows
		selectdo
		selectempty
			|* Record is present in history tpppc295. The cost
			|* component cannot be retrieved from tpppc295, because
			|* there is no hours link with tpppc295. The 'best' cost
			|* component is the one of tphrs100.
			l.cost.component = i.ccco
		endselect
	endif

	if not isspace(l.cost.component) then
		get.cost.components(
				bcorig.tppdm,
				"",
				l.cost.component,
				dummy.err.mess)
		l.cost.component = bcstc006.cost
	endif

	return(l.cost.component)
}

function domain tcdsca get.cost.object.description(
		domain	tppdm.cotp	i.cotp,
		domain	tppdm.task	i.ccun)
{
	static	domain	tppdm.cotp	s.cotp
	static	domain	tppdm.task	s.ccun	fixed
	static	domain	tcdsca		s.ccun.dsca

	if i.cotp = s.cotp and
	   i.ccun = s.ccun then
		return(s.ccun.dsca)
	endif

	on case i.cotp
	case tppdm.cotp.tasks:
		|* Function present in include itppdm0007
		read.task(	i.ccun,
				tphrs100.cprj,
				s.ccun.dsca,
				2,
				0,
				0)
		break
	case tppdm.cotp.indirect:
		|* Function present in include itppdm0066
		copy.string.with.different.domains(
				i.ccun,		|* Input
				i.ccun,		|* Output
				"tppdm.cico")
		|* Function present in include itppdm0011
		read.indirect.cost.code(

				i.ccun,
				tphrs100.cprj,

				s.ccun.dsca,
				2,
				0,
				0)
		break
	endcase

	s.cotp = i.cotp
	s.ccun = i.ccun

	return(s.ccun.dsca)
}

function void fill.labor.costs.from.ti(
		domain	tcmcs.long	i.day.number)
{
	|**********************************************************************
	|* Field bptmm211.prlc is an array of depth 24.
	|* position 1, 2, 3 belongs to costs of day 1
	|* position 4, 5, 6 belongs to costs of day 2
	|* position 7, 8, 9 belongs to costs of day 3
	|* ...
	|* During migration only positions 1, 4, 7, ..., 22 are filled, because
	|* only the first position of the currency-array is being filled.
	|* Day 1 relates to position 1

	|* Day 2 relates to position 4
	|* Day 3 relates to position 7
	|* ...
	|* The total is stored in position 22.
	|* A bit of mathematics helps us in determining the exact relationship:
	|* <position.in.array> = <day.number - 1> * 3 + 1

	|**********************************************************************


		domain	tcmcs.long	l.position

	set.mem(var.bptmm211.prlc, 0.0)

	|if tihra100.wgca = 0.0 then				|#MCR.o
	if fill.mc.field.double(tihra100.wgca,1,0.0) = 0.0 then |#MCR.n
		return
	endif

	l.position = ((i.day.number - 1) * 3) + 1

|	var.bptmm211.prlc(l.position) = tihra100.wgca		|#MCR.o
								|#MCR.sn
	var.bptmm211.prlc(l.position) = fill.mc.field.double(
							tihra100.wgca,
							1,
							0.0)
	|* The below changes are used to fill the positions (2,3,5,6,8,9 ---etc)
	|* with MCR fields
	l.position = ((i.day.number - 1) * 3) + 2
	var.bptmm211.prlc(l.position) = fill.mc.field.double(
							tihra100.wgca,
							2,
							0.0)
	l.position = ((i.day.number - 1) * 3) + 3
	var.bptmm211.prlc(l.position) = fill.mc.field.double(
							tihra100.wgca,
							3,
							0.0)	|#MCR.en
|	var.bptmm211.prlc(22) = tihra100.wgca			|#MCR.o
								|#MCR.sn
	var.bptmm211.prlc(22) = fill.mc.field.double(tihra100.wgca, 1, 0.0)
	var.bptmm211.prlc(23) = fill.mc.field.double(tihra100.wgca, 2, 0.0)
	var.bptmm211.prlc(24) = fill.mc.field.double(tihra100.wgca, 3, 0.0)
								|#MCR.en
}

function void fill.hours.from.tp()
{
		domain	tisfc.prtm	l.sum.hrea
		domain	tcmcs.long	l.day.number

	set.mem(var.bptmm211.hrea, 0.0)
	l.sum.hrea = 0.0

	for l.day.number = 1 to 7
		var.bptmm211.hrea(l.day.number) = tphrs100.nhrs(l.day.number)

		l.sum.hrea = l.sum.hrea + tphrs100.nhrs(l.day.number)
	endfor

	var.bptmm211.hrea(8) = l.sum.hrea
}

function domain tcckow get.labor.type.from.tp(
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco)
{
		domain	tcckow		l.labor.type

	read.labor.type.from.wage.component(	i.empr,
						i.wgco,
						l.labor.type)	|* empty FOR EXPENSES

	if not isspace(l.labor.type) then

		ret.long = get.labor.type(
				bcsbp.orig.tp,
				l.labor.type,	|* Input
				dummy.err.mess)
		if ret.long = 0 then
			return(bcsbp002.nhlt)
		endif

	else
		ret.long = get.default.labor.type(
				l.labor.type,	|* Output
				dummy.err.mess)
		if ret.long = 0 then
			return(l.labor.type)
		endif

	endif

	return("")
}


function void get.expense.description(
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco,
	ref	domain	tcdsca		o.dsca)
{
	static	domain	tphrs.empr	s.empr
	static	domain	tphrs.wgco	s.wgco	fixed
	static	domain	tcdsca		s.dsca

	if s.empr = i.empr and
	   s.wgco = i.wgco then
		o.dsca = s.dsca
		return
	endif

	select	tphrs010.desc :o.dsca
	from	tphrs010
	where	tphrs010._index1 = {	:i.empr,
					:i.wgco}
	as set with 1 rows
	selectdo

		s.empr = i.empr
		s.wgco = i.wgco
		s.dsca = o.dsca
	endselect
}

function domain bcstd.fent get.department.from.employee(
		domain	tcemno		i.emno)
{
	static	domain	tcemno		s.emno
	static	domain	bcstd.fent	s.department

	if s.emno <> i.emno then

		select	tccom001.cwoc :s.department
		from	tccom001
		where	tccom001._index1 = {:i.emno}
		as set with 1 rows
		selectdo
			s.emno = i.emno
			if isspace(s.department) then		|#800-154843.sn
				s.department = bcsbp000.cwoc
			endif					|#800-154843.en
		selectempty
			s.department = bcsbp000.cwoc
		endselect
	endif

	return(s.department)
}

function domain bcstd.fent get.service.department(
		domain	tcpdno		i.orno)

{
	static	domain	tcpdno		s.orno
		domain	tcncmp		l.log.company
		domain	tcncmp		l.fin.company
		domain	bcstd.fent	l.enterprise.unit

	static	domain	bcstd.fent	s.service.dept

	if s.orno <> i.orno then
		select	tssma301.ncmp :l.fin.company
		from	tssma301
		where	tssma301._index1 = {:i.orno}
		as set with 1 rows
		selectdo
			l.log.company = get.compnr()
			l.enterprise.unit =	str$(l.log.company) &
						str$(l.fin.company)


			select	bcstc018.srfd :s.service.dept
			from	bcstc018
			where	bcstc018._index1 = {:l.enterprise.unit}
			as set with 1 rows
			selectdo
				s.orno = i.orno
				return(s.service.dept)
			endselect

		endselect
	endif

	return(s.service.dept)
}

function void read.hra.parameters()
{
	static	domain	tcbool		s.hra.parameters.read

	if s.hra.parameters.read then
		return
	endif

	select	tihra000.cltm, tihra000.perl
	from	tihra000
	where	tihra000._index1 = {1}
	as set with 1 rows
	selectdo
		s.hra.parameters.read = true
	endselect
}

function domain tccpcp get.cost.component.from.ts(
		domain	tcpdno		i.orno,
		domain	tctano		i.task)
{
		domain	tccuno		l.customer
		domain	tccpcp		l.cost.component
		domain	tcitem		l.item
		domain	tcpric		o.dummy.pric
		domain	tssma.mess	o.dummy.mess

	l.customer = read.customer.from.service.order(i.orno)

	|if itssma0011.search.rate.from.task(			|#MCR.so
	|				l.customer,
	|				i.task,
	|				l.item,
	|				o.dummy.pric,
	|				o.dummy.mess) then	|#MCR.eo
	if search.rate.from.task(				|#MCR.sn
			l.customer,
			i.task,
			l.item,
			o.dummy.pric,
			o.dummy.mess) then			|#MCR.en
		l.cost.component = read.cost.component.from.item(l.item)
	else
		l.cost.component = bcsbp000.cpcp
	endif

	get.cost.components(
			bcorig.ticpr,
			l.cost.component,
			"",
			dummy.err.mess)

	return(bcstc006.cost)
}

function domain tccuno read.customer.from.service.order(
		domain	tcpdno		i.orno)
{

	static	domain	tcpdno		s.orno
	static	domain	tccuno		s.cuno

	if s.orno = i.orno then
		return(s.cuno)
	endif

	select	tssma301.cuno :s.cuno
	from	tssma301
	where	tssma301._index1 = {:i.orno}
	as set with 1 rows
	selectdo
		s.orno = i.orno
	endselect

	return(s.cuno)
}

function domain tccpcp read.cost.component.from.item(
		domain	tcitem		i.item)
{
	static	domain	tcitem		s.item
	static	domain	tccpcp		s.cpcp

	if s.item = i.item then
		return(s.cpcp)
	endif

	select	tiitm001.cpcp :s.cpcp
	from	tiitm001
	where	tiitm001._index1 = {:i.item}
	as set with 1 rows
	selectdo
		s.item = i.item
	endselect

	return(s.cpcp)
}

function long fill.field.long(
		long			i.value)
{
	if i.value = 0 then
		ret.long = empty()
	else
		ret.long = i.value
	endif

	return(ret.long)
}

function double fill.field.double(
		double			i.value)
{
	if i.value = 0.0 then
		ret.double = empty()
	else
		ret.double = i.value
	endif


	return(ret.double)
}

function string fill.field.string(
		domain	bcstr50		i.value)
{
	if isspace(i.value) then
		ret.string = empty()
	else

		ret.string = i.value
	endif

	return(ret.string)
}

function domain	bcstr22 get.transaction.date(			|#147118.sn
		domain	tcdate		i.transaction.date,
		domain	tctime		i.transaction.time)
{
	if i.transaction.date = 0 then
		|* Transaction Date not filled; fill with
		|* current time stamp.
		return( utc(date.num(), time.num()) )
	endif
		
	return( utc(i.transaction.date, i.transaction.time) )
}								|#147118.en


								|#158562.sn
function domain tcmcs.long get.sequence.number(
		domain	tcyrno		i.year,
		domain	tcweek		i.week,
		domain	tcemno		i.emno,
		domain	tcmcs.long	i.type)
{
|	Dllusage
|	Expl:	This function determines the sequence number. 
|		The sequence number is not taken anymore from BaanIV because
|		there were too large numbers present which did not fit in the domain
|		present in LN (maximum of domain bpmdm.serd is 9999; maximum
|		of domain tihra.sern is 99999).
|		Now tihra100 is read with order by on year, week, emno, koht. Then
|		we can determine the sequence number in the right order to be used
|		in LN.
|	Input:
|		i.year - Year
|		i.week - Week Number
|		i.emno - Employee
|		i.type - Indicates type of hours
|	Enddllusage

	static	domain	tcyrno		hold.year
	static	domain	tcweek		hold.week
	static	domain	tcemno		hold.emno
	static	domain	tcmcs.long	hold.type
	static	domain	tcmcs.long	hold.sequence

	if hold.year = i.year and
	   hold.week = i.week and
	   hold.emno = i.emno and
	   hold.type = i.type then
		hold.sequence = hold.sequence + 1
		return(hold.sequence)
	endif

	hold.year = i.year
	hold.week = i.week
	hold.emno = i.emno
	hold.type = i.type
	hold.sequence = 1
	return(hold.sequence)
}								|#158562.en

								|#GEN-0205.sn
function domain tccwoc get.employee.department(
		domain	tcemno		i.employee)
{
	static	domain	tcemno		s.employee
	static	domain	tccwoc		s.employee.department	fixed

	if s.employee = i.employee then
		return(s.employee.department)
	endif

	s.employee = i.employee
	
	select	tccom001.cwoc:s.employee.department
	from	tccom001
	where	tccom001._index1 = {:i.employee}
	as set with 1 rows
	selectdo
		break
	selectempty
		s.employee.department = ""
	endselect
	
	return(s.employee.department)
}
								|#GEN-0205.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acp
:TFLNO:100
:FUNC:START
function void fill.dimensions(
		domain	tcsuno		i.business.partner,
	ref	domain	tfgld.dimx	o.dimension.1,
	ref	domain	tfgld.dimx	o.dimension.2,
	ref	domain	tfgld.dimx	o.dimension.3,
	ref	domain	tfgld.dimx	o.dimension.4,
	ref	domain	tfgld.dimx	o.dimension.5)
{
	static	domain	tcsuno		hold.business.partner
	static	domain	tfacp.fisu	hold.financial.suppl.group
	
	o.dimension.1 = "" 
	o.dimension.2 = ""
	o.dimension.3 = ""
	o.dimension.4 = ""
	o.dimension.5 = ""

	|* The dimensions could be read within 1 query whereby table tfacp005
	|* and tccom020 are joined. This has a disadvantage. Different business
	|* partners can belong to the same financial supplier group. By
	|* splitting the query we can seperatly check if the business partner
	|* changed and if the financial supplier group changed.
	if i.business.partner <> hold.business.partner then
		hold.business.partner = i.business.partner 
		get.financial.supplier.group(
			i.business.partner,
			hold.financial.suppl.group)
	endif
	
	get.dimensions.control.account(
	 	hold.financial.suppl.group,
		o.dimension.1, 
		o.dimension.2,
		o.dimension.3,
		o.dimension.4,
		o.dimension.5)	
}

function void get.financial.supplier.group(
		domain	tcsuno		i.business.partner,
	ref	domain	tfacp.fisu	o.financial.supplier.group)
{
	o.financial.supplier.group = ""
	
	select	tccom020.cfsg:o.financial.supplier.group
	from	tccom020
	where	tccom020._index1 = {:i.business.partner}
	as set with 1 rows
	selectdo	
	endselect
}



function void get.dimensions.control.account(
		domain	tfacp.fisu	i.financial.supplier.group,
	ref	domain	tfgld.dimx	o.dimension.1,
	ref	domain	tfgld.dimx	o.dimension.2,
	ref	domain	tfgld.dimx	o.dimension.3,
	ref	domain	tfgld.dimx	o.dimension.4,
	ref	domain	tfgld.dimx	o.dimension.5)
{
	static	domain	tfgld.dimx	hold.dimension.1
	static	domain	tfgld.dimx	hold.dimension.2
	static	domain	tfgld.dimx	hold.dimension.3
	static	domain	tfgld.dimx	hold.dimension.4
	static	domain	tfgld.dimx	hold.dimension.5
	static	domain	tcsuno		hold.financial.suppl.group

	if i.financial.supplier.group = hold.financial.suppl.group then
		o.dimension.1 = hold.dimension.1 
		o.dimension.2 = hold.dimension.2
		o.dimension.3 = hold.dimension.3
		o.dimension.4 = hold.dimension.4
		o.dimension.5 = hold.dimension.5
		return
	endif

	hold.financial.suppl.group = i.financial.supplier.group

	select	tfacp005.dim1:o.dimension.1, tfacp005.dim2:o.dimension.2,
		tfacp005.dim3:o.dimension.3, tfacp005.dim4:o.dimension.4,
		tfacp005.dim5:o.dimension.5
	from	tfacp005
	where	tfacp005._index1 = {:i.financial.supplier.group,
				    tfacp.tran.control}
	as set with 1 rows
	selectdo
	endselect

	hold.dimension.1 =  o.dimension.1
	hold.dimension.2 =  o.dimension.2
	hold.dimension.3 =  o.dimension.3
	hold.dimension.4 =  o.dimension.4
	hold.dimension.5 =  o.dimension.5
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acp
:TFLNO:200
:FUNC:START
function void convert.payment.procedure.step(
		domain	tfcmg.step   i.source.step,
	ref	long   		     o.target.step)
{
	on case i.source.step
	case tfcmg.step.doc.selected:
		o.target.step = target(tfcmg.step.doc.selected)
		break
	case tfcmg.step.doc.to.cust:
		o.target.step = target(tfcmg.step.doc.to.cust)
		break
	case tfcmg.step.doc.acc.by.cust:
		o.target.step = target(tfcmg.step.doc.acc.by.cust)
		break
	case tfcmg.step.doc.to.bank:
		o.target.step = target(tfcmg.step.doc.to.bank)
		break
	case tfcmg.step.doc.received:
		o.target.step = target(tfcmg.step.doc.received)
		break
	case tfcmg.step.doc.acc.sent:
		o.target.step = target(tfcmg.step.doc.acc.sent)
		break
	case tfcmg.step.doc.paid:
		o.target.step = target(tfcmg.step.doc.paid)
		break
	case tfcmg.step.doc.rejected:
		o.target.step = target(tfcmg.step.doc.rejected)
		break
	endcase
}

function domain tcbool factor.company.linked.to.business.partner(  
		domain 	tccuno		i.supplier,  
| 	 ref	domain 	tccuno	 	o.factor.company,			|#1431629.o
 	 ref	domain 	tccfcc	 	o.factor.company,			|#1431629.n
	 ref	domain	tccban		o.factor.bank)
{
	o.factor.bank = ""
	o.factor.company = ""

	select	tccom020.cfcc:o.factor.company, tcmcs025.cban:o.factor.bank
	from	tccom020, tcmcs025
	where	tccom020._index1 = {:i.supplier}
	and	tccom020.cfcc <> ""
	and	tccom020.cfcc refers to tcmcs025
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function void get.control.account(				|#FP3.sn
		domain	tcncmp		i.company,
		domain	tfgld.year	i.year,
		domain	tfgld.btno	i.batch,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
		domain	tfgld.lino	i.line,
		domain	tfacp.tpay	i.document.type,
	ref	domain	tfgld.leac	o.control.account,
	ref	domain	tfgld.dimx	o.dimension1,		|#1056135.sn
	ref	domain	tfgld.dimx	o.dimension2,		
	ref	domain	tfgld.dimx	o.dimension3,		
	ref	domain	tfgld.dimx	o.dimension4,		
	ref	domain	tfgld.dimx	o.dimension5)		|#1056135.en
{		
|******************************************************************************
|* This functions tries to get the control account from the (non) finalized
|* transactions. If the invoice is not finalized we read tfgld102. If it is
|* finalized we read tfgl106. It is possible that an invoice is corrected. In
|* that case we have to read the the correction document and, if the control
|* account is changed, overwrite it with the new value. Only finalized invoices
|* can be corrected.
|******************************************************************************

		domain	tfgld.ttyp	trans.type.corr
		domain	tfgld.docn	document.corr
		domain	tfgld.btno	batch.corr
		domain	tfgld.year	year.corr

	o.control.account = ""
	o.dimension1 = ""					|#1056135.sn
	o.dimension2 = ""				
	o.dimension3 = ""				
	o.dimension4 = ""					
	o.dimension5 = ""					|#1056135.en
		
	if invoice.is.finalized(
	      i.transaction.type,
	      i.document) then
		get.control.account.from.finalized.transactions(
	      	   i.transaction.type,
	      	   i.document,
	      	   i.line,
	      	   o.control.account,
	      	   o.dimension1,				|#1056135.sn
	      	   o.dimension2,			
	      	   o.dimension3,				
	      	   o.dimension4,				
	      	   o.dimension5)				|#1056135.sn
		|* Process Purchase Corrections
		if (i.document.type = tfacp.tpay.invoice or
		   i.document.type = tfacp.tpay.credit) and
		   get.correction.document(
		      i.transaction.type,
		      i.document,
		      trans.type.corr,
		      document.corr,
		      batch.corr,
		      year.corr) then
			get.control.account.from.correction(
				i.company,
				trans.type.corr,
				document.corr,
				batch.corr,
				year.corr,
				o.control.account,
				o.dimension1,			|#1056135.sn
				o.dimension2,			
				o.dimension3,				
				o.dimension4,				
				o.dimension5)			|#1056135.sn
		endif
	else
		get.control.account.from.nonfinalized.trans(
				i.company,
				i.year,
				i.batch,
				i.transaction.type,
				i.document,
				i.line,
				o.control.account,
				o.dimension1,			|#1056135.sn
				o.dimension2,			
				o.dimension3,				
				o.dimension4,				
				o.dimension5)			|#1056135.sn
		|* Purchase Corrections can be left out in case
		|* an invoice is not finalized because non
		|* finalized invoices cannot be corrected.
	endif	
}

function get.control.account.from.finalized.transactions(
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
		domain	tfgld.lino	i.line,
	ref	domain	tfgld.leac	o.control.account,
	ref	domain	tfgld.dimx	o.dimension1,		|#1056135.sn
	ref	domain	tfgld.dimx	o.dimension2,		
	ref	domain	tfgld.dimx	o.dimension3,		
	ref	domain	tfgld.dimx	o.dimension4,		
	ref	domain	tfgld.dimx	o.dimension5)		|#1056135.en
{
	|* Initialize
	o.control.account = ""
	o.dimension1 = ""					|#1056135.sn
	o.dimension2 = ""				
	o.dimension3 = ""				
	o.dimension4 = ""					
	o.dimension5 = ""					|#1056135.en
	
	select	tfgld106.leac:o.control.account,
		tfgld106.dim1:o.dimension1,			|#1056135.sn
		tfgld106.dim2:o.dimension2,			
		tfgld106.dim3:o.dimension3,			
		tfgld106.dim4:o.dimension4,		
		tfgld106.dim5:o.dimension5			|#1056135.sn
	from	tfgld106
	where	tfgld106._index1 = {:i.transaction.type,
								|#229214.so
|			            :i.document,
|				    :i.line}
								|#229214.eo
			            :i.document}		|#229214.n
	and	tfgld106.cont = tcyesno.yes
	and 	( tfgld106.catg in (tfgld.catg.purchase.inv,
				    tfgld.catg.purchase.cred) or
								|#229214.so
|		 (tfgld106.catg = tfgld.catg.cash and
|		  tfgld106.tcsh = tfcmg.tcsh.ssgn and
|		   tfgld106.tran in (tfcmg.tran.unalloc.paym,
|		   		     tfcmg.tran.advance.paym)))
								|#229214.eo
								|#229214.sn
		 (tfgld106.catg = tfgld.catg.cash and
		  tfgld106.tcsh = tfcmg.tcsh.ssgn and
		   tfgld106.tran in (tfcmg.tran.unalloc.paym,	
		   		     tfcmg.tran.advance.paym) and
		   tfgld106.olin = :i.line))	
								|#229214.en
	as set with 1 rows
	selectdo
	endselect
}	

function get.control.account.from.correction(
		domain	tcncmp		i.company,
		domain	tfgld.ttyp	i.trans.type.corr,
		domain	tfgld.docn	i.document.corr,
		domain	tfgld.btno	i.batch.corr,
		domain	tfgld.year	i.year.corr,
	ref	domain	tfgld.leac	io.control.account,
	ref	domain	tfgld.dimx	io.dimension1,		|#1056135.sn
	ref	domain	tfgld.dimx	io.dimension2,		
	ref	domain	tfgld.dimx	io.dimension3,		
	ref	domain	tfgld.dimx	io.dimension4,		
	ref	domain	tfgld.dimx	io.dimension5)		|#1056135.en
{
		domain	tfgld.leac	hold.control.account
		domain	tfgld.dimx	hold.dimension1		|#1056135.sn
		domain	tfgld.dimx	hold.dimension2		
		domain	tfgld.dimx	hold.dimension3		
		domain	tfgld.dimx	hold.dimension4		
		domain	tfgld.dimx	hold.dimension5		|#1056135.en
		domain	tfgld.lino	hold.line
|******************************************************************************
|* Explanation: this functions searches for the last correction document
|* to which a purchse invoice is linked. The control account of that last
|* correction document overwrites the original control account of the
|* invoice. The new attributes of the invoice, as a result of the
|* correction, are stored in the document with the highest line number
|* (tfgld106.olin/tfgld102.lino). So the order by is on the document 
|* date/time of the correction document and the line number,
|* all descending. 
|******************************************************************************

	|* Initialize
	hold.control.account = ""
	hold.dimension1 = ""					|#1056135.sn
	hold.dimension2 = ""				
	hold.dimension3 = ""				
	hold.dimension4 = ""					
	hold.dimension5 = ""					|#1056135.en

	select	tfgld102.leac:hold.control.account,
		tfgld102.dim1:hold.dimension1,			|#1056135.sn
		tfgld102.dim2:hold.dimension2,			
		tfgld102.dim3:hold.dimension3,			
		tfgld102.dim4:hold.dimension4,		
		tfgld102.dim5:hold.dimension5,			|#1056135.sn
		tfgld102.lino:hold.line	
	from	tfgld102
	where	tfgld102._index1 = {:i.company, :i.year.corr, :i.batch.corr,
				    :i.trans.type.corr, :i.document.corr}
	and 	tfgld102.catg = tfgld.catg.purchase.corr
	order by tfgld102.lino desc	
	as set with 1 rows
	selectdo
		io.control.account = hold.control.account
		io.dimension1 = hold.dimension1		|#1056135.sn
		io.dimension2 = hold.dimension1		
		io.dimension3 = hold.dimension1		
		io.dimension4 = hold.dimension1		
		io.dimension5 = hold.dimension1		|#1056135.sn
		return
		|* Non finalized corrections is will be regarded as of a later
		|* date than a finalized correction
	endselect

	select	tfgld106.leac:hold.control.account,
		tfgld106.dim1:hold.dimension1,			|#1056135.sn
		tfgld106.dim2:hold.dimension2,			
		tfgld106.dim3:hold.dimension3,			
		tfgld106.dim4:hold.dimension4,		
		tfgld106.dim5:hold.dimension5,			|#1056135.sn
		tfgld106.olin:hold.line		
	from	tfgld106
	where	tfgld106._index1 = {:i.trans.type.corr,
		                    :i.document.corr}
	and	tfgld106.cont = tcyesno.yes
	and 	tfgld106.catg = tfgld.catg.purchase.corr
	order by tfgld106.olin desc	
	as set with 1 rows
	selectdo
		io.control.account = hold.control.account
		io.dimension1 = hold.dimension1		|#1056135.sn
		io.dimension2 = hold.dimension1		
		io.dimension3 = hold.dimension1		
		io.dimension4 = hold.dimension1		
		io.dimension5 = hold.dimension1		|#1056135.sn
		return
	endselect
}	

function get.control.account.from.nonfinalized.trans(
		domain	tcncmp		i.company,
		domain	tfgld.year	i.year,
		domain	tfgld.btno	i.batch,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
		domain	tfgld.lino	i.line,
	ref	domain	tfgld.leac	o.control.account,
	ref	domain	tfgld.dimx	o.dimension1,		|#1056135.sn
	ref	domain	tfgld.dimx	o.dimension2,		
	ref	domain	tfgld.dimx	o.dimension3,		
	ref	domain	tfgld.dimx	o.dimension4,		
	ref	domain	tfgld.dimx	o.dimension5)		|#1056135.en
{
	|* Initialize
	o.control.account = ""
	o.dimension1 = ""					|#1056135.sn
	o.dimension2 = ""				
	o.dimension3 = ""				
	o.dimension4 = ""					
	o.dimension5 = ""					|#1056135.en
	
	select	tfgld102.leac:o.control.account,
		tfgld102.dim1:o.dimension1,			|#1056135.sn
		tfgld102.dim2:o.dimension2,			
		tfgld102.dim3:o.dimension3,			
		tfgld102.dim4:o.dimension4,		
		tfgld102.dim5:o.dimension5			|#1056135.sn
	from	tfgld102
	where	tfgld102._index1 = {:i.company,
				    :i.year,
				    :i.batch,
				    :i.transaction.type,
								|#229214.so
|			            :i.document,
|				    :i.line}
								|#229214.eo
			            :i.document}		|#229214.n
	and	tfgld102.cont = tcyesno.yes
	and 	( tfgld102.catg in (tfgld.catg.purchase.inv,
				    tfgld.catg.purchase.cred) or
								|#229214.so
|		 (tfgld102.catg = tfgld.catg.cash and
|		  tfgld102.tcsh = tfcmg.tcsh.ssgn and
|		   tfgld102.tran in (tfcmg.tran.unalloc.paym,
|		   		     tfcmg.tran.advance.paym)))
								|#229214.eo
								|#229214.sn
		 (tfgld102.catg = tfgld.catg.cash and
		  tfgld102.tcsh = tfcmg.tcsh.ssgn and
		   tfgld102.tran in (tfcmg.tran.unalloc.paym,
				     tfcmg.tran.advance.paym) and
		   tfgld102.lino = :i.line))	
								|#229214.en
	as set with 1 rows
	selectdo
	endselect
}
			
function domain tcbool invoice.is.finalized(
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document)
{
		domain	tfgld.trun	hold.run.number
		
	select	tfgld018.trun:hold.run.number
	from	tfgld018
	where	tfgld018._index1 = {:i.transaction.type,
				    :i.document}
	and	tfgld018.trun > 0			    
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}	

function domain tcbool get.correction.document(
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
	ref	domain	tfgld.ttyp	o.trans.type.corr,
	ref	domain	tfgld.docn	o.document.corr,
	ref	domain	tfgld.btno	o.batch.corr,
	ref	domain	tfgld.year	o.year.corr)
{

		domain	tfgld.date	hold.document.date
		domain	tcmcs.long	hold.time
		
	o.trans.type.corr = ""
	o.document.corr = 0
	o.batch.corr = 0
	o.year.corr = 0
	
	select	a.tdoc:o.trans.type.corr, a.docn:o.document.corr,
		a.btno:o.batch.corr, a.year:o.year.corr,
		tfgld018.date:hold.document.date, tfgld018.time:hold.time	
	from	tfacp200 a, tfgld018
	where	a._index1 = {:i.transaction.type, :i.document}
	and	a.tdoc <> ""
	and 	a.tpay = tfacp.tpay.correction
	and	tfgld018.ttyp = a.tdoc
	and	tfgld018.docn = a.docn
	order by tfgld018.date desc, tfgld018.time desc
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}								|#FP3.en

								|#1025236.so
|function get.payment.info(
|	ref domain	tcncmp		o.tfacp200.pcom,		
|	ref domain	tfgld.ttyp	o.tfacp200.payt,
|	ref domain	tfgld.docn	o.tfacp200.payd,
|	ref domain	tfgld.lino	o.tfacp200.payl)
|
|{
|	o.tfacp200.pcom = 0
|	o.tfacp200.payt = ""
|	o.tfacp200.payd = 0
|	o.tfacp200.payl = 0
|
|	select 	tfgld112.cono:o.tfacp200.pcom,
|		tfgld112.ttyp:o.tfacp200.payt,
|		tfgld112.docn:o.tfacp200.payd
|	from	tfgld112
|	where	tfgld112._index2 = {	:current.companyany,
|			    		:tfacp200.tdoc,
|			    		:tfacp200.docn}
|	as set with 1 rows
|	selectdo
|		o.tfacp200.payl = tfacp200.lino
|	selectempty
|		if not isspace(tfacp200.tdoc) then
|			o.tfacp200.pcom = current.companyany
|			o.tfacp200.payt = tfacp200.tdoc
|			o.tfacp200.payd = tfacp200.docn
|			o.tfacp200.payl = tfacp200.lino
|		endif
|	endselect
|}
								|#1025236.eo
								
								|#1025236.sn
function get.payment.info(
	ref domain	tcncmp		o.tfacp200.pcom,		
	ref domain	tfgld.ttyp	o.tfacp200.payt,
	ref domain	tfgld.docn	o.tfacp200.payd,
	ref domain	tfgld.lino	o.tfacp200.payl)

{
	domain	tcncmp		l.icom
	domain	tfgld.ttyp	l.ttyp
	domain	tfgld.docn	l.docn
	domain	tfgld.ttyp	l.search.ttyp
	domain	tfgld.docn	l.search.ninv

	o.tfacp200.pcom = 0
	o.tfacp200.payt = ""
	o.tfacp200.payd = 0
	o.tfacp200.payl = 0
	
	l.search.ttyp = ""
	l.search.ninv = 0


	if isspace(tfacp200.tdoc) then
		|* handle headers
		l.search.ttyp = ""
		l.search.ninv = 0			
		o.tfacp200.pcom = current.company
		if tfacp200.line > 0 then |* Payment header then
			o.tfacp200.payt = tfacp200.ttyp
			o.tfacp200.payd = tfacp200.ninv
			o.tfacp200.payl = tfacp200.line	
		endif
		if tfacp200.line > 0 and
                   (tfacp200.tpay = tfacp.tpay.advance.ant or 
                   tfacp200.tpay = tfacp.tpay.unallocated.ant or 
		   tfacp200.tpay = tfacp.tpay.advance or
                   tfacp200.tpay = tfacp.tpay.unallocated ) then                        
                        l.search.ttyp = tfacp200.ttyp
			l.search.ninv = tfacp200.ninv
                endif
		
	else
		|*handle subdocuments		
		l.search.ttyp = tfacp200.tdoc
		l.search.ninv = tfacp200.docn
	endif
	
	|*handle possible intercompany situation
	if not isspace(l.search.ttyp) then		
		if invoice.is.finalized(
				      l.search.ttyp,
				      l.search.ninv) then
			select 	tfgld106.osrn
			from	tfgld106
			where	tfgld106._index1 = {:l.search.ttyp,
						:l.search.ninv}
			and	tfgld106.ocmp <> :current.company 
			as set with 1 rows
			selectdo
				select 	tfgld112.cono:l.icom,
					tfgld112.ttyp:l.ttyp,
					tfgld112.docn:l.docn
				from	tfgld112
				where	tfgld112._index2 = {:current.company,
						    	:l.search.ttyp,
						    	:l.search.ninv}
				as set with 1 rows
				selectdo
					o.tfacp200.pcom = l.icom
					o.tfacp200.payt = l.ttyp
					o.tfacp200.payd = l.docn				
					o.tfacp200.payl = fill.payl.field(
								o.tfacp200.pcom,
								o.tfacp200.payt,
								o.tfacp200.payd)
				endselect
			selectempty
				|* no intercompany
				o.tfacp200.pcom = current.company
				o.tfacp200.payt = l.search.ttyp
				o.tfacp200.payd = l.search.ninv			
				o.tfacp200.payl = fill.payl.field(
							o.tfacp200.pcom,
							o.tfacp200.payt,
							o.tfacp200.payd)
			endselect 
		else
			select 	tfgld112.cono:l.icom,
				tfgld112.ttyp:l.ttyp,
				tfgld112.docn:l.docn
			from	tfgld112
			where	tfgld112._index2 = {:current.company,
					    	:l.search.ttyp,
					    	:l.search.ninv}
			as set with 1 rows
			selectdo
				o.tfacp200.pcom = l.icom
				o.tfacp200.payt = l.ttyp
				o.tfacp200.payd = l.docn				
				o.tfacp200.payl = fill.payl.field(
							o.tfacp200.pcom,
							o.tfacp200.payt,
							o.tfacp200.payd)
			selectempty
				|* no intercompany
				o.tfacp200.pcom = current.company
				o.tfacp200.payt = l.search.ttyp
				o.tfacp200.payd = l.search.ninv			
				o.tfacp200.payl = fill.payl.field(
							o.tfacp200.pcom,
							o.tfacp200.payt,
							o.tfacp200.payd)
			endselect
		endif
	endif
			
}



function domain	tfgld.lino fill.payl.field(
	 domain	tcncmp		i.tfacp200.pcom,		
	 domain	tfgld.ttyp	i.tfacp200.payt,
	 domain	tfgld.docn	i.tfacp200.payd)
{
	domain	tfgld.lino	line.number
	
	line.number = 0
	
	on case tfacp200.tpay
	case tfacp.tpay.assignment:
		line.number = get.line.mapped(tfacp200.lino)
		break
	case tfacp.tpay.normal:
	case tfacp.tpay.anticipated:
		if tfacp200.lino >= 10000 then
			line.number =
		   		tfcmg.dll8112.get.line.composed.anticipated(
					i.tfacp200.pcom,
					i.tfacp200.payt,
					i.tfacp200.payd,
					tfacp200.typa,
					tfacp200.doca)
		else
			line.number = tfacp200.lino
		endif
		break
	case tfacp.tpay.advance.ant:
	case tfacp.tpay.unallocated.ant:
	case tfacp.tpay.advance:
	case tfacp.tpay.unallocated:
		if tfacp200.line >= 10000 then
			line.number =
		   		tfcmg.dll8112.get.line.composed.anticipated(
					i.tfacp200.pcom,
					i.tfacp200.payt,
					i.tfacp200.payd,
					tfacp200.typa,
					tfacp200.doca)
		else
			line.number = tfacp200.line
		endif
		break
	default:
		line.number = tfacp200.lino
	endcase
	return(line.number)
}

function domain tfgld.lino get.line.mapped(
		domain	tfgld.lino	i.lino)
{
	domain	tfgld.lino	line.number

	|* Line number 10002 has to be mapped to 1 because the
	|* 10000 series belong to payment line 1. Line number
	|* 20001 has to be mapped to 2 because the 20000 series
	|* belong to payment line 2.
	line.number = i.lino \ 10000
	line.number = i.lino - line.number
	line.number = line.number / 10000

	return(line.number)
}


function extern domain tfgld.lino tfcmg.dll8112.get.line.composed.anticipated(
			domain	tcncmp		i.finance.company,
			domain	tfgld.ttyp	i.transaction.type,
			domain	tfgld.docn	i.document.number,
			domain	tfgld.ttyp	i.transaction.type.ant.payment,
			domain	tfgld.docn	i.document.number.ant.payment)
{
	DllUsage
	Expl.	: This function determines the payment line number. The 
		  transaction type, document number and sequence number remain
		  default.

	Pre	: -
	Post	: -
	Input	: i.finance.company	- Finance company
		  i.transaction.type	- The transaction type
		  i.document.number	- The document number
		  i.transaction.type.ant.payment
		  			- The transaction type of the
					  anticipated payment
		  i.document.number.ant.payment	
		  			- The document number of
					  the anticipated payment
		  i.business.partner	- The business partner

	Return	: line number
	EndDllUsage

	domain	tfgld.lino	line.number

	select	tfcmg112.clin:line.number
	from	tfcmg112
	where	tfcmg112._index2 = {:i.transaction.type, :i.document.number}
	and	tfcmg112.tdoc = :i.transaction.type.ant.payment
	and	tfcmg112.pdoc = :i.document.number.ant.payment
	and	tfcmg112._compnr = :i.finance.company
	as set with 1 rows
	selectdo
		break
	selectempty
		line.number = 0
	endselect
	
	return(line.number)
}								
								|#1025236.en

function void get.supplier.fin.business.partner.group(		|#Migr. 10.6.sn
		domain	tcncmp		i.company,
		domain	tcsuno		i.supplier	fixed,
	ref	domain	tfacp.fisu	o.fisc.bp.group)
{
	|**************************************************************
	|* Initialize variables.
	|**************************************************************
	o.fisc.bp.group = ""

	select	tccom020.cfsg:o.fisc.bp.group
	from	tccom020
	where	tccom020._compnr = :i.company
	and	tccom020._index1 = {:i.supplier}
	as set with 1 rows
	selectdo
	endselect
}								|#Migr. 10.6.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acp
:TFLNO:240
:FUNC:START
function handle.order.line()
{      
	|    *  Next situations are possible:   
	|    *    -  One entry in tdpur045 for order  /  position   
	|    *     	     -  no.of.receipt.lines  =  0 (no receipts yet)   
	|    *     	     -  tdpur045.srnb  =  0   
	|    *     	     -  one record added to tfacp240   
	|    *    -  One entry in tdpur045 for order  /  position   
	|    *     	     -  no.of.receipt.lines  =  1 (1 receipt)   
	|    *     	     -  tdpur045.srnb  =  1   
	|    *     	     -  one record added to tfacp240   
	|    *     	     -  one record added to tfacp245   
	|    *     	     -  This is a collect order (order and receipt in once)   
	|    *    -  Two entries in tdpur045 for order  /  position   
	|    *     	     -  no.of.repeipt.lines  =  1 (first receipt)   
	|    *     	     -  one record added to tfacp240   
	|    *     	     -  one record added to tfacp245, if tdpur045.date  <>  0   
	|    *    -  More than two entries in tdpur045 for order  /  position   
	|    *     	   (backorders present)   
	|    *     	     -  no.of.receipt.lines  >=  2   
	|    *     	     -  one record added to tfacp240 (original order line)   
	|    *     	     -  one record added to tfacp240 for each backorder   
	|    *     	     -  for each record of tdpur045 with tdpur045.date  <>  0   
	|    *     	      	   (actual receipt date) a record is added to   
	|    *     	      	   tfacp245.   
	if no.of.receipt.lines  =  0 or tdpur045.srnb  >=  1 then   
		|    *  Original order line and the backorders are inserted   
		|    *  in tfacp240   
		|    *  Only if there is no receipt tfacp240 record is   
		|    *  added seperately. Else tfacp240 is inserted when   
		|    *  tfacp245 is inserted.   
		var.tfacp240.sqnb  =  get.order.line.sequence(   
		      	      	   tdpur045.srnb)   
		var.tfacp240.otbp  =  get.business.partner.code(   
		      	      	   tdpur040.suno,   
		      	      	   bcstc.orgn.suno,   
		      	      	   error.message)   
		|    *  Also used for ifbp and sfbp   
		var.tfacp240.proj  =  get.project.by.company(   
		      	      	   tdpur045.cprj,   
		      	      	   logistic.company)   
		if not isspace(tdpur045.cprj) then   
			var.tfacp240.bppr  =  get.baan.project(   
						logistic.company,   
						tdpur045.cprj)   
		else   
			var.tfacp240.bppr  =  tcyesno.no   
		endif   	      
		if tdpur045.srnb  <  2 then   
		     |    *  Normal order   
			convert.quantity.from.order.unit.to.inventory.unit(   
				tdpur041.oqua,   
				var.tfacp240.iuni,   
				tdpur041.cvqp,   
				"",    
				"",    
				"tcqrd1",    
				var.tfacp240.oqan)   
		else   
			convert.quantity.from.order.unit.to.inventory.unit (   
				prev.bqua,   
				var.tfacp240.iuni,   
				tdpur041.cvqp,   
				"",    
				"",    
				"tcqrd1",    
				var.tfacp240.oqan)   
				|    *  Back order   
		endif   	      
		get.order.amount(var.tfacp240.oamt)   
		|    *  Calculate amount in home currencies   
		for element  =  1 to 3   
			ret.long  =  calculate.amount.in.home.currency(   
		      	      	   var.tfacp240.oamt,   
		      	      	   tdpur040.ccur,   
		      	      	   tdpur040.ratp,   
		      	      	   tdpur040.ratf,   
		      	      	   element,   
		      	      	   var.tfacp240.amth(element),   
		      	      	   error.message)   
		endfor   
		
		
		|    *  In Baan IV only one receipt per order line.   
		|    *  Quantities on order line can be taken from the receipt.   
		|    *  Mapping table is used for the receipts
		var.tfacp245.rcno  =  get.receipt.number()   
		var.tfacp245.rseq  =  get.receipt.line()   
		if double.cmp(tdpur041.oqua, 0.0, 0.00001)  =    -  1 then   
			|    *  Return order   
			convert.quantity.from.order.unit.to.inventory.unit (   
				tdpur045.quad,   	  
				var.tfacp240.iuni,   
				tdpur041.cvqp,   
				"",    
				"",    
				"tcqrd1",    
				var.tfacp240.tapq)   
			
				|    *  Approved quantity equals rejected quantity   
				var.tfacp240.trqu  =  0.0   
				|    *  Rejected quantity is zero   
				get.shipment.number.and.line(    
				tckoor.act.pur,   
				tdpur045.orno,   
				tdpur045.pono,   
				var.tfacp240.sqnb,   
				0,   	     |    *  Outbound Sequence     
				0,   
				error.message)   	     |    *  ref   
      	   	else   
			convert.quantity.from.order.unit.to.inventory.unit (   
			      	   tdpur045.quap,      
			      	   var.tfacp240.iuni,   
			      	   tdpur041.cvqp,   
			      	   "",    
			      	   "",    
			      	   "tcqrd1",    
			      	   var.tfacp240.tapq)   	      
			convert.quantity.from.order.unit.to.inventory.unit (   
			      	   tdpur045.quad,   	    
			      	   var.tfacp240.iuni,   
			      	   tdpur041.cvqp,   
			      	   "",    
			      	   "",    
			      	   "tcqrd1",    
			      	   var.tfacp240.trqu)   
		endif   
		convert.quantity.from.order.unit.to.inventory.unit (   
			tdpur045.dqua,   	 
			var.tfacp240.iuni,   
			tdpur041.cvqp,   
			"",    
			"",    
			"tcqrd1",    
			var.tfacp240.rqty)   
      	      
		if (tdpur045.matc  =  tdpur.matc.complete) or   
		   (tdpur045.stsd  =  tcstsd.all.approved) or 
		   (tdpur045.date  <>  0 and zero.receipt(   
						tdpur045.dqua,   
						tdpur045.quap,   
						tdpur045.quad,   
						tdpur041.oqua)) then   
		|    *  Zero receipt. In BaanIV there is only 1   
		|    *  receipt per orderline  /  sequence number.   
		|    *  If thereis no receipt a back order with   
		|    *  a new sequence number is made.   
			var.tfacp240.toma  =  tcyesno.yes   
		else   
			var.tfacp240.toma  =  tcyesno.no   
		endif   
		if tdpur045.date  <>  0 then   
			var.tfacp240.fire  =  tcyesno.yes   
		else   
			var.tfacp240.fire  =  tcyesno.no   
		endif   	      
		var.tfacp240.mcfr  =  get.tfacp240.mcfr()   
		|    *  Fill Rates   
		for element  =  1 to 3   
			var.tfacp240.rate(element)  =  fill.mc.field.double(   
								tdpur040.ratp,   
								element,   
								0.000001)   	      
			var.tfacp240.ratf(element)  =  fill.mc.field.long(   
								tdpur040.ratf,   
								element,   
								1)   	      
		endfor
		
		if prev.orno <> tdpur045.orno or
		   prev.pono <> tdpur045.pono or
		   prev.srnb <> tdpur045.srnb then
			dump 1
		endif   
		prev.orno = tdpur045.orno
		prev.pono = tdpur045.pono
		prev.srnb = tdpur045.srnb
	endif   
	
	|* now dump the receipts with the usage of the mapping table
	if (tdpur045.date  <>  0) and not zero.receipt(   
						tdpur045.dqua,   
						tdpur045.quap,   
						tdpur045.quad,   
						tdpur041.oqua) then 

		get.order.receipt.amount(var.tfacp245.oamt)   
 		if double.cmp(tdpur041.oqua, 0.0, 0.00001)  =    -  1 then   
			if g.use.mapping.table then   
				g.quantity  =  bcswh010.qapr   
			else   
				g.quantity  =  tdpur045.quad   
			endif   
			|    *  Return order   
			convert.quantity.from.order.unit.to.inventory.unit (   
				g.quantity,   	  
				var.tfacp240.iuni,   
				tdpur041.cvqp,   
				"",    
				"",    
				"tcqrd1",    
				var.tfacp245.quan)   
			
				|    *  Approved quantity equals rejected quantity   
				var.tfacp245.trqn  =  0.0   
      	   	else   
			if g.use.mapping.table then   
				g.quantity  =  bcswh010.qapr   
			else   
				g.quantity  =  tdpur045.quap   
			endif   
			convert.quantity.from.order.unit.to.inventory.unit (   
			      	   g.quantity,      
			      	   var.tfacp240.iuni,   
			      	   tdpur041.cvqp,   
			      	   "",    
			      	   "",    
			      	   "tcqrd1",    
			      	   var.tfacp245.quan)   	      
			if g.use.mapping.table then   
			   	g.quantity  =  bcswh010.qrej   
			else   
			   	g.quantity  =  tdpur045.quad   
			endif   
			convert.quantity.from.order.unit.to.inventory.unit (   
			      	   g.quantity,   	    
			      	   var.tfacp240.iuni,   
			      	   tdpur041.cvqp,   
			      	   "",    
			      	   "",    
			      	   "tcqrd1",    
			      	   var.tfacp245.trqn)   
		endif   
		if g.use.mapping.table then   
			g.quantity  =  bcswh010.qrcv   
		else   
			g.quantity  =  tdpur045.dqua   
		endif   
		convert.quantity.from.order.unit.to.inventory.unit (   
			g.quantity,   	 
			var.tfacp240.iuni,   
			tdpur041.cvqp,   
			"",    
			"",    
			"tcqrd1",    
			var.tfacp245.rqty)   

 		|    *  Actual receipt   
		var.tfacp245.borf  =  make.business.obj.ref(   
						tdpur045.pono,   
						tdpur045.srnb,   
						var.tfacp245.rcno,   
						var.tfacp245.rseq)  |     
		dump 3   
		
		if double.cmp(var.tfacp240.oqan, 0.0,0.000001)  =  0 then   
			var.tfacp249.gamt  =  var.tfacp245.oamt   
		else   
			var.tfacp249.gamt  = (var.tfacp240.oamt / 
						var.tfacp240.oqan) * var.tfacp245.quan   
			tccri.dll0011.get.rounding.code(   
				tdpur040.ccur,   
				var.inv.crnd)   
				var.tfacp249.gamt  =  icom0001.domain.round(   
				var.tfacp249.gamt,   
				"tfgld.amnt",    
				tdpur040.ccur,   
				var.inv.crnd)   
		endif
		dump 4   
	endif 
	g.prev.bqua  =  tdpur045.bqua   
 	g.prev.amld  =  tdpur045.ldam   
 	g.prev.amod  =  tdpur045.odam
  	      
}


function read.purchase.order.and.order.type.data(
		domain	tcorno		i.order,
		domain	tcncmp		i.logistic.company)
{
 	 select  tdpur040.cotp, tdpur040.suno, tdpur040.ccor,
 	 	 tdpur040.orno, tdpur040.ccur, tdpur040.odis,
		 tdpur040.odat, tdpur040.ratp, tdpur040.ratf,
		 tdpur040.ccon, 
		 tdpur040.cdec, tdpur040.cpay,			|#migration fp5.n
		 tcmcs042.reto, tcmcs042.coun,
		 tcmcs002.crnd
 	 from 	 tdpur040, tcmcs002, tcmcs042
 	 where 	 tdpur040._index1 = { :i.order}
	 and	 tdpur040._compnr = :i.logistic.company
 	 and 	 tcmcs002.ccur = tdpur040.ccur
	 and	 tcmcs042.cotp = tdpur040.cotp
 	 as set with 1 rows
 	 selectdo
 	 endselect
}

function void get.item.attributes(
		domain 	tcitem 	i.item,
		domain	tcittb	i.item.category,
		domain	tccprj	i.project,
	ref	domain	tckitm	o.item.type,
	ref	domain	tccitg	o.item.group,
	ref	domain	tccuni	o.inventory.unit)
{
	 	domain 	tcpuma 	 	project.item.type
 	static  domain  tccprj 	  	s.project
 	static  domain  tcittb 	  	s.item.category
 	static  domain  tcitem 	  	s.item
	static	domain	tckitm		s.item.type
	static	domain	tccitg		s.item.group
	static	domain	tccuni		s.inventory.unit
	 
	o.item.type = empty
	o.inventory.unit = "" 
	o.item.group = ""
	
 	 if s.project = i.project and
 	    s.item.category = i.item.category and
 	    s.item = i.item then
		o.item.type = s.item.type 
		o.item.group = s.item.group 
		o.inventory.unit = s.inventory.unit 
 	 	 return
 	 else
 	 	 s.project = i.project 
 	 	 s.item.category = i.item.category
 	 	 s.item = i.item
 	 endif
 	| **************************************************************  
 	| * For standard items the data is retrieved from tiitm001.  
 	| * For customized items the data is retrieved from tipcs021.  
 	| **************************************************************  
	if i.item.category = tcittb.standard then	
		select 	tiitm001.kitm:o.item.type,  
			tiitm001.citg:o.item.group,
			tiitm001.cuni:o.inventory.unit
 		from	tiitm001  
 		where	tiitm001._index1 =  {:i.item}  
 		as set with 1 rows 
		selectdo 





	 	endselect
	else	
		 select tipcs021.kitm:project.item.type,
 	 	 	 tipcs021.cuni:o.inventory.unit,
			 tipcs021.citg:o.item.group
 	 	 from 	 tipcs021
 	 	 where 	 tipcs021._index1 = {:i.project,
 	 	 	 	 	     :i.item}
 	 	 as set with 1 rows
 	 	 selectdo
 	 	 	 if project.item.type = tcpuma.purchase then
 	 	 	 	 o.item.type = tckitm.purchase
 	 	 	 endif
 	 	 	 if project.item.type = tcpuma.manufacture then
 	 	 	 	 o.item.type = tckitm.manufacture
 	 	 	 endif
 	 	 endselect
	endif	 
	
	s.item.type = o.item.type
	s.item.group = o.item.group 
	s.inventory.unit = o.inventory.unit
}

function domain tcpono get.order.line.sequence (domain tcsrnb i.sequence)
{
	if i.sequence = 0 then
		return ( 1 )
	else
		return ( i.sequence )
	endif
}

function get.number.of.receipt.lines ( 
		domain	tcorno	i.order,
		domain	tcpono	i.position,
		domain	tcncmp	i.logistic.company,
	ref	long		o.no.of.receipt.lines)	
{
 	 	 domain 	 tcorno 	 	 l.orno
 	 	 domain 	 tcpono 	 	 l.pono
 	 	 domain 	 tcsrnb 	 	 l.srnb
		 
 	 o.no.of.receipt.lines = 0

 	 select  tdpur045.srnb :o.no.of.receipt.lines,
 	 	 tdpur045.orno :l.orno,
 	 	 tdpur045.pono :l.pono,
 	 	 tdpur045.srnb :l.srnb
 	 from 	 tdpur045
 	 where 	 tdpur045._index1 = { 	 :i.order,
 	 	 	 	 	 :i.position}
	and	tdpur045._compnr = :i.logistic.company
 	 order by tdpur045._index1 desc
 	 as set with 1 rows
 	 selectdo 	
 	 endselect
}

 function domain tcitem get.item(
 		domain	tcncmp		i.logistic.company,
 		domain 	tcitem 		i.item,
		domain	tcittb		i.item.category,
		domain	tccprj		i.project)
 {
 	 if i.item.category = tcittb.customized then
 	 	 return ( segment.item.code (
 	 	 	 	 i.item,
 	 	 	 	 get.project.by.company(
				    i.project,
				    i.logistic.company)))
 	 else
 	 	 return ( segment.item.code (
 	 	 	 	 i.item,
 	 	 	 	 "" ))
 	 endif
 }
 
 function domain tcyesno get.baan.project(
 		domain	tcncmp	i.logistic.company,
 		domain 	tccprj 	i.project)
 {
 	 static 	 domain 	 tccprj 	 s.prev.project
 	 static 	 domain 	 tcyesno 	 s.baan.project

 	 if s.prev.project = i.project then
 	 	 return ( s.baan.project)
 	 else
 	 	 s.prev.project = i.project
 	 	 s.baan.project = tcyesno.no
 	 endif
	 
 	 if isspace ( i.project ) then
 	 	 return ( s.baan.project )
 	 endif
	 
 	 select  tipcs020.prtp
 	 from 	 tipcs020
 	 where 	 tipcs020._index1 = {:i.project}
	 and	tipcs020._compnr = :i.logistic.company
 	 as set with 1 rows
 	 selectdo
 	 	 if tipcs020.prtp = tcyesno.yes then
 	 	 	 s.baan.project = tcyesno.yes
 	 	 else
 	 	 	 s.baan.project = tcyesno.no
 	 	 endif
	 selectempty


	 	 s.baan.project = tcyesno.yes
 	 endselect
	 
 	 return ( s.baan.project )
 }

function string make.business.obj.ref(
		domain	tcpono		i.position,
|		domain	tcsrnb		i.sequence)		|#236822.so
		domain	tcsrnb		i.sequence,		|#236822.sn
		domain	tcmcs.str9	i.rcno,	
		domain	tcpono		i.rseq)			|#236822.en
{
	string		bo.reference(40) 
	
	i.sequence =  get.order.line.sequence(i.sequence)
	
								|#236822.so
|	bo.reference = str$(i.position) & chr$(47) & str$(i.sequence)
								|#236822.eo
								|#236822.sn
	 bo.reference = str$(i.position) &  chr$(47) &   
 	 	 	str$(i.sequence) &  chr$(47) &   
 	 	 	shiftl$(strip$(i.rcno)) &  chr$(47) &   
 	 	 	str$(i.rseq) &  chr$(47) &   
  	 	 	str$(0) &  chr$(47) &   
 	 	 	str$(0) &  chr$(47) &   
 	 	 	str$(0) 	 	 	 	 	  
								|#236822.en
	return(bo.reference)
}

 function domain bccadr get.bp.address (
 		domain	tcncmp		i.logistic.company,
 		domain	tcorno		i.order,
		domain	tcsuno		i.supplier,
		domain	tcccor		i.postal.address)
 {
 	| * Expl. : Use the function to convert purchase order  
 	| * 	 ship - from BP address.  
 	| * Pre 	 : tdpur040 fields current  
 	| * Return : Converted BP address
		domain		tcccor		post.address.supplier
 	 	 domain 	 bcstc.keys 	  l.key fixed
 	 static  domain 	 tcorno 	  s.order
 	 static  domain 	 bccadr 	  s.address
 	| * Initialization.  
 	 l.key = ""
 	| * To prevent determining the addres multiple times for 1  
 	| * order.  

 	 if s.order = i.order then
 	 	 return ( s.address )
 	 else
 	 	 s.order = i.order
 	 	 s.address = ""
 	 endif
	 
 	| * 1. Get postal address from purchase order header.  
 	 if not isspace ( i.postal.address ) then
 	 	 l.key ( 1;6 ) = i.supplier
 	 	 l.key ( 7;3 ) = i.postal.address
 	 	 s.address = get.address.code (
 	 	 	 	 "tccom022",
 	 	 	 	 l.key,
 	 	 	 	 error.message ) 	| * ref  
 	 	 return ( s.address )
 	 endif
	 
 	| * 2. Get specific postal address.  
 	 select	 tdpur042._index1
 	 from 	 tdpur042
 	 where 	 tdpur042._index1 = { 	 :i.order,
 	 	 	 	 	 0, 	| * Position number  
 	 	 	 	 	 1} 	| * Type : postal address 
	 and	tdpur042._compnr = :i.logistic.company				 
 	 as set with 1 rows
 	 selectdo
 	 	 l.key ( 1;6 ) = edit$ ( tdpur042.orno, "999999" )
 	 	 l.key ( 7;4 ) = edit$ ( tdpur042.pono, "9999" )
 	 	 l.key ( 11;2 ) = edit$ ( tdpur042.ckor, "99" )
 	 	 s.address = get.address.code (
 	 	 	 	 "tdpur042",
 	 	 	 	 l.key,
 	 	 	 	 error.message ) 	| * ref  
 	 	 return ( s.address )
 	 endselect
	 
 	| * 3. Get postal address from supplier.  
 	 read.supplier (i.supplier, post.address.supplier)
 	 if not isspace ( post.address.supplier ) then
 	 	 l.key ( 1;6 ) = i.supplier
 	 	 l.key ( 7;3 ) = post.address.supplier
 	 	 s.address = get.address.code (
 	 	 	 	 "tccom022",
 	 	 	 	 l.key,
 	 	 	 	 error.message )
 	 	 return ( s.address )
 	 endif
	 


 	| * 4. Get address from supplier.  
 	 s.address = get.address.code (
 	 	 	 "tccom020",
 	 	 	 i.supplier,
 	 	 	 error.message )
 	 return ( s.address )
 }

 function read.supplier (
 		domain	tcsuno		i.supplier,
	ref	domain	tcccor		o.post.address.supplier)
 {
 	 static domain 	 tcsuno 	  s.supplier
	 static	domain	tcccor		s.post.address.supplier
	 
 	 if s.supplier = i.supplier then
		 o.post.address.supplier = s.post.address.supplier 
 	 	 return
 	 else
 	 	 s.supplier = i.supplier
 	 endif
 	 select  tccom020.ccor:s.post.address.supplier
 	 from 	 tccom020
 	 where 	 tccom020._index1 = { :i.supplier}
 	 as set with 1 rows
 	 selectdo
 	 endselect

	 o.post.address.supplier = s.post.address.supplier
 }

function domain bccadr get.delivery.address ()
 {
 	| * Expl. : Use the function to convert purchase order line  
 	| * 	 delivery address.  
 	| * Pre 	 : tdpur041 present  
 	| * Return : Converted delivery address  
 	 	 	domain 	 	bcstc.keys 	 l.key fixed


 	 static 	 domain 	 tcorno 	  s.order

 	 static 	 domain 	 tcpono 	  s.position
 	 static 	 domain 	 bccadr 	  s.address
 	| * To prevent determining the addres multiple times for 1  
 	| * order.  
	
 	 if s.order = tdpur041.orno and
 	 s.position = tdpur041.pono then
 	 	 return ( s.address )
 	 else
 	 	 s.order = tdpur041.orno
 	 	 s.position = tdpur041.pono
 	 	 s.address = ""
 	 endif
 	| * 1. Get specific delivery address.  
 	 select  tdpur042._index1
 	 from 	 tdpur042
 	 where 	 tdpur042._index1 = { 	 :tdpur041.orno,
 	 	 	 	 	 :tdpur041.pono,
 	 	 	 	 	 0} 	| * Type : delivery address  
	 and	tdpur042._compnr = :logistic.company				 
 	 as set with 1 rows
 	 selectdo
 	 	 l.key ( 1;6 ) = edit$ ( tdpur042.orno, "999999" )
 	 	 l.key ( 7;4 ) = edit$ ( tdpur042.pono, "9999" )
 	 	 l.key ( 11;2 ) = edit$ ( tdpur042.ckor, "99" )
 	 	 s.address = get.address.code (
 	 	 	 	 "tdpur042",
 	 	 	 	 l.key,
 	 	 	 	 error.message )

 	 	 return ( s.address )
 	 endselect
 	| * 2. Get delivery address from warehouse.  
 	 if not isspace ( tdpur041.cwar ) then
 	 	 s.address = get.address.code (

 	 	 	 	 "tcmcs003",
 	 	 	 	 tdpur041.cwar,
 	 	 	 	 error.message )
 	 	 return ( s.address )
 	 endif
	 
 	 return ( s.address )
 }

								
function get.order.amount(
 	 	 ref 	 domain  tcamnt 	 o.order.amount)
 {
 	 	 domain 	 tcamnt 	 l.dummy.amount
		 
 	if tdpur045.srnb < 2 then
 	 	 o.order.amount = tdpur041.amta
	else	 
	|* Backorder	
	 	 if tdpur045.pric = 0 or
 		 	prev.bqua = 0 then
 	 	 	o.order.amount = 0.0
		else 
 	 		if tdpur045.date <> 0 then
 	 		 	tdpur.dll0022.calc.net.amount (
 	 		 		tdpur041.cvqp,
 	 		 		tdpur041.cvpp,
 	 		 		prev.bqua,
 	 		 		tdpur045.pric,
 	 		 		tdpur040.ccur,
 	 	 	 		tcmcs002.crnd,
 	 	 	 		tdpur041.dmth,
 	 	 	 		tdpur045.disc,
 	 	 	 		tdpur045.damt,
 	 	 	 		o.order.amount, 	| * ref  
 	 	 	 		l.dummy.amount ) 	| * ref  
			else		 
 		| * When no receipt is done yet, the order line discount  
 		| * amount of a backorder line must be calculated out of the  
 		| * backorder quantity of the previous receipt line and the  
 		| * price and discounts of the current line.  
 	 			tdpur.dll0022.calc.net.amount (
 	 		 	tdpur041.cvqp,
 	 		 	tdpur041.cvpp,
 	 		 	prev.bqua,
 	 		 	tdpur045.pric,
 	 		 	tdpur040.ccur,
 	 		 	tcmcs002.crnd,
 	 		 	tdpur041.dmth,
 	 		 	tdpur045.disc,
 	 		 	tdpur045.damt,
 	 		 	o.order.amount, 	| * ref  
 	 		 	l.dummy.amount) 	| * ref
			endif	
		endif	
	endif
	
	if not item.type = tckitm.cost or
 	       item.type = tckitm.service then
  	 	o.order.amount = imcs0010.amnt.min.odis (
  	 	 	 	 	 o.order.amount,
  	 	 	 	 	 tdpur040.odis,
  	 	 	 	 	 tdpur040.ccur,
  	 	 	 	 	 tcmcs002.crnd )
 	endif	
}
								
function  get.order.receipt.amount(
			ref 	 domain  tcamnt 	 o.order.amount)
{
		domain	tcamnt		l.dummy.amount

	get.amounts(	o.order.amount,	|* ref
			l.dummy.amount,	|* ref
			l.dummy.amount)	|* ref

}

function get.amounts(					
		ref	domain	tcamnt		o.order.amount,
		ref	domain	tcamnt		o.line.disc.amount,
		ref	domain	tcamnt		o.order.disc.amount)
{
	static	domain	tcorno		s.order.number
	static	domain	tcpono		s.position
	static	domain	tcpono		s.sequence
	static	domain	tcpono		s.ilc.sequence
	static	domain	tcamnt		s.order.amount
	static	domain	tcamnt		s.line.disc.amount
	static	domain	tcamnt		s.order.disc.amount
		domain	tcamnt		gross.order.amount
		domain	tcamnt		order.amount
		domain	tcamnt		line.disc.amount
		domain	tcamnt		order.disc.amount

	o.order.amount = 0.0
	o.line.disc.amount = 0.0
	o.order.disc.amount = 0.0

	if s.order.number = tdpur045.orno and
	   s.position = tdpur045.pono and
	   s.sequence = tdpur045.srnb and
	   s.ilc.sequence = bcswh010.srni then
	   	o.order.amount = s.order.amount 
		o.line.disc.amount = s.line.disc.amount
		o.order.disc.amount = s.order.disc.amount
		return
	endif

	s.order.number = tdpur045.orno
	s.position = tdpur045.pono
	s.sequence = tdpur045.srnb
	s.ilc.sequence = bcswh010.srni


	if logistic.company <> current.company then		|#1645612.sn
		compnr.check(logistic.company)			
	endif							|#1645612.en
	bcint.dll0000.get.order.line.amounts(	
		tdpur045.orno,
		tdpur045.pono,
		tdpur045.srnb,
		false,			|* total line
		|* Below: input from tdpur040
		tdpur040.ccur,	
		tdpur040.odis, 
		tcmcs002.crnd, 
		|* Below: input from tdpur041
		item.type,
		tdpur041.bqua,	
		tdpur041.cvqp,
		tdpur041.cvpp,
		tdpur041.amta,
		tdpur041.dmth, 
		|* Below: input from tdpur045
		tdpur045.date,
		tdpur045.pric,
		tdpur045.disc,
		tdpur045.damt,
		tdpur045.ldam,
		tdpur045.odam,
		g.prev.amld,
		g.prev.amod,
		g.prev.bqua,
		gross.order.amount,	|* ref
		order.amount,	|* ref
		line.disc.amount,	|* ref
		order.disc.amount)	|* ref

	if not g.use.mapping.table then
		|* we're done: calculated amounts are on tdpur045
		|* level and in the current situation, we create only 
		|* a single tdpur401 level (total lines excluded) for
		|* each tdpur045 record
		o.order.amount = order.amount
		o.line.disc.amount = line.disc.amount
		o.order.disc.amount = order.disc.amount
	   	s.order.amount = o.order.amount 
		s.line.disc.amount = o.line.disc.amount
		s.order.disc.amount = o.order.disc.amount
		if logistic.company <> current.company then	|#1645612.sn
			compnr.check(current.company)		
		endif						|#1645612.en
		return
	endif

	|* Function is called on ilc-level; divide the calculated
	|* amounts over the advice lines.
	bcint.dll0000.divide.amounts.according.to.received.qty(
		tdpur045.orno,
		tdpur045.pono,
		tdpur045.srnb,
		bcswh010.srni,
		var.tfacp240.iuni,
		tdpur040.ccur,	
		tcmcs002.crnd, 
		order.amount,
		line.disc.amount,
		order.disc.amount,
		o.order.amount,		|* ref
		o.line.disc.amount,	|* ref
		o.order.disc.amount)	|* ref
					
	s.order.amount = o.order.amount 
	s.line.disc.amount = o.line.disc.amount
	s.order.disc.amount = o.order.disc.amount
	if logistic.company <> current.company then		|#1645612.sn
		compnr.check(current.company)			
	endif							|#1645612.en

}

function domain tcbool zero.receipt(
		domain	tcqrd1		i.delivered.quantity,
		domain	tcqrd1		i.approved.quantity,
		domain	tcqrd1		i.rejected.quantity,
		domain	tcqrd1		i.order.line.quantity)
{
								|#207117.sn
	 if double.cmp(i.order.line.quantity, 0.0, 0.00001) = -1 then
		 |* Return order
		 return(false)
	 endif			
	|**********************************************************************
	|* In BaanIV, in case of a return order, the deliverd quantity and the
	|* rejected quantity are always equal. So, a return order looks like an
	|* order of which all that is delivered is rejected (zero receipt). But
	|* a return order must not be handled as a normal order with zero
	|* receipt.
	|**********************************************************************
								|#207117.en

 	 if (double.cmp(  
 	 	 i.approved.quantity +  i.rejected.quantity,  
 	 	 i.delivered.quantity,  
 	 	 0.00001) =  0 and  
 	 | *  Aproved plus rejected equals delivered;  
 	 | *  all inspections are closed  
  	 	double.cmp(  
   	 		i.delivered.quantity,  
  	 		i.rejected.quantity,  
  	 		0.00001) =  0 and  
  	 | *  All delivered is rejected  
  	 	 double.cmp(i.order.line.quantity, 0.0, 0.00001) <>  0) then 
  	 | *  Not a cost item with quantity zero)  
	 	return(true)
	endif
	
	return(false)
}

								|#1422401.so
|function domain tcpono get.receipt.line()			|#149975.sn
|{
|		domain	tcpono		receipt.line
|		domain	tcrcno		max.receipt.number
|	static	domain	tcorno		s.order
|	static	domain	tcpono		s.position
|	static	domain	tcpono		s.sequence
||	static	domain	tcpono		s.array.receipt.line(1) based|#1019539.o
|	static		long		s.array.length
|			long		ret			|#228027.sn
|		domain	tcmcs.s200m	error.message		|#228027.en
|		
|	if tdpur045.date = 0 then
|		return(0)
|	endif
|
|	if tdpur045.reno = 0 then
|		return(0)
|	endif
|
|	if tdpur041.oqua < 0 then				|#228027.sn
|		if tdpur041.btsp = tcbtsp.bookto.comp then	
|			|* For return of components the receipt sequence
|			|* (shipment line) is 0 on the purchase order line.
|			return(0)
|		endif						
|		ret = get.shipment.number.and.line(
|			tckoor.act.pur,
|			tdpur045.orno,
|			tdpur045.pono,
|			get.order.line.sequence(tdpur045.srnb),
|			0,		|* Outbound Sequence	|#243681.n
|			0,
|			error.message)	|* ref
|		return(bcswh008.shln)
|	endif							|#228027.en
|
|	if s.array.length = 0 then
|		max.receipt.number =
|			lpow(10, icom0015.domain.length("tcrcno")) - 1
|		s.array.length = max.receipt.number
|		alloc.mem(s.array.receipt.line, s.array.length)
|	endif
|
|	if s.order = tdpur045.orno and
|	   s.position = tdpur045.pono and
|	   s.sequence = tdpur045.srnb then
|		receipt.line = s.array.receipt.line(tdpur045.reno)
|	else
|		|* Default step size is 10 (same as in WHINH and TDPUR).
|		receipt.line = s.array.receipt.line(tdpur045.reno) + 10
|		s.array.receipt.line(tdpur045.reno) = receipt.line
|		s.order = tdpur045.orno
|		s.position = tdpur045.pono
|		s.sequence = tdpur045.srnb
|	endif
|
|	return(receipt.line)
|}								|#149975.en
								|#1422401.eo

								|#1422401.sn
function domain tcpono get.receipt.line()		
{
		domain	tcpono		receipt.line
			long		ret			
		domain	tcmcs.s200m	error.message		
		
	receipt.line = 0 
	if tdpur045.date = 0 then
		return(receipt.line)
	endif
								|#fp10.so
|	if tdpur045.reno = 0 then
|		return(receipt.line)
|	endif
								|#fp10.eo
	
	if g.use.mapping.table then				|#FP10.sn
		return(bcswh010.rcln)
	else							|#fp10.en
		ret = get.receipt.rseq(
			tckoor.act.pur,
			tdpur045.orno,
			tdpur045.pono,
			get.order.line.sequence(tdpur045.srnb),
			0,	
			0,
			receipt.line,
			error.message)	|* ref
	endif							|#FP10.en
	
	return(receipt.line)
}						
								|#1422401.en
								|#1422401.so
|function string get.receipt.number()				|#221043.sn
|{
|			long		ret			
|	static	domain	tcorno		s.order
|	static	domain	tcpono		s.position
|	static	domain	tcpono		s.sequence
|	static	domain	tcrcno		s.last.receipt.number
|		domain	tcmcs.s200m	error.message		|#228027.n
|
|	if tdpur045.date = 0 then
|		return("")
|	endif
|
|	if tdpur045.reno = 0 then			
|								|#10110971.so
||		if s.order <> tdpur045.orno or
||		   s.position <> tdpur045.pono or
||		   s.sequence <> tdpur045.srnb then
||			s.last.receipt.number = s.last.receipt.number + 1
||			s.order = tdpur045.orno
||			s.position = tdpur045.pono
||			s.sequence = tdpur045.srnb
||		endif
||		return(	shiftl$(strip$(bcswh000.conv)) &
||			edit$(s.last.receipt.number, "999999"))
|								|#10110971.eo
|								|#10110971.sn
|		select  bcstf014.rcno
|		from 	bcstf014
|		where 	bcstf014._index1 = { 	 
|					:tdpur045.orno,
|		 	 	 	:tdpur045.pono,
|		 	 	 	:tdpur045.srnb}  
|		as set with 1 rows
|		selectdo
|			return ( bcstf014.rcno )
|		endselect
|								|#10110971.en
|								
|	endif					
|								
||	if double.cmp(tdpur041.oqua, 0.0, 0.00001) = 1 then	|#228027.so
||		return(convert.number(	bcckon.receipt.number,
||					tdpur045.reno))
||	else
||		return(bcswh008.shpm)		
||	endif							|#228027.eo
|	if tdpur041.oqua >= 0 then				|#228027.sn
|		return(convert.number(	bcckon.receipt.number,
|					tdpur045.reno))
|	else
|		ret = get.shipment.number.and.line(		
|			tckoor.act.pur,
|			tdpur045.orno,
|			tdpur045.pono,
|			get.order.line.sequence(tdpur045.srnb),
|			0,		|* Outbound Sequence	|#243681.n
|			0,
|			error.message)	|* ref
|		return(bcswh008.shpm)				
|	endif							|#228027.en								
|
|}								|#221043.en
								|#1422401.eo
								|#1422401.sn
function string get.receipt.number()				
{
	domain	bcorno		receipt.number
		long		ret			
	domain	tcmcs.s200m	error.message	
	
	receipt.number = ""
	
	if tdpur045.date = 0 then
		return(	receipt.number )
	endif

	if g.use.mapping.table then				|#FP10.sn
		return(bcswh010.rcno)
	else							|#fp10.en
		ret = get.receipt.rcno(		
				tckoor.act.pur,
				tdpur045.orno,
				tdpur045.pono,
				get.order.line.sequence(tdpur045.srnb),
				0,		|* Outbound Sequence	
				0,
				receipt.number, 
				error.message)	|* ref
	endif							|#fp10.n
			
	return(receipt.number)
}								
								|#1422401.en

								|#228053.sn
function domain tccwar get.warehouse()
{
	static	domain	tccprj		s.project
	static	domain	tccwar		s.warehouse
	static	domain	tccwar		s.new.warehouse fixed

	|* Other item types are not received in warehousing, so the
	|* warehouse must be empty. In BaanIV the warehouse can be
	|* filled.
	if item.type <> tckitm.purchase and
	   item.type <> tckitm.manufacture then
		return("")
	endif

	|* In BaanIV the warehouse can be filled, even when the
	|* ordered quantity is zero. In Baan 6 this is not possible.
	if tdpur041.oqua = 0 then
		return("")
	endif

	if s.project = tdpur045.cprj and
	   s.warehouse = tdpur045.cwar then
		return(s.new.warehouse)
	else
		s.project = tdpur045.cprj
		s.warehouse = tdpur045.cwar
		s.new.warehouse = ""
	endif

	|* For order lines linked to a TP project, the warehouse

	|* field is always filled in BaanIV. In Baan 6 the warehouse
	|* field can be empty or can be filled with a project
	|* warehouse. During the migration the warehouse field is
	|* always made empty for TP project related order lines.
	if not isspace(tdpur045.cprj) and
	   project.is.tp.project(tdpur045.cprj) then
		return(s.new.warehouse)
	endif

	s.new.warehouse = tdpur045.cwar

	return(s.new.warehouse)
}
								|#228053.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acp
:TFLNO:250
:FUNC:START
function handle.matched.invoice()								|#fp10.sn
{
		long			element
		static	domain 	tcorno		prev.orno
		static	domain	tcpono		prev.pono
		static	domain	tcsrnb		prev.srnb
		static	domain	tcsrnb		prev.srni	|#1693462.n


	if hold.orno  <>  tdpur046.orno then   
		read.purchase.order.and.order.type.data (   
			tdpur046.orno,   
			logistic.company)   
		var.tfacp250.orno  =  convert.number(   
			bcckon.act.pur,   
			tdpur046.orno)  
		hold.orno = tdpur046.orno
	endif   
	var.tfacp250.ifbp  =  get.business.partner.code(   
		tdpur046.suno,   
		bcstc.orgn.suno,   
		error.message)   
	get.inventory.unit(   	  
		logistic.company,   
		tdpur041.opol,   
		tdpur041.item,   
		tdpur041.cprj,   
		inventory.unit)   	  
	convert.quantity.from.order.unit.to.inventory.unit (   
		tdpur046.qana,   
		inventory.unit,    
		tdpur041.cvqp,   
		"",    
		"",    
		"tcqrd1",    
		var.converted.iqan)   
	
	var.tfacp250.iamt = tdpur046.amtb
	var.tfacp250.iqan = var.converted.iqan
	var.tfacp251.rcno  =  get.receipt.number()   
	var.tfacp251.rseq  =  get.receipt.line()   	   
	var.tfacp250.sqnb  =  get.order.line.sequence(  
					tdpur045.srnb)    
	if g.use.mapping.table then
		if logistic.company <> current.company then	|#1645612.sn
			compnr.check(logistic.company)			
		endif						|#1645612.en
		bcint.dll0000.divide.matched.amount.according.to.received.qty(	
				tdpur045.orno,
				tdpur045.pono,
				tdpur045.srnb,
				bcswh010.srni,
				inventory.unit,
				tdpur040.ccur,
				tcmcs002.crnd, 
				tdpur046.amtb,
				var.converted.iqan,
				var.tfacp251.iamt,
				var.tfacp251.iqan)
		if logistic.company <> current.company then	|#1645612.sn
			compnr.check(current.company)			
		endif						|#1645612.en
		for element = 1 to 3
 	  	 	if calculate.amount.in.home.currency(  
	 	  	 		 var.tfacp251.iamt,  
					 var.tfacp250.ccur,	
	 	  		 	 var.tfacp250.rate(element),  
					 var.tfacp250.ratf(element),	
	 	  		 	 element,  
	 	  		 	 var.tfacp251.amth(element),  
	 	  		 	 error.message) <>  0 then  
 	  		 	 var.tfacp251.amth(element) =  0.0  
 	  		 endif  			
		endfor
	else
		|    *  Only 1 receipt per order line.
		var.tfacp251.iamt = tdpur046.amtb
		var.tfacp251.iqan = var.converted.iqan
		for element = 1 to 3
			var.tfacp251.amth(element) = var.tfacp250.amth(element)
		endfor
	endif			
   
	if prev.orno <> tdpur046.orno or 
	   prev.pono <> tdpur046.pono or
	   prev.srnb <> tdpur046.srnb or
	   prev.srni <> tdpur046.srni then 			|#1693462.n
		dump 1 
	endif
	prev.orno = tdpur046.orno
	prev.pono = tdpur046.pono
	prev.srnb = tdpur046.srnb 
	prev.srni = tdpur046.srni 				|#1693462.n
	dump 2   
	dump 3   
}

function read.purchase.order.and.order.type.data(
		domain	tcorno		i.order,
		domain	tcncmp		i.logistic.company)
{
 	 select  tdpur040.cotp, tdpur040.suno, tdpur040.ccor,
 	 	 tdpur040.orno, tdpur040.ccur, tdpur040.odis,
		 tdpur040.odat, tdpur040.ratp, tdpur040.ratf,
		 tdpur040.ccon, 
		 tdpur040.cdec, tdpur040.cpay,			|#migration fp5.n
		 tcmcs042.reto, tcmcs042.coun,
		 tcmcs002.crnd
 	 from 	 tdpur040, tcmcs002, tcmcs042
 	 where 	 tdpur040._index1 = { :i.order}
	 and	 tdpur040._compnr = :i.logistic.company
 	 and 	 tcmcs002.ccur = tdpur040.ccur
	 and	 tcmcs042.cotp = tdpur040.cotp
 	 as set with 1 rows
 	 selectdo
 	 endselect
}



								|#fp10.en


function domain tcbool get.invoice.related.data()		|#217083.sn
{
|******************************************************************************
|* This functions reads additional information from the invoice. The rate
|* information is needed to calculate the home amount(s). If the invoice is not
|* present (because fully paid invoices where removed) we try to read the rate
|* information from the ledger account entry in tfgld106 (finalized
|* transactions). Because it is unknow if the object is run in a
|* 'multi-currency' we can not store the rate information of the invoice in
|* local variables (we don't if these variables must be declared as an array).
|******************************************************************************

		long			ret.long
		long			element

	|* Initialize
	var.tfacp250.ccur = ""
	ret.long = set.mem(var.tfacp250.rate, 0.0) +
		   set.mem(var.tfacp250.ratf, 1) + 
		   set.mem(var.tfacp250.amth, 0.0) 
	var.tfacp250.tapr = ""
	var.tfacp250.dapr = 0
	var.tfacp250.ratd = ""

	|* First try to read the open entries.
	select	tfacp200.ccur:var.tfacp250.ccur, tfacp200.rate,
		tfacp200.ratf, tfacp200.docd,
		tfacp200.tapr:var.tfacp250.tapr,
		tfacp200.dapr:var.tfacp250.dapr
	from	tfacp200
	where	tfacp200._index1 = {:tdpur046.ttyp,
				    :tdpur046.invn}
	and	tfacp200.line = 0
	and	tfacp200.tdoc = ""
	and	tfacp200._compnr = :current.company
	as set with 1 rows
	selectdo
		for element = 1 to 3
 	  	 	if calculate.amount.in.home.currency(  
 	  	 		 tdpur046.amtb,  
				 var.tfacp250.ccur,	
 	  		 	 tfacp200.rate,  
				 tfacp200.ratf,	
 	  		 	 element,  
 	  		 	 var.tfacp250.amth(element),  
 	  		 	 error.message) <>  0 then  
 	  		 	 var.tfacp250.amth(element) =  0.0  
 	  		 endif  			
 	  	 	var.tfacp250.rate(element) = fill.mc.field.double(  
 	  	 	 	tfacp200.rate,  
 	  	 	 	element,  
 	  	 	 	0.000001) 	  
 	  	 	var.tfacp250.ratf(element) =  fill.mc.field.long(  
 	  	 	 	tfacp200.ratf,  
 	  	 	 	element,  
 	  	 	 	1)
		endfor
		var.tfacp250.ratd =  utc(tfacp200.docd, 0)
		return(true) |* Information found
	selectempty
								|#227033.so
|		 if (tdpur045.matc =  tdpur.matc.complete) or  
|	 	    (tdpur045.date <>  0 and zero.receipt(  
								|#227033.eo
		 if (tdpur045.date <>  0 and zero.receipt(  	|#227033.n
				 	tdpur045.dqua,  
				 	tdpur045.quap,  
				 	tdpur045.quad,  
				 	tdpur041.oqua)) then  
			 | *  Zero receipt. In BaanIV there is only 1  
			 | *  receipt per orderline /  sequence number.  
			 | *  If thereis no receipt a back order with  
			 | *  a new sequence number is made.  
			return(false)
			|* Invoice is not found (fully paid invoices are removed)
			|* and the order line is totally matched, so migration
			|* of record can be skipped.	
		endif		
	endselect

	|* If the invoice is removed try to find the information within the
	|* finalized transactions. The approval document cannot be determined in
	|* that case. This seems not to be a problem because tfacp250.tapr/dapr
	|* are not used on target side.
	select	tfgld106.ccur:var.tfacp250.ccur, tfgld106.rate,
		tfgld106.fact, tfgld106.dcdt	
	from	tfgld106
	where	tfgld106._index1 = {:tdpur046.ttyp,
				    :tdpur046.invn}
	and	tfgld106.olin = 0
	and	tfgld106._compnr = :current.company
	as set with 1 rows
	selectdo
		for element = 1 to 3
 	  	 	if calculate.amount.in.home.currency(  
 	  	 		 tdpur046.amtb,  
				 var.tfacp250.ccur,	
 	  		 	 tfgld106.rate,  
				 tfgld106.fact,	
 	  		 	 element,  
 	  		 	 var.tfacp250.amth(element),  
 	  		 	 error.message) <>  0 then  
 	  		 	 var.tfacp250.amth(element) =  0.0  
 	  		 endif  			
 	  	 	var.tfacp250.rate(element) = fill.mc.field.double(  
 	  	 	 	tfgld106.rate,  
 	  	 	 	element,  
 	  	 	 	0.000001) 	  
 	  	 	var.tfacp250.ratf(element) =  fill.mc.field.long(  
 	  	 	 	tfgld106.fact,  
 	  	 	 	element,  
 	  	 	 	1)
		endfor
		var.tfacp250.ratd =  utc(tfgld106.dcdt, 0)
		return(true)
	endselect

	return(false)
}								|#217083.en

function domain tcbool zero.receipt(				|#217083.sn
		domain	tcqrd1		i.delivered.quantity,
		domain	tcqrd1		i.approved.quantity,
		domain	tcqrd1		i.rejected.quantity,
		domain	tcqrd1		i.order.line.quantity)
{
	 if double.cmp(i.order.line.quantity, 0.0, 0.00001) = -1 then
		 |* Return order
		 return(false)
	 endif			
	|**********************************************************************
	|* In BaanIV, in case of a return order, the deliverd quantity and the
	|* rejected quantity are always equal. So, a return order looks like an
	|* order of which all that is delivered is rejected (zero receipt). But
	|* a return order must not be handled as a normal order with zero
	|* receipt.
	|**********************************************************************
	
 	 if (double.cmp(  
 	 	 i.approved.quantity +  i.rejected.quantity,  
 	 	 i.delivered.quantity,  
 	 	 0.00001) =  0 and  
 	 | *  Aproved plus rejected equals delivered;  
 	 | *  all inspections are closed  
  	 	double.cmp(  
   	 		i.delivered.quantity,  
  	 		i.rejected.quantity,  
  	 		0.00001) =  0 and  
  	 | *  All delivered is rejected  
  	 	 double.cmp(i.order.line.quantity, 0.0, 0.00001) <>  0) then 
  	 | *  Not a cost item with quantity zero)  
	 	return(true)
	endif
	
	return(false)
}								|#217083.en

								|#fp10.so
|function read.purchase.order.and.order.type.data (
|		domain tcorno 	i.order,
|		domain	tcncmp	i.logistic.company)
|{
| 	 select  	 tcmcs042.reto
| 	 from 	 tdpur040, tcmcs042
| 	 where 	 tdpur040._index1 = { :i.order}
|	 and	 tdpur040._compnr = :i.logistic.company
|	 and	 tcmcs042.cotp = tdpur040.cotp
| 	 as set with 1 rows
| 	 selectdo
| 	 endselect
|}
								|#fp10.eo

								|#1422401.so
|function domain tcpono get.receipt.line()			|#149975.sn
|{
|			long		ret			|#215078.sn
|		domain	tcmcs.s200m	error.message		|#215078.en
|		domain	tcpono		receipt.line
|								|#239860.so
||		domain	tcrcno		max.receipt.number
||	static	domain	tcorno		s.order
||	static	domain	tcpono		s.position
||	static	domain	tcpono		s.sequence
||	static	domain	tcpono		s.array.receipt.line(1) based
||	static		long		s.array.length
|								|#239860.eo
|								|#247628.sn
|		domain	tcorno		order.number
|		domain	tcpono		order.position
|		domain	tcpono		order.sequence
|								|#247628.en
|			long		counter			|#239860.n
|
|	if tdpur045.date = 0 then
|		return(0)
|	endif
|
|	if tdpur045.reno = 0 then
|		return(0)
|	endif
|
|	if tdpur041.oqua < 0 then				|#215078.sn
|		if tdpur041.btsp = tcbtsp.bookto.comp then	|#228027.sn
|			|* For return of components the receipt sequence
|			|* (shipment line) is 0 on the purchase order line.
|			return(0)
|		endif						|#228027.en
|		ret = get.shipment.number.and.line(
|			tckoor.act.pur,
|			tdpur045.orno,
|			tdpur045.pono,
|			get.order.line.sequence(tdpur045.srnb),
|			0,		|* Outbound Sequence	|#243681.n
|			0,
|			error.message)	|* ref
|		return(bcswh008.shln)
|	endif							|#215078.en
|
|								|#239860.so
||	if s.array.length = 0 then
||		max.receipt.number =
||			lpow(10, icom0015.domain.length("tcrcno")) - 1
||		s.array.length = max.receipt.number
||		alloc.mem(s.array.receipt.line, s.array.length)
||	endif
||
||	if s.order = tdpur045.orno and
||	   s.position = tdpur045.pono and
||	   s.sequence = tdpur045.srnb then
||		receipt.line = s.array.receipt.line(tdpur045.reno)
||	else
||		|* Default step size is 10 (same as in WHINH and TDPUR).
||		receipt.line = s.array.receipt.line(tdpur045.reno) + 10
||		s.array.receipt.line(tdpur045.reno) = receipt.line
||		s.order = tdpur045.orno
||		s.position = tdpur045.pono
||		s.sequence = tdpur045.srnb
||	endif
|								|#239860.eo
|								|#239860.sn
|	counter = 0
|	receipt.line = 0
|								|#247628.so
||	 select  tdpur045.pono
|| 	 from 	 tdpur045
|| 	 where 	 tdpur045._index6 = { :tdpur045.reno,:tdpur045.orno}
|| 	 selectdo
|| 	 	counter = counter + 1
|| 	 	if tdpur045.pono = tdpur046.pono then
|| 	 		receipt.line = counter * 10
|| 	 		break
|| 	 	endif
|| 	 endselect
||								|#239860.en
|								|#247628.eo
|								|#1011516.so
||								|#247628.sn
||	 select  tdpur045.orno:order.number,
||	 	tdpur045.pono:order.position,
||	 	tdpur045.srnb:order.sequence
|| 	 from 	 tdpur045
|| 	 where 	 tdpur045._index6 = { :tdpur045.reno}
|| 	 order by tdpur045._index1
|| 	 selectdo
|| 	 	counter = counter + 1
|| 	 	if order.number = tdpur046.orno and
|| 	 	   order.position = tdpur046.pono then
|| 	 		receipt.line = counter * 10
|| 	 		break
|| 	 	endif
|| 	 endselect
||								|#247628.en
|								|#1011516.eo
|								|#1011516.sn
|	select	count(tdpur045.orno):counter
|	from	tdpur045
|	where	tdpur045._index6 = { :tdpur045.reno }
|	and	tdpur045._index1 <= { :tdpur046.orno, :tdpur046.pono}
|	and	tdpur045.date <> 0 				|#fp9.n
|	selectdo
|	endselect
|	receipt.line = counter * 10
|								|#1011516.en
|	return(receipt.line)
|}								|#149975.en
								|#1422401.eo

function domain tcpono get.order.line.sequence(			|#215078.sn
		domain	tcsrnb		i.sequence)
{
	if i.sequence = 0 then
		return(1)
	else
		return(i.sequence)
	endif
}								|#215078.en

function void get.inventory.unit(					|#208878.sn
		domain	tcncmp		i.logistic.company,
		domain	tcittb		i.item.category,
		domain	tcitem		i.item,
		domain	tccprj		i.project,
	ref	domain	tccuni		o.inventory.unit)
{
	|* Initialize
	o.inventory.unit = ""
	
	if i.item.category = tcittb.standard then
		get.inventory.unit.from.items(
			i.logistic.company,
			i.item,
			o.inventory.unit)
	else
		get.inventory.unit.from.customized.items(
			i.logistic.company,
			i.project,
			i.item,
			o.inventory.unit)
	endif	
}	
		
function void get.inventory.unit.from.items(
		domain	tcncmp		i.logistic.company,
		domain	tcitem		i.item,
	ref	domain	tccuni		o.inventory.unit)
{
	|* Initialize
	o.inventory.unit = ""
	
	select	tiitm001.cuni:o.inventory.unit
	from	tiitm001
	where	tiitm001._index1 = {:i.item}
	and	tiitm001._compnr = :i.logistic.company
	as set with 1 rows
	selectdo
	endselect
}	

function void get.inventory.unit.from.customized.items(
		domain	tcncmp		i.logistic.company,
		domain	tccprj		i.project,
		domain	tcitem		i.item,
	ref	domain	tccuni		o.inventory.unit)
{
	|* Initialize
	o.inventory.unit = ""
	
	select	tipcs021.cuni:o.inventory.unit
	from	tipcs021
	where	tipcs021._index1 = {:i.project, :i.item}
	and	tipcs021._compnr = :i.logistic.company
	as set with 1 rows
	selectdo
	endselect
}								|#208878.en


								|#1422401.so
|								|#249604.sn
|function string get.receipt.number()			
|{
|			long		ret			
|	static	domain	tcorno		s.order
|	static	domain	tcpono		s.position
|	static	domain	tcpono		s.sequence
|	static	domain	tcrcno		s.last.receipt.number
|		domain	tcmcs.s200m	error.message		
|		
|	if tdpur045.date = 0 then
|		return("")
|	endif
|
|	if tdpur045.reno = 0 then			
|								|#10110971.so
||		if s.order <> tdpur045.orno or
||		   s.position <> tdpur045.pono or
||		   s.sequence <> tdpur045.srnb then
||			s.last.receipt.number = s.last.receipt.number + 1
||			s.order = tdpur045.orno
||			s.position = tdpur045.pono
||			s.sequence = tdpur045.srnb
||		endif
||		return(	shiftl$(strip$(bcswh000.conv)) &
||			edit$(s.last.receipt.number, "999999"))
|								|#10110971.eo
|								|#10110971.sn
|		select  bcstf014.rcno
|		from 	bcstf014
|		where 	bcstf014._index1 = { 	 
|					:tdpur045.orno,
|		 	 	 	:tdpur045.pono,
|		 	 	 	:tdpur045.srnb}  
|		as set with 1 rows
|		selectdo
|			return ( bcstf014.rcno )
|		endselect
|								|#10110971.en
|	endif					
|								
|	if tdpur041.oqua >= 0 then				
|		return(convert.number(	bcckon.receipt.number,
|					tdpur045.reno))
|	else
|		ret = get.shipment.number.and.line(		
|			tckoor.act.pur,
|			tdpur045.orno,
|			tdpur045.pono,
|			get.order.line.sequence(tdpur045.srnb),
|			0,		|* Outbound Sequence	
|			0,
|			error.message)	|* ref
|		return(bcswh008.shpm)				
|	endif
|
|}								
|								|#249604.en
								|#1422401.eo
								
								|#1422401.sn
function string get.receipt.number()				
{
	domain	bcorno		receipt.number
		long		ret			
	domain	tcmcs.s200m	error.message	
	
	receipt.number = ""
	
	if tdpur045.date = 0 then
		return(	receipt.number )
	endif

	if g.use.mapping.table then				|#FP10.sn
		return(bcswh010.rcno)
	else							|#FP10.en
		ret = get.receipt.rcno(		
				tckoor.act.pur,
				tdpur045.orno,
				tdpur045.pono,
				get.order.line.sequence(tdpur045.srnb),
				0,		|* Outbound Sequence	
				0,
				receipt.number, 
				error.message)	|* ref
				
		return(receipt.number)
	endif							|#fp10.n
}


function domain tcpono get.receipt.line()		
{
		domain	tcpono		receipt.line
			long		ret			
		domain	tcmcs.s200m	error.message		
		
	receipt.line = 0 
	if tdpur045.date = 0 then
		return(receipt.line)
	endif
	if tdpur045.reno = 0 then
		return(receipt.line)
	endif
	
	if g.use.mapping.table then				|#FP10.sn
		return(bcswh010.rcln)
	else							|#FP10.en
		ret = get.receipt.rseq(
			tckoor.act.pur,
			tdpur045.orno,
			tdpur045.pono,
			get.order.line.sequence(tdpur045.srnb),
			0,	
			0,
			receipt.line,
			error.message)	|* ref
		
		return(receipt.line)
	endif							|#FP10.n
}	
								|#1422401.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acr
:TFLNO:005
:FUNC:START
function string get.parent.or.dependent(domain tfacr.tran i.tran)
{
	if tfacr005.tran = tfacr.tran.control or
	tfacr005.tran = tfacr.tran.advance or
	tfacr005.tran = tfacr.tran.unall.receipt or
	tfacr005.tran = tfacr.tran.anticipated or
	tfacr005.tran = tfacr.tran.real.curr.prof or
	tfacr005.tran = tfacr.tran.real.curr.loss or
	tfacr005.tran = tfacr.tran.not.real.curr.p or
	tfacr005.tran = tfacr.tran.not.real.curr.l or
	tfacr005.tran = tfacr.tran.int.invoice then
		acct.type = "independent"
	else
		acct.type = "dependent"
	endif
	return(acct.type)
}

function fill.child.dimensions.in.target(domain tfacr.tran i.tran)	 
{

	itfgld0038.dim.rcd(ttfacr005)
	itfgld0038.store.rcd.fields(ttfacr005)

	on case i.tran
		case tfacr.tran.doubtful:
		case tfacr.tran.discount:
		case tfacr.tran.surcharge:
		case tfacr.tran.payment.diff:
		case tfacr.tran.deductions:

		get.parent.leac.and.dimensions (parent.leac, 
					           parent.dim,
					           tfacr.tran.control)
		break
		case tfacr.tran.net.advance:
		case tfacr.tran.gross.advance:
		get.parent.leac.and.dimensions(parent.leac, 
					          parent.dim,
						tfacr.tran.advance)		
		break	
		default:
		break		
	endcase
	get.dim.options(parent.leac, parent.dim.opt)
	fill.child.dimensions.based.on.BUA()
				
	itfgld0038.restore.rcd.fields(ttfacr005)

}

function get.parent.leac.and.dimensions (ref domain tfgld.leac o.parent.leac, ref domain tfgld.dimx o.parent.dim(), domain tfacr.tran  i.tran)	
{
	select	tfacr005.*
	from tfacr005
	where tfacr005._index1 = {:i.ficu,:i.tran}
	as set with 1 rows
	selectdo
		o.parent.leac = tfacr005.leac
		o.parent.dim(1,1 )= tfacr005.dim1
		o.parent.dim(1,2 )= tfacr005.dim2
		o.parent.dim(1,3) = tfacr005.dim3
		o.parent.dim(1,4) = tfacr005.dim4
		o.parent.dim(1,5) = tfacr005.dim5
	endselect
}
function get.dim.options(domain tfgld.leac i.leac, ref domain tfgld.dopt o.dim.option())  
{
	select	tfgld008.*
	from	tfgld008
	where	tfgld008._index1= {:i.leac}
	as set with 1 rows
	selectdo
		o.dim.option(1) = tfgld008.dim1
		o.dim.option(2) = tfgld008.dim2
		o.dim.option(3)= tfgld008.dim3
		o.dim.option(4) = tfgld008.dim4
		o.dim.option(5) = tfgld008.dim5

	endselect
}

function get.child.dimensions.from.source()
{
	child.dim(1,1) = tfacr005.dim1
	child.dim(1,2)= tfacr005.dim2
	child.dim(1,3) = tfacr005.dim3
	child.dim(1,4) = tfacr005.dim4
	child.dim(1,5) = tfacr005.dim5

}

function fill.child.dimensions.based.on.BUA()
{
	long i
	
	for i = 1 to 5
		if parent.dim.opt(i) = tfgld.dopt.mandatory then 
				var.tfacr005.dim(1,i) = parent.dim(1,i)
		else
			if parent.dim.opt(i) = tfgld.dopt.optional then
				if  child.dim.opt(i) = tfgld.dopt.no then
					var.tfacr005.dim(1,i) = parent.dim(1,i)
				else
					var.tfacr005.dim(1,i)= child.dim(1,i)
				endif
			else
				var.tfacr005.dim(1,i) = child.dim(1,i)
			endif
		endif
	endfor
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acr
:TFLNO:200
:FUNC:START
|#200-101927.sn

function  initialize.variables()
{
				|#200-102115.sn
	long i
			
	for i = 1 to 3
		
		var.tfacr200.amth(i) =0.0
		var.tfacr200.vath(i) = 0.0
		var.tfacr200.svah(i) = 0.0
		var.tfacr200.laph(i) = 0.0
		var.tfacr200.dc1h(i) = 0.0
		var.tfacr200.dc2h(i) = 0.0
		var.tfacr200.dc3h(i) = 0.0
		var.tfacr200.padh(i) = 0.0
		var.tfacr200.balh(i) = 0.0
		var.tfacr200.bahc(i) = 0.0
		var.tfacr200.baca(i) = 0.0
		var.tfacr200.cdam(i) = 0.0
		var.tfacr200.tore(i) = 0.0
	endfor	

				|#200-102115.en

				|#200-102115.so
|	var.tfacr200.amth =0.0
|	var.tfacr200.vath = 0.0
|	var.tfacr200.svah = 0.0
|	var.tfacr200.laph = 0.0
|	var.tfacr200.dc1h = 0.0
|	var.tfacr200.dc2h = 0.0
|	var.tfacr200.dc3h = 0.0
|	var.tfacr200.padh = 0.0
|	var.tfacr200.balh = 0.0
|	var.tfacr200.bahc = 0.0
|	var.tfacr200.baca = 0.0
|	var.tfacr200.cdam = 0.0
|	var.tfacr200.tore = 0.0
				|#200-102115.eo



	var.tfacr200.amnt = 0.0
	var.tfacr200.amti = 0.0
	var.tfacr200.vata = 0.0
	var.tfacr200.vati = 0.0
	var.tfacr200.svam =0.0
	var.tfacr200.lapa = 0.0
	var.tfacr200.lapi = 0.0
	var.tfacr200.dc1a = 0.0
	var.tfacr200.dc1i = 0.0
	var.tfacr200.dc2a = 0.0
	var.tfacr200.dc2i = 0.0
	var.tfacr200.dc3a = 0.0
	var.tfacr200.dc3i = 0.0
	var.tfacr200.pada = 0.0
	var.tfacr200.padi = 0.0
	var.tfacr200.balc = 0.0
	var.tfacr200.bala = 0.0
	var.tfacr200.baco = 0.0
	var.tfacr200.basi = 0.0

}

function fill.positive.amounts.invoice()	
{
								|#200-102115.sn
	long i

							
	for i = 1 to 3 
								|#1057355.so
|		var.tfacr200.amth(i) =fill.mc.field.double(tfacr200.amth,i)
|		var.tfacr200.vath(i) =fill.mc.field.double(tfacr200.vath,i)
|		var.tfacr200.svah(i) =fill.mc.field.double(tfacr200.svah,i)
|		var.tfacr200.laph(i) =fill.mc.field.double(tfacr200.laph,i)
|		var.tfacr200.dc1h(i) =fill.mc.field.double(tfacr200.dc1h,i)
|		var.tfacr200.dc2h(i) =fill.mc.field.double(tfacr200.dc2h,i)
|		var.tfacr200.dc3h(i) =fill.mc.field.double(tfacr200.dc3h,i)
|		var.tfacr200.padh(i) =fill.mc.field.double(tfacr200.padh,i)
|		var.tfacr200.balh(i) =fill.mc.field.double(tfacr200.balh,i)
|		var.tfacr200.bahc(i) =fill.mc.field.double(tfacr200.bahc,i)
|		var.tfacr200.baca(i) =fill.mc.field.double(tfacr200.baca,i)
|		var.tfacr200.cdam(i) =fill.mc.field.double(tfacr200.cdam,i)
|		var.tfacr200.tore(i) =fill.mc.field.double(tfacr200.tore,i)
								|#1057355.eo
								|#1057355.sn
		var.tfacr200.amth(i) =fill.mc.field.double(tfacr200.amth,i,0.0)
		var.tfacr200.vath(i) =fill.mc.field.double(tfacr200.vath,i,0.0)
		var.tfacr200.svah(i) =fill.mc.field.double(tfacr200.svah,i,0.0)
		var.tfacr200.laph(i) =fill.mc.field.double(tfacr200.laph,i,0.0)
		var.tfacr200.dc1h(i) =fill.mc.field.double(tfacr200.dc1h,i,0.0)
		var.tfacr200.dc2h(i) =fill.mc.field.double(tfacr200.dc2h,i,0.0)
		var.tfacr200.dc3h(i) =fill.mc.field.double(tfacr200.dc3h,i,0.0)
		var.tfacr200.padh(i) =fill.mc.field.double(tfacr200.padh,i,0.0)
		var.tfacr200.balh(i) =fill.mc.field.double(tfacr200.balh,i,0.0)
		var.tfacr200.bahc(i) =fill.mc.field.double(tfacr200.bahc,i,0.0)
		var.tfacr200.baca(i) =fill.mc.field.double(tfacr200.baca,i,0.0)
		var.tfacr200.cdam(i) =fill.mc.field.double(tfacr200.cdam,i,0.0)
		var.tfacr200.tore(i) =fill.mc.field.double(tfacr200.tore,i,0.0)
								|#1057355.en

	endfor
							|#200-102115.en	

								|#200-102115.so
|		var.tfacr200.amth =tfacr200.amth
|		var.tfacr200.vath =tfacr200.vath
|		var.tfacr200.svah =tfacr200.svah
|		var.tfacr200.laph =tfacr200.laph
|		var.tfacr200.dc1h =tfacr200.dc1h
|		var.tfacr200.dc2h =tfacr200.dc2h
|		var.tfacr200.dc3h =tfacr200.dc3h
|		var.tfacr200.padh =tfacr200.padh
|		var.tfacr200.balh =tfacr200.balh
|		var.tfacr200.bahc =tfacr200.bahc
|		var.tfacr200.baca =tfacr200.baca
|		var.tfacr200.cdam =tfacr200.cdam
|		var.tfacr200.tore =tfacr200.tore
								|#200-102115.eo
		
	
	var.tfacr200.amnt = tfacr200.amnt
	var.tfacr200.amti = tfacr200.amti
	var.tfacr200.vata = tfacr200.vata
	var.tfacr200.vati = tfacr200.vati

	var.tfacr200.svam =tfacr200.svam

	var.tfacr200.lapa = tfacr200.lapa
	var.tfacr200.lapi = tfacr200.lapi

	var.tfacr200.dc1a = tfacr200.dc1a
	var.tfacr200.dc1i = tfacr200.dc1i

	var.tfacr200.dc2a = tfacr200.dc2a
	var.tfacr200.dc2i = tfacr200.dc2i

	var.tfacr200.dc3a = tfacr200.dc3a
	var.tfacr200.dc3i = tfacr200.dc3i

	var.tfacr200.pada = tfacr200.pada
	var.tfacr200.padi = tfacr200.padi

	var.tfacr200.balc = tfacr200.balc
	var.tfacr200.bala = tfacr200.bala

	var.tfacr200.baco = tfacr200.baco

	var.tfacr200.basi = tfacr200.basi

}

|function fill.negative.amounts.credit.note()
|{
|								|#200-102115.sn
|	long i
|
|							
|	for i = 1 to 3 
|		var.tfacr200.amth(i) =fill.mc.field.double(-tfacr200.amth,i)
|		var.tfacr200.vath(i) =fill.mc.field.double(-tfacr200.vath,i)
|		var.tfacr200.svah(i) =fill.mc.field.double(-tfacr200.svah,i)
|		var.tfacr200.laph(i) =fill.mc.field.double(-tfacr200.laph,i)
|		var.tfacr200.dc1h(i) =fill.mc.field.double(-tfacr200.dc1h,i)
|		var.tfacr200.dc2h(i) =fill.mc.field.double(-tfacr200.dc2h,i)
|		var.tfacr200.dc3h(i) =fill.mc.field.double(-tfacr200.dc3h,i)
|		var.tfacr200.padh(i) =fill.mc.field.double(-tfacr200.padh,i)
|		var.tfacr200.balh(i) =fill.mc.field.double(-tfacr200.balh,i)
|		var.tfacr200.bahc(i) =fill.mc.field.double(-tfacr200.bahc,i)
|		var.tfacr200.baca(i) =fill.mc.field.double(-tfacr200.baca,i)
|		var.tfacr200.cdam(i) =fill.mc.field.double(-tfacr200.cdam,i)
|		var.tfacr200.tore(i) =fill.mc.field.double(-tfacr200.tore,i)
|
|	endfor
|								|#200-102115.en	
|
|
|								|#200-102115.so
||		var.tfacr200.amth =-tfacr200.amth
||		var.tfacr200.vath =-tfacr200.vath
||		var.tfacr200.svah =-tfacr200.svah
||		var.tfacr200.laph =-tfacr200.laph
||		var.tfacr200.dc1h =-tfacr200.dc1h
||		var.tfacr200.dc2h =-tfacr200.dc2h
||		var.tfacr200.dc3h =-tfacr200.dc3h
||		var.tfacr200.padh =-tfacr200.padh
||		var.tfacr200.balh =-tfacr200.balh
||		var.tfacr200.bahc =-tfacr200.bahc
||		var.tfacr200.baca =-tfacr200.baca
||		var.tfacr200.cdam =-tfacr200.cdam
||		var.tfacr200.tore =-tfacr200.tore
|								|#200-102115.eo
|
|
|	var.tfacr200.amnt = -tfacr200.amnt
|	var.tfacr200.amti = -tfacr200.amti
|	var.tfacr200.vata = -tfacr200.vata
|	var.tfacr200.vati = -tfacr200.vati
|	var.tfacr200.svam = -tfacr200.svam
|	
|	var.tfacr200.lapa = -tfacr200.lapa
|	var.tfacr200.lapi = -tfacr200.lapi
|	var.tfacr200.dc1a = -tfacr200.dc1a
|	var.tfacr200.dc1i = -tfacr200.dc1i
|	var.tfacr200.dc2a = -tfacr200.dc2a
|	var.tfacr200.dc2i = -tfacr200.dc2i
|	var.tfacr200.dc3a = -tfacr200.dc3a
|	var.tfacr200.dc3i = -tfacr200.dc3i
|	var.tfacr200.pada = -tfacr200.pada
|	var.tfacr200.padi = -tfacr200.padi
|	var.tfacr200.balc = -tfacr200.balc
|	var.tfacr200.bala = -tfacr200.bala
|	var.tfacr200.baco = -tfacr200.baco
|	var.tfacr200.basi = -tfacr200.basi
|
|
|}
							|#200-101927.en
									|#FP3.sn
function fill.tfacr200.pysh()
{
		on case tfacr200.trec
		case tfacr.trec.invoice:
			select	tfacr201.ttyp
			from	tfacr201
			where	tfacr201._index1 = {:tfacr200.ttyp, :tfacr200.ninv}
			as set with 1 rows
			selectdo	
			var.tfacr200.pysh = target(tcyesno.yes)
			selectempty					|#218981.sn
			var.tfacr200.pysh = target(tcyesno.no)		|#218981.en
			endselect
			break
		default:
			var.tfacr200.pysh = target(tcyesno.no)
		endcase
}
									|#FP3.en

function fill.tfacr200.leac()						|#FP3.sn
{
	select	tfgld106.leac,
		tfgld106.dim1,				|#1056135.sn
		tfgld106.dim2,	
		tfgld106.dim3,	
		tfgld106.dim4,	
		tfgld106.dim5				|#1056135.en
	from	tfgld106
								|#221097.so
|	where	tfgld106._index1 = {:tfacr200.ttyp, :tfacr200.ninv, :tfacr200.line}
								|#221097.eo
								|#221097.sn
	where	tfgld106._index1 = {:tfacr200.ttyp, :tfacr200.ninv}
								|#221097.en

	and 	tfgld106.cont = tcyesno.yes
	and 	(tfgld106.catg in
			   (tfgld.catg.sales.inv,
			    tfgld.catg.sales.cred) or
			  (tfgld106.catg = tfgld.catg.cash and
			   tfgld106.tcsh = tfcmg.tcsh.ssgn and
			   tfgld106.tran in
			   				|#221097.so
|					(tfcmg.tran.unalloc.rec,
|					 tfcmg.tran.advance.rec)))
				 			|#221097.eo
				 			|#221097.sn
				 (tfcmg.tran.unalloc.rec,
				 tfcmg.tran.advance.rec))
			and 	tfgld106.olin = :tfacr200.line)	
							|#221097.en
	as set with 1 rows
	selectdo
		var.tfacr200.leac = tfgld106.leac		
		var.dimension1 = tfgld106.dim1		|#1056135.sn
		var.dimension2 = tfgld106.dim2		
		var.dimension3 = tfgld106.dim3		
		var.dimension4 = tfgld106.dim4		
		var.dimension5 = tfgld106.dim5		|#1056135.sn
	endselect
	if (tfacr200.trec = tfacr.trec.invoice or
		    tfacr200.trec = tfacr.trec.credit) then
		itfgld0038.dim.rcd(ttfacr200)
		itfgld0038.store.rcd.fields(ttfacr200)
		select	tfacr200.ttyp, tfacr200.ninv,
			tfacr200.tdoc, tfacr200.docn
		from	tfacr200
		where	tfacr200._index1 = {:tfacr200.ttyp,
			 	            :tfacr200.ninv}
		and	tfacr200.trec = tfacr.trec.correction
		and	tfacr200.doub = tcyesno.no
		and 	not exists (select a_tfacr200.ttyp
				   from   tfacr200  a_tfacr200
				   where  a_tfacr200._index1 =
				   	  {tfacr200.ttyp,
				   	   tfacr200.ninv, 0,
				   	   tfacr200.tdoc,
				   	   tfacr200.docn}
				   and	   a_tfacr200.doub = tcyesno.yes)
		as set with 1 rows
		selectdo
				select	tfgld106.leac, tfgld106.olin,
					tfgld106.dim1,	|#1056135.sn
					tfgld106.dim2,	
					tfgld106.dim3,	
					tfgld106.dim4,	
					tfgld106.dim5	|#1056135.en
				from	tfgld106
				where	tfgld106._index1 =
						{:tfacr200.tdoc,
						 :tfacr200.docn}
				and	tfgld106.cont = tcyesno.yes
				and 	tfgld106.catg =
						tfgld.catg.sales.corr
				order by tfgld106.olin desc
				as set with 1 rows
				selectdo
					var.tfacr200.leac = tfgld106.leac	
					var.dimension1 = tfgld106.dim1		|#1056135.sn
					var.dimension2 = tfgld106.dim2		
					var.dimension3 = tfgld106.dim3		
					var.dimension4 = tfgld106.dim4		
					var.dimension5 = tfgld106.dim5		|#1056135.sn
				endselect
		endselect
		itfgld0038.restore.rcd.fields(ttfacr200)
	endif
				
}					|#FP3.en

								|#FP7.sn
								|#1025236.so
|function get.receipt.info(
|	ref domain	tcncmp		o.o.tfacr200.rcom,		
|	ref domain	tfgld.ttyp	o.o.tfacr200.rect,
|	ref domain	tfgld.docn	o.o.tfacr200.recd,
|	ref domain	tfgld.lino	o.o.tfacr200.recl)
|
|{
|	o.o.tfacr200.rcom = 0
|	o.o.tfacr200.rect = ""
|	o.o.tfacr200.recd = 0
|	o.o.tfacr200.recl = 0
|
|	select 	tfgld112.cono:o.o.tfacr200.rcom,
|		tfgld112.ttyp:o.o.tfacr200.rect,
|		tfgld112.docn:o.o.tfacr200.recd
|	from	tfgld112
|	where	tfgld112._index2 = {	:current.company,
|			    		:tfacr200.tdoc,
|			    		:tfacr200.docn}
|	as set with 1 rows
|	selectdo
|		o.o.tfacr200.recl = tfacr200.lino
|	selectempty
|		if not isspace(tfacr200.tdoc) then
|			o.o.tfacr200.rcom = current.company
|			o.o.tfacr200.rect = tfacr200.tdoc
|			o.o.tfacr200.recd = tfacr200.docn
|			o.o.tfacr200.recl = tfacr200.lino
|		endif
|	endselect
|}
								|#1025236.eo
								|#1025236.sn
function get.receipt.info(
	ref domain	tcncmp		o.tfacr200.rcom,		
	ref domain	tfgld.ttyp	o.tfacr200.rect,
	ref domain	tfgld.docn	o.tfacr200.recd,
	ref domain	tfgld.lino	o.tfacr200.recl)

{
	domain	tcncmp		l.icom
	domain	tfgld.ttyp	l.ttyp
	domain	tfgld.docn	l.docn
	domain	tfgld.ttyp	l.search.ttyp
	domain	tfgld.docn	l.search.ninv

	o.tfacr200.rcom = 0
	o.tfacr200.rect = ""
	o.tfacr200.recd = 0
	o.tfacr200.recl = 0

	if isspace(tfacr200.tdoc) then
		|* handle headers
		l.search.ttyp = ""
		l.search.ninv = 0			
		o.tfacr200.rcom = current.company
		if tfacr200.line > 0 then
			|* Header Document
			o.tfacr200.rect = tfacr200.ttyp
			o.tfacr200.recd = tfacr200.ninv
			o.tfacr200.recl = tfacr200.line	
		endif
		if tfacr200.line > 0 and
                   (tfacr200.trec = tfacr.trec.advance.ant or 
                   tfacr200.trec = tfacr.trec.unallocated.ant or 
		   tfacr200.trec = tfacr.trec.advance or
                   tfacr200.trec = tfacr.trec.unallocated ) then                        
                        l.search.ttyp = tfacr200.ttyp
			l.search.ninv = tfacr200.ninv
                endif
		
	else
		|*handle subdocuments		
		l.search.ttyp = tfacr200.tdoc
		l.search.ninv = tfacr200.docn
	endif
	
	|*handle possible intercompany situation
	if not isspace(l.search.ttyp) then		
		if invoice.is.finalized(
				      l.search.ttyp,
				      l.search.ninv) then
			select 	tfgld106.osrn
			from	tfgld106
			where	tfgld106._index1 = {:l.search.ttyp,
						:l.search.ninv}
			and	tfgld106.ocmp <> :current.company
			as set with 1 rows
			selectdo
				select 	tfgld112.cono:l.icom,
					tfgld112.ttyp:l.ttyp,
					tfgld112.docn:l.docn
				from	tfgld112
				where	tfgld112._index2 = {:current.company,
						    	:l.search.ttyp,
						    	:l.search.ninv}
				as set with 1 rows
				selectdo
					o.tfacr200.rcom = l.icom
					o.tfacr200.rect = l.ttyp
					o.tfacr200.recd = l.docn				
					o.tfacr200.recl = fill.recl.field(
								o.tfacr200.rcom,
								o.tfacr200.rect,
								o.tfacr200.recd)
				endselect
			selectempty
				|* no intercompany
				o.tfacr200.rcom = current.company
				o.tfacr200.rect = l.search.ttyp
				o.tfacr200.recd = l.search.ninv			
				o.tfacr200.recl = fill.recl.field(
								o.tfacr200.rcom,
								o.tfacr200.rect,
								o.tfacr200.recd)
			endselect 
		else
			select 	tfgld112.cono:l.icom,
				tfgld112.ttyp:l.ttyp,
				tfgld112.docn:l.docn
			from	tfgld112
			where	tfgld112._index2 = {:current.company,
					    	:l.search.ttyp,
					    	:l.search.ninv}
			as set with 1 rows
			selectdo
				o.tfacr200.rcom = l.icom
				o.tfacr200.rect = l.ttyp
				o.tfacr200.recd = l.docn				
				o.tfacr200.recl = fill.recl.field(
								o.tfacr200.rcom,
								o.tfacr200.rect,
								o.tfacr200.recd)
			selectempty
				|* no intercompany
				o.tfacr200.rcom = current.company
				o.tfacr200.rect = l.search.ttyp
				o.tfacr200.recd = l.search.ninv			
				o.tfacr200.recl = fill.recl.field(
								o.tfacr200.rcom,
								o.tfacr200.rect,
								o.tfacr200.recd)
			endselect
		endif
	endif
}
function domain	tfgld.lino fill.recl.field(
	 domain	tcncmp		i.tfacr200.rcom,		
	 domain	tfgld.ttyp	i.tfacr200.rect,
	 domain	tfgld.docn	i.tfacr200.recd)
{
	domain	tfgld.lino	line.number
	
	line.number = 0
	
	on case tfacr200.trec
	case tfacr.trec.assignment:
		line.number = get.line.mapped(tfacr200.lino)
		break
	case tfacr.trec.normal:
	case tfacr.trec.anticipated:
		if tfacr200.lino >= 10000 then
			line.number =
		   		tfcmg.dll8112.get.line.composed.anticipated(
					i.tfacr200.rcom,
					i.tfacr200.rect,
					i.tfacr200.recd,
					tfacr200.typa,
					tfacr200.doca)
		else
			line.number = tfacr200.lino
		endif
		break
	case tfacr.trec.advance.ant:
	case tfacr.trec.unallocated.ant:
	case tfacr.trec.advance:
	case tfacr.trec.unallocated:
		if tfacr200.line >= 10000 then
			line.number =
		   		tfcmg.dll8112.get.line.composed.anticipated(
					i.tfacr200.rcom,
					i.tfacr200.rect,
					i.tfacr200.recd,
					tfacr200.typa,
					tfacr200.doca)
		else
			line.number = tfacr200.line
		endif
		break
	default:
		line.number = tfacr200.lino
	endcase
	return(line.number)
}

function domain tfgld.lino get.line.mapped(
		domain	tfgld.lino	i.lino)
{
	domain	tfgld.lino	line.number

	|* Line number 10002 has to be mapped to 1 because the
	|* 10000 series belong to payment line 1. Line number
	|* 20001 has to be mapped to 2 because the 20000 series
	|* belong to payment line 2.
	line.number = i.lino \ 10000
	line.number = i.lino - line.number
	line.number = line.number / 10000

	return(line.number)
}

function domain tcbool invoice.is.finalized(
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document)
{
		domain	tfgld.trun	hold.run.number

	select	tfgld018.trun:hold.run.number
	from	tfgld018
	where	tfgld018._index1 = {:i.transaction.type,
				    :i.document}
	and	tfgld018.trun > 0
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function extern domain tfgld.lino tfcmg.dll8112.get.line.composed.anticipated(
			domain	tcncmp		i.finance.company,
			domain	tfgld.ttyp	i.transaction.type,
			domain	tfgld.docn	i.document.number,
			domain	tfgld.ttyp	i.transaction.type.ant.payment,
			domain	tfgld.docn	i.document.number.ant.payment)
{
	DllUsage
	Expl.	: This function determines the payment line number. The 
		  transaction type, document number and sequence number remain
		  default.

	Pre	: -
	Post	: -
	Input	: i.finance.company	- Finance company
		  i.transaction.type	- The transaction type
		  i.document.number	- The document number
		  i.transaction.type.ant.payment
		  			- The transaction type of the
					  anticipated payment
		  i.document.number.ant.payment	
		  			- The document number of
					  the anticipated payment
		  i.business.partner	- The business partner

	Return	: line number
	EndDllUsage

	domain	tfgld.lino	line.number

	select	tfcmg112.clin:line.number
	from	tfcmg112
	where	tfcmg112._index2 = {:i.transaction.type, :i.document.number}
	and	tfcmg112.tdoc = :i.transaction.type.ant.payment
	and	tfcmg112.pdoc = :i.document.number.ant.payment
	and	tfcmg112._compnr = :i.finance.company
	as set with 1 rows
	selectdo
		break
	selectempty
		line.number = 0
	endselect
	
	return(line.number)
}								
								|#1025236.en


function get.step()
{
	on case etol(tfacr200.step)
	
		case 0:
			var.tfacr200.step = 20
			break
		case 1: 
			var.tfacr200.step = 1
			break
		case 2:
			var.tfacr200.step =5
			break
		case 3:
			var.tfacr200.step =6
			break
		case 4:
			var.tfacr200.step =10
			break
		case 5:
			var.tfacr200.step =3
			break
		case 6:
			var.tfacr200.step =7
			break
		case 7:
			var.tfacr200.step =16
			break
		case 8:
			var.tfacr200.step =17
			break
	        default:
	                var.tfacr200.step = 20
	                break
	endcase
}
								|#FP7.en

function void get.customer.fin.business.partner.group(		|#Migr. 10.6.sn
		domain	tcncmp		i.company,
		domain	tccuno		i.customer	fixed,
	ref	domain	tfacr.ficu	o.fisc.bp.group)
{
	|**************************************************************
	|* Initialize variables.
	|**************************************************************
	o.fisc.bp.group = ""

	select	tccom010.cfcg:o.fisc.bp.group
	from	tccom010
	where	tccom010._compnr = :i.company
	and	tccom010._index1 = {:i.customer}
	as set with 1 rows
	selectdo
	endselect
}								|#Migr. 10.6.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acr
:TFLNO:312
:FUNC:START
function void fill.default.agan()
{
|* This function fills the default aging analysis code from ACR parameters if present otherwise 
|* it fills the aging analysis code from the first record of tfacp004(Aging Analysis Data)	

	select	tfacr000.agan
	from	tfacr000
	as set with 1 rows
	selectdo
		if not isspace(tfacr000.agan) then
			var.tfacr312.agan = tfacr000.agan
		else
			select tfacp004.agan:var.tfacr312.agan
			from   tfacp004
			as set with 1 rows
			selectdo
				break
			endselect
		endif
	endselect  

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:cat
:TFLNO:221
:FUNC:START
|#203100.sn

function long get.highest.count.of.sern()
{
	itfgld0038.dim.rcd(tctcat215)
	itfgld0038.store.rcd.fields(tctcat215)

	select ctcat215.*
	from ctcat215
	where ctcat215._index1 = {:hold.ncmp,
				    :hold.dtyp,
				    :hold.dimx,
				    :hold.refu}
	selectdo
	selecteos
		sern.count = ctcat215.sern
	endselect

	itfgld0038.restore.rcd.fields(tctcat215)

	return(sern.count)
}			
				|#203100.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:cmg
:TFLNO:101
:FUNC:START
function domain tcbool standing.is.advance()
{

	select	tfcmg104.*
	from	tfcmg104 
	where   tfcmg104._index1 = {:tfcmg101.suno,:tfcmg101.ninv}
	and	tfcmg104.post = tfcmg.post.advance
	order by tfcmg104._index1
	as set with 1 rows
	selectdo 
		return(true)
	endselect

	return(false)
}

function string get.default.crdt()
{
		domain	tfgld.date	batch.created.on
	static	domain	tcncmp		s.company
	static	domain	tfgld.btno	s.payment.batch
	static	string			s.batch.created.on(22)

	if s.company = tfcmg101._compnr and
	   s.payment.batch = tfcmg101.btno then
	   	return(s.batch.created.on)
	endif

	select	tfcmg109.date:batch.created.on
	from	tfcmg109
	where	tfcmg109._index1 = {:tfcmg101.btno}
	and	tfcmg109._compnr = :tfcmg101._compnr
	as set with 1 rows
	selectdo
		s.batch.created.on = utc (batch.created.on,0)   
 	selectempty
 		s.batch.created.on = utc(date.num(),time.num())	
	endselect
	|**********************************************************************
	|* Store the static values
	|**********************************************************************
	s.company 	= tfcmg101._compnr
	s.payment.batch = tfcmg101.btno
	return(s.batch.created.on)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:fam
:TFLNO:000
:FUNC:START
function get.data.from.tffas203()					|#213701.sn
{
	select	tffas203.*
	from	tffas203
	where	tffas203._index1 = {:tffas001.fdpc}
	and	tffas203.dpca = tcyesno.yes
	order by tffas203._index1 desc
	as set with 1 rows
	selectdo
		var.lng.tffam000.year = tffas203.year	 	
		var.lng.tffam000.prod = tffas203.prod
	endselect
}								|#213701.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:fam
:TFLNO:100
:FUNC:START
|#200-100959.sn
function get.pur.inv.no.from.tffas100()
{

  	select tffas100._index2
	from  tffas100
	where tffas100._index2= {:tffas007.fasc}
	order by tffas100._index2
	selectdo
	selecteos
		g.invn = tffas100.ttyp & str$(tffas100.docn) & str$(tffas100.line)		

	selectempty
		g.invn = empty.string(1;20)
	endselect

}
							|#200-100959.en
							|#200-100985.sn
function read.transfered.asset()
{
	select 	tffas103.*
	from	tffas103
	where	tffas103._index1 = {:tffas007.fasc}
	as set with 1 rows
	selectdo
	endselect
}							|#200-100985.en
function domain tcbool check.record.existin.tffas101.tffas103()	|#227065.sn
{
	select	tffas101.fasc					|#1106841.sn
	from	tffas101
	where	tffas101.fasc = :tffas007.fasc
	as set with 1 rows
	selectdo
		return(true)
	endselect

	select	tffas103.ofas
	from	tffas103
	where	(tffas103.ofas = :tffas007.fasc
	and	 tffas103.ofas <> tffas103.fasc)
	as set with 1 rows
	selectdo
		return(true)
	endselect						|#1106841.en

|	select	tffas101.fasc, tffas103.ofas|			|#1106841.so
|	from	tffas101, tffas103
|	where	tffas101.fasc= :tffas007.fasc or (tffas103.ofas=:tffas007.fasc and tffas103.ofas<> tffas103.fasc)
|	selectdo
|		return(true)
|	endselect						|#1106841.eo
	return(false)
}								|#227065.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:fbs
:TFLNO:005
:FUNC:START
|function get.year.range.from.tffbs101(				|#1150172.so
|	ref	domain	tcmcs.long	o.var.first.year,
|	ref	domain	tcmcs.long	o.var.last.year)
|{
|	select	tffbs101.year:o.var.first.year
|	from	tffbs101
|	order by tffbs101.year asc
|	as set with 1 rows
|	selectdo
|	endselect
|
|	select	tffbs101.year:o.var.last.year
|	from	tffbs101
|	order by tffbs101.year desc
|	as set with 1 rows
|	selectdo
|	endselect
|}								|#1150172.eo

function get.year.range.for.budget(				|#1150172.sn
		domain	tffbs.budg	i.budget,
	ref	domain	tfgld.year	o.first.year,
	ref	domain	tfgld.year	o.last.year)
{
		domain	tfgld.year	first.year
		domain	tfgld.year	last.year

	o.first.year = 0
	o.last.year = 0

	select	min(tffbs101.year):o.first.year,
		max(tffbs101.year):o.last.year
	from	tffbs101
	where	tffbs101._index4 = {	:i.budget}
	as set with 1 rows
	selectdo
	endselect

	select	min(ctcat110.year):first.year,
		max(ctcat110.year):last.year
	from	ctcat110
	where	ctcat110.budg = :i.budget
	as set with 1 rows
	selectdo
		o.first.year = min(o.first.year, first.year)
		o.last.year = max(o.last.year, last.year)
	endselect
}								|#1150172.en

function get.budget.definitive.status(
 	 	 domain	tfgld.year	i.budget.year,
 	 	 domain	tffbs.budg	i.budget)
{
 	var.tffbs005.defi = tcyesno.no 	|* default value No (tcyesno.no)
	select	ctcat016.defi
 	from 	ctcat016
 	where 	ctcat016._index1 =  {	:i.budget.year,
 					:i.budget}
 	as set with 1 rows
 	selectdo
 		var.tffbs005.defi = ctcat016.defi
 	endselect
}

|function insert.linked.budgets(
|		domain	tffbs.budg	i.budget)
|{
|	select	atffbs101.budg
|	from	tffbs101 atffbs101
|	where	atffbs101.budg = :i.budget
|	and	atffbs101.year = :var.tffbs101.year
|	as set with 1 rows
|	selectdo
|	selectempty
|		var.tffbs005.budg = i.budget
|		dump 3   | *  tffbs005
|	endselect
|}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:gld
:TFLNO:008
:FUNC:START
function void read.dimensions.used(				|#210751.sn
	ref	domain	tcyesno		o.dimension1.used,
	ref	domain	tcyesno		o.dimension2.used,
	ref	domain	tcyesno		o.dimension3.used,
	ref	domain	tcyesno		o.dimension4.used,
	ref	domain	tcyesno		o.dimension5.used)
{
	|* Initialize
	o.dimension1.used = empty
	o.dimension2.used = empty
	o.dimension3.used = empty
	o.dimension4.used = empty
	o.dimension5.used = empty
	
	select	tfgld003.dim1:o.dimension1.used,
		tfgld003.dim2:o.dimension2.used, 
		tfgld003.dim3:o.dimension3.used, 
		tfgld003.dim4:o.dimension4.used, 
		tfgld003.dim5:o.dimension5.used 
	from	tfgld003
	as set with 1 rows
	selectdo
	endselect
}								|#210751.en
								|#223230.sn
function domain tcbool leac.in.ctcat110.and.primary.costs(
			domain	tfgld.leac	i.ledger.account)
{
	select	ctcat110.prse, ctcat110.leac
	from	ctcat110
	where	ctcat110.leac = :i.ledger.account
	and	ctcat110.prse = ctcat.prdc.primary
	selectdo
		return (true)
	endselect
	return (false)
}	
								|#223230.en
function void set.attributes.non.integration.account()
{
	var.tfgld008.alat = target(tfgld.atat.not.applicable)	|#223230.n
	|* Not Applicable
	
	if tfgld008.atyp =  tfgld.atyp.profitloss then 
|	 	 if tfgld008.subl >  0 then  			|#223230.so
|	 	 	 var.tfgld008.alat =  target(tfgld.atat.primary)
|			 |* Primary 
|	 	 else  
|	 	 	 if tfgld008.dbcr =  tfgld.dbcr.debit then  
|	 	 	 	 var.tfgld008.alat =
|				 	target(tfgld.atat.sec.debit) 
|				 	|* Secondary Debit
|	 	 	 else  
|	 	 	 	 var.tfgld008.alat = 
|				 	target(tfgld.atat.sec.credit) 
|				   	|* Secondary Credit
|	 	 	 endif  
|	 	 endif  
|	 else  
|	 	 var.tfgld008.alat =  target(tfgld.atat.not.applicable)
|		 	|* Not Applicable			|#223230.eo
								|#223230.sn
		if leac.in.ctcat110.and.primary.costs(tfgld008.leac) then
			var.tfgld008.alat = target(tfgld.atat.primary)
			|* Primary
		endif
		if tfgld008.dbcr = tfgld.dbcr.debit then
			select	ctcat400.tlea
			from	ctcat400
			where	ctcat400.tlea = :tfgld008.leac
			selectdo
				var.tfgld008.alat = target(tfgld.atat.sec.debit)
				|* Secondary Debit
			selectempty
				select	ctcat500.tlea
				from	ctcat500
				where	ctcat500.tlea = :tfgld008.leac
				selectdo
					var.tfgld008.alat = 
						target(tfgld.atat.sec.debit)
						|* Secondary Debit
				endselect
			endselect
		else
			select	ctcat400.slea
			from	ctcat400
			where	ctcat400.slea = :tfgld008.leac
			selectdo
				var.tfgld008.alat = 
					target(tfgld.atat.sec.credit)
				|* Secondary Credit
			selectempty
				select	ctcat500
				from	ctcat500
				where	ctcat500.slea = :tfgld008.leac
				selectdo
					var.tfgld008.alat = 
						target(tfgld.atat.sec.credit)
					|* Secondary Credit
				endselect
			endselect
		endif						|#223230.en
	 endif
  
|	 if tfgld008.iprj =  empty then  			|#1397945.so
|	  	 var.tfgld008.iprj =  target(tfgld.intr.not.applicable)	
|		 			|* Not Applicable 
|	 else  
|	 	if (etol (tfgld008.iprj) <  7) and (tfgld008.iprj <> empty) then
|		|* Transportation enums are expired	
|	 	 	 var.tfgld008.iprj =  etol(tfgld008.iprj)  
|	 	else  
|	 	 	 var.tfgld008.iprj =  target(tfgld.intr.not.applicable)	
|			 			|* Not Applicable
|	 	endif  
|	 endif  						|#1397945.eo
	 							|#1397945.sn
	on case	tfgld008.iprj
	case tfgld.intr.pcs:
		|#PCS Project
		var.tfgld008.iprj =  target(tfgld.intr.pcs)
	 	break
	case tfgld.intr.proj.costs:
		|#Project Costs
		var.tfgld008.iprj =  target(tfgld.intr.proj.costs)
	 	break
	case tfgld.intr.proj.sales:
		|# Project Sales
		var.tfgld008.iprj =  target(tfgld.intr.proj.sales)
	
	default:
		|# Transportation enums are expired -> mapping to not applicable
		|# ISM enums are not considered -> mapping to not applicable
	 	var.tfgld008.iprj =  target(tfgld.intr.not.applicable)
	 	break
	endcase	
	 							|#1397945.en 

								|#FP10.sn
	var.cvr.udim1 = ""	 					
	var.cvr.udim2 = ""	 					
	var.cvr.udim3 = ""	 					
	var.cvr.udim4 = ""	 					
	var.cvr.udim5 = ""	 					
								|#FP10.en
	if tfgld008.subl > 0 then
		set.value.all.dimensions(
			tfgld.dopt.no,
	 		var.tfgld008.dim1,
	 		var.tfgld008.dim2,
	 		var.tfgld008.dim3,
	 		var.tfgld008.dim4,
	 		var.tfgld008.dim5)
	else
		if account.is.interim.account(tfgld008.leac) or
		   account.is.control.account(tfgld008.leac) then
|			set.value.all.dimensions(		|#210751.so
|				tfgld.dopt.optional,
|	 			var.tfgld008.dim1,
|	 			var.tfgld008.dim2,
|	 			var.tfgld008.dim3,
|	 			var.tfgld008.dim4,
|				var.tfgld008.dim5)		|#210751.eo
			|* Add dimension ranges by ledger account (if needed)
			if dimension1.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim1 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					1,
					var.cvr.udim1)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim1 = tfgld.dopt.no 
			endif					|#210751.en
			if dimension2.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim2 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					2,
					var.cvr.udim2)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim2 = tfgld.dopt.no 
			endif					|#210751.en
			if dimension3.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim3 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					3,
					var.cvr.udim3)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim3 = tfgld.dopt.no 
			endif					|#210751.en
			if dimension4.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim4 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					4,
					var.cvr.udim4)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim4 = tfgld.dopt.no 
			endif					|#210751.en
			if dimension5.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim5 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					5,
					var.cvr.udim5)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim5 = tfgld.dopt.no 
			endif					|#210751.en
		else
			var.tfgld008.dim1 = tfgld008.dim1
			var.tfgld008.dim2 = tfgld008.dim2
			var.tfgld008.dim3 = tfgld008.dim3
			var.tfgld008.dim4 = tfgld008.dim4
			var.tfgld008.dim5 = tfgld008.dim5
		endif	
								|#FP10.sn
	      	dim.options(1)  =  var.tfgld008.dim1   
 	      	dim.options(2)  =  var.tfgld008.dim2   
 	      	dim.options(3)  =  var.tfgld008.dim3   
 	      	dim.options(4)  =  var.tfgld008.dim4   
 	      	dim.options(5)  =  var.tfgld008.dim5   
 	      	nof.elements  =  0   
 	      	set.mem(fdim.array, "")   
 	      	set.mem(tdim.array, "")   
 	      	convert.dimension.ranges.to.cvr.rule(   
			tfgld008.leac,   
			dim.options,   
			array.depth,   
			nof.elements,   
			fdim.array,   
			tdim.array)   
 	      	dmp.vcr.rule.elements.from.arrays(   
			tfgld008.leac,   
			dim.options,   
			var.cvr.udim1,   
			var.cvr.udim2,   
			var.cvr.udim3,   
			var.cvr.udim4,   
			var.cvr.udim5,   
			array.depth,   
			nof.elements,   
			fdim.array,   
			tdim.array)   
								|#FP10.en

	endif	
	 
	 if tfgld008.ifas =  empty then  
	  	 var.tfgld008.ifas =  target(tfgld.ifas.not.applicable)
		 			|* Not Applicable
	 else  
	  	 var.tfgld008.ifas =  etol(tfgld008.ifas)  
	 endif
	 
	 if tfgld008.bloc = empty then
		var.tfgld008.bloc = tfgld.bloc.free
	else
		var.tfgld008.bloc = tfgld008.bloc
	endif

	if isspace(tfgld008.uni1)
	and (var.tfgld008.ifas = target(tfgld.ifas.disposal) or
	       var.tfgld008.ifas = target(tfgld.ifas.investment)) then
		|* Unit1 is defaulted with the 'Base Inventory Unit' (bcstc000.cuni).
		var.tfgld008.uni1 = bcstc000.cuni
	else
		var.tfgld008.uni1 = tfgld008.uni1
	endif
}

function void set.common.attributes()
{
	 if (tfgld008.leac =  tfgld004.clac) or 
	    (tfgld008.leac =  tfgld004.clcl) then  
	 	 var.tfgld008.icur =  tcicur.not.required  
	 else  
	 	 var.tfgld008.icur =  tfgld008.icur  
	 endif
 
	 if tfgld008.dbcr =  empty then  
	 	 var.tfgld008.dbcr =  tfgld.dbcr.debit 

	 else
		 var.tfgld008.dbcr = tfgld008.dbcr 
	 endif
	 
	if tfgld008.atyp = empty then
|		var.tfgld008.atyp = tfgld.atyp.balance		|#fp10.o
		var.tfgld008.atyp = ltoe(target(tfgld.atyp.balance))|#fp10.n
	else
|		var.tfgld008.atyp = tfgld008.atyp		|#fp10.o
		var.tfgld008.atyp = convert.account.type(tfgld008.atyp)|#fp10.n
	endif

}

function domain tfgld.atyp get.account.type(domain tfgld.leac i.account)
{
	domain	tfgld.atyp	account.type

	select	tfgld008.atyp:account.type
	from	tfgld008
	where	tfgld008._index1 = {:i.account}
	as set with 1 rows
	selectdo
	endselect
	account.type = convert.account.type(account.type)	|#fp10.n

	return(account.type)
}

function void set.value.all.dimensions(
		domain	tfgld.dopt	i.dimension.value,
	ref	domain	tfgld.dopt	o.dimension.1,
	ref	domain	tfgld.dopt	o.dimension.2,
	ref	domain	tfgld.dopt	o.dimension.3,
	ref	domain	tfgld.dopt	o.dimension.4,
	ref	domain	tfgld.dopt	o.dimension.5)
{
	o.dimension.1 = i.dimension.value
	o.dimension.2 = i.dimension.value
	o.dimension.3 = i.dimension.value
	o.dimension.4 = i.dimension.value
	o.dimension.5 = i.dimension.value
}

function domain tcbool account.is.interim.account(
		domain	tfgld.leac	i.account)
{
		domain	tfgld.srno	dummy.srno

	select	tfgld004.srno:dummy.srno
	from	tfgld004
	where	tfgld004.srno = 1
	and	(tfgld004.fpor = :i.account or
		 tfgld004.fpnw = :i.account or
		 tfgld004.rpor = :i.account or
		 tfgld004.rpnw = :i.account )
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}	

function domain tcbool account.is.control.account(
		domain	tfgld.leac	i.account)
{
		domain	tfgld.leac	dummy.account

	select	tfacp005.leac:dummy.account
	from	tfacp005
	where	tfacp005.leac = :i.account
	selectdo
		return(true)
	endselect

	select	tfacr005.leac:dummy.account
	from	tfacr005
	where	tfacr005.leac = :i.account
	selectdo
		return(true)
	endselect

	return(false)
}

function void add.dimension.range.by.ledger.account(
		domain	tfgld.leac	i.account,
		domain	tfgld.srno	i.dimension,
	ref 	domain	tccvat		o.max.dim)		|#FP10.n
{
		domain	tfgld.leac	dummy.account

	select	tfgld009.leac:dummy.account
	from	tfgld009
	where	tfgld009._index1 = {:i.account, :i.dimension}
	as set with 1 rows
	selectdo
	selectempty
		var.tfgld009.leac = i.account
		var.tfgld009.dtyp = i.dimension
		var.tfgld009.fdim = ""
|		set.fmax(var.tfgld009.udim)			|#FP10.o
		o.max.dim = var.tfgld009.udim			|#FP10.n
		dump 5 |* New dimensions ranges (buss. unit)
	endselect
}

								|#FP10.sn
function convert.dimension.ranges.to.cvr.rule(
		domain	tfgld.leac	i.ledger.account,
	const	domain	tfgld.dopt	i.dim.options(),
	ref		long		io.array.depth,
	ref		long		io.nof.elements,
	ref	domain	tcmcs.str132	io.fdim.array(),
	ref	domain	tcmcs.str132	io.tdim.array())
{
		
	domain	tccvat	dimension.to
	
	dimension.to = ""

	select	tfgld009.*
	from	tfgld009
	where	tfgld009._index1 = {	:i.ledger.account}
	order by tfgld009._index1
	selectdo
		if tfgld009.udim = var.max.dimension(1;6) then
			dimension.to = var.tfgld009.udim
		else
			dimension.to = tfgld009.udim
		endif
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			tfgld009.dtyp,
			tfgld009.fdim,
			dimension.to,
			io.array.depth,		|* ref
			io.nof.elements,	|* ref
			io.fdim.array,		|* ref
			io.tdim.array)		|* ref
	endselect
	
}


function process.dimension.range.into.cvr(
		domain	tfgld.leac	i.ledger.account,
	const	domain	tfgld.dopt	i.dim.options(),
		domain	tfgld.dtyp	i.dim.type,
		domain	tccvat		i.dim.f,
		domain	tccvat		i.dim.t,
	ref		long		io.array.depth,
	ref		long		io.nof.elements,
	ref	domain	tcmcs.str132	io.fdim.array(),
	ref	domain	tcmcs.str132	io.tdim.array())
{
			long		i
			long		start.pos
		domain	tcbool		first.time
		domain	tcmcs.str132	fdim
		domain	tcmcs.str132	tdim
		domain	tccvat		prev.fdim
		domain	tccvat		prev.tdim
		domain	tccvat		current.fdim
		domain	tccvat		current.tdim
	static	domain	tfgld.leac	s.ledger.account
	static	domain	tfgld.dtyp	s.dim.type



	if i.ledger.account = s.ledger.account and
	   i.dim.type = s.dim.type then
		if i.dim.options(i.dim.type) = tfgld.dopt.no then
			return
		endif

		prev.fdim = ""
		prev.tdim = ""
		first.time = true

		|**************************************************************
		|* When the dimension type is the same as the previous dimenion
		|* type, then make a copy of the current set in tfgld052 and
		|* change only the fdim and tdim for this dimension.
		|* When this happens for the third time (or more), not a
		|* complete copy is required, but only the same set of records
		|* as the first time needs to be copied. This will avoid an
		|* exponential growth.
		|**************************************************************

		for i = 1 to io.nof.elements
			fdim = io.fdim.array(1, i)
			tdim = io.tdim.array(1, i)
			start.pos = 1 + ((i.dim.type - 1) * DIM.LENGTH)
			current.fdim = fdim(start.pos; DIM.LENGTH)
			current.tdim = tdim(start.pos; DIM.LENGTH)

			if not first.time and
			   (prev.fdim <> current.fdim or
			    prev.tdim <> current.tdim) then
				return
			endif
			first.time = false
			prev.fdim = current.fdim 
			prev.tdim = current.tdim 

			io.nof.elements = io.nof.elements + 1
			if io.nof.elements > io.array.depth then
				io.array.depth = io.array.depth + ALLOC.BLOCK
				alloc.mem(io.fdim.array, 132, io.array.depth)
				alloc.mem(io.tdim.array, 132, io.array.depth)
			endif
			fdim(start.pos; DIM.LENGTH) = i.dim.f
			tdim(start.pos; DIM.LENGTH) = i.dim.t
			io.fdim.array(1,io.nof.elements) = fdim
			io.tdim.array(1,io.nof.elements) = tdim
		endfor
		return
	endif

	s.dim.type = i.dim.type
	s.ledger.account = i.ledger.account

	|**********************************************************************
	|* If dimension type is not the same as the previous time, insert a
	|* new array element or update existing elements with the range of this
	|* dimension type.
	|**********************************************************************

	if io.nof.elements = 0 then
		io.nof.elements = io.nof.elements + 1
		if io.nof.elements > io.array.depth then
			io.array.depth = io.array.depth + ALLOC.BLOCK
			alloc.mem(io.fdim.array, 132, io.array.depth)
			alloc.mem(io.tdim.array, 132, io.array.depth)
		endif
		start.pos = 1 + ((i.dim.type - 1) * DIM.LENGTH)
		if i.dim.options(i.dim.type) <> tfgld.dopt.no then
			fdim = ""
			tdim = ""
			fdim(start.pos;DIM.LENGTH) = i.dim.f
			tdim(start.pos; DIM.LENGTH) = i.dim.t
			io.fdim.array(1,io.nof.elements) = fdim
			io.tdim.array(1,io.nof.elements) = tdim
		endif
	else
		for i = 1 to io.nof.elements
			if i.dim.options(i.dim.type) <> tfgld.dopt.no then
				fdim = io.fdim.array(1,i)
				tdim = io.tdim.array(1,i)
				start.pos = 1 + ((i.dim.type - 1) * DIM.LENGTH)
				fdim(start.pos;DIM.LENGTH) = i.dim.f
				tdim(start.pos; DIM.LENGTH) = i.dim.t
				io.fdim.array(1,i) = fdim
				io.tdim.array(1,i) = tdim
			endif
		endfor
	endif
}


function dmp.vcr.rule.elements.from.arrays(
		domain	tfgld.leac	i.ledger.account,
	const	domain	tfgld.dopt	i.dim.options(),
		domain	tccvat		i.var.cvr.udim1,
		domain	tccvat		i.var.cvr.udim2,
		domain	tccvat		i.var.cvr.udim3,
		domain	tccvat		i.var.cvr.udim4,
		domain	tccvat		i.var.cvr.udim5,
	ref		long		io.array.depth,
	ref		long		io.nof.elements,
	ref	domain	tcmcs.str132	io.fdim.array(),
	ref	domain	tcmcs.str132	io.tdim.array())

{
		long 		i,j,start.pos
	domain	tcmcs.str132	fdim
	domain	tcmcs.str132	tdim
	
	if not isspace(i.var.cvr.udim1) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			1,
			"",
			i.var.cvr.udim1,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if not isspace(i.var.cvr.udim2) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			2,
			"",
			i.var.cvr.udim2,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if not isspace(i.var.cvr.udim3) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			3,
			"",
			i.var.cvr.udim3,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if not isspace(i.var.cvr.udim4) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			4,
			"",
			i.var.cvr.udim4,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if not isspace(i.var.cvr.udim5) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			5,
			"",
			i.var.cvr.udim5,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if io.nof.elements = 0 then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			1,
			"",
			"",
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif
	
	var.tfgld052.leac = i.ledger.account
	
	for i = 1 to io.nof.elements 
		var.max.validation.line = var.max.validation.line + 1
		fdim = io.fdim.array(1,i)
		tdim = io.tdim.array(1,i)
		for j = 1 to MAX.DIMENSIONS
			start.pos = 1 + ((j - 1) * DIM.LENGTH)
			var.from.dim(1,j) = fdim(start.pos;DIM.LENGTH)
			var.to.dim(1,j) = tdim(start.pos;DIM.LENGTH)
		endfor
		dump 8 |cross validation rule element
	endfor
}




function dummy.dumps()
{
	dump 1  |    *  Other, non integration accounts   
	dump 2  |    *  Integration Accounts 
	dump 3  |    *  Currency Difference Contra account is added.
	dump 4	|* Exchange Gain and Loss Account is added
	dump 5 |* New Dimension Ranges
	dump 6  |    *  Existing Dimension Ranges
	dump 7 |* Cross validation rule
	dump 8 |*cross validation rule element

}
								|#FP10.en
								|#fp10.sn
function domain tfgld.atyp convert.account.type( domain tfgld.atyp old.account.type)
{
	domain	tfgld.atyp var.008.atyp

	on case old.account.type   
	case tfgld.atyp.balance:   
		var.008.atyp  =  ltoe(target(tfgld.atyp.balance))   
		break   
	case tfgld.atyp.profitloss:   
		var.008.atyp  =  ltoe(target(tfgld.atyp.profitloss))   
		break   
	case tfgld.atyp.intercompany:   
		var.008.atyp  =  ltoe(target(tfgld.atyp.intercompany))   
		break   
	case tfgld.atyp.text:   
		var.008.atyp  =  ltoe(target(tfgld.atyp.text))   
		break   
	default:
		var.008.atyp  =  ltoe(target(tfgld.atyp.balance))   
		break      	   	
	endcase   
      	   
      	return(var.008.atyp)
}
								|#fp10.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:gld
:TFLNO:018
:FUNC:START
function void determine.exchange.rate.type(
		domain	tcmcs.str3	i.var.str.pur.rtyp,
		domain	tcmcs.str3	i.var.str.sls.rtyp,
		domain	tcmcs.str3	i.var.str.eu.rtyp,
		domain	tcncmp		i.company,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
		domain	tfgld.year	i.year,
		domain	tfgld.btno	i.batch,
		domain	tcmcs.long	i.run.number,
	ref	domain	tcmcs.str3	o.exchange.rate.type)
{
		domain	tfgld.catg	transaction.categorie
		
	o.exchange.rate.type = ""
	
 	determine.transaction.categorie(
		i.transaction.type,
		transaction.categorie)
	
 	 on case transaction.categorie  
 	 	 case tfgld.catg.journals :  
 	 	 case tfgld.catg.recurr.journ :  
 	 	  	 o.exchange.rate.type =  i.var.str.eu.rtyp  
 	 	  	 break  
 	 	 case tfgld.catg.sales.inv :  
 	 	 case tfgld.catg.sales.cred :  
 	 	 case tfgld.catg.sales.corr :  
 	 	  	 o.exchange.rate.type =  i.var.str.sls.rtyp  
 	 	  	 break  
 	 	 case tfgld.catg.purchase.inv :  
 	 	 case tfgld.catg.purchase.cred :  
 	 	 case tfgld.catg.purchase.corr :  
 	 	  	 o.exchange.rate.type =  i.var.str.pur.rtyp  
 	 	  	 break  
 	 	 case tfgld.catg.opening.bal :  
		 	o.exchange.rate.type = i.var.str.eu.rtyp
			break
 	 	 case tfgld.catg.cash :  
		 	if i.run.number = 0 then
				non.finalized.transaction(
					i.var.str.pur.rtyp,
					i.var.str.sls.rtyp,
					i.var.str.eu.rtyp,
					i.company,
					i.year,
					i.batch,
					i.transaction.type,
					i.document,
					o.exchange.rate.type)
			else	
				finalized.transaction(
					i.var.str.pur.rtyp,
					i.var.str.sls.rtyp,
					i.var.str.eu.rtyp,
					i.transaction.type,
					i.document,
					o.exchange.rate.type)
			endif
			break
  	 endcase  
}

function void determine.transaction.categorie(
		domain	tfgld.ttyp	i.transaction.type,
	ref	domain	tfgld.catg	o.transaction.categorie)
{	
	static	domain	tfgld.ttyp	s.transaction.type	FIXED
	static	domain	tfgld.catg	s.transaction.categorie
	
	o.transaction.categorie = empty
	
	if i.transaction.type = s.transaction.type then
		o.transaction.categorie = s.transaction.categorie
		return
	endif
	
	select 	tfgld011.catg:o.transaction.categorie
 	from 	tfgld011  
 	where 	tfgld011._index1 =  {:i.transaction.type}  
 	as set with 1 rows
 	selectdo  
		s.transaction.type = i.transaction.type
		s.transaction.categorie = o.transaction.categorie
	endselect
}

function void non.finalized.transaction(
		domain	tcmcs.str3	i.var.str.pur.rtyp,
		domain	tcmcs.str3	i.var.str.sls.rtyp,
		domain	tcmcs.str3	i.var.str.eu.rtyp,
		domain	tcncmp		i.company,
		domain	tfgld.year	i.year,
		domain	tfgld.btno	i.batch,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
	ref	domain	tcmcs.str3	o.exchange.rate.type)
{
	domain	tfcmg.tran	type.of.transaction
	
 	select	tfgld102.tran:type.of.transaction
 	from 	tfgld102  
 	where 	tfgld102._index1 =  {:i.company, :i.year, :i.batch,  
 	 	 	 	     :i.transaction.type, :i.document}  
 	as set with 1 rows  
 	selectdo  
		convert.transaction.type.to.rate.type(
			i.var.str.pur.rtyp,
			i.var.str.sls.rtyp,
			i.var.str.eu.rtyp,
			type.of.transaction,
			o.exchange.rate.type)
	endselect
}

function void finalized.transaction(
		domain	tcmcs.str3	i.var.str.pur.rtyp,
		domain	tcmcs.str3	i.var.str.sls.rtyp,
		domain	tcmcs.str3	i.var.str.eu.rtyp,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
	ref	domain	tcmcs.str3	o.exchange.rate.type)
{
	domain	tfcmg.tran	type.of.transaction
	
 	select	tfgld106.tran:type.of.transaction
 	from 	tfgld106  
 	where 	tfgld106._index1 =  {:i.transaction.type, :i.document}  
 	as set with 1 rows  
 	selectdo  
		convert.transaction.type.to.rate.type(
			i.var.str.pur.rtyp,
			i.var.str.sls.rtyp,
			i.var.str.eu.rtyp,
			type.of.transaction,
			o.exchange.rate.type)
	endselect
}

function void convert.transaction.type.to.rate.type(
		domain	tcmcs.str3	i.var.str.pur.rtyp,
		domain	tcmcs.str3	i.var.str.sls.rtyp,
		domain	tcmcs.str3	i.var.str.eu.rtyp,
		domain	tfcmg.tran	i.type.of.transaction,
	ref	domain	tcmcs.str3	o.exchange.rate.type)
{
	 on case i.type.of.transaction
   	 	case tfcmg.tran.customer :  
   	 	case tfcmg.tran.unalloc.rec :  
   	 	case tfcmg.tran.advance.rec :  
  	  	case tfcmg.tran.reconc.cust :  
  	  	 	 o.exchange.rate.type =  i.var.str.sls.rtyp  
  	 	 	 break  
  	 	case tfcmg.tran.supplier :  
  	  	case tfcmg.tran.unalloc.paym :  
  	 	case tfcmg.tran.advance.paym :  
  	 	case tfcmg.tran.reconc.suppl :  
  	 	 	 o.exchange.rate.type =  i.var.str.pur.rtyp  
  	 		 break  
  	 	default:
  	 	 	 o.exchange.rate.type =  i.var.str.eu.rtyp  
  	 	 	 break  
  	  endcase
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:ti
:TCMOD:bom
:TFLNO:010
:FUNC:START
|#10.5.1.sn
function set.var.tibom010.sizu(
		double	i.length,
		double	i.width)
{
	if i.length > 0.0 or i.width > 0.0 then
		var.tibom010.sizu = tibom000.buom
	else
		var.tibom010.sizu = ""
	endif
}
										|#10.5.1.en

function fill.expiry.date(domain tidate i.date)					|#1859837.sn
{
	if i.date > 0 then
		var.tibom010.exdt = utc(i.date,0)
	else
		var.tibom010.exdt = bcrun.convdll.get.max.utc.date()
	endif
}										|#1859837.en
										|#1859837.so
|function fill.expiry.date(domain tidate i.date)
|{
|	static domain tidate max.date
|	static string max.str.date(22)
|
|	if i.date > 0 then
|		var.tibom010.exdt = utc(i.date,0)
|	else
|		var.tibom010.exdt = utc(748747,0)
|	endif
|}										|#1859837.eo
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:ti
:TCMOD:cpr
:TFLNO:000
:FUNC:START
|#CNT.sn
function domain tcyesno include.fixed.costs(
			domain	ticpty i.cpty)
{
 	 if i.cpty =  ticpty.full then  
		|* full cost price : fixed prices included
 	 	 return(tcyesno.yes)
 	 else  
		|* marginal cost price: only variable prices;
		|* fixed costs not included
 	 	 return(tcyesno.no)
 	 endif  
}
|#CNT.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:ti
:TCMOD:cpr
:TFLNO:110
:FUNC:START
function fill.var.ticpr110.chpr(
		string	i.table(8))
{
	if i.table = "ticpr130" then
		ticpr110.cmth 	= ticpr130.cmth
		ticpr110.kocl	= ticpr130.kocl
		ticpr110.cpch	= ticpr130.cpch
	endif

	|* ticpr110.chpr = surcharge percentage. 
	|* In Baan IV amount and percentage use the same field (ticpr110.cpch)

	if ticpr110.cmth = tccmth.amount then 
		|* percentage := 0
		var.ticpr110.chpr = 0 
	else 
		|* methode = %. 
		if ticpr110.kocl = tckocl.charge then 	
			|* ticpr110.cpch contains %. So also in case of MC,
			|* only the first element is filled. 
			var.ticpr110.chpr = fill.mc.field.double(ticpr110.cpch,1) 
		else
			|* Line type is subtotal or cumulative
			|* set percentage to zero.
			var.ticpr110.chpr = 0 
		endif 
	endif

}

function fill.var.ticpr110.cpch(
		string	i.table(8))
{
	if i.table = "ticpr130" then
		ticpr110.cmth 	= ticpr130.cmth
		ticpr110.kocl	= ticpr130.kocl
		ticpr110.cpch	= ticpr130.cpch
	endif

	|* ticpr110.cpch = surcharge amount
	|* In Baan IV amount and percentage use the same field (ticpr110.cpch)

 	 for element =  1 to 3  
 	 	 if ticpr110.cmth =  tccmth.amount and ticpr110.kocl =  tckocl.charge then  
			|* method is amount and line type = surcharge. 
 	 	 	 var.ticpr110.cpch(element) =  fill.mc.field.double(  
 	 	 	 	 	 ticpr110.cpch,  
 	 	 	 	 	 element,  
 	 	 	 	 	 0.0) 	  
 	 	 else  
			|* percentage or total /cumul line. 
 	 	 	 var.ticpr110.cpch(element) =  0.0  
 	 	 endif  
 	 endfor
}

function fill.var.ticpr110.chcp(
		domain	tccpcp	i.cpcp)
{
	|* ticpr110.chcp = Cost Component

	get.cost.components(bcorig.ticpr, i.cpcp, "", tmp.errmess) 
	var.ticpr110.chcp = bcstc006.cost

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:ti
:TCMOD:cpr
:TFLNO:150
:FUNC:START
|#CNT.sn
function domain tcyesno variable.costs(
			domain tivafi	i.vafi)
{

	 if i.vafi =  tivafi.var then  
 	 	 return(tcyesno.yes)
 	 else  
 	 	 return( tcyesno.no  )
 	 endif  

}

function long set.orty(
		domain	tioprt	i.oprt, 	|* Direct / Overhead
		domain	tccmoh	i.cmoh)	|* Calc. Method
{
	long 	retval


	retval = 0

	on case i.oprt
	case tioprt.wages:
		|* Wages -> labor costs
		retval = 1
		break
	case tioprt.machine:
		|* Machine -> machine costs
		retval = 2
		break
	case tioprt.overhead: |* overhead
		if (i.cmoh =  tccmoh.man.hrs) then  
			| *  Overhead on Man Hours  
 	 		retval = 3 
 	 	else  
			| *  Overhead on Machine Hours  
 	 		 retval =  4 
 	 	endif
		break
	endcase
	return(retval)
}
|#CNT.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:ti
:TCMOD:cpr
:TFLNO:200
:FUNC:START
function get.item.type.group(
			domain	tccprj	i.cprj, 
			domain	tcitem 	i.item,
		ref	domain	tckitm	o.kitm, 
		ref	domain	tccitg	o.citg,
		ref	domain	tccpcp	o.cpcp)

{
	o.citg  = ""
	o.kitm = tckitm.manufacture
	if isspace(i.cprj) then
	 	select 	tiitm001.kitm:o.kitm, 
			tiitm001.citg:o.citg,
			tiitm001.cpcp:o.cpcp
	 	from tiitm001  
 		where tiitm001._index1 =  {:i.item}  
	 	as set with 1 rows  
 		selectdo  
		endselect
	else
	 	select 	tipcs021.kitm:o.kitm, 
			tipcs021.citg:o.citg,
			tipcs021.cpcp:o.cpcp
	 	from tipcs021  
 		where tipcs021._index1 =  {:i.cprj, :i.item}  
	 	as set with 1 rows  
 		selectdo  
		endselect

	endif
}

function get.calc.part.type.group(
			domain	tccbdg	i.cbdg,
			domain	tcitem 	i.ccpc,
		ref	domain	tckitm	o.kitm, 
		ref	domain	tccitg	o.citg, 
		ref	domain	tccpcp	o.cpcp)

{
	o.citg = ""
	o.kitm = tckitm.manufacture

	select	tipcs002.kitm:o.kitm,
		tipcs002.citg:o.citg,
		tipcs002.cpcp:o.cpcp
	from	tipcs002
	where	tipcs002._index1 = {:i.cbdg, :i.ccpc}
	as set with 1 rows
	selectdo
	endselect
}


function fill.var.spit.for.ticpr201(
			domain tccpcc	i.cpcc,
			domain	tcitem	i.item)  

{
	select	ticpr220.cpcc
	from	ticpr220
	where	ticpr220._index1 = {:i.cpcc, :i.item}
	as set with 1 rows
	selectdo	
		var.spit = tcyesno.yes
	selectempty
		var.spit = tcyesno.no
	endselect
}

function  fill.var.spit.for.ticpr301(  
 	 	 domain	tcitem	i.item,  
 	 	 long		i.indt)  

{
	select	ticpr320.indt
	from	ticpr320
	where	ticpr320._index1 = {:i.item, "", :i.indt}
	as set with 1 rows
	selectdo	
		var.spit = tcyesno.yes
	selectempty
		var.spit = tcyesno.no
	endselect

}


function get.aggr.cpcp(
		domain	tckitm	i.kitm, 
		domain	tccitg	i.citg, 
		domain	tccpcp	i.cpcp)  

{
	|*---------------------------------------------------------
	|* this function fill the following global variables
	|* - aggr.mat.cpcp
	|* - aggr.oper.cpcp
	|* - aggr.surch.cpcp
	|* Use data from bcstc010 (Defaults by Item Type /Group)
	|* If these are not filled, then use company settings. 
	|* 
	|* Precondition: bcstc000 has been read
	|*-----------------------------------------------------------

	get.aggr.cost.comp.for.item(	i.kitm,
					i.citg,
					i.cpcp,
					aggr.mat.cpcp,
					aggr.opr.cpcp,
					aggr.sur.cpcp)

							|#213536.sn
	|* cost items only have aggr. comp. for material costs
	if i.kitm = tckitm.cost then
		aggr.opr.cpcp = ""
		aggr.sur.cpcp = ""
	endif						|#213536.en
}

function  init.aggr.amnts.and.nuni() 
{

	|*------------------------------------------------------------------
	|* this function sets the following global variables to zero
	|* - aggr.mat.amnt / aggr.mat.amnt.var / aggr.mat.amnt.fixed
	|* - aggr.opr.amnt / aggr.opr.amnt.var / aggr.opr.amnt.fixed
	|* - aggr.sur.amnt / aggr.sur.amnt.var / aggr.sur.amnt.fixed
	|* - aggr.mat.nuni
	|* - aggr.opr.nuni
	|* - aggr.sur.nuni
	|*-------------------------------------------------------------------
	long	ret				|#MCR.n

|	aggr.mat.amnt = 0.0			|#MCR.so
|	aggr.opr.amnt = 0.0
|	aggr.sur.amnt = 0.0			|#MCR.eo
	ret =  set.mem(aggr.mat.amnt,0.0,3)	|#MCR.sn
	ret =  set.mem(aggr.opr.amnt,0.0,3)
	ret =  set.mem(aggr.sur.amnt,0.0,3)	|#MCR.en
	
	aggr.mat.amnt.var	= 0.0		|#CNT.sn
	aggr.mat.amnt.fixed	= 0.0	
	aggr.opr.amnt.var	= 0.0		
	aggr.opr.amnt.fixed	= 0.0		
	aggr.sur.amnt.var	= 0.0		
	aggr.sur.amnt.fixed	= 0.0		|#CNT.en

	aggr.mat.nuni = 0.0
	aggr.opr.nuni = 0.0
	aggr.sur.nuni = 0.0

	tmp.max.nr.cpcp.cpr206 = 0
	tmp.max.nr.cpcp.cpr306 = 0
}


function save.max.nr.cpcp()
{
	if tmp.max.nr.cpcp.cpr206 > var.max.nr.cpcp then
		var.max.nr.cpcp = tmp.max.nr.cpcp.cpr206
	endif

	if tmp.max.nr.cpcp.cpr306 > var.max.nr.cpcp then
		var.max.nr.cpcp = tmp.max.nr.cpcp.cpr306
	endif

}

 function  aggr.amount(  	 
		|domain	tccopr	i.amnt, 	|#MCR.o
	ref	domain	tccopr	i.amnt(), 	|#MCR.n
		domain	tccopr	i.amnt.var,	|#CNT.n
		domain	tccopr	i.amnt.fixed,	|#CNT.n
 	   	domain	tcqnu1	i.nuni,  
		domain	tccref	i.cref, 
		domain	tckitm	i.kitm)  

	|*------------------------------------------------------------------
	|* this function aggregates i.amnt and i.nuni into:
	|* - aggr.mat.amnt  / aggr.mat.amnt.var / aggr.mat.amnt.fixed
	|* - aggr.opr.amnt  / aggr.opr.amnt.var / aggr.opr.amnt.fixed
	|* - aggr.sur.amnt / aggr.sur.amnt.var / aggr.sur.amnt.fixed
	|* - aggr.mat.nuni
	|* - aggr.opr.nuni
	|* - aggr.sur.nuni
	|* NB. General Costs are aggregated on Surcharges, 
	|*       except for Cost Items: these are aggr. on Mat. Comp.
	|*-------------------------------------------------------------------
{
	if i.cref = tccref.material or (i.kitm = tckitm.cost) then
		|aggr.mat.amnt = aggr.mat.amnt + i.amnt		|#MCR.o
		aggr.mat.amnt(1) = aggr.mat.amnt(1) + i.amnt(1)	|#MCR.sn
		aggr.mat.amnt(2) = aggr.mat.amnt(2) + i.amnt(2)
		aggr.mat.amnt(3) = aggr.mat.amnt(3) + i.amnt(3)	|#MCR.en

		aggr.mat.amnt.var   = aggr.mat.amnt.var + i.amnt.var		|#CNT.n
		aggr.mat.amnt.fixed = aggr.mat.amnt.fixed + i.amnt.fixed	|#CNT.n

		aggr.mat.nuni = aggr.mat.nuni + i.nuni
		return
	endif

	if i.cref = tccref.operation then
		|aggr.opr.amnt = aggr.opr.amnt + i.amnt		|#MCR.o
		aggr.opr.amnt(1) = aggr.opr.amnt(1) + i.amnt(1)	|#MCR.sn
		aggr.opr.amnt(2) = aggr.opr.amnt(2) + i.amnt(2)
		aggr.opr.amnt(3) = aggr.opr.amnt(3) + i.amnt(3)	|#MCR.en

		aggr.opr.amnt.var   = aggr.opr.amnt.var + i.amnt.var	|#CNT.n
		aggr.opr.amnt.fixed = aggr.opr.amnt.fixed + i.amnt.fixed	|#CNT.n

		aggr.opr.nuni = aggr.opr.nuni + i.nuni
		return
	endif

	if (i.cref = tccref.chg.operation) or 
	    (i.cref = tccref.chg.material) or
	    (i.cref = tccref.general) then
		|aggr.sur.amnt = aggr.sur.amnt + i.amnt		|#MCR.o
		aggr.sur.amnt(1) = aggr.sur.amnt(1) + i.amnt(1)	|#MCR.sn
		aggr.sur.amnt(2) = aggr.sur.amnt(2) + i.amnt(2)
		aggr.sur.amnt(3) = aggr.sur.amnt(3) + i.amnt(3)	|#MCR.en

		aggr.sur.amnt.var   = aggr.sur.amnt.var + i.amnt.var	|#CNT.n
		aggr.sur.amnt.fixed = aggr.sur.amnt.fixed + i.amnt.fixed	|#CNT.n

		aggr.sur.nuni = aggr.sur.nuni + i.nuni
	endif

}

function domain tcbool detailed.cpcp(
		domain	tckitm	i.kitm, 
		domain	tccitg	i.citg,
		domain	tccpcp	i.cpcp)

{
	|*-----------------------------------------------------
	|* If cost component is in the Cost Component Chart,
	|* of the item type/group, then the costs components,
	|* must be tracked in detail.
	|* Otherwise use aggregated cost components.  
	|*
	|* NB. i.cpcp must be Baan IV cost component 
	|* 	(from ticpr010). 
	|* global input:
	|* - var.cpcp
	|*-----------------------------------------------------

	long	ret.val

	if var.cpcp = aggr.mat.cpcp or
	   var.cpcp = aggr.opr.cpcp or
	   var.cpcp = aggr.sur.cpcp then
		|* Aggregated Cost Comp. So, not detaield. 
		return(false)
	endif

							|#213536.sn
	|* always aggregate on material cost component
	|* when cost item
	if i.kitm = tckitm.cost then
		return(false)
	endif						|#213536.en

	ret.val = get.cost.component.charts.per.item.type.and.group(
		  		 i.kitm, 
				 i.citg, 
				 tmp.errmess)
	if ret.val = 0 then
		|* chart found
		ret.val = get.cost.component.charts.details(
				bcsti007.chrt, 
				i.cpcp,
				tmp.errmess)
		if ret.val = 0 then
			|* cost component found in chart. Detailed Cost Comp!
			return(true)
		endif
	endif
	return(false)
			
}

function domain tcbool cpcp.filled()				|#213940.sn
{
	|* global input: var.cpcp
	return(not isspace(var.cpcp))
}								|#213940.en


							|#237409.sn
function long item.type.has.valuation.price( domain tckitm i.item.type ) 
{
	if 	i.item.type = tckitm.cost 
	  or	i.item.type = tckitm.service
	then
		return(0)
	endif

	return(1)
}
							|#237409.en

function dump.1.ticpr200.eff.cpcp()
{
	|* skip  zero records					|#FP7_PLZA.sn
	if is.zero.record() then
		return
	endif
								|#FP7_PLZA.en
	|* Cost Price by Calc. Code, Item, Eff. Cost Comp.
	if cpcp.filled() then					|#213940.n
		dump 1		|* ticpr200
	endif							|#213940.n
}

function dump.1.ticpr200.aggr.cpcp()
{
	|*-----------------------------------------------------
	|* This function dumps the data into ticpr200
	|* 	Cost Price by Calc. Code, Item, Eff. Cost Comp
	|* 
	|* Global inputs: 
	|* var.item
	|* var.cpcc
	|* aggr.mat.cpcp, aggr.mat.nuni, aggr.mat.amnt
	|*--------------------------------------------------------------

	var.cpcp = aggr.mat.cpcp
	var.nuni = aggr.mat.nuni
	|var.amnt = aggr.mat.amnt			|#MCR.o
	var.amnt(1) = aggr.mat.amnt(1)			|#MCR.sn
	var.amnt(2) = aggr.mat.amnt(2)
	var.amnt(3) = aggr.mat.amnt(3)			|#MCR.en
	
	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
|	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n  |#zero.o
	|* var.cpcp should always be filled, also for Cost Items|#213940.n
|		dump 1					|#FP7_PLZA.o
|	endif						|#zero.o

	|* dump  non-zero records only			|#FP7_PLZA.sn
	if not is.zero.record() then
		dump 1 |* ticpr200
	endif						|#FP7_PLZA.en

	var.cpcp = aggr.opr.cpcp
	var.nuni = aggr.opr.nuni
	|var.amnt = aggr.opr.amnt			|#MCR.o
	var.amnt(1) = aggr.opr.amnt(1)			|#MCR.sn
	var.amnt(2) = aggr.opr.amnt(2)
	var.amnt(3) = aggr.opr.amnt(3)			|#MCR.en

	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
|	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
	if 	cpcp.filled()				|#213940.n
	  and 	not is.zero.record() 			|#FP7_PLZA.n
	then
		dump 1	|*ticpr200
	endif							|#213940.n

|	endif						|#zero.o

	var.cpcp = aggr.sur.cpcp
	var.nuni = aggr.sur.nuni
	|var.amnt = aggr.sur.amnt			|#MCR.o
	var.amnt(1) = aggr.sur.amnt(1)			|#MCR.sn
	var.amnt(2) = aggr.sur.amnt(2)
	var.amnt(3) = aggr.sur.amnt(3)			|#MCR.en
	
	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
|	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
	if 	cpcp.filled()				|#213940.n
	  and 	not is.zero.record() 			|#FP7_PLZA.n
	then
		dump 1		|*ticpr200
	endif						|#213940.n
|	endif						|#zero.o

}

function dump.2.ticpr201()
{
	|* Calc. Code, Item
	dump 2		|*ticpr201
}

function dump.3.ticpr205.det.cpcp()
{

	|* skip  zero records				|#FP7_PLZA.sn
	if is.zero.record() then
		return
	endif
							|#FP7_PLZA.en
	|* Cost Price by Calc. Code, Item, Detailed Cost Comp. 
	dump 3		|*ticpr205
}

function dump.4.ticpr300.eff.cpcp()
{
	|* skip  zero records				|#FP7_PLZA.sn
	if is.zero.record() then
		return
	endif
							|#FP7_PLZA.en
	|* Standard Cost Price Item, Eff. Cost Comp.
	if cpcp.filled() then					|#213940.n
		dump 4		|*ticpr300
	endif							|#213940.n
}

function dump.4.ticpr300.aggr.cpcp()
{
	|*-----------------------------------------------------
	|* This function dumps the data into ticpr300
	|* 	Standard Cost Price Item, Eff. Cost Comp
	|* 
	|* Global Input:
	|* var.item
	|* aggr.mat.cpcp, aggr.mat.nuni, aggr.mat.amnt
	|*--------------------------------------------------------------

	var.cpcp = aggr.mat.cpcp
	var.nuni = aggr.mat.nuni
	|var.amnt = aggr.mat.amnt			|#MCR.o
	var.amnt(1) = aggr.mat.amnt(1)			|#MCR.sn
	var.amnt(2) = aggr.mat.amnt(2)
	var.amnt(3) = aggr.mat.amnt(3)			|#MCR.en
	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
|	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
	|* var.cpcp should always be filled, also for Cost Items|#213940.n
|		dump 4					|#FP7_PLZA.o
|	endif						|#zero.o

	|* dump  non-zero records only			|#FP7_PLZA.sn
	if not is.zero.record() then
		dump 4 |* ticpr300
	endif						|#FP7_PLZA.en

	var.cpcp = aggr.opr.cpcp
	var.nuni = aggr.opr.nuni
	|var.amnt = aggr.opr.amnt			|#MCR.o
	var.amnt(1) = aggr.opr.amnt(1)			|#MCR.sn
	var.amnt(2) = aggr.opr.amnt(2)
	var.amnt(3) = aggr.opr.amnt(3)			|#MCR.en
	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
|	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
	if 	cpcp.filled()				|#213940.n
	  and 	not is.zero.record() 			|#FP7_PLZA.n
	then
		dump 4		|*ticpr300
	endif						|#213940.n

|	endif						|#zero.o

	var.cpcp = aggr.sur.cpcp
	var.nuni = aggr.sur.nuni
	|var.amnt = aggr.sur.amnt			|#MCR.o
	var.amnt(1) = aggr.sur.amnt(1)			|#MCR.sn
	var.amnt(2) = aggr.sur.amnt(2)
	var.amnt(3) = aggr.sur.amnt(3)			|#MCR.en
	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
|	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
	if	 cpcp.filled() 				|#213940.n
	  and 	not is.zero.record() 			|#FP7_PLZA.n
	then
		dump 4		|*ticpr300
	endif						|#213940.n
|	endif						|#zero.o

}

function dump.5.ticpr301()
{
	|* Item, Effective Date
	dump 5			|*ticpr301
}

function dump.6.ticpr305.det.cpcp()
{	
	|* skip  zero records				|#FP7_PLZA.sn
	if is.zero.record() then
		return
	endif
							|#FP7_PLZA.en
	|* Standard Cost Price by Item, Detailed Cost Comp. 
	dump 6			|*ticpr305
}

function dump.7.ticpr306.aggr.cpcp()
{
	|*--------------------------------------------------------
	|* Effective Standard Cost Price Structure : 
	|* 	Item, indt, cpcp
	|*
	|* Precondition: the following fields are filled:
	|* - var.item
	|*---------------------------------------------------------

	
|	if aggr.mat.nuni <> 0.0 or aggr.mat.amnt(1) <> 0 then	|#zero.o
		var.cpcp = aggr.mat.cpcp
		var.agcp = 2
	|* var.cpcp should always be filled, also for Cost Items|#213940.n
		dump 7			|*ticpr306
		tmp.max.nr.cpcp.cpr306 = tmp.max.nr.cpcp.cpr306  + 1

|	endif							|#zero.o

|	if aggr.opr.nuni <> 0.0 or aggr.opr.amnt(1) <> 0 then	|#zero.o
		var.cpcp = aggr.opr.cpcp
		var.agcp = 3
	if cpcp.filled() then					|#213940.n
		dump 7			|*ticpr306
		tmp.max.nr.cpcp.cpr306 = tmp.max.nr.cpcp.cpr306  + 1
	endif							|#213940.n
|	endif							|#zero.o

|	if aggr.sur.nuni <> 0.0 or aggr.sur.amnt(1) <> 0 then	|#zero.o
		var.cpcp = aggr.sur.cpcp
		var.agcp = 5 
	if cpcp.filled() then					|#213940.n
		tmp.max.nr.cpcp.cpr306 = tmp.max.nr.cpcp.cpr306  + 1
		dump 7			|*ticpr306
	endif							|#213940.n
|	endif							|#zero.o

	save.max.nr.cpcp()
}

function dump.7.ticpr306.eff.cpcp()
{
	|*--------------------------------------------------------
	|* Effective Cost Price Structure : 
	|* The non-aggragated cpcp's that must be
	|* tracked in detail. 
	|*
	|* Precondition: the following fields are filled:
	|* - var.item
	|* - var.cpcp
	|*---------------------------------------------------------

	|* do not insert a record if Cost Component is
	|* known as aggregated cost component. 
	if var.cpcp = aggr.mat.cpcp or 
	   var.cpcp = aggr.opr.cpcp or 
	  var.cpcp = aggr.sur.cpcp or
	  not cpcp.filled() then				|#213940.n
		return
	endif
	

	var.agcp = 1
	dump 7				|*ticpr306

	tmp.max.nr.cpcp.cpr306 = tmp.max.nr.cpcp.cpr306  + 1
	save.max.nr.cpcp()

}

function dump.8.ticpr206.aggr.cpcp()
{	
	|*--------------------------------------------------------
	|* Cost Price Structure : 
	|* 	Item, cpcp
	|*
	|* Precondition: the following fields are filled:
	|* - var.item
	|*---------------------------------------------------------

|	if aggr.mat.nuni <> 0.0 or aggr.mat.amnt(1) <> 0 then		|#zero.o
		var.cpcp = aggr.mat.cpcp
		var.agcp = 2
	|* var.cpcp should always be filled, also for Cost Items|#213940.n
		dump 8			|*ticpr206
		tmp.max.nr.cpcp.cpr206 = tmp.max.nr.cpcp.cpr206 + 1
|	endif								|#zero.o
	
|	if aggr.opr.nuni <> 0.0 or aggr.opr.amnt(1) <> 0 then		|#zero.o
		var.cpcp = aggr.opr.cpcp
		var.agcp = 3
	if cpcp.filled() then					|#213940.n
		dump 8			|*ticpr206
		tmp.max.nr.cpcp.cpr206 = tmp.max.nr.cpcp.cpr206 + 1
	endif							|#213940.n
|	endif								|#zero.o
	
|	if aggr.sur.nuni <> 0.0 or aggr.sur.amnt(1) <> 0 then		|#zero.o
		var.cpcp = aggr.sur.cpcp
		var.agcp = 5 
	if cpcp.filled() then					|#213940.n
		dump 8			|*ticpr206
		tmp.max.nr.cpcp.cpr206 = tmp.max.nr.cpcp.cpr206 + 1
	endif							|#213940.n
|	endif								|#zero.o

	save.max.nr.cpcp()

}

function dump.8.ticpr206.eff.cpcp()
{
	|*--------------------------------------------------------
	|* Cost Price Structure : 
	|* The non-aggragated cpcp's that must be
	|* tracked in detail. 
	|*
	|* Precondition: the following fields are filled:
	|* - var.item
	|* - var.cpcp
	|*---------------------------------------------------------


	|* do not insert a record if Cost Component is
	|* known as aggregated cost component. 
	if var.cpcp = aggr.mat.cpcp or 
	   var.cpcp = aggr.opr.cpcp or 
	  var.cpcp = aggr.sur.cpcp or
	 not cpcp.filled() then					|#213940.n
		return
	endif
	
	var.agcp = 1
	dump 8			|*ticpr206
	
	tmp.max.nr.cpcp.cpr206 = tmp.max.nr.cpcp.cpr206 + 1
	save.max.nr.cpcp()

}

function dump.9.ticpr240.aggr.cpcp(
		domain tckitm i.item.type) |#237409.n
{
	|*-----------------------------------------------------
	|* This function dumps the data into ticpr240
	|* 	Item Valuation Price (by Calc. Code, 
	|* 		on Cost Comp. Struct.)
	|* 
	|* Global inputs: 
	|* var.item
	|* var.cpcc
	|* aggr.mat.cpcp, aggr.mat.nuni, aggr.mat.amnt
	|*--------------------------------------------------------------

				|#237409.sn
	if not item.type.has.valuation.price(i.item.type) then
		return
	endif			|#237409.en

	|*------------------------------
	|* Material costs
	|*------------------------------
	var.cpcp = aggr.mat.cpcp
	var.nuni = aggr.mat.nuni
|#CNT.sn
	|* Fixed costs are included in cost price, depening on ticpr / tipcs parameter
	|* "include fixed costs". 
	if var.cpty = ticpty.full then
		var.ticpr240.amnt = aggr.mat.amnt(1)
	else
		var.ticpr240.amnt = aggr.mat.amnt.var
	endif
	var.amnt.var 	= aggr.mat.amnt.var
	var.amnt.fixed	= aggr.mat.amnt.fixed
	|* var.cpcp should always be filled, also for Cost Items|#213940.n
|	dump 9						|#FP7_PLZA.o
|#CNT.en
|#CNT.so
|	|var.amnt = aggr.mat.amnt			|#MCR.o
|	var.amnt(1) = aggr.mat.amnt(1)			|#MCR.sn
|	var.amnt(2) = aggr.mat.amnt(2)
|	var.amnt(3) = aggr.mat.amnt(3)			|#MCR.en
|	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
||	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n|#zero.o
|		dump 9		|*ticpr240
||	endif						|#zero.o
|#CNT.eo
	|* dump  non-zero records only			|#FP7_PLZA.sn
	if not is.zero.record() then
		dump 9  |* ticpr240
	endif						|#FP7_PLZA.en

	|*------------------------------
	|* Operation costs
	|*------------------------------
	var.cpcp = aggr.opr.cpcp
	var.nuni = aggr.opr.nuni
|#CNT.sn
	|* Fixed costs are included in cost price, depening on ticpr / tipcs parameter
	|* "include fixed costs". 
	if var.cpty = ticpty.full then
		var.ticpr240.amnt = aggr.opr.amnt(1)
	else
		var.ticpr240.amnt = aggr.opr.amnt.var
	endif
	var.amnt.var 	= aggr.opr.amnt.var
	var.amnt.fixed	= aggr.opr.amnt.fixed
	if 	cpcp.filled()				|#213940.n
	  and 	not is.zero.record() 			|#FP7_PLZA.n
	then
		dump 9			|*ticpr240
	endif						|#213940.n
|#CNT.en
|#CNT.so
|	|var.amnt = aggr.opr.amnt			|#MCR.o
|	var.amnt(1) = aggr.opr.amnt(1)			|#MCR.sn
|	var.amnt(2) = aggr.opr.amnt(2)
|	var.amnt(3) = aggr.opr.amnt(3)			|#MCR.en
|	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
||	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
|		dump 9
|	endif						|#zero.o
|#CNT.eo

	|*------------------------------
	|* Surcharge costs
	|*------------------------------
	var.cpcp = aggr.sur.cpcp
	var.nuni = aggr.sur.nuni
|#CNT.sn
	|* Fixed costs are included in cost price, depening on ticpr / tipcs parameter
	|* "include fixed costs". 
	if var.cpty = ticpty.full then
		var.ticpr240.amnt = aggr.sur.amnt(1)
	else
		var.ticpr240.amnt = aggr.sur.amnt.var
	endif
	var.amnt.var 	= aggr.sur.amnt.var
	var.amnt.fixed	= aggr.sur.amnt.fixed
	if 	cpcp.filled() 				|#213940.n
	  and 	not is.zero.record() 			|#FP7_PLZA.n
	then
		dump 9			|*ticpr240
	endif						|#213940.n
|#CNT.en
|#CNT.so
|	|var.amnt = aggr.sur.amnt			|#MCR.o
|	var.amnt(1) = aggr.sur.amnt(1)			|#MCR.sn
|	var.amnt(2) = aggr.sur.amnt(2)
|	var.amnt(3) = aggr.sur.amnt(3)			|#MCR.en
|	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
||	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
|		dump 9
|	endif						|#zero.o
|#CNT.eo

}

function dump.9.ticpr240.eff.cpcp(
		domain tckitm i.item.type) |#237409.n
{

	|*-----------------------------------------------------
	|* This function dumps the Cost Components
	|*  that must be tracked in detail, into ticpr240
	|* 	Item Valuation Price (by Calc. Code, 
	|* 		on Cost Comp. Struct.)
	|* 
	|* Global inputs: 
	|* var.item
	|* var.cpcc
	|* var.cpcp
	|*-------------------------------------------------------------

				|#237409.sn
	if not item.type.has.valuation.price(i.item.type) then
		return
	endif			|#237409.en

	|* skip  zero records				|#FP7_PLZA.sn
	if is.zero.record() then
		return
	endif
							|#FP7_PLZA.en

|#CNT.sn
	|* Fixed costs are included in cost price, depening on ticpr / tipcs parameter
	|* "include fixed costs". 
	if var.cpty = ticpty.full then
		var.ticpr240.amnt = var.amnt(1)
	else
		var.ticpr240.amnt = var.amnt.var
	endif
|#CNT.en

	if cpcp.filled() then					|#213940.n
		dump 9			|*ticpr240
	endif							|#213940.n
}


function dump.10.ticpr340.aggr.cpcp(
		domain tckitm i.item.type) |#237409.n
{
	|*-----------------------------------------------------
	|* This function dumps the data into ticpr340
	|* 	Item Standard Valuation Price ( 
	|* 		on Cost Comp. Struct.)
	|* 
	|* Global inputs: 
	|* var.item
	|* aggr.mat.cpcp, aggr.mat.nuni, aggr.mat.amnt
	|*--------------------------------------------------------------

				|#237409.sn
	if not item.type.has.valuation.price(i.item.type) then
		return
	endif			|#237409.en

	|*------------------------------
	|* Material costs
	|*------------------------------
	var.cpcp = aggr.mat.cpcp
	var.nuni = aggr.mat.nuni
|#CNT.sn
	|* Fixed costs are included in cost price, depening on ticpr / tipcs parameter
	|* "include fixed costs". 
	if var.cpty = ticpty.full then
		var.ticpr340.amnt = aggr.mat.amnt(1)
	else
		var.ticpr340.amnt = aggr.mat.amnt.var
	endif
	var.amnt.var 	= aggr.mat.amnt.var
	var.amnt.fixed	= aggr.mat.amnt.fixed
	|* var.cpcp should always be filled, also for Cost Items|#213940.n
|	dump 10					|#FP7_PLZA.o

	|* dump  non-zero records only			|#FP7_PLZA.sn
	if not is.zero.record() then
		dump 10 |* ticpr340
	endif						|#FP7_PLZA.en
|#CNT.en
|#CNT.so
|	|var.amnt = aggr.mat.amnt			|#MCR.o
|	var.amnt(1) = aggr.mat.amnt(1)			|#MCR.sn
|	var.amnt(2) = aggr.mat.amnt(2)
|	var.amnt(3) = aggr.mat.amnt(3)			|#MCR.en
|	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
||	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
|		dump 10
||	endif						|#zero.o
|#CNT.so


	|*------------------------------
	|* Operation costs
	|*------------------------------
	var.cpcp = aggr.opr.cpcp
	var.nuni = aggr.opr.nuni
|#CNT.sn
	|* Fixed costs are included in cost price, depening on ticpr / tipcs parameter
	|* "include fixed costs". 
	if var.cpty = ticpty.full then
		var.ticpr340.amnt = aggr.opr.amnt(1)
	else
		var.ticpr340.amnt = aggr.opr.amnt.var
	endif
	var.amnt.var 	= aggr.opr.amnt.var
	var.amnt.fixed	= aggr.opr.amnt.fixed
	if 	cpcp.filled()				|#213940.n
	  and	not is.zero.record() 			|#FP7_PLZA.n
	then
		dump 10		|* ticpr340
	endif						|#213940.n
|#CNT.en
|#CNT.so
|	|var.amnt = aggr.opr.amnt			|#MCR.o
|	var.amnt(1) = aggr.opr.amnt(1)			|#MCR.sn
|	var.amnt(2) = aggr.opr.amnt(2)
|	var.amnt(3) = aggr.opr.amnt(3)			|#MCR.en
|	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
||	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
|		dump 10	
||	endif						|#zero.o
|#CNT.eo


	|*------------------------------
	|* Surcharge costs
	|*------------------------------
	var.cpcp = aggr.sur.cpcp
	var.nuni = aggr.sur.nuni
|#CNT.sn
	|* Fixed costs are included in cost price, depening on ticpr / tipcs parameter
	|* "include fixed costs". 
	if var.cpty = ticpty.full then
		var.ticpr340.amnt = aggr.sur.amnt(1)
	else
		var.ticpr340.amnt = aggr.sur.amnt.var
	endif
	var.amnt.var 	= aggr.sur.amnt.var
	var.amnt.fixed	= aggr.sur.amnt.fixed
	if 	cpcp.filled()				|#213940.n
	  and	not is.zero.record() 			|#FP7_PLZA.n
	then
		dump 10		|* ticpr340
	endif						|#213940.n
|#CNT.en
|#CNT.so
|	|var.amnt = aggr.sur.amnt			|#MCR.o
|	var.amnt(1) = aggr.sur.amnt(1)			|#MCR.sn
|	var.amnt(2) = aggr.sur.amnt(2)
|	var.amnt(3) = aggr.sur.amnt(3)			|#MCR.en
|	|if var.nuni <> 0.0 or var.amnt <> 0.0 then	|#MCR.o
||	if var.nuni <> 0.0 or var.amnt(1) <> 0.0 then	|#MCR.n	|#zero.o
|		dump 10
||	endif						|#zero.o
|#CNT.eo	

}

function dump.10.ticpr340.eff.cpcp(
		domain tckitm i.item.type) |#237409.n
{

	|*-----------------------------------------------------
	|* This function dumps the Cost Components

	|*  that must be tracked in detail, into ticpr340
	|* 	Item Standard Valuation Price (
	|* 		on Cost Comp. Struct.)
	|* 
	|* Global inputs: 
	|* var.item
	|* var.amnt
	|* var.nuni
	|* var.cpcp
	|*-------------------------------------------------------------

				|#237409.sn
	if not item.type.has.valuation.price(i.item.type) then
		return
	endif			|#237409.en

|#CNT.sn
	|* Fixed costs are included in cost price, depening on ticpr / tipcs parameter
	|* "include fixed costs". 
	
	|* skip  zero records				|#FP7_PLZA.sn
	if is.zero.record() then
		return
	endif
							|#FP7_PLZA.en
	if var.cpty = ticpty.full then
		var.ticpr340.amnt = var.amnt(1)
	else
		var.ticpr340.amnt = var.amnt.var
	endif
|#CNT.en
|
	if cpcp.filled() then					|#213940.n
		dump 10		|* ticpr340
	endif							|#213940.n
}

function dump.11.ticpr099()
{
	|* This function fills ticpr099. It has one record and contains
	|* the maximum size of the Cost Component Structure

	dump 11		|* ticpr099

}

						|#FP7_PLZA.sn
|--------------------------------------------------------------------
| FP7_PLZA: Prevent logging of zero amounts. See doc PD#49801.
| This solution should reduce the size of the following tables:
| - ticpr200
| - ticpr205
| - ticpr240
| - ticpr300
| - ticpr305
| - ticpr340
|---------------------------------------------------------------------

function domain  tcbool  is.zero.record()
{
	if var.nuni.is.non.zero() then
		return(false)
	endif

	if var.amount.is.non.zero() then
		return(false)
	endif

	return(true)
}


function domain  tcbool  var.amount.is.non.zero()
{
	 for element =  1 to 3
		 if double.cmp(var.amnt(element), 0.0, 0.0001) <> 0 then
			return(true)
		endif
 	 endfor

	return(false)
}

function domain  tcbool  var.nuni.is.non.zero()
{
	 if double.cmp(var.nuni, 0.0, 0.0001) <> 0 then
		return(true)
	endif

	return(false)
}


								|#FP7_PLZA.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:ti
:TCMOD:cpr
:TFLNO:220
:FUNC:START
function domain tcyesno variable.costs(
			domain tivafi	i.vafi)
{

	 if i.vafi =  tivafi.var then  
 	 	 return(tcyesno.yes)
 	 else  
 	 	 return( tcyesno.no  )
 	 endif  

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:ti
:TCMOD:cpr
:TFLNO:320
:FUNC:START
function domain tcyesno variable.costs(
			domain tivafi	i.vafi)
{

	 if i.vafi =  tivafi.var then  
 	 	 return(tcyesno.yes)
 	 else  
 	 	 return( tcyesno.no  )
 	 endif  

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:ti
:TCMOD:pcs
:TFLNO:000
:FUNC:START
function domain tcyesno include.fixed.costs(
			domain	ticpty i.cpty)
{
 	 if i.cpty =  ticpty.full then  
		|* full cost price : fixed prices included
 	 	 return(tcyesno.yes)
 	 else  
		|* marginal cost price: only variable prices;
		|* fixed costs not included
 	 	 return(tcyesno.no)
 	 endif  
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:ti
:TCMOD:pcs
:TFLNO:320
:FUNC:START
function domain tcyesno variable.costs(
			domain tivafi	i.vafi)
{

	 if i.vafi =  tivafi.var then  
 	 	 return(tcyesno.yes)
 	 else  
 	 	 return( tcyesno.no  )
 	 endif  

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:wh
:TCMOD:inr
:TFLNO:110
:FUNC:START
|#800-145084.sn

function split.up.lots.inventory()
{
	long		arr.nr
	domain	tcqiv1		tmp.whinr110.qstk
	domain	tcbool		split.up
	domain	tdltc.clot	new.lot
	domain	tcqiv1		new.qty
	domain	tcqiv1		qty.to.be.split
	domain	tdltc.clot	lot.serial				|# fp3.sn

	|* Note1: var.whinr110.serl or clot can be filled	|#2043803_2.sn
	|* also for var.whinr110.valm = 1 FTP
	|*
	|* Note2: that this function must be called only
	|* for var.whinr110.valm = 5 Lot priced
	|* or var.whinr110.valm = 6 Serial priced		|#2043803_2.en   

	 if not isspace(var.whinr110.serl) then
 		 lot.serial =  var.whinr110.serl
 	 else
 		 lot.serial =  var.whinr110.clot
	 endif
									|# fp3.en
 	if var.whina112.qskt >  0 then
 		 | *  not consumed
		qty.to.be.split = var.whina112.qstk
 	 else
 	 	 | *  partially consumed
 	  	 var.whina112.qskt =  onhand.inventory -  to.be.received

		if nr.lots = 1 and
|		   strip$(arr.lots(1,1;20)) = strip$(var.whinr110.clot) then	|# fp3.o
		   strip$(arr.lots(1,1;20)) = strip$(lot.serial) then		|# fp3.n
			qty.to.be.split = 0.0
		else
			qty.to.be.split = var.whina112.qskt
		endif

		if var.whinr110.qstk > 0 then
			tmp.whinr110.qstk = var.whinr110.qstk - qty.to.be.split
		else
			tmp.whinr110.qstk = var.whinr110.qstk + qty.to.be.split
		endif

		if tmp.whinr110.qstk <> 0 then
			var.whinr110.qstk = tmp.whinr110.qstk
			dump 1
			var.whina112.qstk = abs(var.whinr110.qstk)
			var.whina112.qskt = 0.0
			var.whina112.clot = var.whinr110.clot	|#2043803.n
			var.whina112.serl = var.whinr110.serl	|#2043803.n
			dump 3

 	 	 	 	 	 	 	 	 | #  fp3.sn
 	 	 	 if not isspace(var.whinr110.serl) then 	 |serialized
 	 	 	 	 dump 14
 	 	 	 endif
 	 	 	 	 	 	 	 	 | #  fp3.en

| 			if isspace(var.whinr110.clot) then  				|# fp3.o
			if isspace(lot.serial) then					|# fp3.n
				|* Exception for lot or serial priced item |#2043803_2.n
 	 			get.standard.cost.price(tdinv700.item, abs(var.whinr110.qstk),
 			 	 			hold.tdinv700.trdt, true, false)
 			else
| 	 			get.lot.price(tdinv700.item, var.whinr110.clot, abs(var.whinr110.qstk),  |# fp3.o
 	 			get.lot.price(tdinv700.item, lot.serial, abs(var.whinr110.qstk),  	|# fp3.n
 				 	 	 	hold.tdinv700.trdt,
 				 	 	 	true,			|#FP5.n
 				 	 	 	true,
 				 	 	 	false)
 			endif
			if qty.to.be.split = 0 then
				return
			endif

			var.whinr110.seqn = var.whinr110.seqn + 1
		endif
 	 endif

	split.up = false
	repeat
		determine.lot.onhand(arr.lots, nr.lots, arr.nr, new.lot, new.qty)
		lot.serial = new.lot						|# fp3.n

		if new.qty = 0 then
			new.qty = qty.to.be.split
		endif

		if new.qty >= qty.to.be.split then
			arr.lots(21,arr.nr) = str$(new.qty - qty.to.be.split)
										|# fp3.sn
			if var.whinr110.valm = 6 then
				var.whinr110.serl = new.lot
				var.whina112.serl = new.lot	|#2043803_2.n
			else							|# fp3.en
				|* var.whinr110.valm = 5 here	|#2043803_2.n
				var.whinr110.clot = new.lot
				var.whina112.clot = new.lot	|#2043803_2.n
			endif							|# fp3.n

			if var.whinr110.qstk > 0 then
				var.whinr110.qstk = qty.to.be.split
			else
				var.whinr110.qstk = -qty.to.be.split
			endif
			dump 1
			var.whina112.qstk = qty.to.be.split
			var.whina112.qskt = qty.to.be.split
			dump 3

 	 	 	 	 	 	 	 	 | #  fp3.sn
 	 	 	 if not isspace(var.whinr110.serl) then 	 |serialized
 	 	 	 	 dump 14
 	 	 	 endif
 	 	 	 	 	 	 	 	 | #  fp3.en

			split.up = true
		else
			arr.lots(21,arr.nr) = str$(0.0)
										|# fp3.sn
			if var.whinr110.valm = 6 then
				var.whinr110.serl = new.lot
				var.whina112.serl = new.lot	|#2043803_2.n
			else							|# fp3.en
				|* var.whinr110.valm = 5 here	|#2043803_2.n
				var.whinr110.clot = new.lot
				var.whina112.clot = new.lot	|#2043803_2.n
			endif							|# fp3.n

			if var.whinr110.qstk > 0 then
				var.whinr110.qstk = new.qty
			else
				var.whinr110.qstk = -new.qty
			endif
			dump 1
			var.whina112.qstk = new.qty

			var.whina112.qskt = new.qty
			dump 3

 	 	 	 	 	 	 	 	 | #  fp3.sn
 	 	 	 if not isspace(var.whinr110.serl) then 	 |serialized
 	 	 	 	 dump 14
 	 	 	 endif
 	 	 	 	 	 	 	 	 | #  fp3.en

			qty.to.be.split = qty.to.be.split - new.qty
		endif
|		if isspace(var.whinr110.clot) then  				|# fp3.o
		if isspace(lot.serial) then					|# fp3.n
			|* Exception for lot or serial priced item |#2043803_2.n
 			get.standard.cost.price(tdinv700.item, abs(var.whinr110.qstk),
	 	 	 			hold.tdinv700.trdt, true, false)
		else
| 			get.lot.price(tdinv700.item, var.whinr110.clot, abs(var.whinr110.qstk),  |# fp3.o
 			get.lot.price(tdinv700.item, lot.serial, abs(var.whinr110.qstk),  	|# fp3.n
			 	 	 	hold.tdinv700.trdt,
			 	 	 	true,			|#FP5.n
			 	 	 	true,
			 	 	 	false)



		endif
		if not split.up then
			var.whinr110.seqn = var.whinr110.seqn + 1

		endif
	until split.up
}

function determine.lot.onhand(	ref	string			i.arr.lots(,),
					long			i.nr.of.lots,
				ref	long			o.arr.nr,
				ref	domain	tdltc.clot	o.lot,
				ref	domain	tcqiv1		o.qty)
{
	long	i

	o.lot = ""
	o.arr.nr = 1

	for i = 1 to i.nr.of.lots
		o.qty = val(i.arr.lots(21,i))
		if o.qty > 0 then
			o.lot = i.arr.lots(1,i;20)
			o.arr.nr = i
			break
		endif
	endfor

}


function determine.lot.inventory.onhand(	domain	tcitem	i.item,
					domain	tccwar	i.cwar,
				ref	long		o.nr.lots,
				ref	string		o.arr.lots(,))
{
	long	i
	long	first
	domain	tdltc.clot	hold.clot
	domain	tcqiv1		hold.stks

	static	domain	tcbool	memory.allocated

	if memory.allocated then
		set.mem(o.arr.lots,"")
	else
		alloc.mem(o.arr.lots, 40, 100)
		memory.allocated = true
	endif

	hold.clot = ""
	first = true
	i = 0

	select	tdilc101.stks, tdilc101.clot
	from	tdilc101
	where	tdilc101._index7 = {:i.cwar, "", :i.item, ""}
	and	tdilc101.stks > 0
	order by tdilc101.clot
	selectdo
		if tdilc101.clot <> hold.clot then
			if first then
				first = false
			else
				i = i + 1
				o.arr.lots(1,i;20) = hold.clot
				o.arr.lots(21,i) = str$(hold.stks)
			endif
			hold.clot = tdilc101.clot
			hold.stks = 0.0
		endif
		hold.stks = hold.stks + tdilc101.stks
	selecteos
		i = i + 1
		o.arr.lots(1,i;20) = hold.clot
		o.arr.lots(21,i) = str$(hold.stks)
	endselect

	o.nr.lots = i
}
									|#800-145084.en



function domain tcpono determine.rpl.order.sequence(	domain	tcorno	i.orno,
							domain	tcpono	i.pono,
							domain	tckost	i.kost,
							domain	tccwar	i.cwar,
							domain	tcdate	i.trdt)
{
	if i.kost = tckost.rpl.delivery then
		select	tdrpl100.srnb
		from	tdrpl100
		where	tdrpl100._index1 = {:i.orno, :i.pono}
		and	tdrpl100.rwar = :i.cwar
		and	tdrpl100.rddt = :i.trdt
		as set with 1 rows
		selectdo
			 return(tdrpl100.srnb)
		endselect
	else
		select	tdrpl100.srnb
		from	tdrpl100
		where	tdrpl100._index1 = {:i.orno, :i.pono}
		and	tdrpl100.dwar = :i.cwar
		and	tdrpl100.rrdt = :i.trdt
		as set with 1 rows
		selectdo
			 return(tdrpl100.srnb)
		endselect
	endif
	return(0)
}
								|#SL204550.so
|function domain tcpono determine.sales.order.sequence(	domain	tcbool	i.inv,	|#800-143063.n
|							domain	tcorno	i.orno,
|							domain	tcpono	i.pono,
|							domain	tcdate	i.trdt,
|							domain	tctime	i.time,	|#800-143063.n
|							domain	tcsern	i.sern,	|#800-143063.n
|							domain	tccprj	i.cprj,	|#800-143063.n
|							domain	tcqiv1	i.quan)	|#800-143063.n
|{
|										|#800-143063.sn
|	domain	tcqiv1	already.handled
|	domain	tcqiv1	delivered
|
|
|	already.handled = 0.0
|	delivered = 0.0
|
|	if i.inv then
|		select	sum(tdinv700.quan):already.handled
|		from	tdinv700
|		where	tdinv700._index2 ={tckoor.act.sls, :i.orno, :i.pono, :i.trdt}
|		and	(tdinv700.trtm < :i.time or (tdinv700.trtm = :i.time and tdinv700.sern < :i.sern))
|		selectdo
|		endselect
|	else
|		select	sum(tipcs700.quan):already.handled
|		from	tipcs700
|		where	tipcs700._index2 ={:i.cprj, tckoor.act.sls, :i.orno, :i.pono, :i.trdt}
|		and	(tipcs700.trtm < :i.time or (tipcs700.trtm = :i.time and tipcs700.sern < :i.sern))
|		selectdo
|		endselect
|	endif
|										|#800-143063.en
|
|	select	tdsls045.srnb,tdsls045.ddat, tdsls045.dqua
|	from	tdsls045
|	where	tdsls045._index1 = {:i.orno, :i.pono}
|	and	tdsls045.ddat >= :i.trdt
|	order by tdsls045.ddat asc
||	as set with 1 rows							|#800-143063.o
|	selectdo
|		delivered = delivered + tdsls045.dqua				|#800-143063.n
|		if delivered >= already.handled + i.quan then			|#800-143063.n
|			return(get.sls.order.line.sequence.number(   	 i.orno,
| 	 	 	 	 		 	 		 i.pono,
| 	 	 	 	 		 	 		 tdsls045.srnb))
|		endif								|#800-143063.n
|	endselect
|
|	return(0)
|}
								|#SL204550.eo
								|#SL204550.sn
function get.sales.order.line.data(
			domain	tcbool	i.inv,
			domain	tcorno	i.orno,
			domain	tcpono	i.pono,
			domain	tcdate	i.trdt,
			domain	tctime	i.time,
			domain	tcsern	i.sern,
			domain	tccprj	i.cprj,
			domain	tcitem	i.item,
			domain	tcqiv1	i.quan,
		ref	domain	tcpono	o.srnb,	|* sales order line sequence
		ref	domain	tcpono	o.ponb)	|* bom line
{
	domain	tcqiv1	already.handled
	domain	tcqiv1	delivered


	already.handled = 0.0
	delivered = 0.0

	if i.inv then
		select	sum(tdinv700.quan):already.handled
		from	tdinv700
		where	tdinv700._index2 = {	tckoor.act.sls,
						:i.orno,
						:i.pono,
						:i.trdt}
		and	(tdinv700.trtm < :i.time
		or	(tdinv700.trtm = :i.time and tdinv700.sern < :i.sern))
		selectdo
		endselect
	else
		select	sum(tipcs700.quan):already.handled
		from	tipcs700
		where	tipcs700._index2 ={	:i.cprj,
						tckoor.act.sls,
						:i.orno,
						:i.pono,
						:i.trdt}
		and	(tipcs700.trtm < :i.time
		or	(tipcs700.trtm = :i.time and tipcs700.sern < :i.sern))
		selectdo
		endselect
	endif

	o.srnb = 0
|	select	tdsls045.srnb,tdsls045.ddat, tdsls045.dqua	|#213329.so
|	from	tdsls045
|	where	tdsls045._index1 = {:i.orno, :i.pono}
|	and	tdsls045.ddat >= :i.trdt
|	order by tdsls045.ddat asc				|#213329.eo

	select	tdsls045.orno, tdsls045.pono,			|#213329.sn
		tdsls045.srnb, tdsls045.ddat, tdsls045.dqua
	from	tdsls045
	where	tdsls045._index1 = {	:i.orno,
					:i.pono}
	and	tdsls045.ddat >= :i.trdt
	order by tdsls045.orno, tdsls045.pono, tdsls045.ddat asc|#213329.en
	selectdo
		delivered = delivered + tdsls045.dqua
		if delivered >= already.handled + i.quan then
			o.srnb = get.sls.order.line.sequence.number(
						i.orno,
 	 	 	 	 		i.pono,
 	 	 	 	 		tdsls045.srnb)
			break					|#213329.n
		endif
	endselect

	o.ponb = 0
	select	tdsls047.ponb:o.ponb
	from	tdsls047
	where	tdsls047._index2 = {	:i.cprj,
					:i.item,
					"",		|* container
					:i.orno,
					:i.pono}
	as set with 1 rows
	selectdo
	endselect
}

|function get.return.purchase.order.line.data(			|#213329.o
function get.purchase.order.line.data(				|#213329.n
			domain	tcbool	i.inv,
			domain	tcorno	i.orno,
			domain	tcpono	i.pono,
			domain	tcdate	i.trdt,
			domain	tctime	i.time,
			domain	tcsern	i.sern,
			domain	tccprj	i.cprj,
			domain	tcitem	i.item,
			domain	tcqiv1	i.quan,
		ref	string		o.rcno,		|#228896.sn
		ref	domain	tcpono	o.rcln,		|#227896.en
		ref	domain	tcpono	o.srnb,	|* purchase order line sequence
		ref	domain	tcpono	o.ponb)	|* bom line
{
	domain	tcqiv1	already.handled
	domain	tcqiv1	delivered


	already.handled = 0.0
	delivered = 0.0

	if i.inv then
		select	sum(tdinv700.quan):already.handled
		from	tdinv700
		where	tdinv700._index2 = {	tckoor.act.pur,
						:i.orno,
						:i.pono,
						:i.trdt}
		and	(tdinv700.trtm < :i.time
		or	(tdinv700.trtm = :i.time and tdinv700.sern < :i.sern))
		selectdo
		endselect
	else
		select	sum(tipcs700.quan):already.handled
		from	tipcs700
		where	tipcs700._index2 ={	:i.cprj,
						tckoor.act.pur,
						:i.orno,
						:i.pono,
						:i.trdt}
		and	(tipcs700.trtm < :i.time
		or	(tipcs700.trtm = :i.time and tipcs700.sern < :i.sern))
		selectdo
		endselect
	endif

	already.handled = abs(already.handled)
	o.srnb = 0
	o.rcno = ""						|#227896.sn
	o.rcln = 0						|#227896.en
|	select	tdpur045.srnb, tdpur045.date, tdpur045.diqu	|#213329.so
|	from	tdpur045
|	where	tdpur045._index1 = {:i.orno, :i.pono}
|	and	tdpur045.date >= :i.trdt
|	order by tdpur045.date asc				|#213329.eo

	select	tdpur045.orno, tdpur045.pono,			|#213329.sn
		tdpur045.reno,					|#227896.n
		tdpur045.srnb, tdpur045.date, tdpur045.diqu
	from	tdpur045
	where	tdpur045._index1 = {:i.orno, :i.pono}
	and	tdpur045.srnb > 0
	and	tdpur045.date >= :i.trdt
	order by tdpur045.orno, tdpur045.pono, tdpur045.date asc|#213329.en
	selectdo
		delivered = delivered + tdpur045.diqu
		if delivered >= already.handled + i.quan then
			o.srnb = get.pur.order.line.sequence.number(
	 	 	 	 		tdpur045.srnb)
			break					|#213329.n
		endif
		if tdpur045.reno > 0 then			|#227896.sn
			get.receipt.and.receipt.line(		|#1470346.sn
						tdpur045.orno,
						tdpur045.pono,
						tdpur045.srnb,
						o.rcno,
						o.rcln)		|#1470346.en
		endif
	endselect

	o.ponb = 0
	select	tdpur047.ponb:o.ponb
	from	tdpur047
	where	tdpur047._index2 = {	:i.cprj,
					:i.item,
					"",		|* container
					:i.orno,
					:i.pono}
	as set with 1 rows
	selectdo
	endselect
}
								|#SL204550.en
function get.standard.cost.price(domain	tcitem	i.item,
				domain	tcqiv1	i.quan,
				domain	tcdate	i.date,
				domain	tcbool	i.receipt,

				domain	tcbool	i.negative)
{
	long	first.record
	long	result
	long	i, j
	long	counter

	domain	tcdate		hold.date

	static	domain	bccost		material.component
	static	domain	bccost		operation.component
	static	domain	bccost		surcharge.component
	static	domain tcamnt		material.component.amnt(3)
	static	domain tcamnt		operation.component.amnt(3)
	static	domain tcamnt		surcharge.component.amnt(3)
		domain tcamnt		hold.amnt(3)

	static	domain	bccost		arr.cpcp(1)		based
	static	double			arr.cpcp.amnt(1,1)	based

	static	domain	tcbool	allocated

	domain	bccost		detail.cost.component

	first.record = true

	if not allocated then
		allocated = true
		alloc.mem(arr.cpcp, 8, 20)
		alloc.mem(arr.cpcp.amnt, 20, 3)
	endif

	set.mem(arr.cpcp,"")
	set.mem(arr.cpcp.amnt, 0.0)

	counter = 0

 	  set.mem(material.component.amnt, 0.0)
 	  set.mem(operation.component.amnt, 0.0)
 	  set.mem(surcharge.component.amnt, 0.0)


	select	ticpr300.*
 	  from  	  ticpr300
 	  where ticpr300._index1 =  {:i.item}
	and	ticpr300.cntr = ""
 	  and  	  ticpr300.indt <= :i.date
	order by ticpr300.indt desc
 	  selectdo
		if first.record then
			hold.date = ticpr300.indt
			first.record = false
		else
			if hold.date <> ticpr300.indt then
				break
			endif
		endif

  	 	 for i =  1 to g.mc.depth
  	 	 	 hold.amnt(i) =  get.amount.per.cost.component(
							"ticpr300", i)
  	 	 endfor

 	   	  analyze.cost.component( i.item,
 	   	   	   	   	  "",
 	   	   	   	   	  ticpr300.cpcp,
 	   	   	   	   	  detail.cost.component,
 	   	   	   	   	  material.component,
 	   	   	   	   	  operation.component,
 	   	   	   	   	  surcharge.component,
 	   	   	   	   	  result)
 	   	  on case result
 	   	  case 0:
 	   	   	   |  *  Cost Component in chart



 	   	   	   |  *  Detail Cost Component
 			counter = counter + 1
			arr.cpcp(1,counter) = detail.cost.component
  	 		 for i =  1 to g.mc.depth
				arr.cpcp.amnt(counter,i) = hold.amnt(i)
			endfor
			if i.receipt then
 	   	   		  var.whina113.cpcp =  detail.cost.component
		  	 	 for i =  1 to g.mc.depth
	 	   	   		  var.whina113.amnt(i) =  hold.amnt(i) * i.quan
					  var.whina113.mauc(i) = hold.amnt(i)
				endfor
 	   	   		  dump 7  |  *  whina113
			else
 	   	   		  var.whina115.cpcp =  detail.cost.component
  	 			 for i =  1 to g.mc.depth
 	   	   		  	var.whina115.amnt(i) =  hold.amnt(i) * i.quan
				endfor
				if i.negative then
					dump 12	|* whina121
				else
 	   	   		  	dump 8  |  *  whina115
				endif
			endif

 	   	   	  break
 	   	  case 1:
 	   	   	   |  *  Merge to aggregated material cost component

  	 		 for i =  1 to g.mc.depth
 	   	   	  	material.component.amnt(i) =
 	   	   	   	  material.component.amnt(i) + hold.amnt(i)
			endfor
 	   	   	  break
 	   	  case 2:
 	   	   	   |  *  Merge to aggregated operation cost component

  	 		 for i =  1 to g.mc.depth
 	   		   	  operation.component.amnt(i) =
 	   	   	   	  operation.component.amnt(i) + hold.amnt(i)
			endfor
 	   	   	  break
 	   	  case 3:
 	   	   	   |  *  Merge to aggregated surcharge cost component

  	 		 for i =  1 to g.mc.depth
 	   		   	  surcharge.component.amnt(i) =
 	   	   	   	  surcharge.component.amnt(i) + hold.amnt(i)
			endfor
 	   	   	  break
 	   	  endcase
	selectempty
 	   	  analyze.cost.component( i.item,
 	   	   	   	   	  "",

 	   	   	   	   	  "",
 	   	   	   	   	  detail.cost.component,
 	   	   	   	   	  material.component,
 	   	   	   	   	  operation.component,
 	   	   	   	   	  surcharge.component,
 	   	   	   	   	  result)
 	  endselect

	if i.receipt then
	 	  var.whina113.cpcp =  material.component
  	 	 for i =  1 to g.mc.depth
 			  var.whina113.amnt(i) =  material.component.amnt(i) * i.quan
			  var.whina113.mauc(i) = material.component.amnt(i)
		endfor
		dump 7

 		  var.whina113.cpcp =  operation.component
  	 	 for i =  1 to g.mc.depth
	 		  var.whina113.amnt(i) =  operation.component.amnt(i) * i.quan
	 		  var.whina113.mauc(i) =  operation.component.amnt(i)
		endfor
		dump 7

	 	  var.whina113.cpcp =  surcharge.component
  	 	 for i =  1 to g.mc.depth
	 		  var.whina113.amnt(i) =  surcharge.component.amnt(i) * i.quan
	 		  var.whina113.mauc(i) =  surcharge.component.amnt(i)
		endfor
		dump 7
	else
	 	  var.whina115.cpcp =  material.component
  	 	 for i =  1 to g.mc.depth
 		  	var.whina115.amnt(i) =  material.component.amnt(i) * i.quan
		endfor
		if i.negative then
			dump 12	|* whina121
		else
   		  	dump 8  	|  *  whina115
		endif

 		  var.whina115.cpcp =  operation.component
  	 	 for i =  1 to g.mc.depth
	 	 	 var.whina115.amnt(i) =  operation.component.amnt(i) * i.quan
		endfor
		if i.negative then
			dump 12	|* whina121
		else

   		  	dump 8  	|  *  whina115
		endif

	 	  var.whina115.cpcp =  surcharge.component
  	 	 for i =  1 to g.mc.depth
	 	 	 var.whina115.amnt(i) =  surcharge.component.amnt(i) * i.quan
		endfor

		if i.negative then
			dump 12		|* whina121
		else
   		  	dump 8  	|  *  whina115
		endif
	endif

}
										|#TELAIR.sn
function get.lot.price(		domain	tcitem		i.item,
				domain	tdltc.clot	i.clot,
				domain	tcqiv1		i.quan,
				domain	tcdate		i.date,
				domain	tcbool		i.split.lots.inv,	|#FP5.n
				domain	tcbool		i.receipt,
				domain	tcbool		i.negative)

{
	long	result
	long	i

	domain	bccost		material.component
	domain	bccost		operation.component
	domain	bccost		surcharge.component
	domain tcamnt		material.component.price(3)
	domain tcamnt		operation.component.price(3)
	domain tcamnt		surcharge.component.price(3)
	domain tcamnt		hold.amnt(3)
	domain	bccost		detail.cost.component

	|* Note: that this function must be called only		|#2043803_2.sn   
	|* for var.whinr110.valm = 5 Lot priced
	|* or var.whinr110.valm = 6 Serial priced		|#2043803_2.en   

 	  set.mem(material.component.price, 0.0)
 	  set.mem(operation.component.price, 0.0)
 	  set.mem(surcharge.component.price, 0.0)

 	  select tdltc004.*
 	  from 	 tdltc004
 	  where tdltc004._index1 =  { 	 :i.item,
 	 	 	 	 	 "",
 	  	  	 	 	 :i.clot}
 	  selectdo
 	  	 analyze.cost.component( tdltc004.item,
 	  	 	 	 	 "",
 	  	 	 	 	 tdltc004.cpcp,
 	  	 	 	 	 detail.cost.component,
 	  	 	 	 	 material.component,
 	  	 	 	 	 operation.component,
 	  	 	 	 	 surcharge.component,
	  	 	 	 	 result)

 	  	 on case result
 	  	 case 0:
 	  	 	 | *  Cost Component in chart
 	  	 	 | *  Dump record

 	  	 	 for i =  1 to g.mc.depth
 	  	 	 	 hold.amnt(i) = get.amount.per.cost.component( "tdltc004", i)
 	 	 	 endfor

			if i.receipt then
 	   	   		  var.whina113.cpcp =  detail.cost.component
		  	 	 for i =  1 to g.mc.depth
	 	   	   		  var.whina113.amnt(i) =  hold.amnt(i) * i.quan
					  var.whina113.mauc(i) = hold.amnt(i)
				endfor
 	   	   		  dump 7  |  *  whina113
									|# fp3.sn
|				if not isspace(var.whinr110.serl) then |serial pricing	|#FP5.o
									|#FP5.sn
				if i.split.lots.inv and
				   not isspace(var.whinr110.serl) then |serial pricing
				   					|#FP5.en
					dump 15
				endif
									|# fp3.en
			else
 	   	   		  var.whina115.cpcp =  detail.cost.component
  	 			 for i =  1 to g.mc.depth
 	   	   		  	var.whina115.amnt(i) =  hold.amnt(i) * i.quan
				endfor
				if i.negative then
					dump 12	|* whina121
				else
 	   	   		  	dump 8  |  *  whina115
				endif
			endif


 	 	 	  break
 	 	  case 1:
 	 	  	 | *  Merge to aggregated material cost component
 	  	 	 for i =  1 to g.mc.depth
 	  	 	 	 material.component.price(i) =  material.component.price(i) +
 	  	 	 	 	 get.amount.per.cost.component( "tdltc004", i)
 	  	 	 endfor

 	 	  	 break
 	 	  case 2:
 	 	  	 | *  Merge to aggregated opration cost component
 	 	   	 for i =  1 to g.mc.depth
 	 	  	 	 operation.component.price(i) =  operation.component.price(i) +
 	 	   	 	 	 get.amount.per.cost.component( "tdltc004", i)
 	 	   	 endfor

 	 	  	 break
 	 	  case 3:
 	 	  	 | *  Merge to aggregated surcharge cost component
 	  	 	 for i =  1 to g.mc.depth
 	  	 	 	 surcharge.component.price(i) =  surcharge.component.price(i) +
 	  	 	 	 	 get.amount.per.cost.component( "tdltc004", i)
 	  	 	 endfor

 	 	  	 break
 	 	  endcase
 	 endselect


	if i.receipt then
	 	  var.whina113.cpcp =  material.component
  	 	 for i =  1 to g.mc.depth
 			  var.whina113.amnt(i) = material.component.price(i) * i.quan

			  var.whina113.mauc(i) = material.component.price(i)

		endfor
		dump 7

							|# fp3.sn
|		if not isspace(var.whinr110.serl) then |serial pricing	|#FP5.o
									|#FP5.sn
		if i.split.lots.inv and
		   not isspace(var.whinr110.serl) then |serial pricing
				   					|#FP5.en
			dump 15
		endif
							|# fp3.en

 		  var.whina113.cpcp =  operation.component
  	 	 for i =  1 to g.mc.depth
	 		  var.whina113.amnt(i) =  operation.component.price(i) * i.quan
	 		  var.whina113.mauc(i) =  operation.component.price(i)
		endfor
		dump 7

							|# fp3.sn
|		if not isspace(var.whinr110.serl) then |serial pricing	|#FP5.o
									|#FP5.sn
		if i.split.lots.inv and
		   not isspace(var.whinr110.serl) then |serial pricing
				   					|#FP5.en
			dump 15
		endif
							|# fp3.en

	 	  var.whina113.cpcp =  surcharge.component
  	 	 for i =  1 to g.mc.depth
	 		  var.whina113.amnt(i) =  surcharge.component.price(i) * i.quan
	 		  var.whina113.mauc(i) =  surcharge.component.price(i)
		endfor
		dump 7

							|# fp3.sn
|		if not isspace(var.whinr110.serl) then |serial pricing	|#FP5.o
									|#FP5.sn
		if i.split.lots.inv and
		   not isspace(var.whinr110.serl) then |serial pricing
				   					|#FP5.en
			dump 15
		endif
							|# fp3.en

	else
	 	  var.whina115.cpcp =  material.component
  	 	 for i =  1 to g.mc.depth
 		  	var.whina115.amnt(i) =  material.component.price(i) * i.quan
		endfor
		if i.negative then
			dump 12	|* whina121
		else
   		  	dump 8  	|  *  whina115
		endif

 		  var.whina115.cpcp =  operation.component
  	 	 for i =  1 to g.mc.depth
	 	 	 var.whina115.amnt(i) =  operation.component.price(i) * i.quan
		endfor
		if i.negative then
			dump 12	|* whina121
		else
   		  	dump 8  	|  *  whina115
		endif

	 	  var.whina115.cpcp =  surcharge.component
  	 	 for i =  1 to g.mc.depth
	 	 	 var.whina115.amnt(i) =  surcharge.component.price(i) * i.quan
		endfor

		if i.negative then
			dump 12		|* whina121
		else
   		  	dump 8  	|  *  whina115
		endif
	endif
}

function domain tdltc.clot determine.lot(
		domain	tcqiv1	i.quantity,			|#2043803.n
		domain	tckost	i.transaction.type,		|#2043803.n
		domain	tcdate	i.trdt)
{
	domain	tcqiv1		quantity			|#2043803_2.n

	select	tdilc301.clot,
		tdilc301.qstk:quantity				|#2043803_2.n
	from	tdilc301
	where	tdilc301._index1 = {"", :tdinv700.item, ""}
	and	tdilc301.cwar = :tdinv700.cwar
	and	tdilc301.trdt = :i.trdt
	and	tdilc301.koor = :tdinv700.koor
|	and	tdilc301.kost = :tdinv700.kost			|#2043803.o
	and	tdilc301.kost = :i.transaction.type		|#2043803.n
	and	tdilc301.orno = :tdinv700.orno
	and	(tdilc301.koor = tckoor.wrh.order or tdilc301.pono = :tdinv700.pono)
|	and	tdilc301.qstk = :tdinv700.quan			|#2043803.o
|	and	tdilc301.qstk = :i.quantity	|#2043803.n	|#2043803_2.o
|	as set with 1 rows					|#2043803_2.o
	selectdo
		if abs(quantity) = abs(i.quantity) then		|#2043803_2.n
			return(tdilc301.clot)
		endif						|#2043803_2.n
	endselect

	return("")
}									|#TELAIR.en

								|#2043803_4.sn
function domain tdltc.clot determine.lot.for.tipcs700(
		domain	tcqiv1	i.quantity,			
		domain	tckost	i.transaction.type,		
		domain	tcdate	i.trdt)
{
	|* Cwar not present in tipcs700 but retrieved in precondition section 
	|* from tdil301 or tipcs021
	
	domain	tcqiv1		quantity			

	select	tdilc301.clot,
		tdilc301.qstk:quantity				
	from	tdilc301
	where	tdilc301._index1 = {:tipcs700.cprj, :tipcs700.item, ""}
	and	tdilc301.cwar = :var.whinr110.cwar
	and	tdilc301.trdt = :i.trdt
	and	tdilc301.koor = :tipcs700.koor
	and	tdilc301.kost = :i.transaction.type		
	and	tdilc301.orno = :tipcs700.orno
	and	(tdilc301.koor = tckoor.wrh.order or
		 tdilc301.pono = :orig.tipcs700.pono)
	selectdo
		if abs(quantity) = abs(i.quantity) then
			return(tdilc301.clot)
		endif					
	endselect

	return("")
}
								|#2043803_4.en

function get.customized.cost.price(	domain	tccprj	i.cprj,
					domain	tcitem	i.item,
					domain	tcqiv1	i.quan,
					domain	tcbool	i.receipt)
{
	long	result
	long	i, j
	long	counter

	static	domain	tccprj		hold.cprj
	static	domain	tcitem		hold.item

	static	domain	bccost		material.component
	static	domain	bccost		operation.component
	static	domain	bccost		surcharge.component

	static	domain tcamnt		material.component.amnt(3)
	static	domain tcamnt		operation.component.amnt(3)
	static	domain tcamnt		surcharge.component.amnt(3)
		domain tcamnt		hold.amte(3)

	domain	bccost			detail.cost.component


	static	domain	bccost		arr.cpcp(1)		based
	static	double			arr.cpcp.amnt(1,1)	based

	static	domain	tcbool	allocated

	if not allocated then
		allocated = true
		alloc.mem(arr.cpcp, 8, 20)
		alloc.mem(arr.cpcp.amnt, 20, 3)
	endif

if hold.item <> i.item or
  hold.cprj <> i.cprj   then
	set.mem(arr.cpcp,"")
	set.mem(arr.cpcp.amnt, 0.0)

	counter = 0
	hold.item = i.item
	hold.cprj = i.cprj

 	  set.mem(material.component.amnt,  0.0)
 	  set.mem(operation.component.amnt, 0.0)
 	  set.mem(surcharge.component.amnt, 0.0)

|	select tipcs350.cpcp, tipcs350.amte 				|#CNT.o
	select	tipcs350.*						|#CNT.n
 	  from  	  tipcs350
 	  where tipcs350._index1 =  {:i.cprj, :i.item}
 	  selectdo

  	 	 for i =  1 to g.mc.depth
|  	 	 	 hold.amte(i) =  fill.mc.field.double(tipcs350.amte, i) |#CNT.o
  	 	 	 hold.amte(i) =  get.amount.per.cost.component(
							"tipcs350", i)  	|#CNT.n
  	 	 endfor

 	   	  analyze.cost.component( i.item,

 	   	   	   	   	  i.cprj,
 	   	   	   	   	  tipcs350.cpcp,
 	   	   	   	   	  detail.cost.component,
 	   	   	   	   	  material.component,
 	   	   	   	   	  operation.component,
 	   	   	   	   	  surcharge.component,
 	   	   	   	   	  result)
 	   	  on case result
 	   	  case 0:
 	   	   	   |  *  Cost Component in chart

 	   	   	   |  *  Detail Cost Component

  			counter = counter + 1
			arr.cpcp(1,counter) = detail.cost.component
  	 		 for i =  1 to g.mc.depth
				arr.cpcp.amnt(counter,i) = hold.amte(i)
			endfor
			if i.receipt then

 	   		   	  var.whina113.cpcp =  detail.cost.component
  	 			 for i =  1 to g.mc.depth
 	   		   	  	var.whina113.amnt(i) =  hold.amte(i) * i.quan
				  	var.whina113.mauc(i) = hold.amte(i)
				endfor
 	   		   	  dump 9  |  *  whina113
			else


 	   		   	  var.whina115.cpcp =  detail.cost.component
   	 			 for i =  1 to g.mc.depth
	   		   	 	 var.whina115.amnt(i) =  hold.amte(i) * i.quan
				endfor
 	   		   	  dump 10  |  *  whina115
			endif

 	   	   	  break
 	   	  case 1:


 	   	   	   |  *  Merge to aggregated material cost component

  	 		 for i =  1 to g.mc.depth
 	   	   		  material.component.amnt(i) =
 	   	   	   	  material.component.amnt(i) + hold.amte(i)
			endfor
 	   	   	  break
 	   	  case 2:
 	   	   	   |  *  Merge to aggregated operation cost component

  	 		 for i =  1 to g.mc.depth
 	   	   	  	operation.component.amnt(i) =
 	   	   	   	  operation.component.amnt(i) + hold.amte(i)
			endfor
 	   	   	  break

 	   	  case 3:
 	   	   	   |  *  Merge to aggregated surcharge cost component

  	 		 for i =  1 to g.mc.depth
 	   	   		  surcharge.component.amnt(i) =
 	   	   	   	  surcharge.component.amnt(i) + hold.amte(i)
			endfor
 	   	   	  break
 	   	  endcase
	selectempty
 	   	  analyze.cost.component( i.item,
 	   	   	   	   	  i.cprj,
 	   	   	   	   	  "",
 	   	   	   	   	  detail.cost.component,
 	   	   	   	   	  material.component,
 	   	   	   	   	  operation.component,
 	   	   	   	   	  surcharge.component,
 	   	   	   	   	  result)
 	  endselect

else
	j = 1
 	while strip$(arr.cpcp(1,j)) <> ""
		if i.receipt then
		     var.whina113.cpcp =  arr.cpcp(1,j)
  	 		 for i =  1 to g.mc.depth
 		     		var.whina113.amnt(i) =  arr.cpcp.amnt(j,i) * i.quan
		     		var.whina113.mauc(i) = arr.cpcp.amnt(j,i)

			endfor
 		     dump 9  |  *  whina113
		else

		     var.whina115.cpcp =  arr.cpcp(1,j)
  	 	 	for i =  1 to g.mc.depth
 		     		var.whina115.amnt(i) =  arr.cpcp.amnt(j,i) * i.quan
			endfor
 		     dump 10  |  *  whina115
		endif
	     j = j + 1
	endwhile
endif

 	   |  *  Material Cost Component

	if i.receipt then
	 	  var.whina113.cpcp =  material.component
  	 	 for i =  1 to g.mc.depth
	 	  	var.whina113.amnt(i) =  material.component.amnt(i) * i.quan
		  	var.whina113.mauc(i) = material.component.amnt(i)
		endfor
		dump 9

 		  var.whina113.cpcp =  operation.component
  	 	 for i =  1 to g.mc.depth
	 		  var.whina113.amnt(i) =  operation.component.amnt(i) * i.quan
 			  var.whina113.mauc(i) =  operation.component.amnt(i)
		endfor
		dump 9

 		  var.whina113.cpcp =  surcharge.component
  	 	 for i =  1 to g.mc.depth
 		 	 var.whina113.amnt(i) =  surcharge.component.amnt(i) * i.quan
 			  var.whina113.mauc(i) =  surcharge.component.amnt(i)
		endfor
		dump 9



	else
	 	  var.whina115.cpcp =  material.component
  	 	 for i =  1 to g.mc.depth
	 	  	var.whina115.amnt(i) =  material.component.amnt(i) * i.quan
		endfor
		dump 10

 		  var.whina115.cpcp =  operation.component
  	 	 for i =  1 to g.mc.depth
 			  var.whina115.amnt(i) =  operation.component.amnt(i) * i.quan
		endfor
 		dump 10

 		  var.whina115.cpcp =  surcharge.component
  	 	 for i =  1 to g.mc.depth
 		 	 var.whina115.amnt(i) =  surcharge.component.amnt(i) * i.quan
		endfor
		dump 10
	endif

}

function domain tcpono determine.new.pono( domain tcorno i.orno)
{
	domain tcpono new.position

	new.position = tdinv700.pono

	select tdinv100.*
	from tdinv100
	where tdinv100._index2 = {:i.orno}
	as set with 1 rows
	selectdo
		select tdilc102.serb
		from tdilc102
		where tdilc102._index1 = { 	:tdinv100.cprj,
						:tdinv100.item,
						:tdinv100.cntr,
						:tdinv100.cwar,
						:tdinv100.idat,
						:tdinv100.sern}
		order by tdilc102.serb desc
		as set with 1 rows
		selectdo
			new.position = tdilc102.serb
		selectempty

			new.position = tdinv100.sern
		endselect
	endselect

	return(new.position)
}



function domain tcbool receipt.transaction(
			domain	tckoor	i.koor,						|#205835.n
			domain	tckost	i.transaction.type,



			domain	tcqiv1	i.quantity)
{
 	if (i.transaction.type =  tckost.stc.correction and i.quantity >  0) or
 	   ((i.transaction.type =  tckost.pur.receipt or
 	     i.transaction.type =  tckost.prd.receipt or
 	     i.transaction.type =  tckost.rpl.receipt) and i.quantity >  0) or
 	   ((i.transaction.type =  tckost.sls.delivery or

 	      i.transaction.type =  tckost.prd.mat.issue or
 	      i.transaction.type =  tckost.rpl.delivery) and i.quantity <  0) or
| 	   (i.transaction.type =  tckost.stc.transfer and i.quantity >  0) then  	|#205835.o
 	   (i.transaction.type =  tckost.stc.transfer and i.quantity >  0 and		|#205835.n
	    i.koor <> tckoor.act.sls and i.koor <> tckoor.sma.order) then	  	|#205835.n
		return(true)
	else
		return(false)
	endif
}

							|#209255.sn
function handle.inventory.without.tdinv700()
{
	domain	tcbool	skip.record				|#216914.n

	select	tdinv001.cwar, tdinv001.item, tdinv001.ltdt, tdinv001.stoc,
		tdinv001.hstd,					|#1013954.n
		tiitm001.stva,
		tiitm001.kltc						|# fp3.n
	from	tdinv001, tiitm001
	where 	 tdinv001.item >  ""
	and 	 tdinv001.cntr =  ""
	and	tdinv001.stoc > 0
								|#216914.so
|	and	not exists(	select	tdinv700.item
|				from	tdinv700
|				where	tdinv700._index1 = {tdinv001.item, "", tdinv001.cwar})
								|#216914.eo
	and	tiitm001.item = tdinv001.item
	selectdo
								|#216914.sn
		skip.record = false

		select	tdinv700.item
		from	tdinv700
		where	tdinv700._index1 = {:tdinv001.item, "", :tdinv001.cwar}
 	 	and 	 tdinv700.trdt >=  :tdinv001.hstd	|#1013954.n
		as set with 1 rows
		selectdo
			skip.record = true
		endselect

		if skip.record then
			continue
		endif
								|#216914.en

		tdinv700.cwar = tdinv001.cwar
		tdinv700.item = tdinv001.item
		tdinv700.cprj = ""
		tdinv700.logn = logname$
		tdinv700.pono = 10

		var.whinr110.trdt = utc(tdinv001.ltdt,0)

 	 	 itid =  itid +  1
 	 	 var.whinr110.itid =  edit$(itid,"999999999")
 	 	 var.whinr110.itse =  1
 	 	 var.whinr110.koor =  52 	 | *  adjustment order
 	 	 var.whinr110.kost =  1 	 | *  stock correction
 	 	 var.whinr110.orno =  "MIGRATION"
		var.whinr110.pono = 10
		var.whinr110.srnb = 0
 	 	 var.whinr110.seqn =  1
 	 	 var.whinr110.qstk =  tdinv001.stoc
 	 	 var.whinr110.bpid =  ""
 	 	 var.whinr110.qhnd =  tdinv001.stoc
		var.whinr110.shpm = ""
		var.whinr110.shpo = 0
		var.whinr110.prnt = tcyesno.no
		var.whinr110.rcno = ""				|#234040.sn
		var.whinr110.rcln = 0				|#234040.en

 	 	 if tiitm001.stva =  tcstva.stnd.copr then
 	 	 	 var.whinr110.valm =  1 | *  ftp
 	 	 else
			if tiitm001.kltc = tckltc.batch then		|# fp3.n
 	 	 	 	var.whinr110.valm =  5 | *  lot
			else						|# fp3.sn
				var.whinr110.valm = 6 | * serial
			endif						|# fp3.en

 	 	 	 determine.lot.inventory.onhand(  tdinv700.item,
 	 	 	 	 	 	 	 tdinv700.cwar,
 	 	 	 	 	 	 	 nr.lots,
 	 	 	 	 	 	 	 arr.lots)
 	 	 endif

| 		 if var.whinr110.valm =  5 then		|#2043803.so
| 		 	 var.whinr110.clot =  determine.lot(tdinv001.ltdt)
| 		 else					|#2043803.eo
 		 	 var.whinr110.clot =  empty()
| 		 endif					|#2043803.o

									|# fp3.sn
| 		 if var.whinr110.valm =  6 then		|#2043803.so
|		 	 var.whinr110.serl =  determine.lot(tdinv001.ltdt)
| 		 else					|#2043803.eo
 		 	 var.whinr110.serl =  empty()
| 		 endif					|#2043803.o

									|# fp3.en
		hold.tdinv700.trdt = tdinv001.ltdt

 	 	 var.whina112.qskt =  tdinv001.stoc
 	 	 var.whina112.qstk =  tdinv001.stoc
	 	var.whina112.clot =  empty()		|#2043803_2.n
	 	var.whina112.serl =  empty()		|#2043803_2.n

| 	 	 if var.whinr110.valm =  5 then				|# fp3.o
		if var.whinr110.valm = 5 or var.whinr110.valm = 6 then	|# fp3.n
			|* new lots and serials with price	|#2043803_2.n
			|* can be determined in function below	|#2043803_2.n
 	 	 	 split.up.lots.inventory()
 	 	 else
 	 	 	 dump 1

 	 	 	 dump 3
| 	 	 	 if isspace(var.whinr110.clot) then	|#2043803_2.o
 	 	 	 	 get.standard.cost.price(tdinv700.item, tdinv001.stoc,
 	 	 	 	 	 	 	 tdinv001.ltdt, true, false)
| 	 	 	 else					|#2043803_2.so
| 	 	 	 	 get.lot.price(tdinv700.item, var.whinr110.clot, tdinv001.stoc,
| 	 	 	 	 	 	 	 tdinv001.ltdt,
| 	 	 	 	 	 	 	 false,			|#FP5.n
| 	 	 	 	 	 	 	 true,
| 	 	 	 	 	 	 	 false)
| 	 	 	 endif					|#2043803_2.eo
 	 	 endif
	endselect
}

function handle.inventory.without.tipcs700()
{
	domain	tcbool	skip.record				|#216914.n

	select	tipcs021.cprj, tipcs021.item, tipcs021.cwar, tipcs021.ltdt, tipcs021.stoc
	from	tipcs021
	where	tipcs021.cprj > ""
	and	tipcs021.stoc > 0
								|#216914.so
|	and	not exists(	select	tipcs700.item
|				from	tipcs700
|				where	tipcs700._index1 = {tipcs021.cprj, tipcs021.item})
								|#216914.eo
	selectdo

								|#216914.sn
		skip.record = false

		select	tipcs700.item
		from	tipcs700
		where	tipcs700._index1 = {:tipcs021.cprj, :tipcs021.item}
		as set with 1 rows
		selectdo
			skip.record = true
		endselect

		if skip.record then
			continue
		endif
								|#216914.en

		tipcs700.item = tipcs021.item
		tipcs700.logn = logname$
		tipcs700.pono = 10
 	 	var.whinr110.cprj =  get.project(tipcs021.cprj)

		var.whinr110.trdt = utc(tipcs021.ltdt,0)

 	 	 itid =  itid +  1
 	 	 var.whinr110.itid =  edit$(itid,"999999999")
 	 	 var.whinr110.itse =  1
 	 	 var.whinr110.koor =  52 	 | *  adjustment
 	 	 var.whinr110.kost =  1 	 | *  stock correction
 	 	 var.whinr110.orno =  "MIGRATION"
 	 	 var.whinr110.seqn =  1
 	 	 var.whinr110.qstk =  tipcs021.stoc
 	 	 var.whinr110.qhnd =  tipcs021.stoc
		var.whinr110.shpm = ""
		var.whinr110.shpo = 0
		var.whinr110.rcno = ""				|#234040.sn
		var.whinr110.rcln = 0				|#234040.en
		var.whinr110.prnt = tcyesno.no
		var.whinr110.valm = 1
		var.whinr110.clot = ""
		var.whinr110.serl = ""				|#2043803_2.n
		var.whina112.clot = ""				|#2043803_2.n
		var.whina112.serl = ""				|#2043803_2.n

 	 	 dump 2
  	 	 var.whina112.qstk =  tipcs021.stoc
  	 	 var.whina112.qskt =  tipcs021.stoc

 	 	 dump 5
 	 	 get.customized.cost.price(tipcs021.cprj, tipcs021.item,
 	 	 	 	 	 	 tipcs021.stoc, true)

	endselect

}
							|#209255.en

								|#214852.sn
function determine.received.and.issued.due.to.stock.transfers(	|#1133346.sn
 	 	domain	tccprj		i.project,
 	 	domain	tcitem		i.item,
		domain	tccwar		i.warehouse,
 	ref	domain	tcqiv1		o.to.be.received,
 	ref	domain	tcqiv1		o.issued)
{
	domain	tcbool		skip.record

	o.to.be.received = 0.0
	o.issued = 0.0

	select	tdilc301.*
	from	tdilc301
	where	tdilc301.cprj = :i.project
	and	tdilc301.item = :i.item
	and	tdilc301.cwar = :i.warehouse
	and	tdilc301.koor = tckoor.wrh.order
	and	tdilc301.kost = tckost.stc.transfer
	order by tdilc301._index1 asc
	selectdo
		select	tdilc301.*
		from	tdilc301
		where	tdilc301._index1 = {	:tdilc301.cprj,
						:tdilc301.item,
						:tdilc301.cntr,
						:tdilc301.clot,
						:tdilc301.date,
						:tdilc301.cwar}
		and	tdilc301.orno = :tdilc301.orno
		and	tdilc301.sern <> :tdilc301.sern
		as set with 1 rows
		selectdo
			skip.record = true
		selectempty
			skip.record = false
		endselect

		if skip.record then
			continue
		endif

		if tdilc301.qstr > 0 then
			o.to.be.received = o.to.be.received + tdilc301.qstr
		else
			o.issued = o.issued - tdilc301.qstr
		endif
	endselect
}

function handle.inventory.transfers.customized.items(
				domain	tccprj		i.project,
				domain	tcitem		i.item,
					long		i.array.length,
			ref	domain	tccwar		i.warehouse.array())
{
		long		i
		double		to.be.received
		double		total.received
		double		total.issued
		double		quantity
	domain	tcbool		warehouse.already.used

	select	tdilc301.cprj,
		tdilc301.item,
		tdilc301.cwar,
		tipcs021.stoc
	from	tdilc301, tipcs021
	where	tdilc301._index1 = {	:i.project,
					:i.item}
	and	tdilc301.koor = tckoor.wrh.order
	and	tdilc301.kost = tckost.stc.transfer
	and	tipcs021._index1 = {	tdilc301.cprj, tdilc301.item}
	group by tdilc301.cprj, tdilc301.item, tdilc301.cwar, tipcs021.stoc
	selectdo
		warehouse.already.used = false
		for i = 1 to i.array.length
			if tdilc301.cwar = i.warehouse.array(1,i) then
				warehouse.already.used = true
				break
			endif
		endfor

		if warehouse.already.used then
			continue
		endif

		total.received = 0.0
		total.issued = 0.0

		select	tdilc301.qstk:quantity
		from	tdilc301
		where	tdilc301._index1 = {	:tdilc301.cprj,
						:tdilc301.item}
		and	tdilc301.koor = tckoor.wrh.order
		and	tdilc301.kost = tckost.stc.transfer
		and	tdilc301.cwar = :tdilc301.cwar
		selectdo
			if quantity > 0 then
				total.received = total.received + quantity
			else
				total.issued = total.issued + abs(quantity)
			endif
		endselect

		to.be.received = total.received - total.issued

		if to.be.received > 0.0 then
			itid =  itid +  1
			tipcs700.item = i.item
			tipcs700.logn = logname$
			tipcs700.pono = 10
			var.whinr110.cwar = tdilc301.cwar
 	 		var.whinr110.cprj = get.project(i.project)
 	 		var.whinr110.itid = edit$(itid,"999999999")
 	 		var.whinr110.itse = 1
 	 		var.whinr110.koor = 52 	 | *  adjustment
 	 		var.whinr110.kost = 1 	 | *  stock correction
 	 		var.whinr110.trdt = utc(date.to.num(1980,1,1), 0)
 	 		var.whinr110.orno = "MIGRATION"
 	 		var.whinr110.seqn = 1
 	 		var.whinr110.qstk = total.received - total.issued
 	 		var.whinr110.qhnd = total.received - total.issued
 		 	var.whinr110.rcno = ""
	 		var.whinr110.rcln = 0

 			dump 2		|#whinr110

			if tipcs021.stoc = 0.0 then		|#1466029.sn
				|* Totally consumed
				var.whina112.qskt = 0.0
			else
				if tipcs021.stoc <= to.be.received then
					|* Partially consumed, remaining qty
					|* can still be consumed
					var.whina112.qskt = tipcs021.stoc
				else
					|* Stock greater than to be received
					|* the to.be.received qty should still
					|* be consumed!
					var.whina112.qskt = to.be.received
				endif
			endif					|#1466029.en

								|#1466029.so
| 	 		if to.be.received >=  tipcs021.stoc then
| 	 	 		| *  totally consumed
| 	 	 		var.whina112.qskt =  0.0
| 	 		else
| 	 	 		if var.whina112.qskt >  0 then
| 	 	 	 	| *  not consumed
| 	 	 	 		var.whina112.qskt = to.be.received
| 	 	 		else
| 	 	 	 	| *  partially consumed
| 	 	 	 		var.whina112.qskt = tipcs021.stoc - to.be.received
| 	 	 		endif
| 	 		endif					|#1466029.eo

 	 		var.whina112.qstk =  to.be.received
			|* clot and serl are filled with ""	|#2043803_2.n
 	 		dump 5		|#whina112
		endif

	endselect
}								|#1133346.en

function fill.tipcs700.array(	domain	tccprj	i.project,
				domain	tcitem	i.item)
{
	long	i

	domain	tccwar	new.cwar

	i = 0

	set.mem(arr.tipcs700.cwar,"")
	set.mem(arr.tipcs700.trdt, 0)
	set.mem(arr.tipcs700.trtm, 0)
	set.mem(arr.tipcs700.sern, 0)
	set.mem(arr.tipcs700.proc, 0)

	select	tipcs700._index1, tipcs700.koor, tipcs700.kost, tipcs700.orno, tipcs700.pono
	from	tipcs700
	where	tipcs700._index1 = {:i.project, :i.item}
	order by tipcs700._index1
	selectdo
	    new.cwar = ""

	    if tdilc000.iilc = tcyesno.no then
									|#218014.sn
 	 	 	 select tipcs021.cwar:new.cwar
 	 	 	 from 	 tipcs021
 	 	 	 where 	 tipcs021._index1 =  {:i.project, :i.item}
 	 	 	 as set with 1 rows
 	 	 	 selectdo
 	 	 	 endselect
									|#218014.en

|		new.cwar = tipcs021.cwar				|#218014.o
	    else
		select	tdilc301.cwar:new.cwar
		from	tdilc301
		where	tdilc301._index1 = {:i.project, :i.item}
		and	tdilc301.koor = :tipcs700.koor
		and	tdilc301.kost = :tipcs700.kost
		and	tdilc301.orno = :tipcs700.orno
		and	tdilc301.pono = :tipcs700.pono
		as set with 1 rows
		selectdo
		selectempty
			on case tipcs700.koor
			case tckoor.act.sfc:
				if tipcs700.pono = 0 then
					select	tisfc001.cwar:new.cwar
					from	tisfc001
					where	tisfc001._index1 = {	:tipcs700.orno}
					as set with 1 rows
					selectdo
					endselect
				else
					select	ticst001.cwar:new.cwar
					from	ticst001
					where	ticst001._index1 = {	:tipcs700.orno,
									:tipcs700.pono}
					as set with 1 rows
					selectdo
					endselect
				endif
				break
			case tckoor.act.pur:
				select	tdpur041.cwar:new.cwar
				from	tdpur041
				where	tdpur041._index1 = {:tipcs700.orno, :tipcs700.pono}
				as set with 1 rows
				selectdo
				endselect
				break
			case tckoor.act.sls:
				select	tdsls041.cwar:new.cwar
				from	tdsls041
				where	tdsls041._index1 = {:tipcs700.orno, :tipcs700.pono}
				as set with 1 rows
				selectdo
				endselect
				break
			case tckoor.pcs.wrh:
				select	tipcs530.*
				from	tipcs530
				where	tipcs530._index4 = {:tipcs700.orno}
				as set with 1 rows
				selectdo
					on case tipcs530.kood
					case tckood.wp:
					case tckood.pw:
					case tckood.tp:
						new.cwar = tipcs530.cwar
						break
					case tckood.pp:
						if tipcs530.prjt = i.project and
						   tipcs530.itmt = i.item    then
							new.cwar = tipcs530.wart
						else
							new.cwar = tipcs530.cwar
						endif
						break
					endcase
				endselect
				break
			case tckoor.wrh.order:
				break
			case tckoor.sma.order:
				select	tssma310.cwar:new.cwar
				from	tssma310
				where	tssma310._index1 = {:tipcs700.orno, :tipcs700.pono}
				as set with 1 rows
				selectdo
				endselect
				break
			endcase
		endselect
	    endif

		i = i + 1
		if i > arr.len.tipcs700 then
			arr.len.tipcs700 = arr.len.tipcs700 + 50
			alloc.mem(arr.tipcs700.cwar, len(new.cwar), arr.len.tipcs700)
			alloc.mem(arr.tipcs700.trdt, arr.len.tipcs700)
			alloc.mem(arr.tipcs700.trtm, arr.len.tipcs700)
			alloc.mem(arr.tipcs700.sern, arr.len.tipcs700)
			alloc.mem(arr.tipcs700.proc, arr.len.tipcs700)
		endif

		arr.tipcs700.cwar(1,i) = new.cwar
		arr.tipcs700.trdt(i) = tipcs700.trdt
		arr.tipcs700.trtm(i) = tipcs700.trtm
		arr.tipcs700.sern(i) = tipcs700.sern
		arr.tipcs700.proc(i) = 0

	endselect
}
								|#214852.en

								|#218323.sn
function create.tdinv700.correction.record()
{
 	 receipt.counter =  receipt.counter +  1

 	 hold.koor =  var.whinr110.koor
 	 hold.kost =  var.whinr110.kost
 	 hold.orno =  var.whinr110.orno
 	 hold.trdt =  var.whinr110.trdt
 	 hold.seqn =  var.whinr110.seqn
 	 hold.qstk =  var.whinr110.qstk
 	 hold.bpid =  var.whinr110.bpid

 	 itid =  itid +  1
 	 var.whinr110.itid =  edit$(itid,"999999999")
 	 var.whinr110.itse =  1
 	 var.whinr110.koor =  52 	 | *  adjustment order
 	 var.whinr110.kost =  1 	 | *  stock correction
 	 var.whinr110.orno =  "MIGRATION"
 	 var.whinr110.trdt =  arr.whina112.trdt(1,dummy.array.nr)
 	 var.whinr110.seqn =  1
 	 var.whinr110.qstk =  dummy.receipt.qty
 	 var.whinr110.bpid =  empty()
 	 var.whinr110.qhnd =  dummy.receipt.qty

 	 if var.whinr110.valm =  5 or var.whinr110.valm =  6 then
 	 	 var.whina112.qstk =  dummy.receipt.qty
 	 	 receipt.counter =  receipt.counter +  1
 	 	 to.be.received =  to.be.received -  dummy.receipt.qty
 	 	 if to.be.received >=  onhand.inventory then
 	 	 	 | *  totally consumed
 	 	 	 dump 1
 	 	 	 var.whina112.qskt =  0.0
 	 	 	 dump 3
 	 	 	 if isspace(var.whinr110.clot) and
 	 	 	 isspace(var.whinr110.serl) then
				|* Exception for lot or serial priced item |#2043803_2.n
 	 	 	 	 get.standard.cost.price(
 	 	 	 	 	 	 tdinv700.item,
 	 	 	 	 	 	 var.whina112.qstk,
 	 	 	 	 	 	 hold.tdinv700.trdt,
 	 	 	 	 	 	 true,
 	 	 	 	 	 	 false)
 	 	 	 else
 	 	 	 	 if not isspace(var.whinr110.serl) then
 	 	 	 	 	 lot.serial =  var.whinr110.serl
 	 	 	 	 else
 	 	 	 	 	 lot.serial =  var.whinr110.clot
 	 	 	 	 endif
 	 	 	 	 get.lot.price( 	 tdinv700.item,
 	 	 	 	 	 	 lot.serial,
 	 	 	 	 	 	 var.whina112.qstk,
 	 	 	 	 	 	 hold.tdinv700.trdt,
 	 	 	 	 	 	 false,			|#FP5.n
 	 	 	 	 	 	 true,
 	 	 	 	 	 	 false)
 	 	 	 endif
 	 	 else
 	 	 	 split.up.lots.inventory()
 	 	 endif
 	 else
		|* var.whinr110.valm =  1			|# 2043803.n
 	 	 dump 1
 	 	 to.be.received =  to.be.received -  dummy.receipt.qty
 	 	 if to.be.received >=  onhand.inventory then
 	 	 	 | *  totally consumed
 	 	 	 var.whina112.qskt =  0.0
 	 	 else
 	 	 	 if var.whina112.qskt >  0 then
 	 	 	 	 | *  not consumed
 	 	 	 	 var.whina112.qskt =  dummy.receipt.qty
 	 	 	 else
 	 	 	 	 | *  partially consumed
 	 	 	 	 var.whina112.qskt =  onhand.inventory -  to.be.received
 	 	 	 endif
 	 	 endif
 	 	 var.whina112.qstk =  dummy.receipt.qty
 	 	 dump 3

 	 	 if not isspace(var.whinr110.serl) then 	 |serialized
 	 	 	 dump 14
 	 	 endif
| 	 	 if isspace(var.whinr110.clot) and		|#2043803_2.so
|	 	 isspace(var.whinr110.serl) then		|#2043803_2.eo
 	 	 	 get.standard.cost.price(
 	 	 	 	 	 	 tdinv700.item,
 	 	 	 	 	 	 dummy.receipt.qty,
 	 	 	 	 	 	 hold.tdinv700.trdt,
 	 	 	 	 	 	 true,
 	 	 	 	 	 	 false)
| 	 	 else						|#2043803_2.so
| 	 	 	 if not isspace(var.whinr110.serl) then
| 	 	 	 	 lot.serial =  var.whinr110.serl
| 	 	 	 else
| 	 	 	 	 lot.serial =  var.whinr110.clot
| 	 	 	 endif
| 	 	 	 get.lot.price( 	 tdinv700.item,
| 	 	 	 	 	 lot.serial,
| 	 	 	 	 	 dummy.receipt.qty,
| 	 	 	 	 	 hold.tdinv700.trdt,
| 	 	 	 	 	 false,			|#FP5.n
| 	 	 	 	 	 true,
| 	 	 	 	 	 false)
| 	 	 endif						|#2043803_2.eo
 	 endif
 	 var.whinr110.koor =  hold.koor
 	 var.whinr110.kost =  hold.kost
 	 var.whinr110.orno =  hold.orno
 	 var.whinr110.trdt =  hold.trdt
 	 var.whinr110.seqn =  hold.seqn
 	 var.whinr110.qstk =  hold.qstk
 	 var.whinr110.bpid =  hold.bpid
}

function create.tipcs700.correction.record()
{

 	 receipt.counter =  receipt.counter +  1

 	 hold.koor =  var.whinr110.koor
 	 hold.kost =  var.whinr110.kost
 	 hold.orno =  var.whinr110.orno
 	 hold.trdt =  var.whinr110.trdt
 	 hold.seqn =  var.whinr110.seqn
 	 hold.qstk =  var.whinr110.qstk

 	 itid =  itid +  1
 	 var.whinr110.itid =  edit$(itid,"999999999")
 	 var.whinr110.itse =  1
 	 var.whinr110.koor =  52 	 | *  adjustment
 	 var.whinr110.kost =  1 	 | *  stock correction
 	 var.whinr110.trdt =  arr.whina112.trdt(1,dummy.array.nr)
 	 var.whinr110.orno =  "MIGRATION"
 	 var.whinr110.seqn =  1
 	 var.whinr110.qstk =  dummy.receipt.qty
 	 var.whinr110.qhnd =  dummy.receipt.qty

 	 dump 2
 	 to.be.received =  to.be.received -  dummy.receipt.qty
 	 if to.be.received >=  onhand.inventory then
 	 	 | *  totally consumed
 	 	 var.whina112.qskt =  0.0
 	 else
 	 	 if var.whina112.qskt >  0 then
 	 	 	 | *  not consumed
 	 	 	 var.whina112.qskt =  dummy.receipt.qty
 	 	 else
 	 	 	 | *  partially consumed
 	 	 	 var.whina112.qskt =  onhand.inventory -  to.be.received
 	 	 endif
 	 endif
 	 var.whina112.qstk =  dummy.receipt.qty
 	 dump 5
 	 get.customized.cost.price(tipcs700.cprj, tipcs700.item,
 	 	 	 	 	 dummy.receipt.qty, true)
 	 var.whinr110.koor =  hold.koor
 	 var.whinr110.kost =  hold.kost
 	 var.whinr110.orno =  hold.orno
 	 var.whinr110.trdt =  hold.trdt
 	 var.whinr110.seqn =  hold.seqn
 	 var.whinr110.qstk =  hold.qstk
}

								|#218323.en
function void get.receipt.and.receipt.line(			|#1470346.sn
			domain	tcorno		i.order,
			domain	tcpono		i.position,
			domain	tcpono		i.sequence,
		ref		string		o.receipt,
		ref	domain	tcpono		o.receipt.line)
{
		long		no.records

	o.receipt = ""
	o.receipt.line = 0

	no.records = 0
	select	count(*):no.records
	from	bcswh010
	where	bcswh010._index1 = {	tckoor.act.pur,
					:i.order,
					:i.position,
					:i.sequence}
	as set with 1 rows
	selectdo
	endselect

	select	bcswh010.rcno:o.receipt,
		bcswh010.rcln:o.receipt.line
	from	bcswh010
	where	bcswh010._index1 = {	tckoor.act.pur,
					:i.order,
					:i.position,
					:i.sequence}
	as set with 1 rows
	selectdo
	endselect

	if no.records > 1 then
		o.receipt.line = 0
	endif
}								|#1470346.en

								|#234040.sn
function fill.pur.receipt.array()
{
	|* THIS FUNCTION SHOULD BE CALLED BEFORE MIGRATING TDINV700 and
	|* TIPCS700.
	|* This function fills two arrays:
	|* 1) an array of Order Numbers, Positions, Sequences, Receipt Numbers
	|* 2) an array of corresponding Receipt Lines
	|* Receipts are searched for all purchase orders in table Purchase
	|* Receipts (tdpur045), for which
	|* - Receipt Number is filled
	|* - Sequence Number is greater than zero.
	|* - Order Line Quantity > 0.0
	|* - Receipt Date > Item Historical Inv. Balance
	|* - Container is empty
	|* - Return Order is No
	|* - Direct Delivery is No
	|* - Warehouse Type is not Transito
	|* The array is needed in order to find the correct Receipt Line
	|* while executing function get.purchase.order.line.data(..)
	|* without having to do a count on tdpur045 for each record.

	domain tcpono	dummy.receipt.line
	domain	tcmcs.long	number.of.rows			|#249789.n

	|* Initialize array
								|#249789.sn
	|* For large tables the allocation of the array is not optimal, so
	|* first count the records of tdpur045 and allocate once.
	|* Note that not all conditions of the 'real select' are taken over
	|* for performance reasons
	select	count(*):number.of.rows
	from	tdpur045, tdinv001
	where	tdinv001._index1 = {tdpur045.cwar, tdpur045.item}
	and 	tdpur045.date >= tdinv001.hstd
	and	tdpur045.cntr =  ""
	and	tdpur045.reno > 0
	and	tdpur045.srnb > 0
	as set with 1 rows
	selectdo
	endselect

	if number.of.rows = 0 then				|#1465992.sn
		|* The number of rows is set to 1 if there are no tdpur045 records.
		|* The array still needs to be allocated.
		number.of.rows = 1
	endif							|#1465992.en
								|#249789.en

	 handle.pur.receipt.array(
				1,	|* allocate array
				0,	|* order
				0,	|* order line
				0,	|* sequence
				0,	|* receipt nr
				number.of.rows,			|#249789.n
				dummy.receipt.line)

	select	tdpur045.reno, tdpur045.orno, tdpur045.pono, tdpur045.srnb,
		tdpur045.date, tdpur045.cwar, tdpur045.item,
		tdpur040.cotp,
		tdpur041.oqua,
		tdinv001.hstd,
		tcmcs003.typw,
		tcmcs042.cotp, tcmcs042.drct, tcmcs042.reto
	from	tdpur045, tdpur040, tdpur041, tdinv001, tcmcs003, tcmcs042
	where	tdpur040._index1 = {tdpur045.orno}
	and	tdpur041._index1 = {tdpur045.orno, tdpur045.pono}
	and	tdinv001._index1 = {tdpur045.cwar, tdpur045.item}
	and 	tcmcs003._index1 = {tdpur045.cwar}
	and	tcmcs042._index1 = {tdpur040.cotp}
	and	tdpur041.oqua > 0.0		|* Skip negative qties
	and 	tdpur045.date >= tdinv001.hstd
	and	tdpur045.cntr =  ""
	and	tdpur045.reno > 0
	and	tdpur045.srnb > 0
	and 	tcmcs003.typw <>  tctypw.transito
	and 	tcmcs042.drct = tcyesno.no	|* Skip direct deliveries.
	and	tcmcs042.reto = tcyesno.no	|* Skip return orders
	order by tdpur045.reno, tdpur045.orno, tdpur045.pono,
		tdpur045.srnb, tdpur045.date asc
	selectdo
		 handle.pur.receipt.array(
		 			2,	|* Fill array
		 			tdpur045.orno,
		 			tdpur045.pono,
		 			tdpur045.srnb,
		 			tdpur045.reno,
					50,	|* allocation block |#249789.n
		 			dummy.receipt.line)
	endselect

}
								|#234040.en
								|#234040.sn
function long handle.pur.receipt.array(
			domain	tcmcs.long	i.action,
			domain	tcorno		i.orno,
			domain	tcpono		i.pono,
			domain	tcpono		i.srnb,
			domain	tcrcno		i.reno,
			domain	tcmcs.long	i.alloc.block,	|#249789.n
		ref	domain	tcpono		o.receipt.line)
{
	domain	tcmcs.long	index
	domain	tcmcs.str25	search.string

	static 	domain	tcorno	s.orno
	static	domain	tcpono	s.pono
	static	domain	tcpono	s.srnb
	static	domain	tcrcno	s.reno
	static	domain	tcpono	s.receipt.line

	#define	DEFAULT.STEP.SIZE	10

	#define	COMPOSE.SEARCH.STRING
^		search.string(orno.pos) = str$(i.orno)
^		search.string(pono.pos) = str$(i.pono)
^		search.string(srnb.pos) = str$(i.srnb)
^		search.string(reno.pos) = str$(i.reno)

	|* fixed nr of 50 replaced by i.alloc.block		|#249789.n
	#define	ALLOCATE.ARRAY.MEM
^		alloc.counter = alloc.counter + 1
^		if alloc.counter > alloc.elements then
^			alloc.elements = alloc.elements + i.alloc.block
^			alloc.mem(pur.receipt.arr, array.length, alloc.elements)
^			alloc.mem(receipt.line.arr, alloc.elements)
^		endif

	on case i.action
	case	1:	|* Initialize
		s.orno = 0
		s.pono = 0
		s.srnb = 0
		s.reno = 0
		s.receipt.line = 0

		if not lengths.set then
			array.length = icom0015.domain.length("tcmcs.str25")
			orno.length = icom0015.domain.length("tcorno")
			pono.length = icom0015.domain.length("tcpono")
			srnb.length = icom0015.domain.length("tcpono")
			reno.length = icom0015.domain.length("tcrcno")

			|* Determine search string layout (start/end positions
			|* of array elements
			orno.pos = 1
			pono.pos = orno.pos + orno.length + 1
			srnb.pos = pono.pos + pono.length + 1
			reno.pos = srnb.pos + srnb.length + 1

			lengths.set = true
		endif

		free.mem.pur.receipt.array()

								|#249789.so
|		alloc.elements = 50	|* Allocate blocks of 50 elements
								|#249789.eo
		alloc.elements = i.alloc.block			|#249789.n
		alloc.counter = 0

		alloc.mem(pur.receipt.arr, array.length, alloc.elements)
		alloc.mem(receipt.line.arr, alloc.elements)

		set.mem(pur.receipt.arr, "")
		set.mem(receipt.line.arr, 0)

		init.search.definition()

		break
	case	2:	|* Fill Array

		COMPOSE.SEARCH.STRING

		if i.reno <> s.reno then
			|* Add to array
			|* First Receipt Line = 10
			ALLOCATE.ARRAY.MEM
			pur.receipt.arr(1, alloc.counter) = search.string
			receipt.line.arr(alloc.counter) = DEFAULT.STEP.SIZE
		else
			if not (i.orno = s.orno and
				i.pono = s.pono and
				i.srnb = s.srnb) then
				|* Same Receipt Number, but Order, Position
				|* and/or Sequence is different.
				|* Add receipt line to array
				|* Increment Receipt Line + 10
				ALLOCATE.ARRAY.MEM
				pur.receipt.arr(1,alloc.counter) = search.string
				receipt.line.arr(alloc.counter) =
					s.receipt.line + DEFAULT.STEP.SIZE
			else
				|* Do nothing
			endif
		endif

		s.orno = i.orno
		s.pono = i.pono
		s.srnb = i.srnb
		s.reno = i.reno
		s.receipt.line = receipt.line.arr(alloc.counter)

		break
	case	3:	|* Search Array

		COMPOSE.SEARCH.STRING

		|* look for search string (=Receipt Number) in array
		index = qss.search(
				   QSS.EQUAL,
				   search.string,
				   pur.receipt.arr,
				   search_def)

		if index < 0 then
			|* Receipt not found in array
			|* Add receipt line to array

			ALLOCATE.ARRAY.MEM

			pur.receipt.arr(1, alloc.counter) = search.string
			receipt.line.arr(alloc.counter) = DEFAULT.STEP.SIZE
			s.receipt.line = receipt.line.arr(alloc.counter)
		else
			|* Receipt Found in array
			|* Determine Output Receipt Line
			s.receipt.line = receipt.line.arr(index)
		endif

		break
	endcase

	o.receipt.line = s.receipt.line

	return(0)
}
								|#234040.en
								|#234040.sn
function init.search.definition()
{
	static	long	init.search.def
		long	ret

	if not init.search.def then

		|* Search Definition for complete search string
		qss.start( search_def, 1, 1)
		qss.type(  search_def, 1, DB.STRING)
		qss.length(search_def, 1, array.length)

		init.search.def = 1
	endif
}
								|#234040.en
								|#234040.sn
function free.mem.pur.receipt.array()
{
	free.mem(pur.receipt.arr)
	free.mem(receipt.line.arr)
}
								|#234040.en

function determine.transaction.type.and.quantity(		|#2043803.sn
		domain	tckoor		i.kind.of.order,
		domain	tccprj		i.project,
		domain	tcqiv1		i.inv.quantity,
		domain	tckost		i.inv.transaction.type,
	ref	domain	tcqiv1		o.ilc.quantity,
	ref	domain	tckost		o.ilc.transaction.type)
{
	o.ilc.quantity = i.inv.quantity
	o.ilc.transaction.type = i.inv.transaction.type

	if i.kind.of.order = tckoor.act.sfc and
	   not isspace(i.project) and
	   i.inv.transaction.type = tckost.stc.transfer then
		o.ilc.quantity = abs(i.inv.quantity)
		o.ilc.transaction.type = tckost.prd.mat.issue
	endif
}								|#2043803.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:wh
:TCMOD:ltc
:TFLNO:100
:FUNC:START
function domain tcbool serial.on.other.location()
{
	domain	tdltc.clot	lot.code

 	 select 	tdilc101.clot:lot.code   
 	 from 	tdilc101  
 	 where 	tdilc101._index3 =  {	:tdilc101.cprj,  
 	  	 	 	 	:tdilc101.item,  
 	  	 	 	 	:tdilc101.cntr,  
 	  	 	 	 	:tdilc101.clot,
					:tdilc101.date,
					:tdilc101.stun,
					:tdilc101.cwar}
	and	tdilc101.loca <> :tdilc101.loca  
 	and 	tdilc101.strs >  0.0  
	as set with 1 rows
 	selectdo
		return(true)
	endselect

	return(false)
}

function	void determine.blocked.quantity(	
			domain	tccwar		i.warehouse,
			domain	tdilc.loca	i.location,
			domain	tccprj		i.project,
			domain	tcitem		i.item,
			domain	tdltc.clot	i.lot,
			domain	tcdate		i.inventory.date,
			domain	tccuni		i.unit,
			domain	tcqst1		i.blocked.quantity,
			domain	tcqst1		i.allocated.quantity,
		ref	domain	tcqst1		o.process.blocked,
		ref	domain	tcqst1		o.manual.blocked)
{
	domain	tcqst1	approved.quantity

	o.process.blocked = 0.0
	o.manual.blocked = 0.0
	if i.blocked.quantity = 0.0 then
		return
	endif
	if lot.is.blocked(	i.project,
			i.item,
			"",		|* Container
			i.lot) then
		o.process.blocked = 0.0
		o.manual.blocked = i.blocked.quantity
		return
	endif

	if tdilc000.iilc = tcyesno.yes then 
		if location.is.blocked(	
				i.warehouse,
				i.location) then
			o.process.blocked = 0.0
			o.manual.blocked = i.blocked.quantity
			return
		endif
		if stock.point.is.blocked(	
				i.warehouse,
				i.location,
				i.project,
				i.item,
				"",		|* Container
				i.lot,
				i.inventory.date,
				i.unit) then
			o.process.blocked = 0.0
			o.manual.blocked = i.blocked.quantity
			return
		endif
	endif
	if inspection.location(	i.warehouse,
				i.location) then
		o.process.blocked = i.blocked.quantity - i.allocated.quantity
		if o.process.blocked > 0 then
			get.approved.quantity(	i.warehouse,
						i.location,
						i.project,
						i.item,
						"",		|* container
						i.lot,
						i.inventory.date,
						i.unit,
						approved.quantity)
			o.process.blocked = i.blocked.quantity - approved.quantity
		endif
		if o.process.blocked < 0 then
			o.process.blocked = 0
		endif
	else
		o.process.blocked = i.blocked.quantity
	endif
	o.manual.blocked = 0.0
}

function domain tcbool lot.is.blocked(	
			domain	tccprj		i.project,
			domain	tcitem		i.item,
			domain	tccuni		i.container,
			domain	tdltc.clot	i.lot)
{
	select	tdltc003.*
	from	tdltc003
	where	tdltc003._index1 = {	:i.project,
					:i.item,
					:i.container,
					:i.lot}
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function domain tcbool location.is.blocked(	
			domain	tccwar		i.warehouse,
			domain	tdilc.loca	i.location)
{
	select	tdilc009.*
	from	tdilc009
	where	tdilc009._index1 = {	:i.warehouse,
					:i.location}
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function domain tcbool stock.point.is.blocked(	
			domain	tccwar		i.warehouse,
			domain	tdilc.loca	i.location,
			domain	tccprj		i.project,
			domain	tcitem		i.item,
			domain	tccuni		i.container,
			domain	tdltc.clot	i.lot,
			domain	tcdate		i.inventory.date,
			domain	tccuni		i.unit)
{
	select	tdilc104.*
	from	tdilc104
	where	tdilc104._index1 = {	:i.warehouse,
					:i.location,
					:i.project,
					:i.item,
					:i.container,
					:i.lot,
					:i.inventory.date,
					:i.unit}
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function domain tcbool inspection.location(	
			domain	tccwar		i.warehouse,
			domain	tdilc.loca	i.location)
{
	domain	tdilc.loca	insp.location

	select	tdilc001.loca:insp.location
	from	tdilc001
	where	tdilc001._index1 = {	:i.warehouse,
					:i.location}
	and	tdilc001.aplo = tcyesno.yes
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function void get.approved.quantity(	
			domain	tccwar		i.warehouse,
			domain	tdilc.loca	i.location,
			domain	tccprj		i.project,
			domain	tcitem		i.item,
			domain	tccuni		i.container,
			domain	tdltc.clot	i.lot,
			domain	tcdate		i.inventory.date,
			domain	tccuni		i.unit,
		ref	domain	tcqst1		o.approved.quantity)
{
	o.approved.quantity = 0.0

	select	sum(tdilc111.stra):o.approved.quantity
	from	tdilc111
	where	tdilc111._index2 = {	:i.warehouse,
					:i.location,
					:i.project,
					:i.item,
					:i.container,
					:i.lot,
					:i.unit,
					:i.inventory.date}
	as set with 1 rows
	selectdo
	endselect
}

function domain	tcbool fifo.lifo.item(
			domain	tccprj	i.project,
			domain	tcitem	i.item)
{
	domain	tcobpr	outbound.priority

	if isspace(i.project) then
		select  	tiitm001.obpr:outbound.priority  
	  	from  	tiitm001  
 	  	where  	tiitm001._index1 =  {:i.item}  
		and	tiitm001.obpr = tcobpr.loca
 	  	as set with 1 rows  
	  	selectdo
			return(false)  
 	  	endselect
	else
		select  	tipcs021.obpr:outbound.priority  
	  	from  	tipcs021  
 	  	where  	tipcs021._index1 =  {	:i.project,
						:i.item}  
		and	tipcs021.obpr = tcobpr.loca
 	  	as set with 1 rows  
	  	selectdo  
			return(false)
 	  	endselect
	endif

	return(true)  
}

function domain tcbool item.revision.is.valid(
		domain	tcitem		i.item,
		domain	tiedm.revi	i.revision)
{
	select 	 tiedm100.*
	from 	 tiedm100
	where 	 tiedm100._index1 = {	:i.item,
					:i.revision}          
 	and 	 tiedm100.rele =  tiedm.rele.yes
	as set with 1 rows
	selectdo
 	 	 return(true)
 	 endselect

	return(false)
}

function	determine.location.allocated.quantity(
			domain	tccwar		i.warehouse,
			domain	tccprj		i.project,
			domain	tcitem		i.item,
			domain	tdltc.clot	i.lot,
		ref	domain	tcqiv1		o.location.allocated.quantity)
{
	|* PERF
	|       replace old query with full table scan by a query which is only once executed
	|      the warehouse + items + lot + sum(tdilc401,qsts) with proc=no are stored in a memory array
	|      it is expected that  there is only a small percentage of records in tdilc401 with proc=no

	static	domain	tcitem	cwar_item_lot_array(1) based	| item array
	static	domain	tcqiv1	qsts_array(1) based		| sum(tdilc401.qsts) array
	
	static	long 	array.size		| current size of the array
	static	long 	array.index		| current index in the array
		long	result			| result of search
	static	long	table.read		| static for table tdicl401 has been read
	long	search_def(1,4)			| qss search definition
	long	length.of.item
	long	length.of.lot
	long	length.of.warehouse	
	long 	dummy.long

	if not table.read then
		if length.of.item = 0 then
			rdi.domain.string("tcitem", length.of.item, dummy.long)
			rdi.domain.string("tdltc.clot", length.of.lot, dummy.long)		
			rdi.domain.string("tccwar",length.of.warehouse, dummy.long)
		endif
		| clean array
		if array.index > 0 then
			set.mem(cwar_item_lot_array,"")
			array.index = 0
		endif

		| fill array for new warehouse
		select	sum(tdilc401.qsts):o.location.allocated.quantity,tdilc401.cwar,tdilc401.item,tdilc401.clot   
		from	tdilc401  
		where	tdilc401.proc =  tcyesno.no
		and 	tdilc401.qsts >  0.0  
		group by tdilc401.cwar,tdilc401.item,tdilc401.clot
		selectdo
			| fill array 
			array.index = array.index + 1
			if array.index > array.size then
				| alloc (additional) memory
				array.size = array.size + 100
				alloc.mem(cwar_item_lot_array,length.of.warehouse+length.of.item+length.of.lot, array.size)	
				alloc.mem(qsts_array, array.size)
			endif
			| store item, lot and sum(tdilc401.qsts)
			cwar_item_lot_array(1,array.index) = tdilc401.cwar
			cwar_item_lot_array(1+length.of.warehouse,array.index) = tdilc401.item
			cwar_item_lot_array(1+length.of.warehouse+length.of.item,array.index) = tdilc401.clot
			qsts_array(array.index) = o.location.allocated.quantity			
		selectempty
			| no values found with delivered = no
		endselect
		table.read = true
	endif

	| search array for values
	if array.index = 0 then
		o.location.allocated.quantity = 0.0
	else
		qss.start(search_def,1,1)
		qss.way(search_def,1,QSS.UP)
		qss.type(search_def,1,DB.STRING)
		qss.length(search_def,1,length.of.warehouse+length.of.item+length.of.lot)
		result = qss.search(QSS.EQUAL,i.warehouse&i.item&i.lot,cwar_item_lot_array,search_def,array.index)
		if result > 0 then
			o.location.allocated.quantity = qsts_array(result)
		else
			o.location.allocated.quantity = 0.0
		endif
	endif

|	o.location.allocated.quantity = 0.0	
|	select	sum(tdilc401.qsts):o.location.allocated.quantity   
|	from	tdilc401  
|	where	tdilc401.proc =  tcyesno.no
|	and	tdilc401.cwar = :i.warehouse  
|	and 	tdilc401.item = :i.item
|	and	tdilc401.clot = :i.lot                                
|	and 	tdilc401.qsts >  0.0  
|	as set with 1 rows
|	selectdo  
|	endselect
}

function dump.cost.details()
{
	if not isspace(var.whltc101.cpcp) then
		Dump 2
	endif
}  

|							|#245164.sn 	|#1376637.so
|function get.item.default.warehouse(domain	tcitem	i.item)
|{
|	static	domain	tcitem		s.item	
|	static	domain	tccwar		s.cwar
|	
|	if s.item = i.item then	
|		var.whltc500.cwar = s.cwar
|		return
|	else
|		s.item = i.item
|		var.whltc500.cwar = ""
|	endif
|
|	|# get the warehouse of the item.
|	select 	tiitm001.cwar:var.whltc500.cwar
|	from	tiitm001
|	where	tiitm001.item = :i.item
|	as set with 1 rows
|	selectdo
|	endselect
|	
|	s.cwar = var.whltc500.cwar
|	return
|}
|							|#245164.en	|#1376637.eo
									|#1376637.sn
function get.item.default.warehouse(
			domain	tccprj	i.project,
			domain	tcitem	i.item)
{
	static	domain	tccprj		s.project
	static	domain	tcitem		s.item	
	static	domain	tccwar		s.cwar
	
	if s.project = i.project and
	   s.item = i.item then	
		|* Same project/item then return static
		var.whltc500.cwar = s.cwar
		return
	else
		s.project = i.project
		s.item = i.item
		var.whltc500.cwar = ""
	endif

	|* retrieve the warehouse for customized item from tipcs021.
	|* Warehouse is a mandatory field in the table.
	if not isspace(i.project) then
		select	tipcs021.cwar:var.whltc500.cwar
		from	tipcs021
		where	tipcs021._index1 = {:i.project,:i.item}
		as set with 1 rows
		selectdo
		endselect

		s.cwar = var.whltc500.cwar
		return
	endif

	|# get the warehouse of the item.
	select 	tiitm001.cwar:var.whltc500.cwar
	from	tiitm001
	where	tiitm001.item = :i.item
	as set with 1 rows
	selectdo
	endselect
	
	s.cwar = var.whltc500.cwar
	return
}									|#1376637.en

									 |#1858553.sn    	  
function set.serial.ownership(
			domain	tcqiv1	i.serial.on.hand.quantity)  	
{
	if i.serial.on.hand.quantity = 0.0 then
		|* No serial inventory
		var.whltc500.owns = target(tcowns.not.appl)
	else
		|* Serial inventory present
		var.whltc500.owns = target(tcowns.comp.owned)
	endif
}
									|#1858553.en


|********************
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:61
:VERS:B4ct
:RELE:61
:CUST:f106
:TCPAC:wh
:TCMOD:wmd
:TFLNO:215
:FUNC:START
function migrate.distribution.item.data.by.warehouse()
{
 	| *  only items of item type purchased or manufactured are converted.
 	| *  item type (project) should not be equipment or subcontracting.
 	| *  so only type (project) item is dumped.

	fill.array.for.location.allocated.quantity.item()

	select 	 tdinv001.* , tcmcs003.sloc,
	 	 tiitm001.item, tiitm001.kitm, tiitm001.ltcp,
		 tiitm001.stva,
		 tiitm001.cont,					|#FP9.n
		 tiitm001.sftm,  tiitm001.abcc,  tiitm001.itmt
	from 	 tdinv001,
	 	 tcmcs003,
	 	 tiitm001
	where 	 tdinv001.cwar > ""
	 and 	 tdinv001.cntr = ""
	 and 	 tcmcs003._index1 = {tdinv001.cwar}
	 and 	 tcmcs003.typw <> tctypw.transito
	 and 	 tiitm001._index1 = {tdinv001.item}
	 and 	 (tiitm001.kitm = tckitm.purchase or
	 	 tiitm001.kitm = tckitm.manufacture )
	 and 	 tiitm001.itmt = tppdm.itmt.item
	order by tdinv001._index1
	selectdo
		if tiitm001.cont = tcyesno.yes then		|#FP9.sn
			log.error(sprintf$(
				"Records Skipped for: item %s, warehouse %s." &
				" Reason: container items not migrated. ",
				tdinv001.item,
				tdinv001.cwar))
			continue
		endif						|#FP9.en

		create.item.by.warehouse()
		create.inventory()
	 	create.inventory.cost.details()
	endselect
}

function create.item.by.warehouse()
{
|	if (tdinv001.maxs >  99999999) then  			|#1356322.so
|	 	 tdinv001.maxs =  99999999
| 	endif  							|#1356322.eo
 	if (tdinv001.maxs > 9999999.99) then			|#1356322.sn
	 	var.whwmd210.maxs =  9999999.99
	else
		var.whwmd210.maxs =  tdinv001.maxs
	endif  							|#1356322.en
 	var.whwmd210.item =  segment.item.code( 	 tdinv001.item,
 		 	 	 	 	 	 "")
 	if tdinv001.oqmf >  0 then
 		 var.whwmd210.oqmf =  tdinv001.oqmf
 	else
 		 var.whwmd210.oqmf =  1.0
 	endif

	default.location.type()

 	var.whwmd210.sftm =  tiitm001.sftm
 	var.whwmd210.abcc =  tiitm001.abcc

	if tiitm001.stva = tcstva.lot.price then
		if lot.controlled.item("", tdinv001.item) then
			var.whwmd210.valm = 5	|* target lot price
		else
			var.whwmd210.valm = 1	|* target FTP
		endif
	else
		var.whwmd210.valm = 1	|* target FTP
	endif

	dump 210  |*whwmd210
}

function default.location.type()
{
 	 | *  determine default location type for inbound advice.
 	 if tcmcs003.sloc =  tcyesno.yes then
 	 	 var.whwmd210.ialt =  5 | *  whwmd.loct.bulk
 	 else
 	 	 var.whwmd210.ialt =  20 | *  whwmd.loct.not.appl
 	 endif
}

function create.inventory()
{
	var.whwmd215.cwar = tdinv001.cwar
	var.whwmd215.item = segment.item.code(
					tdinv001.item,
					"")	|* Project
	if is.masterdata.conversion() then
		init.inventory.fields()
	else
		var.whwmd215.qhnd = tdinv001.stoc
		var.whwmd215.qchd = 0.0
		set.quantities.blocked(
				tdinv001.cwar,
				tdinv001.item,
				"",
				tdinv001.blck,
				tdinv001.qbsi)
		var.whwmd215.qord = tdinv001.ordr
		var.whwmd215.qcor = 0.0
		var.whwmd215.qint = 0.0
		set.quantities.allocated.and.onorder()
		var.whwmd215.qcal = 0.0
		var.whwmd215.qcom = tdinv001.hall
		if tdilc000.iilc = tcyesno.yes then
			determine.location.allocated.quantity(
					tdinv001.cwar,
					tdinv001.item,
					var.whwmd215.qlal)
		else
			var.whwmd215.qlal = 0.0
		endif
		var.whwmd215.qcpr = 0.0
		var.whwmd215.ltdt = utc(tdinv001.ltdt, 0)
		var.whwmd215.hstd = utc(0,0)
		var.whwmd215.lsid = utc(tdinv001.lsid, 0)
		var.whwmd215.qcis = tdinv001.uscu
		var.whwmd215.qhib = 0.0
	endif

	dump 215	| * whwmd215 - Item Inventory by Warehouse
	dump 216	| * whwmd216 - Inventory by Warehouse, Item and Eff.Unit
								|#FP8.sn
	dump 260	| * whwmd260 - Project Pegged Inventory
								|#FP8.en
	handle.insert.inventory.by.specification()		|#1922484.n
}

function handle.insert.inventory.by.specification()		|#1922484.sn
{
	if var.whwmd215.qhnd <> 0.0 or
	   var.whwmd215.qblk <> 0.0 or
	   var.whwmd215.qord <> 0.0 or
	   var.whwmd215.qall <> 0.0 or
	   var.whwmd215.qlal <> 0.0 then
		dump 219	| * whwmd219 - Inventory by Specification
	endif
}								|#1922484.en

function init.inventory.fields()
{
	var.whwmd215.qhnd = 0.0
	var.whwmd215.qchd = 0.0
	var.whwmd215.qblk = 0.0
|	var.whwmd215.qbpl = 0.0		|#1425776.o
	var.whwmd215.qord = 0.0
	var.whwmd215.qcor = 0.0
	var.whwmd215.qint = 0.0
	var.whwmd215.qall = 0.0
	var.whwmd215.qcal = 0.0
	var.whwmd215.qcom = 0.0
	var.whwmd215.qlal = 0.0
	var.whwmd215.qcpr = 0.0
	var.whwmd215.ltdt = utc(0,0)
	var.whwmd215.hstd = utc(0,0)
	var.whwmd215.lsid = utc(0,0)
	var.whwmd215.qcis = 0.0
	var.whwmd215.qhib = 0.0

	var.whwmd260.qprb = 0.0		|#fp8.sn
|	var.whwmd260.qppl = 0.0		|#1425776.o
	var.whwmd260.qblk = 0.0
|	var.whwmd260.qbpl = 0.0		|#fp8.en	|#1425776.o
}

function set.quantities.blocked(
	domain	tccwar	i.warehouse,
	domain	tcitem	i.item,
	domain	tccprj	i.project,
	double		i.qty.blocked,
	double		i.qty.blocked.for.inspection)
{
	var.whwmd215.qblk = i.qty.blocked + i.qty.blocked.for.inspection
	if tdilc000.iilc = tcyesno.yes and
	   var.whwmd215.qblk > 0.0 then
		determine.blocked.quantity.decrease(
			i.warehouse,
			i.project,
			i.item,
			blocked.quantity.decrease)
		if blocked.quantity.decrease > 0.0 then
			var.whwmd215.qblk = var.whwmd215.qblk -
						blocked.quantity.decrease
			if var.whwmd215.qblk < 0.0 then
				var.whwmd215.qblk = 0.0
			endif
		endif
	endif
|	var.whwmd215.qbpl = var.whwmd215.qblk			|#1425776.o
	calculate.manual.blocked.quantity(			|#1425776.sn
				i.warehouse,
				i.item,
				i.project,
				var.whwmd260.qblk)

	if double.cmp(var.whwmd260.qblk, 0.0, 0.000001) = 0 then
		|* If no manual blocked qty then blocked qty is process blocked
		var.whwmd260.qprb = var.whwmd215.qblk
	else
		|* Manual blockings present. The process blocked cannot be
		|* easily determined. Here the best guess is done:
		|* Process blocked = total (wmd215) blocked - manual blocked
		var.whwmd260.qprb = var.whwmd215.qblk - var.whwmd260.qblk
	endif

	|* Blocked for Planning
	|* Note that the blocked for planning quantities are always zero.
	|* Because for manual blockings the reason code is migrated with reason
	|* type 'Inventory Blocking excluding EP'. For process blocked, the
	|* parameter 'Inspection Blocks Enterprise Planning' and 'Disposition
	|* Inventory Blocked for Planning' are migrated with value 'no'.
								|#1425776.en
}

function void calculate.manual.blocked.quantity(		|#1425776.sn
		domain	tccwar		i.warehouse,
		domain	tcitem		i.item,
		domain	tccprj		i.project,
	ref	domain	tcqiv1		o.manual.blocked.quantity)
{
	o.manual.blocked.quantity = 0.0

	select	sum(tdilc101.stkb):o.manual.blocked.quantity
	from	tdilc101
	where	tdilc101._index4 = {	:i.warehouse,
					:i.project,
					:i.item}
	and	(exists(
			select	tdilc104.*	|* blocked by stock point
			from	tdilc104
			where	tdilc104._index1 = {
					tdilc101.cwar,
					tdilc101.loca,
					tdilc101.cprj,
					tdilc101.item,
					tdilc101.cntr,
					tdilc101.clot,
					tdilc101.date,
					tdilc101.stun})
		or exists(
			select	tdilc009.*	|* blocked by location
			from	tdilc009
			where	tdilc009._index1 = {
					tdilc101.cwar,
					tdilc101.loca})
		or exists(
			select	tdltc003.*	|* blocked by lot
			from	tdltc003
			where	tdltc003._index1 = {
					tdilc101.cprj,
					tdilc101.item,
					tdilc101.cntr,
					tdilc101.clot}))
	as set with 1 rows
	selectdo
	endselect
}								|#1425776.en

function set.quantities.allocated.and.onorder()
{
	if tipcs000.ipcs = tcyesno.yes then
		| * PCS implemented
		var.whwmd215.qall =
			determine.prp.inv.correction.for.standard.item(
					tdinv001.item,
					tdinv001.cwar,
					tdinv001.allo)
	else
		var.whwmd215.qall = tdinv001.allo
	endif

	| * Purhase return order in Baan4 creates a negative on order quantity,
	| * In Baan6 it must be a positive allocated quantity.
	| * Sales return order in Baan4 creates a negative allocated quantity,
	| * In Baan6 it must be a positive on order quantity.

	move.to.allocated = 0.0
	move.to.onorder = 0.0
	if var.whwmd215.qord < 0.0 then
		move.to.allocated = abs(var.whwmd215.qord)
	endif
	if var.whwmd215.qall < 0.0 then
		move.to.onorder = abs(var.whwmd215.qall)
	endif
	if move.to.allocated +  move.to.onorder > 0.0 and
	  move.to.allocated <> move.to.onorder then
		if move.to.allocated > move.to.onorder then
			move.to.allocated = move.to.allocated - move.to.onorder
			var.whwmd215.qord = 0.0
			var.whwmd215.qall = var.whwmd215.qall + move.to.allocated
		else
			move.to.onorder = move.to.onorder - move.to.allocated
			var.whwmd215.qall = 0.0
			var.whwmd215.qord = var.whwmd215.qord + move.to.onorder
		endif
	endif
}

function create.inventory.cost.details()
{
	date.last.cost.price.change = tiitm001.ltcp
	| * Initialize variables.
	initialize.ftpa.amounts()
	| * Read standard cost price table.
	select	ticpr300.*
	from	ticpr300
	where	ticpr300._index1 = {:tdinv001.item}
	and	ticpr300.indt = :date.last.cost.price.change
	selectdo
		for i = 1 to g.mc.depth
			amount.per.cc(i) = get.amount.per.cost.component(
						"ticpr300",
						i)
		endfor
		create.detailed.inventory.cost.details(
					tdinv001.item,
					"",	|* project
					ticpr300.cpcp,
					amount.per.cc)
	selectempty
		| * No cost components, use aggregated components with ftp = 0.
		material.component = bcstc000.matc
		operation.component = bcstc000.oprc
		surcharge.component = bcstc000.surc
		initialize.ftpa.amounts()
	endselect

	create.remainder.inventory.cost.details()
}

function domain tcqiv1 determine.prp.inv.correction.for.standard.item(
			domain	tcitem	i.item,
			domain	tccwar	i.warehouse,
			domain	tcqiv1	i.old.allo.quantity)
{
	|FunctionUsage
	|Expl:	In Baan IV when a planned PRP production order has a standard
	|	item as material, an inventory transaction is logged in tdinv150
	|	with Order Type = PRP Production Order and Transaction
	|	Type = - (negative). This leads to an increase of the Allocated
	|	Inventory in tiitm001. But in Baan 6 inventory transactions
	|	for planned production orders are not logged in whinp100, but
	|	only in cprrp010. These transactions are not reflected in the
	|	inventory data in tcibd100 and whwmd215. This means that the Allocated
	|	Inventory in tiitm001 needs to be adjusted for these type
	|	of transactions.
	|EndFunctionUsage

	domain	tcqiv1	prp.allocated.inventory

	prp.allocated.inventory = 0.0

	select	sum(tdinv150.qana):prp.allocated.inventory
	from	tdinv150
	where	tdinv150._index4 = {	:i.item,
					"",
					:i.warehouse}
	and	tdinv150.koor = tckoor.pcs.sfc
	and	tdinv150.kotr = tckotr.requirement
	as set with 1 rows
	selectdo
	endselect

	return(i.old.allo.quantity - prp.allocated.inventory)
}

function initialize.ftpa.amounts()
{
	e = set.mem(material.component.ftpa, 0)
	e = set.mem(operation.component.ftpa, 0)
	e = set.mem(surcharge.component.ftpa, 0)
}

function void	determine.blocked.quantity.decrease(
			domain	tccwar	i.warehouse,
			domain	tccprj	i.project,
			domain	tcitem	i.item,
		ref	domain	tcqiv1	o.blocked.quantity.decrease)
{
	domain	tdilc.loca	insp.location
	domain	tcqiv1		process.blocked.quantity
	domain	tcqiv1		approved.quantity

	o.blocked.quantity.decrease = 0.0
	select	tdilc101.*,
		tdilc001.loca:insp.location
	from	tdilc101, tdilc001
	where	tdilc101._index4 = {	:i.warehouse,
					:i.project,
					:i.item}
	and	tdilc101.cntr = ""
	and	tdilc101.loca <> ""
	and	tdilc101.stkb > 0.0
	and	tdilc001._index1 = {	tdilc101.cwar,
					tdilc101.loca}
	and	tdilc001.aplo = tcyesno.yes
	selectdo
		process.blocked.quantity = tdilc101.stkb - tdilc101.stka
		if process.blocked.quantity > 0 then
			get.approved.quantity(	tdilc101.cwar,
						tdilc101.loca,
						tdilc101.cprj,
						tdilc101.item,
						"",		|* container
						tdilc101.clot,
						tdilc101.date,
						tdilc101.stun,
						approved.quantity)
			process.blocked.quantity = tdilc101.stkb - approved.quantity
		endif
		if process.blocked.quantity < 0 then
			process.blocked.quantity = 0
		endif
		o.blocked.quantity.decrease = o.blocked.quantity.decrease +
				(tdilc101.stkb - process.blocked.quantity)
	endselect
}

function void get.approved.quantity(
			domain	tccwar		i.warehouse,
			domain	tdilc.loca	i.location,
			domain	tccprj		i.project,
			domain	tcitem		i.item,
			domain	tccuni		i.container,
			domain	tdltc.clot	i.lot,
			domain	tcdate		i.inventory.date,
			domain	tccuni		i.unit,
		ref	domain	tcqst1		o.approved.quantity)
{
	o.approved.quantity = 0.0

	select	sum(tdilc111.stra):o.approved.quantity
	from	tdilc111
	where	tdilc111._index2 = {	:i.warehouse,
					:i.location,
					:i.project,
					:i.item,
					:i.container,
					:i.lot,
					:i.unit,
					:i.inventory.date}
	as set with 1 rows
	selectdo
	endselect
}

function determine.location.allocated.quantity(
			domain	tccwar	i.warehouse	fixed,
			domain	tcitem	i.item		fixed,
		ref	domain	tcqiv1	o.location.allocated.quantity)
{
	|* PERF
	|    replace old query by query which is only once executed for each warehouse
	|	the items + sum(tdilc401,qsts) are stored in a memory array
	|	function must be called with input sorted index1 of tdinv001 (= sorted on cwar + item)

	domain	tcmcs.str19	search.string
		long	result			| result of search
		long	search_def(1,4)		| qss search definition
		long	length.of.key
		long	dummy.long

	rdi.domain.string("tcmcs.str19", length.of.key, dummy.long)

	| search array for values
	if cwar.item.array.index = 0 then
		o.location.allocated.quantity = 0.0
	else
		qss.start(search_def,1,1)
		qss.way(search_def,1,QSS.UP)
		qss.type(search_def,1,DB.STRING)
		qss.length(search_def, 1, length.of.key)
		search.string = i.warehouse(1;3) & i.item
		result = qss.search(	QSS.EQUAL,
					search.string,
					cwar_item_array,
					search_def,
					cwar.item.array.index)
		if result > 0 then
			o.location.allocated.quantity = qsts_array(result)
		else
			o.location.allocated.quantity = 0.0
		endif
	endif
}

function fill.array.for.location.allocated.quantity.item()
{
	domain	tcqiv1	location.allocated.quantity
		long	length.of.key
		long	array.size
		long	dummy.long

	cwar.item.array.index = 0
	rdi.domain.string("tcmcs.str19", length.of.key, dummy.long)

	select	count(*):array.size
	from	tdinv001
	as set with 1 rows
	selectdo
	endselect

	if array.size = 0 then					|#1116586.sn
		return
	endif							|#1116586.en

	alloc.mem(cwar_item_array, length.of.key, array.size)
	alloc.mem(qsts_array, array.size)
	| clean array
	set.mem(cwar_item_array, "")

	select	sum(tdilc401.qsts):location.allocated.quantity,
		tdilc401.cwar, tdilc401.item
	from	tdilc401
	where	tdilc401.proc = tcyesno.no
	and	tdilc401.cprj = ""
	and	tdilc401.qsts > 0.0
	group by tdilc401.cwar, tdilc401.item
	selectdo
		cwar.item.array.index = cwar.item.array.index + 1
		cwar_item_array(1, cwar.item.array.index) = tdilc401.cwar(1;3) & tdilc401.item
		qsts_array(cwar.item.array.index) = location.allocated.quantity
	selectempty
		| no values found with delivered = no
	endselect
}


|********* MIGRATE FROM TIPCS021 ******************

function migrate.project.item.data.by.warehouse()
{
	fill.array.for.location.allocated.quantity.project.item()

	select 	 tipcs021.* , tipcs020.prtp, tcmcs003.sloc
|		 tiitm001.cont				|#FP9.n	|#1635344.o
|	from 	 tipcs021, tipcs020, tcmcs003			|#FP9.o
|	from 	 tipcs021, tipcs020, tiitm001, tcmcs003	|#FP9.n	|#1635344.o
	from 	 tipcs021, tipcs020, tcmcs003			|#1635344.n
	where 	 tipcs020._index1 = {tipcs021.cprj}
|	and	 tiitm001._index1 = {tipcs021.item}	|#FP9.n	|#1635344.o
	and	 tcmcs003.cwar = tipcs021.cwar
	order by tipcs021._index1
	selectdo
								|#1635344.so
|		if tiitm001.cont = tcyesno.yes then		|#FP9.sn
								|#1635344.eo
		if item.is.container.item(tipcs021.item) then	|#1635344.n
			log.error(sprintf$(
				"Records Skipped for: item %s, warehouse %s." &
				" Reason container items not migrated. ",
				tipcs021.item,
				tipcs021.cwar))
			continue
		endif						|#FP9.en

		if tipcs020.prtp = tcyesno.yes then
			skip.record = false
			select 	 tppdm605.itmt
			from 	 tppdm605
			where 	 tppdm605._index1 = {:tipcs021.cprj, :tipcs021.item}
			and 	 tppdm605.itmt = tppdm.itmt.item
			and 	 tppdm605.prit <> tppdm.prit.manufactured
			as set with 1 rows
			selectdo
			selectempty
				skip.record = true
			endselect
			if skip.record then
				continue
			endif
		endif
		create.item.by.warehouse.from.project()
	endselect

	free.mem(warehouses)					|#1399055.sn
	free.mem(qty.allocated)
	free.mem(qty.on.order)
	free.mem(warehouse.handled)				|#1399055.en
}

function domain tcbool item.is.container.item(			|#1635344.sn
			domain	tcitem		i.item)
{
	domain	tcyesno		dummy.container.item

	select	tiitm001.cont:dummy.container.item
	from	tiitm001
	where	tiitm001._index1 = {:i.item}
	and	tiitm001.cont = tcyesno.yes
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}								|#1635344.en

function create.item.by.warehouse.from.project()
{
	long	local.i						|#1871849.n

	determine.allocated.and.on.order.qty.by.warehouse(	|#1399055.sn
					tipcs021.cprj,
					tipcs021.item)		|#1399055.en

	default.location.type()
	g.new.project.code =  get.project(tipcs021.cprj)
 	var.whwmd210.item =  segment.item.code( 	 tipcs021.item,
 	 	 	 	 	 	 	 g.new.project.code)
 	var.whwmd210.valm = 1	|* FTP
	var.whwmd210.cwar = tipcs021.cwar

	dump 211  |*whwmd211

	create.inventory.from.project(var.whwmd210.cwar)
	create.inventory.cost.details.from.project()

	select	tdilc101.cwar
	from	tdilc101
	where	tdilc101._index2 = {:tipcs021.cprj, :tipcs021.item}
	and	tdilc101.cntr = ""
	and	tdilc101.cwar <> :tipcs021.cwar
	group by tdilc101.cwar
	selectdo
		var.whwmd210.cwar = tdilc101.cwar

		select	tcmcs003.sloc				|#1461988.sn
		from	tcmcs003
		where	tcmcs003._index1 = {:var.whwmd210.cwar}
		selectdo
			default.location.type()
		endselect					|#1461988.en

		dump 211  |*whwmd211

		create.inventory.from.project(var.whwmd210.cwar)
		create.inventory.cost.details.from.project()
	endselect

								|#1871849.so
|	for i = 1 to no.warehouses				|#1399055.sn
|		if warehouse.handled(i) then
|			continue
|		endif
|
|		var.whwmd210.cwar = warehouses(1, i)
								|#1871849.eo
	for local.i = 1 to no.warehouses			|#1871849.sn
		if warehouse.handled(local.i) then
			continue
		endif

		var.whwmd210.cwar = warehouses(1, local.i)	|#1871849.en

		select	tcmcs003.sloc				|#1461988.sn
		from	tcmcs003
		where	tcmcs003._index1 = {:var.whwmd210.cwar}
		selectdo
			default.location.type()
		endselect					|#1461988.en

		dump 211  |*whwmd211

		create.inventory.from.project(var.whwmd210.cwar)
		create.inventory.cost.details.from.project()
	endfor							|#1399055.en
}

function create.inventory.from.project(	domain	tccwar	i.warehouse)
{
	long	local.i						|#1871849.n
	long	wh.number					|#1399055.n

	var.whwmd215.cwar = i.warehouse
	var.whwmd215.item = var.whwmd210.item

	wh.number = 0						|#1399055.sn
								|#1871849.so
|	for i = 1 to no.warehouses
|		if trim$(warehouses(1, i)) = trim$(i.warehouse) then
|			wh.number = i
								|#1871849.eo
								|#1871849.sn
	for local.i = 1 to no.warehouses
|		if trim$(warehouses(1, local.i)) = trim$(i.warehouse) then	|#1912023.o
		if shiftl$(strip$(warehouses(1, local.i))) = 			|#1912023.n
					shiftl$(strip$(i.warehouse)) then	|#1912023.n
			wh.number = local.i
								|#1871849.en
			warehouse.handled(wh.number) = true
			break
		endif
	endfor							|#1399055.en

	if is.masterdata.conversion() then
		init.inventory.fields()
	else
		var.whwmd215.qhnd = 0.0				|#1871849.n
		if tdilc000.iilc = tcyesno.no then
			if var.whwmd215.cwar = tipcs021.cwar then |#1871849.n
				var.whwmd215.qhnd = tipcs021.stoc
			endif					|#1871849.n
		else
|			var.whwmd215.qhnd = 0.0			|#1871849.o
			select	sum(tdilc101.stks):var.whwmd215.qhnd
			from	tdilc101
			where	tdilc101._index2 = {:tipcs021.cprj, :tipcs021.item, "", :var.whwmd215.cwar}
			as set with 1 rows
			selectdo
			endselect
		endif

		var.whwmd215.qchd = 0.0
		if var.whwmd215.cwar = tipcs021.cwar then	|#1871849.n
			set.quantities.blocked(
					var.whwmd215.cwar,
					tipcs021.item,
					tipcs021.cprj,
					tipcs021.blck,
					0.0)	|* blocked for inspection
		else						|#1871849.sn
			var.whwmd215.qblk = 0.0
			var.whwmd260.qblk = 0.0
			var.whwmd260.qprb = 0.0
		endif						|#1871849.en

|		if var.whwmd215.cwar = tipcs021.cwar then	|#1399055.so
|			var.whwmd215.qord = tipcs021.ordr
|		else
|			var.whwmd215.qord = 0.0
|		endif						|#1399055.eo
		if wh.number = 0 then				|#1399055.sn
			var.whwmd215.qord = 0.0
		else
			var.whwmd215.qord = qty.on.order(wh.number)
		endif						|#1399055.en
		var.whwmd215.qcor = 0.0
		var.whwmd215.qint = 0.0
|		if var.whwmd215.cwar = tipcs021.cwar then	|#1399055.so
|			var.whwmd215.qall = tipcs021.allo
|		else
|			var.whwmd215.qall = 0.0
|		endif						|#1399055.eo
		if wh.number = 0 then				|#1399055.sn
			var.whwmd215.qall = 0.0
		else
			var.whwmd215.qall = qty.allocated(wh.number)
		endif						|#1399055.en

		var.whwmd215.qcal = 0.0
		var.whwmd215.qcom = 0.0				|#1871849.n
		if tdilc000.iilc = tcyesno.no then
			if var.whwmd215.cwar = tipcs021.cwar then |#1871849.n
				var.whwmd215.qcom = tipcs021.hall
			endif					|#1871849.n
|		else						|#1871849.o
|			var.whwmd215.qcom = 0.0			|#1871849.o
		endif

		if tdilc000.iilc = tcyesno.yes and
		   var.whwmd215.qhnd > 0.0 then
			determine.location.allocated.quantity.project.item(
					var.whwmd215.cwar,
					tipcs021.cprj,
					tipcs021.item,
					var.whwmd215.qlal)
		else
			var.whwmd215.qlal = 0.0
		endif
		var.whwmd215.qcpr = 0.0
		var.whwmd215.ltdt = utc(tipcs021.ltdt, 0)
		var.whwmd215.hstd = utc(0, 0)
		var.whwmd215.lsid = utc(0, 0)
		if var.whwmd215.cwar = tipcs021.cwar then
			var.whwmd215.qcis = tipcs021.uscu
		else
			var.whwmd215.qcis = 0.0
		endif

		var.whwmd215.qhib = 0.0
	endif

	dump 215	| *  whwmd215 -  Item Inventory by Warehouse
	dump 216	| *  whwmd216 -  Inventory by Warehouse, Item and Eff.Unit
								|#FP8.sn
	dump 260	| *  whwmd260 -  Project Pegged Inventory
								|#FP8.sn
	handle.insert.inventory.by.specification()		|#1922484.n
}

function create.inventory.cost.details.from.project()
{
	initialize.ftpa.amounts()

	| *  Read cost price by customized table.
	select	tipcs350.*
	from	tipcs350
	where	tipcs350._index1 = { :tipcs021.cprj, :tipcs021.item }
	selectdo
		for i = 1 to g.mc.depth
			amount.per.cc(i) = get.amount.per.cost.component(
						"tipcs350",
						i)
		endfor
		create.detailed.inventory.cost.details(
				tipcs021.item,
				tipcs021.cprj,
				tipcs350.cpcp,
				amount.per.cc)

	endselect

	create.remainder.inventory.cost.details()
}

|* call of function create.detailed.inventory.cost.details() always before
|* call of function create.remainder.inventory.cost.details()
function create.detailed.inventory.cost.details(
			domain	tcitem	i.item,
			domain	tccprj	i.project,
			domain	tccpcp	i.cost.component,
		ref	domain	tccopr	i.amount())
{
	domain	tcbool		zero.amount
	domain	tcamnt		amount

	analyze.cost.component(	i.item,
				i.project,
				i.cost.component,
				detail.cost.component,
				material.component,
				operation.component,
				surcharge.component,
				result)
	on case result
	case 0:
		| *  Cost Component in chart
		| *  Detail Cost Component
		var.whwmd217.cpcp = detail.cost.component
		zero.amount = true
		for i = 1 to g.mc.depth
			var.whwmd217.ftpa(i) = i.amount(i)
			amount = var.whwmd217.ftpa(i)
			if double.cmp(amount, 0.0, 0.000001) <> 0 then
				zero.amount = false
			endif
		endfor
		if is.masterdata.conversion() then
			e = set.mem(var.whwmd217.mauc, 0)
		else
			e = copy.mem(	var.whwmd217.mauc,
					var.whwmd217.ftpa,
					g.mc.depth)
			for i = 1 to g.mc.depth
				var.whwmd217.mauc(i) = var.whwmd215.qhnd *
						var.whwmd217.mauc(i)
				amount = var.whwmd217.mauc(i)
				if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
					zero.amount = false
				endif
			endfor
		endif

		if not zero.amount then
			dump 217 | *  whwmd217 - Item Inventory by Warehouse
				|* Cost Details
		endif
		break
	case 1:
		| *  Merge to aggregated material cost component
		for i = 1 to g.mc.depth
			material.component.ftpa(i) =
				material.component.ftpa(i) + i.amount(i)

		endfor
		break
	case 2:
		| *  Merge to aggregated opration cost component
		for i = 1 to g.mc.depth
			operation.component.ftpa(i) =
				operation.component.ftpa(i) + i.amount(i)
		endfor
		break
	case 3:
		| *  Merge to aggregated surcharge cost component
		for i = 1 to g.mc.depth
			surcharge.component.ftpa(i) =
				surcharge.component.ftpa(i) + i.amount(i)
		endfor
		break
	endcase
}

function create.remainder.inventory.cost.details()
{
	domain	tcbool		zero.amount
	domain	tcamnt		amount

	|* init the mauc value; overwrite if not masterdata conversion
	e = set.mem(var.whwmd217.mauc, 0)

	| *  Material Cost Component
	zero.amount = true
	var.whwmd217.cpcp = material.component
	e = copy.mem(var.whwmd217.ftpa, material.component.ftpa, g.mc.depth)
	for i = 1 to 3
		amount = var.whwmd217.ftpa(i)
		if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
			zero.amount = false
		endif
	endfor
	if not is.masterdata.conversion() then
		e = copy.mem(var.whwmd217.mauc, var.whwmd217.ftpa, g.mc.depth)
		for i = 1 to g.mc.depth
			var.whwmd217.mauc(i) = var.whwmd215.qhnd *
						var.whwmd217.mauc(i)
			amount = var.whwmd217.mauc(i)
			if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
				zero.amount = false
			endif
		endfor
	endif
	if not zero.amount then
		dump 217 | *  whwmd217 - Item Inventory by Warehouse
			|* Cost Details
	endif

	| *  Operation Cost Component
	zero.amount = true
	var.whwmd217.cpcp = operation.component
	e = copy.mem(var.whwmd217.ftpa, operation.component.ftpa, g.mc.depth)
	for i = 1 to 3
		amount = var.whwmd217.ftpa(i)
		if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
			zero.amount = false
		endif
	endfor
	if not is.masterdata.conversion() then
		e = copy.mem(var.whwmd217.mauc, var.whwmd217.ftpa, g.mc.depth)
		for i = 1 to g.mc.depth
			var.whwmd217.mauc(i) = var.whwmd215.qhnd *
						var.whwmd217.mauc(i)
			amount = var.whwmd217.mauc(i)
			if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
				zero.amount = false
			endif
		endfor
	endif
	if not zero.amount then
		dump 217 | *  whwmd217 - Item Inventory by Warehouse
			|* Cost Details
	endif

	| *  Surcharge Cost Component
	zero.amount = true
	var.whwmd217.cpcp = surcharge.component
	e = copy.mem(var.whwmd217.ftpa, surcharge.component.ftpa, g.mc.depth)
	for i = 1 to 3
		amount = var.whwmd217.ftpa(i)
		if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
			zero.amount = false
		endif
	endfor

	if not is.masterdata.conversion() then
		e = copy.mem(var.whwmd217.mauc, var.whwmd217.ftpa, g.mc.depth)
		for i = 1 to g.mc.depth
			var.whwmd217.mauc(i) = var.whwmd215.qhnd *
						var.whwmd217.mauc(i)
			amount = var.whwmd217.mauc(i)
			if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
				zero.amount = false
			endif
		endfor
	endif
	if not zero.amount then
		dump 217 | *  whwmd217 - Item Inventory by Warehouse
			|* Cost Details
	endif
}

function read.parameters()
{
	select	tipcs000.*
	from	tipcs000
	as set with 1 rows
	selectdo
	endselect

	select 	 tdilc000.iilc, tdilc000.take
	from 	 tdilc000
	as set with 1 rows
	selectdo
	endselect

	select	bcstc000.*
	from	bcstc000
	as set with 1 rows
	selectdo
	endselect
}

function set.period.variables()
{
	if tdinv000.perl = tcperl.month then
		var.whwmd210.ptyp = 3 |* month.
		var.whwmd210.nopr = 1 |* nr. of buckets per period.
	else
		var.whwmd210.ptyp = 2 |* week
		var.whwmd210.nopr = 4 |* nr. of buckets per period.
	endif
}

function determine.location.allocated.quantity.project.item(
			domain	tccwar	i.warehouse	fixed,
			domain	tccprj	i.project 	fixed,
			domain	tcitem	i.item		fixed,
		ref	domain	tcqiv1	o.location.allocated.quantity)
{
	domain	tcmcs.str25	search.string
		long	result
		long	search_def(1,4)
		long	length.of.key
		long	dummy.long

	rdi.domain.string("tcmcs.str25", length.of.key, dummy.long)

	if cwar.cprj.item.array.index = 0 then
		o.location.allocated.quantity = 0.0
	else
		qss.start(search_def, 1, 1)
		qss.way(search_def, 1, QSS.UP)
		qss.type(search_def, 1, DB.STRING)
		qss.length(search_def, 1, length.of.key)
		search.string = i.warehouse(1;3) & i.project(1;6) & i.item
		result = qss.search(	QSS.EQUAL,
					search.string,
					cwar_cprj_item_array,
					search_def,
					cwar.cprj.item.array.index)
		if result > 0 then
			o.location.allocated.quantity = qsts_cprj_array(result)
		else
			o.location.allocated.quantity = 0.0
		endif
	endif
}

function fill.array.for.location.allocated.quantity.project.item()
{
	domain	tcqiv1	location.allocated.quantity
		long	length.of.key
		long	array.size
		long	dummy.long

	cwar.cprj.item.array.index = 0
	rdi.domain.string("tcmcs.str25", length.of.key, dummy.long)

	select	count(*):array.size
	from	tipcs021
	as set with 1 rows
	selectdo
	endselect

	if array.size = 0 then					|#1116586.sn
		return
	endif							|#1116586.en

	alloc.mem(cwar_cprj_item_array, length.of.key, array.size)
	alloc.mem(qsts_cprj_array, array.size)
	| clean array
	set.mem(cwar_cprj_item_array, "")

	select	sum(tdilc401.qsts):location.allocated.quantity,
		tdilc401.cwar, tdilc401.cprj, tdilc401.item
	from	tdilc401
	where	tdilc401.proc = tcyesno.no
	and	tdilc401.cprj <> ""
	and	tdilc401.qsts > 0.0
	group by tdilc401.cwar, tdilc401.cprj, tdilc401.item
	selectdo
		cwar.cprj.item.array.index = cwar.cprj.item.array.index + 1
		cwar_cprj_item_array(1, cwar.cprj.item.array.index) = tdilc401.cwar(1;3) & tdilc401.cprj(1;6) & tdilc401.item
		qsts_cprj_array(cwar.cprj.item.array.index) = location.allocated.quantity
	selectempty
		| no values found with delivered = no
	endselect
}

								|#1399055.sn
function void determine.allocated.and.on.order.qty.by.warehouse(
		domain	tccprj		i.project,
		domain	tcitem		i.item)
{
			long		dummy.long
			long		warehouse.length
	static		long		no.warehouses.allocated
		domain	tcbool		warehouse.in.array
		domain	tccwar		tipcs500.warehouse

	no.warehouses = 0

	rdi.domain.string(	domainof(warehouses),
				warehouse.length,
				dummy.long)

	select	tipcs500.koor, tipcs500.orno, tipcs500.pono,
		tipcs500.kotr, tipcs500.qana
	from	tipcs500
	where	tipcs500._index2 = {:i.project, :i.item}
	selectdo
		tipcs500.warehouse = get.warehouse(
				tipcs500.koor,
				tipcs500.orno,
				tipcs500.pono,
				tipcs500.kotr,
				i.project)

		warehouse.in.array = false

		for i = 1 to no.warehouses
|			if trim$(warehouses(1, i)) =				|#1912023.o
|						trim$(tipcs500.warehouse) then	|#1912023.o
			if shiftl$(strip$(warehouses(1, i))) =			|#1912023.n
				  shiftl$(strip$(tipcs500.warehouse)) then	|#1912023.n
				if tipcs500.kotr = tckotr.requirement  then
					qty.allocated(i) =
						qty.allocated(i) +
						tipcs500.qana
				else
					qty.on.order(i) =
						qty.on.order(i) +
						tipcs500.qana
				endif
				warehouse.in.array = true
				break
			endif
		endfor

		if warehouse.in.array then
			continue
		endif

		no.warehouses = no.warehouses + 1

		if no.warehouses > no.warehouses.allocated then
			no.warehouses.allocated = no.warehouses.allocated + 10

			alloc.mem(	warehouses,
					warehouse.length,
					no.warehouses.allocated)
			alloc.mem(	qty.allocated,
					no.warehouses.allocated)
			alloc.mem(	qty.on.order,
					no.warehouses.allocated)
			alloc.mem(	warehouse.handled,
					no.warehouses.allocated)
		endif

		warehouses(1, no.warehouses) = tipcs500.warehouse
		warehouse.handled(no.warehouses) = false
		if tipcs500.kotr = tckotr.requirement  then
			qty.allocated(no.warehouses) = tipcs500.qana
			qty.on.order(no.warehouses) = 0.0
		else
			qty.allocated(no.warehouses) = 0.0
			qty.on.order(no.warehouses) = tipcs500.qana
		endif
	endselect
}								|#1399055.en

function domain	tccwar get.warehouse(				|#1399055.sn
		domain	tckoor		i.kind.of.order,
		domain	tcorno		i.order,
		domain	tcpono		i.order.line,
		domain	tckotr		i.transaction.type,
		domain	tccprj		i.project)
{
	domain	tccwar		return.warehouse

	return.warehouse = tipcs021.cwar

	on case i.kind.of.order
	case tckoor.act.pur:
		select	tdpur041.cwar
		from	tdpur041
		where	tdpur041._index1 = {:i.order, :i.order.line}
		as set with 1 rows
		selectdo
			return.warehouse = tdpur041.cwar
		endselect
		break
	case tckoor.act.sls:
		select	tdsls041.cwar
		from	tdsls041
		where	tdsls041._index1 = {:i.order, :i.order.line}
		as set with 1 rows
		selectdo
			return.warehouse = tdsls041.cwar
		endselect
		break
	case tckoor.pcs.wrh:
		select	tipcs530.kood, tipcs530.cwar, tipcs530.swar,
			tipcs530.wart
		from	tipcs530
		where	tipcs530._index1 = {:i.project, :i.order}
		as set with 1 rows
		selectdo
			on case tipcs530.kood
			case tckood.wp:
				if i.transaction.type = tckotr.requirement then
					return.warehouse = tipcs530.swar
				else
					return.warehouse = tipcs530.cwar
				endif
				break
			case tckood.pw:
				if i.transaction.type = tckotr.requirement then
					return.warehouse = tipcs530.cwar
				else
					return.warehouse = tipcs530.swar
				endif
				break
			case tckood.pp:
				if i.transaction.type = tckotr.requirement then
					return.warehouse = tipcs530.cwar
				else
					return.warehouse = tipcs530.wart
				endif
				break
			case tckood.tp:
				if i.transaction.type = tckotr.requirement then
					return.warehouse = tipcs530.cwar
				endif
				break
			endcase
		endselect
		break
	case tckoor.act.sfc:
		if i.transaction.type = tckotr.requirement then
			select	ticst001.cwar
			from	ticst001
			where	ticst001._index1 = {:i.order, :i.order.line}
			as set with 1 rows
			selectdo
				return.warehouse = ticst001.cwar
			endselect
		else
			select	tisfc001.cwar
			from	tisfc001
			where	tisfc001._index1 = {:i.order}
			as set with 1 rows
			selectdo
				return.warehouse = tisfc001.cwar
			endselect
		endif
		break
	case tckoor.act.rpl:
		select	tdrpl100._index1,
			tdrpl100.rwar, tdrpl100.dwar
		from	tdrpl100
		where	tdrpl100._index1 = {:i.order, :i.order.line}
		order by tdrpl100._index1 desc
		as set with 1 rows
		selectdo
			if i.transaction.type = tckotr.requirement then
				return.warehouse = tdrpl100.rwar
			else
				return.warehouse = tdrpl100.dwar
			endif
		endselect
		break
	endcase

	return(return.warehouse)
}								|#1399055.en





|* CODE.OLD:
|function migrate.distribution.item.data.by.warehouse()
|{
| 	| *  only items of item type purchased or manufactured are converted.
| 	| *  item type (project) should not be equipment or subcontracting.
| 	| *  so only type (project) item is dumped.
|
|	fill.array.for.location.allocated.quantity.item()	|#1011283.n
|
|	select 	 tdinv001.* , tcmcs003.sloc,
|	 	 tiitm001.item, tiitm001.kitm, tiitm001.ltcp,
|		tiitm001.stva,					|#146674.n
|		tiitm001.sftm,  tiitm001.abcc,  tiitm001.itmt
|	from 	 tdinv001,
|	 	 tcmcs003,
|	 	 tiitm001
|	where 	 tdinv001.cwar > ""
|	 and 	 tdinv001.cntr = ""
|	 and 	 tcmcs003._index1 = {tdinv001.cwar}
|	 and 	 tcmcs003.typw <> tctypw.transito
|	 and 	 tiitm001._index1 = {tdinv001.item}
|	 and 	 (tiitm001.kitm = tckitm.purchase or
|	 	 tiitm001.kitm = tckitm.manufacture )
|	 and 	 tiitm001.itmt = tppdm.itmt.item
|	order by tdinv001._index1				|#1011283.n
|	selectdo
|		create.item.by.warehouse()
|		 create.inventory()
|	 	 create.inventory.cost.details()
|	endselect
|}
|
|function create.item.by.warehouse()
|{
|	if (tdinv001.maxs >  99999999) then
| 	 	 tdinv001.maxs =  99999999
| 	 endif
| 	 var.whwmd210.item =  segment.item.code( 	 tdinv001.item,
| 	 	 	 	 	 	 	 "")
| 	 if tdinv001.oqmf >  0 then
| 	 	 var.whwmd210.oqmf =  tdinv001.oqmf
| 	 else
| 	 	 var.whwmd210.oqmf =  1.0
| 	 endif
| 	 	 	 	 	 	 	 	 | #  800 -  142003.sn
|	default.location.type()
| 	 	 	 	 	 	 	 	 | #  800 -  142003.en
| 	 var.whwmd210.sftm =  tiitm001.sftm
| 	 var.whwmd210.abcc =  tiitm001.abcc
|								|#146674.sn
|	if tiitm001.stva = tcstva.lot.price then
|		if lot.controlled.item("", tdinv001.item) then
|			var.whwmd210.valm = 5	|* target lot price
|		else
|			var.whwmd210.valm = 1	|* target FTP
|		endif
|	else
|		var.whwmd210.valm = 1	|* target FTP
|	endif
|								|#146674.en
|
|	dump 210
|}
|
|function default.location.type()
|{
| 	 | *  determine default location type for inbound advice.
| 	 if tcmcs003.sloc =  tcyesno.yes then
| 	 	 var.whwmd210.ialt =  5 | *  whwmd.loct.bulk
| 	 else
| 	 	 var.whwmd210.ialt =  20 | *  whwmd.loct.not.appl
| 	 endif
|}
|
|function create.inventory()
|{
|	var.whwmd215.cwar = tdinv001.cwar
|	var.whwmd215.item = segment.item.code(
|					tdinv001.item,
|					"")	|* Project
|	if is.masterdata.conversion() then
|		init.inventory.fields()
|	else
|		var.whwmd215.qhnd = tdinv001.stoc
|		var.whwmd215.qchd = 0.0
|		set.quantities.blocked(
|				tdinv001.cwar,
|				tdinv001.item,
|				"",				|#214852.n
|				tdinv001.blck,
|				tdinv001.qbsi)
|		var.whwmd215.qord = tdinv001.ordr
|		var.whwmd215.qcor = 0.0
|		var.whwmd215.qint = 0.0
|		set.quantities.allocated.and.onorder()
|		var.whwmd215.qcal = 0.0
|		var.whwmd215.qcom = tdinv001.hall
|		if tdilc000.iilc = tcyesno.yes then
|			determine.location.allocated.quantity(
|					tdinv001.cwar,
||					"", |*  Project 	|#1011283.o
|					tdinv001.item,
|					var.whwmd215.qlal)
|		else
|			var.whwmd215.qlal = 0.0
|		endif
|		var.whwmd215.qcpr = 0.0
|		var.whwmd215.ltdt = utc(tdinv001.ltdt, 0)
||		var.whwmd215.hstd = utc(tdinv001.hstd, 0)		|#217134.o
|		var.whwmd215.hstd = utc(0,0)				|#217134.n
|		var.whwmd215.lsid = utc(tdinv001.lsid, 0)
|		var.whwmd215.qcis = tdinv001.uscu
||		var.whwmd215.qhib = tdinv001.hstb			|#217134.o
|		var.whwmd215.qhib = 0.0					|#217134.n
|	endif
|
|	dump 215	| * whwmd215 - Item Inventory by Warehouse
|	dump 216	| * whwmd216 - Inventory by Warehouse, Item and Eff.Unit
|
|}
|
|function init.inventory.fields()
|{
|	var.whwmd215.qhnd = 0.0
|	var.whwmd215.qchd = 0.0
|	var.whwmd215.qblk = 0.0
|	var.whwmd215.qbpl = 0.0
|	var.whwmd215.qord = 0.0
|	var.whwmd215.qcor = 0.0
|	var.whwmd215.qint = 0.0
|	var.whwmd215.qall = 0.0
|	var.whwmd215.qcal = 0.0
|	var.whwmd215.qcom = 0.0
|	var.whwmd215.qlal = 0.0
|	var.whwmd215.qcpr = 0.0
|	var.whwmd215.ltdt = utc(0,0)
|	var.whwmd215.hstd = utc(0,0)
|	var.whwmd215.lsid = utc(0,0)
|	var.whwmd215.qcis = 0.0
|	var.whwmd215.qhib = 0.0
|}
|
|function set.quantities.blocked(
|	domain	tccwar	i.warehouse,
|	domain	tcitem	i.item,
|	domain	tccprj	i.project,				|#214852.n
|	double		i.qty.blocked,
|	double		i.qty.blocked.for.inspection)
|{
|	var.whwmd215.qblk = i.qty.blocked + i.qty.blocked.for.inspection
|	if tdilc000.iilc = tcyesno.yes and
|	   var.whwmd215.qblk > 0.0 then
|		determine.blocked.quantity.decrease(
|			i.warehouse,
||			"", 	|* Project			|#214852.o
|			i.project,				|#214852.n
|			i.item,
|			blocked.quantity.decrease)
|		if blocked.quantity.decrease > 0.0 then
|			var.whwmd215.qblk = var.whwmd215.qblk -
|						blocked.quantity.decrease
|			if var.whwmd215.qblk < 0.0 then
|				var.whwmd215.qblk = 0.0
|			endif
|		endif
|	endif
|	var.whwmd215.qbpl = var.whwmd215.qblk
|}
|
|function set.quantities.allocated.and.onorder()
|{
|	if tipcs000.ipcs = tcyesno.yes then
|		| * PCS implemented
|		var.whwmd215.qall =
|			determine.prp.inv.correction.for.standard.item(
|					tdinv001.item,
|					tdinv001.cwar,
|					tdinv001.allo)
|	else
|		var.whwmd215.qall = tdinv001.allo
|	endif
|
|	| * Purhase return order in Baan4 creates a negative on order quantity,
|	| * In Baan6 it must be a positive allocated quantity.
|	| * Sales return order in Baan4 creates a negative allocated quantity,
|	| * In Baan6 it must be a positive on order quantity.
|
|	move.to.allocated = 0.0
|	move.to.onorder = 0.0
|	if var.whwmd215.qord < 0.0 then
|		move.to.allocated = abs(var.whwmd215.qord)
|	endif
|	if var.whwmd215.qall < 0.0 then
|		move.to.onorder = abs(var.whwmd215.qall)
|	endif
|	if move.to.allocated +  move.to.onorder > 0.0 and
|	  move.to.allocated <> move.to.onorder then
|		if move.to.allocated > move.to.onorder then
|			move.to.allocated = move.to.allocated - move.to.onorder
|			var.whwmd215.qord = 0.0
|			var.whwmd215.qall = var.whwmd215.qall + move.to.allocated
|		else
|			move.to.onorder = move.to.onorder - move.to.allocated
|			var.whwmd215.qall = 0.0
|			var.whwmd215.qord = var.whwmd215.qord + move.to.onorder
|		endif
|	endif
|}
|
|function create.inventory.cost.details()
|{
|	date.last.cost.price.change = tiitm001.ltcp
|	| * Initialize variables.
|	initialize.ftpa.amounts()
|	| * Read standard cost price table.
||	select	ticpr300.cpcp, 						|#CNT.so
||		ticpr300.amnt 						|#CNT.eo
|	select	ticpr300.*						|#CNT.n
|	from	ticpr300
|	where	ticpr300._index1 = {:tdinv001.item}
|	and	ticpr300.indt = :date.last.cost.price.change
|	selectdo
|									|#CNT.sn
|		for i = 1 to g.mc.depth
|			amount.per.cc(i) = get.amount.per.cost.component(
|						"ticpr300",
|						i)
|		endfor
|									|#CNT.en
|		create.detailed.inventory.cost.details(
|					tdinv001.item,
|					"",	|* project
|					ticpr300.cpcp,
|					amount.per.cc)			|#CNT.n
||					ticpr300.amnt)			|#CNT.o
|	selectempty
|		| * No cost components, use aggregated components with ftp = 0.
|		material.component = bcstc000.matc
|		operation.component = bcstc000.oprc
|		surcharge.component = bcstc000.surc
|		initialize.ftpa.amounts()
|	endselect
|
|	create.remainder.inventory.cost.details()
|}
|
|function domain tcqiv1 determine.prp.inv.correction.for.standard.item(
|			domain	tcitem	i.item,
|			domain	tccwar	i.warehouse,
|			domain	tcqiv1	i.old.allo.quantity)
|{
|	|FunctionUsage
|	|Expl:	In Baan IV when a planned PRP production order has a standard
|	|	item as material, an inventory transaction is logged in tdinv150
|	|	with Order Type = PRP Production Order and Transaction
|	|	Type = - (negative). This leads to an increase of the Allocated
|	|	Inventory in tiitm001. But in Baan 6 inventory transactions
|	|	for planned production orders are not logged in whinp100, but
|	|	only in cprrp010. These transactions are not reflected in the
|	|	inventory data in tcibd100 and whwmd215. This means that the Allocated
|	|	Inventory in tiitm001 needs to be adjusted for these type
|	|	of transactions.
|	|EndFunctionUsage
|
|	domain	tcqiv1	prp.allocated.inventory
|
|	prp.allocated.inventory = 0.0
|
|	select	sum(tdinv150.qana):prp.allocated.inventory
|	from	tdinv150
|	where	tdinv150._index4 = {	:i.item,
|					"",
|					:i.warehouse}
|	and	tdinv150.koor = tckoor.pcs.sfc
|	and	tdinv150.kotr = tckotr.requirement
|	as set with 1 rows					|#1011283.n
|	selectdo
|	endselect
|
|	return(i.old.allo.quantity - prp.allocated.inventory)
|}
|
|function initialize.ftpa.amounts()
|{
|	e = set.mem(material.component.ftpa, 0)
|	e = set.mem(operation.component.ftpa, 0)
|	e = set.mem(surcharge.component.ftpa, 0)
|}
|
|function void	determine.blocked.quantity.decrease(
|			domain	tccwar	i.warehouse,
|			domain	tccprj	i.project,
|			domain	tcitem	i.item,
|		ref	domain	tcqiv1	o.blocked.quantity.decrease)
|{
|	domain	tdilc.loca	insp.location
|	domain	tcqiv1		process.blocked.quantity
|	domain	tcqiv1		approved.quantity
|
|	o.blocked.quantity.decrease = 0.0
|	select	tdilc101.*,
|		tdilc001.loca:insp.location
|	from	tdilc101, tdilc001
|	where	tdilc101._index4 = {	:i.warehouse,
|					:i.project,
|					:i.item}
|	and	tdilc101.cntr = ""
|	and	tdilc101.loca <> ""
|	and	tdilc101.stkb > 0.0
|	and	tdilc001._index1 = {	tdilc101.cwar,
|					tdilc101.loca}
|	and	tdilc001.aplo = tcyesno.yes
|	selectdo
|		process.blocked.quantity = tdilc101.stkb - tdilc101.stka
|		if process.blocked.quantity > 0 then
|			get.approved.quantity(	tdilc101.cwar,
|						tdilc101.loca,
|						tdilc101.cprj,
|						tdilc101.item,
|						"",		|* container
|						tdilc101.clot,
|						tdilc101.date,
|						tdilc101.stun,
|						approved.quantity)
|			process.blocked.quantity = tdilc101.stkb - approved.quantity
|		endif
|		if process.blocked.quantity < 0 then
|			process.blocked.quantity = 0
|		endif
|		o.blocked.quantity.decrease = o.blocked.quantity.decrease +
|				(tdilc101.stkb - process.blocked.quantity)
|	endselect
|}
|
|function void get.approved.quantity(
|			domain	tccwar		i.warehouse,
|			domain	tdilc.loca	i.location,
|			domain	tccprj		i.project,
|			domain	tcitem		i.item,
|			domain	tccuni		i.container,
|			domain	tdltc.clot	i.lot,
|			domain	tcdate		i.inventory.date,
|			domain	tccuni		i.unit,
|		ref	domain	tcqst1		o.approved.quantity)
|{
|	o.approved.quantity = 0.0
|
|	select	sum(tdilc111.stra):o.approved.quantity
|	from	tdilc111
|	where	tdilc111._index2 = {	:i.warehouse,
|					:i.location,
|					:i.project,
|					:i.item,
|					:i.container,
|					:i.lot,
|					:i.unit,
|					:i.inventory.date}
|	as set with 1 rows
|	selectdo
|	endselect
|}
|
|function determine.location.allocated.quantity(
||			domain	tccwar	i.warehouse,		|#1011283.so
||			domain	tccprj	i.project,
||			domain	tcitem	i.item,			|#1011283.eo
|			domain	tccwar	i.warehouse	fixed,	|#1011283.n
|			domain	tcitem	i.item		fixed,	|#1011283.n
|		ref	domain	tcqiv1	o.location.allocated.quantity)
|{
|	|* PERF
|	|    replace old query by query which is only once executed for each warehouse
|	|	the items + sum(tdilc401,qsts) are stored in a memory array
|	|	function must be called with input sorted index1 of tdinv001 (= sorted on cwar + item)
|
|								|#1011283.so
||	static	domain	tcitem	item_array(1) based	| item array
||	static	domain	tcqiv1	qsts_array(1) based	| sum(tdilc401.qsts) array
||
||	static	long array.size		| current size of the array
||	static	long array.index		| current index in the array
|								|#1011283.eo
|	domain	tcmcs.str19	search.string			|#1011283.n
|		long	result			| result of search
|		long	search_def(1,4)		| qss search definition
|		long	length.of.key				|#1011283.n
|		long	dummy.long				|#1011283.n
|
|	rdi.domain.string("tcmcs.str19", length.of.key, dummy.long)	|#1011283.n
|								|#1011283.so
||	static	domain	tccwar	s.warehouse	| static for warehouse
||	static	domain tcitem	s.item		| static for item
||	static	domain tcqiv1	s.o.location.allocated.quantity
||	long	length.of.item
||	long	dummy.long
||
||	| same input then return earlier value
||	if s.warehouse = i.warehouse and
||	  s.item = i.item then
||		o.location.allocated.quantity = s.o.location.allocated.quantity
||		return
||	endif
||
||	if s.warehouse <> i.warehouse then
||		if length.of.item = 0 then
||			rdi.domain.string("tcitem", length.of.item, dummy.long)
||		endif
||		| clean array
||		if array.index > 0 then
||			set.mem(item_array,"")
||			array.index = 0
||		endif
||
||		| fill array for new warehouse
||		select	sum(tdilc401.qsts):o.location.allocated.quantity,
||			tdilc401.item
||		from	tdilc401
||		where	tdilc401.proc = tcyesno.no
||		and	tdilc401.cwar = :i.warehouse
||		and	tdilc401.qsts > 0.0
||		group by tdilc401.item
||		selectdo
||			| fill array
||			array.index = array.index + 1
||			if array.index > array.size then
||				| alloc (additional) memory
||				array.size = array.size + 100
||				alloc.mem(item_array, length.of.item, array.size)
||				alloc.mem(qsts_array, array.size)
||			endif
||			| store item and sum(tdilc401.qsts)
||			item_array(1,array.index) = tdilc401.item
||			qsts_array(array.index) = o.location.allocated.quantity
||		selectempty
||			| no values found with delivered = no
||		endselect
||		s.warehouse = i.warehouse
||	endif							|#1011283.eo
|
|	| search array for values
||	if array.index = 0 then					|#1011283.o
|	if cwar.item.array.index = 0 then			|#1011283.n
|		o.location.allocated.quantity = 0.0
|	else
|		qss.start(search_def,1,1)
|		qss.way(search_def,1,QSS.UP)
|		qss.type(search_def,1,DB.STRING)
||		qss.length(search_def, 1, length.of.item)	|#1011283.o
||		result = qss.search(QSS.EQUAL, i.item, item_array, search_def, array.index)	|#1011283.o
|		qss.length(search_def, 1, length.of.key)	|#1011283.sn
|		search.string = i.warehouse(1;3) & i.item
|		result = qss.search(	QSS.EQUAL,
|					search.string,
|					cwar_item_array,
|					search_def,
|					cwar.item.array.index)	|#1011283.en
|		if result > 0 then
|			o.location.allocated.quantity = qsts_array(result)
|		else
|			o.location.allocated.quantity = 0.0
|		endif
|	endif
|
||	s.item = i.item						|#1011283.so
||	s.o.location.allocated.quantity = o.location.allocated.quantity
|								|#1011283.eo
|
||	o.location.allocated.quantity = 0.0
||	select	sum(tdilc401.qsts):o.location.allocated.quantity
||	from	tdilc401
||	where	tdilc401.proc = tcyesno.no
||	and	tdilc401.cwar = :i.warehouse
||	and	tdilc401.item = :i.item
||	and	tdilc401.qsts > 0.0
||	as set with 1 rows
||	selectdo
||	endselect
|}
|
|function fill.array.for.location.allocated.quantity.item()	|#1011283.sn
|{
|	domain	tcqiv1	location.allocated.quantity
|		long	length.of.key
|		long	array.size
|		long	dummy.long
|
|	cwar.item.array.index = 0
|	rdi.domain.string("tcmcs.str19", length.of.key, dummy.long)
|
|	select	count(*):array.size
|	from	tdinv001
|	as set with 1 rows
|	selectdo
|	endselect
|
|	alloc.mem(cwar_item_array, length.of.key, array.size)
|	alloc.mem(qsts_array, array.size)
|	| clean array
|	set.mem(cwar_item_array, "")
|
|	select	sum(tdilc401.qsts):location.allocated.quantity,
|		tdilc401.cwar, tdilc401.item
|	from	tdilc401
|	where	tdilc401.proc = tcyesno.no
|	and	tdilc401.cprj = ""
|	and	tdilc401.qsts > 0.0
|	group by tdilc401.cwar, tdilc401.item
|	selectdo
|		cwar.item.array.index = cwar.item.array.index + 1
|		cwar_item_array(1, cwar.item.array.index) = tdilc401.cwar(1;3) & tdilc401.item
|		qsts_array(cwar.item.array.index) = location.allocated.quantity
|	selectempty
|		| no values found with delivered = no
|	endselect
|}								|#1011283.en
|
|
||********* MIGRATE FROM TIPCS021 ******************
|
|function migrate.project.item.data.by.warehouse()
|{
|	fill.array.for.location.allocated.quantity.project.item()	|#1011283.n
|
|	select 	 tipcs021.* , tipcs020.prtp, tcmcs003.sloc
|	from 	 tipcs021, tipcs020, tcmcs003
|	where 	 tipcs020._index1 = {tipcs021.cprj}
|	and	tcmcs003.cwar = tipcs021.cwar
|	order by tipcs021._index1				|#1011283.n
|	selectdo
|		if tipcs020.prtp = tcyesno.yes then
|			skip.record = false
|			select 	 tppdm605.itmt
|			from 	 tppdm605
|			where 	 tppdm605._index1 = {:tipcs021.cprj, :tipcs021.item}
|			and 	 tppdm605.itmt = tppdm.itmt.item
|			and 	 tppdm605.prit <> tppdm.prit.manufactured
|			as set with 1 rows
|			selectdo
|			selectempty
|				skip.record = true
|			endselect
|			if skip.record then
|				continue
|			endif
|		endif
|		create.item.by.warehouse.from.project()
||		create.inventory.from.project()  				|#214852.o
||		create.inventory.cost.details.from.project()  			|#214852.o
|	endselect
|}
|
|function create.item.by.warehouse.from.project()
|{
|	default.location.type()
| 	 g.new.project.code =  get.project(tipcs021.cprj)
| 	 var.whwmd210.item =  segment.item.code( 	 tipcs021.item,
| 	 	 	 	 	 	 	 g.new.project.code)
| 	 var.whwmd210.valm = 1	|* FTP				|#146674.n
|
|	var.whwmd210.cwar = tipcs021.cwar				|#214852.n
|
|	dump 211
|
|	create.inventory.from.project(var.whwmd210.cwar)  		|#214852.sn
|	create.inventory.cost.details.from.project()
|
|	select	tdilc101.cwar
|	from	tdilc101
|	where	tdilc101._index2 = {:tipcs021.cprj, :tipcs021.item}
|	and	tdilc101.cntr = ""
|	and	tdilc101.cwar <> :tipcs021.cwar
|	group by tdilc101.cwar
|	selectdo
|		var.whwmd210.cwar = tdilc101.cwar
|		dump 211
|
|		create.inventory.from.project(var.whwmd210.cwar)
|		create.inventory.cost.details.from.project()
|	endselect
|									|#214852.en
|
|}
|
||function create.inventory.from.project()					|#214852.o
|function create.inventory.from.project(	domain	tccwar	i.warehouse)	|#214852.n
|{
||	var.whwmd215.cwar = tipcs021.cwar  				|#214852.o
|	var.whwmd215.cwar = i.warehouse				|#214852.n
|
||	g.new.project.code = get.project(tipcs021.cprj)  			|#214852.o
||	var.whwmd215.item = segment.item.code(	tipcs021.item, 	|#214852.o
||						g.new.project.code)  	|#214852.o
|
|	var.whwmd215.item = var.whwmd210.item			|#214852.n
|
|	if is.masterdata.conversion() then
|		init.inventory.fields()
|	else
|		if tdilc000.iilc = tcyesno.no then			|#214852.n
|			var.whwmd215.qhnd = tipcs021.stoc
|		else							|#214852.sn
|			var.whwmd215.qhnd = 0.0
|			select	sum(tdilc101.stks):var.whwmd215.qhnd
|			from	tdilc101
|			where	tdilc101._index2 = {:tipcs021.cprj, :tipcs021.item, "", :var.whwmd215.cwar}
|			as set with 1 rows			|#1011283.n
|			selectdo
|			endselect
|		endif							|#214852.en
|
|		var.whwmd215.qchd = 0.0
|		set.quantities.blocked(
||				tipcs021.cwar,				|#214852.o
|				var.whwmd215.cwar,			|#214852.n
|				tipcs021.item,
|				tipcs021.cprj,				|#214852.n
|				tipcs021.blck,
|				0.0)	|* blocked for inspection
|
|		if var.whwmd215.cwar = tipcs021.cwar then		|#214852.n
|			var.whwmd215.qord = tipcs021.ordr
|		else							|#214852.sn
|			var.whwmd215.qord = 0.0
|		endif							|#214852.en
|		var.whwmd215.qcor = 0.0
|		var.whwmd215.qint = 0.0
|		if var.whwmd215.cwar = tipcs021.cwar then		|#214852.n
|			var.whwmd215.qall = tipcs021.allo
|		else							|#214852.sn
|			var.whwmd215.qall = 0.0
|		endif							|#214852.en
|
|		var.whwmd215.qcal = 0.0
|		if tdilc000.iilc = tcyesno.no then			|#214852.n
|			var.whwmd215.qcom = tipcs021.hall
|		else							|#214852.sn
|			var.whwmd215.qcom = 0.0
|		endif							|#214852.en
|
||		if tdilc000.iilc = tcyesno.yes then		|#219545.so
||			determine.location.allocated.quantity(
|||					tipcs021.cwar, 			|#214852.o
||					var.whwmd215.cwar,		|#214852.n
||					tipcs021.cprj,
||					tipcs021.item,
||					var.whwmd215.qlal)	|#219545.eo
|		if tdilc000.iilc = tcyesno.yes and		|#219545.sn
|		   var.whwmd215.qhnd > 0.0 then
|			determine.location.allocated.quantity.project.item(
|					var.whwmd215.cwar,
|					tipcs021.cprj,
|					tipcs021.item,
|					var.whwmd215.qlal)	|#219545.en
|		else
|			var.whwmd215.qlal = 0.0
|		endif
|		var.whwmd215.qcpr = 0.0
|		var.whwmd215.ltdt = utc(tipcs021.ltdt, 0)
|		var.whwmd215.hstd = utc(0, 0)
|		var.whwmd215.lsid = utc(0, 0)
|		if var.whwmd215.cwar = tipcs021.cwar then		|#214852.n
|			var.whwmd215.qcis = tipcs021.uscu
|		else							|#214852.sn
|			var.whwmd215.qcis = 0.0
|		endif							|#214852.en
|
|		var.whwmd215.qhib = 0.0
|	endif
|
|	dump 215	| *  whwmd215 -  Item Inventory by Warehouse
|	dump 216	| *  whwmd216 -  Inventory by Warehouse, Item and Eff.Unit
|
|}
|
|function create.inventory.cost.details.from.project()
|{
|	initialize.ftpa.amounts()
|
|	| *  Read cost price by customized table.
||	select	tipcs350.cpcp, 						|#CNT.so
||		tipcs350.amte  						|#CNT.eo
|	select	tipcs350.*
|	from	tipcs350
|	where	tipcs350._index1 = { :tipcs021.cprj, :tipcs021.item }
|	selectdo
|									|#CNT.sn
|		for i = 1 to g.mc.depth
|			amount.per.cc(i) = get.amount.per.cost.component(
|						"tipcs350",
|						i)
|		endfor
|									|#CNT.en
|		create.detailed.inventory.cost.details(
|				tipcs021.item,
|				tipcs021.cprj,
|				tipcs350.cpcp,
|				amount.per.cc)				|#CNT.n
||				tipcs350.amte)				|#CNT.o
|
|	endselect
|
|	create.remainder.inventory.cost.details()
|}
|
||* call of function create.detailed.inventory.cost.details() always before
||* call of function create.remainder.inventory.cost.details()
|function create.detailed.inventory.cost.details(
|			domain	tcitem	i.item,
|			domain	tccprj	i.project,
|			domain	tccpcp	i.cost.component,
|		ref	domain	tccopr	i.amount())
||			domain	tccopr	i.amount)			|#CNT.o
|{
|	domain	tcbool		zero.amount			|#236430.n
|	domain	tcamnt		amount				|#236430.n
|									|#CNT.so
||	analyze.cost.component(	tipcs021.item,
||				tipcs021.cprj,
||				tipcs350.cpcp,
||				detail.cost.component,
||				material.component,
||				operation.component,
||				surcharge.component,
||				result)
|									|#CNT.eo
|									|#CNT.sn
|	analyze.cost.component(	i.item,
|				i.project,
|				i.cost.component,
|				detail.cost.component,
|				material.component,
|				operation.component,
|				surcharge.component,
|				result)
|									|#CNT.en
|	on case result
|	case 0:
|		| *  Cost Component in chart
|		| *  Detail Cost Component
|		var.whwmd217.cpcp = detail.cost.component
|		zero.amount = true				|#236430.n
|		for i = 1 to g.mc.depth
|									|#CNT.so
||			var.whwmd217.ftpa(i) =
||				fill.mc.field.double(tipcs350.amte, i)
|									|#CNT.eo
|			var.whwmd217.ftpa(i) = i.amount(i)		|#CNT.n
|								|#236430.sn
|			amount = var.whwmd217.ftpa(i)
|			if double.cmp(amount, 0.0, 0.000001) <> 0 then
|				zero.amount = false
|			endif
|								|#236430.en
|		endfor
|		if is.masterdata.conversion() then
|			e = set.mem(var.whwmd217.mauc, 0)
|		else
||			e = copy.mem(	var.whwmd217.mauc(i),	|#PV.o
||					var.whwmd217.ftpa(i),	|#PV.o
|			e = copy.mem(	var.whwmd217.mauc,	|#PV.n
|					var.whwmd217.ftpa,	|#PV.n
|					g.mc.depth)
|								|#225526.sn
|			for i = 1 to g.mc.depth
|				var.whwmd217.mauc(i) = var.whwmd215.qhnd *
|						var.whwmd217.mauc(i)
|								|#236430.sn
|				amount = var.whwmd217.mauc(i)
|				if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
|					zero.amount = false
|				endif
|								|#236430.en
|			endfor
|								|#225526.en
|		endif
|
|		if not zero.amount then				|#236430.n
|			dump 217 | *  whwmd217 - Item Inventory by Warehouse
|				|* Cost Details
|		endif						|#236430.n
|		break
|	case 1:
|		| *  Merge to aggregated material cost component
|		for i = 1 to g.mc.depth
||			material.component.ftpa(i) =  				|#CNT.eo
||				material.component.ftpa(i) +
||				fill.mc.field.double(tipcs350.amte, i)  	|#CNT.eo
|			material.component.ftpa(i) = 				|#CNT.sn
|				material.component.ftpa(i) + i.amount(i)	|#CNT.en
|
|		endfor
|		break
|	case 2:
|		| *  Merge to aggregated opration cost component
|		for i = 1 to g.mc.depth
||			operation.component.ftpa(i) =  				|#CNT.so
||				operation.component.ftpa(i) +
||				fill.mc.field.double(tipcs350.amte, i)  	|#CNT.eo
|			operation.component.ftpa(i) = 				|#CNT.sn
|				operation.component.ftpa(i) + i.amount(i)	|#CNT.en
|		endfor
|		break
|	case 3:
|		| *  Merge to aggregated surcharge cost component
|		for i = 1 to g.mc.depth
||			surcharge.component.ftpa(i) =  				|#CNT.so
||				surcharge.component.ftpa(i) +
||				fill.mc.field.double(tipcs350.amte, i)  	|#CNT.eo
|			surcharge.component.ftpa(i) = 				|#CNT.sn
|				surcharge.component.ftpa(i) + i.amount(i)	|#CNT.en
|		endfor
|		break
|	endcase
|}
|
|function create.remainder.inventory.cost.details()
|{
|	domain	tcbool		zero.amount			|#236430.n
|	domain	tcamnt		amount				|#236430.n
|
|	|* init the mauc value; overwrite if not masterdata conversion
|	e = set.mem(var.whwmd217.mauc, 0)
|
|	| *  Material Cost Component
|	zero.amount = true					|#236430.n
|	var.whwmd217.cpcp = material.component
|	e = copy.mem(var.whwmd217.ftpa, material.component.ftpa, g.mc.depth)
|	for i = 1 to 3						|#236430.sn
|		amount = var.whwmd217.ftpa(i)
|		if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
|			zero.amount = false
|		endif
|	endfor							|#236430.en
|	if not is.masterdata.conversion() then
|		e = copy.mem(var.whwmd217.mauc, var.whwmd217.ftpa, g.mc.depth)
|								|#225526.sn
|		for i = 1 to g.mc.depth
|			var.whwmd217.mauc(i) = var.whwmd215.qhnd *
|						var.whwmd217.mauc(i)
|								|#236430.sn
|			amount = var.whwmd217.mauc(i)
|			if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
|				zero.amount = false
|			endif
|								|#236430.en
|		endfor
|								|#225526.en
|	endif
|	if not zero.amount then					|#236430.n
|		dump 217 | *  whwmd217 - Item Inventory by Warehouse
|			|* Cost Details
|	endif							|#236430.n
|
|	| *  Operation Cost Component
|	zero.amount = true					|#236430.n
|	var.whwmd217.cpcp = operation.component
|	e = copy.mem(var.whwmd217.ftpa, operation.component.ftpa, g.mc.depth)
|	for i = 1 to 3						|#236430.sn
|		amount = var.whwmd217.ftpa(i)
|		if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
|			zero.amount = false
|		endif
|	endfor							|#236430.en
|	if not is.masterdata.conversion() then
|		e = copy.mem(var.whwmd217.mauc, var.whwmd217.ftpa, g.mc.depth)
|								|#225526.sn
|		for i = 1 to g.mc.depth
|			var.whwmd217.mauc(i) = var.whwmd215.qhnd *
|						var.whwmd217.mauc(i)
|								|#236430.sn
|			amount = var.whwmd217.mauc(i)
|			if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
|				zero.amount = false
|			endif
|								|#236430.en
|		endfor
|								|#225526.en
|	endif
|	if not zero.amount then					|#236430.n
|		dump 217 | *  whwmd217 - Item Inventory by Warehouse
|			|* Cost Details
|	endif							|#236430.n
|
|	| *  Surcharge Cost Component
|	zero.amount = true					|#236430.n
|	var.whwmd217.cpcp = surcharge.component
|	e = copy.mem(var.whwmd217.ftpa, surcharge.component.ftpa, g.mc.depth)
|	for i = 1 to 3						|#236430.sn
|		amount = var.whwmd217.ftpa(i)
|		if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
|			zero.amount = false
|		endif
|	endfor							|#236430.en
|
|	if not is.masterdata.conversion() then
|		e = copy.mem(var.whwmd217.mauc, var.whwmd217.ftpa, g.mc.depth)
|								|#225526.sn
|		for i = 1 to g.mc.depth
|			var.whwmd217.mauc(i) = var.whwmd215.qhnd *
|						var.whwmd217.mauc(i)
|								|#236430.sn
|			amount = var.whwmd217.mauc(i)
|			if double.cmp(amount, 0.0, 0.000001) <> 0.0 then
|				zero.amount = false
|			endif
|								|#236430.en
|		endfor
|								|#225526.en
|	endif
|	if not zero.amount then					|#236430.n
|		dump 217 | *  whwmd217 - Item Inventory by Warehouse
|			|* Cost Details
|	endif							|#236430.n
|}
|
|function read.parameters()
|{
|	select	tipcs000.*
|	from	tipcs000
|	as set with 1 rows
|	selectdo
|	endselect
|
|	select 	 tdilc000.iilc, tdilc000.take
|	from 	 tdilc000
|	as set with 1 rows
|	selectdo
|	endselect
|
|	select	bcstc000.*
|	from	bcstc000
|	as set with 1 rows
|	selectdo
|	endselect
|}
|
|function set.period.variables()
|{
|	if tdinv000.perl = tcperl.month then
|		var.whwmd210.ptyp = 3 |* month.
|		var.whwmd210.nopr = 1 |* nr. of buckets per period.
|	else
|		var.whwmd210.ptyp = 2 |* week
|		var.whwmd210.nopr = 4 |* nr. of buckets per period.
|	endif
|}
|								|#219545.sn
|function determine.location.allocated.quantity.project.item(
||			domain	tccwar	i.warehouse,		|#1011283.so
||			domain	tccprj	i.project,
||			domain	tcitem	i.item,			|#1011283.eo
|			domain	tccwar	i.warehouse	fixed,	|#1011283.sn
|			domain	tccprj	i.project 	fixed,
|			domain	tcitem	i.item		fixed,	|#1011283.en
|		ref	domain	tcqiv1	o.location.allocated.quantity)
|{
||	static	domain	tcitem		s.item			|#1011283.so
||	static	domain	tccprj		s.project
||	static	domain	tccwar		s.warehouse
||	static	domain	tcqiv1		s.location.allocated.quantity
||
||	if s.project <> i.project or
||	   s.item <> i.item or
||	   s.warehouse <> i.warehouse then
||		o.location.allocated.quantity = 0.0
||		select	sum(tdilc401.qsts):o.location.allocated.quantity
||		from	tdilc401
||		where	tdilc401.proc = tcyesno.no
||		and	tdilc401.cwar = :i.warehouse
||		and	tdilc401.item = :i.item
||		and	tdilc401.cprj = :i.project
||		and	tdilc401.qsts > 0.0
||		as set with 1 rows
||		selectdo
||		endselect
||		s.project = i.project
||	   	s.item = i.item
||	   	s.warehouse = i.warehouse
||		s.location.allocated.quantity = o.location.allocated.quantity
||	else
||		s.location.allocated.quantity = o.location.allocated.quantity
||	endif							|#1011283.eo
|
|	domain	tcmcs.str25	search.string			|#1011283.sn
|		long	result
|		long	search_def(1,4)
|		long	length.of.key
|		long	dummy.long
|
|	rdi.domain.string("tcmcs.str25", length.of.key, dummy.long)
|
|	if cwar.cprj.item.array.index = 0 then
|		o.location.allocated.quantity = 0.0
|	else
|		qss.start(search_def, 1, 1)
|		qss.way(search_def, 1, QSS.UP)
|		qss.type(search_def, 1, DB.STRING)
|		qss.length(search_def, 1, length.of.key)
|		search.string = i.warehouse(1;3) & i.project(1;6) & i.item
|		result = qss.search(	QSS.EQUAL,
|					search.string,
|					cwar_cprj_item_array,
|					search_def,
|					cwar.cprj.item.array.index)
|		if result > 0 then
|			o.location.allocated.quantity = qsts_cprj_array(result)
|		else
|			o.location.allocated.quantity = 0.0
|		endif
|	endif							|#1011283.en
|}								|#219545.en
|								|#1011283.sn
|function fill.array.for.location.allocated.quantity.project.item()
|{
|	domain	tcqiv1	location.allocated.quantity
|		long	length.of.key
|		long	array.size
|		long	dummy.long
|
|	cwar.cprj.item.array.index = 0
|	rdi.domain.string("tcmcs.str25", length.of.key, dummy.long)
|
|	select	count(*):array.size
|	from	tipcs021
|	as set with 1 rows
|	selectdo
|	endselect
|
|	alloc.mem(cwar_cprj_item_array, length.of.key, array.size)
|	alloc.mem(qsts_cprj_array, array.size)
|	| clean array
|	set.mem(cwar_cprj_item_array, "")
|
|	select	sum(tdilc401.qsts):location.allocated.quantity,
|		tdilc401.cwar, tdilc401.cprj, tdilc401.item
|	from	tdilc401
|	where	tdilc401.proc = tcyesno.no
|	and	tdilc401.cprj <> ""
|	and	tdilc401.qsts > 0.0
|	group by tdilc401.cwar, tdilc401.cprj, tdilc401.item
|	selectdo
|		cwar.cprj.item.array.index = cwar.cprj.item.array.index + 1
|		cwar_cprj_item_array(1, cwar.cprj.item.array.index) = tdilc401.cwar(1;3) & tdilc401.cprj(1;6) & tdilc401.item
|		qsts_cprj_array(cwar.cprj.item.array.index) = location.allocated.quantity
|	selectempty
|		| no values found with delivered = no
|	endselect
|}								|#1011283.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:bp
:TCMOD:tmm
:TFLNO:100
:FUNC:START
function void fill.bptmm100(
		domain	tcmcs.long	i.type,
		domain	tcmcs.long	i.seqn,
		domain	bcstr22		i.rgdt,
		domain	bcstr22		i.trdt,			|#147118.n
		domain	tisfc.prtm	i.hrea,
		domain	tccwtt		i.cwtt,			|#200-100940.n
		domain	bcstr3		i.chlt,
		domain	tisfc.prtm	i.hrma,
		domain	bcstr22		i.stdt,
		domain	bcstr22		i.endt,
		domain	tcprcg		i.perc,
		domain	tctxtn		i.txta)
{
|	bptmm100.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm100.year = tihra100.year		|* FILLED IN RULE
|	bptmm100.peri = tihra100.week		|* FILLED IN RULE
	on case i.type
	case GENERAL_TASK:
		var.bptmm100.type = target(bptmm.type.general)
		break
	case PCS:
		var.bptmm100.type = target(bptmm.type.pcs.project)
		break
	case PRODUCTION:
		var.bptmm100.type = target(bptmm.type.prod.order)
		break
	case SERVICE:
		var.bptmm100.type = target(bptmm.type.serv.order)
		break
	endcase
	var.bptmm100.seqn = i.seqn
								|#147118.so
|	bptmm100.trdt = utc(tihra100.trdt, 0)	|* FILLED IN RULE
								|#147118.eo
	var.bptmm100.trdt = i.trdt				|#147118.n
|	bptmm100.htst = tihra100.htst		|* FILLED IN RULE
	var.bptmm100.rgdt = i.rgdt
|	bptmm100.cwoc = tihra100.cwoc		|* FILLED IN RULE
|	bptmm100.wtsc = tihra100.cwtt				|#200-100940.o
	var.bptmm100.wtsc = i.cwtt				|#200-100940.n
|	bptmm100.cuni = bcsbp000.dsbt		|* FILLED IN RULE
	var.bptmm100.hrea = i.hrea
	var.bptmm100.chlt = i.chlt
	var.bptmm100.hrma = i.hrma
	var.bptmm100.stdt = i.stdt
|	bptmm100.stup = target(tcyesno.no)	|* FILLED IN RULE
	var.bptmm100.endt = i.endt
	var.bptmm100.perc = i.perc
|	bptmm100.cnfd = target(tcyesno.no)	|* FILLED IN RULE
|	bptmm100.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm100.cgrp = empty()		|* FILLED IN RULE
|	bptmm100.hrdv = target(tcyesno.no)	|* FILLED IN RULE
|	bptmm100.proc = target(tcyesno.no)	|* FILLED IN RULE
|	bptmm100.logn = logname$		|* FILLED IN RULE
|	bptmm100.agui = empty()			|* FILLED IN RULE
|	bptmm100.drpr = tihra100.crpr		|* FILLED IN RULE
|	bptmm100.crpr = tihra100.srpr		|* FILLED IN RULE
	var.bptmm100.txta = i.txta
							|#GEN-0205.sn
	var.bptmm100.emdp = 
		get.employee.department(tihra100.emno)
							|#GEN-0205.en
	dump 100
}

function void fill.bptmm111()
{
	domain	tisfc.prtm	day.hrea
	domain	tctxtn		day.txta
	domain	tcmcs.long	day.seq.nr

	domain	bcstr22		start.date
	domain	bcstr22		reg.date

|	bptmm111.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm111.year = tihra100.year			|* FILLED IN RULE
|	bptmm111.peri = tihra100.week			|* FILLED IN RULE
|	bptmm111.type = target(bptmm.type.general)	|* FILLED IN RULE
|	bptmm111.seqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm111.cgrp = empty()				|* FILLED IN RULE
|	bptmm111.cprj = empty()				|* FILLED IN RULE
|	bptmm111.cspa = empty()				|* FILLED IN RULE
|	bptmm111.cact = empty()				|* FILLED IN RULE
|	bptmm111.cstl = empty()				|* FILLED IN RULE
|	bptmm111.swct = target(bpmdm.swct.labor)	|* FILLED IN RULE
|	bptmm111.ccun = empty()				|* FILLED IN RULE

|	bptmm111.seqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration
	var.bptmm111.seqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				GENERAL_TASK)		
								|#158562.en
				
	ret = get.general.task(	bcsbp.orig.ti,
				str$(tihra100.tano),
				0,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm111.gtsk = bcsbp004.ntsk
		var.bptmm111.dsca = bcsbp004.ntsd
	else
		var.bptmm111.gtsk = empty()
		var.bptmm111.dsca = empty()
	endif
|	bptmm111.expn = empty()				|* FILLED IN RULE
	var.bptmm111.cpcp = bcsbp000.cpgh
|	bptmm111.cwoc = tihra100.cwoc			|* FILLED IN RULE
	set.mem(var.bptmm111.hrea, 0.0)
	day.number = convert.dayname.to.daynumber(tihra100.stdy)|#PERF.n
								|#PERF.so
|	day.number = itihra0001.convert.dayname.to.daynumber(tihra100.stdy)
								|#PERF.eo
	var.bptmm111.hrea(day.number) = tihra100.hrea
	var.bptmm111.hrea(8) = tihra100.hrea
	ret = get.labor.type(	bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm111.chlt = bcsbp002.nhlt
	else
		var.bptmm111.chlt = empty()
	endif
|	bptmm111.rgdt = empty()				|* FILLED IN RULE
								|#147118.so
|	bptmm111.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm111.trdt = get.transaction.date(tihra100.trdt)	|#147118.n
|	bptmm111.quan = empty()				|* FILLED IN RULE
|	bptmm111.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm111.pric = empty()				|* FILLED IN RULE
|	bptmm111.ccur = empty()				|* FILLED IN RULE
|	bptmm111.amnt = empty()				|* FILLED IN RULE
|	bptmm111.rdat = empty()				|* FILLED IN RULE
|	bptmm111.rate = empty()				|* FILLED IN RULE
|	bptmm111.rate(2) = empty()			|* FILLED IN RULE
|	bptmm111.rate(3) = empty()			|* FILLED IN RULE
|	bptmm111.ratf = empty()				|* FILLED IN RULE
|	bptmm111.ratf(2) = empty()			|* FILLED IN RULE
|	bptmm111.ratf(3) = empty()			|* FILLED IN RULE
|	bptmm111.rtyp = empty()				|* FILLED IN RULE
|	bptmm111.cnfd = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm111.proc = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm111.hrdv = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm111.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm111.logn = logname$			|* FILLED IN RULE
|	bptmm111.blbl = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm111.agui = empty()				|* FILLED IN RULE
	day.txta = fill.text.fields(day.number)

	dump 111

	if day.number <> 0 then
		day.hrea = var.bptmm111.hrea(day.number)
|		day.seq.nr = (tihra100.wsrn * 10) + day.number		|#158562.o
		day.seq.nr = (var.bptmm111.seqn * 10) + day.number	|#158562.n
	else
		day.hrea = var.bptmm111.hrea(8)
|		day.seq.nr = (tihra100.wsrn * 10) + 8			|#158562.o
		day.seq.nr = (var.bptmm111.seqn * 10) + 8		|#158562.n
	endif

	start.date = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				1200)
	|* Time part of start date for General Tasks is always 12:00:00.

	fill.bptmm150(
		day.seq.nr,
		start.date,
		var.bptmm111.gtsk,
		var.bptmm111.dsca,
		var.bptmm111.cpcp)

	reg.date = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
	|* The registration date in bptmm100 is always the date part of
	|* the start date.

	fill.bptmm100(
		GENERAL_TASK,
		day.seq.nr,
		reg.date,
		var.bptmm111.trdt,				|#147118.n
		day.hrea,
		"",		|* Working Time Schedule Code	|#200-100940.n
		var.bptmm111.chlt,
		0.0,		|* Machine Time
		start.date,
		utc(0, 0),	|* End Date
		0.0,		|* Percentage Completed
		day.txta)
}

function void fill.bptmm120()
{
|	bptmm120.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm120.year = tihra100.year			|* FILLED IN RULE
|	bptmm120.peri = tihra100.week			|* FILLED IN RULE
|	bptmm120.seqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm120.seqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm120.seqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				PRODUCTION)		
								|#158562.en
	var.bptmm120.stdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	ret = get.labor.type(	bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm120.chlt = bcsbp002.nhlt
	else
		var.bptmm120.chlt = ""
	endif
	var.bptmm120.wtsc = get.wts.code(tihra100.cwtt,		|#200-100940.sn
					 tihra100.stdy,
					 tihra100.sttm,
					 tihra100.entm,
					 var.bptmm120.chlt)	|#200-100940.en
|	if not isspace(tihra100.cwtt) then			|#200-100940.o
	if not isspace(var.bptmm120.wtsc) then			|#200-100940.n
		var.bptmm120.endt = convert.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm120.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm120.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm120.trdt = get.transaction.date(tihra100.trdt)	|#147118.n

	var.bptmm120.rgdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm120.htst = tihra100.htst			|* FILLED IN RULE
|	bptmm120.cgrp = empty()				|* FILLED IN RULE
|	bptmm120.wtsc = tihra100.cwtt				|#200-100940.o
|	bptmm120.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm120.hrea = tihra100.hrea			|* FILLED IN RULE
|	bptmm120.hrma = tihra100.hrma			|* FILLED IN RULE
|	bptmm120.stup = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm120.orno = convert.number(bcckon.act.sfc, tihra100.pdno)	|* FILLED IN RULE
|	bptmm120.opno = tihra100.opno			|* FILLED IN RULE
|	bptmm120.tano = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm120.mcno = tihra100.mcno			|* FILLED IN RULE
|	bptmm120.perc = tihra100.pccp			|* FILLED IN RULE
|	bptmm120.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm120.cprj = get.ti.project(tihra100.cprj)	|* FILLED IN RULE
|	bptmm120.cact = tihra100.cact			|* FILLED IN RULE
|	bptmm120.hrdv = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm120.cnfd = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm120.proc = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm120.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm120.logn = logname$			|* FILLED IN RULE
|	bptmm120.agui = empty()				|* FILLED IN RULE
|	bptmm120.txta = tihra100.txta			|* FILLED IN RULE

	dump 120

	fill.bptmm100(
		PRODUCTION,
|		tihra100.wsrn,					|#158562.o
		var.bptmm120.seqn,				|#158562.n
		var.bptmm120.rgdt,
		var.bptmm120.trdt,				|#147118.n
		tihra100.hrea,
		var.bptmm120.wtsc,				|#200-100940.n
		var.bptmm120.chlt,
		tihra100.hrma,
		var.bptmm120.stdt,
		var.bptmm120.endt,
		tihra100.pccp,
		tihra100.txta)
}

function void fill.bptmm130()
{
|	bptmm130.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm130.year = tihra100.year			|* FILLED IN RULE
|	bptmm130.peri = tihra100.week			|* FILLED IN RULE
|	bptmm130.seqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm130.seqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm130.seqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				SERVICE)		
								|#158562.en

	var.bptmm130.stdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	ret = get.labor.type(	bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm130.chlt = bcsbp002.nhlt
	else
		var.bptmm130.chlt = ""
	endif
	var.bptmm130.wtsc = get.wts.code(tihra100.cwtt,		|#200-100940.sn
					 tihra100.stdy,
					 tihra100.sttm,
					 tihra100.entm,
					 var.bptmm130.chlt)	|#200-100940.en
|	if not isspace(tihra100.cwtt) then			|#200-100940.o
	if not isspace(var.bptmm130.wtsc) then			|#200-100940.n
		var.bptmm130.endt = convert.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm130.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm130.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm130.trdt = get.transaction.date(tihra100.trdt)	|#147118.n

	var.bptmm130.rgdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm130.cgrp = empty()				|* FILLED IN RULE
|	bptmm130.wtsc = tihra100.cwtt				|#200-100940.o
|	bptmm130.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm130.hrea = tihra100.hrea			|* FILLED IN RULE
|	bptmm130.orno = convert.number(bcckon.sma.order, tihra100.pdno)	|* FILLED IN RULE
|	bptmm130.stsk = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm130.acln = empty()				|* FILLED IN RULE
|	bptmm130.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm130.lbtv = target(bptmm.serl.labo)		|* FILLED IN RULE
|	bptmm130.lbln = empty()				|* FILLED IN RULE
|	bptmm130.tvln = empty()				|* FILLED IN RULE
	var.bptmm130.cpcp = get.cost.component.from.ts(
					tihra100.pdno,
					tihra100.tano)
|	bptmm130.hrdv = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm130.cnfd = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm130.proc = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm130.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm130.logn = logname$			|* FILLED IN RULE
|	bptmm130.agui = empty()				|* FILLED IN RULE
|	bptmm130.txta = tihra100.txta			|* FILLED IN RULE

	dump 130

	fill.bptmm100(
		SERVICE,
|		tihra100.wsrn,					|#158562.o
		var.bptmm130.seqn,				|#158562.n
		var.bptmm130.rgdt,
		var.bptmm130.trdt,				|#147118.n
		tihra100.hrea,
		var.bptmm130.wtsc,				|#200-100940.n
		var.bptmm130.chlt,
		0.0,		|* Machine Time
		var.bptmm130.stdt,
		var.bptmm130.endt,
		0.0,		|* Percentage Completed
		tihra100.txta)
}

function void fill.bptmm150(
		domain	tcmcs.long	i.seqn,
		domain	bcstr22		i.stdt,
		domain	bcstr8		i.gtsk,
		domain	tcdsca		i.dsca,
		domain	bcstr8		i.cpcp)
{
|	bptmm150.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm150.year = tihra100.year			|* FILLED IN RULE
|	bptmm150.peri = tihra100.week			|* FILLED IN RULE
	var.bptmm150.seqn = i.seqn
|	bptmm150.cgrp = empty()			|* FILLED IN RULE
	var.bptmm150.stdt = i.stdt
	var.bptmm150.gtsk = i.gtsk
|	bptmm150.expn = empty()			|* FILLED IN RULE
	var.bptmm150.dsca = i.dsca
|	bptmm150.cwoc = tihra100.cwoc		|* FILLED IN RULE
	var.bptmm150.cpcp = i.cpcp
|	bptmm150.quan = empty()			|* FILLED IN RULE
|	bptmm150.cuni = bcsbp000.dsbt		|* FILLED IN RULE
|	bptmm150.pric = empty()			|* FILLED IN RULE
|	bptmm150.ccur = empty()			|* FILLED IN RULE
|	bptmm150.amnt = empty()			|* FILLED IN RULE
|	bptmm150.rdat = empty()			|* FILLED IN RULE
|	bptmm150.rate(1) = empty()		|* FILLED IN RULE
|	bptmm150.rate(2) = empty()		|* FILLED IN RULE
|	bptmm150.rate(3) = empty()		|* FILLED IN RULE
|	bptmm150.ratf(1) = empty()		|* FILLED IN RULE
|	bptmm150.rate(2) = empty()		|* FILLED IN RULE
|	bptmm150.rate(3) = empty()		|* FILLED IN RULE
|	bptmm150.rtyp = empty()			|* FILLED IN RULE

	dump 150
}

function void fill.bptmm170()
{
|	bptmm170.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm170.year = tihra100.year			|* FILLED IN RULE
|	bptmm170.peri = tihra100.week			|* FILLED IN RULE
|	bptmm170.seqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm170.seqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm170.seqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				PCS)		
								|#158562.en

	var.bptmm170.stdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	ret = get.labor.type(	bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret = 0 then
		var.bptmm170.chlt = bcsbp002.nhlt
	else
		var.bptmm170.chlt = ""
	endif
	var.bptmm170.wtsc = get.wts.code(tihra100.cwtt,		|#200-100940.sn
					 tihra100.stdy,
					 tihra100.sttm,
					 tihra100.entm,
					 var.bptmm170.chlt)	|#200-100940.en
|	if not isspace(tihra100.cwtt) then			|#200-100940.o
	if not isspace(var.bptmm170.wtsc) then			|#200-100940.n
		var.bptmm170.endt = convert.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm170.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm170.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm170.trdt = get.transaction.date(tihra100.trdt)	|#147118.n

	var.bptmm170.rgdt = convert.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm170.htst = tihra100.htst			|* FILLED IN RULE
|	bptmm170.cgrp = empty()				|* FILLED IN RULE
|	bptmm170.wtsc = tihra100.cwtt				|#200-100940.o
|	bptmm170.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm170.hrea = tihra100.hrea			|* FILLED IN RULE
|	bptmm170.hrma = tihra100.hrma			|* FILLED IN RULE
|	bptmm170.cprj = get.ti.project(tihra100.cprj)	|* FILLED IN RULE
|	bptmm170.cact = tihra100.cact			|* FILLED IN RULE
|	bptmm170.tano = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm170.mcno = tihra100.mcno			|* FILLED IN RULE
|	bptmm170.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm170.hrdv = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm170.cnfd = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm170.proc = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm170.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm170.logn = logname$			|* FILLED IN RULE
|	bptmm170.agui = empty()				|* FILLED IN RULE
|	bptmm170.txta = tihra100.txta			|* FILLED IN RULE

	dump 170

	fill.bptmm100(
		PCS,
|		tihra100.wsrn,					|#158562.o
		var.bptmm170.seqn,				|#158562.n
		var.bptmm170.rgdt,
		var.bptmm170.trdt,				|#147118.n
		tihra100.hrea,
		var.bptmm170.wtsc,				|#200-100940.n
		var.bptmm170.chlt,
		tihra100.hrma,
		var.bptmm170.stdt,
		var.bptmm170.endt,
		0.0,		|* Percentage Completed
		tihra100.txta)
}

function string convert.day.to.utc.date(
		domain	tcyrno		i.year,
		domain	tcweek		i.week,
		domain	tcdynm		i.dayname,
		domain	tctmhs		i.time)
{
|	Dllusage
|	Expl:	This function calculates the utc-date based on an input day and
|		an input time.
|	Input:
|		i.year - Year
|		i.week - Week Number
|		i.dayname - Enumerate (Monday, Tuesday, etc.)
|		i.time - Time (interpretation depends on tihra100.cltm)
|	Enddllusage

		domain	tcmcs.long	l.day.number
		domain	tcmcs.long	l.utc.date.part
		domain	tcmcs.long	l.utc.time.part
		domain	tcmcs.long	l.number.of.hours

	l.day.number = convert.dayname.to.daynumber(i.dayname)	|#PERF.sn
	l.utc.date.part = week.to.num(	i.week,
					i.year,
					l.day.number)		|#PERF.en
								|#PERF.so
|	l.utc.date.part = itihra0001.convert.dayname.to.date(	i.year,
|								i.week,
|								i.dayname)
								|#PERF.eo
	if i.time <> 0 then
		read.hra.parameters()

		if tihra000.cltm = tccltm.minutes then
		|* 850 is to be interpreted as 08:50.
			l.number.of.hours = i.time/100
			l.utc.time.part = (l.number.of.hours * 60 * 60) +
				((i.time - l.number.of.hours * 100) * 60)
		else
		|* 850 is to be interpreted as 08:30.
			l.number.of.hours = i.time/100
			l.utc.time.part = (l.number.of.hours * 60 * 60) +
				((i.time - l.number.of.hours * 100) *
							tihra000.perl * 60)
		endif
	else
		l.utc.time.part = 0
		return(utc(l.utc.date.part, l.utc.time.part, 2)) |#225709.n
	endif

	return(utc(l.utc.date.part, l.utc.time.part))
}

function domain tcmcs.long convert.dayname.to.daynumber(	|#PERF.sn
		domain	tcdynm		i.day.name)
{
		domain	tcmcs.long	l.ret
		string			l.dummy
	static	domain	tcncmp		s.company
	static	domain	tcmcs.long	s.first.day.of.week
		domain	tcmcs.long	l.day.nr

	if s.company <> ext.lng.comp then
		l.ret = tt.company(	ext.lng.comp,
					l.dummy,
					l.dummy,
					s.first.day.of.week)
		s.company = ext.lng.comp
	endif

	if i.day.name = empty then
		i.day.name = ltoe(s.first.day.of.week)
	endif

	on case i.day.name
		case tcdynm.sunday:
			l.day.nr = 1
			break
		case tcdynm.monday:
			l.day.nr = 2
			break
		case tcdynm.tuesday:
			l.day.nr = 3
			break
		case tcdynm.wednesday:
			l.day.nr = 4
			break
		case tcdynm.thursday:
			l.day.nr = 5
			break
		case tcdynm.friday:
			l.day.nr = 6
			break
		case tcdynm.saturday:
			l.day.nr = 7
			break
		default:
			l.day.nr = 0
			break
	endcase

	if l.day.nr <> 0 then
		|* Make correction for first day of the week
		l.day.nr = l.day.nr + 1 + (7 - s.first.day.of.week)
		if l.day.nr > 7 then
			l.day.nr = l.day.nr - 7
		endif
	endif

	return(l.day.nr)
}								|#PERF.en

function domain tccwtt get.wts.code(				|#200-100940.sn
		domain	tccwtt		i.working.time.table,
		domain	tcdynm		i.day.name,
		domain	tctmhs		i.start.time,
		domain	tctmhs		i.end.time,
		domain	tcckow		i.labor.type)
{
	|* In BaanIVc4 the labor type of the working time table serves as
	|* default value for the hours record.
	|* In BaanERP6.1a the labor type of the hours record must be according
	|* to the labor type as it is present in the working time schedule.
	|* The function below checks if the labor type of the hours record is
	|* according to the working time table. Is this is not the case, then
	|* the working time table is set to empty (code in selectempty). In this
	|* way the BaanVI DAL checks is working fine. This solution is
	|* acceptable, because after having saved an hours record, the working
	|* time table has not any functionality anymore.

		domain	tctmhs		wtt.start.time
		domain	tctmhs		wtt.end.time

	if isspace(i.working.time.table) or
	   isspace(i.labor.type) then
		return("")
	endif

	select	tihra120.sttm :wtt.start.time,
		tihra120.entm :wtt.end.time
	from	tihra120
	where	tihra120._index1 = {	:i.working.time.table,
					:i.day.name}
	and	tihra120.ckow = :i.labor.type
	selectdo
		if i.start.time >= wtt.start.time and
		   i.end.time <= wtt.end.time then
			return(i.working.time.table)
		endif
	selectempty
		return("")
	endselect

	return("")
}								|#200-100940.en

function domain	tctxtn fill.text.fields(
		domain	tcmcs.long	day.number)
{
	var.bptmm111.txta = 0

	var.bptmm111.txtb = 0
	var.bptmm111.txtc = 0
	var.bptmm111.txtd = 0
	var.bptmm111.txte = 0
	var.bptmm111.txtf = 0
	var.bptmm111.txtg = 0

	on case day.number
	case 0:
	case 1:
		var.bptmm111.txta = tihra100.txta

		return(var.bptmm111.txta)
		break
	case 2:
		var.bptmm111.txtb = tihra100.txta
		return(var.bptmm111.txtb)
		break
	case 3:
		var.bptmm111.txtc = tihra100.txta
		return(var.bptmm111.txtc)
		break
	case 4:
		var.bptmm111.txtd = tihra100.txta
		return(var.bptmm111.txtd)
		break
	case 5:
		var.bptmm111.txte = tihra100.txta
		return(var.bptmm111.txte)
		break
	case 6:
		var.bptmm111.txtf = tihra100.txta
		return(var.bptmm111.txtf)
		break
	case 7:
		var.bptmm111.txtg = tihra100.txta
		return(var.bptmm111.txtg)
		break
	default:
		return(0)
	endcase

	return(0)
}

function void read.hra.parameters()
{
	static	domain	tcbool		s.hra.parameters.read

	if s.hra.parameters.read then
		return
	endif

	select	tihra000.cltm, tihra000.perl
	from	tihra000
	where	tihra000._index1 = {1}
	as set with 1 rows
	selectdo
		s.hra.parameters.read = true
	endselect
}

function domain tccpcp get.cost.component.from.ts(
		domain	tcpdno		i.orno,
		domain	tctano		i.task)
{
		domain	tccuno		l.customer
		domain	tccpcp		l.cost.component
		domain	tcitem		l.item
		domain	tcpric		o.dummy.pric
		domain	tssma.mess	o.dummy.mess

	l.customer = read.customer.from.service.order(i.orno)

	|if itssma0011.search.rate.from.task(		|#MCR.so
	|				l.customer,
	|				i.task,
	|				l.item,		|* Output
	|				o.dummy.pric,
	|				o.dummy.mess) then
							|#MCR.eo
	if search.rate.from.task(			|#MCR.sn
				l.customer,
				i.task,
				l.item,			|* Output
				o.dummy.pric,
				o.dummy.mess) then	|#MCR.en
		l.cost.component = read.cost.component.from.item(l.item)
		get.cost.components(
				bcorig.ticpr,
				l.cost.component,
				"",
				dummy.err.mess)
		return(bcstc006.cost)
	else
		l.cost.component = bcsbp000.cpcp
		return(l.cost.component)
	endif
}

function domain tccuno read.customer.from.service.order(
		domain	tcpdno		i.orno)
{
	static	domain	tcpdno		s.orno
	static	domain	tccuno		s.cuno

	if s.orno = i.orno then
		return(s.cuno)
	endif

	select	tssma301.cuno :s.cuno
	from	tssma301
	where	tssma301._index1 = {:i.orno}
	as set with 1 rows
	selectdo
		s.orno = i.orno
	endselect

	return(s.cuno)
}

function domain tccpcp read.cost.component.from.item(
		domain	tcitem		i.item)
{
	static	domain	tcitem		s.item
	static	domain	tccpcp		s.cpcp

	if s.item = i.item then
		return(s.cpcp)
	endif


	select	tiitm001.cpcp :s.cpcp
	from	tiitm001
	where	tiitm001._index1 = {:i.item}
	as set with 1 rows
	selectdo
		s.item = i.item
	endselect

	return(s.cpcp)
}

function domain	bcstr22 get.transaction.date(			|#147118.sn
		domain	tidate		i.transaction.date)
{
	if i.transaction.date = 0 then
		|* Transaction Date not filled; fill with
		|* current time stamp.
		return( utc(date.num(), time.num()) )
	endif
		
	return( utc(i.transaction.date, 0) )
}								|#147118.en

								|#158562.sn
function domain tcmcs.long get.sequence.number(
		domain	tcyrno		i.year,
		domain	tcweek		i.week,
		domain	tcemno		i.emno,
		domain	tcmcs.long	i.type)
{
|	Dllusage
|	Expl:	This function determines the sequence number. 
|		The sequence number is not taken anymore from BaanIV because
|		there were too large numbers present which did not fit in the domain
|		present in LN (maximum of domain bpmdm.serd is 9999; maximum
|		of domain tihra.sern is 99999).
|		Now tihra100 is read with order by on year, week, emno, koht. Then
|		we can determine the sequence number in the right order to be used
|		in LN.
|	Input:
|		i.year - Year
|		i.week - Week Number
|		i.emno - Employee
|		i.type - Indicates type of hours
|	Enddllusage

	static	domain	tcyrno		hold.year
	static	domain	tcweek		hold.week
	static	domain	tcemno		hold.emno
	static	domain	tcmcs.long	hold.type
	static	domain	tcmcs.long	hold.sequence

	if hold.year = i.year and
	   hold.week = i.week and
	   hold.emno = i.emno and
	   hold.type = i.type then
		hold.sequence = hold.sequence + 1
		return(hold.sequence)
	endif

	hold.year = i.year
	hold.week = i.week
	hold.emno = i.emno
	hold.type = i.type
	hold.sequence = 1
	return(hold.sequence)
}								|#158562.en
								|#GEN-0205.sn
function domain tccwoc get.employee.department(
		domain	tcemno		i.employee)
{
	static	domain	tcemno		s.employee
	static	domain	tccwoc		s.employee.department	fixed

	if s.employee = i.employee then
		return(s.employee.department)
	endif

	s.employee = i.employee
	
	select	tccom001.cwoc:s.employee.department
	from	tccom001
	where	tccom001._index1 = {:i.employee}
	as set with 1 rows
	selectdo
		break
	selectempty
		s.employee.department = ""
	endselect
	
	return(s.employee.department)
}
								|#GEN-0205.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:bp
:TCMOD:tmm
:TFLNO:200
:FUNC:START
function void fill.and.dump.bptmm200(
		domain	tcemno		i.original.employee.code, |#GEN-0205.n
		domain	bcstr6		i.emno,
		domain	tcyrno		i.year,
		domain	tcpern		i.peri,
		domain	tcmcs.long	i.type,
		domain	tcmcs.long	i.seqn,
		domain	bcstr6		i.cwoc,
		domain	bcstr3		i.wtsc,
		domain	tisfc.prtm	i.hrea,
		domain	tisfc.prtm	i.hrma,
		domain	bcstr3		i.chlt,
		domain	bcstr22		i.stdt,
		domain	bcstr22		i.endt,
		domain	tcprcg		i.perc,
		domain	bcstr22		i.rgdt,
|		domain	tcamnt		i.prlc,			|#MCR.so
|		domain	tcamnt		i.prmc,
|		domain	tcamnt		i.proc,			|#MCR.eo
	ref	domain	tcamnt		i.prlc(),		|#MCR.sn
	ref	domain	tcamnt		i.prmc(),
	ref	domain	tcamnt		i.proc(),		|#MCR.en
		domain	bcstr22		i.trdt,
		domain	tclogn		i.logn,
		domain	tctxtn		i.txta)
{
	var.bptmm200.emno = fill.field.string(i.emno)
|	var.bptmm200.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
	var.bptmm200.year = fill.field.long(i.year)
	var.bptmm200.peri = fill.field.long(i.peri)
	on case i.type
	case GENERAL_TASK:
		var.bptmm200.type = 1
		break
	case PROJECT:
		var.bptmm200.type = 2
		break
	case PCS:
		var.bptmm200.type = 3
		break
	case PRODUCTION:
		var.bptmm200.type = 4
		break
	case SERVICE_TI:
	case SERVICE_TP:
		var.bptmm200.type = 6
		break
	endcase
	var.bptmm200.psqn = fill.field.long(i.seqn)
	var.bptmm200.cwoc = fill.field.string(i.cwoc)
	var.bptmm200.wtsc = fill.field.string(i.wtsc)
|	bptmm200.cuni = bcsbp000.dsbt			|* FILLED IN RULE
	var.bptmm200.hrea = fill.field.double(i.hrea)
	var.bptmm200.hrma = fill.field.double(i.hrma)
	var.bptmm200.chlt = fill.field.string(i.chlt)
|	bptmm200.stup = target(tcyesno.no)		|* FILLED IN RULE
	var.bptmm200.stdt = fill.field.string(i.stdt)
	var.bptmm200.endt = fill.field.string(i.endt)
	var.bptmm200.perc = fill.field.double(i.perc)
|	bptmm200.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
	var.bptmm200.rgdt = fill.field.string(i.rgdt)

|	var.bptmm200.prlc = fill.field.double(i.prlc)		|#MCR.so
|	var.bptmm200.prmc = fill.field.double(i.prmc)
|	var.bptmm200.proc = fill.field.double(i.proc)		|#MCR.eo

	var.bptmm200.prlc(1) = fill.field.double(i.prlc(1))	|#MCR.sn
	var.bptmm200.prlc(2) = fill.field.double(i.prlc(2))
	var.bptmm200.prlc(3) = fill.field.double(i.prlc(3))
	var.bptmm200.prmc(1) = fill.field.double(i.prmc(1))
	var.bptmm200.prmc(2) = fill.field.double(i.prmc(2))
	var.bptmm200.prmc(3) = fill.field.double(i.prmc(3))
	var.bptmm200.proc(1) = fill.field.double(i.proc(1))
	var.bptmm200.proc(2) = fill.field.double(i.proc(2))
	var.bptmm200.proc(3) = fill.field.double(i.proc(3))	|#MCR.en

	var.bptmm200.trdt = fill.field.string(i.trdt)
|	bptmm200.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm200.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm200.logn = fill.field.string(i.logn)
	var.bptmm200.txta = fill.field.long(i.txta)
							|#GEN-0205.sn
	var.bptmm200.emdp = 
		get.employee.department(i.original.employee.code)
							|#GEN-0205.en
	dump 200
}

function void fill.and.dump.bptmm210(
		domain	tcmcs.long	i.psqn,
		domain	bcstr22		i.stdt,
		domain	tcmcs.long	i.swct,
		domain	bcstr8		i.cpcp)
{
|	bptmm210.emno = format.emno(tphrs100.emno)	|* FILLED IN RULE
|	bptmm210.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
|	bptmm210.year = tphrs100.year			|* FILLED IN RULE
|	bptmm210.peri = tphrs100.peri			|* FILLED IN RULE
	var.bptmm210.psqn = i.psqn
	var.bptmm210.stdt = i.stdt
|	bptmm210.cprj = get.tp.project(tphrs100.cprj)	|* FILLED IN RULE
	var.bptmm210.swct = i.swct
|	bptmm210.cspa = tphrs100.cspa			|* FILLED IN RULE
|	bptmm210.cact = tphrs100.cact			|* FILLED IN RULE
|	bptmm210.cstl = tphrs100.cstl			|* FILLED IN RULE
	var.bptmm210.cpcp = i.cpcp
|	bptmm210.ccun = tphrs100.ccun			|* FILLED IN RULE
	var.bptmm210.dsca = get.cost.object.description(
				tphrs100.cotp,
				tphrs100.ccun)
|	bptmm210.quan = empty()				|* FILLED IN RULE
|	bptmm210.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm210.pric = empty()				|* FILLED IN RULE
|	bptmm210.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
|	bptmm210.amnt = tphrs100.amnt			|* FILLED IN RULE
	if tphrs100.amnt = 0.0 then
		|* Hours record
		var.bptmm210.rdat = utc(0, 0)
		var.bptmm210.rate = empty()
		var.bptmm210.ratf = empty()
		var.bptmm210.rtyp = empty()
	else
		var.bptmm210.rdat = i.stdt
		var.bptmm210.rate = 1.0
		var.bptmm210.ratf = 1
		var.bptmm210.rtyp = get.internal.exchange.rate.type()
	endif
|	bptmm210.rate(2) = empty()			|* FILLED IN RULE
|	bptmm210.rate(3) = empty()			|* FILLED IN RULE
|	bptmm210.ratf(2) = empty()			|* FILLED IN RULE
|	bptmm210.ratf(3) = empty()			|* FILLED IN RULE

	dump 210
}

function void fill.and.dump.bptmm211.from.tihra100()
{
	domain	tcmcs.long	l.day.number
	domain	tcmcs.long	l.day.seq.nr			|#158562.n
	domain	tctxtn		l.day.txta

	domain	bcstr22		l.start.date
	domain	bcstr22		l.reg.date

	domain	tcmcs.long	l.ret				|#MCR.sn
	domain	tcamnt		var.211.prlc(3)
	domain	tcamnt		dummy.var.prmc(3)
	domain	tcamnt		dummy.var.proc(3)		|#MCR.en


	var.bptmm211.emno = format.emno(tihra100.emno)
|	bptmm211.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
	var.bptmm211.year = tihra100.year
	var.bptmm211.peri = tihra100.week
	var.bptmm211.type = target(bptmm.type.general)
|	var.bptmm211.psqn = tihra100.wsrn			|#158562.o
	var.bptmm211.psqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				GENERAL_TASK)		
								|#158562.en

	var.bptmm211.cprj = empty()
	var.bptmm211.cspa = empty()
	var.bptmm211.cact = empty()
	var.bptmm211.cstl = empty()
	var.bptmm211.swct = target(bpmdm.swct.labor)
	var.bptmm211.ccun = empty()
	ret.long = get.general.task(
				bcsbp.orig.ti,
				str$(tihra100.tano),
				0,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm211.gtsk = bcsbp004.ntsk
		var.bptmm211.dsca = bcsbp004.ntsd
	else
		var.bptmm211.gtsk = empty()
		var.bptmm211.dsca = empty()
	endif
	var.bptmm211.expn = empty()
	var.bptmm211.cpcp = bcsbp000.cpgh |remark 800-137648
	var.bptmm211.cwoc = tihra100.cwoc
	set.mem(var.bptmm211.hrea, 0.0)
								|#PERF.so
|	l.day.number = itihra0001.convert.dayname.to.daynumber(tihra100.stdy)
								|#PERF.eo
								|#PERF.sn
	l.day.number = convert.dayname.to.daynumber(tihra100.stdy)
								|#PERF.en
	var.bptmm211.hrea(l.day.number) = tihra100.hrea
	var.bptmm211.hrea(8) = tihra100.hrea
	ret.long = get.labor.type(
				bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm211.chlt = bcsbp002.nhlt
	else
		var.bptmm211.chlt = empty()
	endif
|	var.bptmm211.rgdt = utc(0, 0)				|#225709.o
	var.bptmm211.rgdt = utc(0, 0, 2)			|#225709.n
|	var.bptmm211.trdt = utc(tihra100.trdt, 0)		|#147118.o
	var.bptmm211.trdt = get.transaction.date(tihra100.trdt, 0) |#147118.n

|	bptmm211.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm211.quan = empty()				|* FILLED IN RULE
|	bptmm211.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm211.pric = empty()				|* FILLED IN RULE
|	bptmm211.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
	var.bptmm211.amnt = empty()
	fill.labor.costs.from.ti(l.day.number)
|	bptmm211.prls = empty()				|* FILLED IN RULE
|	bptmm211.rdat = empty()				|* FILLED IN RULE
	var.bptmm211.rdat = utc(0, 0)
|	bptmm211.rate(1) = empty()			|* FILLED IN RULE
|	bptmm211.rate(2) = empty()			|* FILLED IN RULE
|	bptmm211.rate(3) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(1) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(2) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(3) = empty()			|* FILLED IN RULE
|	bptmm211.rtyp = empty()				|* FILLED IN RULE
|	bptmm211.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm211.logn = logname$
	var.bptmm211.potf = tcyesno.yes
	l.day.txta = fill.text.fields(l.day.number)

	dump 211

|	var.bptmm211.psqn = tihra100.wsrn * 10 + l.day.number		|#158562.o
	l.day.seq.nr =  var.bptmm211.psqn * 10 + l.day.number		|#158562.n

	|* Time part of start date for General Tasks is always 12:00:00.
	l.start.date = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				1200)
	l.reg.date = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)

	fill.and.dump.bptmm250(
		var.bptmm211.emno,
		var.bptmm211.year,
		var.bptmm211.peri,
|		var.bptmm211.psqn,					|#158562.o
		l.day.seq.nr,						|#158562.n
		l.start.date,
		var.bptmm211.gtsk,
		"",	|* Expense
		var.bptmm211.dsca,
		var.bptmm211.cwoc,
		var.bptmm211.cpcp,
		0.0,	|* Amount
		var.bptmm211.potf)

	var.211.prlc(1) = var.bptmm211.prlc(22)		|#MCR.sn
	var.211.prlc(2) = var.bptmm211.prlc(23)
	var.211.prlc(3) = var.bptmm211.prlc(24)

	l.ret = set.mem(dummy.var.prmc, 0.0, 3)
	l.ret = set.mem(dummy.var.proc, 0.0, 3)		|#MCR.en

	fill.and.dump.bptmm200(
		tihra100.emno,						|#GEN-0205.n
		var.bptmm211.emno,
		var.bptmm211.year,
		var.bptmm211.peri,
		GENERAL_TASK,
|		var.bptmm211.psqn,					|#158562.o
		l.day.seq.nr,						|#158562.n
		var.bptmm211.cwoc,
		"",			|* Working Time Schedule Code
		var.bptmm211.hrea(l.day.number),
		0.0,
		var.bptmm211.chlt,
		l.start.date,
		utc(0, 0),		|* End Date
		0.0,
		l.reg.date,
								|#MCR.so
|		var.bptmm211.prlc(22),	|* Total amount (always for 1 day)
|		0.0,			|* Machine Costs
|		0.0,			|* Overhead Costs	|#MCR.eo
		var.211.prlc,					|#MCR.sn
		dummy.var.prmc,
		dummy.var.proc,					|#MCR.en
		var.bptmm211.trdt,
		var.bptmm211.logn,
		l.day.txta)
}

function void fill.and.dump.bptmm211.from.tphrs100()
{
		domain	tcmcs.long	l.day.number
		domain	bcstr22		l.start.date
		domain	bcstr22		l.reg.date

		domain	tcmcs.long	l.dummy.long

	l.dummy.long = 0

	var.bptmm211.emno = format.emno(tphrs100.emno)
	var.bptmm211.year = tphrs100.year
	var.bptmm211.peri = tphrs100.peri

	on case tphrs100.splt
	case tphrs.splt.project:
		var.bptmm211.type = target(bptmm.type.project)
		var.bptmm211.gtsk = empty()
		var.bptmm211.expn = empty()
		var.bptmm211.dsca = get.cost.object.description(
						tphrs100.cotp,
						tphrs100.ccun)
		var.bptmm211.cwoc = ""
		var.bptmm211.cpcp = get.cost.component.from.tpppc(
						tphrs100.ccco,
						tphrs100.cprj,
						tphrs100.cotp,
						tphrs100.year,
						tphrs100.peri,
						tphrs100.empr,
						tphrs100.emno,
						tphrs100.serc)
		var.bptmm211.potf = tcyesno.no
		break
	case tphrs.splt.finance:
	case tphrs.splt.no:
		var.bptmm211.type = target(bptmm.type.general)
		if tphrs100.amnt = 0.0 then
			ret.long = get.general.task(
					bcsbp.orig.tp,
					str$(tphrs100.wgco),
					tphrs100.empr,
					dummy.err.mess)
			if ret.long = 0 then
				var.bptmm211.gtsk = bcsbp004.ntsk
				var.bptmm211.dsca = bcsbp004.ntsd
			else
				var.bptmm211.gtsk = empty()
				var.bptmm211.dsca = empty()
			endif
			var.bptmm211.expn = empty()
		else
			var.bptmm211.expn = tphrs100.wgco
			get.expense.description(tphrs100.empr,
						tphrs100.wgco,
						var.bptmm211.dsca)
			var.bptmm211.gtsk = empty()
		endif
		var.bptmm211.cwoc = get.department.from.employee(tphrs100.emno)
		var.bptmm211.cpcp = bcsbp000.cpgh	|remark 800-137648
		if tphrs100.splt = tphrs.splt.finance then
			var.bptmm211.potf = tcyesno.yes
		else
			var.bptmm211.potf = tcyesno.no
		endif
		break
	default:
		|* For Service records this function will not be entered.
		break
	endcase





	var.bptmm211.cprj = get.tp.project(tphrs100.cprj)
	var.bptmm211.cspa = tphrs100.cspa
	var.bptmm211.cact = tphrs100.cact
	var.bptmm211.cstl = tphrs100.cstl
	var.bptmm211.ccun = tphrs100.ccun
	if tphrs100.cotp = tppdm.cotp.tasks then
		var.bptmm211.swct = target(bpmdm.swct.labor)
	else
		var.bptmm211.swct = target(bpmdm.swct.indirect)
	endif
	if tphrs100.amnt = 0.0 then
		|* Hours record
		get.hours.sequence.number.tp(
					"bptmm211",
					var.bptmm211.emno,
					var.bptmm211.year,
					var.bptmm211.peri,
					l.dummy.long,
					l.dummy.long,
					l.dummy.long,
					var.bptmm211.psqn)
		var.bptmm211.rgdt = utc(0, 0)
		var.bptmm211.chlt = get.labor.type.from.tp(	tphrs100.empr,
								tphrs100.wgco)
		var.bptmm211.amnt = empty()
		var.bptmm211.rdat = utc(0, 0)
		var.bptmm211.rate = empty()
		var.bptmm211.ratf = empty()
		var.bptmm211.rtyp = empty()
	else
		|* Expense record
|		var.bptmm211.psqn = tphrs100.serc + 900
		get.expense.sequence.number.tp(
					"bptmm211",
					var.bptmm211.emno,
					var.bptmm211.year,
					var.bptmm211.peri,
					0,	|* Sequence no, only applicable
						|* if day number is filled.
					var.bptmm211.psqn)
		var.bptmm211.rgdt = utc(tphrs100.rgdt, 0)
		var.bptmm211.chlt = empty()
		var.bptmm211.amnt = tphrs100.amnt
		var.bptmm211.rdat = utc(tphrs100.rgdt, TWELVE_HOURS)
		var.bptmm211.rate = 1.0
		var.bptmm211.ratf = 1
		var.bptmm211.rtyp = get.internal.exchange.rate.type()
	endif
	fill.hours.from.tp()
|	var.bptmm211.trdt = utc(tphrs100.ltdt, tphrs100.lttm)	|#147118.o
	var.bptmm211.trdt = get.transaction.date(		|#147118.sn
					tphrs100.ltdt, 
					tphrs100.lttm) 		|#147118.en
|	bptmm211.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm211.quan = empty()				|* FILLED IN RULE
	|* Quantity field is only applicable for expenses. There is no quantity
	|* field for expenses in BaanIVc4.
|	bptmm211.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm211.pric = empty()				|* FILLED IN RULE
|	bptmm211.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
	set.mem(var.bptmm211.prlc, 0.0)
|	bptmm211.prls = empty()				|* FILLED IN RULE
|	bptmm211.rate(2) = empty()			|* FILLED IN RULE
|	bptmm211.rate(3) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(2) = empty()			|* FILLED IN RULE
|	bptmm211.ratf(3) = empty()			|* FILLED IN RULE
|	bptmm211.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm211.logn = tphrs100.loco
|	bptmm211.txta = empty()				|* FILLED IN RULE
|	bptmm211.txtb = empty()				|* FILLED IN RULE
|	bptmm211.txtc = empty()				|* FILLED IN RULE
|	bptmm211.txtd = empty()				|* FILLED IN RULE
|	bptmm211.txte = empty()				|* FILLED IN RULE
|	bptmm211.txtf = empty()				|* FILLED IN RULE
|	bptmm211.txtg = empty()				|* FILLED IN RULE

	dump 211

	if tphrs100.amnt = 0.0 then
		for l.day.number = 1 to 7
			if tphrs100.nhrs(l.day.number) <> 0.0 then
				l.start.date =
					utc(tphrs100.rgdt + l.day.number - 1,
					TWELVE_HOURS)
				l.reg.date =
					utc(tphrs100.rgdt + l.day.number - 1,
					0)
				dump.bptmm210.or.bptmm250.and.bptmm200.from.tp(
						tphrs100.splt,
						l.day.number,
						tphrs100.emno,	|#GEN-0205.n
						var.bptmm211.emno,
						var.bptmm211.year,
						var.bptmm211.peri,
						var.bptmm211.psqn,
						l.start.date,
						l.reg.date,
						var.bptmm211.cprj,
						var.bptmm211.swct,
						var.bptmm211.cspa,
						var.bptmm211.cact,
						var.bptmm211.cstl,
						var.bptmm211.cpcp,
						var.bptmm211.ccun,
						var.bptmm211.dsca,
						var.bptmm211.hrea(l.day.number),
						var.bptmm211.amnt,
						var.bptmm211.gtsk,
						"",		|* Expense
						var.bptmm211.potf,
						var.bptmm211.cwoc,
						var.bptmm211.chlt,
						var.bptmm211.trdt,
						var.bptmm211.logn,
						var.bptmm211.txta)
			endif
		endfor
	else
		|* Expense Record.
		l.start.date = utc(tphrs100.rgdt, TWELVE_HOURS)
		l.reg.date = utc(tphrs100.rgdt, 0)

		dump.bptmm210.or.bptmm250.and.bptmm200.from.tp(
				tphrs100.splt,
				l.day.number,
				tphrs100.emno,	|#GEN-0205.n
				var.bptmm211.emno,
				var.bptmm211.year,
				var.bptmm211.peri,
				var.bptmm211.psqn,
				l.start.date,
				l.reg.date,
				var.bptmm211.cprj,
				var.bptmm211.swct,
				var.bptmm211.cspa,
				var.bptmm211.cact,
				var.bptmm211.cstl,
				var.bptmm211.cpcp,
				var.bptmm211.ccun,
				var.bptmm211.dsca,
				var.bptmm211.hrea(1),	|0.0
				var.bptmm211.amnt,
				"",		|* General Task
				var.bptmm211.expn,
				var.bptmm211.potf,
				var.bptmm211.cwoc,
				var.bptmm211.chlt,
				var.bptmm211.trdt,
				var.bptmm211.logn,
				var.bptmm211.txta)
	endif
}

function void fill.and.dump.bptmm220()
{
		domain	tcamnt	var.220.prlc(3)			|#MCR.sn
		domain	tcamnt	var.220.prmc(3)
		domain	tcamnt	var.220.proc(3)			|#MCR.en

|	bptmm220.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm220.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
|	bptmm220.year = tihra100.year			|* FILLED IN RULE
|	bptmm220.peri = tihra100.week			|* FILLED IN RULE
|	bptmm220.psqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm220.psqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm220.psqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				PRODUCTION)		
								|#158562.en

	var.bptmm220.stdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,

				tihra100.stdy,
				tihra100.sttm)
	if not isspace(tihra100.cwtt) then
		var.bptmm220.endt = convert.hra.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm220.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm220.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm220.trdt = get.transaction.date(tihra100.trdt, 0) |#147118.n

	var.bptmm220.rgdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm220.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm220.wtsc = tihra100.cwtt			|* FILLED IN RULE
|	bptmm220.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm220.hrea = tihra100.hrea			|* FILLED IN RULE
	ret.long = get.labor.type(
				bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm220.chlt = bcsbp002.nhlt
	else
		var.bptmm220.chlt = empty()
	endif
|	bptmm220.hrma = tihra100.hrma			|* FILLED IN RULE
|	bptmm220.stup = target(tcyesno.no)		|* FILLED IN RULE
|	bptmm220.orno = convert.number(bcckon.act.sfc, tihra100.pdno)	|* FILLED IN RULE
|	bptmm220.opno = tihra100.opno			|* FILLED IN RULE
|	bptmm220.tano = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm220.mcno = tihra100.mcno			|* FILLED IN RULE
	var.bptmm220.perc = correct.percentage.completed(tihra100.pccp)
|	bptmm220.perc = tihra100.pccp			|* FILLED IN RULE
|	bptmm220.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm220.cprj = get.ti.project(tihra100.cprj)	|* FILLED IN RULE
|	bptmm220.cact = tihra100.cact			|* FILLED IN RULE
|	bptmm220.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
|	bptmm220.prlc(1) = tihra100.wgca		|* FILLED IN RULE
|	bptmm220.prlc(2) = empty()			|* FILLED IN RULE
|	bptmm220.prlc(3) = empty()			|* FILLED IN RULE
|	bptmm220.prmc(1) = tihra100.mcca		|* FILLED IN RULE
|	bptmm220.prmc(2) = empty()			|* FILLED IN RULE
|	bptmm220.prmc(3) = empty()			|* FILLED IN RULE
|	bptmm220.proc(1) = tihra100.ohca		|* FILLED IN RULE
|	bptmm220.proc(2) = empty()			|* FILLED IN RULE
|	bptmm220.proc(3) = empty()			|* FILLED IN RULE
|	bptmm220.prls(1) = empty()			|* FILLED IN RULE
|	bptmm220.prls(2) = empty()			|* FILLED IN RULE
|	bptmm220.prls(3) = empty()			|* FILLED IN RULE
|	bptmm220.prms(1) = empty()			|* FILLED IN RULE

|	bptmm220.prms(2) = empty()			|* FILLED IN RULE
|	bptmm220.prms(3) = empty()			|* FILLED IN RULE
|	bptmm220.pros(1) = empty()			|* FILLED IN RULE
|	bptmm220.pros(2) = empty()			|* FILLED IN RULE
|	bptmm220.pros(3) = empty()			|* FILLED IN RULE
|	bptmm220.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm220.logn = logname$			|* FILLED IN RULE
|	bptmm220.txta = tihra100.txta			|* FILLED IN RULE

	for element = 1 to 3					|#MCR.sn
		var.220.prlc(element) = fill.mc.field.double(
							tihra100.wgca,
							element,
							0.0)
		var.220.prmc(element) = fill.mc.field.double(
							tihra100.mcca,
							element,
							0.0)
		var.220.proc(element) = fill.mc.field.double(
							tihra100.ohca,
							element,
							0.0)
	endfor							|#MCR.en



	dump 220


	fill.and.dump.bptmm200(
		tihra100.emno,					|#GEN-0205.n
		format.emno(tihra100.emno),
		tihra100.year,
		tihra100.week,
		PRODUCTION,
|		tihra100.wsrn,					|#158562.o
		var.bptmm220.psqn,				|#158562.n
		tihra100.cwoc,
		tihra100.cwtt,
		tihra100.hrea,
		tihra100.hrma,
		var.bptmm220.chlt,
		var.bptmm220.stdt,
		var.bptmm220.endt,
		var.bptmm220.perc,
		var.bptmm220.rgdt,
|		tihra100.wgca,					|#MCR.so
|		tihra100.mcca,
|		tihra100.ohca,					|#MCR.eo
		var.220.prlc,					|#MCR.sn
		var.220.prmc,
		var.220.proc,					|#MCR.en
|		utc(tihra100.trdt, 0),				|#147118.o
		var.bptmm220.trdt,				|#147118.n
		logname$,
		tihra100.txta)
}

function void fill.and.dump.bptmm230.from.tihra100()
{
	domain	tcmcs.long	l.ret				|#MCR.n
	domain	tcamnt		dummy.var.prmc(3)		|#MCR.n

	var.bptmm230.emno = format.emno(tihra100.emno)
|	bptmm230.cpdt = var.bptmm2xx.cpdt			|* FILLED IN RULE
	var.bptmm230.year = tihra100.year
	var.bptmm230.peri = tihra100.week
|	var.bptmm230.psqn = tihra100.wsrn			|#158562.o
								|#158562.sn
	var.bptmm230.psqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				SERVICE_TI)		
								|#158562.en

	var.bptmm230.stdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	if not isspace(tihra100.cwtt) then
		var.bptmm230.endt = convert.hra.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm230.endt = utc(0, 0)
	endif
|	var.bptmm230.trdt = utc(tihra100.trdt, 0)		|#147118.o
	var.bptmm230.trdt = get.transaction.date(tihra100.trdt, 0) |#147118.n

	var.bptmm230.rgdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm230.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
	var.bptmm230.wtsc = tihra100.cwtt
|	bptmm230.cuni = bcsbp000.dsbt			|* FILLED IN RULE
	var.bptmm230.hrea = tihra100.hrea
	ret.long = get.labor.type(
				bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm230.chlt = bcsbp002.nhlt
	else
		var.bptmm230.chlt = empty()
	endif
	var.bptmm230.orno = convert.number(bcckon.sma.order, tihra100.pdno)
	var.bptmm230.stsk = format.tano(tihra100.tano)
|	bptmm230.acln = empty()				|* FILLED IN RULE
	var.bptmm230.cwoc = tihra100.cwoc
|	bptmm230.lbtv = target(bptmm.serl.labo)		|* FILLED IN RULE
|	bptmm230.lbln = empty()				|* FILLED IN RULE
|	bptmm230.tvln = empty()				|* FILLED IN RULE
	var.bptmm230.cpcp = get.cost.component.from.ts(
					tihra100.pdno,
					tihra100.tano)
|	bptmm230.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE

|	var.bptmm230.prlc = tihra100.wgca			|#MCR.o

	for element = 1 to 3					|#MCR.sn
		var.bptmm230.prlc(element) = fill.mc.field.double(tihra100.wgca,
								  element,
							   	  0.0)
	endfor							|#MCR.en

|	bptmm230.prls = empty()				|* FILLED IN RULE

|	var.bptmm230.proc = tihra100.ohca			|#MCR.o

	for element = 1 to 3					|#MCR.sn
		var.bptmm230.proc(element) = fill.mc.field.double(tihra100.ohca,
								  element,
							   	  0.0)
	endfor							|#MCR.en
	l.ret = set.mem(dummy.var.prmc, 0.0, 3)			|#MCR.n

|	bptmm230.pros = empty()				|* FILLED IN RULE
|	bptmm230.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm230.logn = logname$
	var.bptmm230.txta = tihra100.txta

	dump 230


	fill.and.dump.bptmm200(
		tihra100.emno,					|#GEN-0205.n
		var.bptmm230.emno,
		var.bptmm230.year,
		var.bptmm230.peri,
		SERVICE_TI,
		var.bptmm230.psqn,
		var.bptmm230.cwoc,
		var.bptmm230.wtsc,
		var.bptmm230.hrea,
		0.0,		|* Machine Hours
		var.bptmm230.chlt,
		var.bptmm230.stdt,
		var.bptmm230.endt,
		0.0,		|* Percentage Completed
		var.bptmm230.rgdt,
|		var.bptmm230.prlc,				|#MCR.so
|		0.0,		|* Machine Costs
|		var.bptmm230.proc,				|#MCR.eo
		var.bptmm230.prlc,				|#MCR.sn
		dummy.var.prmc,
		var.bptmm230.proc,				|#MCR.en
		var.bptmm230.trdt,
		var.bptmm230.logn,
		var.bptmm230.txta)
}

function void fill.and.dump.bptmm230.from.tphrs100()
{
	static	domain	tcmcs.long	s.counter
		domain	tcmcs.long	l.day.number

		domain	tcmcs.long	l.ret			|#MCR.sn
		domain	tcamnt		dummy.var.prlc(3)
		domain	tcamnt		dummy.var.prmc(3)
		domain	tcamnt		dummy.var.proc(3)	|#MCR.en

	if tphrs100.amnt <> 0.0 then
		|* Service expenses are not supported in BaanVI.
		return
	endif

	var.bptmm230.emno = format.emno(tphrs100.emno)
|	bptmm230.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
	var.bptmm230.year = tphrs100.year
	var.bptmm230.peri = tphrs100.peri
	var.bptmm230.endt = utc(0, 0)
	|* endt is only applicable if the Working Time Schedule code is filled.
	|* This field is not migrated.
|	var.bptmm230.trdt = utc(tphrs100.ltdt, tphrs100.lttm)	|#147118.o
	var.bptmm230.trdt = get.transaction.date(		|#147118.sn
					tphrs100.ltdt, 
					tphrs100.lttm) 		|#147118.en

|	bptmm230.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
	var.bptmm230.wtsc = empty()
|	bptmm230.cuni = bcsbp000.dsbt			|* FILLED IN RULE

	var.bptmm230.chlt = get.labor.type.from.tp(	tphrs100.empr,
							tphrs100.wgco)
	var.bptmm230.orno = convert.number(bcckon.sma.order, tphrs100.orno)


	var.bptmm230.stsk = format.tano(tphrs100.tano)
|	bptmm230.acln = empty()				|* FILLED IN RULE
	var.bptmm230.cwoc = get.service.department(tphrs100.orno)
|	bptmm230.lbtv = target(bptmm.serl.labo)		|* FILLED IN RULE
|	bptmm230.lbln = empty()				|* FILLED IN RULE
|	bptmm230.tvln = empty()				|* FILLED IN RULE
	var.bptmm230.cpcp = get.cost.component.from.ts(	tphrs100.orno,
							tphrs100.tano)
|	bptmm230.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE

|	var.bptmm230.prlc = empty()				|#MCR.o
	l.ret = set.mem(var.bptmm230.prlc, 0.0, 3)		|#MCR.n

|	bptmm230.prls = empty()				|* FILLED IN RULE

|	var.bptmm230.proc = empty()				|#MCR.o
	l.ret = set.mem(var.bptmm230.proc, 0.0, 3)		|#MCR.n

|	bptmm230.pros = empty()				|* FILLED IN RULE
|	bptmm230.orig = target(bptmm.orig.external)	|* FILLED IN RULE
	var.bptmm230.logn = tphrs100.loco
	var.bptmm230.txta = empty()

	s.counter = 1
	for l.day.number = 1 to 7
		if tphrs100.nhrs(l.day.number) <> 0.0 then
			get.hours.sequence.number.tp(
						"bptmm230",
						var.bptmm230.emno,
						var.bptmm230.year,
						var.bptmm230.peri,
						l.day.number,
						0,
						s.counter,
						var.bptmm230.psqn)
			var.bptmm230.stdt =
					utc(tphrs100.rgdt + l.day.number - 1,
					TWELVE_HOURS)
			var.bptmm230.rgdt =
					utc(tphrs100.rgdt + l.day.number - 1, 0)
			var.bptmm230.hrea = tphrs100.nhrs(l.day.number)

			l.ret = set.mem(dummy.var.prlc, 0.0, 3)	|#MCR.sn
			l.ret = set.mem(dummy.var.prmc, 0.0, 3)
			l.ret = set.mem(dummy.var.proc, 0.0, 3)	|#MCR.en

			dump 230

			fill.and.dump.bptmm200(
					tphrs100.emno,			|#GEN-0205.n
					var.bptmm230.emno,
					var.bptmm230.year,
					var.bptmm230.peri,
					SERVICE_TP,
					var.bptmm230.psqn,
					var.bptmm230.cwoc,
					var.bptmm230.wtsc,
					var.bptmm230.hrea,
					0.0,		|* Machine Hours
					var.bptmm230.chlt,
					var.bptmm230.stdt,
					var.bptmm230.endt,
					0.0,		|* Percentage Completed
					var.bptmm230.rgdt,
|					0.0,	|* Labor Costs		|#MCR.so
|					0.0,	|* Machine Costs
|					0.0,	|* Overhead Costs	|#MCR.eo
					dummy.var.prlc,			|#MCR.sn
					dummy.var.prmc,
					dummy.var.proc,			|#MCR.en
					var.bptmm230.trdt,
					var.bptmm230.logn,
					var.bptmm230.txta)
			s.counter = s.counter + 1
		endif
	endfor
}

function void fill.and.dump.bptmm250(
		domain	bcstr6		i.emno,
		domain	tppdm.year	i.year,
		domain	tcpern		i.peri,
		domain	tcmcs.long	i.psqn,
		domain	bcstr22		i.stdt,
		domain	bcstr8		i.gtsk,
		domain	bcstr8		i.expn,
		domain	tcdsca		i.dsca,
		domain	bcstr6		i.cwoc,
		domain	bcstr8		i.cpcp,
		domain	tcamnt		i.amnt,
		domain	tcyesno		i.potf)
{
	var.bptmm250.emno = fill.field.string(i.emno)
|	var.bptmm250.cpdt = var.bptmm2xx.cpdt	|* FILLED IN RULE
	var.bptmm250.year = fill.field.long(i.year)
	var.bptmm250.peri = fill.field.long(i.peri)
	var.bptmm250.psqn = fill.field.long(i.psqn)

	var.bptmm250.stdt = fill.field.string(i.stdt)
	var.bptmm250.gtsk = fill.field.string(i.gtsk)
	var.bptmm250.expn = fill.field.string(i.expn)
	var.bptmm250.dsca = fill.field.string(i.dsca)
	var.bptmm250.cwoc = fill.field.string(i.cwoc)
	var.bptmm250.cpcp = fill.field.string(i.cpcp)
|	bptmm250.quan = empty()			|* FILLED IN RULE
|	bptmm250.cuni = bcsbp000.dsbt		|* FILLED IN RULE

|	bptmm250.pric = empty()			|* FILLED IN RULE
|	bptmm250.ccur = var.bptmm2xx.ccur	|* FILLED IN RULE
	var.bptmm250.amnt = fill.field.double(i.amnt)
	if i.amnt = 0.0 then
		var.bptmm250.rdat = utc(0, 0)
		var.bptmm250.rate = empty()
		var.bptmm250.ratf = empty()
		var.bptmm250.rtyp = empty()
	else
		var.bptmm250.rdat = fill.field.string(i.stdt)
		var.bptmm250.rate = 1.0
		var.bptmm250.ratf = 1
		var.bptmm250.rtyp = get.internal.exchange.rate.type()
	endif
|	bptmm250.rate(2) = empty()		|* FILLED IN RULE
|	bptmm250.rate(3) = empty()		|* FILLED IN RULE
|	bptmm250.ratf(2) = empty()		|* FILLED IN RULE
|	bptmm250.ratf(3) = empty()		|* FILLED IN RULE
	var.bptmm250.potf = i.potf

	dump 250
}

function void fill.and.dump.bptmm270()
{
		domain	tcamnt	var.270.prlc(3)			|#MCR.sn
		domain	tcamnt	var.270.prmc(3)
		domain	tcamnt	var.270.proc(3)			|#MCR.en

|	bptmm270.emno = format.emno(tihra100.emno)	|* FILLED IN RULE
|	bptmm270.cpdt = var.bptmm2xx.cpdt		|* FILLED IN RULE
|	bptmm270.year = tihra100.year			|* FILLED IN RULE

|	bptmm270.peri = tihra100.week			|* FILLED IN RULE
|	bptmm270.psqn = tihra100.wsrn			|* FILLED IN RULE
|	bptmm270.psqn not filled in rule anymore; sequence	|#158562.sn
|	number will be determined during migration

	var.bptmm270.psqn = get.sequence.number(
				tihra100.year,
				tihra100.week,
				tihra100.emno,
				PCS)		
								|#158562.en

	var.bptmm270.stdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				tihra100.sttm)
	if not isspace(tihra100.cwtt) then
		var.bptmm270.endt = convert.hra.day.to.utc.date(
					tihra100.year,
					tihra100.week,
					tihra100.endy,
					tihra100.entm)
	else
		var.bptmm270.endt = utc(0, 0)
	endif
								|#147118.so
|	bptmm270.trdt = utc(tihra100.trdt, 0)		|* FILLED IN RULE
								|#147118.eo
	var.bptmm270.trdt = get.transaction.date(tihra100.trdt, 0) |#147118.n

	var.bptmm270.rgdt = convert.hra.day.to.utc.date(
				tihra100.year,
				tihra100.week,
				tihra100.stdy,
				0)
|	bptmm270.prdt = var.bptmm2xx.prdt		|* FILLED IN RULE
|	bptmm270.wtsc = tihra100.cwtt			|* FILLED IN RULE



|	bptmm270.cuni = bcsbp000.dsbt			|* FILLED IN RULE
|	bptmm270.hrea = tihra100.hrea			|* FILLED IN RULE
	ret.long = get.labor.type(
				bcsbp.orig.ti,
				tihra100.ckow,
				dummy.err.mess)
	if ret.long = 0 then
		var.bptmm270.chlt = bcsbp002.nhlt
	else
		var.bptmm270.chlt = empty()
	endif
|	bptmm270.hrma = tihra100.hrma			|* FILLED IN RULE
|	bptmm270.cprj = get.ti.project(tihra100.cprj)	|* FILLED IN RULE
|	bptmm270.cact = tihra100.cact			|* FILLED IN RULE
|	bptmm270.tano = format.tano(tihra100.tano)	|* FILLED IN RULE
|	bptmm270.mcno = tihra100.mcno			|* FILLED IN RULE
|	bptmm270.cwoc = tihra100.cwoc			|* FILLED IN RULE
|	bptmm270.ccur = var.bptmm2xx.ccur		|* FILLED IN RULE
|	bptmm270.prlc = tihra100.wgca			|* FILLED IN RULE
|	bptmm270.prmc =	tihra100.mcca			|* FILLED IN RULE
|	bptmm270.proc = tihra100.ohca			|* FILLED IN RULE
|	bptmm270.prls = empty()				|* FILLED IN RULE
|	bptmm270.prms = empty()				|* FILLED IN RULE
|	bptmm270.pros = empty()				|* FILLED IN RULE
|	bptmm270.orig = target(bptmm.orig.external)	|* FILLED IN RULE
|	bptmm270.logn = logname$			|* FILLED IN RULE
|	bptmm270.txta = tihra100.txta			|* FILLED IN RULE

	for element = 1 to 3					|#MCR.sn
		var.270.prlc(element) = fill.mc.field.double(
							tihra100.wgca,
							element,
							0.0)
		var.270.prmc(element) = fill.mc.field.double(
							tihra100.mcca,
							element,
							0.0)
		var.270.proc(element) = fill.mc.field.double(
							tihra100.ohca,
							element,
							0.0)
	endfor							|#MCR.en


	dump 270


	fill.and.dump.bptmm200(
		tihra100.emno,					|#GEN-0205.n
		format.emno(tihra100.emno),
		tihra100.year,
		tihra100.week,
		PCS,
|		tihra100.wsrn,					|#158562.o
		var.bptmm270.psqn,				|#158562.n
		tihra100.cwoc,
		tihra100.cwtt,
		tihra100.hrea,
		tihra100.hrma,
		var.bptmm270.chlt,
		var.bptmm270.stdt,
		var.bptmm270.endt,
		tihra100.pccp,
		var.bptmm270.rgdt,
|		tihra100.wgca,					|#MCR.so
|		tihra100.mcca,
|		tihra100.ohca,					|#MCR.eo
		var.270.prlc,					|#MCR.sn
		var.270.prmc,
		var.270.proc,					|#MCR.en
|		utc(tihra100.trdt, 0),				|#147118.o
		var.bptmm270.trdt,				|#147118.n
		logname$,
		tihra100.txta)
}

function void get.period.table.code.for.ti(
	ref	domain	bcstp.cpdt	o.period.table.code)
{
	get.period.table.code(	"tihra100",
				bcsbp000.prtc,
				"",
				dummy.err.mess)
	o.period.table.code = bcstc029.tptc
}

function void get.period.table.code.for.tp(
	ref	domain	bcstp.cpdt	o.period.table.code)
{
	|******************************************************
	|* If HRA records are present OR if both HRA and HRS
	|* records are present then the HRA periods are used.
	|* If only HRS records are present then the HRS periods
	|* are used.
	|******************************************************

	get.period.table.code(	"tpppc001",
				bcstc000.pfpp,
				bcstp000.cpth,
				dummy.err.mess)
	o.period.table.code = bcstc029.tptc

	if hra.period.definition = true then
	|* Variable is set at the moment an tiHRA record is selected to migrate.
		if periods.are.equal() then
			get.period.table.code(	"tihra100",
						bcsbp000.prtc,
						"",
						dummy.err.mess)
			o.period.table.code = bcstc029.tptc	|bcsbp000.prtc
		endif
	endif
}

function domain tcbool periods.are.equal()
{
		domain	tppdm.date	l.period.date

	select	tpppc001.*
	from	tpppc001
	order by tpppc001._index1
	selectdo
		l.period.date = week.to.num(
					tpppc001.peri,	|* Week number
					tpppc001.year,	|* Year number
					1)		|* First day of week
		if l.period.date = tpppc001.date then
			continue
			|* Check next record...
		else
			return(false)
		endif
	endselect

	return(true)
}

function domain tcbool expense.record.with.zero.amount(
		domain	tphrs.amnt	i.amnt,
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco)
{
	|**********************************************************************

	|* In BaanIV it is possible to register an expense with no amount. These
	|* records are not supported in BaanVI. Therefore these records must be

	|* skipped during migration.
	|**********************************************************************

		domain	tppdm.wgct	o.wgct

	if i.amnt <> 0.0 then
		|* Early return if the amount is filled.
		return(false)
	endif

	read.wage.component.type.from.wage.component(
					i.empr,
					i.wgco,
					o.wgct)

	if o.wgct = tppdm.wgct.amount then
		return(true)
	endif

	return(false)
}

function domain tcprcg correct.percentage.completed(
		domain	tcprcg	i.perc.completed)
{
	if i.perc.completed > 100.0 then
		return(100.0)
	endif

	if i.perc.completed < 0.0 then
		return(0.0)
	endif

	return(i.perc.completed)
}

function void read.wage.component.type.from.wage.component(
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco,
	ref	domain	tppdm.wgct	o.wgct)
{
	static	domain	tphrs.empr	s.empr
	static	domain	tphrs.wgco	s.wgco	fixed
	static	domain	tppdm.wgct	s.wgct

	if s.empr = i.empr and
	   s.wgco = i.wgco then
		o.wgct = s.wgct
		return
	endif

	select	tphrs010.wgct :o.wgct
	from	tphrs010
	where	tphrs010._index1 = {	:i.empr,
					:i.wgco}
	as set with 1 rows

	selectdo
		s.empr = i.empr
		s.wgco = i.wgco
		s.wgct = o.wgct
	endselect
}

function void read.labor.type.from.wage.component(
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco,
	ref	domain	tpppc.cwgt	o.cwgt)
{
	static	domain	tphrs.empr	s.empr
	static	domain	tphrs.wgco	s.wgco	fixed
	static	domain	tpppc.cwgt	s.cwgt

	if s.empr = i.empr and
	   s.wgco = i.wgco then
		o.cwgt = s.cwgt
		return
	endif


	select	tphrs010.cwgt :o.cwgt
	from	tphrs010
	where	tphrs010._index1 = {	:i.empr,
					:i.wgco}
	as set with 1 rows
	selectdo
		s.empr = i.empr
		s.wgco = i.wgco
		s.cwgt = o.cwgt
	endselect




}

function void dump.bptmm210.or.bptmm250.and.bptmm200.from.tp(
		domain	tphrs.splt	i.splt,
		domain	tcmcs.long	i.dayn,
		domain	tcemno		i.original.employee.code, |#GEN-0205.n
		domain	bcstr6		i.emno,
		domain	tppdm.year	i.year,
		domain	tppdm.peri	i.peri,
		domain	tcmcs.long	i.serc,
		domain	bcstr22		i.stdt,
		domain	bcstr22		i.rgdt,
		domain	tccprj		i.cprj,
		domain	tcmcs.long	i.swct,
		domain	bcstr8		i.cspa,
		domain	bcstr8		i.cact,
		domain	bcstr4		i.cstl,
		domain	bcstr8		i.cpcp,
		domain	bcstr8		i.ccun,
		domain	tcdsca		i.dsca,
		domain	tisfc.prtm	i.hrea,
		domain	tcamnt		i.amnt,
		domain	bcstr8		i.gtsk,
		domain	bcstr8		i.expn,
		domain	tcyesno		i.potf,
		domain	bcstr6		i.cwoc,
		domain	bcstr3		i.chlt,
		domain	bcstr22		i.trdt,
		domain	tclogn		i.logn,
		domain	tctxtn		i.txta)
{
		domain	tcbool		l.hours.record
		domain	tcmcs.long	l.dummy.long


		domain	tcmcs.long	l.ret			|#MCR.sn
		domain	tcamnt		dummy.var.prlc(3)
		domain	tcamnt		dummy.var.prmc(3)
		domain	tcamnt		dummy.var.proc(3)	|#MCR.en

	l.hours.record = false
	l.dummy.long = 0

	if i.amnt = 0.0 then
		l.hours.record = true
	endif

	if i.splt = tphrs.splt.project then
		if l.hours.record = true then
			get.hours.sequence.number.tp(
						"bptmm210",
						i.emno,
						i.year,
						i.peri,
						i.dayn,
						i.serc,
						l.dummy.long,
						var.bptmm210.psqn)
		else
			get.expense.sequence.number.tp(
						"bptmm210",
						i.emno,
						i.year,
						i.peri,
						i.serc,
						var.bptmm210.psqn)
		endif
		fill.and.dump.bptmm210(
			var.bptmm210.psqn,
			i.stdt,
			i.swct,
			i.cpcp)
	else
		if l.hours.record = true then
			get.hours.sequence.number.tp(
						"bptmm250",
						i.emno,
						i.year,
						i.peri,
						i.dayn,
						i.serc,
						l.dummy.long,
						var.bptmm250.psqn)
		else
			get.expense.sequence.number.tp(
						"bptmm250",
						i.emno,
						i.year,
						i.peri,
						i.serc,
						var.bptmm250.psqn)
		endif
		fill.and.dump.bptmm250(
			i.emno,
			i.year,
			i.peri,
			var.bptmm250.psqn,
			i.stdt,
			i.gtsk,
			i.expn,
			i.dsca,
			i.cwoc,
			i.cpcp,
			i.amnt,
			i.potf)
	endif

	if i.splt = tphrs.splt.project then

		var.bptmm200.type = PROJECT
		var.bptmm200.psqn = var.bptmm210.psqn
	else
		var.bptmm200.type = GENERAL_TASK
		var.bptmm200.psqn = var.bptmm250.psqn
	endif

	l.ret = set.mem(dummy.var.prlc, 0.0, 3)		|#MCR.sn
	l.ret = set.mem(dummy.var.prmc, 0.0, 3)
	l.ret = set.mem(dummy.var.proc, 0.0, 3)		|#MCR.en


	fill.and.dump.bptmm200(
		i.original.employee.code,			|#GEN-0205.n
		i.emno,
		i.year,
		i.peri,
		var.bptmm200.type,
		var.bptmm200.psqn,
		i.cwoc,
		"",		|* Working Time Schedule Code
		i.hrea,
		0.0,		|* Machine Hours
		i.chlt,
		i.stdt,

		utc(0, 0),	|* End Date
		0.0,		|* Percentage Completed
		i.rgdt,
|		0.0,		|* Labor Costs			|#MCR.so
|		0.0,		|* Machine Costs
|		0.0,		|* Overhead Costs		|#MCR.eo
		dummy.var.prlc,					|#MCR.sn
		dummy.var.prmc,
		dummy.var.proc,					|#MCR.en
		i.trdt,
		i.logn,
		i.txta)
}

function void get.hours.sequence.number.tp(
		domain	bcstr8		i.target.table,
		domain	bcstr6		i.emno	fixed,
		domain	tppdm.year	i.year,
		domain	tppdm.peri	i.peri,
		domain	tcmcs.long	i.dayn,
		domain	tcmcs.long	i.seqn,
		domain	tcmcs.long	i.counter,	|* Only applicable for
							|* Service
	ref	domain	tcmcs.long	o.seqn)
{
	|* Note: i.seqn is only applicable if i.dayn is filled. The value of
	|* i.seqn must be the result of a previous call of this function (and
	|* may not be retrieved from tphrs100).

	static	domain	bcstr6		s.emno	fixed
	static	domain	tppdm.year	s.year
	static	domain	tppdm.peri	s.peri
	static	domain	tcmcs.long	s.seqn

	on case i.target.table
	case "bptmm211":
		if s.emno = i.emno and
		   s.year = i.year and
		   s.peri = i.peri then
			s.seqn = s.seqn + 1
		else
			s.seqn = 1
			s.emno = i.emno
			s.year = i.year
			s.peri = i.peri
		endif
		o.seqn = s.seqn
		break
	case "bptmm210":
	case "bptmm250":
		o.seqn = i.seqn * 10 + i.dayn
		break
	case "bptmm230":
		if i.counter = 1 then
			if s.emno = i.emno and
			   s.year = i.year and
			   s.peri = i.peri then
				s.seqn = s.seqn + 1
			else
				s.seqn = 1
				s.emno = i.emno
				s.year = i.year
				s.peri = i.peri
			endif
		endif
		o.seqn = s.seqn * 10 + i.dayn
		break
	endcase
}

function void get.expense.sequence.number.tp(
		domain	bcstr8		i.target.table,
		domain	bcstr6		i.emno	fixed,
		domain	tppdm.year	i.year,
		domain	tppdm.peri	i.peri,
		domain	tcmcs.long	i.seqn,
	ref	domain	tcmcs.long	o.seqn)
{
	|* Note: i.seqn is only applicable if i.dayn is filled. The value of
	|* i.seqn must be the result of a previous call of this function (and
	|* may not be retrieved from tphrs100).

	static	domain	bcstr6		s.emno	fixed
	static	domain	tppdm.year	s.year
	static	domain	tppdm.peri	s.peri
	static	domain	tcmcs.long	s.seqn


	on case i.target.table
	case "bptmm211":
		if s.emno = i.emno and
		   s.year = i.year and
		   s.peri = i.peri then
			s.seqn = s.seqn + 1
		else
			s.seqn = 900
			s.emno = i.emno
			s.year = i.year
			s.peri = i.peri
		endif
		o.seqn = s.seqn
		break
	case "bptmm210":
	case "bptmm250":
		|* The sequence number for expense records start from 900;
		|* In table bptmm210 / bptmm250 they must start with 9000.
		o.seqn = 9000 + (i.seqn - 900)
		break
	case "bptmm230":
		|* Service expenses are not supported in BaanVI.
		break
	endcase
}


function domain	bcrtyp get.internal.exchange.rate.type()
{

	static	domain	bcrtyp		s.internal.rate.type

	if isspace(s.internal.rate.type) then
		get.exchange.rate.type(	"EU",
					dummy.err.mess)
		s.internal.rate.type = bcstc011.brtp
	endif

	return(s.internal.rate.type)
}

function string convert.hra.day.to.utc.date(
		domain	tcyrno		i.year,
		domain	tcweek		i.week,
		domain	tcdynm		i.dayname,
		domain	tctmhs		i.time)
{
|	Dllusage
|	Expl:	This function calculates the utc-date based on an input day and
|		an input time.
|	Input:
|		i.year - Year
|		i.week - Week Number
|		i.dayname - Enumerate (Monday, Tuesday, etc.)
|		i.time - Time (interpretation depends on tihra100.cltm)
|	Enddllusage

|		domain	tcmcs.long	l.ret			|#PERF.o
|		string			l.dummy			|#PERF.o
		domain	tcmcs.long	l.day.number		|#PERF.n
		domain	tcmcs.long	l.utc.date.part
		domain	tcmcs.long	l.utc.time.part
		domain	tcmcs.long	l.number.of.hours

|	if i.dayname = empty then				|#PERF.so
|		l.ret = tt.company(	get.compnr(),
|					l.dummy,
|					l.dummy,
|					l.first.day.of.week)
|		i.dayname = ltoe(l.first.day.of.week)
|	endif							|#PERF.eo

	l.day.number = convert.dayname.to.daynumber(i.dayname)	|#PERF.sn
	l.utc.date.part = week.to.num(	i.week,
					i.year,
					l.day.number)		|#PERF.en
								|#PERF.so
|	l.utc.date.part = itihra0001.convert.dayname.to.date(	i.year,
|								i.week,
|								i.dayname)
								|#PERF.eo
	if i.time <> 0 then
		read.hra.parameters()

		if tihra000.cltm = tccltm.minutes then
		|* 850 is to be interpreted as 08:50.
			l.number.of.hours = i.time/100
			l.utc.time.part = (l.number.of.hours * 60 * 60) +
				((i.time - l.number.of.hours * 100) * 60)
		else
		|* 850 is to be interpreted as 08:30.
			l.number.of.hours = i.time/100
			l.utc.time.part = (l.number.of.hours * 60 * 60) +
				((i.time - l.number.of.hours * 100) *
							tihra000.perl * 60)
		endif
	else
		l.utc.time.part = 0
		return(utc(l.utc.date.part, l.utc.time.part, 2)) |#225709.n
	endif

	return(utc(l.utc.date.part, l.utc.time.part))
}

function domain tcmcs.long convert.dayname.to.daynumber(	|#PERF.sn
		domain	tcdynm		i.day.name)
{
		domain	tcmcs.long	l.ret
		string			l.dummy
	static	domain	tcncmp		s.company
	static	domain	tcmcs.long	s.first.day.of.week
		domain	tcmcs.long	l.day.nr

	if s.company <> ext.lng.comp then
		l.ret = tt.company(	ext.lng.comp,
					l.dummy,
					l.dummy,
					s.first.day.of.week)
		s.company = ext.lng.comp
	endif

	if i.day.name = empty then
		i.day.name = ltoe(s.first.day.of.week)
	endif

	on case i.day.name
		case tcdynm.sunday:
			l.day.nr = 1
			break
		case tcdynm.monday:
			l.day.nr = 2
			break
		case tcdynm.tuesday:
			l.day.nr = 3
			break
		case tcdynm.wednesday:
			l.day.nr = 4
			break
		case tcdynm.thursday:
			l.day.nr = 5
			break
		case tcdynm.friday:
			l.day.nr = 6
			break
		case tcdynm.saturday:
			l.day.nr = 7
			break
		default:
			l.day.nr = 0
			break
	endcase

	if l.day.nr <> 0 then
		|* Make correction for first day of the week
		l.day.nr = l.day.nr + 1 + (7 - s.first.day.of.week)
		if l.day.nr > 7 then
			l.day.nr = l.day.nr - 7
		endif
	endif

	return(l.day.nr)
}								|#PERF.en



function domain	tctxtn fill.text.fields(
		domain	tcmcs.long	i.day.number)
{
	var.bptmm211.txta = 0
	var.bptmm211.txtb = 0
	var.bptmm211.txtc = 0
	var.bptmm211.txtd = 0
	var.bptmm211.txte = 0
	var.bptmm211.txtf = 0
	var.bptmm211.txtg = 0

	on case i.day.number

	case 0:
	case 1:
		var.bptmm211.txta = tihra100.txta
		return(var.bptmm211.txta)
	case 2:
		var.bptmm211.txtb = tihra100.txta
		return(var.bptmm211.txtb)
	case 3:
		var.bptmm211.txtc = tihra100.txta
		return(var.bptmm211.txtc)
	case 4:
		var.bptmm211.txtd = tihra100.txta
		return(var.bptmm211.txtd)
	case 5:
		var.bptmm211.txte = tihra100.txta
		return(var.bptmm211.txte)
	case 6:
		var.bptmm211.txtf = tihra100.txta

		return(var.bptmm211.txtf)
	case 7:
		var.bptmm211.txtg = tihra100.txta
		return(var.bptmm211.txtg)
	default:
		return(0)
	endcase

	return(0)
}

function domain bccost get.cost.component.from.tpppc(
		domain	tppdm.ccco	i.ccco,
		domain	tppdm.cprj	i.cprj,
		domain	tppdm.cotp	i.cost.type,
		domain	tppdm.year	i.year,
		domain	tppdm.peri	i.peri,
		domain	tphrs.empr	i.empr,
		domain	tcemno		i.emno,
		domain	tppdm.serc	i.serc)
{
		domain	bccost		l.cost.component

	if isspace(i.ccco) then
		|* If no cost components are defined in tphrs100, then cost
		|* components are not allowed due to the cost control settings
		|* in session tppdm6102s000.
		return("")

	endif

	l.cost.component = ""

	if i.cost.type = tppdm.cotp.tasks then
		select	tpppc231.ccco :l.cost.component
		from	tpppc231
		where	tpppc231._index9 = {	:i.year,
						:i.peri,
						:i.empr,
						:i.emno,
						:i.serc}
		as set with 1 rows
		selectdo
		selectempty
			|* Record is present in history tpppc235. The cost
			|* component cannot be retrieved from tpppc235, because
			|* there is no hours link with tpppc235. The 'best' cost
			|* component is the one of tphrs100.
			l.cost.component = i.ccco
		endselect
	else
		select	tpppc291.ccco :l.cost.component
		from	tpppc291
		where	tpppc291._index7 = {	:i.year,
						:i.peri,
						:i.empr,
						:i.emno,
						:i.serc}
		as set with 1 rows
		selectdo
		selectempty
			|* Record is present in history tpppc295. The cost
			|* component cannot be retrieved from tpppc295, because
			|* there is no hours link with tpppc295. The 'best' cost
			|* component is the one of tphrs100.
			l.cost.component = i.ccco
		endselect
	endif

	if not isspace(l.cost.component) then
		get.cost.components(
				bcorig.tppdm,
				"",
				l.cost.component,
				dummy.err.mess)
		l.cost.component = bcstc006.cost
	endif

	return(l.cost.component)
}

function domain tcdsca get.cost.object.description(
		domain	tppdm.cotp	i.cotp,
		domain	tppdm.task	i.ccun)
{
	static	domain	tppdm.cotp	s.cotp
	static	domain	tppdm.task	s.ccun	fixed
	static	domain	tcdsca		s.ccun.dsca

	if i.cotp = s.cotp and
	   i.ccun = s.ccun then
		return(s.ccun.dsca)
	endif

	on case i.cotp
	case tppdm.cotp.tasks:
		|* Function present in include itppdm0007
		read.task(	i.ccun,
				tphrs100.cprj,
				s.ccun.dsca,
				2,
				0,
				0)
		break
	case tppdm.cotp.indirect:
		|* Function present in include itppdm0066
		copy.string.with.different.domains(
				i.ccun,		|* Input
				i.ccun,		|* Output
				"tppdm.cico")
		|* Function present in include itppdm0011
		read.indirect.cost.code(

				i.ccun,
				tphrs100.cprj,

				s.ccun.dsca,
				2,
				0,
				0)
		break
	endcase

	s.cotp = i.cotp
	s.ccun = i.ccun

	return(s.ccun.dsca)
}

function void fill.labor.costs.from.ti(
		domain	tcmcs.long	i.day.number)
{
	|**********************************************************************
	|* Field bptmm211.prlc is an array of depth 24.
	|* position 1, 2, 3 belongs to costs of day 1
	|* position 4, 5, 6 belongs to costs of day 2
	|* position 7, 8, 9 belongs to costs of day 3
	|* ...
	|* During migration only positions 1, 4, 7, ..., 22 are filled, because
	|* only the first position of the currency-array is being filled.
	|* Day 1 relates to position 1

	|* Day 2 relates to position 4
	|* Day 3 relates to position 7
	|* ...
	|* The total is stored in position 22.
	|* A bit of mathematics helps us in determining the exact relationship:
	|* <position.in.array> = <day.number - 1> * 3 + 1

	|**********************************************************************


		domain	tcmcs.long	l.position

	set.mem(var.bptmm211.prlc, 0.0)

	|if tihra100.wgca = 0.0 then				|#MCR.o
	if fill.mc.field.double(tihra100.wgca,1,0.0) = 0.0 then |#MCR.n
		return
	endif

	l.position = ((i.day.number - 1) * 3) + 1

|	var.bptmm211.prlc(l.position) = tihra100.wgca		|#MCR.o
								|#MCR.sn
	var.bptmm211.prlc(l.position) = fill.mc.field.double(
							tihra100.wgca,
							1,
							0.0)
	|* The below changes are used to fill the positions (2,3,5,6,8,9 ---etc)
	|* with MCR fields
	l.position = ((i.day.number - 1) * 3) + 2
	var.bptmm211.prlc(l.position) = fill.mc.field.double(
							tihra100.wgca,
							2,
							0.0)
	l.position = ((i.day.number - 1) * 3) + 3
	var.bptmm211.prlc(l.position) = fill.mc.field.double(
							tihra100.wgca,
							3,
							0.0)	|#MCR.en
|	var.bptmm211.prlc(22) = tihra100.wgca			|#MCR.o
								|#MCR.sn
	var.bptmm211.prlc(22) = fill.mc.field.double(tihra100.wgca, 1, 0.0)
	var.bptmm211.prlc(23) = fill.mc.field.double(tihra100.wgca, 2, 0.0)
	var.bptmm211.prlc(24) = fill.mc.field.double(tihra100.wgca, 3, 0.0)
								|#MCR.en
}

function void fill.hours.from.tp()
{
		domain	tisfc.prtm	l.sum.hrea
		domain	tcmcs.long	l.day.number

	set.mem(var.bptmm211.hrea, 0.0)
	l.sum.hrea = 0.0

	for l.day.number = 1 to 7
		var.bptmm211.hrea(l.day.number) = tphrs100.nhrs(l.day.number)

		l.sum.hrea = l.sum.hrea + tphrs100.nhrs(l.day.number)
	endfor

	var.bptmm211.hrea(8) = l.sum.hrea
}

function domain tcckow get.labor.type.from.tp(
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco)
{
		domain	tcckow		l.labor.type

	read.labor.type.from.wage.component(	i.empr,
						i.wgco,
						l.labor.type)	|* empty FOR EXPENSES

	if not isspace(l.labor.type) then

		ret.long = get.labor.type(
				bcsbp.orig.tp,
				l.labor.type,	|* Input
				dummy.err.mess)
		if ret.long = 0 then
			return(bcsbp002.nhlt)
		endif

	else
		ret.long = get.default.labor.type(
				l.labor.type,	|* Output
				dummy.err.mess)
		if ret.long = 0 then
			return(l.labor.type)
		endif

	endif

	return("")
}


function void get.expense.description(
		domain	tphrs.empr	i.empr,
		domain	tphrs.wgco	i.wgco,
	ref	domain	tcdsca		o.dsca)
{
	static	domain	tphrs.empr	s.empr
	static	domain	tphrs.wgco	s.wgco	fixed
	static	domain	tcdsca		s.dsca

	if s.empr = i.empr and
	   s.wgco = i.wgco then
		o.dsca = s.dsca
		return
	endif

	select	tphrs010.desc :o.dsca
	from	tphrs010
	where	tphrs010._index1 = {	:i.empr,
					:i.wgco}
	as set with 1 rows
	selectdo

		s.empr = i.empr
		s.wgco = i.wgco
		s.dsca = o.dsca
	endselect
}

function domain bcstd.fent get.department.from.employee(
		domain	tcemno		i.emno)
{
	static	domain	tcemno		s.emno
	static	domain	bcstd.fent	s.department

	if s.emno <> i.emno then

		select	tccom001.cwoc :s.department
		from	tccom001
		where	tccom001._index1 = {:i.emno}
		as set with 1 rows
		selectdo
			s.emno = i.emno
			if isspace(s.department) then		|#800-154843.sn
				s.department = bcsbp000.cwoc
			endif					|#800-154843.en
		selectempty
			s.department = bcsbp000.cwoc
		endselect
	endif

	return(s.department)
}

function domain bcstd.fent get.service.department(
		domain	tcpdno		i.orno)

{
	static	domain	tcpdno		s.orno
		domain	tcncmp		l.log.company
		domain	tcncmp		l.fin.company
		domain	bcstd.fent	l.enterprise.unit

	static	domain	bcstd.fent	s.service.dept

	if s.orno <> i.orno then
		select	tssma301.ncmp :l.fin.company
		from	tssma301
		where	tssma301._index1 = {:i.orno}
		as set with 1 rows
		selectdo
			l.log.company = get.compnr()
			l.enterprise.unit =	str$(l.log.company) &
						str$(l.fin.company)


			select	bcstc018.srfd :s.service.dept
			from	bcstc018
			where	bcstc018._index1 = {:l.enterprise.unit}
			as set with 1 rows
			selectdo
				s.orno = i.orno
				return(s.service.dept)
			endselect

		endselect
	endif

	return(s.service.dept)
}

function void read.hra.parameters()
{
	static	domain	tcbool		s.hra.parameters.read

	if s.hra.parameters.read then
		return
	endif

	select	tihra000.cltm, tihra000.perl
	from	tihra000
	where	tihra000._index1 = {1}
	as set with 1 rows
	selectdo
		s.hra.parameters.read = true
	endselect
}

function domain tccpcp get.cost.component.from.ts(
		domain	tcpdno		i.orno,
		domain	tctano		i.task)
{
		domain	tccuno		l.customer
		domain	tccpcp		l.cost.component
		domain	tcitem		l.item
		domain	tcpric		o.dummy.pric
		domain	tssma.mess	o.dummy.mess

	l.customer = read.customer.from.service.order(i.orno)

	|if itssma0011.search.rate.from.task(			|#MCR.so
	|				l.customer,
	|				i.task,
	|				l.item,
	|				o.dummy.pric,
	|				o.dummy.mess) then	|#MCR.eo
	if search.rate.from.task(				|#MCR.sn
			l.customer,
			i.task,
			l.item,
			o.dummy.pric,
			o.dummy.mess) then			|#MCR.en
		l.cost.component = read.cost.component.from.item(l.item)
	else
		l.cost.component = bcsbp000.cpcp
	endif

	get.cost.components(
			bcorig.ticpr,
			l.cost.component,
			"",
			dummy.err.mess)

	return(bcstc006.cost)
}

function domain tccuno read.customer.from.service.order(
		domain	tcpdno		i.orno)
{

	static	domain	tcpdno		s.orno
	static	domain	tccuno		s.cuno

	if s.orno = i.orno then
		return(s.cuno)
	endif

	select	tssma301.cuno :s.cuno
	from	tssma301
	where	tssma301._index1 = {:i.orno}
	as set with 1 rows
	selectdo
		s.orno = i.orno
	endselect

	return(s.cuno)
}

function domain tccpcp read.cost.component.from.item(
		domain	tcitem		i.item)
{
	static	domain	tcitem		s.item
	static	domain	tccpcp		s.cpcp

	if s.item = i.item then
		return(s.cpcp)
	endif

	select	tiitm001.cpcp :s.cpcp
	from	tiitm001
	where	tiitm001._index1 = {:i.item}
	as set with 1 rows
	selectdo
		s.item = i.item
	endselect

	return(s.cpcp)
}

function long fill.field.long(
		long			i.value)
{
	if i.value = 0 then
		ret.long = empty()
	else
		ret.long = i.value
	endif

	return(ret.long)
}

function double fill.field.double(
		double			i.value)
{
	if i.value = 0.0 then
		ret.double = empty()
	else
		ret.double = i.value
	endif


	return(ret.double)
}

function string fill.field.string(
		domain	bcstr50		i.value)
{
	if isspace(i.value) then
		ret.string = empty()
	else

		ret.string = i.value
	endif

	return(ret.string)
}

function domain	bcstr22 get.transaction.date(			|#147118.sn
		domain	tcdate		i.transaction.date,
		domain	tctime		i.transaction.time)
{
	if i.transaction.date = 0 then
		|* Transaction Date not filled; fill with
		|* current time stamp.
		return( utc(date.num(), time.num()) )
	endif
		
	return( utc(i.transaction.date, i.transaction.time) )
}								|#147118.en


								|#158562.sn
function domain tcmcs.long get.sequence.number(
		domain	tcyrno		i.year,
		domain	tcweek		i.week,
		domain	tcemno		i.emno,
		domain	tcmcs.long	i.type)
{
|	Dllusage
|	Expl:	This function determines the sequence number. 
|		The sequence number is not taken anymore from BaanIV because
|		there were too large numbers present which did not fit in the domain
|		present in LN (maximum of domain bpmdm.serd is 9999; maximum
|		of domain tihra.sern is 99999).
|		Now tihra100 is read with order by on year, week, emno, koht. Then
|		we can determine the sequence number in the right order to be used
|		in LN.
|	Input:
|		i.year - Year
|		i.week - Week Number
|		i.emno - Employee
|		i.type - Indicates type of hours
|	Enddllusage

	static	domain	tcyrno		hold.year
	static	domain	tcweek		hold.week
	static	domain	tcemno		hold.emno
	static	domain	tcmcs.long	hold.type
	static	domain	tcmcs.long	hold.sequence

	if hold.year = i.year and
	   hold.week = i.week and
	   hold.emno = i.emno and
	   hold.type = i.type then
		hold.sequence = hold.sequence + 1
		return(hold.sequence)
	endif

	hold.year = i.year
	hold.week = i.week
	hold.emno = i.emno
	hold.type = i.type
	hold.sequence = 1
	return(hold.sequence)
}								|#158562.en
								|#GEN-0205.sn
function domain tccwoc get.employee.department(
		domain	tcemno		i.employee)
{
	static	domain	tcemno		s.employee
	static	domain	tccwoc		s.employee.department	fixed

	if s.employee = i.employee then
		return(s.employee.department)
	endif

	s.employee = i.employee
	
	select	tccom001.cwoc:s.employee.department
	from	tccom001
	where	tccom001._index1 = {:i.employee}
	as set with 1 rows
	selectdo
		break
	selectempty
		s.employee.department = ""
	endselect
	
	return(s.employee.department)
}
								|#GEN-0205.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:dm
:TCMOD:chm
:TFLNO:040
:FUNC:START
function string get.change.header(domain		tcorno	orno)
{
	tmp.var = str$(orno)
	str.lenth = len (tmp.var)
	on case  str.lenth
		case 1:
			change.id = concat$("-","CH",concat$("0","0000",tmp.var(1;1)))
			break
		case 2:
			change.id = concat$("-","CH",concat$("0","000",tmp.var(1;2)))
			break
		case 3:
			change.id = concat$("-","CH",concat$("0","00",tmp.var(1;3)))
			break
		case 4:
			change.id = concat$("-","CH",concat$("0","0",tmp.var(1;4)))
			break
		case 5:
			change.id = concat$("-","CH",concat$("0","",tmp.var(1;5)))
			break
		case 6:
			change.id = concat$("-","CH",tmp.var)
	endcase
	return(change.id)
}

function	get.company.currency()
{
	domain	tcncmp	comp.no
	comp.no = get.compnr()
	select	tccom000.ccur
	from	tccom000
	where	tccom000.ncmp = :comp.no
	as set with 1 rows
	selectdo
	endselect
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:dm
:TCMOD:chm
:TFLNO:050
:FUNC:START
function string get.change.header(domain		tcorno	orno)
{
	tmp.var = str$(orno)
	str.lenth = len (tmp.var)
	on case  str.lenth
		case 1:
			change.id = concat$("-","CH",concat$("0","0000",tmp.var(1;1)))
			break
		case 2:
			change.id = concat$("-","CH",concat$("0","000",tmp.var(1;2)))
			break
		case 3:
			change.id = concat$("-","CH",concat$("0","00",tmp.var(1;3)))
			break
		case 4:
			change.id = concat$("-","CH",concat$("0","0",tmp.var(1;4)))
			break
		case 5:
			change.id = concat$("-","CH",concat$("0","",tmp.var(1;5)))
			break
		case 6:
			change.id = concat$("-","CH",tmp.var)
	endcase
	return(change.id)
}

function string get.order.id(domain	tcorno	orno)
{
	tmp.var = str$(orno)
	str.lenth = len (tmp.var)
	on case  str.lenth
		case 1:
			order.id = concat$("-","O",concat$("0","0000",tmp.var(1;1)))
			break
		case 2:
			order.id = concat$("-","O",concat$("0","000",tmp.var(1;2)))
			break
		case 3:
			order.id = concat$("-","O",concat$("0","00",tmp.var(1;3)))
			break
		case 4:
			order.id = concat$("-","O",concat$("0","0",tmp.var(1;4)))
			break
		case 5:
			order.id = concat$("-","O",concat$("0","",tmp.var(1;5)))
			break
		case 6:
			order.id = concat$("-","O",tmp.var)
	endcase
	return(order.id)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tc
:TCMOD:com
:TFLNO:001
:FUNC:START
function domain	tccwoc get.default.service.department()
{
	if not get.enterprise.unit(get.compnr(), get.compnr(), dummy.err.mess) then
		return(bcstc018.srfd)
	endif

	return("")
}

function domain tcbool service.employee.must.be.created(
		domain	tcemno		i.emno)
{
	static	domain	tcemno		s.emno
	static	domain	tcbool		s.retval

	if s.emno = i.emno then
		return(s.retval)
	endif

	select	bcsts002.ccar
	from	bcsts002
	where	bcsts002._index1 = {:i.emno}
	as set with 1 rows
	selectdo
		s.retval = true
		return(true)
	selectempty
		s.retval = false
		return(false)
	endselect

	s.retval = false
	return(false)
}
										|#215115.sn
function get.logon.code.for.theemployee()
{
	select	bcsti011.*
	from	bcsti011
	where	bcsti011.emno = :tccom001.emno
	as set with 1 rows
	selectdo
	selectempty						|#235143.sn
		bcsti011.logn = ""				|#235143.en
	endselect
}
										|#215115.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tc
:TCMOD:com
:TFLNO:100
:FUNC:START
#include	"itcmcs0095"

function create.default.parent.bp(
		ref	domain	bcstc.bpid	o.dpbp)
{

	|**************************************************************
	|* This functions creates and returns de default parent business partner.
	|* The code of the default parent (target format)  is defined in bcstc000
	|* Only tccom100 will be created (using dump13).
	|**************************************************************

	|* early return if def. parent is not defined
	if isspace(bcstc000.dpbp) then
		o.dpbp = ""
		return
	else
		o.dpbp = bcstc000.dpbp
	endif

	|* make sure that name is filled
	if isspace(bcstc000.nama) then
		bcstc000.nama = "Default Parent"
	endif


	|* get adress code of company (via mapping table bcst004)
	 if get.addresses("tccom000", edit$(hold.cmpnr, "999"), o.mess) then
 	 	 var.tccom010.cadr =  ""
 	 else
 	 	 var.tccom010.cadr =  bcstc004.cadr
 	 endif

	get.dial.codes.of.country(	bcstc004.ccty,
					g.dialcode.for.telephone,
					g.dialcode.for.fax)
	var.phone.number  =  determine.communication.number(
					bcstc004.telp,
					g.dialcode.for.telephone)
	var.country.for.phone.number  =  determine.country.for.communication.number(
					bcstc004.telp,
					g.dialcode.for.telephone)
	var.fax.number  =  determine.communication.number(
					bcstc004.tefx,
					g.dialcode.for.fax)
	var.country.for.fax.number  =  determine.country.for.communication.number(
					bcstc004.tefx,
					g.dialcode.for.fax)

	|* language and currency are read from tccom000.
	|* this record is already current; will be used in dump.
	|*  no furthur action required here

	|* all other fields will be empy or default (dump)

	dump 13 |#tccom100
}

function add.business.partner.to.list(domain bcstc.bpid i.business.partner)
{
        get.next.code.depth(bp.code.depth)
        bp.code.list(1, bp.code.depth) = i.business.partner
}

function sort.bp.code.list()
{
	long 	rt

	rt = qss.sort(bp.code.list, sorted.bp.code.def, bp.code.depth)

        	if rt <> 0 then
                bp.code.depth = 0
        	endif
}

function get.next.code.depth(REF long  i)
{
                long    rt
        static  long    already.allocated
        static  long    allocated.length

        i = i + 1

        if not already.allocated or i > allocated.length then
                if not already.allocated then
                        allocated.length = MEMORY_ALLOCATION_SIZE
                        already.allocated = true
                else
                        allocated.length = allocated.length + MEMORY_ALLOCATION_SIZE
                endif

                rt = alloc.mem(bp.code.list, bp.code.length, allocated.length)
        endif
}

function define.length.and.search.def()
{
	long	dummy.convert

        rdi.domain.string("bcstc.bpid", bp.code.length, dummy.convert)

        qss.start(sorted.bp.code.def, 1, 1)
        qss.way(sorted.bp.code.def, 1, QSS.UP)
        qss.type(sorted.bp.code.def, 1, DB.STRING)
        qss.length(sorted.bp.code.def, 1, bp.code.length)
}

function domain tcbool bp.code.in.sorted.list(domain bcstc.bpid i.bp.code)
{
	long ret
	if bp.code.depth = 0 then
		return(false)
	endif

	ret = qss.search(QSS.EQUAL+QSS.SRC.IS.SORTED, i.bp.code,
			 bp.code.list, sorted.bp.code.def,
			 bp.code.depth)

	if ret > 0 then
		return(true)
	endif
	return(false)
}

function domain tcbool purchase.contract.or.order.found(domain tcsuno i.suno)
{
	|* check for purchase contract present
	select	tdpur300.cono, tdpur300.suno
	from	tdpur300
	where	tdpur300._index2 = {:i.suno}
	as set with 1 rows
	selectdo
		return(true)
	endselect

	|* check for purchase orders present
	select 	tdpur040.orno, tdpur040.suno
	from	tdpur040
	where	tdpur040._index2 = {:i.suno}
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function get.mstm.docm()
{
	var.mstm = ""
	var.docm = ""

	select tfacp316.mstm:var.mstm
	from  tfacp316
	where tfacp316._index1 = {:tccom020.suno}
	selectdo
	endselect

	select tfacp262.suno,tfacp262.docm:var.docm
	from  tfacp262
	where tfacp262._index1 = {:tccom020.suno}
	selectdo
	endselect
}

function void get.dial.codes.of.country(
		domain	tcccty		i.country,
	ref	domain	tctfcd		o.dial.code.telephone,
	ref	domain	tctfcd		o.dial.code.fax)
{
	static	domain	tcccty		s.country
	static	domain	tctfcd		s.dial.code.telephone
	static	domain	tctfcd		s.dial.code.fax

	o.dial.code.telephone = ""
	o.dial.code.fax = ""

	if isspace(i.country) then
		return
	endif

	if i.country = s.country then
		o.dial.code.telephone = s.dial.code.telephone
		o.dial.code.fax = s.dial.code.fax
		return
	endif

	s.country = i.country

	select	tcmcs010.tfcd :o.dial.code.telephone,
		tcmcs010.fxcd :o.dial.code.fax
	from	tcmcs010
	where	tcmcs010._index1 = {:i.country}
	as set with 1 rows
	selectdo
		s.dial.code.telephone = o.dial.code.telephone
		s.dial.code.fax = o.dial.code.fax
	endselect
}

function string determine.communication.number(
		domain	tctelp		i.number,
		domain	tctfcd		i.dial.code)
{
	return(bcint.dll0000.determine.communication.number(
					i.number,
					i.dial.code))
}

function string determine.country.for.communication.number(
		domain	tctelp		i.number,
		domain	tctfcd		i.dial.code)
{
	return(bcint.dll0000.determine.country.for.communication.number(
					i.number,
					i.dial.code))
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tc
:TCMOD:mcs
:TFLNO:010
:FUNC:START
function void read.tccom999.bnch()
{
	select	tccom999.bnch
	from	tccom999
	where	tccom999._index1 = {1}
	as set with 1 rows
	selectdo
	endselect
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acp
:TFLNO:005
:FUNC:START
function void convert.kind.of.transaction(
		domain	tfacp.tran	org.transaction.type,
	ref	long			conv.transaction.type)
{
	conv.transaction.type = 0

	on case org.transaction.type
	case tfacp.tran.advance:
		conv.transaction.type = target(tfacp.tran.advance)
		|* Advance Payments-Tangible Assets
		break
	case tfacp.tran.real.curr.prof:
		conv.transaction.type = target(tfacp.tran.real.curr.prof)
		|* Realized Currency Profit
		break
	case tfacp.tran.real.curr.loss:
		conv.transaction.type = target(tfacp.tran.real.curr.loss)
		|* Realized Currency Loss
		break
	case tfacp.tran.not.real.curr.p:
		conv.transaction.type = target(tfacp.tran.not.real.curr.p)
		|* Unrealized Currency Profit
		break
	case tfacp.tran.not.real.curr.l:
		conv.transaction.type = target(tfacp.tran.not.real.curr.l)
		|* Unrealized Currency Loss
		break	
	case tfacp.tran.payment.diff:
		conv.transaction.type = target(tfacp.tran.payment.diff)
		|* Payment Differences
		break
	case tfacp.tran.discount:
		conv.transaction.type = target(tfacp.tran.discount)
		|* Discount
		break
	case tfacp.tran.surcharge:
		conv.transaction.type = target(tfacp.tran.surcharge)
		|* Late Payment Surcharge
		break
	case tfacp.tran.inv.receipts:
		conv.transaction.type = target(tfacp.tran.inv.receipts)
		|* Invoice Receipts
		break
	case tfacp.tran.anticipated:
		conv.transaction.type = target(tfacp.tran.anticipated)
		|* Anticipated Payments
		break
	case tfacp.tran.deductions:
		conv.transaction.type = target(tfacp.tran.deductions)
		|* Deductions Antipated Payments
		break
	case tfacp.tran.net.advance:
		conv.transaction.type = target(tfacp.tran.net.advance)
		|* Nett Advanced Payments-Tangible Assets
		break
	case tfacp.tran.gross.advance:	
		conv.transaction.type = target(tfacp.tran.gross.advance)
		|* Gross Advanced Payments-Tangible Assets
		break
	case tfacp.tran.price.diff:
		conv.transaction.type = target(tfacp.tran.price.diff)
		|* Price Differences
		break
	case tfacp.tran.unall.payment:
		conv.transaction.type = target(tfacp.tran.unall.payment)
		|* Unallocated Payments
		break
	case tfacp.tran.advance.intang:				|#200511.sn
		conv.transaction.type = target(tfacp.tran.advance.intng)
		|* Advance Payments-Intangible Assets
		break
	case tfacp.tran.gross.adv.intan:
		conv.transaction.type = target(tfacp.tran.gross.adv.intng)
		|* Gross Advance Payments-Intangible Assets
		break
	case tfacp.tran.net.adv.intang:
		conv.transaction.type = target(tfacp.tran.net.adv.intng)
		|* Net Advance Payments-Intangible Assets
		break
	case tfacp.tran.adv.inventory:
		conv.transaction.type = target(tfacp.tran.adv.inventory)
		|* Advance Payments-Inventory
		break
	case tfacp.tran.net.adv.invent:
		conv.transaction.type = target(tfacp.tran.net.adv.invent)
		|* Net Advance Payments-Inventories
		break
	case tfacp.tran.gross.adv.inven:
		conv.transaction.type = target(tfacp.tran.gross.adv.inven)
		|* Gross Advance Payments-Inventories
		break
	case tfacp.tran.advance.other:
		conv.transaction.type = target(tfacp.tran.advance.other)
		|* Advance Payments-Other Assets
		break
	case tfacp.tran.net.adv.other:
		conv.transaction.type = target(tfacp.tran.net.adv.other)
		|* Net Advance Payments-Other Assets
		break
	case tfacp.tran.gross.adv.other:
		conv.transaction.type = target(tfacp.tran.gross.adv.other)
		|* Gross Advance Payments-Other Assets
		break						|#200511.en
	endcase
}

function void add.defaults.new.type.of.transactions()
{
|* For each Type of Transaction that is new compared to BaanIV a record is added to tfacp005.
|* This is consistent with adding ledger accounts for a financial business partner group in Baan 6. 
|* The dimension types are filled if in the group  parameters the Dimension Used is set to yes.

	set.default.dimensions(
		var.tfacp005.typ1,
		var.tfacp005.typ2,
		var.tfacp005.typ3,
		var.tfacp005.typ4,
		var.tfacp005.typ5)
	
	var.tfacp005.tran = target(tfacp.tran.commissions)
	|* Commission Invoice Accrual
	dump 3
	
|	var.tfacp005.tran = target(tfacp.tran.advance.intng)	|#200511.so
|	|* Advance Payments Intangible Assets 
|	dump 3
|	
|	var.tfacp005.tran = target(tfacp.tran.net.adv.intng)
|	|* Nett Advance Payments Intangible Assets 
|	dump 3
|	
|	var.tfacp005.tran = target(tfacp.tran.gross.adv.intng)
|	|* Gross Advance Payments Intangible Assets 
|	dump 3
|	
|	var.tfacp005.tran = target(tfacp.tran.adv.inventory)
|	|* Advance Payments Inventory
|	dump 3
|	
|	var.tfacp005.tran = target(tfacp.tran.net.adv.invent)
|	|* Nett Advance Payments Inventory
|	dump 3
|	
|	var.tfacp005.tran = target(tfacp.tran.gross.adv.inven)
|	|* Gross Advance Payments Inventory
|	dump 3
|	
|	var.tfacp005.tran = target(tfacp.tran.advance.other)
|	|* Advance Payments-Other Assets
|	dump 3
|	
|	var.tfacp005.tran = target(tfacp.tran.net.adv.other)
|	|* Net Advance Payments- Other Assets
|	dump 3
|	
|	var.tfacp005.tran = target(tfacp.tran.gross.adv.other)
|	|* Gross Advance Payments- Other Assets
|	dump 3							|#200511.eo
	
	var.tfacp005.tran = target(tfacp.tran.doc.issued)
	|* Document Issued
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.to.bp)
	|* Document Sent to Business Partner
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.received)
	|* Document Received
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.payable)
	|* Document Payable
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.accepted)
	|* Document Accepted/Sent
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.matured)
	|* Document Matured
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.cancelled)
	|* Document Canceled
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.dishonored)
	|* Document Dishonored
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.paid)
	|* Document Paid
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.endorsed)
	|* Document Endorsed/Discounted/Collateral
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.doc.void)
	|* Document Void
	dump 3
	
	var.tfacp005.tran = target(tfacp.tran.adunal.ant.py)
	|* Anticipate Advance/Unallocated Payment
	dump 3
}	

function void set.default.dimensions(
	ref	domain	tfgld.dimn	o.dimension.type.1,
	ref	domain	tfgld.dimn	o.dimension.type.2,
	ref	domain	tfgld.dimn	o.dimension.type.3,
	ref	domain	tfgld.dimn	o.dimension.type.4,
	ref	domain	tfgld.dimn	o.dimension.type.5)
{
	domain	tcncmp 		group.company

	domain	tcyesno		dimension.type.1
	domain	tcyesno		dimension.type.2
	domain	tcyesno		dimension.type.3
	domain	tcyesno		dimension.type.4
	domain	tcyesno		dimension.type.5
	
	o.dimension.type.1 = 0
	o.dimension.type.2 = 0
	o.dimension.type.3 = 0
	o.dimension.type.4 = 0
	o.dimension.type.5 = 0

	select	tfgld004.gcmp:group.company
	from	tfgld004
	where	tfgld004._index1 = {1}
	as set with 1 rows
	selectdo
	endselect

	select	tfgld003.dim1:dimension.type.1,
		tfgld003.dim2:dimension.type.2,
		tfgld003.dim3:dimension.type.3,
		tfgld003.dim4:dimension.type.4,
		tfgld003.dim5:dimension.type.5
	from	tfgld003
	where	tfgld003._index1 = {1}
	and	tfgld003._compnr = :group.company
	as set with 1 rows
	selectdo
		if dimension.type.1 = tcyesno.yes then
			o.dimension.type.1 = 1
		endif	
		if dimension.type.2 = tcyesno.yes then
			o.dimension.type.2 = 2
		endif	
		if dimension.type.3 = tcyesno.yes then
			o.dimension.type.3 = 3
		endif	
		if dimension.type.4 = tcyesno.yes then
			o.dimension.type.4 = 4
		endif	
		if dimension.type.5 = tcyesno.yes then
			o.dimension.type.5 = 5
		endif	
	endselect	
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acp
:TFLNO:100
:FUNC:START
function void fill.dimensions(
		domain	tcsuno		i.business.partner,
	ref	domain	tfgld.dimx	o.dimension.1,
	ref	domain	tfgld.dimx	o.dimension.2,
	ref	domain	tfgld.dimx	o.dimension.3,
	ref	domain	tfgld.dimx	o.dimension.4,
	ref	domain	tfgld.dimx	o.dimension.5)
{
	static	domain	tcsuno		hold.business.partner
	static	domain	tfacp.fisu	hold.financial.suppl.group
	
	o.dimension.1 = "" 
	o.dimension.2 = ""
	o.dimension.3 = ""
	o.dimension.4 = ""
	o.dimension.5 = ""

	|* The dimensions could be read within 1 query whereby table tfacp005
	|* and tccom020 are joined. This has a disadvantage. Different business
	|* partners can belong to the same financial supplier group. By
	|* splitting the query we can seperatly check if the business partner
	|* changed and if the financial supplier group changed.
	if i.business.partner <> hold.business.partner then
		hold.business.partner = i.business.partner 
		get.financial.supplier.group(
			i.business.partner,
			hold.financial.suppl.group)
	endif
	
	get.dimensions.control.account(
	 	hold.financial.suppl.group,
		o.dimension.1, 
		o.dimension.2,
		o.dimension.3,
		o.dimension.4,
		o.dimension.5)	
}

function void get.financial.supplier.group(
		domain	tcsuno		i.business.partner,
	ref	domain	tfacp.fisu	o.financial.supplier.group)
{
	o.financial.supplier.group = ""
	
	select	tccom020.cfsg:o.financial.supplier.group
	from	tccom020
	where	tccom020._index1 = {:i.business.partner}
	as set with 1 rows
	selectdo	
	endselect
}



function void get.dimensions.control.account(
		domain	tfacp.fisu	i.financial.supplier.group,
	ref	domain	tfgld.dimx	o.dimension.1,
	ref	domain	tfgld.dimx	o.dimension.2,
	ref	domain	tfgld.dimx	o.dimension.3,
	ref	domain	tfgld.dimx	o.dimension.4,
	ref	domain	tfgld.dimx	o.dimension.5)
{
	static	domain	tfgld.dimx	hold.dimension.1
	static	domain	tfgld.dimx	hold.dimension.2
	static	domain	tfgld.dimx	hold.dimension.3
	static	domain	tfgld.dimx	hold.dimension.4
	static	domain	tfgld.dimx	hold.dimension.5
	static	domain	tcsuno		hold.financial.suppl.group

	if i.financial.supplier.group = hold.financial.suppl.group then
		o.dimension.1 = hold.dimension.1 
		o.dimension.2 = hold.dimension.2
		o.dimension.3 = hold.dimension.3
		o.dimension.4 = hold.dimension.4
		o.dimension.5 = hold.dimension.5
		return
	endif

	hold.financial.suppl.group = i.financial.supplier.group

	select	tfacp005.dim1:o.dimension.1, tfacp005.dim2:o.dimension.2,
		tfacp005.dim3:o.dimension.3, tfacp005.dim4:o.dimension.4,
		tfacp005.dim5:o.dimension.5
	from	tfacp005
	where	tfacp005._index1 = {:i.financial.supplier.group,
				    tfacp.tran.control}
	as set with 1 rows
	selectdo
	endselect

	hold.dimension.1 =  o.dimension.1
	hold.dimension.2 =  o.dimension.2
	hold.dimension.3 =  o.dimension.3
	hold.dimension.4 =  o.dimension.4
	hold.dimension.5 =  o.dimension.5
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acp
:TFLNO:200
:FUNC:START
function void convert.payment.procedure.step(
		domain	tfcmg.step   i.source.step,
	ref	long   		     o.target.step)
{
	on case i.source.step
	case tfcmg.step.doc.selected:
		o.target.step = target(tfcmg.step.doc.selected)
		break
	case tfcmg.step.doc.to.cust:
		o.target.step = target(tfcmg.step.doc.to.cust)
		break
	case tfcmg.step.doc.acc.by.cust:
		o.target.step = target(tfcmg.step.doc.acc.by.cust)
		break
	case tfcmg.step.doc.to.bank:
		o.target.step = target(tfcmg.step.doc.to.bank)
		break
	case tfcmg.step.doc.received:
		o.target.step = target(tfcmg.step.doc.received)
		break
	case tfcmg.step.doc.acc.sent:
		o.target.step = target(tfcmg.step.doc.acc.sent)
		break
	case tfcmg.step.doc.paid:
		o.target.step = target(tfcmg.step.doc.paid)
		break
	case tfcmg.step.doc.rejected:
		o.target.step = target(tfcmg.step.doc.rejected)
		break
	endcase
}

function domain tcbool factor.company.linked.to.business.partner(  
		domain 	tccuno		i.supplier,  
| 	 ref	domain 	tccuno	 	o.factor.company,			|#1431629.o
 	 ref	domain 	tccfcc	 	o.factor.company,			|#1431629.n
	 ref	domain	tccban		o.factor.bank)
{
	o.factor.bank = ""
	o.factor.company = ""

	select	tccom020.cfcc:o.factor.company, tcmcs025.cban:o.factor.bank
	from	tccom020, tcmcs025
	where	tccom020._index1 = {:i.supplier}
	and	tccom020.cfcc <> ""
	and	tccom020.cfcc refers to tcmcs025
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function void get.control.account(				|#FP3.sn
		domain	tcncmp		i.company,
		domain	tfgld.year	i.year,
		domain	tfgld.btno	i.batch,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
		domain	tfgld.lino	i.line,
		domain	tfacp.tpay	i.document.type,
	ref	domain	tfgld.leac	o.control.account,
	ref	domain	tfgld.dimx	o.dimension1,		|#1056135.sn
	ref	domain	tfgld.dimx	o.dimension2,		
	ref	domain	tfgld.dimx	o.dimension3,		
	ref	domain	tfgld.dimx	o.dimension4,		
	ref	domain	tfgld.dimx	o.dimension5)		|#1056135.en
{		
|******************************************************************************
|* This functions tries to get the control account from the (non) finalized
|* transactions. If the invoice is not finalized we read tfgld102. If it is
|* finalized we read tfgl106. It is possible that an invoice is corrected. In
|* that case we have to read the the correction document and, if the control
|* account is changed, overwrite it with the new value. Only finalized invoices
|* can be corrected.
|******************************************************************************

		domain	tfgld.ttyp	trans.type.corr
		domain	tfgld.docn	document.corr
		domain	tfgld.btno	batch.corr
		domain	tfgld.year	year.corr

	o.control.account = ""
	o.dimension1 = ""					|#1056135.sn
	o.dimension2 = ""				
	o.dimension3 = ""				
	o.dimension4 = ""					
	o.dimension5 = ""					|#1056135.en
		
	if invoice.is.finalized(
	      i.transaction.type,
	      i.document) then
		get.control.account.from.finalized.transactions(
	      	   i.transaction.type,
	      	   i.document,
	      	   i.line,
	      	   o.control.account,
	      	   o.dimension1,				|#1056135.sn
	      	   o.dimension2,			
	      	   o.dimension3,				
	      	   o.dimension4,				
	      	   o.dimension5)				|#1056135.sn
		|* Process Purchase Corrections
		if (i.document.type = tfacp.tpay.invoice or
		   i.document.type = tfacp.tpay.credit) and
		   get.correction.document(
		      i.transaction.type,
		      i.document,
		      trans.type.corr,
		      document.corr,
		      batch.corr,
		      year.corr) then
			get.control.account.from.correction(
				i.company,
				trans.type.corr,
				document.corr,
				batch.corr,
				year.corr,
				o.control.account,
				o.dimension1,			|#1056135.sn
				o.dimension2,			
				o.dimension3,				
				o.dimension4,				
				o.dimension5)			|#1056135.sn
		endif
	else
		get.control.account.from.nonfinalized.trans(
				i.company,
				i.year,
				i.batch,
				i.transaction.type,
				i.document,
				i.line,
				o.control.account,
				o.dimension1,			|#1056135.sn
				o.dimension2,			
				o.dimension3,				
				o.dimension4,				
				o.dimension5)			|#1056135.sn
		|* Purchase Corrections can be left out in case
		|* an invoice is not finalized because non
		|* finalized invoices cannot be corrected.
	endif	
}

function get.control.account.from.finalized.transactions(
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
		domain	tfgld.lino	i.line,
	ref	domain	tfgld.leac	o.control.account,
	ref	domain	tfgld.dimx	o.dimension1,		|#1056135.sn
	ref	domain	tfgld.dimx	o.dimension2,		
	ref	domain	tfgld.dimx	o.dimension3,		
	ref	domain	tfgld.dimx	o.dimension4,		
	ref	domain	tfgld.dimx	o.dimension5)		|#1056135.en
{
	|* Initialize
	o.control.account = ""
	o.dimension1 = ""					|#1056135.sn
	o.dimension2 = ""				
	o.dimension3 = ""				
	o.dimension4 = ""					
	o.dimension5 = ""					|#1056135.en
	
	select	tfgld106.leac:o.control.account,
		tfgld106.dim1:o.dimension1,			|#1056135.sn
		tfgld106.dim2:o.dimension2,			
		tfgld106.dim3:o.dimension3,			
		tfgld106.dim4:o.dimension4,		
		tfgld106.dim5:o.dimension5			|#1056135.sn
	from	tfgld106
	where	tfgld106._index1 = {:i.transaction.type,
								|#229214.so
|			            :i.document,
|				    :i.line}
								|#229214.eo
			            :i.document}		|#229214.n
	and	tfgld106.cont = tcyesno.yes
	and 	( tfgld106.catg in (tfgld.catg.purchase.inv,
				    tfgld.catg.purchase.cred) or
								|#229214.so
|		 (tfgld106.catg = tfgld.catg.cash and
|		  tfgld106.tcsh = tfcmg.tcsh.ssgn and
|		   tfgld106.tran in (tfcmg.tran.unalloc.paym,
|		   		     tfcmg.tran.advance.paym)))
								|#229214.eo
								|#229214.sn
		 (tfgld106.catg = tfgld.catg.cash and
		  tfgld106.tcsh = tfcmg.tcsh.ssgn and
		   tfgld106.tran in (tfcmg.tran.unalloc.paym,	
		   		     tfcmg.tran.advance.paym) and
		   tfgld106.olin = :i.line))	
								|#229214.en
	as set with 1 rows
	selectdo
	endselect
}	

function get.control.account.from.correction(
		domain	tcncmp		i.company,
		domain	tfgld.ttyp	i.trans.type.corr,
		domain	tfgld.docn	i.document.corr,
		domain	tfgld.btno	i.batch.corr,
		domain	tfgld.year	i.year.corr,
	ref	domain	tfgld.leac	io.control.account,
	ref	domain	tfgld.dimx	io.dimension1,		|#1056135.sn
	ref	domain	tfgld.dimx	io.dimension2,		
	ref	domain	tfgld.dimx	io.dimension3,		
	ref	domain	tfgld.dimx	io.dimension4,		
	ref	domain	tfgld.dimx	io.dimension5)		|#1056135.en
{
		domain	tfgld.leac	hold.control.account
		domain	tfgld.dimx	hold.dimension1		|#1056135.sn
		domain	tfgld.dimx	hold.dimension2		
		domain	tfgld.dimx	hold.dimension3		
		domain	tfgld.dimx	hold.dimension4		
		domain	tfgld.dimx	hold.dimension5		|#1056135.en
		domain	tfgld.lino	hold.line
|******************************************************************************
|* Explanation: this functions searches for the last correction document
|* to which a purchse invoice is linked. The control account of that last
|* correction document overwrites the original control account of the
|* invoice. The new attributes of the invoice, as a result of the
|* correction, are stored in the document with the highest line number
|* (tfgld106.olin/tfgld102.lino). So the order by is on the document 
|* date/time of the correction document and the line number,
|* all descending. 
|******************************************************************************

	|* Initialize
	hold.control.account = ""
	hold.dimension1 = ""					|#1056135.sn
	hold.dimension2 = ""				
	hold.dimension3 = ""				
	hold.dimension4 = ""					
	hold.dimension5 = ""					|#1056135.en

	select	tfgld102.leac:hold.control.account,
		tfgld102.dim1:hold.dimension1,			|#1056135.sn
		tfgld102.dim2:hold.dimension2,			
		tfgld102.dim3:hold.dimension3,			
		tfgld102.dim4:hold.dimension4,		
		tfgld102.dim5:hold.dimension5,			|#1056135.sn
		tfgld102.lino:hold.line	
	from	tfgld102
	where	tfgld102._index1 = {:i.company, :i.year.corr, :i.batch.corr,
				    :i.trans.type.corr, :i.document.corr}
	and 	tfgld102.catg = tfgld.catg.purchase.corr
	order by tfgld102.lino desc	
	as set with 1 rows
	selectdo
		io.control.account = hold.control.account
		io.dimension1 = hold.dimension1		|#1056135.sn
		io.dimension2 = hold.dimension1		
		io.dimension3 = hold.dimension1		
		io.dimension4 = hold.dimension1		
		io.dimension5 = hold.dimension1		|#1056135.sn
		return
		|* Non finalized corrections is will be regarded as of a later
		|* date than a finalized correction
	endselect

	select	tfgld106.leac:hold.control.account,
		tfgld106.dim1:hold.dimension1,			|#1056135.sn
		tfgld106.dim2:hold.dimension2,			
		tfgld106.dim3:hold.dimension3,			
		tfgld106.dim4:hold.dimension4,		
		tfgld106.dim5:hold.dimension5,			|#1056135.sn
		tfgld106.olin:hold.line		
	from	tfgld106
	where	tfgld106._index1 = {:i.trans.type.corr,
		                    :i.document.corr}
	and	tfgld106.cont = tcyesno.yes
	and 	tfgld106.catg = tfgld.catg.purchase.corr
	order by tfgld106.olin desc	
	as set with 1 rows
	selectdo
		io.control.account = hold.control.account
		io.dimension1 = hold.dimension1		|#1056135.sn
		io.dimension2 = hold.dimension1		
		io.dimension3 = hold.dimension1		
		io.dimension4 = hold.dimension1		
		io.dimension5 = hold.dimension1		|#1056135.sn
		return
	endselect
}	

function get.control.account.from.nonfinalized.trans(
		domain	tcncmp		i.company,
		domain	tfgld.year	i.year,
		domain	tfgld.btno	i.batch,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
		domain	tfgld.lino	i.line,
	ref	domain	tfgld.leac	o.control.account,
	ref	domain	tfgld.dimx	o.dimension1,		|#1056135.sn
	ref	domain	tfgld.dimx	o.dimension2,		
	ref	domain	tfgld.dimx	o.dimension3,		
	ref	domain	tfgld.dimx	o.dimension4,		
	ref	domain	tfgld.dimx	o.dimension5)		|#1056135.en
{
	|* Initialize
	o.control.account = ""
	o.dimension1 = ""					|#1056135.sn
	o.dimension2 = ""				
	o.dimension3 = ""				
	o.dimension4 = ""					
	o.dimension5 = ""					|#1056135.en
	
	select	tfgld102.leac:o.control.account,
		tfgld102.dim1:o.dimension1,			|#1056135.sn
		tfgld102.dim2:o.dimension2,			
		tfgld102.dim3:o.dimension3,			
		tfgld102.dim4:o.dimension4,		
		tfgld102.dim5:o.dimension5			|#1056135.sn
	from	tfgld102
	where	tfgld102._index1 = {:i.company,
				    :i.year,
				    :i.batch,
				    :i.transaction.type,
								|#229214.so
|			            :i.document,
|				    :i.line}
								|#229214.eo
			            :i.document}		|#229214.n
	and	tfgld102.cont = tcyesno.yes
	and 	( tfgld102.catg in (tfgld.catg.purchase.inv,
				    tfgld.catg.purchase.cred) or
								|#229214.so
|		 (tfgld102.catg = tfgld.catg.cash and
|		  tfgld102.tcsh = tfcmg.tcsh.ssgn and
|		   tfgld102.tran in (tfcmg.tran.unalloc.paym,
|		   		     tfcmg.tran.advance.paym)))
								|#229214.eo
								|#229214.sn
		 (tfgld102.catg = tfgld.catg.cash and
		  tfgld102.tcsh = tfcmg.tcsh.ssgn and
		   tfgld102.tran in (tfcmg.tran.unalloc.paym,
				     tfcmg.tran.advance.paym) and
		   tfgld102.lino = :i.line))	
								|#229214.en
	as set with 1 rows
	selectdo
	endselect
}
			
function domain tcbool invoice.is.finalized(
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document)
{
		domain	tfgld.trun	hold.run.number
		
	select	tfgld018.trun:hold.run.number
	from	tfgld018
	where	tfgld018._index1 = {:i.transaction.type,
				    :i.document}
	and	tfgld018.trun > 0			    
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}	

function domain tcbool get.correction.document(
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
	ref	domain	tfgld.ttyp	o.trans.type.corr,
	ref	domain	tfgld.docn	o.document.corr,
	ref	domain	tfgld.btno	o.batch.corr,
	ref	domain	tfgld.year	o.year.corr)
{

		domain	tfgld.date	hold.document.date
		domain	tcmcs.long	hold.time
		
	o.trans.type.corr = ""
	o.document.corr = 0
	o.batch.corr = 0
	o.year.corr = 0
	
	select	a.tdoc:o.trans.type.corr, a.docn:o.document.corr,
		a.btno:o.batch.corr, a.year:o.year.corr,
		tfgld018.date:hold.document.date, tfgld018.time:hold.time	
	from	tfacp200 a, tfgld018
	where	a._index1 = {:i.transaction.type, :i.document}
	and	a.tdoc <> ""
	and 	a.tpay = tfacp.tpay.correction
	and	tfgld018.ttyp = a.tdoc
	and	tfgld018.docn = a.docn
	order by tfgld018.date desc, tfgld018.time desc
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}								|#FP3.en

								|#1025236.so
|function get.payment.info(
|	ref domain	tcncmp		o.tfacp200.pcom,		
|	ref domain	tfgld.ttyp	o.tfacp200.payt,
|	ref domain	tfgld.docn	o.tfacp200.payd,
|	ref domain	tfgld.lino	o.tfacp200.payl)
|
|{
|	o.tfacp200.pcom = 0
|	o.tfacp200.payt = ""
|	o.tfacp200.payd = 0
|	o.tfacp200.payl = 0
|
|	select 	tfgld112.cono:o.tfacp200.pcom,
|		tfgld112.ttyp:o.tfacp200.payt,
|		tfgld112.docn:o.tfacp200.payd
|	from	tfgld112
|	where	tfgld112._index2 = {	:current.companyany,
|			    		:tfacp200.tdoc,
|			    		:tfacp200.docn}
|	as set with 1 rows
|	selectdo
|		o.tfacp200.payl = tfacp200.lino
|	selectempty
|		if not isspace(tfacp200.tdoc) then
|			o.tfacp200.pcom = current.companyany
|			o.tfacp200.payt = tfacp200.tdoc
|			o.tfacp200.payd = tfacp200.docn
|			o.tfacp200.payl = tfacp200.lino
|		endif
|	endselect
|}
								|#1025236.eo
								
								|#1025236.sn
function get.payment.info(
	ref domain	tcncmp		o.tfacp200.pcom,		
	ref domain	tfgld.ttyp	o.tfacp200.payt,
	ref domain	tfgld.docn	o.tfacp200.payd,
	ref domain	tfgld.lino	o.tfacp200.payl)

{
	domain	tcncmp		l.icom
	domain	tfgld.ttyp	l.ttyp
	domain	tfgld.docn	l.docn
	domain	tfgld.ttyp	l.search.ttyp
	domain	tfgld.docn	l.search.ninv

	o.tfacp200.pcom = 0
	o.tfacp200.payt = ""
	o.tfacp200.payd = 0
	o.tfacp200.payl = 0
	
	l.search.ttyp = ""
	l.search.ninv = 0


	if isspace(tfacp200.tdoc) then
		|* handle headers
		l.search.ttyp = ""
		l.search.ninv = 0			
		o.tfacp200.pcom = current.company
		if tfacp200.line > 0 then |* Payment header then
			o.tfacp200.payt = tfacp200.ttyp
			o.tfacp200.payd = tfacp200.ninv
			o.tfacp200.payl = tfacp200.line	
		endif
		if tfacp200.line > 0 and
                   (tfacp200.tpay = tfacp.tpay.advance.ant or 
                   tfacp200.tpay = tfacp.tpay.unallocated.ant or 
		   tfacp200.tpay = tfacp.tpay.advance or
                   tfacp200.tpay = tfacp.tpay.unallocated ) then                        
                        l.search.ttyp = tfacp200.ttyp
			l.search.ninv = tfacp200.ninv
                endif
		
	else
		|*handle subdocuments		
		l.search.ttyp = tfacp200.tdoc
		l.search.ninv = tfacp200.docn
	endif
	
	|*handle possible intercompany situation
	if not isspace(l.search.ttyp) then		
		if invoice.is.finalized(
				      l.search.ttyp,
				      l.search.ninv) then
			select 	tfgld106.osrn
			from	tfgld106
			where	tfgld106._index1 = {:l.search.ttyp,
						:l.search.ninv}
			and	tfgld106.ocmp <> :current.company 
			as set with 1 rows
			selectdo
				select 	tfgld112.cono:l.icom,
					tfgld112.ttyp:l.ttyp,
					tfgld112.docn:l.docn
				from	tfgld112
				where	tfgld112._index2 = {:current.company,
						    	:l.search.ttyp,
						    	:l.search.ninv}
				as set with 1 rows
				selectdo
					o.tfacp200.pcom = l.icom
					o.tfacp200.payt = l.ttyp
					o.tfacp200.payd = l.docn				
					o.tfacp200.payl = fill.payl.field(
								o.tfacp200.pcom,
								o.tfacp200.payt,
								o.tfacp200.payd)
				endselect
			selectempty
				|* no intercompany
				o.tfacp200.pcom = current.company
				o.tfacp200.payt = l.search.ttyp
				o.tfacp200.payd = l.search.ninv			
				o.tfacp200.payl = fill.payl.field(
							o.tfacp200.pcom,
							o.tfacp200.payt,
							o.tfacp200.payd)
			endselect 
		else
			select 	tfgld112.cono:l.icom,
				tfgld112.ttyp:l.ttyp,
				tfgld112.docn:l.docn
			from	tfgld112
			where	tfgld112._index2 = {:current.company,
					    	:l.search.ttyp,
					    	:l.search.ninv}
			as set with 1 rows
			selectdo
				o.tfacp200.pcom = l.icom
				o.tfacp200.payt = l.ttyp
				o.tfacp200.payd = l.docn				
				o.tfacp200.payl = fill.payl.field(
							o.tfacp200.pcom,
							o.tfacp200.payt,
							o.tfacp200.payd)
			selectempty
				|* no intercompany
				o.tfacp200.pcom = current.company
				o.tfacp200.payt = l.search.ttyp
				o.tfacp200.payd = l.search.ninv			
				o.tfacp200.payl = fill.payl.field(
							o.tfacp200.pcom,
							o.tfacp200.payt,
							o.tfacp200.payd)
			endselect
		endif
	endif
			
}



function domain	tfgld.lino fill.payl.field(
	 domain	tcncmp		i.tfacp200.pcom,		
	 domain	tfgld.ttyp	i.tfacp200.payt,
	 domain	tfgld.docn	i.tfacp200.payd)
{
	domain	tfgld.lino	line.number
	
	line.number = 0
	
	on case tfacp200.tpay
	case tfacp.tpay.assignment:
		line.number = get.line.mapped(tfacp200.lino)
		break
	case tfacp.tpay.normal:
	case tfacp.tpay.anticipated:
		if tfacp200.lino >= 10000 then
			line.number =
		   		tfcmg.dll8112.get.line.composed.anticipated(
					i.tfacp200.pcom,
					i.tfacp200.payt,
					i.tfacp200.payd,
					tfacp200.typa,
					tfacp200.doca)
		else
			line.number = tfacp200.lino
		endif
		break
	case tfacp.tpay.advance.ant:
	case tfacp.tpay.unallocated.ant:
	case tfacp.tpay.advance:
	case tfacp.tpay.unallocated:
		if tfacp200.line >= 10000 then
			line.number =
		   		tfcmg.dll8112.get.line.composed.anticipated(
					i.tfacp200.pcom,
					i.tfacp200.payt,
					i.tfacp200.payd,
					tfacp200.typa,
					tfacp200.doca)
		else
			line.number = tfacp200.line
		endif
		break
	default:
		line.number = tfacp200.lino
	endcase
	return(line.number)
}

function domain tfgld.lino get.line.mapped(
		domain	tfgld.lino	i.lino)
{
	domain	tfgld.lino	line.number

	|* Line number 10002 has to be mapped to 1 because the
	|* 10000 series belong to payment line 1. Line number
	|* 20001 has to be mapped to 2 because the 20000 series
	|* belong to payment line 2.
	line.number = i.lino \ 10000
	line.number = i.lino - line.number
	line.number = line.number / 10000

	return(line.number)
}


function extern domain tfgld.lino tfcmg.dll8112.get.line.composed.anticipated(
			domain	tcncmp		i.finance.company,
			domain	tfgld.ttyp	i.transaction.type,
			domain	tfgld.docn	i.document.number,
			domain	tfgld.ttyp	i.transaction.type.ant.payment,
			domain	tfgld.docn	i.document.number.ant.payment)
{
	DllUsage
	Expl.	: This function determines the payment line number. The 
		  transaction type, document number and sequence number remain
		  default.

	Pre	: -
	Post	: -
	Input	: i.finance.company	- Finance company
		  i.transaction.type	- The transaction type
		  i.document.number	- The document number
		  i.transaction.type.ant.payment
		  			- The transaction type of the
					  anticipated payment
		  i.document.number.ant.payment	
		  			- The document number of
					  the anticipated payment
		  i.business.partner	- The business partner

	Return	: line number
	EndDllUsage

	domain	tfgld.lino	line.number

	select	tfcmg112.clin:line.number
	from	tfcmg112
	where	tfcmg112._index2 = {:i.transaction.type, :i.document.number}
	and	tfcmg112.tdoc = :i.transaction.type.ant.payment
	and	tfcmg112.pdoc = :i.document.number.ant.payment
	and	tfcmg112._compnr = :i.finance.company
	as set with 1 rows
	selectdo
		break
	selectempty
		line.number = 0
	endselect
	
	return(line.number)
}								
								|#1025236.en

function void get.supplier.fin.business.partner.group(		|#Migr. 10.6.sn
		domain	tcncmp		i.company,
		domain	tcsuno		i.supplier	fixed,
	ref	domain	tfacp.fisu	o.fisc.bp.group)
{
	|**************************************************************
	|* Initialize variables.
	|**************************************************************
	o.fisc.bp.group = ""

	select	tccom020.cfsg:o.fisc.bp.group
	from	tccom020
	where	tccom020._compnr = :i.company
	and	tccom020._index1 = {:i.supplier}
	as set with 1 rows
	selectdo
	endselect
}								|#Migr. 10.6.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acp
:TFLNO:240
:FUNC:START
function handle.order.line()
{      
	|    *  Next situations are possible:   
	|    *    -  One entry in tdpur045 for order  /  position   
	|    *     	     -  no.of.receipt.lines  =  0 (no receipts yet)   
	|    *     	     -  tdpur045.srnb  =  0   
	|    *     	     -  one record added to tfacp240   
	|    *    -  One entry in tdpur045 for order  /  position   
	|    *     	     -  no.of.receipt.lines  =  1 (1 receipt)   
	|    *     	     -  tdpur045.srnb  =  1   
	|    *     	     -  one record added to tfacp240   
	|    *     	     -  one record added to tfacp245   
	|    *     	     -  This is a collect order (order and receipt in once)   
	|    *    -  Two entries in tdpur045 for order  /  position   
	|    *     	     -  no.of.repeipt.lines  =  1 (first receipt)   
	|    *     	     -  one record added to tfacp240   
	|    *     	     -  one record added to tfacp245, if tdpur045.date  <>  0   
	|    *    -  More than two entries in tdpur045 for order  /  position   
	|    *     	   (backorders present)   
	|    *     	     -  no.of.receipt.lines  >=  2   
	|    *     	     -  one record added to tfacp240 (original order line)   
	|    *     	     -  one record added to tfacp240 for each backorder   
	|    *     	     -  for each record of tdpur045 with tdpur045.date  <>  0   
	|    *     	      	   (actual receipt date) a record is added to   
	|    *     	      	   tfacp245.   
	if no.of.receipt.lines  =  0 or tdpur045.srnb  >=  1 then   
		|    *  Original order line and the backorders are inserted   
		|    *  in tfacp240   
		|    *  Only if there is no receipt tfacp240 record is   
		|    *  added seperately. Else tfacp240 is inserted when   
		|    *  tfacp245 is inserted.   
		var.tfacp240.sqnb  =  get.order.line.sequence(   
		      	      	   tdpur045.srnb)   
		var.tfacp240.otbp  =  get.business.partner.code(   
		      	      	   tdpur040.suno,   
		      	      	   bcstc.orgn.suno,   
		      	      	   error.message)   
		|    *  Also used for ifbp and sfbp   
		var.tfacp240.proj  =  get.project.by.company(   
		      	      	   tdpur045.cprj,   
		      	      	   logistic.company)   
		if not isspace(tdpur045.cprj) then   
			var.tfacp240.bppr  =  get.baan.project(   
						logistic.company,   
						tdpur045.cprj)   
		else   
			var.tfacp240.bppr  =  tcyesno.no   
		endif   	      
		if tdpur045.srnb  <  2 then   
		     |    *  Normal order   
			convert.quantity.from.order.unit.to.inventory.unit(   
				tdpur041.oqua,   
				var.tfacp240.iuni,   
				tdpur041.cvqp,   
				"",    
				"",    
				"tcqrd1",    
				var.tfacp240.oqan)   
		else   
			convert.quantity.from.order.unit.to.inventory.unit (   
				prev.bqua,   
				var.tfacp240.iuni,   
				tdpur041.cvqp,   
				"",    
				"",    
				"tcqrd1",    
				var.tfacp240.oqan)   
				|    *  Back order   
		endif   	      
		get.order.amount(var.tfacp240.oamt)   
		|    *  Calculate amount in home currencies   
		for element  =  1 to 3   
			ret.long  =  calculate.amount.in.home.currency(   
		      	      	   var.tfacp240.oamt,   
		      	      	   tdpur040.ccur,   
		      	      	   tdpur040.ratp,   
		      	      	   tdpur040.ratf,   
		      	      	   element,   
		      	      	   var.tfacp240.amth(element),   
		      	      	   error.message)   
		endfor   
		
		
		|    *  In Baan IV only one receipt per order line.   
		|    *  Quantities on order line can be taken from the receipt.   
		|    *  Mapping table is used for the receipts
		var.tfacp245.rcno  =  get.receipt.number()   
		var.tfacp245.rseq  =  get.receipt.line()   
		if double.cmp(tdpur041.oqua, 0.0, 0.00001)  =    -  1 then   
			|    *  Return order   
			convert.quantity.from.order.unit.to.inventory.unit (   
				tdpur045.quad,   	  
				var.tfacp240.iuni,   
				tdpur041.cvqp,   
				"",    
				"",    
				"tcqrd1",    
				var.tfacp240.tapq)   
			
				|    *  Approved quantity equals rejected quantity   
				var.tfacp240.trqu  =  0.0   
				|    *  Rejected quantity is zero   
				get.shipment.number.and.line(    
				tckoor.act.pur,   
				tdpur045.orno,   
				tdpur045.pono,   
				var.tfacp240.sqnb,   
				0,   	     |    *  Outbound Sequence     
				0,   
				error.message)   	     |    *  ref   
      	   	else   
			convert.quantity.from.order.unit.to.inventory.unit (   
			      	   tdpur045.quap,      
			      	   var.tfacp240.iuni,   
			      	   tdpur041.cvqp,   
			      	   "",    
			      	   "",    
			      	   "tcqrd1",    
			      	   var.tfacp240.tapq)   	      
			convert.quantity.from.order.unit.to.inventory.unit (   
			      	   tdpur045.quad,   	    
			      	   var.tfacp240.iuni,   
			      	   tdpur041.cvqp,   
			      	   "",    
			      	   "",    
			      	   "tcqrd1",    
			      	   var.tfacp240.trqu)   
		endif   
		convert.quantity.from.order.unit.to.inventory.unit (   
			tdpur045.dqua,   	 
			var.tfacp240.iuni,   
			tdpur041.cvqp,   
			"",    
			"",    
			"tcqrd1",    
			var.tfacp240.rqty)   
      	      
		if (tdpur045.matc  =  tdpur.matc.complete) or   
		   (tdpur045.stsd  =  tcstsd.all.approved) or 
		   (tdpur045.date  <>  0 and zero.receipt(   
						tdpur045.dqua,   
						tdpur045.quap,   
						tdpur045.quad,   
						tdpur041.oqua)) then   
		|    *  Zero receipt. In BaanIV there is only 1   
		|    *  receipt per orderline  /  sequence number.   
		|    *  If thereis no receipt a back order with   
		|    *  a new sequence number is made.   
			var.tfacp240.toma  =  tcyesno.yes   
		else   
			var.tfacp240.toma  =  tcyesno.no   
		endif   
		if tdpur045.date  <>  0 then   
			var.tfacp240.fire  =  tcyesno.yes   
		else   
			var.tfacp240.fire  =  tcyesno.no   
		endif   	      
		var.tfacp240.mcfr  =  get.tfacp240.mcfr()   
		|    *  Fill Rates   
		for element  =  1 to 3   
			var.tfacp240.rate(element)  =  fill.mc.field.double(   
								tdpur040.ratp,   
								element,   
								0.000001)   	      
			var.tfacp240.ratf(element)  =  fill.mc.field.long(   
								tdpur040.ratf,   
								element,   
								1)   	      
		endfor
		
		if prev.orno <> tdpur045.orno or
		   prev.pono <> tdpur045.pono or
		   prev.srnb <> tdpur045.srnb then
			dump 1
		endif   
		prev.orno = tdpur045.orno
		prev.pono = tdpur045.pono
		prev.srnb = tdpur045.srnb
	endif   
	
	|* now dump the receipts with the usage of the mapping table
	if (tdpur045.date  <>  0) and not zero.receipt(   
						tdpur045.dqua,   
						tdpur045.quap,   
						tdpur045.quad,   
						tdpur041.oqua) then 

		get.order.receipt.amount(var.tfacp245.oamt)   
 		if double.cmp(tdpur041.oqua, 0.0, 0.00001)  =    -  1 then   
			if g.use.mapping.table then   
				g.quantity  =  bcswh010.qapr   
			else   
				g.quantity  =  tdpur045.quad   
			endif   
			|    *  Return order   
			convert.quantity.from.order.unit.to.inventory.unit (   
				g.quantity,   	  
				var.tfacp240.iuni,   
				tdpur041.cvqp,   
				"",    
				"",    
				"tcqrd1",    
				var.tfacp245.quan)   
			
				|    *  Approved quantity equals rejected quantity   
				var.tfacp245.trqn  =  0.0   
      	   	else   
			if g.use.mapping.table then   
				g.quantity  =  bcswh010.qapr   
			else   
				g.quantity  =  tdpur045.quap   
			endif   
			convert.quantity.from.order.unit.to.inventory.unit (   
			      	   g.quantity,      
			      	   var.tfacp240.iuni,   
			      	   tdpur041.cvqp,   
			      	   "",    
			      	   "",    
			      	   "tcqrd1",    
			      	   var.tfacp245.quan)   	      
			if g.use.mapping.table then   
			   	g.quantity  =  bcswh010.qrej   
			else   
			   	g.quantity  =  tdpur045.quad   
			endif   
			convert.quantity.from.order.unit.to.inventory.unit (   
			      	   g.quantity,   	    
			      	   var.tfacp240.iuni,   
			      	   tdpur041.cvqp,   
			      	   "",    
			      	   "",    
			      	   "tcqrd1",    
			      	   var.tfacp245.trqn)   
		endif   
		if g.use.mapping.table then   
			g.quantity  =  bcswh010.qrcv   
		else   
			g.quantity  =  tdpur045.dqua   
		endif   
		convert.quantity.from.order.unit.to.inventory.unit (   
			g.quantity,   	 
			var.tfacp240.iuni,   
			tdpur041.cvqp,   
			"",    
			"",    
			"tcqrd1",    
			var.tfacp245.rqty)   

 		|    *  Actual receipt   
		var.tfacp245.borf  =  make.business.obj.ref(   
						tdpur045.pono,   
						tdpur045.srnb,   
						var.tfacp245.rcno,   
						var.tfacp245.rseq)  |     
		dump 3   
		
		if double.cmp(var.tfacp240.oqan, 0.0,0.000001)  =  0 then   
			var.tfacp249.gamt  =  var.tfacp245.oamt   
		else   
			var.tfacp249.gamt  = (var.tfacp240.oamt / 
						var.tfacp240.oqan) * var.tfacp245.quan   
			tccri.dll0011.get.rounding.code(   
				tdpur040.ccur,   
				var.inv.crnd)   
				var.tfacp249.gamt  =  icom0001.domain.round(   
				var.tfacp249.gamt,   
				"tfgld.amnt",    
				tdpur040.ccur,   
				var.inv.crnd)   
		endif
		dump 4   
	endif 
	g.prev.bqua  =  tdpur045.bqua   
 	g.prev.amld  =  tdpur045.ldam   
 	g.prev.amod  =  tdpur045.odam
  	      
}


function read.purchase.order.and.order.type.data(
		domain	tcorno		i.order,
		domain	tcncmp		i.logistic.company)
{
 	 select  tdpur040.cotp, tdpur040.suno, tdpur040.ccor,
 	 	 tdpur040.orno, tdpur040.ccur, tdpur040.odis,
		 tdpur040.odat, tdpur040.ratp, tdpur040.ratf,
		 tdpur040.ccon, 
		 tdpur040.cdec, tdpur040.cpay,			|#migration fp5.n
		 tcmcs042.reto, tcmcs042.coun,
		 tcmcs002.crnd
 	 from 	 tdpur040, tcmcs002, tcmcs042
 	 where 	 tdpur040._index1 = { :i.order}
	 and	 tdpur040._compnr = :i.logistic.company
 	 and 	 tcmcs002.ccur = tdpur040.ccur
	 and	 tcmcs042.cotp = tdpur040.cotp
 	 as set with 1 rows
 	 selectdo
 	 endselect
}

function void get.item.attributes(
		domain 	tcitem 	i.item,
		domain	tcittb	i.item.category,
		domain	tccprj	i.project,
	ref	domain	tckitm	o.item.type,
	ref	domain	tccitg	o.item.group,
	ref	domain	tccuni	o.inventory.unit)
{
	 	domain 	tcpuma 	 	project.item.type
 	static  domain  tccprj 	  	s.project
 	static  domain  tcittb 	  	s.item.category
 	static  domain  tcitem 	  	s.item
	static	domain	tckitm		s.item.type
	static	domain	tccitg		s.item.group
	static	domain	tccuni		s.inventory.unit
	 
	o.item.type = empty
	o.inventory.unit = "" 
	o.item.group = ""
	
 	 if s.project = i.project and
 	    s.item.category = i.item.category and
 	    s.item = i.item then
		o.item.type = s.item.type 
		o.item.group = s.item.group 
		o.inventory.unit = s.inventory.unit 
 	 	 return
 	 else
 	 	 s.project = i.project 
 	 	 s.item.category = i.item.category
 	 	 s.item = i.item
 	 endif
 	| **************************************************************  
 	| * For standard items the data is retrieved from tiitm001.  
 	| * For customized items the data is retrieved from tipcs021.  
 	| **************************************************************  
	if i.item.category = tcittb.standard then	
		select 	tiitm001.kitm:o.item.type,  
			tiitm001.citg:o.item.group,
			tiitm001.cuni:o.inventory.unit
 		from	tiitm001  
 		where	tiitm001._index1 =  {:i.item}  
 		as set with 1 rows 
		selectdo 





	 	endselect
	else	
		 select tipcs021.kitm:project.item.type,
 	 	 	 tipcs021.cuni:o.inventory.unit,
			 tipcs021.citg:o.item.group
 	 	 from 	 tipcs021
 	 	 where 	 tipcs021._index1 = {:i.project,
 	 	 	 	 	     :i.item}
 	 	 as set with 1 rows
 	 	 selectdo
 	 	 	 if project.item.type = tcpuma.purchase then
 	 	 	 	 o.item.type = tckitm.purchase
 	 	 	 endif
 	 	 	 if project.item.type = tcpuma.manufacture then
 	 	 	 	 o.item.type = tckitm.manufacture
 	 	 	 endif
 	 	 endselect
	endif	 
	
	s.item.type = o.item.type
	s.item.group = o.item.group 
	s.inventory.unit = o.inventory.unit
}

function domain tcpono get.order.line.sequence (domain tcsrnb i.sequence)
{
	if i.sequence = 0 then
		return ( 1 )
	else
		return ( i.sequence )
	endif
}

function get.number.of.receipt.lines ( 
		domain	tcorno	i.order,
		domain	tcpono	i.position,
		domain	tcncmp	i.logistic.company,
	ref	long		o.no.of.receipt.lines)	
{
 	 	 domain 	 tcorno 	 	 l.orno
 	 	 domain 	 tcpono 	 	 l.pono
 	 	 domain 	 tcsrnb 	 	 l.srnb
		 
 	 o.no.of.receipt.lines = 0

 	 select  tdpur045.srnb :o.no.of.receipt.lines,
 	 	 tdpur045.orno :l.orno,
 	 	 tdpur045.pono :l.pono,
 	 	 tdpur045.srnb :l.srnb
 	 from 	 tdpur045
 	 where 	 tdpur045._index1 = { 	 :i.order,
 	 	 	 	 	 :i.position}
	and	tdpur045._compnr = :i.logistic.company
 	 order by tdpur045._index1 desc
 	 as set with 1 rows
 	 selectdo 	
 	 endselect
}

 function domain tcitem get.item(
 		domain	tcncmp		i.logistic.company,
 		domain 	tcitem 		i.item,
		domain	tcittb		i.item.category,
		domain	tccprj		i.project)
 {
 	 if i.item.category = tcittb.customized then
 	 	 return ( segment.item.code (
 	 	 	 	 i.item,
 	 	 	 	 get.project.by.company(
				    i.project,
				    i.logistic.company)))
 	 else
 	 	 return ( segment.item.code (
 	 	 	 	 i.item,
 	 	 	 	 "" ))
 	 endif
 }
 
 function domain tcyesno get.baan.project(
 		domain	tcncmp	i.logistic.company,
 		domain 	tccprj 	i.project)
 {
 	 static 	 domain 	 tccprj 	 s.prev.project
 	 static 	 domain 	 tcyesno 	 s.baan.project

 	 if s.prev.project = i.project then
 	 	 return ( s.baan.project)
 	 else
 	 	 s.prev.project = i.project
 	 	 s.baan.project = tcyesno.no
 	 endif
	 
 	 if isspace ( i.project ) then
 	 	 return ( s.baan.project )
 	 endif
	 
 	 select  tipcs020.prtp
 	 from 	 tipcs020
 	 where 	 tipcs020._index1 = {:i.project}
	 and	tipcs020._compnr = :i.logistic.company
 	 as set with 1 rows
 	 selectdo
 	 	 if tipcs020.prtp = tcyesno.yes then
 	 	 	 s.baan.project = tcyesno.yes
 	 	 else
 	 	 	 s.baan.project = tcyesno.no
 	 	 endif
	 selectempty


	 	 s.baan.project = tcyesno.yes
 	 endselect
	 
 	 return ( s.baan.project )
 }

function string make.business.obj.ref(
		domain	tcpono		i.position,
|		domain	tcsrnb		i.sequence)		|#236822.so
		domain	tcsrnb		i.sequence,		|#236822.sn
		domain	tcmcs.str9	i.rcno,	
		domain	tcpono		i.rseq)			|#236822.en
{
	string		bo.reference(40) 
	
	i.sequence =  get.order.line.sequence(i.sequence)
	
								|#236822.so
|	bo.reference = str$(i.position) & chr$(47) & str$(i.sequence)
								|#236822.eo
								|#236822.sn
	 bo.reference = str$(i.position) &  chr$(47) &   
 	 	 	str$(i.sequence) &  chr$(47) &   
 	 	 	shiftl$(strip$(i.rcno)) &  chr$(47) &   
 	 	 	str$(i.rseq) &  chr$(47) &   
  	 	 	str$(0) &  chr$(47) &   
 	 	 	str$(0) &  chr$(47) &   
 	 	 	str$(0) 	 	 	 	 	  
								|#236822.en
	return(bo.reference)
}

 function domain bccadr get.bp.address (
 		domain	tcncmp		i.logistic.company,
 		domain	tcorno		i.order,
		domain	tcsuno		i.supplier,
		domain	tcccor		i.postal.address)
 {
 	| * Expl. : Use the function to convert purchase order  
 	| * 	 ship - from BP address.  
 	| * Pre 	 : tdpur040 fields current  
 	| * Return : Converted BP address
		domain		tcccor		post.address.supplier
 	 	 domain 	 bcstc.keys 	  l.key fixed
 	 static  domain 	 tcorno 	  s.order
 	 static  domain 	 bccadr 	  s.address
 	| * Initialization.  
 	 l.key = ""
 	| * To prevent determining the addres multiple times for 1  
 	| * order.  

 	 if s.order = i.order then
 	 	 return ( s.address )
 	 else
 	 	 s.order = i.order
 	 	 s.address = ""
 	 endif
	 
 	| * 1. Get postal address from purchase order header.  
 	 if not isspace ( i.postal.address ) then
 	 	 l.key ( 1;6 ) = i.supplier
 	 	 l.key ( 7;3 ) = i.postal.address
 	 	 s.address = get.address.code (
 	 	 	 	 "tccom022",
 	 	 	 	 l.key,
 	 	 	 	 error.message ) 	| * ref  
 	 	 return ( s.address )
 	 endif
	 
 	| * 2. Get specific postal address.  
 	 select	 tdpur042._index1
 	 from 	 tdpur042
 	 where 	 tdpur042._index1 = { 	 :i.order,
 	 	 	 	 	 0, 	| * Position number  
 	 	 	 	 	 1} 	| * Type : postal address 
	 and	tdpur042._compnr = :i.logistic.company				 
 	 as set with 1 rows
 	 selectdo
 	 	 l.key ( 1;6 ) = edit$ ( tdpur042.orno, "999999" )
 	 	 l.key ( 7;4 ) = edit$ ( tdpur042.pono, "9999" )
 	 	 l.key ( 11;2 ) = edit$ ( tdpur042.ckor, "99" )
 	 	 s.address = get.address.code (
 	 	 	 	 "tdpur042",
 	 	 	 	 l.key,
 	 	 	 	 error.message ) 	| * ref  
 	 	 return ( s.address )
 	 endselect
	 
 	| * 3. Get postal address from supplier.  
 	 read.supplier (i.supplier, post.address.supplier)
 	 if not isspace ( post.address.supplier ) then
 	 	 l.key ( 1;6 ) = i.supplier
 	 	 l.key ( 7;3 ) = post.address.supplier
 	 	 s.address = get.address.code (
 	 	 	 	 "tccom022",
 	 	 	 	 l.key,
 	 	 	 	 error.message )
 	 	 return ( s.address )
 	 endif
	 


 	| * 4. Get address from supplier.  
 	 s.address = get.address.code (
 	 	 	 "tccom020",
 	 	 	 i.supplier,
 	 	 	 error.message )
 	 return ( s.address )
 }

 function read.supplier (
 		domain	tcsuno		i.supplier,
	ref	domain	tcccor		o.post.address.supplier)
 {
 	 static domain 	 tcsuno 	  s.supplier
	 static	domain	tcccor		s.post.address.supplier
	 
 	 if s.supplier = i.supplier then
		 o.post.address.supplier = s.post.address.supplier 
 	 	 return
 	 else
 	 	 s.supplier = i.supplier
 	 endif
 	 select  tccom020.ccor:s.post.address.supplier
 	 from 	 tccom020
 	 where 	 tccom020._index1 = { :i.supplier}
 	 as set with 1 rows
 	 selectdo
 	 endselect

	 o.post.address.supplier = s.post.address.supplier
 }

function domain bccadr get.delivery.address ()
 {
 	| * Expl. : Use the function to convert purchase order line  
 	| * 	 delivery address.  
 	| * Pre 	 : tdpur041 present  
 	| * Return : Converted delivery address  
 	 	 	domain 	 	bcstc.keys 	 l.key fixed


 	 static 	 domain 	 tcorno 	  s.order

 	 static 	 domain 	 tcpono 	  s.position
 	 static 	 domain 	 bccadr 	  s.address
 	| * To prevent determining the addres multiple times for 1  
 	| * order.  
	
 	 if s.order = tdpur041.orno and
 	 s.position = tdpur041.pono then
 	 	 return ( s.address )
 	 else
 	 	 s.order = tdpur041.orno
 	 	 s.position = tdpur041.pono
 	 	 s.address = ""
 	 endif
 	| * 1. Get specific delivery address.  
 	 select  tdpur042._index1
 	 from 	 tdpur042
 	 where 	 tdpur042._index1 = { 	 :tdpur041.orno,
 	 	 	 	 	 :tdpur041.pono,
 	 	 	 	 	 0} 	| * Type : delivery address  
	 and	tdpur042._compnr = :logistic.company				 
 	 as set with 1 rows
 	 selectdo
 	 	 l.key ( 1;6 ) = edit$ ( tdpur042.orno, "999999" )
 	 	 l.key ( 7;4 ) = edit$ ( tdpur042.pono, "9999" )
 	 	 l.key ( 11;2 ) = edit$ ( tdpur042.ckor, "99" )
 	 	 s.address = get.address.code (
 	 	 	 	 "tdpur042",
 	 	 	 	 l.key,
 	 	 	 	 error.message )

 	 	 return ( s.address )
 	 endselect
 	| * 2. Get delivery address from warehouse.  
 	 if not isspace ( tdpur041.cwar ) then
 	 	 s.address = get.address.code (

 	 	 	 	 "tcmcs003",
 	 	 	 	 tdpur041.cwar,
 	 	 	 	 error.message )
 	 	 return ( s.address )
 	 endif
	 
 	 return ( s.address )
 }

								
function get.order.amount(
 	 	 ref 	 domain  tcamnt 	 o.order.amount)
 {
 	 	 domain 	 tcamnt 	 l.dummy.amount
		 
 	if tdpur045.srnb < 2 then
 	 	 o.order.amount = tdpur041.amta
	else	 
	|* Backorder	
	 	 if tdpur045.pric = 0 or
 		 	prev.bqua = 0 then
 	 	 	o.order.amount = 0.0
		else 
 	 		if tdpur045.date <> 0 then
 	 		 	tdpur.dll0022.calc.net.amount (
 	 		 		tdpur041.cvqp,
 	 		 		tdpur041.cvpp,
 	 		 		prev.bqua,
 	 		 		tdpur045.pric,
 	 		 		tdpur040.ccur,
 	 	 	 		tcmcs002.crnd,
 	 	 	 		tdpur041.dmth,
 	 	 	 		tdpur045.disc,
 	 	 	 		tdpur045.damt,
 	 	 	 		o.order.amount, 	| * ref  
 	 	 	 		l.dummy.amount ) 	| * ref  
			else		 
 		| * When no receipt is done yet, the order line discount  
 		| * amount of a backorder line must be calculated out of the  
 		| * backorder quantity of the previous receipt line and the  
 		| * price and discounts of the current line.  
 	 			tdpur.dll0022.calc.net.amount (
 	 		 	tdpur041.cvqp,
 	 		 	tdpur041.cvpp,
 	 		 	prev.bqua,
 	 		 	tdpur045.pric,
 	 		 	tdpur040.ccur,
 	 		 	tcmcs002.crnd,
 	 		 	tdpur041.dmth,
 	 		 	tdpur045.disc,
 	 		 	tdpur045.damt,
 	 		 	o.order.amount, 	| * ref  
 	 		 	l.dummy.amount) 	| * ref
			endif	
		endif	
	endif
	
	if not item.type = tckitm.cost or
 	       item.type = tckitm.service then
  	 	o.order.amount = imcs0010.amnt.min.odis (
  	 	 	 	 	 o.order.amount,
  	 	 	 	 	 tdpur040.odis,
  	 	 	 	 	 tdpur040.ccur,
  	 	 	 	 	 tcmcs002.crnd )
 	endif	
}
								
function  get.order.receipt.amount(
			ref 	 domain  tcamnt 	 o.order.amount)
{
		domain	tcamnt		l.dummy.amount

	get.amounts(	o.order.amount,	|* ref
			l.dummy.amount,	|* ref
			l.dummy.amount)	|* ref

}

function get.amounts(					
		ref	domain	tcamnt		o.order.amount,
		ref	domain	tcamnt		o.line.disc.amount,
		ref	domain	tcamnt		o.order.disc.amount)
{
	static	domain	tcorno		s.order.number
	static	domain	tcpono		s.position
	static	domain	tcpono		s.sequence
	static	domain	tcpono		s.ilc.sequence
	static	domain	tcamnt		s.order.amount
	static	domain	tcamnt		s.line.disc.amount
	static	domain	tcamnt		s.order.disc.amount
		domain	tcamnt		gross.order.amount
		domain	tcamnt		order.amount
		domain	tcamnt		line.disc.amount
		domain	tcamnt		order.disc.amount

	o.order.amount = 0.0
	o.line.disc.amount = 0.0
	o.order.disc.amount = 0.0

	if s.order.number = tdpur045.orno and
	   s.position = tdpur045.pono and
	   s.sequence = tdpur045.srnb and
	   s.ilc.sequence = bcswh010.srni then
	   	o.order.amount = s.order.amount 
		o.line.disc.amount = s.line.disc.amount
		o.order.disc.amount = s.order.disc.amount
		return
	endif

	s.order.number = tdpur045.orno
	s.position = tdpur045.pono
	s.sequence = tdpur045.srnb
	s.ilc.sequence = bcswh010.srni


	if logistic.company <> current.company then		|#1645612.sn
		compnr.check(logistic.company)			
	endif							|#1645612.en
	bcint.dll0000.get.order.line.amounts(	
		tdpur045.orno,
		tdpur045.pono,
		tdpur045.srnb,
		false,			|* total line
		|* Below: input from tdpur040
		tdpur040.ccur,	
		tdpur040.odis, 
		tcmcs002.crnd, 
		|* Below: input from tdpur041
		item.type,
		tdpur041.bqua,	
		tdpur041.cvqp,
		tdpur041.cvpp,
		tdpur041.amta,
		tdpur041.dmth, 
		|* Below: input from tdpur045
		tdpur045.date,
		tdpur045.pric,
		tdpur045.disc,
		tdpur045.damt,
		tdpur045.ldam,
		tdpur045.odam,
		g.prev.amld,
		g.prev.amod,
		g.prev.bqua,
		gross.order.amount,	|* ref
		order.amount,	|* ref
		line.disc.amount,	|* ref
		order.disc.amount)	|* ref

	if not g.use.mapping.table then
		|* we're done: calculated amounts are on tdpur045
		|* level and in the current situation, we create only 
		|* a single tdpur401 level (total lines excluded) for
		|* each tdpur045 record
		o.order.amount = order.amount
		o.line.disc.amount = line.disc.amount
		o.order.disc.amount = order.disc.amount
	   	s.order.amount = o.order.amount 
		s.line.disc.amount = o.line.disc.amount
		s.order.disc.amount = o.order.disc.amount
		if logistic.company <> current.company then	|#1645612.sn
			compnr.check(current.company)		
		endif						|#1645612.en
		return
	endif

	|* Function is called on ilc-level; divide the calculated
	|* amounts over the advice lines.
	bcint.dll0000.divide.amounts.according.to.received.qty(
		tdpur045.orno,
		tdpur045.pono,
		tdpur045.srnb,
		bcswh010.srni,
		var.tfacp240.iuni,
		tdpur040.ccur,	
		tcmcs002.crnd, 
		order.amount,
		line.disc.amount,
		order.disc.amount,
		o.order.amount,		|* ref
		o.line.disc.amount,	|* ref
		o.order.disc.amount)	|* ref
					
	s.order.amount = o.order.amount 
	s.line.disc.amount = o.line.disc.amount
	s.order.disc.amount = o.order.disc.amount
	if logistic.company <> current.company then		|#1645612.sn
		compnr.check(current.company)			
	endif							|#1645612.en

}

function domain tcbool zero.receipt(
		domain	tcqrd1		i.delivered.quantity,
		domain	tcqrd1		i.approved.quantity,
		domain	tcqrd1		i.rejected.quantity,
		domain	tcqrd1		i.order.line.quantity)
{
								|#207117.sn
	 if double.cmp(i.order.line.quantity, 0.0, 0.00001) = -1 then
		 |* Return order
		 return(false)
	 endif			
	|**********************************************************************
	|* In BaanIV, in case of a return order, the deliverd quantity and the
	|* rejected quantity are always equal. So, a return order looks like an
	|* order of which all that is delivered is rejected (zero receipt). But
	|* a return order must not be handled as a normal order with zero
	|* receipt.
	|**********************************************************************
								|#207117.en

 	 if (double.cmp(  
 	 	 i.approved.quantity +  i.rejected.quantity,  
 	 	 i.delivered.quantity,  
 	 	 0.00001) =  0 and  
 	 | *  Aproved plus rejected equals delivered;  
 	 | *  all inspections are closed  
  	 	double.cmp(  
   	 		i.delivered.quantity,  
  	 		i.rejected.quantity,  
  	 		0.00001) =  0 and  
  	 | *  All delivered is rejected  
  	 	 double.cmp(i.order.line.quantity, 0.0, 0.00001) <>  0) then 
  	 | *  Not a cost item with quantity zero)  
	 	return(true)
	endif
	
	return(false)
}

								|#1422401.so
|function domain tcpono get.receipt.line()			|#149975.sn
|{
|		domain	tcpono		receipt.line
|		domain	tcrcno		max.receipt.number
|	static	domain	tcorno		s.order
|	static	domain	tcpono		s.position
|	static	domain	tcpono		s.sequence
||	static	domain	tcpono		s.array.receipt.line(1) based|#1019539.o
|	static		long		s.array.length
|			long		ret			|#228027.sn
|		domain	tcmcs.s200m	error.message		|#228027.en
|		
|	if tdpur045.date = 0 then
|		return(0)
|	endif
|
|	if tdpur045.reno = 0 then
|		return(0)
|	endif
|
|	if tdpur041.oqua < 0 then				|#228027.sn
|		if tdpur041.btsp = tcbtsp.bookto.comp then	
|			|* For return of components the receipt sequence
|			|* (shipment line) is 0 on the purchase order line.
|			return(0)
|		endif						
|		ret = get.shipment.number.and.line(
|			tckoor.act.pur,
|			tdpur045.orno,
|			tdpur045.pono,
|			get.order.line.sequence(tdpur045.srnb),
|			0,		|* Outbound Sequence	|#243681.n
|			0,
|			error.message)	|* ref
|		return(bcswh008.shln)
|	endif							|#228027.en
|
|	if s.array.length = 0 then
|		max.receipt.number =
|			lpow(10, icom0015.domain.length("tcrcno")) - 1
|		s.array.length = max.receipt.number
|		alloc.mem(s.array.receipt.line, s.array.length)
|	endif
|
|	if s.order = tdpur045.orno and
|	   s.position = tdpur045.pono and
|	   s.sequence = tdpur045.srnb then
|		receipt.line = s.array.receipt.line(tdpur045.reno)
|	else
|		|* Default step size is 10 (same as in WHINH and TDPUR).
|		receipt.line = s.array.receipt.line(tdpur045.reno) + 10
|		s.array.receipt.line(tdpur045.reno) = receipt.line
|		s.order = tdpur045.orno
|		s.position = tdpur045.pono
|		s.sequence = tdpur045.srnb
|	endif
|
|	return(receipt.line)
|}								|#149975.en
								|#1422401.eo

								|#1422401.sn
function domain tcpono get.receipt.line()		
{
		domain	tcpono		receipt.line
			long		ret			
		domain	tcmcs.s200m	error.message		
		
	receipt.line = 0 
	if tdpur045.date = 0 then
		return(receipt.line)
	endif
								|#fp10.so
|	if tdpur045.reno = 0 then
|		return(receipt.line)
|	endif
								|#fp10.eo
	
	if g.use.mapping.table then				|#FP10.sn
		return(bcswh010.rcln)
	else							|#fp10.en
		ret = get.receipt.rseq(
			tckoor.act.pur,
			tdpur045.orno,
			tdpur045.pono,
			get.order.line.sequence(tdpur045.srnb),
			0,	
			0,
			receipt.line,
			error.message)	|* ref
	endif							|#FP10.en
	
	return(receipt.line)
}						
								|#1422401.en
								|#1422401.so
|function string get.receipt.number()				|#221043.sn
|{
|			long		ret			
|	static	domain	tcorno		s.order
|	static	domain	tcpono		s.position
|	static	domain	tcpono		s.sequence
|	static	domain	tcrcno		s.last.receipt.number
|		domain	tcmcs.s200m	error.message		|#228027.n
|
|	if tdpur045.date = 0 then
|		return("")
|	endif
|
|	if tdpur045.reno = 0 then			
|								|#10110971.so
||		if s.order <> tdpur045.orno or
||		   s.position <> tdpur045.pono or
||		   s.sequence <> tdpur045.srnb then
||			s.last.receipt.number = s.last.receipt.number + 1
||			s.order = tdpur045.orno
||			s.position = tdpur045.pono
||			s.sequence = tdpur045.srnb
||		endif
||		return(	shiftl$(strip$(bcswh000.conv)) &
||			edit$(s.last.receipt.number, "999999"))
|								|#10110971.eo
|								|#10110971.sn
|		select  bcstf014.rcno
|		from 	bcstf014
|		where 	bcstf014._index1 = { 	 
|					:tdpur045.orno,
|		 	 	 	:tdpur045.pono,
|		 	 	 	:tdpur045.srnb}  
|		as set with 1 rows
|		selectdo
|			return ( bcstf014.rcno )
|		endselect
|								|#10110971.en
|								
|	endif					
|								
||	if double.cmp(tdpur041.oqua, 0.0, 0.00001) = 1 then	|#228027.so
||		return(convert.number(	bcckon.receipt.number,
||					tdpur045.reno))
||	else
||		return(bcswh008.shpm)		
||	endif							|#228027.eo
|	if tdpur041.oqua >= 0 then				|#228027.sn
|		return(convert.number(	bcckon.receipt.number,
|					tdpur045.reno))
|	else
|		ret = get.shipment.number.and.line(		
|			tckoor.act.pur,
|			tdpur045.orno,
|			tdpur045.pono,
|			get.order.line.sequence(tdpur045.srnb),
|			0,		|* Outbound Sequence	|#243681.n
|			0,
|			error.message)	|* ref
|		return(bcswh008.shpm)				
|	endif							|#228027.en								
|
|}								|#221043.en
								|#1422401.eo
								|#1422401.sn
function string get.receipt.number()				
{
	domain	bcorno		receipt.number
		long		ret			
	domain	tcmcs.s200m	error.message	
	
	receipt.number = ""
	
	if tdpur045.date = 0 then
		return(	receipt.number )
	endif

	if g.use.mapping.table then				|#FP10.sn
		return(bcswh010.rcno)
	else							|#fp10.en
		ret = get.receipt.rcno(		
				tckoor.act.pur,
				tdpur045.orno,
				tdpur045.pono,
				get.order.line.sequence(tdpur045.srnb),
				0,		|* Outbound Sequence	
				0,
				receipt.number, 
				error.message)	|* ref
	endif							|#fp10.n
			
	return(receipt.number)
}								
								|#1422401.en

								|#228053.sn
function domain tccwar get.warehouse()
{
	static	domain	tccprj		s.project
	static	domain	tccwar		s.warehouse
	static	domain	tccwar		s.new.warehouse fixed

	|* Other item types are not received in warehousing, so the
	|* warehouse must be empty. In BaanIV the warehouse can be
	|* filled.
	if item.type <> tckitm.purchase and
	   item.type <> tckitm.manufacture then
		return("")
	endif

	|* In BaanIV the warehouse can be filled, even when the
	|* ordered quantity is zero. In Baan 6 this is not possible.
	if tdpur041.oqua = 0 then
		return("")
	endif

	if s.project = tdpur045.cprj and
	   s.warehouse = tdpur045.cwar then
		return(s.new.warehouse)
	else
		s.project = tdpur045.cprj
		s.warehouse = tdpur045.cwar
		s.new.warehouse = ""
	endif

	|* For order lines linked to a TP project, the warehouse

	|* field is always filled in BaanIV. In Baan 6 the warehouse
	|* field can be empty or can be filled with a project
	|* warehouse. During the migration the warehouse field is
	|* always made empty for TP project related order lines.
	if not isspace(tdpur045.cprj) and
	   project.is.tp.project(tdpur045.cprj) then
		return(s.new.warehouse)
	endif

	s.new.warehouse = tdpur045.cwar

	return(s.new.warehouse)
}
								|#228053.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acp
:TFLNO:250
:FUNC:START
function handle.matched.invoice()								|#fp10.sn
{
		long			element
		static	domain 	tcorno		prev.orno
		static	domain	tcpono		prev.pono
		static	domain	tcsrnb		prev.srnb
		static	domain	tcsrnb		prev.srni	|#1693462.n


	if hold.orno  <>  tdpur046.orno then   
		read.purchase.order.and.order.type.data (   
			tdpur046.orno,   
			logistic.company)   
		var.tfacp250.orno  =  convert.number(   
			bcckon.act.pur,   
			tdpur046.orno)  
		hold.orno = tdpur046.orno
	endif   
	var.tfacp250.ifbp  =  get.business.partner.code(   
		tdpur046.suno,   
		bcstc.orgn.suno,   
		error.message)   
	get.inventory.unit(   	  
		logistic.company,   
		tdpur041.opol,   
		tdpur041.item,   
		tdpur041.cprj,   
		inventory.unit)   	  
	convert.quantity.from.order.unit.to.inventory.unit (   
		tdpur046.qana,   
		inventory.unit,    
		tdpur041.cvqp,   
		"",    
		"",    
		"tcqrd1",    
		var.converted.iqan)   
	
	var.tfacp250.iamt = tdpur046.amtb
	var.tfacp250.iqan = var.converted.iqan
	var.tfacp251.rcno  =  get.receipt.number()   
	var.tfacp251.rseq  =  get.receipt.line()   	   
	var.tfacp250.sqnb  =  get.order.line.sequence(  
					tdpur045.srnb)    
	if g.use.mapping.table then
		if logistic.company <> current.company then	|#1645612.sn
			compnr.check(logistic.company)			
		endif						|#1645612.en
		bcint.dll0000.divide.matched.amount.according.to.received.qty(	
				tdpur045.orno,
				tdpur045.pono,
				tdpur045.srnb,
				bcswh010.srni,
				inventory.unit,
				tdpur040.ccur,
				tcmcs002.crnd, 
				tdpur046.amtb,
				var.converted.iqan,
				var.tfacp251.iamt,
				var.tfacp251.iqan)
		if logistic.company <> current.company then	|#1645612.sn
			compnr.check(current.company)			
		endif						|#1645612.en
		for element = 1 to 3
 	  	 	if calculate.amount.in.home.currency(  
	 	  	 		 var.tfacp251.iamt,  
					 var.tfacp250.ccur,	
	 	  		 	 var.tfacp250.rate(element),  
					 var.tfacp250.ratf(element),	
	 	  		 	 element,  
	 	  		 	 var.tfacp251.amth(element),  
	 	  		 	 error.message) <>  0 then  
 	  		 	 var.tfacp251.amth(element) =  0.0  
 	  		 endif  			
		endfor
	else
		|    *  Only 1 receipt per order line.
		var.tfacp251.iamt = tdpur046.amtb
		var.tfacp251.iqan = var.converted.iqan
		for element = 1 to 3
			var.tfacp251.amth(element) = var.tfacp250.amth(element)
		endfor
	endif			
   
	if prev.orno <> tdpur046.orno or 
	   prev.pono <> tdpur046.pono or
	   prev.srnb <> tdpur046.srnb or
	   prev.srni <> tdpur046.srni then 			|#1693462.n
		dump 1 
	endif
	prev.orno = tdpur046.orno
	prev.pono = tdpur046.pono
	prev.srnb = tdpur046.srnb 
	prev.srni = tdpur046.srni 				|#1693462.n
	dump 2   
	dump 3   
}

function read.purchase.order.and.order.type.data(
		domain	tcorno		i.order,
		domain	tcncmp		i.logistic.company)
{
 	 select  tdpur040.cotp, tdpur040.suno, tdpur040.ccor,
 	 	 tdpur040.orno, tdpur040.ccur, tdpur040.odis,
		 tdpur040.odat, tdpur040.ratp, tdpur040.ratf,
		 tdpur040.ccon, 
		 tdpur040.cdec, tdpur040.cpay,			|#migration fp5.n
		 tcmcs042.reto, tcmcs042.coun,
		 tcmcs002.crnd
 	 from 	 tdpur040, tcmcs002, tcmcs042
 	 where 	 tdpur040._index1 = { :i.order}
	 and	 tdpur040._compnr = :i.logistic.company
 	 and 	 tcmcs002.ccur = tdpur040.ccur
	 and	 tcmcs042.cotp = tdpur040.cotp
 	 as set with 1 rows
 	 selectdo
 	 endselect
}



								|#fp10.en


function domain tcbool get.invoice.related.data()		|#217083.sn
{
|******************************************************************************
|* This functions reads additional information from the invoice. The rate
|* information is needed to calculate the home amount(s). If the invoice is not
|* present (because fully paid invoices where removed) we try to read the rate
|* information from the ledger account entry in tfgld106 (finalized
|* transactions). Because it is unknow if the object is run in a
|* 'multi-currency' we can not store the rate information of the invoice in
|* local variables (we don't if these variables must be declared as an array).
|******************************************************************************

		long			ret.long
		long			element

	|* Initialize
	var.tfacp250.ccur = ""
	ret.long = set.mem(var.tfacp250.rate, 0.0) +
		   set.mem(var.tfacp250.ratf, 1) + 
		   set.mem(var.tfacp250.amth, 0.0) 
	var.tfacp250.tapr = ""
	var.tfacp250.dapr = 0
	var.tfacp250.ratd = ""

	|* First try to read the open entries.
	select	tfacp200.ccur:var.tfacp250.ccur, tfacp200.rate,
		tfacp200.ratf, tfacp200.docd,
		tfacp200.tapr:var.tfacp250.tapr,
		tfacp200.dapr:var.tfacp250.dapr
	from	tfacp200
	where	tfacp200._index1 = {:tdpur046.ttyp,
				    :tdpur046.invn}
	and	tfacp200.line = 0
	and	tfacp200.tdoc = ""
	and	tfacp200._compnr = :current.company
	as set with 1 rows
	selectdo
		for element = 1 to 3
 	  	 	if calculate.amount.in.home.currency(  
 	  	 		 tdpur046.amtb,  
				 var.tfacp250.ccur,	
 	  		 	 tfacp200.rate,  
				 tfacp200.ratf,	
 	  		 	 element,  
 	  		 	 var.tfacp250.amth(element),  
 	  		 	 error.message) <>  0 then  
 	  		 	 var.tfacp250.amth(element) =  0.0  
 	  		 endif  			
 	  	 	var.tfacp250.rate(element) = fill.mc.field.double(  
 	  	 	 	tfacp200.rate,  
 	  	 	 	element,  
 	  	 	 	0.000001) 	  
 	  	 	var.tfacp250.ratf(element) =  fill.mc.field.long(  
 	  	 	 	tfacp200.ratf,  
 	  	 	 	element,  
 	  	 	 	1)
		endfor
		var.tfacp250.ratd =  utc(tfacp200.docd, 0)
		return(true) |* Information found
	selectempty
								|#227033.so
|		 if (tdpur045.matc =  tdpur.matc.complete) or  
|	 	    (tdpur045.date <>  0 and zero.receipt(  
								|#227033.eo
		 if (tdpur045.date <>  0 and zero.receipt(  	|#227033.n
				 	tdpur045.dqua,  
				 	tdpur045.quap,  
				 	tdpur045.quad,  
				 	tdpur041.oqua)) then  
			 | *  Zero receipt. In BaanIV there is only 1  
			 | *  receipt per orderline /  sequence number.  
			 | *  If thereis no receipt a back order with  
			 | *  a new sequence number is made.  
			return(false)
			|* Invoice is not found (fully paid invoices are removed)
			|* and the order line is totally matched, so migration
			|* of record can be skipped.	
		endif		
	endselect

	|* If the invoice is removed try to find the information within the
	|* finalized transactions. The approval document cannot be determined in
	|* that case. This seems not to be a problem because tfacp250.tapr/dapr
	|* are not used on target side.
	select	tfgld106.ccur:var.tfacp250.ccur, tfgld106.rate,
		tfgld106.fact, tfgld106.dcdt	
	from	tfgld106
	where	tfgld106._index1 = {:tdpur046.ttyp,
				    :tdpur046.invn}
	and	tfgld106.olin = 0
	and	tfgld106._compnr = :current.company
	as set with 1 rows
	selectdo
		for element = 1 to 3
 	  	 	if calculate.amount.in.home.currency(  
 	  	 		 tdpur046.amtb,  
				 var.tfacp250.ccur,	
 	  		 	 tfgld106.rate,  
				 tfgld106.fact,	
 	  		 	 element,  
 	  		 	 var.tfacp250.amth(element),  
 	  		 	 error.message) <>  0 then  
 	  		 	 var.tfacp250.amth(element) =  0.0  
 	  		 endif  			
 	  	 	var.tfacp250.rate(element) = fill.mc.field.double(  
 	  	 	 	tfgld106.rate,  
 	  	 	 	element,  
 	  	 	 	0.000001) 	  
 	  	 	var.tfacp250.ratf(element) =  fill.mc.field.long(  
 	  	 	 	tfgld106.fact,  
 	  	 	 	element,  
 	  	 	 	1)
		endfor
		var.tfacp250.ratd =  utc(tfgld106.dcdt, 0)
		return(true)
	endselect

	return(false)
}								|#217083.en

function domain tcbool zero.receipt(				|#217083.sn
		domain	tcqrd1		i.delivered.quantity,
		domain	tcqrd1		i.approved.quantity,
		domain	tcqrd1		i.rejected.quantity,
		domain	tcqrd1		i.order.line.quantity)
{
	 if double.cmp(i.order.line.quantity, 0.0, 0.00001) = -1 then
		 |* Return order
		 return(false)
	 endif			
	|**********************************************************************
	|* In BaanIV, in case of a return order, the deliverd quantity and the
	|* rejected quantity are always equal. So, a return order looks like an
	|* order of which all that is delivered is rejected (zero receipt). But
	|* a return order must not be handled as a normal order with zero
	|* receipt.
	|**********************************************************************
	
 	 if (double.cmp(  
 	 	 i.approved.quantity +  i.rejected.quantity,  
 	 	 i.delivered.quantity,  
 	 	 0.00001) =  0 and  
 	 | *  Aproved plus rejected equals delivered;  
 	 | *  all inspections are closed  
  	 	double.cmp(  
   	 		i.delivered.quantity,  
  	 		i.rejected.quantity,  
  	 		0.00001) =  0 and  
  	 | *  All delivered is rejected  
  	 	 double.cmp(i.order.line.quantity, 0.0, 0.00001) <>  0) then 
  	 | *  Not a cost item with quantity zero)  
	 	return(true)
	endif
	
	return(false)
}								|#217083.en

								|#fp10.so
|function read.purchase.order.and.order.type.data (
|		domain tcorno 	i.order,
|		domain	tcncmp	i.logistic.company)
|{
| 	 select  	 tcmcs042.reto
| 	 from 	 tdpur040, tcmcs042
| 	 where 	 tdpur040._index1 = { :i.order}
|	 and	 tdpur040._compnr = :i.logistic.company
|	 and	 tcmcs042.cotp = tdpur040.cotp
| 	 as set with 1 rows
| 	 selectdo
| 	 endselect
|}
								|#fp10.eo

								|#1422401.so
|function domain tcpono get.receipt.line()			|#149975.sn
|{
|			long		ret			|#215078.sn
|		domain	tcmcs.s200m	error.message		|#215078.en
|		domain	tcpono		receipt.line
|								|#239860.so
||		domain	tcrcno		max.receipt.number
||	static	domain	tcorno		s.order
||	static	domain	tcpono		s.position
||	static	domain	tcpono		s.sequence
||	static	domain	tcpono		s.array.receipt.line(1) based
||	static		long		s.array.length
|								|#239860.eo
|								|#247628.sn
|		domain	tcorno		order.number
|		domain	tcpono		order.position
|		domain	tcpono		order.sequence
|								|#247628.en
|			long		counter			|#239860.n
|
|	if tdpur045.date = 0 then
|		return(0)
|	endif
|
|	if tdpur045.reno = 0 then
|		return(0)
|	endif
|
|	if tdpur041.oqua < 0 then				|#215078.sn
|		if tdpur041.btsp = tcbtsp.bookto.comp then	|#228027.sn
|			|* For return of components the receipt sequence
|			|* (shipment line) is 0 on the purchase order line.
|			return(0)
|		endif						|#228027.en
|		ret = get.shipment.number.and.line(
|			tckoor.act.pur,
|			tdpur045.orno,
|			tdpur045.pono,
|			get.order.line.sequence(tdpur045.srnb),
|			0,		|* Outbound Sequence	|#243681.n
|			0,
|			error.message)	|* ref
|		return(bcswh008.shln)
|	endif							|#215078.en
|
|								|#239860.so
||	if s.array.length = 0 then
||		max.receipt.number =
||			lpow(10, icom0015.domain.length("tcrcno")) - 1
||		s.array.length = max.receipt.number
||		alloc.mem(s.array.receipt.line, s.array.length)
||	endif
||
||	if s.order = tdpur045.orno and
||	   s.position = tdpur045.pono and
||	   s.sequence = tdpur045.srnb then
||		receipt.line = s.array.receipt.line(tdpur045.reno)
||	else
||		|* Default step size is 10 (same as in WHINH and TDPUR).
||		receipt.line = s.array.receipt.line(tdpur045.reno) + 10
||		s.array.receipt.line(tdpur045.reno) = receipt.line
||		s.order = tdpur045.orno
||		s.position = tdpur045.pono
||		s.sequence = tdpur045.srnb
||	endif
|								|#239860.eo
|								|#239860.sn
|	counter = 0
|	receipt.line = 0
|								|#247628.so
||	 select  tdpur045.pono
|| 	 from 	 tdpur045
|| 	 where 	 tdpur045._index6 = { :tdpur045.reno,:tdpur045.orno}
|| 	 selectdo
|| 	 	counter = counter + 1
|| 	 	if tdpur045.pono = tdpur046.pono then
|| 	 		receipt.line = counter * 10
|| 	 		break
|| 	 	endif
|| 	 endselect
||								|#239860.en
|								|#247628.eo
|								|#1011516.so
||								|#247628.sn
||	 select  tdpur045.orno:order.number,
||	 	tdpur045.pono:order.position,
||	 	tdpur045.srnb:order.sequence
|| 	 from 	 tdpur045
|| 	 where 	 tdpur045._index6 = { :tdpur045.reno}
|| 	 order by tdpur045._index1
|| 	 selectdo
|| 	 	counter = counter + 1
|| 	 	if order.number = tdpur046.orno and
|| 	 	   order.position = tdpur046.pono then
|| 	 		receipt.line = counter * 10
|| 	 		break
|| 	 	endif
|| 	 endselect
||								|#247628.en
|								|#1011516.eo
|								|#1011516.sn
|	select	count(tdpur045.orno):counter
|	from	tdpur045
|	where	tdpur045._index6 = { :tdpur045.reno }
|	and	tdpur045._index1 <= { :tdpur046.orno, :tdpur046.pono}
|	and	tdpur045.date <> 0 				|#fp9.n
|	selectdo
|	endselect
|	receipt.line = counter * 10
|								|#1011516.en
|	return(receipt.line)
|}								|#149975.en
								|#1422401.eo

function domain tcpono get.order.line.sequence(			|#215078.sn
		domain	tcsrnb		i.sequence)
{
	if i.sequence = 0 then
		return(1)
	else
		return(i.sequence)
	endif
}								|#215078.en

function void get.inventory.unit(					|#208878.sn
		domain	tcncmp		i.logistic.company,
		domain	tcittb		i.item.category,
		domain	tcitem		i.item,
		domain	tccprj		i.project,
	ref	domain	tccuni		o.inventory.unit)
{
	|* Initialize
	o.inventory.unit = ""
	
	if i.item.category = tcittb.standard then
		get.inventory.unit.from.items(
			i.logistic.company,
			i.item,
			o.inventory.unit)
	else
		get.inventory.unit.from.customized.items(
			i.logistic.company,
			i.project,
			i.item,
			o.inventory.unit)
	endif	
}	
		
function void get.inventory.unit.from.items(
		domain	tcncmp		i.logistic.company,
		domain	tcitem		i.item,
	ref	domain	tccuni		o.inventory.unit)
{
	|* Initialize
	o.inventory.unit = ""
	
	select	tiitm001.cuni:o.inventory.unit
	from	tiitm001
	where	tiitm001._index1 = {:i.item}
	and	tiitm001._compnr = :i.logistic.company
	as set with 1 rows
	selectdo
	endselect
}	

function void get.inventory.unit.from.customized.items(
		domain	tcncmp		i.logistic.company,
		domain	tccprj		i.project,
		domain	tcitem		i.item,
	ref	domain	tccuni		o.inventory.unit)
{
	|* Initialize
	o.inventory.unit = ""
	
	select	tipcs021.cuni:o.inventory.unit
	from	tipcs021
	where	tipcs021._index1 = {:i.project, :i.item}
	and	tipcs021._compnr = :i.logistic.company
	as set with 1 rows
	selectdo
	endselect
}								|#208878.en


								|#1422401.so
|								|#249604.sn
|function string get.receipt.number()			
|{
|			long		ret			
|	static	domain	tcorno		s.order
|	static	domain	tcpono		s.position
|	static	domain	tcpono		s.sequence
|	static	domain	tcrcno		s.last.receipt.number
|		domain	tcmcs.s200m	error.message		
|		
|	if tdpur045.date = 0 then
|		return("")
|	endif
|
|	if tdpur045.reno = 0 then			
|								|#10110971.so
||		if s.order <> tdpur045.orno or
||		   s.position <> tdpur045.pono or
||		   s.sequence <> tdpur045.srnb then
||			s.last.receipt.number = s.last.receipt.number + 1
||			s.order = tdpur045.orno
||			s.position = tdpur045.pono
||			s.sequence = tdpur045.srnb
||		endif
||		return(	shiftl$(strip$(bcswh000.conv)) &
||			edit$(s.last.receipt.number, "999999"))
|								|#10110971.eo
|								|#10110971.sn
|		select  bcstf014.rcno
|		from 	bcstf014
|		where 	bcstf014._index1 = { 	 
|					:tdpur045.orno,
|		 	 	 	:tdpur045.pono,
|		 	 	 	:tdpur045.srnb}  
|		as set with 1 rows
|		selectdo
|			return ( bcstf014.rcno )
|		endselect
|								|#10110971.en
|	endif					
|								
|	if tdpur041.oqua >= 0 then				
|		return(convert.number(	bcckon.receipt.number,
|					tdpur045.reno))
|	else
|		ret = get.shipment.number.and.line(		
|			tckoor.act.pur,
|			tdpur045.orno,
|			tdpur045.pono,
|			get.order.line.sequence(tdpur045.srnb),
|			0,		|* Outbound Sequence	
|			0,
|			error.message)	|* ref
|		return(bcswh008.shpm)				
|	endif
|
|}								
|								|#249604.en
								|#1422401.eo
								
								|#1422401.sn
function string get.receipt.number()				
{
	domain	bcorno		receipt.number
		long		ret			
	domain	tcmcs.s200m	error.message	
	
	receipt.number = ""
	
	if tdpur045.date = 0 then
		return(	receipt.number )
	endif

	if g.use.mapping.table then				|#FP10.sn
		return(bcswh010.rcno)
	else							|#FP10.en
		ret = get.receipt.rcno(		
				tckoor.act.pur,
				tdpur045.orno,
				tdpur045.pono,
				get.order.line.sequence(tdpur045.srnb),
				0,		|* Outbound Sequence	
				0,
				receipt.number, 
				error.message)	|* ref
				
		return(receipt.number)
	endif							|#fp10.n
}


function domain tcpono get.receipt.line()		
{
		domain	tcpono		receipt.line
			long		ret			
		domain	tcmcs.s200m	error.message		
		
	receipt.line = 0 
	if tdpur045.date = 0 then
		return(receipt.line)
	endif
	if tdpur045.reno = 0 then
		return(receipt.line)
	endif
	
	if g.use.mapping.table then				|#FP10.sn
		return(bcswh010.rcln)
	else							|#FP10.en
		ret = get.receipt.rseq(
			tckoor.act.pur,
			tdpur045.orno,
			tdpur045.pono,
			get.order.line.sequence(tdpur045.srnb),
			0,	
			0,
			receipt.line,
			error.message)	|* ref
		
		return(receipt.line)
	endif							|#FP10.n
}	
								|#1422401.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acr
:TFLNO:005
:FUNC:START
function string get.parent.or.dependent(domain tfacr.tran i.tran)
{
	if tfacr005.tran = tfacr.tran.control or
	tfacr005.tran = tfacr.tran.advance or
	tfacr005.tran = tfacr.tran.unall.receipt or
	tfacr005.tran = tfacr.tran.anticipated or
	tfacr005.tran = tfacr.tran.real.curr.prof or
	tfacr005.tran = tfacr.tran.real.curr.loss or
	tfacr005.tran = tfacr.tran.not.real.curr.p or
	tfacr005.tran = tfacr.tran.not.real.curr.l or
	tfacr005.tran = tfacr.tran.int.invoice then
		acct.type = "independent"
	else
		acct.type = "dependent"
	endif
	return(acct.type)
}

function fill.child.dimensions.in.target(domain tfacr.tran i.tran)	 
{

	itfgld0038.dim.rcd(ttfacr005)
	itfgld0038.store.rcd.fields(ttfacr005)

	on case i.tran
		case tfacr.tran.doubtful:
		case tfacr.tran.discount:
		case tfacr.tran.surcharge:
		case tfacr.tran.payment.diff:
		case tfacr.tran.deductions:

		get.parent.leac.and.dimensions (parent.leac, 
					           parent.dim,
					           tfacr.tran.control)
		break
		case tfacr.tran.net.advance:
		case tfacr.tran.gross.advance:
		get.parent.leac.and.dimensions(parent.leac, 
					          parent.dim,
						tfacr.tran.advance)		
		break	
		default:
		break		
	endcase
	get.dim.options(parent.leac, parent.dim.opt)
	fill.child.dimensions.based.on.BUA()
				
	itfgld0038.restore.rcd.fields(ttfacr005)

}

function get.parent.leac.and.dimensions (ref domain tfgld.leac o.parent.leac, ref domain tfgld.dimx o.parent.dim(), domain tfacr.tran  i.tran)	
{
	select	tfacr005.*
	from tfacr005
	where tfacr005._index1 = {:i.ficu,:i.tran}
	as set with 1 rows
	selectdo
		o.parent.leac = tfacr005.leac
		o.parent.dim(1,1 )= tfacr005.dim1
		o.parent.dim(1,2 )= tfacr005.dim2
		o.parent.dim(1,3) = tfacr005.dim3
		o.parent.dim(1,4) = tfacr005.dim4
		o.parent.dim(1,5) = tfacr005.dim5
	endselect
}
function get.dim.options(domain tfgld.leac i.leac, ref domain tfgld.dopt o.dim.option())  
{
	select	tfgld008.*
	from	tfgld008
	where	tfgld008._index1= {:i.leac}
	as set with 1 rows
	selectdo
		o.dim.option(1) = tfgld008.dim1
		o.dim.option(2) = tfgld008.dim2
		o.dim.option(3)= tfgld008.dim3
		o.dim.option(4) = tfgld008.dim4
		o.dim.option(5) = tfgld008.dim5

	endselect
}

function get.child.dimensions.from.source()
{
	child.dim(1,1) = tfacr005.dim1
	child.dim(1,2)= tfacr005.dim2
	child.dim(1,3) = tfacr005.dim3
	child.dim(1,4) = tfacr005.dim4
	child.dim(1,5) = tfacr005.dim5

}

function fill.child.dimensions.based.on.BUA()
{
	long i
	
	for i = 1 to 5
		if parent.dim.opt(i) = tfgld.dopt.mandatory then 
				var.tfacr005.dim(1,i) = parent.dim(1,i)
		else
			if parent.dim.opt(i) = tfgld.dopt.optional then
				if  child.dim.opt(i) = tfgld.dopt.no then
					var.tfacr005.dim(1,i) = parent.dim(1,i)
				else
					var.tfacr005.dim(1,i)= child.dim(1,i)
				endif
			else
				var.tfacr005.dim(1,i) = child.dim(1,i)
			endif
		endif
	endfor
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acr
:TFLNO:200
:FUNC:START
|#200-101927.sn

function  initialize.variables()
{
				|#200-102115.sn
	long i
			
	for i = 1 to 3
		
		var.tfacr200.amth(i) =0.0
		var.tfacr200.vath(i) = 0.0
		var.tfacr200.svah(i) = 0.0
		var.tfacr200.laph(i) = 0.0
		var.tfacr200.dc1h(i) = 0.0
		var.tfacr200.dc2h(i) = 0.0
		var.tfacr200.dc3h(i) = 0.0
		var.tfacr200.padh(i) = 0.0
		var.tfacr200.balh(i) = 0.0
		var.tfacr200.bahc(i) = 0.0
		var.tfacr200.baca(i) = 0.0
		var.tfacr200.cdam(i) = 0.0
		var.tfacr200.tore(i) = 0.0
	endfor	

				|#200-102115.en

				|#200-102115.so
|	var.tfacr200.amth =0.0
|	var.tfacr200.vath = 0.0
|	var.tfacr200.svah = 0.0
|	var.tfacr200.laph = 0.0
|	var.tfacr200.dc1h = 0.0
|	var.tfacr200.dc2h = 0.0
|	var.tfacr200.dc3h = 0.0
|	var.tfacr200.padh = 0.0
|	var.tfacr200.balh = 0.0
|	var.tfacr200.bahc = 0.0
|	var.tfacr200.baca = 0.0
|	var.tfacr200.cdam = 0.0
|	var.tfacr200.tore = 0.0
				|#200-102115.eo



	var.tfacr200.amnt = 0.0
	var.tfacr200.amti = 0.0
	var.tfacr200.vata = 0.0
	var.tfacr200.vati = 0.0
	var.tfacr200.svam =0.0
	var.tfacr200.lapa = 0.0
	var.tfacr200.lapi = 0.0
	var.tfacr200.dc1a = 0.0
	var.tfacr200.dc1i = 0.0
	var.tfacr200.dc2a = 0.0
	var.tfacr200.dc2i = 0.0
	var.tfacr200.dc3a = 0.0
	var.tfacr200.dc3i = 0.0
	var.tfacr200.pada = 0.0
	var.tfacr200.padi = 0.0
	var.tfacr200.balc = 0.0
	var.tfacr200.bala = 0.0
	var.tfacr200.baco = 0.0
	var.tfacr200.basi = 0.0

}

function fill.positive.amounts.invoice()	
{
								|#200-102115.sn
	long i

							
	for i = 1 to 3 
								|#1057355.so
|		var.tfacr200.amth(i) =fill.mc.field.double(tfacr200.amth,i)
|		var.tfacr200.vath(i) =fill.mc.field.double(tfacr200.vath,i)
|		var.tfacr200.svah(i) =fill.mc.field.double(tfacr200.svah,i)
|		var.tfacr200.laph(i) =fill.mc.field.double(tfacr200.laph,i)
|		var.tfacr200.dc1h(i) =fill.mc.field.double(tfacr200.dc1h,i)
|		var.tfacr200.dc2h(i) =fill.mc.field.double(tfacr200.dc2h,i)
|		var.tfacr200.dc3h(i) =fill.mc.field.double(tfacr200.dc3h,i)
|		var.tfacr200.padh(i) =fill.mc.field.double(tfacr200.padh,i)
|		var.tfacr200.balh(i) =fill.mc.field.double(tfacr200.balh,i)
|		var.tfacr200.bahc(i) =fill.mc.field.double(tfacr200.bahc,i)
|		var.tfacr200.baca(i) =fill.mc.field.double(tfacr200.baca,i)
|		var.tfacr200.cdam(i) =fill.mc.field.double(tfacr200.cdam,i)
|		var.tfacr200.tore(i) =fill.mc.field.double(tfacr200.tore,i)
								|#1057355.eo
								|#1057355.sn
		var.tfacr200.amth(i) =fill.mc.field.double(tfacr200.amth,i,0.0)
		var.tfacr200.vath(i) =fill.mc.field.double(tfacr200.vath,i,0.0)
		var.tfacr200.svah(i) =fill.mc.field.double(tfacr200.svah,i,0.0)
		var.tfacr200.laph(i) =fill.mc.field.double(tfacr200.laph,i,0.0)
		var.tfacr200.dc1h(i) =fill.mc.field.double(tfacr200.dc1h,i,0.0)
		var.tfacr200.dc2h(i) =fill.mc.field.double(tfacr200.dc2h,i,0.0)
		var.tfacr200.dc3h(i) =fill.mc.field.double(tfacr200.dc3h,i,0.0)
		var.tfacr200.padh(i) =fill.mc.field.double(tfacr200.padh,i,0.0)
		var.tfacr200.balh(i) =fill.mc.field.double(tfacr200.balh,i,0.0)
		var.tfacr200.bahc(i) =fill.mc.field.double(tfacr200.bahc,i,0.0)
		var.tfacr200.baca(i) =fill.mc.field.double(tfacr200.baca,i,0.0)
		var.tfacr200.cdam(i) =fill.mc.field.double(tfacr200.cdam,i,0.0)
		var.tfacr200.tore(i) =fill.mc.field.double(tfacr200.tore,i,0.0)
								|#1057355.en

	endfor
							|#200-102115.en	

								|#200-102115.so
|		var.tfacr200.amth =tfacr200.amth
|		var.tfacr200.vath =tfacr200.vath
|		var.tfacr200.svah =tfacr200.svah
|		var.tfacr200.laph =tfacr200.laph
|		var.tfacr200.dc1h =tfacr200.dc1h
|		var.tfacr200.dc2h =tfacr200.dc2h
|		var.tfacr200.dc3h =tfacr200.dc3h
|		var.tfacr200.padh =tfacr200.padh
|		var.tfacr200.balh =tfacr200.balh
|		var.tfacr200.bahc =tfacr200.bahc
|		var.tfacr200.baca =tfacr200.baca
|		var.tfacr200.cdam =tfacr200.cdam
|		var.tfacr200.tore =tfacr200.tore
								|#200-102115.eo
		
	
	var.tfacr200.amnt = tfacr200.amnt
	var.tfacr200.amti = tfacr200.amti
	var.tfacr200.vata = tfacr200.vata
	var.tfacr200.vati = tfacr200.vati

	var.tfacr200.svam =tfacr200.svam

	var.tfacr200.lapa = tfacr200.lapa
	var.tfacr200.lapi = tfacr200.lapi

	var.tfacr200.dc1a = tfacr200.dc1a
	var.tfacr200.dc1i = tfacr200.dc1i

	var.tfacr200.dc2a = tfacr200.dc2a
	var.tfacr200.dc2i = tfacr200.dc2i

	var.tfacr200.dc3a = tfacr200.dc3a
	var.tfacr200.dc3i = tfacr200.dc3i

	var.tfacr200.pada = tfacr200.pada
	var.tfacr200.padi = tfacr200.padi

	var.tfacr200.balc = tfacr200.balc
	var.tfacr200.bala = tfacr200.bala

	var.tfacr200.baco = tfacr200.baco

	var.tfacr200.basi = tfacr200.basi

}

|function fill.negative.amounts.credit.note()
|{
|								|#200-102115.sn
|	long i
|
|							
|	for i = 1 to 3 
|		var.tfacr200.amth(i) =fill.mc.field.double(-tfacr200.amth,i)
|		var.tfacr200.vath(i) =fill.mc.field.double(-tfacr200.vath,i)
|		var.tfacr200.svah(i) =fill.mc.field.double(-tfacr200.svah,i)
|		var.tfacr200.laph(i) =fill.mc.field.double(-tfacr200.laph,i)
|		var.tfacr200.dc1h(i) =fill.mc.field.double(-tfacr200.dc1h,i)
|		var.tfacr200.dc2h(i) =fill.mc.field.double(-tfacr200.dc2h,i)
|		var.tfacr200.dc3h(i) =fill.mc.field.double(-tfacr200.dc3h,i)
|		var.tfacr200.padh(i) =fill.mc.field.double(-tfacr200.padh,i)
|		var.tfacr200.balh(i) =fill.mc.field.double(-tfacr200.balh,i)
|		var.tfacr200.bahc(i) =fill.mc.field.double(-tfacr200.bahc,i)
|		var.tfacr200.baca(i) =fill.mc.field.double(-tfacr200.baca,i)
|		var.tfacr200.cdam(i) =fill.mc.field.double(-tfacr200.cdam,i)
|		var.tfacr200.tore(i) =fill.mc.field.double(-tfacr200.tore,i)
|
|	endfor
|								|#200-102115.en	
|
|
|								|#200-102115.so
||		var.tfacr200.amth =-tfacr200.amth
||		var.tfacr200.vath =-tfacr200.vath
||		var.tfacr200.svah =-tfacr200.svah
||		var.tfacr200.laph =-tfacr200.laph
||		var.tfacr200.dc1h =-tfacr200.dc1h
||		var.tfacr200.dc2h =-tfacr200.dc2h
||		var.tfacr200.dc3h =-tfacr200.dc3h
||		var.tfacr200.padh =-tfacr200.padh
||		var.tfacr200.balh =-tfacr200.balh
||		var.tfacr200.bahc =-tfacr200.bahc
||		var.tfacr200.baca =-tfacr200.baca
||		var.tfacr200.cdam =-tfacr200.cdam
||		var.tfacr200.tore =-tfacr200.tore
|								|#200-102115.eo
|
|
|	var.tfacr200.amnt = -tfacr200.amnt
|	var.tfacr200.amti = -tfacr200.amti
|	var.tfacr200.vata = -tfacr200.vata
|	var.tfacr200.vati = -tfacr200.vati
|	var.tfacr200.svam = -tfacr200.svam
|	
|	var.tfacr200.lapa = -tfacr200.lapa
|	var.tfacr200.lapi = -tfacr200.lapi
|	var.tfacr200.dc1a = -tfacr200.dc1a
|	var.tfacr200.dc1i = -tfacr200.dc1i
|	var.tfacr200.dc2a = -tfacr200.dc2a
|	var.tfacr200.dc2i = -tfacr200.dc2i
|	var.tfacr200.dc3a = -tfacr200.dc3a
|	var.tfacr200.dc3i = -tfacr200.dc3i
|	var.tfacr200.pada = -tfacr200.pada
|	var.tfacr200.padi = -tfacr200.padi
|	var.tfacr200.balc = -tfacr200.balc
|	var.tfacr200.bala = -tfacr200.bala
|	var.tfacr200.baco = -tfacr200.baco
|	var.tfacr200.basi = -tfacr200.basi
|
|
|}
							|#200-101927.en
									|#FP3.sn
function fill.tfacr200.pysh()
{
		on case tfacr200.trec
		case tfacr.trec.invoice:
			select	tfacr201.ttyp
			from	tfacr201
			where	tfacr201._index1 = {:tfacr200.ttyp, :tfacr200.ninv}
			as set with 1 rows
			selectdo	
			var.tfacr200.pysh = target(tcyesno.yes)
			selectempty					|#218981.sn
			var.tfacr200.pysh = target(tcyesno.no)		|#218981.en
			endselect
			break
		default:
			var.tfacr200.pysh = target(tcyesno.no)
		endcase
}
									|#FP3.en

function fill.tfacr200.leac()						|#FP3.sn
{
	select	tfgld106.leac,
		tfgld106.dim1,				|#1056135.sn
		tfgld106.dim2,	
		tfgld106.dim3,	
		tfgld106.dim4,	
		tfgld106.dim5				|#1056135.en
	from	tfgld106
								|#221097.so
|	where	tfgld106._index1 = {:tfacr200.ttyp, :tfacr200.ninv, :tfacr200.line}
								|#221097.eo
								|#221097.sn
	where	tfgld106._index1 = {:tfacr200.ttyp, :tfacr200.ninv}
								|#221097.en

	and 	tfgld106.cont = tcyesno.yes
	and 	(tfgld106.catg in
			   (tfgld.catg.sales.inv,
			    tfgld.catg.sales.cred) or
			  (tfgld106.catg = tfgld.catg.cash and
			   tfgld106.tcsh = tfcmg.tcsh.ssgn and
			   tfgld106.tran in
			   				|#221097.so
|					(tfcmg.tran.unalloc.rec,
|					 tfcmg.tran.advance.rec)))
				 			|#221097.eo
				 			|#221097.sn
				 (tfcmg.tran.unalloc.rec,
				 tfcmg.tran.advance.rec))
			and 	tfgld106.olin = :tfacr200.line)	
							|#221097.en
	as set with 1 rows
	selectdo
		var.tfacr200.leac = tfgld106.leac		
		var.dimension1 = tfgld106.dim1		|#1056135.sn
		var.dimension2 = tfgld106.dim2		
		var.dimension3 = tfgld106.dim3		
		var.dimension4 = tfgld106.dim4		
		var.dimension5 = tfgld106.dim5		|#1056135.sn
	endselect
	if (tfacr200.trec = tfacr.trec.invoice or
		    tfacr200.trec = tfacr.trec.credit) then
		itfgld0038.dim.rcd(ttfacr200)
		itfgld0038.store.rcd.fields(ttfacr200)
		select	tfacr200.ttyp, tfacr200.ninv,
			tfacr200.tdoc, tfacr200.docn
		from	tfacr200
		where	tfacr200._index1 = {:tfacr200.ttyp,
			 	            :tfacr200.ninv}
		and	tfacr200.trec = tfacr.trec.correction
		and	tfacr200.doub = tcyesno.no
		and 	not exists (select a_tfacr200.ttyp
				   from   tfacr200  a_tfacr200
				   where  a_tfacr200._index1 =
				   	  {tfacr200.ttyp,
				   	   tfacr200.ninv, 0,
				   	   tfacr200.tdoc,
				   	   tfacr200.docn}
				   and	   a_tfacr200.doub = tcyesno.yes)
		as set with 1 rows
		selectdo
				select	tfgld106.leac, tfgld106.olin,
					tfgld106.dim1,	|#1056135.sn
					tfgld106.dim2,	
					tfgld106.dim3,	
					tfgld106.dim4,	
					tfgld106.dim5	|#1056135.en
				from	tfgld106
				where	tfgld106._index1 =
						{:tfacr200.tdoc,
						 :tfacr200.docn}
				and	tfgld106.cont = tcyesno.yes
				and 	tfgld106.catg =
						tfgld.catg.sales.corr
				order by tfgld106.olin desc
				as set with 1 rows
				selectdo
					var.tfacr200.leac = tfgld106.leac	
					var.dimension1 = tfgld106.dim1		|#1056135.sn
					var.dimension2 = tfgld106.dim2		
					var.dimension3 = tfgld106.dim3		
					var.dimension4 = tfgld106.dim4		
					var.dimension5 = tfgld106.dim5		|#1056135.sn
				endselect
		endselect
		itfgld0038.restore.rcd.fields(ttfacr200)
	endif
				
}					|#FP3.en

								|#FP7.sn
								|#1025236.so
|function get.receipt.info(
|	ref domain	tcncmp		o.o.tfacr200.rcom,		
|	ref domain	tfgld.ttyp	o.o.tfacr200.rect,
|	ref domain	tfgld.docn	o.o.tfacr200.recd,
|	ref domain	tfgld.lino	o.o.tfacr200.recl)
|
|{
|	o.o.tfacr200.rcom = 0
|	o.o.tfacr200.rect = ""
|	o.o.tfacr200.recd = 0
|	o.o.tfacr200.recl = 0
|
|	select 	tfgld112.cono:o.o.tfacr200.rcom,
|		tfgld112.ttyp:o.o.tfacr200.rect,
|		tfgld112.docn:o.o.tfacr200.recd
|	from	tfgld112
|	where	tfgld112._index2 = {	:current.company,
|			    		:tfacr200.tdoc,
|			    		:tfacr200.docn}
|	as set with 1 rows
|	selectdo
|		o.o.tfacr200.recl = tfacr200.lino
|	selectempty
|		if not isspace(tfacr200.tdoc) then
|			o.o.tfacr200.rcom = current.company
|			o.o.tfacr200.rect = tfacr200.tdoc
|			o.o.tfacr200.recd = tfacr200.docn
|			o.o.tfacr200.recl = tfacr200.lino
|		endif
|	endselect
|}
								|#1025236.eo
								|#1025236.sn
function get.receipt.info(
	ref domain	tcncmp		o.tfacr200.rcom,		
	ref domain	tfgld.ttyp	o.tfacr200.rect,
	ref domain	tfgld.docn	o.tfacr200.recd,
	ref domain	tfgld.lino	o.tfacr200.recl)

{
	domain	tcncmp		l.icom
	domain	tfgld.ttyp	l.ttyp
	domain	tfgld.docn	l.docn
	domain	tfgld.ttyp	l.search.ttyp
	domain	tfgld.docn	l.search.ninv

	o.tfacr200.rcom = 0
	o.tfacr200.rect = ""
	o.tfacr200.recd = 0
	o.tfacr200.recl = 0

	if isspace(tfacr200.tdoc) then
		|* handle headers
		l.search.ttyp = ""
		l.search.ninv = 0			
		o.tfacr200.rcom = current.company
		if tfacr200.line > 0 then
			|* Header Document
			o.tfacr200.rect = tfacr200.ttyp
			o.tfacr200.recd = tfacr200.ninv
			o.tfacr200.recl = tfacr200.line	
		endif
		if tfacr200.line > 0 and
                   (tfacr200.trec = tfacr.trec.advance.ant or 
                   tfacr200.trec = tfacr.trec.unallocated.ant or 
		   tfacr200.trec = tfacr.trec.advance or
                   tfacr200.trec = tfacr.trec.unallocated ) then                        
                        l.search.ttyp = tfacr200.ttyp
			l.search.ninv = tfacr200.ninv
                endif
		
	else
		|*handle subdocuments		
		l.search.ttyp = tfacr200.tdoc
		l.search.ninv = tfacr200.docn
	endif
	
	|*handle possible intercompany situation
	if not isspace(l.search.ttyp) then		
		if invoice.is.finalized(
				      l.search.ttyp,
				      l.search.ninv) then
			select 	tfgld106.osrn
			from	tfgld106
			where	tfgld106._index1 = {:l.search.ttyp,
						:l.search.ninv}
			and	tfgld106.ocmp <> :current.company
			as set with 1 rows
			selectdo
				select 	tfgld112.cono:l.icom,
					tfgld112.ttyp:l.ttyp,
					tfgld112.docn:l.docn
				from	tfgld112
				where	tfgld112._index2 = {:current.company,
						    	:l.search.ttyp,
						    	:l.search.ninv}
				as set with 1 rows
				selectdo
					o.tfacr200.rcom = l.icom
					o.tfacr200.rect = l.ttyp
					o.tfacr200.recd = l.docn				
					o.tfacr200.recl = fill.recl.field(
								o.tfacr200.rcom,
								o.tfacr200.rect,
								o.tfacr200.recd)
				endselect
			selectempty
				|* no intercompany
				o.tfacr200.rcom = current.company
				o.tfacr200.rect = l.search.ttyp
				o.tfacr200.recd = l.search.ninv			
				o.tfacr200.recl = fill.recl.field(
								o.tfacr200.rcom,
								o.tfacr200.rect,
								o.tfacr200.recd)
			endselect 
		else
			select 	tfgld112.cono:l.icom,
				tfgld112.ttyp:l.ttyp,
				tfgld112.docn:l.docn
			from	tfgld112
			where	tfgld112._index2 = {:current.company,
					    	:l.search.ttyp,
					    	:l.search.ninv}
			as set with 1 rows
			selectdo
				o.tfacr200.rcom = l.icom
				o.tfacr200.rect = l.ttyp
				o.tfacr200.recd = l.docn				
				o.tfacr200.recl = fill.recl.field(
								o.tfacr200.rcom,
								o.tfacr200.rect,
								o.tfacr200.recd)
			selectempty
				|* no intercompany
				o.tfacr200.rcom = current.company
				o.tfacr200.rect = l.search.ttyp
				o.tfacr200.recd = l.search.ninv			
				o.tfacr200.recl = fill.recl.field(
								o.tfacr200.rcom,
								o.tfacr200.rect,
								o.tfacr200.recd)
			endselect
		endif
	endif
}
function domain	tfgld.lino fill.recl.field(
	 domain	tcncmp		i.tfacr200.rcom,		
	 domain	tfgld.ttyp	i.tfacr200.rect,
	 domain	tfgld.docn	i.tfacr200.recd)
{
	domain	tfgld.lino	line.number
	
	line.number = 0
	
	on case tfacr200.trec
	case tfacr.trec.assignment:
		line.number = get.line.mapped(tfacr200.lino)
		break
	case tfacr.trec.normal:
	case tfacr.trec.anticipated:
		if tfacr200.lino >= 10000 then
			line.number =
		   		tfcmg.dll8112.get.line.composed.anticipated(
					i.tfacr200.rcom,
					i.tfacr200.rect,
					i.tfacr200.recd,
					tfacr200.typa,
					tfacr200.doca)
		else
			line.number = tfacr200.lino
		endif
		break
	case tfacr.trec.advance.ant:
	case tfacr.trec.unallocated.ant:
	case tfacr.trec.advance:
	case tfacr.trec.unallocated:
		if tfacr200.line >= 10000 then
			line.number =
		   		tfcmg.dll8112.get.line.composed.anticipated(
					i.tfacr200.rcom,
					i.tfacr200.rect,
					i.tfacr200.recd,
					tfacr200.typa,
					tfacr200.doca)
		else
			line.number = tfacr200.line
		endif
		break
	default:
		line.number = tfacr200.lino
	endcase
	return(line.number)
}

function domain tfgld.lino get.line.mapped(
		domain	tfgld.lino	i.lino)
{
	domain	tfgld.lino	line.number

	|* Line number 10002 has to be mapped to 1 because the
	|* 10000 series belong to payment line 1. Line number
	|* 20001 has to be mapped to 2 because the 20000 series
	|* belong to payment line 2.
	line.number = i.lino \ 10000
	line.number = i.lino - line.number
	line.number = line.number / 10000

	return(line.number)
}

function domain tcbool invoice.is.finalized(
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document)
{
		domain	tfgld.trun	hold.run.number

	select	tfgld018.trun:hold.run.number
	from	tfgld018
	where	tfgld018._index1 = {:i.transaction.type,
				    :i.document}
	and	tfgld018.trun > 0
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function extern domain tfgld.lino tfcmg.dll8112.get.line.composed.anticipated(
			domain	tcncmp		i.finance.company,
			domain	tfgld.ttyp	i.transaction.type,
			domain	tfgld.docn	i.document.number,
			domain	tfgld.ttyp	i.transaction.type.ant.payment,
			domain	tfgld.docn	i.document.number.ant.payment)
{
	DllUsage
	Expl.	: This function determines the payment line number. The 
		  transaction type, document number and sequence number remain
		  default.

	Pre	: -
	Post	: -
	Input	: i.finance.company	- Finance company
		  i.transaction.type	- The transaction type
		  i.document.number	- The document number
		  i.transaction.type.ant.payment
		  			- The transaction type of the
					  anticipated payment
		  i.document.number.ant.payment	
		  			- The document number of
					  the anticipated payment
		  i.business.partner	- The business partner

	Return	: line number
	EndDllUsage

	domain	tfgld.lino	line.number

	select	tfcmg112.clin:line.number
	from	tfcmg112
	where	tfcmg112._index2 = {:i.transaction.type, :i.document.number}
	and	tfcmg112.tdoc = :i.transaction.type.ant.payment
	and	tfcmg112.pdoc = :i.document.number.ant.payment
	and	tfcmg112._compnr = :i.finance.company
	as set with 1 rows
	selectdo
		break
	selectempty
		line.number = 0
	endselect
	
	return(line.number)
}								
								|#1025236.en


function get.step()
{
	on case etol(tfacr200.step)
	
		case 0:
			var.tfacr200.step = 20
			break
		case 1: 
			var.tfacr200.step = 1
			break
		case 2:
			var.tfacr200.step =5
			break
		case 3:
			var.tfacr200.step =6
			break
		case 4:
			var.tfacr200.step =10
			break
		case 5:
			var.tfacr200.step =3
			break
		case 6:
			var.tfacr200.step =7
			break
		case 7:
			var.tfacr200.step =16
			break
		case 8:
			var.tfacr200.step =17
			break
	        default:
	                var.tfacr200.step = 20
	                break
	endcase
}
								|#FP7.en
function void get.customer.fin.business.partner.group(		|#Migr. 10.6.sn
		domain	tcncmp		i.company,
		domain	tccuno		i.customer	fixed,
	ref	domain	tfacr.ficu	o.fisc.bp.group)
{
	|**************************************************************
	|* Initialize variables.
	|**************************************************************
	o.fisc.bp.group = ""

	select	tccom010.cfcg:o.fisc.bp.group
	from	tccom010
	where	tccom010._compnr = :i.company
	and	tccom010._index1 = {:i.customer}
	as set with 1 rows
	selectdo
	endselect
}								|#Migr. 10.6.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:acr
:TFLNO:312
:FUNC:START
function void fill.default.agan()
{
|* This function fills the default aging analysis code from ACR parameters if present otherwise 
|* it fills the aging analysis code from the first record of tfacp004(Aging Analysis Data)	

	select	tfacr000.agan
	from	tfacr000
	as set with 1 rows
	selectdo
		if not isspace(tfacr000.agan) then
			var.tfacr312.agan = tfacr000.agan
		else
			select tfacp004.agan:var.tfacr312.agan
			from   tfacp004
			as set with 1 rows
			selectdo
				break
			endselect
		endif
	endselect  

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:cmg
:TFLNO:002
:FUNC:START
function fill.serv()
{
	var.tfcmg002.serv = tcyesno.no

	select  tfcmg118.*
	from    tfcmg118, tfcmg002
	where tfcmg118.rept = tfcmg.rept.service
	and     tfcmg118.reas = :tfcmg002.reas
	selectdo
	         var.tfcmg002.serv = tcyesno.yes
	endselect	

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:cmg
:TFLNO:101
:FUNC:START
function domain tcbool standing.is.advance()
{

	select	tfcmg104.*
	from	tfcmg104 
	where   tfcmg104._index1 = {:tfcmg101.suno,:tfcmg101.ninv}
	and	tfcmg104.post = tfcmg.post.advance
	order by tfcmg104._index1
	as set with 1 rows
	selectdo 
		return(true)
	endselect

	return(false)
}

				|FBU.sn
function fill.adty()
{
	select tfcmg295.adty:var.tfcmg101.adty
	from  tfcmg295, tfcmg101
	where tfcmg295._index1 = {:tfcmg101.btno,
				      :tfcmg101.suno,
				      :tfcmg101.comp,
				      :tfcmg101.tadv,
				      :tfcmg101.ttyp,
				      :tfcmg101.ninv,
				      :tfcmg101.srno}
	selectdo
	endselect

}

				|FBU.en

function string get.default.crdt()
{
		domain	tfgld.date	batch.created.on
	static	domain	tcncmp		s.company
	static	domain	tfgld.btno	s.payment.batch
	static	string			s.batch.created.on(22)

	if s.company = tfcmg101._compnr and
	   s.payment.batch = tfcmg101.btno then
	   	return(s.batch.created.on)
	endif

	select	tfcmg109.date:batch.created.on
	from	tfcmg109
	where	tfcmg109._index1 = {:tfcmg101.btno}
	and	tfcmg109._compnr = :tfcmg101._compnr
	as set with 1 rows
	selectdo
		s.batch.created.on = utc (batch.created.on,0)   
 	selectempty
 		s.batch.created.on = utc(date.num(),time.num())	
	endselect
	|**********************************************************************
	|* Store the static values
	|**********************************************************************
	s.company 	= tfcmg101._compnr
	s.payment.batch = tfcmg101.btno
	return(s.batch.created.on)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:cmg
:TFLNO:104
:FUNC:START
|FBU.sn
function fill.adty()
{
	select tfcmg297.adty:var.tfcmg104.adty
	from  tfcmg297, tfcmg104
	where tfcmg297._index1 = {:tfcmg104.suno,:tfcmg104.orno}
	selectdo
	endselect

}

				|FBU.en

function void get.supplier.fin.business.partner.group(		|#Migr. 10.6.sn
		domain	tcncmp		i.company,
		domain	tcsuno		i.supplier	fixed,
	ref	domain	tfacp.fisu	o.fisc.bp.group)
{
	|**************************************************************
	|* Initialize variables.
	|**************************************************************
	o.fisc.bp.group = ""

	select	tccom020.cfsg:o.fisc.bp.group
	from	tccom020
	where	tccom020._compnr = :i.company
	and	tccom020._index1 = {:i.supplier}
	as set with 1 rows
	selectdo
	endselect
}								|#Migr. 10.6.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:cmg
:TFLNO:111
:FUNC:START
function get.rate.date.and.rate.type.acp.record()
{
	 select 	 tfacp200.docd  
	 from 	 tfacp200  
	 where 	 tfacp200._index1 =  {:tfcmg111.ttyp, :tfcmg111.docn, :tfcmg111.srno,"",0,0}  
	 as set with 1 rows  
	 selectdo  
	 	 var.str.rdin =  utc (tfacp200.docd,0)  
	 	 var.str.rtin =  var.str.pur.rtyp  
	 selectempty  
	 	 var.str.rdin =  utc.default  
	 	 var.str.rtin =  var.str.pur.rtyp  
	 endselect  
}

function get.rate.date.and.rate.type.acr.record()
{
	 select 	 tfacr200.docd  
	 from 	 tfacr200  
	 where 	 tfacr200._index1 =  {:tfcmg111.ttyp, :tfcmg111.docn, :tfcmg111.srno,"",0,0}  
	 as set with 1 rows  
	 selectdo  
		 var.str.rdin =  utc (tfacr200.docd,0)  
	 	 var.str.rtin =  var.str.sls.rtyp  
	 selectempty  
	 	 var.str.rdin =  utc.default  
	 	 var.str.rtin =  var.str.sls.rtyp  
	 endselect  
}

					|FBU.sn
function fill.adty()
{
	select tfcmg296.adty:var.tfcmg111.adty
	from  tfcmg296, tfcmg111
	where tfcmg296._index1 = {:tfcmg111.bank,
				      :tfcmg111.tdoc,
				      :tfcmg111.pdoc,
				      :tfcmg111.cono,
				      :tfcmg111.tant,
				      :tfcmg111.cuno,
				      :tfcmg111.suno,
				      :tfcmg111.ttyp,
				      :tfcmg111.docn,
				      :tfcmg111.srno }
	selectdo
	endselect

}

				|FBU.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:cmg
:TFLNO:204
:FUNC:START
|FBU.sn
function fill.adty()
{
	select tfcmg294.adty:var.tfcmg204.adty
	from  tfcmg294, tfcmg204
	where tfcmg294._index1 = {:tfcmg204.year,
				      :tfcmg204.btno,
				      :tfcmg204.ttyp,
				      :tfcmg204.docn,
				      :tfcmg204.lino}
				      
	selectdo
	endselect

}

				|FBU.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:fam
:TFLNO:000
:FUNC:START
function get.data.from.tffas203()					|#213701.sn
{
	select	tffas203.*
	from	tffas203
	where	tffas203._index1 = {:tffas001.fdpc}
	and	tffas203.dpca = tcyesno.yes
	order by tffas203._index1 desc
	as set with 1 rows
	selectdo
		var.lng.tffam000.year = tffas203.year	 	
		var.lng.tffam000.prod = tffas203.prod
	endselect
}								|#213701.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:fam
:TFLNO:100
:FUNC:START
|#200-100959.sn
function get.pur.inv.no.from.tffas100()
{

  	select tffas100._index2
	from  tffas100
	where tffas100._index2= {:tffas007.fasc}
	order by tffas100._index2
	selectdo
	selecteos
		g.invn = tffas100.ttyp & str$(tffas100.docn) & str$(tffas100.line)		

	selectempty
		g.invn = empty.string(1;20)
	endselect

}
							|#200-100959.en
							|#200-100985.sn
function read.transfered.asset()
{
	select 	tffas103.*
	from	tffas103
	where	tffas103._index1 = {:tffas007.fasc}
	as set with 1 rows
	selectdo
	endselect
}							|#200-100985.en
function domain tcbool check.record.existin.tffas101.tffas103()	|#227065.sn
{
	select	tffas101.fasc					|#1106841.sn
	from	tffas101
	where	tffas101.fasc = :tffas007.fasc
	as set with 1 rows
	selectdo
		return(true)
	endselect

	select	tffas103.ofas
	from	tffas103
	where	(tffas103.ofas = :tffas007.fasc
	and	 tffas103.ofas <> tffas103.fasc)
	as set with 1 rows
	selectdo
		return(true)
	endselect						|#1106841.en

|	select	tffas101.fasc, tffas103.ofas|			|#1106841.so
|	from	tffas101, tffas103
|	where	tffas101.fasc= :tffas007.fasc or (tffas103.ofas=:tffas007.fasc and tffas103.ofas<> tffas103.fasc)
|	selectdo
|		return(true)
|	endselect						|#1106841.eo
	return(false)
}								|#227065.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:fst
:TFLNO:100
:FUNC:START
function domain tffst.stmt get.financial.statement.code(
		domain	bcfst.styp	i.type,
		domain	tffst.stmt	i.statement)
{
	 e =  get.financial.statement(i.type, i.statement, errmess, true)
 	 return(bcstf011.fstm)
}

function string get.fst.calculation.group()
{
	e = read.parameters.finance(errmess, true)
	return(bcstf000.cagr)
}

function domain tffst.stds get.description(
		domain	tffst.stds 	i.description,
		domain	tffst.stmt	i.statement)
{
		domain	tffst.stds	description
 	 description =  i.description
 	 if isspace(description) then  
 	 	 description =  i.statement  
 	 endif 
	return(description)
}

function domain tfgld.user get.user.modified(
		domain	tfgld.user	i.user.modified,
		domain	tfgld.user	i.user.created)
{
	if isspace(i.user.modified) then
		return(i.user.created)
	endif
	return(i.user.modified)
}

function domain tfgld.date get.last.modified.date(
		domain	tfgld.date	i.modified.date,
		domain	tfgld.date	i.creation.date)
{
	if i.modified.date <= 0 then
		return(i.creation.date)
	endif
	return(i.modified.date)
}

function domain tfgld.date get.last.modified.time(
		domain	tfgld.time	i.modified.time,
		domain	tfgld.time	i.creation.time)
{
	if i.modified.time <= 0 then
		return(i.creation.time)
	endif
	return(i.modified.time)
}

function long time.in.seconds(
		long	i.time)
{
			string		time(6)

	time = str$(i.time)
	return( (3600 * lval(time(1;2))) + (60 * lval(time(3;2))) + lval(time(5;2)))
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:gld
:TFLNO:008
:FUNC:START
function void read.dimensions.used(				|#210751.sn
	ref	domain	tcyesno		o.dimension1.used,
	ref	domain	tcyesno		o.dimension2.used,
	ref	domain	tcyesno		o.dimension3.used,
	ref	domain	tcyesno		o.dimension4.used,
	ref	domain	tcyesno		o.dimension5.used)
{
	|* Initialize
	o.dimension1.used = empty
	o.dimension2.used = empty
	o.dimension3.used = empty
	o.dimension4.used = empty
	o.dimension5.used = empty
	
	select	tfgld003.dim1:o.dimension1.used,
		tfgld003.dim2:o.dimension2.used, 
		tfgld003.dim3:o.dimension3.used, 
		tfgld003.dim4:o.dimension4.used, 
		tfgld003.dim5:o.dimension5.used 
	from	tfgld003
	as set with 1 rows
	selectdo
	endselect
}
								|#210751.en
								|#223230.sn
function domain	tcbool leac.in.ctcat110.and.primary.costs(
			domain	tfgld.leac	i.ledger.account)
{
	select	ctcat110.prse, ctcat110.leac
	from	ctcat110
	where	ctcat110.leac = :i.ledger.account
	and	ctcat110.prse = ctcat.prdc.primary
	selectdo
		return (true)
	endselect
	return (false)
}								|#223230.en

function void set.attributes.non.integration.account()
{
	var.tfgld008.alat = target(tfgld.atat.not.applicable)	|#223230.n
	|* Not Applicable
	
	if tfgld008.atyp =  tfgld.atyp.profitloss then 
|	 	 if tfgld008.subl >  0 then  			|#223230.so
|	 	 	 var.tfgld008.alat =  target(tfgld.atat.primary)
|			 |* Primary 
|	 	 else  
|	 	 	 if tfgld008.dbcr =  tfgld.dbcr.debit then  
|	 	 	 	 var.tfgld008.alat =
|				 	target(tfgld.atat.sec.debit) 
|				 	|* Secondary Debit
|	 	 	 else  
|	 	 	 	 var.tfgld008.alat = 
|				 	target(tfgld.atat.sec.credit) 
|				   	|* Secondary Credit
|	 	 	 endif  
|	 	 endif  
|	 else  
|	 	 var.tfgld008.alat =  target(tfgld.atat.not.applicable)
|		 	|* Not Applicable
|		 						|#223230.eo
								|#223230.sn
		if leac.in.ctcat110.and.primary.costs(tfgld008.leac) then
			var.tfgld008.alat = target(tfgld.atat.primary)
			|* Primary
		endif
		if tfgld008.dbcr = tfgld.dbcr.debit then
			select	ctcat400.tlea
			from	ctcat400
			where	ctcat400.tlea = :tfgld008.leac
			selectdo
				var.tfgld008.alat = target(tfgld.atat.sec.debit)
				|* Secondary Debit
			selectempty
				select	ctcat500.tlea
				from	ctcat500
				where	ctcat500.tlea = :tfgld008.leac
				selectdo
					var.tfgld008.alat = 
						target(tfgld.atat.sec.debit)
						|* Secondary Debit
				endselect
			endselect
		else
			select	ctcat400.slea
			from	ctcat400
			where	ctcat400.slea = :tfgld008.leac
			selectdo
				var.tfgld008.alat = 
					target(tfgld.atat.sec.credit)
				|* Secondary Credit
			selectempty
				select	ctcat500
				from	ctcat500
				where	ctcat500.slea = :tfgld008.leac
				selectdo
					var.tfgld008.alat = 
						target(tfgld.atat.sec.credit)
					|* Secondary Credit
				endselect
			endselect
		endif						|#223230.en
	 endif
  

|	 if tfgld008.iprj =  empty then  			|#1397945.so
|	  	 var.tfgld008.iprj =  target(tfgld.intr.not.applicable)	
|		 			|* Not Applicable 
|	 else  
|	 	if (etol (tfgld008.iprj) <  7) and (tfgld008.iprj <> empty) then
|		|* Transportation enums are expired	
|	 	 	 var.tfgld008.iprj =  etol(tfgld008.iprj)  
|	 	else  
|	 	 	 var.tfgld008.iprj =  target(tfgld.intr.not.applicable)	
|			 			|* Not Applicable
|	 	endif  
|	 endif  						|#1397945.eo
	 							|#1397945.sn
	on case	tfgld008.iprj
	case tfgld.intr.pcs:
		|#PCS Project
		var.tfgld008.iprj =  target(tfgld.intr.pcs)
	 	break
	case tfgld.intr.proj.costs:
		|#Project Costs
		var.tfgld008.iprj =  target(tfgld.intr.proj.costs)
	 	break
	case tfgld.intr.proj.sales:
		|# Project Sales
		var.tfgld008.iprj =  target(tfgld.intr.proj.sales)
	
	default:
		|# Transportation enums are expired -> mapping to not applicable
		|# ISM enums are not considered -> mapping to not applicable
	 	var.tfgld008.iprj =  target(tfgld.intr.not.applicable)
	 	break
	endcase	
	 							|#1397945.en

	if tfgld008.subl > 0 then
		set.value.all.dimensions(
			tfgld.dopt.no,
	 		var.tfgld008.dim1,
	 		var.tfgld008.dim2,
	 		var.tfgld008.dim3,
	 		var.tfgld008.dim4,
	 		var.tfgld008.dim5)
	else
								|#FP10.sn
		var.cvr.udim1  =  ""   	      	      	      	      	      	      
 	      	var.cvr.udim2  =  ""   	      	      	      	      	      	      
 	      	var.cvr.udim3  =  ""   	      	      	      	      	      	      
 	      	var.cvr.udim4  =  ""   	      	      	      	      	      	      
 	      	var.cvr.udim5  =  ""   
								|#FP10.en
		if account.is.interim.account(tfgld008.leac) or
		   account.is.control.account(tfgld008.leac) then
|			set.value.all.dimensions(		|#210751.so
|				tfgld.dopt.optional,
|	 			var.tfgld008.dim1,
|	 			var.tfgld008.dim2,
|	 			var.tfgld008.dim3,
|	 			var.tfgld008.dim4,
|				var.tfgld008.dim5)		|#210751.eo
			|* Add dimension ranges by ledger account (if needed)
			if dimension1.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim1 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					1,
					var.cvr.udim1)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim1 = tfgld.dopt.no 
			endif					|#210751.en
			if dimension2.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim2 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					2,
					var.cvr.udim2)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim2 = tfgld.dopt.no 
			endif					|#210751.en
			if dimension3.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim3 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					3,
					var.cvr.udim3)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim3 = tfgld.dopt.no 
			endif					|#210751.en
			if dimension4.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim4 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					4,
					var.cvr.udim4)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim4 = tfgld.dopt.no 
			endif					|#210751.en
			if dimension5.used = tcyesno.yes then	|#210751.sn
	 			var.tfgld008.dim5 = tfgld.dopt.optional 
								|#210751.en
				add.dimension.range.by.ledger.account(
					tfgld008.leac,
					5,
					var.cvr.udim5)		|#FP10.n
			else					|#210751.sn
	 			var.tfgld008.dim5 = tfgld.dopt.no 
			endif					|#210751.en

		else
			var.tfgld008.dim1 = tfgld008.dim1
			var.tfgld008.dim2 = tfgld008.dim2
			var.tfgld008.dim3 = tfgld008.dim3
			var.tfgld008.dim4 = tfgld008.dim4
			var.tfgld008.dim5 = tfgld008.dim5
		endif	

								|#FP10.sn
	      	dim.options(1)  =  var.tfgld008.dim1   
 	      	dim.options(2)  =  var.tfgld008.dim2   
 	      	dim.options(3)  =  var.tfgld008.dim3   
 	      	dim.options(4)  =  var.tfgld008.dim4   
 	      	dim.options(5)  =  var.tfgld008.dim5   
 	      	nof.elements  =  0   
 	      	set.mem(fdim.array, "")   
 	      	set.mem(tdim.array, "")   
 	      	convert.dimension.ranges.to.cvr.rule(   
			tfgld008.leac,   
			dim.options,   
			array.depth,   
			nof.elements,   
			fdim.array,   
			tdim.array)   
 	      	dmp.vcr.rule.elements.from.arrays(   
			tfgld008.leac,   
			dim.options,   
			var.cvr.udim1,   
			var.cvr.udim2,   
			var.cvr.udim3,   
			var.cvr.udim4,   
			var.cvr.udim5,   
			array.depth,   
			nof.elements,   
			fdim.array,   
			tdim.array)   
								|#FP10.en
	endif	
	 
	 if tfgld008.ifas =  empty then  
	  	 var.tfgld008.ifas =  target(tfgld.ifas.not.applicable)
		 			|* Not Applicable
	 else  
	  	 var.tfgld008.ifas =  etol(tfgld008.ifas)  
	 endif  

	if tfgld008.cuac = tcyesno.yes then			|#148329.sn
		 var.tfgld008.dmsq = get.default.matching.sequence(
		 			tfgld008.leac)
	else
		 var.tfgld008.dmsq = 0
	endif							|#148329.en

	if tfgld008.bloc = empty then
		var.tfgld008.bloc = tfgld.bloc.free
	else
		var.tfgld008.bloc = tfgld008.bloc
	endif

	if isspace(tfgld008.uni1)
	and (var.tfgld008.ifas = target(tfgld.ifas.disposal) or
	       var.tfgld008.ifas = target(tfgld.ifas.investment)) then
		|* Unit1 is defaulted with the 'Base Inventory Unit' (bcstc000.cuni).
		var.tfgld008.uni1 = bcstc000.cuni
	else
		var.tfgld008.uni1 = tfgld008.uni1
	endif
}

function void set.common.attributes()
{
	 if (tfgld008.leac =  tfgld004.clac) or 
	    (tfgld008.leac =  tfgld004.clcl) then  
	 	 var.tfgld008.icur =  tcicur.not.required  
	 else  
	 	 var.tfgld008.icur =  tfgld008.icur  
	 endif
 
	 if tfgld008.dbcr =  empty then  
	 	 var.tfgld008.dbcr =  tfgld.dbcr.debit 

	 else
		 var.tfgld008.dbcr = tfgld008.dbcr 
	 endif
	
	if tfgld008.atyp = empty then
|		var.tfgld008.atyp = tfgld.atyp.balance		|#fp10.o
		var.tfgld008.atyp = ltoe(target(tfgld.atyp.balance))|#fp10.n
	else
|		var.tfgld008.atyp = tfgld008.atyp		|#fp10.o
		var.tfgld008.atyp = convert.account.type(tfgld008.atyp)|#fp10.n
	endif
}

function domain tfgld.atyp get.account.type(domain tfgld.leac i.account)
{
	domain	tfgld.atyp	account.type

	select	tfgld008.atyp:account.type
	from	tfgld008
	where	tfgld008._index1 = {:i.account}
	as set with 1 rows
	selectdo
	endselect
	account.type = convert.account.type(account.type)	|#fp10.n

	return(account.type)
}

function void set.value.all.dimensions(
		domain	tfgld.dopt	i.dimension.value,
	ref	domain	tfgld.dopt	o.dimension.1,
	ref	domain	tfgld.dopt	o.dimension.2,
	ref	domain	tfgld.dopt	o.dimension.3,
	ref	domain	tfgld.dopt	o.dimension.4,
	ref	domain	tfgld.dopt	o.dimension.5)
{
	o.dimension.1 = i.dimension.value
	o.dimension.2 = i.dimension.value
	o.dimension.3 = i.dimension.value
	o.dimension.4 = i.dimension.value
	o.dimension.5 = i.dimension.value
}

function domain tcbool account.is.interim.account(
		domain	tfgld.leac	i.account)
{
		domain	tfgld.srno	dummy.srno

	select	tfgld004.srno:dummy.srno
	from	tfgld004
	where	tfgld004.srno = 1
	and	(tfgld004.fpor = :i.account or
		 tfgld004.fpnw = :i.account or
		 tfgld004.rpor = :i.account or
		 tfgld004.rpnw = :i.account )
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}	

function domain tcbool account.is.control.account(
		domain	tfgld.leac	i.account)
{
		domain	tfgld.leac	dummy.account

	select	tfacp005.leac:dummy.account
	from	tfacp005
	where	tfacp005.leac = :i.account
	selectdo
		return(true)
	endselect

	select	tfacr005.leac:dummy.account
	from	tfacr005
	where	tfacr005.leac = :i.account
	selectdo
		return(true)
	endselect

	return(false)
}

function void add.dimension.range.by.ledger.account(
		domain	tfgld.leac	i.account,
		domain	tfgld.srno	i.dimension,
	ref 	domain	tccvat		o.max.dim)		|#FP10.n
{
		domain	tfgld.leac	dummy.account

	select	tfgld009.leac:dummy.account
	from	tfgld009
	where	tfgld009._index1 = {:i.account, :i.dimension}
	as set with 1 rows
	selectdo
	selectempty
		var.tfgld009.leac = i.account
		var.tfgld009.dtyp = i.dimension
		var.tfgld009.fdim = ""
|		set.fmax(var.tfgld009.udim)			|#FP10.o
		o.max.dim = var.tfgld009.udim			|#FP10.n
		dump 5
	endselect
}

function domain tfgld.seri get.default.matching.sequence(	|#148329.sn
		domain	tfgld.leac	i.account)
{
		domain	tfgld.seri	sequence

	|* Initialize
	sequence = 0

	select	tfgld198.sera:sequence
	from	tfgld198
	where	tfgld198._index1 = {:i.account}
	and	tfgld198.stnd = tcyesno.yes
	as set with 1 rows
	selectdo
	endselect

	return(sequence)
}								|#148329.en

								|#FP10.sn
function convert.dimension.ranges.to.cvr.rule(
		domain	tfgld.leac	i.ledger.account,
	const	domain	tfgld.dopt	i.dim.options(),
	ref		long		io.array.depth,
	ref		long		io.nof.elements,
	ref	domain	tcmcs.str132	io.fdim.array(),
	ref	domain	tcmcs.str132	io.tdim.array())
{
		
	domain	tccvat	dimension.to
	
	dimension.to = ""

	select	tfgld009.*
	from	tfgld009
	where	tfgld009._index1 = {	:i.ledger.account}
	order by tfgld009._index1
	selectdo
		if tfgld009.udim = var.max.dimension(1;6) then
			dimension.to = var.tfgld009.udim
		else
			dimension.to = tfgld009.udim
		endif
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			tfgld009.dtyp,
			tfgld009.fdim,
			dimension.to,
			io.array.depth,		|* ref
			io.nof.elements,	|* ref
			io.fdim.array,		|* ref
			io.tdim.array)		|* ref
	endselect
	
}


function process.dimension.range.into.cvr(
		domain	tfgld.leac	i.ledger.account,
	const	domain	tfgld.dopt	i.dim.options(),
		domain	tfgld.dtyp	i.dim.type,
		domain	tccvat		i.dim.f,
		domain	tccvat		i.dim.t,
	ref		long		io.array.depth,
	ref		long		io.nof.elements,
	ref	domain	tcmcs.str132	io.fdim.array(),
	ref	domain	tcmcs.str132	io.tdim.array())
{
			long		i
			long		start.pos
		domain	tcbool		first.time
		domain	tcmcs.str132	fdim
		domain	tcmcs.str132	tdim
		domain	tccvat		prev.fdim
		domain	tccvat		prev.tdim
		domain	tccvat		current.fdim
		domain	tccvat		current.tdim
	static	domain	tfgld.leac	s.ledger.account
	static	domain	tfgld.dtyp	s.dim.type



	if i.ledger.account = s.ledger.account and
	   i.dim.type = s.dim.type then
		if i.dim.options(i.dim.type) = tfgld.dopt.no then
			return
		endif

		prev.fdim = ""
		prev.tdim = ""
		first.time = true

		|**************************************************************
		|* When the dimension type is the same as the previous dimenion
		|* type, then make a copy of the current set in tfgld052 and
		|* change only the fdim and tdim for this dimension.
		|* When this happens for the third time (or more), not a
		|* complete copy is required, but only the same set of records
		|* as the first time needs to be copied. This will avoid an
		|* exponential growth.
		|**************************************************************

		for i = 1 to io.nof.elements
			fdim = io.fdim.array(1, i)
			tdim = io.tdim.array(1, i)
			start.pos = 1 + ((i.dim.type - 1) * DIM.LENGTH)
			current.fdim = fdim(start.pos; DIM.LENGTH)
			current.tdim = tdim(start.pos; DIM.LENGTH)

			if not first.time and
			   (prev.fdim <> current.fdim or
			    prev.tdim <> current.tdim) then
				return
			endif
			first.time = false
			prev.fdim = current.fdim 
			prev.tdim = current.tdim 

			io.nof.elements = io.nof.elements + 1
			if io.nof.elements > io.array.depth then
				io.array.depth = io.array.depth + ALLOC.BLOCK
				alloc.mem(io.fdim.array, 132, io.array.depth)
				alloc.mem(io.tdim.array, 132, io.array.depth)
			endif
			fdim(start.pos; DIM.LENGTH) = i.dim.f
			tdim(start.pos; DIM.LENGTH) = i.dim.t
			io.fdim.array(1,io.nof.elements) = fdim
			io.tdim.array(1,io.nof.elements) = tdim
		endfor
		return
	endif

	s.dim.type = i.dim.type
	s.ledger.account = i.ledger.account

	|**********************************************************************
	|* If dimension type is not the same as the previous time, insert a
	|* new array element or update existing elements with the range of this
	|* dimension type.
	|**********************************************************************

	if io.nof.elements = 0 then
		io.nof.elements = io.nof.elements + 1
		if io.nof.elements > io.array.depth then
			io.array.depth = io.array.depth + ALLOC.BLOCK
			alloc.mem(io.fdim.array, 132, io.array.depth)
			alloc.mem(io.tdim.array, 132, io.array.depth)
		endif
		start.pos = 1 + ((i.dim.type - 1) * DIM.LENGTH)
		if i.dim.options(i.dim.type) <> tfgld.dopt.no then
			fdim = ""
			tdim = ""
			fdim(start.pos;DIM.LENGTH) = i.dim.f
			tdim(start.pos; DIM.LENGTH) = i.dim.t
			io.fdim.array(1,io.nof.elements) = fdim
			io.tdim.array(1,io.nof.elements) = tdim
		endif
	else
		for i = 1 to io.nof.elements
			if i.dim.options(i.dim.type) <> tfgld.dopt.no then
				fdim = io.fdim.array(1,i)
				tdim = io.tdim.array(1,i)
				start.pos = 1 + ((i.dim.type - 1) * DIM.LENGTH)
				fdim(start.pos;DIM.LENGTH) = i.dim.f
				tdim(start.pos; DIM.LENGTH) = i.dim.t
				io.fdim.array(1,i) = fdim
				io.tdim.array(1,i) = tdim
			endif
		endfor
	endif
}


function dmp.vcr.rule.elements.from.arrays(
		domain	tfgld.leac	i.ledger.account,
	const	domain	tfgld.dopt	i.dim.options(),
		domain	tccvat		i.var.cvr.udim1,
		domain	tccvat		i.var.cvr.udim2,
		domain	tccvat		i.var.cvr.udim3,
		domain	tccvat		i.var.cvr.udim4,
		domain	tccvat		i.var.cvr.udim5,
	ref		long		io.array.depth,
	ref		long		io.nof.elements,
	ref	domain	tcmcs.str132	io.fdim.array(),
	ref	domain	tcmcs.str132	io.tdim.array())

{
		long 		i,j,start.pos
	domain	tcmcs.str132	fdim
	domain	tcmcs.str132	tdim
	
	if not isspace(i.var.cvr.udim1) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			1,
			"",
			i.var.cvr.udim1,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if not isspace(i.var.cvr.udim2) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			2,
			"",
			i.var.cvr.udim2,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if not isspace(i.var.cvr.udim3) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			3,
			"",
			i.var.cvr.udim3,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if not isspace(i.var.cvr.udim4) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			4,
			"",
			i.var.cvr.udim4,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if not isspace(i.var.cvr.udim5) then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			5,
			"",
			i.var.cvr.udim5,
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif 
	if io.nof.elements = 0 then
		process.dimension.range.into.cvr(
			i.ledger.account,
			i.dim.options,
			1,
			"",
			"",
			io.array.depth,
			io.nof.elements,
			io.fdim.array,
			io.tdim.array)
	endif
	
	var.tfgld052.leac = i.ledger.account
	
	for i = 1 to io.nof.elements 
		var.max.validation.line = var.max.validation.line + 1
		fdim = io.fdim.array(1,i)
		tdim = io.tdim.array(1,i)
		for j = 1 to MAX.DIMENSIONS
			start.pos = 1 + ((j - 1) * DIM.LENGTH)
			var.from.dim(1,j) = fdim(start.pos;DIM.LENGTH)
			var.to.dim(1,j) = tdim(start.pos;DIM.LENGTH)
		endfor
		dump 8 |cross validation rule element
	endfor
}




function dummy.dumps()
{
	dump 1  |    *  Other, non integration accounts   
	dump 2  |    *  Integration Accounts 
	dump 3  |    *  Currency Difference Contra account is added.
	dump 4	|* Exchange Gain and Loss Account is added
	dump 5 |* New Dimension Ranges
	dump 6  |    *  Existing Dimension Ranges
	dump 7 |* Cross validation rule
	dump 8 |*cross validation rule element

}
								|#FP10.en
								|#fp10.sn
function domain tfgld.atyp convert.account.type( domain tfgld.atyp old.account.type)
{
	domain	tfgld.atyp var.008.atyp

	on case old.account.type   
	case tfgld.atyp.balance:   
		var.008.atyp  =  ltoe(target(tfgld.atyp.balance))   
		break   
	case tfgld.atyp.profitloss:   
		var.008.atyp  =  ltoe(target(tfgld.atyp.profitloss))   
		break   
	case tfgld.atyp.intercompany:   
		var.008.atyp  =  ltoe(target(tfgld.atyp.intercompany))   
		break   
	case tfgld.atyp.text:   
		var.008.atyp  =  ltoe(target(tfgld.atyp.text))   
		break   
	default:
		var.008.atyp  =  ltoe(target(tfgld.atyp.balance))   
		break      	   	
	endcase   
      	   
      	return(var.008.atyp)
}
								|#fp10.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:gld
:TFLNO:018
:FUNC:START
function void determine.exchange.rate.type(
		domain	tcmcs.str3	i.var.str.pur.rtyp,
		domain	tcmcs.str3	i.var.str.sls.rtyp,
		domain	tcmcs.str3	i.var.str.eu.rtyp,
		domain	tcncmp		i.company,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
		domain	tfgld.year	i.year,
		domain	tfgld.btno	i.batch,
		domain	tcmcs.long	i.run.number,
	ref	domain	tcmcs.str3	o.exchange.rate.type)
{
		domain	tfgld.catg	transaction.categorie
		
	o.exchange.rate.type = ""
	
 	determine.transaction.categorie(
		i.transaction.type,
		transaction.categorie)
	
 	 on case transaction.categorie  
 	 	 case tfgld.catg.journals :  
 	 	 case tfgld.catg.recurr.journ :  
 	 	  	 o.exchange.rate.type =  i.var.str.eu.rtyp  
 	 	  	 break  
 	 	 case tfgld.catg.sales.inv :  
 	 	 case tfgld.catg.sales.cred :  
 	 	 case tfgld.catg.sales.corr :  
 	 	  	 o.exchange.rate.type =  i.var.str.sls.rtyp  
 	 	  	 break  
 	 	 case tfgld.catg.purchase.inv :  
 	 	 case tfgld.catg.purchase.cred :  
 	 	 case tfgld.catg.purchase.corr :  
 	 	  	 o.exchange.rate.type =  i.var.str.pur.rtyp  
 	 	  	 break  
 	 	 case tfgld.catg.opening.bal :  
		 	o.exchange.rate.type = i.var.str.eu.rtyp
			break
 	 	 case tfgld.catg.cash :  
		 	if i.run.number = 0 then
				non.finalized.transaction(
					i.var.str.pur.rtyp,
					i.var.str.sls.rtyp,
					i.var.str.eu.rtyp,
					i.company,
					i.year,
					i.batch,
					i.transaction.type,
					i.document,
					o.exchange.rate.type)
			else	
				finalized.transaction(
					i.var.str.pur.rtyp,
					i.var.str.sls.rtyp,
					i.var.str.eu.rtyp,
					i.transaction.type,
					i.document,
					o.exchange.rate.type)
			endif
			break
  	 endcase  
}

function void determine.transaction.categorie(
		domain	tfgld.ttyp	i.transaction.type,
	ref	domain	tfgld.catg	o.transaction.categorie)
{	
	static	domain	tfgld.ttyp	s.transaction.type	FIXED
	static	domain	tfgld.catg	s.transaction.categorie
	
	o.transaction.categorie = empty
	
	if i.transaction.type = s.transaction.type then
		o.transaction.categorie = s.transaction.categorie
		return
	endif
	
	select 	tfgld011.catg:o.transaction.categorie
 	from 	tfgld011  
 	where 	tfgld011._index1 =  {:i.transaction.type}  
 	as set with 1 rows
 	selectdo  
		s.transaction.type = i.transaction.type
		s.transaction.categorie = o.transaction.categorie
	endselect
}

function void non.finalized.transaction(
		domain	tcmcs.str3	i.var.str.pur.rtyp,
		domain	tcmcs.str3	i.var.str.sls.rtyp,
		domain	tcmcs.str3	i.var.str.eu.rtyp,
		domain	tcncmp		i.company,
		domain	tfgld.year	i.year,
		domain	tfgld.btno	i.batch,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
	ref	domain	tcmcs.str3	o.exchange.rate.type)
{
	domain	tfcmg.tran	type.of.transaction
	
 	select	tfgld102.tran:type.of.transaction
 	from 	tfgld102  
 	where 	tfgld102._index1 =  {:i.company, :i.year, :i.batch,  
 	 	 	 	     :i.transaction.type, :i.document}  
 	as set with 1 rows  
 	selectdo  
		convert.transaction.type.to.rate.type(
			i.var.str.pur.rtyp,
			i.var.str.sls.rtyp,
			i.var.str.eu.rtyp,
			type.of.transaction,
			o.exchange.rate.type)
	endselect
}

function void finalized.transaction(
		domain	tcmcs.str3	i.var.str.pur.rtyp,
		domain	tcmcs.str3	i.var.str.sls.rtyp,
		domain	tcmcs.str3	i.var.str.eu.rtyp,
		domain	tfgld.ttyp	i.transaction.type,
		domain	tfgld.docn	i.document,
	ref	domain	tcmcs.str3	o.exchange.rate.type)
{
	domain	tfcmg.tran	type.of.transaction
	
 	select	tfgld106.tran:type.of.transaction
 	from 	tfgld106  
 	where 	tfgld106._index1 =  {:i.transaction.type, :i.document}  
 	as set with 1 rows  
 	selectdo  
		convert.transaction.type.to.rate.type(
			i.var.str.pur.rtyp,
			i.var.str.sls.rtyp,
			i.var.str.eu.rtyp,
			type.of.transaction,
			o.exchange.rate.type)
	endselect
}

function void convert.transaction.type.to.rate.type(
		domain	tcmcs.str3	i.var.str.pur.rtyp,
		domain	tcmcs.str3	i.var.str.sls.rtyp,
		domain	tcmcs.str3	i.var.str.eu.rtyp,
		domain	tfcmg.tran	i.type.of.transaction,
	ref	domain	tcmcs.str3	o.exchange.rate.type)
{
	 on case i.type.of.transaction
   	 	case tfcmg.tran.customer :  
   	 	case tfcmg.tran.unalloc.rec :  
   	 	case tfcmg.tran.advance.rec :  
  	  	case tfcmg.tran.reconc.cust :  
  	  	 	 o.exchange.rate.type =  i.var.str.sls.rtyp  
  	 	 	 break  
  	 	case tfcmg.tran.supplier :  
  	  	case tfcmg.tran.unalloc.paym :  
  	 	case tfcmg.tran.advance.paym :  
  	 	case tfcmg.tran.reconc.suppl :  
  	 	 	 o.exchange.rate.type =  i.var.str.pur.rtyp  
  	 		 break  
  	 	default:
  	 	 	 o.exchange.rate.type =  i.var.str.eu.rtyp  
  	 	 	 break  
  	  endcase
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:gld
:TFLNO:102
:FUNC:START
function  long fill.adty.from.tfcmg294() 
{
	select tfcmg294.*
	from  tfcmg294
	where tfcmg294.year = :tfgld102.year
	and     tfcmg294.ttyp = :tfgld102.ttyp 
	and    tfcmg294.docn = :tfgld102.docn
	and    tfcmg294.lino = :tfgld102.lino 
	order by tfcmg294._index1
	as set with 1 rows
	selectdo
		return(etol(tfcmg294.adty))
	endselect

	return(0)
}

function  long fill.adty.from.tfcmg295() 
{
	select tfcmg101.*,tfcmg295.*
	from  tfcmg101,tfcmg295
	where tfcmg101.tadv = tfcmg.tadv.advance
	and   tfcmg101.ptyp = :tfgld102.ttyp
	and   tfcmg101.pdoc  = :tfgld102.docn
	and    tfcmg295._index1 = tfcmg101._index1 
	order by tfcmg295._index1
	as set with 1 rows
	selectdo
		return(etol(tfcmg295.adty))
	endselect

	return(0)

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:gld
:TFLNO:106
:FUNC:START
function  long fill.adty.from.tfcmg294() 
{
	select tfcmg294.*
	from  tfcmg294
	where tfcmg294.year = :tfgld106.oyer
	and    tfcmg294.ttyp = :tfgld106.otyp 
	and    tfcmg294.docn = :tfgld106.odoc
	and    tfcmg294.lino = :tfgld106.olin 
	order by tfcmg294._index1
	as set with 1 rows
	selectdo
		return(etol(tfcmg294.adty))
	endselect

	return(0)
}

function  long fill.adty.from.tfcmg295() 
{
	select tfcmg101.*,tfcmg295.*
	from  tfcmg101,tfcmg295
	where tfcmg101.tadv = tfcmg.tadv.advance
	and    tfcmg101.ptyp = :tfgld106.otyp 
	and    tfcmg101.pdoc = :tfgld106.odoc
	and    tfcmg295._index1 = tfcmg101._index1 
	order by tfcmg295._index1
	as set with 1 rows
	selectdo
		return(etol(tfcmg295.adty))
	endselect

	return(0)

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tf
:TCMOD:gld
:TFLNO:116
:FUNC:START
function void get.tax.information.from.transaction(  
		domain	tcncmp		i.company,
		domain 	tfgld.ttyp 	i.transaction.type,  
 		domain	tfgld.docn	i.document,  
 		domain	tfgld.lino	i.line.number,  
 		domain	tfgld.serl	i.sequence.number,  
 		domain	tfgld.srno	i.background.number,  
 	ref	domain	tcccty	 	o.tax.country,
	ref	domain 	tccvat		o.tax.code)
{
		domain	tfgld.year	year
		domain	tfgld.btno	batch

	o.tax.country = ""
	o.tax.code = ""
	year = 0
	batch = 0
	
	if transaction.is.finalized(
		i.transaction.type,
		i.document,
		year,
		batch) then
		read.tax.info.finalized.transactions(
			i.transaction.type,  
 			i.document,  
 			i.line.number,  
 			i.sequence.number,  
 			i.background.number,  
 			o.tax.country,
			o.tax.code)
	else	
		read.tax.info.non.finalized.transactions(
			i.company,
			year,
			batch,
			i.transaction.type,  
 			i.document,  
 			i.line.number,  
 			i.sequence.number,  
 			i.background.number,  
 			o.tax.country,
			o.tax.code)
	endif
}

function domain tcbool transaction.is.finalized(
		domain 	tfgld.ttyp 	i.transaction.type,  
 		domain	tfgld.docn	i.document,  
	ref	domain	tfgld.year	o.year,
	ref	domain	tfgld.btno	o.batch)
{
	static	domain 	tfgld.ttyp 	hold.transaction.type
 	static	domain	tfgld.docn	hold.document
	static	domain	tcbool		hold.return
	static	domain	tfgld.year	hold.year
	static	domain	tfgld.btno	hold.batch
	
	
	if i.transaction.type = hold.transaction.type and
	   i.document = hold.document then
	   	o.year = hold.year
		o.batch = hold.batch
	   	return(hold.return)
	endif

	hold.transaction.type = i.transaction.type
	hold.document = i.document
	
	select	tfgld018.trun:o.year,
		tfgld018.btno:o.batch,
		tfgld018.trun
	from	tfgld018
	where	tfgld018._index1 = {:i.transaction.type, :i.document}
	as set with 1 rows
	selectdo
		if tfgld018.trun <> 0 then
			hold.return = true
		else
			hold.return = false
		endif	
		hold.year = o.year
		hold.batch = o.batch
		return(hold.return)
	endselect

	|* This statement must not be reached
	hold.return = false
	hold.batch = 0
	return(false)
}

function void read.tax.info.finalized.transactions(  
		domain 	tfgld.ttyp 	i.transaction.type,  
 		domain	tfgld.docn	i.document,  
 		domain	tfgld.lino	i.line.number,  
 		domain	tfgld.serl	i.sequence.number,  
 		domain	tfgld.srno	i.background.number,  
 	ref	domain	tcccty	 	o.tax.country,
	ref	domain 	tccvat		o.tax.code)
{
	o.tax.country = ""
	o.tax.code = ""

	select	tfgld106.ccty:o.tax.country, tfgld106.cvat:o.tax.code
	from	tfgld106
	where	tfgld106._index1 = {	:i.transaction.type,  
 					:i.document,  
			 		:i.line.number,  
 					:i.sequence.number,  
 					:i.background.number}
	as set with 1 rows
	selectdo
	endselect
}	

function void read.tax.info.non.finalized.transactions(  
		domain	tcncmp		i.company,
		domain	tfgld.year	i.year,
		domain	tfgld.btno	i.batch,
		domain 	tfgld.ttyp 	i.transaction.type,  
 		domain	tfgld.docn	i.document,  
 		domain	tfgld.lino	i.line.number,  
 		domain	tfgld.serl	i.sequence.number,  
 		domain	tfgld.srno	i.background.number,  
 	ref	domain	tcccty	 	o.tax.country,
	ref	domain 	tccvat		o.tax.code)
{
	o.tax.country = ""
	o.tax.code = ""

	select	tfgld102.ccty:o.tax.country, tfgld102.cvat:o.tax.code
	from	tfgld102
	where	tfgld102._index1 = {	:i.company,
					:i.year,
					:i.batch,
					:i.transaction.type,  
 					:i.document,  
			 		:i.line.number,  
 					:i.sequence.number,  
 					:i.background.number}
	as set with 1 rows
	selectdo
	endselect
}

function domain tfgld.catg get.category.from.transaction.type(
		domain	tfgld.ttyp	i.transaction.type)
{
		domain	tfgld.catg	category

	category = empty

	select	tfgld011.catg:category
	from	tfgld011
	where	tfgld011._index1 = {:i.transaction.type}
	as set with 1 rows
	selectdo
	endselect

	return(category)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:62
:VERS:B4cf
:RELE:61
:CUST:f106
:TCPAC:tp
:TCMOD:ppc
:TFLNO:291
:FUNC:START
function determine.default.value.for.tpppc291.blbl()
{
	var.tpppc291.blbl = tcyesno.no

	select 	tppdm600.agty, tppdm600.invm,tppdm600.ncmp,tpptc050.stlm
	from 	tppdm600, tpptc050,tpppc291
	where	(tppdm600._index1 = {:tpppc291.cprj}
	or	 tpptc050._index1 = {:tpppc291.cprj,:tpppc291.cstl})
	and	tpppc291.cprj refers to tppdm600
	and 	tpppc291.c002 refers to tpptc050
	and 	(not ((  tppdm600.agty = tppdm.agty.contract
	or 	  	tppdm600.invm = tppdm.invm.unit.rate)
	and   	(tpppc291.cstl = "")
	or	(tpppc291.cstl <> ""
	and	 tpptc050.stlm <> tpptc.stlm.real.costs)))
	as set with 1 rows
	selectdo
		var.tpppc291.blbl = tcyesno.yes
	endselect
}
#define	POSTING.TYPE.FOR.INVOICING(PT)
^	( ##PT## = target(tpppc.ptyc.project.control) or
^	##PT## = target(tpppc.ptyc.cost.invoice) or
^	##PT## = target(tpppc.ptyc.inventory)  or
^	##PT## = target(tpppc.ptyc.pur.invoice) or
^	##PT## = target(tpppc.ptyc.stage.invoice) or
^	##PT## = target(tpppc.ptyc.field.service) or
^	##PT## = target(tpppc.ptyc.manual.sales) or
^	##PT## = target(tpppc.ptyc.labor) or
^	##PT## = target(tpppc.ptyc.expenses) or
^	##PT## = target(tpppc.ptyc.hours.subcontr) or
^	##PT## = target(tpppc.ptyc.applied.ovhd) or
^	##PT## = target(tpppc.ptyc.ln.cost) or
^	##PT## = target(tpppc.ptyc.ln.cost.var) or
^	##PT## = target(tpppc.ptyc.warh.surch.rec) or
^	##PT## = target(tpppc.ptyc.warh.surch.iss) or
^	##PT## = target(tpppc.ptyc.item.surch.rec) or
^	##PT## = target(tpppc.ptyc.item.surch.iss) or
^	##PT## = target(tpppc.ptyc.receipt) or
^	##PT## = target(tpppc.ptyc.issue) or
^	##PT## = target(tpppc.ptyc.issue.result) or
^	##PT## = target(tpppc.ptyc.cost.peg.trf.sp) or
^	##PT## = target(tpppc.ptyc.wip.hours) or
^	##PT## = target(tpppc.ptyc.contract) or
^	##PT## = target(tpppc.ptyc.repair.warranty) or
^	##PT## = target(tpppc.ptyc.warranty.costs) or
^	##PT## = target(tpppc.ptyc.goodwill) or
^	##PT## = target(tpppc.ptyc.general) or
^	##PT## = target(tpppc.ptyc.general.fin) or
^	##PT## = target(tpppc.ptyc.tool.costs) )

function domain tcbool cost.history.allow.invoicing(
		domain	tccprj		i.project,
		domain	tpptc.cstl	i.extension,
|		domain	tccono		i.contract,
|		domain	tpctm.cnln	i.contract.line,
|		domain	tpppc.ptyc	i.posting.type,
			long		i.posting.type.long)
|		domain	tcyesno		i.inter.company,
|		domain	tcyesno		i.cost.transaction,
|		domain	tctror		i.transaction.origin)
{
	FunctionUsage
	Expl:	This function determines whether the cost history transaction
		is eligible for invoicing.

		Function is derived from
		tpppc.dll0200.cost.history.allow.invoicing(
	Input:	See List
	Return:	true	this transaction is eligible for invoicing
		false	otherwise
	EndFunctionUsage

|* Default set to "No" . So not applicable for the migration.
|	if i.cost.transaction = tcyesno.no then
|		|* Transactions that only move costs (to TP WIP) and do
|		|* not add costs are not billable.
|		return(false)
|	endif

|* Default set to "No" . So not applicable for the migration.
|	if i.inter.company = tcyesno.yes then
|		|* Transactions that only move costs (from company A to
|		|* company B) and do not add costs are not billable.
|		return(false)
|	endif

	if not POSTING.TYPE.FOR.INVOICING(i.posting.type.long) then
		|* Transactions like commitments, project surcharges and
		|* variances (in most cases) are not billable.
		return(false)
	endif

|	if i.posting.type = tpppc.ptyc.receipt and
|	   (i.transaction.origin = tctror.pur or
|	    i.transaction.origin = tctror.pur.schedule or	|#SCHED_PEG.n
|	    i.transaction.origin = tctror.not.applicable) then

|* In the migration Transaction Origin is always "Not Applicable"
	if i.posting.type.long = target(tpppc.ptyc.receipt) then
		|* A Purchase Receipt is considered as Hard Commitment and
		|* therefore not billable.
		|* Transaction Origin 'Purchase' = pegged
		|* Transaction Origin 'Not Applicable' = non-pegged
		return(false)
	endif

	if not billable.according.project.and.extension(
				i.project,
				i.extension) then
		return(false)
	endif
|	if isspace(i.extension) then
|		if tpctm.dll0110.contract.line.contract.type.is.fixed.price(
|					i.contract,
|					i.contract.line) then
|			return(false)
|		endif
|	else
|		if not tpptc.dll0050.extension.has.inv.method.actual.costs(
|					i.project,
|					i.extension) then
|			return(false)
|		endif
|		if tpptc.dll0050.extension.type.is.provisional.amount(
|					i.project,
|					i.extension) then
|			return(false)
|		endif
|	endif
	return(true)
}

|*function determine.default.value.for.tpppc215.blbl(
function domain tcbool billable.according.project.and.extension(
		domain	tccprj		i.project,
		domain	tpptc.cstl	i.extension)
{
		domain	tcbool		billable

	billable = false
	if isspace(i.extension) then
		select 	tppdm600.agty, tppdm600.invm,tppdm600.ncmp,tppdm600.cprj
		from	tppdm600
		where	tppdm600._index1 = {:i.project}
		and 	not (  tppdm600.agty = tppdm.agty.contract or
		 	  	tppdm600.invm = tppdm.invm.unit.rate)
		as set with 1 rows
		selectdo
			billable = true
		endselect
	else
		select 	tpptc050.stlm, tpptc050.cprj, tpptc050.cstl
		from 	tpptc050
		where	tpptc050._index1 = {:i.project,:i.extension}
		and	 tpptc050.stlm <> tpptc.stlm.real.costs
		as set with 1 rows
		selectdo
			billable = true
		endselect
	endif
	return(billable)
}

|function extern tpppc.dll0200.determine.posting.type.costs.material(
function extern determine.posting.type.costs.sundry(
			long		i.posting.type.temp.table.long,
	ref		long		o.posting.type.history.long)
|		domain	tpppc.teti	i.posting.type.temp.table,
|	ref	domain	tpppc.ptyc	o.posting.type.history)
{
	DllUsage
	Expl:	This function determines the posting type for cost history,
		based upon posting type from material costs
	Input:	i.posting.type.temp.table	- Posting Type from:
						  tpppc211 (Material)
	Output: o.posting.type.history		- Posting Type in tpppc200
	Return:
	EndDllUsage

	on case i.posting.type.temp.table.long
	case target(tpppc.teti.manu):
		o.posting.type.history.long = target(tpppc.ptyc.project.control)
		break
	case target(tpppc.teti.invoice):
		o.posting.type.history.long = target(tpppc.ptyc.cost.invoice)
		break
	default:
		o.posting.type.history.long = 0	|*empty
		break
	endcase
}
:FUNC:END
:TEXT:

:TEXT:END
