/****** Object:  View [dbo].[ViwAccFactorFactorDetail]    Script Date: 10/25/2025 11:36:23 ب.ظ ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER VIEW [dbo].[ViwAccFactorFactorDetail]
AS
SELECT DISTINCT 
                      Factor.Type, Factor.Number, Factor.UserNumber, Factor.Date, Factor.SeriallID, Factor.PersonTotalID, Factor.PersonControlID, Factor.PersonDetailID1, Factor.PersonDetailID2, Factor.PersonAddress, 
                      Factor.PersonFinancialCode, Factor.PersonTel, Factor.PersonFax, Factor.PersonEmail, Factor.PersonWeb, Factor.Description,
					  CASE WHEN Factor.Type in (2,9) OR Factor.TaxIns=4  THEN -Factor.Discont ELSE Factor.Discont END as Discont, Factor.DiscontIsPercent, Factor.PayType, 
                      Factor.UpdateDate AS FactorUpdateDate, Factor.Updater AS FactorUpdater, Factor.DiscontTotalID, Factor.DiscontControlID, Factor.DiscontDetailID1, Factor.DiscontDetailID2, Factor.WarehouseID, 
                      Factor.WareOutID, Factor.FromWarehouse, 
					  CASE WHEN Factor.Type in (2,9) OR Factor.TaxIns=4  THEN -Factor.Avarez ELSE Factor.Avarez END as Avarez, Factor.AvarezIsPercent, Factor.AvarezTotalID, Factor.AvarezControlID, Factor.AvarezDetailID1, Factor.AvarezDetailID2, 
					  CASE WHEN Factor.Type in (2,9) OR Factor.TaxIns=4  THEN -Factor.Haml ELSE Factor.Haml END as Haml, Factor.HamlIsPercent, Factor.HamlTotalID, Factor.HamlControlID, Factor.HamlDetailID1, Factor.HamlDetailID2, dbo.FARSI_DATE2(Factor.Date) AS DatePer, Detail.ID, Detail.FactorID, 
                      Detail.OrderTotalID, Detail.OrderControlID, Detail.OrderDetailID1, Detail.OrderDetailID2, 
					  CASE WHEN Factor.Type in (2,9) OR Factor.TaxIns=4  THEN -Detail.Request ELSE Detail.Request END as Request, Detail.RequestUnit, 
					  CASE WHEN Factor.Type in (2,9) OR Factor.TaxIns=4  THEN -Detail.NetPrice ELSE Detail.NetPrice END as NetPrice, 
					  CASE WHEN Factor.Type in (2,9) OR Factor.TaxIns=4  THEN -Detail.TotalPrice ELSE Detail.TotalPrice END as TotalPrice, Detail.DocItemID, Detail.DocHeaderID, 
                      Detail.Status, Detail.OtherTotalID, Detail.OtherControlID, Detail.OtherDetailID1, Detail.OtherDetailID2, 
					  CASE WHEN Factor.Type in (2,9) OR Factor.TaxIns=4  THEN -Detail.Value2 ELSE Detail.Value2 END AS Value2, Detail.Unit2, Detail.UpdateDate, Detail.Updater, Factor.PayTime, 
                      CASE WHEN Factor.Type in (2,9) OR Factor.TaxIns=4  THEN -Detail.Avarez ELSE Detail.Avarez END AS DetailAvarez, 
					  Factor.FactorReturn*Detail.Maliat AS DetailMaliat, 
					  Detail.Discont AS DetailDiscont, Detail.Heigh, Detail.Lenght, Detail.Width, Detail.ZowjInUnit, Factor.Paid, Factor.IsPermanent, 
                      Factor.VizitorTotalID, Factor.VizitorControlID, Factor.VizitorDetailID1, Factor.VizitorDetailID2, Factor.SellType, Detail.Pakaging, 
                      CASE WHEN Factor.TotalPrice = 0 THEN 0 ELSE CAST(Detail.TotalPrice AS decimal(30, 5)) / Factor.TotalPrice * Factor.Discont END AS DetailDiscontDivided, 
					  Detail.Oft, Detail.WarehouseNo, Detail.SumInFactor, Factor.PersonCodePosti, Factor.PersonSabt, Factor.PersonShenase, Factor.CostCenter, Factor.Project, Factor.OrderID, Factor.IsFalsi, Factor.Zir5Darsad, Factor.HaghAmalKari, 
                      Factor.Type1Code, Factor.CityCode, Factor.KalaTypeID, Detail.DocItemRelationID, Detail.OrderID AS DetailOrderID, dbo.AccDocHeader.Number AS DocNum, dbo.AccDocHeader.DocDate, 
                      OrderDocHeader.Number AS OrderDocNumber, OrderDocHeader.DocDate AS OrderDocDate,
					 Factor.Taxid, Factor.Irtaxid, Factor.TaxType, factor.TaxUID, Factor.TaxIns,Factor.FactorReturn
FROM         dbo.ViwFactorTotalPrice AS Factor WITH (nolock) LEFT OUTER JOIN
                      dbo.AccDocItem WITH (nolock) LEFT OUTER JOIN
                      dbo.AccDocHeader WITH (nolock) ON dbo.AccDocItem.HeaderID = dbo.AccDocHeader.ID RIGHT OUTER JOIN
                      dbo.AccDocItem AS OrderDocItem WITH (nolock) LEFT OUTER JOIN
                      dbo.AccDocHeader AS OrderDocHeader WITH (nolock) ON OrderDocItem.HeaderID = OrderDocHeader.ID AND OrderDocItem.ID > 0 RIGHT OUTER JOIN
                      dbo.AccFactorDetail AS Detail WITH (nolock) LEFT OUTER JOIN
                      dbo.AccSellFactorTrucks AS Trucks WITH (nolock) ON Detail.OrderID = Trucks.ID ON OrderDocItem.ID = Trucks.DocItemID AND OrderDocItem.ID > 0 ON dbo.AccDocItem.ID = Detail.DocItemID AND 
                      dbo.AccDocItem.ID > 0 ON Factor.ID = Detail.FactorID
where not Detail.FactorID is null
GO


