
/****** Object:  View [dbo].[ViwAccFactorFactorDetail]    Script Date: 7/29/2023 11:34:06 ب.ظ ******/
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, Factor.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, Factor.Avarez, Factor.AvarezIsPercent, Factor.AvarezTotalID, Factor.AvarezControlID, Factor.AvarezDetailID1, Factor.AvarezDetailID2, Factor.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, Detail.Request, Detail.RequestUnit, Detail.NetPrice, Detail.TotalPrice, Detail.DocItemID, Detail.DocHeaderID, 
                      Detail.Status, Detail.OtherTotalID, Detail.OtherControlID, Detail.OtherDetailID1, Detail.OtherDetailID2, Detail.Value2, Detail.Unit2, Detail.UpdateDate, Detail.Updater, Factor.PayTime, 
                      Detail.Avarez 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


