declare @Pcount int-- เก็บจำนวน record สินค้าว่ามีกี่รายการ
declare @I int set @I=0 ;-- running number
declare @VolumeCount int -- เก็บจำนวน record ของ productvolume
select @Pcount=COUNT(ItemID) from ICON_EntForms_Products where ServiceType='Sale'
if(@Pcount>0)
begin
while (@I<@Pcount)
begin
set @I=@I+1;
select top 1 @ItemID=ItemID,@ProductNo=ProductNo,@ProductName=ProductName,@GoldPoints=GoldPoints,@PlatinumPoints=PlatinumPoints,@Cost=Cost,@Price=Price,@ProductStyle=ProductStyle,@Balance=Balance,@Unit=Unit from @TempProduct
if (@ProductStyle='Volume')
begin
-- count จำนวน record ของ product volume
select @VolumeCount=COUNT(ID) from ICON_EntForms_Products_Volume where Product_ItemID=@ItemID
-- save record product volume to @TempProductVolume
insert into @TempProductVolume(ID,Product_ItemID,LotNo,Amount,Balance,Cost)
SELECT ID, Product_ItemID, LotNo, Amount, Balance, Cost FROM ICON_EntForms_Products_Volume WHERE (Product_ItemID = @ItemID)
if (@VolumeCount>0)
begin
--จำนวน record ของ product volume
declare @J int set @J=0 ;
while (@J<@VolumeCount)
begin
set @J=@J+1;
-- ข้อมูลสินค้าแต่ละแถวของ product volume
SELECT top 1 @VolumeID= ID,@VolumeProduct_ItemID= Product_ItemID,@VolumeLotNo= LotNo,@VolumeAmount= Amount,@VolumeBalance= Balance,@VolumeCost= Cost FROM @TempProductVolume
-- insert ข้อมูลสินค้าเป็นไอเทมตามจำนวน balance
declare @Temp int set @Temp=0 ;
while (@Temp<@VolumeBalance)
begin
insert into @TempTable(ItemID,ProductVolumeID,ProductNo,ProductName,GoldPoints,PlatinumPoints,Price,Cost,ProductStyle,Balance,Unit,Status,LotNo)
values(@ItemID,@VolumeID,@ProductNo,@ProductName,@GoldPoints,@PlatinumPoints,@Price,@Cost,@ProductStyle,@Balance,@Unit,'Ready',@VolumeLotNo)
delete @TempProductVolume where ID =@VolumeID;
set @Temp=@Temp+1;
end
-- สินค้าที่ถูก adjust
declare @ADCount int set @ADCount=0;
SELECT @ADCount=COUNT( AdjustmentID) FROM POS_AdjustMent WHERE (ProductVoumeID = @VolumeID) AND (IsLock = 1)
insert into @TempProductAdjust(AdjustmentID ,Numb ,AdjustType ,Product_ItemID , LotNo ,ProductVoumeID)
SELECT AdjustmentID, Numb, AdjustType, Product_ItemID, LotNo, ProductVoumeID FROM POS_AdjustMent WHERE (ProductVoumeID = @VolumeID) AND (IsLock = 1)
declare @ADindex int set @ADindex=0;
while (@ADindex<@ADCount)
begin
set @ADindex=@ADindex+1;
select top 1 @AdjustmentID=AdjustmentID,@AdNumb=Numb,@AdjustType=AdjustType,@AdLotNo=LotNo,@AdProduct_ItemID=Product_ItemID,@AdProductVoumeID=ProductVoumeID from @TempProductAdjust
declare @AdTemp int set @AdTemp=0;
while (@AdTemp<@AdNumb)
begin
set @AdTemp=@AdTemp+1;
insert into @TempTable(ItemID,ProductVolumeID,ProductNo,ProductName,GoldPoints,PlatinumPoints,Price,Cost,ProductStyle,Balance,Unit,SalePrice,SaleTotalPrice,SalePoint,FullName,MemberType,SalePercenDiscount,SaleSpecialDiscount,SaleTypeSpecialDiscount,SaleTotalDiscount,SaleDiscount,SaleDay,SaleMonth,SaleYear,Status,LotNo)
values(@ItemID,@VolumeID,@ProductNo,@ProductName,@GoldPoints,@PlatinumPoints,@Price,@Cost,@ProductStyle,@Balance,@Unit,@SalePrice,@SaleTotalPrice,@SalePoint,@FullName,@MemberType,@SalePercenDiscount,@SaleSpecialDiscount,@SaleTypeSpecialDiscount,@SaleTotalDiscount,@SaleDiscount,@SaleDay,@SaleMonth,@SaleYear,@AdjustType,@AdLotNo)
delete @TempProductAdjust where AdjustmentID=@AdjustmentID;
end
end
--สินค้าที่ขายไปแล้ว
declare @SaleCount int set @SaleCount=0;
select @SaleCount=COUNT(SaleID) from POS_Sale where Product_ItemID=@ItemID AND ProductVolumeID=@VolumeID
insert into @SaleTable (ItemID ,ProductVolumeID ,ProductNo ,ProductName ,ProductStyle ,SaleID ,Numb , Unit ,Price ,TotalPrice ,Points ,Cost , BillID ,PercenDiscount ,SpecialDiscount ,TypeSpecialDiscount ,TotalDiscount ,Discount ,FullName ,MemberType ,Day ,Month ,Year,LotNo)
SELECT s.Product_ItemID, s.ProductVolumeID, p.ProductNo, p.ProductName, s.ProductStyle, s.SaleID, s.Numb, p.Unit, s.Price, s.TotalPrice, s.Point, v.Cost, s.BillID,
s.PercenDiscount, s.SpecialDiscount, s.TypeSpecialDiscount, s.TotalDiscount, s.Discount, c.Title + c.FirstName + ' ' + c.LastName AS FullName,
CASE m.MemberType WHEN 1 THEN 'Gold' WHEN 2 THEN 'Platinum' ELSE 'NULL' END AS MemberType, s.Day, s.Month, s.Year, v.LotNo
FROM ICON_EntForms_Products_Volume AS v RIGHT OUTER JOIN
POS_Sale AS s ON v.ID = s.ProductVolumeID LEFT OUTER JOIN
ICON_EntForms_Contacts AS c RIGHT OUTER JOIN
Member AS m ON c.ItemId = m.ItemId ON s.MemberID = m.MemberID LEFT OUTER JOIN
ICON_EntForms_Products AS p ON s.Product_ItemID = p.ItemID
WHERE (p.ServiceType = N'Sale') AND (s.Product_ItemID = @ItemID) AND (s.ProductVolumeID = @VolumeID)
if (@SaleCount>0)
begin
declare @K int set @K=0;
while (@K<@SaleCount)
begin
set @K=@K+1;
select top 1 @ItemID=ItemID,@VolumeID=ProductVolumeID,@ProductNo=ProductNo,@ProductStyle=ProductStyle,@ProductName=ProductName,@SaleID= SaleID ,@numb=Numb ,@Unit=Unit ,@SalePrice=Price ,@SaleTotalPrice=TotalPrice ,@SalePoint= Points ,@Cost=Cost ,@BillID= BillID ,@SalePercenDiscount= PercenDiscount/Numb ,@SaleSpecialDiscount= SpecialDiscount/Numb ,@SaleTypeSpecialDiscount= TypeSpecialDiscount ,@SaleTotalDiscount= TotalDiscount/Numb ,@SaleDiscount=Discount/Numb ,@FullName=FullName ,@MemberType=MemberType ,@SaleDay=Day ,@SaleMonth=Month ,@SaleYear=Year,@SaleLotNo=LotNo from @SaleTable
declare @L int set @L=0;
while (@L<@Numb)
begin
set @L=@L+1;
insert into @TempTable(ItemID,ProductVolumeID,SaleBillID,ProductNo,ProductName,GoldPoints,PlatinumPoints,Price,Cost,ProductStyle,Balance,Unit,SalePrice,SaleTotalPrice,SalePoint,FullName,MemberType,SalePercenDiscount,SaleSpecialDiscount,SaleTypeSpecialDiscount,SaleTotalDiscount,SaleDiscount,SaleDay,SaleMonth,SaleYear,Status,LotNo)
values(@ItemID,@VolumeID,@BillID,@ProductNo,@ProductName,@GoldPoints,@PlatinumPoints,@Price,@Cost,@ProductStyle,@Balance,@Unit,@SalePrice,@SaleTotalPrice,@SalePoint,@FullName,@MemberType,@SalePercenDiscount,@SaleSpecialDiscount,@SaleTypeSpecialDiscount,@SaleTotalDiscount,@SaleDiscount,@SaleDay,@SaleMonth,@SaleYear,'Soled',@SaleLotNo)
end
delete @SaleTable where SaleID=@SaleID;
end
end
delete @TempProductVolume where ID=@VolumeID
end
end
delete @TempProduct where ItemID=@ItemID;
end
-- กรณีที่ไม่ใช้สินค้าประเภท volume
else
begin
if (@Balance>0)
begin
insert into @TempTable(ItemID,ProductNo,ProductName,GoldPoints,PlatinumPoints,Price,Cost,ProductStyle,Balance,Unit,Status)
--SalePrice,SaleTotalPrice,SalePoint,MemberType,SalePercenDiscount,SaleSpecialDiscount,SaleTypeSpecialDiscount,SaleTotalDiscount,SaleDiscount,SaleDay,SaleMonth,SaleYear,
values(@ItemID,@ProductNo,@ProductName,@GoldPoints,@PlatinumPoints,@Price,@Cost,@ProductStyle,@Balance,@Unit,'Ready')
--@SalePrice,@SaleTotalPrice,@SalePoint,@MemberType,@SalePercenDiscount,@SaleSpecialDiscount,@SaleTypeSpecialDiscount,@SaleTotalDiscount,@SaleDiscount,@SaleDay,@SaleMonth,@SaleYear,
delete @TempProduct where ItemID=@ItemID;
end
else
begin
insert into @TempTable(ItemID,ProductNo,ProductName,GoldPoints,PlatinumPoints,Price,Cost,ProductStyle,Balance,Unit,SalePrice,SaleTotalPrice,SalePoint,FullName,MemberType,SalePercenDiscount,SaleSpecialDiscount,SaleTypeSpecialDiscount,SaleTotalDiscount,SaleDiscount,SaleDay,SaleMonth,SaleYear,Status)
values(@ItemID,@ProductNo,@ProductName,@GoldPoints,@PlatinumPoints,@Price,@Cost,@ProductStyle,@Balance,@Unit,@SalePrice,@SaleTotalPrice,@SalePoint,@FullName,@MemberType,@SalePercenDiscount,@SaleSpecialDiscount,@SaleTypeSpecialDiscount,@SaleTotalDiscount,@SaleDiscount,@SaleDay,@SaleMonth,@SaleYear,'Soled')
delete @TempProduct where ItemID=@ItemID;
end
end
end
end
select ProductNo,ProductName,GoldPoints,PlatinumPoints,Price,Cost,Unit,SalePrice,SaleTotalPrice,SalePoint,FullName,MemberType,SalePercenDiscount,SaleSpecialDiscount,SaleTypeSpecialDiscount,SaleTotalDiscount,SaleDiscount,SaleDay,SaleMonth,SaleYear,Status,SaleBillID,LotNo from @TempTable;
END