PyJeeves/jeeves_sp_updates/Jeeves_Esales_AddOrderRow.spr

852 lines
25 KiB
Text
Raw Blame History

SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
SET NOCOUNT ON
GO
PRINT 'Begin Jeeves_Esales_AddOrderRow'
GO
PRINT 'Begin Jeeves_Esales_AddOrderRow'
PRINT '***************************************************'
PRINT '* *'
PRINT '* Jeeves_Esales_AddOrderRow *'
PRINT '* *'
PRINT '***************************************************'
GO
IF OBJECT_ID( 'Jeeves_Esales_AddOrderRow', 'P' ) IS NOT NULL
BEGIN
DROP PROCEDURE Jeeves_Esales_AddOrderRow
END
GO
IF (SELECT COUNT(*) FROM xspr WHERE
SprTrgName = 'Jeeves_Esales_AddOrderRow' AND
SprTrgType = 'P' ) > 0
BEGIN
UPDATE xspr SET beskrivning=beskrivning WHERE
SprTrgName = 'Jeeves_Esales_AddOrderRow' AND
SprTrgType = 'P'
END ELSE BEGIN
INSERT INTO xspr(SprTrgName,SprTrgType,RowCreatedDt)
VALUES('Jeeves_Esales_AddOrderRow' , 'P', GETDATE() )
END
GO
----------------------------------------------------
CREATE procedure Jeeves_Esales_AddOrderRow
@c_ForetagKod smallint,
@c_OrderNumber int,
@c_webUserName Jeeves_StrVarChar64 = null,
@c_CompanyNo Jeeves_StrVarChar32 = null,
@c_PersSign Jeeves_StrVarChar6,
@c_LangID smallint = null,
@c_ItemNo Jeeves_StrVarChar32 = null,
@c_Qty Jeeves_Qty = 0,
@c_QtyAltEnh Jeeves_Qty = 0,
@c_RequestedDate Jeeves_StrVarChar10 = null,
@c_BatchId Jeeves_StrVarChar16 = null,
@c_ArtSerieNr Jeeves_StrVarChar32 = null,
@c_OrderType smallint = null,
@c_Run_Type char(1) = null,
@c_TemplateRowID int = null,
@c_Edit Jeeves_StrVarCharMax = null,
@c_EditExt Jeeves_StrVarCharMax = null,
@c_LagStalle Jeeves_StrVarChar10 = null,
@c_AltEnhetKod Jeeves_StrVarChar10 = null,
@c_AllocateAvailable smallint = 0,
@c_OverrideCreditLimit char(1) = 0,
@o_OrderRow int = null output,
@o_NextQty Jeeves_Qty = null output,
@o_NextDate DateTime = null output,
@o_LastQty Jeeves_Qty = null output,
@o_LastDate DateTime = null output,
@o_AllocatedQty Jeeves_Qty = null output,
@o_AllocatedDate DateTime = null output
as
set nocount on
--set ansi_warnings o f f
--set concat_null_yields_null o f f
--set ansi_nulls o f f
--set ansi_padding o f f
declare @withPriceCalc char(1)
select @withPriceCalc = 'Y'
declare @oh_OrdStat smallint,
@orp_OrdRadSt smallint,
@orp_OrdRadNr int,
@orp_vbordradsum money,
@orp_vb_prisinklmoms Jeeves_Amount,
@orp_Vb_Pris Jeeves_Amount,
@wr Jeeves_StrVarChar256,
@x int,
@dbe int,
@dbc int,
@dbp int,
@Enter_TranCount as int,
@NewLine as nvarchar(2),
@PgmId Jeeves_StrVarChar256,
@salj_saljare Jeeves_StrVarChar32,
@kus_kundrabatt float,
@kus_rabklass smallint,
@kus_Kreditsparr char(1),
@kus_kundsaldo money,
@kus_ordsum money,
@kus_kundkredlim money,
@kus_momskod smallint,
@kus_offnr Jeeves_StrVarChar10,
@kus_KundRabattKod0 char(1),
@kus_KundRabattKod1 char(1),
@kus_KundRabattKod2 char(1),
@kus_PrisListaKundSpec int,
@kus_KundKategoriKod smallint,
@kus_tradecalcmarkup float,
@kus_tradefsgmarkup float,
@kus_AddArtEjAktiv char(1),
@ar_ArtProdKlass Jeeves_StrVarChar4,
@ar_OrdvRabKod smallint,
@ar_artkundrabkod char(1),
@ar_artrabklass smallint,
@ar_momskod smallint,
@ar_ordTyp smallint,
@sy1_PrisListaInklMoms char(1),
@sy1_momskod smallint,
@oh_vbordsum money,
@oh_ordsum money,
@oh_lagstalle Jeeves_StrVarChar10,
@Today datetime,
@Momssats float,
@CompanyNo Jeeves_StrVarChar20,
@ContactNo int,
@prislista int,
@Saljare Jeeves_StrVarChar32,
@ar_artfsgforp float,
@ar_palaggdelforp float,
@ar_palaggdelforpbelopp char(1),
@ar_lagstalle Jeeves_StrVarChar10,
@sy1_lagstalle Jeeves_StrVarChar10,
@ValKurs ztCurrencyRate,
@valkod Jeeves_StrVarChar4,
@Samfaktutskr char(1),
@Allowpurchase char(1),
@PCode int,
@sy1_artbeskr_2_orp char(1),
@ArtBeskr Jeeves_StrVarChar64,
@ArtBeskr2 Jeeves_StrVarChar256,
@LangID smallint,
@DateAvailable DateTime,
@sy1_rbn char(1),
@sy1_PrisListaStaffling int,
@sy1_KodAlternativEnhet int,
@AltEnhetKod Jeeves_StrVarChar10,
@AltEnhetOmrFaktor ZtPurQty2Stock,
@InkAvt int,
@LevNr JEEVES_CompanyNo,
@ArtLevPrior smallint,
@ArtHuvudAvt JEEVES_Boolean,
@SALES170 JEEVES_Boolean,
@SALES190 smallint,
@SALES048 smallint,
@SALES007 smallint,
@KostBar Jeeves_StrVarChar8,
@KostStalleKod Jeeves_StrVarChar8,
@K4 Jeeves_StrVarChar8,
@K5 Jeeves_StrVarChar8,
@K6 Jeeves_StrVarChar8,
@K7 Jeeves_StrVarChar8,
@AutoRegel Jeeves_StrVarChar4,
@AnskaffningTillv smallint
select @LangID = @c_LangID
exec Jeeves_Esales_GetUserInfo
@c_IntrnCoNo = @c_ForetagKod,
@c_webUserName = @c_webUserName,
@c_CompanyNo = @c_CompanyNo,
@o_LangID = @LangID output,
@o_CompanyNo = @CompanyNo output,
@o_ContactNo = @ContactNo output,
@o_Prislista = @Prislista output,
@o_Saljare = @Saljare output,
@o_Valkod = @Valkod output,
@o_ValKurs = @Valkurs output
--Adding from a template
if (isnull(@c_TemplateRowID,-1) > -1) begin
if exists(select 1 from web_orp where ConnToSQLIDENTITY = @c_TemplateRowID) begin
declare cr_items cursor static forward_only local for
select artnr, ordantal from web_orp where ConnToSQLIDENTITY = @c_TemplateRowID
open cr_items
while 1=1 begin
fetch next from cr_items into
@c_ItemNo,
@c_Qty
if @@Fetch_Status<>0 BREAK
if @@Error<>0 BREAK
if ( isnull(@c_Qty,0) > 0) and (@c_ItemNo is not null) begin
exec @x = Jeeves_Esales_AddOrderRow
@c_ForetagKod = @c_ForetagKod,
@c_OrderNumber = @c_OrderNumber,
@c_webUserName = @c_webUserName,
@c_PersSign = @c_PersSign,
@c_LangID = @c_LangID,
@c_ItemNo = @c_ItemNo,
@c_Qty = @c_Qty,
@c_AllocateAvailable = @c_AllocateAvailable,
@c_OverrideCreditLimit = @c_OverrideCreditLimit,
@c_RequestedDate = @c_RequestedDate,
@c_BatchId = @c_BatchId,
@c_ArtSerieNr = @c_ArtSerieNr,
@c_OrderType = @c_OrderType
if @x<0 Break
End
End
close cr_items
deallocate cr_items
End
if @c_Run_Type = 'R' exec Jeeves_Esales_GetOrp @c_ForetagKod,@c_webUserName,@LangID,@c_OrderNumber
Return isnull(@x,0)
End
if @c_qty <= 0 Return -210
select @Today = getdate()
if @c_RequestedDate is null select @c_RequestedDate = convert(nvarchar(10),getdate(),112)
---Get customer discount, pricelist, credit info from table kus
select @kus_kundrabatt = kundrabatt,
@kus_rabklass = rabklass,
@kus_Kreditsparr = kreditsparr,
@kus_kundsaldo = kus.kundsaldo,
@kus_ordsum = kus.ordsum,
@kus_kundkredlim = kus.kundkredlim,
@kus_momskod = kus.momskod,
@kus_offnr = offnr,
@Samfaktutskr = samfaktutskr,
@kus_KundRabattKod0 = kundrabattkod0,
@kus_KundRabattKod1 = kundrabattkod1,
@kus_KundRabattKod2 = kundrabattkod2,
@kus_PrisListaKundSpec = prislistakundspec,
@kus_KundKategoriKod = kundkategorikod,
@kus_tradecalcmarkup = tradekalkmarkup,
@kus_tradefsgmarkup = tradefsgmarkup,
@kus_AddArtEjAktiv = AddArtEjAktiv
from kus
where
ftgnr = @CompanyNo and
foretagkod = @c_ForetagKod
--If customer credit is blocked then abort
if @kus_kreditsparr = '1' begin
exec Jeeves_GT @wr output,@c_ForetagKod,@LangID,1344,'Kunden <20>r kreditsp<73>rrad'
EXECUTE Jeeves_RaisError 21001, @wr
return -1344
end
--Get Item discount and descriptions from table ar
select @ar_ArtProdKlass = ar.artprodklass,
@ar_artrabklass = ar.artrabklass,
@ar_artkundrabkod = ar.artkundrabkod,
@ar_OrdvRabKod = ar.ordvrabkod,
@ar_momskod = ar.momskod,
@ar_palaggdelforp = isnull(ar.palaggdelforp,0),
@ar_palaggdelforpbelopp = ar.palaggdelforpbelopp,
@ar_artfsgforp = artfsgforp,
@ar_lagstalle = lagstalle,
@ar_ordTyp = ordTyp,
@Artbeskr = ArtBeskr,
@Artbeskr2 = ArtBeskr2,
@AnskaffningTillv = ar.AnskaffningTillv
from ar
where
ar.artnr = @c_ItemNo and
ar.foretagkod = @c_ForetagKod
--Assign from sy1
select @sy1_PrisListaInklMoms = isnull(prislistainklmoms,0),
@sy1_momskod = momskod,
@sy1_lagstalle = sy1.lagstalle,
@sy1_artbeskr_2_orp = sy1.artbeskr_2_orp,
@sy1_rbn = sy1.sy1_rbn,
@sy1_PrisListaStaffling = PrisListaStaffling,
@sy1_KodAlternativEnhet = KodAlternativEnhet
from sy1
where
sy1.foretagkod = @c_ForetagKod
select @AltEnhetKod = @c_AltEnhetKod
--HACK: If ArtFsgForp is set, find matching AltEnhetKod
if isnull(@ar_artfsgforp,0) > 0 and @AltEnhetKod is NULL begin
if @sy1_KodAlternativEnhet = 2 begin
select @AltEnhetKod = xae.AltEnhetKod, @AltEnhetOmrFaktor = xae.AltEnhetOmrFaktor
from xare join xae
on (xare.AltEnhetKod = xae.AltEnhetKod)
where
xae.AltEnhetOmrFaktor = ROUND(@ar_artfsgforp, 6) and
xare.artnr = @c_ItemNo and
xare.foretagkod = @c_ForetagKod
if @AltEnhetKod is not NULL begin
--Abort from futher logic down the line
select @sy1_KodAlternativEnhet = 0
end
end
end
--Get Item alternative unit from table xae
if @sy1_KodAlternativEnhet = 1 begin
if @AltEnhetKod is not NULL begin
select @AltEnhetOmrFaktor = AltEnhetOmrFaktor
from xae
where
xae.AltEnhetKod = @AltEnhetKod and
xae.foretagkod = @c_ForetagKod
end else begin
select @AltEnhetKod = AltEnhetKod, @AltEnhetOmrFaktor = AltEnhetOmrFaktor
from xae
where
xae.AltEnhetOrderStd = 1 and
xae.foretagkod = @c_ForetagKod
end
end
--Get Item alternative unit from table xare
if @sy1_KodAlternativEnhet = 2 begin
if @AltEnhetKod is not NULL begin
select @AltEnhetOmrFaktor = AltEnhetOmrFaktor
from xare
where
xare.AltEnhetKod = @AltEnhetKod and
xare.artnr = @c_ItemNo and
xare.foretagkod = @c_ForetagKod
end else begin
select @AltEnhetKod = AltEnhetKod, @AltEnhetOmrFaktor = AltEnhetOmrFaktor
from xare
where
xare.AltEnhetOrderStd = 1 and
xare.artnr = @c_ItemNo and
xare.foretagkod = @c_ForetagKod
end
if @AltEnhetOmrFaktor is NULL begin
select @AltEnhetOmrFaktor = AltEnhetOmrFaktor
from xae
where
xae.AltEnhetKod = @AltEnhetKod and
xae.foretagkod = @c_ForetagKod
end
end
if @AltEnhetKod is not NULL and @AltEnhetOmrFaktor > 0 begin
--Set the value that was not provided
if isnull(@c_QtyAltEnh,0) = 0 and isnull(@c_Qty,0) != 0 begin
select @c_QtyAltEnh = @c_Qty / @AltEnhetOmrFaktor
end
if isnull(@c_Qty,0) = 0 and isnull(@c_QtyAltEnh,0) != 0 begin
select @c_Qty = @c_QtyAltEnh * @AltEnhetOmrFaktor
end
--If both Qty and QtyAltEnh is provided then verify correct value provided
if @c_Qty > 0 and @c_QtyAltEnh > 0 begin
if ROUND(@c_Qty, 4) != ROUND(@c_QtyAltEnh * @AltEnhetOmrFaktor, 4) begin
EXECUTE Jeeves_RaisError 21001, 'Qty and QtyAltEnh must follow the AltEnhetOmrFaktor when both are provided'
return 50100
end
end
end else begin
--Fallback if there is no alternative units
if isnull(@c_Qty,0) = 0 and isnull(@c_QtyAltEnh,0) != 0 begin
select @c_Qty = @c_QtyAltEnh
select @c_QtyAltEnh = 0
end
end
select @Enter_TranCount = @@TranCount,
@NewLine=char(13)+char(10),
@dbp=@@ProcId
--Calc pack markup
--TODO: Fix this, as somehow ar_artfsgforp was an int in this SP, which is wrong. And now this is broken.
if isnull(convert(integer,@ar_artfsgforp),0) > 0 begin
if ((select convert(integer,@c_Qty) % convert(integer,@ar_artfsgforp)) = 0)
select @ar_palaggdelforp = 0
end
if @c_OrderNumber is not NULL and @oh_OrdStat is NULL
select
@oh_OrdStat = [oh].[OrdStat],
@oh_vbordsum = oh.vbordsum,
@oh_ordsum = oh.ordsum,
@oh_lagstalle = oh.lagstalle
from [oh] where
[oh].[OrderNr] = @c_OrderNumber and
[oh].[ForetagKod] = @c_ForetagKod
---Use order type from ar if available
select @c_orderType = isnull(@c_orderType, @ar_ordTyp)
--Always use parameter if provided to the store procedure
--Use order location from ar table if present
select @c_Lagstalle = isnull(@c_Lagstalle,@ar_Lagstalle)
--Use order location as default
if @c_Lagstalle is null select @c_lagstalle = @oh_Lagstalle
declare
@QtyAvailable as decimal
exec Jeeves_Ar_Disp_Test
@c_IntrnCoNo = @c_ForetagKod,
@ArtNr = @c_ItemNo,
@OrdAntal = @c_Qty,
@OrdDatum = @c_RequestedDate,
@LagStalle = @c_LagStalle,
@c_Return_Type='Y',
@o_DispQty = @QtyAvailable output,
@o_FirstQty = @o_NextQty output,
@o_FirstDate = @o_NextDate output,
@o_LastQty = @o_LastQty output,
@o_LastDate = @o_LastDate output
if @o_NextQty is null begin
select @o_NextQty = 0
end
if @o_LastQty is null begin
select @o_LastQty = 0
end
-- if the AnskaffningTillv is larger than 0 (the product is produced by orders) then discard Jeeves_Ar_Disp_Test
if @AnskaffningTillv>0 begin
select @c_AllocateAvailable = 0
end
if @c_AllocateAvailable = 1 begin
-- Place the order with the amount available on the requested date and
-- return information about the rest.
if @c_RequestedDate = @o_NextDate begin
--If the first date and the requested date are the same,
--the on hand quantity will appear as first date quantity as well.
select @o_NextDate = null
select @o_NextQty = 0
end
--Prefer next to last.
if @o_NextQty = 0 or @o_NextQty is null begin
select @o_NextQty = @o_LastQty
select @o_NextDate = @o_LastDate
select @o_LastQty = 0
select @o_LastDate = null
end
--If there is nothing available on the requested date, use the next
if @QtyAvailable = 0 begin
select @c_Qty = @o_NextQty
select @DateAvailable = @o_NextDate
select @o_NextQty = @o_LastQty
select @o_NextDate = @o_LastDate
select @o_LastQty = 0
select @o_LastDate = null
end else begin
--Only the amount available on the requested date should be allocated on the order row.
select @DateAvailable = @c_RequestedDate
select @c_Qty = @QtyAvailable
end
if @o_NextQty = 0 begin
select @o_NextDate = null
end
if @o_LastQty = 0 begin
select @o_LastDate = null
end
end else begin
--If everything is available immediately, set detaavailable to now
if(@QtyAvailable >= @c_Qty) begin
select @DateAvailable = @c_RequestedDate
end else begin
--Everything isn't available, if last quantity is 0, the rest is available on first date.
if @o_LastQty = 0 begin
select @DateAvailable = @o_NextDate
end else begin
--leave last date as date available, sice the order can't be delivered before that date.
select @DateAvailable = @o_LastDate
end
end
select @o_NextQty = 0
select @o_NextDate = null
select @o_LastQty = 0
select @o_LastDate = null
end
--Pass the quantity that is placed on the order back to the caller.
select @o_AllocatedQty = @c_Qty
select @o_AllocatedDate = @DateAvailable
select @InkAvt = al.InkAvt, @LevNr=al.FtgNr , @ArtLevPrior = al.ArtLevPrior, @ArtHuvudAvt =al.ArtHuvudAvt from al where al.ArtNr=@c_ItemNo and al.ArtHuvudAvt='1' and al.ForetagKod=@c_ForetagKod;
--@In_SALES170 JEEVES_Boolean=NULL,
--@In_SALES190 smallint=NULL,
select @SALES170 = dbo.Jeeves_FN_GetParam(@c_ForetagKod, 'SALES170', 'B',null)
select @SALES190 = dbo.Jeeves_FN_GetParam(@c_ForetagKod, 'SALES190', 'N',null)
select @SALES048 = dbo.Jeeves_FN_GetParam(@c_ForetagKod, 'SALES048', 'N',null)
select @SALES007 = dbo.Jeeves_FN_GetParam(@c_ForetagKod, 'SALES007', 'N',null)
--Get cost codes from sales person or location (the same way as UF_Oh_Get_InternKonton)
if @SALES048 = 1 begin
--Get settings from inventory location
select
@KostStalleKod = KostStalleKod,
@KostBar = KostBar,
@K4 = K4,
@K5 = K5,
@K6 = K6,
@K7 = K7,
@AutoRegel = AutoRegelForskott
from xb with (NoLock) where
ForetagKod = @c_ForetagKod
and LagStalle = @c_LagStalle;
end else if @SALES048 = 2 begin
--Get settings from sales person
if @Saljare is not null begin
select
@KostStalleKod = KostStalleKod,
@KostBar = KostBar,
@K4 = K4,
@K5 = K5,
@K6 = K6,
@K7 = K7,
@AutoRegel = AutoRegelForskott
from salj with (NoLock) where
ForetagKod = @c_ForetagKod
and Saljare=@Saljare;
end
end else if @SALES048 = 3 begin
-- Try sales person first and then and add values
-- from location is not provided
if @Saljare is not null begin
select
@KostStalleKod = KostStalleKod,
@KostBar = KostBar,
@K4 = K4,
@K5 = K5,
@K6 = K6,
@K7 = K7,
@AutoRegel = AutoRegelForskott
from salj with (NoLock) where
ForetagKod = @c_ForetagKod
and Saljare=@Saljare;
end
select
@KostStalleKod = ISNULL(@KostStalleKod, KostStalleKod),
@KostBar = ISNULL(@KostBar, KostBar),
@K4 = ISNULL(@K4, K4),
@K5 = ISNULL(@K5, k5),
@K6 = ISNULL(@K6, K6),
@K7 = ISNULL(@K7, K7),
@AutoRegel = ISNULL(@AutoRegel, AutoRegelForskott)
from xb with (NoLock) where
ForetagKod = @c_ForetagKod
and LagStalle = @c_LagStalle;
end
--calculate fiscal year
declare @TodayDate datetime;
declare @redar integer;
declare @period integer;
declare @Just_BokfDat datetime;
set @TodayDate = convert(varchar(10), @Today, 112);
execute CalcPeriodP2P @todayDate,
null,
@redar output,
@period output,
@Just_BokfDat output,
@c_ForetagKod
declare @startRow int
declare @endRow int
declare @maxRow int -- The highest row number in the discount row range.
declare @discountQty decimal(17,6)
if @o_OrderRow is not null begin
--All rows that are grouped by being in the same decade should
--be updated with the discount, since they probably make up a split order row,
--and they should all be taken into account when calculating the discount.
--This is only relevant if a row number is provided.
select @startRow = 10 * (@o_OrderRow / 10) -- yx->y0, e.g. 24->20
select @endRow = @startRow + 9
--The updated row is excluded from the sum but the new quantity is added directly to discountQty
select @discountQty = (isnull(sum(ordantal), 0) + @c_Qty), @maxRow = isnull(max(OrdRadNr), @startRow) from orp
where ForetagKod = @c_ForetagKod and OrderNr = @c_OrderNumber and OrdRadNr >= @StartRow and OrdRadNr <= @EndRow
and OrdRadNr <> @o_OrderRow and ArtNr =@c_ItemNo
if @o_OrderRow > @maxRow begin
select @maxRow = @o_OrderRow
end
end else begin
select @discountQty = @c_qty
select @startRow = -1
end
if @withPriceCalc = 'Y' begin
declare @orp_ArtCirkaPris as Jeeves_Amount,
@orp_VolymRabatt as float,
@orp_KundRabatt as float,
@orp_Rabatt1 as float,
@orp_Rabatt2 as float,
@orp_Rabatt3 as float,
@orp_valkod as nvarchar(3),
@orp_valkurs as decimal(18,8),
@orp_ordDatum as datetime
execute @x = Jeeves_orp_produkt_pris
@c_Foretagkod = @c_ForetagKod,
@ArtNr = @c_ItemNo,
@OrdAntal = @discountQty,
@ValKurs = @ValKurs,
@ValKod = @valkod,
@PrisLista = @Prislista,
@OrdDatum = @Today,
@ArtProdKlass = @ar_ArtProdKlass,
@offradnr = -32000,
@ArtRabKlass = @ar_ArtRabKlass,
@RabKlass = @kus_RabKlass,
@OrdvRabKod = @ar_OrdvRabKod,
@ArtKundRabKod = @ar_ArtKundRabKod,
@KundRabattKod0 = @kus_KundRabattKod0,
@KundRabattKod1 = @kus_KundRabattKod1,
@KundRabattKod2 = @kus_KundRabattKod2,
@In_PrisListaStaffling = @sy1_PrisListaStaffling,
@In_PrisListaKundSpec = @kus_PrisListaKundSpec,
@In_KundKategoriKod = @kus_KundKategoriKod,
@KundRabatt = @kus_KundRabatt,
@In_InkAvt = @InkAvt,
@In_LevNr = @LevNr,
@In_ArtLevPrior = @ArtLevPrior,
@In_ArtHuvudAvt = @ArtHuvudAvt,
@In_tradekalkmarkup = @kus_tradecalcmarkup,
@In_tradefsgmarkup = @kus_tradefsgmarkup,
@In_OrdBerLevDat = @DateAvailable,
@In_Sales170 = @SALES170,
@In_Sales190 = @SALES190,
@In_RedovisnAr = @redar,
@in_FtgNr = @CompanyNo,
@Logg = null,
@offnr = @kus_offnr,
@In_LagStalle = @c_Lagstalle,
@O_Svar_Pris_Valuta = @orp_Vb_Pris output,
@O_Svar_VolymRabatt = @orp_VolymRabatt output,
@O_Svar_KundRabatt = @orp_KundRabatt output,
@O_Svar_Rabatt1 = @orp_Rabatt1 output,
@O_Svar_Rabatt2 = @orp_Rabatt2 output,
@O_Svar_Rabatt3 = @orp_Rabatt3 output,
@O_Svar_CirkaPris_Valuta = @orp_ArtCirkaPris output
if @ar_palaggdelforpbelopp = 1 begin
select @orp_vb_pris = @orp_vb_pris + @ar_palaggdelforp
end else begin
-- palaggdelforp is specified in percentage, so 80 should result in a factor of 1.8.
select @orp_vb_pris = @orp_vb_pris * (1 + 0.01 * @ar_palaggdelforp)
end
end
---Calc VAT------------------------------------------------------
if @kus_momskod = 0 begin
select @Momssats = 0
end
else begin
if @ar_momskod is null select @ar_momskod = isnull(@kus_momskod,@sy1_momskod)
select @Momssats = x1.momssats
from x1
where
momskod = @ar_momskod
end
if @sy1_PrisListaInklMoms in ('1','3') begin
select @orp_vb_prisinklmoms = @orp_Vb_Pris
select @orp_vb_pris = @orp_vb_pris/(1.0+(@MomsSats/100.0))
end
else begin
select @orp_vb_prisinklmoms = @orp_vb_pris * (1.0+(@Momssats/100.0))
end
-----------------------------------------------------------
---Check if creditlimit is reached
if @kus_kundkredlim > 0 and @c_OverrideCreditLimit = 0 begin
if (isnull(@oh_ordsum,0) + @kus_kundsaldo + @kus_ordsum + (@Orp_vb_pris*@c_qty)) > @kus_kundkredlim begin
exec Jeeves_GT @wr output,@c_ForetagKod,@LangID,3679,'Kundens kreditgr<67>ns <20>r <20>verskriden'
EXECUTE Jeeves_RaisError 21001, @wr
return -3679
end
end
begin tran
select @oh_vbordsum = isnull(@oh_vbordsum, 0) + (@c_qty * @orp_Vb_Pris * ((100 - @orp_Rabatt1)/100) * ((100 - @orp_Rabatt2)/100) * ((100 - @orp_Rabatt3)/100) * ((100-@orp_kundrabatt)/100))
select @oh_ordsum = 0
select @orp_ordradst = dbo.Jeeves_FN_GetParam (@c_ForetagKod,'WEBAPP018','N','10')
if @oh_OrdStat < @orp_ordradst select @orp_ordradst = @oh_OrdStat
if @sy1_artbeskr_2_orp = 1 begin
--Add description to orp row if artbeskr_2_orp flag is set.
-- First try the old way.
select @Artbeskr = isnull(ArtBeskr, @Artbeskr), @Artbeskr2 = isnull(ArtBeskr2, @Artbeskr2)
from arb where
foretagkod = @c_ForetagKod
and sprakkod = @LangID
and artnr = @c_ItemNo
--Override with modern translation with old transalation value as default
execute @ArtBeskr = Jeeves_FN_GetCustomerDropDownValue
@c_ForetagKod,
@LangID,
'ar',
@c_ItemNo,
@ArtBeskr
end
else begin
select @Artbeskr = null, @Artbeskr2 = null
end
if @c_Qty <= 0
begin
if @@TranCount > 0 ROLLBACK TRANSACTION
EXECUTE Jeeves_RaisError 50100, 'Jeeves_Esales_AddOrderRow'
while @@TranCount<@Enter_TranCount BEGIN TRANSACTION
return 50100
end
execute @x = Jeeves_Init_Insert_orp
@c_ForetagKod = @c_ForetagKod,
@c_OrderNr = @c_OrderNumber,
@c_FtgNr = @CompanyNo,
@c_ArtNr = @c_ItemNo,
@c_ArtSerieNr = @c_ArtSerieNr,
@c_BatchId = @c_BatchID,
@c_Edit = @c_Edit,
@c_EditExt = @c_EditExt,
@c_OrdBegLevDat = @c_RequestedDate,
@c_OrdBerLevDat = @DateAvailable,
@c_Lagstalle = @c_lagstalle,
@c_OrdAntal = @c_Qty,
@c_OrdAntalAltEnh = @c_QtyAltEnh,
@c_OrdTyp = @c_OrderType,
@c_ordradst = @orp_ordradst,
@c_PersSign = @c_PersSign,
@c_volymrabatt = @orp_VolymRabatt,
@c_KundRabatt = @orp_KundRabatt,
@c_Rabatt1 = @orp_Rabatt1,
@c_Rabatt2 = @orp_Rabatt2,
@c_Rabatt3 = @orp_Rabatt3,
@c_Saljare = @Saljare,
@c_vb_pris = @orp_Vb_Pris,
@c_vb_prisinklmoms = @orp_vb_prisinklmoms,
@c_rowcreatedby = @c_perssign,
@c_rowcreateddt = @Today,
@c_Valkod = @valkod,
@c_Valkurs = @valkurs,
@c_OrdRadNr = @o_OrderRow output,
@c_Artbeskr = @ArtBeskr,
@c_OrdArtBeskr = @ArtBeskr2,
@c_KostBar = @KostBar,
@c_KostStalleKod = @KostStalleKod,
@c_K4 = @K4,
@c_K5 = @K5,
@c_K6 = @K6,
@c_K7 = @K7,
@c_AutoRegel = @AutoRegel,
@c_AltEnhetKod = @AltEnhetKod;
if (@@Error <> 0) or (@x < 0) begin
EXECUTE Jeeves_RaisError 21001, 'Error when creating order row'
Rollback tran
Return -100
end
--If the row created with a specific row number, the prices of other rows might be affected.
if @startRow >= 0 begin
declare @rowIndex as int
select @rowIndex = @startRow
while @rowIndex <= @maxRow begin
Update orp SET
orp.vb_pris = @orp_Vb_Pris,
orp.vb_prisinklmoms = @orp_vb_prisinklmoms,
orp.volymrabatt = @orp_VolymRabatt,
orp.KundRabatt = @orp_KundRabatt,
orp.Rabatt1 = @orp_Rabatt1,
orp.Rabatt2 = @orp_Rabatt2,
orp.Rabatt3 = @orp_Rabatt3,
orp.rowupdatedby= @c_PersSign,
orp.rowupdateddt = getdate()
Where ordernr = @c_OrderNumber and
foretagkod = @c_ForetagKod and
ordradnr = @rowIndex and
artnr = @c_ItemNo and
ordradnr <> @o_OrderRow
select @rowIndex = @rowIndex + 1
end
end
update oh set
oh.ordsum = @oh_ordsum,
oh.vbordsum = @oh_vbordsum
where
oh.ordernr = @c_OrderNumber and
oh.foretagkod = @c_ForetagKod
if @@Error <> 0 begin
rollback tran
return -100
end
if @SALES007 = 1 AND @kus_AddArtEjAktiv <> '1'
BEGIN
exec JEEVES_Orp_Create_Tillagg @c_OrderNumber, @c_PersSign, @c_ForetagKod
END
if @@Error <> 0 begin
rollback tran
return -100
end
commit tran
--calculate ordervalue discount;
execute JEEVES_Oh_Kora @c_OrderNumber,NULL,2,null,@c_ForetagKod;
--make sure ordersum is updated;
execute JEEVES_Oh_OrderVarde @c_OrderNumber, @ValKurs, @sy1_rbn, @c_PersSign, @c_ForetagKod = @c_ForetagKod, @call_type = 4
if @c_Run_Type = 'R' exec Jeeves_Esales_GetOrp @c_ForetagKod,@c_webUserName,@LangID,@c_OrderNumber
return 0
GO
PRINT 'End Jeeves_Esales_AddOrderRow'
GO
PRINT 'End Jeeves_Esales_AddOrderRow'
GO