<MvCOMMENT>
|
| Miva Merchant v4.x
|
| This file and the source codes contained herein are the property 
| of Miva Corporation.  Use of this file is restricted to the specific 
| terms and conditions in the License Agreement associated with this 
| file.  Distribution of this file or portions of this file for uses
| not covered by the License Agreement is not allowed without a written 
| agreement signed by an officer of Miva Corporation.
| 
| Copyright 1998-2001 Miva Corporation.  All rights reserved.
| http://www.miva.com/
|
| dBase III Database Layer
| $Source: /home/cvs/merchant/Merchant/features/usl/usl_db.mv,v $
|
| Prefix         : MER-USL-DB-
| Next Error Code: 0    
| Next Text ID   : 8    
|
</MvCOMMENT>

<MvCOMMENT>
|
| Upsell/UpsellXProduct
|
</MvCOMMENT>

<MvFUNCTION NAME = "USL_Store_Create" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = 1>
	<MvCREATE NAME = "Upsell"
			  DATABASE = "{ g._DB_Store_Directory $ 'upsell.dbf' }"
			  FIELDS = "product_id	NUMBER,
						req_percnt	NUMBER( 10.2 ),
						percent		BOOL,
						price		NUMBER( 10.2 ),
						score		NUMBER">
	<MvIF EXPR = "{ MvCREATE_Error }">
		<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00175', MvCREATE_Error ) }">
	<MvELSE>
		<MvASSIGN NAME = "g._DB_Store_Upsell_Open" VALUE = 1>
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvMAKEINDEX NAME = "Upsell"
					 INDEXFILE = "{ g._DB_Store_Directory $ 'us_prod.mvx' }"
					 EXPRESSION = "{ Upsell.d.product_id }"
					 FLAGS = "nounique, ascending">
		<MvIF EXPR = "{ MvMAKEINDEX_Error }">
			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00176', MvMAKEINDEX_Error ) }">
		</MvIF>
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvCLOSE NAME = "Upsell">
		<MvASSIGN NAME = "g._DB_Store_Upsell_Open" VALUE = 0>
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvCREATE NAME = "UpsellXProduct"
				  DATABASE = "{ g._DB_Store_Directory $ 'upslprod.dbf' }"
				  FIELDS = "product_id	NUMBER,
							req_prod	NUMBER">
		<MvIF EXPR = "{ MvCREATE_Error }">
			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00177', MvCREATE_Error ) }">
		<MvELSE>
			<MvASSIGN NAME = "g._DB_Store_UpsellXProduct_Open" VALUE = 1>
		</MvIF>

		<MvIF EXPR = "{ l.ok }">
			<MvMAKEINDEX NAME = "UpsellXProduct"
						 INDEXFILE = "{ g._DB_Store_Directory $ 'usp_prod.mvx' }"
						 EXPRESSION = "{ UpsellXProduct.d.product_id $ ':' $ padl( UpsellXProduct.d.req_prod, 10, '0' ) }"
						 FLAGS = "nounique, ascending">
			<MvIF EXPR = "{ MvMAKEINDEX_Error }">
				<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00178', MvMAKEINDEX_Error ) }">
			</MvIF>
		</MvIF>

		<MvIF EXPR = "{ l.ok }">
			<MvMAKEINDEX NAME = "UpsellXProduct"
						 INDEXFILE = "{ g._DB_Store_Directory $ 'usp_req.mvx' }"
						 EXPRESSION = "{ UpsellXProduct.d.req_prod $ ':' $ padl( UpsellXProduct.d.product_id, 10, '0' ) }"
						 FLAGS = "nounique, ascending">
			<MvIF EXPR = "{ MvMAKEINDEX_Error }">
				<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00380', MvMAKEINDEX_Error ) }">
			</MvIF>
		</MvIF>

		<MvIF EXPR = "{ l.ok }">
			<MvCLOSE NAME = "UpsellXProduct">
			<MvASSIGN NAME = "g._DB_Store_UpsellXProduct_Open" VALUE = 0>
		</MvIF>
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvCREATE NAME = "UpsellOptions"
				  DATABASE = "{ g._DB_Store_Directory $ 'upslopts.dbf' }"
				  FIELDS = "prod_shown	NUMBER,
							prod_sell	NUMBER">
		<MvIF EXPR = "{ MvCREATE_Error }">
			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00381', MvCREATE_Error ) }">
		<MvELSE>
			<MvASSIGN NAME = "g._DB_Store_UpsellOptions_Open" VALUE = 1>

			<MvASSIGN NAME = "UpsellOptions.d.prod_shown" VALUE = "1">
			<MvASSIGN NAME = "UpsellOptions.d.prod_sell"  VALUE = "1">

			<MvADD NAME = "UpsellOptions">
		</MvIF>

		<MvIF EXPR = "{ l.ok }">
			<MvCLOSE NAME = "UpsellOptions">
			<MvASSIGN NAME = "g._DB_Store_UpsellOptions_Open" VALUE = 0>
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "USL_Store_Delete" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.directory" VALUE = "{ g.MerchantPath $ padl( Stores.d.id, 8, '0' ) $ '/' }">

	<MvASSIGN NAME = "l.deleted" VALUE = "{ fdelete( l.directory $ 'upsell.dbf' ) }">
	<MvASSIGN NAME = "l.deleted" VALUE = "{ fdelete( l.directory $ 'us_prod.mvx' ) }">
	<MvASSIGN NAME = "l.deleted" VALUE = "{ fdelete( l.directory $ 'upslprod.dbf' ) }">
	<MvASSIGN NAME = "l.deleted" VALUE = "{ fdelete( l.directory $ 'usp_prod.mvx' ) }">
	<MvASSIGN NAME = "l.deleted" VALUE = "{ fdelete( l.directory $ 'upslopts.dbf' ) }">
	<MvASSIGN NAME = "l.deleted" VALUE = "{ fdelete( l.directory $ 'usp_req.mvx' ) }">

	<MvFUNCTIONRETURN VALUE = 1>
</MvFUNCTION>

<MvFUNCTION NAME = "USL_Store_Pack" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = 1>
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_Upsell() }">
	<MvIF EXPR = "{ l.ok }">
		<MvPACK NAME = "Upsell">
		<MvIF EXPR = "{ MvPACK_Error }">
			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00248', MvPACK_Error ) }">
		<MvELSE>
			<MvCLOSE NAME = "Upsell">
			<MvASSIGN NAME = "g._DB_Store_Upsell_Open" VALUE = 0>
		</MvIF>
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellXProduct() }">
		<MvIF EXPR = "{ l.ok }">
			<MvPACK NAME = "UpsellXProduct">
			<MvIF EXPR = "{ MvPACK_Error }">
				<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00249', MvPACK_Error ) }">
			<MvELSE>
				<MvCLOSE NAME = "UpsellXProduct">
				<MvASSIGN NAME = "g._DB_Store_UpsellXProduct_Open" VALUE = 0>
			</MvIF>
		</MvIF>
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellOptions() }">
		<MvIF EXPR = "{ l.ok }">
			<MvPACK NAME = "UpsellOptions">
			<MvIF EXPR = "{ MvPACK_Error }">
				<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00392', MvPACK_Error ) }">
			<MvELSE>
				<MvCLOSE NAME = "UpsellOptions">
				<MvASSIGN NAME = "g._DB_Store_UpsellOptions_Open" VALUE = 0>
			</MvIF>
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "USL_Create_Data_Files" STANDARDOUTPUTLEVEL = "">
	<MvLOCALIZED NAME = "l.privilege" ID = "MER-USL-DB-00000">
		<MvLOCALIZED-TEXT LANGUAGE = "en-US">Upsold Products</MvLOCALIZED-TEXT>
	</MvLOCALIZED>
	<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.ok" VALUE = "{ Privilege_Insert( 'UPSL', l.privilege ) }">

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "USL_Store_Close" STANDARDOUTPUTLEVEL = "">
	<MvIF EXPR = "{ g._DB_Store_Upsell_Open }">
		<MvCLOSE NAME = "Upsell">
		<MvASSIGN NAME = "g._DB_Store_Upsell_Open" VALUE = 0>
	</MvIF>

	<MvIF EXPR = "{ g._DB_Store_UpsellXProduct_Open }">
		<MvCLOSE NAME = "UpsellXProduct">
		<MvASSIGN NAME = "g._DB_Store_UpsellXProduct_Open" VALUE = 0>
	</MvIF>

	<MvIF EXPR = "{ g._DB_Store_UpsellOptions_Open }">
		<MvCLOSE NAME = "UpsellOptions">
		<MvASSIGN NAME = "g._DB_Store_UpsellOptions_Open" VALUE = 0>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = 1>
</MvFUNCTION>

<MvFUNCTION NAME = "_Upsell_Filter" PARAMETERS = "search" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.word" VALUE = 1>
	<MvASSIGN NAME = "g._upsell_word" INDEX = "{ l.word }" VALUE = "{ gettoken( l.search, ' ', l.word ) }">
	<MvWHILE EXPR = "{ g._upsell_word[ l.word ] }">
		<MvASSIGN NAME = "l.word" VALUE = "{ l.word + 1 }">
		<MvASSIGN NAME = "g._upsell_word" INDEX = "{ l.word }" VALUE = "{ gettoken( l.search, ' ', l.word ) }">
	</MvWHILE>																		   

	<MvASSIGN NAME = "l.filter" VALUE = "( NOT Upsell.d.deleted )">
	<MvASSIGN NAME = "l.pos" VALUE = 1>
	<MvWHILE EXPR = "{ l.pos LE ( l.word - 1 ) }">
		<MvASSIGN NAME = "l.filter" VALUE = "{ l.filter $ ' AND ( 
		( g._upsell_word[' $ l.pos $ '] CIN Upsell.d.req_percnt ) OR 																  ( g._upsell_word[' $ l.pos $ '] CIN Upsell.d.price ) )' }">
		<MvASSIGN NAME = "l.pos" VALUE = "{ l.pos + 1 }">
	</MvWHILE>

	<MvFUNCTIONRETURN VALUE = "{ l.filter }">
</MvFUNCTION>

<MvFUNCTION NAME = "_DB_Store_Open_Upsell" STANDARDOUTPUTLEVEL = "" ERROROUTPUTLEVEL = "syntax, expression">
	<MvASSIGN NAME = "l.ok" VALUE = 1>

	<MvIF EXPR = "{ NOT g._DB_Store_Upsell_Open }">
		<MvOPEN NAME = "Upsell"
				DATABASE = "{ g._DB_Store_Directory $ 'upsell.dbf' }"
				INDEXES = "{ g._DB_Store_Directory $ 'us_prod.mvx' }">
		<MvIF EXPR = "{ MvOPEN_Error }">
			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00086', MvOPEN_Error ) }">
		<MvELSE>
			<MvASSIGN NAME = "g._DB_Store_Upsell_Open" VALUE = 1>
			<MvFILTER NAME = "Upsell" FILTER = "{ NOT Upsell.d.deleted }">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "_DB_Store_Open_UpsellXProduct" STANDARDOUTPUTLEVEL = "" ERROROUTPUTLEVEL = "syntax, expression">
	<MvASSIGN NAME = "l.ok" VALUE = 1>

	<MvIF EXPR = "{ NOT g._DB_Store_UpsellXProduct_Open }">
		<MvOPEN NAME = "UpsellXProduct"
				DATABASE = "{ g._DB_Store_Directory $ 'upslprod.dbf' }"
				INDEXES = "{ g._DB_Store_Directory $ 'usp_prod.mvx, ' $ g._DB_Store_Directory $ 'usp_req.mvx' }">
		<MvIF EXPR = "{ MvOPEN_Error }">
			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00087', MvOPEN_Error ) }">
		<MvELSE>
			<MvASSIGN NAME = "g._DB_Store_UpsellXProduct_Open" VALUE = 1>
			<MvFILTER NAME = "UpsellXProduct" FILTER = "{ NOT UpsellXProduct.d.deleted }">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Insert" PARAMETERS = "product_id, req_percnt, percent, price" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_Upsell() }">
	<MvIF EXPR = "{ l.ok }">
		<MvASSIGN NAME = "Upsell.d.product_id" VALUE = "{ l.product_id }">
		<MvASSIGN NAME = "Upsell.d.req_percnt" VALUE = "{ l.req_percnt }">
		<MvASSIGN NAME = "Upsell.d.percent" VALUE = "{ l.percent }">
		<MvASSIGN NAME = "Upsell.d.price" VALUE = "{ l.price }">
		<MvASSIGN NAME = "Upsell.d.score" VALUE = 0>

		<MvADD NAME = "Upsell">
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Update" PARAMETERS = "product_id, req_percnt, percent, price" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_Upsell() }">
	<MvIF EXPR = "{ l.ok }">
		<MvPRIMARY NAME = "Upsell" INDEX = "{ ( g._DB_Store_Directory $ 'us_prod.mvx' ) }">
		<MvFIND NAME = "Upsell" VALUE = "{ l.product_id }" EXACT>
		<MvIF EXPR = "{ Upsell.d.EOF }">
			<MvLOCALIZED NAME = "l.error" ID = "MER-USL-DB-00001">
				<MvLOCALIZED-TOKEN NAME = "product_id" VALUE = "{ encodeentities( l.product_id ) }">
				<MvLOCALIZED-TEXT LANGUAGE = "en-US">Product '%product_id%' is not configured for upsale</MvLOCALIZED-TEXT>
			</MvLOCALIZED>

			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00088', l.error ) }">
		<MvELSE>
			<MvASSIGN NAME = "Upsell.d.req_percnt" VALUE = "{ l.req_percnt }">
			<MvASSIGN NAME = "Upsell.d.percent" VALUE = "{ l.percent }">
			<MvASSIGN NAME = "Upsell.d.price" VALUE = "{ l.price }">

			<MvUPDATE NAME = "Upsell">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Increment_Score" PARAMETERS = "product_id" STANDARDOUTPUTLEVEL = ""> 
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_Upsell() }">
	<MvIF EXPR = "{ l.ok }">
		<MvPRIMARY NAME = "Upsell" INDEX = "{ ( g._DB_Store_Directory $ 'us_prod.mvx' ) }">
		<MvFIND NAME = "Upsell" VALUE = "{ l.product_id }" EXACT>
		<MvIF EXPR = "{ Upsell.d.EOF }">
			<MvLOCALIZED NAME = "l.error" ID = "MER-USL-DB-00002">
				<MvLOCALIZED-TOKEN NAME = "product_id" VALUE = "{ encodeentities( l.product_id ) }">
				<MvLOCALIZED-TEXT LANGUAGE = "en-US">Product '%product_id%' is not configured for upsale</MvLOCALIZED-TEXT>
			</MvLOCALIZED>

			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00089', l.error ) }">
		<MvELSE>
			<MvIF EXPR = "{ Upsell.d.score LE 999999 }">
				<MvASSIGN NAME = "Upsell.d.score" VALUE = "{ Upsell.d.score + 1 }">
			<MvELSE>
				<MvASSIGN NAME = "Upsell.d.score" VALUE = 1>
			</MvIF>

			<MvUPDATE NAME = "Upsell">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Delete" PARAMETERS = "product_id" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_Upsell() }">
	<MvIF EXPR = "{ l.ok }">
		<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellXProduct() }">
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvPRIMARY NAME = "Upsell" INDEX = "{ ( g._DB_Store_Directory $ 'us_prod.mvx' ) }">
		<MvFIND NAME = "Upsell" VALUE = "{ l.product_id }" EXACT>
		<MvIF EXPR = "{ NOT Upsell.d.EOF }">
			<MvFIND NAME = "UpsellXProduct" VALUE = "{ l.product_id $ ':' }">
			<MvWHILE EXPR = "{ ( NOT UpsellXProduct.d.EOF ) AND ( UpsellXProduct.d.product_id EQ l.product_id ) }">
				<MvDELETE NAME = "UpsellXProduct">
				<MvSKIP NAME = "UpsellXProduct" ROWS = 1>
			</MvWHILE>

			<MvDELETE NAME = "Upsell">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Find_Product_ID" PARAMETERS = "product_id" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvIF EXPR = "{ _DB_Store_Open_Upsell() }">
		<MvPRIMARY NAME = "Upsell" INDEX = "{ ( g._DB_Store_Directory $ 'us_prod.mvx' ) }">
		<MvFIND NAME = "Upsell" VALUE = "{ l.product_id }" EXACT>
		<MvIF EXPR = "{ NOT Upsell.d.EOF }">
			<MvASSIGN NAME = "l.found" VALUE = 1>
		<MvELSE>
			<MvASSIGN NAME = "g.Upsell_Find_Product_ID" VALUE = "{ 'Product \'' $ l.product_id $ '\' is not configured for upsale' }">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.found }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_FindFirst" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvIF EXPR = "{ _DB_Store_Open_Upsell() }">
		<MvGO NAME = "Upsell" ROW = "top">
		<MvIF EXPR = "{ NOT Upsell.d.EOF }">
			<MvASSIGN NAME = "l.found" VALUE = 1>
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.found }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_FindNext" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 1>

	<MvSKIP NAME = "Upsell" ROWS = 1>
	<MvIF EXPR = "{ Upsell.d.EOF }">
		<MvASSIGN NAME = "l.found" VALUE = 0>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.found }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Delete_UpsellProductInBasket" STANDARDOUTPUTLEVEL = "">
	<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.ok" VALUE = "{ _DB_Store_Open_Baskets() }">

	<MvIF EXPR = "{ l.ok }">
		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.ok" VALUE = "{ _DB_Store_Open_BasketList() }">
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvPRIMARY NAME = "Baskets" INDEX = "{ g._DB_Store_Directory $ 'bsk_id.mvx' }">
	</MvIF>

	<MvFIND NAME = "Baskets" VALUE = "{ BasketList.d.basket_id $ ':' }">
	<MvWHILE EXPR = "{ ( NOT Baskets.d.EOF ) AND ( Baskets.d.id EQ BasketList.d.basket_id ) }">
		<MvIF EXPR = "{ Baskets.d.upsold }">
			<MvDELETE NAME = "Baskets">
		</MvIF>

		<MvSKIP NAME = "Baskets" ROWS = 1>
	</MvWHILE>
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Find_Eligible_Product" PARAMETERS = "session_id" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.eligible_score" VALUE = 0>
	<MvASSIGN NAME = "l.eligible_product" VALUE = 0>
	<MvASSIGN NAME = "l.eligible_price" VALUE = 0>
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_Upsell() }">

	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellOptions() }">
	
	<MvASSIGN NAME = "l.eligible_max" VALUE = "{ UpsellOptions.d.prod_shown }">
	<MvIF EXPR = "{ l.ok }">
		<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellXProduct() }">
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.ok" VALUE = "{ _DB_Store_Open_Products() }">
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.ok" VALUE = "{ _DB_Store_Open_BasketList() }">
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.ok" VALUE = "{ _DB_Store_Open_Baskets() }">
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvDO FILE = "{ g.Module_Feature_AGR_DB }" NAME = "l.ok" VALUE = "{ _DB_Store_Open_AvailabilityGroupXCustomer() }">
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvDO FILE = "{ g.Module_Feature_AGR_DB }" NAME = "l.ok" VALUE = "{ _DB_Store_Open_AvailabilityGroupXProduct() }">
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.total" VALUE = "{ Basket_Total( l.session_id ) }">

		<MvPRIMARY NAME = "Products" INDEX = "{ g._DB_Store_Directory $ 'prd_id.mvx' }">
		<MvPRIMARY NAME = "Baskets" INDEX = "{ g._DB_Store_Directory $ 'bsk_id.mvx' }">
		<MvPRIMARY NAME = "BasketList" INDEX = "{ g._DB_Store_Directory $ 'blst_ses.mvx' }">
		<MvPRIMARY NAME = "AvailabilityGroupXCustomer" INDEX = "{ g._DB_Store_Directory $ 'agxct_ct.mvx' }">
		<MvPRIMARY NAME = "AvailabilityGroupXProduct" INDEX = "{ g._DB_Store_Directory $ 'agxp_grp.mvx' }">

		<MvFIND NAME = "BasketList" VALUE = "{ l.session_id }" EXACT>
		<MvIF EXPR = "{ NOT BasketList.d.EOF }">
			<MvASSIGN NAME = "l.eligible" VALUE = 1>
		
			<MvFIND NAME = "Baskets" VALUE = "{ BasketList.d.basket_id $ ':' }">
			<MvWHILE EXPR = "{ ( NOT Baskets.d.EOF ) AND ( Baskets.d.id EQ BasketList.d.basket_id ) }">
				<MvIF EXPR = "{ Baskets.d.upsold }">
					<MvASSIGN NAME = "l.eligible" VALUE = 0>
					<MvWHILESTOP>
				</MvIF>

				<MvSKIP NAME = "Baskets" ROWS = 1>
			</MvWHILE>
		
			<MvIF EXPR = "{ l.eligible }">
				<MvGO NAME = "Upsell" ROW = "top">
				<MvWHILE EXPR = "{ l.ok AND ( NOT Upsell.d.EOF ) }">
					<MvASSIGN NAME = "l.eligible" VALUE = 1>

					<MvFIND NAME = "Products" VALUE = "{ Upsell.d.product_id }" EXACT>

					<MvIF EXPR = "{ Products.d.EOF }">
						<MvLOCALIZED NAME = "l.error" ID = "MER-DTB-00080">
							<MvLOCALIZED-TOKEN NAME = "product_id" VALUE = "{ encodeentities( Upsell.d.product_id ) }">
							<MvLOCALIZED-TEXT LANGUAGE = "en-US">Product '%product_id%' not found</MvLOCALIZED-TEXT>
						</MvLOCALIZED>

						<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00091', l.error ) }">
					</MvIF>
					
					<MvASSIGN NAME = "l.inventory_available" VALUE = "1">
					<MvIF EXPR = "{ g.Features:Inventory:Enabled }">
						<MvDO FILE = "{ g.Module_Feature_INV_RT }" NAME = "l.track_inventory" VALUE = "{ INV_TrackOut( Products.d.id ) }">
						<MvIF EXPR = "{ l.track_inventory }">
		
							<MvDO FILE = "{ g.Module_Feature_INV_RT }" NAME = "l.quantity_available" VALUE = "{ INV_Quantity_Available( Products.d.id ) }">
							<MvIF EXPR = "{ len( l.quantity_available ) AND (l.quantity_available LE 0 ) }">
								<MvASSIGN NAME = "l.inventory_available" VALUE = "0">
							</MvIF>
						</MvIF>
					</MvIF>

					<MvIF EXPR = "{ l.ok AND l.inventory_available }">
						<MvIF EXPR = "{ Upsell.d.percent }">
							<MvASSIGN NAME = "l.price" VALUE = "{ Products.d.price * ( Upsell.d.price / 100 ) }">
						<MvELSE>
							<MvASSIGN NAME = "l.price" VALUE = "{ Upsell.d.price }">
						</MvIF>

						<MvIF EXPR = "{ Products.d.agrpcount }">
							<MvASSIGN NAME = "l.eligible" VALUE = 0>
							<MvIF EXPR = "{ BasketList.d.cust_id }">
								<MvFIND NAME = "AvailabilityGroupXCustomer" VALUE = "{ BasketList.d.cust_id }" EXACT>
								<MvWHILE EXPR = "{ ( NOT AvailabilityGroupXCustomer.d.EOF ) AND ( AvailabilityGroupXCustomer.d.cust_id EQ BasketList.d.cust_id ) }">
									<MvFIND NAME = "AvailabilityGroupXProduct" VALUE = "{ AvailabilityGroupXCustomer.d.agrp_id $ ':' $ padl( Products.d.id, 10, '0' ) }" EXACT>
									<MvIF EXPR = "{ NOT AvailabilityGroupXProduct.d.EOF }">
										<MvASSIGN NAME = "l.eligible" VALUE = 1>
										<MvWHILESTOP>
									</MvIF>

									<MvSKIP NAME = "AvailabilityGroupXCustomer" ROWS = 1>
								</MvWHILE>
							</MvIF>
						</MvIF>

						<MvIF EXPR = "{ l.eligible }">
							<MvFIND NAME = "Baskets" VALUE = "{ BasketList.d.basket_id $ ':' }">
							<MvWHILE EXPR = "{ ( NOT Baskets.d.EOF ) AND ( Baskets.d.id EQ BasketList.d.basket_id ) }">
								<MvIF EXPR = "{ Baskets.d.product_id EQ Upsell.d.product_id }">
									<MvASSIGN NAME = "l.eligible" VALUE = 0>
									<MvWHILESTOP>
								</MvIF>

								<MvSKIP NAME = "Baskets" ROWS = 1>
							</MvWHILE>
						</MvIF>

						<MvIF EXPR = "{ l.eligible }">
							<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ g._DB_Store_Directory $ 'usp_prod.mvx' }">
							<MvFIND NAME = "UpsellXProduct" VALUE = "{ Upsell.d.product_id $ ':' }">
							<MvIF EXPR = "{ NOT UpsellXProduct.d.EOF }">
								<MvASSIGN NAME = "l.eligible" VALUE = 0>

								<MvWHILE EXPR = "{ ( NOT l.eligible ) AND ( NOT UpsellXProduct.d.EOF ) AND ( UpsellXProduct.d.product_id EQ Upsell.d.product_id ) }">
									<MvFIND NAME = "Baskets" VALUE = "{ BasketList.d.basket_id $ ':' }">
									<MvWHILE EXPR = "{ ( NOT Baskets.d.EOF ) AND ( Baskets.d.id EQ BasketList.d.basket_id ) }">
										<MvIF EXPR = "{ Baskets.d.product_id EQ UpsellXProduct.d.req_prod }">
											<MvASSIGN NAME = "l.eligible" VALUE = 1>
											<MvWHILESTOP>
										</MvIF>

										<MvSKIP NAME = "Baskets" ROWS = 1>
									</MvWHILE>

									<MvSKIP NAME = "UpsellXProduct" ROWS = 1>
								</MvWHILE>
							</MvIF>
						</MvIF>

						<MvIF EXPR = "{ l.eligible }">
							<MvIF EXPR = "{ ( Upsell.d.req_percnt GT 0 ) AND ( ( l.total * ( Upsell.d.req_percnt / 100 ) ) LT l.price ) }">
								<MvASSIGN NAME = "l.eligible" VALUE = 0>
							</MvIF>
						</MvIF>
					<MvELSE>
						<MvASSIGN NAME = "l.eligible" VALUE = 0>
					</MvIF>

					<MvIF EXPR = "{ l.ok }">
						<MvIF EXPR = "{ l.eligible }">
							<MvASSIGN NAME = "l.pos1" VALUE = "{ l.eligible_count }">
							<MvASSIGN NAME = "l.pos2" VALUE = "{ l.pos1 + 1 }">
							<MvWHILE EXPR = "{ l.pos1 GT 0 }">
								<MvIF EXPR = "{ l.eligible_score[ l.pos1 ] GT Upsell.d.score }">
									<MvASSIGN NAME = "l.eligible_score" INDEX = "{ l.pos2 }" VALUE = "{ l.eligible_score[ l.pos1 ] }">
									<MvASSIGN NAME = "l.eligible_product" INDEX = "{ l.pos2 }" VALUE = "{ l.eligible_product[ l.pos1 ] }">
									<MvASSIGN NAME = "l.eligible_price" INDEX = "{ l.pos2 }" VALUE = "{ l.eligible_price[ l.pos1 ] }">
								<MvELSE>
									<MvWHILESTOP>
								</MvIF>

								<MvASSIGN NAME = "l.pos1" VALUE = "{ l.pos1 - 1 }">
								<MvASSIGN NAME = "l.pos2" VALUE = "{ l.pos2 - 1 }">
							</MvWHILE>
							<MvASSIGN NAME = "l.eligible_score" INDEX = "{ l.pos2 }" VALUE = "{ Upsell.d.score }">
							<MvASSIGN NAME = "l.eligible_product" INDEX = "{ l.pos2 }" VALUE = "{ Upsell.d.product_id }">
							<MvASSIGN NAME = "l.eligible_price" INDEX = "{ l.pos2 }" VALUE = "{ Upsell.d.price }">							
							<MvIF EXPR = "{ l.eligible_count LT l.eligible_max }">
								<MvASSIGN NAME = "l.eligible_count" VALUE = "{ l.eligible_count + 1 }">
							</MvIF>
						</MvIF>
					</MvIF>

					<MvSKIP NAME = "Upsell" ROWS = 1>

					<MvFIND NAME = "Products" VALUE = "{ Upsell.d.product_id }" EXACT>
				</MvWHILE>
			</MvIF>
		</MvIF>

		<MvASSIGN NAME = "l.eligible_product_list" VALUE = "">
		<MvASSIGN NAME = "l.pos" VALUE = "1">
		
		<MvWHILE EXPR = "{ ( l.ok ) AND ( l.pos LE l.eligible_count ) }">
			<MvASSIGN NAME = "l.ok" VALUE = "{ Upsell_Increment_Score( l.eligible_product[ l.pos ] ) }">
			<MvIF EXPR = "{ l.ok }">
				<MvIF EXPR = "{ len( l.eligible_product_list ) }">
					<MvASSIGN NAME = "l.eligible_product_list" VALUE = "{ l.eligible_product_list $ ',' $ l.eligible_product[ l.pos ] }">
				<MvELSE>
					<MvASSIGN NAME = "l.eligible_product_list" VALUE = "{ l.eligible_product[ l.pos ] }">
				</MvIF>
			</MvIF>
			<MvASSIGN NAME = "l.pos" VALUE = "{ l.pos + 1 }">
		</MvWHILE>
	
		<MvIF EXPR = "{ NOT( l.ok ) OR ( NOT len( l.eligible_product_list ) ) }">
			<MvASSIGN NAME = "l.eligible_product_list" VALUE = 0>
		</MvIF>
	</MvIF>
	
	<MvFUNCTIONRETURN VALUE = "{ l.eligible_product_list }">
</MvFUNCTION>

<MvFUNCTION NAME = "UpsellXProduct_Insert" PARAMETERS = "product_id, req_prod" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellXProduct() }">
	<MvIF EXPR = "{ l.ok }">
		<MvASSIGN NAME = "UpsellXProduct.d.product_id" VALUE = "{ l.product_id }">
		<MvASSIGN NAME = "UpsellXProduct.d.req_prod" VALUE = "{ l.req_prod }">

		<MvADD NAME = "UpsellXProduct">
	</MvIF>
		
	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "UpsellXProduct_Find" PARAMETERS = "product_id, req_prod" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvIF EXPR = "{ _DB_Store_Open_UpsellXProduct() }">
		<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ g._DB_Store_Directory $ 'usp_prod.mvx' }">
		<MvFIND NAME = "UpsellXProduct" VALUE = "{ l.product_id $ ':' $ padl( l.req_prod, 10, '0' ) }" EXACT>
		<MvIF EXPR = "{ NOT UpsellXProduct.d.EOF }">
			<MvASSIGN NAME = "l.found" VALUE = 1>
		<MvELSE>
			<MvLOCALIZED NAME = "l.error" ID = "MER-USL-DB-00003">
				<MvLOCALIZED-TOKEN NAME = "product_id" VALUE = "{ encodeentities( l.product_id ) }">
				<MvLOCALIZED-TOKEN NAME = "req_prod" VALUE = "{ encodeentities( l.req_prod ) }">

				<MvLOCALIZED-TEXT LANGUAGE = "en-US">Product '%product_id%' has no required product '%req_prod%'</MvLOCALIZED-TEXT>
			</MvLOCALIZED>

			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00092', l.error ) }">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.found }">
</MvFUNCTION>

<MvFUNCTION NAME = "UpsellXProduct_Delete" PARAMETERS = "product_id, req_prod" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellXProduct() }">
	<MvIF EXPR = "{ l.ok }">
		<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ g._DB_Store_Directory $ 'usp_prod.mvx' }">
		<MvFIND NAME = "UpsellXProduct" VALUE = "{ l.product_id $ ':' $ padl( l.req_prod, 10, '0' ) }" EXACT>
		<MvIF EXPR = "{ UpsellXProduct.d.EOF }">
			<MvLOCALIZED NAME = "l.error" ID = "MER-USL-DB-00004">
				<MvLOCALIZED-TOKEN NAME = "product_id" VALUE = "{ encodeentities( l.product_id ) }">
				<MvLOCALIZED-TOKEN NAME = "req_prod" VALUE = "{ encodeentities( l.req_prod ) }">

				<MvLOCALIZED-TEXT LANGUAGE = "en-US">Product '%product_id%' has no required product '%req_prod%'</MvLOCALIZED-TEXT>
			</MvLOCALIZED>

			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00093', l.error ) }">
		<MvELSE>
			<MvDELETE NAME = "UpsellXProduct">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "UpsellXProduct_FindFirst" PARAMETERS = "product_id" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvIF EXPR = "{ _DB_Store_Open_UpsellXProduct() AND _DB_Store_Open_Upsell() }">
		<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ g._DB_Store_Directory $ 'usp_prod.mvx' }">
		<MvFIND NAME = "UpsellXProduct" VALUE = "{ l.product_id $ ':' }">
		<MvIF EXPR = "{ NOT UpsellXProduct.d.EOF }">
			<MvASSIGN NAME = "l.found" VALUE = 1>
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.found }">
</MvFUNCTION>

<MvFUNCTION NAME = "UpsellXProduct_FindNext" PARAMETERS = "product_id" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 1>

	<MvSKIP NAME = "UpsellXProduct" ROWS = 1>
	<MvIF EXPR = "{ ( UpsellXProduct.d.EOF ) OR ( UpsellXProduct.d.product_id NE l.product_id ) }">
		<MvASSIGN NAME = "l.found" VALUE = 0>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.found }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Product_FindOffset_Unassigned" PARAMETERS = "product_id, offset, search" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.offset + 1 }">
	<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.ok" VALUE = "{ _DB_Store_Open_Products() }">
	<MvIF EXPR = "{ l.ok AND _DB_Store_Open_UpsellXProduct() }">
		<MvPRIMARY NAME = "Products" INDEX = "{ g._DB_Store_Directory $ 'prd_ord.mvx' }">

		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.filter" VALUE = "{ _Product_Filter( l.search ) }">
		<MvFILTER NAME = "Products" FILTER = "l.filter" FILTER_TYPE = "variable">

		<MvSKIP NAME = "Products" ROWS = "{ l.offset }">
		<MvWHILE EXPR = "{ ( NOT Products.d.EOF ) AND ( NOT l.found ) }">
			<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ g._DB_Store_Directory $ 'usp_prod.mvx' }">
			<MvFIND NAME = "UpsellXProduct" VALUE = "{ l.product_id $ ':' $ padl( Products.d.id, 10, '0' ) }" EXACT>
			<MvIF EXPR = "{ UpsellXProduct.d.EOF }">
				<MvASSIGN NAME = "l.found" VALUE = 1>
				<MvWHILESTOP>
			</MvIF>

			<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.newoffset + 1 }">
			<MvSKIP NAME = "Products" ROWS = 1>
		</MvWHILE>
	</MvIF>

	<MvIF EXPR = "{ l.found }">
		<MvFUNCTIONRETURN VALUE = "{ l.newoffset }">
	<MvELSE>
		<MvFUNCTIONRETURN VALUE = 0>
	</MvIF>
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Product_FindNext_Unassigned" PARAMETERS = "product_id, offset, search" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.offset + 1 }">

	<MvSKIP NAME = "Products" ROWS = 1>
	<MvWHILE EXPR = "{ ( NOT Products.d.EOF ) AND ( NOT l.found ) }">
		<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ g._DB_Store_Directory $ 'usp_prod.mvx' }">
		<MvFIND NAME = "UpsellXProduct" VALUE = "{ l.product_id $ ':' $ padl( Products.d.id, 10, '0' ) }" EXACT>
		<MvIF EXPR = "{ UpsellXProduct.d.EOF }">
			<MvASSIGN NAME = "l.found" VALUE = 1>
			<MvWHILESTOP>
		</MvIF>

		<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.newoffset + 1 }">
		<MvSKIP NAME = "Products" ROWS = 1>
	</MvWHILE>

	<MvIF EXPR = "{ l.found }">
		<MvFUNCTIONRETURN VALUE = "{ l.newoffset }">
	<MvELSE>
		<MvFILTER NAME = "Products" FILTER = "{ NOT Products.d.deleted }">
		<MvFUNCTIONRETURN VALUE = 0>
	</MvIF>
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Product_FindOffset_Assigned" PARAMETERS = "product_id, offset, search" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.offset + 1 }">
	<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.ok" VALUE = "{ _DB_Store_Open_Products() }">

	<MvIF EXPR = "{ l.ok AND _DB_Store_Open_UpsellXProduct() }">
		<MvPRIMARY NAME = "Products" INDEX = "{ g._DB_Store_Directory $ 'prd_id.mvx' }">

		<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.filter" VALUE = "{ _Product_Filter( l.search ) }">
		<MvFILTER NAME = "Products" FILTER = "l.filter" FILTER_TYPE = "variable">

		<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ g._DB_Store_Directory $ 'usp_prod.mvx' }">
		<MvFIND NAME = "UpsellXProduct" VALUE = "{ l.product_id $ ':' }">
		<MvSKIP NAME = "UpsellXProduct" ROWS = "{ l.offset }">

		<MvWHILE EXPR = "{ ( NOT UpsellXProduct.d.EOF ) AND ( UpsellXProduct.d.product_id EQ l.product_id ) AND ( NOT l.found ) }">
			<MvFIND NAME = "Products" VALUE = "{ UpsellXProduct.d.req_prod }" EXACT>
			<MvIF EXPR = "{ NOT Products.d.EOF }">
				<MvASSIGN NAME = "l.found" VALUE = 1>
				<MvWHILESTOP>
			</MvIF>

			<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.newoffset + 1 }">
			<MvSKIP NAME = "UpsellXProduct" ROWS = 1>
		</MvWHILE>
	</MvIF>

	<MvIF EXPR = "{ l.found }">
		<MvFUNCTIONRETURN VALUE = "{ l.newoffset }">
	<MvELSE>
		<MvFUNCTIONRETURN VALUE = 0>
	</MvIF>
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Product_FindNext_Assigned" PARAMETERS = "product_id, offset, search" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.offset + 1 }">

	<MvSKIP NAME = "UpsellXProduct" ROWS = 1>

	<MvWHILE EXPR = "{ ( NOT UpsellXProduct.d.EOF ) AND ( UpsellXProduct.d.product_id EQ l.product_id ) AND ( NOT l.found ) }">
		<MvFIND NAME = "Products" VALUE = "{ UpsellXProduct.d.req_prod }" EXACT>
		<MvIF EXPR = "{ NOT Products.d.EOF }">
			<MvASSIGN NAME = "l.found" VALUE = 1>
			<MvWHILESTOP>
		</MvIF>

		<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.newoffset + 1 }">
		<MvSKIP NAME = "UpsellXProduct" ROWS = 1>
	</MvWHILE>

	<MvIF EXPR = "{ l.found }">
		<MvFUNCTIONRETURN VALUE = "{ l.newoffset }">
	<MvELSE>
		<MvFILTER NAME = "Products" FILTER = "{ NOT Products.d.deleted }">
		<MvFUNCTIONRETURN VALUE = 0>
	</MvIF>
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_FindOffset_All" PARAMETERS = "offset, search" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.offset + 1 }">
	<MvDO FILE = "{ g.Module_Library_DB }" NAME = "l.open_prod" VALUE = "{ _DB_Store_Open_Products() }">

	<MvIF EXPR = "{ l.open_prod AND _DB_Store_Open_Upsell() }">
		<MvPRIMARY NAME = "Upsell" INDEX = "{ g._DB_Store_Directory $ 'us_prod.mvx' }">
		<MvASSIGN NAME = "l.filter" VALUE = "{ _Upsell_Filter( l.search ) }">

		<MvFILTER NAME = "Upsell" FILTER = "l.filter" FILTER_TYPE = "variable">
		<MvSKIP NAME = "Upsell" ROWS = "{ l.offset }">
		<MvIF EXPR = "{ NOT Upsell.d.EOF }">
			<MvASSIGN NAME = "l.found" VALUE = 1>
		</MvIF>
	</MvIF>

	<MvIF EXPR = "{ l.found }">
		<MvFUNCTIONRETURN VALUE = "{ l.newoffset }">
	<MvELSE>
		<MvFILTER NAME = "Upsell" FILTER = "{ ( NOT Upsell.d.deleted ) }">
		<MvFUNCTIONRETURN VALUE = 0>
	</MvIF>
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_FindNext_All" PARAMETERS = "offset, search" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.offset + 1 }">

	<MvSKIP NAME = "Upsell" ROWS = 1>
	<MvIF EXPR = "{ NOT Upsell.d.EOF }">
		<MvFUNCTIONRETURN VALUE = "{ l.newoffset }">
	<MvELSE>
		<MvFILTER NAME = "Upsell" FILTER = "{ ( NOT Upsell.d.deleted ) }">
		<MvFUNCTIONRETURN VALUE = 0>
	</MvIF>
	
</MvFUNCTION>

<MvFUNCTION NAME = "_DB_Store_Open_UpsellOptions" STANDARDOUTPUTLEVEL = "" ERROROUTPUTLEVEL = "syntax, expression">
	<MvASSIGN NAME = "l.ok" VALUE = 1>

	<MvIF EXPR = "{ ( NOT g._DB_Store_UpsellOptions_Open ) }">
		<MvOPEN NAME = "UpsellOptions"
				DATABASE = "{ ( g._DB_Store_Directory $ 'upslopts.dbf' ) }">
		<MvIF EXPR = "{ MvOPEN_Error }">
			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00372', MvOPEN_Error ) }">
		<MvELSE>
			<MvASSIGN NAME = "g._DB_Store_UpsellOptions_Open" VALUE = 1>
			<MvFILTER NAME = "UpsellOptions" FILTER = "{ NOT UpsellOptions.d.deleted }">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "UpsellOptions_Update" PARAMETERS = "prod_shown, prod_sell" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellOptions() }">
	<MvIF EXPR = "{ l.ok }">
		<MvASSIGN NAME = "UpsellOptions.d.prod_shown" VALUE = "{ l.prod_shown }">
		<MvASSIGN NAME = "UpsellOptions.d.prod_sell" VALUE = "{ l.prod_sell }">

		<MvUPDATE NAME = "UpsellOptions">
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "UpsellOptions_Find" PARAMETERS = "" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellOptions() }">

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "UpsellBatchEdit_Update" PARAMETERS = "id, req_percent, percent, price" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_Upsell() }">

	<MvIF EXPR = "{ l.ok }">
		<MvPRIMARY NAME = "Upsell" INDEX = "{ ( g._DB_Store_Directory $ 'us_prod.mvx' ) }">
		<MvFIND NAME = "Upsell" VALUE = "{ l.id }" EXACT>
		<MvIF EXPR = "{ Upsell.d.EOF }">
			<MvLOCALIZED NAME = "l.error" ID = "MER-USL-DB-00005">
				<MvLOCALIZED-TOKEN NAME = "id" VALUE = "{ encodeentities( l.id ) }">
				<MvLOCALIZED-TEXT LANGUAGE = "en-US">Upsell '%id%' not found</MvLOCALIZED-TEXT>
			</MvLOCALIZED>

			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00373', l.error ) }">
		<MvELSE>
			<MvASSIGN NAME = "Upsell.d.req_percnt" VALUE = "{ l.req_percent }">
			<MvASSIGN NAME = "Upsell.d.percent" VALUE = "{ l.percent }">
			<MvASSIGN NAME = "Upsell.d.price" VALUE = "{ l.price }">

			<MvUPDATE NAME = "Upsell">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "UpsellEdit_Delete" PARAMETERS = "id" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_Upsell() }">

	<MvIF EXPR = "{ l.ok }">
		<MvASSIGN NAME = "l.ok" VALUE = "{ _DB_Store_Open_UpsellXProduct() }">
	</MvIF>

	<MvIF EXPR = "{ l.ok }">
		<MvPRIMARY NAME = "Upsell" INDEX = "{ ( g._DB_Store_Directory $ 'us_prod.mvx' ) }">
		<MvFIND NAME = "Upsell" VALUE = "{ l.id }" EXACT>
		<MvIF EXPR = "{ Upsell.d.EOF }">
			<MvLOCALIZED NAME = "l.error" ID = "MER-USL-DB-00006">
				<MvLOCALIZED-TOKEN NAME = "id" VALUE = "{ encodeentities( l.id ) }">
				<MvLOCALIZED-TEXT LANGUAGE = "en-US">Upsale '%id%' not found</MvLOCALIZED-TEXT>
			</MvLOCALIZED>

			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00374', l.error ) }">
		<MvELSE>
			<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ g._DB_Store_Directory $ 'usp_prod.mvx' }">
			<MvFIND NAME = "UpsellXProduct" VALUE = "{ l.id $ ':' }">
			<MvWHILE EXPR = "{ ( NOT UpsellXProduct.d.EOF ) AND ( UpsellXProduct.d.product_id EQ l.id ) }">
				<MvDELETE NAME = "UpsellXProduct">
				<MvSKIP NAME = "UpsellXProduct" ROWS = 1>
			</MvWHILE>

			<MvDELETE NAME = "Upsell">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.ok }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_RequiredProduct_Find" PARAMETERS = "req_prod,product_id" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.found" VALUE = 0>
	<MvIF EXPR = "{ _DB_Store_Open_UpsellXProduct() }">
		<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ g._DB_Store_Directory $ 'usp_req.mvx' }">
		<MvFIND NAME = "UpsellXProduct" VALUE = "{ ( l.req_prod $ ':' $ padl( l.product_id, 10, '0' ) ) }" EXACT>
		<MvIF EXPR = "{ NOT UpsellXProduct.d.EOF }">
			<MvASSIGN NAME = "l.found" VALUE = 1>
		<MvELSE>
			<MvLOCALIZED NAME = "l.error" ID = "MER-USL-DB-00007">
				<MvLOCALIZED-TOKEN NAME = "product_id" VALUE = "{ encodeentities( l.product_id ) }">
				<MvLOCALIZED-TOKEN NAME = "req_prod" VALUE = "{ encodeentities( l.req_prod ) }">

				<MvLOCALIZED-TEXT LANGUAGE = "en-US">Required Product '%req_prod%' has no upsold product '%req_prod%' assigned to it</MvLOCALIZED-TEXT>
			</MvLOCALIZED>

			<MvDO FILE = "{ g.Module_Library_Utilities }" NAME = "l.ok" VALUE = "{ Error( 'MER-DTB-00375', l.error ) }">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.found }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Required_FindFirst" PARAMETERS = "req_prod, offset" STANDARDOUTPUTLEVEL = "">
	<MvASSIGN NAME = "l.newoffset" VALUE = 0>

	<MvIF EXPR = "{ _DB_Store_Open_UpsellXProduct() AND _DB_Store_Open_Upsell() }">

	<MvPRIMARY NAME = "UpsellXProduct" INDEX = "{ ( g._DB_Store_Directory $ 'usp_req.mvx' ) }">
		<MvFIND NAME = "UpsellXProduct" VALUE = "{ ( l.req_prod $ ':' ) }">
		<MvSKIP NAME = "UpsellXProduct" ROWS = "{ l.offset }">

		<MvIF EXPR = "{ ( NOT UpsellXProduct.d.EOF ) AND ( UpsellXProduct.d.req_prod EQ l.req_prod ) }">
			<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.offset + 1 }">
		</MvIF>
	</MvIF>

	<MvFUNCTIONRETURN VALUE = "{ l.newoffset }">
</MvFUNCTION>

<MvFUNCTION NAME = "Upsell_Required_FindNext" PARAMETERS = "req_prod, offset" STANDARDOUTPUTLEVEL = "">
	<MvSKIP NAME = "UpsellXProduct">
	<MvIF EXPR = "{ ( UpsellXProduct.d.EOF ) OR ( UpsellXProduct.d.req_prod NE l.req_prod ) }">
		<MvFUNCTIONRETURN VALUE = 0>
	</MvIF>

	<MvASSIGN NAME = "l.newoffset" VALUE = "{ l.offset + 1 }">
	<MvFUNCTIONRETURN VALUE = "{ l.newoffset }">
</MvFUNCTION>

