/* ----------------------------------------------------------------------------------------------------------------
*	
Function:	LaborCost

Functionality:
Calculates Labor Cost based on hourly rate, time of labor and quantity.

Deployment:
The script can be deployed on Sales Order.

Event triggering the script:
	1) validateLine.
	
Custom Fields:
+---------------------------------------------------------------------------------------------------------------------------------------+
| S. No.|	Id					|	Type			|	Level	|		Description							
+---------------------------------------------------------------------------------------------------------------------------------------+
| (1)	|	_hourlyrate     	|	Decimal			|	Body	|	    It holds the Hourly Rate.										|
| (2)	|	_includelabor     	|	Check Box		|	ItemLine|	    Checks if the item needs to included in Labor Cost Calculation.	|
| (3)	|	_timeoflabor     	|	Decimal			|	ItemLine|	    It holds the time of Labor.										|


Create above custom fields with their id and type as mentioned in table.
*------------------------------------------------------------------------------------------------------------------
*/

// Global Variables
{
	debugValidateLine			= false;
	debugLaborCost				= false;
}
// End - Global Variables

/* -----------------------------------------------------------
 *	Function:	validateLine
 *	Description:
 *		This function is fired when the user hits Add/Done
 *		button on the item line
 * -----------------------------------------------------------
 */

function validateLine ( type )
{
	if ( debugValidateLine ) alert ( "[DEBUG - validateLine] Entering checks on the line item" ) ;

	if ( type == 'item')
	{		
		var currentItem = nlapiGetCurrentLineItemText ( 'item', 'item' ) ;
		
		if(currentItem == 'LABOR')
		{
			var totalLaborCost = LaborCost();
			nlapiSetCurrentLineItemValue ( 'item', 'rate', totalLaborCost) ;
		}
	}

	if ( debugValidateLine ) alert ( "[DEBUG - validateLine] Exiting the validateLine function" ) ;

	return true ;
}


function LaborCost()
{
	if ( debugLaborCost) alert ( "[DEBUG - debugLaborCost] Starting total Labor Cost calculation." ) ;

	var totalLaborCost = 0;
	var LaborCost = 0;
	
	var hourlyRate = nlapiGetFieldValue ( 'custbody_hourlyrate') ;

	if ( debugLaborCost ) alert ( "[DEBUG - debugLaborCost] hourlyRate: " + hourlyRate ) ;

	var itemCount  = nlapiGetLineItemCount ( 'item' ) ;
	var cItemIndex	= nlapiGetCurrentLineItemIndex('item');
	
	if ( debugLaborCost ) alert ( "[DEBUG - debugLaborCost] Item Line count: " + itemCount ) ;
	if ( debugLaborCost ) alert ( "[DEBUG - debugLaborCost] Current Line Index: " + cItemIndex ) ;

	if ( debugLaborCost && itemCount > 0) alert ( "[DEBUG - debugLaborCost] Looping through the item line" ) ;
	
	for ( ctr = 1 ; ctr <= itemCount ; ctr ++ )
	{
		if(cItemIndex == ctr) 
		{
			if ( debugLaborCost ) alert ( "[DEBUG - debugLaborCost] Current Line in edit mode.Skipping" ) ;
			continue;
		}
		
		var currentItem = nlapiGetLineItemText ( 'item', 'item', ctr) ;
		var includeLabor = nlapiGetLineItemValue ( 'item', 'custcol_includelabor', ctr);
		
		if(currentItem != 'LABOR' && includeLabor == 'T')
		{
			var quantity = parseInt(nlapiGetLineItemValue ( 'item', 'quantity', ctr)) ;
			var timeOfLabor = nlapiGetLineItemValue ( 'item', 'custcol_timeoflabor', ctr) ;

			if ( quantity == null || quantity.length == 0 || isNaN ( quantity ) )
			{
				quantity = 0 ;
			}

			if ( timeOfLabor == null || timeOfLabor.length == 0 || isNaN ( timeOfLabor ) )
			{
				timeOfLabor = 0 ;
			}
			if ( debugLaborCost ) alert ( "[DEBUG - debugLaborCost] quantity of Item #" +
											ctr +' = ' + quantity +"\n timeOfLabor of Item #" + 
											ctr + ' = '+timeOfLabor);

			laborCost = quantity*timeOfLabor*hourlyRate;
			
			if ( debugLaborCost ) alert ( "[DEBUG - debugLaborCost] Inside Loop \n laborCost"  +
											' = ' + laborCost);
			
			totalLaborCost += laborCost;
			if ( debugLaborCost ) alert ( "[DEBUG - debugLaborCost] Inside Loop \n totalLaborCost"  +
											' = ' + totalLaborCost );
		}
	}

	return totalLaborCost;
}