-- ============================================= -- Author: -- Create date: <21.11.2009> -- Updated: <26.04.2010> -- Description: -- ============================================= CREATE PROCEDURE [spConsignor] -- Input parameters: -- @OrderID = Search key for order, CustID = Search key for customers, @Param1 is not in use yet @Param1 int, @OrderID int, @CustID int AS DECLARE @Chain AS BIT BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; --Checks if its a chain management is used on the order SET @Chain = (SELECT COUNT(Orderid) FROM g_order WHERE Orderid = @OrderID AND MAINCONTDELIVERY = 1) -- Insert statements for procedure here -- If the chain management is used, this SELECT statement will be executed IF @Chain = 1 BEGIN SELECT -- Customer NULL AS CustomerNum_Customer, NULL AS Name1_Customer, NULL AS Name2_Customer, NULL AS StreetAddress1_Customer, NULL AS StreetAddress2_Customer, NULL AS StreetAddress3_Customer, NULL AS Postcode_Customer, NULL AS City_Customer, G_CPERS.CELLUAR AS Mobile_Customer, G_CPERS.PHONEWORK Phone_Customer, G_CPERS.EMAIL AS Email_Customer, G_CPERS.FAX AS Fax_Customer, NULL AS PostOfficeBox_Customer, NULL AS PostOfficeBoxCity_Customer, NULL AS PostOfficeBoxPostcode_Customer, NULL AS CountryName_Customer, NULL AS CustomerOrigin_Customer, NULL AS Attention_Customer, NULL AS Contact_Customer, NULL AS ReceiverRef_Customer, NULL AS OurRef_Customer, NULL AS MessageToCarrier_Customer, NULL AS MessageToDriver_Customer, NULL AS MessageToReceiver_Customer, NULL AS PurchaseNo_Customer, NULL AS ShipmentTypeNo_Customer, -- Order CustomerOrder / CustomerOrderCopy g_contac.CustID AS CustomerNum_Order, g_order.OrderID AS OrderNumber_Order, NULL AS Name1_Order, NULL AS Name2_Order, NULL AS StreetAddress1_Order, NULL AS StreetAddress2_Order, NULL AS StreetAddress3_Order, NULL AS Postcode_Order, NULL AS City_Order, NULL AS Mobile_Order, NULL AS Phone_Order, NULL AS Email_Order, NULL AS Fax_Order, NULL AS PostOfficeBox_Order, NULL AS PostOfficeBoxCity_Order, NULL AS PostOfficeBoxPostcode_Order, NULL AS CountryName_Order , NULL AS CustomerOrigin_Order, g_order.RefYour AS Attention_Order, g_order.RefYour AS Contact_Order, g_order.Reference AS ReceiverRef_Order, NULL AS OurRef_Order, NULL AS MessageToCarrier_Order, NULL AS MessageToDriver_Order, NULL AS MessageToReceiver_Order, NULL AS PurchaseNo_Order, NULL AS ShipmentTypeNo_Order, -- Delivery NULL AS CustomerNum_Delivery, CASE WHEN ((LEN(g_order.MAINCONTADRDELIV) - LEN(REPLACE(g_order.MAINCONTADRDELIV, char(13), ')))<3) then g_order.MAINCONTNAME ELSE substring(g_order.MAINCONTADRDELIV,1, CHARINDEX(char(13), g_order.MAINCONTADRDELIV)) END AS Name1_Delivery, NULL AS Name2_Delivery, CASE WHEN ((LEN(g_order.MAINCONTADRDELIV) - LEN(REPLACE(g_order.MAINCONTADRDELIV, char(13), ')))<3) then g_order.MAINCONTADRDELIV ELSE SUBSTRING(g_order.MAINCONTADRDELIV,CHARINDEX(char(13), g_order.MAINCONTADRDELIV),LEN(g_order.MAINCONTADRDELIV)) END AS StreetAddress1_Delivery, NULL AS StreetAddress2_Delivery, NULL AS StreetAddress3_Delivery, NULL AS Postcode_Delivery, NULL AS City_Delivery, NULL AS Mobile_Delivery, NULL AS Phone_Delivery, NULL AS Email_Delivery, NULL AS Fax_Delivery, NULL AS PostOfficeBox_Delivery, NULL AS PostOfficeBoxCity_Delivery, NULL AS PostOfficeBoxPostcode_Delivery, NULL AS CountryName_Delivery, NULL AS CustomerOrigin_Delivery, NULL AS Attention_Delivery, NULL AS Contact_Delivery, NULL AS ReceiverRef_Delivery, NULL AS OurRef_Delivery, NULL AS MessageToCarrier_Delivery, NULL AS MessageToDriver_Delivery, NULL AS MessageToReceiver_Delivery, NULL AS PurchaseNo_Delivery, NULL AS ShipmentTypeNo_Delivery, -- Invoice NULL AS CustomerNum_Invoice, g_order.ContName AS Name1_Invoice, NULL AS Name2_Invoice, g_order.AdrInvoice AS StreetAddress1_Invoice, NULL AS StreetAddress2_Invoice, NULL AS StreetAddress3_Invoice, NULL AS Postcode_Invoice, NULL AS City_Invoice, NULL AS Mobile_Invoice, NULL AS Phone_Invoice, NULL AS Email_Invoice, NULL AS Fax_Invoice, NULL AS PostOfficeBox_Invoice, NULL AS PostOfficeBoxCity_Invoice, NULL AS PostOfficeBoxPostcode_Invoice, NULL AS CountryName_Invoice, NULL AS CustomerOrigin_Invoice, NULL AS Attention_Invoice, NULL AS Contact_Invoice, NULL AS ReceiverRef_Invoice, NULL AS OurRef_Invoice, NULL AS MessageToCarrier_Invoice, NULL AS MessageToDriver_Invoice, NULL AS MessageToReceiver_Invoice, NULL AS PurchaseNo_Invoice, NULL AS ShipmentTypeNo_Invoice, -- Contact NULL AS ContactNum_Contact, NULL AS Name1_Contact, NULL AS Name2_Contact, NULL AS StreetAddress1_Contact, NULL AS StreetAddress2_Contact, NULL AS StreetAddress3_Contact, NULL AS Postcode_Contact, NULL AS City_Contact, g_contac.Phone1 AS Mobile_Contact, g_contac.Phone2 AS Phone_Contact, g_contac.Email AS Email_Contact, g_contac.Fax1 AS Fax_Contact, NULL AS PostOfficeBox_Contact, NULL AS PostOfficeBoxCity_Contact, NULL AS PostOfficeBoxPostcode_Contact, NULL AS CountryName_Contact, NULL AS CustomerOrigin_Contact, NULL AS Attention_Contact, NULL AS Contact_Contact, NULL AS CustomerNum_Contact, -- Package CASE WHEN g_order.FreightQty = 0 then 1 else g_order.FreightQty END AS PackagesCount, NULL AS PackagesMarking, NULL AS PackagesContents, g_order.FreightWeightSum * 1000 AS PackagesWeight, NULL AS PackagesHeight, NULL AS PackagesLength, NULL AS PackagesWidth , g_order.FreightVolumeSum AS PackagesVolume, g_order.Data4 AS CODAccount, g_order.Sum_b AS CODAmount, g_order.KID AS CODKID, NULL AS CODReference, NULL AS InsuranceAmount, NULL AS InsuranceCategory, NULL AS InsurancePolicyNo, NULL AS DeliveryTerms, NULL AS Department, NULL AS InvoiceNumber, g_order.Data4 AS PaymentTerms, NULL AS PaymentType, NULL AS ProjectName, NULL AS ProjectNumber, g_order.DATA2 AS CarrierCode, NULL AS SearchKeyOrder1, NULL AS SearchKeyOrder2, NULL AS SearchKeyInvoiceNum, NULL AS SearchKeyCustomerNum, NULL AS SearchKeyContactNum, g_order.StatusID AS Link_Table1, NULL AS Link_Table2, NULL AS Link_Table3, NULL AS Link_Table4, NULL AS Link_Table5, NULL AS Link_Table6, NULL AS Link_Table7, NULL AS Link_Table8, NULL AS Link_Table9, NULL AS Link_Table10 FROM G_CONTAC INNER JOIN G_ORDER ON G_CONTAC.CONTID = G_ORDER.CONTID LEFT OUTER JOIN G_CPERS ON G_CPERS.CPERSID = G_ORDER.FK_CPERS WHERE g_order.OrderID = @OrderID END -- If the order number exist, this SELECT statement will be executed ELSE IF @OrderID !=0 BEGIN SELECT -- Customer NULL AS CustomerNum_Customer, NULL AS Name1_Customer, NULL AS Name2_Customer, NULL AS StreetAddress1_Customer, NULL AS StreetAddress2_Customer, NULL AS StreetAddress3_Customer, NULL AS Postcode_Customer, NULL AS City_Customer, G_CPERS.CELLUAR AS Mobile_Customer, NULL AS Phone_Customer, G_CPERS.Email AS Email_Customer, NULL AS Fax_Customer, NULL AS PostOfficeBox_Customer, NULL AS PostOfficeBoxCity_Customer, NULL AS PostOfficeBoxPostcode_Customer, NULL AS CountryName_Customer, NULL AS CustomerOrigin_Customer, NULL AS Attention_Customer, NULL AS Contact_Customer, NULL AS ReceiverRef_Customer, NULL AS OurRef_Customer, NULL AS MessageToCarrier_Customer, NULL AS MessageToDriver_Customer, NULL AS MessageToReceiver_Customer, NULL AS PurchaseNo_Customer, NULL AS ShipmentTypeNo_Customer, -- Order CustomerOrder / CustomerOrderCopy g_contac.CustID AS CustomerNum_Order, g_order.OrderID AS OrderNumber_Order, NULL AS Name1_Order, NULL AS Name2_Order, NULL AS StreetAddress1_Order, NULL AS StreetAddress2_Order, NULL AS StreetAddress3_Order, NULL AS Postcode_Order, NULL AS City_Order, NULL AS Mobile_Order, NULL AS Phone_Order, NULL AS Email_Order, NULL AS Fax_Order, NULL AS PostOfficeBox_Order, NULL AS PostOfficeBoxCity_Order, NULL AS PostOfficeBoxPostcode_Order, NULL AS CountryName_Order , NULL AS CustomerOrigin_Order, g_order.RefYour AS Attention_Order, g_order.RefYour AS Contact_Order, g_order.Reference AS ReceiverRef_Order, NULL AS OurRef_Order, NULL AS MessageToCarrier_Order, NULL AS MessageToDriver_Order, NULL AS MessageToReceiver_Order, NULL AS PurchaseNo_Order, NULL AS ShipmentTypeNo_Order, -- Delivery NULL AS CustomerNum_Delivery, CASE WHEN ((LEN(g_order.AdrDeliv) - LEN(REPLACE(g_order.AdrDeliv, char(13), ')))<3) then g_order.ContName ELSE substring(g_order.AdrDeliv,1, CHARINDEX(char(13), g_order.AdrDeliv)) END AS Name1_Delivery, NULL AS Name2_Delivery, CASE WHEN ((LEN(g_order.AdrDeliv) - LEN(REPLACE(g_order.AdrDeliv, char(13), ')))<3) then g_order.AdrDeliv ELSE SUBSTRING(g_order.AdrDeliv,CHARINDEX(char(13), g_order.AdrDeliv),LEN(g_order.AdrDeliv)) END AS StreetAddress1_Delivery, NULL AS StreetAddress2_Delivery, NULL AS StreetAddress3_Delivery, NULL AS Postcode_Delivery, NULL AS City_Delivery, NULL AS Mobile_Delivery, NULL AS Phone_Delivery, NULL AS Email_Delivery, NULL AS Fax_Delivery, NULL AS PostOfficeBox_Delivery, NULL AS PostOfficeBoxCity_Delivery, NULL AS PostOfficeBoxPostcode_Delivery, NULL AS CountryName_Delivery, NULL AS CustomerOrigin_Delivery, NULL AS Attention_Delivery, NULL AS Contact_Delivery, NULL AS ReceiverRef_Delivery, NULL AS OurRef_Delivery, NULL AS MessageToCarrier_Delivery, NULL AS MessageToDriver_Delivery, NULL AS MessageToReceiver_Delivery, NULL AS PurchaseNo_Delivery, NULL AS ShipmentTypeNo_Delivery, -- Invoice NULL AS CustomerNum_Invoice, g_order.ContName AS Name1_Invoice, NULL AS Name2_Invoice, g_order.AdrInvoice AS StreetAddress1_Invoice, NULL AS StreetAddress2_Invoice, NULL AS StreetAddress3_Invoice, NULL AS Postcode_Invoice, NULL AS City_Invoice, NULL AS Mobile_Invoice, NULL AS Phone_Invoice, NULL AS Email_Invoice, NULL AS Fax_Invoice, NULL AS PostOfficeBox_Invoice, NULL AS PostOfficeBoxCity_Invoice, NULL AS PostOfficeBoxPostcode_Invoice, NULL AS CountryName_Invoice, NULL AS CustomerOrigin_Invoice, NULL AS Attention_Invoice, NULL AS Contact_Invoice, NULL AS ReceiverRef_Invoice, NULL AS OurRef_Invoice, NULL AS MessageToCarrier_Invoice, NULL AS MessageToDriver_Invoice, NULL AS MessageToReceiver_Invoice, NULL AS PurchaseNo_Invoice, NULL AS ShipmentTypeNo_Invoice, -- Contact NULL AS ContactNum_Contact, NULL AS Name1_Contact, NULL AS Name2_Contact, NULL AS StreetAddress1_Contact, NULL AS StreetAddress2_Contact, NULL AS StreetAddress3_Contact, NULL AS Postcode_Contact, NULL AS City_Contact, g_contac.Phone1 AS Mobile_Contact, g_contac.Phone2 AS Phone_Contact, g_contac.Email AS Email_Contact, g_contac.Fax1 AS Fax_Contact, NULL AS PostOfficeBox_Contact, NULL AS PostOfficeBoxCity_Contact, NULL AS PostOfficeBoxPostcode_Contact, NULL AS CountryName_Contact, NULL AS CustomerOrigin_Contact, NULL AS Attention_Contact, NULL AS Contact_Contact, NULL AS CustomerNum_Contact, -- Package CASE WHEN g_order.FreightQty = 0 then 1 else g_order.FreightQty END AS PackagesCount, NULL AS PackagesMarking, NULL AS PackagesContents, g_order.FreightWeightSum * 1000 AS PackagesWeight, NULL AS PackagesHeight, NULL AS PackagesLength, NULL AS PackagesWidth , g_order.FreightVolumeSum AS PackagesVolume, g_order.Data4 AS CODAccount, g_order.Sum_b AS CODAmount, g_order.KID AS CODKID, NULL AS CODReference, NULL AS InsuranceAmount, NULL AS InsuranceCategory, NULL AS InsurancePolicyNo, NULL AS DeliveryTerms, NULL AS Department, NULL AS InvoiceNumber, g_order.Data4 AS PaymentTerms, NULL AS PaymentType, NULL AS ProjectName, NULL AS ProjectNumber, g_order.DATA2 AS CarrierCode, NULL AS SearchKeyOrder1, NULL AS SearchKeyOrder2, NULL AS SearchKeyInvoiceNum, NULL AS SearchKeyCustomerNum, NULL AS SearchKeyContactNum, g_order.StatusID AS Link_Table1, NULL AS Link_Table2, NULL AS Link_Table3, NULL AS Link_Table4, NULL AS Link_Table5, NULL AS Link_Table6, NULL AS Link_Table7, NULL AS Link_Table8, NULL AS Link_Table9, NULL AS Link_Table10 FROM G_CONTAC INNER JOIN G_ORDER ON G_CONTAC.CONTID = G_ORDER.CONTID LEFT OUTER JOIN G_CPERS ON G_CPERS.CPERSID = G_ORDER.FK_CPERS WHERE g_order.OrderID = @OrderID END -- If @custID is provided and exists, this SELECT statement will be executed ELSE IF @CustID != 0 BEGIN SELECT -- Customer NULL AS CustomerNum_Customer, NULL AS Name1_Customer, NULL AS Name2_Customer, NULL AS StreetAddress1_Customer, NULL AS StreetAddress2_Customer, NULL AS StreetAddress3_Customer, NULL AS Postcode_Customer, NULL AS City_Customer, NULL AS Mobile_Customer, NULL AS Phone_Customer, NULL AS Email_Customer, NULL AS Fax_Customer, NULL AS PostOfficeBox_Customer, NULL AS PostOfficeBoxCity_Customer, NULL AS PostOfficeBoxPostcode_Customer, NULL AS CountryName_Customer, NULL AS CustomerOrigin_Customer, NULL AS Attention_Customer, NULL AS Contact_Customer, NULL AS ReceiverRef_Customer, NULL AS OurRef_Customer, NULL AS MessageToCarrier_Customer, NULL AS MessageToDriver_Customer, NULL AS MessageToReceiver_Customer, NULL AS PurchaseNo_Customer, NULL AS ShipmentTypeNo_Customer, -- Order CustomerOrder / CustomerOrderCopy g_contac.CustID AS CustomerNum_Order, NULL AS OrderNumber_Order, NULL AS Name1_Order, NULL AS Name2_Order, NULL AS StreetAddress1_Order, NULL AS StreetAddress2_Order, NULL AS StreetAddress3_Order, NULL AS Postcode_Order, NULL AS City_Order, NULL AS Mobile_Order, NULL AS Phone_Order, NULL AS Email_Order, NULL AS Fax_Order, NULL AS PostOfficeBox_Order, NULL AS PostOfficeBoxCity_Order, NULL AS PostOfficeBoxPostcode_Order, NULL AS CountryName_Order , NULL AS CustomerOrigin_Order, NULL AS Attention_Order, NULL AS Contact_Order, NULL AS ReceiverRef_Order, NULL AS OurRef_Order, NULL AS MessageToCarrier_Order, NULL AS MessageToDriver_Order, NULL AS MessageToReceiver_Order, NULL AS PurchaseNo_Order, NULL AS ShipmentTypeNo_Order, -- Delivery NULL AS CustomerNum_Delivery, CASE WHEN ((LEN(G_DELI.STREET) - LEN(REPLACE(G_DELI.STREET, char(13), ')))<1) then g_contac.Name ELSE substring(G_DELI.STREET,1, CHARINDEX(char(13), G_DELI.STREET)) END AS Name1_Delivery, NULL AS Name2_Delivery, CASE WHEN ((LEN(G_DELI.STREET) - LEN(REPLACE(G_DELI.STREET, char(13), ')))<1) then G_DELI.STREET ELSE SUBSTRING(G_DELI.STREET,CHARINDEX(char(13), G_DELI.STREET),LEN(G_DELI.STREET)) END AS StreetAddress1_Delivery, NULL AS StreetAddress2_Delivery, NULL AS StreetAddress3_Delivery, G_DELI.ZIPCODE AS Postcode_Delivery, G_DELI.CITY AS City_Delivery, NULL AS Mobile_Delivery, NULL AS Phone_Delivery, NULL AS Email_Delivery, NULL AS Fax_Delivery, NULL AS PostOfficeBox_Delivery, NULL AS PostOfficeBoxCity_Delivery, NULL AS PostOfficeBoxPostcode_Delivery, NULL AS CountryName_Delivery, NULL AS CustomerOrigin_Delivery, NULL AS Attention_Delivery, NULL AS Contact_Delivery, NULL AS ReceiverRef_Delivery, NULL AS OurRef_Delivery, NULL AS MessageToCarrier_Delivery, NULL AS MessageToDriver_Delivery, NULL AS MessageToReceiver_Delivery, NULL AS PurchaseNo_Delivery, NULL AS ShipmentTypeNo_Delivery, -- Invoice NULL AS CustomerNum_Invoice, NULL AS Name1_Invoice, NULL AS Name2_Invoice, NULL AS StreetAddress1_Invoice, NULL AS StreetAddress2_Invoice, NULL AS StreetAddress3_Invoice, NULL AS Postcode_Invoice, NULL AS City_Invoice, NULL AS Mobile_Invoice, NULL AS Phone_Invoice, NULL AS Email_Invoice, NULL AS Fax_Invoice, NULL AS PostOfficeBox_Invoice, NULL AS PostOfficeBoxCity_Invoice, NULL AS PostOfficeBoxPostcode_Invoice, NULL AS CountryName_Invoice, NULL AS CustomerOrigin_Invoice, NULL AS Attention_Invoice, NULL AS Contact_Invoice, NULL AS ReceiverRef_Invoice, NULL AS OurRef_Invoice, NULL AS MessageToCarrier_Invoice, NULL AS MessageToDriver_Invoice, NULL AS MessageToReceiver_Invoice, NULL AS PurchaseNo_Invoice, NULL AS ShipmentTypeNo_Invoice, -- Contact NULL AS ContactNum_Contact, NULL AS Name1_Contact, NULL AS Name2_Contact, NULL AS StreetAddress1_Contact, NULL AS StreetAddress2_Contact, NULL AS StreetAddress3_Contact, NULL AS Postcode_Contact, NULL AS City_Contact, g_contac.Phone1 AS Mobile_Contact, g_contac.Phone2 AS Phone_Contact, g_contac.Email AS Email_Contact, g_contac.Fax1 AS Fax_Contact, NULL AS PostOfficeBox_Contact, NULL AS PostOfficeBoxCity_Contact, NULL AS PostOfficeBoxPostcode_Contact, NULL AS CountryName_Contact, NULL AS CustomerOrigin_Contact, NULL AS Attention_Contact, NULL AS Contact_Contact, NULL AS CustomerNum_Contact, -- Package NULL AS PackagesCount, NULL AS PackagesMarking, NULL AS PackagesContents, NULL AS PackagesWeight, NULL AS PackagesHeight, NULL AS PackagesLength, NULL AS PackagesWidth , NULL AS PackagesVolume, NULL AS CODAccount, NULL AS CODAmount, NULL AS CODKID, NULL AS CODReference, NULL AS InsuranceAmount, NULL AS InsuranceCategory, NULL AS InsurancePolicyNo, NULL AS DeliveryTerms, NULL AS Department, NULL AS InvoiceNumber, NULL AS PaymentTerms, NULL AS PaymentType, NULL AS ProjectName, NULL AS ProjectNumber, NULL AS CarrierCode, NULL AS SearchKeyOrder1, NULL AS SearchKeyOrder2, NULL AS SearchKeyInvoiceNum, NULL AS SearchKeyCustomerNum, NULL AS SearchKeyContactNum, NULL AS Link_Table1, NULL AS Link_Table2, NULL AS Link_Table3, NULL AS Link_Table4, NULL AS Link_Table5, NULL AS Link_Table6, NULL AS Link_Table7, NULL AS Link_Table8, NULL AS Link_Table9, NULL AS Link_Table10 FROM G_CONTAC INNER JOIN G_DELI ON G_CONTAC.CONTID = G_DELI.SOURCEID WHERE (G_DELI.SOURCETYPE = 1) AND (G_DELI.ADRTYPE = 1 AND G_CONTAC.CUSTID = @CustID ) END END GO