BEGIN
-- *****************************************************************************
-- Doklady POS - vsechny sumarni doklady do D1
-- *****************************************************************************
mSectionID = 'D1';
-- *****************************************************************************
SELECT country_id FROM globdata INTO :mLocalCountry_ID;
IF (mLocalCountry_ID = '') THEN
mLocalCountry_ID = :AVATCountry_ID;
-- POS ma jen tuzemske doklady
IF (NOT (avatcountry_id = mlocalcountry_id)) THEN
EXIT;
-- *****************************************************************************
/* radky POS */
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,
NULL Source_ID,
'' SourceDocType,
'' SourceDocNumber,
A.vatrate VATRate, A.localtamountwithoutvat BaseAmount, A.localtamount-A.localtamountwithoutvat VATAmount, 0 AllowanceAmount,
NULL DRC_ID, '' DRCCode, 0 DRCQuantity, '' DRCQUnit,
a.VATIndex_ID VATIndex_ID,
0 RowType,
vi.income Income,
'' ExternalNumber,
A.quantity Quantity,
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
FROM
possummareddocuments2 a
JOIN possummareddocuments 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
JOIN vatindexes vi ON vi.id = a.vatindex_id
WHERE
a.vatindex_id IS NOT NULL
AND (b.vatdocument='A')
--AND (b.simplifiedVATDocument<>'A')
--AND (a.vatmode = 0)
AND (a.vatrate > 0)
--AND (a.rowtype IN (1,2,3,4,5))
--AND ((b.tradetype=1) OR (b.tradetype=4))
AND (b.vatdate$date >= :avatdatefrom AND b.vatdate$date < :avatdateto)
--AND (b.vatcountry_id = :avatcountry_id)
--AND a.drcarticle_id IS NULL
--AND b.isfiscalized='A'
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,
:mrowtype, :mincome, :mexternalnumber, :mquantity, :SortDocNumber,
:mdivision_id, :mbusorder_id, :mbustransaction_id, :mbusproject_id
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), COALESCE(MAX(TaxLineNumber),''), COALESCE(MAX(VATReportMode),'')
FROM VATSummaryDefinitions2
WHERE (vatindex_id = :mvatindex_id AND parent_id=:AVATDefinitionID)
INTO :definitionrow_id, :taxlinenumber, :vatreportmode;
SectionID = mSectionID;
VarSymbol = '';
SUSPEND;
END
END
-- *****************************************************************************
/* radky POS vraceni */
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,
NULL Source_ID,
'' SourceDocType,
'' SourceDocNumber,
A.vatrate VATRate, A.localtamountwithoutvat BaseAmount, A.localtamount-A.localtamountwithoutvat VATAmount, 0 AllowanceAmount,
NULL DRC_ID, '' DRCCode, 0 DRCQuantity, '' DRCQUnit,
a.VATIndex_ID VATIndex_ID,
0 RowType,
vi.income Income,
'' ExternalNumber,
A.quantity Quantity,
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
FROM
possummareddocuments3 a
JOIN possummareddocuments 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
JOIN vatindexes vi ON vi.id = a.vatindex_id
WHERE
a.vatindex_id IS NOT NULL
AND (b.vatdocument='A')
--AND (b.simplifiedVATDocument<>'A')
--AND (a.vatmode = 0)
AND (a.vatrate > 0)
--AND (a.rowtype IN (1,2,3,4,5))
--AND ((b.tradetype=1) OR (b.tradetype=4))
AND (b.vatdate$date >= :avatdatefrom AND b.vatdate$date < :avatdateto)
--AND (b.vatcountry_id = :avatcountry_id)
--AND a.drcarticle_id IS NULL
--AND b.isfiscalized='A'
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,
:mrowtype, :mincome, :mexternalnumber, :mquantity, :SortDocNumber,
:mdivision_id, :mbusorder_id, :mbustransaction_id, :mbusproject_id
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), COALESCE(MAX(TaxLineNumber),''), COALESCE(MAX(VATReportMode),'')
FROM VATSummaryDefinitions2
WHERE (vatindex_id = :mvatindex_id AND parent_id=:AVATDefinitionID)
INTO :definitionrow_id, :taxlinenumber, :vatreportmode;
SectionID = mSectionID;
VarSymbol = '';
SUSPEND;
END
END
-- *****************************************************************************
-- Doklady POS uctenky kde firma ma vyplneno IC DPH - jdou do A1 a C1
-- *****************************************************************************
FOR
SELECT
dq.documenttype DocumentType,
b.ID Doc_ID,
b.firm_id Firm_ID,
f.vatidentnumber VATIdentNumber,
ps.vatdate$date VATDate$DATE,
dq.code || '-' || CAST(b.ordnumber AS VARCHAR(10)) || '/' || pe.code DocNumber,
NULL Source_ID,
'' SourceDocType,
b.RefundedDocName SourceDocNumber,
A.vatrate VATRate, A.localtamountwithoutvat BaseAmount, A.localtamount-A.localtamountwithoutvat VATAmount, 0 AllowanceAmount,
NULL DRC_ID, '' DRCCode, 0 DRCQuantity, '' DRCQUnit,
VI.ID VATIndex_ID,
0 RowType,
vi.income Income,
'' ExternalNumber,
A.quantity Quantity,
dq.Code || '-' || IB_LPad(CAST(b.OrdNumber AS VARCHAR(8)), 8, '0') || '/' || pe.Code SortDocNumber,
VP.division_id Division_ID, B.busorder_id BusOrder_ID, B.bustransaction_id BusTransaction_ID, B.busproject_id BusProject_ID,
B.RefundedDoc_ID
FROM
posdocuments2 a
JOIN posdocuments b ON b.id = a.parent_id
join possummareddocuments ps on ps.id = b.possummareddocument_id
join virtualposs vp on vp.id = b.virtualpos_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
JOIN vatindexes vi ON vi.id = (select max(vatindex_id) from posvatindexes where vatrate_id = a.vatrate_id)
WHERE
(F.VATIdentNumber <> '')
AND (b.SimplifiedVATDocument = 'N')
AND (ps.vatdocument='A')
AND (a.vatrate > 0)
AND (ps.vatdate$date >= :avatdatefrom AND ps.vatdate$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,
:mrowtype, :mincome, :mexternalnumber, :mquantity, :SortDocNumber,
:mdivision_id, :mbusorder_id, :mbustransaction_id, :mbusproject_id,
:mRefundedDoc_ID
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
IF (:VATIdentNumber <> '') THEN
BEGIN
SELECT MAX(ID), COALESCE(MAX(TaxLineNumber),''), COALESCE(MAX(VATReportMode),'')
FROM VATSummaryDefinitions2
WHERE (vatindex_id = :mvatindex_id AND parent_id=:AVATDefinitionID)
INTO :definitionrow_id, :taxlinenumber, :vatreportmode;
IF ((mRefundedDoc_ID = '') OR (mRefundedDoc_ID IS Null)) THEN
BEGIN
SectionID = 'A1';
END
ELSE
BEGIN
SectionID = 'C1';
END
VarSymbol = '';
SUSPEND;
END
END
END
-- *****************************************************************************
END