; ; PrintReport(dgProduct.CurrentView); Empty(); txtSum.CValue=SumOfCol("#Total.Quantity_RealSale",dgProduct.FilterCondition,true); txtSum.CValue=SumOfCol("#Total.Quantity_RealSale",dgProduct.FilterCondition,true); Empty();
@LocationAll = " la.No_Location In (Select No_Location From Location Where Level_Location = 0)" ; if(cbLocation.SelectedItem == "全部") { @Location = "x.Level_Location = 1"; } else { @Location = " x.Level_Location = 1 And x.No_Location BETWEEN ISNULL(@StartNo_Location,x.No_Location) AND ISNULL(@EndNo_Location,x.No_Location) And s.Name_Location = '" + cbLocation.SelectedItem + "'"; } @dateCondition=" between "+" DATEADD(day,0,DATEDIFF(day,0,"+"'"+TypeConvert("String",StartDate.Value)+"'"+")) and "+" DATEADD(day,0,(DATEDIFF(day,0,"+"'"+TypeConvert("String",EndDate.Value)+"'"+")+1))"; @bisNow=" <= DATEADD(day,0,(DATEDIFF(day,0,'"+TypeConvert("String",EndDate.Value)+"')+1))"; @sqlCmd=" Declare @StartNo_Location Nvarchar(64) Set @StartNo_Location = Null" +" Declare @EndNo_Location Nvarchar(64) Set @EndNo_Location = Null"; if(tbLocationNo1.CValue != "") { @sqlCmd = @sqlCmd + " Set @StartNo_Location ='" +tbLocationNo1.CValue+"'"; } if(tbLocationNo2.CValue != "") { @sqlCmd = @sqlCmd + " Set @EndNo_Location ='" +tbLocationNo2.CValue+"'"; } @sqlCmd =@sqlCmd +" Select No_Location,Product.No_Product,Product.Name_Product,Product.Type_Product,Product.Cost_Product,(IsNull(b.OpeningStockQty_ProductStocks,0)-IsNull(d1.Quantity_SaleOutDepotBillProduct,0)-IsNull(f1.OutQuantity_ModifyDepotBillProduct,0)+IsNull(h1.Quantity_PurchaseInDepotBillProduct,0)+IsNull(e1.InQuantity_ModifyDepotBillProduct,0)+IsNull(d.Quantity_SaleOutDepotBillProduct,0)+IsNull(f.OutQuantity_ModifyDepotBillProduct,0)-IsNull(h.Quantity_PurchaseInDepotBillProduct,0)-IsNull(e.InQuantity_ModifyDepotBillProduct,0)) As OpeningStockQty_ProductStocks ,(IsNull(b.OpeningStockQty_ProductStocks,0)-IsNull(d1.Quantity_SaleOutDepotBillProduct,0)-IsNull(f1.OutQuantity_ModifyDepotBillProduct,0)+IsNull(h1.Quantity_PurchaseInDepotBillProduct,0)+IsNull(e1.InQuantity_ModifyDepotBillProduct,0)) As StockQty_ProductStocks," +" IsNull(d.Quantity_SaleOutDepotBillProduct,0) As Quantity_SaleOutDepotBillProduct,IsNull(e.InQuantity_ModifyDepotBillProduct,0) As InQuantity_ModifyDepotBillProduct,IsNull(f.OutQuantity_ModifyDepotBillProduct,0) As OutQuantity_ModifyDepotBillProduct," +" IsNull(h.Quantity_PurchaseInDepotBillProduct,0) As Quantity_PurchaseInDepotBillProduct,IsNull(i.InQuantity_StocktakingBillProduct,0) As InQuantity_StocktakingBillProduct,IsNull(j.OutQuantity_StocktakingBillProduct,0) As OutQuantity_StocktakingBillProduct" +" From " +" ( " +" Select ID_Product,ID_Location From ProductStocks" +" )a" +" Left Join Product On Product.ID_Product = a.ID_Product" +" Join" +" (" +" Select ID_Location,ProductStocks.ID_Product,Sum(OpeningStockQty_ProductStocks) As OpeningStockQty_ProductStocks" +" From ProductStocks" +" Where ID_Location IN(" +" Select x.ID_Location From Location x" +" Left Join Location s On x.ID_Warehouse = s.ID_Location Where " + @Location +" )" +" Group By ID_Product,ID_Location" +" )b On a.ID_Product=b.ID_Product And a.ID_Location = b.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,SaleOutDepotBillProduct.ID_Product,Sum(SaleOutDepotBillProduct.Quantity_SaleOutDepotBillProduct) As Quantity_SaleOutDepotBillProduct" +" From SaleOutDepotBillProduct" +" Left Join Location lb On lb.ID_Location = SaleOutDepotBillProduct.ID_Location" +" Join SaleOutDepotBill On SaleOutDepotBill.ID_SaleOutDepotBill=SaleOutDepotBillProduct.ID_SaleOutDepotBill" +" Join Location la On la.ID_Location=SaleOutDepotBill.ID_Location" +" Where SaleOutDepotBill.ID_Assessor Is Not Null And SaleOutDepotBill.Date_SaleOutDepotBill "+@dateCondition+ " And "+@LocationAll +" Group By SaleOutDepotBillProduct.ID_Product,lb.ID_Location" +" )d On a.ID_Product=d.ID_Product And a.ID_Location = d.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,ModifyDepotBillProduct.ID_Product,Sum(Quantity_ModifyDepotBillProduct) As InQuantity_ModifyDepotBillProduct" +" From ModifyDepotBillProduct" +" Left Join Location lb On lb.ID_Location = ModifyDepotBillProduct.ID_Location" +" Join ModifyDepotBill On ModifyDepotBillProduct.ID_ModifyDepotBill=ModifyDepotBill.ID_ModifyDepotBill" +" Join ModifyTypeSettings On ModifyTypeSettings.ID_ModifyTypeSettings=ModifyDepotBill.Style_ModifyDepotBill" +" Join Location la On la.ID_Location=ModifyDepotBill.ID_Location" +" Where ModifyDepotBill.ID_Assessor Is Not Null And ModifyTypeSettings.Type_ModifyTypeSettings=1 And Date_ModifyDepotBill "+@dateCondition+ " And "+@LocationAll +" Group By ModifyDepotBillProduct.ID_Product,lb.ID_Location" +" )e On a.ID_Product=e.ID_Product And a.ID_Location = e.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,ModifyDepotBillProduct.ID_Product,Sum(Quantity_ModifyDepotBillProduct) As OutQuantity_ModifyDepotBillProduct" +" From ModifyDepotBillProduct" +" Left Join Location lb On lb.ID_Location = ModifyDepotBillProduct.ID_Location" +" Join ModifyDepotBill On ModifyDepotBillProduct.ID_ModifyDepotBill=ModifyDepotBill.ID_ModifyDepotBill" +" Join ModifyTypeSettings On ModifyTypeSettings.ID_ModifyTypeSettings=ModifyDepotBill.Style_ModifyDepotBill" +" Join Location la On la.ID_Location=ModifyDepotBill.ID_Location" +" Where ModifyDepotBill.ID_Assessor Is Not Null And ModifyTypeSettings.Type_ModifyTypeSettings=0 And Date_ModifyDepotBill "+@dateCondition+ " And "+@LocationAll +" Group By ModifyDepotBillProduct.ID_Product,lb.ID_Location" +" )f On a.ID_Product=f.ID_Product And a.ID_Location = f.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,PurchaseInDepotBillProduct.ID_Product,Sum(Quantity_PurchaseInDepotBillProduct) As Quantity_PurchaseInDepotBillProduct" +" From PurchaseInDepotBillProduct" +" Left Join Location lb On lb.ID_Location = PurchaseInDepotBillProduct.ID_Location" +" Join PurchaseInDepotBill On PurchaseInDepotBill.ID_PurchaseInDepotBill=PurchaseInDepotBillProduct.ID_PurchaseInDepotBill" +" Join Location la On la.ID_Location=PurchaseInDepotBill.ID_Location" +" Where PurchaseInDepotBill.ID_Assessor Is Not Null And PurchaseInDepotBill.Date_PurchaseInDepotBill "+@dateCondition+ " And "+@LocationAll +" Group By PurchaseInDepotBillProduct.ID_Product,lb.ID_Location" +" )h On a.ID_Product=h.ID_Product And a.ID_Location = h.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,StocktakingBillProduct.ID_Product,Sum(StocktakingBillProduct.Quantity_StocktakingBillProduct-StocktakingBillProduct.StockQty_StocktakingBillProduct) As InQuantity_StocktakingBillProduct" +" From StocktakingBillProduct" +" Left Join Location lb On lb.ID_Location = StocktakingBillProduct.ID_Location" +" Join StocktakingBill On StocktakingBill.ID_StocktakingBill=StocktakingBillProduct.ID_StocktakingBill" +" Join Location la On la.ID_Location=StocktakingBill.ID_Location" +" Where StocktakingBill.ID_Assessor Is Not Null And (StocktakingBillProduct.Quantity_StocktakingBillProduct-StocktakingBillProduct.StockQty_StocktakingBillProduct) > 0 And StocktakingBill.Date_StocktakingBill "+@dateCondition+ " And "+@LocationAll +" Group By StocktakingBillProduct.ID_Product,lb.ID_Location" +" )i On a.ID_Product=i.ID_Product And a.ID_Location = i.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,StocktakingBillProduct.ID_Product,-Sum(StocktakingBillProduct.Quantity_StocktakingBillProduct-StocktakingBillProduct.StockQty_StocktakingBillProduct) As OutQuantity_StocktakingBillProduct" +" From StocktakingBillProduct" +" Left Join Location lb On lb.ID_Location = StocktakingBillProduct.ID_Location" +" Join StocktakingBill On StocktakingBill.ID_StocktakingBill=StocktakingBillProduct.ID_StocktakingBill" +" Join Location la On la.ID_Location=StocktakingBill.ID_Location" +" Where StocktakingBill.ID_Assessor Is Not Null And (StocktakingBillProduct.Quantity_StocktakingBillProduct-StocktakingBillProduct.StockQty_StocktakingBillProduct) < 0 And StocktakingBill.Date_StocktakingBill "+@dateCondition+ " And "+@LocationAll +" Group By StocktakingBillProduct.ID_Product,lb.ID_Location" +" )j On a.ID_Product=j.ID_Product And a.ID_Location = i.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,SaleOutDepotBillProduct.ID_Product,Sum(SaleOutDepotBillProduct.Quantity_SaleOutDepotBillProduct) As Quantity_SaleOutDepotBillProduct" +" From SaleOutDepotBillProduct" +" Left Join Location lb On lb.ID_Location = SaleOutDepotBillProduct.ID_Location" +" Join SaleOutDepotBill On SaleOutDepotBill.ID_SaleOutDepotBill=SaleOutDepotBillProduct.ID_SaleOutDepotBill" +" Join Location la On la.ID_Location=SaleOutDepotBill.ID_Location" +" Where SaleOutDepotBill.ID_Assessor Is Not Null And SaleOutDepotBill.Date_SaleOutDepotBill "+@bisNow+ " And "+@LocationAll +" Group By SaleOutDepotBillProduct.ID_Product,lb.ID_Location" +" )d1 On a.ID_Product=d1.ID_Product And a.ID_Location = d1.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,ModifyDepotBillProduct.ID_Product,Sum(Quantity_ModifyDepotBillProduct) As InQuantity_ModifyDepotBillProduct" +" From ModifyDepotBillProduct" +" Left Join Location lb On lb.ID_Location = ModifyDepotBillProduct.ID_Location" +" Join ModifyDepotBill On ModifyDepotBillProduct.ID_ModifyDepotBill=ModifyDepotBill.ID_ModifyDepotBill" +" Join ModifyTypeSettings On ModifyTypeSettings.ID_ModifyTypeSettings=ModifyDepotBill.Style_ModifyDepotBill" +" Join Location la On la.ID_Location=ModifyDepotBill.ID_Location" +" Where ModifyDepotBill.ID_Assessor Is Not Null And ModifyTypeSettings.Type_ModifyTypeSettings=1 And Date_ModifyDepotBill "+@bisNow+ " And "+@LocationAll +" Group By ModifyDepotBillProduct.ID_Product,lb.ID_Location" +" )e1 On a.ID_Product=e1.ID_Product And a.ID_Location = e1.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,ModifyDepotBillProduct.ID_Product,Sum(Quantity_ModifyDepotBillProduct) As OutQuantity_ModifyDepotBillProduct" +" From ModifyDepotBillProduct" +" Left Join Location lb On lb.ID_Location = ModifyDepotBillProduct.ID_Location" +" Join ModifyDepotBill On ModifyDepotBillProduct.ID_ModifyDepotBill=ModifyDepotBill.ID_ModifyDepotBill" +" Join ModifyTypeSettings On ModifyTypeSettings.ID_ModifyTypeSettings=ModifyDepotBill.Style_ModifyDepotBill" +" Join Location la On la.ID_Location=ModifyDepotBill.ID_Location" +" Where ModifyDepotBill.ID_Assessor Is Not Null And ModifyTypeSettings.Type_ModifyTypeSettings=0 And Date_ModifyDepotBill "+@bisNow+ " And "+@LocationAll +" Group By ModifyDepotBillProduct.ID_Product,lb.ID_Location" +" )f1 On a.ID_Product=f1.ID_Product And a.ID_Location = f1.ID_Location" +" Left Join" +" (" +" Select lb.ID_Location,PurchaseInDepotBillProduct.ID_Product,Sum(Quantity_PurchaseInDepotBillProduct) As Quantity_PurchaseInDepotBillProduct" +" From PurchaseInDepotBillProduct" +" Left Join Location lb On lb.ID_Location = PurchaseInDepotBillProduct.ID_Location" +" Join PurchaseInDepotBill On PurchaseInDepotBill.ID_PurchaseInDepotBill=PurchaseInDepotBillProduct.ID_PurchaseInDepotBill" +" Join Location la On la.ID_Location=PurchaseInDepotBill.ID_Location" +" Where PurchaseInDepotBill.ID_Assessor Is Not Null And PurchaseInDepotBill.Date_PurchaseInDepotBill "+@bisNow+ " And "+@LocationAll +" Group By PurchaseInDepotBillProduct.ID_Product,lb.ID_Location" +" )h1 On a.ID_Product=h1.ID_Product And a.ID_Location = h1.ID_Location" +" Left Join Location On Location.ID_Location = a.ID_Location"; @where=true; if(cbType1.Text!="") { if(@where==true) { @sqlCmd=@sqlCmd+" Where Type_Product >=" +"'"+cbType1.Text+"'"; @where=false; } else { @sqlCmd=@sqlCmd+" And Type_Product >=" +"'"+cbType1.Text+"'"; } } if(cbType2.Text!="") { if(@where==true) { @sqlCmd=@sqlCmd+" Where Type_Product <="+"'"+cbType2.Text+"'"; @where=false; } else { @sqlCmd=@sqlCmd+" And Type_Product <="+"'"+cbType2.Text+"'"; } } if(ckSuspend.Checked) { if(@where==true) { @sqlCmd=@sqlCmd+" Where (IsNull(IsNull(d.Quantity_SaleOutDepotBillProduct,0)+IsNull(f.OutQuantity_ModifyDepotBillProduct,0)+IsNull(h.Quantity_PurchaseInDepotBillProduct,0)+IsNull(e.InQuantity_ModifyDepotBillProduct,0)+IsNull(i.InQuantity_StocktakingBillProduct,0)+IsNull(j.OutQuantity_StocktakingBillProduct,0),0)) > 0 "; @where=false; } else { @sqlCmd=@sqlCmd+" And (IsNull(d.Quantity_SaleOutDepotBillProduct,0)+IsNull(f.OutQuantity_ModifyDepotBillProduct,0)+IsNull(h.Quantity_PurchaseInDepotBillProduct,0)+IsNull(e.InQuantity_ModifyDepotBillProduct,0)+IsNull(i.InQuantity_StocktakingBillProduct,0)+IsNull(j.OutQuantity_StocktakingBillProduct,0),0)) > 0 "; } } @sqlCmd=@sqlCmd+" Order By Product.No_Product "; MessageBox(@sqlCmd,@Title); SearchEx(@sqlCmd,"Total");