BEGIN
select OverValuationPriceLimit from AssetParameters into :mOverValuationPriceLimit;
for
/* Vybereme vsechny karty, ktere jsou v edidenci nebo vyrazene */
/* po datu ke kteremu vypisujeme knihu zpetne k datu */
/* a zaroven splnuji omezovaci parametry zadane uzivatelem */
select ID, Status, DiscardDate$DATE, FileInDate$DATE, FileInFirstDate$DATE,
TotalIncreased1, TotalIncreased2, TotalDecreaseOfBasePrice, TotalDecreaseOfTaxRemPrice,
TotalTaxDepreciation, TaxBasePriceInitial, AssetOrigin, AccBasePriceInitial,
TotalDecreaseOfAccRemPrice, TotalAccDepreciation, AccDepreciationBeginsAt,
TaxDeprecAccountedInLastYear, TaxDepreciationType, TaxDepreciationBeginsAt,
BasePriceLimit, AccDepreciationType, TaxValueChangesUsedIn
from AssetCards AC
where (/* Omezeni za stav karty ktery platil k datu vypisu knihy */
(/* Jen karty v evidenci */
(:AStatus = 1) and
(
'A' in (select IsOnRecord from AssetCardIsOnRecord(:AToDate, AC.FileInDate$DATE, AC.DiscardDate$DATE))
)
)
or
(/* Jen vyrazene karty*/
(:AStatus = 2) and 'A' in (select IsNotOnRecord from AssetCardIsNotOnRecord(:AToDate, AC.DiscardDate$DATE))
)
or
(/* jen karty v evidenci a vyrazene */
(:AStatus = 0) and
(
(
'A' in (select IsOnRecord from AssetCardIsOnRecord(:AToDate, AC.FileInDate$DATE, AC.DiscardDate$DATE))
)
or
'A' in (select IsNotOnRecord from AssetCardIsNotOnRecord(:AToDate, AC.DiscardDate$DATE))
)
)
) and
(:AnAssetCardSelID = '' or
ID in (select OBJ_ID from SELDAT where SEL_ID=:AnAssetCardSelID)) and
(:AnAssetTypeSelID = '' or
AssetType_ID in (select OBJ_ID from SELDAT where SEL_ID=:AnAssetTypeSelID)) and
(:AExpensesDivisionsSelID = '' or
(select ExpensesDH.SValue from StringValueOfFieldWithDefNEm(AC.ID, 'S0ODIDCD1NEL314Z00C5PPZRR4', 208, :AToDate, AC.ExpensesDivision_ID) ExpensesDH) in /*ExpensesDivision_ID*/
(
SELECT Bx.ID FROM Divisions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :AExpensesDivisionsSelID)) OR
((:AExpensesDivisionsWithChilds in ('1', 'A')) and (Bx.ID in (select ID from Sys$Divisions2 where Superior_ID in (select OBJ_ID from SELDAT where SEL_ID = :AExpensesDivisionsSelID)))))
)
) and
(:AEvidenceDivisionsSelID = '' or
(select EvidenceDH.SValue from StringValueOfFieldWithDefNEm(AC.ID, 'S0ODIDCD1NEL314Z00C5PPZRR4', 207, :AToDate, AC.EvidenceDivision_ID) EvidenceDH) in /*EvidenceDivision_ID*/
(
SELECT Bx.ID FROM Divisions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :AEvidenceDivisionsSelID)) OR
((:AEvidenceDivisionsWithChilds in ('1', 'A')) and (Bx.ID in (select ID from Sys$Divisions2 where Superior_ID in (select OBJ_ID from SELDAT where SEL_ID = :AEvidenceDivisionsSelID)))))
)
) and
(:ABusOrdersSelID = '' or
BusOrder_ID in
(
SELECT Bx.ID FROM BusOrders Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :ABusOrdersSelID)) OR
((:ABusOrdersWithChilds in ('1', 'A')) and (Bx.ID in (select ID from Sys$BusOrders2 where Superior_ID in (select OBJ_ID from SELDAT where SEL_ID = :ABusOrdersSelID)))))
)
) and
(:ABusTransactionsSelID = '' or
BusTransaction_ID in
(
SELECT Bx.ID FROM BusTransactions Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :ABusTransactionsSelID)) OR
((:ABusTransactionsWithChilds in ('1', 'A')) and (Bx.ID in (select ID from Sys$BusTransactions2 where Superior_ID in (select OBJ_ID from SELDAT where SEL_ID = :ABusTransactionsSelID)))))
)
) and
(:ABusProjectsSelID = '' or
BusProject_ID in
(
SELECT Bx.ID FROM BusProjects Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :ABusProjectsSelID)) OR
((:ABusProjectsWithChilds in ('1', 'A')) and (Bx.ID in (select ID from Sys$BusProjects2 where Superior_ID in (select OBJ_ID from SELDAT where SEL_ID = :ABusProjectsSelID)))))
)
) and
(:AnAssetLocationSelID = '' or
AssetLocation_ID in
(
SELECT Bx.ID FROM AssetLocations Bx WHERE Bx.Hidden = 'N' AND
( (Bx.ID in (select OBJ_ID from SELDAT where SEL_ID = :AnAssetLocationSelID)) OR
((:AnAssetLocationWithChilds in ('1', 'A')) and (Bx.ID in (select ID from Sys$AssetLocations2 where Superior_ID in (select OBJ_ID from SELDAT where SEL_ID = :AnAssetLocationSelID)))))
)
) and
(:AResponsibleSelID = '' or
Responsible_ID in (select OBJ_ID from SELDAT where SEL_ID=:AResponsibleSelID)) and
(:ACreateBySelID = '' or
CreatedBy_ID in (select OBJ_ID from SELDAT where SEL_ID=:ACreateBySelID)) and
(((:AFileInDateFrom = 0) and (:AFileDateTo = 0)) or
(FileInDate$DATE >= :AFileInDateFrom AND FileInDate$DATE < :AFileDateTo)) and
(:ATaxDepreciationGroupSelID = '' or
TaxDepreciationGroup_ID in (select OBJ_ID from SELDAT where SEL_ID=:ATaxDepreciationGroupSelID)) and
(:AnAccDepreciationGroupSelID = '' or
AccDepreciationGroup_ID in (select OBJ_ID from SELDAT where SEL_ID=:AnAccDepreciationGroupSelID))
into :mAssetCard_ID, :mStatus, :mDiscardDate$DATE, :mFileInDate$DATE, :mFileInFirstDate$DATE,
:mTotalIncreased1, :mTotalIncreased2, :mTotalDecreaseOfBasePrice, :mTotalDecreaseOfTaxRemPrice,
:mTotalTaxDepreciation, :mTaxBasePriceInitial, :mAssetOrigin, :mAccBasePriceInitial,
:mTotalDecreaseOfAccRemPrice, :mTotalAccDepreciation, :mAccDepreciationBeginsAt,
:mTaxDeprecAccountedInLastYear, :mTaxDepreciationType, :mTaxDepreciationBeginsAt,
:mBasePriceLimit, :mAccDepreciationType, :mTaxValueChangesUsedIn
do begin
/*------------*/
/*DANOVY ODPIS*/
/*------------*/
/*Ceny danove (vst.ceny,zust.ceny a odpisy) se pocitaji tak, ze se k datu, ke kteremu
delame vypis nejprve zjisti, do jakeho patri obdobi a nalezene se posledni mesic
predchazejiciho obdobi. Toto se udela jeste v DELPHI. Do teto funkce pak uz vstupuje
jenom rok a posledni mesic predchazejiciho obdobi. Tyto udaje sem vstupuji jako parametry
ADepreciationYear a ADepreciationMonth. Potom se zkusi najit odpis pro tento rok a mesic.
Pokud se to nepodari, znamena to ze odpis byl prerusen a najde se posledni danovy odpis
s nizsim datumem (rokem a mesicem - posledni odpis pred prerusenim). Od cen na tomto
odpisu se odpichneme.
Vstupni cenu k datu vypisu spocteme tak, ze najdeme vsechny doklady zmeny ceny
a castecna vyrazeni od data dohledaneho odpisu az k datu porizovani knihy. Tyto doklady
pricteme (odecteme) od vstupni ceny na dohledanem odpisu. (VCkonec = VCzacatek + ZMC - VYR)
Zustatkovou cenu k datu vypisu spocteme tak, ze opet najdeme doklady zmeny ceny a cast.
vyrazeni a take napocteme vsechny dan.odpisy od dohledaneho opisu a k datu ke kteremu
porizujeme knihu. Tyto udaje pricteme (odecteme) od zustatove ceny na dohledanem odpisu.
(ZCkonec = ZCzacatek + ZMC - VYR - ODPISY).
Podrobne jsou operace popsany dale.
Poznamka: velikost odpisů může být ovlivněna změnou ceny (ZMC), která se nebude přičítat,
protože doklad ZMC se má do odpisů promítnout až po datu ke kterému děláme výpis.
K tomuto případu dojde jenom tehdy, pokud se nebudou odpisy PRUBĚŽNĚ zaúčtovávat.
Tzn. že uživatel zadá ZMC dříve, než zaúčtuje odpisy za všechny měsíce předcházející
mesíci, ve kterém se má ZMC projevit do odpisů.
Příklad:
v daném roce není zaúčtován žádný odpis. Zadáme ZMC k červenci. Tento ZMC se ale
v odpisech projeví už od ledna (ovlivní výši odpisů). Když nyní budeme dělat výpis
k dubnu, bude se počítat s odpisy ovlivněnými touto ZMC, ale ZMC se nezapočte, protože
se má projevit až později
*/
mTaxBasePrice = 0;
mTaxRemainderPriceInitial = 0;
if (ib_decodedate_year(mFileInDate$DATE) = :ADepreciationYear+1) then
begin
/*Delame vypis zpetne k datu k prvnimu roku odpisovani karty v G3 */
mTaxBasePrice = :mTaxBasePriceInitial;
/*Osetrime limit vstupni ceny danove, ktery ma vliv i na velikost zustatkove danove ceny */
if ((mBasePriceLimit <> 0) and (mTaxBasePrice > mBasePriceLimit)) then begin
mTaxRemainderPriceInitial = :mBasePriceLimit;
end else begin
mTaxRemainderPriceInitial = :mTaxBasePriceInitial;
end
if ((mAssetOrigin = 1) or (mAssetOrigin = 2)) then
/*Zohledníme snížení ceny, odpisy provedené mimo systém Gx*/
mTaxRemainderPriceInitial = mTaxRemainderPriceInitial + mTotalDecreaseOfBasePrice - mTotalDecreaseOfTaxRemPrice - mTotalTaxDepreciation - mTaxDeprecAccountedInLastYear;
mDocumentsDateFrom = :mFileInDate$DATE;
end
else
begin
/*Vybereme odpis k dane karte a mesici a roku od ktereho budeme napocitavat danove
vstupni a zustatkove ceny. Pokud odpis k danemu mesici a datu neexistuje, tak vyberem
odpis nejblizsi nizsi (v takovem pripade to znamena, ze odpisy byly preruseny).
Potom napoctu zvyseni, castecna vyrazeni a odpisy nasledujici za timto odpisem do data,
ke kteremu delame vypis. Temito udaji budu modifikovat hodnoty ziskane z dohledaneho
odpisu*/
mTaxD_Year = 0;
mTaxD_Month = 0;
mTaxBasePrice = 0;
mTaxDepreciation = 0;
mTaxRemainderPriceInitial = 0;
/*Touhle promennou budu testovat, zda se podarilo nejaky odpis vubec najit.
Pokud se najit nepodari, tak to nevleze do vnitr FORu.
To znamena, ze delam vypis k datu, pred kterym nejsou G3 zadne odpisy.
Tzn.ze danove odpisy byly hned od zacatku vlozeni do G3 preruseny.
*/
mExist = 0;
for
select D_Year, D_Month, TaxBasePrice, TaxDepreciation, TaxRemainderPriceInitial
from AssetDepreciations
where (Parent_ID = :mAssetCard_ID) and ((D_Year*100+D_Month) <=
(:ADepreciationYear*100+:ADepreciationMonth)) and (D_Year*100+D_Month) in
(select max(A.D_Year*100+A.D_Month) from AssetDepreciations A where (Parent_ID = :mAssetCard_ID) and
((A.D_Year*100+A.D_Month) <= (:ADepreciationYear*100+:ADepreciationMonth)) and
(select assgp.Period_ID from AssetGetPeriodID(A.D_Month, A.D_Year) assgp) not in
(select Period_ID from AssetInterruptions where
(Parent_ID = :mAssetCard_ID)
)
)
into :mTaxD_Year, :mTaxD_Month, :mTaxBasePrice, :mTaxDepreciation, :mTaxRemainderPriceInitial
do begin
mExist = 1;
end
mCorrectDateFrom = 0;
if (mExist=1) then
begin
mTaxRemainderPriceInitial = :mTaxRemainderPriceInitial - :mTaxDepreciation;
/*Vybereme doklady preceneni, ktere byly zauctovany (vystaveny) po mesici, od ktereho pocitame
danove odpisy az do data, ke kteremu porizujeme vypis knihy.
Zapocitame pritom i doklady, ktere byly vystaveny v dobe eventualniho preruseni odpisu
protoze z hlediska ucetnictvi se cena zmenila, jenom odpisy jsou prerusene.
----------------------------------------------------------------------------------- */
/*Urcime prvni den mesice nasledujici za vybranym danovym odpisem */
select LastDayOfMonth from MonthLastDay(:mTaxD_Month, :mTaxD_Year) into :mLastDay;
mDocumentsDateFrom = ib_encodedate(:mTaxD_Year, :mTaxD_Month, mLastDay) + 1;
mCorrectDateFrom = 1;
end
else
begin
/*Danove odpisy byly hned od zacatku vlozeni do G3 preruseny. Zachovam se stejne
jako kdyz se dela vypis zpetne k datu k prvnimu roku odpisovani karty v G3
*/
mTaxBasePrice = :mTaxBasePriceInitial;
/*Osetrime limit vstupni ceny danove, ktery ma vliv i na velikost zustatkove danove ceny */
if ((mBasePriceLimit <> 0) and (mTaxBasePrice > mBasePriceLimit)) then begin
mTaxRemainderPriceInitial = :mBasePriceLimit;
end else begin
mTaxRemainderPriceInitial = :mTaxBasePriceInitial;
end
if ((mAssetOrigin = 1) or (mAssetOrigin = 2)) then
/*Zohledníme snížení ceny, odpisy provedené mimo systém Gx*/
mTaxRemainderPriceInitial = mTaxRemainderPriceInitial + mTotalDecreaseOfBasePrice - mTotalDecreaseOfTaxRemPrice - mTotalTaxDepreciation - mTaxDeprecAccountedInLastYear;
mDocumentsDateFrom = :mFileInDate$DATE;
end
end
mSumVCTaxBaseToNextPeriod = 0;
mSumVCTaxRemPriceToNextPeriod = 0;
/*Určíme jaké všechny TZ budeme k datu výpisu započítávat*/
if ((mTaxValueChangesUsedIn = 0) and
((mTaxDepreciationType = 2) or
((mTaxDepreciationType = 4) and (mTaxDepreciationBeginsAt = 1)))) then
begin
/*Odpisování podle životnosti nebo rovnoměrný měsíční odpis začátkem odpisování v měsíci následujícím po měsíci zařazení.
Započítáváme TZ a změnu ceny vystavené do měsíce výpisu. Tzn. TZ vystavená v měsíci výpisu knihy již nebudeme započítávat (např.do vstupní ceny).
U tohoto typu odpisů se mají TZ a změny ceny započítávat až od dalšího měsíce.
*/
/*Určím měsíc předcházející datu výpisu */
mEndMonth = IB_DecodeDate_Month(AToDate);
if (mEndMonth = 1) then begin
mEndMonth = 12;
end else begin
mEndMonth = mEndMonth - 1;
end
/*Určím rok předcházející datu výpisu */
if (mEndMonth = 12) then begin
mEndYear = IB_DecodeDate_Year(AToDate) - 1;
end else begin
mEndYear = IB_DecodeDate_Year(AToDate);
end
/*Poslední den v měsící předcházejím datu výpisu*/
select LastDayOfMonth from MonthLastDay(:mEndMonth, :mEndYear) into :mEndDay;
/*Vypočítám datum posledního dne předcházejícího měsíce*/
mEndDate = IB_EncodeDate(mEndYear, mEndMonth, mEndDay);
if (mCorrectDateFrom = 1) then begin
/*Určím měsíc, od kterého se budou ZC napočítavat - posuneme se o měsíc zpět*/
mFromMonth = IB_DecodeDate_Month(mDocumentsDateFrom);
if (mFromMonth = 1) then begin
mFromMonth = 12;
end else begin
mFromMonth = mFromMonth - 1;
end
/*Určím rok*/
if (mFromMonth = 12) then begin
mFromYear = IB_DecodeDate_Year(mDocumentsDateFrom) - 1;
end else begin
mFromYear = IB_DecodeDate_Year(mDocumentsDateFrom);
end
/*Vypočítám datum od kdy počítat ZC*/
mDocumentsDateFrom = IB_EncodeDate(mFromYear, mFromMonth, 1);
/* Zjistíme, zda se neprovádí výpis k prvnímu měsíci v roce (účetním období).
Pokud ano, musíme zjistit, zda existuje nějaké TZ v předchozím roce, které se nepromítlo do VC (vstupní cena) v minulém roce a má se promítnout ve vypisovaném měsíci.
Poznámka: Typicky toto nastane, pokud se provedlo TZ v poslením měsíci předcházejícího roku.
*/
-- V mDocumentsDateFrom je uložen začátek předchozího měsíce. Zjistíme datum konce předchozího měsíce
select LastDayOfMonth from MonthLastDay(:mFromMonth, :mFromYear) into :mLastDay;
mLastDayDate = ib_encodedate(:mFromYear, :mFromMonth, mLastDay);
-- Zjistíme datum konce předchozího roku (účetního období)
select DateFrom$DATE, DateTo$DATE from Periods where
ID in (select Period_ID from AssetGetPeriodID(:mFromMonth, :mFromYear))
into :mBeginOfPrevPeriod, :mEndOfPrevPeriod;
if (mBeginOfPrevPeriod < mFileInDate$DATE) then begin
mBeginOfPrevPeriod = mFileInDate$DATE;
end
mEndOfPrevPeriod = mEndOfPrevPeriod - 1;
mEndOfPrevPeriodMonth = ib_DecodeDate_Month(mEndOfPrevPeriod);
if (mLastDayDate = mEndOfPrevPeriod) then begin
-- promítnutí TZ a ZC z posledního měsíce roku do začátku roku.
-- Suma TZ a ZC v posledním měsíci předchozího roku
select sum(TaxBasePriceAmount), sum(TaxRemainderPriceAmount)
from AssetValueChanges
where ((AssetCard_ID = :mAssetCard_ID) and (IsValorisation = 'A') and (IsApplyInCard = 'A') and
DateUseInAssetCard$DATE is NOT NULL and DateUseInAssetCard$DATE <> 0 and
(DateUseInAssetCard$DATE >= :mDocumentsDateFrom) and
(DateUseInAssetCard$DATE <= :mEndOfPrevPeriod))
or
((AssetCard_ID = :mAssetCard_ID) and (IsValorisation = 'N') and
DateUseInAssetCard$DATE is NOT NULL and DateUseInAssetCard$DATE <> 0 and
(DateUseInAssetCard$DATE >= :mDocumentsDateFrom) and
(DateUseInAssetCard$DATE <= :mEndOfPrevPeriod))
into :mSumVCTaxBasePrice, :mSumVCTaxRemainderPrice;
if (mSumVCTaxBasePrice is NULL) then
mSumVCTaxBasePrice = 0;
if (mSumVCTaxRemainderPrice is NULL) then
mSumVCTaxRemainderPrice = 0;
-- Zjištění částek na počátku předchozího období (případně k měsíci zařazení karty, pokud byla zařazena až v průběhu období)
mBeginOfPrevPeriodMonth = ib_DecodeDate_Month(mBeginOfPrevPeriod);
mBeginOfPrevPeriodYear = ib_DecodeDate_Year(mBeginOfPrevPeriod);
if (mBeginOfPrevPeriodMonth = 12) then begin
mBeginOfPrevPeriodMonth = 1;
mBeginOfPrevPeriodYear = mBeginOfPrevPeriodYear + 1;
end else begin
mBeginOfPrevPeriodMonth = mBeginOfPrevPeriodMonth + 1;
end
mBeginOfPrevPeriodTaxBasePrice = NULL;
select TaxBasePrice
from AssetDepreciations
where (Parent_ID = :mAssetCard_ID) and (D_Month = :mBeginOfPrevPeriodMonth) and (D_Year = :mBeginOfPrevPeriodYear)
into :mBeginOfPrevPeriodTaxBasePrice;
--odpis se v předchozím roce nenašel, zkusíme to ještě na konci roku
--ošetřuje situaci, kdy došlo ke znovuzahájení odpisování na již odepsané kartě po TZ/ZC
if (mBeginOfPrevPeriodTaxBasePrice is NULL) then
select TaxBasePrice
from AssetDepreciations
where (Parent_ID = :mAssetCard_ID) and (D_Month = :mEndOfPrevPeriodMonth) and (D_Year = :mBeginOfPrevPeriodYear)
into :mBeginOfPrevPeriodTaxBasePrice;
if (mBeginOfPrevPeriodTaxBasePrice is NULL) then
mBeginOfPrevPeriodTaxBasePrice = 0;
/* Suma TZ v daném období (mSumVCTaxBasePrice) mínus velikost promítnutých TZ v daném období (mHlpNr ... Rozdíl cena na začátku a konci roku) -> velikost TZ v daném období, které se má případně promítnout až v dalším období
Poznámka:
Nezajímá nás zda TZ překročilo limit, protože pokud nepřekročilo, tak se jedná o TZ, které se mají promítat do karty bez ohledu na cenu.
(Ta TZ u kterých se má zohledňovat promítnutí promítnutí ceny na základě překročení limitu a nepřekročily limit, tak nemají nastaven příznak IsApplyInCard)
*/
/* uschovani prenosu TZ a ZC do pristiho obdobi */
mSumVCTaxBaseToNextPeriod = mSumVCTaxBasePrice;
mSumVCTaxRemPriceToNextPeriod = mSumVCTaxRemainderPrice;
-- Další TZ, které se budou dohledávat musíme dohledat až od prvního měsíce následujícího roku (účetního období). Tedy až od měsíce, ke kterému se provádí výpis
mDocumentsDateFrom = mLastDayDate + 1;
end
end
end else begin
/*Započítávám všechna TZ a změny ceny až do data výpisu včetně.*/
mEndDate = :AToDate;
end
/*Vlastni vyber - tech zhodnoceni (TZ)*/
select sum(TaxBasePriceAmount), sum(TaxRemainderPriceAmount)
from AssetValueChanges
where (AssetCard_ID = :mAssetCard_ID) and (IsValorisation = 'A') and (IsApplyInCard = 'A') and
DateUseInAssetCard$DATE is NOT NULL and DateUseInAssetCard$DATE <> 0 and
(DateUseInAssetCard$DATE >= :mDocumentsDateFrom) and
(DateUseInAssetCard$DATE <= :mEndDate)
into :mSumVCTaxBasePrice, mSumVCTaxRemainderPrice;
if (mSumVCTaxBasePrice is NULL) then
mSumVCTaxBasePrice = 0;
if (mSumVCTaxRemainderPrice is NULL) then
mSumVCTaxRemainderPrice = 0;
/*Zjistime, zda zjištěné TZ přesáhlo limit pro uplatnění:
1.Limit byl překročen -> TZ se uplatní do vstupní a zůstatkové ceny
2.Limit NEbyl překročen -> Zjištěné TZ do vstupní a zůstatkové ceny nepromítáme. Zkusíme jestli existují nějaká TZ,
která se mají do cen na kartě promítnou bez ohledu na překročení limitu pro TZ.
Pozn.Řeší ERR-2390/2008. Dále by toto mělo řešit také ERR-1276/2004, která byla dříve řešena jiným způsobem.
*/
if (:mSumVCTaxBasePrice < :mOverValuationPriceLimit) then begin
/*Případ 2*/
/*Zkusíme sečít TZ, která mají nastaveno promítnutí do karty majetku bez ohledu na překročení limitu. - takové TZ se případně promítnou do vstupní a zůstatkové ceny*/
select sum(TaxBasePriceAmount), sum(TaxRemainderPriceAmount)
from AssetValueChanges
where (AssetCard_ID = :mAssetCard_ID) and (IsValorisation = 'A') and (IsApplyInCard = 'A') and
(ApplyInCardRegardlessOfAmount = 'A') and
DateUseInAssetCard$DATE is NOT NULL and DateUseInAssetCard$DATE <> 0 and
(DateUseInAssetCard$DATE >= :mDocumentsDateFrom) and
(DateUseInAssetCard$DATE <= :mEndDate)
into :mSumVCTaxBasePrice, mSumVCTaxRemainderPrice;
if (mSumVCTaxBasePrice is NULL) then
mSumVCTaxBasePrice = 0;
if (mSumVCTaxRemainderPrice is NULL) then
mSumVCTaxRemainderPrice = 0;
end
/*Vlastni vyber - zmena ceny*/
select sum(TaxBasePriceAmount), sum(TaxRemainderPriceAmount)
from AssetValueChanges
where (AssetCard_ID = :mAssetCard_ID) and (IsValorisation = 'N') and
DateUseInAssetCard$DATE is NOT NULL and DateUseInAssetCard$DATE <> 0 and
(DateUseInAssetCard$DATE >= :mDocumentsDateFrom) and
(DateUseInAssetCard$DATE <= :mEndDate)
into :mHlpBase, :mHlpReminder;
if (mHlpBase is NULL) then
mHlpBase = 0;
if (mHlpReminder is NULL) then
mHlpReminder = 0;
/*Výběr dokladů ZM, které nejsou primární. Tyto doklady mohou existovat jenom u
karty s oceněnými prvky. Jsou to doklady vystavené při rozšíření souboru oceněných prvků
a mají z hlediska odpisů význam změny ceny */
select sum(TaxBasePriceAmount), sum(TaxRemainderPriceAmount)
from AssetPutToEvidence
where (AssetCard_ID = :mAssetCard_ID) and (IsPrimary = 'N') and
(DocDate$DATE >= :mDocumentsDateFrom) and
(DocDate$DATE <= :AToDate)
into :mTaxBasePriceComp, mTaxRemainderPriceComp;
if (mTaxBasePriceComp is NULL) then
mTaxBasePriceComp = 0;
if (mTaxRemainderPriceComp is NULL) then
mTaxRemainderPriceComp = 0;
/* Secteme TZ a zmenu ceny a zařazení nových prvků*/
mSumVCTaxBasePrice = mSumVCTaxBasePrice + mHlpBase + mTaxBasePriceComp;
mSumVCTaxRemainderPrice = mSumVCTaxRemainderPrice + mHlpReminder + mTaxRemainderPriceComp;
/*Vybereme doklady castecneho vyrazeni, ktere byly zauctovany (vystaveny) po mesici, od ktereho pocitame
danove odpisy az do data, ke kteremu porizujeme vypis knihy.*/
select sum(TaxBasePriceAmount), sum(TaxRemainderPriceAmount)
from AssetDiscards
where (AssetCard_ID = :mAssetCard_ID) and (IsFinal='N') and
(AccDate$DATE >= :mDocumentsDateFrom) and
(AccDate$DATE <= :AToDate)
into :mSumDTaxBasePrice, :mSumDTaxRemainderPrice;
if (mSumDTaxBasePrice is NULL) then
mSumDTaxBasePrice = 0;
if (mSumDTaxRemainderPrice is NULL) then
mSumDTaxRemainderPrice = 0;
/*Secteme odpisy od mesice nasledujiciho za dohledanym danovym odpisem do mesice
a roku, ke kteremu delame vypis*/
select sum(TaxDepreciation)
from AssetDepreciations
where (Parent_ID = :mAssetCard_ID) and ((D_Year*100+D_Month) >
(:ADepreciationYear*100+:ADepreciationMonth)) and
((D_Year*100+D_Month) < (:AToYear*100+:AToMonth))
into :mSumTaxDepreciation;
if (mSumTaxDepreciation is NULL) then
mSumTaxDepreciation = 0;
/*Zjistime sumu odpisu za obdobi k jakemu se provadi vypis knihy zpetne k datu
Tento udaj se pouziva v Knize zpetne k datu detailni */
/*Zjistim zacatek periody do ktere patri rok, ke kteremu provadim vypis knihy zpetne k datu */
select DateFrom$DATE from Periods where
ID in (select Period_ID from AssetGetPeriodID(:AToMonth, :AToYear))
into :mStartActPeriod;
/*Ze zacatku si odvodim rok a mesic zacatku */
mStartYearActPeriod = ib_decodedate_year(:mStartActPeriod);
mStartMonthActPeriod = ib_decodedate_month(:mStartActPeriod);
/* Sectu odpisy za danou periodu do mesice pred mesicem, ke kteremu delam vypis */
select sum(TaxDepreciation), sum(AccDepreciation)
from AssetDepreciations
where (Parent_ID = :mAssetCard_ID) and
((D_Year*100+D_Month) >= (:mStartYearActPeriod*100+:mStartMonthActPeriod)) and
((D_Year*100+D_Month) < (:AToYear*100+:AToMonth))
into :mSumActPeriodTaxDepreciation, :mSumActPeriodAccDepreciation;
if (mSumActPeriodTaxDepreciation is NULL) then
mSumActPeriodTaxDepreciation = 0;
if (mSumActPeriodAccDepreciation is NULL) then
mSumActPeriodAccDepreciation = 0;
/* Zohledníme TZ případně promítaná z předchozího roku (účet.období) - nastává jen u karty mTaxDepreciationType = 2, 4 viz výše*/
if (mSumVCTaxBaseToNextPeriod <> 0) then begin
mTaxBasePrice = mTaxBasePrice + mSumVCTaxBaseToNextPeriod;
end
/*Zvysime danove vstupni ceny o castky dohledanych dokladu zvyseni ceny a
snizime danove vstupni ceny o castky dohledanych dokladu castecneho vyrazeni.
Timto dostaneme stav danove vstupni ceny ke dni vypisu knihy*/
if (mSumVCTaxBasePrice <> 0) then begin
mTaxBasePrice = mTaxBasePrice + mSumVCTaxBasePrice;
end
if (mSumDTaxBasePrice <> 0) then begin
mTaxBasePrice = mTaxBasePrice - mSumDTaxBasePrice;
end
/*-------------------------------------------------*/
/*Zvysime danove zustakove ceny o castky dohledanych dokladu zvyseni ceny dale
snizime danove zustakove ceny o castky dohledanych dokladu castecneho vyrazeni a
snizime danove zustakove ceny o sumu odpisu (popis co suma znamena viz vyse) */
--
/* Zohledníme TZ případně promítaná z předchozího roku (účet.období) - nastává jen u karty mTaxDepreciationType = 2, 4 viz výše*/
if (mSumVCTaxRemPriceToNextPeriod <> 0) then begin
mTaxRemainderPriceInitial = mTaxRemainderPriceInitial + mSumVCTaxRemPriceToNextPeriod;
end
if (mSumVCTaxRemainderPrice <> 0) then begin
mTaxRemainderPriceInitial = mTaxRemainderPriceInitial + mSumVCTaxRemainderPrice;
end
if (mSumDTaxRemainderPrice <> 0) then begin
mTaxRemainderPriceInitial = mTaxRemainderPriceInitial - mSumDTaxRemainderPrice;
end
if (mSumTaxDepreciation <> 0) then begin
mTaxRemainderPriceInitial = mTaxRemainderPriceInitial - mSumTaxDepreciation;
end
/*------------*/
/*UCETNI ODPIS*/
/*------------*/
/*Vybereme odpis k dane karte a mesici a roku ke kteremu delame knihu zpetne k datu */
/*(bereme mesic o jednicku nizsi nez je datum, ke kteremu delame vypis)*/
/*Pokud odpis k danemu mesici a datu neexistuje, tak vyberem odpis nejblizsi nizsi*/
mAccD_Year = 0;
mAccD_Month = 0;
mAccBasePrice = 0;
mAccDepreciation = 0;
mAccRemainderPriceInitial = 0;
select D_Year, D_Month, AccBasePrice, AccDepreciation, AccRemainderPriceInitial
from AssetDepreciations
where (Parent_ID = :mAssetCard_ID) and ((D_Year*100+D_Month) <
(:AToYear*100+:AToMonth)) and (D_Year*100+D_Month) in
(select max(A.D_Year*100+A.D_Month) from AssetDepreciations A where (Parent_ID = :mAssetCard_ID) and
((A.D_Year*100+A.D_Month) < (:AToYear*100+:AToMonth)))
into :mAccD_Year, :mAccD_Month, :mAccBasePrice, :mAccDepreciation, :mAccRemainderPriceInitial;
if ((mAccD_Year = 0) or
((ib_decodedate_year(:mFileInDate$DATE) = mAccD_Year) and (ib_decodedate_month(:mFileInDate$DATE) = mAccD_Month) and
(mAccDepreciationBeginsAt = 1))) then begin
/* 1.cast podminky - Vypisuju knihu k mesici, ve kterem byla karta zarazena nebo byly hned po zarazeni odpisy preruseny, nebo je již karta zcela odepsána před zařazením do Gx (takže v Gx již nemá žádné odpisy)
2.cast podminky - Vypisuju knihu k mesici nasledujicim po mesici zarazeni nebo byly odpisy nejak preruseny.
Zaroven plati, ze ucetni odpisy se maji pocitat az od měsíce následujího po mesici zařazení ->
v prvnim mesici neni zadny odpis a zachovame se jako kdyby se jednalo o vypis v mesici zarazeni
*/
if (mAccRemainderPriceInitial > mAccBasePrice) then begin
mAccBasePrice = mAccRemainderPriceInitial;
end else begin
mAccBasePrice = mAccBasePriceInitial;
mAccRemainderPriceInitial = mAccBasePriceInitial;
end
if ((mAssetOrigin = 1) or (mAssetOrigin = 2)) then
/*Zohledníme snížení ceny, odpisy provedené mimo systém Gx*/
mAccRemainderPriceInitial = mAccRemainderPriceInitial + mTotalDecreaseOfBasePrice - mTotalDecreaseOfAccRemPrice - mTotalAccDepreciation;
end
mAccInteruption = 'N';
if (((AToMonth <> 1) and (((mAccD_Month + 1) <> AToMonth) or (mAccD_Year <> AToYear)))
or
((AToMonth = 1) and ((mAccD_Month <> 12) or ((mAccD_Year+1) <> AToYear)))) then begin
mAccInteruption = 'A';
end
/*Zjistime zda vypisujeme knihu k datu, kdy byly preruseny odpisy. */
if (mAccInteruption = 'A') then begin
/*Celkovy zamer teto vetve:
Vybereme doklady preceneni a cast.vyrazeni, ktere byly zauctovany od prvniho dne obdobi preruseni odpisu do data porizovani knihy zpetne k datu
------------------------------------------------------------
Nyni urcime rok a mesic data, od ktereho budeme doklady vybirat - rok a mesic prvniho dne
Pozn: Jedna se o rok a mesic nasledujici za odpisem, ktery byl vybran jako posledni pred prerusenim odpisu */
if (mAccD_Year = 0) then begin
/*Nyni urcime datum, od ktereho budeme doklady dohledavat - od data zařazení karty*/
mDateFrom = mFileInDate$DATE;
end else begin
if (mAccD_Month = 12) then
begin
mYearFrom = mAccD_Year + 1;
mMonthFrom = 1;
end
else
begin
mYearFrom = mAccD_Year;
mMonthFrom= mAccD_Month + 1;
end
/*Nyni urcime datum, od ktereho budeme doklady dohledavat*/
mDateFrom = ib_encodedate(:mYearFrom, :mMonthFrom, 1);
end
/*-------------------------------------------------------*/
/*Nyni vybereme doklady preceneni, ktere byly zauctovany od prvniho dne obdobi preruseni odpisu do data porizovani knihy zpetne k datu */
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetValueChanges
where (AssetCard_ID = :mAssetCard_ID) and
((IsValorisation = 'N') or ((IsValorisation = 'A') and (IsApplyInCard = 'A'))) and
(AccDate$DATE between :mDateFrom and :AToDate)
into :mSumVCAccBasePriceBefore, :mSumVCAccRemainderPriceBefore;
if (mSumVCAccBasePriceBefore is NULL) then
mSumVCAccBasePriceBefore = 0;
if (mSumVCAccRemainderPriceBefore is NULL) then
mSumVCAccRemainderPriceBefore = 0;
-- Vyber dokladu ZM, které nejsou primární. viz. sekce DANOVY ODPIS (POZ-1703/2014)
-- pouzivaji se stejne promenne jako pro sekci danovych odpisu
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetPutToEvidence
where (AssetCard_ID = :mAssetCard_ID) and (IsPrimary = 'N') and
(AccDate$DATE between :mDateFrom and :AToDate)
into :mTaxBasePriceComp, :mTaxRemainderPriceComp;
if (mTaxBasePriceComp is NULL) then
mTaxBasePriceComp = 0;
if (mTaxRemainderPriceComp is NULL) then
mTaxRemainderPriceComp = 0;
-- K dokladum preceneni pricteme zarazeni novych prvku
-- Secteme TZ a zmenu ceny a zarazení novych prvku
mSumVCAccBasePriceBefore = mSumVCAccBasePriceBefore + mTaxBasePriceComp;
mSumVCAccRemainderPriceBefore = mSumVCAccRemainderPriceBefore + mTaxRemainderPriceComp;
/*------------------------------------------------------------------------*/
/*Nyni vybereme doklady cast.vyrazeni, ktere byly zauctovany prvniho dne obdobi preruseni odpisu do data porizovani knihy zpetne k datu */
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetDiscards
where (AssetCard_ID = :mAssetCard_ID) and (IsFinal='N') and
(AccDate$DATE between :mDateFrom and :AToDate)
into :mSumDAccBasePriceBefore, :mSumDAccRemainderPriceBefore;
if (mSumDAccBasePriceBefore is NULL) then
mSumDAccBasePriceBefore = 0;
if (mSumDAccRemainderPriceBefore is NULL) then
mSumDAccRemainderPriceBefore = 0;
end
/* Odpisy nebyly preruseny. */
if (mAccInteruption = 'N') then begin
/* Musime napocitat ZMC a castecna vyrazeni od 1.mesice obdobi nasledujicim po mesici, ke kteremu jsme nalezli ucetni odpis do data porizovani knihy zpetne k datu */
/* ------------------------------------------------------------------------*/
/*Urcime prvni den mesice nasledujici za vybranym ucetnim odpisem */
select LastDayOfMonth from MonthLastDay(:mAccD_Month, :mAccD_Year) into :mLastDay;
-- Zacatek mesice vypisu knihy
mAccDocumentsDateFrom = ib_encodedate(:mAccD_Year, :mAccD_Month, :mLastDay) + 1;
/*------------------------------------------------------------------------*/
-- 0. Vybereme začátek období, do kterého patří datum, ke kterému vypisujeme knihu
mStartOfPeriodMonth = IB_DecodeDate_Month(mAccDocumentsDateFrom);
mStartOfPeriodYear = IB_DecodeDate_Year(mAccDocumentsDateFrom);
select DateFrom$DATE from Periods where
ID in (select Period_ID from AssetGetPeriodID(:mStartOfPeriodMonth, :mStartOfPeriodYear))
into :mStartOfPeriod;
-- 1. Posčítat TZ uplatněné do karty od začátku roku do předcházejícího měsíce
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetValueChanges
where (AssetCard_ID = :mAssetCard_ID) and
((IsValorisation = 'A') and (IsApplyInCard = 'A')) and
(AccDate$DATE between :mStartOfPeriod and (:mAccDocumentsDateFrom - 1))
into :mSumVCAccBasePriceBuffer, :mSumVCAccRemPriceBuffer;
if (mSumVCAccBasePriceBuffer is NULL) then
mSumVCAccBasePriceBuffer = 0;
if (mSumVCAccRemPriceBuffer is NULL) then
mSumVCAccRemPriceBuffer = 0;
-- Datum konce vypisu knihy
mAccDocumentsDateTo = AToDate;
-- 2.
-- Napocteme TZ do karty v mesici vypisu k datu vypisu
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetValueChanges
where (AssetCard_ID = :mAssetCard_ID) and
((IsValorisation = 'A') and (IsApplyInCard = 'A')) and
(AccDate$DATE between :mAccDocumentsDateFrom and :mAccDocumentsDateTo)
into :mSumVCAccBasePriceBefore, :mSumVCAccRemainderPriceBefore;
if (mSumVCAccBasePriceBefore is NULL) then
mSumVCAccBasePriceBefore = 0;
if (mSumVCAccRemainderPriceBefore is NULL) then
mSumVCAccRemainderPriceBefore = 0;
-- 3. Nastavíme, zda a jak budeme dohledaná TZ mSumVCAccBasePriceBuffer a mSumVCAccBasePriceBefore promítat do cen na kartě
-- Pokud TZ v predchozich mesicich nedosahlo limitu
if (mSumVCAccBasePriceBuffer < mOverValuationPriceLimit) then
begin
-- secteme predchozi a aktualni
mSumVCAccBasePriceBefore = mSumVCAccBasePriceBefore + mSumVCAccBasePriceBuffer;
mSumVCAccRemainderPriceBefore = mSumVCAccRemainderPriceBefore + mSumVCAccRemPriceBuffer;
-- mSumVCAccBasePriceBuffer překročilo limit -> budeme mSumVCAccBasePriceBefore a mSumVCAccRemainderPriceBefore promítat do cen karty
if ((mSumVCAccBasePriceBefore) >= mOverValuationPriceLimit) then
begin
-- ale je potreba odecist TZ z predchoziho obdobi ktere jdou ihned bez ohledu na cenu - ty jiz upravily vstupni cenu
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetValueChanges
where (AssetCard_ID = :mAssetCard_ID) and
(IsValorisation = 'A') and (IsApplyInCard = 'A') and (ApplyInCardRegardlessOfAmount = 'A') and
(AccDate$DATE between :mStartOfPeriod and (:mAccDocumentsDateFrom - 1))
into :mSumVCAccBasePriceBuffer, :mSumVCAccRemPriceBuffer;
if (mSumVCAccBasePriceBuffer is NULL) then
mSumVCAccBasePriceBuffer = 0;
if (mSumVCAccRemPriceBuffer is NULL) then
mSumVCAccRemPriceBuffer = 0;
-- odecteni TZ ktere v predchozich mesicich zmenily vstupni cenu
mSumVCAccBasePriceBefore = mSumVCAccBasePriceBefore - mSumVCAccBasePriceBuffer;
mSumVCAccRemainderPriceBefore = mSumVCAccRemainderPriceBefore - mSumVCAccRemPriceBuffer;
------------------------------------------------------------------------
mHlpNr = 0;
end
else
begin
-- suma TZ nepřekročila limit -> nebudeme žádná dohlednaná TZ zohledňovat do VC karty
/* Zohlednime jenom TZ v aktualnim mesici ktere jdou ihned bez ohledu na cenu */
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetValueChanges
where (AssetCard_ID = :mAssetCard_ID) and
((IsValorisation = 'A') and (IsApplyInCard = 'A')) and
(ApplyInCardRegardlessOfAmount = 'A') and
(AccDate$DATE between :mAccDocumentsDateFrom and :mAccDocumentsDateTo)
into :mSumVCAccBasePriceBefore, :mSumVCAccRemainderPriceBefore;
if (mSumVCAccBasePriceBefore is NULL) then
mSumVCAccBasePriceBefore = 0;
if (mSumVCAccRemainderPriceBefore is NULL) then
mSumVCAccRemainderPriceBefore = 0;
------------------------------------------------------------------------
end
end
else
begin
-- TZ v predchozich mesicich dosahlo limitu takze vsechna dalsi TZ upravuji vstupni cenu
-- mSumVCAccBasePriceBuffer překročilo limit -> budeme mSumVCAccBasePriceBefore a mSumVCAccRemainderPriceBefore promítat do cen karty
mHlpNr = 0;
end
/* Doklady skutecnych ZMC */
-- Dotace se zapocita podle data DepreciationCorrectDate$DATE je-li =0 pak k datu zarazeni, je-li <>0 pak k DepreciationCorrectDate$DATE
-- ktere musi byt >= mFileInDate$DATE
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetValueChanges
where (AssetCard_ID = :mAssetCard_ID) and
(
((IsValorisation = 'N') and (IsSubsidy = 'N') and (AccDate$DATE between :mAccDocumentsDateFrom and :mAccDocumentsDateTo)) or
((IsSubsidy = 'A') and ((DepreciationCorrectDate$DATE <> 0) and (DepreciationCorrectDate$DATE between :mAccDocumentsDateFrom and :mAccDocumentsDateTo)))
)
into :mHlpBase, :mHlpReminder;
if (mHlpBase is NULL) then
mHlpBase = 0;
if (mHlpReminder is NULL) then
mHlpReminder = 0;
-- Vyber dokladu ZM, které nejsou primární. viz. sekce DANOVY ODPIS (POZ-1703/2014)
-- pouzivaji se stejne promenne jako pro sekci danoych odpisu
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetPutToEvidence
where (AssetCard_ID = :mAssetCard_ID) and (IsPrimary = 'N') and
(AccDate$DATE between :mAccDocumentsDateFrom and :mAccDocumentsDateTo)
into :mTaxBasePriceComp, :mTaxRemainderPriceComp;
if (mTaxBasePriceComp is NULL) then
mTaxBasePriceComp = 0;
if (mTaxRemainderPriceComp is NULL) then
mTaxRemainderPriceComp = 0;
-- K dokladum preceneni pricteme zarazeni novych prvku
-- Secteme TZ a zmenu ceny a zarazeni novych prvku
mSumVCAccBasePriceBefore = mSumVCAccBasePriceBefore + mHlpBase + mTaxBasePriceComp;
mSumVCAccRemainderPriceBefore = mSumVCAccRemainderPriceBefore + mHlpReminder + mTaxRemainderPriceComp;
/*-------------------------------------------------------*/
/* Napocitame castecne vyrazeni */
select sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetDiscards
where (AssetCard_ID = :mAssetCard_ID) and (IsFinal='N') and
(AccDate$DATE between :mAccDocumentsDateFrom and :mAccDocumentsDateTo)
into :mSumDAccBasePriceBefore, :mSumDAccRemainderPriceBefore;
if (mSumDAccBasePriceBefore is NULL) then
mSumDAccBasePriceBefore = 0;
if (mSumDAccRemainderPriceBefore is NULL) then
mSumDAccRemainderPriceBefore = 0;
end
/*1.Pokud jsme nasli odpis posledni pred zacatkem preruseni, tak musime do vstupnich a */
/*zustatkovych cen promitnout preceneni a castecna vyrazeni provedena do data, ke kteremu knihu vypisujeme */
/*2.Pokud jsme NEnasli odpis posledni pred zacatkem preruseni, tak musime do vstupnich a */
/*zustatkovych cen promitnout preceneni a castecna vyrazeni provedena od dohledaneho odpisu do data, ke kteremu knihu vypisujeme */
if (mSumVCAccBasePriceBefore <> 0) then begin
mAccBasePrice = mAccBasePrice + mSumVCAccBasePriceBefore;
end
if (mSumVCAccRemainderPriceBefore <> 0) then begin
mAccRemainderPriceInitial = mAccRemainderPriceInitial + mSumVCAccRemainderPriceBefore;
end
if (mSumDAccBasePriceBefore <> 0) then begin
mAccBasePrice = mAccBasePrice - mSumDAccBasePriceBefore;
end
if (mSumDAccRemainderPriceBefore <> 0) then begin
mAccRemainderPriceInitial = mAccRemainderPriceInitial - mSumDAccRemainderPriceBefore;
end
/*Secteme odpisy do data, ke kteremu provadime vypis knihy zpetne k datu*/
select sum(TaxDepreciation), sum(AccDepreciation)
from AssetDepreciations
where (Parent_ID = :mAssetCard_ID) and ((D_Year*100+D_Month) <
(:AToYear*100+:AToMonth))
into :mSumTaxDepreciationAll, :mSumAccDepreciationAll;
if (mSumTaxDepreciationAll is NULL) then
mSumTaxDepreciationAll = 0;
mSumTaxDepreciationAll = mSumTaxDepreciationAll + mTotalTaxDepreciation + mTaxDeprecAccountedInLastYear;
if (mSumAccDepreciationAll is NULL) then
mSumAccDepreciationAll = 0;
mSumAccDepreciationAll = mSumAccDepreciationAll + mTotalAccDepreciation;
/* Napocitame všechna vyrazeni */
select sum(TaxBasePriceAmount), sum(TaxRemainderPriceAmount), sum(AccBasePriceAmount), sum(AccRemainderPriceAmount)
from AssetDiscards
where (AssetCard_ID = :mAssetCard_ID) and
(AccDate$DATE <= :AToDate)
into :mSumDTaxBasePriceAll, :mSumDTaxRemainderPriceAll, :mSumDAccBasePriceAll, :mSumDAccRemainderPriceAll;
if (mSumDTaxBasePriceAll is NULL) then
mSumDTaxBasePriceAll = 0;
if (mSumDTaxRemainderPriceAll is NULL) then
mSumDTaxRemainderPriceAll = 0;
if (mSumDAccBasePriceAll is NULL) then
mSumDAccBasePriceAll = 0;
if (mSumDAccRemainderPriceAll is NULL) then
mSumDAccRemainderPriceAll = 0;
/*Zohledníme snížení ceny, provedené mimo systém Gx - POZ-4013/2009*/
mSumDTaxBasePriceAll = mSumDTaxBasePriceAll + mTotalDecreaseOfBasePrice;
mSumDTaxRemainderPriceAll = mSumDTaxRemainderPriceAll + mTotalDecreaseOfTaxRemPrice;
mSumDAccBasePriceAll = mSumDAccBasePriceAll + mTotalDecreaseOfBasePrice;
mSumDAccRemainderPriceAll = mSumDAccRemainderPriceAll + mTotalDecreaseOfAccRemPrice;
/*Nyni se rozhodneme zda zapocitame nebo nezapocitame velikosti ucetniho a danoveho odpisu
do celkove sumy odpisu za danou kartu.
- Nejdrive vybereme vetu s odpisem pro mesic, ke kteremu delame vypis knihy. A tento
odpis do celkove sumy zahrneme za nasledujicich podminek:
A) odpis neni zautovany - vypis knihy se provadi k poslednimu dni mesici. Bere se tedy
******************** jako kdyby byl odpis zauctovan k poslednimu dni mesice
B) odpis je zauctovan - vypis knihy se provadi k datu nasledujici po dni k jakemu byl
****************** odpis zauctovan. (Pokud se provadi vypis ke dni pred zauctovanim
odpisu, odpis se nebude zapocitavat)
- Pokud takovy odpis neexistuje, tak to znamena, ze odpisy byly preruseny a ze tedy
nijak neovlivni celkovou vysi odpisu.
*/
/*Zjistime cislo posledniho dne mesice, ke kteremu provadime vypis */
select LastDayOfMonth from MonthLastDay(:AToMonth, :AToYear) into :mLastDay;
/*Vybereme odpis za mesic ke kteremu provadime vypis za podminky ze je bud nezauctovan
nebo pro nej plati podminka pro zauctovani vysvetlena drive v komentari*/
mExist = 0;
for
select AD.TaxDepreciation, AD.AccDepreciation, AD.AccDate$DATE
from AssetDepreciations AD
left join DocQueues DQ on DQ.ID = AD.DocQueue_ID
where
(AD.Parent_ID = :mAssetCard_ID) and
((AD.D_Year*100+AD.D_Month) = (:AToYear*100+:AToMonth)) and
(
(AD.AccDate$DATE = 0) or
/* pro G1 se nekontroluje GeneralLedger protoze je prazdny */
/* G1 se zde zjisti podle existence typu prubezna polozka v incometypes */
(exists(select 1 from IncomeTypes where Category<0 and BookColumn<0)) or
/*Tady řešíme ostatní Gx*/
/*Pokud se řada účtuje, vezme se datum zaúčtování v deníku (resp.žádostí)*/
(
(DQ.ToAccount = 'A') and
(
(AD.ID in (select REL.LeftSide_ID from Relations REL, GeneralLedger GL
where
(REL.RightSide_ID = GL.ID) and (REL.Rel_Def = 34) and
(REL.LeftSide_ID = AD.ID) and (GL.AccDate$DATE <= :AToDate))
)
or
-- Kdyz je odpis zauctovan ale neni uctovani ulozeno (nesmi se nalezt v deniku - stane se kdyz
-- odpis = nadlimitni slozce odpisu a uctuje se do jednoho ucetniho zapisu
-- (stejne ucty, text atd..), pak se zachovame jako kdyby se odpis v deniku nalezl k vypisovanemu datu
-- POZ-3669/2014
(((AD.AccDocQueue_ID IS not NULL) and (AD.OrdNumber > 0) and (AD.AccDate$DATE <= :AToDate)) and
AD.ID not in (select REL.LeftSide_ID from Relations REL, GeneralLedger GL
where
(REL.RightSide_ID = GL.ID) and (REL.Rel_Def = 34) and
(REL.LeftSide_ID = AD.ID) and (GL.AccDate$DATE > :AToDate))
)
)
) or
/*Pokud se řada odpisů NEúčtuje, vezme se datum z odpisu (tohle datum uživatel nijak neovlivní, je tam vždy konec měsíce*/
(
(DQ.ToAccount = 'N') and (AD.AccDate$DATE <= :AToDate)
)
)
into :mActualTaxDepreciation, :mActualAccDepreciation, :mAccDate$Date
do begin
mExist = 1;
end
/*Nyni osetrime stav kdy se bud odpis nenasel - odpisy jsou preruseny nebo pokud
odpis neni zauctovan*/
if (mExist = 0) then
begin
/*Opis se nenasel */
mActualTaxDepreciation = 0;
mActualAccDepreciation = 0;
end
else
begin
if (mAccDate$DATE = 0) then begin
if (ib_decodedate_day(:AToDate) < :mLastDay) then begin
/*Odpis neni zauctovan a vypis se nedela k poslednimu dni v mesici*/
mActualTaxDepreciation = 0;
mActualAccDepreciation = 0;
end
end
end
/*Velikost odpisu promitneme do celkove sumy odpisu */
mSumTaxDepreciationAll = mSumTaxDepreciationAll + mActualTaxDepreciation;
mSumAccDepreciationAll = mSumAccDepreciationAll + mActualAccDepreciation;
/*Velikost odpisu promitneme do celkove sumy odpisu za aktuální období*/
mSumActPeriodTaxDepreciation = mSumActPeriodTaxDepreciation + mActualTaxDepreciation;
mSumActPeriodAccDepreciation = mSumActPeriodAccDepreciation + mActualAccDepreciation;
/* Naplnime navratove hodnoty pro danou kartu*/
ID = mAssetCard_ID;
outTaxRemainderPrice = mTaxRemainderPriceInitial - mActualTaxDepreciation; /*Zůstatkovou cenu ještě snížíme o odpis za aktuální měsíc. (pokud se má odpis za aktuální měsíc brát v úvahu viz výpočet mActualTaxDepreciation)*/
outAccRemainderPrice = mAccRemainderPriceInitial - mAccDepreciation - mActualAccDepreciation; /*Vezmu zůstatkovou cenu na začátku předchozího měsíce a snížím ji o odpis v minulém měsíci. Tím dostanu zůstatkovou cenu na konci minulého měsíce. Potom ještě snížíme o odpis za aktuální měsíc.(pokud se má odpis za aktuální měsíc brát v úvahu viz výpočet mActualAccDepreciation)*/
outSumTaxDepreciation = mSumTaxDepreciationAll;
outSumAccDepreciation = mSumAccDepreciationAll;
outSumPeriodTaxDepreciation = mSumActPeriodTaxDepreciation;
outSumPeriodAccDepreciation = mSumActPeriodAccDepreciation;
outSumTaxAccumDeprec = mSumTaxDepreciationAll + mSumDTaxRemainderPriceAll - mSumDTaxBasePriceAll; /*Oprávky jsou odpisy + vyřazení */
outSumAccAccumDeprec = mSumAccDepreciationAll + mSumDAccRemainderPriceAll - mSumDAccBasePriceAll;
outStatus = -1;
if (AStatus = 1) then
begin
outStatus = 1; /* Vybíráme jen karty v evidenci, takže karta může být jenom v evidenci */
end
else
begin
if (AStatus = 2) then
begin
outStatus = 2; /* Vybíráme jen karty vyřazené, takže karta může být jenom vyřazená */
end
else
begin
/* Vybíráme karty v evidenci nebo vyřazené -> musíme určit stav karty */
mIsOnRecord = 'N';
select IsOnRecord from AssetCardIsOnRecord(:AToDate, :mFileInDate$DATE, :mDiscardDate$DATE) into :mIsOnRecord;
if ('A' = mIsOnRecord) then
outStatus = 1;
/* Pokud karta není v evidenci, zjistíme jestli není vyřazená */
if (outStatus = -1) then begin
select IsNotOnRecord from AssetCardIsNotOnRecord(:AToDate, :mDiscardDate$DATE) into :mIsNotOnRecord;
if ('A' = mIsNotOnRecord) then
outStatus = 2;
end
end
end
if (outStatus = 2) then begin
outTaxBasePrice = 0;
outAccBasePrice = 0;
end else begin
if ((mBasePriceLimit <> 0) and (mTaxBasePrice > mBasePriceLimit)) then begin
outTaxBasePrice = mBasePriceLimit;
end else begin
outTaxBasePrice = mTaxBasePrice;
end
outAccBasePrice = mAccBasePrice;
end
if (outStatus = 2) then begin
/* Pokud je karta k datu výpisu zcela vyřazená, tak by měla být zůstatková cena 0.
K té nule se dopočítám tak, že :
********************************
1. Pokud se provádí výpis až po měsíci vyřazení:
od zůstatkové ceny na posledním odpisu odečtu
částku vyrovnání oprávek. Pokud pak nevyjde zůstatková cena nula, tak je něco špatně.
2. V měsíci vyřazení. Dám tam natvrdo nulu. To je proto, že v měsíci vyřazení bude zůstatková
cena majetku rovna výši vyřazovacího dokladu až k datu zaúčtování odpisu. My ale chceme,
aby byla zůstatková cena 0 hned po vyřazení majetku.
Poznámka:
Šlo by samozřejmě dávat zůstakovou cenu 0 natvrdo vždy, ale takhle se alespoň v případě
"1." odkontroluje, zda je částka vyřazovacího dokladu správně.
*/
mSumFDTaxRemainderPrice = 0;
mSumFDAccRemainderPrice = 0;
if (ib_decodedate_month(:mDiscardDate$DATE) = AToMonth) then begin
outTaxRemainderPrice = 0;
outAccRemainderPrice = 0;
end else begin
select sum(TaxRemainderPriceAmount), sum(AccRemainderPriceAmount)
from AssetDiscards
where (AssetCard_ID = :mAssetCard_ID) and (IsFinal='A')
into :mSumFDTaxRemainderPrice, :mSumFDAccRemainderPrice;
if (mSumFDTaxRemainderPrice is NULL) then
mSumFDTaxRemainderPrice = 0;
if (mSumFDAccRemainderPrice is NULL) then
mSumFDAccRemainderPrice = 0;
outTaxRemainderPrice = outTaxRemainderPrice - mSumFDTaxRemainderPrice;
outAccRemainderPrice = outAccRemainderPrice - mSumFDAccRemainderPrice;
end
end
suspend;
end
END