CREATE PROCEDURE InsertLineItem
 @InvoiceNum Int,
 @Linenum Int,
 @Date DateTime,
 @Quantity Float,
 @Code VarChar(50),
 @Itemdesc NVarChar(50),
 @Size NVarChar(50),
 @Price Float,
 @LineTotal Float,
 @LineDisc Float,
 @InvDisc Float,
 @Tag NVarChar(5),
 @Mod Float,
 @DiscSeq Int,
 @DiscCode Int, 
 @ParentLineNum Int, 
 @fkPicklistsID Int,
 @GroupID Int,
 @Status VarChar(10),
 @Grouping Int,
 @PreVoid Float
AS
 DECLARE @LineitemID INT

--RC Updated 04/04/07 To Line Line Total UnitCost
DECLARE @LineCostTotal FLOAT
SET @LineCostTotal = (SELECT PRICES.UNITCOST * @Quantity FROM PRICES WHERE PRICES.CODE = @CODE)
If @LineCostTotal = NULL SET @LineCostTotal = 0.0

 -- Check for the blank strings and set them to NULL if found
 If @Code = '' SET @Code = NULL
 If @ItemDesc = '' SET @ItemDesc = NULL
 If @Size = '' SET @Size = NULL
 If @Tag = '' SET @Tag = NULL
 If @Status = '' SET @Status = NULL


-- Insert the lineitem record 
 INSERT INTO LineItem 
  (InvoiceNum, Linenum,Quantity,[Date],Code,Itemdesc,[Size],Price,
   LineTotal,LineDisc,InvDisc,Tag,Mod,DiscSeq,DiscCode,ParentLineNum,fkPicklistsID,GroupID,Status,Grouping,PreVoid,LineCostTotal)
  VALUES(@InvoiceNum, @Linenum,@Quantity,@Date,@Code,@Itemdesc,@Size,
         @Price,@LineTotal,@LineDisc,@InvDisc,@Tag,@Mod,@DiscSeq,@DiscCode,@ParentLineNum,@fkPicklistsID,@GroupID,@Status,@Grouping,@PreVoid,@LineCostTotal)
  SELECT @LineitemID = @@IDENTITY

-- Now check what category type the lineitem is. If a giveaway or promotion and a customer is assigned to the invoice then update the Customer_Promotions table or the 
--Customer_Giveaways table.
-- FIRST check that a customer has been assigned to this invoice. If not then no point in storing the giveaway/promotion
IF (SELECT CustLink FROM Invoices WHERE InvoiceNum = @InvoiceNum) IS NULL
	RETURN

DECLARE @CategoryType INT

SET @CategoryType = (SELECT CategoryType FROM CatMenu
JOIN Prices
ON Prices.CatMenuCode = CatMenu.CatMenuCode
WHERE Prices.Code = @Code)

IF @CategoryType = 0
	RETURN
ELSE IF @CategoryType = 1 -- Promotion
	INSERT INTO Customer_Promotions (fkLineitemID) VALUES(@LineitemID)
ELSE IF @CategoryType = 2 -- Giveaway
	INSERT INTO Customer_Giveaways (fkLineitemID) VALUES(@LineitemID)
