FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:bc
:TCMOD:ttf
:TFLNO:001
:FUNC:START
function write.additional.records()
{
	|* Dummy records for not in baan6 existing tror/fitr combinations
	var.recon.area = 0			|* empty
	var.recon.sub.area = 0
	var.additional.type = 0
	var.ledger.account = ""
	var.priority = 0
	var.order.priority = 0
	var.logistic.company = 0
	var.financial.company = 0

	var.trans.origin = tctror.sls.con
	var.fin.trans = tcfitr.allocation
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sls.inv
	var.fin.trans = tcfitr.allocation
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sma.order
	var.fin.trans = tcfitr.allocation
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.equipment
	var.fin.trans = tcfitr.equipment
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.prd.pmg
	var.fin.trans = tcfitr.general
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.cms
	var.fin.trans = tcfitr.invoice
	var.additional.type = 7			|* (Rebate)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.equipment
	var.fin.trans = tcfitr.invoice
	var.additional.type = 0			|* (Means not another)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.equipment
	var.fin.trans = tcfitr.invoice
	var.additional.type = 6			|* (Revenues)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.project
	var.fin.trans = tcfitr.invoice
	var.additional.type = 0			|* (Means not another)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.project
	var.fin.trans = tcfitr.invoice
	var.additional.type = 6			|* (Revenues)
	var.debit.credit = tfgld.dbcr.debit
	|* Here only a debit record is written
	INCREASE_VAR_SEQUENCE
	write.dump.1()
	
	var.trans.origin = tctror.project
	var.fin.trans = tcfitr.invoice
	var.additional.type = 2			|* (Late Payment Surcharge)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sls
	var.fin.trans = tcfitr.invoice
	var.additional.type = 0			|* (Means not another)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sls
	var.fin.trans = tcfitr.invoice
	var.additional.type = 2			|* (Late Payment Surcharge)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sls
	var.fin.trans = tcfitr.invoice
	var.additional.type = 1			|* (Invoice Totals)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sma.con
	var.fin.trans = tcfitr.invoice
	var.additional.type = 0			|* (Means not another)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sma.con
	var.fin.trans = tcfitr.invoice
	var.additional.type = 2			|* (Late Payment Surcharge)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sma.con
	var.fin.trans = tcfitr.invoice
	var.additional.type = 1			|* (Invoice Totals)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sma.order
	var.fin.trans = tcfitr.invoice
	var.additional.type = 0			|* (Means not another)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sma.order
	var.fin.trans = tcfitr.invoice
	var.additional.type = 2			|* (Late Payment Surcharge)
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.sma.order
	var.fin.trans = tcfitr.invoice
	var.additional.type = 1			|* (Invoice Totals)
	write.debit.and.credit.dump.1()

	var.additional.type = 0
	if tccom999.trtr = tcyesno.yes then
		var.trans.origin = tctror.trans
		var.fin.trans = tcfitr.fuel
		write.debit.and.credit.dump.1()
		
		var.trans.origin = tctror.trans
		var.fin.trans = tcfitr.hours
		write.debit.and.credit.dump.1()
		
		var.trans.origin = tctror.trans
		var.fin.trans = tcfitr.maintenance
		write.debit.and.credit.dump.1()
		
		var.trans.origin = tctror.trans
		var.fin.trans = tcfitr.costs
		write.debit.and.credit.dump.1()
		
		var.trans.origin = tctror.trans
		var.fin.trans = tcfitr.invoice
		var.additional.type = 0			|* (Means not another)
		write.debit.and.credit.dump.1()
		
		var.trans.origin = tctror.trans
		var.fin.trans = tcfitr.invoice
		var.additional.type = 6			|* (Revenues)
		write.debit.and.credit.dump.1()
		
		var.trans.origin = tctror.trans
		var.fin.trans = tcfitr.invoice
		var.additional.type = 3			|* (Order Discount)
		write.debit.and.credit.dump.1()
		
		var.trans.origin = tctror.trans
		var.fin.trans = tcfitr.invoice
		var.additional.type = 2		|* (Late Payment Surcharge)
		write.debit.and.credit.dump.1()
		
		var.trans.origin = tctror.trans
		var.fin.trans = tcfitr.invoice
		var.additional.type = 1			|* (Invoice Totals)
		write.debit.and.credit.dump.1()

		var.additional.type = 0
	endif

	var.trans.origin = tctror.prd.pmg
	var.fin.trans = tcfitr.issue
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.prd.pmg
	var.fin.trans = tcfitr.lot.result
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.pur.inv
	var.fin.trans = tcfitr.on.order
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.prd.pmg
	var.fin.trans = tcfitr.operation
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.prd.pmg
	var.fin.trans = tcfitr.prd.result
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.cms
	var.fin.trans = tcfitr.pur.invoice
	var.additional.type = 8			|* (Commission)
	write.debit.and.credit.dump.1()
	var.additional.type = 0

	var.trans.origin = tctror.prd.pmg
	var.fin.trans = tcfitr.receipt
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.prd.pmg
	var.fin.trans = tcfitr.receiptvar
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.prd.pmg
	var.fin.trans = tcfitr.revaluation
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.prp.wrh
	var.fin.trans = tcfitr.revenue
	var.debit.credit = tfgld.dbcr.credit
	INCREASE_VAR_SEQUENCE
	write.dump.1()
	
	var.trans.origin = tctror.prd.pmg
	var.fin.trans = tcfitr.surcharge
	write.debit.and.credit.dump.1()
	
	var.trans.origin = tctror.project
	var.fin.trans = tcfitr.wip.equip.int
	write.debit.and.credit.dump.1()
}

function write.debit.and.credit.dump.1()
{
	var.debit.credit = tfgld.dbcr.debit
	INCREASE_VAR_SEQUENCE
	write.dump.1()
	var.debit.credit = tfgld.dbcr.credit
	INCREASE_VAR_SEQUENCE
	write.dump.1()
}

function write.dump.1()
{
	if var.recon.area = 0 or
	   (var.recon.area <> 0 and not isspace(var.ledger.account)) then
		|* Dummy records for not in baan6 existing tror/fitr have no LA
		|* Normal records must have a filled LA
		dump 1
	endif
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:bc
:TCMOD:ttf
:TFLNO:002
:FUNC:START
function write.dump.1()
{
	if var.recon.area = 0 or
	   (var.recon.area <> 0 and not isspace(var.ledger.account)) then
		|* Dummy records for not in baan6 existing tror/fitr have no LA
		|* Normal records must have a filled LA
		dump 1
	endif
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:bc
:TCMOD:ttf
:TFLNO:020
:FUNC:START
function determine.order(
		domain	tctror		i.trans.origin,
		domain	tcfitr		i.fin.trans,
		domain	tfgld.orno	i.old.order,
		domain	tccwar		i.warehouse,
	ref	domain	bcorno		o.new.order)
{
	domain	bcorno		temp.order

	o.new.order = ""

	if i.old.order <= 0 then
		|* Order is left empty
		return
	endif

	on case i.trans.origin
	case tctror.pur:
		| Purchase
		o.new.order = convert.number(bcckon.act.pur, i.old.order)
		break
	case tctror.pur.con:
		| Purchase Contract
		o.new.order = convert.number(bcckon.act.pur.contr, i.old.order)
		break
	case tctror.pur.inv:
		| Purchase (INV)
		o.new.order = convert.number(bcckon.wrh.order, i.old.order)
		break
	case tctror.sls:
		| Sales
		o.new.order = convert.number(bcckon.act.sls, i.old.order)
		break
	case tctror.sls.con:
		| Sales Contract
		o.new.order = convert.number(bcckon.act.sls.contr, i.old.order)
		break
	case tctror.sls.inv:
		| Sales (INV)
		o.new.order = convert.number(bcckon.wrh.order, i.old.order)
		break
	case tctror.prd:
		| Production (SFC)
		o.new.order = convert.number(bcckon.act.sfc, i.old.order)
		break
	case tctror.prd.inv:
		| Production (INV)
		o.new.order = convert.number(bcckon.wrh.order, i.old.order)
		break
	case tctror.wrh.order:
		| Warehouse Order
		on case i.fin.trans
		case tcfitr.revaluation:
			o.new.order = convert.number(bcckon.wh.inv.val,
								i.old.order)
			break
		case tcfitr.adjustment:
		case tcfitr.lot.result:
			temp.order = str$(i.old.order)
			|* because whinr110 and whinh520 have a convert.number
			|* with bcckon.wrh.order the bcckon.inv.adjustment is
			|* not used here anylonger.
								|#SL214851.so
|			if temp.order(1;1) = "5" then
|				|* Adjustment orders
|				o.new.order = convert.number(
|							bcckon.inv.adjustment,
|							i.old.order)
|				break
|			endif
								|#SL214851.eo
			if temp.order(1;1) = "7" then
				|* Cycle Count orders
				o.new.order = get.new.cycle.count.order.number(
								i.old.order,
								i.warehouse)
				break
			endif
			|* fall back for other lot results at transfer
			o.new.order = convert.number(bcckon.wrh.order,
								i.old.order)
			break
		default:
			|* The fitr = transfer comes here
			o.new.order = convert.number(bcckon.wrh.order,
								i.old.order)
			break
		endcase
		break						|#SL214851.n
	case tctror.rpl.order:
		| Replenishment Order
		|* Order comes from tdrpl100
		o.new.order = convert.number(bcckon.repl.order, i.old.order)
		break
	case tctror.prp.wrh:
		| PRP Warehouse
		|* Order comes from tipcs530
		o.new.order = convert.number(bcckon.ti.prp, i.old.order)
		break
	case tctror.pcs:
		| PCS
|		on case i.fin.trans
|		case tcfitr.general:
|			|* Order comes from tipcs340.sern
|			o.new.order = str$(i.old.order)
|			break
|		case tcfitr.operation:
|			|* Order comes from tihra100.pdno
|			o.new.order = convert.number(bcckon.act.sfc,
|								i.old.order)
|		|* other fitrs have orno = 0
|		endcase
		o.new.order = ""		|* order not used in baan6
		break
	case tctror.sma.con:
		| Service Contract
		|* Order is always 0 for Service Contracts
		break
	case tctror.sma.order:
		| Service Order
		o.new.order = convert.number(bcckon.sma.order, i.old.order)
		break
	case tctror.trans:
		| Transport
		on case i.fin.trans
		case tcfitr.costs:
			|* Order comes from trtoc101.orno
			o.new.order = convert.number(bcckon.toc.order,
								i.old.order)
		|* other fitrs have orno = 0
		endcase
		break
	case tctror.project:
		| Project
|		on case i.fin.trans
|		case tcfitr.wip.stock:
|			o.new.order = convert.number(bcckon.repl.order,
|								i.old.order)
|			break
|		case tcfitr.wip.equip.int:
|			|* Equipment (ISM) orders not known in baan6
|			o.new.order = ""
|			break
|		case tcfitr.wip.titp:
|			o.new.order = convert.number(bcckon.ti.prp,
|								i.old.order)
|			break
|		case tcfitr.comp.finan:
|			|* Although the order number is filled here, no
|			|* difference can be made for pur / rpl / ti.prp so
|			|* make the ordernumber empty
|			o.new.order = ""
|			break
|		case tcfitr.wip.rev.manu:
|		case tcfitr.comp.revenue:
|		case tcfitr.invoice:
|			|* Note that revenues do not have ordernumbers
|			break
|		default:
|			|* In most situations the order number in TP is 
|			|* a purchase order.
|			o.new.order = convert.number(bcckon.act.pur,
|								i.old.order)
|		endcase
		o.new.order = ""		|* order not used in baan6
		break
	case tctror.equip.wrh:
		| ISM Warehouse
	case tctror.equipment:
		| Equipment (ISM)
		o.new.order = str$(i.old.order)
		break
	case tctror.project.wrh:
	| Project Warehouse
		|* not applicable
		break
	case tctror.project.pur:
	| Project Purchase
		|* not applicable
		break
	case tctror.extern:
		| External Package
		|* not applicable
		break
	case tctror.prd.pmg:
		| Production (PMG)
		o.new.order = convert.number(bcckon.act.pmg, i.old.order)
		break
	case tctror.cms:
		| CMS
		o.new.order = convert.number(bcckon.act.sls, i.old.order)
		break
	endcase
}

function determine.order.line(				|#SL214851.sn
		domain	tctror		i.trans.origin,
		domain	tcfitr		i.fin.trans,
		domain	tfgld.orno	i.old.order,
		domain	tcpono		i.old.order.line,
	ref	domain	tcpono		o.new.order.line)
{
	|* Note that the logic of this functions comes
	|* from Conversion Object whinr110
	domain	bcorno		temp.order

	o.new.order.line = i.old.order.line

	if i.old.order <= 0 then
		|* Order is left empty
		return
	endif

	on case i.trans.origin
	case tctror.wrh.order:
		| Warehouse Order
		on case i.fin.trans
		case tcfitr.adjustment:
		case tcfitr.lot.result:
			temp.order = str$(i.old.order)
			if temp.order(1;1) = "5" or
			   temp.order(1;1) = "7" then
				|* Adjustment Order
				|* Cycle Count orders
				o.new.order.line = determine.new.order.line(
								i.old.order,
								i.old.order.line)
			endif
			break
		endcase
		break
	case tctror.prp.wrh:
		| PRP Warehouse
		|* Order comes from tipcs530
		o.new.order.line = 10
		break
	endcase
}								|#SL214851.en

function determine.kind.of.order(
		domain	tctror		i.trans.origin,
		domain	tcfitr		i.fin.trans,
		domain	tcorno		i.order,
	ref	domain	tcmcs.long	o.new.kind.of.order)
{
	domain	tckoor	old.kind.of.order

	if i.order = 0 then
		o.new.kind.of.order = 0
		return
	endif
	on case i.trans.origin
	case tctror.pur.inv:
		old.kind.of.order = tckoor.wrh.order
		break
	case tctror.sls.inv:
		old.kind.of.order = tckoor.wrh.order
		break
	case tctror.prd.inv:
		old.kind.of.order = tckoor.wrh.order
		break
	case tctror.rpl.order:
		old.kind.of.order = tckoor.act.rpl
		break
	case tctror.prp.wrh:
		old.kind.of.order = tckoor.pcs.wrh
		break
	case tctror.wrh.order:
		old.kind.of.order = tckoor.wrh.order
		break
	default:
		o.new.kind.of.order = 0
		return
		break
	endcase

	o.new.kind.of.order = convert.tckoor(	old.kind.of.order,
						i.order)
|	on case i.old.koor
|		case tckoor.wrh.order:
|			series = i.orno/100000
|			on case series
|			case 1:
|				l.new.koor = 33	| purchase manual
|				break
|			case 2:
|				l.new.koor = 34 | sales manual
|				break
|			case 3:
|			case 4:
|				l.new.koor = 32 | production manual
|				break
|			case 5:
|				l.new.koor = 52 | adjustment
|				break
|			case 6:
|				l.new.koor = 36 | transfer manual
|				break
|			case 7:
|				l.new.koor = 51 | cycle counting
|				break
|			case 8:
|				l.new.koor = 55 | revaluation
|				break
|			endcase
|			break
|		case tckoor.act.rpl:
|			l.new.koor = 22
|			break
|		case tckoor.pcs.wrh:
|			l.new.koor = 22	|transfer
|			break
}

function determine.project(
		domain	tcncmp		i.logistic.company,
		domain	tctror		i.trans.origin,
		domain	tccprj		i.old.project,
	ref	domain	bcstc.nprj	o.new.project)
{
	o.new.project = ""

	if isspace(i.old.project) then
		return
	endif

	on case i.trans.origin
	case tctror.project:
	case tctror.equip.wrh:
	case tctror.equipment:
	case tctror.rpl.order:
		o.new.project = get.tp.project(i.old.project)
		break
	default:
		|* In other packages projects from TI or from TP
		|* can be used.
		o.new.project = get.project(i.old.project)
		break
	endcase
}

function domain tccwar read.warehouse.wrh.order(
		domain	tcncmp		i.company,
		domain	tctror		i.trans.origin,
		domain	tcfitr		i.fin.trans,
		domain	tfgld.date	i.transaction.date,
		domain	tctime		i.transaction.time,
		domain	tfgld.sern	i.serial.number)
{
	|* Because fitr is <> invoice and <> pur.invoice, the
	|* line in tfgld410 is 0 (that debit record also has
	|* a lot of fields for the credit record).
static	domain	tccwar		hold.warehouse
static	domain	tcncmp		save.company
static	domain	tctror		save.trans.origin
static	domain	tcfitr		save.fin.trans
static	domain	tfgld.date	save.transaction.date
static	domain	tctime		save.transaction.time
static	domain	tfgld.sern	save.serial.number


	if i.company		<> save.company or
	   i.trans.origin	<> save.trans.origin or
	   i.fin.trans		<> save.fin.trans or
	   i.transaction.date	<> save.transaction.date or
	   i.transaction.time	<> save.transaction.time or
	   i.serial.number	<> save.serial.number then
		hold.warehouse = ""
		select	tfgld410.cwar:hold.warehouse
		from	tfgld410
		where	tfgld410._index1 = {	:i.company,
						:i.trans.origin,
						:i.fin.trans,
						:i.transaction.date,
						:i.transaction.time,
						:i.serial.number,
						0 }
		selectdo
		endselect
		save.company		= i.company
		save.trans.origin	= i.trans.origin
		save.fin.trans		= i.fin.trans
		save.transaction.date	= i.transaction.date
		save.transaction.time	= i.transaction.time
		save.serial.number	= i.serial.number
	endif
	return(hold.warehouse)
}

function domain	tcpono determine.new.order.line(
		domain	tcorno		i.old.order,
		domain	tcpono		i.old.order.line)
{
static	domain	tcorno		hold.order
static	domain	tcpono		new.order.line
	domain	tccprj		project
	domain	tcitem		item
	domain	tccuni		container
	domain	tccwar		warehouse
	domain	tcdate		input.date
	domain	tcsern		sequence
	domain	tcsern		last.sequence

	if i.old.order <> hold.order then
		new.order.line = i.old.order.line

		select	tdinv100.cprj:project,
			tdinv100.item:item,
			tdinv100.cntr:container,
			tdinv100.cwar:warehouse,
			tdinv100.idat:input.date,
			tdinv100.sern:sequence
		from	tdinv100 
		where	tdinv100._index2 = {:i.old.order} 
		as set with 1 rows 
		selectdo 
			select tdilc102.serb:last.sequence 
			from tdilc102 
			where tdilc102._index1 = { 	:project, 
							:item, 
							:container, 
							:warehouse, 
							:input.date, 
							:sequence} 
			order by tdilc102.serb desc
			as set with 1 rows 
			selectdo 
				new.order.line = last.sequence
			selectempty
				new.order.line = sequence
			endselect 
		endselect 
		hold.order = i.old.order
	endif
	
	return(new.order.line)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:bp
:TCMOD:mdm
:TFLNO:000
:FUNC:START
function domain tcbool hra.hours.accounting.is.used()
{
	domain	tcemno	dummy.emno

	select	tihra100.emno :dummy.emno
	from	tihra100
	selectdo
		return(true)
	endselect

	return(false)
}

function domain tcckow read.default.labor.type()
{
	select	bcsbp002.nhlt
	from	bcsbp002
	where	bcsbp002.deft = tcyesno.yes
	as set with 1 rows
	selectdo
		return(bcsbp002.nhlt)
	endselect

	return("")
}

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)
{
	get.period.table.code(	"tpppc001",
				bcstc000.pfpp,
				bcstp000.cpth,
				dummy.err.mess)
	o.period.table.code = bcstc029.tptc
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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

	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
	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
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:bp
:TCMOD:tmm
:TFLNO:200
:FUNC:START
function void fill.and.dump.bptmm200(
		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)

	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)
|	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(
		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,
						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,
				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(
		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(
		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(
					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(
		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	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.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
	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
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ci
:TCMOD:sli
:TFLNO:100
:FUNC:START
function domain tfgld.ttyp read.tfgld011.iseq(domain tfgld.ttyp i.ttyp)
{
	

	 select tfgld011.*
 	 from tfgld011
 	 where  tfgld011._index1 =  {:i.ttyp}
	 and tfgld011.iseq =  tcyesno.yes  
	 as set with 1 rows
	 selectdo
		return(i.ttyp)
	 selectempty
		return(empty.string(1;3))
	 endselect	

	return(i.ttyp)
}
function call.tfgld011()
{
	 select tfgld011.*
 	 from tfgld011
 	 where  tfgld011._index1 =  {:tdcms000.crtt}
	 and (tfgld011.catg =  tfgld.catg.sales.inv  
	 or tfgld011.catg =  tfgld.catg.sales.cred)  
	 and tfgld011.iseq = tcyesno.yes
	 as set with 1 rows
 	 selectdo  
 	 	 if tfgld011.catg =  tfgld.catg.sales.inv then  
 	 	 	 check.if.sales.invoice()  
 	 	 else  
 	 	 	 check.if.credit.note()  
 	 	 endif  
	selectempty
		var.cisli100.rett =  empty.string(1;3)  
 	 	var.cisli100.resr =  0  
	
 	 endselect

}
function check.if.sales.invoice()
{
	 if tfgld011.ineg =  tcyesno.yes then  
 	 	var.cisli100.rett =  tdcms000.crtt  
 	 	var.cisli100.resr =  tdcms000.crse  
 	  else  
 	 	var.cisli100.rett =  empty.string(1;3)  
 	 	var.cisli100.resr =  0  
 	  endif  
}

function check.if.credit.note()
{
	 if tfgld011.cpst =  tcyesno.yes then  
 	 	  var.cisli100.rett =  tdcms000.crtt  
 	 	  var.cisli100.resr =  tdcms000.crse  
 	else  
 	 	 var.cisli100.rett =  empty.string(1;3)  
 	 	 var.cisli100.resr =  0  
 	endif  

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ci
:TCMOD:sli
:TFLNO:205
:FUNC:START
|#800-141306.sn
function fill.lps.cisli205.salesorders()
{
	select tcmcs011.*   
 	from   tcmcs011  
 	where  tcmcs011._index1 =  {:tdsls040.ccrs}  
 	selectdo  

 		var.cisli205.scai =  var.net.amti * (tcmcs011.perc / 100)
		var.cisli205.scai =  icom0001.domain.round(var.cisli205.scai,
					"tfgld.amnt", tdsls040.ccur,tcmcs002.crnd)

		if var.cisli205.scai <>  0.0 then
| *  MCR -  new changes .s
|			for i = 1 to 3|*MCR
|	 	 	 	 calculate.amount.in.other.currency (var.cisli205.scai,
|					|tdsls045.rats,tdsls045.ratf,  
|					fill.mc.field.double(tdsls045.rats,i),|*MCR
|					fill.mc.field.long(tdsls045.ratf,i),|*MCR
| 	 	 	 	 	tdsls040.ccur,
|					|var.cisli205.scah,
|					var.cisli205.scah(i), |*MCR
|					err.mess)  
|			endfor |*MCR
			for i =  1 to 3 	 	 	 	 	 	  
 	 	 	 	 calculate.amount.in.home.currency.variables (var.cisli205.scai, 	 	 	  
 	 	 	 	 tdsls040.ccur,
 	 	 	 	 fill.mc.field.double(tdsls045.rats,i),
				 fill.mc.field.long(tdsls045.ratf,i),
 	 	 	 	 i,  
 	 	 	 	 var.cisli205.scah(i), 	 	 	 	 	 	 	  
 	 	 	 	 err.mess) 	 	 	 	 	 	  
 	 	 	endfor 	 	 	 	 	 	  
 | *  MCR -  new changes .e 	 	 	 	
 	 	 else  
			|*MCR .s
	 	 	| var.cisli205.scah =  0.0  
			for i = 1 to 3
				var.cisli205.scah(i) =  0.0  
			endfor
			|*MCR .e
 	 	 endif   		
		 var.cisli205.scdt = tdsls045.invd + tcmcs011.days

	selectempty  
		var.cisli205.scdt = 0
		var.cisli205.scai =  0.0  
		|*MCR .s
	 	| var.cisli205.scah =  0.0  
		for i = 1 to 3
			var.cisli205.scah(i) =  0.0  
		endfor
		|*MCR .e
	endselect  
}							
							
							|#800-141306.en

function fill.lps.cisli205.rebates()
{
	select tcmcs011.*   
 	from   tcmcs011  
 	where  tcmcs011._index1 =  {:var.cisli290.ccrs}  
 	selectdo  

 		var.cisli205.scai =  var.net.amti * (tcmcs011.perc / 100)
		var.cisli205.scai =  icom0001.domain.round(var.cisli205.scai,
					"tfgld.amnt", tdcms201.ccur,tcmcs002.crnd)

		if var.cisli205.scai <>  0.0 then  
| *  MCR -  new changes .s		
|			for i = 1 to 3 |*MCR
| 	 	 	 	calculate.amount.in.other.currency (var.cisli205.scai, 	 	 	 			
| 	 	 	 			 | *  MCR -  s  
| 	 	 	 	 	 	 |tdcms201.rate,  
| 	 	 	 	 	 	 |tdcms201.ratf,  
| 	 	 	 	 	 	 fill.mc.field.double(tdcms201.rate,i),  
| 	 	 	 	 	 	 fill.mc.field.long(tdcms201.ratf,i),  
| 	 	 	 	 	 	 | *  MCR -  e  
| 	 	 	 	 	 	tdcms201.ccur,
| 	 	 	 	 	 	|var.cisli205.scah,
| 	 	 	 	 	 	var.cisli205.scah(i),|*MCR
| 	 	 	 	 	 	err.mess)  
| 	 	 	 endfor |*MCR	 	 	
 	 	 	 for i =  1 to 3 	 	 	 	 	 	  
 	 	 	 	 calculate.amount.in.home.currency.variables (var.cisli205.scai, 	 	 	  
 	 	 	 	 tdcms201.ccur,
 	 	 	 	 fill.mc.field.double(tdcms201.rate,i),
				 fill.mc.field.long(tdcms201.ratf,i),
 	 	 	 	 i,  
 	 	 	 	 var.cisli205.scah(i), 	 	 	 	 	 	 	  
 	 	 	 	 err.mess) 	 	 	 	 	 	  
 	 	 	endfor 	 	 	 	 	 	  
 | *  MCR -  new changes .e 	 	 	 	
 	 	 else  
 	 	 	 |*MCR - s
 	 	 	 |var.cisli205.scah =  0.0  
 	 	 	 for i = 1 to 3
 	 	 	 	var.cisli205.scah(i) =  0.0  	
 	 	 	 endfor		
 	 	 	 |*MCR - e
 	 	 endif   		
		 var.cisli205.scdt = tdcms201.crdt + tcmcs011.days

	selectempty  
		var.cisli205.scdt = 0
		var.cisli205.scai =  0.0  
		|*MCR - s
 	 	|var.cisli205.scah =  0.0  
 	 	for i = 1 to 3
 	 	 	var.cisli205.scah(i) =  0.0  	
 	 	endfor		
 	 	|*MCR - e
	endselect  
}
function fill.lps.cisli205.interest()
{
	select tcmcs011.*   
 	from   tcmcs011  
 	where  tcmcs011._index1 =  {:var.cisli210.ccrs}  
 	selectdo  

 		var.cisli205.scai =  old.net.amti * (tcmcs011.perc / 100)
		var.cisli205.scai =  icom0001.domain.round(var.cisli205.scai,
					"tfgld.amnt", var.cisli205.ccur,tcmcs002.crnd)

		if var.cisli205.scai <>  0.0 then  
| *  MCR -  new changes .s		
|			for i = 1 to 3	|*MCR
| 		 	 	 calculate.amount.in.other.currency (var.cisli205.scai, 
|					|*MCR -s
|					|var.cisli205.rate,
|					|var.cisli205.ratf,  
|					var.cisli205.rate(i),
|					var.cisli205.ratf(i),  		
|					|*MCR - e
| 	 	 	 	 	var.cisli205.ccur, 
|					|*MCR -s
|					|var.cisli205.scah,
|					var.cisli205.scah(i), 
|					|*MCR -e
|					err.mess)  
|			endfor |*MCR
			for i =  1 to 3 	 	 	 	 	 	  
 	 	 	 	 calculate.amount.in.home.currency.variables (var.cisli205.scai, 	 	 	  
 	 	 	 	 var.cisli205.ccur,
 	 	 	 	 var.cisli205.rate(i),
				 var.cisli205.ratf(i), 
 	 	 	 	 i,  
 	 	 	 	 var.cisli205.scah(i), 	 	 	 	 	 	 	  
 	 	 	 	 err.mess) 	 	 	 	 	 	  
 	 	 	endfor 	 	 	 	 	 	  
 | *  MCR -  new changes .e 	 	 	 	
 	 	 else  
			|*MCR - s
 	 	 	|var.cisli205.scah =  0.0  
			for i = 1 to 3
				var.cisli205.scah(i) =  0.0  
			endfor
			|*MCR - e
 	 	 endif   		
		 var.cisli205.scdt = var.tfacr200.docd + tcmcs011.days

	selectempty  
		var.cisli205.scdt = 0
		var.cisli205.scai =  0.0  
		|*MCR - s
 	 	|var.cisli205.scah =  0.0  
		for i = 1 to 3
			var.cisli205.scah(i) =  0.0  
		endfor
		|*MCR - e
	endselect  
}




function restore.pointer.tfacr200()
{
 	select tfacr200.*   
 	from tfacr200  
 	where tfacr200._index1 =  {:prev.ttyp, :prev.ninv, :prev.line,"",0,0}  
 	as set with 1 rows  
 	selectdo
 	endselect	


}
function get.customer.data()
{
 	select tccom010.paym:var.cisli240.paym
 	 from tccom010  
 	 where tccom010.cuno =  :tdsls045.cuno  
 	 as set with 1 rows  
 	 selectdo  
 	 selectempty  
		 var.cisli240.paym = ""
 	 endselect  

}
function  dump.advance.payment.records()
{
	select   tppin120.*  , tppin010.cpro  ,tppin100.*
 	from 	 tppin120, tppin010 , tppin100 
 	where 	 tppin120._index1 =  {tppin100.stin, tppin100.ninv} and
 	   	 tppin100.stin =  tppdm.stin.final and
 		 tppin100.cfpo =  tppdm.yeno.yes and  
 		 tppin100.post =  tppdm.yeno.yes and
 		 tppin100.invt =  tppdm.invt.advance and
 		 tppin120.cprj =  :bcstp001.cprj and 
 		 tppin120.cuno =  :bcstp001.cuno and 
 	  	 tppin010._index1 =  {tppin120.cprj,tppin120.cuno,tppin120.sera}  
 	 order by tppin120.cmbc  
 	 selectdo  
 	 	 var.cisli235.invt =  1 	 | tcinvt.adv.rec.request 
 	 	 get.defaults.for.projects( tppin120.cprj,tppin120.cuno,10 )  
 	 	 initialize.addresses()  
 	 	 get.tax.by.country.code(tppin100.ccty,  
 	 	  	 	 	  tppin120.cvat,  
 	 	 	 	 	  new.txct,  
 	 	 	 	 	  new.txid,  
 	 	 	 	 	  err.mess )  
 	 	 if not calculate.amount.in.other.currency(tppin120.amnt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 	 var.pin120.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 	 var.pin120.sli235.amth =  0  
 	 	 endif  
 	 	  if not calculate.amount.in.other.currency( tppin120.amvt,tppin100.rats,tppin100.ratf,  
 	 	  	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	  	 var.pin120.sli235.txah =  o.amth  
 	 	  else  
 	 	  	 var.pin120.sli235.txah =  0  
 	 	  endif  
 	 	  tax.base.120 =  calc.tax.base.amount( tppin100.cpay, tppin120.amnt, tppin120.amdi,  
 	 	  	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	  
 | 	 	  dump 2 	 	 	 	 | #  ravi.o  
 	 	  dump.cisli235.tppin120()  
							|#ravi_array.so
| 	 	  update.fields.bcsci002.cisli206.cisli209( tppin120.ninv, new.txid,tppin120.amvt,  
| 	 	  	 	 	 	 tax.base.120,tppin120.cuno ) 	 	  
							|#ravi_array.eo
 	 	   
 	  endselect  

}
function dump.cost.plus.records()
{

	 var.cisli235.invt =  3 	|Cost Plus 	 
 	 
 	 select tppin140.* , tppin100.*
 	 from  tppin140, tppin100
 	 where tppin140._index1 =  {tppin100.stin, tppin100.ninv} and
	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.prime.cost and
 	       tppin140.cprj =  :bcstp001.cprj and
 	       tppin140.cuno =  :bcstp001.cuno and
 	       tppin140.cdmt <> tppin.cdmt.advance
 	 order by tppin140.cmbc
 	 selectdo  
 	 	 get.defaults.for.projects( tppin140.cprj,tppin140.cuno,1 )  
 	 	 initialize.addresses()  
 	 	 get.tax.by.country.code(tppin100.ccty,  
 	 	  	 	 	  tppin140.cvat,  
 	 	 	 	 	  new.txct,  
 	 	 	 	 	  new.txid,  
 	 	 	 	 	  err.mess )  
										|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency(tppin140.amnt -  tppin140.amdl,tppin100.rats,tppin100.ratf,
										|#200-101916.eo
										|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency(tppin140.amnt,tppin100.rats,tppin100.ratf,
										|#200-101916.en
	
 	 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin140.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin140.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency(tppin140.amvt,tppin100.rats,tppin100.ratf,  
	 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin140.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin140.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.140 =  calc.tax.base.amount(tppin100.cpay, tppin140.amnt -  tppin140.amdl, tppin140.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	 	 	 	 	 	 	 	 | #  ravi.en  
 | 	 	 dump 4 	 	 	 | #  ravi.o  
  	 	 dump.cisli235.tppin140()  
							|#ravi_array.so
|  	 	 update.fields.bcsci002.cisli206.cisli209(tppin140.ninv,new.txid,tppin140.amvt,  
| 	 	 	 	 	 	 	 tax.base.140,tppin140.cuno ) 	 	  
							|#ravi_array.eo

 	 	 if not isspace ( tppin140.cdoc ) then  
 	 	 	 select tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  {:tppin140.stin, :tppin140.ninv, :tppin140.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 10 	 	 | #  ravi.o  
  	 	 	 dump.cisli236.tppin140()  
 	 	 endif  
 	 endselect  
 	 
 	 
 	select tppin150.* , tppin100.*
 	from   tppin150, tppin100
 	where  tppin150._index1 = {tppin100.stin, tppin100.ninv} and  
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.prime.cost and
 	       tppin150.cprj =  :bcstp001.cprj and 
 	       tppin150.cuno =  :bcstp001.cuno and
 	       tppin150.cdmt <> tppin.cdmt.advance
 	 order by tppin150.cmbc  
	 selectdo
 	 	 get.defaults.for.projects( tppin150.cprj,tppin150.cuno,2 )  
 	 	 initialize.addresses()  
 	 	 get.tax.by.country.code(tppin100.ccty,  
 	 	 	 	 	 tppin150.cvat,  
 	 	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
								|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency(tppin150.amnt -  tppin150.amdl,tppin100.rats,tppin100.ratf,  
								|#200-101916.eo
								|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency(tppin150.amnt,tppin100.rats,tppin100.ratf,  
								|#200-101916.en
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin150.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin150.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency(tppin150.amvt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin150.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin150.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.150 =  calc.tax.base.amount(tppin100.cpay, tppin150.amnt -  tppin150.amdl, tppin150.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	 	 	 	 	 	 	 | #  ravi.en  
| 	 	 dump 5 	 	 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin150 ()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209 ( tppin150.ninv,new.txid,tppin150.amvt,  
| 	 	 	 	 	 	 tax.base.150,tppin150.cuno ) 	 	  
							|#ravi_array.eo
 	 	  
 	 	 if not isspace ( tppin150.cdoc ) then  
 	 	 	 select 	 tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  { :tppin150.stin, :tppin150.ninv, :tppin150.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 11 	 	 	 | #  ravi.o  
 	 	 	 dump.cisli236.tppin150()  
 	 	 endif  
 	 endselect  


 	select tppin160.* , tppin100.*
 	from   tppin160, tppin100
 	where  tppin160._index1 = {tppin100.stin, tppin100.ninv} and
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.prime.cost and
 	       tppin160.cprj =  :bcstp001.cprj and 
 	       tppin160.cuno =  :bcstp001.cuno and
 	       tppin160.cdmt <> tppin.cdmt.advance
 	 order by tppin160.cmbc  
 	 selectdo  
 	 	 get.defaults.for.projects( tppin160.cprj,tppin160.cuno,3 )  
 	 	 initialize.addresses()  
 	 	 get.tax.by.country.code(tppin100.ccty,  
 	 	 	 	 	 tppin160.cvat,  
 	 	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
									|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency( tppin160.amnt -  tppin160.amdl,tppin100.rats,tppin100.ratf,  
									|#200-101916.eo
									|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency( tppin160.amnt,tppin100.rats,tppin100.ratf,  
									|#200-101916.en

 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin160.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin160.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency( tppin160.amvt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin160.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin160.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.160 =  calc.tax.base.amount ( tppin100.cpay, tppin160.amnt -  tppin160.amdl, tppin160.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	 	 	 	 	 	 	 | #  ravi.en  
| 	 	 dump 6 	 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin160()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209( tppin160.ninv,new.txid,tppin160.amvt,  
| 	 	 	 	 	 	 tax.base.160,tppin160.cuno ) 	 	  
							|#ravi_array.eo
 	 	  
 	 	 if not isspace( tppin160.cdoc ) then  
 	 	 	 select tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  { :tppin160.stin, :tppin160.ninv, :tppin160.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 12 	 	 	 | #  ravi.o  
 	 	 	 dump.cisli236.tppin160()  
 	 	 endif  
 	 endselect  
 	 	  
 	 	 | *  Cost Plus -  Subcontracting  
 	 	 
 	 	 
 	select tppin170.* , tppin100.*
 	from   tppin170, tppin100
 	where  tppin170._index1 = {tppin100.stin, tppin100.ninv} and
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.prime.cost and
 	       tppin170.cprj =  :bcstp001.cprj and 
 	       tppin170.cuno =  :bcstp001.cuno and
 	       tppin170.cdmt <> tppin.cdmt.advance
 	 order by tppin170.cmbc  
 	 selectdo
 	 	 get.defaults.for.projects( tppin170.cprj,tppin170.cuno,4 )  
 	 	 initialize.addresses()  
 	 	 get.tax.by.country.code( tppin100.ccty,  
 	 	 	 	 	 tppin170.cvat,  
 	 	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
									|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency( tppin170.amnt -  tppin170.amdl,tppin100.rats,tppin100.ratf,  
									|#200-101916.eo
									|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency( tppin170.amnt,tppin100.rats,tppin100.ratf,  
									|#200-101916.en

 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin170.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin170.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency( tppin170.amvt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin170.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin170.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.170 =  calc.tax.base.amount( tppin100.cpay, tppin170.amnt -  tppin170.amdl, tppin170.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	 	 	 	 	 	 	 | #  ravi.en  
| 	 	 dump 7 	 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin170 ()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209( tppin170.ninv,new.txid,tppin170.amvt,  
| 	 	 	 	 	 	 tax.base.170,tppin170.cuno ) 	 	  
							|#ravi_array.eo
 	 	  
 	 	 if not isspace(tppin170.cdoc) then  
 	 	 	 select 	 tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  { :tppin170.stin, :tppin170.ninv, :tppin170.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 13 	 	 	 | #  ravi.o  
 	 	 	 dump.cisli236.tppin170 ()  
 	 	 endif  
 	 endselect  
 	 	  
 	 | *  Cost Plus -  Sundry Cost  
 	 	 
 	select tppin180.* , tppin100.*
 	from   tppin180, tppin100
 	where  tppin180._index1 = {tppin100.stin, tppin100.ninv} and
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.prime.cost and
 	       tppin180.cprj =  :bcstp001.cprj and 
 	       tppin180.cuno =  :bcstp001.cuno and
 	       tppin180.cdmt <> tppin.cdmt.advance
 	 order by tppin180.cmbc  
 	 selectdo   	 	 
 	 	 get.defaults.for.projects ( tppin180.cprj,tppin180.cuno,5 )  
 	 	 initialize.addresses ()  
 	 	 get.tax.by.country.code ( tppin100.ccty,  
 	 	 	 	 	 tppin180.cvat,  
 	 	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
									|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency ( tppin180.amnt -  tppin180.amdl,tppin100.rats,tppin100.ratf,  
									|#200-101916.eo
									|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency ( tppin180.amnt,tppin100.rats,tppin100.ratf,  
									|#200-101916.en

 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin180.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin180.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency ( tppin180.amvt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin180.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin180.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.180 =  calc.tax.base.amount ( tppin100.cpay, tppin180.amnt -  tppin180.amdl, tppin180.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
| 	 	 dump 8 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin180 ()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209 ( tppin180.ninv,new.txid,tppin180.amvt,  
| 	 	 	 	 	 	 tax.base.180,tppin180.cuno ) 	 	  
							|#ravi_array.eo
 	 	 if not isspace ( tppin180.cdoc ) then  
 	 	 	 select tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  { :tppin180.stin, :tppin180.ninv, :tppin180.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 14 	 	 	 | #  ravi.o  
 	 	 	 dump.cisli236.tppin180 ()  
 	 	 endif
 	 endselect

}
function dump.extension.records()
{

	 var.cisli235.invt =  4 	 | tcinvt.extension	 
 	 
 	 select tppin140.* , tppin100.*
 	 from  tppin140, tppin100
 	 where tppin140._index1 =  {tppin100.stin, tppin100.ninv} and
	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.settlement and
 	       tppin140.cprj =  :bcstp001.cprj and
 	       tppin140.cuno =  :bcstp001.cuno and
 	       tppin140.cdmt <> tppin.cdmt.advance
 	 order by tppin140.cmbc
 	 selectdo  
 	 	 get.defaults.for.projects( tppin140.cprj,tppin140.cuno,1 )  
 	 	 initialize.addresses()  
 	 	 get.tax.by.country.code(tppin100.ccty,  
 	 	  	 	 	  tppin140.cvat,  
 	 	 	 	 	  new.txct,  
 	 	 	 	 	  new.txid,  
 	 	 	 	 	  err.mess )  
									|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency(tppin140.amnt -  tppin140.amdl,tppin100.rats,tppin100.ratf,
									|#200-101916.eo
									|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency(tppin140.amnt,tppin100.rats,tppin100.ratf,
									|#200-101916.en
 	 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin140.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin140.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency(tppin140.amvt,tppin100.rats,tppin100.ratf,  
	 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin140.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin140.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.140 =  calc.tax.base.amount(tppin100.cpay, tppin140.amnt -  tppin140.amdl, tppin140.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	 	 	 	 	 	 	 	 | #  ravi.en  
 | 	 	 dump 4 	 	 	 | #  ravi.o  
  	 	 dump.cisli235.tppin140()  
							|#ravi_array.so
|  	 	 update.fields.bcsci002.cisli206.cisli209(tppin140.ninv,new.txid,tppin140.amvt,  
| 	 	 	 	 	 	 	 tax.base.140,tppin140.cuno ) 	 	  
							|#ravi_array.eo
 	 	 if not isspace ( tppin140.cdoc ) then  
 	 	 	 select tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  { :tppin140.stin, :tppin140.ninv, :tppin140.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 10 	 	 | #  ravi.o  
  	 	 	 dump.cisli236.tppin140()  
 	 	 endif  
 	 endselect  
 	 
 	 
 	select tppin150.* , tppin100.*
 	from   tppin150, tppin100
 	where  tppin150._index1 = {tppin100.stin, tppin100.ninv} and  
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.settlement and
 	       tppin150.cprj =  :bcstp001.cprj and 
 	       tppin150.cuno =  :bcstp001.cuno and
 	       tppin150.cdmt <> tppin.cdmt.advance
 	 order by tppin150.cmbc  
	 selectdo
 	 	 get.defaults.for.projects( tppin150.cprj,tppin150.cuno,2 )  
 	 	 initialize.addresses()  
 	 	 get.tax.by.country.code(tppin100.ccty,  
 	 	 	 	 	 tppin150.cvat,  
 	 	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
								|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency(tppin150.amnt -  tppin150.amdl,tppin100.rats,tppin100.ratf,  
								|#200-101916.eo
								|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency(tppin150.amnt,tppin100.rats,tppin100.ratf,  
								|#200-101916.en


 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin150.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin150.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency(tppin150.amvt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin150.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin150.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.150 =  calc.tax.base.amount(tppin100.cpay, tppin150.amnt -  tppin150.amdl, tppin150.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	 	 	 	 	 	 	 | #  ravi.en  
| 	 	 dump 5 	 	 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin150 ()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209 ( tppin150.ninv,new.txid,tppin150.amvt,  
| 	 	 	 	 	 	 tax.base.150,tppin150.cuno ) 	 	  
							|#ravi_array.eo
 	 	  
 	 	 if not isspace ( tppin150.cdoc ) then  
 	 	 	 select 	 tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  { :tppin150.stin, :tppin150.ninv, :tppin150.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 11 	 	 	 | #  ravi.o  
 	 	 	 dump.cisli236.tppin150 ()  
 	 	 endif  
 	 endselect  


 	select tppin160.* , tppin100.*
 	from   tppin160, tppin100
 	where  tppin160._index1 = {tppin100.stin, tppin100.ninv} and
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.settlement and
 	       tppin160.cprj =  :bcstp001.cprj and 
 	       tppin160.cuno =  :bcstp001.cuno and
 	       tppin160.cdmt <> tppin.cdmt.advance 	       
 	 order by tppin160.cmbc  
 	 selectdo  
 	 	 get.defaults.for.projects( tppin160.cprj,tppin160.cuno,3 )  
 	 	 initialize.addresses()  
 	 	 get.tax.by.country.code( tppin100.ccty,  
 	 	 	 	 	 tppin160.cvat,  
 	 	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
									|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency( tppin160.amnt -  tppin160.amdl,tppin100.rats,tppin100.ratf,  
									|#200-101916.eo
									|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency( tppin160.amnt,tppin100.rats,tppin100.ratf,  
									|#200-101916.en

 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  

 	 	 	 var.pin160.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin160.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency( tppin160.amvt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin160.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin160.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.160 =  calc.tax.base.amount ( tppin100.cpay, tppin160.amnt -  tppin160.amdl, tppin160.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	 	 	 	 	 	 	 | #  ravi.en  
| 	 	 dump 6 	 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin160 ()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209 ( tppin160.ninv,new.txid,tppin160.amvt,  
| 	 	 	 	 	 	 tax.base.160,tppin160.cuno ) 	 	  
							|#ravi_array.eo
 	 	  
 	 	 if not isspace ( tppin160.cdoc ) then  
 	 	 	 select tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  { :tppin160.stin, :tppin160.ninv, :tppin160.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 12 	 	 	 | #  ravi.o  
 	 	 	 dump.cisli236.tppin160 ()  
 	 	 endif  
 	 endselect  
 	 	  
 	 	 | *  Cost Plus -  Subcontracting  
 	 	 
 	 	 
 	select tppin170.* , tppin100.*
 	from   tppin170, tppin100
 	where  tppin170._index1 = {tppin100.stin, tppin100.ninv} and
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.settlement and
 	       tppin170.cprj =  :bcstp001.cprj and 
 	       tppin170.cuno =  :bcstp001.cuno and
 	       tppin170.cdmt <> tppin.cdmt.advance 	       
 	 order by tppin170.cmbc  
 	 selectdo
 	 	 get.defaults.for.projects( tppin170.cprj,tppin170.cuno,4 )  
 	 	 initialize.addresses()  
 	 	 get.tax.by.country.code( tppin100.ccty,  
 	 	 	 	 	 tppin170.cvat,  
 	 	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
									|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency( tppin170.amnt -  tppin170.amdl,tppin100.rats,tppin100.ratf,  
									|#200-101916.eo
									|#200-101916.sn
	 	 if not calculate.amount.in.other.currency( tppin170.amnt,tppin100.rats,tppin100.ratf,  
									|#200-101916.en
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin170.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin170.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency( tppin170.amvt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin170.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin170.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.170 =  calc.tax.base.amount( tppin100.cpay, tppin170.amnt -  tppin170.amdl, tppin170.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	 	 	 	 	 	 	 | #  ravi.en  
| 	 	 dump 7 	 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin170 ()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209( tppin170.ninv,new.txid,tppin170.amvt,  
| 	 	 	 	 	 	 tax.base.170,tppin170.cuno ) 	 	  
							|#ravi_array.eo
 	 	  
 	 	 if not isspace(tppin170.cdoc) then  
 	 	 	 select 	 tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  { :tppin170.stin, :tppin170.ninv, :tppin170.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 13 	 	 	 | #  ravi.o  
 	 	 	 dump.cisli236.tppin170 ()  
 	 	 endif  
 	 endselect  
 	 	  
 	 | *  Cost Plus -  Sundry Cost  
 	 	 
 	select tppin180.* , tppin100.*
 	from   tppin180, tppin100
 	where  tppin180._index1 = {tppin100.stin, tppin100.ninv} and
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.settlement and
 	       tppin180.cprj =  :bcstp001.cprj and 
 	       tppin180.cuno =  :bcstp001.cuno and
 	       tppin180.cdmt <> tppin.cdmt.advance 	       
 	 order by tppin180.cmbc  
 	 selectdo   	 	 
 	 	 get.defaults.for.projects ( tppin180.cprj,tppin180.cuno,5 )  
 	 	 initialize.addresses ()  
 	 	 get.tax.by.country.code ( tppin100.ccty,  
 	 	 	 	 	 tppin180.cvat,  
 	 	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
										|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency ( tppin180.amnt -  tppin180.amdl,tppin100.rats,tppin100.ratf,  
										|#200-101916.eo
										|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency ( tppin180.amnt,tppin100.rats,tppin100.ratf,  
										|#200-101916.en

 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin180.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin180.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency ( tppin180.amvt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin180.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin180.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.180 =  calc.tax.base.amount( tppin100.cpay, tppin180.amnt -  tppin180.amdl, tppin180.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
| 	 	 dump 8 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin180()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209( tppin180.ninv,new.txid,tppin180.amvt,  
| 	 	 	 	 	 	 tax.base.180,tppin180.cuno ) 	 	  
							|#ravi_array.eo
 	 	 if not isspace( tppin180.cdoc ) then  
 	 	 	 select tppin105.txtn  
 	 	 	 from 	 tppin105  
 	 	 	 where 	 tppin105._index1 =  { :tppin180.stin, :tppin180.ninv, :tppin180.cdoc}  
 	 	 	 selectdo  
 	 	 	 	 var.cisli236.ntxt =  tppin105.txtn  
 	 	 	 endselect  
| 	 	 	 dump 14 	 	 	 | #  ravi.o  
 	 	 	 dump.cisli236.tppin180 ()  
 	 	 endif
 	 endselect

}
function dump.installment.records()
{
	select tppin130.*  , tppin020.cpro  , tppin100.*, tppdm600.pcwa
 	from   tppin130, tppin020  , tppin100, tppdm600
 	where  tppin130._index1 =  {tppin100.stin, tppin100.ninv} and  
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.instalment and
 	       tppin130.cprj =  :bcstp001.cprj and 
 	       tppin130.cuno =  :bcstp001.cuno and 
 	       tppin130.cdmt <> tppin.cdmt.advance and	       
 	       tppin020._index1 =  {tppin130.cprj,tppin130.cuno,tppin130.nins} and 
	       tppdm600._index1 = {tppin130.cprj}	
 	 order by tppin130.cmbc  
 	 selectdo  
 	 	 var.cisli235.invt =  2 	 | tcinvt.installment 	  
 	  	 get.defaults.for.projects ( tppin130.cprj,tppin130.cuno,10 )  
 	  	 initialize.addresses ()  
 	  	 get.tax.by.country.code(tppin100.ccty,  
 	  	 	 	 	 tppin130.cvat,  
 	  	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
 	 	 if not calculate.amount.in.other.currency(tppin130.amnt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 	   tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 	 var.pin130.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 	 var.pin130.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency(tppin130.amvt,tppin100.rats,tppin100.ratf,  
	 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	  	 var.pin130.sli235.txah =  o.amth  
 	 	 else  
 	 	  	 var.pin130.sli235.txah =  0  
 	 	 endif  
 	 	 tax.base.130 =  calc.tax.base.amount(tppin100.cpay, tppin130.amnt, tppin130.amdi,  
 	 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 | 	 	 	 dump 3 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin130 ()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209(tppin130.ninv,new.txid,tppin130.amvt,  
| 	 	 	 	 	 	 	 tax.base.130,tppin130.cuno ) 	 	  
							|#ravi_array.eo
 	 endselect
}

function dump.unit.rate.records()
{
 	 var.cisli235.invt =  5 	 | tcinvt.unit.rate   	 
 	 
 	select tppin190.* , tppin100.* , tppdm600.pcwa
 	from   tppin190, tppin100, tppdm600
 	where  tppin190._index1 = {tppin100.stin, tppin100.ninv} and  
 	       tppin100.stin =  tppdm.stin.final and
 	       tppin100.cfpo =  tppdm.yeno.yes and  
 	       tppin100.post =  tppdm.yeno.yes and
 	       tppin100.invt =  tppdm.invt.unit.rate and
 	       tppin190.cprj =  :bcstp001.cprj and 
 	       tppin190.cuno =  :bcstp001.cuno and
 	       tppin190.cdmt <> tppin.cdmt.advance and 	       
 	       tppdm600._index1 = {tppin190.cprj}
 	 order by tppin190.cmbc
 	 selectdo  
 	 	 get.defaults.for.projects ( tppin190.cprj,tppin190.cuno,10 )  
 	 	 initialize.addresses ()  
 	 	 get.tax.by.country.code ( tppin100.ccty,  
 	 	 	 	 	 tppin190.cvat,  
 	 	 	 	 	 new.txct,  
 	 	 	 	 	 new.txid,  
 	 	 	 	 	 err.mess )  
										|#200-101916.so
| 	 	 if not calculate.amount.in.other.currency ( tppin190.amnt -  tppin190.amdl,tppin100.rats,tppin100.ratf,  
										|#200-101916.eo
										|#200-101916.sn
 	 	 if not calculate.amount.in.other.currency ( tppin190.amnt,tppin100.rats,tppin100.ratf,  
										|#200-101916.en
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin190.sli235.amth =  o.amth  
 	 	 else  
 	 	 	 var.pin190.sli235.amth =  0  
 	 	 endif  
 	 	 if not calculate.amount.in.other.currency ( tppin190.amvt,tppin100.rats,tppin100.ratf,  
 	 	 	 	 	 	 tppin100.ccur,o.amth, err.mess ) then  
 	 	 	 var.pin190.sli235.txah =  o.amth  
 	 	 else  
 	 	 	 var.pin190.sli235.txah =  0  
 	 	 endif  
 	 	  
 	 	 tax.base.190 =  calc.tax.base.amount ( tppin100.cpay, tppin190.amnt -  tppin190.amdl, tppin190.amdi,  
 	 	 	 	 	 	 tppin100.ccrs, tppin100.ccur )  
 	 	 	 	 	 	 	 	 | #  ravi.en  
| 	 	 dump 9 	 	 	 | #  ravi.o  
 	 	 dump.cisli235.tppin190 ()  
							|#ravi_array.so
| 	 	 update.fields.bcsci002.cisli206.cisli209 ( tppin190.ninv,new.txid,tppin190.amvt,  
| 	 	 	 	 	 	 tax.base.190,tppin190.cuno ) 	 	  
							|#ravi_array.eo
 	 endselect  

}

function fill.cutoff.date()

{  	
	 select tssma230.psdt  
 	 from 	 tssma230  
 	 where 	 tssma230._index1 =  {:tssma221.bkyr}  
 	 as set with 1 rows  
 	 selectdo  
 	 	 var.cisli265.codt =  tssma230.psdt (tssma221.cper)  
 	 endselect  

}

function reset.variables.cisli205()
{


	var.cisli205.sfcp = get.compnr()
	var.cisli205.ityp = ""
	var.cisli205.idoc = 0
	var.cisli205.scmp = 0
	var.cisli205.brid = 0
	var.cisli205.doct = 0
	var.cisli205.stat = 0
	var.cisli205.cpay = ""
	var.cisli205.ccrs = ""
	var.cisli205.paym = ""
	var.cisli205.bpid = ""
	var.cisli205.cadr = ""
	var.cisli205.cofc = ""
	var.cisli205.txct = ""
	var.cisli205.fcrt = 0
	var.cisli205.niiv = 0
	var.cisli205.nmsi = 0
	var.cisli205.nprj = 0
	var.cisli205.nslo = 0
	var.cisli205.nsvo = 0

	var.cisli205.nsco = 0
	var.cisli205.doct = 0
	var.cisli205.idat = 0
	var.cisli205.ccur = ""
	var.cisli205.amti = 0.0
	|var.cisli205.amth = 0.0
	var.cisli205.txai = 0.0
	|var.cisli205.txah = 0.0
	var.cisli205.stai = 0.0
	|var.cisli205.stah = 0.0
	|var.cisli205.rate = 0.0
	|var.cisli205.ratf = 0
	var.cisli205.scdt = 0

	var.cisli205.scai = 0.0
	|var.cisli205.scah = 0.0
	var.cisli205.gdai = 0.0
	var.cisli205.csai = 0.0

	var.cisli205.rmai = 0.0
	var.cisli205.d1ai = 0.0
	|var.cisli205.d1ah = 0.0
	var.cisli205.d1dt = 0.0
	var.cisli205.d2ai = 0.0
	|var.cisli205.d2ah = 0.0
	var.cisli205.d2dt = 0.0
	var.cisli205.d3ai = 0.0
	|var.cisli205.d3ah = 0.0
	var.cisli205.d3dt = 0.0
	var.cisli205.nreb = 0
	var.cisli205.dcai = 0.0
	var.cisli205.tbai = 0.0
	tbai = 0.0
	
	|*MCR - s
	for i = 1 to 3
		var.cisli205.amth(i) = 0.0
		var.cisli205.txah(i) = 0.0
		var.cisli205.stah(i) = 0.0
		var.cisli205.scah(i) = 0.0
		var.cisli205.rate(i) = 0.0
		var.cisli205.ratf(i) = 0
		var.cisli205.d1ah(i) = 0.0
		var.cisli205.d2ah(i) = 0.0
		var.cisli205.d3ah(i) = 0.0
	endfor	
	|*MCR - e  
	
}

function reset.variables.1()
{
	test.count = 0
	var.shift.amti = 0.0
	|var.shift.amth = 0.0
	var.net.amti = 0.0
	|var.net.amth = 0.0
	var.tax.amti = 0.0
	|var.tax.amth = 0.0
	
	|*MCR - s
	for i = 1 to 3
		var.shift.amth(i) = 0.0
		var.net.amth(i) = 0.0
		var.tax.amth(i) = 0.0
	endfor
	|*MCR - e	

}

function reset.variables()
{
	test.count = 0
	temp.prev.ttyp = ""
	temp.prev.ninv = 0
	var.shift.amti = 0.0
	|var.shift.amth = 0.0
	var.net.amti = 0.0
	|var.net.amth = 0.0
	var.tax.amti = 0.0
	|var.tax.amth = 0.0
	
	old.shift.amti = 0.0
	|old.shift.amth = 0.0
	old.net.amti = 0.0
	|old.net.amth = 0.0
	old.tax.amti = 0.0
	|old.tax.amth = 0.0
	cnt = 0
	count.records = 0
		
	|*MCR - s
	for i = 1 to 3	
		var.shift.amth(i) = 0.0	
		var.net.amth(i) = 0.0
		var.tax.amth(i) = 0.0
		old.shift.amth(i) = 0.0
		old.net.amth(i) = 0.0
		old.tax.amth(i) = 0.0
	endfor
	|*MCR - e

}

function count.records.same.invoice()
{
	
	cnt = 0

	if tfacr510.iist <> tfacr.iist.selected then

 	 	select tfacr510.*
	 	 from tfacr510  
	 	 where tfacr510._index3 =  {:prev.cuno,  
  		 	 	 	    :prev.ityp,  
 	 		 	 	    :prev.idoc}  
	 	 order by tfacr510._index3	 	 	    
 		 selectdo
 	 		cnt = cnt + 1
	 	 endselect	 	
	endif


}

function check.for.records.with.different.invoice()
 {
	if (temp.prev.ttyp <> prev.ttyp) or 
	   (temp.prev.ninv <> prev.ninv) then
		temp.prev.ttyp = prev.ttyp
		temp.prev.ninv = prev.ninv	
		count.records = count.records + test.count
		old.net.amti = old.net.amti + var.net.amti
		|*MCR - s
		for i = 1 to 3
			|old.net.amth = old.net.amth + var.net.amth
			old.net.amth(i) = old.net.amth(i) + var.net.amth(i)
		endfor
		|*MCR - e

		old.tax.amti = old.tax.amti + var.tax.amti

		|*MCR - s
		for i = 1 to 3
			|old.tax.amth = old.tax.amth + var.tax.amth
			old.tax.amth(i) = old.tax.amth(i) + var.tax.amth(i)
		endfor
		|*MCR - e


		if tcmcs036.tvat = tctvat.shifted then
			old.shift.amti = old.shift.amti + var.shift.amti

			|*MCR - s
			for i = 1 to 3
				|old.shift.amth = old.shift.amth + var.shift.amth
				old.shift.amth(i) = old.shift.amth(i) + var.shift.amth(i)
			endfor
			|*MCR - e	
		endif
	endif			   	
  
 }

function restore.pointer.tfacr510() 
{
	select tfacr510.*   
 	from tfacr510  
 	where tfacr510._index1 =  {:prev.ttyp,  
 	  	 	 	     :prev.ninv,  
 	 	 	 	     :prev.line,
				     :prev.tdoc,	
				     :prev.docn,
				     :prev.lino
			   	    }				
	as set with 1 rows
	selectdo
	endselect
}  
									|binda25-06.o
						
|function restore.pointer.tdcms050()
|{
|	select tdcms050.*
|	from   tdcms050
|	where tdcms050._index1 = {:keep.type,:keep.orno,:keep.pono,
|				      :keep.reln,:keep.sern}
|	as set with 1 rows
|	selectdo
|	endselect
|}
|function restore.pointer.tssma310()
|{
|	select tssma310.*
|	from   tssma310
|	where  tssma310._index1 = {:keep.orno,:keep.seqn}
|	as set with 1 rows
|	selectdo
|	endselect
|}
|function restore.pointer.tssma221()
|{
|	select tssma221.*
|	from   tssma221
|	where  tssma221._index1 = {:keep.ccon,:keep.bkyr,:keep.seqn}
|	as set with 1 rows
|	selectdo
|	endselect
|}
|function restore.pointer.tdsls045()
|{
|	select tdsls045.*
|	from   tdsls045
|	where  tdsls045._index1 = {:keep.orno,:keep.pono,:keep.srnb}
|	as set with 1 rows
|	selectdo
|	endselect
|}
|
|function store.index.fields.rebates()
| {
| 	keep.type = tdcms050.type
| 	keep.orno = tdcms050.orno
| 	keep.pono = tdcms050.pono
| 	keep.reln = tdcms050.reln
| 	keep.sern = tdcms050.sern
|  
| }
|
|function store.index.fields.service.orders()
| {
| 	keep.orno = tssma310.orno
| 	keep.seqn = tssma310.seqn
| 	  
| }
| function store.index.fields.service.contracts()
| {
| 	keep.ccon = tssma221.ccon
| 	keep.bkyr = tssma221.bkyr
| 	keep.seqn = tssma221.seqn  
| }
|function store.index.fields.sales.orders()
| {
| 	keep.orno = tdsls045.orno
| 	keep.pono = tdsls045.pono
| 	keep.srnb = tdsls045.srnb  
| }
									|binda25-06.o
function domain tcbool installment.or.proforma.invoice()
{
	if tdsls045.pono = 0 then
		|* Installment Line.
		return(true)
	endif

	if tcmcs042.pinv = tcyesno.yes then
		|* Proforma invoice part of order type.
		return(true)
	endif

	return(false)
}

function insert.records.bcstp001(
|				domain tppdm.aalc i.amount,
|				domain tppdm.aajc i.disc.amount,
				domain tppdm.cprj i.cprj,
				domain tccuno i.cuno,
				domain tppdm.stin i.inv.status,
				domain tppdm.ninv  i.ninv
				)
{

 	 if save.cprj <>  i.cprj and  
 	 save.cuno <>  i.cuno then  
 	 	 insert.new.record.in.bcstp001 (i.cprj, i.cuno,  
 	 	 	 	 	 i.inv.status, i.ninv)  
 	 endif

}
function get.defaults.for.projects(domain tppdm.cprj i.cprj,
				domain tccuno i.cuno,
				domain tppdm.serf i.cprj.line)

{
	 initialize.cisli206.variables() 	 	 	 | #  ravi.n  
	 if save.cprj <>  i.cprj and  
 	 	 save.cuno <>  i.cuno then  
 	 	 save.cprj =  i.cprj  
 	 	 save.cuno =  i.cuno  
 	 	 var.cisli235.piln =  i.cprj.line 
 	 else 	  
 	  	 var.cisli235.piln =  var.cisli235.piln +  10  
 	 endif  
 	 get.business.partner ( i.cuno,bcstc.orgn.cuno,err.mess )  
 	 get.default.sls.type.for.tfacr ( var.cisli235.styp, err.mess )  
 	 get.addresses ( "tppdm600",i.cprj,err.mess )                        

}
function initialize.addresses()
{
	 var.cisli235.sfad =  bcstc004.cadr  
 	 var.cisli206.sfcy =  bcstc004.ccty 	 	 	 | #  ravi.n  
 	 if tccom999.txpr =  tcyesno.yes then  
 	 	 var.cisli235.stad =  bcstc004.cadr  
 	 	 var.cisli206.stcy =  bcstc004.ccty 	 	 | #  ravi.n 	  
 	 else  
 	 	 var.cisli235.stad =  ""                                                                                                                                                                                          
	 	 var.cisli206.stcy =  ""                                                                         	                                                                                                                                                  	                                                                                                                                                                                                           
 	 endif 	  

}

function initialize.variables()
{
 	 var.cisli220.itoa =  ""
	 var.cisli220.fcrt = 0
	 var.cisli220.stoa =  ""	|#200-98679.n
 	 var.tfacr400.cuno =  "" |  this var is used to send as an argument to bcstc004 table
 	 var.temp1 =  ""            
 	 var.cisli220.crep =  ""                                                                             
 	 var.cisli225.lpai =  0.0  
 	 |var.cisli225.lpah =  0.0  
	 var.cisli225.txai = 0.0
	 |var.cisli225.txah = 0.0
	 var.shift.amti = 0.0
	 |var.shift.amth = 0.0
 	 strlen1 =  0  
 	 strlen2 =  0  
 	total.vata = 0.0
	shift.vata = 0.0
	total.amnt = 0.0
	nett.amnt = 0.0
	amnt.cvat = .0
	var.net.amti = 0.0
	|var.net.amth = 0.0
	var.tax.amti = 0.0
	|var.tax.amth = 0.0
	var.lps.amti = 0.0
	|var.lps.amth = 0.0
	temp.scai = 0.0	
	new.txct = ""
	new.txid = ""
	
	|*MCR - s
	for i = 1 to 3	
		var.cisli225.lpah(i) =  0.0 
		var.cisli225.txah(i) = 0.0 
		var.shift.amth(i) = 0.0
		var.lps.amth(i) = 0.0
		var.net.amth(i) = 0.0
		var.tax.amth(i) = 0.0
	endfor	
	|*MCR - e	
}

|**********************CISLI220,CISLI225,CISLI226*******************************
function fill.fcrt.and.business.partner.cisli220()
{
 	 select tfacr200.spot, tfacr200.docd, tfacr200._index1
 	 from tfacr200  
 	 where tfacr200._index1 =  {:tfacr400.ttyp,:tfacr400.ninv,0, "",0,0}                                                                                                                                                          
 	 as set with 1 rows  
 	 selectdo  
 	 	 if (tfacr200.spot =  tcyesno.yes) then  
 	 	 	 var.cisli220.fcrt =  1  
 	 	 	 else if (tfacr200.spot =  tcyesno.no) then  
 	 	 	 	 var.cisli220.fcrt =  6  
 	 	 	 endif  
 	 	 endif  
 	 	 var.cisli220.rdat =  tfacr200.docd  
 	 selectempty  
 	 	 var.cisli220.fcrt =  6  
 	 	 var.cisli220.rdat =  tfacr400.invd  
 	 endselect  
 	 if get.business.partner(tfacr400.cuno,bcstc.orgn.cuno,err.mess) then  

 	 	 var.cisli220.bpid =  ""                                                                                                                                                                                        
 	 else  
 	 	 var.cisli220.bpid =  bcstc008.bpid  
 	 endif  


}
function fill.sales.representative.cisli220()
{
 	 select tccom010.crep  
 	 from tccom010  
 	 where tccom010.cuno =  :tfacr400.cuno  
 	 as set with 1 rows  
 	 selectdo  


 	 	 if tccom010.crep <>  0 then  
 	 	 	 var.cisli220.crep =  str$(tccom010.crep)  
 	 	 else  
 	 	 	 var.cisli220.crep =  ""                        
 	 	 endif  
 	 selectempty  
		 var.cisli220.crep =  ""                                                
 	 endselect  

}

function	 fill.postal.address.cisli220()
{
 	 var.tfacr400.cuno =  tfacr400.cuno  
 	 strlen1 =  len(var.tfacr400.cuno)  
 	 if not isspace(tfacr400.adrs) then  
 	 	 strlen2 =  len(tfacr400.adrs)  
 	 	 var.temp1(1;strlen1) =  var.tfacr400.cuno  
 	 	 var.temp1(strlen1 +  1;strlen2) =  tfacr400.adrs  
 	 	 if not get.addresses("tccom012", var.temp1,err.mess) then  
 	 	 	 var.cisli220.itoa =  bcstc004.cadr  
			 var.cisli206.itcy = bcstc004.ccty					|#ravi.n
 	 	 endif  
 	 else  
 	 	 if not get.addresses("tccom010", var.tfacr400.cuno,err.mess) then  
 	 	 	 var.cisli220.itoa =  bcstc004.cadr  
			 var.cisli206.itcy = bcstc004.ccty |#200-98809.n
 	 	 endif  
 	 endif  
}
function fill.cisli220.stoa()								|#ravi.sn
{
	string		temp.key.cdel(9) 
domain	bccadr		var.tccom010.cadr 	

	select	tccom010.cdel
	from 	tccom010
	where	tccom010._index1 = {:tfacr400.cuno}
	as set with 1 rows
	selectdo
	endselect
	if get.addresses("tccom010", tfacr400.cuno, err.mess) then  
 		 var.tccom010.cadr =  ""                                                                              
 	else  
 	 	 var.tccom010.cadr =  bcstc004.cadr  
		 var.cisli206.stcy = bcstc004.ccty	  
 	endif 	 	 	 	  
	
	if (not isspace(tccom010.cdel)) then  
| 		temp.key.cdel(1;6) =  tccom010.cuno   |#200-98679.o
 		temp.key.cdel(1;6) =  tfacr400.cuno      |#200-98679.n
 	 	temp.key.cdel(7;9) =  tccom010.cdel  
 	 	if get.addresses("tccom013", temp.key.cdel, err.mess) then  
 	 		var.cisli220.stoa =  var.tccom010.cadr  
 	 	else  
 	 		var.cisli220.stoa =  bcstc004.cadr  
			var.cisli206.stcy = bcstc004.ccty	  
 	 	endif  

	 else					|#200-98679.sn
		var.cisli220.stoa = var.tccom010.cadr 	|#200-98679.en		
| 	 else  
|		 if get.addresses("tccom010", tfacr400.cuno, err.mess) then  
| 		 	 var.cisli220.stoa =  ""                                                                              
| 	 	 else  
| 	 	 	var.cisli220.stoa =  bcstc004.cadr  
|			var.cisli206.stcy = bcstc004.ccty	  
|	 	 endif 	 	 	 
 	 endif  
}												|#ravi.en

function fill.tax.amount.cisli225()
{
	read.tcmcs036(new.txct, new.txid)
	add.totals.cisli225()
	 
}

|function check.vat.details()

|{
|	select 	tcmcs036.* 
|	from 	tcmcs036
|	where	tcmcs036._index1 = {:tfacr401.vatc,:tfacr401.cvat}
|	selectdo
|		if tcmcs036.tvat = tctvat.shifted then
|			tcmcs036.gnen = tcgnen.nett
|		endif
|
|	endselect
|	
|}

function add.totals.cisli225()
{
	calculate.line.amounts.cisli225()

	if tcmcs036.tvat = tctvat.shifted then
		shift.vata = shift.vata + amnt.cvat
		nett.amnt = tfacr401.amnt
		
	endif							|bin.n

|		total.amnt = total.amnt + nett.amnt 		|bin.so
|	else							|bin.eo
		total.amnt = total.amnt + nett.amnt + amnt.cvat
|	endif							|bin.o
	total.vata = total.vata + amnt.cvat

	var.cisli225.txai = total.vata
| *  MCR -  new changes .s										|bin.sn	
|	for i = 1 to 3 |*MCR
|		calculate.amount.in.other.currency(nett.amnt, 
|				|*MCR - s
|				|tfacr400.rate,
|				|tfacr400.ratf,  
|				fill.mc.field.double(tfacr400.rate,i),
| 	 	 		fill.mc.field.long(tfacr400.ratf,i), 	
|				|*MCR - e				
| 		 	 	tfacr400.ccur, 
|				|nett.amth,
|				nett.amth(i), |*MCR
|				err.mess)
|	endfor  |*MCR	
	for i =  1 to 3 	 	 	 	 	 	  
 	 	 calculate.amount.in.home.currency.variables (nett.amnt,	 	 	  
 	 	 tfacr400.ccur,
		 fill.mc.field.double(tfacr400.rate,i),
 		 fill.mc.field.long(tfacr400.ratf,i),
 	 	 i,  
 	 	 nett.amth(i), 	 	 	 	 	 	 	  
 	 	 err.mess) 	 	 	 	 	 	  
 	endfor 	 	 	 	 	 	  
 | *  MCR -  new changes .e 
					|bin.en
| *  MCR -  new changes .e 					
|	for i = 1 to 3 |*MCR
|		calculate.amount.in.other.currency(var.cisli225.txai,
|				|*MCR - s
|				|tfacr400.rate,
|				|tfacr400.ratf,  
|				fill.mc.field.double(tfacr400.rate,i),
| 	 	 		fill.mc.field.long(tfacr400.ratf,i), 	
|				|*MCR - e
| 	 	 	 	tfacr400.ccur,
|				|var.cisli225.txah,
|				var.cisli225.txah(i), |*MCR 
|				err.mess)  
|	endfor |*MCR

	for i =  1 to 3 	 	 	 	 	 	  
 	 	 calculate.amount.in.home.currency.variables (var.cisli225.txai,	 	 	  
 	 	 tfacr400.ccur,
		 fill.mc.field.double(tfacr400.rate,i),
 		 fill.mc.field.long(tfacr400.ratf,i),
 	 	 i,  
 	 	 var.cisli225.txah(i), 	 	 	 	 	 	 	  
 	 	 err.mess) 	 	 	 	 	 	  
 	endfor 	 	 	 	 	 	  
| *  MCR -  new changes .e 	

| *  MCR -  new changes .s	
|	for i = 1 to 3 |*MCR		
|		calculate.amount.in.other.currency(shift.vata, 
|				|*MCR - s
|				|tfacr400.rate,
|				|tfacr400.ratf,  
|				fill.mc.field.double(tfacr400.rate,i),
| 	 	 		fill.mc.field.long(tfacr400.ratf,i), 	
|				|*MCR - e
| 	 	 	 	tfacr400.ccur, 
|				|shift.vath,
|				shift.vath(i), |*MCR
|				err.mess)  
|	endfor |*MCR
	for i =  1 to 3 	 	 	 	 	 	  
 	 	 calculate.amount.in.home.currency.variables (shift.vata,	 	 	  
 	 	 tfacr400.ccur,
		 fill.mc.field.double(tfacr400.rate,i),
 		 fill.mc.field.long(tfacr400.ratf,i),
 	 	 i,  
 	 	 shift.vath(i), 	 	 	 	 	 	 	  
 	 	 err.mess) 	 	 	 	 	 	  
 	endfor 	 	 	 	 	 	  
| *  MCR -  new changes .e 	
}

function calculate.line.amounts.cisli225()
{
								|#209190.sn
	select	tfgld011.catg
	from	tfgld011
	where	tfgld011._index1 = {:tfacr401.ttyp}
	as set with 1 rows
	selectdo
		if etol(tfgld011.catg) = 4 then
			tfacr401.quan = (tfacr401.quan)*(-1)
			tfacr401.amnt = (tfacr401.amnt)*(-1)
			tfacr401.vamt = (tfacr401.vamt)*(-1)
		endif
	endselect
	 							|#209190.en
	if tfacr401.vamt <> 0 then 
		amnt.cvat = tfacr401.vamt
		if tcmcs036.gnen = tcgnen.nett then
			nett.amnt = tfacr401.amnt
		else
			nett.amnt = tfacr401.amnt - amnt.cvat
			|# Round the amount if required
		endif
	else
		nett.amnt = tfacr401.amnt

		amnt.cvat = 0.0
	endif
								
|	calculate.amount.in.other.currency(nett.amnt, tfacr400.rate, tfacr400.ratf,  
| 		 	 	 	 	 	 	 tfacr400.ccur, nett.amth, err.mess)

|*  MCR -  new changes .s
|	for i = 1 to 3 |*MCR 							
|		calculate.amount.in.other.currency(amnt.cvat, 
|				|*MCR - s
|				|tfacr400.rate,
|				|tfacr400.ratf,  
|				fill.mc.field.double(tfacr400.rate,i),
| 	 	 		fill.mc.field.long(tfacr400.ratf,i), 	
|				|*MCR - e
| 	 	 	 	tfacr400.ccur,
|				|amnt.cvath, 
|				amnt.cvath(i), 
|				err.mess)	
|	endfor |*MCR	
	for i =  1 to 3 	 	 	 	 	 	  
 	 	 calculate.amount.in.home.currency.variables (amnt.cvat,
 	 	 tfacr400.ccur,
		 fill.mc.field.double(tfacr400.rate,i),
 		 fill.mc.field.long(tfacr400.ratf,i),
 	 	 i,  
 	 	 amnt.cvath(i), 	 	 	 	 	 	 	  
 	 	 err.mess) 	 	 	 	 	 	  
 	endfor 	 	 	 	 	 	  
|*  MCR -  new changes .e 	
}
function fill.lps.cisli225()

{
	get.crnd(tfacr400.ccur) 	

 	select tcmcs011.*   
 	from tcmcs011  
 	where tcmcs011._index1 =  {:tfacr400.ccrs}  
 	selectdo  
| 		var.cisli225.lpai =  (total.amnt - total.vata + shift.vata) * (tcmcs011.perc / 100)
 		var.cisli225.lpai =  nett.amnt * (tcmcs011.perc / 100)

		var.cisli225.lpai =  icom0001.domain.round(var.cisli225.lpai,
					"tfgld.amnt", tfacr400.ccur, 
					crnd.orno)	
|*  MCR -  new changes .e 							
|		for i = 1 to 3|*MCR	
| 			calculate.amount.in.other.currency(var.cisli225.lpai,
|				|*MCR - s
|				|tfacr400.rate,
|				|tfacr400.ratf,  
|				fill.mc.field.double(tfacr400.rate,i),
| 	 	 		fill.mc.field.long(tfacr400.ratf,i), 	
|				|*MCR - e
| 	 	 	 	tfacr400.ccur, 
|				|var.cisli225.lpah,
|				var.cisli225.lpah(i),
|				err.mess)  
|		endfor |* MCR
		for i =  1 to 3 	 	 	 	 	 	  
	 	 	 calculate.amount.in.home.currency.variables (var.cisli225.lpai,
	 	 	 tfacr400.ccur,
			 fill.mc.field.double(tfacr400.rate,i),
	 		 fill.mc.field.long(tfacr400.ratf,i),
	 	 	 i,  
	 	 	 var.cisli225.lpah(i), 	 	 	 	 	 	 	  
	 	 	 err.mess) 	 	 	 	 	 	  
	 	endfor 	 	 	 	 	 	  
|*  MCR -  new changes .e 	
	selectempty  
		var.cisli225.lpai =  0.0  
		|*MCR - s
		|var.cisli225.lpah =  0.0  
		for i = 1 to 3
			var.cisli225.lpah(i) =  0.0  
		endfor
		|*MCR - e
	endselect  
}

function get.crnd(domain tcccur i.ccur)
{
	domain	tppdm.long	retval			|#ravi.perf.sn
	domain	tppdm.long	s.table.cached
	domain	tcncmp		l.curr.comp.nr
	domain	tcbool		first.call			|#ravi.perf.en

|	select	tcmcs002.crnd:crnd.orno			|#ravi.perf.so
|	from 	tcmcs002
|	where	tcmcs002._index1 = {:i.ccur}
|	selectdo
|	endselect					|#ravi.perf.eo

	l.curr.comp.nr  = get.compnr()

	if not first.call then
                s.table.cached = bcstc.dll0017.cache.table.in.memory(
                                        "tcmcs002",
                                        l.curr.comp.nr)
                first.call = true
        endif
        
        if s.table.cached then
                |* set values to search for
                tcmcs002.ccur = i.ccur
                if not bcstc.dll0017.make.object.current(
                                "tcmcs002",
                                l.curr.comp.nr) <> 0 then
                	crnd.orno = tcmcs002.crnd        
                endif
        endif


}

function fill.status.cisli225()
{
 	 	 on case tfacr400.inst  
| 	 	 	 case tfacr.inst.open:  
| 	 	 	 	 var.cisli225.stat =  1 | On Hold  
| 	 	 	 	 break  
 	 	 	 case tfacr.inst.printed:  
 	 	 	 	 var.cisli225.stat =  5 | Printed  
 	 	 	 	 break  
 	 	 	 case tfacr.inst.posted:  
 	 	 	 	 var.cisli225.stat =  6 | Posted  
 	 	 	 	 break  
 	 	 	 case tfacr.inst.cancelled:  
 	 	 	 	 var.cisli225.stat =  2 | Cancel  
 	 	 	 	 break  
 	 	  endcase  

}

|********************* CISLI240 ***********************************************
						|#200284.sn
function domain tcbool valid.installments.present()

{

	if tdsls045.pono = 0 and tdsls040.futo <> tcfuto.no then
		|* Installment Line
		select	tdsls048.*
		from	tdsls048
		where	tdsls048._index1 = {	:tdsls045.orno,
						:tdsls045.srnb,
						0}
		as set with 1 rows
		selectdo
			return(true)
		selectempty
			return(false)
		endselect
	endif

	return(true)
}							
							
						|#200284.en



function read.sales.order.header.data()
{
	
							|binda21-07.sn
	|WR PERF statics added
	static domain tcorno s.orno

      if s.orno <> tdsls045.orno then
							|binda21-07.en


	select	tdsls040.*,
		tcmcs002.crnd, tcmcs046.lang
	from	tdsls040, tcmcs002, tcmcs046
	where	tdsls040._index1 = {:tdsls045.orno}
	and	tdsls040.ccur refers to tcmcs002
	and	tdsls040.clan refers to tcmcs046
	as set with 1 rows
	selectdo
	endselect
	
							|binda21-07.sn
	s.orno = tdsls045.orno
      endif
      							|binda21-07.en
	

}

function string convert.sales.order()
{
	static		string	l.orno(9)
	static	domain	tcorno	s.sls045.orno

	if s.sls045.orno <> tdsls045.orno then
		s.sls045.orno = tdsls045.orno
		l.orno = convert.number(bcckon.act.sls, tdsls045.orno)
	endif

	return(l.orno)
}
function string convert.service.order()
{
	static		string	l.orno(9)
	static	domain	tcorno	s.sma310.orno

	if s.sma310.orno <> tssma310.orno then
		s.sma310.orno = tssma310.orno
		l.orno = convert.number(bcckon.sma.order, tssma310.orno)
	endif

	return(l.orno)
}

function domain bcstc.bpid convert.business.partner()
{
	static	domain	bcstc.bpid	l.business.part
	static	domain	tccuno		s.sls040.cuno

	if s.sls040.cuno <> tdsls040.cuno then
		s.sls040.cuno = tdsls040.cuno
		l.business.part = get.business.partner.code(
					tdsls040.cuno,

					bcstc.orgn.cuno,
 		   	   	   	o.error.mess)
	endif

	return(l.business.part)
}

function domain bccadr convert.address()
{
	|* This function converts the BaanIV Postal Address (tdsls040.ccor)
	|* into the various addresses related to the various BP roles in Baan 6;
	|* sold-to address, pay-from address and invoice-to address.

	static	domain	bccadr		l.address
	static	domain	tcorno		s.sls045.orno


	if s.sls045.orno <> tdsls045.orno then
		s.sls045.orno = tdsls045.orno
		l.address = convert.sales.address(
					tdsls040.ccor,
					tdsls040.cuno,
					tdsls045.orno,
					0,
					1)	|* address type: postal address
	endif

	return(l.address)
}

|function long convert.cisli240.fcrt()
||* Rate Determiner
|{
|	if tdsls040.fcrt = tcfcrt.fixed then
|		return(target(tcfcrt.fixed))	|* Fixed
|	endif
|
|	if tdsls040.fcrt = tcfcrt.ddta then
|		return(target(tcfcrt.ddat))	|* Delivery Date
|	endif
|
|	if tdsls040.fcrt = tcfcrt.invd then
|		return(target(tcfcrt.docd))	|* Document Date
|	endif
|
|	return(target(tcfcrt.docd))		|* Document Date
|}

function domain	tccfrw convert.cisli240.cfrw()
|* Forwarding Agent
{
	static	domain	tccfrw	l.forwarding.agent
	static	domain	tccfrw	s.sls040.cfrw

	if s.sls040.cfrw <> tdsls040.cfrw and
	   not isspace(tdsls040.cfrw) then
		s.sls040.cfrw = tdsls040.cfrw
		if not get.carrier.for.subcontractor(
					"tc",
					tdsls040.cfrw,
					o.error.mess) then
			l.forwarding.agent = bcstc019.newc
		else
			l.forwarding.agent = ""
		endif
	endif

	return(l.forwarding.agent)
}

function long convert.cisli240.ctyn()
|* Calculate Tax
{
	domain tcyesno taxyesnoflag

	if tdsls040.futo <> tcfuto.no then
		|* Invoice by Installments
		if tdsls000.atin.4 = tcyesno.yes then
			|* Apply Tax to Installments [y/n]

			taxyesnoflag = get.tax.flag.via.bp(
			tdsls040.cuno,
			bcstc.orgn.cuno,
			err.mess)

			return(etol(taxyesnoflag))
		else
			return(etol(tcyesno.no))
		endif
	endif

	return(etol(tdsls040.cvyn))
}

|********************************** CISLI245 **********************************

function string convert.cisli245.borf()
|* Business Object Reference
{
	if tdsls045.pono = 0 then
		|* Installment Line
		return(concat$("/", tdsls045.pono, 0))
	endif

	return(concat$("/", tdsls045.pono, tdsls045.srnb))
}

function string convert.cisli255.borf()
{
|* Business Object Reference
	
	long	temp.cost.type
	long	enum.constant.cost.type
	
	
	temp.cost.type = get.invoice.type()

	if temp.cost.type = 0 then
		return(concat$("/",tssma310.seqn,0))
	endif

	if temp.cost.type = 10 then
		enum.constant.cost.type = 5
	else
		if temp.cost.type = 11 then
			enum.constant.cost.type = 10
		else
			
			enum.constant.cost.type = 35
		endif
	endif

	return(concat$("/",tssma310.seqn,enum.constant.cost.type))


}
function string convert.cisli265.borf()
|* Business Object Reference
{

	return(concat$("/",0,0,tssma221.seqn,0,0,0,0))


}


function domain bcitem convert.cisli245.item()
|* Item
{
	domain bcstc.nprj	l.conv.project

	if tdsls041.opol <> tcittc.standard and
	   not isspace(tdsls045.cprj) then
		l.conv.project = convert.cisli245.cprj()
		return(segment.item.code(
				tdsls045.item,
				l.conv.project))
	endif

	return(segment.item.code(tdsls045.item, ""))
}

function domain bcstc.nprj convert.cisli245.cprj()
|* Project
{
	static	domain	tccprj		s.sls045.cprj
	static	domain	bcstc.nprj	s.project

	if isspace(tdsls045.cprj) then
		return("")
	endif

	if s.sls045.cprj <> tdsls045.cprj then
		s.sls045.cprj = tdsls045.cprj
		select	tipcs020.prtp
		from	tipcs020
		where	tipcs020._index1 = {:tdsls045.cprj}
		as set with 1 rows
		selectdo
			if tipcs020.prtp = tcyesno.yes then
				s.project = get.tp.project(tdsls045.cprj)
			else
				s.project = get.ti.project(tdsls045.cprj)
			endif
		endselect
	endif

	return(s.project)
}

function domain tcdsca convert.cisli245.desc()
|* Description
{
	if tdsls045.pono = 0 then
		|* Installment Line
		select	tdsls048.dsca
		from	tdsls048
		where	tdsls048._index1 = {	:tdsls045.orno,
						:tdsls045.srnb,
						0}
		as set with 1 rows
		selectdo
			return(tdsls048.dsca)
		endselect
	endif

	return(tiitm001.dsca)
}

function domain bccadr convert.cisli245.sfra()
|* Ship-from Address
{
	static	domain	bccadr		s.address
		domain	tccwar		l.cwar

	l.cwar = ""


	if tdsls045.pono <> 0 then
		if tdsls041.drct = tcyesno.yes then
			select	tdpur040.cwar:l.cwar
			from	tdpur040
			where	tdpur040._index1 = {:tdsls041.porn}
			as set with 1 rows
			selectdo
			endselect
		else
			l.cwar = tdsls045.cwar
		endif
		s.address = get.address.code(	"tcmcs003",
						l.cwar(1;3),

						o.error.mess)
	else
		s.address = ""
	endif

	return(s.address)
}

function domain bccadr convert.cisli245.stoa()
|* Ship-to Address
{
		domain	tccdel	l.delivery.address
	static	domain	bccadr	l.address
	static	domain	tcorno	s.sls045.orno

	if tdsls045.pono = 0 then
		|* Installment Line
		l.delivery.address = tdsls040.cdel
	else
		l.delivery.address = tdsls041.cdel
	endif

	if s.sls045.orno <> tdsls045.orno then
		s.sls045.orno = tdsls045.orno
	else
		if tdsls045.pono = 0 then
			return(l.address)
		endif
	endif

	l.address = convert.sales.address(
				l.delivery.address,
				tdsls045.cuno,
				tdsls045.orno,
				tdsls045.pono,
				0)	|* address type: delivery address

	return(l.address)
}

function long convert.cisli245.stat()
|* Invoice Status
{
	select	tfacr200.ttyp
	from	tfacr200
	where	tfacr200._index1 = {:tdsls045.ttyp, :tdsls045.invn}
	as set with 1 rows
	selectdo
		var.stat = 6				|#800-141306.n
		return(target(cisli.stat.posted))
							|#800-141306.sn		
	selectempty
		var.stat = 5
		return(target(cisli.stat.printed))
							|#800-141306.en
	endselect
	
	

	return(0)
}

|function double convert.cisli245.amth()
|* Invoice amount in Home Currency
|{
|	|domain	tcamnt	l.amnt.in.hc |*MCR .o
|	double	l.amnt.in.hc(3) |*MCR .n
|		
|	for i = 1 to 3 |*MCR .n	
|		tccri.dll0011.calculate.amount.in.home.currency(
|				tdsls040.ccur,
|				tdsls045.amnt,
|				date.num(),
|				2,		|* Sales Rate
|				|tdsls045.rats,
|				|tdsls045.ratf,
|				|l.amnt.in.hc)
|				fill.mc.field.double(tdsls045.rats,i),|*MCR .n
|				fill.mc.field.long(tdsls045.ratf,i),|*MCR .n
|				l.amnt.in.hc(i))|*MCR .n
|	endfor|*MCR .n		
|
|	return(l.amnt.in.hc)
|}

function domain tcqsl1 convert.cisli245.dqua()
|* Delivered Quantity
{
	if tdsls045.pono = 0 then
		|* Installment Line
		return(0.0)
	endif

	|* Delivered Quantity in BaanIV (tdsls041.dqua) is stored in








	|* sales unit. In Baan 6 it is stored in inventory unit.
	return(itccom0007.qty.domain.round(
				(tdsls045.dqua * tdsls041.cvqs),
				"tcqsl1",
				tiitm001.cuni))
}

function domain tcqsl1 convert.cisli245.oqua()
|* Ordered Quantity
{
	domain	tcqsl1	qty.in.inv.unit

	if tdsls045.pono = 0 then
		|* Installment Line
		return(0.0)
	endif

	|* Delivered Quantity in BaanIV (tdsls041.dqua) is stored in
	|* sales unit. In Baan 6 it is stored in inventory unit.

	return(itccom0007.qty.domain.round(
				(tdsls045.oqua * tdsls041.cvqs),

				"tcqsl1",
				tiitm001.cuni))

}

function domain bccpvl convert.cisli245.cpva()
|* Product Variant
{
		long		ret

	if isspace(tdsls041.cpva) or
	   tdsls045.pono = 0 then
		return(0)
	else
		ret = get.new.product.variant(tdsls041.cpva, o.error.mess)
	endif

	return(bcsti001.cpvl)
}

function domain tccopr convert.cisli245.copr()
|* Standard Costs
{
	
	|*MCR .s	
	|if double.cmp(tdsls045.copr, 0.0, 0.0001) and
	for i = 1 to 3
		if double.cmp(fill.mc.field.double(tdsls045.copr,i,0.00001),0.0, 0.0001) and
		   tdsls045.pono <> 0 then
			return(imcs0010.round.amnt(
				|(tdsls045.oqua * tdsls041.cvqs * tdsls045.copr),
				(tdsls045.oqua * tdsls041.cvqs * fill.mc.field.double(tdsls045.copr,i,0.00001)),
				tdsls040.ccur,
				tcmcs002.crnd))
		endif
	endfor
	|*MCR .e
	return(0.0)
}

function long convert.cisli245.chtp()
|* Charge Type
{
	if tdsls045.pono = 0 then
		|* Installment invoice
		return(target(cisli.chtp.none))
	endif

	if tiitm001.itmt = tppdm.itmt.equipment then
		return(target(cisli.chtp.goods))
	endif

	if tiitm001.itmt = tppdm.itmt.subcontracting then
		return(target(cisli.chtp.costs))
	endif

	|* tiitm001.kitm = tppdm.itmt.item ... Now test the Item Type.
	on case	tiitm001.kitm
	case tckitm.purchase:
	case tckitm.manufacture:
	case tckitm.generic:
		return(target(cisli.chtp.goods))

	case tckitm.cost:
	case tckitm.service:
	case tckitm.subcontracting:
		return(target(cisli.chtp.costs))
	endcase
	return(target(cisli.chtp.goods))	|* Initial value DD
}

function domain	bcstd.fent convert.cisli245.cofc()
|* Department
{
			long	ret
	static	domain	tcncmp	s.sls040.comp

	if s.sls040.comp <> tdsls040.comp then
		s.sls040.comp = tdsls040.comp
		ret = get.enterprise.unit(
				tdsls040.comp,
				get.compnr(),
				o.error.mess)
	endif

	return(bcstc018.sofd)
}

function domain	tctxtn convert.cisli245.itxt()
|* Item Text
{
	domain	tccuno		l.text.cuno

	if not isspace(tdsls040.pctx) then
		l.text.cuno = tdsls040.pctx
	else
		l.text.cuno = tdsls040.cuno
	endif

	select	tdsls020.txta
	from	tdsls020
	where	tdsls020._index1 = {:l.text.cuno, :tdsls045.item}
	as set with 1 rows
	selectdo
		return(tdsls020.txta)
	endselect


	return(tiitm001.txts)
}
function fill.cost.component.cogs()
{
		 	 
	select tiitm001.copr
	from   tiitm001
	where tiitm001._index1 = {:tdsls041.item}
	as set with 1 rows
	selectdo
|		var.cisli246.cpcp = tiitm001.cpcp
		|*MCR .s
		|var.cisli246.cogsh = tiitm001.copr
		for i = 1 to 3
			var.cisli246.cogsh(i) = fill.mc.field.double(tiitm001.copr,i,0.00001)
		endfor
		|*MCR .e
	endselect
}

|******************************CISLI250****************************************
function domain bcstd.fent get.department.tssma611()

{
	if not ( bcstc018.fcmp = tssma611.ncmp and
	   bcstc018.lcmp = ext.lng.comp ) then
		get.enterprise.unit(tssma611.ncmp, ext.lng.comp, err.mess)
	endif
	return(bcstc018.srfd)
}

function fill.cluster.tssma611()
{
	if not isspace(tssma301.cloc) then			|ravi_1607.n
		if not get.configuration(tssma301.cloc, err.mess) then
			var.cisli250.clst = bcsts005.clst
		else						|ravi_1607.sn
			var.cisli250.clst = empty.string (1;8)
		endif						|ravi_1607.en
	else
		var.cisli250.clst = empty.string (1;8)
	endif

|	if bcsts005.cins = tssma301.cins then
|		return
|	endif

|	select	bcsts005.clst
|	from	bcsts005
|	where	bcsts005.cins = :tssma301.cins
|	as set with 1 rows
|	selectdo
|		var.cisli250.clst = bcsts005.clst
|	selectempty
|		var.cisli250.clst = empty.string (1;8)
|	endselect
}

function fill.contract.tssma611()
{
	if (len (shiftl$ (shiftr$ (tssma301.ccon))) > 9) then
		var.cisli250.csco = str$ ((1 * asc (tssma301.ccon (1;1)))+
			(2 * asc (tssma301.ccon (2;1)))+
			(3 * asc (tssma301.ccon (3;1)))+
			(4 * asc (tssma301.ccon (4;1)))+
			(5 * asc (tssma301.ccon (5;1)))+
			(6 * asc (tssma301.ccon (6;1)))+
			(7 * asc (tssma301.ccon (7;1)))+
			(8 * asc (tssma301.ccon (8;1)))+
			(9 * asc (tssma301.ccon (9;1)))+
			(10 * asc (tssma301.ccon (10;1))))
	else
		var.cisli250.csco = shiftl$ (shiftr$ (tssma301.ccon))
	endif
}

function fill.business.partners.tssma611()
{
	if get.business.partner(tssma301.cuno,
				bcstc.orgn.cuno,
				err.mess) then
		var.cisli250.bpid = empty.string(1;9)
	else
		var.cisli250.bpid = bcstc008.bpid
	endif
}

function fill.addresses.tssma611()
{
	
							|binda21-07.sn
	static domain tccuno s.cuno	|WR PERF add
	static domain tccdel s.cdel	|WR PERF add
	static domain tcccor s.ccor	|WR PERF add
	static domain tssma.cloc s.cloc	|WR PERF add
	static domain tcorno s.orno	|WR PERF add
							|binda21-07.en

							|#SL-147622.so
|	var.cisli250.itad = empty.string (1;9)
|	var.cisli250.stad = empty.string (1;9)
|	var.cisli250.lcad = empty.string (1;9)
							|#SL-147622.eo

	if tssma301.cuno <> s.cuno then		|WR PERF add	|binda21-07.n

		if not get.addresses ("tccom010",tssma301.cuno,err.mess) then
			var.str.defaddr = bcstc004.cadr
			var.cisli206.stcy = bcstc004.ccty		|#ravi.n
			var.cisli206.itcy = bcstc004.ccty		|#ravi.n
		endif
	endif							|binda21-07.n

								|binda21-07.sn
	if not isspace(tssma301.cloc) then	|WR PERF add
	   if s.cloc <> tssma301.cloc then	|WR PERF add
	   							|binda21-07.en

		select	tssma110.*
		from	tssma110
		where	tssma110._index1 = {:tssma301.cloc}
		as set with 1 rows
		selectdo
			if isspace (tssma110.cdel) then
				var.cisli250.lcad = var.str.defaddr
			else
				var.str.add = tssma110.cuno (1;6)& 
					tssma110.cdel (1;3)
				if not get.addresses ("tccom013",
					var.str.add,err.mess) then
					var.cisli250.stad = bcstc004.cadr
					var.cisli206.stcy = bcstc004.ccty		|#ravi.n
					var.cisli250.lcad = bcstc004.cadr
				endif
			endif

		selectempty

			var.cisli250.lcad = var.str.defaddr
		endselect
							|binda21-07.sn
		s.cloc = tssma301.cloc
	endif
      else
	  var.cisli250.lcad = var.str.defaddr
      endif
							|binda21-07.en

	if s.orno <> tssma301.orno then		|WR PERF add	|binda21-07.n

	select	tssma302.*

	from	tssma302
	where	tssma302.orno = :tssma301.orno
	selectdo
		if tssma302.ckor = 1 then
			var.str.addvar1 = edit$ (tssma302.orno,"999999") & 
					tssma302.cloc (1;10) & 
					edit$ (tssma302.ckor,"99")
			if not get.addresses("tssma302",
					var.str.addvar1,err.mess) then
				var.cisli250.itad = bcstc004.cadr
				var.cisli206.itcy = bcstc004.ccty		|#ravi.n
			endif
		endif

		if tssma302.ckor = 2 then
			var.str.addvar1 = edit$(tssma302.orno,"999999") & 
					tssma302.cloc (1;10) & 
					edit$ (tssma302.ckor,"99")
			if not get.addresses ("tssma302",
					var.str.addvar1,err.mess) then
				var.cisli250.stad = bcstc004.cadr
				var.cisli206.stcy = bcstc004.ccty		|#ravi.n
			endif
		endif
	endselect
							|binda21-07.sn
	s.orno = tssma301.orno 
      endif
      							|binda21-07.en

	if isspace(var.cisli250.stad) then
		if isspace (tssma301.cdel) then
			var.cisli250.stad = var.str.defaddr
		else
								|binda21-07.sn
			 if s.cuno <> tssma301.cuno or s.cdel <> tssma301.cdel then	|WR PERF add
			 					|binda21-07.en

				var.str.add = tssma301.cuno (1;6) & tssma301.cdel (1;3)
				if not get.addresses ("tccom013", 
						var.str.add,err.mess) then
					var.cisli250.stad = bcstc004.cadr
					var.cisli206.stcy = bcstc004.ccty		|#ravi.n
				else
					var.cisli250.stad = var.str.defaddr
				endif
								|binda21-07.sn
				s.cdel = tssma301.cdel
		   	  endif
								|binda21-07.en

		endif
	endif

	if isspace(var.cisli250.itad) then
		if isspace (tssma301.ccor) then
			var.cisli250.itad = var.str.defaddr
		else

								|binda21-07.sn
			if s.cuno <> tssma301.cuno or s.ccor <> tssma301.ccor then	|WR PERF add
								|binda21-07.en

				var.str.add = tssma301.cuno (1;6) & tssma301.ccor (1;3)
				if not get.addresses ("tccom012",
						var.str.add,err.mess) then
					var.cisli250.itad = bcstc004.cadr
					var.cisli206.itcy = bcstc004.ccty		|#ravi.n

				else
					var.cisli250.itad = var.str.defaddr
				endif
									|binda21-07.sn
				s.ccor = tssma301.ccor
		     endif
		     							|binda21-07.en

		endif
	endif
	s.cuno = tssma301.cuno	|WR PERF add			|binda21-07.n
}

function fill.customer.related.fields.tssma611()
{
	long	temp.crep
								|binda21-07.sn

	static domain tccuno s.cuno			|WR PERF add
	if s.cuno <> tssma301.cuno then			|WR PERF add
	 							|binda21-07.en


		select	tccom010.paym :var.cisli250.paym, 
			tccom010.crep :temp.crep, 
			tccom010.cbrn :var.cisli250.cbrn
		from	tccom010
		where	tccom010.cuno = :tssma301.cuno
		selectdo
			var.cisli250.crep=(temp.crep?format.emno(temp.crep):"")
		selectempty
			var.cisli250.paym = ""
			var.cisli250.crep= ""
			var.cisli250.cbrn= ""
		endselect
								|binda21-07.sn
		s.cuno = tssma301.cuno
    	 endif
   	  							|binda21-07.en


}

|****************************CISLI255*****************************************
function fill.cisli255.fields()
{
	var.cisli255.invt = get.invoice.type()
	get.tax.by.country.code(  
 	 	 	 	 tssma310.ccty,  
 	 	 	 	 tssma310.cvat,  
 	 	 	 	 new.txct,  
 	 	 	 	 new.txid,  
 	 	 	 	 err.mess)  
	

	var.cisli255.cstp = bcsts000.cstp(1, etol(tssma301.sert))
	if get.default.sls.type.for.tfacr(var.cisli255.styp, err.mess) then
		var.cisli255.styp =  ""  
	endif

	if tssma310.amta <> 0.0 then
|*  MCR -  new changes .e 	
|	        for i = 1 to 3 |*MCR
|			calculate.amount.in.other.currency(tssma310.amta,
|						|*MCR - s	
|						|tssma301.rats,
|						|tssma301.ratf,
|						fill.mc.field.double(tssma301.rats,i),
|						fill.mc.field.long(tssma301.ratf,i),
|						|*MCR - e
|						tssma301.ccur,
|						|var.cisli255.amth,
|						var.cisli255.amth(i), |*MCR
|						err.mess) 
|		endfor |*MCR	
		for i =  1 to 3 	 	 	 	 	 	  
|	 	 	 calculate.amount.in.home.currency.variables (tssma310.amta,	|#200-101916.o
	 	 	 calculate.amount.in.home.currency.variables (tssma310.amta+tssma310.ldam,	|#200-101916.n
	 	 	 tssma301.ccur,
			 fill.mc.field.double(tssma301.rats,i),
	 		 fill.mc.field.long(tssma301.ratf,i),
	 	 	 i,  
|	 	 	 var.cisli225.amth(i), 	 		|#200-101926.o 	 	 	 	 	  
	 	 	 var.cisli255.amth(i), 	 		|#200-101926.n 	 	 	 	 	  
	 	 	 err.mess) 	 	 	 	 	 	  
	 	endfor 	 	 	 	 	 	  
|*  MCR -  new changes .e 				
	else
		|*MCR - s
		|var.cisli255.amth = 0.0
		for i = 1 to 3
			var.cisli255.amth(i) = 0.0
		endfor
		|*MCR
	endif


	if get.exchange.rate.type("SLS",err.mess)  then
		var.cisli255.rtyp = "" 
	else
		var.cisli255.rtyp = bcstc011.rtyp 
	endif
	calculate.amounts()

	 var.cisli255.amti =  calc.tax.base.amount (tssma301.cpay, tssma310.amta, 0.0,  
 	 	 	 	 	 	       tssma301.ccrs, tssma301.ccur) 
	 if new.txid <>  "" then 
		 if tssma310.txam.l <> 0  then
			var.cisli255.txai = tssma310.txam.l
		 else
		 
 	 	 	var.cisli255.txai =  calc.tax.amount.logistics(new.txct,new.txid,tssma301.ccur,  
 	 	 					|*MCR -s
 	 	 	 				|tssma301.rats,tssma301.ratf,
 	 	 	 				fill.mc.field.double(tssma301.rats,1),
						        fill.mc.field.long(tssma301.ratf,1),
						        |*MCR -e
 	 	 	 				var.cisli255.amti,tssma301.cuno,tssma310.invd)
		 endif
 	 	 if var.cisli255.txai <>  0.0 then  
|*  MCR -  new changes .e 	 	 
| 	 	 	for i = 1 to 3 |*MCR
| 	 	 	 	calculate.amount.in.other.currency (var.cisli255.txai,
| 	 	 	 		 	|*MCR - s	
|						|tssma301.rats,
|						|tssma301.ratf,
|						fill.mc.field.double(tssma301.rats,i),
|						fill.mc.field.long(tssma301.ratf,i),
|						|*MCR - e
| 	 	 	 	 		tssma301.ccur,
| 	 	 	 	 		|var.cisli255.txah,
| 	 	 	 	 		var.cisli255.txah(i), |*MCR
| 	 	 	 	 		err.mess) 	 	  
| 	 	 	 endfor |*MCR	 		
 	 	 	 for i =  1 to 3 	 	 	 	 	 	  
		 	 	 calculate.amount.in.home.currency.variables (var.cisli255.txai,
		 	 	 tssma301.ccur,
				 fill.mc.field.double(tssma301.rats,i),
		 		 fill.mc.field.long(tssma301.ratf,i),
		 	 	 i,  
|		 	 	 var.cisli225.txah(i), 	 	 	|#200-101926.o 	 	 	 	  
		 	 	 var.cisli255.txah(i), 	 	 	|#200-101926.n
		 	 	 err.mess) 	 	 	 	 	 	  
	 		endfor 	 	 	 	 	 	  
|*  MCR -  new changes .e 
 	 	 else  
 	 	 	 |*MCR - s
 	 	 	 |var.cisli255.txah =  0.0  
 	 	 	 for i = 1 to 3
 	 	 	 	var.cisli255.txah(i) =  0.0  
 	 	 	 endfor
 	 	 	 |*MCR -e
 	 	 endif  
 	 else  
 	 	 
 	 	 var.cisli255.txai =  0.0  
 	 	 |*MCR - s
 	 	 |var.cisli255.txah =  0.0  
 	 	 for i = 1 to 3
 	 	  	var.cisli255.txah(i) =  0.0  
 	 	 endfor
 	 	 |*MCR -e
 	 endif  
}

function long get.invoice.type()
{
	on case tssma310.tcst
	case tssma.tcst.material:
		return(10)
		|* tcinvt.material
		break

	case tssma.tcst.worktime:
		if tssma310.bhra = tcyesno.yes then
			return(11)
			|* tcinvt.labor
		else
			return(24)
			|* tcinvt.other
		endif
		break
						|#200-101926.sn
	 case tssma.tcst.other:
		if tiitm001.kitm = tckitm.purchase or
		   tiitm001.kitm = tckitm.manufacture then
			return(10)
			|* Material
		else
			return(24)
			|* Other
		endif
		break
						|#200-101926.en
	default:
		return(24)
		|* tcinvt.other
		break
	endcase
	return(0)

}

function calculate.amounts()
{	
	|*MCR .s
	|var.dbl.asto = tssma310.qana * tssma310.pric
	for i = 1 to 3
		var.dbl.asto = tssma310.qana * fill.mc.field.double(tssma310.pric,i,0.00001)
	endfor
	|*MCR .e
	
	var.dbl.aswa = (tssma310.gper * var.dbl.asto)/ 100
	var.dbl.asco =(tssma310.cper * var.dbl.asto) / 100
	var.dbl.asin = var.dbl.asto - (var.dbl.aswa + var.dbl.asco)
	var.dbl.asdi = (tssma310.disc * var.dbl.asin)/ 100
	var.dbl.inam = var.dbl.asin - var.dbl.asdi
}

|*****************************CISLI260*****************************************
function fill.contract()
{
	if (len (shiftl$ (shiftr$ (tssma220.ccon))) > 9) then
		var.cisli260.csco = str$ ((1 * asc (tssma220.ccon (1;1)))+
			(2 * asc (tssma220.ccon (2;1)))+
			(3 * asc (tssma220.ccon (3;1)))+
			(4 * asc (tssma220.ccon (4;1)))+
			(5 * asc (tssma220.ccon (5;1)))+
			(6 * asc (tssma220.ccon (6;1)))+
			(7 * asc (tssma220.ccon (7;1)))+

			(8 * asc (tssma220.ccon (8;1)))+
			(9 * asc (tssma220.ccon (9;1)))+
			(10 * asc (tssma220.ccon (10;1))))

	else
		var.cisli260.csco = shiftl$ (shiftr$ (tssma220.ccon))
	endif
}

function domain bcstd.fent get.department()
{
	if not ( bcstc018.fcmp = tssma611.ncmp and

	   bcstc018.lcmp = ext.lng.comp ) then
		get.enterprise.unit(tssma611.ncmp, ext.lng.comp, err.mess)
	endif
	return(bcstc018.srfd)
}
function fill.business.partner()
{
	if get.business.partner(tssma220.cuno,
				bcstc.orgn.cuno,
				err.mess) then
		var.cisli260.bpid = empty.string(1;9)
	else
		var.cisli260.bpid = bcstc008.bpid
	endif
}

function fill.addresses()
{
	var.cisli260.itad = empty.string (1;9)

	if not get.addresses ("tccom010",tssma220.cuno,err.mess) then
		var.str.defaddr = bcstc004.cadr
		var.cisli206.itcy = bcstc004.ccty					|#ravi.n
	endif

	if isspace (tssma220.ccor) then
		var.cisli260.itad = var.str.defaddr
	else
		var.str.add = tssma220.cuno (1;6) & tssma220.ccor (1;3)
		if not get.addresses ("tccom012",
			var.str.add,err.mess) then
			var.cisli260.itad = bcstc004.cadr
			var.cisli206.itcy = bcstc004.ccty					|#ravi.n
		else
			var.cisli260.itad = var.str.defaddr
		endif
	endif
}

function fill.customer.related.fields()
{
	long temp.crep1
	select	tccom010.paym :var.cisli260.paym, 
		tccom010.crep :temp.crep1, 
		tccom010.cbrn :var.cisli260.cbrn
	from	tccom010
	where	tccom010.cuno = :tssma220.cuno
	selectdo
		var.cisli260.crep=(temp.crep1?format.emno(temp.crep1):"")
	selectempty
		var.cisli260.paym = ""

		var.cisli260.crep= ""
		var.cisli260.cbrn= ""
	endselect
}

|******************************** CISLI265*************************************
function fill.cisli265.fields()
{
	if get.default.sls.type.for.tfacr(var.cisli265.styp, err.mess) then


		var.cisli265.styp =  ""  
	endif
	get.tax.by.country.code(  
 	 	 	 	 tssma611.ccty,  

 	 	 	 	 tssma220.cvat,  
 	 	 	 	 new.txct,  
 	 	 	 	 new.txid,  
 	 	 	 	 err.mess)  
	
|*  MCR -  new changes .s
|	for i = 1 to 3 |*MCR.n
|		if calculate.amount.in.other.currency( 	tssma221.nprc,
|						|tssma611.rats, |*MCR .o
|						|tssma611.ratf, |*MCR .o
|						fill.mc.field.double(tssma611.rats,i),|*MCR.n
|						fill.mc.field.long(tssma611.ratf,i),|*MCR.n
|						tssma611.ccur,
|						|var.cisli265.amth,|*MCR.o
|						var.cisli265.amth(i),|*MCR.n
|						err.mess) then
|			|var.cisli265.amth = 0.0000  |*MCR.o
|			var.cisli265.amth(i) = 0.0000|*MCR.n
|		endif
|	endfor |*MCR.n
	for i =  1 to 3 	 	 	 	 	 	  
|	 	if  calculate.amount.in.home.currency.variables (tssma221.nprc,	|#200-101916.o
	 	if  calculate.amount.in.home.currency.variables (tssma221.pric,	|#200-101916.n
	 	 	tssma611.ccur,
			fill.mc.field.double(tssma611.rats,i),
			fill.mc.field.long(tssma611.ratf,i),
	 	 	i,  
	 	 	var.cisli265.amth(i), 	 	 	 	 	 	 	  
	 	 	err.mess)   then

	 	 	var.cisli265.amth(i) = 0.0000		 
		endif	 	 	 	 	  
	endfor 	 	 	 	 	 	  
|*  MCR -  new changes .e 

	var.cisli265.amti =  calc.tax.base.amount (tssma220.cpay, tssma221.nprc, 0.0,  
 	 	 	 	 	 	 	 tssma220.ccrs, tssma611.ccur)   
	if new.txid <>  "" then        
  
		 if tssma221.txam.l <> 0  then
			var.cisli265.txai = tssma221.txam.l
		 else
 	 	 
		 	var.cisli265.txai =  calc.tax.amount.logistics(new.txct, new.txid,tssma611.ccur,
		 					|tssma611.rats,tssma611.ratf, |*MCR .o
		 					fill.mc.field.double(tssma611.rats,1),|*MCR.n
							fill.mc.field.long(tssma611.ratf,1),|*MCR.n
 	 	 				var.cisli265.amti,tssma220.cuno,tssma221.invd)
		 endif

 	 	 if var.cisli265.txai <>  0.0 then  
|*  MCR -  new changes .s 	 	 
| 	 	 	for i = 1 to 3 |*MCR.n
| 	 	 	 	calculate.amount.in.other.currency (var.cisli265.txai,
| 	 	 	 			|tssma611.rats, |*MCR .o
|						|tssma611.ratf, |*MCR .o
|						fill.mc.field.double(tssma611.rats,i),|*MCR.n
|						fill.mc.field.long(tssma611.ratf,i),|*MCR.n
| 	 	 	 	 	 	tssma611.ccur,
| 	 	 	 	 	 	|var.cisli265.txah,|*MCR .o
| 	 	 	 	 	 	var.cisli265.txah(i), |*MCR .n
| 	 	 	 	 	 	err.mess) 	 
| 	 	 	 endfor |*MCR .n	 	 	
			for i = 1 to 3
	 	 	 	 calculate.amount.in.home.currency.variables(var.cisli265.txai,
		 	 	 tssma611.ccur,
				 fill.mc.field.double(tssma611.rats,i),
				 fill.mc.field.long(tssma611.ratf,i),
		 	 	 i,  
		 	 	 var.cisli265.txah(i), 	 	 	 	 	 	 	  
		 	 	 err.mess)  
		 	endfor 	 
|*  MCR -  new changes .e		 	
 	 	 else  
 	 	 	 |*MCR.s
 	 	 	 |var.cisli265.txah =  0.0  
 	 	 	 for i = 1 to 3 
 	 	 	 	var.cisli265.txah(i) =  0.0  
 	 	 	 endfor	
 	 	 	 |*MCR.e
 	 	 endif  
 	 else  
 	 	 
 	 	 var.cisli265.txai =  0.0  
 	 	 |*MCR.s
 	 	 |var.cisli265.txah =  0.0  
 	 	 for i = 1 to 3 
 	 	 	var.cisli265.txah(i) =  0.0  
 	 	 endfor	
 	 	 |*MCR.e
 	 endif  
 	 
|*  MCR -  new changes .s
|		for i = 1 to 3 |*MCR .n
|			if calculate.amount.in.other.currency( 	tssma221.cpam,
|						|tssma611.rats,|*MCR.o
|						|tssma611.ratf,|*MCR.o
|						fill.mc.field.double(tssma611.rats,i),|*MCR.n
|						fill.mc.field.long(tssma611.ratf,i),|*MCR.n
|						tssma611.ccur,
|						|var.cisli265.fcah, |*MCR.o
|						var.cisli265.fcah(i),|*MCR.n
|						err.mess) then
|						
|				|var.cisli265.fcah = 0.0000 |*MCR.o
|				var.cisli265.fcah(i) = 0.0000 |*MCR.n
|
|			endif
|			
|		endfor |*MCR .n	

		for i = 1 to 3
	 	 	if  calculate.amount.in.home.currency.variables(
	 	 		 |tssma221.cpam, |*MCR .o
	 	 		 fill.mc.field.double(tssma221.cpam,i), |*MCR .n
		 	 	 tssma611.ccur,
				 fill.mc.field.double(tssma611.rats,i),
				 fill.mc.field.long(tssma611.ratf,i),
		 	 	 i,  
		 	 	 var.cisli265.fcah(i), 	 	 	 	 	 	 	  
		 	 	 err.mess) then
		 	 	 
			 	 var.cisli265.fcah(i) = 0.0000	 
			endif
		 	 	  
		endfor 	 
|*  MCR -  new changes .e		




	if get.exchange.rate.type("SLS",err.mess)  then
		var.cisli265.rtyp = "" 
	else
		var.cisli265.rtyp = bcstc011.rtyp 
	endif
}

|***************************CISLI290*******************************************
function domain tcbool read.sales.order.history()
{
	static	domain	tcorno	s.cms050.orno

	if s.cms050.orno <> tdcms050.orno then
		select  	tdsls050._index1, tdsls050.comp, tdsls050.cpay, tdsls050.ccrs,
			tdsls050.ccor, tdsls050.cuno, tdsls050.crep,

	 	   	tdsls050.cotp, tdsls050.txta
 		from  	tdsls050
	 	where  	tdsls050._index1 =  {:tdcms050.orno}
 		order by tdsls050._index1 desc
	 	as set with 1 rows
 		selectdo
			s.cms050.orno = tdcms050.orno
			return(true)
		selectempty
			return(false)
		endselect
	endif

	return(true)
}

function domain	tcbool read.sales.order.line.history()
{
	static	domain	tcorno	s.cms050.orno
	static	domain	tcpono	s.cms050.pono

	if s.cms050.orno <> tdcms050.orno or
	   s.cms050.pono <> tdcms050.pono then
		select  	tdsls050.comp, tdsls050.cpay, tdsls050.ccrs,
			tdsls050.ccor, tdsls050.cuno, tdsls050.crep, 	   		tdsls050.cotp, tdsls050.txta,
			tdsls051._index1, tdsls051.item, tdsls051.cprj, tdsls051.ddta,

			tdsls051.ttyp, tdsls051.invn,
			tiitm001.dsca
	 	from  	tdsls050, tdsls051, tiitm001
 		where  	tdsls051._index1 =  {:tdcms050.orno,:tdcms050.pono}
		and	tdsls051.ckor <> 2
		and	tdsls050._index1 = {tdsls051.orno}
		and	tiitm001._index1 = {tdsls051.item}
	 	order by tdsls051._index1 desc
 		as set with 1 rows
	 	selectdo
			s.cms050.orno = tdcms050.orno
			s.cms050.pono = tdcms050.pono
			return(true)
		selectempty
			return(false)
		endselect
	endif

	return(true)
}

function domain tcbool convert.rebate.invoice.header.fields()
{
	convert.cisli290.reso()
	if not convert.cisli290.cofc() then
		return(false)
	endif
	if not convert.cisli290.ofbp() then
		return(false)
	endif
	convert.cisli290.ofad()
	convert.cisli290.paym()

	return(true)
}
function convert.cisli290.reso()
{
	static	domain	tcorno	s.sli290.reso

	if s.sli290.reso <> tdcms050.orno then
		s.sli290.reso = tdcms050.orno
		var.cisli290.reso = convert.number(
					bcckon.act.sls,
					tdcms050.orno)
	endif
}

function domain tcbool convert.cisli290.cofc()
{
	static	domain	tcncmp	s.sls050.comp

	if s.sls050.comp <> tdsls050.comp then
		s.sls050.comp = tdsls050.comp
		if not get.enterprise.unit(
 		   	   	  tdsls050.comp,
 	   		   	  var.cisli290.slcp,
 	   	   		  o.error.mess ) then

			var.cisli290.cofc = bcstc018.sofd
			return(true)
		else
			return(false)
	 	endif
	endif

	return(true)
}

function domain tcbool convert.cisli290.ofbp()
{
	static	domain	tdcms.reln	s.cms050.reln

	if s.cms050.reln <>  tdcms050.reln then
	  	s.cms050.reln = tdcms050.reln
 		if not get.business.partner(
 	   	   	   	     tdcms010.cuno,
 	   	   	   	     bcstc.orgn.cuno,
 	   	   	   	     o.error.mess) then
			var.cisli290.ofbp = bcstc008.bpid
			return(true)
 	   	else
 	   	   	return(false)
 	   	endif
	endif

	return(true)
}

function convert.cisli290.ofad()
{
	var.cisli290.ofad = convert.sales.address(
				tdsls050.ccor,
				tdsls050.cuno,
				tdcms050.orno,
				tdcms050.pono,
				1)	|* address type: postal address
}
function convert.cisli290.paym()

{
	static	domain	tccuno	s.sls050.cuno

	if s.sls050.cuno <> tdsls050.cuno then
		s.sls050.cuno = tdsls050.cuno

		select	tccom010.paym:var.cisli290.paym,
			tccom010.ccrs:var.cisli290.ccrs
		from	tccom010
		where	tccom010._index1 = {:tdsls050.cuno}
		as set with 1 rows
		selectdo
		selectempty
			var.cisli290.paym = empty()
			var.cisli290.ccrs = empty()
		endselect

		if not isspace(tdsls050.ccrs) then
			var.cisli290.ccrs = tdsls050.ccrs
		endif
	endif
}


|********************************CISLI295*************************************
function convert.rebate.invoice.line.fields()
{
	convert.cisli295.codt()
	convert.cisli295.cprj()
	convert.cisli295.item()
	convert.cisli295.stat()
	convert.cisli295.txai()
}

function convert.cisli295.codt()
{
	if tdcms000.lnrp =  tdcms.lnrp.order then
		var.cisli295.codt = var.utc.zero
	else
		var.cisli295.codt = utc(tdsls051.ddta, 0)
	endif

}


function convert.cisli295.cprj()
{

	if tdcms000.lnrp =  tdcms.lnrp.order then
		var.cisli295.cprj = empty()

		return
	endif

	static	domain	tccprj	s.cisli295.cprj

	if s.cisli295.cprj <> tdsls051.cprj then
		s.cisli295.cprj = tdsls051.cprj
		if not isspace(tdsls051.cprj) then
			select	tipcs020.prtp
			from	tipcs020
			where	tipcs020._index1 = {:tdsls051.cprj}
			as set with 1 rows
			selectdo
				if tipcs020.prtp = tcyesno.yes then
					var.cisli295.cprj = get.tp.project(tdsls051.cprj)
				else
					var.cisli295.cprj = get.ti.project(tdsls051.cprj)
				endif
			endselect
		else
			var.cisli295.cprj = empty()
		endif

	endif
}

function convert.cisli295.item()
{
	static	domain	tcitem	s.cisli295.item

	if tdcms000.lnrp =  tdcms.lnrp.order then
		var.cisli295.item = empty()
		return
	endif

	if s.cisli295.item <> tdsls051.item then
		s.cisli295.item = tdsls051.item



		var.cisli295.item = segment.item.code(tdsls051.item, var.cisli295.cprj)
	endif
}

function convert.cisli295.stat()
{
	if tdcms050.cmst = tdcms.cmst.ready then
		var.cisli295.stat = target(cisli.stat.printed)
		var.cisli205.stat =  5  
 	 	var.cisli205.brid =  2  
	else
		if tdcms050.cmst = tdcms.cmst.closed then
			var.cisli295.stat = target(cisli.stat.posted)
			var.cisli205.stat =  6  
 	 		var.cisli205.brid =  1  
		endif



	endif
}
function convert.cisli295.txai()
{
	
	 get.tax.by.country.code( tdcms201.ccty,  
 	 	 	 	  tdcms201.cvat,  
 	 	 	 	  new.txct,  
 	 	 	 	  new.txid,  
 	 	 	 	  err.mess)  

|*MCR .s

	|if tdcms050.cmam <> 0.0 then
	for i = 1 to 3
		if (fill.mc.field.double(tdcms050.cmam,i,0.00001) <> 0.0) then
|*  MCR -  new changes .s		
|		for i = 1 to 3 |*MCR .n
|			calculate.amount.in.other.currency(tdcms050.cmam,
|						|tdcms201.rate, |*MCR .o
|						|tdcms201.ratf, |*MCR .o
|						fill.mc.field.double(tdcms201.rate,i), |*MCR .n
|						fill.mc.field.long(tdcms201.ratf,i), |*MCR .n
|						tdcms201.ccur,
|						|var.cisli295.amth,|*MCR .o
|						var.cisli295.amth(i),|*MCR .n
|						err.mess) 
|		endfor |*MCR .n
|		for i = 1 to 3
			|calculate.amount.in.home.currency(tdcms050.cmam,
			calculate.amount.in.home.currency.variables(fill.mc.field.double(tdcms050.cmam,i,0.00001),
		 	 	 tdcms201.ccur,
				 fill.mc.field.double(tdcms201.rate,i),
				 fill.mc.field.long(tdcms201.ratf,i),
		 	 	 i,  
		 	 	 var.cisli295.amth(i), 	 	 	 	 	 	 	  
		 	 	 err.mess)
|		 endfor	 	 
|*  MCR -  new changes .e		 
		else
		|*MCR .s
		|var.cisli295.amth = 0.0
|		for i = 1 to 3
			var.cisli295.amth(i) = 0.0
|		endfor
		|*MCR .e
		endif
	endfor |*MCR .e

| 	var.cisli295.amti =  calc.tax.base.amount (tdcms010.cpay, tdcms050.cmam, 0.0,  |#200-98750.o
| 	var.cisli295.amti =  calc.tax.base.amount (tdcms010.cpay, -tdcms050.cmam, 0.0,  |#200-98750.n |*MCR .s
| 	 	 	 	 	 	 	 "", tdcms201.ccur)  	|*MCR .s
	for  i = 1 to 3
		var.tdcms050.cmam = fill.mc.field.double(tdcms050.cmam,i,0.00001)
		var.cisli295.amti =  calc.tax.base.amount (tdcms010.cpay, - var.tdcms050.cmam,0.0,"", tdcms201.ccur)
	endfor
	|*MCR .e
	if new.txid <>  "" then 
		 var.cisli295.txai =  calc.tax.amount.rebates(new.txct, new.txid,var.cisli295.amti,tdcms201.crdt)  
 		
 	 	 if var.cisli295.txai <>  0.0 then  
|*  MCR -  new changes .s 	 	 
| 	 	 	for i = 1 to 3|*MCR .n
| 	 	 	 	calculate.amount.in.other.currency (var.cisli295.txai,
| 	 	 	 			|tdcms201.rate, |*MCR .o
|						|tdcms201.ratf, |*MCR .o
|						fill.mc.field.double(tdcms201.rate,i), |*MCR .n
|						fill.mc.field.long(tdcms201.ratf,i), |*MCR .n  
| 	 	 	 	 	 	tdcms201.ccur,
| 	 	 	 	 	 	|var.cisli295.txah, |*MCR .o
| 	 	 	 	 	 	var.cisli295.txah(i), |*MCR .n
| 	 	 	 	 	 	err.mess) 	 	  
| 	 	 	endfor |*MCR .n 	
 	 	 	for i = 1 to 3
				calculate.amount.in.home.currency.variables(var.cisli295.txai,
			 	 	 tdcms201.ccur,
					 fill.mc.field.double(tdcms201.rate,i),
					 fill.mc.field.long(tdcms201.ratf,i),
			 	 	 i,  
			 	 	 var.cisli295.txah(i), 	 	 	 	 	 	 	  
			 	 	 err.mess)
		 	endfor	 	 
|*  MCR -  new changes .e 	 	 
 	 	 else   	 	 	
 	 	 	|*MCR .s
			|var.cisli295.txah = 0.0
			for i = 1 to 3
				var.cisli295.txah(i) = 0.0
			endfor
			|*MCR .e
 	 	 endif  
 	 else   	 	 

 	 	var.cisli295.txai =  0.0  
 	 	|*MCR .s
		|var.cisli295.txah = 0.0
		for i = 1 to 3
			var.cisli295.txah(i) = 0.0
		endfor
		|*MCR .e
 	 endif  



}

function string convert.cisli295.soty()
{
	if tdcms000.lnrp =  tdcms.lnrp.order then
		return(" ")
	endif

	return(tdsls051.ttyp)
}

function long convert.cisli295.soin()
{
	if tdcms000.lnrp =  tdcms.lnrp.order then

		return(0)
	endif

	return(tdsls051.invn)
}

function domain tcdsca convert.cisli295.desc()
{
	if tdcms000.lnrp =  tdcms.lnrp.order then
		return(empty.string(1;30))
	endif

	return(tiitm001.dsca)
}


|**************************TFACR510, CISLI210 *********************************
function domain tfgld.date get.invoice.date.of.interest.invoice()
{
	select tfacr200.docd:var.tfacr200.docd
	from tfacr200
	where tfacr200._index1 = {:tfacr510.ityp, :tfacr510.idoc,0,"",0,0}
	as set with 1 rows
	selectdo
	endselect

	return(var.tfacr200.docd)	

}

|function read.tcmcs036()
|{

	|* Read tax codes by country
|	select 	tcmcs036.*
|	from	tcmcs036
|	where	tcmcs036._index1 = {:tfacr200.vatc, :tfacr501.cvat}
|	selectdo
|		vat.code.found = true
|	selectempty

|		vat.code.found = false

|	endselect
|}


function calc.tax.amount.interest.invoice()                     
{
        |* This function calculates the tax amounts to be filled in cisli210
	

        if isspace(tfacr501.cvat) or (not vat.code.found) or

           (var.cisli210.ctyn = tcyesno.no) or
           tcmcs036.gnen <> tcgnen.nett then

                var.cisli210.txai = 0.00
                return
        endif
     						|#200-102112.so
|        var.cisli210.txai = calc.tax.amount(var.cisli210.txct, tfacr501.cvat, 
        					|#200-102112.eo
	       					|#200-102112.sn
        var.cisli210.txai = calculate.interest.tax.amount(var.cisli210.txct, tfacr501.cvat, 
        					|#200-102112.en        					
 	 	 	var.tfacr510.amnt, tfacr510.ccur, date.num(), 
			|*MCR - s
			|tfacr200.rate, 
			|tfacr200.ratf,  
			fill.mc.field.double(tfacr200.rate,1),
 	 	 	fill.mc.field.long(tfacr200.ratf,1),
			|*MCR - e
| 	 	 	 get.compnr(), tfacr510.cuno)		|#200-102112.o
 	 	 	 get.compnr(), tfacr510.cuno,crnd.orno)	|#200-102112.n
         if var.cisli210.txai <> 0.0 then
|*  MCR -  new changes .s         
|		for i = 1 to 3 		|*MCR 
|			calculate.amount.in.other.currency(var.cisli210.txai,
|						|*MCR - s
|						|tfacr200.rate,
|						|tfacr200.ratf,
|						fill.mc.field.double(tfacr200.rate,i),
| 	 	 	 	 	 	fill.mc.field.long(tfacr200.ratf,i),
|						|*MCR - e
|						tfacr510.ccur,
|						|*MCR - s
|						|var.cisli210.txah,
|						var.cisli210.txah(i),						
|						|*MCR - e
|						err.mess)	
|
|		endfor |*MCR 		
		for i = 1 to 3
			calculate.amount.in.home.currency.variables(var.cisli210.txai,
		 	 	 tfacr510.ccur,
				 fill.mc.field.double(tfacr200.rate,i),
				 fill.mc.field.long(tfacr200.ratf,i),
		 	 	 i,  
		 	 	 var.cisli210.txah(i), 	 	 	 	 	 	 	  
		 	 	 err.mess)
		 endfor	 	 
|*  MCR -  new changes .e 	 	
         else
		|*MCR - s
		|var.cisli210.txah = 0.0
		for i = 1 to 3
			var.cisli210.txah(i) = 0.0
		endfor
		|*MCR - e
         endif
}                                                               

function domain bccadr get.invoice.to.address(string  var.tfacr200.post.add(50))
{
	string	temp.key.ccor(50)

	domain bccadr	temp.var.cisli210.itoa
	domain bccadr	var.tccom010.cadr
	string	var.tfacr200.cuno(50)
	long   strlen1

	long   strlen2
	

	strlen1 = len(tfacr200.cuno)
	strlen2 = len(var.tfacr200.post.add)

	if not get.addresses("tccom010",tfacr200.cuno, var.str.err.mess) then
		var.tccom010.cadr = bcstc004.cadr
		var.cisli206.itcy = bcstc004.ccty			|#ravi.n

	endif

	if not isspace(var.tfacr200.post.add) then
		temp.key.ccor(1;strlen1) = var.tfacr200.cuno
		temp.key.ccor(strlen1+1;strlen2) = var.tfacr200.post.add
		
 	 	
		if not get.addresses("tccom012",temp.key.ccor, var.str.err.mess) then  
 	   	   	 temp.var.cisli210.itoa =  bcstc004.cadr  
			var.cisli206.itcy = bcstc004.ccty			|#ravi.n
 	   	else  

 	   	   	  temp.var.cisli210.itoa =  var.tccom010.cadr  

 	   	endif
	else
		temp.var.cisli210.itoa =  var.tccom010.cadr 	   
 	endif  

	return(temp.var.cisli210.itoa)
}
function read.customers.table()

{

	|* This function reads the Financial BP group and also the "Calculate tax'  field from the Customers table
  	select tccom010.cvyn:var.cisli210.ctyn,
	         tccom010.cfcg:var.cfcg
	from  tccom010
	where tccom010._index1 = {:tfacr510.cuno}	
	as set with 1 rows

	endselect


}

function get.rounding.code()
{
	|* Function to get the rounding code

	select	tcmcs002.crnd:temp.crnd
	from	tcmcs002
|	where	tcmcs002._index1 = {:tfacr200.ccur}	|#200-101240.o
	where	tcmcs002._index1 = {:tfacr510.ccur}	|#200-101240.n
	as set with 1 rows
	selectdo
		break
	endselect
}

function void read.tfacr501()
{
	|* Function to read the interest invoice related data so as to retrieve the number of days for interest calculation

	select tfacr501.*
	from tfacr501
	as set with 1 rows
	selectdo
		if tfacr501.dfic = tfacr.dfic.day.360 then
			var.tfacr501.intsel =  360
		else
			var.tfacr501.intsel =  365
		endif
		break

	endselect
	temp.txid	= tfacr501.cvat

}

function void get.interest.percentage()
{
	|* Function to retrieve the interest percentages
	
	if sch.present = false then
		var.tfacr510.days = tfacr510.days
	endif		  


	select  tfacr502.*   
            from   tfacr502  
 	where tfacr502.ficu =  :var.cfcg  
 	and     tfacr502.days <=  :var.tfacr510.days  
            order by tfacr502._index1 desc  	   	   	   	   

 	as set with 1 rows  	   	   	   	   	   
 	selectdo  
 	selecteos  
 	   	  var.tfacr510.iprc =  tfacr502.perc  
 	endselect  	   
}
function void store.schedule.data()

{
	|* Function to store the schedule amount and schedule date, if schedules are present for an interest invoice.
	|* Also sch.paid(..) is initialized to 'false'. This variable becomes true once a given schedule number is paid.

	
	size = 0
	select tfacr201.*
	from   tfacr201
	where  tfacr201._index1 = {:prev.ttyp,:prev.ninv}	
	selectdo

		size = size+1
		sch.amnt (size) = tfacr201.amnt
		sch.date (size) = tfacr201.recd
		sch.paid(size) = false 

		sch.present = true	
	selectempty
		sch.present = false
		
	endselect		
}

function void check.for.records.based.on.schedule.date()
{
	|* This function evaluates based on the receipt date, upto what schedule line number the calculations for interest should
	|* be done

	|* 'size' is the number of schedules present
	for i = 1 to size 

		if tfacr200.docd < sch.date(i) then
			check.rec = i-1
			break
		else
			if  tfacr200.docd = sch.date(i) then
				check.rec = i
				break
			endif
		endif
	endfor
	if check.rec = 0 then
		check.rec = size
	endif
}

function domain tcbool no.schedule.amount.left()
{
	|* This function performs the calculations when the given schedule line amount is exhausted

	if sch.paid(i) = true then
		return(true)
	|* Continue with the next schedule line


	else			

 	 	if double.cmp(tot.rec.amnt,sch.amnt(i),0.00001) = -1 then
			var.tfacr510.days = tfacr200.docd - sch.date(i)
			get.interest.percentage()
			discount.and.lps = find.discount.and.lps(tot.rec.amnt,sch.amnt(i))
			var.tfacr510.icam = tot.rec.amnt - discount.and.lps
			var.tfacr510.seqn = i
			sch.amnt.left = sch.amnt(i) - var.tfacr510.icam - discount.and.lps
   	                var.sum.tfacr510.icam =  var.sum.tfacr510.icam +  var.tfacr510.icam  
			var.tfacr510.amnt = (var.tfacr510.icam * var.tfacr510.days/var.tfacr501.intsel )*(var.tfacr510.iprc/100)
			var.sum.tfacr510.amnt = var.sum.tfacr510.amnt + var.tfacr510.amnt
			
		        if var.tfacr510.amnt <> 0.0 then
|*  MCR -  new changes .s		        
|		        	for i = 1 to 3 |*MCR .s
|	 	 	          	calculate.amount.in.other.currency (var.tfacr510.amnt,  
| 	 	 	 	 	 	 	 	 |tfacr200.rate,  
| 	 	 	 	 	 	 	 	 |tfacr200.ratf,  
| 	 	 	 	 	 	 	 	 tmp.mc.tfacr200.rate(i),
| 	 	 	 	 	 	 	 	 tmp.mc.tfacr200.ratf(i), 	 	 	 	 	 	 	 	 
| 	 	 	 	 	 	 	 	 tfacr510.ccur,  
| 	 	 	 	 	 	 	 	 |var.tfacr510.amth,  
| 	 	 	 	 	 	 	 	 var.tfacr510.amth(i),  
| 	 	 	 	 	 	 	 	 err.mess)  
|				endfor	|*MCR .e
				for i = 1 to 3
					calculate.amount.in.home.currency.variables(var.tfacr510.amnt,
			 	 	 tfacr510.ccur,
					 tmp.mc.tfacr200.rate(i),
 	 	 	 	 	 tmp.mc.tfacr200.ratf(i), 
			 	 	 i,  
			 	 	 var.tfacr510.amth(i), 	 	 	 	 	 	 	  
			 	 	 err.mess)
				endfor	 	 
|*  MCR -  new changes .e 	
				
			else
				|*MCR - s
				|var.tfacr510.amth = 0.0	
				for i = 1 to 3
					var.tfacr510.amth(i) = 0.0	
				endfor
				|*MCR - e				
		        endif

			get.rounding.code()
			  if tfacr510.iist =  tfacr.iist.selected then  
 	   	   	   	  dump 44  
 	   	   	  else  
				  var.count =  var.count +  1
				   no.of.records = no.of.records+1
				  calc.tax.amount.interest.invoice()
				 var.net.amti = var.net.amti + var.tfacr510.amnt
				 |*MCR .s
		 	 	 |var.net.amth =  var.net.amth +  var.tfacr510.amth  
		 	 	 for i = 1 to 3
		 	 	 	var.net.amth(i) =  var.net.amth(i) +  var.tfacr510.amth(i)
		 	 	 endfor
		 	 	 |*MCR .e
		 	 	 
		 	 	 var.tax.amti =  var.tax.amti +  var.cisli210.txai  
		 	 	 
		 	 	 |*MCR .s
		 	 	 |var.tax.amth =  var.tax.amth +  var.cisli210.txah 
		 	 	 for i = 1 to 3
		 	 	 	var.tax.amth(i) =  var.tax.amth(i) +  var.cisli210.txah(i) 
		 	 	 endfor
		 	 	 |*MCR .e
		 	 	 
				if tcmcs036.tvat = tctvat.shifted then 
				  var.shift.amti = var.shift.amti + var.cisli210.txai
				  |*MCR .s
				  |var.shift.amth = var.shift.amth + var.cisli210.txah
				  for i = 1 to 3
		 	 	 	var.shift.amth(i) = var.shift.amth(i) + var.cisli210.txah(i)
		 	 	 endfor
		 	 	 |*MCR .e
 	 	 	 	endif
 	   	   	   	  dump 22
				 get.financial.company.country(var.compnr)  				|#ravi.sn
 	 	 	 	|#var.cisli206.stcy =  var.cisli206.itcy  					
 	 	 	 	|#var.cisli206.sfcy =  var.fin.comp.country  
								|#ravi_array.so
|				update.fields.bcsci002.cisli206(var.compnr, tfacr510.ityp,			
|					tfacr510.idoc, var.cisli210.txct, var.cisli210.txid,
|					var.cisli210.txai, var.tfacr510.amnt,
|					tfacr200.rate, tfacr200.ratf, tfacr510.ccur,
|					var.fin.comp.country, var.cisli206.itcy,
|					var.fin.comp.country, var.cisli206.itcy, "", "")
|											|#ravi.en
								|#ravi_array.eo
								|#207750.so
								|#ravi_array.sn
|				 update.tax.amounts.array( var.cisli210.txid, 	
|					var.cisli210.txai, var.tfacr510.amnt)	
								|#ravi_array.en
								|#207750.eo
								|#207750.sn
				if update.tax.amounts.array( var.cisli210.txid, 	
					var.cisli210.txai, var.tfacr510.amnt) then
								|#207750.en

 	 	 	 	 get.tax.percentage(var.cisli210.txct, var.cisli210.txid, var.compnr, 	 | #  ravi.sn  
 	 	 	 	 	 	 var.tfacr200.docd, var.singular.tax, var.cisli209.pvat) | #  ravi.en  

							|ravi_cisli209.so
|				 update.fields.bcsci002.cisli209(var.cisli210.txai, tfacr510.ccur,  
| 	 	 	 	 	 	 	 var.tfacr200.docd,var.cisli.rtyp,tfacr200.rate,  
| 	 	 	 	 	 	 	 tfacr200.ratf, var.cisli209.pvat,tcmcs036.ctau.l,tfacr510.ityp,  
| 	 	 	 	 	 	 	 tfacr510.idoc, 1,var.compnr, tcmcs142.reas,tcmcs142.exno,
|							 var.tfacr510.amnt)  
							|ravi_cisli209.eo
							|ravi_cisli209.sn				
				
				update.row.in.cache.cisli209(				
					var.compnr,
					tfacr510.ityp,
					tfacr510.idoc,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					var.cisli210.txai,
					var.tfacr510.amnt,
	 	 	 	 	var.cisli206.rcod,
					tfacr510.ccur,  
	 	 	 	 	var.tfacr200.docd,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tfacr200.rate, |*MCR .o	 
	 	 	 	 	|tfacr200.ratf)	|*MCR .o	
	 	 	 	 	tmp.mc.tfacr200.rate,|*MCR .s
	 	 	 	 	tmp.mc.tfacr200.ratf)|*MCR .s
							|ravi_cisli209.en
					cisli209.dumped = true	|#207750.sn
				else
					cisli209.dumped = false
				endif				|#207750.en
																			
 	   	   	  endif  

			if sch.amnt.left = 0 then
				sch.paid(i) = true
			endif	
			|* Break from FOR loop		
			return(false)
		else
			var.tfacr510.days = tfacr200.docd - sch.date(i)
			get.interest.percentage()
			discount.and.lps = find.discount.and.lps(tot.rec.amnt,sch.amnt(i))
			var.tfacr510.icam = sch.amnt(i) - discount.and.lps
			var.tfacr510.seqn = i
			sch.amnt.left = sch.amnt(i) - var.tfacr510.icam - discount.and.lps

			rec.amnt.left = tot.rec.amnt - var.tfacr510.icam - discount.and.lps

                        var.sum.tfacr510.icam =  var.sum.tfacr510.icam +  var.tfacr510.icam  
			var.tfacr510.amnt = (var.tfacr510.icam * var.tfacr510.days/var.tfacr501.intsel )*(var.tfacr510.iprc/100)
			var.sum.tfacr510.amnt = var.sum.tfacr510.amnt + var.tfacr510.amnt
		         if var.tfacr510.amnt <> 0.0 then
|*  MCR -  new changes .s		         
|		            	for i = 1 to 3 |*MCR .n
|					calculate.amount.in.other.currency(var.tfacr510.amnt,								
|								 |tfacr200.rate,  |*MCR .o
| 	 	 	 	 	 	 	 	 |tfacr200.ratf,  |*MCR .o
| 	 	 	 	 	 	 	 	 tmp.mc.tfacr200.rate(i), |*MCR .n
| 	 	 	 	 	 	 	 	 tmp.mc.tfacr200.ratf(i), |*MCR .n
|								 tfacr510.ccur,
|								 |var.tfacr510.amth,|*MCR .o
|								 var.tfacr510.amth(i),|*MCR .n
|								 err.mess)	
|				endfor |*MCR .n	
				for i = 1 to 3
					calculate.amount.in.home.currency.variables(var.tfacr510.amnt,
			 	 	 tfacr510.ccur,
					 tmp.mc.tfacr200.rate(i),
 	 	 	 	 	 tmp.mc.tfacr200.ratf(i), 
			 	 	 i,  
			 	 	 var.tfacr510.amth(i), 	 	 	 	 	 	 	  
			 	 	 err.mess)
				endfor	 	 
|*  MCR -  new changes .e 	
							 

					
			else
				|*MCR .s
				|var.tfacr510.amth = 0.0
				for i = 1 to 3
					var.tfacr510.amth(i) = 0.0
				endfor
				|*MCR.e
				
		        endif

			get.rounding.code()
			  if tfacr510.iist =  tfacr.iist.selected then  
 	   	   	   	  dump 44  
 	   	   	  else  

				  var.count =  var.count +  1



				 no.of.records = no.of.records+1
				 calc.tax.amount.interest.invoice()
				 var.net.amti = var.net.amti + var.tfacr510.amnt
				|*MCR .s 
		 	 	|var.net.amth =  var.net.amth +  var.tfacr510.amth  
		 	 	for i = 1 to 3
		 	 		var.net.amth(i) =  var.net.amth(i) +  var.tfacr510.amth(i)
		 	 	endfor
		 	 	|*MCR .e
		 	 	var.tax.amti =  var.tax.amti +  var.cisli210.txai  
		 	 	|*MCR .s
		 	 	|var.tax.amth =  var.tax.amth +  var.cisli210.txah  
		 	 	for i = 1 to 3
		 	 		var.tax.amth(i) =  var.tax.amth(i) +  var.cisli210.txah(i)
		 	 	endfor
		 	 	|*MCR .e
				if tcmcs036.tvat = tctvat.shifted then
  			     	 var.shift.amti = var.shift.amti + var.cisli210.txai
  			     	|*MCR .s 
				|var.shift.amth = var.shift.amth + var.cisli210.txah
				for i = 1 to 3
					var.shift.amth(i) = var.shift.amth(i) + var.cisli210.txah(i)
				endfor
				|*MCR .e
				endif

 	 	 	 	dump 22 
				 get.financial.company.country(var.compnr)  				|#ravi.sn
 	 	 	 	|#var.cisli206.stcy =  var.cisli206.itcy  					
	 	 	 	|#var.cisli206.sfcy =  var.fin.comp.country  

								|#ravi_array.so
|				 update.fields.bcsci002.cisli206(var.compnr, tfacr510.ityp,			
|					tfacr510.idoc, var.cisli210.txct, var.cisli210.txid,
|					var.cisli210.txai, var.tfacr510.amnt,
|					tfacr200.rate, tfacr200.ratf, tfacr510.ccur,
|					var.fin.comp.country, var.cisli206.itcy,
|					var.fin.comp.country, var.cisli206.itcy, "", "")
								|#ravi_array.eo
								|#207750.so
								|#ravi_array.sn
|				 update.tax.amounts.array( var.cisli210.txid, 	
|					var.cisli210.txai, var.tfacr510.amnt)	
								|#ravi_array.en
								|#207750.eo
								|#207750.sn
				if update.tax.amounts.array( var.cisli210.txid, 	
					var.cisli210.txai, var.tfacr510.amnt) then
								|#207750.en	

 	 	 	 get.tax.percentage(var.cisli210.txct, var.cisli210.txid, var.compnr, 	 | #  ravi.sn  
	 	 	 	 	 var.tfacr200.docd, var.singular.tax, var.cisli209.pvat) | #  ravi.en  

							|#ravi_cisli209.so
|			 update.fields.bcsci002.cisli209(var.cisli210.txai, tfacr510.ccur,  
| 	 	 	 	 	 	 	 var.tfacr200.docd,var.cisli.rtyp,tfacr200.rate,  
| 	 	 	 	 	 	 	 tfacr200.ratf, var.cisli209.pvat,tcmcs036.ctau.l,tfacr510.ityp,  
| 	 	 	 	 	 	 	 tfacr510.idoc, 1,var.compnr, tcmcs142.reas,tcmcs142.exno,
|							 var.tfacr510.amnt)  
							|#ravi_cisli209.eo

								|#ravi.en
							
									
 					update.row.in.cache.cisli209(
						var.compnr,
						tfacr510.ityp,
						tfacr510.idoc,
						tcmcs036.ctau.l,
		 	 	 	 	var.cisli206.ceno,
						var.cisli209.pvat,
						var.cisli210.txai,
						var.tfacr510.amnt,
		 	 	 	 	var.cisli206.rcod,
						tfacr510.ccur,  
		 	 	 	 	var.tfacr200.docd,  
		 	 	 	 	var.cisli.rtyp,  
		 	 	 	 	|tfacr200.rate, |*MCR .o	 
						|tfacr200.ratf)	|*MCR .o	
						tmp.mc.tfacr200.rate,|*MCR .s
						tmp.mc.tfacr200.ratf)|*MCR .s
							|ravi_cisli209.en
					cisli209.dumped = true	|#207750.sn
				else
					cisli209.dumped = false
				endif				|#207750.en
	   	   	  endif  

			if sch.amnt.left = 0 then
				sch.paid(i) = true
			endif			
			while rec.amnt.left > 0 
				if i <= size then
					if i >= check.rec then

						if i+1 > size then
							return(false)						
						endif
						if double.cmp(sch.amnt(i+1),rec.amnt.left,0.00001) = -1 then
							discount.and.lps = find.discount.and.lps(tot.rec.amnt,sch.amnt(i+1))

							var.tfacr510.icam = sch.amnt(i+1) - discount.and.lps
							
						else
							|* Amount is received earlier and does not carry interest	
							sch.amnt.left = sch.amnt(i+1) - rec.amnt.left
							rec.amnt.left = 0
						|* Break from FOR loop
							return(false)
						endif
					else
						if double.cmp(sch.amnt(i+1),rec.amnt.left,0.00001) = -1 then
							discount.and.lps = find.discount.and.lps(tot.rec.amnt,sch.amnt(i+1))

							var.tfacr510.icam = sch.amnt(i+1) - discount.and.lps
						else
							discount.and.lps = find.discount.and.lps(tot.rec.amnt,rec.amnt.left)
							var.tfacr510.icam = rec.amnt.left - discount.and.lps
						endif
					endif
					var.tfacr510.days = tfacr200.docd - sch.date(i+1)
					get.interest.percentage()
					var.tfacr510.seqn = i + 1
					rec.amnt.left = rec.amnt.left - var.tfacr510.icam - discount.and.lps
					sch.amnt.left = sch.amnt(i+1) - var.tfacr510.icam - discount.and.lps
					var.tfacr510.amnt = (var.tfacr510.icam * var.tfacr510.days/var.tfacr501.intsel )*(var.tfacr510.iprc/100)
					var.sum.tfacr510.amnt = var.sum.tfacr510.amnt + var.tfacr510.amnt
				        if var.tfacr510.amnt <> 0.0 then
|*  MCR -  new changes .s				        
|				        	for i = 1 to 3 |*MCR .n
|							calculate.amount.in.other.currency(var.tfacr510.amnt,
|									|tfacr200.rate,  |*MCR .o
| 	 	 	 	 	 	 	 	 	|tfacr200.ratf,  |*MCR .o
| 	 	 	 	 	 	 	 	 	tmp.mc.tfacr200.rate(i),|*MCR .s
|									tmp.mc.tfacr200.ratf(i),|*MCR .s
|									tfacr510.ccur,
|									|var.tfacr510.amth, |*MCR .o
|									var.tfacr510.amth(i), |*MCR .n
|									err.mess)	
|						endfor |*MCR .n	
						for i = 1 to 3
							calculate.amount.in.home.currency.variables(var.tfacr510.amnt,
					 	 	 tfacr510.ccur,
							 tmp.mc.tfacr200.rate(i),
		 	 	 	 	 	 tmp.mc.tfacr200.ratf(i), 
					 	 	 i,  
					 	 	 var.tfacr510.amth(i), 	 	 	 	 	 	 	  
					 	 	 err.mess)
						endfor	 	 
|*  MCR -  new changes .e 			

				
					else
						|*MCR .s
						|var.tfacr510.amth = 0.0
						for i = 1 to 3
							var.tfacr510.amth(i) = 0.0
						endfor
						|*MCR .e
				
				        endif
				        var.sum.tfacr510.icam =  var.sum.tfacr510.icam +  var.tfacr510.icam  
					get.rounding.code()
					 if tfacr510.iist =  tfacr.iist.selected then  
 	   	   	   	 	 	dump 44  
 	   	   	  		else  
						var.count =  var.count +  1
						 no.of.records = no.of.records+1
						calc.tax.amount.interest.invoice()	
						 var.net.amti = var.net.amti + var.tfacr510.amnt
						 
						 |*MCR -s
				 	 	 |var.net.amth =  var.net.amth +  var.tfacr510.amth  
				 	 	 for i = 1 to 3
				 	 	 	var.net.amth(i) =  var.net.amth(i) +  var.tfacr510.amth(i)  
				 	 	 endfor
				 	 	 |*MCR -e
				 	 	 
				 	 	 var.tax.amti =  var.tax.amti +  var.cisli210.txai  
				 	 	 
				 	 	 |*MCR -s
				 	 	 |var.tax.amth =  var.tax.amth +  var.cisli210.txah  
				 	 	 for i = 1 to 3
				 	 	 	var.tax.amth(i) =  var.tax.amth(i) +  var.cisli210.txah(i)
				 	 	 endfor
				 	 	 |*MCR -e
				 	 	 
						 if tcmcs036.tvat = tctvat.shifted then
						 	 var.shift.amti = var.shift.amti + var.cisli210.txai
						 	|*MCR .s
						  	|var.shift.amth = var.shift.amth + var.cisli210.txah						  	
						  	for i = 1 to 3
						  		var.shift.amth(i) = var.shift.amth(i) + var.cisli210.txah(i)
						  	endfor
						  	|*MCR.e
						 endif

		 	 	 	 	

 	   	   	   	

			   		dump 22 


					 get.financial.company.country(var.compnr)  				|#ravi.sn
 	 			 	 	|#var.cisli206.stcy =  var.cisli206.itcy  					
 	 	 	 			|#var.cisli206.sfcy =  var.fin.comp.country  

								|#ravi_array.so
|						update.fields.bcsci002.cisli206(var.compnr, tfacr510.ityp,		
|							tfacr510.idoc, var.cisli210.txct, var.cisli210.txid,
|							var.cisli210.txai, var.tfacr510.amnt,
|							tfacr200.rate, tfacr200.ratf, tfacr510.ccur,
|							var.fin.comp.country, var.cisli206.itcy,
|							var.fin.comp.country, var.cisli206.itcy, "", "")
								|#ravi_array.eo
								|#207750.so
								|#ravi_array.sn
|					 update.tax.amounts.array( var.cisli210.txid, 	
|						var.cisli210.txai, var.tfacr510.amnt)	
							|#ravi_array.en
								|#207750.eo
								|#207750.sn
					if update.tax.amounts.array( var.cisli210.txid, 	
						var.cisli210.txai, var.tfacr510.amnt) then	
								|#207750.en			

		 	 	 	 get.tax.percentage(var.cisli210.txct, var.cisli210.txid, var.compnr, 	 | #  ravi.sn  
 	 	 	 		 	 var.tfacr200.docd, var.singular.tax, var.cisli209.pvat) | #  ravi.en  

							|#ravi_cisli209.so
|					 update.fields.bcsci002.cisli209(var.cisli210.txai, tfacr510.ccur,  
| 	 	 	 	 	 	 	 var.tfacr200.docd,var.cisli.rtyp,tfacr200.rate,  
| 	 	 	 	 	 	 	 tfacr200.ratf, var.cisli209.pvat,tcmcs036.ctau.l,tfacr510.ityp,  
| 	 	 	 	 	 	 	 tfacr510.idoc, 1,var.compnr, tcmcs142.reas,tcmcs142.exno,
|							 var.tfacr510.amnt)  
							|#ravi_cisli209.so
								|#ravi.en
						
							|ravi_cisli209.sn
					
						
							
					update.row.in.cache.cisli209(
						var.compnr,
						tfacr510.ityp,
						tfacr510.idoc,
						tcmcs036.ctau.l,
		 	 	 	 	var.cisli206.ceno,
						var.cisli209.pvat,
						var.cisli210.txai,
						var.tfacr510.amnt,
		 	 	 	 	var.cisli206.rcod,
						tfacr510.ccur,  
		 	 	 	 	var.tfacr200.docd,  
		 	 	 	 	var.cisli.rtyp,  
		 	 	 	 	|tfacr200.rate, |*MCR .o	 
						|tfacr200.ratf)	|*MCR .o	
						tmp.mc.tfacr200.rate,|*MCR .s
						tmp.mc.tfacr200.ratf)|*MCR .s
						|ravi_cisli209.en
						cisli209.dumped = true	|#207750.sn
				else
						cisli209.dumped = false
				endif					|#207750.en

 	   	   	 		 endif  

					if sch.amnt.left = 0 then
						sch.paid(i+1) = true
					endif				
					i = i + 1
				else

					rec.amnt.left = 0 
					|* Break from WHILE loop


					break
				endif					
			endwhile
			|* Break from FOR loop
			return(false)
						
		endif		
	endif

}

function domain tcbool schedule.amount.left()
{
	|* This function performs the calculations when some outstanding amount is left on the schedule line

	if sch.paid(i) = true then
	|* Continue with the next schedule line
		return(true)
	else		
		if double.cmp(tot.rec.amnt,sch.amnt.left,0.00001) = -1 then



			var.tfacr510.seqn = i
			var.tfacr510.days = tfacr200.docd - sch.date(i)
			get.interest.percentage()
			discount.and.lps = find.discount.and.lps(tot.rec.amnt,tot.rec.amnt)
			var.tfacr510.icam = tot.rec.amnt - discount.and.lps
			sch.amnt.left = sch.amnt.left - var.tfacr510.icam - discount.and.lps
                        var.sum.tfacr510.icam =  var.sum.tfacr510.icam +  var.tfacr510.icam  
			var.tfacr510.amnt = (var.tfacr510.icam * var.tfacr510.days/var.tfacr501.intsel )*(var.tfacr510.iprc/100)
			var.sum.tfacr510.amnt = var.sum.tfacr510.amnt + var.tfacr510.amnt
		            if var.tfacr510.amnt <> 0.0 then
|*  MCR -  new changes .s 						            
|		            		for i = 1 to 3 |*MCR
|						calculate.amount.in.other.currency(var.tfacr510.amnt,
|							|tfacr200.rate,  |*MCR .o
|							|tfacr200.ratf,  |*MCR .o
|							tmp.mc.tfacr200.rate(i),|*MCR .s
|							tmp.mc.tfacr200.ratf(i),|*MCR .s
|							tfacr510.ccur,
|							|var.tfacr510.amth,
|							var.tfacr510.amth(i),
|							err.mess)	
|					endfor |*MCR 	
					for i = 1 to 3
							calculate.amount.in.home.currency.variables(var.tfacr510.amnt,
					 	 	 tfacr510.ccur,
							 tmp.mc.tfacr200.rate(i),
		 	 	 	 	 	 tmp.mc.tfacr200.ratf(i), 
					 	 	 i,  
					 	 	 var.tfacr510.amth(i), 	 	 	 	 	 	 	  
					 	 	 err.mess)
					endfor	 	 
|*  MCR -  new changes .e 				

				
			else
				|MCR .s
				|var.tfacr510.amth = 0.0
				for i = 1 to 3
					var.tfacr510.amth(i) = 0.0
				endfor
				|*MCR .e
				
		        endif

			get.rounding.code()
			 if tfacr510.iist =  tfacr.iist.selected then  
 	   	   	   	  dump 44  
 	   	   	  else  
				  var.count =  var.count +  1
				   no.of.records = no.of.records+1
				  calc.tax.amount.interest.invoice()	
				 var.net.amti = var.net.amti + var.tfacr510.amnt
				 |*MCR .s
		 	 	 |var.net.amth =  var.net.amth +  var.tfacr510.amth  
		 	 	 for i = 1 to 3
		 	 	 	var.net.amth(i) =  var.net.amth(i) +  var.tfacr510.amth(i)
		 	 	 endfor	
		 	 	 |*MCR .e
		 	 	 var.tax.amti =  var.tax.amti +  var.cisli210.txai  
		 	 	 |*MCR .s
		 	 	 |var.tax.amth =  var.tax.amth +  var.cisli210.txah  
		 	 	 for i = 1 to 3
		 	 	 	var.tax.amth(i) =  var.tax.amth(i) +  var.cisli210.txah(i)
		 	 	 endfor
		 	 	 |*MCR .e	
				if tcmcs036.tvat = tctvat.shifted then
				  var.shift.amti = var.shift.amti + var.cisli210.txai
				  |*MCR .s
				  |var.shift.amth = var.shift.amth + var.cisli210.txah
				  for i = 1 to 3
				  	var.shift.amth(i) = var.shift.amth(i) + var.cisli210.txah(i)
				  endfor	
				  |*MCR .e
				endif
				 	 	 	
 	   	   	   	  dump 22 
				 	 get.financial.company.country(var.compnr)  				|#ravi.sn
 	 	 	 	|#var.cisli206.stcy =  var.cisli206.itcy  					
 	 	 	 	|#var.cisli206.sfcy =  var.fin.comp.country  

								|#ravi_array.so
|				 update.fields.bcsci002.cisli206(var.compnr, tfacr510.ityp,			
|					tfacr510.idoc, var.cisli210.txct, var.cisli210.txid,
|					var.cisli210.txai, var.tfacr510.amnt,
|					tfacr200.rate, tfacr200.ratf, tfacr510.ccur,
|					var.fin.comp.country, var.cisli206.itcy,
|					var.fin.comp.country, var.cisli206.itcy, "", "")
								|#ravi_array.eo
								|#207750.so
								|#ravi_array.sn
|				 update.tax.amounts.array( var.cisli210.txid, 	
|					var.cisli210.txai, var.tfacr510.amnt)	
								|#ravi_array.en
								|#207750.eo
												|#ravi.en
								|#207750.sn
				if update.tax.amounts.array( var.cisli210.txid, 	
					var.cisli210.txai, var.tfacr510.amnt) then
								|#207750.en	
	 	 	 	 get.tax.percentage(var.cisli210.txct, var.cisli210.txid, var.compnr, 	 | #  ravi.sn  
		 	 	 	 var.tfacr200.docd, var.singular.tax, var.cisli209.pvat) | #  ravi.en  


						|ravi_cisli209.so
|				 update.fields.bcsci002.cisli209(var.cisli210.txai, tfacr510.ccur,  
| 	 	 	 	 	 	 	 var.tfacr200.docd,var.cisli.rtyp,tfacr200.rate,  
| 	 	 	 	 	 	 	 tfacr200.ratf, var.cisli209.pvat,tcmcs036.ctau.l,tfacr510.ityp,  
| 	 	 	 	 	 	 	 tfacr510.idoc, 1,var.compnr, tcmcs142.reas,tcmcs142.exno,
|							 var.tfacr510.amnt)  
						|ravi_cisli209.eo

						|ravi_cisli209.sn											

				update.row.in.cache.cisli209(
					var.compnr,
					tfacr510.ityp,
					tfacr510.idoc,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					var.cisli210.txai,
					var.tfacr510.amnt,
	 	 	 	 	var.cisli206.rcod,
					tfacr510.ccur,  
	 	 	 	 	var.tfacr200.docd,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tfacr200.rate, |*MCR .o	 
					|tfacr200.ratf)	|*MCR .o	
					tmp.mc.tfacr200.rate,|*MCR .s
					tmp.mc.tfacr200.ratf)|*MCR .s
	 	 	 	 	
						|ravi_cisli209.en
					cisli209.dumped = true	|#207750.sn
				else
					cisli209.dumped = false
				endif
 	   	   	  endif  

			if sch.amnt.left = 0 then
				sch.paid(i) = true
			endif		
			|*  Break from FOR loop
			return(false)
		else
			var.tfacr510.seqn = i
			var.tfacr510.days = tfacr200.docd - sch.date(i)

			get.interest.percentage()

			discount.and.lps = find.discount.and.lps(tot.rec.amnt,sch.amnt.left)
			var.tfacr510.icam = sch.amnt.left - discount.and.lps
			sch.amnt.left = sch.amnt.left - var.tfacr510.icam - discount.and.lps
			rec.amnt.left = tot.rec.amnt - var.tfacr510.icam - discount.and.lps
		        var.sum.tfacr510.icam =  var.sum.tfacr510.icam +  var.tfacr510.icam  
			var.tfacr510.amnt = (var.tfacr510.icam * var.tfacr510.days/var.tfacr501.intsel )*(var.tfacr510.iprc/100)
			var.sum.tfacr510.amnt = var.sum.tfacr510.amnt + var.tfacr510.amnt
		            if var.tfacr510.amnt <> 0.0 then
|*  MCR -  new changes .s		            
|		            	for i = 1 to 3 |*MCR .n
|					calculate.amount.in.other.currency(var.tfacr510.amnt,
|							|tfacr200.rate,  |*MCR .o
|							|tfacr200.ratf,  |*MCR .o
|							tmp.mc.tfacr200.rate(i),|*MCR .s
|							tmp.mc.tfacr200.ratf(i),|*MCR .s
|							tfacr510.ccur,
|							|var.tfacr510.amth, |*MCR .o
|							var.tfacr510.amth(i), |*MCR .n
|							err.mess)	
|				endfor |*MCR .n
				for i = 1 to 3
					calculate.amount.in.home.currency.variables(var.tfacr510.amnt,
			 	 	 tfacr510.ccur,
					 tmp.mc.tfacr200.rate(i),
 	 	 	 	 	 tmp.mc.tfacr200.ratf(i), 
			 	 	 i,  
			 	 	 var.tfacr510.amth(i), 	 	 	 	 	 	 	  
			 	 	 err.mess)
				endfor	 	 
|*  MCR -  new changes .e 			
				
			else
				|*MCR .s
				|var.tfacr510.amth = 0.0
				for i = 1 to 3
					var.tfacr510.amth(i) = 0.0
				endfor
				|*MCR .e
				
		       endif


			get.rounding.code()
			 if tfacr510.iist =  tfacr.iist.selected then  
 	   	   	   	  dump 44  
 	   	   	  else  
				  var.count =  var.count +  1
				   no.of.records = no.of.records+1
				  calc.tax.amount.interest.invoice() 
				 var.net.amti = var.net.amti + var.tfacr510.amnt
				 
				 |*MCR .s
		 	 	 |var.net.amth =  var.net.amth +  var.tfacr510.amth  
		 	 	 for i = 1 to 3
		 	 	 	var.net.amth(i) =  var.net.amth(i) +  var.tfacr510.amth(i)  
		 	 	 endfor
		 	 	 |*MCR .e
		 	 	 
		 	 	 var.tax.amti =  var.tax.amti +  var.cisli210.txai  
		 	 	 
		 	 	 |*MCR .s
		 	 	 |var.tax.amth =  var.tax.amth +  var.cisli210.txah  
		 	 	 for i = 1 to 3
		 	 	 	var.tax.amth(i) =  var.tax.amth(i) +  var.cisli210.txah(i)  
		 	 	 endfor
		 	 	 |*MCR .e
				
				if tcmcs036.tvat = tctvat.shifted then
				 var.shift.amti = var.shift.amti + var.cisli210.txai
				 |*MCR .s
				 |var.shift.amth = var.shift.amth + var.cisli210.txah
				 for i = 1 to 3
		 	 	 	var.shift.amth(i) = var.shift.amth(i) + var.cisli210.txah(i)
		 	 	 endfor
		 	 	 |*MCR .e
				endif
 	 	 	

 	   	   	   	  dump 22 
				 	 get.financial.company.country(var.compnr)  				|#ravi.sn
 	 	 	 	|#var.cisli206.stcy =  var.cisli206.itcy  					
 	 	 	 	|#var.cisli206.sfcy =  var.fin.comp.country  

								|#ravi_array.so
|				 update.fields.bcsci002.cisli206(var.compnr, tfacr510.ityp,			
|					tfacr510.idoc, var.cisli210.txct, var.cisli210.txid,
|					var.cisli210.txai, var.tfacr510.amnt,
|					tfacr200.rate, tfacr200.ratf, tfacr510.ccur,
|					var.fin.comp.country, var.cisli206.itcy,
|					var.fin.comp.country, var.cisli206.itcy, "", "")
								|#ravi_array.eo
								|#207750.so
								|#ravi_array.sn
|				 update.tax.amounts.array( var.cisli210.txid, 	
|					var.cisli210.txai, var.tfacr510.amnt)	
								|#ravi_array.en
								|#207750.eo
								|#207750.sn
				if update.tax.amounts.array( var.cisli210.txid, 	
					var.cisli210.txai, var.tfacr510.amnt) then	
								|#207750.en
	 	 	 get.tax.percentage(var.cisli210.txct, var.cisli210.txid, var.compnr, 	 | #  ravi.sn  
	 	 	 	 	 var.tfacr200.docd, var.singular.tax, var.cisli209.pvat) | #  ravi.en  

						|ravi_cisli209.so
|			 update.fields.bcsci002.cisli209(var.cisli210.txai, tfacr510.ccur,  
| 	 	 	 	 	 	 	 var.tfacr200.docd,var.cisli.rtyp,tfacr200.rate,  
| 	 	 	 	 	 	 	 tfacr200.ratf, var.cisli209.pvat,tcmcs036.ctau.l,tfacr510.ityp,  
| 	 	 	 	 	 	 	 tfacr510.idoc, 1,var.compnr, tcmcs142.reas,tcmcs142.exno,
|							 var.tfacr510.amnt)  
						|ravi_cisli209.eo
									|#ravi.en
							
							|ravi_cisli209.sn
														
						
				update.row.in.cache.cisli209(	
					var.compnr,
					tfacr510.ityp,
					tfacr510.idoc,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					var.cisli210.txai,
					var.tfacr510.amnt,
	 	 	 	 	var.cisli206.rcod,
					tfacr510.ccur,  
	 	 	 	 	var.tfacr200.docd,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tfacr200.rate, |*MCR .o	 
					|tfacr200.ratf)	|*MCR .o	
					tmp.mc.tfacr200.rate,|*MCR .s
					tmp.mc.tfacr200.ratf)|*MCR .s
						
						
						|ravi_cisli209.en
					cisli209.dumped = true	|#207750.sn
				else
					cisli209.dumped = false
				endif				|#207750.en

 	   	   	  endif  

			if sch.amnt.left = 0 then
				sch.paid(i) = true
			endif		
			while rec.amnt.left > 0 

				if i <= size then

					if i >= check.rec then
						if i+1 > size then
							return(false)						
						endif

						if double.cmp(sch.amnt(i+1),rec.amnt.left,0.00001) = -1 then
							discount.and.lps = find.discount.and.lps(tot.rec.amnt,sch.amnt(i+1))

							var.tfacr510.icam = sch.amnt(i+1) - discount.and.lps
						else
							| means that the amount is received earlier and does not carry interest	
							sch.amnt.left = sch.amnt(i+1) - rec.amnt.left
							rec.amnt.left = 0
							|* Break from FOR loop 
							return(false)
						endif
					else
						if double.cmp(sch.amnt(i+1),rec.amnt.left,0.00001) = -1 then
							discount.and.lps = find.discount.and.lps(tot.rec.amnt,sch.amnt(i+1))	

							var.tfacr510.icam = sch.amnt(i+1) - discount.and.lps
						else
							discount.and.lps = find.discount.and.lps(tot.rec.amnt,rec.amnt.left)	
							var.tfacr510.icam = rec.amnt.left - discount.and.lps
						endif
					endif
					var.tfacr510.seqn = i + 1
					var.tfacr510.days = tfacr200.docd - sch.date(i+1)
					get.interest.percentage()
					rec.amnt.left = rec.amnt.left - (var.tfacr510.icam + discount.and.lps)
					sch.amnt.left = sch.amnt(i+1) - (var.tfacr510.icam + discount.and.lps)
					var.tfacr510.amnt = (var.tfacr510.icam * var.tfacr510.days/var.tfacr501.intsel)*(var.tfacr510.iprc/100)
					var.sum.tfacr510.amnt = var.sum.tfacr510.amnt + var.tfacr510.amnt
				            if var.tfacr510.amnt <> 0.0 then
|*  MCR -  new changes .s				            
|				            	for i = 1 to 3 |*MCR .n
|							calculate.amount.in.other.currency(var.tfacr510.amnt,
|								|tfacr200.rate, |*MCR .o
|								|tfacr200.ratf, |*MCR .o
|								tmp.mc.tfacr200.rate(i),|*MCR .s
|								tmp.mc.tfacr200.ratf(i),|*MCR .s
|								tfacr510.ccur,
|								|var.tfacr510.amth, |*MCR .o
|								var.tfacr510.amth(i), |*MCR .n
|								err.mess)	
|						endfor |*MCR .n
						for i = 1 to 3
							calculate.amount.in.home.currency.variables(var.tfacr510.amnt,
					 	 	 tfacr510.ccur,
							 tmp.mc.tfacr200.rate(i),
		 	 	 	 	 	 tmp.mc.tfacr200.ratf(i), 
					 	 	 i,  
					 	 	 var.tfacr510.amth(i), 	 	 	 	 	 	 	  
					 	 	 err.mess)
						endfor	 	 
|*  MCR -  new changes .e 		
				
					else
						|*MCR .s
						|var.tfacr510.amth = 0.0
						for i = 1 to 3
							var.tfacr510.amth(i) = 0.0
						endfor
						|*MCR .e
				
				         endif

					var.sum.tfacr510.icam =  var.sum.tfacr510.icam +  var.tfacr510.icam  
				            get.rounding.code()
					 if tfacr510.iist =  tfacr.iist.selected then  
 	   	   	   	  		dump 44  
 	   	   	  		else  
						var.count =  var.count +  1
						 no.of.records = no.of.records+1
						calc.tax.amount.interest.invoice()
						 var.net.amti = var.net.amti + var.tfacr510.amnt
						 |*MCR .s
				 	 	 |var.net.amth =  var.net.amth +  var.tfacr510.amth  
				 	 	 for i = 1 to 3 
				 	 	 	var.net.amth(i) =  var.net.amth(i) +  var.tfacr510.amth(i)
						 endfor 
						 |*MCR .e
				 	 	 
				 	 	 var.tax.amti =  var.tax.amti +  var.cisli210.txai  
				 	 	 |*MCR .s
				 	 	 |var.tax.amth =  var.tax.amth +  var.cisli210.txah  
				 	 	 for i = 1 to 3 
				 	 	 	var.tax.amth(i) =  var.tax.amth(i) +  var.cisli210.txah(i)  
						 endfor 
						 |*MCR .e
						 
						if tcmcs036.tvat = tctvat.shifted then
						  var.shift.amti = var.shift.amti + var.cisli210.txai
						  |*MCR .s
						  |var.shift.amth = var.shift.amth + var.cisli210.txah
						  for i = 1 to 3 
				 	 	 	var.shift.amth(i) =  var.shift.amth(i) +  var.cisli210.txah(i)  
						 endfor 
						 |*MCR .e
						  
		 	 	 	 	endif
						
	   	   	   	  		dump 22 
						 get.financial.company.country(var.compnr)  				|#ravi.sn
 	 			 	 	|#var.cisli206.stcy =  var.cisli206.itcy  					
 	 	 	 			|#var.cisli206.sfcy =  var.fin.comp.country  

								|#ravi_array.so
|						update.fields.bcsci002.cisli206(var.compnr, tfacr510.ityp,		
|							tfacr510.idoc, var.cisli210.txct, var.cisli210.txid,
|							var.cisli210.txai, var.tfacr510.amnt,
|							tfacr200.rate, tfacr200.ratf, tfacr510.ccur,
|							var.fin.comp.country, var.cisli206.itcy,
|							var.fin.comp.country, var.cisli206.itcy, "", "")
							|#ravi.en
								|#ravi_array.eo

								|#207750.so
 								|#ravi_array.sn
|						 update.tax.amounts.array( var.cisli210.txid, 	
|
|							var.cisli210.txai, var.tfacr510.amnt)	
								|#ravi_array.en
								|#207750.eo
								|#207750.sn
						if update.tax.amounts.array( var.cisli210.txid, 	

							var.cisli210.txai, var.tfacr510.amnt) then
								|#207750.en
	 	 	 	 	 	 get.tax.percentage(var.cisli210.txct, var.cisli210.txid, var.compnr, 	 | #  ravi.sn  
 	 	 	 	 	 	 	 	 var.tfacr200.docd, var.singular.tax, var.cisli209.pvat) | #  ravi.en  

							|ravi_cisli209.so
|						 update.fields.bcsci002.cisli209(var.cisli210.txai, tfacr510.ccur,  
| 	 	 	 	 	 	 	 var.tfacr200.docd,var.cisli.rtyp,tfacr200.rate,  
| 	 	 	 	 	 	 	 tfacr200.ratf, var.cisli209.pvat,tcmcs036.ctau.l,tfacr510.ityp,  
| 	 	 	 	 	 	 	 tfacr510.idoc, 1,var.compnr, tcmcs142.reas,tcmcs142.exno,
|							 var.tfacr510.amnt)  
							|ravi_cisli209.eo
						
							|ravi_cisli209.sn
							
													
							update.row.in.cache.cisli209(				
								var.compnr,
								tfacr510.ityp,
								tfacr510.idoc,
								tcmcs036.ctau.l,
				 	 	 	 	var.cisli206.ceno,
								var.cisli209.pvat,
								var.cisli210.txai,
								var.tfacr510.amnt,
				 	 	 	 	var.cisli206.rcod,
								tfacr510.ccur,  
				 	 	 	 	var.tfacr200.docd,  
				 	 	 	 	var.cisli.rtyp,  
				 	 	 	 	|tfacr200.rate, |*MCR .o	 
								|tfacr200.ratf)	|*MCR .o	
								tmp.mc.tfacr200.rate,|*MCR .s
								tmp.mc.tfacr200.ratf)|*MCR .s
				 	 	 	 	
							|ravi_cisli209.en
							cisli209.dumped = true	|#207750.sn
						else
							cisli209.dumped = false
						endif				|#207750.en


 	   	   	 		 endif  

					if sch.amnt.left = 0 then
						sch.paid(i+1) = true
					endif				
					i = i + 1
				else
					rec.amnt.left = 0 
					|* Break from WHILE loop
					break
				endif
			endwhile
			|* Break from FOR loop
			return(false)
		endif
	endif
}

|*******************************CISLI205**************************************

function domain tfgld.amnt find.discount.and.lps(
						domain tfgld.amnt tot.recp.amnt,
						domain tfgld.amnt applied.amnt)
{
	domain tfgld.amnt tot.discount
	domain tfgld.amnt app.discount
	domain tfgld.amnt tot.lps
	domain tfgld.amnt app.lps

	get.rounding.code()

	tot.discount = (-1 * tfacr200.dc1i)
	app.discount = applied.amnt * (tot.discount / tot.recp.amnt)

							|#200-101240.so	
|	app.discount = icom0001.domain.round(app.discount, "tfgld.amnt",
|						  tfacr200.ccur, temp.crnd,true)
							|#200-101240.eo
							|#200-101240.sn	
	app.discount = icom0001.domain.round(app.discount, "tfgld.amnt",
						  tfacr510.ccur, temp.crnd,true)
							|#200-101240.en
	tot.lps = (-1 * tfacr200.lapi)
	app.lps = applied.amnt * (tot.lps / tot.recp.amnt)
							|#200-101240.so
|	app.lps = icom0001.domain.round(app.lps, "tfgld.amnt",
|						  tfacr200.ccur, temp.crnd,true)
							|#200-101240.eo
							|#200-101240.sn
	app.lps = icom0001.domain.round(app.lps, "tfgld.amnt",
						  tfacr510.ccur, temp.crnd,true)
							|#200-101240.en	
	return(app.discount + app.lps)
}				


function fill.cisli205.fields(     domain tcncmp 	i.comp,		
			      domain tfgld.ttyp	i.ityp,		
			      domain tfgld.docn	i.idoc,		
			      long		i.brid,		
			      long		i.stat,		

			      string		i.bpid(9),		
			      string		i.addr(9),		
			      string		i.cofc(6),
			      domain tcccrs	i.ccrs,		
			      domain tccpay	i.cpay,
			      domain tcccty	i.txct,		

			      long		i.fcrt,		
			      long		i.niiv,		
			      long		i.nmsi,		
			      long		i.nprj,		
			      long		i.nslo,		
			      long		i.nsvo,		
			      long		i.nsco,		
			      domain tcccur	i.ccur,		
			      domain tfgld.date	i.idat,
			      double	 	i.amti,		
			      |double		i.amth,  |*MCR .o
			  ref double		i.amth(),|*MCR .n
		  	      double		i.txai,
			      |double	 	i.txah,	 |*MCR.o
			 ref  double	 	i.txah(),|*MCR .n
			      double		i.stai,
			      |double		i.stah,	 |*MCR .o
			 ref  double		i.stah(),|*MCR .o



			      |double		i.rate,	  |*MCR .o
			ref   double		i.rate(), |*MCR .n

			     |  long		i.ratf, |*MCR .o
			 ref    long		i.ratf(), |*MCR .n
			      domain tfgld.date     i.lpdt,		
			      double		i.scai,
			      |double	 	i.scah,	|*MCR .o	
			ref   double	 	i.scah(),|*MCR .n
			      double	 	i.gdai,		
			      double	 	i.csai,		
			      double	 	i.rmai,		

			      double		i.d1ai,
			      |double	 	i.d1ah,	|*MCR .o	
			ref    double	 	i.d1ah(), |*MCR.n
			      domain tfgld.date	i.d1dt,
			      double		i.d2ai,
			     |double	 	i.d2ah,	|*MCR.o
			ref   double	 	i.d2ah(),|*MCR.n
			      domain tfgld.date	i.d2dt,
			      double		i.d3ai,
			      |double	 	i.d3ah, |*MCR.o		
			ref    double	 	i.d3ah(),|*MCR .n
			      domain tfgld.date	i.d3dt,
			      string		i.paym(3),		
			      long		i.nreb,
			      double		i.dcai,
|			      double		i.tbai)			|#200-101927.o
									|#200-101927.sn
			      double		i.tbai,		
			      long		i.doct)
									|#200.101927.en		
{
	var.cisli205.sfcp = i.comp
	var.cisli205.ityp = i.ityp
	var.cisli205.idoc = i.idoc
	var.cisli205.scmp = i.comp
	var.cisli205.brid = i.brid
	var.cisli205.doct = 0
	var.cisli205.stat = i.stat
	var.cisli205.cpay = i.cpay
	var.cisli205.ccrs = i.ccrs
	var.cisli205.paym = i.paym
	var.cisli205.bpid = i.bpid
	var.cisli205.cadr = i.addr
	var.cisli205.cofc = i.cofc
	var.cisli205.txct = i.txct
	var.cisli205.fcrt = i.fcrt
	var.cisli205.niiv = i.niiv
	var.cisli205.nmsi = i.nmsi
	var.cisli205.nprj = i.nprj
	var.cisli205.nslo = i.nslo
	var.cisli205.nsvo = i.nsvo
	var.cisli205.nsco = i.nsco

|	var.cisli205.doct = 0				|#200-101927.o
	var.cisli205.doct = i.doct				|#200-101927.n
	var.cisli205.idat = i.idat
	var.cisli205.ccur = i.ccur
	if interest.invoice or rebate.invoice then
	
		|* if shifted tax is present , then the total amount = net amount (tax is not included,lps is included)
		if i.stai <> 0.0 then
			var.cisli205.amti = i.amti - i.txai
		else
			var.cisli205.amti = i.amti
		endif
		
		|*MCR.s
		|if i.stah <> 0.0 then
		for i = 1 to 3 
			if i.stah(i) <> 0.0 then						
				|var.cisli205.amth = i.amth - i.txah
				var.cisli205.amth(i) = i.amth(i) - i.txah(i) 				
			else
				|var.cisli205.amth = i.amth
				var.cisli205.amth(i) = i.amth(i)
			endif
		endfor	
		|*MCR .e
	else
		var.cisli205.amti = i.amti 
		|var.cisli205.amth = i.amth |*MCR .o
		|*MCR .sn
		for i =1 to 3
			var.cisli205.amth(i) = i.amth(i)
		endfor
		|*MCR .en
	endif
		
	var.cisli205.txai = i.txai
	|var.cisli205.txah = i.txah |*MCR .o
	var.cisli205.stai = i.stai
	|var.cisli205.stah = i.stah |*MCR .o
	|var.cisli205.rate = i.rate |*MCR .o
	|var.cisli205.ratf = i.ratf |*MCR .o
	var.cisli205.scdt = i.lpdt

	var.cisli205.scai = i.scai
	|var.cisli205.scah = i.scah |*MCR .o
	var.cisli205.gdai = i.gdai
	var.cisli205.csai = i.csai

	var.cisli205.rmai = i.rmai
	var.cisli205.d1ai = i.d1ai
	|var.cisli205.d1ah = i.d1ah |*MCR .o
	var.cisli205.d1dt = i.d1dt
	var.cisli205.d2ai = i.d2ai

	|var.cisli205.d2ah = i.d2ah |*MCR .o
	var.cisli205.d2dt = i.d2dt
	var.cisli205.d3ai = i.d3ai
	|var.cisli205.d3ah = i.d3ah |*MCR .o
	var.cisli205.d3dt = i.d3dt
	var.cisli205.nreb = i.nreb
	var.cisli205.dcai = i.dcai
	var.cisli205.tbai = i.tbai
	
	|*MCR . sn
	for i = 1 to 3
		var.cisli205.txah(i) = i.txah(i)
		var.cisli205.stah(i) = i.stah(i)
		var.cisli205.rate(i) = i.rate(i)
		var.cisli205.ratf(i) = i.ratf(i)
		var.cisli205.scah(i) = i.scah(i)
		var.cisli205.d1ah(i) = i.d1ah(i)
		var.cisli205.d2ah(i) = i.d2ah(i)
		var.cisli205.d3ah(i) = i.d3ah(i)
	endfor
	|*MCR .en
}

|********************** Tax calculation for Interest Invoices  *******************************
								|#200-102112.so

|function double calc.tax.amount(domain tcccty i.ccty, domain tccvat i.cvat, double i.amnt, domain tcccur i.ccur, 
| 	 	 	 domain tfgld.date i.date, domain tcrate i.rate, domain tcratf i.ratf,  
| 	 	 	 domain tcncmp i.comp, domain tccuno i.cuno)                     
|{
|
|    	get.crnd(i.ccur)
|
|		return(itfgld0017.calc.default.vat.amount(
|                                        i.ccty,
|                                        i.cvat,
|                                        i.amnt,
|                                        i.ccur,
|                                        i.date,
|                                        i.rate,
|                                        i.ratf,
|
|                                        |tccom000.ccur, |*MCR.o
|                                        get.home.currency.orig.comp(),|MCR.n
|                                        crnd.orno,
|			   		 tcyesno.no,
|
|                                        tcyesno.no, 
|			    		 0.0, 
|			    		 get.compnr(),
|                                        i.cuno, 
|			    		 "", 
|		   	    		 "",
|                                        "", 
|			    		 0,
|                                        0, empty, empty,
|                                        empty, empty, 0,
|                                        tcyesno.yes))
|}
								|#200-102112.eo
|********************** Tax calculation for Rebates *******************************

function double calc.tax.amount.rebates(domain tcccty i.ccty, 
					  domain tccvat i.cvat, 
				          double i.amnt, 
 	 	 	 		  domain tfgld.date i.date)
 	 		 
{
	tax.perc1 = 0
	line.tax.amount = 0
	
	select	tcmcs036.*
	from	tcmcs036
	where	tcmcs036._index1 = {:i.ccty,:i.cvat}
	selectdo
		

		if tcmcs036.smpl = tcyesno.yes and
		   tcmcs036.tvat <> tctvat.on.payments then
			calculate.single.vat.amount1(i.amnt,i.date)
			|* only single tax allowed for comm/rebt
		else
			return(0.0)

		endif

	endselect
	return(line.tax.amount)
}
function double calculate.single.vat.amount1(double i.amnt, 
 	 	 	 	     domain tfgld.date i.date)
{
							|ravi.perf_12.so
|	select	tcmcs032.*
|	from	tcmcs032
|	where	tcmcs032._index1 inrange
|				{:tcmcs036.ccty,:tcmcs036.cvat} and
|				{:tcmcs036.ccty,:tcmcs036.cvat,:i.date}
|	order by tcmcs032.ccty desc, tcmcs032.cvat desc, tcmcs032.edat desc
|	as set with 1 rows
|	selectdo
|		tax.perc1 = tcmcs032.pvat
|		line.tax.amount = i.amnt * tcmcs032.pvat /100.0
		|* Calculate total VAT amount		
|	endselect
							|ravi.perf_12.eo			

	static domain tcccty local.vatc			|#ravi.perf_12.sn
	static domain tccvat local.cvat
	static domain tfgld.date local.date

	if (tcmcs036.ccty = local.vatc) and 
	   (tcmcs036.cvat = local.cvat) and 
	   (i.date =  local.date) then
	
		tax.perc1 = tcmcs032.pvat
		line.tax.amount = i.amnt * tcmcs032.pvat /100.0
		return(line.tax.amount)
	else 
		local.vatc = tcmcs036.ccty
		local.cvat = tcmcs036.cvat
		local.date = i.date
	endif				


	select	tcmcs032.*

	from 	tcmcs032
	where	tcmcs032._index1 = {:tcmcs036.ccty, :tcmcs036.cvat}
		and tcmcs032.edat <= :i.date
	order by tcmcs032._index1 desc
	as set with 1 rows
	selectdo
		tax.perc1 = tcmcs032.pvat
		line.tax.amount = i.amnt * tcmcs032.pvat /100.0
		|* Calculate total VAT amount		
	endselect					|#ravi.perf_12.en		



	return(line.tax.amount)
	
}


|******************* Tax calculation for Service orders/Contracts/Sales orders *******************************
function double calc.tax.amount.logistics(domain tcccty i.ccty, 
					  domain tccvat i.cvat, 
					  domain tcccur i.ccur,
					  domain tcrate i.rate, 
					  domain tcratf i.ratf,
				          double i.amnt, 
				          domain tccuno i.cuno,
 	 	 	 		  domain tfgld.date i.date)
 	 		 
{
	tax.perc1 = 0
	line.tax.amount = 0

|	read.tcmcs036(i.ccty, i.cvat)			|#ravi.perf_7.n |ravi_array.o

|	read.home.currency()				|#ravi.perf.o
	
	select	tcmcs036.*					|#ravi.perf_7.so |ravi_array.sn
	from	tcmcs036
	where	tcmcs036._index1 = {:i.ccty,:i.cvat}
	selectdo
		if tcmcs036.smpl = tcyesno.yes then		|#ravi.perf_7.eo |ravi_array.en
|		if var.singular.tax then			|#ravi.perf_7.n  |ravi_array.o
			calculate.single.vat.amount(i.ccur,i.rate,i.ratf,i.amnt,i.cuno,i.date)
		else
			calculate.multiple.vat.amount(i.ccur,i.rate,i.ratf,i.amnt,i.cuno,i.date)
		endif
	endselect						|#ravi.perf_7.o		|ravi_array.n
	return(line.tax.amount)
}
function double calculate.single.vat.amount(domain tcccur i.ccur,
					    domain tcrate i.rate, 
					    domain tcratf i.ratf,
				            double i.amnt, 
				            domain tccuno i.cuno,
 	 	 	 		    domain tfgld.date i.date)
{
							|#ravi.perf_12.so
|	select	tcmcs032.*
|	from	tcmcs032
|	where	tcmcs032._index1 inrange
|				{:tcmcs036.ccty,:tcmcs036.cvat} and
|				{:tcmcs036.ccty,:tcmcs036.cvat,:i.date}
|	order by tcmcs032.ccty desc, tcmcs032.cvat desc, tcmcs032.edat desc
|	as set with 1 rows
							|#ravi.perf_12.eo
							|#ravi.perf_12.sn
	select	tcmcs032.*
	from	tcmcs032
	where	tcmcs032._index1 = {:tcmcs036.ccty, :tcmcs036.cvat}
		and tcmcs032.edat <= :i.date
	order by tcmcs032._index1 desc	
	as set with 1 rows
							|#ravi.perf_12.en

	selectdo
		adjust.pvat.for.exemption(tcmcs032.pvat, 
					   tcmcs032.pext.l,i.cuno,i.date) 
		tax.perc1 = tcmcs032.pvat
		tax.perc1 = round(tax.perc1, 3, 1)
		check.if.base.amount.exceeds(tcmcs032.atxb.l, i.amnt,i.rate,i.ratf,i.ccur)
		if base.exceeds1 then
				calculate.vat(
					line.tax.amount,
					i.amnt,
					tcmcs032.pvat,
					tcmcs032.atxb.l,
					tcmcs032.pext.l,
					i.rate,i.ratf,i.ccur)
		else					
			if tcmcs036.rnva = tcyesno.yes then	|#46370.sn
				line.tax.amount = icom0001.domain.round(
				i.amnt * tcmcs032.pvat /100.0, "tcamnt",i.ccur,crnd.orno,true)
			else					|#46370.en
				line.tax.amount = imcs0010.round.amnt(
				i.amnt * tcmcs032.pvat /100.0,i.ccur,crnd.orno)
				
			endif				
			
		endif	
		check.and.recalculate.vat.amounts(	|#glo0.s
				line.tax.amount,
				tcmcs032.amxt.l,
				i.rate,i.ratf,i.ccur)		|#glo0.e
		
	endselect
	return(line.tax.amount)
	
}

function double calculate.multiple.vat.amount(domain tcccur i.ccur,
					    domain tcrate i.rate, 
					    domain tcratf i.ratf,
				            double i.amnt, 
				            domain tccuno i.cuno,
 	 	 	 		    domain tfgld.date i.date)
{
	domain tcamnt vat.amount
        domain tcdate latest.date               | most recent VAT-date
        domain tcbool first.line                | flag for first line
	domain tcsern save.sern			| sequence number vat-lines
	domain tcamnt temp.line.tax.amount
								
	vat.amount  = 0
	latest.date = 0
	first.line  = true
	save.sern   = 0		
	temp.line.tax.amount = 0
							|#ravi.perf_12.so
|	select  tcmcs033.*, tcmcs035.clcm
|	from    tcmcs033, tcmcs035
|	where   tcmcs033._index2 inrange 
|			         {:tcmcs036.ccty,:tcmcs036.cvat} and
|			         {:tcmcs036.ccty,:tcmcs036.cvat,:i.date}
|	and     tcmcs033.cmba refers to tcmcs035
|	order by tcmcs033.ccty desc, tcmcs033.cvat desc, tcmcs033.sern asc,
|		tcmcs033.edat desc				
							|#ravi.perf_12.eo

							|#ravi.perf_12.sn				
	select  tcmcs033.*, tcmcs035.clcm
	from    tcmcs033, tcmcs035
	where   tcmcs033._index2 = {:tcmcs036.ccty,:tcmcs036.cvat}
	and 	tcmcs033.edat <= :i.date	
	and     tcmcs033.cmba refers to tcmcs035
	order by tcmcs033.ccty desc, tcmcs033.cvat desc, tcmcs033.sern asc,
		tcmcs033.edat desc				
							|#ravi.perf_12.en
	selectdo
		if tcmcs033.sern <> save.sern then		
			latest.date = tcmcs033.edat
			save.sern   = tcmcs033.sern		
		endif
		if tcmcs033.edat <> latest.date then
			continue				
		endif
		
		adjust.pvat.for.exemption(tcmcs033.pvat, 
					   tcmcs033.pext.l,i.cuno,i.date) 
		
		
		if tcmcs035.clcm = tcmcs.clcm.parallel then
			tax.perc1 = tax.perc1 + tcmcs033.pvat
			| Calculate VAT amount over base amount
			
			check.if.base.amount.exceeds(
				tcmcs033.atxb.l, i.amnt,i.rate,i.ratf,i.ccur)
					     
			
			if base.exceeds1 then
				calculate.vat(
					temp.line.tax.amount,
					i.amnt,
					tcmcs033.pvat,
					tcmcs033.atxb.l,
					tcmcs033.pext.l,
					i.rate,i.ratf,i.ccur)
		
			
			else
			
				if tcmcs036.rnva = tcyesno.yes then	|#46370.sn
					temp.line.tax.amount = icom0001.domain.round(
					i.amnt * tcmcs033.pvat /100.0, "tcamnt",i.ccur,crnd.orno,true)
				else					|#46370.en
					temp.line.tax.amount = imcs0010.round.amnt(
					i.amnt * tcmcs033.pvat /100.0,i.ccur,crnd.orno)
				
				endif				
			
			endif			
						
			
 		else
			tax.perc1 = tax.perc1 + tcmcs033.pvat +
			         ( tax.perc1 / 100 * tcmcs033.pvat )
						
			check.if.base.amount.exceeds(
				tcmcs033.atxb.l, line.tax.amount + i.amnt,i.rate,i.ratf,i.ccur)			
						
			if base.exceeds1 then
				calculate.vat(
					temp.line.tax.amount,
					i.amnt+line.tax.amount,
					tcmcs033.pvat,
					tcmcs033.atxb.l,
					tcmcs033.pext.l,
					i.rate,i.ratf,i.ccur)
			else
				if tcmcs036.rnva = tcyesno.yes then	|#46370.sn
					temp.line.tax.amount = icom0001.domain.round(
					(i.amnt+line.tax.amount) * tcmcs033.pvat /100.0, "tcamnt",i.ccur,crnd.orno,true)
				else					|#46370.en
					temp.line.tax.amount = imcs0010.round.amnt(
					(i.amnt+line.tax.amount) * tcmcs033.pvat /100.0,i.ccur,crnd.orno)
				
				endif				
			
			endif		
 		endif
		check.and.recalculate.vat.amounts(	|#glo0.s
				temp.line.tax.amount,
				tcmcs033.amxt.l,
				i.rate,i.ratf,i.ccur)
		line.tax.amount = line.tax.amount + temp.line.tax.amount				
		
	endselect
	return(line.tax.amount)
}

function adjust.pvat.for.exemption(ref domain tcpvat tax.percentage1,
				    ref domain tcpvat tax.percentage2,
				    domain tccuno i.cuno,
				    domain tfgld.date i.date)
{
	tax.exno = ""
	if (tax.percentage1 <> 0.0 or tax.percentage2 <> 0.0) and
	   read.tax.exemptions.for.customer(i.cuno,i.date) then
		tax.percentage1 = 0.0
		tax.percentage2 = 0.0
	endif
}
function domain tcbool read.tax.exemptions.for.customer(domain tccuno i.cuno,
							 domain tfgld.date i.date)
{
	table   ttcmcs142       |Tax Exemptions for Customer by 
				|Authority

	domain  tcmcs.ctau.l    tax.authority
	domain  tcbool	  tax.exempt

	tax.exempt = false
	if tcmcs036.smpl = tcyesno.yes then
		tax.authority = tcmcs036.ctau.l
	else
		tax.authority = tcmcs035.ctau.l
	endif
	select  tcmcs142.*
	from    tcmcs142
	where   tcmcs142._index1 = {:i.cuno, :tax.authority}
	and     tcmcs142.exdt >= :i.date
	as set with 1 rows
	selectdo
		if not isspace(tcmcs142.exno) then 
			tax.exno = tcmcs142.exno
			tax.exempt = true
		endif
	selectempty
			tax.exno = ""
	endselect
	return(tax.exempt)
}					
function check.if.base.amount.exceeds(
			domain  tcamnt  i.tax.base,
			domain  tcamnt  i.tot.amnt,
  		        domain tcrate 	i.rats, 
			domain tcratf 	i.ratf,
			domain tcccur 	i.ccur)
			
{
	
	domain  tcamnt  o.amth |*MCR .n
	
	base.exceeds1 = false
	if i.ratf = 0 then
		i.ratf = 1
	endif
	if i.rats = 0 then				
		i.rats = 1
	endif							
	if not ( i.ccur = home.ccur) then
		
		i.rats2 = 0.00001
		i.rats3 = 0.00001
		i.ratf2 = 1
		i.ratf2 = 1		
|*MCR .s
|	tdpur0001.convert.to.home.currency.and.round.amount(    
|					i.ccur,
|					i.tot.amnt,
|					date.num(),
|					2,
|					i.rats,
|					i.ratf,
|					"tcamnt",
|					i.tot.amnt)	
	convert.to.home.currency.and.round.amount(i.ccur,       
						i.tot.amnt,       
						date.num(),       
						2,            
						i.rats,           
						i.rats2,           
						i.rats3,           
						i.ratf,           
						i.ratf2,
						i.ratf3,
						"tcamnt", 
						o.amth)
						
|*MCR .e
								
	endif
	
	i.tot.amnt = o.amth |*MCR .n
	
	if i.tax.base > 0 and
	   abs(i.tot.amnt) > i.tax.base then
		base.exceeds1 = true
	endif
}
function calculate.vat(
		ref     domain  tcamnt	  io.tax.amnt,
			domain  tcamnt	  i.tot.amnt,
			domain  tcpvat	  i.pvat,
			domain  tcamnt	  i.tax.base,
			domain  tcpvat	  i.excess.rate,
			domain 	tcrate 	  i.rats, 
			domain 	tcratf 	  i.ratf,
			domain 	tcccur 	  i.ccur )
{
	long    sign
	domain  tcamnt  normal.rate.amount
	domain  tcamnt  excess.rate.amount
	domain  tcamnt  normal.tax
	domain  tcamnt  excess.tax
	domain  tcamnt  total.tax
	domain  tcamnt  t.net.amth
	domain  tcamnt	o.amth |*MCR.n

		
	i.rats2 = 0.00001
	i.rats3 = 0.00001
	i.ratf2 = 1
	i.ratf2 = 1
|*MCR .s								
|	tdpur0001.convert.to.home.currency.and.round.amount(
|					i.ccur,
|					i.tot.amnt,
|					date.num(),
|					2,
|					i.rats,
|					i.ratf,
|					"tcamnt",
|					i.tot.amnt)

	convert.to.home.currency.and.round.amount(i.ccur,       
						i.tot.amnt,       
						date.num(),       
						2,            
						i.rats,           
						i.rats2,           
						i.rats3,           
						i.ratf,           
						i.ratf2,
						i.ratf3,
						"tcamnt", 
						o.amth)
						
|*MCR .e
	
|	t.net.amth = t.base.amnt + t.csqz.amnt

	i.tot.amnt = o.amth |*MCR .n
	sign = i.tot.amnt / abs(i.tot.amnt)

	normal.rate.amount = sign * i.tax.base
	excess.rate.amount = sign * ( abs(i.tot.amnt) - i.tax.base )
	normal.tax = normal.rate.amount * i.pvat / 100

	excess.tax = excess.rate.amount * i.excess.rate / 100

	if tcmcs036.rnva = tcyesno.yes then
		total.tax = icom0001.domain.round( normal.tax + excess.tax,
			"tcamnt", home.ccur, home.crnd,true )
	else			    
		total.tax = imcs0010.round.amnt( normal.tax + excess.tax,
				home.ccur, home.crnd)
	endif		   
								
	tccri.dll0011.cal.amnt.in.def.curr.with.own.rates(	
						i.ccur,
						total.tax,
						i.rats,
						i.ratf,
						io.tax.amnt)
	io.tax.amnt = imcs0010.round.amnt(io.tax.amnt, i.ccur,crnd.orno)
	
}
function check.and.recalculate.vat.amounts(
		ref     domain  tcamnt	  io.tax.amnt,
			domain  tcamnt	  i.max.tax,
			domain 	tcrate 	  i.rats, 
			domain 	tcratf 	  i.ratf,
			domain 	tcccur 	  i.ccur )		
			
{
	|----------------------------------------------------------------------
	|* This routine checks if the vat amount exceeds the maximum
	|* vat amount and if so, the vat amount is made equal to the
	|* maximum vat amount and redistributes it amoung bvat amount
	|* and qvat amount. The max tax amount is absolute in HC.
	|----------------------------------------------------------------------
	long    sign
	domain  tcamnt  cvat.amnt.hc
	domain  tcamnt  save.cvat.amnt

	save.cvat.amnt = io.tax.amnt
								
|*MCR .s	
		i.rats2 = 0.00001
		i.rats3 = 0.00001
		i.ratf2 = 1
		i.ratf2 = 1	
|	tdpur0001.convert.to.home.currency.and.round.amount(	
|					i.ccur,
|					io.tax.amnt,
|					date.num(),
|					2,
|					i.rats,
|					i.ratf,
|					"tcamnt",
|					cvat.amnt.hc)
        convert.to.home.currency.and.round.amount(i.ccur,       
						io.tax.amnt,       
						date.num(),       
						2,            
						i.rats,           
						i.rats2,           
						i.rats3,           
						i.ratf,           
						i.ratf2,
						i.ratf3,
						"tcamnt", 
						cvat.amnt.hc)
						
|*MCR .e							

	if not ( i.max.tax > 0 and abs(cvat.amnt.hc) > i.max.tax ) then
		return
	endif
	sign = io.tax.amnt / abs(io.tax.amnt)
	cvat.amnt.hc = sign * i.max.tax
								
	tccri.dll0011.cal.amnt.in.def.curr.with.own.rates(	
						i.ccur,
						cvat.amnt.hc,
						i.rats,
						i.ratf,
						io.tax.amnt)
	io.tax.amnt = imcs0010.round.amnt(io.tax.amnt, i.ccur,crnd.orno)
								

	cvat.amnt.hc = sign * i.max.tax
	
}

function read.home.currency()					
{
	select  tccom000.ccur:home.ccur,
		tcmcs002.crnd:home.crnd
	from    tccom000,
		tcmcs002
	where   tccom000._index1 = { :1 }
	and     tccom000.ccur refers to tcmcs002
	wherebind (1, get.compnr())
	selectdo
	endselect
}
|*********************** Base for Tax calculation ********************************************

function double calc.tax.base.amount(domain tccpay i.cpay, double i.line.amnt,
					double i.odis.amnt, domain tcccrs i.ccrs,
					domain tcccur i.ccur)
{
	double i.csqz.amnt, i.base.amnt
	static domain tccpay local.cpay			|#ravi.perf.sn
	
	domain	tcbool first.call

	if i.cpay = local.cpay then
		first.call = false
	else
		local.cpay = i.cpay
		first.call = true
	endif						

if first.call then					|#ravi.perf.en
	select	tcmcs013.*
	from	tcmcs013
	where	tcmcs013._index1 = {:i.cpay}
	as set with 1 rows
	selectdo
	selectempty
		tcmcs013.pdin = tcyesno.no
		tcmcs013.prca = 0
	endselect
endif						|#ravi.perf.n
	if i.ccrs <> empty.string(1;3) then

		select tcmcs011.perc
		from tcmcs011
		where tcmcs011.ccrs = :i.ccrs
		as set with 1 rows
		selectdo
		endselect

		i.csqz.amnt = (i.line.amnt - i.odis.amnt) * tcmcs011.perc / 100.0

	else
		i.csqz.amnt = 0.0
	endif

	
	if tcmcs013.pdin = tcyesno.yes then

		i.base.amnt = (i.line.amnt - i.odis.amnt) * (1 - tcmcs013.prca /100)
	else
		i.base.amnt = i.line.amnt - i.odis.amnt
	endif

	get.crnd(i.ccur)
	return(icom0001.domain.round((i.base.amnt + i.csqz.amnt),"tfgld.amnt", i.ccur, crnd.orno))

}


function get.invoice.to.address.proj.customer(domain tccuno i.cuno)

{
	string		temp.key.ccor(9)

	var.temp.cisli206.itcy = ""
	select    tccom010.cuno,tccom010.ccor
	from 	tccom010
	where 	tccom010._index1 =  {:i.cuno}  
	as set with 1 rows  
	selectdo
		if get.addresses("tccom010", tccom010.cuno, err.mess) then
			 var.tccom010.cadr =  ""
|			 var.cisli206.itcy = ""		|#ravi.n
		               var.temp.cisli206.itcy = ""		|#ravi.n
		else
			 var.tccom010.cadr =  bcstc004.cadr
|			 var.cisli206.itcy = bcstc004.ccty		|#ravi.n
			var.temp.cisli206.itcy = bcstc004.ccty		|#ravi.n
		endif
		
	 	if not isspace(tccom010.ccor) then
	 	 	 temp.key.ccor(1;6) =  tccom010.cuno  
	 	 	 temp.key.ccor(7;9) =  tccom010.ccor  
	 	 	 if get.addresses("tccom012", temp.key.ccor, err.mess)then
	 	 	 	 var.tccom110.cadr =  var.tccom010.cadr
				 
	 	 	 else  
	 	 	 	 var.tccom110.cadr =  bcstc004.cadr
|				 var.cisli206.itcy = bcstc004.ccty	|#ravi.n	
				var.temp.cisli206.itcy = bcstc004.ccty	|#ravi.n
	 	 	 endif  
	 	else
	 	 	 var.tccom110.cadr =  var.tccom010.cadr  
	 	endif  
	endselect
}

function insert.new.record.in.bcstp001(domain tppdm.cprj i.cprj,
				       domain tccuno     i.cuno, 
				       domain tppdm.stin i.stin,	

				       domain tppdm.ninv i.ninv)
{
	select	bcstp001.cprj
	from	bcstp001
	where	bcstp001._index1 = {:i.cprj,:i.cuno}
	selectdo
		return
	selectempty
		db.set.to.default(tbcstp001)
		bcstp001.cprj = i.cprj
		bcstp001.cuno = i.cuno
		bcstp001.stin = i.stin
		bcstp001.ninv = i.ninv
		db.insert(tbcstp001,db.retry)
		commit.transaction()
	endselect
	return
}
|******************************************** CISLI206*****************************************
											|#ravi.sn
function get.financial.company.country(domain	tcncmp i.fin.comp)
{
	static domain tcncmp local.comp			|#ravi.perf.sn
	
	if i.fin.comp = local.comp then
		return
	else
		local.comp = i.fin.comp
	endif						|#ravi.perf.en

	var.fin.comp.country = ""
	select 	tccom000.ccty
	from	tccom000
	where 	tccom000.ncmp = :i.fin.comp
	as set with 1 rows
	selectdo
		var.fin.comp.country = tccom000.ccty
	selectempty
		var.fin.comp.country = ""
	endselect
}
Function calculate.tax.amounts()
{
								|#binda.sn

	if tdsls045.pono <> 0 then

	 	 get.tax.by.country.code (  
	 	 	 	 tdsls041.ccty,  
 	 	 	 	 tdsls041.cvat,  
 	 	 	 	 new.txct,  
 	 	 	 	 new.txid,  
 	 	 	 	 err.mess)  
								|#binda.sn
	else
		get.tax.code.and.country.from.tdsls048()

		get.tax.by.country.code (  
	 	 	 	 inst.ccty,  
 	 	 	 	 inst.cvat,  
 	 	 	 	 new.txct,  
 	 	 	 	 new.txid,  
 	 	 	 	 err.mess)  

	endif
								|#binda.en	

 	 var.cisli245.amti =  calc.tax.base.amount (tdsls040.cpay, tdsls045.amnt, tdsls045.odam,  
 	 	 	 	 	 	 	 tdsls040.ccrs, tdsls040.ccur)  
	if new.txid <>  "" then
 	 
		 if tdsls045.txam.l <> 0 then
			var.cisli245.txai = tdsls045.txam.l
		 else
 
			if tdsls045.pono <> 0 then 

 	 			var.cisli245.txai =  calc.tax.amount.logistics(new.txct, new.txid,tdsls040.ccur,
 	 					|tdsls045.rats,tdsls045.ratf,
 	 					tmp.mc.tdsls045.rats(1),|*MCR
 	 					tmp.mc.tdsls045.ratf(1),|*MCR
 	 					var.cisli245.amti,tdsls045.cuno,tdsls045.invd)
			else
				var.cisli245.txai =  calc.tax.amount.logistics(new.txct, new.txid,tdsls040.ccur,
						|tdsls040.rats,tdsls040.ratf,
						tmp.mc.tdsls045.rats(1),|*MCR
 	 					tmp.mc.tdsls045.ratf(1),|*MCR
						var.cisli245.amti,tdsls045.cuno,tdsls045.invd)
			
			endif
		 endif

 	 	 if var.cisli245.txai <>  0.0 then 
|*  MCR -  new changes .s 	 	  
| 	 	 	for  i = 1 to 3
| 	 	 	 	calculate.amount.in.other.currency (var.cisli245.txai,
| 	 	 	 		|tdsls045.rats, tdsls045.ratf,  
| 	 	 	 		tmp.mc.tdsls045.rats(i),|*MCR
| 	 				tmp.mc.tdsls045.ratf(i),|*MCR
| 	 	 	 	 	tdsls040.ccur,
| 	 	 	 	 	|var.cisli245.txah,
| 	 	 	 	 	var.cisli245.txah(i), |*MCR
| 	 	 	 	 	err.mess) 	 	   	 	 	 	 	
| 	 	 	endfor 	 	 
			for i = 1 to 3
				calculate.amount.in.home.currency.variables(var.cisli245.txai,
		 	 	 tdsls040.ccur,
				 tmp.mc.tdsls045.rats(i),
 	 	 	 	 tmp.mc.tdsls045.ratf(i), 
		 	 	 i,  
		 	 	 var.cisli245.txah(i), 	 	 	 	 	 	 	  
		 	 	 err.mess)
			endfor	 	 
|*  MCR -  new changes .e 		
 	 	 else  
 	 	 	 |*MCR .s
 	 	 	 |var.cisli245.txah =  0.0  
 	 	 	 for i = 1 to 3
 	 	 	 	var.cisli245.txah(i) =  0.0  
 	 	 	 endfor
 	 	 	 |*MCR .e
 	 	 endif  
	else
		var.cisli245.txai = 0.0
		|*MCR .s
 	 	|var.cisli245.txah =  0.0  
 	 	for i = 1 to 3
 	 		var.cisli245.txah(i) =  0.0  
 	 	endfor
 	 	|*MCR .e
	endif


}
								|binda.sn
function get.tax.code.and.country.from.tdsls048()
{
	inst.cvat = empty.string(1;9)
	inst.ccty = empty.string(1;3)


	select tdsls048.cvat:inst.cvat,tdsls048.ccty:inst.ccty
	from   tdsls048
	where  tdsls048._index1 = {:tdsls045.orno,:tdsls045.srnb,0}
	as set with 1 rows
	selectdo
	endselect

}

								|binda.en	

function read.warehouse.address(
				domain	tcncmp	i.slcp,
				domain	tccwar	i.cwar)
{
							|binda21-07.sn
	static domain tcccty s.warehouse.country	|WR PERF add
	static domain tcncmp s.slcp			|WR PERF add
	static domain tccwar s.cwar			|WR PERF add
	
	|WR PERF statics added
	if s.cwar <> i.cwar or s.slcp <> i.slcp then
							|binda21-07.en

		select	tcmcs003.ccty:warehouse.country
		from	tcmcs003
		where	tcmcs003._index1 = { :i.cwar }
		and	tcmcs003._compnr = :i.slcp
		selectdo
			break
		selectempty
			warehouse.country = ""
		endselect
							|binda21-07.sn
		s.warehouse.country = warehouse.country
		s.cwar = i.cwar
		s.slcp = i.slcp
	else
		warehouse.country = s.warehouse.country
	endif
							|binda21-07.en		

}



function get.tax.code.details(
		domain 	tcccty		i.ccty,
		domain 	tccvat		i.cvat,
		domain	tfgld.date	i.date)
{
	select tcmcs036.*,tcmcs142.*,tcmcs032.*
	from   tcmcs036, tcmcs142,tcmcs032
	where  tcmcs036._index1 = {:i.ccty,:i.cvat,:i.date}
|	where  tcmcs036._index1 = {:tdsls041.ccty,:tdsls041.cvat}
	and    tcmcs032._index1 = {:tcmcs036.ccty,:tcmcs036.cvat}
	and    :tcmcs032.edat <= :tdsls045.invd 		
	and    tcmcs142._index1 = { :tdsls041.cuno,
				    :tcmcs036.ctau.l
				  }
	and    :i.date <= tcmcs142.exdt
	selectdo
		var.cisli206.rcod = tcmcs142.reas
		var.cisli206.ceno = tcmcs142.exno
	endselect
}	

function fill.cisli206.fields(domain tcncmp 	i.fcmp,
			 domain tfgld.ttyp	i.ityp,		
			 domain tfgld.docn	i.idoc,		
			 domain tcccty	i.txct,
			 domain tccvat	i.txid,
			 domain tcfovn	i.ceno,
			 domain tcncmp	i.scmp,
			 domain tfgld.amnt  i.tbai,
			 domain tccdis	i.rcod,
			 |domain tfgld.amnt  i.tbah, |*MCR.o
		ref	 domain tfgld.amnt  i.tbah(),|*MCR.e
			 domain tfgld.amnt  i.txai,
			 |domain tfgld.amnt  i.txah,|*MCR.o
		ref	 domain tfgld.amnt  i.txah(),|*MCR.e
			 domain tcccty	i.sfcy,
			 domain tcccty	i.stcy,
			 domain tcccty	i.ifcy,
			 domain tcccty	i.itcy)
{
	var.cisli206.fcmp = i.fcmp
	var.cisli206.ityp = i.ityp
	var.cisli206.ninv = i.idoc
	var.cisli206.txct = i.txct
	var.cisli206.txid = i.txid
	var.cisli206.ceno = i.ceno
	var.cisli206.tbai = i.tbai
	var.cisli206.rcod = i.rcod
	|var.cisli206.tbah = i.tbah |*MCR .o
	|*MCR .sn
		for i = 1 to 3
			var.cisli206.tbah(i) = i.tbah(i)
		endfor
	|*MCR .en	
	var.cisli206.txai = i.txai
	|var.cisli206.txah = i.txah |*MCR .o
	|*MCR .sn
		for i = 1 to 3
			var.cisli206.txah(i) = i.txah(i)
		endfor
	|*MCR .en
	var.cisli206.sfcy = i.sfcy
	var.cisli206.stcy = i.stcy
	var.cisli206.ifcy = i.ifcy
	var.cisli206.itcy = i.itcy
}

function fill.cisli209.fields(domain tcncmp 	i.fcmp,
			 domain tfgld.ttyp	i.ityp,		
			 domain tfgld.docn	i.idoc,		
			 domain tcmcs.ctau.l	i.ctau,
			 domain tcdsca  i.dsca,  
			 domain tcmcs.long i.ctyp,
			 domain tcfovn	i.ceno,
			 domain tcpvat i.perc,
			 domain tfgld.amnt  i.txai,
			 |domain tfgld.amnt  i.txah,|*MCR .o
		ref 	domain tfgld.amnt  i.txah(), |*MCR .n
 		  	 domain tccdis	i.rcod,
			 domain tfgld.amnt  i.tbai,
			 |domain tfgld.amnt  i.tbah) |*MCR.o
		ref	 domain tfgld.amnt  i.tbah()) |*MCR .n
{
	long elem

	var.cisli209.sfcp = i.fcmp
	var.cisli209.ityp = i.ityp
	var.cisli209.idoc = i.idoc
	var.cisli209.ctau = i.ctau
	var.cisli209.dsca = i.dsca
	var.cisli209.ctyp = i.ctyp
	var.cisli209.exno = i.ceno
	var.cisli209.perc = i.perc
	var.cisli209.txai = i.txai
	|var.cisli209.txah = i.txah |*MCR .o
	var.cisli209.rcod = i.rcod
	var.cisli209.tbai = i.tbai
	|var.cisli209.tbah = i.tbah |*MCR .o
	
	|*MCR - s
|	for i = 1 to 3					|binda.o
	for elem = 1 to 3					|binda.o
		var.cisli209.txah(elem) = i.txah(elem)
		var.cisli209.tbah(elem) = i.tbah(elem)
	endfor
	|*MCR - e
	
}

function initialize.cisli206.variables()
{
	var.cisli206.fcmp = 0
	var.cisli206.ityp = ""
	var.cisli206.ninv = 0
	var.cisli206.txct = ""
	var.cisli206.txid = ""
	var.cisli206.ceno = ""
	var.cisli206.scmp = 0
	var.cisli206.tbai = 0.0
	var.cisli206.rcod = ""
	|var.cisli206.tbah = 0.0
	var.cisli206.txai = 0.0
	|var.cisli206.txah = 0.0
	var.cisli206.sfcy = ""
	var.cisli206.stcy = ""
	var.cisli206.ifcy = ""
	var.cisli206.itcy = ""
	var.cisli209.sfcp = 0
	var.cisli209.ityp = ""
	var.cisli209.idoc = 0
	var.cisli209.ctau = ""
	var.cisli209.dsca = ""
	var.cisli209.ctyp = 0
	var.cisli209.exno = ""
	var.cisli209.perc = 0.0
	var.cisli209.txai = 0.0
	|var.cisli209.txah = 0.0

	var.cisli209.rcod = ""
	var.cisli209.tbai = 0.0
	|var.cisli209.tbah = 0.0
	
	|*MCR .sn
		for i = 1 to 3
			var.cisli206.tbah(i) = 0.0
			var.cisli206.txah(i) = 0.0
			var.cisli209.tbah(i) = 0.0
			var.cisli209.txah(i) = 0.0
		endfor
	|*MCR .en

}

function read.tcmcs032(	domain 	tcccty	i.vatc,
		       	domain 	tccvat	i.cvat,
			domain	tfgld.date	i.date)	
	
{
	static domain tcccty local.vatc			|#ravi.perf.sn
	static domain tccvat local.cvat
	static domain tfgld.date local.date
	if i.vatc = local.vatc and i.cvat = local.cvat and i.date =  local.date then
		return
	else 
		local.vatc = i.vatc
		local.cvat = i.cvat
		local.date = i.date
	endif						|#ravi.perf.en

	select	tcmcs032.*
	from 	tcmcs032
	where	tcmcs032._index1 = {:i.vatc, :i.cvat}
		and tcmcs032.edat >= :i.date
	as set with 1 rows
	selectdo
	endselect
}

function read.tcmcs142(	domain 	tccuno	i.cuno,
			domain	tcmcs.ctau.l	i.ctau.l,
			domain	tfgld.date	i.date)
{
	static	domain	tccuno	local.cuno			|#ravi.perf_10.sn
	static	domain	tcmcs.ctau.l	local.ctau.l
	static	domain	tfgld.date	local.date

	if (i.cuno = local.cuno) and (i.ctau.l = local.ctau.l) and (i.date = local.date) then
		return
	else
		local.cuno = i.cuno
		local.ctau.l = i.ctau.l
		local.date = i.date
	endif							|#ravi.perf_10.en

	select	tcmcs142.*
	from	tcmcs142
	where	tcmcs142._index1 = {:i.cuno, :i.ctau.l, :i.date}
	as set with 1 rows	selectdo
	endselect	
}	
function read.tcmcs036(
			domain 	tcccty	i.vatc,
		       	domain 	tccvat	i.cvat)
{
	|* Read tax codes by country
	
	static domain tcccty local.vatc			|#ravi.perf.sn
	static domain tccvat local.cvat
	
	if i.vatc = local.vatc and i.cvat = local.cvat then
		return
	else 
		local.vatc = i.vatc
		local.cvat = i.cvat
	endif						|#ravi.perf.en


	var.singular.tax =  false
	vat.code.found = false				|#binda.n

	select 	tcmcs036.*
	from	tcmcs036
	where	tcmcs036._index1 = {:i.vatc, :i.cvat}
	as set with 1 rows				|#ravi.perf_11.n
	selectdo
		vat.code.found = true
		if tcmcs036.smpl = tcyesno.yes then
			var.singular.tax =  true
		else
			var.singular.tax =  false
		endif
	selectempty
		vat.code.found = false
	endselect

}
function get.tax.percentage(
		domain	tcccty		i.tax.country,
		domain	tccvat		i.tax.code,
		domain	tcncmp		i.company,
		domain	tfgld.date	i.date,
		domain	tcbool		i.singular.tax,
	ref	domain	tfgld.perc	o.total.perc)
{
	|**************************************************
	|* This function calculates the tax percentage
	|* of a certain tax code...
	|**************************************************
	domain	tfgld.perc	multiple.tax.perc

	|* Variables for reading tcmcs035 ( Multiple tax codes )
	domain	tcccty		dummy.country
	domain	tccvat		dummy.vat.code
	domain 	tcsern		sequence.no
	domain 	tcmcs.clcm	calc.method
	domain 	tcmcs.ctau.l	tax.authority
	domain	tcyesno		purchase.tax


							|binda21-07.sn
	static domain tcccty s.tax.country	|WR PERF add
	static domain tccvat s.tax.code		|WR PERF add
	static domain tfgld.perc s.total.perc	|WR PERF add
							|binda21-07.en


	o.total.perc = 0
|	if i.singular.tax = true then			|#ravi.perf_11.o
	if var.singular.tax = true then			|#ravi.perf_11.n
		|***************************
		|* Single Tax Code
		|***************************
|		select	tcmcs036._index1		|#ravi.perf_11.so
|		from	tcmcs036
|		where	tcmcs036._index1 = {:i.tax.country, :i.tax.code}
|		and 	tcmcs036._compnr = :i.company
|		order by tcmcs036._index1
|		selectdo				|#ravi.perf_11.eo

							|binda21-07.sn
			|WR PERF statics added
			if s.tax.country <> i.tax.country or
				s.tax.code <> i.tax.code then
							|binda21-07.en

				select  tcmcs032.pvat:o.total.perc,
					tcmcs032._index1
				from    tcmcs032
				where 	tcmcs032._index1 = { :i.tax.country,
							     :i.tax.code }
				and	tcmcs032.edat <= :i.date
				and	tcmcs032._compnr = :i.company
				order by tcmcs032._index1 desc
				as set with 1 rows
				selectdo
				selectempty
	
					if not isspace(i.tax.code) then	
						o.total.perc = 0.0
					endif				
				endselect

									|binda21-07.sn
				s.tax.code = i.tax.code
				s.tax.country = i.tax.country
				s.total.perc = o.total.perc
			else
				o.total.perc = s.total.perc
			endif
									|binda21-07.en

|		endselect				|#ravi.perf_11.o
	else
		|***************************
		|* Multiple Tax Code
		|***************************
|		select	tcmcs035.ccty:dummy.country,	|#ravi.perf_11.so
|			tcmcs035.cvat:dummy.vat.code,
|			tcmcs035.sern:sequence.no,
|			tcmcs035.clcm:calc.method,
|			tcmcs035.ctau.l:tax.authority,
|			tcmcs035.extx:purchase.tax
|		from	tcmcs035
|		where	tcmcs035._index1 = {:i.tax.country, :i.tax.code}
|		and	tcmcs035._compnr = :i.company
|		order by tcmcs035._index1
|		selectdo				|#ravi.perf_11.eo
							|#ravi.perf_11.sn
		select  tcmcs033.pvat:multiple.tax.perc, tcmcs033._index1,
			tcmcs035.clcm:calc.method,
			tcmcs035.ctau.l |#ravi_array.n
		from    tcmcs033, tcmcs035
		where   tcmcs033._index2 = {:i.tax.country,:i.tax.code}
		and 	tcmcs033.edat <= :i.date	
		and	tcmcs033._compnr = :i.company
		and     tcmcs033.cmba refers to tcmcs035
		order by tcmcs033.ccty desc, tcmcs033.cvat desc, tcmcs033.sern asc,
		tcmcs033.edat desc			|#ravi.perf_11.en

|			select	tcmcs033.pvat:multiple.tax.perc, |#ravi.perf_11.so
|				tcmcs033._index1
|			from	tcmcs033
|			where	tcmcs033._index1 inrange { :i.tax.country,
|							   :i.tax.code,
|							   :sequence.no,
|							   0 }
|						 and	 { :i.tax.country,
|							   :i.tax.code,
|							   :sequence.no,
|							   :i.date }
|			and	tcmcs033._compnr = :i.company
|			order by tcmcs033._index1 desc
|			as set with 1 rows		|#ravi.perf_11.eo
		selectdo
			if calc.method = tcmcs.clcm.parallel then
				o.total.perc = o.total.perc +
				multiple.tax.perc
			else
				o.total.perc = o.total.perc +
					     ( multiple.tax.perc *
					      o.total.perc / 100.0 ) +
					      multiple.tax.perc
			endif
							|#ravi.perf_11.sn
		selectempty

			if not isspace(i.tax.code) then		
				o.total.perc = 0
			endif					
		endselect
							|#ravi.perf_11.en	

							|#ravi.perf_11.so
|		selectempty
|
|			if not isspace(i.tax.code) then		
|				o.total.perc = 0
|			endif					
|		endselect
							|#ravi.perf_11.eo
	endif
}
											|#ravi.en

|****************************************** DUMPING FUNCTIONS ************************************************
function dump.header.record.tfacr510()
{
	dump 510
}

function dump.cisli210.without.schedule()

{
	dump 210

}

function dump.tfacr510.lines()
{
	dump 555
}
function dump.cisli205()
{
	dump 205
}

function dump.cisli220()
{
	dump 220
}
function dump.cisli225()
{
	dump 225

}

function dump.cisli226()
{
	dump 226
}
function dump.cisli230()
{
 	dump 1
}
function dump.cisli235.tppin120()
{
	dump 2
}
function dump.cisli235.tppin130()
{
	dump 3
}
function dump.cisli235.tppin140()
{
	dump 4
}

function dump.cisli235.tppin150()
{
	dump 5
}
function dump.cisli235.tppin160()
{
	dump 6
}
function dump.cisli235.tppin170()
{
	dump 7
}
function dump.cisli235.tppin180()
{
	dump 8

}
function dump.cisli235.tppin190()
{
	dump 9
}
function dump.cisli236.tppin140()
{
	dump 10
}
function dump.cisli236.tppin150()
{
	dump 11
}

function dump.cisli236.tppin160()
{
	dump 12
}
function dump.cisli236.tppin170()
{
	dump 13
}
function dump.cisli236.tppin180()
{
	dump 14
}

function dump.cisli240()
{
	dump 240
}
function long dump.cisli245()
{
	if convert.cisli245.stat() then
		dump 245
	else
		return(0)
	endif

	return (1)
}
function dump.cisli246()
{
	dump 246
}
function dump.cisli250()
{
	dump 250
}

function dump.cisli255()
{
	dump 255
}

function dump.cisli260()
{
	dump 260
}
function dump.cisli265()
{
	dump 265
}
function dump.cisli290()
{
	dump 290
}

function dump.cisli295()
{
	dump 295
}

function void read.tax.authority.desc(
			domain	tcmcs.ctau.l	i.tax.authority,
		ref	domain	tcdsca		o.tax.authority.desc )
{
	static domain tcmcs.ctau.l local.tax.authority		|#ravi.perf.sn
		
	domain	tcbool first.call

	if isspace(i.tax.authority) then
		o.tax.authority.desc = ""
		local.tax.authority = ""
		return
	endif
	if i.tax.authority = local.tax.authority then
		first.call = false
	else
		local.tax.authority = i.tax.authority
		first.call = true
	endif						
	if first.call then						|#ravi.perf.en

		select	tcmcs137.desc:o.tax.authority.desc
		from	tcmcs137
		where	tcmcs137._index1 = {:i.tax.authority}
		as set with 1 rows
		selectdo
		endselect
	endif							|#ravi.perf.n
}
								|#ravi_array.sn
|*******************************************************************************
| All the below functions are added for performance issues related to 
| temporary table. For migrating cisli206 arrays are used instead of 
| temporary table. For migrating cisli209 bcsci002 (temp table) is kept in 
| cache memory and all the databse operations are performed on cache memory
| only.
function dump.records.for.cisli206.cisli209.projects()
{
						
						
	
	

select 	tppin100.*
from	tppin100
where	tppin100.stin =  tppdm.stin.final and
	tppin100.cfpo =  tppdm.yeno.yes and
	tppin100.post =  tppdm.yeno.yes
order by tppin100.ninv
selectdo
	no.of.records = 0			|binda.n
	tppin190.accessed = false
	tax.allowed  = false
	if tppin100.cvyn = tppdm.yeno.yes then	
		tax.allowed = true
	endif
	initialize.cisli206.variables()
	allocate.memories(tppin100.ccty)
	select.tppin120.records()
	select.tppin130.records()
	select.tppin140.records()
	select.tppin150.records()
	select.tppin160.records()
	select.tppin170.records()
	select.tppin180.records()
	select.tppin190.records()
	tppin190.accessed = true
	get.invoice.to.address.proj.customer(tppin100.cuno)
	get.financial.company.country( tppin100.ocmp ) 
 	get.addresses( "tppdm600", tppin100.cprj,err.mess )                        
	initialize.addresses()
	fill.project.fields.cisli206()
	dump.records.from.cache.cisli209()
	if (tppin190.accessed and total.records >= 20000) then
		reinitialize.cache()
	endif

	free.memories()				
	dump.cisli205.for.projects.invoices()	
endselect

}
function allocate.memories(
		domain	tcccty	i.ccty)
{
	tax.codes.count = 0	
	blank.tax.code.amount = 0
	select count(tcmcs036.cvat):tax.codes.count
	from	tcmcs036
	where	tcmcs036._index1 = {:i.ccty}
	selectdo
	selectempty						|#207750.sn
		return						|#207750.en
	endselect					
	
	if tax.codes.count > 0 then
		alloc.mem(tax.codes.array,9,tax.codes.count)
		set.mem(tax.codes.array, " ")

		alloc.mem(tax.amounts.array,8,tax.codes.count)
		set.mem(tax.amounts.array, 0.0)

		alloc.mem(tax.base.amounts.array,8,tax.codes.count)
		set.mem(tax.base.amounts.array, 0.0)

		alloc.mem(tax.amounts.filled.array,1,tax.codes.count)
		set.mem(tax.amounts.filled.array, 0)

	endif
	tax.counter = 0
	select 	tcmcs036.cvat
	from	tcmcs036
	where	tcmcs036._index1 = {:i.ccty}
	order by tcmcs036.cvat
	selectdo
		tax.counter = tax.counter + 1
	 	if tax.codes.array(1, tax.counter) =  empty.string (1;9) then  
	 		tax.codes.array(1, tax.counter) =  tcmcs036.cvat
 		endif  
	
	endselect

}


function select.tppin120.records()
{
	long 	no.of.records1			
	domain tfgld.amnt	sumamnt, sumamdi, sumamvt

	domain	tccvat prev.tax.code

	prev.tax.code = ""
	no.of.records1 = 0			
	
	select	tppin120.cvat, 	sum(tppin120.amvt):sumamvt, sum(tppin120.amnt):sumamnt,
				sum(tppin120.amdi):sumamdi,count(*):no.of.records1
	from	tppin120
	where	tppin120._index1 = {tppdm.stin.final, :tppin100.ninv}
	group by tppin120.cvat
	order by tppin120.cvat
	selectdo
		no.of.records = no.of.records + no.of.records1 
		var.cisli205.rmai =  var.cisli205.rmai +  sumamnt  
 		var.cisli205.dcai =  var.cisli205.dcai +  sumamdi 
		if tax.allowed then
			if prev.tax.code <> tppin120.cvat then
				get.tax.by.country.code(tppin100.ccty,  
	 	 			tppin120.cvat,  new.txct, 
					new.txid, err.mess )
				prev.tax.code = tppin120.cvat
			endif
	
			tax.base.120 =  calc.tax.base.amount( tppin100.cpay, 
						sumamnt, sumamdi,
						tppin100.ccrs, tppin100.ccur )  
			
|			update.tax.amounts.array(new.txid, 	|#207750.so
|						sumamvt, tax.base.120)	
								|#207750.eo
			if not isspace(tppin100.ccty) then		
		 	 	read.tcmcs036 (tppin100.ccty, tppin120.cvat) 
			endif					
	 	 	get.tax.percentage(		
					tppin100.ccty, tppin120.cvat, 
					var.compnr, tppin100.idat, 
					var.singular.tax, var.cisli209.pvat) 	
					
			
			|*MCR .s
			tmp.mc.tppin100.rats(1) = tppin100.rats
			tmp.mc.tppin100.ratf(1) = tppin100.ratf			
			for i = 2 to 3				
				tmp.mc.tppin100.rats(i) = 0
				tmp.mc.tppin100.ratf(i) = 0									
			endfor	
			|*MCR .e		
			if update.tax.amounts.array(new.txid, 	|#207750.sn
						sumamvt, tax.base.120) then
								|#207750.en
			update.row.in.cache.cisli209(				
					var.compnr,
					tpprc000.ttys,
					tppin100.ninv,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					sumamvt,
					tax.base.120,
	 	 	 	 	var.cisli206.rcod,
					tppin100.ccur,  
	 	 	 	 	tppin100.idat,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tppin100.rats, |*MCR .o	 
	 	 	 	 	|tppin100.ratf) |*MCR .o
	 	 	 	 	tmp.mc.tppin100.rats, |*MCR .n
					tmp.mc.tppin100.ratf) |*MCR .n	
					cisli209.dumped = true	|#207750.sn
			else
					cisli209.dumped = false
			endif					|#207750.en	
		endif	
	endselect
}
function select.tppin130.records()
{
	long 	no.of.records1			
	domain tfgld.amnt	sumamnt, sumamdi, sumamvt
	domain	tccvat prev.tax.code

	prev.tax.code = ""
	no.of.records1 = 0			
	
	select	tppin130.cvat, 	sum(tppin130.amvt):sumamvt, 
				sum(tppin130.amnt):sumamnt,
				sum(tppin130.amdi):sumamdi,count(*):no.of.records1
	from	tppin130
	where	tppin130._index1 = {tppdm.stin.final, :tppin100.ninv}
	group by tppin130.cvat
	order by tppin130.cvat
	selectdo
		no.of.records = no.of.records + no.of.records1
		var.cisli205.rmai =  var.cisli205.rmai +  sumamnt  
 		var.cisli205.dcai =  var.cisli205.dcai +  sumamdi  
		if tax.allowed then
			if prev.tax.code <> tppin130.cvat then
				get.tax.by.country.code(tppin100.ccty,  
	 	 			tppin130.cvat,  new.txct,  
					new.txid, err.mess )
				prev.tax.code = tppin130.cvat
			endif
	
			tax.base.130 =  calc.tax.base.amount( tppin100.cpay, 
						sumamnt, sumamdi,
						tppin100.ccrs, tppin100.ccur )  
|			update.tax.amounts.array(new.txid, sumamvt, tax.base.130)	|#207750.o
	
			if not isspace(tppin100.ccty) then		
		 	 	read.tcmcs036 (tppin100.ccty, tppin130.cvat) 	
			endif					
	 	 	get.tax.percentage(		
					tppin100.ccty, tppin130.cvat,
					var.compnr, tppin100.idat, 
					var.singular.tax, var.cisli209.pvat) 	
			
			|*MCR .s
			tmp.mc.tppin100.rats(1) = tppin100.rats
			tmp.mc.tppin100.ratf(1) = tppin100.ratf			
			for i = 2 to 3				
				tmp.mc.tppin100.rats(i) = 0
				tmp.mc.tppin100.ratf(i) = 0									
			endfor	
			|*MCR .e		
	
			if update.tax.amounts.array(new.txid, sumamvt, tax.base.130) then	|#207750.n
			update.row.in.cache.cisli209(
					var.compnr,
					tpprc000.ttys,
					tppin100.ninv,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					sumamvt,
					tax.base.130,
	 	 	 	 	var.cisli206.rcod,
					tppin100.ccur,  
	 	 	 	 	tppin100.idat,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tppin100.rats, |*MCR .o	 
	 	 	 	 	|tppin100.ratf) |*MCR .o
	 	 	 	 	tmp.mc.tppin100.rats, |*MCR .n
					tmp.mc.tppin100.ratf) |*MCR .n	
					cisli209.dumped = true	|#207750.sn
			else
					cisli209.dumped = false
			endif					|#207750.en	
		endif
	endselect
}

function select.tppin140.records()
{
	long 	no.of.records1			
	domain 	tfgld.amnt	sumamnt, sumamdi, sumamvt, sumamdl
	domain	tccvat prev.tax.code

	prev.tax.code = ""
	no.of.records1 = 0			

	select	tppin140.cvat, 	sum(tppin140.amvt):sumamvt, 
				sum(tppin140.amnt):sumamnt,
				sum(tppin140.amdi):sumamdi,
				sum(tppin140.amdl):sumamdl,
				count(*):no.of.records1
	from	tppin140
	where	tppin140._index1 = {tppdm.stin.final, :tppin100.ninv}
	group by tppin140.cvat
	order by tppin140.cvat
	selectdo
		no.of.records = no.of.records + no.of.records1
		var.cisli205.rmai =  var.cisli205.rmai +  sumamnt  
 		var.cisli205.dcai =  var.cisli205.dcai +  sumamdi  
		if tax.allowed then
			if prev.tax.code <> tppin140.cvat then
				get.tax.by.country.code(tppin100.ccty,  
	 	 			tppin140.cvat,  new.txct,  
					new.txid, err.mess )
				prev.tax.code = tppin140.cvat
			endif
	
			tax.base.140 =  calc.tax.base.amount( tppin100.cpay, 
						sumamnt - sumamdl, sumamdi,		
						tppin100.ccrs, tppin100.ccur )  
		
|			update.tax.amounts.array(new.txid, sumamvt, 	|#207750.so
|						tax.base.140)		|#207750.eo
	
			if not isspace(tppin100.ccty) then		
		 	 	read.tcmcs036 (tppin100.ccty, tppin140.cvat) 	
			endif					
	 	 	get.tax.percentage(		
					tppin100.ccty, tppin140.cvat, 
					var.compnr, tppin100.idat, 
					var.singular.tax, var.cisli209.pvat) 	
			
			|*MCR .s
			tmp.mc.tppin100.rats(1) = tppin100.rats
			tmp.mc.tppin100.ratf(1) = tppin100.ratf			
			for i = 2 to 3				
				tmp.mc.tppin100.rats(i) = 0
				tmp.mc.tppin100.ratf(i) = 0									
			endfor	
			|*MCR .e
			if update.tax.amounts.array(new.txid, sumamvt, 	|#207750.sn
						tax.base.140) then	|#207750.en
			update.row.in.cache.cisli209(
					var.compnr,
					tpprc000.ttys,
					tppin100.ninv,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					sumamvt,
					tax.base.140,
	 	 	 	 	var.cisli206.rcod,
					tppin100.ccur,  
	 	 	 	 	tppin100.idat,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tppin100.rats, |*MCR .o	 
	 	 	 	 	|tppin100.ratf) |*MCR .o
	 	 	 	 	tmp.mc.tppin100.rats, |*MCR .n
					tmp.mc.tppin100.ratf) |*MCR .n	
					cisli209.dumped = true	|#207750.sn
			else
					cisli209.dumped = false
			endif					|#207750.en
		endif
	endselect
}

function select.tppin150.records()
{
	long 	no.of.records1			
	domain	tfgld.amnt	sumamnt, sumamdi, sumamvt, sumamdl
	domain	tccvat prev.tax.code

	prev.tax.code = ""
	no.of.records1 = 0			

	select	tppin150.cvat, 	sum(tppin150.amvt):sumamvt, 
				sum(tppin150.amnt):sumamnt,
				sum(tppin150.amdi):sumamdi,
				sum(tppin150.amdl):sumamdl,
				count(*):no.of.records1
	from	tppin150
	where	tppin150._index1 = {tppdm.stin.final, :tppin100.ninv}
	group by tppin150.cvat
	order by tppin150.cvat
	selectdo
		
		no.of.records = no.of.records + no.of.records1
		var.cisli205.rmai =  var.cisli205.rmai +  sumamnt  
 		var.cisli205.dcai =  var.cisli205.dcai +  sumamdi  
		if tax.allowed then
			if prev.tax.code <> tppin150.cvat then
				get.tax.by.country.code(tppin100.ccty,  
	 	 			tppin150.cvat,  new.txct, 
					new.txid, err.mess )
				prev.tax.code = tppin150.cvat
			endif
	
			tax.base.150 =  calc.tax.base.amount( tppin100.cpay, 
						sumamnt - sumamdl, sumamdi,
						tppin100.ccrs, tppin100.ccur )  
		
|			update.tax.amounts.array(new.txid, sumamvt, 	|#207750.so
|						tax.base.150)		|#207750.eo
	
			if not isspace(tppin100.ccty) then		
		 	 	read.tcmcs036 (tppin100.ccty, tppin150.cvat) 	
			endif					
	 	 	get.tax.percentage(		
					tppin100.ccty, tppin150.cvat, 
					var.compnr, tppin100.idat, 
					var.singular.tax, var.cisli209.pvat) 	
	
			
			|*MCR .s
			tmp.mc.tppin100.rats(1) = tppin100.rats
			tmp.mc.tppin100.ratf(1) = tppin100.ratf			
			for i = 2 to 3				
				tmp.mc.tppin100.rats(i) = 0
				tmp.mc.tppin100.ratf(i) = 0									
			endfor	
			|*MCR .e
			if update.tax.amounts.array(new.txid, sumamvt, 	|#207750.sn
						tax.base.150) then	|#207750.en
			update.row.in.cache.cisli209(
					var.compnr,
					tpprc000.ttys,
					tppin100.ninv,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					sumamvt,
					tax.base.150,
	 	 	 	 	var.cisli206.rcod,
					tppin100.ccur,  
	 	 	 	 	tppin100.idat,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tppin100.rats, |*MCR .o	 
	 	 	 	 	|tppin100.ratf) |*MCR .o
	 	 	 	 	tmp.mc.tppin100.rats, |*MCR .n
					tmp.mc.tppin100.ratf) |*MCR .n		
					cisli209.dumped = true	|#207750.sn
			else
					cisli209.dumped = false
			endif					|#207750.en
		endif
	endselect
}
function select.tppin160.records()
{
	long 	no.of.records1			
	domain 	tfgld.amnt	sumamnt, sumamdi, sumamvt, sumamdl
	domain	tccvat 		prev.tax.code

	prev.tax.code = ""
	no.of.records1 = 0			

	select	tppin160.cvat,	sum(tppin160.amvt):sumamvt, 
				sum(tppin160.amnt):sumamnt,
				sum(tppin160.amdi):sumamdi,
				sum(tppin160.amdl):sumamdl,
				count(*):no.of.records1
	from	tppin160
	where	tppin160._index1 = {tppdm.stin.final, :tppin100.ninv}
	group by tppin160.cvat
	order by tppin160.cvat
	selectdo
		no.of.records = no.of.records + no.of.records1
		var.cisli205.rmai =  var.cisli205.rmai +  sumamnt  
 		var.cisli205.dcai =  var.cisli205.dcai +  sumamdi  
		if tax.allowed then
			if prev.tax.code <> tppin160.cvat then
				get.tax.by.country.code(tppin100.ccty,  
	 	 			tppin160.cvat,  new.txct,  
					new.txid, err.mess )
				prev.tax.code = tppin160.cvat
			endif
	
			tax.base.160 =  calc.tax.base.amount( tppin100.cpay, 
						sumamnt- sumamdl, sumamdi,
						tppin100.ccrs, tppin100.ccur )  
		
|			update.tax.amounts.array(new.txid, sumamvt, 	|#207750.so
|						tax.base.160)		|#207750.eo
	
			if not isspace(tppin100.ccty) then		
		 	 	read.tcmcs036 (tppin100.ccty, tppin160.cvat) 	
			endif					
	 	 	get.tax.percentage(		
					tppin100.ccty, tppin160.cvat, 
					var.compnr, tppin100.idat, 
					var.singular.tax, var.cisli209.pvat) 	
			
			|*MCR .s
			tmp.mc.tppin100.rats(1) = tppin100.rats
			tmp.mc.tppin100.ratf(1) = tppin100.ratf			
			for i = 2 to 3				
				tmp.mc.tppin100.rats(i) = 0
				tmp.mc.tppin100.ratf(i) = 0									
			endfor	
			|*MCR .e
			if update.tax.amounts.array(new.txid, sumamvt, 	|#207750.sn
						tax.base.160) then	|#207750.en
			update.row.in.cache.cisli209(
					var.compnr,
					tpprc000.ttys,
					tppin100.ninv,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					sumamvt,
					tax.base.160,
	 	 	 	 	var.cisli206.rcod,
					tppin100.ccur,  
	 	 	 	 	tppin100.idat,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tppin100.rats, |*MCR .o	 
	 	 	 	 	|tppin100.ratf) |*MCR .o
	 	 	 	 	tmp.mc.tppin100.rats, |*MCR .n
					tmp.mc.tppin100.ratf) |*MCR .n		
					cisli209.dumped = true	|#207750.sn
			else
					cisli209.dumped = false
			endif					|#207750.en	
		endif

	endselect
}

function select.tppin170.records()
{
	long 	no.of.records1			
	domain 	tfgld.amnt	sumamnt, sumamdi, sumamvt, sumamdl
	domain	tccvat		prev.tax.code

	prev.tax.code = ""
	no.of.records1 = 0			

	select	tppin170.cvat,	sum(tppin170.amvt):sumamvt, 
				sum(tppin170.amnt):sumamnt,
				sum(tppin170.amdi):sumamdi,
				sum(tppin170.amdl):sumamdl,
				count(*):no.of.records1
	from	tppin170
	where	tppin170._index1 = {tppdm.stin.final, :tppin100.ninv}
	group by tppin170.cvat
	order by tppin170.cvat
	selectdo
		no.of.records = no.of.records + no.of.records1
		var.cisli205.rmai =  var.cisli205.rmai +  sumamnt  
 		var.cisli205.dcai =  var.cisli205.dcai +  sumamdi  

		if tax.allowed then
			if prev.tax.code <> tppin170.cvat then
				get.tax.by.country.code(tppin100.ccty,  
	 	 			tppin170.cvat,  new.txct, 
					new.txid, err.mess )
				prev.tax.code = tppin170.cvat
			endif
	
			tax.base.170 =  calc.tax.base.amount( tppin100.cpay, 
						sumamnt - sumamdl, sumamdi,
						tppin100.ccrs, tppin100.ccur )  

|			update.tax.amounts.array(new.txid, sumamvt, tax.base.170)	|#207750.o
			if not isspace(tppin100.ccty) then		
		 	 	read.tcmcs036 (tppin100.ccty, tppin170.cvat) 	
			endif					
	 	 	get.tax.percentage(		
					tppin100.ccty, tppin170.cvat, 
					var.compnr, tppin100.idat, 
					var.singular.tax, var.cisli209.pvat) 	
			
			|*MCR .s
			tmp.mc.tppin100.rats(1) = tppin100.rats
			tmp.mc.tppin100.ratf(1) = tppin100.ratf			
			for i = 2 to 3				
				tmp.mc.tppin100.rats(i) = 0
				tmp.mc.tppin100.ratf(i) = 0									
			endfor	
			|*MCR .e
			if update.tax.amounts.array(new.txid, sumamvt, tax.base.170) then	|#207750.n
			update.row.in.cache.cisli209(
					var.compnr,
					tpprc000.ttys,
					tppin100.ninv,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					sumamvt,
					tax.base.170,
	 	 	 	 	var.cisli206.rcod,
					tppin100.ccur,  
	 	 	 	 	tppin100.idat,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tppin100.rats, |*MCR .o	 
	 	 	 	 	|tppin100.ratf) |*MCR .o
	 	 	 	 	tmp.mc.tppin100.rats, |*MCR .n
					tmp.mc.tppin100.ratf) |*MCR .n	
					cisli209.dumped = true	|#207750.sn
			else
					cisli209.dumped = false
			endif					|#207750.en
					
		endif

	endselect
}
function select.tppin180.records()
{
	long 	no.of.records1			
	domain 	tfgld.amnt	sumamnt, sumamdi, sumamvt, sumamdl
	domain	tccvat 		prev.tax.code

	prev.tax.code = ""
	no.of.records1 = 0			

	select	tppin180.cvat,	sum(tppin180.amvt):sumamvt, 
				sum(tppin180.amnt):sumamnt,
				sum(tppin180.amdi):sumamdi,
				sum(tppin180.amdl):sumamdl,
				count(*):no.of.records1	
	from	tppin180
	where	tppin180._index1 = {tppdm.stin.final, :tppin100.ninv}
	group by tppin180.cvat
	order by tppin180.cvat
	selectdo
		no.of.records = no.of.records + no.of.records1
		var.cisli205.rmai =  var.cisli205.rmai +  sumamnt  
 		var.cisli205.dcai =  var.cisli205.dcai +  sumamdi  
 
		if tax.allowed then
			if prev.tax.code <> tppin180.cvat then
				get.tax.by.country.code(tppin100.ccty,  
	 	 			tppin180.cvat,  new.txct,  
					new.txid, err.mess )
				prev.tax.code = tppin180.cvat
			endif
	
			tax.base.180 =  calc.tax.base.amount( tppin100.cpay, 
						sumamnt - sumamdl, sumamdi,
						tppin100.ccrs, tppin100.ccur )  
		
|			update.tax.amounts.array(new.txid, sumamvt, 	|#207750.so
|							tax.base.180)	|#207750.eo

			if not isspace(tppin100.ccty) then		
		 	 	read.tcmcs036 (tppin100.ccty, tppin180.cvat) 	
			endif					
	 	 	get.tax.percentage(		
					tppin100.ccty, tppin180.cvat, 
					var.compnr, tppin100.idat, 
					var.singular.tax, var.cisli209.pvat) 	
	
			|*MCR .s
			tmp.mc.tppin100.rats(1) = tppin100.rats
			tmp.mc.tppin100.ratf(1) = tppin100.ratf			
			for i = 2 to 3				
				tmp.mc.tppin100.rats(i) = 0
				tmp.mc.tppin100.ratf(i) = 0									
			endfor	
			|*MCR .e
			if update.tax.amounts.array(new.txid, sumamvt, 	|#207750.sn
							tax.base.180) then	|#207750.en
			update.row.in.cache.cisli209(
					var.compnr,
					tpprc000.ttys,
					tppin100.ninv,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					sumamvt,
					tax.base.180,
	 	 	 	 	var.cisli206.rcod,
					tppin100.ccur,  
	 	 	 	 	tppin100.idat,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tppin100.rats, |*MCR .o	 
	 	 	 	 	|tppin100.ratf) |*MCR .o
	 	 	 	 	tmp.mc.tppin100.rats, |*MCR .n
					tmp.mc.tppin100.ratf) |*MCR .n	
					cisli209.dumped = true	|#207750.sn
			else
					cisli209.dumped = false
			endif					|#207750.en
		endif
	
	endselect
}
function select.tppin190.records()
{	
	long 	no.of.records1			
	domain 	tfgld.amnt	sumamnt, sumamdi, sumamvt, sumamdl
	domain	tccvat 		prev.tax.code

	prev.tax.code = ""
	no.of.records1 = 0			

	select	tppin190.cvat,	sum(tppin190.amvt):sumamvt, 
				sum(tppin190.amnt):sumamnt,
				sum(tppin190.amdi):sumamdi,
				sum(tppin190.amdl):sumamdl,
				count(*):no.of.records1	
	from	tppin190
	where	tppin190._index1 = {tppdm.stin.final, :tppin100.ninv}
	group by tppin190.cvat
	order by tppin190.cvat
	selectdo
		no.of.records = no.of.records + no.of.records1
		var.cisli205.rmai =  var.cisli205.rmai +  sumamnt  
 		var.cisli205.dcai =  var.cisli205.dcai +  sumamdi  
							
		if tax.allowed then
			if prev.tax.code <> tppin190.cvat then
				get.tax.by.country.code(tppin100.ccty,  
	 	 			tppin190.cvat,  new.txct,  
					new.txid, err.mess )
				prev.tax.code = tppin190.cvat
			endif
	
			tax.base.190 =  calc.tax.base.amount( tppin100.cpay, 
						sumamnt - sumamdl, sumamdi,
						tppin100.ccrs, tppin100.ccur )
		
|			update.tax.amounts.array(new.txid, sumamvt, 	|#207750.so
|							tax.base.190)	|#207750.eo
							
			if not isspace(tppin100.ccty) then		
		 	 	read.tcmcs036 (tppin100.ccty, tppin190.cvat) 	
			endif					
	 	 	get.tax.percentage(		
					tppin100.ccty, tppin190.cvat, 
					var.compnr, tppin100.idat, 
					var.singular.tax, var.cisli209.pvat) 	
			
			|*MCR .s
			tmp.mc.tppin100.rats(1) = tppin100.rats
			tmp.mc.tppin100.ratf(1) = tppin100.ratf			
			for i = 2 to 3				
				tmp.mc.tppin100.rats(i) = 0
				tmp.mc.tppin100.ratf(i) = 0									
			endfor	
			|*MCR .e
			if update.tax.amounts.array(new.txid, sumamvt, 	|#207750.sn
							tax.base.190) then	|#207750.en
			update.row.in.cache.cisli209(
					var.compnr,
					tpprc000.ttys,
					tppin100.ninv,
					tcmcs036.ctau.l,
	 	 	 	 	var.cisli206.ceno,
					var.cisli209.pvat,
					sumamvt,
					tax.base.190,
	 	 	 	 	var.cisli206.rcod,
					tppin100.ccur,  
	 	 	 	 	tppin100.idat,  
	 	 	 	 	var.cisli.rtyp,  
	 	 	 	 	|tppin100.rats, |*MCR .o	 
	 	 	 	 	|tppin100.ratf) |*MCR .o
	 	 	 	 	tmp.mc.tppin100.rats, |*MCR .n
					tmp.mc.tppin100.ratf) |*MCR .n	
					cisli209.dumped = true	|#207750.sn
			else
					cisli209.dumped = false
			endif					|#207750.en	
		endif
	endselect
}
								
|function update.tax.amounts.array(				|#207750.o
function domain tcbool update.tax.amounts.array(		|#207750.n
			 domain tccvat	i.cvat,
			 domain tfgld.amnt i.tax.amount,
			 domain tfgld.amnt i.tax.base.amount)
{
	long	tax.code.cellno
	long 	i
	
	i = 0
	tax.code.cellno = 0					|#215284.n
	if tax.codes.count = 0 then				|#207750.sn
		return(false)
	endif							|#207750.en
	if isspace(i.cvat) then
		blank.tax.code.amount = blank.tax.code.amount + i.tax.amount
		tax.base.amounts.array(1,1) = 
				tax.base.amounts.array(1,1) + i.tax.base.amount

		if (tax.amounts.filled.array(1,1) = 0) then
			tax.amounts.filled.array(1,1) = 1
		endif
|		return						|#207750.o
		return(true)					|#207750.n
	endif
		for i = 1 to tax.counter
			if i.cvat = tax.codes.array(1,i) then
				tax.code.cellno = i
				break
			endif
		endfor
	if not isspace(i.cvat) then
		if tax.code.cellno <> 0 then			|#207750.n
		tax.amounts.array(1,tax.code.cellno) = tax.amounts.array(1,tax.code.cellno) + i.tax.amount
		else						|#207750.sn
		return(false)
	       endif						|#207750.en
	endif
	tax.base.amounts.array(1,tax.code.cellno) = tax.base.amounts.array(1,tax.code.cellno) + i.tax.base.amount
	if tax.amounts.filled.array(1,tax.code.cellno) = 0 then
		tax.amounts.filled.array(1,tax.code.cellno) = 1
	endif
	return(true)						|#207750.n
}
function fill.project.fields.cisli206()
{

	long i, j,element			|binda.n
	domain	tfgld.amnt	var.txah, var.tbah
	domain  tcbool		blank.tax.code.dumped

	i = 0
	blank.tax.code.dumped = false
	if tax.codes.count = 0 then				|#207750.sn
		return
	endif							|#207750.enF
	if isspace(tax.codes.array(1,1)) and (tax.amounts.filled.array(1,1) = 1) then
		| This if part code is to fill tax amount for blank tax codes
 	 	calculate.amount.in.other.currency (
					blank.tax.code.amount, tppin100.rats, 
					tppin100.ratf, tppin100.ccur, 
					var.txah, err.mess) 	 

 	 	calculate.amount.in.other.currency (tax.base.amounts.array(1,1),
					tppin100.rats, tppin100.ratf, 
					tppin100.ccur, var.tbah, err.mess) 	 
					
		|*MCR .sn		
		tmp.mc.var.tbah(1) = var.tbah
		tmp.mc.var.txah(1) = var.txah		
|		for i = 2 to 3					|binda.o
		for element = 2 to 3					|binda.n
			tmp.mc.var.tbah(element) = 0
			tmp.mc.var.txah(element) = 0
		endfor
		|*MCR .en

		fill.cisli206.fields(
			var.compnr,			|1
			tpprc000.ttys,			|2
			tppin100.ninv,			|3
			tppin100.ccty,			|4
			tax.codes.array(1,1),		|5
			 "", 				|i.ceno, 6
			var.compnr, 			|7
			tax.base.amounts.array(1,1),	|8
			"", 				|i.rcod, 9
			|var.tbah,			|10 |*MCR .o
			tmp.mc.var.tbah,		|10 |*MCR .n
			blank.tax.code.amount,		|11
			|var.txah, 			|12 |*MCR .o
			tmp.mc.var.txah,		|12 |*MCR .n	
			var.cisli206.sfcy,		|13
			var.cisli206.stcy,  		|14
			var.fin.comp.country, 		|15
			var.temp.cisli206.itcy)		|16
								
			dump.cisli206.from.arrays()	|binda_23.n	
			blank.tax.code.dumped =  true
	endif
	if blank.tax.code.dumped = true then
		j = 2
	else
		j = 1
	endif
	for i = j to tax.counter
 	 	calculate.amount.in.other.currency(
				tax.amounts.array(1,i), tppin100.rats, 
					tppin100.ratf, tppin100.ccur, 
					var.txah, err.mess) 	 

 	 	calculate.amount.in.other.currency(
					tax.base.amounts.array(1,i),
					tppin100.rats, tppin100.ratf, 
					tppin100.ccur, 
					var.tbah, err.mess) 	 
		
		|*MCR .sn		
		tmp.mc.var.tbah(1) = var.tbah
		tmp.mc.var.txah(1) = var.txah		
|		for i = 2 to 3				|binda.o
		for element = 2 to 3				|binda.n
			tmp.mc.var.tbah(element) = 0
			tmp.mc.var.txah(element) = 0
		endfor
		|*MCR .en			

		if tax.amounts.filled.array(1,i) = 1 then
			fill.cisli206.fields(
				var.compnr,			|1
				tpprc000.ttys,			|2
				tppin100.ninv,			|3
				tppin100.ccty,			|4
				tax.codes.array(1,i),		|5
				 "", 				|i.ceno, 6
				var.compnr, 			|7
				tax.base.amounts.array(1,i),	|8
				"", 				|i.rcod, 9
				|var.tbah,			|10 |*MCR . o
				tmp.mc.var.tbah,		|10 |*MCR . n
				tax.amounts.array(1,i),		|11
				|var.txah,			|12 |*MCR .o
				tmp.mc.var.txah,		|12 |*MCR .n
				var.cisli206.sfcy,		|13
				var.cisli206.stcy,  		|14
				var.fin.comp.country, 		|15
				var.temp.cisli206.itcy)		|16
									
				dump.cisli206.from.arrays()	|binda_23.n	
				
		endif
	endfor
			
}
								|binda_23.sn
function dump.cisli206.from.arrays()
{
		dump 206
}
								|binda_23.en

function update.row.in.cache.cisli209(
			domain	tcncmp		i.company,
			domain	tfgld.ttyp 	i.trans.type,    
			domain	tfgld.docn	i.document,    
			domain 	tcmcs.ctau.l	i.tax.authority,  
			domain	tcfovn		i.exno,
			domain 	tcpvat		i.tax.percentage,
			domain 	tfgld.amnt 	i.tax.amount,
			domain	tfgld.amnt	i.tax.base,
			domain	tccdis		i.exempt.reason,
			domain 	tcccur 		i.currency,
			domain 	tcdate 		i.rate.date,
			domain 	bcrtyp	 	i.rate.type,
		|const	domain 	tcrats 		i.rate, |*MCR .o
		|const	domain 	tcratf		i.ratf) |*MCR .o
		ref 	domain 	tcrats 		i.rate(), |*MCR .n
		ref 	domain 	tcratf		i.ratf()) |*MCR .n
   
{

	long		ret.val
	domain	tcdsca	tax.authority.desc
								|bin-MCR.sn
	double var.bcsci002.txah(3)
	double var.bcsci002.tbah(3)
								|bin-MCR.en
	
	if not already.accessed then
		r.val = idb.init("bcsci002","", 1, 0, table.fields,get.compnr(),
					1, -1, false, 100,10,0)
	
		bcsci002.table.cached = idb.load("bcsci002","")
		already.accessed = true
	endif
	read.tax.authority.desc(i.tax.authority,
				tax.authority.desc )
	if not r.val then
		bcsci002.fcmp = i.company
		bcsci002.ttyp = i.trans.type
		bcsci002.ninv = i.document
		bcsci002.vatc = ""
		bcsci002.cvat = ""
		bcsci002.ctau = i.tax.authority
		bcsci002.dsca = tax.authority.desc 
		bcsci002.ctyp = 0
		bcsci002.exno = i.exno
		bcsci002.perc = i.tax.percentage
		bcsci002.rcod = i.exempt.reason
		|bcsci002.seqn = 2			
		ret.val = idb.eq("bcsci002")
		if not ret.val then
			if isspace(bcsci002.exno) then
				bcsci002.taxi = bcsci002.taxi + i.tax.amount
								|bin-MCR.so

|				for i = 1 to 3 |*MCR .n
|			              calculate.amount.in.other.currency (
|						bcsci002.taxi,
|						|i.rate, |*MCR .o
|						|i.ratf, |*MCR .o 
|						i.rate(i),|*MCR .n
|						i.ratf(i),|*MCR .n
|	                                        i.currency,
|	                                        |bcsci002.txah, |*MCR .o
|	                                        bcsci002.txah(i), |*MCR .n
|						err.mess)  
|				endfor |*MCR . n
|				
|				bcsci002.tbai = bcsci002.tbai + i.tax.base
|				for i = 1 to 3 |*MCR .n
|			              	calculate.amount.in.other.currency (
|						bcsci002.tbai,
|						|i.rate, |*MCR .o
|						|i.ratf, |*MCR .o 

|						i.rate(i),|*MCR .n
|						i.ratf(i),|*MCR .n
|	                                        i.currency,
|	                                        |bcsci002.tbah, |*MCR .o
|	                                        bcsci002.tbah(i), |*MCR .n
|						err.mess)  
|				endfor |*MCR .n		
								|bin-MCR.eo
								|bin-MCR.sn

				for i = 1 to 3
			              calculate.amount.in.home.currency.variables (
						bcsci002.taxi,
						i.currency,
						i.rate(i),
						i.ratf(i),
	                                        i,
	                                        var.bcsci002.txah(i), 
						err.mess)  
				endfor 
				fill.mc.table.field.double(var.bcsci002.txah,bcsci002.txah)
				
				
				bcsci002.tbai = bcsci002.tbai + i.tax.base
				for i = 1 to 3 |*MCR .n
			              	calculate.amount.in.home.currency.variables (
						bcsci002.tbai,
						i.currency,
						i.rate(i),
						i.ratf(i),
	                                        i,
	                                        var.bcsci002.tbah(i), 
						err.mess)  
				endfor 		
				fill.mc.table.field.double(var.bcsci002.tbah,bcsci002.tbah)
								|bin-MCR.en


			else
									|bin-MCR.so
|				bcsci002.taxi = 0.0
|				bcsci002.tbai = 0.0
|				|*MCR - s			 
|				|bcsci002.txah = 0.0
|				|bcsci002.tbah = 0.0	  
|				for i = 1 to 3
|					bcsci002.txah(i) = 0.0
|					bcsci002.tbah(i) = 0.0
|				endfor
|				|*MCR - e
									|bin-MCR.eo

									|bin-MCR.sn
				for i = 1 to 3
					var.bcsci002.txah(i) = 0.0					
					var.bcsci002.tbah(i) = 0.0
				endfor
				fill.mc.table.field.double(var.bcsci002.txah,bcsci002.txah)
				fill.mc.table.field.double(var.bcsci002.tbah,bcsci002.tbah)					

									|bin-MCR.en


			
			endif
			ret.val = idb.update("bcsci002")
		else
			if isspace( bcsci002.exno ) then
				bcsci002.perc = i.tax.percentage
				bcsci002.taxi = i.tax.amount
								|bin-MCR.so
|				for  i = 1 to 3 |*MCR .n
|				        calculate.amount.in.other.currency (
|						bcsci002.taxi,
|						|i.rate, |*MCR .o
|						|i.ratf, |*MCR .o 
|						i.rate(i),|*MCR .n
|						i.ratf(i),|*MCR .n
|		                                i.currency,
|		                                |bcsci002.txah, |*MCR .o
|		                                bcsci002.txah(i), |*MCR .n
|		                                err.mess)  
|		                endfor |*MCR .n                
|
|				bcsci002.tbai = i.tax.base

|				for  i = 1 to 3 |*MCR .n
|			              calculate.amount.in.other.currency (
|						bcsci002.tbai,
|						|i.rate, |*MCR .o
|						|i.ratf, |*MCR .o 
|						i.rate(i),|*MCR .n
|						i.ratf(i),|*MCR .n
|	                                	i.currency,
|	                                	|bcsci002.tbah, |*MCR .o
|	                                        bcsci002.tbah(i), |*MCR .n
|	                                	err.mess)  
|	                        endfor |*MCR .n            
								|bin-MCR.eo            
								|bin-MCR.sn
				for i = 1 to 3
			              calculate.amount.in.home.currency.variables (
						bcsci002.taxi,
						i.currency,
						i.rate(i),
						i.ratf(i),
	                                        i,
	                                        var.bcsci002.txah(i), 
						err.mess)  
				endfor 
				fill.mc.table.field.double(var.bcsci002.txah,bcsci002.txah)

				bcsci002.tbai = i.tax.base
|				bcsci002.tbai = bcsci002.tbai + i.tax.base		|#202350.o
				for i = 1 to 3 |*MCR .n
			              	calculate.amount.in.home.currency.variables (
						bcsci002.tbai,
						i.currency,
						i.rate(i),
						i.ratf(i),
	                                        i,
	                                        var.bcsci002.tbah(i), 
						err.mess)  
				endfor 		
				fill.mc.table.field.double(var.bcsci002.tbah,bcsci002.tbah)
								|bin-MCR.en

			else
								|bin-MCR.so	
|				bcsci002.perc = 0.0
|				bcsci002.taxi = 0.0
|				bcsci002.tbai = 0.0			 
|				|*MCR - s			 
|				|bcsci002.txah = 0.0
|				|bcsci002.tbah = 0.0	  
|				for i = 1 to 3
|					bcsci002.txah(i) = 0.0
|					bcsci002.tbah(i) = 0.0
|				endfor
|				|*MCR - e
								|bin-MCR.eo
								|bin-MCR.sn
				for i = 1 to 3
					var.bcsci002.txah(i) = 0.0					
					var.bcsci002.tbah(i) = 0.0
				endfor
				fill.mc.table.field.double(var.bcsci002.txah,bcsci002.txah)
				fill.mc.table.field.double(var.bcsci002.tbah,bcsci002.tbah)					

								|bin-MCR.en
			
		
			endif
			ret.val = idb.insert("bcsci002")
|			idb.sort("bcsci002")			|WR.perf.n	|ravi_july28th.o
			|sort array after each insert, this will highly improve idb.eq and idb.update actions
			if not ret.val then
				total.records = total.records + 1
|				if (tppin190.accessed and 		|Haddock.err.so
|				cisli209.dumped and total.records >= 20000) then
|					reinitialize.cache()
|				endif					|Haddock.err.eo
			endif		
		endif
	endif
}
function dump.records.from.cache.cisli209()
{
		long	ret.val
		long 	element
		long	i.rec				|ravi_july28th.n
		double 	temp.txah(3)
		double 	temp.tbah(3)

		for element =  1 to 3 				|*MCR.sn
			temp.txah(element) = fill.mc.field.double(bcsci002.txah, element)
			temp.tbah(element) = fill.mc.field.double(bcsci002.tbah, element)
		endfor						|*MCR.en
	i.rec = 0					|ravi_july28th.n
	for i.rec = record.counter+1 to total.records
		ret.val = idb.get.row("bcsci002", i.rec)		
		fill.cisli209.fields(
			bcsci002.fcmp,
			bcsci002.ttyp,		
			bcsci002.ninv,		
			bcsci002.ctau,
			bcsci002.dsca,  
			bcsci002.ctyp,
			bcsci002.exno,
			bcsci002.perc,
			bcsci002.taxi,
|			bcsci002.txah,		|*MCR.o
			temp.txah,		|*MCR.n
 		  	bcsci002.rcod,
			bcsci002.tbai,
|			bcsci002.tbah)		|*MCR.o
			temp.tbah)		|*MCR.n

			dump 209

		record.counter = record.counter + 1
		if table.reinitialized = true then
			record.counter = 0
			table.reinitialized = false
		endif
	endfor

}

function dump.fields.cisli206.from.array(
				domain	tcncmp		i.company,
				domain 	tfgld.ttyp	i.trans.type,
				domain 	tfgld.docn	i.document,
				domain 	tcccty		i.tax.country,
		 	|const	domain 	tcrats 		i.rate, |*MCR .o
		 	|const	domain 	tcratf		i.ratf, |*MCR .o
		 	ref	domain 	tcrats 		i.rate(), |*MCR .n
		 	ref	domain 	tcratf		i.ratf(), |*MCR .n
				domain 	tcccur 		i.ccur,
				domain tcccty		i.sfcy,
				domain tcccty		i.stcy,
				domain tcccty		i.ifcy,
				domain tcccty		i.itcy,
				domain tcfovn 		i.ceno,
				domain tccdis		i.rcod)
{
	long i, j,element				|binda.n
	|domain	tfgld.amnt	var.txah, var.tbah |*MCR.o
		 double	var.txah(3) |*MCR.n
		 double	var.tbah(3) |*MCR.n
	domain  tcbool		blank.tax.code.dumped
	i = 0
	blank.tax.code.dumped = false
	if tax.codes.count = 0 then				|#207750.sn
		return
	endif							|#207750.en
	if isspace(tax.codes.array(1,1)) and (tax.amounts.filled.array(1,1) = 1) then
		| This fucnction call is to fill tax amount for blank tax codes
|*  MCR -  new changes .s		
|		for i = 1 to 3 |*MCR.n
| 	 		calculate.amount.in.other.currency (
|					blank.tax.code.amount,
|					|i.rate, |*MCR .o
|					|i.ratf, |*MCR .o
|					i.rate(i), |*MCR .n
|					i.ratf(i), |*MCR .n
|					i.ccur, 
|					|var.txah, |*MCR.o
|					var.txah(i), |*MCR.n
|					err.mess) 	 
|		endfor |*MCR .n			
		for i = 1 to 3
			calculate.amount.in.home.currency.variables(blank.tax.code.amount,
		 		i.ccur,
			 	i.rate(i),
 	 	 		i.ratf(i), 
		 		i,  
		 	 	var.txah(i), 	 	 	 	 	 	 	  
		 	 	err.mess)
		endfor	 	 
|*  MCR -  new changes .e
 
|*  MCR -  new changes .s
|		for i = 1 to 3 |*MCR.n
| 	 		calculate.amount.in.other.currency (tax.base.amounts.array(1,1),
|					|i.rate, |*MCR .o
|					|i.ratf, |*MCR .o
|					i.rate(i), |*MCR .n
|					i.ratf(i), |*MCR .n
|					i.ccur, 
|					|var.tbah, |*MCR.o
|					var.tbah(i), |*MCR.n
|					err.mess) 	 
|		endfor |*MCR .n				
		for i = 1 to 3
			calculate.amount.in.home.currency.variables(tax.base.amounts.array(1,1),
		 		i.ccur,
			 	i.rate(i),
 	 	 		i.ratf(i), 
		 		i,  
		 	 	var.tbah(i), 	 	 	 	 	 	 	  
		 	 	err.mess)
		endfor	 	 
|*  MCR -  new changes .e 		

		fill.cisli206.fields(
			i.company,			|1
			i.trans.type,			|2
			i.document,			|3
			i.tax.country,			|4
			tax.codes.array(1,1),		|5
			i.ceno, 			|6
			i.company, 			|7
			tax.base.amounts.array(1,1),	|8
			i.rcod,				|9
			var.tbah,			|10, i.tbah
			blank.tax.code.amount,		|11
			var.txah, 			|12, i.txah
			i.sfcy,				|13
			i.stcy,  			|14
			i.ifcy, 			|15
			i.itcy)				|16
								
			dump.cisli206.from.arrays()	
			blank.tax.code.dumped =  true
	endif
	if blank.tax.code.dumped = true then
		j = 2
	else
		j = 1
	endif
	for i = j to tax.counter
		if tax.amounts.filled.array(1,i) = 1 then

|*  MCR -  new changes .s 				
||			for i = 1 to 3 |*MCR.n			|binda.o
|			for element = 1 to 3 |*MCR.n		|binda.n
|	 	 		calculate.amount.in.other.currency(
|					tax.amounts.array(1,i),
|						|i.rate, |*MCR .o
|						|i.ratf, |*MCR .o
|						i.rate(element), |*MCR .n
|						i.ratf(element), |*MCR .n
|						i.ccur, 
|						|var.txah, |*MCR.o
|						var.txah(element), |*MCR.n
|						err.mess) 	 
|			endfor |*MCR .n	
			for element = 1 to 3
				calculate.amount.in.home.currency.variables(tax.amounts.array(1,i),
			 		i.ccur,
				 	i.rate(element),
	 	 	 		i.ratf(element), 
			 		element,  
			 	 	var.txah(element), 	 	 	 	 	 	 	  
			 	 	err.mess)
			endfor	 	 
|*  MCR -  new changes .e 	
|*  MCR -  new changes .s				
|			for element = 1 to 3 |*MCR.n	
|	 	 		calculate.amount.in.other.currency(
|						tax.base.amounts.array(1,i),
|						|i.rate, |*MCR .o
|						|i.ratf, |*MCR .o
|						i.rate(element), |*MCR .n
|						i.ratf(element), |*MCR .n
|						i.ccur, 
|						|var.tbah, |*MCR.o
|						var.tbah(element), |*MCR.e
|						err.mess) 	 
|			endfor |*MCR .n	

			
	
			for element = 1 to 3
				calculate.amount.in.home.currency.variables(tax.base.amounts.array(1,i),
			 		i.ccur,
				 	i.rate(element),				 	
	 	 	 		i.ratf(element), 
			 		element,  
			 	 	var.tbah(element), 	 	 	 	 	 	 	  			 	 	
			 	 	err.mess)			 	 	
			endfor	 	 
|*  MCR -  new changes .e 			
			
			fill.cisli206.fields(
				i.company,			|1
				i.trans.type,			|2
				i.document,			|3
				i.tax.country,			|4
				tax.codes.array(1,i),		|5
				 "", 				|i.ceno, 6
				i.company, 			|7
				tax.base.amounts.array(1,i),	|8
				"", 				|9, i.rcod
				var.tbah,			|10, i.tbah
				tax.amounts.array(1,i),		|11
				var.txah, 			|12, i.txah
				i.sfcy,				|13
				i.stcy,  			|14
				i.ifcy, 			|15
				i.itcy)				|16
									
				dump.cisli206.from.arrays()	
				
		endif
	endfor

}
function reinitialize.cache()
{
	table.reinitialized = true
	dump.records.from.cache.cisli209()
	idb.free()
	r.val = idb.init("bcsci002","", 1, 0, table.fields,get.compnr(),
	1, -1, false, 100,10,0)

	bcsci002.table.cached = idb.load("bcsci002","")
	already.accessed = true
	total.records = 0
	if table.reinitialized = true then
		record.counter = 0
		table.reinitialized = false
	endif

}
function free.memories()
{
	free.mem(tax.codes.array)
	free.mem(tax.amounts.array)
	free.mem(tax.base.amounts.array)
	free.mem(tax.amounts.filled.array)
}
function dump.cisli205.for.projects.invoices()
{
	 	 famn =  round.amnt.itpdround (( tppin100.amoi -  tppin100.amdi -  tppin100.amdl +  tppin100.csqz ) ,tppin100.ccur,TP_PDM_AALC )  
	 	 if famn <  0 then  
	 	 	 invoice.transaction.type =  tpprc000.ttsc  
	 	 else  
	 	 	 invoice.transaction.type =  tpprc000.ttys  
	 	 endif  
	 	 record.found =  true  
	 	 select tfacr200.*   
	 	 from tfacr200  
	 	 where tfacr200._index1 =  { :invoice.transaction.type, :tppin100.ninv, 0}  
|	 	 and tfacr200.trec =  tfacr.trec.invoice  		|#200-101927.o
									|#200-101927.sn
	 	 and (tfacr200.trec =  tfacr.trec.invoice  or		
		      tfacr200.trec =  tfacr.trec.credit)
									|#200-101927.en
	 	 as set with 1 rows  
	 	 selectdo  
									|#200-101927.sn
			if tfacr200.trec = tfacr.trec.invoice then
				var.tfacr200.trec = target(cisli.doct.invoice)
			else
				var.tfacr200.trec = target(cisli.doct.credit)	
									|#148886.so
|				 if var.cisli205.dcai > 0 then
|					var.cisli205.dcai = -var.cisli205.dcai
|				 endif
|				 if var.cisli205.rmai > 0 then
|					var.cisli205.rmai = -var.cisli205.rmai
|				 endif
									|#148886.eo
			endif

									|#200-101927.en

	 	 selectempty  
	 	 	 record.found =  false  
	 	 endselect 	 	  
	 	 if record.found =  false then  
	 	 	 return  
	 	 endif  
	 	 get.addresses ( "tccom000", edit$  (  ext.lng.comp,"999"  )  , err.mess  )
	 	 get.enterprise.unit ( tppin100.ocmp,ext.lng.comp,err.mess )  
	 	 
	 	  |*MCR - sn	 
	 	 	 for i = 1 to 3	 
	 	 	 	tmp.mc.tfacr200.amth(i) = fill.mc.field.double(tfacr200.amth,i)
	 	 	 	tmp.mc.tfacr200.vath(i) = fill.mc.field.double(tfacr200.vath,i)
	 	 	 	tmp.mc.tfacr200.svah(i) = fill.mc.field.double(tfacr200.svah,i)
	 	 	 	tmp.mc.tfacr200.rate(i) = fill.mc.field.double(tfacr200.rate,i)
	 	 	 	tmp.mc.tfacr200.ratf(i) = fill.mc.field.long(tfacr200.ratf,i)
	 	 	 	tmp.mc.tfacr200.laph(i) = fill.mc.field.double(tfacr200.laph,i)
	 	 	 	tmp.mc.tfacr200.dc1h(i) = fill.mc.field.double(tfacr200.dc1h,i)
	 	 	 	tmp.mc.tfacr200.dc2h(i) = fill.mc.field.double(tfacr200.dc2h,i)
	 	 	 	tmp.mc.tfacr200.dc3h(i) = fill.mc.field.double(tfacr200.dc3h,i)
 	 	 	 endfor
 	 	 |*MCR - en
	 	 
 	 	 fill.cisli205.fields ( tppin100.ocmp,tfacr200.ttyp, tfacr200.ninv,1,6,  
 	 	 bcstc008.bpid, bcstc004.cadr, bcstc018.sofd,  
 	 	 tfacr200.ccrs, tfacr200.cpay, new.txct,6,0,0,
		 no.of.records,0,0,0, tfacr200.ccur,tfacr200.docd,  
 	 	 tfacr200.amti , 
 	 	 |tfacr200.amth,
 	 	 tmp.mc.tfacr200.amth,
 	 	 tfacr200.vati , 
		 |tfacr200.vath ,
		 tmp.mc.tfacr200.vath,
		 tfacr200.svam ,
		 |tfacr200.svah ,  
		 tmp.mc.tfacr200.svah,
 	 	 |tfacr200.rate,
 	 	 tmp.mc.tfacr200.rate,
 	 	 |tfacr200.ratf,
 	 	 tmp.mc.tfacr200.ratf,
 	 	 tfacr200.lpdt, tfacr200.lapa , 
		 |tfacr200.laph ,
		 tmp.mc.tfacr200.laph,
		 0.0,  0.0,var.cisli205.rmai, tfacr200.dc1i ,
		 |tfacr200.dc1h , 
		 tmp.mc.tfacr200.dc1h,
		 tfacr200.did1, tfacr200.dc2i , 
		 |tfacr200.dc2h ,
		 tmp.mc.tfacr200.dc2h,
		 tfacr200.did2,tfacr200.dc3i ,  
 	 	 |tfacr200.dc3h ,
 	 	 tmp.mc.tfacr200.dc3h,
 	 	 tfacr200.did3, tfacr200.paym, 0,
							
|		 var.cisli205.dcai,tfacr200.amti -  tfacr200.vati )  	|#200-101927.o
									|#200-101927.sn			
		 var.cisli205.dcai,tfacr200.amti -  tfacr200.vati,var.tfacr200.trec )  
									|#200-101927.en	
 	
	 	 dump.cisli205 ()  
 	
	 	 reset.variables.cisli205 ()  			

}
								
								
|**************   End of Library   *********************************************
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:cp
:TCMOD:rrp
:TFLNO:100
:FUNC:START
function long get.plmt(domain tcplcd 	i.plmt,
		  domain timrp.plnm	i.plnm)
{
	long	plmt

	if i.plmt = tcplcd.forward then
		if i.plnm = timrp.plnm.lead.time then
			plmt = target(cprrp.plmt.forw.lt)
		else
			plmt = target(cprrp.plmt.forward)
		endif
	else
		if i.plnm = timrp.plnm.lead.time then
			plmt = target(cprrp.plmt.backw.lt)
		else
			plmt = target(cprrp.plmt.backward)
		endif
	endif

	return(plmt)
}

function long get.mps.osta(domain timps.osta i.osta)
{
	long	osta

	on case i.osta
	case timps.osta.planned:
		osta = target(cprrp.osta.planned)

		break
	case timps.osta.firm.planned:
		osta = target(cprrp.osta.firmplanned)

		break
	case timps.osta.confirmed:
		osta = target(cprrp.osta.confirmed)

		break
	default:
		osta = target(cprrp.osta.planned)
	endcase

	return(osta)
}

function long get.mrp.osta(domain tcorsa i.osta)
{
	long	osta

	on case i.osta
	case tcorsa.planned:
		osta = target(cprrp.osta.planned)

		break
	case tcorsa.firm.planned:
		osta = target(cprrp.osta.firmplanned)

		break
	case tcorsa.release:
		osta = target(cprrp.osta.confirmed)

		break
	default:
		osta = target(cprrp.osta.planned)
	endcase

	return(osta)
}

function long get.time(	domain	tckotr	i.kotr)
{
|* Requirement is at the start of the day; Receipt is at the end of the day.
	if i.kotr = tckotr.requirement then
		return(bcsbp000.swkh * 3600 + bcsbp000.swkm * 60)
	else
		return(bcsbp000.ewkh * 3600 + bcsbp000.ewkm * 60)
	endif
}

function domain tcpono get.inv150.position.number(
				domain	tccprj	i.cprj,
				domain	tcorno	i.orno)
{
	static	domain	tcncmp		hold.site
	static	domain	tccprj		hold.cprj
	static	domain	tcorno		hold.orno
	static	domain	tcpono		hold.position.number

	if hold.site <> get.compnr() or
	   hold.cprj <> i.cprj or
	   hold.orno <> i.orno then
		hold.position.number = 0
		select	count(*):hold.position.number
		from	tipcs500
		where	tipcs500._index1 = {:i.cprj, tckoor.pcs.sfc, :i.orno, tckotr.requirement}
		as set with 1 rows
		endselect

		hold.site = get.compnr()
		hold.cprj = i.cprj
		hold.orno = i.orno
	endif

	hold.position.number = hold.position.number + 1

	return(hold.position.number)
}

function domain tirou.opro get.routing(	domain	tccprj	i.cprj,
					domain	tcitem	i.item)
{
	domain	tcitem	dum.item

	select	tipcs023.mitm:dum.item
	from	tipcs023
	where	tipcs023._index1 = {:i.cprj, :i.item}
	as set with 1 rows
	selectdo
		return(default.routing)
	endselect

	return("")
}

function string get.utc(	domain	tcdate	i.date.field,	|#206522.sn
				long	i.time.field)
|# as these utc fields are mandatory, date is filled with Jan. 04 1970.
|# if left empty, error messages will occur.
{
	if i.date.field = 0 then
		return(utc(719166, i.time.field))
	else
		return(utc(i.date.field, i.time.field))
	endif
}							|#206522.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ec
:TCMOD:edi
:TFLNO:200
:FUNC:START
|#202607.sn
function domain	 tcbool added.to.array()
{
			long		l.number.of.records
			long		i
		domain	tcedi.orga	l.new.organisation(1)	based
		domain	tcedi.mess	l.new.message(1)	based
		domain	tcedi.str35	l.new.code(1)		based
	static	domain	tcbool		s.array.allocated

	|* Allocate Array only required once per conversion

	if not s.array.allocated then
		select	count(*):l.number.of.records
		from	tcedi200
		selectdo
		selectempty
			l.number.of.records = 0
		endselect

		alloc.mem(l.new.organisation,	3, 	l.number.of.records)
		alloc.mem(l.new.message, 	6, 	l.number.of.records)
		alloc.mem(l.new.code, 		35, 	l.number.of.records)
		s.array.allocated = true
	endif


	|* Search array for duplicate value
	for i = 1 to l.number.of.records
		if l.new.organisation(1,i) = bcstc028.norg and
		   l.new.message(1,i) = bcstc028.nmes and
		   l.new.code(1,i) = tcedi200.code then
			|* Duplicate found exit function
			return(false)
		endif
 		if isspace(l.new.organisation(1, i)) then
			|* current slot is empty store new mapping in slot
			l.new.organisation(1, i) = bcstc028.norg
			l.new.message(1, i)	 = bcstc028.nmes
			l.new.code(1, i)	 = tcedi200.code
			return(true)
		endif
	endfor
	return(true)
}								|#202607.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ec
:TCMOD:edi
:TFLNO:463
:FUNC:START
function domain bccadr convert.address.code.in.application(
				domain	tccuno	i.customer,
				domain	tccdel	i.code.in.appl,
				domain	bctabl	i.table.name)
{
		domain	bcstc.keys	l.key		fixed
	static	domain	bcstc.keys	s.key		fixed
	static	domain	bctabl		s.table.name	fixed
	static	domain	bccadr		s.address

	l.key(1;6) = i.customer(1;6)
	l.key(7;3) = i.code.in.appl(1;3)

	if s.key <> l.key or
	   s.table.name <> i.table.name then
		s.key = l.key
		s.table.name = i.table.name
		s.address = get.address.code(
 					i.table.name,
 	 	 			l.key,
	 	 	 		err.mess)
	endif

	return(s.address)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:fm
:TCMOD:frc
:TFLNO:250
:FUNC:START
function fill.fmfrc.record.variables(	domain	tcitem	i.item,
					long 		i.luni,
					double	i.llim,
					double	i.ulim)
{

var.fmfrc250.cbas = 5		|* Initialize variable
var.fmfrc250.ccur = empty()	|* Initialize variable
var.fmfrc250.unit = empty()	|* Initialize variable
var.fmfrc250.llwg = 0		|* Initialize variable
var.fmfrc250.ulwg = 0		|* Initialize variable
var.fmfrc250.llqu = 0		|* Initialize variable
var.fmfrc250.ulqu = 0		|* Initialize variable
var.fmfrc250.llfv = 0		|* Initialize variable
var.fmfrc250.ulfv = 0		|* Initialize variable

|* Constant values in B60
|* fm.cbas.qnty = 1
|* fm.cbas.wght = 2
|* fm.cbas.fval = 5

on case i.luni
case 1:
| weight
	var.fmfrc250.cbas = 2
	var.fmfrc250.llwg = i.llim
	var.fmfrc250.ulwg = i.ulim
	var.fmfrc250.unit = tcmcs000.cwei
	break
case 2:
| gross.amount
case 3:
| net.amount
	var.fmfrc250.cbas = 5
	var.fmfrc250.llfv = i.llim
	var.fmfrc250.ulfv = i.ulim

	select	tiitm001.ccur
	from	tiitm001
	where	tiitm001._index1 = {:i.item}
	as set with 1 rows
	selectdo
	selectempty
		 tiitm001.ccur = ""
	endselect
	var.fmfrc250.ccur = tiitm001.ccur
	break
case 4:
| stock.unit
case 5:
| price.unit
case 6:
| sales.unit / purchase.unit
case 7:
| pack.unit
case 8:
| plan.unit
case 9:
| rate.unit
	var.fmfrc250.cbas = 1
	var.fmfrc250.llqu = i.llim
	var.fmfrc250.ulqu = i.ulim
|	var.fmfrc250.unit = bcstc000.buqu		|#211893.o
	var.fmfrc250.unit = bcsfm000.buqu		|#211893.n
	break
endcase

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tc
:TCMOD:com
:TFLNO:000
:FUNC:START
|function create.intermediate.file.for.tccom000.vatl()   	|#137585.sn
|{
|       		long    fp
|        	long    ret
|		long	int.comp.no
|        	string  var.comm.path(1024)
|        	string  var.comp.no(50)
|	extern	string	SEPARATOR(1) 	|* Field Seperator
|        
|        var.comp.no = edit$(get.compnr(),"999")
|	get.var(pid, "extn.separator", SEPARATOR)
|        
|        if ostype() = OS_UNIX then
|                var.comm.path = sprintf$("%s/tmp/migration/%s",getenv$("BSE"),
|				var.comp.no)
|        else
|                var.comm.path = sprintf$("%s\tmp\migration\%s",getenv$("BSE"),
|				var.comp.no)      
|        endif
|        
|        ret = dir.open(var.comm.path)
|
|        if ret < 0 then
|                mkdir(var.comm.path)    
|        else
|                dir.close(ret)
|        endif 
|                
|        if ( ostype() = OS_UNIX ) then
|                fp = seq.open(sprintf$("%s/COM.%s",strip$(var.comm.path),
|					var.comp.no), "w")
|        else
|                fp = seq.open(sprintf$("%s\COM.%s",strip$(var.comm.path),
|					var.comp.no), "w")
|        endif
|
|
|	int.comp.no = lval(var.comp.no)
|        
|        select  bccnv901.cadr, tccom000.vatl
|        from    tccom000, bccnv901
|	where	tccom000.ncmp = :int.comp.no and 
|		bccnv901.keys = :var.comp.no and
|		bccnv901.tabl = "tccom000"
|        selectdo
|                seq.puts(tccom000.vatl, fp)
|        endselect
|        seq.close(fp)
|}                                                               |#137585.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tc
:TCMOD:com
:TFLNO:100
:FUNC:START
#include	"itcmcs0095"


|#201931.sn

function	 create.bps.for.delivery.adresses()   
{
	string	l.adress.key(9)

	if bcstc000.bpda = tcyesno.no then		|#209387.sn
		return
	endif						|#209387.en
	
	hold.tccom100.bpid = var.tccom100.bpid
	hold.tccom010.cadr = var.tccom010.cadr
	hold.tccom111.cadr = var.tccom111.cadr
	hold.tccom110.ccnt = var.tccom110.ccnt

	select	tccom013.*
	from	tccom013
	where	tccom013._index1 = {:tccom010.cuno}
	selectdo
		|* build id for new business partner. E.g cuno = "   ABC"; cdel = "  1"
		|* bpid will be: "ABC     1". cuno is shifted left, cdel not. Reasons for this:
		|* - minimize risks for duplicates,
		|* - maxime ease of use when doing a Find in session tccom1117m000
		|*. NB If this logic is changed, also change migration object tccom117. 
		var.tccom100.bpid = ""
		var.tccom100.bpid(1;6) = shiftl$(tccom013.cuno)
		var.tccom100.bpid(7;3) = tccom013.cdel

		|* determine adress codes
		l.adress.key = ""
		l.adress.key(1;6) = tccom013.cuno 
		l.adress.key(7;3) = tccom013.cdel 
		if get.addresses("tccom013", l.adress.key, o.mess) then  
 	 	 	var.tccom010.cadr =  ""                                                                                                                                                                                                                                                                                                                                                                                                    
			var.tccom111.cadr = ""
 		 else  
			var.tccom010.cadr =  bcstc004.cadr  
			var.tccom111.cadr =  bcstc004.cadr
	 	 endif 	 	

		|* new bp's (for delivery adress) do not have a contact code
		var.tccom110.ccnt = ""

		|* create general business partner
		dump 1

		|* create Sold to Role
		dump3

	endselect
	
	var.tccom100.bpid = hold.tccom100.bpid
	var.tccom010.cadr = hold.tccom010.cadr
	var.tccom111.cadr = hold.tccom111.cadr
	var.tccom110.ccnt = hold.tccom110.ccnt
}

|#201931.en



function create.default.parent.bp(		|#200003.sn
		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 	 	

	|* 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
}						|#200003.en
	
	

function add.business.partner.to.list(domain bcstc.bpid i.business.partner)	|#800-141749.sn
{
        get.next.code.depth(bp.code.depth)
        bp.code.list(1, bp.code.depth) = i.business.partner
}									|#800-141749.en

function sort.bp.code.list()				|#800-141749.sn
{
	long 	rt

	rt = qss.sort(bp.code.list, sorted.bp.code.def, bp.code.depth)

        	if rt <> 0 then
                bp.code.depth = 0
        	endif
}								|#800-141749.en

function get.next.code.depth(REF long  i)				|#800-141749.sn
{
                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
}								|#800-141749.en

function define.length.and.search.def()				|#800-141749.sn
{
	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)
}								|#800-141749.en

function domain tcbool bp.code.in.sorted.list(domain bcstc.bpid i.bp.code)	|#800-141749.sn
{
	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)
}								|#800-141749.en

								|#800-142237.sn
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)
}								|#800-142237.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tc
:TCMOD:com
:TFLNO:140
:FUNC:START
|#210314.sn

function init.cmsk()
{
	var.tccom145.cmsk.1 = target(tcyesno.no)
	var.tccom145.cmsk.2 = target(tcyesno.no)
	var.tccom145.cmsk.3 = target(tcyesno.no)
	var.tccom145.cmsk.4 = target(tcyesno.no)
	var.tccom145.cmsk.5 = target(tcyesno.no)
	var.tccom145.cmsk.6 = target(tcyesno.no)
	var.tccom145.cmsk.7 = target(tcyesno.no)
	var.tccom145.cmsk.8 = target(tcyesno.no)
}
	
|#210314.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tc
:TCMOD:ibd
:TFLNO:001
:FUNC:START
function void determine.weight.unit(
			domain	tcuset	i.unit.set,
		ref	domain	tccuni	o.weight.unit)
{
	|FunctionUsage
	|Expl:	Check and fill weight unit. If default weight unit exists in unit set,
	|	weight unit is filled, else it is empty.
	|Pre:	Conversion Parameters Common (bcstc000) have been read.
	|EndFunctionUsage

	static	domain	tcuset	hold.unit.set
	static	domain	tccuni	hold.weight.unit

	if i.unit.set = hold.unit.set then
		o.weight.unit = hold.weight.unit
	else
		select	tcmcs012.uset
		from	tcmcs012
		where	tcmcs012._index1 =  {:i.unit.set, :bcstc000.cwun}
		as set with 1 rows
		selectdo  
			o.weight.unit =  bcstc000.cwun
		selectempty
			o.weight.unit =  empty.string(1;3)
		endselect
		hold.unit.set = i.unit.set
		hold.weight.unit = o.weight.unit
	endif
}

|#800-138991.sn  
function void create.item.from.tiitm001(
			domain	tcbool	i.customize.item,
			domain	tccprj	i.cprj,
			domain	tcbool	i.is.budget,
			domain	tcbccd	i.bccd)
{
	|FunctionUsage
	|Expl:	This function creates new item tables based on tiitm001 data.
	|	When a standard item from tiitm001 is migrated to a standard
	|	item in Baan 6, item tables whwmd400, tppdm005, tiipd001,
	|	tcibd200, tcibd100, ticpr007 and tcibd001 are created. 
	|	When a standard item on the customized BOM or a Material Sheet
	|	is customized, all tables except tppdm005 are filled.
	|Pre:	Record from tiitm001 has been read.
	|Input:	i.customize.item	-> false: create standard item
	|				-> true: create customized item
	|	i.cprj			-> (only relevant when i.customize.item = true)
	|				   The Project or Budget
	|	i.is.budget		-> Determines whether i.cprj is a Project
	|				   or a Budget.
	|	i.bccd			-> (Only relevant when i.is.budget = true)
	|				   Budget Calculation Code.
	|EndFunctionUsage

	if i.customize.item then
		if i.is.budget then
			var.tcibd001.cprj = get.budget.code(i.cprj, i.bccd)
		else
			var.tcibd001.cprj = get.ti.project(i.cprj)
		endif
	else
		var.tcibd001.cprj =  empty()  
	endif
	var.tcibd001.item =  segment.item.code(tiitm001.item,
						var.tcibd001.cprj)
	var.tcibd001.dfit = segment.item.code(tiitm001.item, 
						empty.string(1;9))
 	 var.ticpr007.ltcp =  utc(tiitm001.ltcp,0)  
|#800-140863.sn
	|* Do not dump to whwmd400 when Item Type = Equipment.
	if tiitm001.itmt <> tppdm.itmt.equipment then
|#800-140863.en
	 	 | *  Determine Lot Price value.  
	 	 if tiitm001.kltc =  tckltc.batch and  
	 	 tiitm001.stva =  tcstva.lot.price then  
	 	 	 var.whwmd400.ltpr =  tcyesno.yes  
	 	 else  
	 	 	 var.whwmd400.ltpr =  tcyesno.no  
	 	 endif  
|#800-140855.sn, |#800-140950.sn, |#800-140967.sn
		|* When item is lot controlled, Lot Tracking should get the
		|* value of Lot Tracking Implemented (tdltc000.tltc).
		if tiitm001.kltc = tckltc.batch then
			var.whwmd400.lttr = tdltc000.tltc
	 	 	var.whwmd400.linv =  tcyesno.yes  	|#SP1.n
		else
			var.whwmd400.lttr = tcyesno.no
	 	 	var.whwmd400.linv =  tcyesno.no  	|#SP1.n
		endif
		|* When item is serialized, Serial Tracking should get the
		|* value of Lot Tracking Implemented (tdltc000.tltc).
	 	|* Determine Serials in Inventory value. Set to 'Yes' when
		|* item is serialized.  
 	 	if tiitm001.kltc = tckltc.unit 
|		   or   (check.tssma.for.tcibd001.seri and	|#800-141936.o
|		    item.used.in.tssma("", tiitm001.item)) 	|#800-141936.o
		then
			var.whwmd400.srtr = tdltc000.tltc
	 	 	var.whwmd400.sinv =  tcyesno.yes  
		else
			var.whwmd400.srtr = tcyesno.no
	 	 	var.whwmd400.sinv =  tcyesno.no  
		endif
|#800-140855.en, |#800-140950.en, |#800-140967.en
|#800-139498.sn
		|* When ILC is implemented and Item Type is not equal to
		|* Cost or Service, field Location Controlled Item should be
		|* set to Yes. Else it is set to No.
		if tdilc000.iilc = tcyesno.yes and 
		   tiitm001.kitm <> tckitm.cost and
		   tiitm001.kitm <> tckitm.service then
			var.whwmd400.locc = tcyesno.yes
		else
			var.whwmd400.locc = tcyesno.no
		endif
|#800-139498.en
|#200-99295.sn
		|* Outbound methods Fifo or Lifo are only allowed when
		|* item type is purchased, manufactured or generic.
		 if tiitm001.kitm = tckitm.purchase or
	 	    tiitm001.kitm = tckitm.manufacture or
		    tiitm001.kitm = tckitm.generic then
			var.whwmd400.obpr = tiitm001.obpr
		else
			var.whwmd400.obpr = tcobpr.loca
		endif
|#200-99295.en
	 	 | *  Dump for whwmd400 from tiitm001.
	 	 dump 1
|#800-140863.sn
	endif
|#800-140863.en

	|* Do not create tppdm005 for customized items.
|# 200 -  95543.sn  
	|* Only items with Item Type (Project) =  Item are allowed
	|* in tppdm005, type =  Equipment or type =  Subcontracting
	|* are not allowed (check combined with other check).
|# 200 -  95543.en  
	if not i.customize.item and tiitm001.itmt =  tppdm.itmt.item then  
		 if (tiitm001.osys =  tcosys.mrp) then
	 	 	 | *  tppdm.osyb.prp (PRP)
 		 	 var.tppdm005.osys =  3
		 else
 		 	 | *  tppdm.osyb.mnl (Manually)
 	 		 var.tppdm005.osys =  9  
	 	 endif  
		 select	tppss901.item  
		 from 	tppss901  
		 where	tppss901.item =  :tiitm001.item  
		 as set with 1 rows  
		 selectdo  
		 	 var.tppdm005.usyn =  tppdm.yeno.yes  
		 selectempty  
		 	 var.tppdm005.usyn =  tppdm.yeno.no  
		 endselect
|#800-139354.sn
		|* When TP is not implemented (tppdm000.pprj = No),
		|* no check is done if the Cost Control Code is correctly filled.
		|* But at the target side there is a check, which will fail then
		|* in some cases.
		if tppdm000.pprj = tppdm.yeno.no
		   and tiitm001.ccit <> tiitm001.reli then
			var.tppdm005.ccit = tiitm001.reli
		else
			var.tppdm005.ccit = tiitm001.ccit
		endif
|#800-139354.en
	 	 | *  Dump for tppdm005 from tiitm001.  
	 	 dump 2  
	endif
	|* Create tiipd001 only for item types Manufactured,
	|* Purchased or Generic.
	if tiitm001.kitm = tckitm.manufacture or
	   tiitm001.kitm = tckitm.purchase or
	   tiitm001.kitm = tckitm.generic then
	 	if (tiitm001.unom =  0) then  
 		 	 var.tiipd001.unom =  1  
	 	else  
 		 	 var.tiipd001.unom =  tiitm001.unom  
	 	endif  
	 	if tiitm001.kitm =  tckitm.manufacture then  
 		 	 var.tiipd001.repi =  tiitm001.repi  
 	 		 var.tiipd001.scdl =  tiitm001.scdl  
	 	 	 if tiitm001.repi =  tcyesno.yes then  
 		 	 	 var.tiipd001.pcrp =  tiitm001.pcrp  
 	 		 else  
	 	 	 	 var.tiipd001.pcrp =  0  
 		 	 endif  
	 	else  
 		 	 var.tiipd001.repi =  tcyesno.no  
 	 		 var.tiipd001.scdl =  empty.string(1;3)  
	 	 	 var.tiipd001.pcrp =  0  
 		endif  
	 	if tiitm001.pics =  tcyesno.yes then  
 		 	 var.tiipd001.drin =  tcyesno.yes  
 	 		 var.tiipd001.dris =  tcyesno.yes  
	 	else  
 		 	 var.tiipd001.drin =  tcyesno.no  
 	 		 var.tiipd001.dris =  tcyesno.no  
	 	endif  
|# 200 -  95451.sn  
	 	|* When Backflush if Material is Yes, Direct Process
		|* Outbound is mandatory to be Yes too. This
		|* overrules the setting of Direct Process Outbound
		|* based on Floor Stock (above).
	 	 if tiitm001.bfcp =  tcyesno.yes then  
 		 	 var.tiipd001.dris =  tcyesno.yes  
	 	 endif  
|# 200 -  95451.en  
		 if (tiitm001.runi <>  0) then  
 		 	 var.tiipd001.runi =  tiitm001.runi  
	 	 else  
 		 	 var.tiipd001.runi =  1  
	 	 endif  
 		 if tiitm001.oqdr =  empty then  
 	 		 var.tiipd001.oqdr =  tcyesno.no  
	 	 else  
 		 	 var.tiipd001.oqdr =  tiitm001.oqdr  
	 	 endif  
| #  206137.sn
		check.var.tiipd001.drin.bfcp(tiitm001.bfcp)  
| #  206137.en  
 		 | *  Dump for tiipd001 from tiitm001.  
	 	 dump 3
	endif
|#800-139560.sn
	|* Do not create tcibd200 for cost and subcontracting items.
	|* Skip all logic for determining tcibd200 variables.
	if tiitm001.kitm <> tckitm.cost and
	   tiitm001.kitm <> tckitm.subcontracting then 	 
|#800-139560.en
	 	var.tcibd200.cplb =  format.emno(tiitm001.cplb)  
		if i.customize.item then
			|* In this case Order System should always be Planned.
			var.tcibd200.osys = 2
			|* Order Method always Lot-for-Lot.
			var.tcibd200.omth = tcomth.lfl
			var.tcibd200.uaoq = tcyesno.no
		else
			 on case tiitm001.osys  
			 case tcosys.sic:  
			 	 var.tcibd200.osys =  1  
			 	 break  
			 case tcosys.mps:  
			 case tcosys.mrp:  
			 case tcosys.fas:  
			 	 var.tcibd200.osys =  2  
			 	 break  
			 case tcosys.mnl:  
			 	 var.tcibd200.osys =  9  
			 	 break  
			 endcase  
			|* When item exists in bcstc003, then set
			|* var.tcibd200.osys to 2.  
		 	if get.check.if.order.system.manual(
				 	tiitm001.item,  
 	 	 	 	 	error.message) =  0 then  
			 	 var.tcibd200.osys =  2 	
			 	 |* Order System is Planned
		 	endif
			var.tcibd200.omth = tiitm001.omth
			var.tcibd200.uaoq = tcyesno.no
		endif
	 	if (tiitm001.maxs >  9999999.99) then  
	 		 var.tcibd200.maxs =  9999999.99  
	 	else  
	 		 var.tcibd200.maxs =  tiitm001.maxs  
	 	endif  
		|* Field Order Quantity Multiple Of is not allowed to be zero.
	 	if (tiitm001.oqmf =  0) then  
	 	 	 var.tcibd200.oqmf =  1  
	 	else  
	 	 	 var.tcibd200.oqmf =  tiitm001.oqmf  
	 	endif  
 | #  200 -  95747.sn ( +  200 -  96570)  
	 	 |* When Order System is MPS, then take Safety Stock,
	 	 |* Seasonal Pattern for Demand and Seasonal Pattern for
	 	 |* Inventory from Plan Item Data.  
	 	 if tiitm001.osys =  tcosys.mps then  
	 	 	 if timps000.irpi =  tcyesno.no then  
	 	 	 	 select	timps200.sfst, timps200.cspd,
	 	 	 	 	timps200.csps  
	 	 	 	 from 	timps200  
	 	 	 	 where	timps200._index3 = {:timps000.apln,
							:tiitm001.item}
	 	 	 	 as set with 1 rows  
	 	 	 	 selectdo  
	 	 	 	 	 var.tcibd200.sfst =  timps200.sfst  
	 	 	 	 	 var.tcibd200.cspd =  timps200.cspd  
	 	 	 	 	 var.tcibd200.csps =  timps200.csps  
	 	 	 	 selectempty  
	 	 	 	 	 var.tcibd200.sfst =  tiitm001.sfst  
	 	 	 	 	 var.tcibd200.cspd =  tiitm001.cspd  
	 	 	 	 	 var.tcibd200.csps =  tiitm001.csps  
	 	 	 	 endselect  
	 	 	 else  
	 	 	 	 select	cprpd100.sfst, cprpd100.plnc,
	 	 	 	 	cprpd100.plvl, cprpd100.plni,
	 	 	 	 	cprpd100.cont  
	 	 	 	 from 	cprpd100  
	 	 	 	 where	cprpd100._index2 =  {:cprpd000.aplc,
							:tiitm001.item}  
	 	 	 	 and 	 cprpd100.cont =  ""                                                                                                                                                                                                                                                                                                                                        
	 	 	 	 as set with 1 rows  
	 	 	 	 selectdo  
	 	 	 	 	 var.tcibd200.sfst =  cprpd100.sfst  
	 	 	 	 	 select	cprpd110.cspd, cprpd110.csps  
	 	 	 	 	 from 	cprpd110  
	 	 	 	 	 where	cprpd110._index1 =  {  
	 	 	 	 	 	 	 :cprpd100.plnc,  
	 	 	 	 	 	 	 :cprpd100.plvl,  
	 	 	 	 	 	 	 :cprpd100.plni,  
	 	 	 	 	 	 	 :cprpd100.cont}  
	 	 	 	 	 as set with 1 rows  
	 	 	 	 	 selectdo  
	 	 	 	 	 	 var.tcibd200.cspd = 
	 	 	 	 	 	 	cprpd110.cspd  
	 	 	 	 	 	 var.tcibd200.csps = 
	 	 	 	 	 	 	cprpd110.csps  
	 	 	 	 	 selectempty  
	 	 	 	 	 	 var.tcibd200.cspd = 
	 	 	 	 	 	 	tiitm001.cspd  
	 	 	 	 	 	 var.tcibd200.csps = 
	 	 	 	 	 	 	tiitm001.csps  
	 	 	 	 	 endselect  
	 	 	 	 selectempty  
	 	 	 	 	 var.tcibd200.sfst = tiitm001.sfst  
	 	 	 	 	 var.tcibd200.cspd = tiitm001.cspd  
	 	 	 	 	 var.tcibd200.csps = tiitm001.csps  
	 	 	 	 endselect  
	 	 	 endif  
	 	 else  
	 	 	 var.tcibd200.sfst =  tiitm001.sfst  
	 	 	 var.tcibd200.cspd =  tiitm001.cspd  
	 	 	 var.tcibd200.csps =  tiitm001.csps  
	 	 endif  
|# 200 -  95747.en ( +  200 -  96570)  
|#200-97803.sn
		|* When field Order Method is 'Lot for Lot' or 'Replenish to Max. Inv.'
		|* then field Minimum Order Quantity should at least have the value
		|* of field Order Quantity Multiple Of.
		if var.tcibd200.omth = tcomth.lfl or 
		   var.tcibd200.omth = tcomth.max then
			if double.cmp(tiitm001.mioq, var.tcibd200.oqmf,
							0.0001) = -1 then
				|* Set the minimum order quantity to the order
				|* quantity increment value.
				var.tcibd200.mioq = var.tcibd200.oqmf
			else
				var.tcibd200.mioq = tiitm001.mioq
			endif
		else
			var.tcibd200.mioq = tiitm001.mioq
		endif
		|* Field Fixed Order Quantity should not be less than the
		|* Minimum Order Quantity, although the source version 
		|* allows this.
		if double.cmp(tiitm001.fioq, var.tcibd200.mioq,
							0.0001) = -1 then
			|* Set the fixed order quantity to the minimum
			|* order quantity.
			var.tcibd200.fioq = var.tcibd200.mioq
		else
			var.tcibd200.fioq = tiitm001.fioq
		endif
|#200-97803.en
	 	 | *  Dump for tcibd200 from tiitm001.  
	 	 dump 4  
|#800-139560.sn
	endif
|#800-139560.en
	|* Table tcibd100 needs to be created also for the items which
	|* are customized. But fields are filled completely different.
	|* Therefore a new dump has been created for tcibd100.
	if i.customize.item then
		|* Dump for tcibd100 from tiitm001 when a standard
		|* item is customized.
		dump 28
	else
	 	 if is.masterdata.conversion() then  
 		 	|* In case only master data is migrated, the inventory  
 	 		|* fields in tcibd100 need to be reset. Because in the  
	 	 	|* other situation most field rules are filled with table  
 		 	|* fields and no special constructions, it is chosen here  
 	 		|* to create a separate dump for master data migration.

			|* Dump for tcibd100 from tiitm001 in case of
			|* master data conversion.
	 	 	 dump 31
	 	 else  
|#800-139016.sn
			|* Correct Allocated Inventory when Item Type is
			|* Purchased or Manufactured and when PCS is
			|* implemented.
			if tipcs000.ipcs = tcyesno.yes and
			   (tiitm001.kitm = tckitm.purchase or
			    tiitm001.kitm = tckitm.manufacture) then
				var.tcibd100.allo =
				determine.prp.inv.correction.for.standard.item(
					tiitm001.item,
					tiitm001.allo)
			else
				var.tcibd100.allo = tiitm001.allo
			endif
|#800-139016.en
|#800-140893.sn
			|* Correct Blocked Quantity: approved quantity
			|* on an inspection location is blocked quantity
			|* in Baan IV but not in Baan 6.
			var.tcibd100.blck = tiitm001.blck
			if tdilc000.iilc = tcyesno.yes and
			   var.tcibd100.blck > 0.0 then
				determine.blocked.quantity.decrease(
					"",		|* Project
					tiitm001.item,
					tmp.blocked.qty.decrease)
				if tmp.blocked.qty.decrease > 0.0 then
					var.tcibd100.blck = 
						var.tcibd100.blck - 
						tmp.blocked.qty.decrease
					if var.tcibd100.blck < 0.0 then
						var.tcibd100.blck = 0.0
					endif
				endif
			endif
|#800-140893.en
			|* Dump for tcibd100 from tiitm001 when a standard
			|* item is created from a standard item.
		 	 dump 5  
		endif
	endif
 	 if (tiitm001.kitm =  tckitm.purchase or  
 	 tiitm001.kitm =  tckitm.manufacture or  
 	 tiitm001.kitm =  tckitm.generic) then  
		get.aggr.cost.comp.for.item(
 	  	 	 	tiitm001.kitm,  
 	  	 	 	tiitm001.citg,  
				tiitm001.cpcp,
 	  	 	 	var.ticpr007.acpm,  
 	  	 	 	var.ticpr007.acpo,  
 	  	 	 	var.ticpr007.acps)  
 	 endif  
 	 |*  For cost and service items, only the material cost component needs
 	 |*  to be filled. This cost component is read from bcstc006.  
 	 if tiitm001.kitm =  tckitm.cost or  
 	 tiitm001.kitm =  tckitm.service then  
 	 	 return.value =  get.cost.components(  
 	 	 	 	 	 	 bcorig.ticpr,  
 	 	 	 	 	 	 tiitm001.cpcp,  
 	 	 	 	 	 	 "",                                                                                                                                                                                                                                                                                                                                        
 	 	 	 	 	 	 error.message)  
 	 	 var.ticpr007.acpm =  bcstc006.cost  
 	 	 var.ticpr007.acpo =  empty.string(1;8)  
 	 	 var.ticpr007.acps =  empty.string(1;8)  
 	 endif  
 	 | *  Read cost component chart if present.  
 	 if get.cost.component.charts.per.item.type.and.group(  
 	 	 	 	 	 tiitm001.kitm,  
 	 	 	 	 	 tiitm001.citg,  
 	 	 	 	 	 error.message) =  0 then  
 	 	 var.ticpr007.chrt =  bcsti007.chrt  
 	 else  
 	 	 var.ticpr007.chrt =  ""                                                                                                                                                                                                                                                                                                                                        
 	 endif  
|#200-97615.sn
	|* Surcharge by Item should only be Yes for Purchased,
	|* Manufactured or Generic items.
 	 if (tiitm001.kitm =  tckitm.purchase or
 	 tiitm001.kitm =  tckitm.manufacture or
 	 tiitm001.kitm =  tckitm.generic) then
		var.ticpr007.spit = tcyesno.yes
	else
		var.ticpr007.spit = tcyesno.no
	endif
|#200-97615.en
	 for element =  1 to 3  
 	 	 var.ticpr007.ecpr(element) =  fill.mc.field.double(  
 	 	 	 	 tiitm001.copr,  
 	 	 	 	 element,  
 	 	 	 	 0.0) 	  
 	 	 var.ticpr007.emtc(element) =  fill.mc.field.double(  
 	 	 	 	 tiitm001.matc,  
 	 	 	 	 element,  
 	 	 	 	 0.0) 	  
 	 	 var.ticpr007.eopc(element) =  fill.mc.field.double(  
 	 	 	 	 tiitm001.oprc,  
 	 	 	 	 element,  
 	 	 	 	 0.0) 	  
 	 endfor  	 
 	 if (tiitm001.kitm <>  tckitm.subcontracting) then  
 	 	 if(tiitm001.itmt <>  tppdm.itmt.equipment and  
 	 	 tiitm001.itmt <>  tppdm.itmt.subcontracting ) then  
 	 	 	 | *  Dump for ticpr007 from tiitm001.  
 	 	 	 dump 6  
 	 	 endif  
 	 endif  
 	 if (tiitm001.itmt =  tppdm.itmt.equipment) then  
 	 	 | *  tckitm.equipment (Equipment)  
 	 	 var.tcibd001.kitm =  15  
 	 else  
 	 	 if (tiitm001.itmt =  tppdm.itmt.subcontracting) then  
 	 	 	 | *  tckitm.subcontracting (Subcontracting)  
 	 	 	 var.tcibd001.kitm =  6  
 	 	 else  
 	 	 	 var.tcibd001.kitm =  etol(tiitm001.kitm)  
 	 	 endif  
 	 endif  
 	 equipment.item =  left.align( tiitm001.item, 16 )  
 	 equipment.item =  right.align( equipment.item, 10 )  
 	 subcontracting.item =  left.align(tiitm001.item, 16 )  

 	 subcontracting.item =  right.align(subcontracting.item, 8 )  
 	 if tiitm001.itmt =  tppdm.itmt.item then  
 	 	 var.tcibd001.cuni =  tiitm001.cuni  
 	 	 determine.weight.unit(tiitm001.uset, var.tcibd001.cwun)  
 	 endif  
 	 if tiitm001.itmt =  tppdm.itmt.equipment then  
 	 	 select tppdm025.cuti  
 	 	 from 	 tppdm025  
 	 	 where tppdm025._index1 =  { :equipment.item }  
 	 	 as set with 1 rows  
 	 	 selectdo  
 	 	 	 var.tcibd001.cuni =  tppdm025.cuti  
 	 	 	 var.tcibd001.cwun =  empty.string(1;3)  
 	 	 endselect  
 	 endif  
 	 if tiitm001.itmt =  tppdm.itmt.subcontracting then  
 	 	 select tppdm035.cuti, tppdm035.dsbc, tppdm035.cuni  
 	 	 from 	 tppdm035  
 	 	 where tppdm035._index1 =  { :subcontracting.item }  
 	 	 as set with 1 rows  
 	 	 selectdo  
 	 	 	 var.tcibd001.cwun =  empty.string(1;3)  
 	 	 	 if (tppdm035.dsbc =  tppdm.dsbc.quantity) then  
 	 	 	 	 var.tcibd001.cuni =  tppdm035.cuni  
 	 	 	 else  
 	 	 	 	 var.tcibd001.cuni =  tppdm035.cuti  
 	 	 	 endif  
 	 	 endselect  
 	 endif  
 | #  200 -  95541.sn  
 	 | *  Cost and Service items do not have an inventory unit in Baan IV,  
 	 | *  but mandatory have one in Baan 6.0. Fill inventory unit with  
 	 | *  sales price unit.  
 	 if tiitm001.kitm =  tckitm.cost or  
 	 tiitm001.kitm =  tckitm.service then  
 	 	 var.tcibd001.cuni =  tiitm001.cups  
 	 endif  
 | #  200 -  95541.en  
 	 | *  Determine Lot Control yes /  no and Serialized yes /  no.  
 	 on case tiitm001.kltc  
 	 case tckltc.batch:  
 	 	 var.tcibd001.ltct =  tcyesno.yes  
 	 	 var.tcibd001.seri =  tcyesno.no  
 	 break  
 	 case tckltc.unit:  
 	 	 var.tcibd001.ltct =  tcyesno.no  
 	 	 var.tcibd001.seri =  tcyesno.yes  
 	 break  
 	 case tckltc.none:  
 	 	 var.tcibd001.ltct =  tcyesno.no  
 	 	 var.tcibd001.seri =  tcyesno.no  
 	 break  
 	 default:  
 	 	 var.tcibd001.ltct =  tcyesno.no  
 	 	 var.tcibd001.seri =  tcyesno.no  
 	 break  
 	 endcase
|#800-141936.so
|	|* If item is used in Service package, flag Serialized also
|	|* needs to be set to 'Yes'.
|	if var.tcibd001.seri = tcyesno.no and
|	  check.tssma.for.tcibd001.seri then
|		if item.used.in.tssma("", tiitm001.item) then
|			var.tcibd001.seri = tcyesno.yes
|		endif
|	endif
|#800-141936.eo
 	 | *  Determine Cost Component.  
 	 if not isspace(tiitm001.ccco) then  
 	 	 | *  Read bcstc006 for tppdm cost component type.  
 	 	 return.value =  get.cost.components(  
 	 	 	 	 	 bcorig.tppdm,  
 	 	 	 	 	 "",                                                                                                                                                                                                                                                                                                                                        
 	 	 	 	 	 tiitm001.ccco,  
 	 	 	 	 	 error.message)  
 	 	 var.tcibd001.cpcp =  bcstc006.cost  
 	 else  
 	 	 | *  Read bcstc006 for ticpr cost component type.  
 	 	 return.value =  get.cost.components(  
 	 	 	 	 	 bcorig.ticpr,  
 	 	 	 	 	 tiitm001.cpcp,  
 	 	 	 	 	 "",                                                                                                                                                                                                                                                                                                                                        
 	 	 	 	 	 error.message)  
 	 	 | *  Field bcstc006.cost is empty when record not found.  
 	 	 if not isspace(bcstc006.cost) then  
 	 	 	 var.tcibd001.cpcp =  bcstc006.cost  
 	 	 else  
 	 	 	 var.tcibd001.cpcp =  empty.string(1; 8)  
 	 	 endif  
 	 endif  

|#sp1.sn
	|* enum value of tiipd001.umer ( 'Update E - Item Relationship)
	|* are now indentical in Baan IV and ERP LN SP1.
	var.tcibd001.umer = tiitm001.umer
|#sp1.en
|#sp1.so
|| # 200 - 96920.sn 
|	| * Enum values for 'Update E - Item Relationship are changed. 
|	on case tiitm001.umer 
|	case tiitm.umer.yes : 
||		var.tcibd001.umer = target(tcibd.umer.automatic) |SP1.o
|		var.tcibd001.umer = target(tcibd.umer.yes) 	|SP1.n
|		break 
|	case tiitm.umer.freeze : 
|		var.tcibd001.umer = target(tcibd.umer.freeze) 	|SP1.sn
|		break 						|SP1.en
||#800-142021.sn
|	case tiitm.umer.break : 
||#800-142021.en
||		var.tcibd001.umer = target(tcibd.umer.manual) 	|SP1.o
|		var.tcibd001.umer = target(tcibd.umer.break) 	|SP1.n
|		break 
||#800-142021.so
||	case tiitm.umer.break : 
||#800-142021.eo
|	case tiitm.umer.not.appl : 
|	default : 
|		var.tcibd001.umer = target(tcibd.umer.not.appl) 
|		break 
|	endcase 
|| # 200 - 96920.en
|#sp1.eo
	
 	 | *  Dump for tcibd001 from tiitm001.  
 	 dump 7

}
|#800-138991.en

|#800-139016.sn
function domain tcqiv1 determine.prp.inv.correction.for.standard.item(
			domain	tcitem	i.item,
			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. This means that the Allocated
	|	Inventory in tiitm001 needs to be adjusted for these type
	|	of transactions.
	|EndFunctionUsage

	domain	tcqiv1	tmp.allocated.inventory

	tmp.allocated.inventory = 0.0

	select	sum(tdinv150.qana):tmp.allocated.inventory
	from	tdinv150
	where	tdinv150._index2 = {:i.item}
	and	tdinv150.cntr = ""
	and	tdinv150.koor = tckoor.pcs.sfc
	and	tdinv150.kotr = tckotr.requirement
	selectdo
	endselect

	return(i.old.allo.quantity - tmp.allocated.inventory)
}

function domain tcqiv1 correct.inv.on.order.customized.item.for.prp(
			domain	tccprj	i.project,
			domain	tcitem	i.item,
			domain	tcqiv1	i.old.ordr.quantity)
{

	|FunctionUsage
	|Expl:	See function usage of function
	|	'determine.prp.inv.correction.for.standard.item'.
	|	This function determines how the Inventory On Order of
	|	customized items needs to be corrected for Planned Receipts
	|	because of PRP Purchase Orders and PRP Production Orders.
	|EndFunctionUsage

	domain	tcqiv1	tmp.inventory.on.order

	tmp.inventory.on.order = 0.0

	select	sum(tipcs500.qana):tmp.inventory.on.order
	from	tipcs500
	where	tipcs500._index2 = {:i.project, :i.item}
	and	(tipcs500.koor = tckoor.pcs.sfc or
		 tipcs500.koor = tckoor.pcs.pur)
	and	tipcs500.kotr = tckotr.receipt

	selectdo
	endselect

	return(i.old.ordr.quantity - tmp.inventory.on.order)
}

function domain tcqiv1 correct.allocated.inv.customized.item.for.prp(
			domain	tccprj	i.project,
			domain	tcitem	i.item,
			domain	tcqiv1	i.old.allo.quantity)
{
	|FunctionUsage
	|Expl:	See function usage of function
	|	'determine.prp.inv.correction.for.standard.item'.
	|	This function determines how the Allocated Inventory of
	|	customized items needs to be corrected for Mat. Requirements
	|	because of PRP Production Orders.
	|EndFunctionUsage

	domain	tcqiv1	tmp.allocated.inventory

	tmp.allocated.inventory = 0.0

	select	sum(tipcs500.qana):tmp.allocated.inventory
	from	tipcs500
	where	tipcs500._index2 = {:i.project, :i.item}
	and	tipcs500.koor = tckoor.pcs.sfc
	and	tipcs500.kotr = tckotr.requirement
	selectdo
	endselect

	return(i.old.allo.quantity - tmp.allocated.inventory)
}
|#800-139016.en
|#800-141936.so
|function void check.if.records.in.tssma.tables()
|{
|	|FunctionUsage
|	|Expl:	This function checks if there are records present
|	|	in tables tssma102 and/or tssma105.
|	|EndFunctionUsage
|
|	select	tssma102.cins
|	from	tssma102
|	as set with 1 rows
|	selectdo
|		check.tssma.for.tcibd001.seri = true
|	selectempty
|		select	tssma105.item
|		from	tssma105
|		as set with 1 rows
|		selectdo
|			check.tssma.for.tcibd001.seri = true
|		selectempty
|			check.tssma.for.tcibd001.seri = false
|		endselect
|	endselect
|}

|function void load.items.from.tssma.tables.in.memory()
|{
|	|FunctionUsage
|	|Expl.:	This function loads all items from tssma102 and
|	|	tssma105 in memory. There is a first array with
|	|	standard items and a second array with 
|	|	customized items.
|	|	Standard items can come from both tables.
|	|	Customized items can only come from tssma102.
|	|	The dummy item from bcsts000 is also loaded
|	|	in the array with standard items.
|	|EndFunctionUsage
|
|		domain	tcmcs.long	project.length
|		domain	tcmcs.long	item.length
|		domain	tcmcs.long	sort.definition(1, 4)
|
|	g.nr.standard.items = 0
|	g.nr.customer.items = 0
|
|	|* Load dummy item from bcsts000 into memory.
|	return.value = read.parameters.service(error.message)
|	if not isspace(bcsts000.item) then
|		g.nr.standard.items = g.nr.standard.items + 1
|		allocate.memory.standard.item.array()
|		gt.sma.standard.items(1, g.nr.standard.items) =
|			bcsts000.item
|	endif
|
|	|* Load items from tssma105 into memory.
|	|* No check required if tssma105.item is filled, because
|	|* it is a mandatory field.
|	select	tssma105.item
|	from	tssma105
|	group by tssma105.item
|	order by tssma105.item
|	selectdo
|		g.nr.standard.items = g.nr.standard.items + 1
|		allocate.memory.standard.item.array()
|		gt.sma.standard.items(1, g.nr.standard.items) =
|			tssma105.item
|	endselect
|
|	|* Load items from tssma102 into memory. But skip the
|	|* items which were already loaded from tssma105.
|	|* No check required if tssma102.cprj and/or tssma102.item
|	|* are filled. They can be empty, but in that case the
|	|* dummy item needs to be used.
|	select	tssma102.cprj, tssma102.item
|	from	tssma102
|	where	tssma102.cprj <> "" or
|		(tssma102.cprj = "" and not
|		 exists	(select	tssma105.item
|			from	tssma105
|			where	tssma105.item = tssma102.item))
|	group by tssma102.cprj, tssma102.item
|	order by tssma102.cprj, tssma102.item
|	selectdo
|		if dummy.item.has.to.be.used(
|				tssma102.cprj,
|				tssma102.item) then
|			|* Dummy item is already loaded in memory.
|			continue
|		endif
|		if isspace(tssma102.cprj) then
|			g.nr.standard.items = g.nr.standard.items + 1
|			allocate.memory.standard.item.array()
|			gt.sma.standard.items(1,
|				g.nr.standard.items) = tssma102.item
|		else
|			g.nr.customer.items = g.nr.customer.items + 1
|			allocate.memory.customer.item.array()
|			gt.sma.customer.items(1,
|				g.nr.customer.items) =
|				tssma102.cprj & tssma102.item
|		endif
|	endselect
|
|	|* Sort the arrays, that enables binary search.
|	|* Arrays are only sorted for the number of filled array
|	|* elements, otherwise spaces will come before filled 
|	|* array elements.
|	read.domain.lengths(project.length, item.length)
|	if g.nr.standard.items > 0 then
|		QSS.START(sort.definition, 1, 1)
|		QSS.TYPE(sort.definition, 1, DB.STRING)
|		QSS.LENGTH(sort.definition, 1, item.length)
|		QSS.SORT(	gt.sma.standard.items,
|				sort.definition,
|				g.nr.standard.items)
|	endif
|	if g.nr.customer.items > 0 then
|		QSS.START(sort.definition, 1, 1)
|		QSS.TYPE(sort.definition, 1, DB.STRING)
|		QSS.LENGTH(sort.definition, 1, 
|				project.length + item.length)
|		QSS.SORT(	gt.sma.customer.items,
|				sort.definition,
|				g.nr.customer.items)
|	endif
|}


|function void allocate.memory.standard.item.array()
|{
|		domain	tcmcs.long	project.length
|		domain	tcmcs.long	item.length
|	static	domain	tcmcs.long	allocated.memory
|
|	|* Allocate memory if required.
|	if allocated.memory < g.nr.standard.items then
|		read.domain.lengths(project.length, item.length)
|		allocated.memory = allocated.memory + 1000
|		return.value = alloc.mem(gt.sma.standard.items, 
|			item.length, allocated.memory)
|	endif
|}

|function void allocate.memory.customer.item.array()
|{
|		domain	tcmcs.long	project.length
|		domain	tcmcs.long	item.length
|	static	domain	tcmcs.long	allocated.memory
|
|	|* Allocate memory if required.
|	if allocated.memory < g.nr.customer.items then
|		read.domain.lengths(project.length, item.length)
|		allocated.memory = allocated.memory + 1000
|		return.value = alloc.mem(gt.sma.customer.items, 
|				project.length + item.length,
|				allocated.memory)
|	endif
|}

|function void read.domain.lengths(
|		ref	domain	tcmcs.long	o.project.length,
|		ref	domain	tcmcs.long	o.item.length)
|{
|	static	domain	tcbool		already.initialized
|	static	domain	tcmcs.long	hold.project.length
|	static	domain	tcmcs.long	hold.item.length
|		domain	tcmcs.long	dummy.convert
|
|	|* Read item domain length and project domain length
|	|* if not done before.
|	if not already.initialized then
|		rdi.domain.string("tccprj",
|			hold.project.length, dummy.convert)
|
|		rdi.domain.string("tcitem",
|			hold.item.length, dummy.convert)
|		already.initialized = true
|	endif
|	o.project.length = hold.project.length
|	o.item.length = hold.item.length
|}

|function domain tcbool item.used.in.tssma(
|			domain	tccprj	i.project,
|			domain	tcitem	i.item)
|{
|	static	domain	tcbool		already.initialized
|	static	domain	tcmcs.long	search.def.standard(1, 4)
|	static	domain	tcmcs.long	search.def.customer(1, 4)
|		domain	tcmcs.long	project.length
|		domain	tcmcs.long	item.length
|		domain	tcmcs.long	qss.search.index
|
|	if not already.initialized then
|		read.domain.lengths(project.length, item.length)
|
|		QSS.START(search.def.standard, 1, 1)
|		QSS.TYPE(search.def.standard, 1, DB.STRING)
|		QSS.LENGTH(search.def.standard, 1, item.length)
|
|		QSS.START(search.def.customer, 1, 1)
|		QSS.TYPE(search.def.customer, 1, DB.STRING)
|		QSS.LENGTH(search.def.customer, 1, 
|				project.length + item.length)
|
|		already.initialized = true
|	endif
|
|	if isspace(i.project) then
|		if g.nr.standard.items > 0 then
|			qss.search.index = QSS.SEARCH(
|			QSS.EQUAL + QSS.SRC.IS.SORTED,
|			i.item, gt.sma.standard.items,
|			search.def.standard, g.nr.standard.items)
|			if qss.search.index > 0 then
|				return(true)
|			endif
|		endif
|	else
|		if g.nr.customer.items > 0 then
|			qss.search.index = QSS.SEARCH(
|			QSS.EQUAL + QSS.SRC.IS.SORTED,
|			i.project & i.item, gt.sma.customer.items,
|			search.def.customer, g.nr.customer.items)
|			if qss.search.index > 0 then
|				return(true)
|			endif
|		endif
|	endif
|	return(false)
|}
|#800-141936.eo
|#800-140893.sn
function void determine.blocked.quantity.decrease(
			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._index2 = {	: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
}
|#800-140893.en

function check.var.tiipd001.drin.bfcp(
		domain tcyesno	i.bfcp)
{
	|* The fields tiipd001.drin and tiipd001.bfcp cannot be both on yes
	if var.tiipd001.drin = tcyesno.yes and i.bfcp = tcyesno.yes 
	then
		var.tiipd001.drin = tcyesno.no
	endif
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tc
:TCMOD:ibd
:TFLNO:002
:FUNC:START
|#206188.sn
function void determine.weight.unit(
			domain	tcuset	i.unit.set,
		ref	domain	tccuni	o.weight.unit)
{
	|FunctionUsage
	|Expl:	Check and fill weight unit. If default weight unit exists in unit set,
	|	weight unit is filled, else it is empty.
	|Pre:	Conversion Parameters Common (bcstc000) have been read.
	|EndFunctionUsage

	static	domain	tcuset	hold.unit.set
	static	domain	tccuni	hold.weight.unit

	if i.unit.set = hold.unit.set then
		o.weight.unit = hold.weight.unit
	else
		select	tcmcs012.uset
		from	tcmcs012
		where	tcmcs012._index1 =  {:i.unit.set, :bcstc000.cwun}
		as set with 1 rows
		selectdo  
			o.weight.unit =  bcstc000.cwun
		selectempty
			o.weight.unit =  empty.string(1;3)
		endselect
		hold.unit.set = i.unit.set
		hold.weight.unit = o.weight.unit
	endif
}

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

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tc
:TCMOD:ibd
:TFLNO:401
:FUNC:START
function domain tcbool	item.is.serialized(
				domain	tccprj	i.cprj, 
				domain	tcitem	i.item)
{
	domain	tcbool	tmp.serialized

	tmp.serialized = false

	if isspace(i.cprj)then
		select	tiitm001.kltc
		from	tiitm001
		where	tiitm001.item = :i.item
		as set with 1 rows
		selectdo
			if tiitm001.kltc = tckltc.unit then
				tmp.serialized = true
			endif
		endselect
	else
		
		select	tipcs021.kltc
		from	tipcs021
		where	tipcs021.cprj = :i.cprj
		and	tipcs021.item = :i.item
		as set with 1 rows
		selectdo
			if tipcs021.kltc = tckltc.unit then
				tmp.serialized = true
			endif
		endselect
	endif
	return(tmp.serialized)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tc
:TCMOD:mcs
:TFLNO:036
:FUNC:START
function dump.eu.related.records.in.tcmcs036(
		domain	tccvat	i.cvat,
		domain	tcdsca	i.dsca)
{
	|* One record required per country

	select	tccom000.ccty
	from	tccom000, tcmcs010
	where	tccom000.ccty refers to tcmcs010
	and	tcmcs010.meec =  tcyesno.yes
	group by tccom000.ccty
	selectdo
		var.tcmcs036.cvat =  i.cvat
		var.tcmcs036.dsca =  i.dsca
		dump 4
	endselect
}


function dump.non.eu.related.records.in.tcmcs036(
		domain	tccvat	i.cvat,
		domain	tcdsca	i.dsca)
{
	|* One record required per country

	select	tccom000.ccty
	from	tccom000
	group by tccom000.ccty
	selectdo
		var.tcmcs036.cvat =  i.cvat
		var.tcmcs036.dsca =  i.dsca
		dump 4
	endselect
}

function domain tcbool generate.vat.code(	domain	tccvat	i.cvat)
{
	select	tcmcs037.cvat
	from	tcmcs037
	where	tcmcs037._index1 = {:i.cvat}
	as set with 1 rows
	selectdo
		return(false)
		|* Tax code is already available in tcmcs037 --> do not generate
	selectempty
		return(true)
		|* Tax code is not yet available in tcmcs037 --> generate
	endselect

	return(false)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:isa
:TFLNO:001
:FUNC:START
function convert.sales.and.sales.price.unit()
{
	var.tdisa001.cuqs =  tiitm001.cuqs
	var.tdisa001.cups =  tiitm001.cups

	on case tiitm001.itmt
	case tppdm.itmt.equipment:
		eq.item =  left.align(tiitm001.item, 16)
		eq.item =  right.align(eq.item, 10)
		select 	 tppdm025.cuti
		from 	 tppdm025
		where 	 tppdm025._index1 =  {:eq.item}
		as set with 1 rows
		selectdo
			var.tdisa001.cuqs =  tppdm025.cuti
			var.tdisa001.cups =  tppdm025.cuti
		endselect
		break
	case tppdm.itmt.subcontracting:
		sub.item =  left.align(tiitm001.item, 16)
		sub.item =  right.align(sub.item, 8)
		select 	 tppdm035.cuni, tppdm035.dsbc, tppdm035.cuti
		from 	 tppdm035
		where 	 tppdm035._index1 =  {:sub.item}
		as set with 1 rows
		selectdo
			if tppdm035.dsbc =  tppdm.dsbc.quantity then
				var.tdisa001.cups =  tppdm035.cuni
				var.tdisa001.cuqs =  tppdm035.cuni
			else
				if not isspace(tppdm035.cuti) then
					var.tdisa001.cups =  tppdm035.cuti
					var.tdisa001.cuqs =  tppdm035.cuti
				endif
			endif
		endselect
		break
	default:
		break
	endcase
}

function domain tcqrd2 get.direct.delivery.from.quantity()
{
	if tiitm001.kitm = tckitm.cost or
	   tiitm001.kitm = tckitm.service or
	   tiitm001.kitm = tckitm.subcontracting then
		return(9999999.99)
	endif
	return(tiitm001.ddfq)
}

							|#213035.sn
function release.to.warehousing.allowed()
{
	if tiitm001.kitm = tckitm.purchase or
	   tiitm001.kitm = tckitm.manufacture or
	   tiitm001.kitm = tckitm.generic then
		|* For these item type release to warehousing is allowed and
		|* can also the overdelivery fields be activated
		var.tdisa001.retw = target(tcyesno.yes)
		var.tdisa001.alod = target(tcyesno.yes)
		var.tdisa001.tltp = target(tcisa.tltp.quantity)
		var.tdisa001.actn = target(tchstp.no)
	else	
		|* Release to wareghousing not allowed thus also 
		|* No overdelivery possible.
		var.tdisa001.retw = target(tcyesno.no)
		var.tdisa001.alod = target(tcyesno.no)
		var.tdisa001.tltp = target(tcisa.tltp.no)
		var.tdisa001.actn = target(tchstp.block)
	endif
}
							|#213035.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:isa
:TFLNO:010
:FUNC:START
function convert.tdisa010.gefo() 
{
	if tdsls000.agto.4 = tdsls.acpd.automatic or
	   tdsls000.agto.4 = tdsls.acpd.interactive then
		var.tdisa010.gefo = tcyesno.yes
	else
		var.tdisa010.gefo = tcyesno.no
	endif
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:pur
:TFLNO:000
:FUNC:START
function domain bcstc.ract get.availability.type()
{
	read.parameters.distribution(error.message)
	return(bcstd000.ract)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:pur
:TFLNO:100
:FUNC:START
function long convert.status()
{
	domain	tcqono	l.bind.qono

	if tdpur001.qspa = 1 then
		return(target(tdpur.rsta.booked))
	endif

	if tdpur001.qspa = 2 then
		return(target(tdpur.rsta.modified))
	endif

	if tdpur001.qspa = 3 then
		return(target(tdpur.rsta.printed))
	endif

	if tdpur001.qspa = 4 then
		select	tdpur011.qono:l.bind.qono
		from	tdpur011
		where	tdpur011._index1 = {:tdpur001.qono}
		and	tdpur011.qspa = 3
		as set with 1 rows
		selectdo
			return(target(tdpur.rsta.processed))
		endselect
	endif

	return(target(tdpur.rsta.returned))
}

function domain bccadr convert.delivery.address()
{
	static	domain	tccwar	s.pur001.cwar
	static	domain	bccadr	s.address	fixed

	if isspace(tdpur001.cwar) then
		s.address = ""
		s.pur001.cwar = ""
		return(s.address)
	endif

	if s.pur001.cwar <> tdpur001.cwar then
		s.pur001.cwar = tdpur001.cwar
		s.address = get.address.code(
 				"tcmcs003",
 	 		 	tdpur001.cwar,
 	 	 		g.error.mess)
	endif

	return(s.address)
}

function domain bccadr convert.by.from.address()
{
		domain	bctabl		l.table		fixed
		domain	bcstc.keys	l.key		fixed
	static	domain	bctabl		s.table
	static	domain	bcstc.keys	s.key
	static	domain	bccadr		s.address	fixed

	l.table = ""
	l.key = ""

	l.key(1;6) = tdpur001.suno

	if not isspace(tdpur001.ccor) then
		l.key(7;3) = tdpur001.ccor
		l.table = "tccom022"
	else
		read.supplier.postal.address()
		if not isspace(tccom020.ccor) then
			l.key(7;3) = tccom020.ccor
			l.table = "tccom022"
		else
			l.table = "tccom020"
		endif
	endif

	if s.table <> l.table or s.key <> l.key then
		s.table = l.table
		s.key = l.key
		s.address = get.address.code(
 					l.table,
 	 	 			l.key,
	 	 	 		g.error.mess)
	endif

	return(s.address)
}

function domain bcstc.bpid convert.business.partner()
{
	static	domain	tcsuno		s.pur001.suno
	static	domain	bcstc.bpid	s.business.partner	fixed

	if s.pur001.suno <> tdpur001.suno then
		s.pur001.suno = tdpur001.suno
 		s.business.partner = get.business.partner.code(
						tdpur001.suno,
		 	   	   	   	bcstc.orgn.suno,
		 	   	   	   	g.error.mess)
	endif

	return(s.business.partner)
}

function domain bcstc.ccnt convert.by.from.contact()
{
		long			ret
	static	domain	tcsuno		s.pur001.suno
	static	domain	bcstc.ccnt	s.contact	fixed

	if s.pur001.suno <> tdpur001.suno then
		s.pur001.suno = tdpur001.suno
		ret = get.cross.reference.for.contact.code(
					"tccom020a",
					tdpur001.suno,
					g.error.mess)
		s.contact = bcstc005.ccnt
	endif

	return(s.contact)
}

function domain bcstd.fent convert.purchase.office()
{
		long			ret
	static	domain	tcncmp		s.pur001.comp
	static	domain	bcstd.fent	s.office		fixed

	if s.pur001.comp <> tdpur001.comp then
		s.pur001.comp = tdpur001.comp
		ret = get.enterprise.unit(
 		 	 	 tdpur001.comp,
 	 		 	 get.compnr(),
 	 	 		 g.error.mess)
		s.office = bcstc018.pofd
	endif

	return(s.office)
}

function long convert.tax()
{
	static		long		s.tax.flag
	static	domain	tcsuno		s.pur001.suno

	if s.pur001.suno <> tdpur001.suno then
		s.pur001.suno = tdpur001.suno
		if get.tax.flag.via.bp(
				tdpur001.suno,
				bcstc.orgn.suno,
				g.error.mess) = tcyesno.yes then
			s.tax.flag = 1
		else
|			s.tax.flag = 0		|#149782.o
			s.tax.flag = 2		|#149782.n
		endif
	endif

	return(s.tax.flag)
}

function long convert.reminder.status()
{
	if tdpur001.rsts = empty then
		return(target(tcrsts.none))
	endif

	return(etol(tdpur001.rsts))
}

function read.supplier.postal.address()
{
	static	domain	tcsuno		s.supplier

	if s.supplier = tdpur001.suno then
		return
	else
		s.supplier = tdpur001.suno
	endif

	select	tccom020.ccor
	from	tccom020
	where	tccom020._index1 = {:tdpur001.suno}
	as set with 1 rows
	selectdo
	endselect
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:pur
:TFLNO:150
:FUNC:START
function domain bccadr convert.delivery.address()
{
	static	domain	tccwar	s.pur010.cwar
	static	domain	bccadr	s.address	fixed

	if isspace(tdpur010.cwar) then
		s.address = ""
		s.pur010.cwar = ""
		return(s.address)
	endif

	if s.pur010.cwar <> tdpur010.cwar then
		s.pur010.cwar = tdpur010.cwar
		s.address = get.address.code(
					"tcmcs003",
					tdpur010.cwar,
					g.error.mess)
	endif

	return(s.address)
}

function domain bcstd.fent convert.purchase.office()
{
		long			ret
	static	domain	tcncmp		s.pur010.comp
	static	domain	bcstd.fent	s.office	fixed

	if s.pur010.comp <> tdpur010.comp then
		s.pur010.comp = tdpur010.comp
		ret = get.enterprise.unit(
				tdpur010.comp,
				get.compnr(),
				g.error.mess)
		s.office = bcstc018.pofd
	endif

	return(s.office)
}

function domain bcstc.bpid convert.business.partner()
{
	static	domain	tcsuno		s.pur010.suno
	static	domain	bcstc.bpid	s.business.partner	fixed

	if s.pur010.suno <> tdpur010.suno then
		s.pur010.suno = tdpur010.suno
		s.business.partner = get.business.partner.code(
						tdpur010.suno,
						bcstc.orgn.suno,
						g.error.mess)
	endif

	return(s.business.partner)
}

function domain bccadr convert.by.from.address()
{
		domain	bctabl		l.table		fixed
		domain	bcstc.keys	l.key		fixed
	static	domain	bctabl		s.table
	static	domain	bcstc.keys	s.key
	static	domain	bccadr		s.address	fixed

	l.table = ""
	l.key = ""

	l.key(1;6) = tdpur010.suno

	if not isspace(tdpur010.ccor) then
		l.key(7;3) = tdpur010.ccor
		l.table = "tccom022"
	else
		l.table = "tccom020"
	endif

	if s.table <> l.table or s.key <> l.key then
		s.table = l.table
		s.key = l.key
		s.address = get.address.code(
					l.table,
					l.key,
					g.error.mess)
	endif

	return(s.address)
}

function long convert.tax()
{
	static		long		s.tax.flag
	static	domain	tcsuno		s.pur010.suno

	if s.pur010.suno <> tdpur010.suno then
		s.pur010.suno = tdpur010.suno
		if get.tax.flag.via.bp(
				tdpur010.suno,
				bcstc.orgn.suno,
				g.error.mess) = tcyesno.yes then
			s.tax.flag = 1
		else
|			s.tax.flag = 0		|#149782.o
			s.tax.flag = 2		|#149782.n
		endif
	endif

	return(s.tax.flag)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:pur
:TFLNO:300
:FUNC:START
|******************************************************************************
|* Script: tdpur300 - functions
|* Author: Evert-Jan Bosch
|******************************************************************************

function domain bccadr get.bp.address()
{
|* Expl	 : Use the function to convert purchase order BP addresses.
|* Pre	 : tdpur300 present
|* Return: Converted BP address

		domain	bcstc.keys	l.key	fixed
	static	domain	tccono		s.contract
	static	domain	bccadr		s.address

	|* To prevent determining the addres multiple times for 1 order.
	if s.contract = tdpur300.cono then
		return(s.address)
	else
		s.contract = tdpur300.cono
		s.address = ""
	endif

	|* Initialization.
	l.key = ""

	|* 1. Get postal address from purchase contract header
	if not isspace(tdpur300.ccor) then
		l.key(1;6) = tdpur300.suno
		l.key(7;3) = tdpur300.ccor
		s.address = get.address.code(
			"tccom022",
			l.key,
			error.message)
		return(s.address)
	endif

	|* 2. Get specific address.
	select	tdpur303._index1
	from	tdpur303
	where	tdpur303._index1 = {	:tdpur300.cono,
					1}	|* Type: postal address
	as set with 1 rows
	selectdo
		l.key(1;6) = edit$(tdpur303.cono,"999999")
		l.key(7;2) = edit$(tdpur303.ckor,"99")
		s.address = get.address.code(
			"tdpur303",
			l.key,
			error.message)
		return(s.address)
	endselect

	|* 3. Get postal address from supplier.
	read.supplier()
	if not isspace(tccom020.ccor) then
		l.key(1;6) = tdpur300.suno
		l.key(7;3) = tccom020.ccor
		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",
		tdpur300.suno,
		error.message)

	return(s.address)
}

function domain bccadr get.delivery.address()
{
|* Expl	 : Use the function to convert purchase contract delivery address.
|* Pre	 : tdpur300 present
|* Return: Converted delivery address

		domain	bcstc.keys	l.key	fixed

		domain	tccwar		l.warehouse
	static	domain	tccono		s.contract
	static	domain	bccadr		s.address

	|* To prevent determining the addres multiple times for 1 order.
	if s.contract = tdpur300.cono then
		return(s.address)
	else
		s.contract = tdpur300.cono
		s.address = ""
	endif

	|* Initialization.
	l.key = ""

	if not isspace(tdpur300.cwar) then
		s.address = get.address.code(
			"tcmcs003",
			tdpur300.cwar,
			error.message)
		return(s.address)
	endif

	select	tdpur303._index1
	from	tdpur303
	where	tdpur303._index1 = {	:tdpur300.cono,
					0}	|* Type: delivery address
	as set with 1 rows
	selectdo
		l.key(1;6) = edit$(tdpur303.cono,"999999")
		l.key(7;2) = edit$(tdpur303.ckor,"99")
		s.address = get.address.code(
			"tdpur303",
			l.key,
			error.message)
		return(s.address)
	endselect

	if isspace(tdpur300.cwar) then
		l.warehouse = convert.warehouse()
		s.address = get.address.code(
				"tcmcs003",
				l.warehouse,
				error.message)
	endif

	return(s.address)
}

function domain bcstd.fent get.purchase.office()
{
	read.supplier()
	if tccom020.comp <> 0 then
		get.enterprise.unit(
			tccom020.comp,
			get.compnr(),
			error.message)
		return(bcstc018.pofd)
 	endif

	read.parameters.distribution(error.message)
	return(bcstd000.pofc)
}

function domain tccsts get.print.status()
{
	|* Target field is mandatory.
	|* Source field is not mandatory.
	if tdpur300.csts = empty then
		return(tccsts.not.printed)
	endif

	return(tdpur300.csts)
}

function read.supplier()
{
	static	domain	tcsuno		s.supplier

	if s.supplier = tdpur300.suno then
		return
	else
		s.supplier = tdpur300.suno
	endif

	select	tccom020.ccor,
		tccom020.comp
	from	tccom020
	where	tccom020._index1 = {	:tdpur300.suno}
	as set with 1 rows
	selectdo
	endselect
}

function domain tccwar convert.warehouse()
{
	domain	tccwar	l.warehouse	fixed

	if isspace(tdpur300.cwar) then
		select	tdpur301.cwar:l.warehouse
		from	tdpur301
		where	tdpur301._index1 = {:tdpur300.cono}
		as set with 1 rows
		selectdo
			return(l.warehouse)
		endselect
	endif

	return(tdpur300.cwar)
}

|* End of script **************************************************************
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:pur
:TFLNO:301
:FUNC:START
|******************************************************************************
|* Script: tdpur301 - functions
|* Author: Evert-Jan Bosch
|******************************************************************************

function void read.contract.header.data()			|#206214.sn
{
	static	domain	tccono		s.contract

	if s.contract = tdpur301.cono then
		return
	else
		s.contract = tdpur301.cono
	endif

	select	tdpur300.icap, tdpur300.sdat,
		tdpur300.edat, tdpur300.ccon,
		tdpur300.cdec, tdpur300.ccor,
		tdpur300.csts
	from	tdpur300
	where	tdpur300._index1 = {:tdpur301.cono}
	as set with 1 rows
	selectdo
	endselect
}								|#206214.en

function read.supplier.data()
{
	static	domain	tcsuno		s.supplier

	if s.supplier = tdpur301.suno then
		return
	else
		s.supplier = tdpur301.suno
	endif

	select	tccom020.ccor, tccom020.comp
	from	tccom020
	where	tccom020._index1 = {:tdpur301.suno}
	as set with 1 rows
	selectdo
	endselect
}

function domain bccadr get.bp.address()
{
		domain	bcstc.keys	l.key	fixed
	static	domain	tcorno		s.contract
	static	domain	bccadr		s.address

	|* To prevent determining the addres multiple times for 1 order.
	if s.contract = tdpur300.cono then
		return(s.address)
	else
		s.contract = tdpur300.cono
		s.address = ""
	endif

	|* Initialization.
	l.key = ""

	|* 1. Get postal address from purchase contract header
	if not isspace(tdpur300.ccor) then
		l.key(1;6) = tdpur300.suno
		l.key(7;3) = tdpur300.ccor
		s.address = get.address.code(
			"tccom022",
			l.key,
			error.message)
		return(s.address)
	endif

	|* 2. Get specific address.
	select	tdpur303._index1
	from	tdpur303
	where	tdpur303._index1 = {	:tdpur300.cono,
					1}	|* Type: postal address
	as set with 1 rows
	selectdo
		l.key(1;6) = edit$(tdpur303.cono,"999999")
		l.key(7;2) = edit$(tdpur303.ckor,"99")
		s.address = get.address.code(
			"tdpur303",
			l.key,
			error.message)
		return(s.address)
	endselect

	|* 3. Get postal address from supplier.
|	read.supplier()						|#206214.o
	if not isspace(tccom020.ccor) then
		l.key(1;6) = tdpur300.suno
		l.key(7;3) = tccom020.ccor
		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",
		tdpur300.suno,
		error.message)

	return(s.address)
}

function domain bccadr get.delivery.address()
{
|		domain	bcstc.keys	l.key	fixed		|#206214.so
|
|	|* Initialization.
|	l.key = ""						|#206214.eo

	static	domain	tccwar		s.warehouse		|#206214.sn
	static	domain	bccadr		s.del.address

	if s.warehouse = tdpur301.cwar then
		return(s.del.address)
	else
		s.warehouse = tdpur301.cwar
		s.del.address = ""
	endif

	if not isspace(tdpur301.cwar) then
		s.del.address = get.address.code(
					"tcmcs003",
					tdpur301.cwar,
					error.message)
	endif

	return(s.del.address)					|#206214.en

|	if not isspace(tdpur301.cwar) then			|#206214.so
|		return(get.address.code(
|			"tcmcs003",
|			tdpur301.cwar,
|			error.message))
|	endif
|
|	return("")						|#206214.eo
}

function domain bcstd.fent get.purchase.office()
{
	static	domain	tcsuno		s.supplier		|#206214.sn
	static	domain	bcstd.fent	s.pur.office
		domain	tcncmp		fin.company
			long		ret

	if s.supplier = tdpur301.suno then
		return(s.pur.office)
	else
		s.supplier= tdpur301.suno
		s.pur.office = ""
	endif

	fin.company = tccom020.comp
	if fin.company = 0 then
		s.pur.office = bcstd000.pofc
	else
		ret = get.enterprise.unit(
			fin.company,
			get.compnr(),
			error.message)
		s.pur.office = bcstc018.pofd
	endif
	
	return(s.pur.office)					|#206214.en

|	select	tccom020.comp					|#206214.so
| 	from 	tccom020
| 	where 	tccom020._index1 = {:tdpur301.suno}
|	and	tccom020.comp <> 0
| 	as set with 1 rows
| 	selectdo
|		e = get.enterprise.unit(
|			tccom020.comp,
|			get.compnr(),
|			error.message)
|		return(bcstc018.pofd)
| 	endselect
|
|	e = read.parameters.distribution(error.message)
|	return(bcstd000.pofc)					|#206214.eo
}

function long get.delivery.schedule()
{
	static	domain	tccono		s.contract		|#206214.sn
	static	domain	tcpono		s.position
	static		long		s.del.schedule

	if s.contract = tdpur301.cono and
	   s.position = tdpur301.pono then
		return(s.del.schedule)
	else
		s.contract = tdpur301.cono
		s.position = tdpur301.pono
		s.del.schedule = target(tcyesno.no)
	endif							|#206214.en

|	select	tdpur302.*					|#206214.o
	select	tdpur302.cono					|#206214.n
	from	tdpur302
	where	tdpur302._index1 = {	:tdpur301.cono,
					:tdpur301.pono}
	as set with 1 rows
	selectdo
		s.del.schedule = target(tcyesno.yes)		|#206214.n
|		return(target(tcyesno.yes))			|#206214.o
	endselect

|	return(target(tcyesno.no))				|#206214.o
	return(s.del.schedule)					|#206214.n
}

function domain tcieva get.evaluation()
{
	|* Target field is mandatory.
	|* Source field is not mandatory.
	if tdpur301.ieva = empty then
		return(tcieva.no)
	endif

	return(tdpur301.ieva)
}

function domain tccsts get.print.status()
{
	|* Target field is mandatory.
	|* Source field is not mandatory.
	if tdpur300.csts = empty then
		return(tccsts.not.printed)
	endif

	return(tdpur300.csts)
}

function domain tcqrd1 convert.quantity.from.order.unit.to.inventory.unit(
		domain	tcqrd1		i.quantity.in.order.unit)
{
	read.item.data(get.item.code.for.contract())
	return(itccom0007.qty.domain.round(
		i.quantity.in.order.unit * tdpur301.cvqp,
		"tcqrd1",
		tiitm001.cuni))
}

function domain tcqiv2 get.economic.order.quantity()
{
	read.item.data(get.item.code.for.contract())
	if not double.cmp(tiitm001.ecoq, 0.0, 0.0001) then
		|* Econimic order quantity is a mandatory field.
		return(1.0)
	endif

	return(tiitm001.ecoq)
}

function domain tcqiv2 get.fixed.order.quantity()
{
	read.item.data(get.item.code.for.contract())
	if not double.cmp(tiitm001.fioq, 0.0, 0.0001) then
		|* Fixed order quantity is a mandatory field.
		return(1.0)
	endif

	return(tiitm001.fioq)
}

function domain tcqiv2 get.maximum.order.quantity()
{
	read.item.data(get.item.code.for.contract())
	return(tiitm001.maoq)
}

function domain tcqiv2 get.minimum.order.quantity()
{
	read.item.data(get.item.code.for.contract())
	return(tiitm001.mioq)
}

function domain tcqrd3 get.order.quantity.multiple.of()
{
	read.item.data(get.item.code.for.contract())
	if not double.cmp(tiitm001.oqmf, 0.0, 0.0001) then
		|* Order Quantity Increment is a mandatory field.
		return(1.0)
	endif

	return(tiitm001.oqmf)
}

function domain tcitem get.item.code.for.contract()
{
	if isspace(tdpur301.item) then
		return(get.item.code.via.price.group())
	endif

	return(tdpur301.item)
}

function domain tcitem get.item.code.via.price.group()
{
	static	domain	tccprg		s.price.group
	static	domain	tcitem		s.item

	if s.price.group = tdpur301.cpgp then
		return(s.item)
	endif

	select	tiitm001.item:s.item
	from	tiitm001
	where	tiitm001.cpgp = :tdpur301.cpgp
	order by tiitm001._index1
	as set with 1 rows
	selectdo
	endselect

	s.price.group = tdpur301.cpgp
	return(s.item)
}

function read.item.data(
		domain	tcitem		i.item)
{
	static	domain	tcitem		s.item

	if i.item = s.item then
		return
	else
		s.item = i.item
	endif

	select	tiitm001.oqmf,
		tiitm001.mioq,
		tiitm001.maoq,
		tiitm001.fioq,
		tiitm001.ecoq,
		tiitm001.cuni
	from	tiitm001
	where	tiitm001._index1 = {	:i.item}
	as set with 1 rows
	selectdo
	endselect
}

function domain tccvat get.tax()
{
		domain	tcccty		new.country
		domain	tccvat		new.tax

	e = get.tax.by.country.code(
		tdpur301.ccty,
		tdpur301.cvat,
		new.country,
		new.tax,
		error.message)

	return(new.tax)
}

function domain tcccty get.country()
{
		domain	tcccty		new.country
		domain	tccvat		new.tax

	e = get.tax.by.country.code(
		tdpur301.ccty,
		tdpur301.cvat,
		new.country,
		new.tax,
		error.message)

	return(new.country)
}

|function domain tcqrd1 get.advised.quantity()	|#203849.sn	|#206214.so
|{
|		domain	tcqrd1		advised.quantity
|
|	advised.quantity = 0
|	read.item.data(get.item.code.for.contract())
|
|	select	tdpur041.oqua, tdpur041.cvqp
|	from	tdpur045, tdpur041,
|		tdpur040, tcmcs042
|	where	tdpur045._index8 = {:tdpur301.suno}
|	and	tdpur045.cono = :tdpur301.cono
|	and	tdpur045.posi = :tdpur301.pono
|	and	tdpur045.date = 0
|	and	tdpur045.cmbe refers to tdpur041
|	and	tdpur045.orno refers to tdpur040
|	and	tdpur040.cotp refers to tcmcs042
|	and	tcmcs042.coun = tcyesno.yes
|	selectdo
|		advised.quantity = advised.quantity + 
|				   itccom0007.qty.domain.round(
|					tdpur041.oqua * tdpur041.cvqp,
|					"tcqrd1",
|					tiitm001.cuni)
|	endselect
|
|	return(advised.quantity)
|}						|#203849.en	|#206214.eo

function void get.advised.and.called.quantity(			|#206214.sn
	ref	domain	tcqrd1		o.advised.quantity,
	ref	domain	tcqrd1		o.called.quantity)
{
	static	domain	tccono		s.contract
	static	domain	tcpono		s.position
	static	domain	tcqrd1		s.advised.quantity
	static	domain	tcqrd1		s.called.quantity

	if s.contract = tdpur301.cono and
	   s.position = tdpur301.pono then
		o.advised.quantity = s.advised.quantity
		o.called.quantity = s.called.quantity
		return
	else
		s.contract = tdpur301.cono
		s.position = tdpur301.pono
		s.advised.quantity = 0
		s.called.quantity = 0
	endif

	o.advised.quantity = 0
	o.called.quantity = 0
	read.item.data(get.item.code.for.contract())

	select	tdpur041.oqua, tdpur041.cvqp,
		tdpur040.ccur, tdpur045._index8
	from	tdpur045, tdpur041,
		tdpur040, tcmcs042
	where	tdpur045._index8 = {:tdpur301.suno}
	and	tdpur045.cono = :tdpur301.cono
	and	tdpur045.posi = :tdpur301.pono
	and	tdpur045.date = 0
	and	tdpur045.cmbe refers to tdpur041
	and	tdpur045.orno refers to tdpur040
	and	tdpur040.cotp refers to tcmcs042
	and	tcmcs042.coun = tcyesno.yes
	order by tdpur045._index8
	selectdo
		o.advised.quantity = o.advised.quantity + 
					itccom0007.qty.domain.round(
						tdpur041.oqua * tdpur041.cvqp,
						"tcqrd1",
						tiitm001.cuni)
	endselect

	o.called.quantity = convert.quantity.from.order.unit.to.inventory.unit(
				tdpur301.cqan)
	o.called.quantity = o.called.quantity - o.advised.quantity

	s.called.quantity = o.called.quantity
	s.advised.quantity = o.advised.quantity
}								|#206214.en

|* End of script **************************************************************
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:pur
:TFLNO:350
:FUNC:START
function domain tcicap get.contract.status()
{
	|* Target field is mandatory.
	|* Source field is not mandatory.
	if tdpur305.icap = empty then
		return(tcicap.free)
	endif
	
	return(tdpur305.icap)
}

function domain tcicyp get.contract.type()
{
	|* Target field is mandatory.
	|* Source field is not mandatory.
	if tdpur305.icyp = empty then
		return(tcicyp.year)
	endif
	
	return(tdpur305.icyp)
}

function domain tcicpr get.contract.acknowledgement()
{
	|* Target field is mandatory.
	|* Source field is not mandatory.
	if tdpur305.icpr = empty then
		return(tcicpr.print)
	endif
	
	return(tdpur305.icpr)
}

function domain tcidtr get.transaction.type()
{
	|* Target field is mandatory.
	|* Source field is not mandatory.
	if tdpur305.idtr = empty then
		return(tcidtr.add)
	endif
	
	return(tdpur305.idtr)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:pur
:TFLNO:401
:FUNC:START
|******************************************************************************
|* Script: tdpur401, tdpur402 and tdpur406 - functions
|* Author: Hans Mulder
|******************************************************************************

function initialize.prev.variables()
{
	static	domain	tcorno		s.order.number

	if s.order.number = tdpur045.orno then
		return
	else
		s.order.number = tdpur045.orno
	endif

	g.pot.bo.qty = 0.0
	g.prev.bqua = 0.0
	g.prev.amld = 0.0
	g.prev.amod = 0.0
}

|**** General functions *******************************************************

function long get.number.of.receipt.lines()
{
	static		long		s.no.of.receipt.lines
	static	domain	tcorno		s.order.number
	static	domain	tcpono		s.position.number
		domain	tcorno		l.orno
		domain	tcpono		l.pono
		domain	tcpmsk		l.pmsk

	if s.order.number = tdpur045.orno and
	   s.position.number = tdpur045.pono then
		return(s.no.of.receipt.lines)
	else
		s.order.number = tdpur045.orno
		s.position.number = tdpur045.pono
		s.no.of.receipt.lines = 0
	endif

	g.extra.bo.line.must.be.inserted = false

	select	tdpur045.srnb:s.no.of.receipt.lines,
		tdpur045.orno:l.orno,
		tdpur045.pono:l.pono,
		tdpur045.pmsk:l.pmsk
	from	tdpur045
	where	tdpur045._index1 = {	:tdpur045.orno,
					:tdpur045.pono}
	order by tdpur045._index1 desc
	as set with 1 rows
	selectdo
	endselect

	g.last.sequence.number = s.no.of.receipt.lines

	if insert.extra.backorder.line.for.direct.delivery(
			l.pmsk) then
		g.extra.bo.line.must.be.inserted = true
		s.no.of.receipt.lines = s.no.of.receipt.lines + 1
	endif

	return(s.no.of.receipt.lines)
}

function domain tcbool linked.order.line.data.present()
{
			long		l.ret

	if tdpur041.sorn <> 0 or
	   tdpur041.srvo <> 0 or
	   tdpur045.pdno <> 0 then
		return(true)
	endif

	if tccom999.trtr = tcyesno.yes then
		l.ret = convert.transport.order.number(
				tdpur045.torn,
				tdpur045.tpon,
				g.error.message)
		if not isspace(bcsfm001.norn) then
			return(true)
		endif
	endif

	return(false)
}

function domain tcbool insert.extra.backorder.line.for.direct.delivery(
		domain	tcpmsk		l.procedure.mask)
{
		domain	tcstno		l.stepno

	if tdpur041.sorn = 0 or
	   not isspace(tdpur041.cwar) then
		|* Only for Direct Delivery.
		return(false)
	endif

	if tdpur041.bqua = 0 then
		|* If the potential backorder quantity is zero, no
		|* extra backorder line must be inserted.
		return(false)
	endif

	l.stepno = read.step.number("tdpur4223m000")
	if l.procedure.mask(l.stepno;1) = "y" then
		|* In case the purchase order line is not yet
		|* processed, no related sales delivery and
		|* backorder line do not exist yet, so it is not
		|* necessary to insert an extra backorder line for
		|* the purchase order.
		return(false)
	endif

	return(true)
}

function get.total.line.values()
{
	static	domain	tcorno		s.order.number
	static	domain	tcpono		s.position.number

	if s.order.number = tdpur045.orno and
	   s.position.number = tdpur045.pono then
		return
	else
		s.order.number = tdpur045.orno
		s.position.number = tdpur045.pono
		g.total.damt = 0.0
		g.total.amld = 0.0
		g.total.amod = 0.0
		g.total.qips = 0.0
		g.total.qibo = 0.0
		g.total.ddte = 0
	endif

	select	sum(tdpur045.amnt):g.total.damt,
		sum(tdpur045.ldam):g.total.amld,
		sum(tdpur045.odam):g.total.amod,
		sum(tdpur045.diqu):g.total.qips,
		sum(tdpur045.bqua):g.total.qibo,
		max(tdpur045.date):g.total.ddte
	from	tdpur045
	where	tdpur045._index1 = {	:tdpur045.orno,
					:tdpur045.pono}
	and	tdpur045.srnb <> 0
	selectdo
	endselect
}

|**** Read data functions *****************************************************

function read.purchase.order.and.related.data()
{
	static	domain	tcorno		s.order.number

	if s.order.number = tdpur045.orno then
		return
	else
		s.order.number = tdpur045.orno
	endif

	select	tdpur040.corg, tdpur040.cotp,
		tdpur040.suno, tdpur040.ccor,
		tdpur040.cfrw, tdpur040.orno,
		tdpur040.ccur, tdpur040.odis,
		tdpur040.pspr, tdpur040.cplp,
		tccom020.ccor, tccom020.comp,
		tccom020.cuno, tccom020.cplp,
		tcmcs042.reto, tcmcs042.coun,
		tcmcs042.drct, tcmcs042.stno,
		tcmcs042.casi, tcmcs002.crnd
	from	tdpur040, tccom020,
		tcmcs042, tcmcs002
	where	tdpur040.orno = :tdpur045.orno
	and	tccom020.suno = tdpur040.suno
	and	tcmcs042.cotp = tdpur040.cotp
	and	tcmcs002.ccur = tdpur040.ccur
	as set with 1 rows
	selectdo
	endselect

	load.new.session.code.position.array()
}

function load.new.session.code.position.array()
{
	static	domain	tccotp		s.order.type
			long		l.ret

	if s.order.type = tdpur040.cotp then
		return
	else
		s.order.type = tdpur040.cotp
		l.ret = set.mem(g.new.session.code.position.array, "")
	endif

	|* Array with new order type session codes now loaded.
	select	bcstd004.acno, bcstd004.acti
	from	bcstd004
	where	bcstd004._index2 = {:tdpur040.cotp}
	selectdo
		g.new.session.code.position.array(1,bcstd004.acno) =
				bcstd004.acti
	endselect
}

function read.item.data()
{
		domain	tcpuma		l.project.item.type
	static	domain	tccprj		s.project
	static	domain	tcittb		s.opol
	static	domain	tcitem		s.item

	if s.project = tdpur041.cprj and
	   s.opol = tdpur041.opol and
	   s.item = tdpur041.item then
		return
	else
		s.project = tdpur041.cprj
		s.opol = tdpur041.opol
		s.item = tdpur041.item
	endif

	|**************************************************************
	|* For standard items the data is retrieved from tiitm001.
	|* For customized items the data is retrieved from tipcs021.
	|**************************************************************

	if tdpur041.opol = tcittb.standard then
		select	tiitm001.kitm:g.item.type,
			tiitm001.cuni:g.inventory.unit,
			tiitm001.cpgp:g.price.group
		from	tiitm001
		where	tiitm001._index1 = {	:tdpur041.item}
		as set with 1 rows
		selectdo
		endselect
	else
		select	tipcs021.kitm:l.project.item.type,
			tipcs021.cuni:g.inventory.unit,
			tiitm001.cpgp:g.price.group
		from	tipcs021, tiitm001
		where	tipcs021._index1 = {	:tdpur041.cprj,
						:tdpur041.item}
		and	tipcs021.dfit refers to tiitm001 unref clear
		as set with 1 rows
		selectdo
			if l.project.item.type = tcpuma.purchase then
				g.item.type = tckitm.purchase
			endif
			if l.project.item.type = tcpuma.manufacture then
				g.item.type = tckitm.manufacture
			endif
			if isspace(g.price.group) then
				g.price.group = bcstd000.cprg
			endif
		endselect
	endif
}

|**** Order fields ************************************************************

function domain bcorno get.purchase.order.number()
{
	static	domain	tcorno		s.old.orno
	static	domain	bcorno		s.new.orno fixed

	if tdpur045.orno = s.old.orno then
		return(s.new.orno)
	else
		s.old.orno = tdpur045.orno
		s.new.orno = ""
	endif

	s.new.orno = convert.number(bcckon.act.pur, tdpur045.orno)

	return(s.new.orno)
}

function domain tcpono get.order.line.sequence()
{
	if g.total.line then
		return(0)
	endif

	if g.extra.dir.del.bo.line then
		return(tdpur045.srnb + 1)
	endif

	if tdpur045.srnb = 0 then
		return(1)
	else
		return(tdpur045.srnb)
	endif
}

function long get.origin()
{
	on case tdpur040.corg
	case tdpur.corg.pcs:
		return(target(tdpur.corg.mrp))
	case tdpur.corg.mps:
		return(target(tdpur.corg.mrp))
	default:
		return(etol(tdpur040.corg))
	endcase
}

function long get.order.line.type()
{
	if g.total.line then
		return(target(tdgen.oltp.total))
	endif

	if g.extra.dir.del.bo.line then
		return(target(tdgen.oltp.backorder))
	endif

	if tdpur045.srnb > 1 then
		return(target(tdgen.oltp.backorder))
	endif

	if g.no.of.receipt.lines > 1 then
		return(target(tdgen.oltp.detail))
	else
		return(target(tdgen.oltp.orderline))
	endif
}

function long get.parent.sequence()
{
	if g.total.line or
	   tdpur045.srnb = 0 then
		return(0)
	endif

	if g.extra.dir.del.bo.line then
		|* The last sequence is the parent of the extra
		|* inserted backorder line.
		return(tdpur045.srnb)
	endif

	return(tdpur045.srnb - 1)



























}

function long get.linked.sequence()
{
	if g.total.line or
	   g.extra.dir.del.bo.line or
	   tdpur045.date = 0 or
	   tdpur045.srnb = g.no.of.receipt.lines then
		return(0)
	endif

	return(tdpur045.srnb + 1)
}

|**** Linked Order Line Data fields *******************************************

function domain bcorno get.sales.order.number()
{
	static	domain	tcorno		s.old.sorn
	static	domain	bcorno		s.new.sorn fixed

	if tdpur041.sorn = s.old.sorn then
		return(s.new.sorn)
	else
		s.old.sorn = tdpur041.sorn
		s.new.sorn = ""
	endif

	if tdpur041.sorn <> 0 then
		s.new.sorn = convert.number(bcckon.act.sls, tdpur041.sorn)
	endif

	return(s.new.sorn)
}

function domain tcpono get.linked.sales.order.line.sequence()
{
	if tdpur041.sorn = 0 then
		return(0)
	endif

	if not isspace(get.warehouse()) then
		|* For cross docking orders the sales sequence number
		|* is not stored in the linked order line data.
		return(0)
	endif

	if g.no.of.receipt.lines = 1 then
		|* If only one receipt is done, then there is no
		|* total line present. In Sales the order line
		|* sequence is 0 in that case.
		return(0)	|* = tdpur045.srnb - 1
	endif

	if g.extra.dir.del.bo.line then
		return(tdpur045.srnb + 1)
	endif

	if tdpur045.date = 0 then
		|* In case the purchase order line is not yet
		|* received, the related sales deliveries and
		|* backorder lines do not exist yet, so 0 is
		|* returned.
		return(0)
	endif

	|* For direct delivery orders the purchase and the sales
	|* sequence are the same.
	return(tdpur045.srnb)
}

function domain bcorno get.production.order.number()
{
	static	domain	tcpdno		s.old.pdno
	static	domain	bcorno		s.new.pdno fixed

	if tdpur041.pdno = s.old.pdno then
		return(s.new.pdno)
	else
		s.old.pdno = tdpur041.pdno
		s.new.pdno = ""
	endif

	if tdpur041.pdno <> 0 then
		s.new.pdno = convert.number(bcckon.act.sfc, tdpur041.pdno)
	endif

	return(s.new.pdno)
}

function domain bcorno get.service.order.number()
{

	static	domain	tcorno		s.old.srvo
	static	domain	bcorno		s.new.srvo fixed

	if tdpur041.srvo = s.old.srvo then
		return(s.new.srvo)
	else
		s.old.srvo = tdpur041.srvo
		s.new.srvo = ""
	endif

	if tdpur041.srvo <> 0 then
		s.new.srvo = convert.number(bcckon.sma.order, tdpur041.srvo)
	endif

	return(s.new.srvo)
}

|**** Item fields *************************************************************

function domain bcstc.item get.item()
{
	static	domain	tccprj		s.project
	static	domain	tcittb		s.opol
	static	domain	tcitem		s.item
	static	domain	bcstc.item	s.segmented.item fixed

	if s.item = tdpur041.item and
	   s.project = tdpur041.cprj and
	   s.opol = tdpur041.opol then
		return(s.segmented.item)
	else
		s.item = tdpur041.item
		s.project = tdpur041.cprj
		s.opol = tdpur041.opol
		s.segmented.item = ""
	endif

	if tdpur041.opol = tcittb.customized then
		s.segmented.item = segment.item.code(
					tdpur041.item,
					get.project(tdpur045.cprj))
	else
		s.segmented.item = segment.item.code(
					tdpur041.item,
					"")
	endif

	return(s.segmented.item)
}

|**** Warehouse fields ********************************************************

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 g.item.type <> tckitm.purchase and
	   g.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)
}

function domain bccadr get.receipt.address()
{
		domain	bcstc.keys	l.key fixed
		domain	tcncmp		l.current.company
	static	domain	tcorno		s.order
	static	domain	tcpono		s.position
	static	domain	bccadr		s.address fixed

	|* To prevent determining the addres multiple times for 1
	|* order.
	if s.order = tdpur045.orno and
	   s.position = tdpur045.pono then
		return(s.address)
	else
		s.order = tdpur045.orno
		s.position = tdpur045.pono
		s.address = ""
	endif

	|* Initialization.
	l.key = ""

	|* 1. Get specific receipt address.
	select	tdpur042._index1
	from	tdpur042
	where	tdpur042._index1 = {	:tdpur045.orno,
					:tdpur045.pono,
					0}	|* Type: receipt address
	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,
				g.error.message)
		return(s.address)
	endselect

	|* 2. Get receipt address from warehouse.
	if not isspace(tdpur045.cwar) then
		|* For e.g. cost and service items the warehouse is
		|* used for the receipt address. So in that case the
		|* receipt address is taken from the warehouse, but
		|* the warehouse itself is emptied.
		s.address = get.address.code(
				"tcmcs003",
				tdpur045.cwar,
				g.error.message)
		return(s.address)
	endif

	|* 3. Get receipt address from company.
	l.current.company = get.compnr()			|#800-141909.sn
	l.key(1;3) = edit$(l.current.company, "999")
	s.address = get.address.code(
			"tccom000",
			l.key,

			g.error.message)			|#800-141909.en

	return(s.address)
}

function domain tcyesno get.inspection.flag()
{
	|* In Baan 6 inspection is not possible for receiving in
	|* components. In BaanIV it is.
	if tdpur041.btsp = tcbtsp.bookto.comp then
		return(tcyesno.no)
	endif

	return(tdpur041.qual)
}

function long get.inventory.handling()
{
	on case tdpur041.btsp
	case tcbtsp.bookto.main:
		return(target(tcinup.by.main.item))
	case tcbtsp.bookto.comp:
		return(target(tcinup.by.component))
	default:
		return(target(tcinup.by.main.item))
	endcase
}

function string get.release.date()
{
	if isspace(get.warehouse()) then
		return(utc(0, 0))
	endif

	return(utc(tdpur041.ddtb, 0))
}

|**** Cost Component fields ***************************************************

function domain bccost get.project.cost.component()
{
	static	domain	tppdm.ccco	s.old.proj.cost.comp
	static	domain	bccost		s.project.cost.comp fixed
			long		l.ret

	if s.old.proj.cost.comp = tdpur041.ccco then
		return(s.project.cost.comp)
	else
		s.old.proj.cost.comp = tdpur041.ccco
		s.project.cost.comp = ""
	endif

	if isspace(tdpur041.ccco) then
		return(s.project.cost.comp)
	endif

	l.ret = get.cost.components(
		bcorig.tppdm,
		"",
		tdpur041.ccco,
		g.error.message)
	s.project.cost.comp = bcstc006.cost

	return(s.project.cost.comp)
}

function domain bccost get.cost.price.component()
{
	static	domain	tccpcp		s.old.cost.pr.comp
	static	domain	bccost		s.cost.price.comp fixed
			long		l.ret

	if s.old.cost.pr.comp = tdpur045.cpcp then
		return(s.cost.price.comp)
	else
		s.old.cost.pr.comp = tdpur045.cpcp
		s.cost.price.comp = ""
	endif

	if isspace(tdpur045.cpcp) then
		return(s.cost.price.comp)
	endif

	l.ret = get.cost.components(
		bcorig.ticpr,

		tdpur045.cpcp,
		"",
		g.error.message)
	s.cost.price.comp = bcstc006.cost

	return(s.cost.price.comp)
}

|**** Receipt fields **********************************************************

function domain tcyesno get.final.receipt.flag()
{

	if g.total.line or
	   g.extra.dir.del.bo.line or
	   tdpur045.date = 0 then
		return(tcyesno.no)
	endif

	return(tcyesno.yes)
}

function long get.no.of.deliveries.done()
{
	if g.total.line or
	   g.extra.dir.del.bo.line or
	   tdpur045.date = 0 then
		return(0)
	endif

	return(1)
}

function string get.receipt.number()
{
			long		ret			|#215078.sn
		domain	tcmcs.s200m	error.message		|#215078.en
	static	domain	tcrcno		s.last.receipt.number	|#206207.n

	if g.total.line or
	   g.extra.dir.del.bo.line then
		return("")
	endif

	if tdpur045.date = 0 then
		return("")
	endif

	if tdpur045.reno = 0 then				|#206207.sn
		s.last.receipt.number = s.last.receipt.number + 1
		return(	shiftl$(strip$(bcswh000.conv)) &
			edit$(s.last.receipt.number, "999999"))
	endif							|#206207.en

	if tdpur041.oqua >= 0 then
		return(convert.number(	bcckon.receipt.number,
					tdpur045.reno))
	else
|		|***********************************************|#215078.so
|		|* For Purchase Return Orders (PRO), the receipt
|		|* number field is filled with the shipment ID. In
|		|* BaanIV a receipt number is present for return
|		|* orders. (The same construction is used for WH and
|		|* tfacp245. The purchase return shipment series is
|		|* used for the new shipment ID.
|		|******************************************************
|		return(	shiftl$(strip$(bcswh000.prsh)) &
|			edit$(tdpur045.reno, "999999"))		|#215078.eo
		ret = get.shipment.number.and.line(		|#215078.sn
			tckoor.act.pur,
			tdpur045.orno,
			tdpur045.pono,
			get.order.line.sequence(),
			0,
			error.message)	|* ref
		return(bcswh008.shpm)				|#215078.en
	endif
}

function domain tcpono get.receipt.line()			|#149975.sn
{
			long		ret			|#215078.sn
		domain	tcmcs.s200m	error.message		|#215078.en
		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
	static		long		s.array.length

	if g.total.line or
	   g.extra.dir.del.bo.line then
		return(0)
	endif

	if tdpur045.date = 0 then
		return(0)
	endif

	if tdpur045.reno = 0 then
|		return(0)					|#206207.o
		|* Default step size is 10 (same as in WH).
		return(10)					|#206207.n
	endif

	if tdpur041.oqua < 0 then				|#215078.sn
		ret = get.shipment.number.and.line(
			tckoor.act.pur,
			tdpur045.orno,
			tdpur045.pono,
			get.order.line.sequence(),
			0,
			error.message)	|* ref
		return(bcswh008.shln)
	endif							|#215078.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 WH).
		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

function string get.packing.slip.number()
{
	if g.total.line then
		return("")
	endif

	if g.extra.dir.del.bo.line then
		return("")
	endif

	if tdpur045.date <> 0 then
		return(tdpur045.dino)
	else
		return("")
	endif
}

function string get.actual.receipt.date()
{
	if g.total.line then
		return(utc(g.total.ddte, 0))
	endif

	if g.extra.dir.del.bo.line then
		return(utc(0, 0))
	endif

	return(utc(tdpur045.date, 0))
}

|**** Quantity fields *********************************************************

function domain tcqrd1 get.ordered.quantity()
{
	if g.extra.dir.del.bo.line then
		return(tdpur041.bqua)
	endif

	if g.total.line or
	   tdpur045.srnb < 2 then
		return(tdpur041.oqua)
	endif

	|* When the backorder line is already created, but not yet
	|* received, the ordered quantity is equal to the 'to be
	|* confirmed backorder quantity': tdpur041.bqua.
	if tdpur045.date = 0 then
		return(tdpur041.bqua)
	endif


	|* The backorder quantity of the previous receipt line is the
	|* ordered quantity of the current line.
	return(g.prev.bqua)
}

function domain tcqrd1 convert.quantity.from.order.unit.to.inventory.unit(
		domain	tcqrd1		i.quantity.in.order.unit)
{
	return(itccom0007.qty.domain.round(
			i.quantity.in.order.unit * tdpur041.cvqp,
			"tcqrd1",
			g.inventory.unit))
}

function domain tcqrd1 get.received.quantity()
{
	if tdpur041.oqua = 0 then
		return(0.0)
	endif

	if g.total.line then
		return(convert.quantity.from.order.unit.to.inventory.unit(
				tdpur041.dqua))
	endif

	if g.extra.dir.del.bo.line then
		return(0.0)
	endif

	return(convert.quantity.from.order.unit.to.inventory.unit(
			tdpur045.dqua))
}

function domain tcqrd1 get.packing.slip.quantity()


{
	if tdpur041.oqua = 0 then
		return(0.0)
	endif

	if g.total.line then
		return(convert.quantity.from.order.unit.to.inventory.unit(
				g.total.qips))
	endif

	if g.extra.dir.del.bo.line then
		return(0.0)
	endif

	return(convert.quantity.from.order.unit.to.inventory.unit(
			tdpur045.diqu))
}

function domain tcqrd1 get.approved.quantity()
{
		domain	tcqrd1		l.quantity

	|* In case of return orders the approved quantity is filled
	|* in tdpur041/045.quad.

	if tdpur041.oqua = 0 then
		return(0.0)
	endif

	if g.extra.dir.del.bo.line then
		return(0.0)
	endif

	if tdpur041.oqua < 0 then
		|* Return order
		if g.total.line then
			l.quantity = tdpur041.quad
		else
			l.quantity = tdpur045.quad
		endif
	else
		if g.total.line then
			l.quantity = tdpur041.quap
		else
			l.quantity = tdpur045.quap
		endif
 	endif

	return(convert.quantity.from.order.unit.to.inventory.unit(
			l.quantity))
}

function domain tcqrd1 get.rejected.quantity()
{
	if tdpur041.oqua = 0 then
		return(0.0)
	endif

	if g.extra.dir.del.bo.line then
		return(0.0)
	endif

	if tdpur041.oqua < 0 then
		|* Return order
		return(0.0)
	endif


	if g.total.line then
		return(convert.quantity.from.order.unit.to.inventory.unit(
				tdpur041.quad))
	endif

	return(convert.quantity.from.order.unit.to.inventory.unit(
			tdpur045.quad))
}

function domain tccdis get.return.reason.for.rejection()
{
			long		l.ret

	if g.total.line or
	   g.extra.dir.del.bo.line then
		return("")
	endif

	if get.rejected.quantity() = 0 then
		return("")
	endif

	|* Make bcstc017 record current.
	l.ret = get.reason.for.rejection(
			bcreje.tcmcs005,
			tdpur045.cdis,
			g.error.message)	|* ref

	if not isspace(bcstc017.newr) then
		return(bcstc017.newr)
	endif

	|* Default Reason for Rejection.
	return(bcstd000.dror)
}

function domain tcqrd1 get.potential.backorder.quantity(
		domain	tcqrd1		i.ordered.quantity)
{
	if g.extra.dir.del.bo.line then

		return(tdpur041.bqua)
	endif

	if g.total.line or
	   tdpur045.srnb < 2 then
		return(0.0)
	endif

	if tdpur041.oqua < 0 then
		return(i.ordered.quantity - tdpur045.dqua)
	else
		return(i.ordered.quantity - tdpur045.dqua + tdpur045.quad)

	endif
}

function domain tcqrd1 get.backorder.quantity()
{
	if tdpur041.oqua = 0 then
		return(0.0)
	endif

	if g.extra.dir.del.bo.line then
		return(0.0)
	endif

	if g.total.line then
		return(convert.quantity.from.order.unit.to.inventory.unit(
				g.total.qibo))
	endif

	return(convert.quantity.from.order.unit.to.inventory.unit(
			tdpur045.bqua))
}

function domain tcqrd1 get.to.be.confirmed.backorder.quantity()
{
	|* The to be confirmed backorder quantity is stored in order
	|* unit in Baan IV, so the backorder unit after conversion is
	|* the order unit too.

	if to.be.confirmed.bo.qty.is.applicable() then
		|* Value of tdpur045.bqua is the backorder quantity
		|* of the (backorder) line, tdpur041.bqua is the
		|* total open backorder quantity for the whole order
		|* line.
		return(tdpur041.bqua)
	endif

	return(0.0)
}

function domain tccuni get.backorder.unit()
{
	|* When the to be confirmed backorder quantity is not
	|* applicable or is zero (0.0 returned), then the backorder
	|* unit is empty.

	if tdpur041.bqua <> 0 and
	   to.be.confirmed.bo.qty.is.applicable() then
		|* The to be confirmed backorder quantity is stored
		|* order unit in Baan IV, so the backorder unit after
		|* conversion is the order unit too.
		return(tdpur041.cuqp)
	endif

	return("")
}

function domain tcconv get.backorder.unit.conversion.factor()
{
	|* When the backorder unit is empty, then the backorder unit
	|* conversion factor is zero.

	if tdpur041.bqua <> 0 and
	   to.be.confirmed.bo.qty.is.applicable() then
		|* The to be confirmed backorder quantity is stored
		|* order unit in Baan IV, so the backorder unit after
		|* conversion is the order unit too.
		return(tdpur041.cvqp)
	endif


	return(0.0)
}

function domain tcbool to.be.confirmed.bo.qty.is.applicable()
{
	if tdpur041.oqua = 0 then
		return(false)
	endif

	if g.extra.dir.del.bo.line then
		return(false)
	endif

	if g.total.line or
	   tdpur045.srnb = 0 then
		return(false)
	endif

	if tdpur045.srnb <> g.no.of.receipt.lines or
	   tdpur045.date = 0 then
		return(false)
	endif

	return(true)
}

|**** Amount fields ***********************************************************

function domain tcamnt get.order.amount()
{
		domain	tcamnt		l.order.amount
		domain	tcamnt		l.dummy.amount

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

	return(l.order.amount)
}

function domain tcamnt get.order.line.discount.amount()
{
		domain	tcamnt		l.disc.amount
		domain	tcamnt		l.dummy.amount

	get.amounts(	l.dummy.amount,	|* ref
			l.disc.amount,	|* ref
			l.dummy.amount)	|* ref
	return(l.disc.amount)
}

function domain tcamnt get.order.discount.amount()
{
		domain	tcamnt		l.disc.amount
		domain	tcamnt		l.dummy.amount

	get.amounts(	l.dummy.amount,	|* ref
			l.dummy.amount,	|* ref
			l.disc.amount)	|* ref
	return(l.disc.amount)
}

function get.amounts(
		ref	domain	tcamnt		o.order.amount,
		ref	domain	tcamnt		o.line.disc.amount,
		ref	domain	tcamnt		o.order.disc.amount)
{
		domain	tcamnt		l.dummy.amount
	static	domain	tcorno		s.orno
	static	domain	tcpono		s.pono
	static	domain	tcsrnb		s.srnb
	static	domain	tcamnt		s.order.amount
	static	domain	tcamnt		s.line.disc.amount
	static	domain	tcamnt		s.order.disc.amount

	if s.orno = tdpur045.orno and
	   s.pono = tdpur045.pono and
	   s.srnb = tdpur045.srnb then
		o.order.amount = s.order.amount
		o.line.disc.amount = s.line.disc.amount
		o.order.disc.amount = s.order.disc.amount
		return
	else
		s.orno = tdpur045.orno
		s.pono = tdpur045.pono
		s.srnb = tdpur045.srnb
		o.order.amount = 0.0
		o.line.disc.amount = 0.0
		o.order.disc.amount = 0.0
	endif

	if g.total.line then
		|* On the total line the total amounts are stored.
		o.order.amount = tdpur041.amta
		o.line.disc.amount = g.total.amld
		o.order.disc.amount = g.total.amod
		s.order.amount = o.order.amount
		s.line.disc.amount = o.line.disc.amount
		s.order.disc.amount = o.order.disc.amount
		return
	endif

	if tdpur045.srnb = 0 then
		|* When no receipt is done for a line that is not a
		|* backorder line, the amounts are taken from 041 and
		|* 045.
		o.order.amount = tdpur041.amta
		o.line.disc.amount = tdpur045.ldam

		o.order.disc.amount = tdpur045.odam
		s.order.amount = o.order.amount
		s.line.disc.amount = o.line.disc.amount
		s.order.disc.amount = o.order.disc.amount
		return
	endif

	if tdpur045.srnb = 1 then
		|* When no receipt is done for a line that is not a
		|* backorder line, the amounts are taken from 041 and
		|* 045. When a receipt is done, the discount amounts
		|* are taken from the line with srnb = 0.
		o.order.amount = tdpur041.amta
		if tdpur045.date <> 0 then
			o.line.disc.amount = tdpur045.ldam
			o.order.disc.amount = tdpur045.odam
		else
			o.line.disc.amount = g.prev.amld
			o.order.disc.amount = g.prev.amod
		endif
		s.order.amount = o.order.amount
		s.line.disc.amount = o.line.disc.amount

		s.order.disc.amount = o.order.disc.amount
		return
	endif

	|* Sequence srnb is greater than 1, so the next part is only
	|* for backorder lines.

	if tdpur045.pric = 0 then
		|* When the price is zero, the amounts are zero too.
		s.order.amount = o.order.amount
		s.line.disc.amount = o.line.disc.amount
		s.order.disc.amount = o.order.disc.amount
		return
	endif

	if tdpur045.date <> 0 then
		if g.prev.bqua <> 0 then
			|* The ordered quantity of the backorder line
			|* is equal to g.prev.bqua in this case. When
			|* the quantity is zero, the amounts are zero
			|* too.
			o.line.disc.amount = tdpur045.ldam
			o.order.disc.amount = tdpur045.odam
			tdpur.dll0022.calc.net.amount(
				tdpur041.cvqp,
				tdpur041.cvpp,
				g.prev.bqua,
				tdpur045.pric,

				tdpur040.ccur,
				tcmcs002.crnd,
				tdpur041.dmth,
				tdpur045.disc,
				tdpur045.damt,


				o.order.amount,	|* ref
				l.dummy.amount)	|* ref
		endif
		s.order.amount = o.order.amount
		s.line.disc.amount = o.line.disc.amount
		s.order.disc.amount = o.order.disc.amount
		return
	endif

	|* 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 order line (to be confirmed
	|* backorder quantity) and the price and discounts of the
	|* current line.
	|* This is also applicable for extra inserted backorder lines
	|* for direct delivery orders.
	tdpur.dll0022.calc.net.amount(
		tdpur041.cvqp,
		tdpur041.cvpp,
		tdpur041.bqua,
		tdpur045.pric,
		tdpur040.ccur,
		tcmcs002.crnd,
		tdpur041.dmth,
		tdpur045.disc,
		tdpur045.damt,
		o.order.amount,		|* ref
		o.line.disc.amount)	|* ref

	if g.item.type = tckitm.cost or
	   g.item.type = tckitm.service then
		|* For cost and service items the order discount
		|* amount (amod) is always zero.
		o.order.disc.amount = 0.0
	else
		o.order.disc.amount = o.order.amount -
					imcs0010.amnt.min.odis(
						o.order.amount,
						tdpur040.odis,
						tdpur040.ccur,

						tcmcs002.crnd)
	endif

	s.order.amount = o.order.amount
	s.line.disc.amount = o.line.disc.amount
	s.order.disc.amount = o.order.disc.amount
}

function domain tcamnt get.receipt.amount()
{
	if g.total.line then
		return(g.total.damt)
	endif

	if g.extra.dir.del.bo.line then
		return(0.0)
	endif

	if tdpur045.date <> 0 then
		return(tdpur045.amnt)
	endif

	return(0.0)
}

function domain tcdisc get.structure.discount()
{
	|* Field is not implemented yet in Baan 6. Not yet clear how
	|* the field must be filled.

	return(0.0)
}

|**** Invoice fields **********************************************************

function domain bcorno get.purchase.invoice.number()
{
	static	domain	tcinvn		s.old.invn
	static	domain	bcorno		s.new.invn fixed

	if g.total.line or
	   g.extra.dir.del.bo.line then
		return("")
	endif


	if tdpur045.invn = s.old.invn then
		return(s.new.invn)
	else
		s.old.invn = tdpur045.invn
		s.new.invn = ""
	endif

	if tdpur045.invn <> 0 then
		s.new.invn = convert.number(bcckon.invoice.pur, tdpur045.invn)
	endif

	return(s.new.invn)
}

function string get.invoice.date()
{
	if g.total.line or
	   g.extra.dir.del.bo.line then
		return(utc(0, 0))
	endif

	return(utc(tdpur045.invd, 0))
}

function domain tcqrd1 get.invoiced.quantity()
{
	if g.total.line or
	   g.extra.dir.del.bo.line then
		return(0.0)
	endif

	if tdpur045.stsd = tcstsd.all.approved then
		return(convert.quantity.from.order.unit.to.inventory.unit(
				tdpur045.iqan))
	endif

	if tdpur045.stsd = tcstsd.approved then
		return(get.posted.invoice.quantity.and.amount(true))
	endif

	return(0.0)
}

function domain tcamnt get.invoice.amount()
{
	if g.total.line or
	   g.extra.dir.del.bo.line then
		return(0.0)
	endif

	if tdpur045.stsd = tcstsd.all.approved then
		return(tdpur045.iamt)
	endif

	if tdpur045.stsd = tcstsd.approved then
		return(get.posted.invoice.quantity.and.amount(false))
	endif

	return(0.0)

}


function double get.posted.invoice.quantity.and.amount(
		domain	tcbool		i.read.quantity)
{
	static	domain	tcorno		s.orno
	static	domain	tcpono		s.pono
	static	domain	tcsrnb		s.srnb
	static	domain	tcqrd1		s.posted.quantity
	static	domain	tcamnt		s.posted.amount

	if s.orno = tdpur045.orno and
	   s.pono = tdpur045.pono and
	   s.srnb = tdpur045.srnb then
		if i.read.quantity then
			return(s.posted.quantity)
		else
			return(s.posted.amount)
		endif
	else
		s.orno = tdpur045.orno
		s.pono = tdpur045.pono
		s.srnb = tdpur045.srnb
		s.posted.quantity = 0.0
		s.posted.amount = 0.0
	endif

	select  sum(tdpur046.qana):s.posted.quantity,
		sum(tdpur046.amtc):s.posted.amount
	from    tdpur046
	where   tdpur046._index1 = {	:tdpur040.suno,
					:tdpur045.orno,
					:tdpur045.pono,

					:tdpur045.srnb}

	and     tdpur046.stsp = tcstsp.invoice.passed
	selectdo

	endselect

	if i.read.quantity then
		return(s.posted.quantity)
	else
		return(s.posted.amount)
	endif
}

|**** Contract fields *********************************************************

function domain bcorno get.purchase.contract.number()
{
	static	domain	tccono		s.old.cono
	static	domain	bcorno		s.new.cono fixed

	if tdpur045.cono = s.old.cono then
		return(s.new.cono)
	else
		s.old.cono = tdpur045.cono
		s.new.cono = ""
	endif

	if tdpur041.oqua = 0 then
		return(s.new.cono)
	endif

	if tdpur045.cono <> 0 then
		s.new.cono = convert.number(bcckon.act.pur.contr, tdpur045.cono)
	endif

	return(s.new.cono)
}

function domain tcpono get.purchase.contract.line.number()
{
	if tdpur041.oqua = 0 then
		return(0)
	endif

	return(tdpur045.posi)
}

function domain bcstd.fent get.contract.purchase.office()
{
			long		l.ret

	if tdpur045.cono = 0 then
		return("")
	endif

	if tdpur041.oqua = 0 then
		return("")
	endif

	if tccom020.comp <> 0 then
		l.ret = get.enterprise.unit(
				tccom020.comp,
				get.compnr(),
				g.error.message)	|* ref
		return(bcstc018.pofd)

	endif



	return(bcstd000.pofc)
}

|**** Price fields ************************************************************

function long get.price.origin()
{
	if tdpur041.oqua = 0 then
		return(target(tdgen.porg.not.applicable))
	endif

	if tdpur041.pric <> tdpur045.pric then
		return(target(tdgen.porg.manual))
	endif

	on case tdpur041.plev
	case tdpur.plev.contract:
		return(target(tdgen.porg.contract))
|	case tdpur.plev.supl.item:				|#800-141295.o
	case tdpur.plev.plst.item:
		return(target(tdgen.porg.price.structure))
	case tdpur.plev.supl.item:				|#800-141295.n
	case tdpur.plev.ilst:
		return(target(tdgen.porg.supp.pr.book))
	case tdpur.plev.item:
		return(target(tdgen.porg.item.pur))
	case tdpur.plev.not.def:
		return(target(tdgen.porg.not.applicable))
	default:
|		return(target(tdgen.porg.not.applicable))	|#FUT.o
		break						|#FUT.n
	endcase

	return(target(tdgen.porg.manual))
}

function domain bcstr9 get.price.matrix.definition()
{
	static	domain	tdpur.plev	s.price.level
	static	domain	bcstr9		s.matrix.definition fixed
			long		l.ret

	if s.price.level = tdpur041.plev then
		return(s.matrix.definition)
	else
		s.price.level = tdpur041.plev
		s.matrix.definition = ""
	endif

	if tdpur041.oqua = 0 then
		return(s.matrix.definition)
	endif

	l.ret = get.price.matrix.definition.pur(
			tdpur041.plev,
			s.matrix.definition,	|* ref
			g.error.message)	|* ref

	return(s.matrix.definition)
}

function domain tcpric get.customs.value()
{
	|* Field is not implemented yet in Baan 6. Not yet clear how
	|* the field must be filled.

		domain	tcpric		l.cuva

	if tdpur041.oqua >= 0 then
		return(0.0)
	endif

	if tdpur045.ldam = 0 and
	   tdpur045.odam = 0 then
		return(tdpur045.pric)
	endif

	if tdpur041.cvqp = 0 then
		tdpur041.cvqp = 1.0
	endif

	l.cuva = ((tdpur041.amta - tdpur045.odam) / tdpur041.oqua) *
			(tdpur041.cvpp / tdpur041.cvqp)

	l.cuva = imcs0010.round.pric(
			l.cuva,
			tdpur040.ccur,
			tcmcs002.crnd)

	if l.cuva < 0 then

		l.cuva = -1.0 * l.cuva
	endif

	return(l.cuva)
}

|**** Discount fields *********************************************************

function long get.discount.origin(
			long		i.element)
{
	if tdpur045.pric = 0 then
		return(target(tdgen.dorg.not.applicable))
	endif

	if tdpur045.disc(i.element) = 0 and			|#800-141028.sn
	   tdpur045.damt(i.element) = 0 then
		return(target(tdgen.dorg.not.applicable))
	endif							|#800-141028.en

	if tdpur041.disc(i.element) <> tdpur045.disc(i.element) or
	   tdpur041.ldam(i.element) <> tdpur045.damt(i.element) then
		return(target(tdgen.dorg.manual))
	endif

	on case tdpur041.dlev(i.element)
	case tdpur.dlev.contract:
		return(target(tdgen.dorg.contract))
	case tdpur.dlev.supp:
	case tdpur.dlev.supp.item:
	case tdpur.dlev.supp.pgrp:
	case tdpur.dlev.plst:

	case tdpur.dlev.plst.item:
	case tdpur.dlev.plst.pgrp:

	case tdpur.dlev.ilst:					|#800-141028.n
		return(target(tdgen.dorg.disc.structure))
|	case tdpur.dlev.ilst:					|#800-141028.so
|		return(target(tdgen.dorg.disc.pr.book))		|#800-141028.eo
	case tdpur.dlev.not.def:
		return(target(tdgen.dorg.not.applicable))
	default:
|		return(target(tdgen.dorg.not.applicable))	|#FUT.o
		break						|#FUT.n
	endcase

	return(target(tdgen.dorg.manual))
}

function long get.discount.matrix.type(
			long		i.element)
{
	if tdpur041.dlev(i.element) = tdpur.dlev.contract or
	   tdpur041.dlev(i.element) = tdpur.dlev.manual or
	   tdpur041.dlev(i.element) = tdpur.dlev.not.def or
	   tdpur041.disc(i.element) <> tdpur045.disc(i.element) or
	   tdpur041.ldam(i.element) <> tdpur045.damt(i.element) then
		return(target(tdpcg.maty.none))
	endif

	if tdpur045.pric = 0 then
		return(target(tdpcg.maty.none))
	endif

	if tdpur045.disc(i.element) = 0 and			|#800-141028.sn
	   tdpur045.damt(i.element) = 0 then
		return(target(tdpcg.maty.none))
	endif							|#800-141028.en

	return(target(tdpcg.maty.poldsc))
}

function domain bcstr9 get.discount.matrix.definition(
				long		i.element)
{

	static	domain	tdpur.dlev	s.discount.level
	static	domain	bcstr9		s.matrix.definition fixed
			long		l.ret

	if s.discount.level = tdpur041.dlev(i.element) then
		return(s.matrix.definition)
	else
		s.discount.level = tdpur041.dlev(i.element)
		s.matrix.definition = ""
	endif

	if tdpur045.pric = 0 then
		return(s.matrix.definition)
	endif

	if tdpur045.disc(i.element) = 0 and			|#800-141028.sn
	   tdpur045.damt(i.element) = 0 then
		return(s.matrix.definition)
	endif							|#800-141028.en

	l.ret = get.discount.matrix.definition.pur(
			tdpur041.dlev(i.element),
			s.matrix.definition,	|* ref
			g.error.message)	|* ref

	return(s.matrix.definition)
}

function domain tcyesno get.determining.flag()
{
			long		l.disc.lvl
			long		l.array.length

	if tdpur045.pric = 0 then
		|* Field is not applicable, so the default value is
		|* set.
		return(tcyesno.yes)
	endif

	l.array.length = tdpur.dll0022.array.length()
	for l.disc.lvl = 1 to l.array.length
		if tdpur041.dlev(l.disc.lvl) = tdpur.dlev.supp.pgrp then
			read.disc.parameters.supp.pgrp(l.disc.lvl)
			return(tdpur034.stsa)
		endif

		if tdpur041.dlev(l.disc.lvl) = tdpur.dlev.plst.pgrp then
			read.disc.parameters.plst.pgrp(l.disc.lvl)
			return(tdpur035.stsa)
		endif
	endfor

|	return(tcyesno.no)					|#800-142925.o
	return(tcyesno.yes)					|#800-142925.n
}

function domain tcyesno get.eligible.flag()
{
			long		l.disc.lvl
			long		l.array.length

	if tdpur045.pric = 0 then
		|* Field is not applicable, so the default value is
		|* set.
		return(tcyesno.yes)
	endif

	l.array.length = tdpur.dll0022.array.length()
	for l.disc.lvl = 1 to l.array.length
		if tdpur041.dlev(l.disc.lvl) = tdpur.dlev.supp.pgrp then
			read.disc.parameters.supp.pgrp(l.disc.lvl)
			return(tdpur034.stsb)

		endif
		if tdpur041.dlev(l.disc.lvl) = tdpur.dlev.plst.pgrp then
			read.disc.parameters.plst.pgrp(l.disc.lvl)
			return(tdpur035.stsb)
		endif
	endfor

|	return(tcyesno.no)					|#800-142925.o
	return(tcyesno.yes)					|#800-142925.n
}

function read.disc.parameters.supp.pgrp(
			domain	tddile		i.discount.level)
{
	static	domain	tcsuno		s.supplier
	static	domain	tcitem		s.item
	static	domain	tddile		s.discount.level

	if s.supplier = tdpur045.suno and
	   s.item = tdpur041.item and
	   s.discount.level = i.discount.level then
		return
	else
		s.supplier = tdpur045.suno
		s.item = tdpur041.item
		s.discount.level = i.discount.level
	endif

	select	tdpur034.stsa, tdpur034.stsb
	from	tdpur034
	where	tdpur034._index1 = {	:tdpur045.suno,
					:g.price.group,
					:i.discount.level}
	as set with 1 rows

	selectdo
	endselect
}

function read.disc.parameters.plst.pgrp(
			domain	tddile		i.discount.level)
{
		domain	tccplt		l.price.list
	static	domain	tcorno		s.order
	static	domain	tcitem		s.item
	static	domain	tddile		s.discount.level

	if s.order = tdpur045.orno and
	   s.item = tdpur041.item and
	   s.discount.level = i.discount.level then
		return
	else
		s.order = tdpur045.orno

		s.item = tdpur041.item
		s.discount.level = i.discount.level
	endif

	l.price.list = get.price.list()

	select	tdpur035.stsa, tdpur035.stsb
	from	tdpur035
	where	tdpur035._index1 = {	:l.price.list,
					:g.price.group,
					:i.discount.level}
	as set with 1 rows
	selectdo

	endselect
}

function domain tccplt get.price.list()
{
	if not isspace(tdpur040.cplp) then
		return(tdpur040.cplp)
	endif

	if not isspace(tdpur040.pspr) then
		return(get.supplier.price.list(tdpur040.pspr))
	else
		return(get.supplier.price.list(tdpur040.suno))
	endif
}

function domain tccplt get.supplier.price.list(
			domain	tcsuno		i.supplier)
{
		domain	tccplt		l.price.list

	if i.supplier = tdpur040.suno then

		return(tccom020.cplp)
	endif

	select	tccom020.cplp:l.price.list
	from	tccom020
	where	tccom020._index1 = {:i.supplier}
	as set with 1 rows
	selectdo
	endselect

	return(l.price.list)
}

|**** Finance/Tax fields ******************************************************

function domain bcglco get.general.ledger.code()
{
	if not isspace(tdpur041.pacn) then
		return(transform.leac.and.dim.in.gl.code(
			tdpur041.pacn,
			tdpur041.dim1,
			tdpur041.dim2,
			tdpur041.dim3,
			tdpur041.dim4,
			tdpur041.dim5))
	endif

	return("")

}


function domain tcccty get.tax.country()
{
		domain	tcccty		l.new.country
		domain	tccvat		l.dummy.tax

	get.tax.fields(
		l.new.country,		|* ref
		l.dummy.tax)		|* ref

	return(l.new.country)
}

function domain tccvat get.tax.code()
{
		domain	tcccty		l.dummy.country
		domain	tccvat		l.new.tax

	get.tax.fields(
		l.dummy.country,	|* ref
		l.new.tax)		|* ref

	return(l.new.tax)
}

function get.tax.fields(
		ref	domain	tcccty		o.new.country,
		ref	domain	tccvat		o.new.code)
{
	static	domain	tcccty		s.tax.country
	static	domain	tccvat		s.tax.code
	static	domain	tcccty		s.new.country fixed
	static	domain	tccvat		s.new.code fixed
			long		l.ret

	if tdpur041.ccty = s.tax.country and
	   tdpur041.cvat = s.tax.code then
		o.new.country = s.new.country
		o.new.code = s.new.code

		return
	else
		s.tax.country = tdpur041.ccty
		s.tax.code = tdpur041.cvat

		s.new.country = ""
		s.new.code = ""
	endif

	l.ret = get.tax.by.country.code(
			tdpur041.ccty,
			tdpur041.cvat,


			s.new.country,		|* ref
			s.new.code,		|* ref
			g.error.message)	|* ref

	o.new.country = s.new.country
	o.new.code = s.new.code
}

|**** Change fields ***********************************************************

function domain tcmcs.str8 get.change.order.sequence.number()
{
	if g.total.line or
	   g.extra.dir.del.bo.line then
		return("")
	endif

	read.purchase.order.change.codes()
	return(tdpur703.cosn)
}

function domain tccdis get.change.reason.code()
{
	if g.total.line or

	   g.extra.dir.del.bo.line then
		return("")
	endif

	read.purchase.order.change.codes()
	return(tdpur703.crcd)
}

function domain tccdis get.change.type.code()
{
	if g.total.line or

	   g.extra.dir.del.bo.line then
		return("")
	endif

	read.purchase.order.change.codes()
	return(tdpur703.ctcd)
}

function read.purchase.order.change.codes()
{
	static	domain	tcorno		s.order
	static	domain	tcpono		s.position

	|* To prevent too much read actions.
	if s.order = tdpur045.orno and
	   s.position = tdpur045.pono then
		return
	else
		s.order = tdpur045.orno
		s.position = tdpur045.pono

	endif

	|* Only the values are read that are needed for conversion.
	select	tdpur703.cosn,
		tdpur703.crcd,
		tdpur703.ctcd
	from	tdpur703
	where	tdpur703._index1 = {	:tdpur045.orno,
					:tdpur045.pono}
	as set with 1 rows
	selectdo
	selectempty
		tdpur703.cosn = ""
		tdpur703.crcd = ""
		tdpur703.ctcd = ""
	endselect
}

function domain tcmcs.str2 get.acknowledgement.code()
{
	static	domain	tcorno		s.order
	static	domain	tcpono		s.position





	if g.total.line or
	   g.extra.dir.del.bo.line then

		return("")
	endif

	|* To prevent too much read actions.
	if s.order = tdpur045.orno and
	   s.position = tdpur045.pono then
		return(tdpur704.akcd)
	else
		s.order = tdpur045.orno
		s.position = tdpur045.pono
		tdpur704.akcd = ""
	endif

	|* Only the value is read that is needed for conversion; when
	|* there are more than one records present for the order
	|* line, then the latest record is taken for conversion.
	select	tdpur704.akcd, tdpur704._index1
	from	tdpur704
	where	tdpur704._index1 = {	:tdpur045.orno,
					:tdpur045.pono}
	order by tdpur704._index1 desc
	as set with 1 rows
	selectdo
	endselect

	return(tdpur704.akcd)
}

|**** Business Partner fields *************************************************

function domain bcstc.bpid convert.business.partner()
{
	static	domain	tcsuno		s.old.suno
	static	domain	bcstc.bpid	s.new.suno fixed

	if tdpur041.suno = s.old.suno then
		return(s.new.suno)
	else
		s.old.suno = tdpur041.suno
		s.new.suno = ""
	endif

	s.new.suno = get.business.partner.code(
				tdpur041.suno,
				bcstc.orgn.suno,
				g.error.message)	|* ref

	return(s.new.suno)
}

function domain bccadr get.business.partner.address()
{
		domain	bcstc.keys	l.key fixed
	static	domain	tcorno		s.order
	static	domain	bccadr		s.address fixed

	|* To prevent determining the addres multiple times for 1
	|* order.
	if s.order = tdpur040.orno then
		return(s.address)
	else
		s.order = tdpur040.orno
		s.address = ""
	endif

	|* Initialization.
	l.key = ""

	|* 1. Get postal address from purchase order header.
	if not isspace(tdpur040.ccor) then
		l.key(1;6) = tdpur040.suno
		l.key(7;3) = tdpur040.ccor
		s.address = get.address.code(
				"tccom022",
				l.key,
				g.error.message)	|* ref
		return(s.address)
	endif

	|* 2. Get specific postal address.
	select	tdpur042._index1
	from	tdpur042
	where	tdpur042._index1 = {	:tdpur040.orno,
					0,	|* Position number
					1}	|* Type: postal address
	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,
				g.error.message)	|* ref
		return(s.address)
	endselect

	|* 3. Get postal address from supplier.
	if not isspace(tccom020.ccor) then
		l.key(1;6) = tdpur040.suno
		l.key(7;3) = tccom020.ccor
		s.address = get.address.code(
				"tccom022",
				l.key,
				g.error.message)
		return(s.address)
	endif

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

function domain bcstc.ccnt get.business.partner.contact()
{
	static	domain	bcstc.ccnt	s.contact fixed
	static	domain	tcsuno		s.pur045.suno
			long		l.ret

	if s.pur045.suno <> tdpur045.suno then


		s.pur045.suno = tdpur045.suno
		l.ret = get.cross.reference.for.contact.code(
				"tccom020a",
				tdpur045.suno,
				g.error.message)
		s.contact = bcstc005.ccnt
		|* If no contact is found in bcstc005, bcstc005.ccnt
		|* will be cleared.

	endif

	return(s.contact)
}

function domain tcyesno get.vendor.rating.flag()
{
	if tdpur041.oqua < 0 then

		return(tcyesno.no)
	endif

	return(tdpst000.surl)
}

|**** Procedure fields ********************************************************

function domain tcpmsk get.procedure.mask()
{
		domain	bcstc.lng2	i
		domain	tcsess		l.old.activity
		domain	bcstc.lng2	l.new.pmsk.pos
	static	domain	tcpmsk		s.procedure.mask
	static	domain	tcorno		s.orno
	static	domain	tcpono		s.pono
	static	domain	tcsrnb		s.srnb
			long		l.ret

	if g.total.line or
	   g.extra.dir.del.bo.line then
		return("nnnnnnnnnnnnnnnnnnnn")
	endif

	if tcmcs042.coun = tcyesno.yes and			|#800-142092.sn
	   tdpur045.date = 0 then
		return("nnnnnnnnnnnnnnnnnnnn")
	endif							|#800-142092.en

	if s.orno = tdpur045.orno and
	   s.pono = tdpur045.pono and
	   s.srnb = tdpur045.srnb then
		return(s.procedure.mask)
	else
		s.orno = tdpur045.orno
		s.pono = tdpur045.pono
		s.srnb = tdpur045.srnb
		s.procedure.mask = "nnnnnnnnnnnnnnnnnnnn"
	endif

	for i = 1 to 20
		if tdpur045.pmsk(i;1) = "n" then
			continue
		endif

		|* Get session code for procedure mask position.
		l.old.activity = g.old.session.code.activities.array(1,i)

		on case strip$(l.old.activity)
		case "tdpur4401m000":
			|* Print Order
			l.new.pmsk.pos = get.new.pmsk.position("tdpur4401m000")
			if l.new.pmsk.pos <> 0 then
				s.procedure.mask(l.new.pmsk.pos;1) =
					tdpur045.pmsk(i;1)
			endif
			break
		case "tdpur4120m000":
			|* Receipt
			if tdpur041.qual = tcyesno.no then
				l.new.pmsk.pos =
|								|#203701.so
|					get.new.pmsk.position("tdpur4501m300")
|								|#203701.eo
								|#203701.sn
					get.new.pmsk.position("tdpur4106m000")
								|#203701.en
				if l.new.pmsk.pos <> 0 then
					s.procedure.mask(l.new.pmsk.pos;1) =
						tdpur045.pmsk(i;1)
				endif
			endif
			break
		case "tdpur4121m000":
			|* Approval
|								|#203701.so
|			l.new.pmsk.pos = get.new.pmsk.position("tdpur4501m300")
|								|#203701.eo
								|#203701.sn
			l.new.pmsk.pos = get.new.pmsk.position("tdpur4106m000")
								|#203701.en
			if l.new.pmsk.pos <> 0 then
				s.procedure.mask(l.new.pmsk.pos;1) =
					tdpur045.pmsk(i;1)
			endif
			break
 	 	case "tdpur4420m000":
			|* Print Claim
			if tcmcs042.coun = tcyesno.no and
			   tcmcs042.reto = tcyesno.no then
				l.new.pmsk.pos =
					get.new.pmsk.position("tdpur4420m000")
				if l.new.pmsk.pos <> 0 then
					s.procedure.mask(l.new.pmsk.pos;1) =
						tdpur045.pmsk(i;1)
				endif
			endif
			break
 	 	case "tdpur4411m000":
			|* Print Return Note
			if tcmcs042.coun = tcyesno.no and
			   tcmcs042.reto = tcyesno.no then
				l.new.pmsk.pos =
					get.new.pmsk.position("tdpur4411m000")
				if l.new.pmsk.pos <> 0 then
					s.procedure.mask(l.new.pmsk.pos;1) =
						tdpur045.pmsk(i;1)
				endif
			endif
			break
 	 	case "tdpur4404m000":
			|* Print Invoice
			l.new.pmsk.pos = get.new.pmsk.position("tdpur4404m000")
			if l.new.pmsk.pos <> 0 then
				s.procedure.mask(l.new.pmsk.pos;1) =
					tdpur045.pmsk(i;1)
			endif
			break
 	 	case "tdpur4223m000":
			|* Process Delivered Order
			l.new.pmsk.pos = get.new.pmsk.position("tdpur4223m000")
			if l.new.pmsk.pos <> 0 then
								|#800-141942.sn
				if to.be.confirmed.bo.qty.is.applicable() and
				   tdpur041.bqua <> 0 then
					s.procedure.mask(l.new.pmsk.pos;1) = "y"
				else				|#800-141942.en
					s.procedure.mask(l.new.pmsk.pos;1) =
						tdpur045.pmsk(i;1)
				endif				|#800-141942.n
			endif

								|#203701.sn
			|* must be execute here, uses pmsk value of 4223m000
			if tcmcs042.drct = tcyesno.yes then
				l.new.pmsk.pos =
					get.new.pmsk.position("tdpur4222m000")
				if l.new.pmsk.pos <> 0 then
					s.procedure.mask(l.new.pmsk.pos;1) =
						s.procedure.mask(20;1)
				endif
			endif					|#203701.en
			break
		endcase
	endfor

	|* Generate Freight Order
	l.new.pmsk.pos = get.new.pmsk.position("tdpur4220m000")
	if tccom999.trtr = tcyesno.yes and
	   l.new.pmsk.pos <> 0 then
		if tdpur045.torn <> 0 then
			|* Closed freight order are not converted to
			|* Baan 6; procedure step becomes "n".
			l.ret = convert.transport.order.number(
					tdpur045.torn,
					tdpur045.tpon,
					g.error.message)
			if isspace(bcsfm001.norn) then
				s.procedure.mask(l.new.pmsk.pos;1) = "n"
			else
				s.procedure.mask(l.new.pmsk.pos;1) = "e"
			endif
		else
			if generate.freight.order() = tcyesno.yes then
				s.procedure.mask(l.new.pmsk.pos;1) = "y"
			endif
		endif
	endif

	|* Release to Warehousing
	if not isspace(get.warehouse()) then
		l.new.pmsk.pos = get.new.pmsk.position("tdpur4246m000")
		if l.new.pmsk.pos <> 0 then

			s.procedure.mask(l.new.pmsk.pos;1) = "e"
		endif
	endif

	|* For collect orders the Receipt step is not present in
	|* BaanIV, but must be present in Baan 6.
|								|#203701.so
|	l.new.pmsk.pos = get.new.pmsk.position("tdpur4501m300") |#800-141852.sn
|								|#203701.eo
	l.new.pmsk.pos = get.new.pmsk.position("tdpur4106m000") |#203701.n
	if l.new.pmsk.pos <> 0 and
	   s.procedure.mask(l.new.pmsk.pos;1) =	"n" then
		if tdpur045.date = 0 then
			s.procedure.mask(l.new.pmsk.pos;1) = "y"
		else
			s.procedure.mask(l.new.pmsk.pos;1) = "e"
		endif
	endif							|#800-141852.en

	return(s.procedure.mask)
}

function fill.old.session.code.activities.array()
{
		domain	tcsess		l.old.session.code.activity
			long		i

	for i = 1 to 20
		select	tcmcs039.sess:l.old.session.code.activity
		from	tcmcs039
		where	tcmcs039._index2 = {	tcmodc.pur,

						:i}
		as set with 1 rows
		selectdo
		selectempty
			l.old.session.code.activity = ""
		endselect

		g.old.session.code.activities.array(1,i) =
					l.old.session.code.activity
	endfor
}

function domain	bcstc.lng2 get.new.pmsk.position(
			domain	tcsess		i.new.activity)
{
	|* This function returns the new activity number for an
	|* activity. If the activity is not part of the order type
	|* the new activity number is 0.

		domain	bcstc.lng2	l.new.activity.number
		domain	bcstc.lng2	l.position

	l.new.activity.number = 0

	for l.position = 1 to 20
		if g.new.session.code.position.array(1,l.position) =
					strip$(i.new.activity) then
			l.new.activity.number = l.position
			break
		endif
	endfor

	return(l.new.activity.number)
}


function domain bcstd.acti get.order.activity()
{
		domain	tcpmsk		l.procedure.mask
		domain	bcstc.lng2	l.step.number
		long			l.last.step		|#202132.sn
		long			l.max.no		|#202132.en
	static	domain	bcstd.acti	s.order.activity fixed
	static	domain	tcpmsk		s.procedure.mask
	static	domain	tcbool		s.total.line
	static	domain	tccotp		s.order.type
	static	domain	tcstsc		s.stsc
	static	domain	tcstsd		s.stsd

|	if g.total.line or					|#800-142092.so
|	   g.extra.dir.del.bo.line then				|#800-142092.eo
	if g.total.line then					|#800-142092.n
		return("")
	endif

	if g.extra.dir.del.bo.line then				|#800-142092.sn
		return("tdpur4210m100")
	endif

	if tcmcs042.coun = tcyesno.yes and
	   tdpur045.date = 0 then
		return("tdpur4210m100")
	endif							|#800-142092.en

	l.procedure.mask = get.procedure.mask()

	if s.procedure.mask = l.procedure.mask and
	   s.total.line = g.total.line and
	   s.order.type = tdpur040.cotp and
	   s.stsc = tdpur045.stsc and
	   s.stsd = tdpur045.stsd then
		return(s.order.activity)
	else
		s.procedure.mask = l.procedure.mask
		s.total.line = g.total.line
		s.order.type = tdpur040.cotp
		s.stsc = tdpur045.stsc
		s.stsd = tdpur045.stsd
		s.order.activity = ""
	endif

|	l.step.number = pos(l.procedure.mask, "y")		|#202132.o
	l.last.step = rpos(l.procedure.mask, "e")		|#202132.sn
	|* Find the last executed activity
	if l.last.step = 0 then
		|* If no activity is executed find the first not yet executed
		|* activity.
		l.step.number = pos(l.procedure.mask, "y")
	else
		l.max.no = fill.max.stepno.isls0014()
		if l.last.step = l.max.no then
			l.step.number = l.max.no
		else
			|* Find the first not yet executed activity after the
			|* last executed activity.
			for l.step.number = l.last.step to l.max.no
				if l.procedure.mask(l.step.number;1) = "y" then
					break
				endif
			endfor
		endif
	endif

	if l.step.number = 0 or
	   l.step.number = l.max.no then			|#202132.en
|	if l.step.number = 0 then				|#202132.o
		if tdpur045.stsd = tcstsd.all.approved and
		   tdpur045.stsc = tcstsc.updated then
			s.order.activity = "tdpur4224m000"
		else
			s.order.activity = "tdpur4223m000"
		endif
		return(s.order.activity)
	endif

	s.order.activity = g.new.session.code.position.array(1,l.step.number)


	return(s.order.activity)
}

function domain tcbool activity.in.order.procedure(
			domain	tcsess		i.activity)
{
	static	domain	tccotp		s.order.type
	static	domain	tcsess		s.activity
	static	domain	tcbool		s.order.step.present
		domain	tcstno		l.step.number
			long		l.max.no
			long		i

	if s.order.type = tdpur040.cotp and
	   s.activity = i.activity then
		return(s.order.step.present)
	else
		s.order.type = tdpur040.cotp
		s.activity = i.activity
		s.order.step.present = false
	endif

	l.step.number = read.step.number(i.activity)
	l.max.no = fill.max.stepno.isls0014()

	for i = 1 to l.max.no
		if tcmcs042.stno(i) = l.step.number then
			s.order.step.present = true
			break
		endif
	endfor

	return(s.order.step.present)
}

function domain tcstno read.step.number(

		domain	tcsess		i.activity)
{
	static	domain	tcsess		s.activity
	static	domain	tcstno		s.step.number

	if s.activity = i.activity then
		return(s.step.number)
	else
		s.activity = i.activity
		s.step.number = 0
	endif

	select  tcmcs039.stno:s.step.number
	from    tcmcs039
	where   tcmcs039._index1 = {:i.activity}
	as set with 1 rows
	selectdo
	endselect

	return(s.step.number)
}

|**** Freight fields **********************************************************


function domain tcyesno generate.freight.order()
{
	if tccom999.trtr = tcyesno.no or
	   tdpur000.agto.4 = tdsls.acpd.no then
		return(tcyesno.no)
	endif

	if isspace(get.warehouse()) then
		return(tcyesno.no)
	endif

	if tcmcs042.coun = tcyesno.yes or
	   tcmcs042.drct = tcyesno.yes then
		return(tcyesno.no)
	endif

	if g.item.type <> tckitm.purchase and
	   g.item.type <> tckitm.manufacture then
		return(tcyesno.no)
	endif

	if activity.in.order.procedure("trtoc2201m000") then
		return(tcyesno.yes)
	endif

	return(tcyesno.no)
}

function string get.freight.service.level()
{
	static	domain	tcorno		s.order
	static	domain	tccprj		s.project
	static	domain	tcitem		s.item
	static	domain	tccwar		s.warehouse
	static		string		s.service.level fixed

	if tdpur045.orno = s.order and
	   tdpur045.cprj = s.project and
	   tdpur045.item = s.item and
	   tdpur045.cwar = s.warehouse then
		return(s.service.level)
	else
		s.order = tdpur045.orno
		s.project = tdpur045.cprj
		s.item = tdpur045.item
		s.warehouse = tdpur045.cwar
		s.service.level = ""
	endif

	if generate.freight.order() = tcyesno.no then
		return(s.service.level)
	endif

	select	trtcd120.serv:s.service.level
	from	trtcd120
	where	trtcd120._index1 = {	:tdpur045.cprj,
					:tdpur045.item}
	as set with 1 rows
	selectdo
	endselect

	return(s.service.level)
}

function string get.planned.load.date()
{
	if generate.freight.order() = tcyesno.no then
		return(utc(0,0))
	endif

	return(utc(tdpur041.odat, 0))
}

function string get.freight.order.number()
{
			long		l.ret

	if g.total.line or
	   g.extra.dir.del.bo.line then
		return("")
	endif

	if tdpur045.torn = 0 then
		return("")
	endif


	if tccom999.trtr = tcyesno.yes then
		l.ret = convert.transport.order.number(
				tdpur045.torn,
				tdpur045.tpon,
				g.error.message)
	else
		return("")
	endif

	return(bcsfm001.norn)
}

function long get.freight.position.number()
{
			long		l.ret

	if g.total.line or
	   g.extra.dir.del.bo.line then
		return(0)
	endif

	if tdpur045.torn = 0 then
		return(0)
	endif

	if tccom999.trtr = tcyesno.yes then
		l.ret = convert.transport.order.number(
				tdpur045.torn,
				tdpur045.tpon,
				g.error.message)
	else
		return(0)
	endif

	if not isspace(bcsfm001.norn) then

		|* New Freight position number is always 10.
		return(10)
	endif

	return(0)
}

|**** Status fields ***********************************************************

function domain tcyesno get.approval.flag()
{
	if g.extra.dir.del.bo.line then
		|* Extra inserted backorder lines for direct delivery
		|* are not yet approved.
		return(tcyesno.no)
	endif

	if g.total.line and
	   g.extra.bo.line.must.be.inserted then
		|* If one of the linked detail/backorder lines is not
		|* approved, then the total line isn't approved
		|* either.
		return(tcyesno.no)
	endif

	if tcmcs042.coun = tcyesno.yes and			|#800-142092.sn
	   tdpur045.date = 0 then
		|* In BaanIV it is possible to enter a collect order
		|* line and not receive this line.
		return(tcyesno.no)
	endif							|#800-142092.en

	return(tcyesno.yes)
}

function long get.history.update.status()
{
	if g.total.line or
	   g.extra.dir.del.bo.line then
		return(target(tcstsc.free))
	endif

	return(etol(tdpur045.stsc))
}

function long get.invoice.status()
{
	if g.total.line or
	   g.extra.dir.del.bo.line then
		return(target(tcstsd.free))
	endif

	return(etol(tdpur045.stsd))
}

|* End of script **************************************************************
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:pur
:TFLNO:451
:FUNC:START
|******************************************************************************
|* Script: tdpur451 - functions
|* Author: Hans Mulder
|******************************************************************************

function read.purchase.order.history()
{
	|* While the postal address can be changed when lines are present,
	|* the order header history must be read every time.  

	static	domain	tcorno		s.order			|#PERF.sn
	static	domain	tcdate		s.trans.date
	static	domain	tctime		s.trans.time

	if s.order = tdpur051.orno and
	   s.trans.date = tdpur051.trdt and
	   s.trans.time = tdpur051.trtm then
		return
	else
		s.order = tdpur051.orno
		s.trans.date = tdpur051.trdt
		s.trans.time = tdpur051.trtm
	endif

	|* This optimalization requires a descending order by on the
	|* main tdpur051 query.
	if tdpur050.orno = tdpur051.orno and 
	   ((tdpur050.trdt = tdpur051.trdt and
	     tdpur050.trtm <= tdpur051.trtm) or
	     tdpur050.trdt < tdpur051.trdt) then
		|* Last retrieved record is also actual for this
		|* request.
		return
	endif							|#PERF.en

	select	tdpur050.corg, tdpur050.ccor,
		tdpur050.cotp, tdpur050.cfrw,
		tdpur050._index1, tcmcs042.casi,
		tcmcs042.reto, tcmcs042.coun,
		tcmcs042.drct, tcmcs042.stno,
		tccom020.ccor, tccom020.comp
	from	tdpur050, tcmcs042, tccom020
	where	tdpur050._index1 = {:tdpur051.orno}
	and	((tdpur050.trdt = :tdpur051.trdt and
		  tdpur050.trtm <= :tdpur051.trtm) or
		 tdpur050.trdt < :tdpur051.trdt)
	and	tcmcs042.cotp = tdpur050.cotp
	and	tccom020.suno = tdpur050.suno
	order by tdpur050._index1 desc
	as set with 1 rows
	selectdo
	endselect

	load.new.session.code.position.array()
}

function load.new.session.code.position.array()
{
	static	domain	tccotp		s.order.type
			long		l.ret

	if s.order.type = tdpur050.cotp then
		return
	else
		s.order.type = tdpur050.cotp
		l.ret = set.mem(g.new.session.code.position.array, "")
	endif

	|* Array with new order type session codes now loaded.
	select	bcstd004.acno, bcstd004.acti
	from	bcstd004
	where	bcstd004._index2 = {:tdpur050.cotp}
	selectdo
		g.new.session.code.position.array(1,bcstd004.acno) =
				bcstd004.acti
	endselect
}

function read.item.data()
{
		domain	tcpuma		l.project.item.type
	static	domain	tccprj		s.project
	static	domain	tcittb		s.opol
	static	domain	tcitem		s.item

	if s.project = tdpur051.cprj and
	   s.opol = tdpur051.opol and
	   s.item = tdpur051.item then
		return
	else
		s.project = tdpur051.cprj
		s.opol = tdpur051.opol
		s.item = tdpur051.item
	endif

	|**************************************************************
	|* For standard items the data is retrieved from tiitm001.
	|* For customized items the data is retrieved from tipcs021.
	|**************************************************************

	if tdpur051.opol <> tcittb.standard then
		select	tipcs021.kitm:l.project.item.type,
			tipcs021.cuni:g.inventory.unit
		from	tipcs021
		where	tipcs021._index1 = {	:tdpur051.cprj,
						:tdpur051.item}
		as set with 1 rows
		selectdo
			if l.project.item.type = tcpuma.purchase then
				g.item.type = tckitm.purchase
			endif
			if l.project.item.type = tcpuma.manufacture then
				g.item.type = tckitm.manufacture
			endif
		endselect
	else
		select	tiitm001.kitm:g.item.type,
			tiitm001.cuni:g.inventory.unit
		from	tiitm001
		where	tiitm001._index1 = {	:tdpur051.item}
		as set with 1 rows
		selectdo
		endselect
	endif
}

function domain bcorno get.purchase.order.number()
{
	static	domain	tcorno		s.old.orno
	static	domain	bcorno		s.new.orno fixed

	if tdpur051.orno = s.old.orno then
		return(s.new.orno)
	else
		s.old.orno = tdpur051.orno
		s.new.orno = ""
	endif

	s.new.orno = convert.number(bcckon.act.pur, tdpur051.orno)

	return(s.new.orno)
}

function long get.serial.number()				|#IDX-ERR.sn
{
	if tdpur051.ckor = 2 then
		|* In Baan 6 the record type (tdpur451.ckor) is
		|* missing in index 5. For normal order handling that
		|* index is unique due to the way tdpur451.sern is
		|* filled in Baan 6. During the migration a duplicate
		|* record error can occur when an order line is
		|* cancelled. In that case an intake record and a
		|* cancellation record are inserted, both with
		|* tdpur051.sern is 0 and sometimes both with the
		|* same transaction date and time.
		|* For this situation the sern is raised with 10 for
		|* the cancellation record to avoid duplicate
		|* records for index 5.
		return(tdpur051.sern + 10)
	endif

	return(tdpur051.sern)
}								|#IDX-ERR.en

function domain bcorno get.purchase.invoice.number()
{
	static	domain	tcinvn		s.old.invn
	static	domain	bcorno		s.new.invn fixed

	if tdpur051.invn = s.old.invn then
		return(s.new.invn)
	else
		s.old.invn = tdpur051.invn
		s.new.invn = ""
	endif

	if tdpur051.invn <> 0 then
		s.new.invn = convert.number(bcckon.invoice.pur, tdpur051.invn)
	endif

	return(s.new.invn)
}

function domain bcorno get.purchase.contract.number()
{
	static	domain	tccono		s.old.cono
	static	domain	bcorno		s.new.cono fixed

	if tdpur051.cono = s.old.cono then
		return(s.new.cono)
	else
		s.old.cono = tdpur051.cono
		s.new.cono = ""
	endif

	if tdpur051.cono <> 0 then
		s.new.cono = convert.number(bcckon.act.pur.contr, tdpur051.cono)
	endif

	return(s.new.cono)
}

function domain bcorno get.sales.order.number()
{
	static	domain	tcorno		s.old.sorn
	static	domain	bcorno		s.new.sorn fixed

	if tdpur051.sorn = s.old.sorn then
		return(s.new.sorn)
	else
		s.old.sorn = tdpur051.sorn
		s.new.sorn = ""
	endif

	if tdpur051.sorn <> 0 then
		s.new.sorn = convert.number(bcckon.act.sls, tdpur051.sorn)
	endif

	return(s.new.sorn)
}

function domain bcorno get.production.order.number()
{
	static	domain	tcpdno		s.old.pdno
	static	domain	bcorno		s.new.pdno fixed

	if tdpur051.pdno = s.old.pdno then
		return(s.new.pdno)
	else
		s.old.pdno = tdpur051.pdno
		s.new.pdno = ""
	endif

	if tdpur051.pdno <> 0 then

		s.new.pdno = convert.number(bcckon.act.sfc, tdpur051.pdno)
	endif

	return(s.new.pdno)
}

function domain bcorno get.service.order.number()
{
	static	domain	tcorno		s.old.srvo
	static	domain	bcorno		s.new.srvo fixed

	if tdpur051.srvo = s.old.srvo then
		return(s.new.srvo)
	else
		s.old.srvo = tdpur051.srvo
		s.new.srvo = ""
	endif

	if tdpur051.srvo <> 0 then
		s.new.srvo = convert.number(bcckon.sma.order, tdpur051.srvo)
	endif

	return(s.new.srvo)
}

function long get.record.type()
{
	if tdpur051.ckor = 0 then
		return(target(tdsls.ckor.intake))
	endif

	return(tdpur051.ckor)
}

function domain bcstc.bpid get.order.line.business.partner()
{
	static	domain	tcsuno		s.old.suno
	static	domain	bcstc.bpid	s.new.suno fixed

	if tdpur051.suno = s.old.suno then
		return(s.new.suno)
	else
		s.old.suno = tdpur051.suno
		s.new.suno = ""
	endif

	s.new.suno = get.business.partner.code(
				tdpur051.suno,
				bcstc.orgn.suno,
				g.error.message)	|* ref

	return(s.new.suno)
}

function domain tcmcs.str2 get.acknowledgement.code()
{
	static	domain	tcmcs.str2	s.acknowl.code fixed
	static	domain	tcorno		s.order
	static	domain	tcpono		s.position

	if s.order = tdpur051.orno and
	   s.position = tdpur051.pono and
	   ((tdpur704.date = tdpur051.trdt and
	     tdpur704.time <= tdpur051.trtm) or
	    tdpur704.date < tdpur051.trdt) then
		|* Last retrieved record is also actual for this
		|* request.
		return(s.acknowl.code)
	else
		s.order = tdpur051.orno
		s.position = tdpur051.pono
		s.acknowl.code = ""
	endif

	|* Only the value is read that is needed for conversion; when
	|* there are more than one records present for the order
	|* line, then the latest record is taken that is created
	|* before the order line history transaction date.

	select	tdpur704.akcd:s.acknowl.code,
		tdpur704._index1
	from	tdpur704
	where	tdpur704._index1 = {	:tdpur051.orno,
					:tdpur051.pono}
	and	((tdpur704.date = :tdpur051.trdt and
		  tdpur704.time <= :tdpur051.trtm) or
		 tdpur704.date < :tdpur051.trdt)
	order by tdpur704._index1 desc
	as set with 1 rows
	selectdo
	endselect

	return(s.acknowl.code)
}

function long get.origin()
{
	on case tdpur050.corg
	case tdpur.corg.pcs:
		return(target(tdpur.corg.mrp))
	case tdpur.corg.mps:
		return(target(tdpur.corg.mrp))
	default:
		return(etol(tdpur050.corg))
	endcase
}

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 g.item.type <> tckitm.purchase and
	   g.item.type <> tckitm.manufacture then
		return("")
	endif

	if s.project = tdpur051.cprj and
	   s.warehouse = tdpur051.cwar then
		return(s.new.warehouse)
	else
		s.project = tdpur051.cprj
		s.warehouse = tdpur051.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(tdpur051.cprj) and
	   project.is.tp.project(tdpur051.cprj) then
		return(s.new.warehouse)
	endif

	s.new.warehouse = tdpur051.cwar

	return(s.new.warehouse)	
}

function long get.inventory.handling()
{
	on case tdpur051.btsp
	case tcbtsp.bookto.main:
		return(target(tcinup.by.main.item))
	case tcbtsp.bookto.comp:
		return(target(tcinup.by.component))
	default:
		return(target(tcinup.by.main.item))
	endcase
}

function long get.vendor.rating.flag()
{
	if tcmcs042.reto = tcyesno.yes then
		return(2)	|* No
	endif

	if tdpst000.surl = tcyesno.no then
		return(2)	|* No
	endif

	return(1)		|* Yes
}

function domain bcstd.fent get.contract.purchase.office()
{
	static	domain	bcstd.fent	s.contract.pur.office fixed
	static	domain	tccomp		s.company
			long		l.ret

	if s.company = tccom020.comp then
		return(s.contract.pur.office)
	else
		s.company = tccom020.comp
		s.contract.pur.office = ""
	endif

	if tdpur051.cono = 0 then
		return(s.contract.pur.office)
	endif

	if tccom020.comp <> 0 then
		l.ret = get.enterprise.unit(
				tccom020.comp,
				get.compnr(),
				g.error.message)	|* ref
		s.contract.pur.office = bcstc018.pofd
	else
		s.contract.pur.office = bcstd000.pofc
	endif

	return(s.contract.pur.office)
}

function string get.current.planned.receipt.date()
{
	if tdpur051.ckor <> 3 then
		|* Not a Turnover history record.
		return(utc(tdpur051.ddtb, 0))
	endif

	if tdpur051.ddtc <> 0 then
		return(utc(tdpur051.ddtc, 0))
	else
		return(utc(tdpur051.ddta, 0))
	endif
}

|function string get.actual.receipt.date()			|#211817.so
|{
|	if tdpur051.ckor = 3 then
|		|* Turnover history record.
|		return(utc(tdpur051.ddtb, 0))
|	else
|		return(utc(0, 0))
|	endif
|}							|#211817.eo

function string get.actual.receipt.date()			|#211817.sn
{
		domain	tcdate		receipt.date
		domain	tcorno		bind.orno
		domain	tcpono		bind.pono
		domain	tcpono		bind.srnb
	static	domain	tcorno		s.order
	static	domain	tcpono		s.position

	if get.record.type() = 1 and
	   (tdpur051.orno <> s.order or
	    tdpur051.pono <> s.position) then
		s.order = tdpur051.orno
		s.position = tdpur051.pono

		select	tdpur045.date:receipt.date,
			tdpur045.orno:bind.orno,
			tdpur045.pono:bind.pono,
			tdpur045.srnb:bind.srnb
		from	tdpur045
		where	tdpur045._index1 = {	:tdpur051.orno,
						:tdpur051.pono}
		order by tdpur045._index1 desc
		as set with 1 rows
		selectdo
			return(utc(receipt.date, 0))
		selectempty
			select	tdpur051.ddtb:receipt.date
			from	tdpur051
			where	tdpur051._index1 = {	:tdpur051.orno,
							:tdpur051.pono}
			and	tdpur051.ckor = 3
			as set with 1 rows
			selectdo
				return(utc(receipt.date, 0))
			endselect
		endselect
	else
		if tdpur051.ckor = 3 then
			|* Turnover history record.
			return(utc(tdpur051.ddtb, 0))
		endif
	endif

	return(utc(0, 0))
}							|#211817.en

function domain bcstc.item get.item()
{
	static	domain	tccprj		s.project
	static	domain	tcittb		s.opol
	static	domain	tcitem		s.item
	static	domain	bcstc.item	s.segmented.item

	if s.item = tdpur051.item and
	   s.project = tdpur051.cprj and
	   s.opol = tdpur051.opol then
		return(s.segmented.item)
	else
		s.item = tdpur051.item
		s.project = tdpur051.cprj
		s.segmented.item = ""
	endif

	if tdpur051.opol = tcittb.customized then
		s.segmented.item = segment.item.code(
					tdpur051.item,
					get.project(tdpur051.cprj))
	else
		s.segmented.item = segment.item.code(
					tdpur051.item,
					"")
	endif

	return(s.segmented.item)
}

function domain tcqrd1 convert.quantity.from.purchase.unit.to.inventory.unit(
		domain	tcqrd1		i.quantity.in.order.unit)
{
	return(itccom0007.qty.domain.round(
			i.quantity.in.order.unit * tdpur051.cvqp,
			"tcqrd1",
			g.inventory.unit))
}

function domain tcqrd1 get.approved.quantity()
{
	|* In case of return orders the approved quantity is filled
	|* in tdpur051.quad.
	if tcmcs042.reto = tcyesno.yes then
		|* Return order
		return(convert.quantity.from.purchase.unit.to.inventory.unit(
				tdpur051.quad))
	else
		return(convert.quantity.from.purchase.unit.to.inventory.unit(
				tdpur051.quap))
 	endif
}

function domain tcqrd1 get.rejected.quantity()
{
	|* In case of return orders the approved quantity is filled
	|* in tdpur051.quad, so return zero for the rejected quantity
	|* in that case.
	if tcmcs042.reto = tcyesno.yes then
		|* Return order
		return(0.0)
	else
		return(convert.quantity.from.purchase.unit.to.inventory.unit(
				tdpur051.quad))
	endif
}

function domain tccdis get.return.reason.for.rejection()
{
			long		l.ret

	if get.rejected.quantity() = 0 then
		return("")
	endif

	|* Make bcstc017 record current.
	l.ret = get.reason.for.rejection(
			bcreje.tcmcs005,
			tdpur051.cdis(1,1),
			g.error.message)	|* ref

	return(bcstc017.newr)
}

function domain tcyesno generate.freight.order()
{
	if tccom999.trtr = tcyesno.no or
	   tdpur000.agto.4 = tdsls.acpd.no then
		return(tcyesno.no)
	endif

	if isspace(tdpur051.cwar) then
		return(tcyesno.no)
	endif

	if tcmcs042.coun = tcyesno.yes or
	   tcmcs042.drct = tcyesno.yes then
		return(tcyesno.no)
	endif

	if g.item.type <> tckitm.purchase and
	   g.item.type <> tckitm.manufacture then
		return(tcyesno.no)
	endif

	if activity.in.order.procedure("trtoc2201m000") then
		return(tcyesno.yes)
	endif

	return(tcyesno.no)
}

function domain tcbool activity.in.order.procedure(
		domain	tcsess		i.activity)
{
	static	domain	tccotp		s.order.type
	static	domain	tcsess		s.activity
	static	domain	tcbool		s.order.step.present
		domain	bcstc.lng2	l.order.step.number
			long		l.act.no

	if s.order.type = tdpur050.cotp and
	   s.activity = i.activity then
		return(s.order.step.present)
	else
		s.order.type = tdpur050.cotp
		s.activity = i.activity
		s.order.step.present = false
	endif

	select  tcmcs039.stno:l.order.step.number
	from    tcmcs039
	where   tcmcs039._index1 = {:i.activity}
	as set with 1 rows
	selectdo
		for l.act.no = 1 to 20
			if tcmcs042.stno(l.act.no) = 0 then
				|* No more activities defined for the
				|* order type -> stop checking order
				|* type.
				break
			endif
			if tcmcs042.stno(l.act.no) = l.order.step.number then
				|* Activity present in order
				|* procedure.
				s.order.step.present = true
				break
			endif
		endfor
	endselect

	return(s.order.step.present)
}

function string get.planned.load.date()
{
	if generate.freight.order() = tcyesno.yes then
		return(utc(tdpur051.odat, 0))
	endif

	return(utc(0, 0))
}

function long get.price.origin()
{
	on case tdpur051.plev
	case tdpur.plev.contract:
		return(target(tdgen.porg.contract))
|	case tdpur.plev.supl.item:				|#800-141295.o
	case tdpur.plev.plst.item:
		return(target(tdgen.porg.price.structure))
	case tdpur.plev.supl.item:				|#800-141295.n
	case tdpur.plev.ilst:
		return(target(tdgen.porg.supp.pr.book))
	case tdpur.plev.item:
		return(target(tdgen.porg.item.pur))
	case tdpur.plev.not.def:
		return(target(tdgen.porg.not.applicable))
	default:
		break
	endcase

	return(target(tdgen.porg.manual))
}

function domain bcstr9 get.price.matrix.definition()
{
	static	domain	tdpur.plev	s.price.level
	static	domain	bcstr9		s.matrix.definition fixed
			long		l.ret

	if s.price.level = tdpur051.plev then
		return(s.matrix.definition)
	else
		s.price.level = tdpur051.plev
		s.matrix.definition = ""
	endif

	l.ret = get.price.matrix.definition.pur(
			tdpur051.plev,
			s.matrix.definition,	|* ref
			g.error.message)	|* ref

	return(s.matrix.definition)
}

function long get.discount.origin(
			long		i.element)
{
	if tdpur051.pric = 0 then
		return(target(tdgen.dorg.not.applicable))
	endif

	if tdpur051.disc(i.element) = 0 and			|#800-141028.sn
	   tdpur051.ldam(i.element) = 0 then
		return(target(tdgen.dorg.not.applicable))
	endif							|#800-141028.en

	on case tdpur051.dlev(i.element)
	case tdpur.dlev.contract:
		return(target(tdgen.dorg.contract))

	case tdpur.dlev.supp:
	case tdpur.dlev.supp.item:
	case tdpur.dlev.supp.pgrp:
	case tdpur.dlev.plst:
	case tdpur.dlev.plst.item:
	case tdpur.dlev.plst.pgrp:
	case tdpur.dlev.ilst:					|#800-141028.n
		return(target(tdgen.dorg.disc.structure))
|	case tdpur.dlev.ilst:					|#800-141028.so
|		return(target(tdgen.dorg.disc.pr.book))		|#800-141028.eo
	case tdpur.dlev.not.def:
		return(target(tdgen.dorg.not.applicable))
	default:
		break 
	endcase

	return(target(tdgen.dorg.manual))
}

function long get.discount.matrix.type(
			long		i.element)
{
	if tdpur051.dlev(i.element) = tdpur.dlev.contract or
	   tdpur051.dlev(i.element) = tdpur.dlev.manual or
	   tdpur051.dlev(i.element) = tdpur.dlev.not.def then
		return(target(tdpcg.maty.none))
	endif

	if tdpur051.pric = 0 then
		return(target(tdpcg.maty.none))
	endif

	if tdpur051.disc(i.element) = 0 and			|#800-141028.sn
	   tdpur051.ldam(i.element) = 0 then
		return(target(tdpcg.maty.none))
	endif							|#800-141028.en

	return(target(tdpcg.maty.poldsc))
}

function domain bcstr9 get.discount.matrix.definition(
				long		i.element)
{
	static	domain	tdpur.dlev	s.discount.level
	static	domain	bcstr9		s.matrix.definition fixed
			long		l.ret

	if s.discount.level = tdpur051.dlev(i.element) then
		return(s.matrix.definition)
	else
		s.discount.level = tdpur051.dlev(i.element)
		s.matrix.definition = ""
	endif

	if tdpur051.pric = 0 then
		return(s.matrix.definition)
	endif

	if tdpur051.disc(i.element) = 0 and			|#800-141028.sn
	   tdpur051.ldam(i.element) = 0 then
		return(s.matrix.definition)
	endif							|#800-141028.en

	l.ret = get.discount.matrix.definition.pur(
			tdpur051.dlev(i.element),
			s.matrix.definition,	|* ref
			g.error.message)	|* ref

	return(s.matrix.definition)
}

function domain bcglco get.general.ledger.code()
{
	if isspace(tdpur051.pacn) then
		return("")
	endif

	return(transform.leac.and.dim.in.gl.code(
		tdpur051.pacn,
		tdpur051.dim1,
		tdpur051.dim2,
		tdpur051.dim3,
		tdpur051.dim4,
		tdpur051.dim5))
}

function domain tcccty get.tax.country()
{
		domain	tcccty		l.new.country
		domain	tccvat		l.dummy.tax

	get.tax.fields(
		l.new.country,		|* ref
		l.dummy.tax)		|* ref

	return(l.new.country)
}

function domain tccvat get.tax.code()
{
		domain	tcccty		l.dummy.country
		domain	tccvat		l.new.tax

	get.tax.fields(
		l.dummy.country,	|* ref
		l.new.tax)		|* ref

	return(l.new.tax)
}

function get.tax.fields(
		ref	domain	tcccty		o.new.country,
		ref	domain	tccvat		o.new.code)
{
	static	domain	tcccty		s.tax.country
	static	domain	tccvat		s.tax.code
	static	domain	tcccty		s.new.country
	static	domain	tccvat		s.new.code
			long		l.ret

	if tdpur051.ccty = s.tax.country and
	   tdpur051.cvat = s.tax.code then
		o.new.country = s.new.country
		o.new.code = s.new.code
		return
	else
		s.tax.country = tdpur051.ccty
		s.tax.code = tdpur051.cvat
		s.new.country = ""
		s.new.code = ""
	endif

	l.ret = get.tax.by.country.code(
			tdpur051.ccty,
			tdpur051.cvat,
			o.new.country,		|* ref
			o.new.code,		|* ref
			g.error.message)	|* ref

	s.new.country = o.new.country
	s.new.code = o.new.code
}

function domain bccadr get.business.partner.address()
{

	|* Expl. : Use the function to convert purchase order
	|*	   ship-from BP address.
	|* Pre	 : tdpur050 fields current
	|* Return: Converted BP address

		domain	bcstc.keys	l.key fixed
	static	domain	tcorno		s.order
	static	domain	tcccor		s.header.address
	static	domain	bccadr		s.address fixed

	|* To prevent determining the addres multiple times for 1
	|* order.
	if s.order = tdpur051.orno and
	   s.header.address = tdpur050.ccor then
		return(s.address)
	else
		s.order = tdpur051.orno
		s.header.address = tdpur050.ccor
		s.address = ""
	endif


	|* Initialization.
	l.key = ""

	|* 1. Get postal address from purchase order header.
	if not isspace(tdpur050.ccor) then
		l.key(1;6) = tdpur051.suno
		l.key(7;3) = tdpur050.ccor

		s.address = get.address.code(
				"tccom022",
				l.key,
				g.error.message)	|* ref
		return(s.address)
	endif

	|* 2. Get specific postal address.
	|* Maybe the specific address is still present in tdpur042
	|* for the order in history.
	select	tdpur042._index1
	from	tdpur042
	where	tdpur042._index1 = {	:tdpur051.orno,
					0,	|* Position number
					1}	|* Type: postal address
	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,
				g.error.message)	|* ref
		return(s.address)
	endselect

	|* 3. Get postal address from supplier.
	if not isspace(tccom020.ccor) then
		l.key(1;6) = tdpur051.suno 
		l.key(7;3) = tccom020.ccor 
		s.address = get.address.code(
				"tccom022",
				l.key,
				g.error.message)	|* ref
		return(s.address)

	endif

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

function domain bcstc.ccnt get.business.partner.contact()
{
	static	domain	bcstc.ccnt	s.contact fixed
	static	domain	tcsuno		s.pur051.suno
			long		l.ret


	if s.pur051.suno = tdpur051.suno then
		return(s.contact)
	else
		s.pur051.suno = tdpur051.suno
		s.contact = ""
	endif

	l.ret = get.cross.reference.for.contact.code(
			"tccom020a",
			tdpur051.suno,
			g.error.message)		|* ref
	s.contact = bcstc005.ccnt
	|* If no contact is found in bcstc005, bcstc005.ccnt will be
	|* cleared.

	return(s.contact)
}

function domain bccadr get.receipt.address()
{
	|* Expl. : Use the function to convert purchase order line
	|* 	   receipt address.
	|* Return: Converted receipt address

		domain	bcstc.keys	l.key fixed
		domain	tcncmp		l.current.company
	static	domain	tcorno		s.order
	static	domain	tcpono		s.position
	static	domain	tccwar		s.warehouse
	static	domain	bccadr		s.address fixed

	|* To prevent determining the addres multiple times for 1
	|* order.
	if s.order = tdpur051.orno and
	   s.position = tdpur051.pono and
	   s.warehouse = tdpur051.cwar then
		return(s.address)
	else
		s.order = tdpur051.orno
		s.position = tdpur051.pono
		s.warehouse = tdpur051.cwar
		s.address = ""
	endif

	|* Initialization.
	l.key = ""

	|* 1. Get specific receipt address.
	|* Maybe the specific address is still present in tdpur042
	|* for the order in history.
	select	tdpur042._index1
	from	tdpur042
	where	tdpur042._index1 = {	:tdpur051.orno,
					:tdpur051.pono,
					0}	|* Type: receipt address
	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,
				g.error.message)	|* ref
		return(s.address)
	endselect

	|* 2. Get receipt address from warehouse.
	if not isspace(tdpur051.cwar) then
		s.address = get.address.code(
				"tcmcs003",
				tdpur051.cwar,
				g.error.message)	|* ref
		return(s.address)
	endif

	|* 3. Get receipt address from company.
	l.current.company = get.compnr()			|#800-141909.sn
	l.key(1;3) = edit$(l.current.company, "999")
	s.address = get.address.code(
			"tccom000",
			l.key,
			g.error.message)			|#800-141909.en

	return(s.address)
}

function domain tcpmsk get.procedure.mask()
{
		domain	bcstc.lng2	i
		domain	tcsess		l.old.activity
		domain	bcstc.lng2	l.new.pmsk.pos
	static	domain	tcpmsk		s.procedure.mask
	static	domain	tcpmsk		s.old.pmsk
	static	domain	tccotp		s.order.type
	static	domain	tccwar		s.warehouse
	static	domain	tcyesno		s.inspection

	if s.order.type = tdpur050.cotp and
	   s.old.pmsk = tdpur051.pmsk and
	   s.warehouse = tdpur051.cwar and
	   s.inspection = tdpur051.qual then
		return(s.procedure.mask)
	else
		s.order.type = tdpur050.cotp
		s.old.pmsk = tdpur051.pmsk
		s.warehouse = tdpur051.cwar
		s.inspection = tdpur051.qual
		s.procedure.mask = "nnnnnnnnnnnnnnnnnnnn"
	endif

	for i = 1 to 20
		if tdpur051.pmsk(i;1) = "n" then

			continue
		endif

		|* Get session code for procedure mask position.
		l.old.activity = g.old.session.code.activities.array(1,i)

		on case strip$(l.old.activity)
		case "tdpur4401m000":
			|* Print Order
			l.new.pmsk.pos = get.new.pmsk.position("tdpur4401m000")
			if l.new.pmsk.pos <> 0 then
				s.procedure.mask(l.new.pmsk.pos;1) =
					tdpur051.pmsk(i;1)
			endif
			break
		case "tdpur4120m000":
			|* Receipt
			if tdpur051.qual = tcyesno.no then
				l.new.pmsk.pos =
					get.new.pmsk.position("tdpur4501m300")
				if l.new.pmsk.pos <> 0 then
					s.procedure.mask(l.new.pmsk.pos;1) =
						tdpur051.pmsk(i;1)
				endif
			endif
			break
		case "tdpur4121m000":
			|* Approval
			l.new.pmsk.pos = get.new.pmsk.position("tdpur4501m300")
			if l.new.pmsk.pos <> 0 then
				s.procedure.mask(l.new.pmsk.pos;1) =
					tdpur051.pmsk(i;1)
			endif
			break
 	 	case "tdpur4420m000":
			|* Print Claim
			if tcmcs042.coun = tcyesno.no and
			   tcmcs042.reto = tcyesno.no then
				l.new.pmsk.pos =
					get.new.pmsk.position("tdpur4420m000")
				if l.new.pmsk.pos <> 0 then
					s.procedure.mask(l.new.pmsk.pos;1) =

						tdpur051.pmsk(i;1)
				endif
			endif
			break
 	 	case "tdpur4411m000":
			|* Print Return Note
			if tcmcs042.coun = tcyesno.no and
			   tcmcs042.reto = tcyesno.no then
				l.new.pmsk.pos =
					get.new.pmsk.position("tdpur4411m000")
				if l.new.pmsk.pos <> 0 then
					s.procedure.mask(l.new.pmsk.pos;1) =
						tdpur051.pmsk(i;1)
				endif
			endif
			break
 	 	case "tdpur4404m000":
			|* Print Invoice
			l.new.pmsk.pos = get.new.pmsk.position("tdpur4404m000")
			if l.new.pmsk.pos <> 0 then
				s.procedure.mask(l.new.pmsk.pos;1) =
					tdpur051.pmsk(i;1)
			endif
			break
 	 	case "tdpur4223m000":
			|* Process Delivered Order
			l.new.pmsk.pos = get.new.pmsk.position("tdpur4223m000")
			if l.new.pmsk.pos <> 0 then
				s.procedure.mask(l.new.pmsk.pos;1) =
					tdpur051.pmsk(i;1)
			endif
			break

		endcase
	endfor

	|* Release to Warehousing
	if not isspace(tdpur051.cwar) then
		l.new.pmsk.pos = get.new.pmsk.position("tdpur4246m000")
		if l.new.pmsk.pos <> 0 then
			s.procedure.mask(l.new.pmsk.pos;1) = "e"

		endif
	endif

	|* For collect orders the Receipt step is not present in
	|* BaanIV, but must be present in Baan 6.
	l.new.pmsk.pos = get.new.pmsk.position("tdpur4501m300") |#800-141852.sn
	if l.new.pmsk.pos <> 0 and
	   s.procedure.mask(l.new.pmsk.pos;1) =	"n" then
		if tdpur051.quap = 0 then
			|* When approved quantity is 0, then assume
			|* that no receipt is done yet (actual
			|* receipt date is not stored in tdpur051).
			s.procedure.mask(l.new.pmsk.pos;1) = "y"
		else
			s.procedure.mask(l.new.pmsk.pos;1) = "e"
		endif
	endif							|#800-141852.en

	return(s.procedure.mask)
}

function fill.old.session.code.activities.array()
{
		domain	tcsess		l.old.session.code.activity
			long		i

	for i = 1 to 20
		select	tcmcs039.sess:l.old.session.code.activity
		from	tcmcs039
		where	tcmcs039._index2 = {	tcmodc.pur,
						:i}
		as set with 1 rows
		selectdo
		selectempty
			l.old.session.code.activity = ""
		endselect

		g.old.session.code.activities.array(1,i) =
					l.old.session.code.activity
	endfor
}

function domain	bcstc.lng2 get.new.pmsk.position(
			domain	tcsess		i.new.activity)
{
	|* This function returns the new activity number for an
	|* activity. If the activity is not part of the order type
	|* the new activity number is 0.

		domain	bcstc.lng2	l.new.activity.number
		domain	bcstc.lng2	l.position
	
	l.new.activity.number = 0

	for l.position = 1 to 20
		if g.new.session.code.position.array(1,l.position) =
					strip$(i.new.activity) then
			l.new.activity.number = l.position
			break
		endif
	endfor

	return(l.new.activity.number)
}

function domain bcstd.acti get.order.activity()
{
	static	domain	bcstd.acti	s.order.activity fixed
	static	domain	tcpmsk		s.procedure.mask
	static	domain	tccotp		s.order.type

		domain	tcpmsk		l.procedure.mask
		domain	bcstc.lng2	l.step.number

	l.procedure.mask = get.procedure.mask()

	if s.procedure.mask = l.procedure.mask and
	   s.order.type = tdpur050.cotp then
		return(s.order.activity)
	else
		s.procedure.mask = l.procedure.mask
		s.order.type = tdpur050.cotp
		s.order.activity = ""
	endif

	l.step.number = pos(l.procedure.mask, "y")

	if l.step.number = 0 then
		s.order.activity = "tdpur4223m000"
	else
		s.order.activity =
			g.new.session.code.position.array(1,l.step.number)
	endif

	return(s.order.activity)
}


|* End of script **************************************************************
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:000
:FUNC:START
|* 800-139471.sn
function long	get.process.quot.to.order.if.shortage()
{
	if tdsls000.proc.1 = tdsls.stock.no then
	|* If the inventory check during processing is no on the source
	|* the Process quotation to sales order in case of inventory
	|* shortage is set to no, even if the tdsls000.prch.1 on the source is yes.
		return(target(tcyesno.no))
	else
		if tdsls000.prch.1 = tcyesno.no then
			return(target(tcyesno.no))
		endif
	endif
	return(target(tcyesno.yes))
}

function long get.recalculation.of.additional.cost()
{
	on case tdsls000.arac.4
	case tdsls.acpd.no:
		return(target(tdsls.acpd.no))
		break
	case tdsls.acpd.automatic:
		return(target(tdsls.acpd.automatic))
		break
	case tdsls.acpd.interactive:
		return(target(tdsls.acpd.interactive))
		break
	endcase

	return(target(tdsls.acpd.no))
}


|* 800-139471.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:094
:FUNC:START
function domain	tcyesno get.allow.purchase.order()
{	
	if tcmcs042.coun = tcyesno.yes then
		|* For collect orders purchase orders are not allowed.
		return(tcyesno.no)
	else
		return(tcyesno.yes)
	endif
}

function long get.return.order.type()
{
	if tcmcs042.reto = tcyesno.yes then
		return(target(tdpur.reto.yes))
	endif
	|* In all other cases return: no return order type
	return(target(tdpur.reto.no))
}

						|#211099.sn
function read.sales.order.parameter()	
{
	select 	tdsls000.crlb.4
	from	tdsls000
	selectdo
	endselect
}
						|#211099.en
function long get.credit.check()			|#211099.sn
{
	if tcmcs042.reto = tcyesno.yes then
		return(target(tcyesno.no))
	endif

	if tdsls000.crlb.4 = tcyesno.yes then
		return(target(tcyesno.yes))
	endif
	return(target(tcyesno.no))
}
						|#211099.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:100
:FUNC:START
function extern long convert.shipping.constraint(domain tcyesno	i.source.field)
{
|* Expl	: This function converts the ship complete field in the shipping constraint field.
|*	   Ship complete is yes on the source corresponds with Ship complete
|*	   on the target side (value = 4).  Ship complete is no on the source side
|*	   corresponds with no shipping constraints on the target side (value = 1)
|* Pre	: na
|* Post	: na
|* Input	: i.source.field -> Ship complete value

|* Output: na
|* Return: Converted shipping constraint

	if i.source.field = tcyesno.yes then
		return(target(tdscon.oc))
	else
		return(target(tdscon.none))

	endif
}

function extern domain tcbool freight.invoice.records.present()
{
|* Expl	: This function checks if there are records in table trtrc140 maintain rate codes
|*	   by customer. If there are no records present then this table does not needed
|*	   to be read for each order and fields tdsls100.frin and tdsls100.infr can be filled
|*	   with default values.
|*	   If records exists in this table then for each order this table needs to be checked
|*	  if for the customer a record exists. If so then fields tdsls100.frin = yes and
|*	  tdsls100.infr = client rates
|* Pre	: na
|* Post	: na
|* Input	: na
|* Output: na
|* Return: False -> no records present otherwise true

	if tccom999.trtr = tcyesno.yes then
	   select	trtrc140.cuno
	   from	trtrc140
	   as set with 1 rows
	   selectdo
		return(true)
	   endselect
	endif
	return( false)
}

function	set.freight.invoice.fields()
{
|* Expl	: This function is called when records in table trtrc140-maintain rate codes
|*	   by customer, are present.
|*	   If records exists in this table then for each order this table needs to be checked
|*	  if for the sales customer a record exists. If so then fields tdsls100.frin = yes and
|*	  tdsls100.infr = client rates if not the fields will be filled with default values.
|* 	  var.tdsls100.frin and var.tdsls100.infr are set.
|*	  for cost and return orders the default values are used.
|* Pre	: Sales customer known
|* Post	: na
|* Input	: na
|* Output: na
|* Return: na

	if g.use.freight.invoice.defaults then
	   read.order.type(tdsls001.cotp)
	   if tcmcs042.reto = tcyesno.no and
	      tcmcs042.sund = tcyesno.no then
		select	trtrc140.cuno
		from	trtrc140
		where	trtrc140.cuno = :tdsls001.cuno
		as set with 1 rows
		selectdo
			var.tdsls100.frin = target(tcyesno.yes)
			var.tdsls100.infr = target(tccom.infr.estimate)
		endselect
	   endif
	endif
}


function read.order.type(domain tccotp i.order.type)
{
	static domain	tccotp		s.order.type

	|* To prevent too much read actions.
	if s.order.type = i.order.type then
		return
	else
		s.order.type = i.order.type
	endif

	|* Only the values are read that are needed for conversion.
	select	tcmcs042.sund,
		tcmcs042.reto
	from	tcmcs042
	where	tcmcs042._index1 = {:i.order.type}
	as set with 1 rows
	selectdo
	selectempty
		tcmcs042.sund = tcyesno.no
		tcmcs042.reto = tcyesno.no
	endselect
}


function domain bccadr convert.address(
		domain	tcccor		i.source.address,
		domain	tccuno		i.source.customer,
		domain	tcorno		i.order,


		domain	tcpono		i.position,
		domain  tcckor		i.address.type)
{

	domain	bcstc.keys	l.key		fixed
	domain	tcmcs.s200m	l.errmess

	|* Initialize
	l.key = ""

	|* 1. Get postal or delivery address from quotation header
	if not isspace(i.source.address) and i.address.type = 0 then
		|* get delivery addres
		l.key ( 1;6 ) = i.source.customer ( 1;6 )
		l.key ( 7;3 ) = i.source.address ( 1;3 )
		return(get.address.code(
			"tccom013",
			l.key,
			l.errmess))	|ref
	endif
	if not isspace(i.source.address) and i.address.type = 1 then
		|* get postal addres
		l.key ( 1;6 ) = i.source.customer ( 1;6 )
		l.key ( 7;3 ) = i.source.address ( 1;3 )
		return(get.address.code(
			"tccom012",
			l.key,
			l.errmess))	|ref
	endif

	|* 2. Get specific sales quotation address.
	select	tdsls003._index1
	from	tdsls003
	where	tdsls003._index1 = {	:i.order,
					:i.position,
					:i.address.type}
	as set with 1 rows
	selectdo
		l.key(1;6)  = edit$(tdsls003.qono,"999999")
		l.key(7;4)  = edit$(tdsls003.pono,"9999")
		l.key(11;2) = edit$(tdsls003.ckor,"99")
		return(get.address.code(
			"tdsls003",
			l.key,
			l.errmess))	|ref
	endselect

	|* 3. Get postal/delivery address from Customer.
	if i.address.type = 0 then
		|* Get Delivery address from customer
		select	tccom010.cdel
		from	tccom010
		where	tccom010._index1 = {	:i.source.customer}
		and	tccom010.cdel <> ""
		as set with 1 rows
		selectdo
			l.key(1;6) = i.source.customer
			|* Delivery addres -> use cdel
			l.key(7;3) = tccom010.cdel
			return(get.address.code(
			"tccom013",
			l.key,
			l.errmess))	|ref
		endselect
	else
		|* Get Postal address from customer
		select	tccom010.ccor
		from	tccom010
		where	tccom010._index1 = {	:i.source.customer}
		and	tccom010.ccor <> ""
		as set with 1 rows
		selectdo
			l.key(1;6) = i.source.customer
			|* Postal address -> use ccor
			l.key(7;3) = tccom010.ccor
			return(get.address.code(
			"tccom012",
			l.key,
			l.errmess))	|ref
		endselect
	endif

	|* 4. Get address from supplier.
	return(get.address.code(
			"tccom010",
			i.source.customer,
			l.errmess))	|ref
}


function domain bcstd.fent get.sales.office()
{
| *  make bcstc018 current to get sales office (bcstc018.sofd).
| *  retrieves office based upon Financial company and logistic company

	long ret
	static domain tcncmp	f.comp
	static domain tcncmp 	l.comp

	if f.comp <> tdsls001.comp or
	   l.comp <> get.compnr() then
		f.comp = tdsls001.comp
		l.comp = get.compnr()
 		ret = get.enterprise.unit(tdsls001.comp,
				l.comp,
				o.errmess)
	endif
	return(bcstc018.sofd)
}

function string get.planned.delivery.date()
{
	if tdsls001.ddat = 0 then
		return(date.to.utc(tdsls001.qdat, TIME.MIN))
	endif

	return(date.to.utc(tdsls001.ddat, tdsls001.dtme))
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:150
:FUNC:START
function extern long convert.shipping.constraint(domain tcyesno	i.source.field)
{
|* Expl	: This function converts the ship complete field in the shipping constraint field.
|*	   Ship complete is yes on the source corresponds with Ship complete 
|*	   on the target side (value = 4).  Ship complete is no on the source side
|*	   corresponds with no shipping constraints on the target side (value = 1)
|* Pre	: na
|* Post	: na
|* Input	: i.source.field -> Ship complete value

|* Output: na
|* Return: Converted shipping constraint

	if i.source.field = tcyesno.yes then 
		return(target(tdscon.oc))
	else 
		return(target(tdscon.none))

	endif 
}

function extern domain tcbool freight.invoice.records.present()
{
|* Expl	: This function checks if there are records in table trtrc140 maintain rate codes 
|*	   by customer. If there are no records present then this table does not needed
|*	   to be read for each order and fields tdsls150.frin and tdsls150.infr can be filled
|*	   with default values.
|*	   If records exists in this table then for each order this table needs to be checked
|*	  if for the customer a record exists. If so then fields tdsls150.frin = yes and
|*	  tdsls150.infr = client rates
|* Pre	: na
|* Post	: na
|* Input	: na
|* Output: na
|* Return: False -> no records present otherwise true

	if tccom999.trtr = tcyesno.yes then
	   select	trtrc140.cuno
	   from	trtrc140
	   as set with 1 rows
	   selectdo
		return(true)
	   endselect
	endif
	return( false)
}

function	set.freight.invoice.fields()
{
|* Expl	: This function is called when records in table trtrc140-maintain rate codes 
|*	   by customer, are present. 
|*	   If records exists in this table then for each order this table needs to be checked
|*	  if for the sales customer a record exists. If so then fields tdsls100.frin = yes and
|*	  tdsls150.infr = client rates if not the fields will be filled with default values.
|* 	  var.tdsls150.frin and var.tdsls150.infr are set.
|*	  for cost and return orders the default values are used.
|* Pre	: Sales customer known
|* Post	: na
|* Input	: na
|* Output: na
|* Return: na

	if g.use.freight.invoice.defaults then
	   read.order.type(tdsls010.cotp)
	   if tcmcs042.reto = tcyesno.no and
	      tcmcs042.sund = tcyesno.no then
		select	trtrc140.cuno
		from	trtrc140
		where	trtrc140.cuno = :tdsls010.cuno
		as set with 1 rows
		selectdo
			var.tdsls150.frin = target(tcyesno.yes)
			var.tdsls150.infr = target(tccom.infr.estimate)	
		endselect
	   endif	
	endif	
}


function read.order.type(domain tccotp i.order.type)
{
	static domain	tccotp		s.order.type

	|* To prevent too much read actions.
	if s.order.type = i.order.type then
		return
	else
		s.order.type = i.order.type
	endif

	|* Only the values are read that are needed for conversion.
	select	tcmcs042.sund,
		tcmcs042.reto
	from	tcmcs042
	where	tcmcs042._index1 = {:i.order.type} 
	as set with 1 rows 
	selectdo 
	selectempty 
		tcmcs042.sund = tcyesno.no
		tcmcs042.reto = tcyesno.no
	endselect 
}


function domain bccadr convert.address(
		domain	tcccor		i.source.address,
		domain	tccuno		i.source.customer,
		domain	tcorno		i.order,

		domain	tcpono		i.position,
		domain  tcckor		i.address.type)
{

	domain	bcstc.keys	l.key		fixed
	domain	tcmcs.s200m	l.errmess

	|* Initialize
	l.key = ""

	|* 1. Get postal or delivery address from quotation header
	if not isspace(i.source.address) and i.address.type = 0 then
		|* get delivery addres
		l.key ( 1;6 ) = i.source.customer ( 1;6 )
		l.key ( 7;3 ) = i.source.address ( 1;3 )
		return(get.address.code(
			"tccom013",
			l.key,
			l.errmess))	|ref
	endif
	if not isspace(i.source.address) and i.address.type = 1 then
		|* get postal addres
		l.key ( 1;6 ) = i.source.customer ( 1;6 )
		l.key ( 7;3 ) = i.source.address ( 1;3 )
		return(get.address.code(
			"tccom012",
			l.key,
			l.errmess))	|ref
	endif

	|* 2. Get specific sales quotation address.
	select	tdsls003._index1
	from	tdsls003
	where	tdsls003._index1 = {	:i.order,
					:i.position,
					:i.address.type}
	as set with 1 rows
	selectdo
		l.key(1;6)  = edit$(tdsls003.qono,"999999")
		l.key(7;4)  = edit$(tdsls003.pono,"9999")
		l.key(11;2) = edit$(tdsls003.ckor,"99")
		return(get.address.code(
			"tdsls003",
			l.key,
			l.errmess))	|ref
	endselect

	|* 3. Get postal/delivery address from Customer.
	if i.address.type = 0 then
		|* Get Delivery address from customer
		select	tccom010.cdel
		from	tccom010
		where	tccom010._index1 = {	:i.source.customer}
		and	tccom010.cdel <> ""
		as set with 1 rows
		selectdo
			l.key(1;6) = i.source.customer
			|* Delivery addres -> use cdel
			l.key(7;3) = tccom010.cdel
			return(get.address.code(
			"tccom013",
			l.key,
			l.errmess))	|ref
		endselect
	else
		|* Get Postal address from customer
		select	tccom010.ccor
		from	tccom010
		where	tccom010._index1 = {	:i.source.customer}
		and	tccom010.ccor <> ""
		as set with 1 rows
		selectdo
			l.key(1;6) = i.source.customer
			|* Postal address -> use ccor
			l.key(7;3) = tccom010.ccor
			return(get.address.code(
			"tccom012",
			l.key,
			l.errmess))	|ref
		endselect
	endif

	|* 4. Get address from supplier.
	return(get.address.code(
			"tccom010",
			i.source.customer,
			l.errmess))	|ref
}


function domain bcstd.fent get.sales.office()
{
| *  make bcstc018 current to get sales office (bcstc018.sofd).  
| *  retrieves office based upon Financial company and logistic company  

	long ret
	static domain tcncmp	f.comp
	static domain tcncmp 	l.comp

	if f.comp <> tdsls010.comp or
	   l.comp <> get.compnr() then
		f.comp = tdsls010.comp
		l.comp = get.compnr()
 		ret = get.enterprise.unit(tdsls010.comp, 
				l.comp, 
				o.errmess)  
	endif
	return(bcstc018.sofd) 
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:300
:FUNC:START
|******************************************************************************
|* Script: tdsls300 - functions
|* Author: Evert-Jan Bosch
|******************************************************************************

function domain bccadr get.bp.address()
{
|* Expl	 : Use the function to convert sales contract BP addresses.
|* Pre	 : tdsls300 present
|* Return: Converted BP address

		domain	bcstc.keys	l.key	fixed
	static	domain	tccono		s.contract
	static	domain	bccadr		s.address

	|* To prevent determining the addres multiple times for 1 contract.
	if s.contract = tdsls300.cono then
		return(s.address)
	else
		s.contract = tdsls300.cono
		s.address = ""
	endif

	|* Initialization.
	l.key = ""

	|* 1. Get postal address from sales contract header
	if not isspace(tdsls300.ccor) then
		l.key(1;6) = tdsls300.cuno
		l.key(7;3) = tdsls300.ccor
		s.address = get.address.code(
			"tccom012",
			l.key,
			error.message)
		return(s.address)
	endif

	|* 2. Get specific address.
	select	tdsls303._index1
	from	tdsls303
	where	tdsls303._index1 = {	:tdsls300.cono,
					1}	|* Type: postal address
	as set with 1 rows
	selectdo
		l.key(1;6) = edit$(tdsls303.cono,"999999")
		l.key(7;2) = edit$(tdsls303.ckor,"99")
		s.address = get.address.code(
			"tdsls303",
			l.key,
			error.message)
		return(s.address)
	endselect

	|* 3. Get postal address from customer.
	read.customer()
	if not isspace(tccom010.ccor) then
		l.key(1;6) = tdsls300.cuno
		l.key(7;3) = tccom010.ccor
		s.address = get.address.code(
			"tccom012",
			l.key,
			error.message)
		return(s.address)
	endif

	|* 4. Get address from customer.
	s.address = get.address.code(
		"tccom010",
		tdsls300.cuno,
		error.message)

	return(s.address)
}

function domain bccadr get.ship.to.address()
{
		domain	bcstc.keys	l.key	fixed

	|* Initialize
	l.key = ""

	|* 1. Get delivery address from contract header
	if not isspace(tdsls300.cdel) then
		l.key(1;6) = tdsls300.cuno
		l.key(7;3) = tdsls300.cdel
		return(get.address.code(
				"tccom013",
				l.key,
				error.message))
	endif

	|* 2. Get specific sales contract delivery address.
	select 	 tdsls303._index1
	from 	 tdsls303
	where 	 tdsls303._index1 = {:tdsls300.cono, 0}
	as set with 1 rows
	selectdo
		l.key(1;6) = edit$(tdsls303.cono,"999999")
		l.key(7;2) = edit$(tdsls303.ckor,"99")
			return(get.address.code(
					"tdsls303",
					l.key,
					error.message))
	endselect

	|* 3. Get default delivery address from customer.
	select 	 tccom010.cdel
	from 	 tccom010
	where 	 tccom010._index1 = {:tdsls300.cuno}
	and 	 tccom010.cdel <> ""
	as set with 1 rows
	selectdo
		l.key(1;6) = tdsls300.cuno
		l.key(7;3) = tccom010.cdel
			return(get.address.code(
					"tccom013",
					l.key,
					error.message))
	endselect

	|* 4. Get address from customer.
	return(get.address.code(
			"tccom010",
			tdsls300.cuno,
			error.message))
}

function domain bcstc.ccnt get.contact()
{
	static	domain	bcstc.ccnt	s.contact
	static	domain	tccuno		s.customer

	if s.customer = tdsls300.cuno then
		return(s.contact)
	endif

	s.contact = ""
	s.customer = tdsls300.cuno

	if get.cross.reference.for.contact.code(
			"tccom010a",
			tdsls300.cuno,
			error.message) = 0 then
		s.contact = bcstc005.ccnt
	endif

	return(s.contact)
}

function domain bcstd.fent get.sales.office()
{
	read.customer()
	if tccom010.comp <> 0 then
		e = get.enterprise.unit(
			tccom010.comp,
			get.compnr(),
			error.message)
		return(bcstc018.sofd)
 	endif

	e = read.parameters.distribution(error.message)
	return(bcstd000.sofc)
}

function read.customer()
{
	static	domain	tccuno		s.customer

	if s.customer = tdsls300.cuno then
		return
	else
		s.customer = tdsls300.cuno
	endif

	select	tccom010.ccor,
		tccom010.comp
	from	tccom010
	where	tccom010._index1 = {	:tdsls300.cuno}
	as set with 1 rows
	selectdo
	endselect
}

function domain tcyesno get.tax.flag()
{
	if not isspace(tdsls300.cuno) then
		return(get.tax.flag.via.bp(
				tdsls300.cuno,
				bcstc.orgn.cuno,
				error.message))
	endif

	return(tcyesno.no)
}

|* End of script **************************************************************
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:350
:FUNC:START
function string convert.sales.contract()
{
	static		string	s.contract(9)	fixed
	static	domain	tccono	s.sls305.cono

	if s.sls305.cono <> tdsls305.cono then
		s.sls305.cono = tdsls305.cono
		s.contract = convert.number(
				bcckon.act.sls.contr,
				tdsls305.cono)
	endif

	return(s.contract)
}

function domain bcstc.bpid convert.business.partner()
{
	static	domain	bcstc.bpid	s.business.part	fixed
	static	domain	tccuno		s.sls305.cuno

	if s.sls305.cuno <> tdsls305.cuno then
		s.sls305.cuno = tdsls305.cuno
		s.business.part = get.business.partner.code(
					tdsls305.cuno,
					bcstc.orgn.cuno,
					g.error.mess)
	endif

	return(s.business.part)
}

function domain bccadr convert.address()
{
	domain	bcstc.keys	l.key		fixed

	|* Initialize
	l.key = ""

	|* 1. Get address from contract header
	if not isspace(tdsls305.cdel) then
		l.key(1;6) = tdsls305.cuno
		l.key(7;3) = tdsls305.cdel
		return(get.address.code(
				"tccom013",
				l.key,
				g.error.mess))
	endif

	|* 2. Get specific sales contract delivery address.
	select	tdsls303._index1
	from	tdsls303
	where	tdsls303._index1 = {:tdsls305.cono, 0}
	as set with 1 rows
	selectdo
		l.key(1;6) = edit$(tdsls303.cono,"999999")
		l.key(7;2) = edit$(tdsls303.ckor,"99")
		return(get.address.code(
				"tdsls303",
				l.key,
				g.error.mess))
	endselect

	|* 3. Get delivery address from customer.
	select	tccom010.cdel
	from	tccom010
	where	tccom010._index1 = {:tdsls305.cuno}
	and	tccom010.cdel <> ""
	as set with 1 rows
	selectdo
		l.key(1;6) = tdsls305.cuno
		l.key(7;3) = tccom010.cdel
		return(get.address.code(
				"tccom013",
				l.key,
				g.error.mess))
	endselect

	|* 4. Get address from customer.
	return(get.address.code(
				"tccom010",
				tdsls305.cuno,
				g.error.mess))
}

function domain bcstd.fent convert.sales.office()
{
		long		ret
	static	domain	tccuno	s.sls305.cuno
	static	domain	tcbool	s.fin.comp.defined

	if s.sls305.cuno <> tdsls305.cuno then
		s.sls305.cuno = tdsls305.cuno
		select	tccom010.comp
		from	tccom010
		where	tccom010._index1 = {:tdsls305.cuno}
		and 	tccom010.comp <> 0
		as set with 1 rows
		selectdo
			s.fin.comp.defined = true
			ret = get.enterprise.unit(
					tccom010.comp,
					get.compnr(),
					g.error.mess)
			return(bcstc018.sofd)
		selectempty
			s.fin.comp.defined = false
		endselect
	else
		if s.fin.comp.defined then
			return(bcstc018.sofd)
		endif
	endif

	return(bcstd000.sofc)
}

function long convert.tdsls350.idtr()
{
	if tdsls305.idtr = empty then
		return(target(tcidtr.modify))
	endif

	return(etol(tdsls305.idtr))
}

function long convert.tdsls350.icyp()
{
	if tdsls305.icyp = empty then
		return(target(tcicyp.year))
	endif

	return(etol(tdsls305.icyp))
}

function long convert.tdsls350.cvyn()
{
	domain	tcyesno	taxyesnoflag

	taxyesnoflag = get.tax.flag.via.bp(
				tdsls305.cuno,
				bcstc.orgn.cuno,
				g.error.mess)

	return(etol(taxyesnoflag))
}

function long convert.tdsls350.icap()
{
	if tdsls305.icap = empty then
		return(target(tcicap.free))
	endif

	return(etol(tdsls305.icap))
}

function long convert.tdsls350.csts()
{
	if tdsls305.csts = empty then
		return(target(tccsts.not.printed))
	endif

	return(etol(tdsls305.csts))
}

function long convert.tdsls350.icpr()
{
	if tdsls305.icpr = empty then
		return(target(tcicpr.print))
	endif

	return(etol(tdsls305.icpr))
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:351
:FUNC:START
function read.sales.contract.header.history()
{
	static	domain	tccono	s.sls306.cono

	if s.sls306.cono = tdsls306.cono then
		return
	endif

	s.sls306.cono = tdsls306.cono
	select	tdsls305._index1,
		tdsls305.cuno, tdsls305.cdel, tdsls305.icap,
		tdsls305.sdat, tdsls305.edat
	from	tdsls305
	where	tdsls305._index1 = {:tdsls306.cono}
	order by tdsls305._index1 desc
	as set with 1 rows
	selectdo
	endselect
}

function string convert.sales.contract()
{
	static		string	s.contract(9)	fixed
	static	domain	tccono	s.sls306.cono

	if s.sls306.cono <> tdsls306.cono then
		s.sls306.cono = tdsls306.cono
		s.contract = convert.number(
				bcckon.act.sls.contr,
				tdsls306.cono)
	endif

	return(s.contract)
}

function domain bcstc.bpid convert.business.partner()
{
	static	domain	bcstc.bpid	s.business.part	fixed
	static	domain	tccuno		s.sls306.cuno

	if s.sls306.cuno <> tdsls306.cuno then
		s.sls306.cuno = tdsls306.cuno
		s.business.part = get.business.partner.code(
					tdsls306.cuno,
					bcstc.orgn.cuno,
					g.error.mess)
	endif

	return(s.business.part)
}

function domain bccadr convert.address()
{
	domain	bcstc.keys	l.key		fixed

	|* Initialize
	l.key = ""

	|* 1. Get address from contract header
	if not isspace(tdsls305.cdel) then
		l.key(1;6) = tdsls306.cuno
		l.key(7;3) = tdsls305.cdel
		return(get.address.code(
				"tccom013",
				l.key,
				g.error.mess))
	endif

	|* 2. Get specific sales contract delivery address.
	select	tdsls303._index1
	from	tdsls303
	where	tdsls303._index1 = {:tdsls305.cono, 0}
	as set with 1 rows
	selectdo
		l.key(1;6) = edit$(tdsls303.cono,"999999")
		l.key(7;2) = edit$(tdsls303.ckor,"99")
		return(get.address.code(
				"tdsls303",
				l.key,
				g.error.mess))
	endselect

	|* 3. Get delivery address from customer.
	select	tccom010.cdel
	from	tccom010
	where	tccom010._index1 = {:tdsls306.cuno}
	and	tccom010.cdel <> ""
	as set with 1 rows
	selectdo
		l.key(1;6) = tdsls306.cuno
		l.key(7;3) = tccom010.cdel

		return(get.address.code(
				"tccom013",
				l.key,
				g.error.mess))
	endselect

	|* 4. Get address from customer.
	return(get.address.code(
				"tccom010",
				tdsls306.cuno,
				g.error.mess))
}

function domain bcstd.fent convert.sales.office()
{
		long		ret
	static	domain	tccomp	s.sls306.comp

	if s.sls306.comp <> tdsls306.comp then
		s.sls306.comp = tdsls306.comp
		ret = get.enterprise.unit(
					tdsls306.comp,
					get.compnr(),
					g.error.mess)
	endif

	return(bcstc018.sofd)
}

function convert.tax()
{
			long	l.ret
	static	domain	tcccty	s.sls306.ccty	fixed
	static	domain	tccvat	s.sls306.cvat	fixed

	if s.sls306.ccty <> tdsls306.ccty or
	   s.sls306.cvat <> tdsls306.cvat then
		l.ret = get.tax.by.country.code(
					tdsls306.ccty,
					tdsls306.cvat,
					var.tdsls351.ccty,
					var.tdsls351.cvat,
					g.error.mess)
		s.sls306.ccty = tdsls306.ccty
		s.sls306.cvat = tdsls306.cvat
	endif
}

function long convert.tdsls351.sdsc()
{
	if tdsls306.sdsc = empty then
		return(target(tcyesno.yes))
	endif

	return(etol(tdsls306.sdsc))
}

function long convert.tdsls351.icyp()
{
	if tdsls306.icyp = empty then
		return(target(tcicyp.year))
	endif

	return(etol(tdsls306.icyp))
}

function long convert.tdsls351.iqab()
{
	if tdsls306.iqab = empty then
		return(target(tcyesno.no))
	endif

	return(etol(tdsls306.iqab))
}

function long convert.tdsls351.pbor()
{
	if tdsls306.iprl = tcyesno.yes then
		return(target(tdpcg.pbor.socn))
	endif

	return(target(tdpcg.pbor.stnd))
}

function long convert.tdsls351.ieva()
{
	if tdsls306.ieva = empty then
		return(target(tcieva.no))
	endif

	return(etol(tdsls306.ieva))
}

function long convert.tdsls351.iprl()
{
	if tdsls306.iprl = empty then
		return(target(tcyesno.yes))
	endif

	return(etol(tdsls306.iprl))
}

function long convert.tdsls351.idil()
{
	if tdsls306.idil = empty then
		return(target(tcyesno.yes))
	endif

	return(etol(tdsls306.idil))
}

function long convert.tdsls351.idtr()
{
	if tdsls306.idtr = empty then
		return(target(tcidtr.modify))
	endif

	return(etol(tdsls306.idtr))
}

function long convert.tdsls351.icap()
{
	if tdsls305.icap = empty then
		return(target(tcicap.free))
	endif

	return(etol(tdsls305.icap))
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:401
:FUNC:START
function read.sales.order.and.order.type.data()
{
	static	domain	tcorno	s.current.order

	if s.current.order <> tdsls045.orno then
		s.current.order = tdsls045.orno
		select	tdsls040.orno, tdsls040.cper, tdsls040.comp,
			tdsls040.cfrw, tdsls040.scom, tdsls040.fcrt,
			tdsls040.crte, tdsls040.ccur, tdsls040.cotp,
			tdsls040.ddat, tdsls040.odat,
			tdsls040.bkyn,				|#Sp1.n
			tdsls040.cdel,				|#209434.n
			tcmcs042.reto, tcmcs042.sund, tcmcs042.casi,
			tcmcs042.stno, tcmcs042.modc,
			tcmcs002.crnd
		from	tdsls040, tcmcs002, tcmcs042
		where	tdsls040._index1 = {:tdsls041.orno}
		and	tcmcs042.cotp = tdsls040.cotp
		and	tcmcs002.ccur = tdsls040.ccur
		as set with 1 rows
		selectdo
		endselect
	endif
}

								|#143063.sn
function location.controlled.implemented()
{
	g.ilc.implemented = false
	select 	 tdilc000.iilc
	from 	 tdilc000
	as set with 1  rows
	selectdo
	endselect
	if tdilc000.iilc =  tcyesno.yes then
		g.ilc.implemented = true
	endif
}
								| # 143063.en
								| # 143063.sn
function migrate.outbound.data.to.sales.order.deliveries()
{

	|* Initialize for each sales order line sequence
	g.create.expected.delivery.line.for.tdilc401 = false
	g.no.of.outbound.advices.per.sls.sequence = 0
	g.outbound.advice.record = 0

	|* Count all outbound advices for sales order line sequence
	select	count(*):g.no.of.outbound.advices.per.sls.sequence
	from	tdilc401
	where	tdilc401._index1 = {	tdilc.koob.act.sls,
					:tdsls045.orno,
					:tdsls045.pono,
					:tdsls045.srnb}
	and	tdilc401.item = :tdsls045.item
	selectdo
	endselect

	if g.no.of.outbound.advices.per.sls.sequence >= 1 then
		|* Outbound advice present. Need to use tdilc401 data
		|* to create sales order line deliveries
		select	tdilc401.*
		from	tdilc401
		where	tdilc401._index1 = {	tdilc.koob.act.sls,
						:tdsls045.orno,
						:tdsls045.pono,
						:tdsls045.srnb}
		and	tdilc401.proc = tcyesno.yes
		and	tdilc401.item = :tdsls045.item
		selectdo
		|* Loop through all tdilc records present for the current
		|* sales order line sequence.
		|* for each delivered tdilc401 record a sales order line
		|* record on the target side is required.
		|* Per delivered outbound advice a new outbound advice
		|* sequence will be determined. 1 for the first delivered
		|* advice, 2 for the second, 3 for the third, etc.
		|* This is required because there can be a few outbound
		|* advices but the tdilc401.serb start from 100, 101,102
		|* tdilc401.serb can be set manually and is not generated
		|* by the system.
		|* The outbound advice record nr handled is stored in
		|* variable: g.outbound.advice.record
			g.outbound.advice.record = g.outbound.advice.record + 1
			dump1.and.dump2.for.migrating.outbound()
			 |* Insert an Actual Delivery record for each delivered
			 |* outbound advice.
		 	 dump.actual.delivery()			|#SP1.n
|*		 	 dump 3 | *  tdsls406 Actual delivery	|#SP1.o
		selecteos
			if g.outbound.advice.record <
				g.no.of.outbound.advices.per.sls.sequence then
				|* More not yet delivered advices present
				|* Create new sequence nr for the expected
				|* ordered qty
				g.outbound.advice.record =
						g.outbound.advice.record + 1
				g.create.expected.delivery.line.for.tdilc401 = true
				dump1.and.dump2.for.migrating.outbound()
				|* Expected advice not yet delivered thus no dump 3
			endif
		selectempty
			|* No delivered sales order advices present. But one (or
			|* more) not delivered advices present. One new sequence
			|* will be created for all the not delivered advices
			|* present. g.outbound.advice.record will be set to one
			|* done because WH and TD needs to create same sqnb.
			|* Data of the sales order line can be used.
			g.outbound.advice.record = 1
			g.create.expected.delivery.line.for.tdilc401 = true
			dump1.and.dump2.for.migrating.outbound()
			|* Expected advice not yet delivered thus no dump 3
		endselect
	else
		|* no advice present no need to use data of the
		|* Outbound advice all data is present on the sales order line.
		g.outbound.advice.record = 1
		g.create.expected.delivery.line.for.tdilc401 = true
		dump1.and.dump2.for.migrating.outbound()
		|* Dump 3 handled in main query
 	endif
}
								|#143063.en
								|#143063.sn
function dump1.and.dump2.for.migrating.outbound()
{
	determine.var.shpm.and.shln()				|#SL204550.n
	dump 1	|*  tdsls401 (sales order lines)
	|* tdsls401 (convert detail or backorder record)
	if convert.linked.order.line.data() then
		dump 2	|* tdsls402 (insert linked order line data record)
	endif
}
								|#143063.en

function domain tcbool total.line.required()
{
	|* This function determines the number of delivery lines
	domain	tcbool	l.total.line.required
	domain	tcsrnb	l.no.of.delivery.lines

	get.total.line.required.and.no.of.delivery.lines(
					l.total.line.required,
					l.no.of.delivery.lines)

	return(l.total.line.required)
}

function domain	tcsrnb	get.no.of.delivery.lines()
{
	domain	tcbool	l.total.line.required
	domain	tcsrnb	l.no.of.delivery.lines

	get.total.line.required.and.no.of.delivery.lines(
					l.total.line.required,
					l.no.of.delivery.lines)
	return(l.no.of.delivery.lines)
}

function get.total.line.required.and.no.of.delivery.lines(
			ref	domain	tcbool	o.total.line.required,
			ref	domain	tcsrnb	o.no.of.delivery.lines)
{
	|* This function determines the number of delivery lines and
	|* determines if a total line is reqiured (true/false)

	static	domain	tcorno	s.order
	static	domain	tcpono	s.position
	static	domain	tcbool	s.total.line.required
	static	domain	tcsrnb	s.no.of.delivery.lines
		domain	tcorno	l.order
		domain	tcpono	l.position
		domain	tcpono	l.sequence
		domain	tcsrnb	l.no.of.delivery.lines
		domain	tcqsl1	l.backorder.quantity

	if s.order = tdsls041.orno and
	   s.position = tdsls041.pono then
		|* Same order line -> early return
		o.total.line.required = s.total.line.required
		o.no.of.delivery.lines = s.no.of.delivery.lines
		return
	else
		s.order = tdsls041.orno
		s.position = tdsls041.pono
		s.total.line.required = false
		s.no.of.delivery.lines = 0
	endif

	select	tdsls045.srnb:l.no.of.delivery.lines,
		tdsls045.bqua:l.backorder.quantity,
		tdsls045.orno:l.order,
		tdsls045.pono:l.position,
		tdsls045.srnb:l.sequence
	from	tdsls045
	where	tdsls045._index1 = {	:tdsls041.orno,
					:tdsls041.pono}
	order by tdsls045._index1 desc
	as set with 1 rows
	selectdo
		if l.no.of.delivery.lines = 0 and
		   double.cmp(l.backorder.quantity, 0.0, 0.0001) = 0 then
		   	o.total.line.required = false
		else
			|* Backorders present in source table tdsls045 or
			|* Backorder expected in source table tdsls045
			|* then create total line in target table tdsls401
			o.total.line.required = true
		endif
	endselect
								|#143063.sn
	if g.ilc.implemented and
	   not o.total.line.required and
	   g.no.of.outbound.advices.per.sls.sequence > 1 and
	   tdsls045.dqua <> 0 then
		|* If only not delivered advice are present no need for a
		|* total line.
		|* If an advice is delivered then dqua is filled on the sales
		|* order line.
		|* Multiple outbound advice leads to multiple sales order
		|* delivery lines and thus requires a total.
		o.total.line.required = true
	endif
								|#143063.en
	o.no.of.delivery.lines = l.no.of.delivery.lines
	s.total.line.required = o.total.line.required
	s.no.of.delivery.lines = o.no.of.delivery.lines
}

function get.total.line.values()
{
	domain	tcbool	l.order.not.complete.delivered
	domain tcstno	l.stno.4223

	domain	tcqsl1	l.delivered.quantity
	domain	tcqsl1	l.ordered.quantity
	domain	tcamnt	l.order.amount
	domain	tcpmsk	l.procedure.mask
	domain	tcqsl1	l.backorder.quantity
	domain	tcdate	l.delivery.date
	domain	tcamnt	l.line.discount.amnt
	domain	tcamnt	l.order.discount.amnt
	domain	tcorno	l.order
	domain	tcpono	l.position
	domain	tcpono	l.sequence

	g.total.oamt = 0.0
	g.total.damt = 0.0
	g.total.amld = 0.0
	g.total.amod = 0.0
	g.total.qoor = 0.0
	g.total.qidl = 0.0
	g.total.qbbo = 0.0
	g.total.dldt = 0
	l.order.not.complete.delivered = false

	l.delivered.quantity 	= 0.0
	l.ordered.quantity   	= 0.0
	l.order.amount       	= 0.0
	l.procedure.mask     	= ""
	l.backorder.quantity 	= 0.0
	l.delivery.date      	= 0
	l.line.discount.amnt	= 0.0
	l.order.discount.amnt	= 0.0

	select	tdsls045.dqua:l.delivered.quantity,
		tdsls045.oqua:l.ordered.quantity,
		tdsls045.amnt:l.order.amount,
		tdsls045.pmsk:l.procedure.mask,
		tdsls045.bqua:l.backorder.quantity,
		tdsls045.ddat:l.delivery.date,
		tdsls045.ldam:l.line.discount.amnt,
		tdsls045.odam:l.order.discount.amnt,
		tdsls045.orno:l.order,
		tdsls045.pono:l.position,
		tdsls045.srnb:l.sequence
	from	tdsls045
	where	tdsls045._index1 = {	:tdsls041.orno,
					:tdsls041.pono}
	order by tdsls045._index1
	selectdo
|		if tdsls045.ddat = 0 then			|#202399.o
		if l.delivery.date = 0 then			|#202399.n
			|* Check if the backorder line should be taken into
			|* account. This is true if the line is not delivered
			|* and process delivered sales order is yes.
			|* Else the sales order line can be skipped for
			|* calculating total values. get next record.
			tdsls0014.session.status("tdsls4223m000", l.stno.4223)
			if l.procedure.mask(l.stno.4223;1) = "e" then
				continue
			endif
			l.order.not.complete.delivered = true
			g.total.dldt = 0
			g.total.qbbo = l.backorder.quantity
			g.total.qoor = g.total.qoor + l.ordered.quantity
			g.total.oamt = g.total.oamt + l.order.amount
			g.total.amld = g.total.amld + l.line.discount.amnt
			g.total.amod = g.total.amod + l.order.discount.amnt
		else
			if not l.order.not.complete.delivered and
			   l.delivery.date > g.total.dldt then
				g.total.dldt = l.delivery.date
			endif

|			if backorder.confirmed() then		|#202399.o
								|#202399.sn
			|* Bind variables passed for evaluation in function.
			if backorder.confirmed(l.backorder.quantity,
					       l.sequence) then
								|#202399.en
				|* When the last sales order line is delivered
				|* but the backorder qty not yet confirmed then
				|* the ordered qty is based upon the ordered
				|* quantity of the sales order line. If the
				|* Back order is confirmed then the ordered qty
				|* is based upon the delivered and backorder
				|* quantity.
				g.total.qoor = g.total.qoor +
|#800-143070.so				       l.delivered.quantity +
|#800-143070.eo				       l.backorder.quantity
|#800-143070.sn
					       l.delivered.quantity
|#800-143070.en
			else
				g.total.qoor = g.total.qoor + l.ordered.quantity
								|#800-143063.sn
				g.total.qbbo = g.total.qbbo + l.backorder.quantity

								|#800-143063.en
			endif
			g.total.oamt = g.total.oamt + l.order.amount
			g.total.damt = g.total.damt + l.order.amount
			g.total.amld = g.total.amld + l.line.discount.amnt
			g.total.amod = g.total.amod + l.order.discount.amnt
		endif
		g.total.qidl = g.total.qidl + l.delivered.quantity
	endselect
}

function domain tcbool convert.linked.order.line.data()
{
	static	domain	tcorno	s.order
	static	domain	tcpono	s.position
	static	domain	tcbool	s.linked.data

	if s.order = tdsls045.orno and
	   s.position = tdsls045.pono then
		return(s.linked.data)
	else
		s.order = tdsls045.orno
		s.position = tdsls045.pono
		s.linked.data = true
	endif

	if tdsls041.porn = 0 and
	   tdsls041.qono = 0 and
	   tdsls045.ddat = 0 and
	   tdsls045.torn = 0 and
	   not purchase.advice.present() then
		s.linked.data = false
	endif
 	return(s.linked.data)
}

function domain tcbool purchase.advice.present()
{
	|* This function checks if there is a purchase order advice.
	|* If an advice is present then linked order line data needs to be
	|* created.

	domain	tcorno	l.planned.purchase.order
	domain	tccwar	l.planned.purchase.order.cwar	fixed

 	l.planned.purchase.order = 0

 	read.purchase.order.advice(
					tdsls045.orno,
					tdsls045.pono,
					l.planned.purchase.order,
					l.planned.purchase.order.cwar)

 	if l.planned.purchase.order <> 0 then
 		return(true)
 	endif

	return(false)
}

function domain bcorno get.purchase.order.advice()
{
	|* The purchase order advice order number is retrieved and coverted
	|* according to the convert number rules.

		domain	tcorno	l.planned.purchase.order
		domain	tccwar	l.planned.purchase.order.cwar		fixed
	static	domain	tcorno	s.planned.purchase.order
	static	domain	bcorno	s.converted.purchase.order.advice	fixed

 	l.planned.purchase.order = 0

  	read.purchase.order.advice(
					tdsls045.orno,
					tdsls045.pono,
					l.planned.purchase.order,
					l.planned.purchase.order.cwar)

	if l.planned.purchase.order = s.planned.purchase.order then
		return(s.converted.purchase.order.advice)
	endif

	s.planned.purchase.order = l.planned.purchase.order
 	if l.planned.purchase.order = 0 then
 		s.converted.purchase.order.advice = ""
 	else
		s.converted.purchase.order.advice = convert.number(
						       bcckon.pur.advice,
						       l.planned.purchase.order)
	endif
	return(s.converted.purchase.order.advice)
}

function read.purchase.order.advice(
			domain	tcorno	i.sales.order,
			domain	tcpono	i.sales.position,
		ref	domain	tcorno	o.planned.purchase.order,
		ref	domain	tccwar	o.planned.purchase.order.cwar)
{
	static	domain	tcorno	s.order
	static	domain	tcpono	s.position
	static	domain	tcorno	s.planned.purchase.order
	static	domain	tccwar	s.planned.purchase.order.cwar	fixed

	if s.order = i.sales.order and
	   s.position = i.sales.position then
		o.planned.purchase.order = s.planned.purchase.order
		o.planned.purchase.order.cwar = s.planned.purchase.order.cwar
		return
	else
		s.order = i.sales.order
		s.position = i.sales.position
		s.planned.purchase.order = 0
		s.planned.purchase.order.cwar = ""
	endif

	select  tdinv310.orno:s.planned.purchase.order,
	 	 tdinv310.cwar:s.planned.purchase.order.cwar
	from 	 tdinv310
	where	 tdinv310._index5 = {:i.sales.order, :i.sales.position}
	as set with 1 rows
	selectdo
	selectempty
	 	s.planned.purchase.order = 0
		s.planned.purchase.order.cwar = ""
	endselect
}

function domain	bcorno	get.order.number()
{
	static	domain	tcorno	s.old.order
	static	domain	bcorno	s.new.order

	if s.old.order <> tdsls045.orno then
		s.old.order = tdsls045.orno
		s.new.order = convert.number(bcckon.act.sls, tdsls045.orno)
	endif
	return(s.new.order)
}

|								|#143063.so
|function domain tcpono get.order.line.sequence()
|{
|	if g.total.line then
|		|* If total line then insert sequence 0
|		return(0)
|	endif
|
|	if not total.line.required() then
|		|* If no backorders are present or expected then insert a
|		|* detail with sequence 0
|		return(tdsls045.srnb)
|	endif
|	|* In all other cases backorders are present and an extra total with
|	|* sequence 0 needs to be inserted. So convert sequence 0 to sequence 1,
|	|*  sequence 1 to sequence 2 etc.
|	return(tdsls045.srnb+1)
|	}
|								|#143063.eo
								|#143063.sn
function domain tcpono get.order.line.sequence()
{
	if g.total.line then
		|* If total line then insert sequence 0
		return(0)
	endif

	if g.ilc.implemented then
		if not total.line.required() then
			|* If only one delivered tdilc401 record is present for
			|* one sales order line sequence then no total required
			|* and the new target seqeuce can be the same as the
			|* source sequence number.
		 	|* If no backorders are present or expected then insert
			|* a detail with sequence 0
			return(tdsls045.srnb)
		endif

		if total.line.required() then
			|* For ILC check tdilc401 for multiple records. For each
			|* delivered tdilc401 record a sales order line delivery
			|* is required. Determine target sequence number using
			|* the tdilc401.serb field
			|* Target sequence has 4 positions: positions 1 and 2
			|* will be used for the sales sequence and the position
			|* 3 and 4 will be used for the outbound sequence number
			|* Assume no more then 99 sales sequences and 99
			|* delivered outbound advices per sales order line.
			return((tdsls045.srnb+1)*100 + g.outbound.advice.record)
		endif
	endif

	|* For non ILC
	if not total.line.required() then
		|* If no backorders are present or expected then insert a
		|* detail with sequence 0
		return(tdsls045.srnb)
	endif
	|* In all other cases backorders are present and an extra total with
	|* sequence 0 needs to be inserted. So convert sequence 0 to sequence 1,
	|*  sequence 1 to sequence 2 etc.
	return(tdsls045.srnb+1)
}
								|#143063.en

function long get.order.line.type()
{
	if g.total.line then
		return(target(tdgen.oltp.total))
	endif

	if tdsls045.srnb > 0 then
		return(target(tdgen.oltp.backorder))
	endif

	|* In all other cases it is just a detail line
	return(target(tdgen.oltp.detail))
}

function long get.delivery.type()
{
	if g.total.line then
		return(target(tdsls.dltp.not.applicable))
	endif

	if tdsls041.drct = tcyesno.yes then
		return(target(tdsls.dltp.direct.delivery))
	endif
	if purchase.advice.present() or
	   tdsls041.porn <> 0 then
		return(target(tdsls.dltp.cross.docking))
	endif

	read.item.data()
	if g.item.type <> tckitm.purchase and
	   g.item.type <> tckitm.manufacture and
	   g.item.type <> tckitm.generic then
		return(target(tdsls.dltp.not.applicable))
	endif
	return(target(tdsls.dltp.warehousing))
}

function read.item.data()
{
	static	domain	tcitem	s.item	fixed

	if s.item <> tdsls045.item then
		s.item = tdsls045.item
		select	tiitm001.kitm:g.item.type,
			tiitm001.stva:g.inventory.valuation
		from	tiitm001
		where	tiitm001._index1 = {	:tdsls045.item}
		as set with 1 rows
		selectdo
		endselect
	endif
}

function domain	bcstc.bpid tdsls401.get.business.partner()
{
	static	domain	tccuno		s.old.customer		fixed
	static	domain	bcstc.bpid	s.new.business.partner	fixed

	if s.old.customer <> tdsls045.cuno then
		s.old.customer = tdsls045.cuno
		s.new.business.partner = get.business.partner.code(
					tdsls045.cuno,
					bcstc.orgn.cuno,
					g.error.message)
	endif
	return(s.new.business.partner)
}

function domain	bccadr get.ship.to.address()
{
	static	domain	tcorno	s.order
	static	domain	tcpono	s.position
	static	domain	bccadr	s.new.address.code	fixed
		domain	tccdel	l.delivery.address		|#209434.n
	

	if s.order = tdsls045.orno and
	   s.position = tdsls045.pono then
       		return(s.new.address.code)
       	else
	       	s.order = tdsls045.orno

	       	s.position = tdsls045.pono
		s.new.address.code = ""
       	endif
       	
       	if isspace(tdsls041.cdel) then				|#209434.sn
       		l.delivery.address = tdsls040.cdel
       	else
       			l.delivery.address = tdsls041.cdel
       	endif							|#209434.en
       	
        s.new.address.code = convert.sales.address(
|        				tdsls041.cdel,		|#209434.o
				        l.delivery.address,	|#209434.n
        				tdsls045.cuno,
        				tdsls045.orno,
        				tdsls045.pono,
        				0)
        return(s.new.address.code)
}

function domain bcstc.ccnt get.ship.to.contact()
{
|* Expl	: This function converts the Sales Order Contact. If the Sales
|*	  contact on the source side is empty then the function returns empty.
|* Pre	: na
|* Post	: na
|* Input	: i.source.customer -> Customer of the order
|*	  i.source.contact -> Tobe converted (source) contact
|* Output: na
|* Return: Converted Sales contact

		domain 	bcstc.keys 	l.contact.key		fixed
	static	domain	bcstc.ccnt	s.converted.contact	fixed
	static	domain	tccuno		s.old.customer		fixed
	static	domain	tdsmi.cper 	s.old.contact		fixed

	if s.old.customer = tdsls045.cuno and
	   s.old.contact = tdsls040.cper then
		return(s.converted.contact)
	else
		s.old.customer = tdsls045.cuno
		s.old.contact = tdsls040.cper
		s.converted.contact = ""
		l.contact.key = ""
	endif

	if not isspace(tdsls040.cper) then
		l.contact.key(1;6) = tdsls045.cuno
		l.contact.key(7;6) = tdsls040.cper


		if get.cross.reference.for.contact.code(
					"tdsmi120",
					l.contact.key,
					g.error.message) = 0 then
			s.converted.contact = bcstc005.ccnt
		endif
	endif
	return(s.converted.contact)
}

function domain bcrtyp get.sales.exchange.rate.type()
{
	static	domain	bcrtyp		s.rate.type	fixed

	if not isspace(s.rate.type) then
		return(s.rate.type)
	endif

	g.ret = get.exchange.rate.type(
		"SLS",
		g.error.message)
	s.rate.type = bcstc011.rtyp

	return(s.rate.type)
}

|function domain tcitem get.item ()				|#202933.o
function domain bcitem get.item ()				|#202933.n
{
 	 if tdsls041.opol = tcittc.customized then
 	 	 return ( segment.item.code (
 	 	 	 	 tdsls045.item,
 	 	 	 	 get.project(tdsls045.cprj)))
 	 else
 	 	 return ( segment.item.code (
 	 	 	 	 tdsls045.item,
 	 	 	 	 "" ))
 	 endif
}

function domain	bccpvl get.product.variant()
{
	static	domain	tccpva	s.old.product.variant	fixed
	static	domain	bccpvl	s.new.product.variant
			long	l.ret

	if isspace(tdsls041.cpva) or
	   g.total.line then
		return(0)
	endif

	if s.old.product.variant <> tdsls041.cpva then
		l.ret = get.new.product.variant(
				tdsls041.cpva,
				g.error.message)
		s.new.product.variant = bcsti001.cpvl
	endif
	return(s.new.product.variant)
}

function long	get.make.customized()
{
	if g.total.line then
		return(target(tcyesno.no))
	endif

	if tdsls041.opol = tcittc.make.customized then
		return(target(tcyesno.yes))
	endif
	return(target(tcyesno.no))
}

								|#Sp1.sn
function long	get.order.line.is.blocked()
{
	|* Blocked flag is not set for total lines
	if g.total.line then
		return(target(tcyesno.no))
	endif

	|* If the order is blocked all lines for this order will be blocked.
	if tdsls040.bkyn = tcyesno.yes then
		return(target(tcyesno.yes))
	endif
	return(target(tcyesno.no))
}
								|#Sp1.en

|*****************************************
|* Determine date fields
|*****************************************
function domain tcdate get.planned.delivery.date.field()
{
	|* If the order is to be shipped completely then the planned delivery
	|* date of the line must be equal to the planned delivery date of the
	|* sales order header (all lines must have the same delivery date)
	|* If the planned delivery date is not set then initialize the
	|* planned delivery date with the order date.
	|* if the planned delivery date is before the order date then set the
	|* planned delivery date equal to the order date.
	|* the function returns the source date field and still needs to be
	|* converted to UTC date.
	if tdsls040.scom = tcyesno.yes then
		return(tdsls040.ddat)
	endif

	if tdsls041.ddta > 0 and
	   tdsls041.ddta > tdsls041.odat then
		return(tdsls041.ddta)
	else
		return(tdsls041.odat)
	endif
}

|function domain tcdate get.actual.delivery.date.field()
|{
|	|* the function returns the source date field and still needs to be
|	|* converted to UTC date.
|	if g.total.line then
|		return(g.total.dldt)
|	else
||		return(tdsls045.ddat)
|		return(get.planned.delivery.date.field())
|	endif
|}

|								|#143063.so
|function string get.actual.delivery.date()
|{
|	if g.total.line then
|		return(utc(g.total.dldt, 0))
|	else
|		return(utc(tdsls045.ddat, 0))
|	endif
|}
|								|#143063.eo
|								|#143063.so
|function string get.actual.delivery.date()
|{
|	if g.total.line then
|		if g.ilc.implemented and
|	   	   g.no.of.outbound.advices.per.sls.sequence > 1 then
|	   	   	|* leave delivery date empty.
|	   	   	|* To determine if the delivery date must for a total
|	   	   	|* line must be filled and how in an ILC situation
|	   	   	|* is a lot of coding. Can be done when customers
|	   	   	|* request for it.
|			return(utc(0,0))
|		else
|			return(get.utc(g.total.dldt))
|		endif
|	else
|		if g.create.expected.delivery.line.for.tdilc401 then
|			|* For the expected sales order delivery line the
|			|* actual delivery date is always zero
|			return(utc(0,0))
|		else
|			return(get.utc(tdsls045.ddat))
|		endif
|	endif
|}
|								|#143063.eo
								|#143063.sn
function string get.actual.delivery.date()
{
	|**************************************
	|* Total line and ILC Implemented
	|**************************************
	if g.total.line and g.ilc.implemented then
		if g.create.expected.delivery.line.for.tdilc401 then
			|* If an expected sales order delivery line is created
			|* then the total line is not fully delivered and thus
			|* the delivery date is set to zero
			return(utc(0,0))
		endif

		|* For all other deliveries set the delivery date equal to the
		|* latest delivery date of the sales order lines (source value).
   	   	|* To determine if the delivery date for a total line must be
   	   	|* filled and how in an ILC situation based upon the outbound
   	   	|* advices is a lot of coding.
   	   	|* Can be done when customers request for it.
		return(get.utc(g.total.dldt))
	endif

	|**************************************
	|* Total line and ILC is not Implemented
	|**************************************
	if g.total.line and not g.ilc.implemented then
		return(get.utc(g.total.dldt))
	endif

	|**************************************
	|* It is not a total line
	|**************************************
	if g.create.expected.delivery.line.for.tdilc401 then
		|* For the expected sales order delivery line the
		|* actual delivery date is always zero
		return(utc(0,0))
	endif

	return(get.utc(tdsls045.ddat))
}
								|#143063.en


|function domain tcdate get.rate.date.field()		|#MCR.so
|{
|	|* the function returns the source date field and still needs to be
|	|* converted to UTC date.
|	if (tdsls040.fcrt = tcfcrt.ddta) and (tdsls041.ddta <> 0) then
|		return(tdsls041.ddta)
|	else
|		return(tdsls040.odat)
|	endif
|}							|#MCR.eo

function string get.release.date()
{
	if isspace(get.warehouse()) then
		return(utc(0,0))
	endif

|	return(utc(get.planned.delivery.date.field(), 0))	|#800-143063.o
	return(get.utc(get.planned.delivery.date.field()))	|#800-143063.n
}

								|#800-143063.so
|function string get.receipt.date()
|{
|	if tdsls041.prdt < tdsls041.ddta then
|		return(utc(get.planned.delivery.date.field(), 0))
||		return(utc(tdsls041.ddta, 0))
|	endif
|	return(utc(tdsls041.prdt, 0))
|}
								|#800-143063.eo
								|#800-143063.sn
function string get.receipt.date()
{
	if tdsls041.prdt < tdsls041.ddta then
		return(get.utc(get.planned.delivery.date.field()))
	endif
	return(get.utc(tdsls041.prdt))
}
								|#800-143063.en
								|#800-143063.sn
function string get.rate.date()
{
|	|* the function returns the source date field and still needs to be
|	|* converted to UTC date.
	if (tdsls040.fcrt = tcfcrt.ddta) and (tdsls041.ddta <> 0) then
		return(get.utc(tdsls041.ddta))
	endif
	return(get.utc(tdsls040.odat))
}
								|#800-143063.en
								|#800-143063.sn
function string get.invoice.date()
{
	return(get.utc(tdsls045.invd))
}
								|#800-143063.en
								|#800-143063.sn
function string get.utc(domain tcdate	i.date.field)
{
	if i.date.field = 0 then
		return(utc(0,0))
	else
		return(date.to.utc(i.date.field, TIME.MIN))
	endif
}
								|#800-143063.en



|*****************************************
|* Determine price/discounts fields
|*****************************************
function domain bcstr9 get.price.matrix.definition()
{
	static	domain	bcstr9	s.matrix.definition	fixed
	static	domain	tdplev	s.price.level

	if not double.cmp(tdsls045.oqua, 0.0, 0.0001) then
		|* If the oqua is zero no price matrix definition -> ""
		return("")
	endif

	if s.price.level <> tdsls041.plev then
		s.price.level = tdsls041.plev
		if get.price.matrix.definition.sls(
				tdsls041.plev,
				s.matrix.definition,
				g.error.message) = 0 then
		endif
	endif
	return(s.matrix.definition)
}

function domain bcstr9 get.disc.matrix.definition(
				long 	 	 i.element )
{
	static	domain	bcstr9	s.discount.matrix.definition	fixed
	static	domain	tddlev	s.discount.level

	if not double.cmp(tdsls045.pric, 0.0, 0.0001) then
		|* If the price is zero no discount matrix definition -> ""
		return("")
	endif

	if not double.cmp(tdsls045.disc(i.element), 0.0, 0.0001) and
	   not double.cmp(tdsls045.damt(i.element), 0.0, 0.0001) then




	   	|* No discount percentage/amount defined.
	   	return("")
	endif

	if s.discount.level <> tdsls041.dlev(i.element) then
		if get.discount.matrix.definition.sls(
				tdsls041.dlev(i.element),
				s.discount.matrix.definition,
				g.error.message) = 0 then
		endif
	endif
	return(s.discount.matrix.definition)
}

function long get.discount.matrix.type(
 	 	 	 long 	 	 i.element )
{
	if not double.cmp(tdsls045.pric, 0.0, 0.0001) then
		|* If the price is zero no discount matrix type -> none
		return(target(tdpcg.maty.none))
	endif

	if not double.cmp(tdsls045.disc(i.element), 0.0, 0.0001) and
	   not double.cmp(tdsls045.damt(i.element), 0.0, 0.0001) then
	   	|* No discount percentage/amount defined.
	   	return(target(tdpcg.maty.none))
	endif


 	 if tdsls041.dlev ( i.element ) = tddlev.contract or
 	 tdsls041.dlev ( i.element ) = tddlev.manual or
 	 tdsls041.dlev ( i.element ) = tddlev.not.def or
 	 tdsls041.disc ( i.element ) <> tdsls045.disc ( i.element ) or
 	 tdsls041.ldam ( i.element ) <> tdsls045.damt ( i.element ) then
 	 	 return ( target ( tdpcg.maty.none ))
 	 endif
 	 return ( target ( tdpcg.maty.soldsc ))
}

function long get.price.origin.sales(domain tdplev	i.price.level)
{
	if double.cmp(tdsls045.oqua, 0.0, 0.0001) = 0 then
		|* If ordered quantity is zero price origin not applicable
		return(target(tdgen.porg.not.applicable))
	endif

	on case i.price.level
	case tdplev.contract:
		return(target(tdgen.porg.contract))
	case tdplev.cust.item:
	case tdplev.plst.item:
		return(target(tdgen.porg.price.structure))
	case tdplev.ilst:
		return(target(tdgen.porg.def.pr.book))
	case tdplev.item:
		return(target(tdgen.porg.item.sls))
	case tdplev.not.def:
		return(target(tdgen.porg.not.applicable))
	default:
		break
	endcase

	return(target(tdgen.porg.manual))
}

function long get.discount.origin.sales(
		ref	domain	tddlev	i.discount.level(),
				long	i.element)
{
	if not double.cmp(tdsls045.pric, 0.0, 0.0001) then
		return(target(tdgen.dorg.not.applicable))
	endif

	if not double.cmp(tdsls045.disc(i.element), 0.0, 0.0001) and
	   not double.cmp(tdsls045.damt(i.element), 0.0, 0.0001) then
	   	|* No discount percentage/amount defined.
	   	return(target(tdgen.dorg.not.applicable))
	endif

	on case i.discount.level(i.element)
	case tddlev.contract:
		return(target(tdgen.dorg.contract))
	case tddlev.cust:
	case tddlev.cust.item:
	case tddlev.cust.pgrp:
	case tddlev.plst:
	case tddlev.plst.item:
	case tddlev.plst.pgrp:
	case tddlev.ilst:
		return(target(tdgen.dorg.disc.structure))
	case tddlev.not.def:
		return(target(tdgen.dorg.not.applicable))
	default:
		break
	endcase

	return(target(tdgen.dorg.manual))
}

function domain tccdsc get.discount.code(long	i.element)
{
	domain	tccdsc	l.discount.code

	if not double.cmp(tdsls045.pric, 0.0, 0.0001) then
		return("")
	endif

	if not double.cmp(tdsls045.disc(i.element), 0.0, 0.0001) and
	   not double.cmp(tdsls045.damt(i.element), 0.0, 0.0001) then
	   	|* No discount percentage/amount defined.
	   	return("")
	endif

	l.discount.code = tdsls041.cdis(1, i.element)
	return(l.discount.code)
}

function long get.discount.method(long	i.element)
{

	if not double.cmp(tdsls045.pric, 0.0, 0.0001) then
		return(target(tddmth.gross))
	endif

	if not double.cmp(tdsls045.disc(i.element), 0.0, 0.0001) and
	   not double.cmp(tdsls045.damt(i.element), 0.0, 0.0001) then
	   	|* No discount percentage/amount defined.
	   	return(target(tddmth.gross))
	endif

	return(etol(tdsls041.dmth(i.element)))
}

function long get.determining.flag()
{
			long		l.disc.lvl
			long		l.array.length

	l.array.length = tdsls.dll0022.array.length()
	for l.disc.lvl = 1 to l.array.length
		if tdsls041.dlev(l.disc.lvl) = tddlev.cust.pgrp then
			read.disc.parameters.by.customer.and.price.group(
								l.disc.lvl)
			return(etol(tdsls034.stsa))
		endif
		if tdsls041.dlev(l.disc.lvl) = tddlev.plst.pgrp then
			read.disc.parameters.by.price.list.and.price.group(
								l.disc.lvl)
			return(etol(tdsls035.stsa))
		endif
	endfor

	return(target(tcyesno.no))
}

function long get.eligible.flag()
{
			long		l.disc.lvl
			long		l.array.length

	l.array.length = tdsls.dll0022.array.length()
	for l.disc.lvl = 1 to l.array.length
		if tdsls041.dlev(l.disc.lvl) = tddlev.cust.pgrp then
			read.disc.parameters.by.customer.and.price.group(
								l.disc.lvl)
			return(etol(tdsls034.stsb))
		endif
		if tdsls041.dlev(l.disc.lvl) = tddlev.plst.pgrp then
			read.disc.parameters.by.price.list.and.price.group(
								l.disc.lvl)
			return(etol(tdsls035.stsb))

		endif
	endfor

	return(target(tcyesno.no))
}

function read.disc.parameters.by.customer.and.price.group(
			domain	tddile		i.discount.level)
{
	|* This function reads the discount parameter by customer price group


	static	domain	tcsuno		s.supplier



	static	domain	tcitem		s.item			fixed
	static	domain	tddile		s.discount.level

	if s.supplier = tdsls045.cuno and
	   s.item = tdsls041.item and
	   s.discount.level = i.discount.level then
		return
	else
		s.supplier = tdsls045.cuno
		s.item = tdsls041.item
		s.discount.level = i.discount.level
	endif

	get.item.inventory.unit.and.price.group()
	select	tdsls034.stsa, tdsls034.stsb
	from	tdsls034
	where	tdsls034._index1 = {	:tdsls045.cuno,
					:g.price.group,
					:i.discount.level}
	as set with 1 rows
	selectdo
	endselect
}

function read.disc.parameters.by.price.list.and.price.group(
			domain	tddile		i.discount.level)
{
	|* This function reads the discount parameter by
	|* price list and price group

		domain	tccplt		l.price.list
	static	domain	tcorno		s.order
	static	domain	tcitem		s.item			fixed
	static	domain	tddile		s.discount.level

	if s.order = tdsls045.orno and
	   s.item = tdsls041.item and
	   s.discount.level = i.discount.level then
		return
	else
		s.order = tdsls045.orno
		s.item = tdsls041.item
		s.discount.level = i.discount.level
	endif

	get.item.inventory.unit.and.price.group()
	l.price.list = get.price.list()
	select	tdsls035.stsa, tdsls035.stsb
	from	tdsls035
	where	tdsls035._index1 = {	:l.price.list,
					:g.price.group,
					:i.discount.level}
	as set with 1 rows
	selectdo
	endselect
}

function domain tccplt get.price.list()
{
	if not isspace(tdsls040.cpls) then
		return(tdsls040.cpls)
	endif

	if not isspace(tdsls040.pcpr) then
		read.customer(tdsls040.pcpr)
	else
		read.customer(tdsls040.cuno)
	endif

	return(tccom010.cpls)
}

function read.customer(
		domain	tccuno	i.get.customer)
{
	static	domain	tccuno		s.customer

	if s.customer = i.get.customer then
		return
	else
		s.customer = i.get.customer
	endif

	select	tccom010.cpls, tccom010.comp
	from	tccom010
	where	tccom010._index1 = {:i.get.customer}
	as set with 1 rows


	selectdo
	endselect
}

function long get.price.changes.allowed.after.delivery()
{
	if tdsls045.ddat <> 0 and
	   tdsls045.invn = 0 then
		return(target(tcyesno.yes))
	endif
	return(target(tcyesno.no))
}

function domain tcpric get.custom.value()
{
	|* Currently the custom value functionality is not yet fully implemented
	|* on SSA ERP ln 6.1
	|* Function will be used in a later version and for this version always
	|* 0.0 will be returned
	return(0.0)

|	if g.total.line then
|		return(0.0)
|	endif
|
|	if tdsls045.oqua > 0 then
|		return(tdsls041.pric)
|	else
|		return(0.0)
|	endif
}

function domain bcstd.fent get.contract.sales.office()
{
	static	domain	tcncmp		s.financial.company
	static	domain	bcstd.fent	s.contract.sales.office	fixed

	if tdsls045.cono = 0 then
		return("")
	endif

	read.customer(tdsls045.cuno)

	if s.financial.company = tccom010.comp then
		return(s.contract.sales.office)
	else
		s.financial.company = tccom010.comp
		s.contract.sales.office = ""
	endif

	if tccom010.comp <> 0 then
		g.ret = get.enterprise.unit(
			tccom010.comp,
			get.compnr(),
			g.error.message)
		s.contract.sales.office = bcstc018.sofd
	else
		s.contract.sales.office = bcstd000.sofc
	endif

	return(s.contract.sales.office)
}

function domain	bcorno	get.contract()
{
	static	domain	tcorno	s.old.contract
	static	domain	bcorno	s.new.contract	fixed

	if s.old.contract <> tdsls045.cono then
		s.old.contract = tdsls045.cono
		if tdsls045.cono <> 0 then
			s.new.contract = convert.number(
						bcckon.act.sls.contr,
						tdsls045.cono)
		else
			s.new.contract = ""
		endif
	endif
	return(s.new.contract)
}

function domain	tccvat get.tax()
{
	domain	tcccty	l.new.country	fixed
	domain	tccvat	l.new.tax	fixed

	get.tax.and.country(l.new.country, l.new.tax)
	return(l.new.tax)
}

function domain	tcccty get.tax.country()
{
	domain	tcccty	l.new.country	fixed
	domain	tccvat	l.new.tax	fixed

	get.tax.and.country(l.new.country, l.new.tax)
	return(l.new.country)
}

function get.tax.and.country(
		ref	domain	tcccty	o.new.country,
		ref	domain	tccvat	o.new.tax)
{

	static	domain tcccty	s.old.country	fixed
	static	domain tccvat	s.old.tax	fixed
	static	domain tcccty	s.new.country	fixed
	static	domain tccvat	s.new.tax	fixed

	if s.old.country = tdsls041.ccty and
	   s.old.tax = tdsls041.cvat then
		o.new.country = s.new.country
		o.new.tax = s.new.tax
		return
	else
		s.old.country = tdsls041.ccty
		s.old.tax = tdsls041.cvat
	endif

	g.ret = get.tax.by.country.code(
				tdsls041.ccty,
				tdsls041.cvat,
				s.new.country,
				s.new.tax,
				g.error.message)
	o.new.country = s.new.country
	o.new.tax = s.new.tax
}

|*****************************************
|* Determine logistic fields
|*****************************************
function domain tccwar	get.warehouse()
{
	if tdsls041.drct = tcyesno.yes then
		return("")
	else
		return(tdsls045.cwar)
	endif
}

function long get.inventory.handling()
{
	if tdsls041.isss = tcisss.issue.comp then
		return(target(tcinup.by.component))
	endif
	|* In all other cases issue by main item
	return(target(tcinup.by.main.item))
}

function long get.lot.selection()
{
	|* If lot control is lot by unit then lot is converted into a serial
	|* Serial will get lot code, serial selection is specific
	|* Lot is made empty and lot selection is any

	if isspace(get.warehouse()) then
		return(target(tclsel.any))
	endif

	if item.is.serialized() or
	   g.total.line then
		return(target(tclsel.any))
	endif
	return(etol(tdsls041.lsel))
}

function domain tdltc.clot get.lot()
{
	|* See comments for function get.lot.selection()
	if isspace(get.warehouse()) then
		return("")
	endif

	if item.is.serialized() or
	   g.total.line then
		return("")
	endif
	return(tdsls041.clot)
}

function long get.serial.selection()
{
	if isspace(get.warehouse()) then
		return(target(tdssel.any))
	endif

	if item.is.serialized() and
	   not g.total.line and
	   tdsls041.lsel = tclsel.specific then
		return(target(tdssel.specific))
	endif
	return(target(tdssel.any))
}

function domain tdltc.clot get.serial()
{
	|* See comments for function get.lot.selection()
	if g.total.line then
		return("")
	endif

	if isspace(get.warehouse()) then
		return("")
	endif

	if tdsls041.lsel = tclsel.any then
		return("")
	endif

	if item.is.serialized() then
		return(tdsls041.clot)
	endif
	return("")
}

function domain tcbool item.is.serialized()
{
	static	domain	tccprj	s.project		fixed
	static	domain	tcitem	s.item			fixed
	static	domain	tcbool	s.item.is.serialized

	if s.project <> tdsls045.cprj or
	   s.item <> tdsls045.item then
		s.item.is.serialized = serialized.item(
						tdsls045.cprj,
						tdsls045.item)
		s.project = tdsls045.cprj
		s.item = tdsls045.item
	endif
	return(s.item.is.serialized)
}

								|#SP1.sn
function domain tcbool item.is.lot.controlled()
{
	static	domain	tccprj	s.project		fixed
	static	domain	tcitem	s.item			fixed
	static	domain	tcbool	s.item.is.lot.controlled

	if s.project <> tdsls045.cprj or
	   s.item <> tdsls045.item then
		s.item.is.lot.controlled = lot.controlled.item(
						tdsls045.cprj,
						tdsls045.item)
		s.project = tdsls045.cprj
		s.item = tdsls045.item
	endif
	return(s.item.is.lot.controlled)
}
								|#SP1.en

								|#SP1.sn
function string get.lot.and.serial.set.401()
{
	domain	tcqsl1 		l.delivered.quantity
	domain	tcpono		l.sequence

	|* For the conversion of table 401 some additional checking needs to
	|* be done. This field is not filled for a total line or when the line
	|* is not yet delivered.

	|* Initialize
	g.lot.and.serial.set = ""

	if g.total.line then
		|* Lot and serial set not set on the total line
		return(g.lot.and.serial.set)
	endif

	l.delivered.quantity = get.delivered.quantity()
	if l.delivered.quantity = 0 then
		|* Order line is not delivered
		return(g.lot.and.serial.set)
	endif

	if item.is.lot.controlled() or
	   item.is.serialized() then
	   	if isspace(tdilc401.clot) and
	   	   isspace(tdsls041.clot) then
	   	   	|* If the item is lotcontrolled or serialized but no
	   	   	|* lot is not delivered then do not set the
	   	   	|* lot and serial set and consequently also not dump
	   	   	|* tcibd410 and ycibd411.
	   	   	|* This is migrating inconsistant data but because
	   	   	|* it is already delivered and WH also migrated we allow
	   	   	|* this migration anyway.
	   		return(g.lot.and.serial.set)
	   	endif
		l.sequence = get.order.line.sequence()
		g.lot.and.serial.set = edit$(tdsls045.orno, "999999") &
				       edit$(tdsls045.pono, "999") &
				       edit$(l.sequence, "999")
		return(g.lot.and.serial.set)
		|* This function detemines the guid for the lot and serial set
		|* The lot and serial set tables will be filled during
		|* dumping the tdsls406 records.
	endif
	return(g.lot.and.serial.set)
}
								|#SP1.en

function extern long get.shipping.constraint()
{
	if g.total.line or
	   tdsls041.drct = tcyesno.yes then
		|* For a total line or a direct delivery line the
		|* shipping constraint is not applicable and set to none.
		return(target(tdscon.none))
	endif

	if tdsls045.srnb > 0 then
		|* For backorders the shipping constraint is always none
		return(target(tdscon.none))
	endif

	if tdsls040.scom = tcyesno.yes then
		|* If the header is marked ship complete all lines are also
		|* marked as ship complete thus the shipping constraint is set
		|* to ship order complete.
		return(target(tdscon.oc))
	else
		if tdsls041.scom = tcyesno.yes then
		|* If the line is set to ship complete but the order is not set
		|* to ship complete then the shipping constraint will be set to
		|* ship line complete.
			return(target(tdscon.sc))
		else
		|* If the line and the header is not set to ship complete then
		|* the shipping constraint is set to None.
			return(target(tdscon.none))
		endif
	endif
}

function domain bcorno get.freight.order()
{
	static	domain	tcorno	s.transport.order
	static	domain	tcpono	s.transport.order.position
	static	domain	bcorno	s.freight.order

	if g.total.line then
		return("")
	endif

	if s.transport.order = tdsls045.torn and
	   s.transport.order.position = tdsls045.tpon then
			return(s.freight.order)
	endif

	s.transport.order = tdsls045.torn
	s.transport.order.position = tdsls045.tpon



	s.freight.order = ""
	if tdsls045.torn <> 0 then
		|* Check if freight order is converted
		if convert.transport.order.number(
					tdsls045.torn,
					tdsls045.tpon,
					g.error.message) <> 0 then
			|* transport order not converted
			s.freight.order = ""
		else
			s.freight.order = bcsfm001.norn
		endif
	endif
	return(s.freight.order)
}

function domain tcpono get.freight.order.position()
{
	domain	bcorno	l.freight.order

	l.freight.order = get.freight.order()
	if not isspace(l.freight.order) then
		|* If the transport order is converted to a freight order
		|* then for the transport order always freight order position
		|* 10 is created.
		return(10)
	endif
	return(0)
}

function long get.generate.freight.order()
{
	if generate.freight.order() = tcyesno.yes then
		return(target(tcyesno.yes))
	else
		return(target(tcyesno.no))
	endif
}

function domain tcyesno generate.freight.order()
{

	static	domain	tcorno		s.order
	static	domain	tcpono		s.position		|#800-141820.n
	static	domain	tcorno		s.torn
	static	domain	tcyesno		s.gen.freight.order
		domain	bcorno		l.freight.order
		domain	bcstc.lng2	l.dummy.acno

	if g.total.line then
		|* For total lines no freight orders
		return(tcyesno.no)
	endif

	if tdsls041.drct = tcyesno.yes then
		|* For direct delivery orders no freight orders
		return(tcyesno.no)
	endif

	if s.order = tdsls045.orno and
	   s.position = tdsls045.pono and			|#800-141820.n
	   s.torn = tdsls045.torn then
	   	return(s.gen.freight.order)
	else
		s.order = tdsls045.orno
		s.position = tdsls045.pono			|#800-141820.n
		s.torn = tdsls045.torn
		s.gen.freight.order = tcyesno.no
	endif

	|* Check if trtoc2200m000 is an activity of the order type
	|* If so then start with Generate freight order = yes else no

	if not activity.present.in.order.procedure("trtoc2200m000",
						    l.dummy.acno) then
		return(s.gen.freight.order)
	endif

	if tdsls045.torn = 0 then
								|#800-141820.sn
		if tdsls045.dqua <> 0 then
			|* If transport order is not yet generated and the
			|* the order line is already delivered then the
			|* freight order cannot be generated anymore on ERP LN.
			s.gen.freight.order = tcyesno.no
			return(s.gen.freight.order)
		endif

		if outbound.advice.present() then
			|* In BaanIV a transport order can be generated after
			|* deliveries are done. In ERP LN this is not possible.
			|* If an outbound advice is present then the sales
			|* order line cannot be changed anymore and thus the
			|* freight order cannot be generated anymore.
			|* If an advice is already present and the transport
			|* order is not created then the generate Freight order
			|* activity will be skipped for this order line.
			|* See also field documentation.
			s.gen.freight.order = tcyesno.no
			return(s.gen.freight.order)
		else
								|#800-141820.en
			|* Freight order can be generated
			s.gen.freight.order = tcyesno.yes
			return(s.gen.freight.order)
		endif
	endif

	|* transport order present
	l.freight.order = get.freight.order()
	if isspace(l.freight.order) then
		|* Freight order could not be converted
		s.gen.freight.order = tcyesno.no
	else
		s.gen.freight.order = tcyesno.yes
	endif
	return(s.gen.freight.order)
}

								|#800-141820.sn
function domain tcbool outbound.advice.present()
{
	if g.ilc.implemented then
		if g.no.of.outbound.advices.per.sls.sequence = 0 then
			|* Outbound advice for order line not present
			return(false)
		endif
	else
		|* No ILC situation no advices
		return(false)
	endif
	return(true)
}
								|#800-141820.en


function domain tcbool activity.present.in.order.procedure(
		domain	tcsess		i.activity,
	ref	domain	bcstc.lng2	o.tcmcs039.step.number)
{
	static	domain	tccotp		s.order.type
	static	domain	tcsess		s.activity
	static	domain	tcbool		s.order.step.present
		domain	tcbool		l.activity.defined
		domain	bcstc.lng2	l.order.step.number
		domain	bcstc.lng2	l.number.pmsk.positions
			long		l.actno

	if s.order.type = tdsls040.cotp and
	   s.activity = i.activity then
		return(s.order.step.present)
	else
		s.order.type = tdsls040.cotp
		s.activity = i.activity
		s.order.step.present = false
	endif

	l.activity.defined = false
	for l.actno = 1 to 20
		if strip$(g.old.session.code.activities.array(1,l.actno)) =
							strip$(i.activity) then
			|* activity defined in tcmcs039
			l.order.step.number = l.actno
			l.activity.defined = true
			break
		endif
	endfor

	if l.activity.defined then
		l.number.pmsk.positions = fill.max.stepno.isls0014()
		for l.actno = 1 to l.number.pmsk.positions
			|* Check if activity is present in the order type.
			if tcmcs042.stno(l.actno) = 0 then
				|* No more activities defined for the
				|* order type -> stop checking order
				|* type.
				break
			endif
			if l.order.step.number = tcmcs042.stno(l.actno) then
				|* Activity present in order
				|* procedure.
				s.order.step.present = true
				break
			endif
		endfor
	endif

	o.tcmcs039.step.number = l.order.step.number
	return(s.order.step.present)
}

function long get.freight.invoice()
{
|* Expl	:
|* This function is called when records in table trtrc140-maintain rate codes
|* by customer, are present. If records exists in this table then for each
|* order this table needs to be checked if for the sales customer a record
|* exists. If for the customer a rate code by customer exists then field the
|* tdsls401.frin is mapped with "yes" if all other cases the field will have the
|* default value.
|* For cost and return orders the default value is used.

	if generate.freight.order() = tcyesno.yes and
	   tcmcs042.reto = tcyesno.no and
	   tcmcs042.sund = tcyesno.no then
		select	trtrc140.cuno
		from	trtrc140
		where	trtrc140.cuno = :tdsls040.cuno
		as set with 1 rows
		selectdo
			return(target(tcyesno.yes))
		endselect
	endif
	return(target(tcyesno.no))
}

function string get.freight.service.level()
{
	static	domain	tccprj		s.project		fixed
	static	domain	tcitem		s.item			fixed
	static	domain	trtco.serv	s.service.level		fixed

	if generate.freight.order() = tcyesno.no then
		|* No freight order will be or is generated ->
		|* no freight service level required.
		return("")
	endif

	if tdsls045.cprj = s.project and
	   tdsls045.item = s.item then
		return(s.service.level)
	else
		s.project = tdsls045.cprj
		s.item = tdsls045.item
		s.service.level = ""
	endif

	select	trtcd120.serv:s.service.level
	from	trtcd120
	where	trtcd120._index1 = {	:tdsls045.cprj,
					:tdsls045.item}
	as set with 1 rows
	selectdo
	endselect
	return(s.service.level)
}

								|#800-143063.so
|function domain	bcorno	get.shipment()
|{
|	domain	bcorno	l.shipment	fixed
|
|	|* WH requires to convert delivered sales orders to shipments.
|	|* SLS requires to have a link between a shipment and a delivered sales
|	|* order.
|	|* To make sure that WH and SLS will use the same shipment id's and
|	|* shipment lines the following rules apply:
|	|* - As Shipment ID on the target a combination between sales order and
|	|*   sales position (from the source side) will be used.
|	|* - As Shipment line on the target the sequence number of the delivery
|	|*   line on the source side will be used.
|
|	l.shipment = ""
|
|	if g.total.line then
|		return(l.shipment)
|	endif
|
|	if tdsls045.ddat <> 0 and
|	   not isspace(tdsls045.cwar) then
|		l.shipment = edit$(tdsls045.orno, "999999") &
|			     edit$(tdsls045.pono, "999")
|	endif
|	return(l.shipment)
|}
								|#800-143063.eo
								|#800-143063.sn
function domain	bcorno	get.shipment()
{
	|* WH requires to convert delivered sales orders to shipments.
	|* SLS requires to have a link between a shipment and a delivered sales
	|* order.
	|* To make sure that WH and SLS will use the same shipment id's and
	|* shipment lines the following rules apply:
	|* - As Shipment ID on the target a combination between sales order and
	|*   sales position (from the source side) will be used.
	|* FOR SHIPMENT LINE SEE FUNCTION GET.SHIPMENT.LINE

	domain	bcorno	l.shipment	fixed

	l.shipment = ""

	if g.total.line then
		return(l.shipment)
	endif

	if g.ilc.implemented then
		if g.create.expected.delivery.line.for.tdilc401 then
			|* No delivery done for expected advice thus no shipment
			l.shipment = ""
		else
			|* Outbound advice delivered
			if tdsls045.ddat <> 0 and
		   	   not isspace(tdsls045.cwar) then
				l.shipment = edit$(tdsls045.orno, "999999") &
					     edit$(tdsls045.pono, "999")
			endif
		endif
	else
		if tdsls045.ddat <> 0 and
		   not isspace(tdsls045.cwar) then
			l.shipment = edit$(tdsls045.orno, "999999") &
				     edit$(tdsls045.pono, "999")
		endif
	endif
	return(l.shipment)
}
								|#800-143063.en
								|#SL204550.sn
function determine.var.shpm.and.shln()	
{
	static	domain	tcorno	static.orno
	static	domain	tcpono	static.pono
	static	domain	tcsrnb	static.srnb
	static	domain	bcorno	static.shpm
	static	domain	tcpono	static.shln

	domain	tcpono		bom.line
	var.shipment = ""
	var.shipment.line = 0

	if g.total.line then
		static.orno = tdsls045.orno
		static.pono = tdsls045.pono
		static.srnb = tdsls045.srnb
		static.shpm = ""
		static.shln = 0
		return
	endif

	if g.ilc.implemented then
		if g.create.expected.delivery.line.for.tdilc401 then
			|* No delivery done for expected advice thus no shipment
			static.orno = tdsls045.orno
			static.pono = tdsls045.pono
			static.srnb = tdsls045.srnb
			static.shpm = ""
			static.shln = 0
			return
		else
			|* Outbound advice delivered
			if tdsls045.ddat <> 0 and
		   	   not isspace(tdsls045.cwar) then
				if tdsls041.isss = tcisss.issue.comp then
					get.first.bom.line(bom.line)
				else
					bom.line = 0
				endif
				if tdsls045.orno = static.orno and
				   tdsls045.pono = static.pono and
				   tdsls045.srnb = static.srnb then
					var.shipment = static.shpm
					var.shipment.line = static.shln + 1
				else
					get.shipment.number.and.line(
						tckoor.act.sls,
						tdsls045.orno,
						tdsls045.pono,
						tdsls045.srnb,
						bom.line,
						errmess)
					if isspace(errmess) then
						var.shipment = 
							bcswh008.shpm
						var.shipment.line = 
						bom.line > 0 ? 0:bcswh008.shln
					else
						var.shipment = 
							get.shipment()
						var.shipment.line = 
							get.shipment.line()
						if bom.line > 0 then
							var.shipment.line = 0
						endif
					endif
				endif
			endif
		endif
	else
		if tdsls045.ddat <> 0 and
		   not isspace(tdsls045.cwar) then
			if tdsls041.isss = tcisss.issue.comp then
				get.first.bom.line(bom.line)
			else
				bom.line = 0
			endif
			get.shipment.number.and.line(
					tckoor.act.sls,
					tdsls045.orno,
					tdsls045.pono,
					tdsls045.srnb,
					bom.line,
					errmess)
			if isspace(errmess) then
				var.shipment = bcswh008.shpm
				var.shipment.line = bom.line>0 ? 0:bcswh008.shln
			else
				var.shipment = get.shipment()
				var.shipment.line = get.shipment.line()
				if bom.line > 0 then
					var.shipment.line = 0
				endif
			endif
		endif
	endif

	static.orno = tdsls045.orno
	static.pono = tdsls045.pono
	static.srnb = tdsls045.srnb
	static.shpm = var.shipment
	static.shln = var.shipment.line
}

function get.first.bom.line(
		ref	domain	tcpono	o.bom.line)
{
	o.bom.line = 0
	select	tdsls047.ponb:o.bom.line
	from	tdsls047
	where	tdsls047._index1 = {	:tdsls041.orno,
					:tdsls041.pono}
	as set with 1 rows
	selectdo
	endselect
}
								|#SL204550.en
								|#800-143063.so
|function domain	tcsrnb get.shipment.line()
|{
|	domain	tcsrnb	l.shipment.line
|
|
|	if g.total.line then
|		return(0)
|	endif
|
|	if tdsls045.ddat <> 0 and
|	   not isspace(tdsls045.cwar) then
|		return(tdsls045.srnb)
|	endif
|	return(0)
|}
								|#800-143063.eo
|								|#202924.so
|								|#800-143063.sn
|function domain	tcpono get.shipment.line()
|{
|	|**********************************************************************
|	|* WH requires to convert delivered sales orders to shipments.
|	|* SLS requires to have a link between a shipment and a delivered sales
|	|* order.
|	|* To make sure that WH and SLS will use the same shipment id's and
|	|* shipment lines the following rules apply:
|	|* - As Shipment ID on the target a combination between sales order and
|	|*   sales position (from the source side) will be used.
|	|* - As Shipment line on the target the following rules are applied:
|	|*   ILC: As Shipment line the record no. of the delivered tdilc401 is
|	|* 	  used as shipment line number
|	|*   NON ILC: For each new shipment the Shipment line will start at 1
|	|*	      When for the same order/ posistion combination another
|	|*	      sales delivery is done this will result in a new shipment
|	|*	      line for the same shipment
|	|*	      stepsize in the shipment is 1 this stepsize is also used
|	|*	      when migrating shipments lines (Wh).
|	|**********************************************************************
|	|* Migrating shipments and migrating sales orders must loop indentical
|	|* through the source sales orders and the source outbound advices.
|	|* This will result in same sales sequences / shipment lines.
|	|* In case of mismatches the solution must be found in the way the
|	|* migration object loops through the source objects.
|	|**********************************************************************
|
|	static	domain	tcpono	s.shipment.line
|	static	domain	tcorno	s.order
|	static	domain	tcpono	s.position
|
|	if g.total.line then
|		return(0)
|	endif
|
|	if g.ilc.implemented then
|		s.shipment.line = 0	|* initialize shipment line
|		if g.create.expected.delivery.line.for.tdilc401 then
|			|* No delivery done for expected advice thus no shipment
|			return(0)
|		else
|			if tdsls045.ddat <> 0 and
|		   	   not isspace(tdsls045.cwar) then
|		   	   |* Outbound advice delivered -> shipment line is set
|			   |* Equal to the processed tdilc401 record.
|				s.shipment.line = g.outbound.advice.record
|			else
|				return(0)
|			endif
|		endif
|	else
|		if tdsls045.ddat <> 0 and
|		   not isspace(tdsls045.cwar) then
|			|* Sales order line sequence = delivered thus shipment
|			|* line must be determined. Check if the shipment line
|			|* is required for the same shipment or a new one.
|			|* Same shipment the shipment line is previous shipment
|			|* line + 1. (stepsize = 1)
|			|* New shipment shipment line start with 1.
|			if s.order <> tdsls045.orno or
|			   s.position <> tdsls045.pono then
|				s.order = tdsls045.orno
|				s.position = tdsls045.pono
|				s.shipment.line = 0
|				|* initialize shipment line for new shipment
|			endif
|
|			s.shipment.line = s.shipment.line + 1
|		else
|			|* No delivery done thus no shipment
|			return(0)
|		endif
|	endif
|
|	return(s.shipment.line)
|
|}
|							|#800-143063.en
|							|#202924.eo
							|#202924.sn
function domain	tcpono get.shipment.line()
{
	|**********************************************************************
	|* WH requires to convert delivered sales orders to shipments.
	|* SLS requires to have a link between a shipment and a delivered sales
	|* order.
	|* To make sure that WH and SLS will use the same shipment id's and
	|* shipment lines the following rules apply:
	|* - As Shipment ID on the target a combination between sales order and
	|*   sales position (from the source side) will be used.
	|* - As Shipment line on the target the migrated sales order sequence
	|*   number is used.
	|*   For each sales order line sequence number (delivery) one shipment
	|*   line exists.
	|*   The migrated sales order line sequence nr is taken because this is
	|*   the data that is available at tdsls401, whinh431 and whinr110
	|* Note:
	|* There is one exeption: When the migrated sales order line sequence
	|* is equal to zero then the shipment line will be set to 10.
	|**********************************************************************

	domain	tcpono	l.sales.sequence
	domain	tcpono	l.shipment.line

	if g.total.line then
		|* No shipment / shipment line on a total line
		return(0)
	endif

	if g.ilc.implemented then
		if g.create.expected.delivery.line.for.tdilc401 then
			|* No delivery done for expected advice thus no shipment
			return(0)
		endif
	endif

	if tdsls045.ddat <> 0 and
	   not isspace(tdsls045.cwar) then
		|* shipment line is set equal to the migrated sales sequence.
		l.sales.sequence = get.order.line.sequence()
		if l.sales.sequence = 0 then
			l.shipment.line = 10
		else
			l.shipment.line = l.sales.sequence
		endif
		return(l.shipment.line)
	endif

	|* no shipment done
	return(0)
}
								|#202924.en


|*****************************************
|* Determine quantity fields
|*****************************************
function domain tcqsl1 quantity.from.order.unit.to.inventory.unit (
 	 	 domain 	 tcqrd1  	 i.quantity.in.order.unit )
{
	get.item.inventory.unit.and.price.group()
 	 return ( itccom0007.qty.domain.round (
 	 	 	 i.quantity.in.order.unit * tdsls041.cvqs,
 	 	 	 "tcqsl1",
 	 	 	 g.inventory.unit))
}


								|#143063.so
|function domain tcqsl1 get.ordered.quantity()
|{
|	if g.total.line then
|		return(g.total.qoor)
|	else
|		return(tdsls045.oqua)
|	endif
|}
								|#143063.eo
								|#143063.sn
function domain tcqsl1 get.ordered.quantity()
{
	|******************************************************************
	|* Get ordered qty for a total line
	|******************************************************************
	if g.total.line then
		return(g.total.qoor)
	endif

	|******************************************************************
	|* Get Ordered qty when ILC implemented and multiple advices present
	|******************************************************************
	if g.ilc.implemented and
	   g.no.of.outbound.advices.per.sls.sequence > 1 then
		if g.create.expected.delivery.line.for.tdilc401 then
			|* Create expected delivery line for outbound advice
			|* Expected delivery is the ordered qty minus the
			|* already delivered quantity. Values can be taken from
			|* the sales order line
			return(tdsls045.oqua - tdsls045.dqua)
		endif

		if g.outbound.advice.record =
				g.no.of.outbound.advices.per.sls.sequence then
			|* If all advices present are delivered but a backorder
			|* quantity is still defined then for the last
			|* advice the ordered qty will be the delivered
			|* adviced quantity + Backrder quantity
			return(tdilc401.qsts + tdsls045.bqua)
		else
			|* Create a sales order delivery line for the delivered
			|* outbound advice record. ordered quantity is equal to
			|* the delivered quantity.
			|* The qsts field is the quantity in inventor unit.
			return(tdilc401.qsts)
		endif
	endif

	|******************************************************************
	|* Get ordered qty for Non-ILC situation and ILC situation but only
	|* 1 advice present for the sales order line sequence.
	|******************************************************************
	return(tdsls045.oqua)

}
								|#143063.en

								|#143063.so
|function domain tcqsl1 get.delivered.quantity()
|{
|	domain	tcqsl1 l.converted.delivered.quantity
|
|	l.converted.delivered.quantity = 0.0
|	if g.total.line then
|		l.converted.delivered.quantity =
|			quantity.from.order.unit.to.inventory.unit(
|								g.total.qidl)
|	else
|		l.converted.delivered.quantity =
|			quantity.from.order.unit.to.inventory.unit(
|								tdsls045.dqua)
|	endif
|	return(l.converted.delivered.quantity)
|}
								|#143063.eo
								|#143063.sn
function domain tcqsl1 get.delivered.quantity()
{
	domain	tcqsl1 l.converted.delivered.quantity

	l.converted.delivered.quantity = 0.0

	|******************************************************************
	|* Get delivered qty for the total line
	|******************************************************************
	if g.total.line then
		l.converted.delivered.quantity =
			quantity.from.order.unit.to.inventory.unit(
								g.total.qidl)
		return(l.converted.delivered.quantity)
	endif

	|******************************************************************
	|* Get delivered qty in ILC situation and for this orderline sequence
	|* there are more than one advice (delivered or not delivered)
	|* If one advice is present than the values from the sales order line
	|* can be used (next section).
	|* For a delivered advice 	-> Delivered qty = advice qty
	|* For a not delivered advice 	-> Delivered qty = 0
	|******************************************************************
	if g.ilc.implemented and
	   g.no.of.outbound.advices.per.sls.sequence > 1 then
	   	if g.create.expected.delivery.line.for.tdilc401 then
			|* For the expected sales order delivery line the
			|* delivered quantity is always zero
			l.converted.delivered.quantity = 0.0
		else
			|* For the delivered outbound advice the delivered
			|* quantity of the sales order delivery line is equal
			|* to the advised quantity of the tdilc401 record.
			|* The qsts field is the quantity in inventor unit.
			l.converted.delivered.quantity = tdilc401.qsts
		endif
		return(l.converted.delivered.quantity)
	endif

	|******************************************************************
	|* In all other cases
	|******************************************************************
	l.converted.delivered.quantity =
				quantity.from.order.unit.to.inventory.unit(
								tdsls045.dqua)

	return(l.converted.delivered.quantity)
}
								|#143063.en

								|#143063.so
|function domain tcqsl1 get.backorder.quantity()
|{
|	if g.total.line then
|		return(g.total.qbbo)
|	else
|		return(tdsls045.bqua)
|	endif
|}
								|#143063.eo
								|#143063.sn
function domain tcqsl1 get.backorder.quantity()
{
	|******************************************************************
	|* Get backorder qty for the total line
	|******************************************************************
	if g.total.line then
		|**********************
		|* ILC Situation
		|**********************
		if g.create.expected.delivery.line.for.tdilc401 then
		|* If an expected sales order delivery line is created
		|* then the backorder qty is handled by the expected delivery
		|* line created.
			return(0.0)
		endif
		|**********************
		|* All other cases for a total line
		|**********************
		return(g.total.qbbo)
	endif

	|******************************************************************
	|* Get backorder quantity in the ILC situation and multiple advices
	|* present for the sales order line sequence.
	|* Backorder qty will be set for delivery line created for the last
	|* advice for the sales order line sequence.
	|* When not yet delivered advice are present then an expected delivery
	|* line will be created. The backorder qty is then handled by the
	|* expected delivery line.
	|* If all advices present are delivered then the backorder qty is
	|* set for the last to be created delivery line.
	|******************************************************************
	if g.ilc.implemented and
	   g.no.of.outbound.advices.per.sls.sequence > 1 then
		if g.outbound.advice.record <
				g.no.of.outbound.advices.per.sls.sequence then
			|* Backorder quantity is handled by the delivery line
			|* for the last advice or on the expected sales order
			|* delivery line.
			return(0.0)
		endif
		if g.create.expected.delivery.line.for.tdilc401 then
			|* Backorder quantity is on the target handled by the
			|* ordered qty of the expected sales order deliveryline.
			|* Backorder qty set to zero.
			return(0.0)
		endif
		if g.outbound.advice.record =
				g.no.of.outbound.advices.per.sls.sequence then
			|* Last advice
			return(tdsls045.bqua)
		endif
	endif

	|******************************************************************
	|* Get backorder qty in a NON-ILC situation.
	|******************************************************************
	return(tdsls045.bqua)
}
								|#143063.en

function domain tcqsl1 get.committed.quantity()
{
	domain	tcqsl1 l.converted.committed.quantity

	l.converted.committed.quantity = 0.0
	if not g.total.line and
	   tdsls045.ddat = 0 then
		|* when sales order line is already delivered
		|* committed quantity on the target is 0
		l.converted.committed.quantity =
			quantity.from.order.unit.to.inventory.unit(
								tdsls045.recq)
	endif
	return(l.converted.committed.quantity)
}

|								|#202399.so
|function long get.backorder.confirmed.flag()
|{
||*	if the backorder quantity is filled and a record with a higher
||* 	sequence number exists then the backorder is confirmed.
||*	The confirmed flag is not applicable for total lines.
|
||#800-143070.so
||	if backorder.confirmed() then
||#800-143070.eo
||#800-143070.sn
|	if not g.total.line and
|	   backorder.confirmed() then
||#800-143070.en
|		return(1)
|	endif
|
|	return(2)
|}
|
|function domain	tcbool	backorder.confirmed()
|{
||#800-143070.so
||	if not g.total.line and
||	   double.cmp(tdsls045.bqua, 0.0, 0.0001) <> 0 and
||#800-143070.eo
||#800-143070.sn
|	   if double.cmp(tdsls045.bqua, 0.0, 0.0001) <> 0 and
||#800-143070.en
|	   tdsls045.srnb < get.no.of.delivery.lines() then
|		return(true)
|	endif
|	return(false)
|}
|								|#202399.eo
								|#202399.sn
function long get.backorder.confirmed.flag()
{
|*	if the backorder quantity is filled and a record with a higher
|* 	sequence number exists then the backorder is confirmed.
|*	The confirmed flag is not applicable for total lines.
	if not g.total.line and
	   backorder.confirmed(tdsls045.bqua, tdsls045.srnb) then
		return(1)
	endif

	return(2)
}

function domain	tcbool	backorder.confirmed(
			domain	tcqsl1	i.backorder.quantity,
			domain	tcpono	i.sales.sequence)
{
	if double.cmp(i.backorder.quantity, 0.0, 0.0001) <> 0 and
	   i.sales.sequence < get.no.of.delivery.lines() then
		return(true)
	endif
	return(false)
}
								|#202399.en

|function domain tcsrnb get.linked.sequence.order.line()	|#210154.so
|{
||*	The linked order line sequence points to the sequence number from which
||*	this line is created -> thus points always to the previous delivery line
||* 	in the source version.
||*	linked sequence in source version =
||*				converted sequence number in target + 1
||*	converted sequence number in target = current sequence number + 1
||*	Thus the linked order line sequence in the target is mapped with the
||*	sequence number + 2 on the source side.
||*	The linked sequence is not applicable for total lines.
|
|	if not g.total.line and
|	   tdsls045.srnb < get.no.of.delivery.lines() then
|		return(tdsls045.srnb + 2)
|	endif
|	return(0)
|}								|#210154.eo
								|#210154.sn
function domain tcpono get.linked.sequence.order.line()
{
|*	The linked order line sequence points to the next back order line 
|*	sequence number for this order line
|*	Thus points always to the next delivery line in the source version.
|* 	Accept for the last delivery line and a total line then it will be 
|*	mapped with zero
|*	The Next backorder line sequence is the current orde line sequence + 1

	domain 	tcpono 	l.next.source.sequence
	domain	tcpono	l.next.target.sequence
	
	if g.total.line then
		|* for total lines lseq is set to zero.
		return(0)
	endif
	
	if tdsls045.srnb = get.no.of.delivery.lines() then
		|* For the last delivery line the linked sequence is zero.
		return(0)
	endif

	if g.outbound.advice.record <
				g.no.of.outbound.advices.per.sls.sequence then
		|* If all advices present are delivered but a backorder
		|* quantity is still defined then only set the lseq on the
		|* last advice line.
		return(0)
	endif
	
	l.next.source.sequence = tdsls045.srnb + 1
	l.next.target.sequence = get.sls.order.line.sequence.number(
						tdsls045.orno,
						tdsls045.pono,
						l.next.source.sequence)
	return(l.next.target.sequence)
}
								|#210154.en

function get.item.inventory.unit.and.price.group()
{
	static	domain	tcorno	s.order
	static	domain	tcpono	s.position

	if s.order = tdsls045.orno and
	   s.position = tdsls045.pono then
	   	return
	else
		s.order = tdsls045.orno
		s.position = tdsls045.pono
		g.inventory.unit = ""
	endif

	if not isspace(tdsls045.cprj) and
	   tdsls041.opol = tcittc.customized then
		select	tipcs021.cuni:g.inventory.unit,
			tiitm001.cpgs:g.price.group
		from	tipcs021, tiitm001
		where	tipcs021._index1 = {:tdsls045.cprj, :tdsls045.item}
		and	tipcs021.dfit refers to tiitm001 unref clear
		as set with 1 rows
		selectdo
		endselect
		if isspace(g.price.group) then
			g.price.group = bcstd000.cprg
		endif
	else
		select	tiitm001.cuni:g.inventory.unit,
			tiitm001.cpgs:g.price.group
		from	tiitm001
		where	tiitm001._index1 = {:tdsls045.item}
		as set with 1 rows
		selectdo
		endselect
	endif
}

|***************************
|*  Retrieve amount related fields
|***************************
function domain tcamnt get.order.discount.amount()
{
	if g.total.line then

		return(g.total.amod)
	else
		return(tdsls045.odam)
	endif
}

function domain tcamnt get.order.line.discount.amount()
{
	if g.total.line then
		return(g.total.amld)
	else
		return(tdsls045.ldam)
	endif
}

function domain tcamnt get.delivered.amount()
{
	if g.total.line then
		return(g.total.damt)
	endif

	if tdsls045.ddat <> 0 then
		return(tdsls045.amnt)
	else
		return(0.0)
	endif
}

function domain tcamnt get.order.line.amount()
{
	domain	tcamnt	l.dummy.line.disc.amount
	domain	tcamnt	l.order.line.amount

	if g.total.line then
		return(g.total.oamt)
	endif

	if tdsls045.ddat = 0 then
		return(tdsls045.amnt)
	endif

	if double.cmp(tdsls045.oqua, 0.0, 0.0001) and
	   double.cmp(tdsls045.pric, 0.0, 0.0001) then
		|* Records where oqua, pric and oamt are zero are not selected.
		|* if oqua and pric are zero then oamt must be filled.
		|* recalculation based upon oqua and pric will lead to zero so
		|* in this case no recalculation.
		return(tdsls045.amnt)
	endif

	|* determine oamt based upon oqua
 	 tdsls.dll0022.calc.net.amount (
	 	 tdsls041.cvqs,
	 	 tdsls041.cvps,
	 	 tdsls045.oqua,
	 	 tdsls045.pric,
	 	 tdsls040.ccur,
 		 tcmcs002.crnd,
	 	 tdsls041.dmth,
	 	 tdsls045.disc,
 		 tdsls045.damt,
	 	 l.order.line.amount, 		| * ref
	 	 l.dummy.line.disc.amount ) 	| * ref
	return(l.order.line.amount)
}

|*****************************************
|*  Retrieve activity related fields
|*****************************************
function domain bcstd.acti get.current.activity()
{
	domain	tcpmsk		l.procedure.mask
	domain	bcstd.acti	l.current.activity	fixed
		long		l.step.number

	l.current.activity = ""
	if not g.total.line then
		l.procedure.mask = get.procedure.mask()
		l.step.number = pos(l.procedure.mask, "y")
		if l.step.number <> 0 then
		      l.current.activity = get.new.session.code.for.step.number(
						tdsls040.cotp,
						l.step.number)
		else
			|* When all activities are executed including
			|* Process delivered sales orders then the activity is
			|* set to delete sales order
			l.current.activity = "tdsls4224m000"
		endif
	endif
	return(l.current.activity)
}

function domain tcpmsk get.procedure.mask()
{
		domain	bcstc.lng2	i
		domain	bcstc.lng2	l.new.pmsk.position
		domain	bcstc.lng2	l.number.pmsk.positions
		domain	tcsess		l.old.activity
		domain	tcsess		l.new.activity
	static	domain	tcpmsk		s.procedure.mask	fixed
	static	domain	tcbool		s.total.line
	static	domain	tcorno		s.orno
	static	domain	tcpono		s.pono
	static	domain	tcsrnb		s.srnb

	if s.orno = tdsls045.orno and
	   s.pono = tdsls045.pono and
	   s.srnb = tdsls045.srnb and
	   s.total.line = g.total.line then
		return(s.procedure.mask)
	else
		s.orno = tdsls045.orno
		s.pono = tdsls045.pono
		s.srnb = tdsls045.srnb
		s.total.line = g.total.line
		s.procedure.mask = "nnnnnnnnnnnnnnnnnnnn"
	endif

	if g.total.line then
		s.procedure.mask = "nnnnnnnnnnnnnnnnnnne"
		return(s.procedure.mask)
	endif

	l.number.pmsk.positions = fill.max.stepno.isls0014()
	for i = 1 to l.number.pmsk.positions
		if tdsls045.pmsk(i;1) = "n" then
			continue
		endif

		|* Get session code for procedure mask position
		l.old.activity = g.old.session.code.activities.array(1,i)

		|* convert old source session codes to new target session codes
		l.new.activity = convert.old.activity(l.old.activity)

		|* Retrieve new procedure mask position for target activity
		l.new.pmsk.position = get.new.pmsk.position(
							tdsls040.cotp,
							l.new.activity)



		if l.new.pmsk.position <> 0 then
			s.procedure.mask(l.new.pmsk.position;1) =
							tdsls045.pmsk(i;1)
		endif
	endfor

	for i = 1 to l.number.pmsk.positions
		|* Fill pmsk with "y" with the new target activities
		l.new.activity = ""
		if s.procedure.mask(i;1) <> "n" then
			continue
		endif
		l.new.activity = get.new.session.code.for.step.number(
					tdsls040.cotp,
					i)
		if not isspace(l.new.activity) then
			 s.procedure.mask(i;1) = "y"
		endif
	endfor

	s.procedure.mask = check.and.correct.pmsk(s.procedure.mask)


	return(s.procedure.mask)
}

function fill.old.session.code.activities.array()
{
		domain	tcsess		l.old.session.code.activity
			long		i

	for i = 1 to 20
		select	tcmcs039.sess:l.old.session.code.activity
		from	tcmcs039
		where	tcmcs039._index2 = {	tcmodc.sls,
						:i}
		as set with 1 rows
		selectdo
		selectempty
			l.old.session.code.activity = ""
		endselect

		g.old.session.code.activities.array(1,i) =
					l.old.session.code.activity
	endfor
}


function domain	tcsess	convert.old.activity(
				domain	tcsess i.old.activity.code)
{
	domain	tcsess	l.new.activity.code

|********************************************************************
|*	OLD Session	-> NEW Session
|*	"tdsls4223m000" -> no change		(PROCESS.LINE.SLS)
|*	"tdsls4401m000"	-> No change		(PRNT.ORD.ACK)
|*	"tdinv2201m000" -> "whinp2200m000"	(GEN.ORD.COMM)
|*	"tipcs2240m000"	-> "tdsls4244m000" 	(GEN.OB.STRUC)
|*	"trtoc2200m000"	-> "tdsls4222m000"	(GEN.FRT.ORDR)
|*
|*			-> "tdsls4224m000"	(DELETE.LINE.SLS)
|*			-> "tdsls4240m000"	(GEN.PUR.ADVI)
|*			-> "tdsls4246m000"	(RLSE.TO.WRHS)
|*			-> "tdsls4247m000"	(RLSE.TO.INVO)
|*			-> "tdsls4101m200"	(MAINT.DELIVERIES)
|********************************************************************
	on case strip$(i.old.activity.code)
	case "tdinv2201m000":
		l.new.activity.code = "whinp2200m000"
		break
	case "tipcs2240m000":
		l.new.activity.code = "tdsls4244m000"
		break
	case "trtoc2200m000":
		l.new.activity.code = "tdsls4222m000"
		break

	default:
		l.new.activity.code = i.old.activity.code
	endcase
	return(l.new.activity.code)
}

|function domain	bcstc.lng2	get.new.pmsk.position(
|			domain	tccotp	i.order.type,
|			domain	tcsess	i.new.activity)
|{
|	|* This function returns the new activity number
|	|* for the activity.
|	|* If the activity is not part of the order type
|	|* the new activity number is 0
|
|	domain	bcstc.lng2	l.new.activity.number
|
|	l.new.activity.number = 0
|
|	select	bcstd001.acno:l.new.activity.number
|	from	bcstd001
|	where	bcstd001._index1 = {	:i.order.type,
|					:i.new.activity}
|	as set with 1 rows
|	selectdo
|	endselect
|	return(l.new.activity.number)
|}
function domain	bcstc.lng2	get.new.pmsk.position(
			domain	tccotp	i.order.type,
			domain	tcsess	i.new.activity)
{
	|* This function returns the new activity number
	|* for the activity.
	|* If the activity is not part of the order type
	|* the new activity number is 0

	domain	bcstc.lng2	l.new.activity.number
	domain	bcstc.lng2	i
	domain	tcsess		l.new.session.codes.by.step.number(20)	fixed

	load.new.session.codes.new.order.type.into.array(
				i.order.type,
				l.new.session.codes.by.step.number)

	l.new.activity.number = 0

	for i = 1 to 20
		if l.new.session.codes.by.step.number(1,i) =
						strip$(i.new.activity) then
			l.new.activity.number = i
			break
		endif
	endfor
	return(l.new.activity.number)
}

|function domain tcsess get.new.session.code.for.step.number(
|		domain	tccotp		i.order.type,
|		domain	bcstc.lng2	i.current.step.number)
|{
|	|* This function returns the new activity code
|	|* for a new target activity.
|	|* The pmsk must also be set for this new activity
|
|	domain	tcsess	l.new.activity.code
|
|	l.new.activity.code = ""
|
|	select	bcstd001.acti:l.new.activity.code
|	from	bcstd001
|	where	bcstd001._index2 = {	:i.order.type,
|					:i.current.step.number}
|	as set with 1 rows
|	selectdo
|	endselect
|	return(l.new.activity.code)
|}

function domain tcsess get.new.session.code.for.step.number(
		domain	tccotp		i.order.type,
		domain	bcstc.lng2	i.current.step.number)
{
	|* This function returns the new activity code
	|* for a new target activity.
	|* The pmsk must also be set for this new activity

	domain	tcsess	l.new.session.codes.by.step.number(20)	fixed

	load.new.session.codes.new.order.type.into.array(
				i.order.type,
				l.new.session.codes.by.step.number)

	return(l.new.session.codes.by.step.number(1, i.current.step.number))
}

function load.new.session.codes.new.order.type.into.array(
			domain	tccotp	i.order.type,
		ref	domain	tcsess	o.new.session.codes.for.order.type())
{
	static	domain	tccotp	s.order.type				fixed
	static	domain	tcsess	s.new.session.codes.for.order.type(20)	fixed
			long	i

	if s.order.type <> i.order.type then
		s.order.type = i.order.type
		|* reset array before loading.
		for i = 1 to 20
			s.new.session.codes.for.order.type(1, i) = ""
		endfor
		|* Load new order type session into array
		select	bcstd001.cotp, bcstd001.acti, bcstd001.acno
		from	bcstd001
		where	bcstd001._index2 = {	:i.order.type }
		selectdo
			s.new.session.codes.for.order.type(1, bcstd001.acno) = bcstd001.acti
		endselect
	endif
	o.new.session.codes.for.order.type = s.new.session.codes.for.order.type
	return
}

function domain	tcpmsk	check.and.correct.pmsk(
			domain	tcpmsk	i.procedure.mask)
{
	domain	bcstc.lng2	l.position
	domain	bcstc.lng2	l.number.pmsk.positions
	domain	tcsess		l.not.executed.acti
	domain	bcorno		l.freight.order
	domain	tcpmsk		l.procedure.mask

	l.procedure.mask = i.procedure.mask

	l.number.pmsk.positions = fill.max.stepno.isls0014()
	for l.position = 1 to l.number.pmsk.positions
		if l.procedure.mask(l.position;1) <> "y" then
			continue
		endif

		l.not.executed.acti = get.new.session.code.for.step.number(
						tdsls040.cotp,
						l.position)

		on case strip$(l.not.executed.acti)
|#SL201971.sn
		case "tdsls4401m000":
		     |* Print order acnowledgement
			if tdsls045.srnb > 0 then
				|* for backorder this activity is not valid
				|* Already printed on existing order.
				l.procedure.mask(l.position;1) = "n"
			endif
			break
		case "tdsls4244m000":
		     |* Generate (project) structure
			if tdsls045.srnb > 0 then
				|* for backorder this activity is not valid
				|* Structure already generated on existing order.
				l.procedure.mask(l.position;1) = "n"
			endif
			break
|#SL201971.en
		case "tdsls4222m000":
		     |* Generate Freight orders
			if tdsls045.torn <> 0 then
				|* if transport order is present on the source
				|* but cannot be converted then the freight
				|* order on the target will be made empty and
				|* the activity is not applicable
				l.freight.order = get.freight.order()
				if isspace(l.freight.order) then
					l.procedure.mask(l.position;1) = "n"
				endif
			endif
								|#214615.sn
			|* If the order line at the target is already 
			|* fully processed then this activity will be 
			|* set to E(xecuted).		
			if l.procedure.mask(20;1) = "e" then
				|* order line fully processed on source
				|* side, set new activity to processed.
				l.procedure.mask(l.position;1) = "e"
			endif	
								|#214615.en			
			break
		case "tdsls4240m000":
		     |* Generate Purchase order advice
			|* if purchase order advice is present or the purchase
			|* order is already present then set to "e" .
			|* If not then generate PO advice is the next step if it
			|* is a direct delivery order line.
			|* If the PO advice is not present and it is not a
			|* direct delivery then the PO advice step will be set
			|* to No.
			if purchase.advice.present() or
			   tdsls041.porn <> 0 then
				l.procedure.mask(l.position;1) = "e"
			else
				if tdsls041.drct = tcyesno.no then
					l.procedure.mask(l.position;1) = "n"
				endif
|#800-143070.sn
				if tdsls045.srnb > 0 then
				|* for backorder this activity is not valid
					l.procedure.mask(l.position;1) = "n"
				endif
|#800-143070.en
			endif
			break
		case "tdsls4246m000":
		     |* Release to Warehousing
			if not isspace(tdsls045.cwar) then
				|* Warehouse filled -> rel to wh applicable
				|* If the warehouse on the sales order on the
				|* source is filled then on the target the
				|* Release to Warehousing activity must be set.
				if set.release.to.warehousing.to.executed() then
					|* default value "y" canged to "e"
					l.procedure.mask(l.position;1) = "e"
				endif
			else
				|* default value "y" canged to "n"
				l.procedure.mask(l.position;1) = "n"
			endif
			break
		case "tdsls4101m200":
		     |* Maintain deliveries for sales orders
			if isspace(tdsls045.cwar) and
			   tdsls041.drct = tcyesno.no then
				|* Deliveries handled via Sales
				if tdsls045.ddat <> 0 then
					|* line = delivered
					l.procedure.mask(l.position;1) = "e"
				endif
								|#214615.sn
				|* If the order line at the target is already 
				|* fully processed then this activity will be 
				|* set to E(xecuted).		
				if l.procedure.mask(20;1) = "e" then
					|* order line fully processed on source
					|* side, set new activity to processed.
					l.procedure.mask(l.position;1) = "e"
				endif
								|#214615.en
			else
				|* Deliveries handled via warehousing
				l.procedure.mask(l.position;1) = "n"
			endif
			break
		case "tdsls4247m000":
		     |* release to invoicing
			if set.release.to.invoicing.to.executed() then
				l.procedure.mask(l.position;1) = "e"
			endif
			break
		endcase
	endfor
	return(l.procedure.mask)
}

function domain	tcbool set.release.to.warehousing.to.executed()
{
	domain	bcstc.lng2	l.activity.number.mcs039

	if generate.freight.order() = tcyesno.yes and
	   tdsls045.torn = 0 then
	   	|* If a freight order needs to be generated for the sales order
		|* the release to warehousing activity must not be set executed.
		|* First generate the freight order then the order can be
		|* released.
		return(false)
	endif

	if tdsls041.opol = tcittc.make.customized then
		|* The sales order line is not yet customized. Customize the
		|* the order line before the line is release to warehousing.
		|* The activity cannot be set to executed.
		return(false)
	endif

	|* Generate order commitment
	if activity.present.in.order.procedure("tdinv2201m000",
						l.activity.number.mcs039) then
		if tdsls045.ssls <> 0 and
		   tdsls045.ssls <= tcmcs042.stno(l.activity.number.mcs039) then
			|* generate inventory commitment is part of the order
			|* procedure but not executed. If so then release to
			|*  warehousing can also not set to executed.
			return(false)
		endif
	endif

	return(true)
}

|* This section is disabled because for the current version cisli was not able
|* to convert sales order line ready for "Print sales invoice" to cisli with
|* status confirmed. This will be available in a next version.
|function domain tcbool set.release.to.invoicing.to.executed()
|{
|	domain	tcsess	l.next.activity.on.source.side
|	domain	tcbool	l.release.to.invoicing.done
|
|	|* Initialize
|	l.next.activity.on.source.side = ""
|
|	|* get the activitycode from the activity array when the next acitivity
|	|* on the source side is specified.
|	if tdsls045.ssls <> 0 then
|		l.next.activity.on.source.side =
|			g.old.session.code.activities.array(1,tdsls045.ssls)
|	endif
|
|	on case strip$(l.next.activity.on.source.side)
|	case "tdsls4404m000":
|	|* Print sales invoice
|	case "tdsls4435m000":
|	|* Print proforma invoices
|	case "tfcmg6200m000":
|	|* Link advance receipts for profo
|	case "tdsls4223m000":
|	|* Process delivered sales orders
|		l.release.to.invoicing.done = true
|		break
|	default:
|		l.release.to.invoicing.done = false
|		break
|	endcase
|	return(l.release.to.invoicing.done)
|}
function domain tcbool set.release.to.invoicing.to.executed()
{
	domain	tcsess	l.next.activity.on.source.side

	|* get the activitycode from the activity array when the next acitivity
	|* on the source side is specified.
	if tdsls045.ssls <> 0 then
		l.next.activity.on.source.side =
			g.old.session.code.activities.array(1,tdsls045.ssls)
	endif

	if strip$(l.next.activity.on.source.side) = "tdsls4223m000" then
		|* It can be possible that the invoice number is zero but the
		|* line is already processed. Order qty is completely taken
		|* as backorder qty -> Nothing delivered -> no invoice -> line
		|*processed
		return(true)
	endif

	if tdsls045.invn = 0 then
		|* Invoice not yet printed -> release still required
		return(false)
	endif
	return(true)
}

|*****************************************
|*  Retrieve order acknowledgement related fields
|*****************************************
 function domain tcmcs.str8 get.change.order.sequence.number ()
 {
 	 read.sales.order.change.codes ()
 	 return ( tdsls703.cosn )
 }

 function domain tccdis get.change.reason ()
 {
 	 read.sales.order.change.codes ()
 	 return ( tdsls703.crcd )
 }

 function domain tccdis get.change.type ()
 {
 	 read.sales.order.change.codes ()
 	 return ( tdsls703.ctcd )
 }

 function domain tcmcs.str2 get.acknowledgement.code ()
 {
 	 read.sales.order.change.codes ()
 	 return ( tdsls703.akcd )
 }

 function read.sales.order.change.codes ()
 {
 	 static domain 	tcorno 	 	s.order
	 static domain	tcpono		s.position
 	| * To prevent too much read actions.
 	 if s.order = tdsls045.orno and
	    s.position = tdsls045.pono then
 	 	 return
 	 else
 	 	 s.order = tdsls045.orno
		 s.position = tdsls045.pono
 	 endif
 	| * Only the values are read that are needed for conversion.
 	 select 	 tdsls703.cosn,
 	 	 tdsls703.crcd,
 	 	 tdsls703.ctcd,
 	 	 tdsls703.akcd
 	 from 	 tdsls703
 	 where 	 tdsls703._index1 = { 	 :tdsls045.orno,
 	 	 	 	 	 :tdsls045.pono}
 	 as set with 1 rows
 	 selectdo
 	 selectempty
 	 	 tdsls703.cosn = ""
 	 	 tdsls703.crcd = ""
 	 	 tdsls703.ctcd = ""
 	 	 tdsls703.akcd = ""
 	 endselect
 }

function domain	bcpsty get.default.sales.type()
{
	static	domain	bcpsty	s.default.sales.type		fixed

	if not isspace (s.default.sales.type) then
		return(s.default.sales.type)
	endif

	|*s.default.sales.type filled with bcstc020.type
	g.ret = get.default.sls.type.for.tfacr(
					s.default.sales.type,
					g.error.message)
	return(s.default.sales.type)
}


function domain bccost get.cost.component()
{
	static	domain	bccost	s.new.cost.component	fixed
	static	domain	bccpcp	s.old.cost.component	fixed

	if s.old.cost.component <> tdsls045.cpcp then

		|* makes table bcstc006 current
		g.ret = get.cost.components(
				bcorig.ticpr,
				tdsls045.cpcp,
				"",
				g.error.message)
		s.new.cost.component = bcstc006.cost
		s.old.cost.component = tdsls045.cpcp
	endif

	return(s.new.cost.component)
}

|****************************************
|* functions for tdsls402
|****************************************
								|#800-143063.sn
function domain tcpono	get.original.sequence()
{
	domain	tcpono	l.original.sequence

	l.original.sequence = 0
	l.original.sequence = get.sls.order.line.sequence.number(
				tdsls045.orno,
				tdsls045.pono,
				tdsls045.srnb)

	return(l.original.sequence)
}
								|#800-143063.en

function domain	bcorno	get.purchase.order()
{
	static	domain	tcorno	s.old.purchase.order
	static	domain	bcorno	s.new.purchase.order	fixed

	if s.old.purchase.order <> tdsls041.porn then
		s.old.purchase.order = tdsls041.porn
		if tdsls041.porn <> 0 then
			s.new.purchase.order = convert.number(
							bcckon.act.pur,
							tdsls041.porn)
		else
			s.new.purchase.order = ""
		endif
	endif
	return(s.new.purchase.order)
}

function domain	bcorno	get.quotation.order()
{
	static	domain	tcorno	s.old.quotation.order
	static	domain	bcorno	s.new.quotation.order	fixed

	if s.old.quotation.order <> tdsls041.qono then
		s.old.quotation.order = tdsls041.qono
		if tdsls041.qono <> 0 then
			s.new.quotation.order = convert.number(
							bcckon.sls.quot,
							tdsls041.qono)
		else
			s.new.quotation.order = ""
		endif
	endif
	return(s.new.quotation.order)
}


function domain tcsrnb get.linked.purchase.order.line.sequence()
{
	if tdsls041.porn = 0 then
		return(0)
	endif

	if not isspace(tdsls045.cwar) then
		|* For cross docking orders the purchase sequence
		|* number is not stored in the linked order line
		|* data.
		return(0)
	endif

	|* For direct delivery orders the purchase and the sales
	|* sequences are not the same.
	return(tdsls045.srnb + 1)
}

function domain	tdsls.altn get.quotation.alternative()
{
	domain tdsls.altn	l.alternative

	l.alternative = 0

	if tdsls041.qono <> 0 then
		select	tdsls002.srnb:l.alternative
		from	tdsls002
		where	tdsls002._index1 = {:tdsls041.qono, :tdsls041.qpon}
		and	tdsls002.lsta = tdsls.lsta.processed
		as set with 1 rows
		selectdo
		selectempty
			|* Check in the quotation history
			select	tdsls011.srnb:l.alternative
			from	tdsls011
			where	tdsls011._index1 = {:tdsls041.qono,
						    :tdsls041.qpon}
			and	tdsls011.lsta = tdsls.lsta.processed
			as set with 1 rows
			selectdo
			endselect
		endselect
	endif
	return(l.alternative)
}

function long get.purchase.order.approved()
{
	if tdsls041.porn <> 0 then
		return(target(tcyesno.yes))
	else
		return(target(tcyesno.no))
	endif
}

function domain tcbool	clusters.records.available.for.order.lines()
{
	static		string	s.source.cluster(10)	fixed
	static	domain	tcbool	s.clusters.checked

	if s.clusters.checked then
		if not isspace(s.source.cluster) then
			return(true)
		else
			return(false)
		endif
	endif

	select	tssma102.cloc,
		tssma102._index8
	from 	tssma102
	where	tssma102.orno <> 0
	and	tssma102.pono <> 0
	order by tssma102._index8
	as set with 1 rows
	selectdo
		s.source.cluster = tssma102.cloc
	endselect

	s.clusters.checked = true
	if not isspace(s.source.cluster) then
		return(true)
	endif
	return(false)
}

function string get.purchase.order.date()
{
	|* This function reads the purchase order date
	if tdsls041.porn <> 0 then
		read.linked.purchase.order.data()
		return(get.utc(tdpur041.odat))
	endif
	return(utc(0,0))
}

function domain tcqsl1 get.received.purchase.quantity()
{
	domain	tcqsl1 l.received.quantity.in.inventory.unit

	l.received.quantity.in.inventory.unit = 0.0

	if tdsls041.porn = 0 then
		return(l.received.quantity.in.inventory.unit)
	endif

	|* Received pur quantity equals the delivered quantity on the sales
	|* order line delivery.
	l.received.quantity.in.inventory.unit =
			quantity.from.order.unit.to.inventory.unit(
								tdsls045.dqua)

	return(l.received.quantity.in.inventory.unit)
}

function read.linked.purchase.order.data()
{
	static	domain	tcorno	s.purchase.order
	static	domain	tcpono	s.purchase.order.position

	|* This function reads all required data from the purchase order linked
	|* to the sales order line

	if s.purchase.order = tdsls041.porn and
	   s.purchase.order.position = tdsls041.ppon then
		return
	else
		s.purchase.order = tdsls041.porn
		s.purchase.order.position = tdsls041.ppon
	endif

	select 	tdpur041.odat
	from	tdpur041
	where	tdpur041._index1 = {:tdsls041.porn, :tdsls041.ppon}
	as set with 1 rows
	selectdo
	selectempty
		|* If the Purchase order cannot be found set the purchase order
		|* date to 0
		tdpur041.odat = 0
	endselect
	return
}

function string get.service.cluster()
{
			string	l.source.cluster(10)
	static	domain	tcorno	s.order
	static	domain	tcpono	s.position
	static 	domain	bcstr8	s.target.cluster	fixed

	l.source.cluster = ""

	if not clusters.records.available.for.order.lines() then
		return(l.source.cluster)
	endif

	if s.order = tdsls045.orno and
	   s.position = tdsls045.pono then
		return(s.target.cluster)
	else
		s.order = tdsls045.orno
	   	s.position = tdsls045.pono
	endif

	s.target.cluster = ""

	select	tssma102.cloc:l.source.cluster
	from 	tssma102
	where 	tssma102._index8 = {:tdsls041.orno, :tdsls041.pono}
	as set with 1 rows
	selectdo
	endselect
	g.ret = get.configuration(l.source.cluster, g.error.message)
	s.target.cluster = bcsts005.clst

	return(s.target.cluster)
}

function domain	tccwar	get.purchase.order.advice.warehouse()
{
	domain	tcorno	l.planned.purchase.order
	domain	tccwar	l.planned.purchase.order.cwar	fixed

  	read.purchase.order.advice(
					tdsls045.orno,
					tdsls045.pono,
					l.planned.purchase.order,
					l.planned.purchase.order.cwar)

	|* if purchase order advice is not present -> cwar empty
	|* else cwar is taken from tdinv310.cwar
	return(l.planned.purchase.order.cwar)
}

function double get.tdsls401.copr(
			long	i.element)
{
		double	l.cost.amnt

	l.cost.amnt = fill.mc.field.double(tdsls045.copr, i.element)

	return(tdsls041.oqua * tdsls041.cvqs * l.cost.amnt)
}

|****************************************
|* functions for tdsls406
|****************************************
function long	get.linked.to.contract()
{
	if tdsls041.cono <> 0 then
		return(target(tcyesno.yes))
	endif
	return(target(tcyesno.no))
}
								|#SP1.sn
function dump.actual.delivery()
{
	dump 3 | *  tdsls406 Actual delivery
	if not isspace(g.lot.and.serial.set) then
		|* dump lot and serial set.
		dump 4	|* Lot and serial set
		dump 5	|* lot and serial set details
	endif
	determine.cogs.and.dump()
								|#205562.so
|	dump 6	|* Actual delivery line cogs
								|#205562.eo
}
								|#SP1.en

								|#SP1.sn
function string get.lot.and.serial.set.406()
{
	|* The lot and serial set is already determined with function
	|* get.lot.and.serial.set.401(). in this function the global variable
	|* g.lot.and.serial.set is filled (or empty). If the variable has a
	|* value then this function handles the dumping of the tcibd410 and
	|* tcibd411 tables.
	|* 1) Dump tdsls401 -> detemine g.lot.and.serial.set
	|* 2) dump tdsls406
	|* 3) if lot.and.serial.set then dump tcibd410, tcibd411
	return(g.lot.and.serial.set)
}
								|#SP1.en
								|#SP1.sn
function domain tdltc.clot get.serial.tcibd411()
{
	if item.is.serialized() then
		if g.ilc.implemented then
			|* delivered tdilc401 record selected
			return(tdilc401.clot)
		else
			return(tdsls041.clot)
		endif
	endif
	return("")
}
								|#SP1.en
								|#SP1.sn
function domain tdltc.clot get.lot.tcibd411()
{
	if item.is.lot.controlled() then
		if g.ilc.implemented then
			|* delivered tdilc401 record selected
			return(tdilc401.clot)
		else
			return(tdsls041.clot)
		endif
	endif
	return("")
}
								|#SP1.en
								|#SP1.sn
function domain	tccuni	get.inventory.unit()
{
	get.item.inventory.unit.and.price.group()
	return(g.inventory.unit)
}
								|#SP1.en

								|#SP1.sn


								|#208734.sn								
function long get.sales.order.invoice.line.status()
{
	domain 	tcstno	l.stno.4223

	tdsls0014.session.status("tdsls4223m000", l.stno.4223)
	if tdsls045.pmsk(l.stno.4223;1) = "e" then
		|* Sales order is already processed -> Invoice line set to processed.
		return(target(tdsls.ivls.processed))
	endif

	if tdsls045.invn <> 0 then
		|* Invoice printed and order not processed -> invoice line set to invoiced.
		return(target(tdsls.ivls.invoiced))
	endif
	
	|* Default:
	return(target(tdsls.ivls.approved))
}
								|#208734.en								
								
|****************************************
|* functions for tdsls409
|****************************************

function determine.cogs.and.dump()
{
	|*********************************************
	|* Same kind of logic is used in migration object whinr110.
	|* If changes to this function (or sub function) is required then
	|* verify if the changes are also needed in WH
	|*********************************************

	domain	tdltc.clot l.source.lotcode

 	if tdsls041.opol = tcittc.customized then
 		|* Item is customized use customized item cost prices
		l.source.lotcode = get.lot.tcibd411()
		if isspace(l.source.lotcode) then
 			l.source.lotcode = get.serial.tcibd411()
 		endif
 		get.customized.cost.price(tdsls045.item,
 			      		  l.source.lotcode,
 			      		  tdsls045.dqua)
		return
 	endif

	if not isspace(g.lot.and.serial.set) then
		read.item.data()
		if g.inventory.valuation = tcstva.lot.price then
	 		|* Lotcontrolled or serialized then use lotprice if
	 		|* inventory valuation is not FTP
	 		l.source.lotcode = get.lot.tcibd411()
	 		if isspace(l.source.lotcode) then
	 			l.source.lotcode = get.serial.tcibd411()
	 		endif

	 		get.lot.price(tdsls045.item,
	 			      l.source.lotcode,
	 			      tdsls045.dqua)
			return
		endif
	endif

	get.standard.cost.price(tdsls045.item, tdsls045.dqua, tdsls045.ddat)

}

function get.standard.cost.price(domain	tcitem	i.item,
				 domain	tcqiv1	i.delivery.quantity,
				 domain	tcdate	i.delivery.date)
{
			long		l.result
			long		i
		domain	tcdate		l.hold.date
		domain	tcbool		l.set.date
		domain	tcamnt		l.hold.amnt(3)
		domain	bccost		l.detail.cost.component
	static	domain	bccost		l.material.component
	static	domain	bccost		l.operation.component
	static	domain	bccost		l.surcharge.component
	static	domain	tcamnt		l.material.component.amnt(3)
	static	domain	tcamnt		l.operation.component.amnt(3)
	static	domain	tcamnt		l.surcharge.component.amnt(3)

	|* initialize
 	e = set.mem(l.material.component.amnt, 0.0)
 	e = set.mem(l.operation.component.amnt, 0.0)
 	e = set.mem(l.surcharge.component.amnt, 0.0)
	l.set.date = true

	|* Select cost record
	select	ticpr300.*
 	from	ticpr300
 	where	ticpr300._index1 =  {:i.item}
	and	ticpr300.cntr = ""
	and	ticpr300.indt <= :i.delivery.date
	order by ticpr300.indt desc
	selectdo
  	 	if l.set.date then
  	 		l.set.date = false
  	 		l.hold.date = ticpr300.indt
  	 	else
  	 		if ticpr300.indt <> l.hold.date then
  	 			|* only handle ticpr300 records for the same
  	 			|* date
  	 			break
  	 		endif
 		endif

		|* handle cost component conversion for the record
  	 	for i =  1 to g.mc.depth
  	 		l.hold.amnt(i) =  get.amount.per.cost.component(
							"ticpr300", i)
  	 	endfor

 	   	analyze.cost.component(	i.item,
 	   	   	   	   	"",
 	   	   	   	   	ticpr300.cpcp,
 	   	   	   	   	l.detail.cost.component,
 	   	   	   	   	l.material.component,
 	   	   	   	   	l.operation.component,
 	   	   	   	   	l.surcharge.component,
 	   	   	   	   	l.result)

		handle.analyze.cost.component.result(
					l.result,
					l.detail.cost.component,
					l.hold.amnt,
					i.delivery.quantity,
					l.material.component.amnt,
					l.operation.component.amnt,
					l.surcharge.component.amnt)
	selectempty
		analyze.cost.component( i.item,
 	   	   	   	   	"",
 	   	   	   	   	"",
 	   	   	   	   	l.detail.cost.component,
 	   	   	   	   	l.material.component,
 	   	   	   	   	l.operation.component,
 	   	   	   	   	l.surcharge.component,
 	   	   	   	   	l.result)
	endselect

	handle.dump.aggregated.cost.components(
					l.material.component,
					l.operation.component,
					l.surcharge.component,
					l.material.component.amnt,
					l.operation.component.amnt,
					l.surcharge.component.amnt,
					i.delivery.quantity)
}

function handle.analyze.cost.component.result(
				long	i.result,
			domain	bccost	i.detail.cost.component,
		ref	domain	tcamnt	i.hold.amnt(),
			domain	tcqiv1	i.delivery.quantity,
		ref	domain	tcamnt	io.material.component.amnt(),
		ref	domain	tcamnt	io.operation.component.amnt(),
		ref	domain	tcamnt	io.surcharge.component.amnt())
{

	long		i


	on case i.result
   	case 0:
	|* Cost Component in chart
	|* Detail Cost Component
		g.cost.component = i.detail.cost.component
		for i =  1 to g.mc.depth
			g.cogs.amnt(i) = i.hold.amnt(i) * i.delivery.quantity
		endfor
		dump 6	|* Actual delivery line cogs
		break
	case 1:
	|* Merge to aggregated material cost component
		for i =  1 to g.mc.depth
   	   		io.material.component.amnt(i) =
   	   	   		io.material.component.amnt(i) +	i.hold.amnt(i)
		endfor
		break
	case 2:
	|* Merge to aggregated operation cost component
		for i =  1 to g.mc.depth
   		   	io.operation.component.amnt(i) =
   	   	   		io.operation.component.amnt(i) + i.hold.amnt(i)
		endfor
   	   	break
	case 3:
   	|* Merge to aggregated surcharge cost component
		for i =  1 to g.mc.depth
			io.surcharge.component.amnt(i) =
   	   	   		io.surcharge.component.amnt(i) + i.hold.amnt(i)
		endfor
   	   	break
	endcase
}

function handle.dump.aggregated.cost.components(
			domain	bccost	i.material.component,
			domain	bccost	i.operation.component,
			domain	bccost	i.surcharge.component,
		ref	domain	tcamnt	i.material.component.amnt(),
		ref	domain	tcamnt	i.operation.component.amnt(),
		ref	domain	tcamnt	i.surcharge.component.amnt(),
			domain	tcqiv1	i.delivery.quantity)
{

	long		i

	|* Material Cost Component
	g.cost.component = i.material.component
	for i =  1 to g.mc.depth
		g.cogs.amnt(i) = i.material.component.amnt(i) *
							i.delivery.quantity
	endfor
	dump 6	|* Actual delivery line cogs

	|* Operation Cost Component
	g.cost.component = i.operation.component
	for i =  1 to g.mc.depth
		g.cogs.amnt(i) = i.operation.component.amnt(i) *
							i.delivery.quantity
	endfor
	dump 6	|* Actual delivery line cogs

	|* Surcharge Cost Component
	g.cost.component = i.surcharge.component
	for i =  1 to g.mc.depth
		g.cogs.amnt(i) = i.surcharge.component.amnt(i) *
				 			i.delivery.quantity
	endfor
	dump 6	|* Actual delivery line cogs
}



function get.lot.price(		domain	tcitem		i.item,
				domain	tdltc.clot	i.clot,
				domain	tcqiv1		i.delivery.quantity)
{
		long	l.result
		long	i

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

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

	select	tdltc004.*
	from	tdltc004
	where	tdltc004._index1 =  { 	 :i.item,
 	 	 	 	 	 "",
 	  	  	 	 	 :i.clot}
	selectdo
		for i =  1 to g.mc.depth
 	  		l.hold.amnt(i) = get.amount.per.cost.component(
 	  						 "tdltc004",
 	  						 i)
 	 	endfor

		analyze.cost.component( tdltc004.item,
 	  	 	 	 	"",
 	  	 	 	 	tdltc004.cpcp,
 	  	 	 	 	l.detail.cost.component,
 	  	 	 	 	l.material.component,
 	  	 	 	 	l.operation.component,
 	  	 	 	 	l.surcharge.component,
 	  	 	 	 	l.result)

		handle.analyze.cost.component.result(
					l.result,
					l.detail.cost.component,
					l.hold.amnt,
					i.delivery.quantity,
					l.material.component.amnt,
					l.operation.component.amnt,
					l.surcharge.component.amnt)
	endselect

	handle.dump.aggregated.cost.components(
					l.material.component,
					l.operation.component,
					l.surcharge.component,
					l.material.component.amnt,
					l.operation.component.amnt,
					l.surcharge.component.amnt,
					i.delivery.quantity)
}

function get.customized.cost.price(	domain	tccprj	i.cprj,
					domain	tcitem	i.item,
					domain	tcqiv1	i.delivery.quantity)
{
			long	l.result
			long	i
		domain	bccost	l.detail.cost.component
		domain	tcamnt	l.hold.amnt(3)
	static	domain	bccost	l.material.component
	static	domain	bccost	l.operation.component
	static	domain	bccost	l.surcharge.component
	static	domain	tcamnt	l.material.component.amnt(3)
	static	domain	tcamnt	l.operation.component.amnt(3)
	static	domain	tcamnt	l.surcharge.component.amnt(3)

	|* Initialize
 	e = set.mem(l.material.component.amnt,  0.0)
 	e = set.mem(l.operation.component.amnt, 0.0)
 	e = set.mem(l.surcharge.component.amnt, 0.0)

	select	tipcs350.*
 	from	tipcs350
 	where	tipcs350._index1 =  {:i.cprj, :i.item}
 	selectdo
		for i =  1 to g.mc.depth
			l.hold.amnt(i) =  get.amount.per.cost.component(
							"tipcs350",
							i)
  	 	 endfor
		analyze.cost.component( i.item,
 	   	   	   	   	i.cprj,
 	   	   	   	   	tipcs350.cpcp,
 	   	   	   	   	l.detail.cost.component,
 	   	   	   	   	l.material.component,
 	   	   	   	   	l.operation.component,
 	   	   	   	   	l.surcharge.component,
 	   	   	   	   	l.result)

		handle.analyze.cost.component.result(
					l.result,
					l.detail.cost.component,
					l.hold.amnt,
					i.delivery.quantity,
					l.material.component.amnt,
					l.operation.component.amnt,
					l.surcharge.component.amnt)
	selectempty
		analyze.cost.component( i.item,
					i.cprj,
					"",
					l.detail.cost.component,
					l.material.component,
					l.operation.component,
					l.surcharge.component,
					l.result)
	endselect

	handle.dump.aggregated.cost.components(
					l.material.component,
					l.operation.component,
					l.surcharge.component,
					l.material.component.amnt,
					l.operation.component.amnt,
					l.surcharge.component.amnt,
					i.delivery.quantity)
}
								|#SP1.en





|* End of script **************************************************************
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:450
:FUNC:START
function domain bcstd.fent get.sales.office ()
 {
 	 | *  make bcstc018 current to get sales office (bcstc018.sofd).  
 	 | *  retrieves office based upon Financial company and logistic company  
 	 | *  if function <>  0 then error detected but not handled.  

 	 static 	 domain 	 tcncmp  	 s.company
 	 static 	 domain 	 bcstd.fent 	 s.sales.office
 	 if s.company = tdsls050.comp then
 	 	 return ( s.sales.office )
 	 else
 	 	 s.company = tdsls050.comp
 	 endif
 	 if get.enterprise.unit (
 	 	 tdsls050.comp,
 	 	 get.compnr () ,
 	 	 o.errmess) = 0 then
	 	s.sales.office = bcstc018.sofd
	else
		|* error occurred make office empty, error should be fixed.
		s.sales.office = ""
	endif
 	 return ( s.sales.office )
 }

function domain bcrtyp get.sales.exchange.rate.type()
{
	static	domain	bcrtyp		s.rate.type

	if not isspace(s.rate.type) then
		return(s.rate.type)
	endif

	e = get.exchange.rate.type(
		"SLS",
		o.errmess)
	s.rate.type = bcstc011.rtyp

	return(s.rate.type)
}

 function extern domain bcstc.ccnt get.contact (
 	 	 	 	 domain tccuno 		 i.source.customer,
 	 	 	 	 domain tdsmi.cper 	 i.source.contact )
 {
| * Expl 	 : This function converts the Sales Order Contact. If the Sales contact on the source  
| * 	 side is empty then the function returns empty.  
| * Pre 	 : na  
| * Post 	 : na  
| * Input 	 : i.source.customer -> Customer of the order  
| * 	 i.source.contact -> Tobe converted ( source ) contact  
| * Output : na  
| * Return : Converted Sales contact  
 	 domain 	 bcstc.keys 	 l.contact.key 	 fixed
 	 domain 	 bcstc.ccnt 	 l.converted.contact
 	 l.converted.contact = ""
 	 l.contact.key = ""
 	 if not isspace ( i.source.contact ) then
 	 	 l.contact.key ( 1;6 ) = i.source.customer
 	 	 l.contact.key ( 7;6 ) = i.source.contact
 	 	 if get.cross.reference.for.contact.code (
 	 	 	 	 	 "tdsmi120",
 	 	 	 	 	 l.contact.key,
 	 	 	 	 	 o.errmess ) = 0 then
 	 	 	 l.converted.contact = bcstc005.ccnt
 	 	 endif
 	 endif
 	 return ( l.converted.contact )
 }
 
function extern long convert.shipping.constraint ( domain tcyesno 	 i.source.field )
 {
| * Expl 	 : This function converts the ship complete field in the shipping constraint field.  
| * 	 Ship complete is yes on the source corresponds with Ship complete  
| * 	 on the target side ( value = 4 ) . Ship complete is no on the source side  
| * 	 corresponds with no shipping constraints on the target side ( value = 1 )  
| * Pre 	 : na  
| * Post 	 : na  
| * Input 	 : i.source.field -> Ship complete value  
| * Output : na  
| * Return : Converted shipping constraint  
 	 if i.source.field = tcyesno.yes then
 	 	 return ( target ( tdscon.oc ))
 	 else
 	 	 return ( target ( tdscon.none ))
 	 endif
 }
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:td
:TCMOD:sls
:TFLNO:451
:FUNC:START
function read.sales.order.header.history()
{
	if tdsls050.orno = tdsls051.orno and
	   ((tdsls050.trdt = tdsls051.trdt and
	     tdsls050.trtm <= tdsls051.trtm) or
	    tdsls050.trdt < tdsls051.trdt) then
	    	return
	endif

	select	tdsls050._index1, tdsls050.comp, tdsls050.scom,
		tdsls050.cfrw, tdsls050.cper, tdsls050.cotp,
		tdsls050.cdel,						|#209434.n
		tcmcs042.casi
	from	tdsls050, tcmcs042
	where	tdsls050._index1 = {:tdsls051.orno}
	and	((tdsls050.trdt = :tdsls051.trdt and
		  tdsls050.trtm <= :tdsls051.trtm) or
		 tdsls050.trdt < :tdsls051.trdt)
	and	tdsls050.cotp refers to tcmcs042 unref clear
	order by tdsls050._index1 desc
	as set with 1 rows
	selectdo
		|* The last recorded record is read which is inserted on or
		|* just before the line was inserted into history.
	endselect
}

function string convert.sales.order()
{
	static	domain	bcorno	s.orno		fixed
	static	domain	tcorno	s.sls051.orno

	if s.sls051.orno <> tdsls051.orno then
		s.sls051.orno = tdsls051.orno
		s.orno = convert.number(bcckon.act.sls, tdsls051.orno)
	endif

	return(s.orno)
}

function long convert.serial.number()				|#IDX-ERR.sn
{
	if tdsls051.ckor = 2 then
		|* In Baan 6 the record type (tdsls451.ckor) is
		|* missing in the indices 6 and 7. For normal order
		|* handling these indices are unique due to the way
		|* tdsls451.sern is filled in Baan 6. During the
		|* migration a duplicate record error can occur when
		|* an order line is cancelled. In that case an intake
		|* record and a cancellation record are inserted,
		|* both with tdsls051.sern is 0 and sometimes both
		|* with the same transaction date and time.
		|* For this situation the sern is raised with 10 for
		|* the cancellation record to avoid duplicate
		|* records for the indices 6 and 7.
		return(tdsls051.sern + 10)
	endif

	return(tdsls051.sern)
}								|#IDX-ERR.en

function domain bcstc.bpid convert.business.partner()
{
	static	domain	bcstc.bpid	s.business.partner	fixed
	static	domain	tccuno		s.sls051.cuno

	if s.sls051.cuno <> tdsls051.cuno then
		s.sls051.cuno = tdsls051.cuno
		s.business.partner = get.business.partner.code(
					tdsls051.cuno,
					bcstc.orgn.cuno,
					g.error.mess)
	endif

	return(s.business.partner)
}

function domain bcstc.ccnt convert.ship.to.contact()
{
		domain	bcstc.keys	l.key 	 	fixed
	static	domain	bcstc.ccnt	s.contact	fixed
	static	domain	tdsmi.cper	s.sls050.cper	fixed

	l.key = ""

	if isspace(tdsls050.cper) then
		s.contact = ""
		s.sls050.cper = ""
		return(s.contact)
	endif

	if s.sls050.cper <> tdsls050.cper then
		l.key(1;6) = tdsls051.cuno
		l.key(7;6) = tdsls050.cper
		if get.cross.reference.for.contact.code(
					"tdsmi120",
					l.key,
					g.error.mess) = 0 then
			s.contact = bcstc005.ccnt
		endif
		s.sls050.cper = tdsls050.cper
	endif

	return(s.contact)
}

function domain bcitem convert.item()
{
	static	domain	tcittc		s.sls051.opol
	static	domain	tccprj		s.sls051.cprj
	static	domain	tcitem		s.sls051.item
	static	domain	bcitem		s.new.item	fixed
		domain	bcstc.nprj	l.project	fixed

	if s.sls051.opol <> tdsls051.opol or
	   s.sls051.cprj <> tdsls051.cprj or
	   s.sls051.item <> tdsls051.item then
	   	s.sls051.opol = tdsls051.opol
		s.sls051.cprj = tdsls051.cprj
		s.sls051.item = tdsls051.item

		if tdsls051.opol = tcittc.customized then
			l.project = get.ti.project(tdsls051.cprj)
		else
			l.project = ""
		endif

		s.new.item = segment.item.code(tdsls051.item, l.project)
	endif

	return(s.new.item)
}

function long convert.product.variant()
{
			long	l.ret
	static	domain	tccpva	s.sls051.cpva		fixed
	static	domain	bccpvl	s.new.product.variant

	if isspace(tdsls051.cpva) then
		return(0)
	endif

	if s.sls051.cpva = tdsls051.cpva then
		return(s.new.product.variant)
	endif

	s.sls051.cpva = tdsls051.cpva

	l.ret = get.new.product.variant(tdsls051.cpva, g.error.mess)
	s.new.product.variant = bcsti001.cpvl

	return(s.new.product.variant)
}

function convert.tax()
{
			long	l.ret
	static	domain	tcccty	s.sls051.ccty	fixed
	static	domain	tccvat	s.sls051.cvat	fixed

	if s.sls051.ccty <> tdsls051.ccty or
	   s.sls051.cvat <> tdsls051.cvat then
	   	l.ret = get.tax.by.country.code(
				tdsls051.ccty,
				tdsls051.cvat,
				var.tdsls451.ccty,
				var.tdsls451.cvat,
				g.error.mess)
		s.sls051.ccty = tdsls051.ccty
	   	s.sls051.cvat = tdsls051.cvat
	endif
}

function long get.make.customized()
{
	if tdsls051.opol = tcittc.make.customized then
		return(target(tcyesno.yes))
	endif

	return(target(tcyesno.no))
}

|function domain tcqsl1 convert.delivered.quantity()		|#209062.so
|{
|	|* tdsls451.qidl is filled with tdsls051.qana after being converted
|	|* into the inventory unit of the item.
|
|		domain	tccuni	l.inventory.unit
|	static	domain	tcqsl1	s.qty.in.inv.unit
|	static	domain	tcitem	s.sls051.item
|	static	domain	tccprj	s.sls051.cprj
|	static	domain	tcittc	s.sls051.opol
|
|	if s.sls051.item <> tdsls051.item or
|	   s.sls051.cprj <> tdsls051.cprj or
|	   s.sls051.opol <> tdsls051.opol then
|		s.sls051.item = tdsls051.item
|	   	s.sls051.cprj = tdsls051.cprj
|	   	s.sls051.opol = tdsls051.opol
|		l.inventory.unit = get.item.inventory.unit()
|
|		s.qty.in.inv.unit = itccom0007.qty.domain.round(
|				(tdsls051.qana * tdsls051.cvqs),
|				"tcqsl1",
|				l.inventory.unit)
|	endif
|
|	return(s.qty.in.inv.unit)
|}								|#209062.eo
								|#209062.sn
function domain tcqsl1 convert.delivered.quantity()	
{
	|* tdsls451.qidl is filled with tdsls051.qana after being converted
	|* into the inventory unit of the item.

		domain	tcqsl1	l.qty.in.inv.unit
	static	domain	tccuni	s.inventory.unit
	static	domain	tcitem	s.sls051.item
	static	domain	tccprj	s.sls051.cprj
	static	domain	tcittc	s.sls051.opol

	if tdsls051.ddtb = 0 then
		|* not delivered -> delivered quantity is zero
		return(0.0)
	endif

	if s.sls051.item <> tdsls051.item or
	   s.sls051.cprj <> tdsls051.cprj or
	   s.sls051.opol <> tdsls051.opol then
		s.sls051.item = tdsls051.item
	   	s.sls051.cprj = tdsls051.cprj
	   	s.sls051.opol = tdsls051.opol
		s.inventory.unit = get.item.inventory.unit()
	endif

	l.qty.in.inv.unit = itccom0007.qty.domain.round(
				(tdsls051.qana * tdsls051.cvqs),
				"tcqsl1",
				s.inventory.unit)

	return(l.qty.in.inv.unit)
}
								|#209062.en
function domain tccuni convert.quantity.unit()
{
	if tdsls051.ckor = 1 or
	   tdsls051.ckor = 2 then
	   	return(tdsls051.cuqs)
	endif

	return(get.item.inventory.unit())
}

function domain tcconv convert.factor.sls.unit.to.inv.unit()
{
	if tdsls051.ckor = 1 or
	   tdsls051.ckor = 2 then
	   	return(tdsls051.cvqs)
	endif

	return(1.0)
}

function domain tccuni get.item.inventory.unit()
{
	|* For customized items the inventory unit is retrieved from tipcs021.
	|* For non-customized items the inventory unit is retrieved from
	|* tiitm001.

	domain	tccuni	l.inventory.unit

	l.inventory.unit = ""

	if not isspace(tdsls051.cprj) and
	   tdsls051.opol = tcittc.customized then
	   	select	tipcs021.cuni:l.inventory.unit
		from	tipcs021
		where	tipcs021._index1 = {:tdsls051.cprj, :tdsls051.item}
		as set with 1 rows
		selectdo
		endselect
	else
		select	tiitm001.cuni:l.inventory.unit
		from	tiitm001
		where	tiitm001._index1 = {:tdsls051.item}
		as set with 1 rows
		selectdo
		endselect
	endif

	return(l.inventory.unit)
}

function domain bcstd.fent convert.contract.office()
{
			long		l.ret
		domain	bcstd.fent	l.office	fixed
	static	domain	tcncmp		s.sls050.comp

	if tdsls051.cono = 0 then
		l.office = ""
		s.sls050.comp = 0
		return(l.office)
	endif

	if s.sls050.comp <> tdsls050.comp then
		s.sls050.comp = tdsls050.comp
		l.ret = get.enterprise.unit(
				tdsls050.comp,
				get.compnr(),
				g.error.mess)
	endif

	return(bcstc018.sofd)
}

function long convert.shipping.constraint()
{
	if tdsls050.scom = tcyesno.yes then
		return(target(tdscon.oc))
		|* Ship Order Complete
	endif

	if tdsls051.scom = tcyesno.yes then
		return(target(tdscon.sc))
		|* Ship Line Complete
	endif

	return(target(tdscon.none))
}

function domain bccadr convert.address()
{
	static	domain	bccadr	s.address	fixed
	static	domain	tcorno	s.sls051.orno
	static 	domain	tcpono	s.sls051.pono

	if s.sls051.orno <> tdsls051.orno or
	   s.sls051.pono <> tdsls051.pono then
		s.sls051.orno = tdsls051.orno
		s.sls051.pono = tdsls051.pono
		s.address = convert.sales.address(
				tdsls051.cdel,
				tdsls051.cuno,
				tdsls051.orno,
				tdsls051.pono,
				0)	|* address type: delivery address
	endif

	return(s.address)
}

function domain	bccadr get.ship.to.address()			|#209434.sn
{
	static	domain	tcorno	s.order
	static	domain	tcpono	s.position
	static	domain	bccadr	s.new.address.code	fixed
		domain	tccdel	l.delivery.address		
	

	if s.order = tdsls051.orno and
	   s.position = tdsls051.pono then
       		return(s.new.address.code)
       	else
	       	s.order = tdsls051.orno

	       	s.position = tdsls051.pono
		s.new.address.code = ""
       	endif
       	
       	if isspace(tdsls051.cdel) then				
       		|* if the delivery address is not filled on the sales order line
       		|* then take the delivery address from the sales order header.
       		l.delivery.address = tdsls050.cdel
       	else
       		l.delivery.address = tdsls051.cdel
       	endif							
       	
        s.new.address.code = convert.sales.address(
				        l.delivery.address,
        				tdsls051.cuno,
        				tdsls051.orno,
        				tdsls051.pono,
        				0)
        return(s.new.address.code)
}								|209434.en



function long convert.delivery.type()
{
	if tdsls051.drct = tcyesno.yes then
		return(target(tdsls.dltp.direct.delivery))
	endif

	if tdsls051.porn <> 0 then
		return(target(tdsls.dltp.cross.docking))
	endif

	return(target(tdsls.dltp.warehousing))
}

function domain tcmcs.str2 get.acknowledgment.code()
{
	|* Only the value is read that is needed for conversion; when
	|* there are more than one records present for the order

	|* line, then the latest record is taken that is created
	|* before the order line history transaction date.

	static	domain	tcmcs.str2	s.acknowl.code fixed
	static	domain	tcorno		s.order
	static	domain	tcpono		s.position

	if s.order = tdsls051.orno and
	   s.position = tdsls051.pono and
	   ((tdsls704.date = tdsls051.trdt and
	     tdsls704.time <= tdsls051.trtm) or
	    tdsls704.date < tdsls051.trdt) then
		return(s.acknowl.code)
		|* last retrieved record is also actual for this request
	else
		s.order = tdsls051.orno
		s.position = tdsls051.pono
		s.acknowl.code = ""
	endif

	select	tdsls704.akcd:s.acknowl.code, tdsls704._index1
	from	tdsls704
	where	tdsls704._index1 = {	:tdsls051.orno,
					:tdsls051.pono}
	and	((tdsls704.date = :tdsls051.trdt and
		  tdsls704.time <= :tdsls051.trtm) or
		 tdsls704.date < :tdsls051.trdt)
	order by tdsls704._index1 desc
	as set with 1 rows
	selectdo
	endselect

	return(s.acknowl.code)
}

function long get.price.origin()
{
	if not double.cmp(tdsls051.qana, 0.0, 0.0001) then
		return(target(tdgen.porg.not.applicable))
	endif

	on case tdsls051.plev
	case tdplev.contract:
		return(target(tdgen.porg.contract))
	case tdplev.cust.item:
	case tdplev.plst.item:
		return(target(tdgen.porg.price.structure))
	case tdplev.ilst:
		return(target(tdgen.porg.def.pr.book))
	case tdplev.item:
		return(target(tdgen.porg.item.sls))
	case tdplev.not.def:
		return(target(tdgen.porg.not.applicable))
	default:
		break
	endcase

	return(target(tdgen.porg.manual))
}

function long get.discount.origin(long	i.element)
{
	if not double.cmp(tdsls051.pric, 0.0, 0.0001) then
		return(target(tdgen.dorg.not.applicable))
	else
		if not double.cmp(tdsls051.disc(i.element), 0.0, 0.0001) and
		   not double.cmp(tdsls051.ldam(i.element), 0.0, 0.0001) then
		   	return(target(tdgen.dorg.not.applicable))
		endif
	endif

	on case tdsls051.dlev(i.element)
	case tddlev.contract:
		return(target(tdgen.dorg.contract))
	case tddlev.cust:
	case tddlev.cust.item:
	case tddlev.cust.pgrp:
	case tddlev.plst:
	case tddlev.plst.item:
	case tddlev.plst.pgrp:
	case tddlev.ilst:
		return(target(tdgen.dorg.disc.structure))
	case tddlev.not.def:
		return(target(tdgen.dorg.not.applicable))
	default:
		break
	endcase

	return(target(tdgen.dorg.manual))
}

function domain bcstr9 get.price.matrix.definition()
{
		long		l.ret
	static	domain	tdplev	s.price.level
	static	domain	bcstr9	s.matrix.definition	fixed

	if not double.cmp(tdsls051.qana, 0.0, 0.0001) then
		return("")
	endif

	if s.price.level <> tdsls051.plev then
		s.price.level = tdsls051.plev
		l.ret = get.price.matrix.definition.sls(
				tdsls051.plev,
				s.matrix.definition,
				g.error.mess)
	endif

	return(s.matrix.definition)
}

function long get.discount.matrix.type(long	i.element)
{
	if not double.cmp(tdsls051.pric, 0.0, 0.0001) then
		|* No Price defined
		return(target(tdpcg.maty.none))
	endif

	if not double.cmp(tdsls051.disc(i.element), 0.0, 0.0001) and
	   not double.cmp(tdsls051.ldam(i.element), 0.0, 0.0001) then
		|* No discount percentage / amount defined.
		return(target(tdpcg.maty.none))
	endif

	if tdsls051.dlev(i.element) = tddlev.manual or
	   tdsls051.dlev(i.element) = tddlev.not.def then
		return(target(tdpcg.maty.none))
	endif

	return(target(tdpcg.maty.soldsc))
}

function domain bcstr9 get.discount.matrix.definition(long	i.element)
{
			long	l.ret
	static	domain	bcstr9	s.discount.matrix.definition	fixed
	static	domain	tddlev	s.discount.level

	if not double.cmp(tdsls051.pric, 0.0, 0.0001) then
		|* No Price defined
		return("")
	endif

	if not double.cmp(tdsls051.disc(i.element), 0.0, 0.0001) and
	   not double.cmp(tdsls051.ldam(i.element), 0.0, 0.0001) then
		|* No discount percentage / amount defined.
		return("")
	endif

	if s.discount.level <> tdsls051.dlev(i.element) then
		s.discount.level = tdsls051.dlev(i.element)
		l.ret = get.discount.matrix.definition.sls(
				tdsls051.dlev(i.element),
				s.discount.matrix.definition,
				g.error.mess)
	endif

	return(s.discount.matrix.definition)
}

function domain tccdsc get.discount.code(
				long	i.element)
{
	if not double.cmp(tdsls051.pric, 0.0, 0.0001) then
		|* No Price defined
		return("")
	endif

	if not double.cmp(tdsls051.disc(i.element), 0.0, 0.0001) and
	   not double.cmp(tdsls051.ldam(i.element), 0.0, 0.0001) then
		|* If no discount defined return None.
		return("")
	endif

	return(tdsls051.cdis(1, i.element))
}

function long convert.price.changes.allowed.after.delivery()
{
	if tdsls051.ddtb <> 0 and
	   tdsls051.invn = 0 then
		return(target(tcyesno.yes))
	endif

	return(target(tcyesno.no))
}

function long get.lot.selection()
{
	|* If lot control is lot by unit then lot is converted into a serial
	|* Serial will get lot code, serial selection is specific
	|* Lot is made empty and lot selection is any

	if isspace(get.warehouse()) then
		return(target(tclsel.any))
	endif

	if item.is.an.serialized.item() then
		return(target(tclsel.any))
	endif
	return(etol(tdsls051.lsel))
}

function domain tdltc.clot get.lot()
{
	|* See comments for function get.lot.selection()
	if isspace(get.warehouse()) then
		return("")
	endif

	if item.is.an.serialized.item() then
		return("")
	endif
	return(tdsls051.clot)
}

function long get.serial.selection()
{
	if isspace(get.warehouse()) then
		return(target(tdssel.any))
	endif

	if item.is.an.serialized.item() and
	   tdsls051.lsel = tclsel.specific then
		return(target(tdssel.specific))
	endif
	return(target(tdssel.any))
}

function domain tdltc.clot get.serial()
{
	|* See comments for function get.lot.selection()
	if isspace(get.warehouse()) then
		return("")
	endif

	if tdsls051.lsel = tclsel.any then
		return("")
	endif

	if item.is.an.serialized.item() then
		return(tdsls051.clot)
	endif
	return("")
}

function domain tcbool item.is.an.serialized.item()
{
	static	domain	tccprj	s.project		fixed
	static	domain	tcitem	s.item			fixed
	static	domain	tcbool	s.item.is.serialized

	if s.project <> tdsls051.cprj or
	   s.item <> tdsls051.item then
		s.item.is.serialized = serialized.item(
						tdsls051.cprj,
						tdsls051.item)
		s.project = tdsls051.cprj

		s.item = tdsls051.item
	endif
	return(s.item.is.serialized)
}

function domain tccwar	get.warehouse()
{
	if tdsls051.drct = tcyesno.yes then
		return("")
	else
		return(tdsls051.cwar)
	endif
}

function long get.inventory.handling()
{
	if tdsls051.isss = tcisss.issue.comp then
		return(target(tcinup.by.component))
	endif
	|* In all other cases issue by main item
	return(target(tcinup.by.main.item))
}

								|#203034.sn
|***************************************************************************
|* Convert cost prices (home cur/ inventory unit to
|* Cost amounts (in home cur/ inventory unit)
|***************************************************************************
function domain	tcamnt get.copr.amount( long i.index)
{
	domain	tcamnt	l.cost.amount

	l.cost.amount = fill.mc.field.double(tdsls051.copr, i.index) *
			qty.from.order.unit.to.inventory.unit(tdsls051.qana)
	return(l.cost.amount)
}

function domain tcqsl1 qty.from.order.unit.to.inventory.unit (
			domain	tcqrd1	i.quantity.in.order.unit )
{
 	 return ( itccom0007.qty.domain.round (
 	 	 	 i.quantity.in.order.unit * tdsls051.cvqs,
 	 	 	 "tcqsl1",
 	 	 	 get.item.inventory.unit()))
}

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

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tf
:TCMOD:acp
:TFLNO:000
:FUNC:START
function domain tfgld.seri get.default.serie(			|#200-95756.sn
		domain	tfgld.ttyp	i.transaction.type)
{
	domain	tfgld.seri	default.serie
	default.serie = 0
	
	select	tfgld011.dsrn:default.serie
	from	tfgld011
	where	tfgld011._index1 = {:i.transaction.type}
	as set with 1 rows
	selectdo
	endselect

	return(default.serie)
}								|#200-95756.en

function domain tcbool category.purchace.invoice(		|#200-95754.sn
		domain	tfgld.ttyp	i.transaction.type)
{
	domain	tfgld.seri	dummy.serie
	
	select	tfgld011.dsrn:dummy.serie
	from	tfgld011
	where	tfgld011._index1 = {:i.transaction.type}
	and	tfgld011.catg = tfgld.catg.purchase.inv
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}								|#200-95754.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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
	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)
	|* 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
	
	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:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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,
	 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)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tf
:TCMOD:acp
:TFLNO:240
:FUNC:START
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, 
		 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)
{
	string		bo.reference(40) 
	
	i.sequence =  get.order.line.sequence(i.sequence)
	
	bo.reference = str$(i.position) & chr$(47) & str$(i.sequence)

	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 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)
}

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
	static		long		s.array.length

	if tdpur045.date = 0 then
		return(0)
	endif

	if tdpur045.reno = 0 then
		return(0)
	endif

	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
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tf
:TCMOD:acp
:TFLNO:250
:FUNC:START
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
}

function domain tcpono get.receipt.line()				|#149975.sn
{
			long		ret			|#215078.sn
		domain	tcmcs.s200m	error.message		|#215078.en
		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
	static		long		s.array.length

	if tdpur045.date = 0 then
		return(0)
	endif

	if tdpur045.reno = 0 then
		return(0)
	endif

	if tdpur041.oqua < 0 then				|#215078.sn
		ret = get.shipment.number.and.line(
			tckoor.act.pur,
			tdpur045.orno,
			tdpur045.pono,
			get.order.line.sequence(tdpur045.srnb),
			0,
			error.message)	|* ref
		return(bcswh008.shln)
	endif							|#215078.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

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
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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 
		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

}

|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
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tf
:TCMOD:acr
:TFLNO:202
:FUNC:START
|#200-101236.sn
function domain tcbool record.present.in.tfacr200()
{

	select tfacr200.*
	from  tfacr200
	where	tfacr200._index1 = {:tfacr202.ttyp,:tfacr202.ninv,:tfacr202.line}
	as set with 1 rows
	selectdo
		return(true)
	selectempty
		return(false)
	endselect

	return(false)

}
								|#200-101236.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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  
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tf
:TCMOD:gld
:TFLNO:003
:FUNC:START
function domain tcbool description.present(domain tfgld.dimn i.type)
{
	domain tfgld.dimn	dummy.type
	
	select	tfgld002.dimn:dummy.type
	from	tfgld002
	where	tfgld002._index1 = {:i.type}
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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

function void set.attributes.non.integration.account()
{
	if tfgld008.atyp =  tfgld.atyp.profitloss then 
	 	 if tfgld008.subl >  0 then  
	 	 	 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
	 endif
  
	 if tfgld008.iprj =  empty then  
	  	 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  

	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)
			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)
			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)
			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)
			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)
			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	

	endif	
	 
	 if tfgld008.ifas =  empty then  
	  	 var.tfgld008.ifas =  target(tfgld.ifas.not.applicable)
		 			|* Not Applicable
	 else  
	  	 var.tfgld008.ifas =  etol(tfgld008.ifas)  
	 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

}

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

	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)
{
		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)
		dump 5
	endselect
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ti
:TCMOD:bom
:TFLNO:010
:FUNC:START
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
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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.opr.amnt
	|* - aggr.sur.amnt
	|* - 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.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	tcqnu1	i.nuni,  
		domain	tccref	i.cref, 
		domain	tckitm	i.kitm)  

	|*------------------------------------------------------------------
	|* this function aggregates i.amnt and i.nuni into:
	|* - aggr.mat.amnt
	|* - aggr.opr.amnt
	|* - aggr.sur.amnt
	|* - 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.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.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.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 detailed. 
		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

function dump.1.ticpr200.eff.cpcp()
{	|* 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 |* ticpr200
|	endif						|#zero.o

	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() then					|#213940.n
		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() then					|#213940.n
		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()
{
	|* Cost Price by Calc. Code, Item, Detailed Cost Comp. 
	dump 3  |* ticpr205
}

function dump.4.ticpr300.eff.cpcp()
{	|* 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  |* ticpr300
|	endif						|#zero.o

	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() then					|#213940.n
		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() then					|#213940.n
		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()
{
	|* 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()
{
	|*-----------------------------------------------------
	|* 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
	|*--------------------------------------------------------------

	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 9  |* ticpr240
|	endif						|#zero.o

	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() then					|#213940.n
		dump 9  |* ticpr240
	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() then					|#213940.n
		dump 9  |* ticpr240
	endif							|#213940.n
|	endif						|#zero.o
	

}

function dump.9.ticpr240.eff.cpcp()
{

	|*-----------------------------------------------------
	|* 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
	|*-------------------------------------------------------------

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


function dump.10.ticpr340.aggr.cpcp()
{
	|*-----------------------------------------------------
	|* 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
	|*--------------------------------------------------------------

	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 10  |* ticpr340
|	endif						|#zero.o

	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() then					|#213940.n
		dump 10	  |* ticpr340
	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() then					|#213940.n
		dump 10  |* ticpr340
	endif							|#213940.n
|	endif						|#zero.o
	

}

function dump.10.ticpr340.eff.cpcp()
{

	|*-----------------------------------------------------
	|* 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
	|*-------------------------------------------------------------

	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

}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ti
:TCMOD:cst
:TFLNO:001
:FUNC:START
function long determine.first.operation(domain tcpdno   i.pdno)
{ 
	long	tmp.opno
	
 	 select 	 tisfc001.oprp  
 	 from 	 tisfc001  
 	 where 	 tisfc001._index1 =  {:i.pdno}  
 	 and 	 tisfc001.oprp =  tcyesno.yes  
 	 as set with 1 rows  
 	 selectdo  
 	 	 select 	 min (tisfc010.opno) :tmp.opno  
 	 	  from 	 tisfc010  
 	 	 where 	 tisfc010._index1 =  {:i.pdno}  
 	 	 as set with 1 rows  
 	 	  selectempty  
 	 	  	 | *  order can be reset  
 	 	  	 select 	 min (ticst002.opno):tmp.opno  
 	 	  	 from 	 ticst002  
 	 	  	 where 	 ticst002.pdno =  :i.pdno  
 	 	  	 as set with 1 rows 	 	 	  
 	 	  	 selectdo  
 	 	  	 endselect  
 	 	  endselect  
 	  selectempty  
 	  	tmp.opno = 0
 	 endselect  

	return(tmp.opno)
 }
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ti
:TCMOD:cst
:TFLNO:300
:FUNC:START
|#214721.sn
function domain	tcitem get.item.sfc.order(
			domain	tcorno	i.orno)
{
	select	tisfc001.mitm
	from	tisfc001
	where	tisfc001._index1 = {:i.orno}
	as set with 1 rows
	selectdo
		return(tisfc001.mitm)
	endselect

	return("")
}
						|#214721.en

function domain tcbool  tipcs340.exists(				|#214707.sn
		domain tccprj	i.cprj, 
		domain	tcorno	i.orno) 
{
	select	tipcs340.sern
	from	tipcs340
	where	tipcs340._index1 = {:i.cprj, :i.orno}
	selectdo
		return(true)
	endselect
	return(false)
}								|#214707.en

function handle.credit.amounts()
{ 	  
 	 | *  handle credit amounts. Copied from ticst0201 (Baan IV)  
 	 | *  is this correct ?   
 	 if tfgld410.dbcr =  tfgld.dbcr.credit then  
| 	 	 tfgld410.amth =  tfgld410.amth * -  1  	|* MC.o
		negative.amount(tfgld410.amth)		|* MC.n
 	 	 tfgld410.nuni =  tfgld410.nuni * -  1  
 	 endif  
 }

	
function set.var.calc.office(
		domain	tcncmp	i.fcom,
		domain	tcncmp	i.ocom)
{	  
 	 | *  Determine Order Calc. Office =  Project Calculation Office  
	 var.tisfc001.clco =  get.calc.office(  
 	 		 	 	 i.fcom, 	 	 | *  fin. comp  
 	 	 		 	 i.ocom) 	 | *  original comp =  log. comp  
 	 var.tipcs020.clco =  var.tisfc001.clco  
}

function set.var.cprj(
		domain	tccprj	i.cprj)
{
	  
 	 | *  Determine project number var.cprj.  
 	 if not isspace (i.cprj) then  
 	 	 var.cprj =  get.ti.project(i.cprj)  
 	 else  
 	 	 var.cprj =  empty()  
 	 endif  
}
	


function domain bccwoc get.calc.office(
				domain	tcncmp	i.fin.comp,
				domain	tcncmp i.log.comp)
{
	long	ret.val

	static	domain	tcncmp	hold.fin.comp
	static	domain	tcncmp	hold.log.comp
	static	domain	bccwoc	hold.clco

	if (i.fin.comp <> hold.fin.comp ) or
	   (i.log.comp <> hold.log.comp) then

		ret.val =    get.enterprise.unit(  
 	 	 		i.fin.comp,  
 	 	 		i.log.comp,  
 	 	 		err.mess)
  
		hold.fin.comp	= i.fin.comp
		hold.log.comp	= i.log.comp
		hold.clco	= bcstc018.wccd
	endif

	return(hold.clco)		
}


function domain tcmcs.st20m set.ticst300.guid()
{
	string	tmp.guid(22)
	string	tmp.seq.nr(9)

	seq.nr.cst = seq.nr.cst + 1
	 tmp.seq.nr = edit$(seq.nr.cst, "999999999")

	tmp.guid = 	"SFC-" &
			str$(var.current.company) &  "-" & 
			tmp.seq.nr
			

	return(tmp.guid)
}


function domain tcmcs.st20m  set.tipcs300.guid()
{
	string	tmp.guid(22)
	string	tmp.seq.nr(9)

	seq.nr.pcs = seq.nr.pcs + 1
	 tmp.seq.nr = edit$(seq.nr.pcs, "999999999")


	tmp.guid = 	"PCS-" &
			str$(var.current.company) &  "-" & 
			tmp.seq.nr
	return(tmp.guid)
}


function domain tcbool convert.to.ticst300()
	
{
	|* Return: 
	|*  False = do not migrate this tror/fitr
	|*  True  = do migrate this tror/fitr

	domain tcbool	do.migrate
	
	do.migrate = false

	if tfgld410.tror =  tctror.prd then
		var.tror	 = target(tctror.prd)
		var.orno = convert.number( bcckon.act.sfc, tfgld410.orno)
	
		on case tfgld410.fitr
			case tcfitr.issue: 
			case tcfitr.material:
				do.migrate 	= true
				var.fitr 		= target(tcfitr.issue)
				var.entp.f 	= target(tcemm.enty.warehouse)
				var.entc.f	= tfgld410.cwar
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tisfc001.clco
				var.cpcp.f	= get.eff.cpcp(
							tfgld410.cpcp,
							tfgld410.item,
							tfgld410.cprj,
							ticpr010.cref)
				var.cpcp.t	= var.cpcp.f
				var.item	= get.item.code.material()
				break
			case tcfitr.operation: 
				do.migrate 	= true
				var.fitr 		= target(tcfitr.operation)
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	=  var.tisfc001.clco 	|* no department in tfgld410 
									|* is in tfgld415; but performance!
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tisfc001.clco
				var.cpcp.f	= get.mapped.cpcp(tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= get.item.code.operation()
				break
			case tcfitr.subcontract: 
				do.migrate 	= true
				var.fitr 		= target(tcfitr.subcontracting)
				if isspace(tfgld410.cprj) then
				    var.entp.f 	= target(tcemm.enty.pur.office)
				    var.entc.f	= get.purchase.office()	 |* use default from Enterprise Unit
				else
				    var.entp.t	= target(tcemm.enty.workcenter)
				    var.entc.t	= var.tisfc001.clco	|* pcs clco  = sfc clco
				endif
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tisfc001.clco
				var.cpcp.f	= get.mapped.cpcp(tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= get.item.code.operation()
				break
			case tcfitr.prd.result: 
				do.migrate 	= true
				var.fitr 		= target(tcfitr.add.calc.offvar)
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tisfc001.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tisfc001.clco
				var.cpcp.f	=  get.eff.cpcp(
							tfgld410.cpcp,
							tfgld410.item,
							tfgld410.cprj,
							ticpr010.cref)
				var.cpcp.t	= var.cpcp.f
|				var.item	= empty()				|#214216.o

				if isspace(tfgld410.item) then				|#214721.sn
					tfgld410.item = get.item.sfc.order(tfgld410.orno)
				endif							|#214721.en

				var.item	= segment.item.code(tfgld410.item , var.cprj)	|#214216.n
				break
			case tcfitr.receipt: 
				do.migrate 	= true
				var.fitr 		= target(tcfitr.receipt)
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tisfc001.clco
				var.entp.t	= target(tcemm.enty.warehouse)
				var.entc.t	= tfgld410.cwar
				var.cpcp.f	= get.eff.cpcp(
							tfgld410.cpcp,
							tfgld410.item,
							tfgld410.cprj,
							ticpr010.cref)
				var.cpcp.t	= var.cpcp.f
				var.item	=  segment.item.code(tfgld410.item , var.cprj)
				break
			case tcfitr.surcharge:
				do.migrate 	= true
				var.fitr 		= target(tcfitr.item.surch.rec)
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tisfc001.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tisfc001.clco
				var.cpcp.f	= get.eff.cpcp(
							tfgld410.cpcp,
							tfgld410.item,
							tfgld410.cprj,
							ticpr010.cref)
				var.cpcp.t	= var.cpcp.f
				var.item	=  segment.item.code(tfgld410.item , var.cprj)
				break
			case tcfitr.revaluation: 
				do.migrate 	= true
				var.tror		= target(tctror.prd)			
				var.fitr 		= target(tcfitr.add.calc.offvar)		
										|#214956.sn
				if tfgld410.pono <> 0 then
					var.fitr 	= target(tcfitr.issue)			
				endif
										|#214956.en
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tisfc001.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tisfc001.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= segment.item.code(tfgld410.item , var.cprj)	
			break


		endcase
	
		|* fill general fields (only if needed)
		if do.migrate then
			fill.misc.var.fields.from.tfgld410()
		endif
	endif
	
	return(do.migrate)
}


function domain tcbool convert.to.tipcs300(
				ref 	domain	tcbool	o.correct.pcs360)
{
	|* Return: 
	|*    False = do not migrate this tror/fitr
	|*    True  = do migrate this tror/fitr

	|* Note: when adding tror/fitrs, also change the 2nd select in "migrate tfgld410"

	domain tcbool	do.migrate
	
	o.correct.pcs360 = false
	do.migrate 	 = false

	if tfgld410.tror =  tctror.pcs then
		var.orno = empty()
		on case tfgld410.fitr
						|#213976.so
|
|			    	do.migrate 	= true
|
|				var.fitr 		= target(tcfitr.cogs)	
|				var.entp.f 	= target(tcemm.enty.workcenter)
|				var.entc.f	= var.tipcs020.clco
|				var.entp.t	= target(tcemm.enty.workcenter)
|				var.entc.t	= var.tipcs020.clco
|				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
|				var.cpcp.t	= var.cpcp.f
|				var.item	= empty()
|			    	break
|			case tcfitr.ecp.sales: 
|				do.migrate 	= true
|				var.tror		= target(tctror.sls)
|				var.fitr 		= target(tcfitr.cogs)	
|				var.entp.f 	= target(tcemm.enty.workcenter)
|				var.entc.f	= var.tipcs020.clco
|				var.entp.t	= target(tcemm.enty.workcenter)
|				var.entc.t	= var.tipcs020.clco
|				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
|				var.cpcp.t	= var.cpcp.f
|				var.item	= empty()
|			    	break
						|#213976.eo
							|#215532.sn
			case tcfitr.acp.sales: 
			    	do.migrate 	= true
				var.tror	= target(tctror.pcs)
				var.fitr 	= target(tcfitr.cogs)	
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				if isspace(tfgld410.item) then
					var.item = empty()
				else
					var.item = segment.item.code(tfgld410.item , var.cprj)
				endif
				var.orno	= "BAANIV"
			    	break
							|#215532.en
			case tcfitr.gen.result:
				do.migrate 	= true
				var.tror		=  target(tctror.pcs)
				var.fitr 		= target(tcfitr.general.result)
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= empty()
			    	break
			case tcfitr.sur.result:
			 	do.migrate 	= true
				var.tror		=  target(tctror.pcs)
				var.fitr 		= target(tcfitr.general.result)
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= empty()
			    	break
			case tcfitr.surcharge:
				do.migrate 	= true
				o.correct.pcs360	= true
				var.tror		=  target(tctror.pcs)
				var.fitr 		= target(tcfitr.project.surch)
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= empty()
			    	break
			case tcfitr.operation:
				do.migrate 	= true
				o.correct.pcs360	= true
				var.tror		=  target(tctror.pcs)
				var.fitr 		= target(tcfitr.general.hours)
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= empty()
			    	break

		endcase
	endif

	if tfgld410.tror =  tctror.prd then
		var.orno = convert.number( bcckon.act.sfc, tfgld410.orno)
		on case tfgld410.fitr
			case tcfitr.prd.result: 
				do.migrate 	= true
				var.tror		= target(tctror.prd)
				var.fitr 		= target(tcfitr.add.calc.offvar)	
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f

				if isspace(tfgld410.item) then				|#214721.sn
					tfgld410.item = get.item.sfc.order(tfgld410.orno)
				endif							|#214721.en

				var.item	= segment.item.code(tfgld410.item , var.cprj)
				break
			case tcfitr.revaluation: 
				do.migrate 	= true
				var.tror		= target(tctror.prd)
				var.fitr 		= target(tcfitr.add.calc.offvar)	
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= segment.item.code(tfgld410.item , var.cprj)	
			break

		endcase
	endif


	
	if tfgld410.tror =  tctror.sls then
		var.orno = convert.number( bcckon.act.sls, tfgld410.orno)
		on case tfgld410.fitr
			case tcfitr.revenue: 
				do.migrate 	= true
				var.tror		= target(tctror.sls)
				var.fitr 		= target(tcfitr.revenues.anal)	
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= segment.item.code(tfgld410.item , var.cprj)
				break
		endcase
	endif

	if tfgld410.tror =  tctror.sma.order then
		var.orno = convert.number( bcckon.sma.order, tfgld410.orno)
		on case tfgld410.fitr
			case tcfitr.revenue: 
				do.migrate 	= true
				var.tror		= target(tctror.service)
				var.fitr 		= target(tcfitr.revenues.anal)	
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= segment.item.code(tfgld410.item , var.cprj)
				break
			case tcfitr.acp.sales: 
				do.migrate 	= true
				var.tror		= target(tctror.service)
				var.fitr 		= target(tcfitr.cogs)	
				var.entp.f 	= target(tcemm.enty.workcenter)
				var.entc.f	= var.tipcs020.clco
				var.entp.t	= target(tcemm.enty.workcenter)
				var.entc.t	= var.tipcs020.clco
				var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
				var.cpcp.t	= var.cpcp.f
				var.item	= segment.item.code(tfgld410.item , var.cprj)
				break
		endcase
	endif

	|#209121
	|# removed all logic for migration tctror.prp.wrh. 
	|# (tctror.prp.wrh was migrated in tctror.service), which was clearly not correct. 
|	if tfgld410.tror =  tctror.prp.wrh then		
|	endif

	|* All general cost transactions are migrated. All origins are migrated to tror=pcs
	if tfgld410.fitr = tcfitr.general then
		do.migrate = true
		o.correct.pcs360 = true
							|#214707.sn
		if tfgld410.tror =  tctror.pcs then		
			|* PC General Costs. Seq.nr from tipcs340 is stored in orno,
			|* and must be used to fill tipcs300.pono. 
			|* This will be done in  fill.misc.var.fields.from.tfgld410 
			|* Skip if tipcs340 record is deleted
			if not tipcs340.exists(tfgld410.cprj, tfgld410.orno) then
				do.migrate 	= false
				o.correct.pcs360 = false
			endif
		endif		
							|#214707.en
		var.orno	= empty()
		var.tror		= target(tctror.pcs)
		var.fitr 		= target(tcfitr.general)	
		var.entp.f 	= target(tcemm.enty.workcenter)
		var.entc.f	= var.tipcs020.clco
		var.entp.t	= target(tcemm.enty.workcenter)
		var.entc.t	= var.tipcs020.clco
		var.cpcp.f	= get.mapped.cpcp( tfgld410.cpcp)
		var.cpcp.t	= var.cpcp.f
		var.item	= segment.item.code(tfgld410.item , var.cprj)
	endif

	|* fill general fields (only if needed)
	if do.migrate then
		fill.misc.var.fields.from.tfgld410()
	endif
	
	return(do.migrate)
}

function set.fixed.values.for.pcs.costs()
{

	|* fixed settings
	prev.cprj 	= empty()
	var.tror		= target(tctror.pur)
	var.fitr		= target(tcfitr.receipt)
	var.time	= 0
|	var.koor	= empty()			|#209375.o
|	var.orno	= empty()			|#209375.o
	var.koor	= target(tckoor.act.pur)		|#209375.sn
	var.orno	= DEFAULT.PUR.ORD.NUM	

	select	bcsti000.item
	from	bcsti000
	selectdo
		var.item = segment.item.code(bcsti000.item , "")
	endselect
							|#209375.en
	var.pono	= empty()
|	var.item	= empty()			|#214961.o		
	var.entp.f	= target(tcemm.enty.workcenter)
	var.entp.t	= var.entp.f
	var.ccur.f	= var.company.currency
	var.ccur.t	= var.ccur.f
	var.cuni		= empty()
}


function dump.ticpr360.correction()  
{
	set.fixed.values.for.pcs.costs()
	var.cprj.f = ""				|#213976.n
	var.cprj.t = var.cprj			|#213976.n	

	|* other values are still curent

	|* amount has to be the negative value
	for element = 1 to 3
		var.amnt(element) =  -1.0 *var.amnt(element)
	endfor

	|* dump record	
	dump.tipcs300()  

}

	
function fill.misc.var.fields.from.tfgld410()
{

	|* fill misc. var.fields from tfgld410. 
	|* This function is both used for ticst300 and tipcs300. 

	var.pono	= tfgld410.pono
						|#214707.sn
	if tfgld410.tror =  tctror.pcs and tfgld410.fitr = tcfitr.general then
		var.pono = tfgld410.orno
	endif					|#214707.en

	|var.amnt	= tfgld410.amnt		|#MCR.o
	
	for element = 1 to 3			|#MCR.sn 
		var.amnt(element) = fill.mc.field.double(tfgld410.amth,  
 	 	 	 	 	 	 	 element,
							 0.0) 	 	 	 	 	 	 
	endfor					|#MCR.en

	var.ccur.f	= tfgld410.ccur
	var.ccur.t	= tfgld410.ccur
	var.date	= tfgld410.trdt
	var.time	= tfgld410.trtm
	var.nuni		= tfgld410.nuni
	var.cuni		= tfgld410.cuni

	if not isspace(tfgld410.cprj) then
		var.koor	= target(tckoor.act.sfc)
		var.cprj.f	= var.cprj  
		var.cprj.t	= var.cprj
	else
		var.cprj.f 	= empty()
		var.cprj.t	= empty()
	endif


	if isspace(var.orno) then
		var.koor = empty()
	else
		on case tfgld410.tror
		case tctror.pcs:
		case tctror.prd:
			var.koor = target(tckoor.act.sfc)
			break
		case tctror.pur:
			var.koor = target(tckoor.act.pur)
			break
		case tctror.sls:
			var.koor = target(tckoor.act.sls)
			break
		case tctror.sma.order:
			var.koor = target(tckoor.act.srv.man)
			break
		case  tctror.prp.wrh:
			var.koor = target(tckoor.wrh.order)
			break
		default:
			var.koor = empty()		
		endcase	
	endif
}






function define.completion.postings() 
{
	var.fitr 		= target(tcfitr.completion)
	var.entp.f 	= target(tcemm.enty.workcenter)
	var.entc.f	= var.tisfc001.clco
	var.entp.t	= target(tcemm.enty.workcenter)
	var.entc.t	= var.tisfc001.clco
	var.cpcp.f	= get.mapped.cpcp(tfgld410.cpcp)
	var.cpcp.t 	= get.eff.cpcp(	tfgld410.cpcp,
					tfgld410.item,
					tfgld410.cprj,
					ticpr010.cref)
	|var.amnt	= tfgld410.amnt		|#MCR.o
	
	for element = 1 to 3			|#MCR.sn 
		var.amnt(element) = fill.mc.field.double(tfgld410.amth,  
 	 	 	 	 	 	 	 element,
							 0.0) 	 	 	 	 	 	 
	endfor					|#MCR.en

	var.ccur.f	= var.company.currency 
	var.ccur.t	= var.company.currency  
	var.item	=  segment.item.code(tfgld410.item , var.cprj)
}


function	 fill.var.cuni.for.customized.item(
		domain	tccprj	i.cprj,
		domain	tcitem	i.item)
{
	var.cuni = ""
	select	tipcs021.cuni
	from	tipcs021
	where	tipcs021._index1 = {:i.cprj, :i.item}
	as set with 1 rows
	selectdo
		var.cuni = tipcs021.cuni
	endselect
}

function domain	bccost	get.eff.cpcp(
		domain	tccpcp		i.cpcp,
		domain	tcitem		i.item,
		domain	tccprj		i.cprj,
		domain	tccref		i.cref)


{
	|* temp fields
		domain	tckitm	tmp.kitm
		domain	tccitg	tmp.citg
		domain	bccost	tmp.eff.cpcp
	
	|* statics
	static 	domain	bccost	hold.eff.cpcp
	static 	domain	tckitm	hold.kitm
	static 	domain	tccitg	hold.citg
	static 	domain	tccpcp	hold.cpcp
	static 	domain	tcitem	hold.item
	static 	domain	tccprj	hold.cprj
	static 	domain	tccref	hold.cref

	

	|* determine item type / item group
	if   (i.item <> 	hold.item) or 
	     (i.cprj  <> 	hold.cprj)  then
	
		if isspace(i.cprj) then
			|* standard items
			select 	tiitm001.kitm:tmp.kitm, 
				tiitm001.citg:tmp.citg
			from	tiitm001
			where	tiitm001.item = :i.item
			selectdo
			endselect
		else
			|* customized items
			select 	tipcs021.kitm:tmp.kitm, 
				tipcs021.citg:tmp.citg
			from	tipcs021
			where	tipcs021._index1 = {:i.cprj, :i.item}
			selectdo
			endselect
		endif

	else





		tmp.kitm = hold.kitm
		tmp.citg  = hold.citg
	endif

	|* determine effective cost component
	if 	(tmp.kitm <> hold.kitm) or
		(tmp.citg  <> hold.citg) or 
		(i.cpcp     <> hold.cpcp) or 
		(i.cref	  <> hold.cref) then

		tmp.eff.cpcp = eff.aggr.cost.comp(
				tmp.kitm,
				tmp.citg,
				i.cpcp,
				i.cref)
	else
		tmp.eff.cpcp = hold.eff.cpcp
	endif

	|* save hold values
	hold.kitm	= tmp.kitm
	hold.citg	= tmp.citg
	hold.item	= i.item
	hold.cprj	= i.cprj
	hold.cpcp	= i.cpcp
	hold.cref	= i.cref
	hold.eff.cpcp	= tmp.eff.cpcp
	
	return(tmp.eff.cpcp)

}

function domain bccost get.mapped.cpcp(
			domain	tccpcp	i.cpcp)
{

	 get.cost.components(	bcorig.ticpr, 
				i.cpcp, "", 
				err.mess)  

	return(bcstc006.cost)
}


function domain bcstd.fent get.purchase.office()
{
		get.enterprise.unit(

				tfgld410.fcom, 
				tfgld410.ocom, 
				err.mess)

		return(bcstc018.pofd)
}


function domain bcstc.item get.item.code.material()
{
	domain	bcstc.item	tmp.item	|* Gemini item code

	|* read material based on tfgld410 data
	select	ticst001.cprj, ticst001.opol, ticst001.sitm
	from	ticst001
	where	ticst001._index1 = {:tfgld410.orno, :tfgld410.pono}
	selectdo

		tmp.item = get.item.code(ticst001.cprj, ticst001.opol, ticst001.sitm)
	selectempty
		tmp.item = "??"
	endselect

	return(tmp.item)
}

function domain bcstc.item get.item.code.operation()
{
	domain	bcstc.item	tmp.item	|* Gemini item code
	
	tmp.item = ""
	if isspace(tfgld410.item) then
		select	tisfc001.mitm
		from	tisfc001
		where	tisfc001.pdno = :tfgld410.orno
		selectdo
			tmp.item =  segment.item.code(
					tisfc001.mitm ,
					var.cprj)	|#200314.n
|					tfgld410.cprj)	|#200314.n
		endselect
	else
		tmp.item =  segment.item.code(
					tfgld410.item,
					var.cprj) 	|#200314.n
|					tfgld410.cprj)	|#200314.o
	endif
	return(tmp.item)
}


function domain	bcstc.item   get.item.code(
			domain	tccprj	i.cprj,
			domain	tcittb	i.opol,
			domain	tcitem	i.item)
			
{
	domain	bcstc.item	tmp.item	|* gemini item code

	 if not isspace (i.cprj) then  
 		 if (i.opol =  tcittb.standard) then  
 	 		 tmp.cprj =  empty()  
	 	 else  
 	 	 	 tmp.cprj =  get.ti.project(i.cprj)  
 		 endif  
	 else  

 		 tmp.cprj =  empty()  
	 endif  
	
	tmp.item = segment.item.code(i.item ,tmp.cprj)
	return(tmp.item)
}

function dump.ticst300()
{
	|if var.amnt <> 0.0 or var.nuni <> 0.0 then	|#MCR.o
	if var.amnt(1) <> 0.0 or var.nuni <> 0.0 then	|#MCR.n
		 var.guid =  set.ticst300.guid()  
		dump 1

	endif
}




function dump.tipcs300()  
{
	|if var.amnt <> 0.0 or var.nuni <> 0.0 then	|#MCR.o
	if var.amnt(1) <> 0.0 or var.nuni <> 0.0 then	|#MCR.n

		 var.guid =  set.tipcs300.guid() 

		if isspace(var.orno) then
			var.koor = target(tckoor.not.appl)
		endif

		dump 2
	endif
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ti
:TCMOD:pcf
:TFLNO:520
:FUNC:START
function void read.budget.product.variants(
		domain	tccpva	i.old.pvar,
		domain	bccpvl	i.single.new.pvar)
{
	|*  As many product variants as there are new product variants
	|*  in bcsti002 for the current product variant were created.
	|*  One product variant was created when nothing found in bcsti002.
	|*  (May be the situation with a product variant for a budget, but no
	|*  calculation parts with that product variant does never occur.
	|*  But this does not lead to errors.)

	static	domain	tccpva	hold.old.pvar

	if hold.old.pvar = i.old.pvar then
		|* New product variant(s) already read. Required data is
		|* already in memory. Return immediately.
		return
	endif

	g.nr.new.pvars = 0
	|* Memory is not reset. This is not required, because the array
	|* positions are always filled from 1 (one) until the counter.
|	select 	bcsti002.cpva	| # 209069.o  
 	 select  bcsti002.cpvr, bcsti002.cpva 	 | # 209069.n  
	from 	bcsti002
 	where 	bcsti002.cpvr =  :i.old.pvar
| 	group by bcsti002.cpva	| # 209069.o  
| 	order by bcsti002.cpva	| # 209069.o  
 	 group by bcsti002.cpvr, bcsti002.cpva 	 | # 209069.n  
 	 order by bcsti002.cpvr, bcsti002.cpva   | # 209069.n  
 	selectdo
		g.nr.new.pvars = g.nr.new.pvars + 1
		if g.nr.new.pvars > g.allocated.memory then
			g.allocated.memory = g.allocated.memory + 50
			return.value = alloc.mem(gt.new.pvars,
					g.allocated.memory)
		endif
		gt.new.pvars(g.nr.new.pvars) = bcsti002.cpva
	selectempty  
		g.nr.new.pvars = g.nr.new.pvars + 1
		if g.nr.new.pvars > g.allocated.memory then
			g.allocated.memory = g.allocated.memory + 50
			return.value = alloc.mem(gt.new.pvars,
					g.allocated.memory)
		endif
		gt.new.pvars(g.nr.new.pvars) = i.single.new.pvar
	endselect
	hold.old.pvar = i.old.pvar
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ti
:TCMOD:rou
:TFLNO:102
:FUNC:START
function fill.introduction.date(domain tidate i.date)
{
	static	string		var.str.min.utc(22)

	if (i.date = 0) then 
		if isspace(var.str.min.utc) then
			var.str.min.utc = utc (date.to.num (1970,01,03) , 1)
		endif
		var.indt = var.str.min.utc
	else 
  		 var.indt = utc (i.date,0) 
	endif
}

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.exdt = utc(i.date,0)
	else
		var.exdt = utc(748747,0)
	endif
}


function set.var.fields(
		double		i.man.occ.runtime,
		double		i.mach.occ,
		domain	tcccap	i.crit.cap.type,
		long		i.sutm,
		double		i.rutm)
{
	double		tmp.occupation

	|* Baan IV set-up time & and run-time is capacity. 
	|* must be converted into lead-time
	var.mach.occ = i.mach.occ
	var.man.occ.runtime = i.man.occ.runtime

	if i.crit.cap.type = tcccap.mach.capacity then
		if i.mach.occ = 0.0 then
			tmp.occupation = 1.0
		else
			tmp.occupation = i.mach.occ
		endif

		var.setup.time = i.sutm/tmp.occupation
		var.cycle.time = i.rutm/tmp.occupation
	else
		if i.man.occ.runtime = 0.0 then
			tmp.occupation = 1.0
		else
			tmp.occupation = i.man.occ.runtime
		endif

		var.setup.time = i.sutm/tmp.occupation
		var.cycle.time = i.rutm/tmp.occupation
	endif

	|* calc. Production Rate
	if var.cycle.time = 0 then
		var.prod.rate = 0
	else
		var.prod.rate = 60 / var.cycle.time
	endif
	
	
 	| *  man occupation during set -  up is defaulted to 1  
 	 | *  in case of set -  up time; else zero.  
 	 if i.sutm <>  0.0 then  
 |	 	 var.man.occ.setup =  1.0  	|#203971.o
		var.man.occ.setup  = tmp.occupation
 	 else  
 	 	 var.man.occ.setup =  0.0  
 	 endif  
	
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ti
:TCMOD:sfc
:TFLNO:000
:FUNC:START
function set.number.groups.and.series()
{
string	error.message(200) mb

get.number.group( bcckon.act.sfc, error.message)
var.tisfc000.ngpd = bcstc002.nrgr
var.tisfc000.psep = bcsti000.psep

get.number.group( bcckon.ti.order.group, error.message)
var.tisfc000.ngog = bcstc002.nrgr

var.tisfc000.pswh = bcsti000.pswh

}

function long convert.on.order.date(long i.value)
{
	on case i.value
	case 1:
		if tisfc000.umpd = tcyesno.yes then
			return(2)	|* (= ord.pl.del.date)
		else
			return(1)	|* (= ord.del.date)
		endif
		break
	case 2:
		return(2)	|* (= ord.pl.del.date)
		break
	case 3:
	case 4:
	case 5:
		return(1)	|* (= ord.del.date)
		break
	endcase
	return(0)
}

function convert.backflushing.method()
{
 	  on case etol(tisfc000.bflm)  
 	  case 1:  
 	   	  var.tisfc000.bflm =  1  
 	   	  break  
 	  case 2:  
 	   	  var.tisfc000.bflm =  3  
 	   	  break  
 	  case 3:  
 	   	  var.tisfc000.bflm =  2  
 	   	  break  
 	  endcase  
}

function move.allocation()
{
 	  if tisfc000.uwwh =  tcyesno.yes then  
 	   	  var.tisfc000.mvsf =  2  
 	  else 
 	   	  var.tisfc000.mvsf =  3  
 	  endif
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ti
:TCMOD:sfc
:TFLNO:010
:FUNC:START
function long determine.operation.status()
{
	domain	tiqep3	tmp.qcmp, tmp.qcpd
	domain	tcopno	tmp.opno

	on case tisfc001.osta
	case tcosta.free:
	case tcosta.planned:
	case tcosta.printed:
		|* Planned
		return(1)
	case tcosta.ready:
		|* Completed
		return(7)
	case tcosta.closed:
		|* Closed
		return(8)
	case tcosta.in.prod:
	case tcosta.hours.adj:
		|* Planned/Ready to Start/Started/Active (in case osta is active)/Completed
		if tisfc010.comp =  tcyesno.yes then
			|* Completed
			return(7)
		else
			if tisfc001.osta = tcosta.hours.adj then
				|* See if there are processed records present 
				|* in HRA for the operation
				select	tihra100.year
				from	tihra100
				where	tihra100._index5 = {tckoht.production,
							:tisfc010.pdno,
							:tisfc010.opno}
				and	tihra100.htpr = tcyesno.yes
				as set with 1 rows
				selectdo
					|* active
					return(5)
				endselect
			endif
			if tisfc010.qcmp <> 0.0
			   or tisfc010.qcpd <> 0.0
			   or tisfc010.qrjc <> 0.0
			   or tisfc010.qrpd <> 0.0 then
				|* started
				return(4)
			endif
			select	alias.qcmp:tmp.qcmp, alias.qcpd:tmp.qcpd, 
				alias.opno:tmp.opno
			from	tisfc010 alias
			where	alias.pdno = :tisfc010.pdno
			and	alias._index1 < {:tisfc010.pdno, :tisfc010.opno}
			order by alias.opno desc
			as set with 1 rows
			selectdo
				if tmp.qcmp <> 0.0 or tmp.qcpd <> 0.0 then
					|* Previous operation is started. So
					|* current operation can be ready to
					|* start
					return(3)
				else
					|* Planned
					return(1)
				endif
			selectempty
				|* First operation, so ready to start
				return(3)
			endselect
		endif
	default:
		|* All other cases Closed
		return(8)
	endcase

	return(0)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tp
:TCMOD:pin
:TFLNO:010
:FUNC:START
function domain tcbool activity.is.milestone(domain tccprj         i.cprj,
			      domain tppss.cpla   i.cpla,
			      domain tppdm.cact  i.cact)
{
	select	tppss200.cact 
	from	tppss200
	where	tppss200._index1 = {:i.cprj, :i.cpla, :i.cact}
	and	tppss200.tact = tppdm.tact.milestone
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tp
:TCMOD:pin
:TFLNO:011
:FUNC:START
function domain tcbool set.settled.invoice.lines()
{

 	 select 	 tppin130.stin, tppin130.ninv, tppin100.ocmp, tppin100.amoi  
 	 from 	 tppin130, tppin100  
 	 where 	 tppin130.cprj =  :tppin010.cprj and  
 	 	 tppin130.cuno =  :tppin010.cuno and  
 	 	 tppin130.sera =  :tppin010.sern and  
 	 	 tppin130.cdmt =  tppin.cdmt.advance and  
 	 	 tppin100.stin =  tppin130.stin and  
 	 	 tppin100.ninv =  tppin130.ninv  
 	 selectdo  
 	 	 if tppin100.amoi >=  0 then  
 	 	 	 var.tppin011.ityp =  tpprc000.ttys  
 	 	 else  
 	 	 	 var.tppin011.ityp =  tpprc000.ttsc  
 	 	 endif  
 	 	 var.tppin011.idoc =  tppin130.ninv
 	 	 var.tppin011.fcmp =  tppin100.ocmp
		 return(true)
  	 endselect 


 	 select 	 tppin140.stin, tppin140.ninv, tppin100.ocmp, tppin100.amoi
 	 from 	 tppin140, tppin100
 	 where 	 tppin140.cprj =  :tppin010.cprj and  
 	 	 tppin140.cuno =  :tppin010.cuno and  
 	 	 tppin140.sera =  :tppin010.sern and  
 	 	 tppin140.cdmt =  tppin.cdmt.advance and  
 	 	 tppin100.stin =  tppin140.stin and  
 	 	 tppin100.ninv =  tppin140.ninv  
 	 selectdo  
 	 	 if tppin100.amoi >=  0 then
 	 	 	 var.tppin011.ityp =  tpprc000.ttys
 	 	 else  
 	 	 	 var.tppin011.ityp =  tpprc000.ttsc
 	 	 endif  
 	 	 var.tppin011.idoc =  tppin140.ninv
 	 	 var.tppin011.fcmp =  tppin100.ocmp
		 return(true)
  	 endselect 

	select 	 tppin150.stin, tppin150.ninv, tppin100.ocmp, tppin100.amoi
 	 from 	 tppin150, tppin100
 	 where 	 tppin150.cprj =  :tppin010.cprj and  
 	 	 tppin150.cuno =  :tppin010.cuno and  
 	 	 tppin150.sera =  :tppin010.sern and  
 	 	 tppin150.cdmt =  tppin.cdmt.advance and  
 	 	 tppin100.stin =  tppin150.stin and  
 	 	 tppin100.ninv =  tppin150.ninv  
 	 selectdo  
 	 	 if tppin100.amoi >=  0 then
 	 	 	 var.tppin011.ityp =  tpprc000.ttys
 	 	 else  
 	 	 	 var.tppin011.ityp =  tpprc000.ttsc
 	 	 endif  
 	 	 var.tppin011.idoc =  tppin150.ninv
 	 	 var.tppin011.fcmp =  tppin100.ocmp
		 return(true)
  	 endselect 

	select 	 tppin160.stin, tppin160.ninv, tppin100.ocmp, tppin100.amoi
 	 from 	 tppin160, tppin100
 	 where 	 tppin160.cprj =  :tppin010.cprj and  
 	 	 tppin160.cuno =  :tppin010.cuno and  
 	 	 tppin160.sera =  :tppin010.sern and  
 	 	 tppin160.cdmt =  tppin.cdmt.advance and  
 	 	 tppin100.stin =  tppin160.stin and  
 	 	 tppin100.ninv =  tppin160.ninv  
 	 selectdo  
 	 	 if tppin100.amoi >=  0 then
 	 	 	 var.tppin011.ityp =  tpprc000.ttys
 	 	 else  
 	 	 	 var.tppin011.ityp =  tpprc000.ttsc
 	 	 endif  
 	 	 var.tppin011.idoc =  tppin160.ninv
 	 	 var.tppin011.fcmp =  tppin100.ocmp
		 return(true)
  	 endselect 


	 select 	 tppin170.stin, tppin170.ninv, tppin100.ocmp, tppin100.amoi
 	 from 	 tppin170, tppin100
 	 where 	 tppin170.cprj =  :tppin010.cprj and  
 	 	 tppin170.cuno =  :tppin010.cuno and  
 	 	 tppin170.sera =  :tppin010.sern and  
 	 	 tppin170.cdmt =  tppin.cdmt.advance and  
 	 	 tppin100.stin =  tppin170.stin and  
 	 	 tppin100.ninv =  tppin170.ninv  
 	 selectdo  
 	 	 if tppin100.amoi >=  0 then
 	 	 	 var.tppin011.ityp =  tpprc000.ttys
 	 	 else  
 	 	 	 var.tppin011.ityp =  tpprc000.ttsc
 	 	 endif  
 	 	 var.tppin011.idoc =  tppin170.ninv
 	 	 var.tppin011.fcmp =  tppin100.ocmp
		 return(true)
  	 endselect 


	 select 	 tppin180.stin, tppin180.ninv, tppin100.ocmp, tppin100.amoi
 	 from 	 tppin180, tppin100
 	 where 	 tppin180.cprj =  :tppin010.cprj and  
 	 	 tppin180.cuno =  :tppin010.cuno and  
 	 	 tppin180.sera =  :tppin010.sern and  
 	 	 tppin180.cdmt =  tppin.cdmt.advance and  
 	 	 tppin100.stin =  tppin180.stin and  
 	 	 tppin100.ninv =  tppin180.ninv  
 	 selectdo  
 	 	 if tppin100.amoi >=  0 then
 	 	 	 var.tppin011.ityp =  tpprc000.ttys
 	 	 else  
 	 	 	 var.tppin011.ityp =  tpprc000.ttsc
 	 	 endif  
 	 	 var.tppin011.idoc =  tppin180.ninv
 	 	 var.tppin011.fcmp =  tppin100.ocmp
		 return(true)
  	 endselect 


	select 	 tppin190.stin, tppin190.ninv, tppin100.ocmp, tppin100.amoi
 	 from 	 tppin190, tppin100
 	 where 	 tppin190.cprj =  :tppin010.cprj and  
 	 	 tppin190.cuno =  :tppin010.cuno and  
 	 	 tppin190.sera =  :tppin010.sern and  
 	 	 tppin190.cdmt =  tppin.cdmt.advance and  
 	 	 tppin100.stin =  tppin190.stin and  
 	 	 tppin100.ninv =  tppin190.ninv  
 	 selectdo  
 	 	 if tppin100.amoi >=  0 then
 	 	 	 var.tppin011.ityp =  tpprc000.ttys
 	 	 else  
 	 	 	 var.tppin011.ityp =  tpprc000.ttsc
 	 	 endif  
 	 	 var.tppin011.idoc =  tppin190.ninv
 	 	 var.tppin011.fcmp =  tppin100.ocmp
		 return(true)
  	 endselect 
	 return(false)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:tp
:TCMOD:pin
:TFLNO:020
:FUNC:START
function domain tcbool activity.is.milestone(domain tccprj         i.cprj,
			      domain tppss.cpla   i.cpla,
			      domain tppdm.cact  i.cact)
{
	select	tppss200.cact 
	from	tppss200
	where	tppss200._index1 = {:i.cprj, :i.cpla, :i.cact}
	and	tppss200.tact = tppdm.tact.milestone
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ts
:TCMOD:soc
:TFLNO:000
:FUNC:START
function fill.number.groups()
{
	var.str.ngsq = get.nrgr(bcckon.ts.quot)
	var.str.ngso = get.nrgr(bcckon.sma.order)
	var.str.grcq = get.nrgr(bcckon.srv.cont.quot)
	var.str.grco = get.nrgr(bcckon.srv.contract)
	var.str.ngfo = get.nrgr(bcckon.ts.sfo)
}

function string get.nrgr(domain bcckon i.ckon)
{
	string	ret.str(3)
	domain	tcmcs.s200m	l.err.mess

	if not get.number.group(i.ckon, l.err.mess) then
		ret.str = bcstc002.nrgr
	else
		ret.str = empty.string(1;3)
	endif

	return(ret.str)
}

function dump.parameters()
{
	dump 1 
	| * tsacm000 - Activity Management Parameters 
	dump 2 
	| * tscfg000 - Configuration Management Parameters 
	dump 3 
	| * tsctm000 - Contract Management Parameters 
	dump 4 
	| * tsmdm000 - General Service Parameters 
	dump 5 
	| * tssoc000 - Service Order Parameters 
	dump 6 
	| * tsspc000 - SPC parameters 
}

function long get.target.search.arg(domain tssma.atrb i.atrb)
{
	if etol(i.atrb) <= 2 then
		return( etol(i.atrb) * 5 )
	else
		return(( etol(i.atrb) * 5 ) + 5)
	endif
	return(0)
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ts
:TCMOD:soc
:TFLNO:200
:FUNC:START
function fill.contract()
{
	var.str.csco = convert.10.to.9(tssma301.ccon)

}


function long get.status(long i.b4stat)
{
	on case i.b4stat
	case 1 :
		if cost.line.with.allocated.qyan.present() then
			return(10)
			|* Free -> Planned
		else
			return(5)
			|* Free -> Free
		endif
		break
	case 2 :
		return(10)
		|* Planned -> Planned
		break
	case 3 :
		return(15)
		|* In Process -> Released
		break
	case 4 :
		return(20)
		|* Completed -> Completed
		break
	case 5 :
		if all.cost.lines.are.invoiced() then
			return(23)
			|* Costed -> Costed
		else
			return(20)
			|* Costed -> Completed
		endif
		break
	case 6 :
		return(25)
		|* History -> Closed
		break
	case 7 :
		return(35)
		|* Canceled -> Canceled
		break
	endcase
	return(0)
}

function domain tcbool cost.line.with.allocated.qyan.present()
{
	select	tssma310.orno
	from	tssma310
	where	tssma310._index1 = {:tssma301.orno}
	and	tssma310.aqan <> 0
	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)
}

function domain tcbool all.cost.lines.are.invoiced()
{
	select	tssma310.orno
	from	tssma310
	where	tssma310._index1 = {:tssma301.orno}
	and	tssma310.scos < tssma.scos.invoiced
	as set with 1 rows
	selectdo
		return(false)
	endselect

	return(true)
}

function domain bcstd.fent get.department()
{
	get.enterprise.unit(tssma301.ncmp, ext.lng.comp, err.mess)
	return(bcstc018.srfd)
}

function fill.addresses()
{
	var.str.itad = empty.string (1;9)
	var.str.stad = empty.string (1;9)
	var.str.lcad = empty.string (1;9)

	if not get.addresses ("tccom010",tssma301.cuno,err.mess) then
		var.str.defaddr = bcstc004.cadr
	endif

	select	tssma110.*
	from	tssma110
	where	tssma110._index1 = {:tssma301.cloc}
	as set with 1 rows
	selectdo
		if isspace (tssma110.cdel) then
			var.str.lcad = var.str.defaddr
		else
			var.str.add = tssma110.cuno (1;6)&
					tssma110.cdel (1;3)
			if not get.addresses ("tccom013",
					var.str.add,err.mess) then
				var.str.stad = bcstc004.cadr
				var.str.lcad = bcstc004.cadr
			endif
		endif
	selectempty
		var.str.lcad = var.str.defaddr
	endselect

	select	tssma302.*
	from	tssma302
	where	tssma302.orno = :tssma301.orno
	selectdo
		if tssma302.ckor = 1 then
			var.str.addvar1 = edit$ (tssma302.orno,"999999") &
					tssma302.cloc (1;10) &
					edit$ (tssma302.ckor,"99")
			if not get.addresses("tssma302",
					var.str.addvar1,err.mess) then
				var.str.itad = bcstc004.cadr
			endif
		endif

		if tssma302.ckor = 0 then
			var.str.addvar1 = edit$(tssma302.orno,"999999") &
					tssma302.cloc (1;10) &
					edit$ (tssma302.ckor,"99")
			if not get.addresses ("tssma302",
					var.str.addvar1,err.mess) then
				var.str.stad = bcstc004.cadr
			endif
		endif
	endselect

	if isspace(var.str.stad) then
		if isspace (tssma301.cdel) then
			var.str.stad = var.str.defaddr
		else
			var.str.add = tssma301.cuno (1;6) & tssma301.cdel (1;3)
			if not get.addresses ("tccom013",
					var.str.add,err.mess) then
				var.str.stad = bcstc004.cadr
			else
				var.str.stad = var.str.defaddr
			endif
		endif
	endif

	if isspace(var.str.itad) then
		if isspace (tssma301.ccor) then
			var.str.itad = var.str.defaddr
		else
			var.str.add = tssma301.cuno (1;6) & tssma301.ccor (1;3)
			if not get.addresses ("tccom012",
					var.str.add,err.mess) then
				var.str.itad = bcstc004.cadr
			else
				var.str.itad = var.str.defaddr
			endif
		endif
	endif
}

function fill.dates()
{
	if etol (tssma301.swor) <= 2 then
		var.str.astm = var.str.utcdef
		var.str.aftm = var.str.utcdef
	else
		var.str.astm = utc (tssma301.ddt6,tssma301.dtm6)
		var.str.aftm = utc (tssma301.ddt7,tssma301.dtm7)
	endif

	if tssma301.ddt2 <> 0 then
		var.lng.appo = 1
		var.str.pstm = utc (tssma301.ddt2,tssma301.dtm2)
		var.lng.date = tssma301.ddt2
		var.lng.time = tssma301.dtm2 + (tssma301.hour * 3600)
		var.str.pftm = utc (var.lng.date,var.lng.time)
	else
		var.lng.appo = 2
		if tssma301.ddt5 <> 0 then
			var.str.pstm = utc (tssma301.ddt5,tssma301.dtm5)
			var.lng.date = tssma301.ddt5
			var.lng.time = tssma301.dtm5 + (tssma301.hour * 3600)
			var.str.pftm = utc (var.lng.date,var.lng.time)
		else
			if tssma301.ddt6 <> 0 then
				var.str.pstm = utc (tssma301.ddt6,tssma301.dtm6)
				var.lng.date = tssma301.ddt6
				var.lng.time = tssma301.dtm6 + (tssma301.hour * 3600)
				var.str.pftm = utc (var.lng.date,var.lng.time)
			else
				var.str.pstm = utc (tssma301.ddt1,tssma301.dtm1)
				var.lng.date = tssma301.ddt1
				var.lng.time = tssma301.dtm1 + (tssma301.hour * 3600)
				var.str.pftm = utc (var.lng.date,var.lng.time)
			endif
		endif
	endif

	var.str.estm = var.str.pstm
	if var.lng.date > tssma301.ddt1 and tssma301.ddt1 <> 0 then
		var.str.estm = utc (tssma301.ddt1,tssma301.dtm1)
	endif

	if var.str.pftm > utc (tssma301.ddt7,tssma301.dtm7) then
		var.str.lftm = var.str.pftm
	else
		var.str.lftm = utc (tssma301.ddt7,tssma301.dtm7)
	endif
}

function fill.cluster()
{
	if isspace(tssma301.cloc) then
		var.str.clst = empty.string (1;8)
		return
	endif

	if not get.configuration(tssma301.cloc, err.mess) then
		var.str.clst = bcsts005.clst
	else
		var.str.clst = empty.string (1;8)
	endif
}

function fill.business.partner()
{
	if get.business.partner(tssma301.cuno,
				bcstc.orgn.cuno,
				err.mess) then
		var.str.bpid = empty.string(1;9)
	else
		var.str.bpid = bcstc008.bpid
	endif
}

function fill.item()
{
	var.tssoc210.item = empty.string(1;47)
	select	tssma102.item, tssma102.cprj
	from	tssma102
	where	tssma102.cins = :tssma301.cins
	as set with 1 rows
	selectdo
		if dummy.item.has.to.be.used(	tssma102.cprj,
						tssma102.item) then
			var.tssoc210.item=
				segment.item.code(bcsts000.item,
					empty.string (1;9))
		else
			var.tssoc210.item=
								|#SL200314.so
				|segment.item.code(tssma102.item, tssma102.cprj)
								|#SL200314.eo
								|#SL200314.sn
				segment.item.code(tssma102.item,
						  get.project(tssma102.cprj))
								|#SL200314.en
		endif
	endselect
}

function get.skills(domain tssma.ctyp i.ctyp,
		ref	string o.skla(),
		ref	string o.sklb(),
		ref	string o.sklc())
{
	long	counter

	counter = 1
	o.skla = empty.string(1;4)
	o.sklb = empty.string(1;4)
	o.sklc = empty.string(1;4)

	select	tssma406.*
	from	tssma406
	where	tssma406.ctyp = :i.ctyp
	order by tssma406.skcd desc, tssma406.skll
	as set with 3 rows
	selectdo
		on case counter
		case 1:
			o.skla = tssma406.skll
			break
		case 2:
			o.sklb = tssma406.skll
			break
		case 3:
			o.sklc = tssma406.skll
			break
		endcase
		counter = counter + 1
	endselect
}

function string get.sales.price.list()
{
	|* This function is directly called in rules

	string	l.cpls(3)

	if isspace(tssma301.pcpr) then
		return(empty.string(1;3))
	endif

	select	tccom010.cpls :l.cpls
	from	tccom010
	where	tccom010._index1 = {:tssma301.pcpr}
	selectdo
		return(l.cpls)
	endselect

	return(empty.string(1;3))
}

function domain tcbool technician.present.for.order()		|#200-101936.sn
{
	select 	 tssma305.orno
	from 	 tssma305
	where 	 tssma305._index3 = {:tssma301.orno}
	and 	tssma305.orno <>  0
 	as set with 1 rows
	selectdo
		return(true)
	endselect

	return(false)

}								|#200-101936.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ts
:TCMOD:soc
:TFLNO:220
:FUNC:START
function fill.line.status()
{
	if tssma310.scos = tssma.scos.free 
	or tssma310.scos = tssma.scos.issued.stock then
		var.lng.clst = 1
	else
		var.lng.clst = 10
	endif
}

function fill.quantities()
{
	var.dbl.noun = 0
	var.dbl.eqan = 0.0
	var.dbl.qtdl = 0.0
	var.dbl.adqt = 0.0

	if double.cmp(tssma310.leng, 0.00, 0.01) or
	   double.cmp(tssma310.widt, 0.00, 0.01) or
	   double.cmp(tssma310.thic, 0.00, 0.01) then
		var.dbl.noun = round(tssma310.qana, 0, 1)

		if tssma310.scos >= tssma.scos.issued.stock then
			var.dbl.adqt = abs(tssma310.qana)
		endif
		return
	endif

	if tssma310.scos = tssma.scos.free then
		var.dbl.eqan = abs(tssma310.qanp) * tssma310.cvqs

		if double.cmp(tssma310.qana, 0.0000, 0.0001) then
			var.dbl.qtdl = abs(tssma310.qana)
		else
			var.dbl.qtdl = abs(tssma310.qanp)
		endif
	else
		var.dbl.eqan = abs(tssma310.qanp) * tssma310.cvqs
		var.dbl.qtdl = abs(tssma310.qana - tssma310.iqan)
		var.dbl.adqt = abs(tssma310.qana)
	endif

}

function fill.amounts()
{	
	long	i

	if tssma310.scos = tssma.scos.free 
	   and var.lng.cotp = 5 then
	   |* Only in case of material line
		for i = 1 to 3
			var.dbl.acpr(i) = 0.0
			var.dbl.amnt(i) = 0.0
			var.dbl.acin(i) = 0.0
		endfor
		var.dbl.asto = 0.0
		var.dbl.asin = 0.0
		var.dbl.asdi = 0.0
		var.dbl.inam = 0.0
	else
		for i = 1 to 3
			var.dbl.acpr(i) = fill.mc.field.double(tssma310.camn, i)
					* tssma310.cvqs
			var.dbl.amnt(i) = abs(tssma310.qana) * var.dbl.acpr(i)
			var.dbl.acin(i) = var.dbl.amnt(i)
		endfor
		var.dbl.asto = abs(tssma310.qana) * 
					(tssma310.cvqs / tssma310.cvps ) *
					fill.mc.field.double(tssma310.pric, 1)
		var.dbl.asin = var.dbl.asto
		var.dbl.asdi = (tssma310.disc * var.dbl.asin)/ 100
		var.dbl.inam = var.dbl.asin - var.dbl.asdi

		if tssma310.tcst = tssma.tcst.cash then
			|* For Cash Payments amounts should be negative
			for i = 1 to 3
				var.dbl.acpr(i) = -1.0 * var.dbl.acpr(i)
				var.dbl.amnt(i) = -1.0 * var.dbl.amnt(i)
				var.dbl.acin(i) = var.dbl.amnt(i)
			endfor
			var.dbl.asto = -1.0 * var.dbl.asto
			var.dbl.asin = -1.0 * var.dbl.asin
			var.dbl.inam = -1.0 * var.dbl.inam
		endif
	endif
}

function fill.print.on.invoice()
{
	if tssma310.prnt = empty or
	   var.dbl.inam <> 0.0 then
		var.lng.prnt = 3
	else
		var.lng.prnt = etol(tssma310.prnt)
	endif
}

function get.tfgld410.podt()
{
	static	long	record.found

	static	domain	tccprj		s.cprj
	static	domain	tcorno		s.orno
	static	domain	tssma.date  	s.invd

	if tssma310.cprj <> s.cprj or
	   tssma310.orno <> s.orno or
	   tssma310.invd <> s.invd then
		select 	 tfgld410.podt
		from 	 tfgld410
		where 	 tfgld410._index2 = {	:ext.lng.comp,
						:tssma310.cprj,
						tctror.sma.order,
						:tssma310.orno}
		and tfgld410.fitr = tcfitr.invoice
		and tfgld410.trdt = :tssma310.invd
		as set with 1 rows
		selectdo
			record.found = true
		selectempty
			record.found = false
		endselect
	endif	

	if not record.found then
		select tfgld410.podt
		from   tfgld410
		where  tfgld410._index2 ={:ext.lng.comp, 
					" ",
					tctror.sma.order,
					:tssma310.orno,
					:tssma310.seqn,
					tcfitr.invoice,
				       	:tssma310.invd}
		as set with 1 rows
		selectdo
		selectempty
			tfgld410.podt = 0
		endselect
	endif

	s.cprj = tssma310.cprj
	s.orno = tssma310.orno
	s.invd = tssma310.invd
}

function fill.cost.type()
{
	 on case tssma310.tcst 	 	 	 	 
  	 case tssma.tcst.material:  
 	 	var.lng.cotp = 5
		|* Material
		break  	 	 
  	 case tssma.tcst.worktime: 
		if tssma310.bhra =  tcyesno.yes then  
			var.lng.cotp = 10
			|* Labor
		else
			var.lng.cotp = 35
			|* Other
		endif
		break  	 	
|  	 case tssma.tcst.cartime:			|#200-101042.so
|	 case tssma.tcst.kilometer:
|		var.lng.cotp = 20
|		|* Travelling
|		break					|#200-101042.en
	 case tssma.tcst.other:
		if tiitm001.kitm = tckitm.purchase or
		   tiitm001.kitm = tckitm.manufacture then
			var.lng.cotp = 5
			|* Material
		else
			var.lng.cotp = 35
			|* Other
		endif
		break
  	 case tssma.tcst.cartime:			|#200-101042.sn
	 case tssma.tcst.kilometer:			|#200-101042[.en
	 case tssma.tcst.subcontracting:
	 case tssma.tcst.declaration:
	 case tssma.tcst.cash:

		var.lng.cotp = 35
		|* Other
		break
  	 endcase 	
}
	
function fill.travel.spec()
{
	on case etol (tssma310.tcst)
	case 3 :
		var.lng.tspt = 10
		break
	case 4 :
		var.lng.tspt = 5
		break
	default :
		var.lng.tspt = 1
		break
	endcase
}

function fill.business.partner()
{
	if  get.business.partner(tssma301.cuno, 
				bcstc.orgn.cuno,
				err.mess) then
		var.str.bpid = empty.string(1;9)
	else
		var.str.bpid = bcstc008.bpid
	endif
}

function fill.cluster()
{
	if isspace(tssma301.cloc) then
		var.str.clst = empty.string (1;8)
		return
	endif

	if not get.configuration(tssma301.cloc, err.mess) then
		var.str.clst = bcsts005.clst
	else
		var.str.clst = empty.string (1;8)
	endif
}

function long get.delivery.type()
{
	if (tssma310.qana < 0.00 ) then
		return(25)
		|* To Warehouse
	endif

	if tssma310.porn then
		return(20)
		|* By Purchase Order
	endif

	return(etol(bcsts000.dltp))
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ts
:TCMOD:soc
:TFLNO:850
:FUNC:START
function  fill.dates() 				 |# 200 - 101952.sn					
{
	no.of.days = date.to.num(tssma501.bkyr,1,1)
	var.htim = utc(no.of.days,0,2)
}						 |# 200 - 101952.en

function fill.cluster()
{
	if isspace(tssma501.cloc) then
		var.str.clst = empty.string (1;8)
		return
	endif

	if not get.configuration(tssma501.cloc, err.mess) then
		var.str.clst = bcsts005.clst
	else	
		var.str.clst = empty.string (1;8)
	endif

}

function fill.business.partner()
{
	if get.business.partner(tssma501.cuno,
				bcstc.orgn.cuno,
				err.mess) then
		var.str.bpid = empty.string(1;9)
	else
		var.str.bpid = bcstc008.bpid
	endif
}

function fill.item()
{
	var.tssoc855.item = empty.string(1;47)
	select	tssma102.item, tssma102.cprj
	from	tssma102
	where	tssma102.cins = :tssma501.cins
	as set with 1 rows
	selectdo
		if dummy.item.has.to.be.used(	tssma102.cprj,
						tssma102.item) then
			var.tssoc855.item=   
				segment.item.code(bcsts000.item,
					empty.string (1;9))  
		else  
			var.tssoc855.item =  segment.item.code(
					tssma102.item,tssma102.cprj)  
		endif  
	endselect
}

function fill.service.type()
{
	var.str.cstp = bcsts000.cstp(1,etol(tssma501.sert))
}

function fill.coverage.type()					|#200-101953.sn
{
	var.str.cctp = bcsts000.cctp(1,etol(tssma501.sert))  
}								|#200-101953.en

function fill.contract()
{	
								|#200-101952.so
|	if (len (shiftl$ (shiftr$ (tssma501.ccon))) > 9) then
|		var.str.csco = str$ ((1 * asc (tssma501.ccon (1;1)))+
|			(2 * asc (tssma501.ccon (2;1)))+
|			(3 * asc (tssma501.ccon (3;1)))+
|			(4 * asc (tssma501.ccon (4;1)))+
|			(5 * asc (tssma501.ccon (5;1)))+
|			(6 * asc (tssma501.ccon (6;1)))+
|			(7 * asc (tssma501.ccon (7;1)))+
|			(8 * asc (tssma501.ccon (8;1)))+
|			(9 * asc (tssma501.ccon (9;1)))+
|			(10 * asc (tssma501.ccon (10;1))))
|	else
|		var.str.csco = shiftl$ (shiftr$ (tssma501.ccon))
|	endif							|#200-101952.eo
	
	var.str.csco = convert.10.to.9(tssma501.ccon)		|#200-101952.n
}


function fill.addresses()
{
	var.str.lcad = empty.string (1;9)
|	var.str.lcad = tssma501.cloc				|#200-101953.o
								|#200-101953.sn
	if not get.addresses ("tccom010",tssma501.cuno,err.mess) then
		var.str.defaddr = bcstc004.cadr
	endif

	select	tssma110.*
	from	tssma110
	where	tssma110._index1 = {:tssma501.cloc}
	as set with 1 rows
	selectdo
		if isspace (tssma110.cdel) then
			var.str.lcad = var.str.defaddr
		else
			var.str.add = tssma110.cuno(1;6)& 
					tssma110.cdel (1;3)
			if not get.addresses ("tccom013",
					var.str.add,err.mess) then				
				var.str.lcad = bcstc004.cadr
			endif
		endif
	selectempty
		var.str.lcad = var.str.defaddr
	endselect						|#200-101953.en

	if (not (isspace (tssma501.ccor))) then
		var.str.temp.add.var = tssma501.cuno (1;6) & tssma501.ccor (1;3)
		get.addresses("tccom012",var.str.temp.add.var,err.mess)
		var.str.itad = bcstc004.cadr
	else
		if get.addresses("tccom010", tssma501.cuno,err.mess) then
			var.str.itad = bcstc004.cadr
		else
			var.str.itad = empty.string (1;9)
		endif
	endif
	if (not (isspace (tssma501.cdel))) then
		var.str.temp.add.var = tssma501.cuno (1;6) & tssma501.cdel (1;3)
		get.addresses("tccom013",var.str.temp.add.var,err.mess)
		var.str.stad = bcstc004.cadr
	else
		if get.addresses("tccom010", tssma501.cuno,err.mess) then
			var.str.stad = bcstc004.cadr
		else 	 	
			var.str.stad = empty.string (1;9)
		endif 	
	endif
	if get.addresses("tccom010",tssma501.cuno,err.mess) then
		var.str.cadr = bcstc004.cadr
	else
		var.str.cadr = empty.string (1;9)
	endif
	
}

function fill.appointment()
{
	if (tssma501.ddt2 <> 0) then
		var.lng.appo = 1
	else
		var.lng.appo = 2
	endif
}

function fill.department()
{
	get.enterprise.unit(tssma501.ncmp, ext.lng.comp, err.mess)
	var.bccnv.fent.hold.off = bcstc018.srfd
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:ts
:TCMOD:soc
:TFLNO:860
:FUNC:START
function fill.sequence.number()
{
	if isspace (tssma501.cins) then
		var.oseq = 0
	else
		var.oseq = 1
	endif
}

function fill.cluster()
{
	if isspace(tssma501.cloc) then
		var.ccfg = empty.string(1;8)
		return
	endif

	if not get.configuration(tssma501.cloc, err.mess) then
		var.ccfg = empty.string(1;8)
	else
		var.ccfg = bcsts005.clst 
	endif
}

function fill.amounts()
{
								|#200-101953.so
|	var.acpr = fill.mc.field.double(tssma510.camn,1) / tssma510.qana
|	var.asto = (tssma510.qana * fill.mc.field.double(tssma510.pric,1))
|	var.aswa = (tssma510.gper * var.asto) / 100
|	var.asco = (tssma510.cper * var.asto) / 100
|	var.asin = var.asto -
|			(var.aswa + var.asco)
|	var.asdi = (tssma510.disc * var.asin) / 100
|	var.asin = var.asin - var.asdi
|	var.acpr = fill.mc.field.double(tssma510.camn,1) / tssma510.qana
								|#200-101953.eo	
								|#200-101953.sn
	long i	
								
	for i = 1 to 3
		var.dbl.acpr(i) = fill.mc.field.double(tssma510.camn, i)
								* tssma510.cvqs
		var.dbl.amnt(i) = abs(tssma510.qana) * var.dbl.acpr(i)
		var.dbl.acin(i) = var.dbl.amnt(i)
	endfor
	
	var.asto = abs(tssma510.qana) * 
				(tssma510.cvqs / tssma510.cvps ) *
					fill.mc.field.double(tssma510.pric, 1)	
	var.asin = var.asto
	var.asdi = (tssma510.disc * var.asin)/ 100
	var.inam = var.asin - var.asdi				|#200-101953.en

	if tssma510.tcst = tssma.tcst.cash then
		|* For Cash Payments amounts should be negative
		var.asto = -1.0 * var.asto
		var.asin = -1.0 * var.asin
		var.inam = -1.0 * var.inam
	endif

}

function fill.cost.component()
{
	if get.cost.components(bcorig.ticpr,tssma510.cpcp,"", err.mess) then
		var.ccmp = empty.string(1;8)
	else
		var.ccmp = bcstc006.cost
	endif
}

function get.posting.date()
{
	static	long	record.found

	static	domain	tccprj		s.cprj
	static	domain	tcorno		s.orno
	static	domain	tssma.date  	s.invd

	if tssma510.cprj <> s.cprj or
	   tssma510.orno <> s.orno or
	   tssma510.invd <> s.invd then
		select 	 tfgld410.podt
		from 	 tfgld410
		where 	 tfgld410._index2 = {	:ext.lng.comp,
						:tssma510.cprj,
						tctror.sma.order,
						:tssma510.orno}
		and tfgld410.fitr = tcfitr.invoice
		and tfgld410.trdt = :tssma510.invd
		as set with 1 rows
		selectdo
			record.found = true
		selectempty
			record.found = false
		endselect
	endif	

	if not record.found then
		select tfgld410.podt
		from   tfgld410
		where  tfgld410._index2 ={:ext.lng.comp, 
					" ",
					tctror.sma.order,
					:tssma510.orno,
					:tssma510.seqn,
					tcfitr.invoice,
				       	:tssma510.invd}
		as set with 1 rows
		selectdo
		selectempty
			tfgld410.podt = 0
		endselect
	endif

	s.cprj = tssma510.cprj
	s.orno = tssma510.orno
	s.invd = tssma510.invd
}

function get.item.description()
{
 	select 	 tiitm001.dsca 
 	from 	 tiitm001 
 	where 	 tiitm001._index1 = {:tssma510.item} 
 	as set with 1 rows 
 	selectdo 
 	endselect 
}

function fill.cost.type()
{
	on case tssma510.tcst 
  	 case tssma.tcst.material:  
 	 	var.cotp = 5
		|* Material
		break  	 	 
  	 case tssma.tcst.worktime: 
		var.cotp = 10
		|* Labor
		break
	case tssma.tcst.other:					|#200-101953.sn
		if tiitm001.kitm = tckitm.purchase or
		   tiitm001.kitm = tckitm.manufacture then
			var.cotp = 5
			|* Material
		else
			var.cotp = 35
			|* Other
		endif
		break						|#200-101953.en
	case tssma.tcst.subcontracting :
	case tssma.tcst.cartime :
	case tssma.tcst.kilometer :
	case tssma.tcst.declaration :
|	case tssma.tcst.other :					|#200-101953.n
	case tssma.tcst.cash :
		var.cotp = 35 
		|* Other
		break 
	endcase 
}

function fill.travel.type()
{
	on case tssma510.tcst 
	case tssma.tcst.cartime :
		var.tspt = 10 
		break 
	case tssma.tcst.kilometer :
		var.tspt = 5 
		break 
	default :
		var.tspt = 1 
		break 
	endcase 	 	 
}

function  fill.dates() 				 |# 200 - 101953.sn					
{
	no.of.days = date.to.num(tssma510.bkyr,1,1)
	var.htim = utc(no.of.days,0,2)
}						 |# 200 - 101953.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:wh
:TCMOD:inh
:TFLNO:020
:FUNC:START
|#SP1.sn
function determine.ship.from.to.type(
			long	i.origin,
			long	i.transaction.type)
{
	on case i.transaction.type
	case TRANS.RECEIPT:
		on case i.origin
		case ORIGIN.MAINT.WRK:
		case ORIGIN.SFC:
			var.whinh020.sfty = 4	|tctyps.work.center
			break
		case ORIGIN.PRJ:
			var.whinh020.sfty = 3	|tctyps.project
			break
		default:
			var.whinh020.sfty = 2	|tctyps.partner
		endcase
		break
	case TRANS.ISSUE:
	case TRANS.TRANSFER:
		var.whinh020.sfty = 1	|tctyps.warehouse
		break
	case TRANS.WIP.TRANSFER:
		var.whinh020.sfty = 4	|tctyps.work.center
		break
	endcase

	on case i.transaction.type
	case TRANS.RECEIPT:
	case TRANS.TRANSFER:
		var.whinh020.stty = 1	|tctyps.warehouse
		break
	case TRANS.ISSUE:
		on case i.origin
		case ORIGIN.MAINT.WRK:
		case ORIGIN.SFC:
			var.whinh020.stty = 4	|tctyps.work.center
			break
		case ORIGIN.PRJ:
			var.whinh020.stty = 3	|tctyps.project
			break
		default:
			var.whinh020.stty = 2	|tctyps.partner
			break
		endcase
		break
	case TRANS.WIP.TRANSFER:
		var.whinh020.stty = 4	|tctyps.work.center
		break
	endcase
}

function determine.create.shipment(
			long	i.origin,
			long	i.transaction.type,
			long	i.sfty,
			long	i.stty)
{
	if i.transaction.type = TRANS.RECEIPT or
	   i.origin = ORIGIN.ASSEMBLY then
		var.whinh020.crsh = 40	|whinh.crsh.not.appl
		return
	endif

	if var.whinh020.sfty = 1 and	|tctyps.warehouse
	   var.whinh020.stty = 1 then
		var.whinh020.crsh = 20	|whinh.crsh.always
		return
	endif

	if var.whinh020.sfty = 4 and	|tctyps.work.center
	   var.whinh020.stty = 4 then
	   	var.whinh020.crsh = 20	|whinh.crsh.always
	   	return
	endif

	if i.transaction.type = TRANS.ISSUE then
	   	if i.origin = ORIGIN.SFC then
	   		var.whinh020.crsh = 10	|whinh.crsh.never
	   	else
			var.whinh020.crsh = 20	|whinh.crsh.always
		endif
		return
	endif
}	
								|#SP1.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:wh
:TCMOD:inh
:TFLNO:200
:FUNC:START
function extern mainprocess()
{
|	INIT.OUTPUT

	init.warehouse.order.migration.program()
	main.process.tdpur()
	main.process.tdsls()
	main.process.tdinv100()
	main.process.SFC.orders()
	main.process.tssma310()
	main.process.tdrpl100()
	main.process.tipcs530()

	migrate.runs()

}

|****************************** internal functions ****************************

function long pointer.to.mail()
{
	return(fpointer.to.mail)
}

function create.warehouse.order.header.if.needed(
		long		i.source,
		domain	tcorno	i.order,
		VARS.WAREHOUSE.ORDER.HEADER,
		DECLARE.HEADER)
{
	STATIC.VARS.WAREHOUSE.ORDER.HEADER
	static	Long	static.source
	static	domain	tcorno	static.order	 	|* Order
		long	converted.source

	converted.source = convert.source.to.origin(
				i.source,
				output.shipto.type)

	if	converted.source = static.source
	and 	i.order  = static.order
	and	COMPARE.STATIC.VARS.WAREHOUSE.ORDER.HEADER
	then
		return
	endif

	if converted.source = static.source and
	   i.order  = static.order  then
		output.order.set  = output.order.set + 1
		var.whinh200.oset = output.order.set
	else
		output.origin = converted.source
		output.order  = convert.number(
					convert.source.to.bcckon(
						i.source),
					i.order)
		output.order.set = 1

		var.whinh200.oorg = output.origin
		var.whinh200.orno = output.order
		var.whinh200.oset = output.order.set
	endif

	static.source = converted.source
	static.order  = i.order
	SET.STATIC.VARS.WAREHOUSE.ORDER.HEADER

	dump 200

	create.header.activities(
			var.whinh200.otyp,
			CALL.HEADER)

	CREATE.HISTORY( 250 )
	Dump 250
}

function create.warehouse.order.activities(
	VARS.ORDER.HEADER.ACTIVITIES)
{
	dump 204
}

function create.warehouse.inbound.order.line(
	DECLARE.HISTORY.INPUT,					|#205865.n
	VARS.INBOUND.ORDER.LINE
)
{
	DECLARE.HISTORY.FIELDS					|#205865.n

	dump 210
|	CREATE.HISTORY( 260 )					|#205865.o
	CREATE.HISTORY( 260 , 1 )				|#205865.n
	dump 260
}

function create.warehouse.approval(
	VARS.WAREHOUSE.APPROVAL)
{
	dump 211
}

function create.warehouse.inbound.advice(
	VARS.INBOUND.ADVICE)
{
	dump 215
}

function create.warehouse.inbound.line.activities(
VARS.INBOUND.LINE.ACTIVITIES)
{
	dump 214
}

function create.warehouse.inbound.BOM.line(
	DECLARE.HISTORY.INPUT,					|#205865.n
	VARS.INBOUND.BOM.LINE)
{
	DECLARE.HISTORY.FIELDS					|#205865.n

	dump 217
|	CREATE.HISTORY( 267 )					|#205865.o
	CREATE.HISTORY( 267 , 1 )				|#205865.n
	dump 267

}

function create.warehouse.outbound.line.activities(
	VARS.OUTBOUND.LINE.ACTIVITIES)
{
	dump 224
}

function create.warehouse.outbound.order.line(
	DECLARE.HISTORY.INPUT,					|#205865.n
	VARS.OUTBOUND.ORDER.LINE)
{
	DECLARE.HISTORY.FIELDS					|#205865.n

	dump 220
|	CREATE.HISTORY( 270  )					|#205865.o
	CREATE.HISTORY( 270 , 1 )				|#205865.en
	Dump 270

}

function create.warehouse.outbound.BOM.line(
		DECLARE.HISTORY.INPUT,				|#205865.n
		VARS.OUTBOUND.BOM.LINE)
{
	DECLARE.HISTORY.FIELDS					|#205865.n

	dump 227
|	CREATE.HISTORY( 277 )					|#205865.o
	CREATE.HISTORY( 277 , 1 )				|#205865.n
	Dump 277

}

function create.warehouse.outbound.advice(
	VARS.OUTBOUND.ADVICE)
{
	dump 225
}

function XXcreate.warehouse.outbound.advice.serial(
		Long	var.whinh226.oorg,	|* Order Origin
		String	var.whinh226.orno(9),	|* Order
		Long	var.whinh226.oset,	|* Set
		Long	var.whinh226.pono,	|* Line
		Long	var.whinh226.seqn,	|* Sequence
		Long	var.whinh226.sern,	|* Advice
		Long	var.whinh226.boml,	|* BOM Line
		Long	var.whinh226.dssq,	|* Distribution Seq
		String	var.whinh226.serl(30),	|* Obsolete
		String	var.whinh226.clot(20),	|* Lot
		String	var.whinh226.cser(30),	|* Serial Number
		Long	var.whinh226.trej,	|* To be Rejected
		Long	var.whinh226.reje,	|* Rejected
		Long	var.whinh226.stgd,	|* Staged
		Double	var.whinh226.qads,	|* Quantity Advise
		String	var.whinh226.cuni(3),	|* 	Storage Unit
		Double	var.whinh226.qtba,	|* Quantity to be A
		Double	var.whinh226.qaps,	|* Quantity Approve
		Double	var.whinh226.qtbr,	|* Quantity to be R
		Double	var.whinh226.qrjs,	|* Quantity Reject
		Double	var.whinh226.qadv,	|* Quantity Advise
		Double	var.whinh226.qapr,	|* Quantity Approv
		Double	var.whinh226.qrej,	|* Quantity Reject
		String	var.whinh226.reas(6),	|* Reason
		String	var.whinh226.apno(9),	|* Inspection
		Long	var.whinh226.apsq)	|* Inspection Line
{
	dump 226
}

function create.warehouse.load(
	VARS.LOAD)
{
	dump 440
	CREATE.HISTORY( 460 )
	Dump 460
}

function create.warehouse.shipment.header(
	VARS.SHIPMENT.HEADER)
{
	dump 430
	CREATE.HISTORY( 450 )
	Dump 450
}

function create.warehouse.shipment.line(
	DECLARE.HISTORY.INPUT,					|#205865.n
	VARS.SHIPMENT.LINE)
{
	DECLARE.HISTORY.FIELDS					|#205865.n

	dump 431
|	CREATE.HISTORY( 451 )					|#205865.o
	CREATE.HISTORY( 451 , 1 )				|#205865.n
	Dump 451
}

function create.warehouse.shipment.line.packing.structure(
	DECLARE.HISTORY.INPUT,					|#205865.n
	VARS.SHIPMENT.LINE.PACKING.STRUCTURE)
{
	DECLARE.HISTORY.FIELDS					|#205865.n

	dump 436
|	CREATE.HISTORY( 456 )					|#205865.o
	CREATE.HISTORY( 456 , 1 )				|#205865.n
	Dump 456
}

function create.warehouse.receipt.header(
	VARS.RECEIPT.HEADER)
{
	dump 310
	CREATE.HISTORY( 360 )
	Dump 360

}

function create.warehouse.receipt.line(
	DECLARE.HISTORY.INPUT,					|#205865.n
	VARS.RECEIPT.LINE)
{
	DECLARE.HISTORY.FIELDS					|#205865.n

	dump 312
|	CREATE.HISTORY( 362 )					|#205865.o
	CREATE.HISTORY( 362 , 1)				|#205865.n
	Dump 362

}

function create.warehouse.receipt.line.packing.structure(
	DECLARE.HISTORY.INPUT,					|#205865.n
	VARS.RECEIPT.LINE.PACKING.STRUCTURE)
{
	DECLARE.HISTORY.FIELDS					|#205865.n

	dump 316
|	CREATE.HISTORY( 366 )					|#205865.o
	CREATE.HISTORY( 366 , 1 )				|#205865.n
	Dump 366
}

function create.warehouse.receipt.BOM.line(
	DECLARE.HISTORY.INPUT,					|#205865.n
	VARS.RECEIPT.BOM.LINE)
{
	DECLARE.HISTORY.FIELDS					|#205865.n

	dump 318
|	CREATE.HISTORY( 368 )					|#205865.o
	CREATE.HISTORY( 368 , 1)				|#205865.n
	Dump 368
}

function create.warehouse.receipt.line.serials(
		String	var.whinh320.rcno(9),		|* Receipt Number
		Long	var.whinh320.rcln,		|* Receipt Line
		String	var.whinh320.serl(30),		|* Serial Number
		Long	var.whinh320.oorg,		|* Order Origin
		String	var.whinh320.orno(9) fixed,	|* Order
		Long	var.whinh320.oset,		|* Set
		Long	var.whinh320.pono,		|* Position
		Long	var.whinh320.seqn,		|* Sequence
		Long	var.whinh320.corr,		|* Receipt Correction
		Long	var.whinh320.trej,		|* Qty to be Rejected
		Long	var.whinh320.arej,		|* Inspection Rejec.
		Long	var.whinh320.tdes,		|* To be Destroyed
		Long	var.whinh320.ades,		|* Inspection Dest
		Long	var.whinh320.reje,		|* In Rejected Inventory
		Long	var.whinh320.prej,		|* Process Rejecte
		Long	var.whinh320.aloc)		|* Allocated
{
|	dump320
|	CREATE.HISTORY( 370 )
|	Dump370
}

function create.warehouse.run(
		String	var.whinh400.runn(12) fixed,
		Long	var.whinh400.kofr,
		String	var.whinh400.dsca(30) mb fixed,
		Long	var.whinh400.lprt,
		String	var.whinh400.ssel(1) fixed,
		String	var.whinh400.logn(16) fixed,
		DECLARE.UTC(	var.whinh400.date))
{
	dump 400
}

function domain tcbool table.must.be.converted(...)
{
	return(false)
}

|#sp1.sn
|* the following functions and dumps are added but never filled:

function create.warehouse.inbound.line.lots.serials(
		long	var.whinh216.oorg,		|* Order Origin
		string	var.whinh216.orno(9) fixed,	|* Order
		Long	var.whinh216.pono,		|* Position
		Long	var.whinh216.seqn,		|* Sequence
		Long	var.whinh216.boml,		|* BOM Line
		Long	var.whinh216.dssq,		|* Distribution Sequence
		string	var.whinh216.clot(20) fixed,	|* Lot
		string	var.whinh216.serl(30) fixed,	|* Serial
		double	var.whinh216.qoro,
		string	var.whinh216.orun,
		double	var.whinh216.qord)
{
	dump 216

	CREATE.HISTORY( 266 )
	Dump 266
}
|#sp1.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:wh
:TCMOD:inr
:TFLNO:100
:FUNC:START
function void get.business.partner.due.to.kind.of.order(
			domain	tcrlcd	i.business.partner,
			domain	tckoor	i.kind.of.order)
{
	domain	tcmcs.s200m	errmess
	if isspace(i.business.partner) then
		var.whinr100.bpid = ""
		return
	endif

 	on case i.kind.of.order
	case tckoor.act.pur :
 	case tckoor.mps.pur :
 	case tckoor.pcs.pur :
 	case tckoor.pur.con :
 	case tckoor.pss.pur :
	case tckoor.mrp.pur :
 		if get.business.partner(	i.business.partner,
					bcstc.orgn.suno,
					errmess)  = 0 then
			var.whinr100.bpid = bcstc008.bpid
		endif
		break
 	case tckoor.act.sls :
 	case tckoor.sls.quot :
 	case tckoor.sls.con :
	case tckoor.sma.order :
 	case tckoor.drp.sls :
 		if get.business.partner(	i.business.partner,
					bcstc.orgn.cuno,
					errmess)  = 0 then
			var.whinr100.bpid = bcstc008.bpid
		endif
 	 	break
	default:
 		if get.business.partner(	i.business.partner,
					bcstc.orgn.suno,
					errmess)  = 0 then
			var.whinr100.bpid = bcstc008.bpid
		endif
		break
 	endcase
}

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

	new.position = tdilc301.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 determine.sern.correction(
	ref long o.sern.correction)
{
	select	count(tdilc301.sern):o.sern.correction
	from	tdilc301
	where	tdilc301._index2 = {	:tdilc301.cwar,
					:tdilc301.loca,
					:tdilc301.cprj,
					:tdilc301.item,
					:tdilc301.cntr}
	and	(tdilc301.clot <= :tdilc301.clot or
		 tdilc301.clot = :tdilc301.clot AND tdilc301.sern <= :tdilc301.sern)
	and	tdilc301.date = :tdilc301.date
	and	tdilc301.stun = :tdilc301.stun
	and	tdilc301.trdt = :tdilc301.trdt
	and	tdilc301.trtm = :tdilc301.trtm
	as set with 1 rows
	selectdo
	endselect
}
								|#
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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

 	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
			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
			dump 3
 			if isspace(var.whinr110.clot) then  
 	 			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),  
 				 	 	 	hold.tdinv700.trdt, 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)
		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)
			var.whinr110.clot = new.lot
			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
			split.up = true
		else
			arr.lots(21,arr.nr) = str$(0.0)
			var.whinr110.clot = new.lot
			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
			qty.to.be.split = qty.to.be.split - new.qty
		endif
		if isspace(var.whinr110.clot) then  
 			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),  
			 	 	 	hold.tdinv700.trdt, 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	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.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
|	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.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						
	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.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

 	  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  
			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

 		  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

	 	  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
	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	tcdate	i.trdt)
{
	select	tdilc301.clot
	from	tdilc301
	where	tdilc301._index1 = {"", :tdinv700.item, ""}
	and	tdilc301.cwar = :tdinv700.cwar
	and	tdilc301.trdt = :i.trdt
|	and	tdilc301.trtm = :tdinv700.trtm
	and	tdilc301.koor = :tdinv700.koor
	and	tdilc301.kost = :tdinv700.kost
	and	tdilc301.orno = :tdinv700.orno
	and	(tdilc301.koor = tckoor.wrh.order or tdilc301.pono = :tdinv700.pono)
	and	tdilc301.qstk = :tdinv700.quan
	as set with 1 rows
	selectdo
		return(tdilc301.clot)
	endselect

	return("")
}									|#TELAIR.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()
{
	select	tdinv001.cwar, tdinv001.item, tdinv001.ltdt, tdinv001.stoc,
		tiitm001.stva
	from	tdinv001, tiitm001
	where 	 tdinv001.item >  ""                                         
	and 	 tdinv001.cntr =  ""
	and	tdinv001.stoc > 0
	and	not exists(	select	tdinv700.item
				from	tdinv700
				where	tdinv700._index1 = {tdinv001.item, "", tdinv001.cwar})
	and	tiitm001.item = tdinv001.item
	selectdo

		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

 	 	 if tiitm001.stva =  tcstva.stnd.copr then  
 	 	 	 var.whinr110.valm =  1 | *  ftp  
 	 	 else  
 	 	 	 var.whinr110.valm =  5 | *  lot  

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

 		 if var.whinr110.valm =  5 then
 		 	 var.whinr110.clot =  determine.lot(tdinv001.ltdt)  
 		 else  
 		 	 var.whinr110.clot =  empty()  
 		 endif 

		hold.tdinv700.trdt = tdinv001.ltdt

 	 	 var.whina112.qskt =  tdinv001.stoc  
 	 	 var.whina112.qstk =  tdinv001.stoc  

 	 	 if var.whinr110.valm =  5 then
 	 	 	 split.up.lots.inventory()  
 	 	 else  
 	 	 	 dump 1
 
 	 	 	 dump 3  
 	 	 	 if isspace(var.whinr110.clot) then
 	 	 	 	 get.standard.cost.price(tdinv700.item, tdinv001.stoc,  
 	 	 	 	 	 	 	 tdinv001.ltdt, true, false)
 	 	 	 else  
 	 	 	 	 get.lot.price(tdinv700.item, var.whinr110.clot, tdinv001.stoc,  
 	 	 	 	 	 	 	 tdinv001.ltdt, true, false)  
 	 	 	 endif
 	 	 endif
	endselect
}

function handle.inventory.without.tipcs700()
{
	select	tipcs021.cprj, tipcs021.item, tipcs021.cwar, tipcs021.ltdt, tipcs021.stoc
	from	tipcs021
	where	tipcs021.cprj > ""
	and	tipcs021.stoc > 0
	and	not exists(	select	tipcs700.item
				from	tipcs700
				where	tipcs700._index1 = {tipcs021.cprj, tipcs021.item})
	selectdo

		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.prnt = tcyesno.no
		var.whinr110.valm = 1
		var.whinr110.clot = ""

 	 	 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 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
		new.cwar = tipcs021.cwar
	    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
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:wh
:TCMOD:inr
:TFLNO:140
:FUNC:START
|#206758.sn
function create.stockpoint.from.stock.point()
|* 206758: copy of main query; there no marking
{
	 if lot.controlled.item( tdilc101.cprj,
 	 	 	 	 tdilc101.item) then
 	 	 lot.present =  true
 	 else
 	 	 lot.present =  false
 	 endif
 	 | *  Note:
 	 | * -  in case of lot.by.unit (serialized) item break on the lot code will be ignored.
 	 | * -  in case of By Location item break on the date will be ignored.
 	 if (hold.tdilc101.cwar <>  tdilc101.cwar or
 	 hold.tdilc101.loca <>  tdilc101.loca or
 	 hold.tdilc101.cprj <>  tdilc101.cprj or
 	 hold.tdilc101.item <>  tdilc101.item or
 	 (hold.tdilc101.clot <>  tdilc101.clot and
 	 lot.present) or
 	 (hold.tdilc101.date <>  tdilc101.date and
 	 fifo.lifo.item(tdilc101.cprj,
 	 	 	 tdilc101.item) 	 and
 	 not lot.present)) then
 	 	 | *  dump previous stock point
 	 	 if not isspace(hold.tdilc101.cwar) then
 	 	 	 var.whinr140.crdt =  utc(creation.date, 0)
 	 	 	 var.whinr140.trdt =  utc(transaction.date, 0)
 	 	 	 var.whinr140.lsid =  utc(last.storage.insp.date, 0)
 	 	 	 if stock.is.present() then
 	 	 	 	 Dump 1
 	 	 	 	 dump.inventory.structure.records()
 	 	 	 endif
 	 	 	 reset.stock.point()
 	 	 	 | *  Reset whinr150 fields and arrays
 	 	 	 reset.inventory.structure()
 	 	 endif
 	 	 hold.tdilc101.cwar =  tdilc101.cwar
 	 	 hold.tdilc101.loca =  tdilc101.loca
 	 	 hold.tdilc101.cprj =  tdilc101.cprj
 	 	 hold.tdilc101.item =  tdilc101.item
 	 	 hold.tdilc101.clot =  tdilc101.clot
 	 	 hold.tdilc101.date =  tdilc101.date
 	 endif
 	 | *  Fill inventory structure
 	 var.whinr140.cwar =  tdilc101.cwar
 	 var.whinr140.loca =  tdilc101.loca
 	 if not isspace(tdilc101.loca) then
 	 	 | *  get message from the location
 	 	 read.location.message(
 	 	 	 	 tdilc101.cwar,
 	 	 	 	 tdilc101.loca,
 	 	 	 	 var.whinr140.crem)
 	 else
 	 	 var.whinr140.crem =  empty()
 	 endif

 	 seg.whinr140.item =  tdilc101.item
 	 seg.whinr140.cprj =  get.ti.project(tdilc101.cprj)
 	 if lot.controlled.item( tdilc101.cprj,
 	 	 	 	 tdilc101.item) then
 	 	 var.whinr140.clot =  tdilc101.clot
 	 else
 	 	 var.whinr140.clot =  empty()
 	 endif
 	 | *  inventory date
 	 if fifo.lifo.item( 	 tdilc101.cprj,
 	 	 	 tdilc101.item) then
 	 	 	 	 	 	 	 	 | #  SL149314.so
 | 	 	 if lot.controlled.item(
 | 	 	 	 	 tdilc101.cprj,
 | 	 	 	 	 tdilc101.item) or
 | 	 	 serialized.item(
 | 	 	 	 	 tdilc101.cprj,
 | 	 	 	 	 tdilc101.item) then
 	 	 	 	 	 	 	 	 | #  SL149304.eo
 	 	 	 	 	 	 	 	 | #  SL149314.sn
 	 	 if lot.controlled.item(
 	 	 	 	 tdilc101.cprj,
 	 	 	 	 tdilc101.item) then
 	 	 	 	 	 	 	 	 | #  SL149304.en
 	 	 	 get.lot.date( 	 tdilc101.cprj,
 	 	 	 	 	 tdilc101.item,
 	 	 	 	 	 tdilc101.clot,
 	 	 	 	 	 inventory.date)
 	 	 else
 	 	 	 inventory.date =  tdilc101.date
 	 	 endif
 	 else
 	 	 inventory.date =  0
 	 endif
 	 if tdilc101.stun =  inventory.unit(tdilc101.cprj, tdilc101.item) or
 	 storage.unit.due.to.conversion.factor(
 	 	 	 	 tdilc101.stun,
 	 	 	 	 tdilc101.stks,
 	 	 	 	 tdilc101.strs) then
 	 	 storage.unit =  tdilc101.stun
 	 	 convert.to.inv.unit =  false
 	 else
 	 	 storage.unit =  inventory.unit(tdilc101.cprj, tdilc101.item)
 	 	 convert.to.inv.unit =  true
 	 endif
 	 get.position.in.array(
 	 	 	 storage.unit,
 	 	 	 position.in.array)
 	 array.whinr150.cuni(1,position.in.array) =  storage.unit
 	 array.whinr150.qhnd(position.in.array) =  array.whinr150.qhnd(position.in.array) +  tdilc101.stks
 	 if convert.to.inv.unit then
 	 	 array.whinr150.qhds(position.in.array) =  array.whinr150.qhds(position.in.array) +  tdilc101.stks
 	 	 if tdilc101.stra =  tdilc101.strs then
 	 	 	 array.whinr150.qals(position.in.array) =
 	 	 	 	 array.whinr150.qals(position.in.array) +  tdilc101.stks
 	 	 endif
 	 else
 	 	 array.whinr150.qhds(position.in.array) =  array.whinr150.qhds(position.in.array) +  tdilc101.strs
 	 	 array.whinr150.qals(position.in.array) =  array.whinr150.qals(position.in.array) +  tdilc101.stra
 	 endif
 	 determine.blocked.quantity(
 	 	 	 tdilc101.cwar,
 	 	 	 tdilc101.loca,
 	 	 	 "",       |*                           Project
 	 	 	 tdilc101.item,
 	 	 	 tdilc101.clot,
 	 	 	 tdilc101.date,
 	 	 	 tdilc101.stun,
 	 	 	 tdilc101.strb,
 	 	 	 tdilc101.stra,
 	 	 	 process.blocked,
 	 	 	 manual.blocked)
 	 if convert.to.inv.unit then
 	 	 if process.blocked >  0.0 then
 	 	 	 array.whinr150.prbl(position.in.array) =
 	 	 	 	 array.whinr150.prbl(position.in.array) +  tdilc101.stks
 	 	 else
 	 	 	 array.whinr150.prbl(position.in.array) =
 	 	 	 	 array.whinr150.prbl(position.in.array) +  process.blocked
 	 	 endif
 	 	 if manual.blocked >  0.0 then
 	 	 	 array.whinr150.mabl(position.in.array) =
 	 	 	 	 array.whinr150.mabl(position.in.array) +  tdilc101.stks
 	 	 else
 	 	 	 array.whinr150.mabl(position.in.array) =
 	 	 	 	 array.whinr150.mabl(position.in.array) +  manual.blocked
 	 	 endif
 	 else
 	 	 array.whinr150.prbl(position.in.array) =
 	 	 	 	 array.whinr150.prbl(position.in.array) +  process.blocked
 	 	 array.whinr150.mabl(position.in.array) =
 	 	 	 	 array.whinr150.mabl(position.in.array) +  manual.blocked
 	 endif
 	 array.whinr150.mabp(position.in.array) =
 	 	 	 array.whinr150.mabp(position.in.array) +  manual.blocked
 	 if tdilc101.qbsi >  0.0 and
 	 tdilc101.stun =  inventory.unit(tdilc101.cprj, tdilc101.item) then
 	 	 array.whinr150.mabl(position.in.array) =
 	 	 	 array.whinr150.mabl(position.in.array) +  tdilc101.qbsi
 	 	 array.whinr150.mabp(position.in.array) =
 	 	 	 array.whinr150.mabp(position.in.array) +  tdilc101.qbsi
 	 	 if array.whinr150.mabl(position.in.array) >  array.whinr150.qhds(position.in.array) then
 	 	 	 array.whinr150.mabl(position.in.array) =  array.whinr150.qhds(position.in.array)
 	 	 	 array.whinr150.mabp(position.in.array) =  array.whinr150.qhds(position.in.array)
 	 	 endif
 	 endif
 	 total.blocked.str =  array.whinr150.prbl(position.in.array) +
 	 	 	 array.whinr150.mabl(position.in.array)
 	 if total.blocked.str =  0 then
 	 	 total.blocked.inv =  0
 	 else
 	 	 if tdilc101.stun =  inventory.unit(tdilc101.cprj, tdilc101.item) then
 	 	 	 total.blocked.inv =  total.blocked.str
 	 	 else
 	 	 	 convert.quantity.to.inventory.unit(
 	 	 	 	 	 	 total.blocked.str,
 	 	 	 	 	 	 tdilc101.stun,
 	 	 	 	 	 	 total.blocked.inv)
 	 	 endif
 	 endif
 	 var.whinr140.idat =  utc(inventory.date, 0)
 	 calculate.available.quantity.in.array(
 	 	 	 	 position.in.array,
 	 	 	 	 decrease.total.blocked.str)
 	 if decrease.total.blocked.str >  0 then
 	 	 convert.quantity.to.inventory.unit( 	 decrease.total.blocked.str,
 	 	 	 	 	 	 	 tdilc101.stun,
 	 	 	 	 	 	 	 decrease.total.blocked.inv)
 	 	 total.blocked.inv =  total.blocked.inv -  decrease.total.blocked.inv
 	 endif
 	 | *  fill dates
 	 | *  creaton date
 	 if creation.date =  0 then
 	 	 creation.date =  tdilc101.crea
 	 else
 	 	 creation.date =  min(creation.date, tdilc101.crea)
 	 endif
 	 | *  transaction date
 	 transaction.date =  max(transaction.date, tdilc101.tran)
 	 | *  last storage inspection date
 	 last.storage.insp.date =  max(last.storage.insp.date, tdilc101.lsid)
 	 | *  cumulate stock quantities
 	 var.whinr140.qhnd =  var.whinr140.qhnd +  tdilc101.stks
 	 var.whinr140.qblk =  var.whinr140.qblk +  total.blocked.inv
 	 var.whinr140.qlal =  var.whinr140.qlal +  tdilc101.stka
 	 var.whinr140.qord =  var.whinr140.qord +  tdilc101.stko
 	 var.whinr140.ball =  tdilc101.blst
 	 var.whinr140.bout =  tdilc101.outb
 	 var.whinr140.btri =  tdilc101.trfb
 	 if tdilc000.take =  tcyesno.yes then
 	 	 var.whinr140.bcyc =  tdilc101.take
 	 else
 	 	 var.whinr140.bcyc =  tcyesno.no
 	 endif

}

function handle.stock.point.end.of.set()
{
	 if not isspace(hold.tdilc101.cwar) then
 	 	 var.whinr140.crdt =  utc(creation.date, 0)
 	 	 var.whinr140.trdt =  utc(transaction.date, 0)
 	 	 var.whinr140.lsid =  utc(last.storage.insp.date, 0)
 	 	 if stock.is.present() then
 	 	 	 Dump 1
 	 	 	 dump.inventory.structure.records()
 	 	 endif
 	 endif
}
						|#206758.en
function reset.hold.variables()
{
	hold.tdilc101.cwar = ""
	hold.tdilc101.loca = ""
	hold.tdilc101.cprj = ""
	hold.tdilc101.item = ""
	hold.tdilc101.clot = ""
	hold.tdilc101.date = 0
}

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 void calculate.available.quantity.in.array(
			long		i.position.in.array,
		ref	domain	tcqst1	o.decrease.blocked.str)
{
	o.decrease.blocked.str = 0.0

	array.whinr150.qavs(i.position.in.array) =
		array.whinr150.qhds(i.position.in.array) -
		array.whinr150.prbl(i.position.in.array) -
		array.whinr150.mabl(i.position.in.array) -
		array.whinr150.qals(i.position.in.array)
	if array.whinr150.qavs(i.position.in.array) < 0 then
		|* Perhaps blocked quantity is too high
		if array.whinr150.prbl(i.position.in.array) >
		   abs(array.whinr150.qavs(i.position.in.array)) then
			o.decrease.blocked.str =
				abs(array.whinr150.qavs(i.position.in.array))
			array.whinr150.prbl(i.position.in.array) =
				array.whinr150.prbl(i.position.in.array) -
				abs(array.whinr150.qavs(i.position.in.array))
			array.whinr150.qavs(i.position.in.array) = 0.0
		endif
	endif
	if array.whinr150.qhds(i.position.in.array) > 0 then
		array.whinr150.qavl(i.position.in.array) =
		array.whinr150.qhnd(i.position.in.array) -
		(array.whinr150.prbl(i.position.in.array) +
		 array.whinr150.mabl(i.position.in.array) +
		 array.whinr150.qals(i.position.in.array)) *
			 (array.whinr150.qhnd(i.position.in.array)/
			  array.whinr150.qhds(i.position.in.array))
	else
		array.whinr150.qavl(i.position.in.array) =
			array.whinr150.qavs(i.position.in.array)
	endif
}

function void calculate.available.quantity()
{
	var.whinr150.qavs = var.whinr150.qhds -
				var.whinr150.prbl -
				var.whinr150.qals
	if var.whinr150.qhds > 0 then
		var.whinr150.qavl =
			var.whinr150.qhnd -
			(var.whinr150.prbl +
		 	 var.whinr150.mabl +
			 var.whinr150.qals) *
			(var.whinr150.qhnd/var.whinr150.qhds)
	else
		var.whinr150.qavl = var.whinr150.qavs
	endif
}

function	reset.stock.point()
{
	var.whinr140.cwar = ""
 	var.whinr140.loca =  ""
 	seg.whinr140.item =  ""
 	seg.whinr140.cprj =  ""
	var.whinr140.idat = utc(0,0)

	creation.date = 0
	transaction.date = 0
	last.storage.insp.date = 0
	inventory.date = 0

 	var.whinr140.qhnd = 0.0
 	var.whinr140.qblk = 0.0
 	var.whinr140.qlal = 0.0
 	var.whinr140.qord = 0.0

 	var.whinr140.ball =  tcyesno.no
 	var.whinr140.bout =  tcyesno.no
 	var.whinr140.btri =  tcyesno.no
	var.whinr140.bcyc =  tcyesno.no
}

function reset.inventory.structure()
{
	long	i

	var.whinr150.cuni = ""
	var.whinr150.qhnd = 0.0
	var.whinr150.qhds = 0.0
	var.whinr150.qals = 0.0
	var.whinr150.qavs = 0.0
	var.whinr150.qavl = 0.0
	var.whinr150.prbl = 0.0
	var.whinr150.mabl = 0.0
	var.whinr150.mabp = 0.0
	for i = 1 to nr.of.units
		array.whinr150.cuni(1,i) = ""
		array.whinr150.qhnd(i) = 0.0
		array.whinr150.qhds(i) = 0.0
		array.whinr150.qals(i) = 0.0
		array.whinr150.qavs(i) = 0.0
		array.whinr150.qavl(i) = 0.0
		array.whinr150.prbl(i) = 0.0
		array.whinr150.mabl(i) = 0.0
		array.whinr150.mabp(i) = 0.0
	endfor
	nr.of.units = 0
}

function get.position.in.array(
			domain	tccuni	i.unit,
		ref	long		o.position.in.array)
{
	long	i

	o.position.in.array = 0
	if nr.of.units = 0 then
		nr.of.units = 1
		o.position.in.array = 1
	else
		for i = 1 to nr.of.units
			if i.unit = array.whinr150.cuni(1,i) then
				o.position.in.array = i
				return
			endif
		endfor
		nr.of.units = nr.of.units + 1
		o.position.in.array = nr.of.units
	endif
}

function domain tccuni inventory.unit(
			domain	tccprj	i.cprj,
			domain	tcitem	i.item)
{
	domain	tccuni	inv.unit
	inv.unit = ""

	if isspace(i.cprj) then
		select	tiitm001.cuni:inv.unit
		from	tiitm001
		where	tiitm001.item = :i.item
		as set with 1 rows
		selectdo
		endselect
	else
		select	tipcs021.cuni:inv.unit
		from	tipcs021
		where	tipcs021._index1 = {:i.cprj, :i.item}
		as set with 1 rows
		selectdo
		endselect
	endif

	return(inv.unit)
}

function read.location.message(
			domain	tccwar		i.warehouse,
			domain	tdilc.loca 	i.location,
		ref	domain	tdilc.cmes 	o.message)
{
	o.message = ""
	select	tdilc001.cmes:o.message
	from	tdilc001
	where	tdilc001._index1 = {	:i.warehouse,
					:i.location}
	as set with 1 rows
	selectdo
	endselect
}

function domain tcbool stock.is.present()
{
	if var.whinr140.qhnd <> 0 or
	var.whinr140.qord <> 0 or
	var.whinr140.qlal <> 0 or
	var.whinr140.qblk <> 0 then
		return(true)
	endif

	return(false)
}

function domain tcbool structure.is.present()
{
	if var.whinr150.qhnd <> 0 or
	var.whinr150.qavl <> 0 or
	var.whinr150.prbl <> 0 or
	var.whinr150.mabl <> 0 then
		return(true)
	endif

	return(false)
}

function void get.lot.date(
			domain	tccprj		i.project,
			domain	tcitem		i.item,
			domain	tdltc.clot	i.lot,
		ref	domain	tcdate		o.lot.date)
{
	o.lot.date = 0
	select	tdltc001.ardt:o.lot.date
	from	tdltc001
	where	tdltc001._index1 = {	:i.project,
					:i.item,
					"",		|* Container
					:i.lot}
	as set with 1 rows
	selectdo
	endselect

	|* If first receipt date of lot is 0 then lot date = current date
 	 if o.lot.date =  0 then
 	  	 o.lot.date =  date.num()
 	  endif
}

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 storage.unit.due.to.conversion.factor(
			domain	tccuni		i.unit,
			domain	tcqiv1		i.quantity.inv.unit,
			domain	tcqst1		i.quantity.st.unit)
{
	domain tcconv	conversion.factor

	if i.quantity.st.unit = 0.0 then
		return(false)
	endif
	conversion.factor = tiitm.dll0003.conv.factor(
					seg.whinr140.cprj,
					seg.whinr140.item,
					"",	|* Item Group
					"",	|* Base Unit
					i.unit)
	if not i.quantity.inv.unit/i.quantity.st.unit = conversion.factor then
		return(false)
	endif
	return(true)
}

function void convert.quantity.to.inventory.unit(
			domain	tcqst1	i.quantity.str,
			domain	tccuni	i.unit,
		ref	domain	tcqiv1	o.quantity.inv)
{
	domain tcconv	conversion.factor

	conversion.factor = tiitm.dll0003.conv.factor(
					seg.whinr140.cprj,
					seg.whinr140.item,
					"",	|* Item Group
					"",	|* Base Unit
					i.unit)
	o.quantity.inv = i.quantity.str * conversion.factor
}
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 dump.inventory.structure.records()
{
	long	i

	for i = 1 to nr.of.units
		var.whinr150.cuni = array.whinr150.cuni(1,i)
		var.whinr150.qhnd = array.whinr150.qhnd(i)
		var.whinr150.qhds = array.whinr150.qhds(i)
		var.whinr150.qals = array.whinr150.qals(i)
		var.whinr150.qavs = array.whinr150.qavs(i)
		var.whinr150.qavl = array.whinr150.qavl(i)
		var.whinr150.prbl = array.whinr150.prbl(i)
		var.whinr150.mabl = array.whinr150.mabl(i)
		var.whinr150.mabp = array.whinr150.mabp(i)
		Dump 2		|* Inventory Structure
	endfor
}

|#800-142497.sn
function correct.stockpoint.for.cycle.count()
{
db.retry.point()

select 	 tdilc101.*
from 	 tiitm001, tdilc501, tdilc101 for update, tcmcs003
where	 tdilc101._index1 = {	tdilc501.cwar,
				tdilc501.loca,
				tdilc501.cprj,
				tdilc501.item,
				tdilc501.cntr,
				tdilc501.clot,
				tdilc501.date,
				tdilc501.stun}
 and 	 tdilc501.item =  tiitm001.item
 and 	 tiitm001.kitm in (tckitm.purchase, tckitm.manufacture)
 and 	 tdilc501.cntr =  ""
 and 	 tdilc501.cwar refers to tcmcs003
 and 	 tcmcs003.typw <>  tctypw.transito
 and 	 tdilc101.take =  tcyesno.no
 and 	 tdilc101.loca <>  tcmcs003.arlo
 and	 tdilc501.orno > 0	|* best for performance
selectdo
	tdilc101.take =  tcyesno.yes
	db.update(ttdilc101, db.retry)
endselect
commit.transaction()

}
|#800-142497.en
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
: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.  

	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
	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,
				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
					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)			|#800-144866.o
		var.whwmd215.hstd = utc(0,0)					|#800-144866.n
		var.whwmd215.lsid = utc(tdinv001.lsid, 0)
		var.whwmd215.qcis = tdinv001.uscu
|		var.whwmd215.qhib = tdinv001.hstb				|#800-144866.o
		var.whwmd215.qhib = 0.0						|#800-144866.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,
	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
			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
	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,
			domain	tccprj	i.project,
			domain	tcitem	i.item,
		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)

	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

		long	result			| result of search
	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	search_def(1,4)			| qss search definition
	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

	| 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.item)
		result = qss.search(QSS.EQUAL,i.item,item_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

	s.item = i.item
	s.o.location.allocated.quantity = o.location.allocated.quantity

|	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
}


|********* MIGRATE FROM TIPCS021 ******************

function migrate.project.item.data.by.warehouse()
{
	select 	 tipcs021.* , tipcs020.prtp, tcmcs003.sloc
	from 	 tipcs021, tipcs020, tcmcs003
	where 	 tipcs020._index1 = {tipcs021.cprj}  
	and	tcmcs003.cwar = tipcs021.cwar
	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()  
		create.inventory.cost.details.from.project()  
	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

	dump 211
}

function create.inventory.from.project()
{
	var.whwmd215.cwar = tipcs021.cwar  
	g.new.project.code = get.project(tipcs021.cprj)  
	var.whwmd215.item = segment.item.code(	tipcs021.item, 
						g.new.project.code)  
	if is.masterdata.conversion() then  
		init.inventory.fields()
	else  
		var.whwmd215.qhnd = tipcs021.stoc  
		var.whwmd215.qchd = 0.0
		set.quantities.blocked(
				tipcs021.cwar,
				tipcs021.item,
				tipcs021.blck,
				0.0)	|* blocked for inspection
		var.whwmd215.qord = tipcs021.ordr  
		var.whwmd215.qcor = 0.0  
		var.whwmd215.qint = 0.0  
		var.whwmd215.qall = tipcs021.allo  
		var.whwmd215.qcal = 0.0  
		var.whwmd215.qcom = tipcs021.hall  
		if tdilc000.iilc = tcyesno.yes then  
			determine.location.allocated.quantity(  
					tipcs021.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)  
		var.whwmd215.qcis = tipcs021.uscu  
		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
{
									|#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  
		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
		endfor	 
		if is.masterdata.conversion() then  
			e = set.mem(var.whwmd217.mauc, 0)
		else
			e = copy.mem(	var.whwmd217.mauc(i),
					var.whwmd217.ftpa(i),
					g.mc.depth)
		endif  
		
		dump 217 | *  whwmd217 -  Item Inventory by Warehouse Cost Details
		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()
{
	|* init the mauc value; overwrite if not masterdata conversion		 
	e = set.mem(var.whwmd217.mauc, 0)

	| *  Material Cost Component  
	var.whwmd217.cpcp = material.component
	e = copy.mem(var.whwmd217.ftpa, material.component.ftpa, g.mc.depth)

	if not is.masterdata.conversion() then
		e = copy.mem(var.whwmd217.mauc, var.whwmd217.ftpa, g.mc.depth)
	endif  
	
	dump 217 | *  whwmd217 -  Item Inventory by Warehouse Cost Details

	| *  Operation Cost Component  
	var.whwmd217.cpcp = operation.component  
	e = copy.mem(var.whwmd217.ftpa, operation.component.ftpa, g.mc.depth)

	if not is.masterdata.conversion() then  
		e = copy.mem(var.whwmd217.mauc, var.whwmd217.ftpa, g.mc.depth)
	endif  
	
	dump 217 | *  whwmd217 -  Item Inventory by Warehouse Cost Details

	| *  Surcharge Cost Component  
	var.whwmd217.cpcp = surcharge.component
	e = copy.mem(var.whwmd217.ftpa, surcharge.component.ftpa, g.mc.depth)

	if not is.masterdata.conversion() then
		e = copy.mem(var.whwmd217.mauc, var.whwmd217.ftpa, g.mc.depth)
	endif  
	
	dump 217 | *  whwmd217 -  Item Inventory by Warehouse Cost Details
}

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 
}
:FUNC:END
:TEXT:

:TEXT:END
FUNC160
:MPRF:18
:VERS:B4st
:RELE:61
:CUST:sy25
:TCPAC:wh
:TCMOD:wmd
:TFLNO:300
:FUNC:START
function set.location.type.values.for.inspection.and.receiving()
{
	|* Inbound
	var.whwmd300.inlo = tcyesno.yes
	var.whwmd300.binb = tdilc001.inbl
	|* Outbound
	var.whwmd300.outl = tcyesno.no
	var.whwmd300.bout = tcyesno.no
	|* Transfer (Receipt)
	var.whwmd300.trfr = tcyesno.no
	var.whwmd300.btrr = tcyesno.no
	|* Transfer (Issue)
	var.whwmd300.trto = tcyesno.no
	var.whwmd300.btri = tcyesno.no
	|* Assembly
	var.whwmd300.aslo = tcyesno.no
	var.whwmd300.bass = tcyesno.no
}

function set.location.type.values.for.bulk()
{
	|* Inbound
	var.whwmd300.inlo = tdilc001.inlo
	var.whwmd300.binb = tdilc001.inbl
	|* Outbound
	var.whwmd300.outl = tdilc001.outl
	var.whwmd300.bout = tdilc001.outb
	|* Transfer (Receipt)
	var.whwmd300.trfr = tdilc001.trfr
	var.whwmd300.btrr = tdilc001.trfb
	|* Transfer (Issue)
	var.whwmd300.trto = tdilc001.trto
	var.whwmd300.btri = tdilc001.trtb
	|* Assembly
	if tdilc001.itlo = tcyesno.no then			|#149381.sn
		var.whwmd300.aslo = tcyesno.no
	else						|#149381.en
		var.whwmd300.aslo = tcyesno.yes
	endif						|#149381.n
	var.whwmd300.bass = tcyesno.no
}
:FUNC:END
:TEXT:

:TEXT:END
