
[dbo].[BcImportCustomers]
CREATE procedure [dbo].[BcImportCustomers]
@items BcCustomerImportItems readonly,
@addresses BcAddressImportItems readonly
as
begin
set nocount on
declare @AccountingCustomerIdHolder table
(
AccountingCustomerFid int not null,
BcId uniqueidentifier not null,
LastUpdatedInBcOn datetimeoffset not null
)
declare @AccountingCustomerAddressIdHolder table
(
AccountingCustomerAddressFid int not null,
AddressBcId uniqueidentifier not null,
AddressCode varchar(64) null
)
;with
MergeData as
(
select
BcCustomer.AccountingCustomerFid,
Items.BcId,
Items.[Name],
Items.CustomerNumber,
Items.Email,
Items.CreditLimit,
Items.LastModified
from @items Items
left outer join BcCustomer on Items.BcId = BcCustomer.BcId
)
merge AccountingCustomer with (tablock) using MergeData on
MergeData.AccountingCustomerFid = AccountingCustomer.AccountingCustomerId
when matched then update set
[Name] = MergeData.[Name],
CustomerNumber = MergeData.CustomerNumber,
Email = MergeData.Email,
OnHold = 0,
CreditLimit = MergeData.CreditLimit,
NoCreditFlag = 0
when not matched then
insert
(
[Name],
CustomerNumber,
Email,
OnHold,
CreditLimit,
NoCreditFlag
)
values
(
MergeData.[Name],
MergeData.CustomerNumber,
MergeData.Email,
0,
MergeData.CreditLimit,
0
)
output
inserted.AccountingCustomerId,
MergeData.BcId,
MergeData.LastModified
into @AccountingCustomerIdHolder;
declare @now datetimeoffset = sysdatetimeoffset()
;with
MergeData as
(
select
AccountingCustomerIdHolder.BcId,
AccountingCustomerIdHolder.AccountingCustomerFid,
LastUpdatedInBcOn
from @AccountingCustomerIdHolder as AccountingCustomerIdHolder
)
merge BcCustomer with (tablock) using MergeData on
MergeData.BcId = BcCustomer.BcId
when matched then update set
LastImportedOn = @now
when not matched then
insert
(
BcId,
AccountingCustomerFid,
CreatedOn,
LastImportedOn,
LastUpdatedInBcOn
)
values
(
MergeData.BcId,
MergeData.AccountingCustomerFid,
@now,
@now,
MergeData.LastUpdatedInBcOn
);
insert into AccountingCustomerAddressType(TypeName)
select Addresses.Code
from @addresses Addresses
except
select AccountingCustomerAddressType.TypeName
from AccountingCustomerAddressType
;with MergeData as
(
select
AccountingCustomer.AccountingCustomerId,
AccountingCustomerAddressType.AccountingCustomerAddressTypeId,
AddressDeleted = convert(bit, case when Addresses.ParentBcId is null then 1 else 0 end ),
AddressBcId = Addresses.BcId,
AddressCode = Addresses.Code,
Addresses.Address1,
Addresses.Address2,
Addresses.City,
Addresses.State,
Addresses.PostalCode,
BcCustomerAddress.AccountingCustomerAddressFid,
CountryCodeStandard.CountryCodeStandardID
from @items Items
inner join BcCustomer on Items.BcId = BcCustomer.BcId
inner join AccountingCustomer on BcCustomer.AccountingCustomerFid = AccountingCustomer.AccountingCustomerId
left outer join @addresses Addresses on BcCustomer.BcId = Addresses.ParentBcId
left outer join BcCustomerAddress on Addresses.BcId = BcCustomerAddress.BcId
inner join AccountingCustomerAddressType on Addresses.Code = AccountingCustomerAddressType.TypeName
left outer join CountryCodeStandard on Addresses.CountryAlpha2Code = CountryCodeStandard.Alpha2Code
) merge AccountingCustomerAddress with (tablock) using MergeData ON
MergeData.AccountingCustomerAddressFid = AccountingCustomerAddressId
when matched and MergeData.AddressDeleted = 0 then update SET
AccountingCustomerAddressTypeFid = MergeData.AccountingCustomerAddressTypeId,
Address1 = MergeData.Address1,
Address2 = MergeData.Address2,
City = MergeData.City,
[State] = MergeData.State,
[PostalCode] = MergeData.PostalCode,
[CountryCodeStandardFID] = MergeData.CountryCodeStandardID
when not matched and AddressDeleted = 0 then insert (
AccountingCustomerFid,
AccountingCustomerAddressTypeFid,
Address1,
Address2,
City,
State,
PostalCode,
CountryCodeStandardFID
) values (
MergeData.AccountingCustomerId,
MergeData.AccountingCustomerAddressTypeId,
MergeData.Address1,
MergeData.Address2,
MergeData.City,
MergeData.State,
MergeData.PostalCode,
MergeData.CountryCodeStandardID
)
output
inserted.AccountingCustomerAddressId,
MergeData.AddressBcId,
MergeData.AddressCode
into @AccountingCustomerAddressIdHolder;
;with MergeData as
(
select
AccountingCustomerAddressIdHolder.AccountingCustomerAddressFid,
AccountingCustomerAddressIdHolder.AddressBcId,
AccountingCustomerAddressIdHolder.AddressCode
from @AccountingCustomerAddressIdHolder AccountingCustomerAddressIdHolder
)
merge BcCustomerAddress with(tablock) using MergeData on
MergeData.AddressBcId = BcCustomerAddress.BcId
when matched then update
set LastImportedOn = @now
when not matched then
insert
(
BcId,
AccountingCustomerAddressFid,
CreatedOn,
LastImportedOn
)
values
(
MergeData.AddressBcId,
MergeData.AccountingCustomerAddressFid,
@now,
@now
);
insert into AccountingCustomerAddress
(
AccountingCustomerFid,
AccountingCustomerAddressTypeFid,
CountryCodeStandardFID
)
select
AccountingCustomerFid = BcCustomer.AccountingCustomerFid,
AccountingCustomerAddressTypeFid = AccountingCustomerAddressType.AccountingCustomerAddressTypeId,
CountryCodeStandardFID = dbo.udfGetDefaultCountryCodeStandardID()
from @items as Items
inner join BcCustomer on Items.BcId = BcCustomer.BcId
inner join AccountingCustomerAddressType on AccountingCustomerAddressType.TypeName = dbo.GetDefaultAccountingCustomerAddressTypeName()
left outer join AccountingCustomerAddress on AccountingCustomerAddress.AccountingCustomerFid = BcCustomer.AccountingCustomerFid and
AccountingCustomerAddress.AccountingCustomerAddressTypeFid = AccountingCustomerAddressType.AccountingCustomerAddressTypeId
where AccountingCustomerAddress.AccountingCustomerAddressId is null
end
GO
GRANT EXECUTE ON [dbo].[BcImportCustomers] TO [MssExec]
GO