BEGIN
-- *****************************************************************************
-- DRC na vstupu pro doklady FP, PV, DZP, JCD */ **
-- *****************************************************************************
mSectionID = 'B1';
mReverseSectionID = 'C2';
SELECT country_id FROM globdata INTO :mLocalCountry_ID;
IF (mLocalCountry_ID = '') THEN
mLocalCountry_ID = :AVATCountry_ID;
-- *****************************************************************************
FOR
SELECT
dq.documenttype DocumentType,
b.ID Doc_ID,
b.firm_id Firm_ID,
f.vatidentnumber VATIdentNumber,
b.vatdate$date VATDate$DATE,
dq.code || '-' || CAST(b.ordnumber AS VARCHAR(10)) || '/' || pe.code DocNumber,
b.sdocument_id Source_ID,
b.sdocumenttype SourceDocType,
'' SourceDocNumber,
a.VATRate VATRate,
CASE :mLocalCountry_ID
WHEN :AVATCountry_ID
THEN a.LocalBaseAmount
ELSE a.BaseAmount
END BaseAmount,
CASE :mLocalCountry_ID
WHEN :AVATCountry_ID
THEN a.LocalVATAmount
ELSE a.VATAmount
END VATAmount,
0 AllowanceAmount,
NULL DRC_ID, '' DRCCode, 0 DRCQuantity, '' DRCQUnit,
A.vatindex_id VATIndex_ID,
vi.allowancevatindex_id AllowanceVATIndex_ID,
b.vatdeductiondate$date VATDeductionDate,
B.VATReportPreference VATReportPreference,
dq.Code || '-' || IB_LPad(CAST(b.OrdNumber AS VARCHAR(8)), 8, '0') || '/' || pe.Code SortDocNumber,
A.division_id Division_ID, A.busorder_id BusOrder_ID, A.bustransaction_id BusTransaction_ID, A.busproject_id BusProject_ID,
VI.IsReverse IsReverse
FROM
reversechargedeclarations2 a
JOIN reversechargedeclarations b ON b.id = a.parent_id
JOIN firms f ON f.id = b.firm_id
JOIN periods pe ON pe.id = b.period_id
JOIN docqueues dq ON dq.id = b.docqueue_id
-- LEFT JOIN drcarticles da ON da.id = a.drcarticle_id
JOIN vatindexes vi ON vi.id = a.vatindex_id
WHERE
((b.sdocumenttype = '04') OR (b.sdocumenttype = '06') OR (b.sdocumenttype = '64') OR (b.sdocumenttype = '12'))
AND b.vatcountry_id=:avatcountry_id
AND a.vatindex_id IS NOT NULL
AND (
(b.vatdate$date >= :avatdatefrom AND b.vatdate$date < :avatdateto) OR
(vi.allowancevatindex_id IS NOT NULL AND b.vatdeductiondate$date >= :avatdatefrom AND b.vatdeductiondate$date < :avatdateto)
)
AND ((:afirmselid = ''
OR (b.firm_id IN (SELECT obj_id FROM seldat WHERE sel_id = :afirmselid))
OR (b.firm_id IN (SELECT id FROM firms WHERE firm_id IN (SELECT obj_id FROM seldat WHERE sel_id = :afirmselid)))
))
INTO
:documenttype, :doc_id, :firm_id, :vatidentnumber, :vatdate$date,
:docnumber, :source_id, :sourcedoctype, :sourcedocnumber, :vatrate, :baseamount, :vatamount, :allowanceamount,
:drc_id, :drccode, :drcquantity, :drcqunit,
:mvatindex_id,
:mallowancevatindex_id,
:mvatdeductiondate, :mVATReportPreference, :SortDocNumber,
:mdivision_id, :mbusorder_id, :mbustransaction_id, :mbusproject_id, :mIsReverse
DO BEGIN
EXECUTE PROCEDURE checkdivisionandbusx
:mdivision_id, :adivisionsselid, :adivisionswithchilds,
:mbusorder_id, :abusordersselid, :abusorderswithchilds,
:mbustransaction_id, :abustransactionsselid, :abustransactionswithchilds,
:mbusproject_id, :abusprojectsselid, :abusprojectswithchilds
RETURNING_VALUES
:msuspend;
IF (msuspend > 0) THEN
BEGIN
SELECT MAX(ID) FROM VATSummaryDefinitions2 WHERE vatindex_id=:mVATIndex_ID AND parent_id=:AVATDefinitionID INTO :DefinitionRow_ID;
SELECT COALESCE(MAX(TaxLineNumber),''), COALESCE(MAX(VATReportMode),'')
FROM VATSummaryDefinitions2
WHERE (vatindex_id = :mAllowanceVATIndex_ID AND parent_id=:AVATDefinitionID)
INTO :taxlinenumber, :vatreportmode;
SectionID = mSectionID;
VarSymbol = '';
IF (mIsReverse = 'A') THEN
BEGIN
SectionID = mReverseSectionID;
END
IF (mVATReportPreference <> '') THEN
BEGIN
SectionID = :mVATReportPreference;
END
if (:sourcedoctype = '04') then
begin
select externalnumber, vatadmitdate$date, VATReportReference from receivedinvoices
where id = :source_id into :sourcedocnumber, :mvatadmitdate, :mVATReportReference;
end
if (:sourcedoctype = '06') then
begin
select externalnumber, vatadmitdate$date, VATReportReference from cashpaid
where id = :source_id into :sourcedocnumber, :mvatadmitdate, :mVATReportReference;
end
if (:sourcedoctype = '64') then
begin
select externalnumber, vatadmitdate$date, VATReportReference from vatreceiveddinvoices
where id = :source_id into :sourcedocnumber, :mvatadmitdate, :mVATReportReference;
end
if (:sourcedoctype = '12') then
begin
select externalnumber, vatadmitdate$date, VATReportReference from customsdeclarations
where id = :source_id into :sourcedocnumber, :mvatadmitdate, :mVATReportReference;
end
sourcedocnumber = COALESCE (:sourcedocnumber, '');
IF ((SourceDocNumber = '') AND (mVATReportReference <> '')) THEN
BEGIN
sourcedocnumber = COALESCE (:mVATReportReference, '');
END
allowanceamount = 0;
IF ((mallowancevatindex_id IS NOT NULL) AND
(mvatdeductiondate >= :avatdatefrom AND mvatdeductiondate < :avatdateto))
THEN BEGIN
allowanceamount = :vatamount;
END
IF ((mallowancevatindex_id IS NOT NULL) AND
((vatdate$date < :avatdatefrom) OR (vatdate$date >= :avatdateto)))
THEN BEGIN
IF (taxlinenumber = '') THEN
definitionrow_id = NULL;
END
if (SectionID = 'C2') then
begin
DocNumber = :SourceDocNumber;
SourceDocNumber = :mVATReportReference;
end
SUSPEND;
END
END
-- *****************************************************************************
END