set term ^;

create or alter procedure FISCALIZE_PAYMENT$FISC (
    ID_DOC_HEAD integer)
returns (
    FISC_SUM numeric(15,4),
    FISC_TEXT varchar(30),
    FISC_GRP varchar(10))
AS
begin
  /*  Description:
  ||
  */
  fisc_sum = null;
  fisc_text = null;
  fisc_grp = null;

  for
    select PT.PAYMENT_NAME, PT.FISCAL_ID, sum(SP.PAYMENT_VALUE)
    from   SMETKA_PAYMENT SP
           join SMETKA_HEAD SH on SH.ID_SMETKA_HEAD = Sp.ID_SMETKA_HEAD
           join NOM_PAYMENT_TYPE PT on PT.ID_PAYMENT_TYPE = SP.ID_PAYMENT_TYPE
    where  SH.ID_SMETKA_HEAD = :id_doc_head
           and SP.ID_PAYMENT_VOID is null
           and SH.STATUS = 2
    group by 1, 2
    into   :fisc_text, :fisc_grp, :fisc_sum
  do begin
    if (:FISC_SUM > 0) then suspend;
  end
end
^

create or alter procedure FISCALIZE_LINE$FISC (
    ID_DOC_HEAD integer,
    DETAIL_LEVEL varchar(20),
    ADDITIONAL_PAYMENT_TEXT varchar(30) = null)
returns (
    FISC_PRICE numeric(15,4),
    FISC_QTY numeric(15,4),
    FISC_TEXT varchar(30),
    FISC_GRP varchar(10),
    TRANSACTION_VALUE numeric(15,4))
AS
  declare variable fisc_status integer;
begin
  /*  Description:
  ||   DETAIL_LEVEL = {1 group,  2 detail}

  function fiscalize(const text, taxGroup: string; qty, price, correction: Double): integer;
  function payment(const text, payType: string; summ: Double): integer;

  */
  fisc_price = null;
  fisc_text = null;
  fisc_grp = null;
  transaction_value = null;

/* PRINT THE DOC NUMBER AS FREE TEXT. NON SUPPORTED FOR NOW!
  fisc_text = :doc_num;
  suspend;
*/

  select max(SP.FISC_STATUS)
  from   SMETKA_PAYMENT SP
         join SMETKA_HEAD SH on SH.ID_SMETKA_HEAD = Sp.ID_SMETKA_HEAD
         join NOM_PAYMENT_TYPE PT on PT.ID_PAYMENT_TYPE = SP.ID_PAYMENT_TYPE
  where  SH.ID_SMETKA_HEAD = :id_doc_head
         and SP.ID_PAYMENT_VOID is null
         and SH.STATUS = 2
  into   :fisc_status;

  if (:fisc_status <> 1) then exit;

  if (:detail_level = 'group') then begin
    execute procedure SYSTEM_GET_TEXT(null, 'FISC_TEXT')
    returning_values  :fisc_text;
    for
      select VG.VAT_CODE, roundto(sum(SL.NETO_SUM), 0.01)
      from   SMETKA_HEAD SH
             join BILL_NETO SL on SL.ID_SMETKA_HEAD = SH.ID_SMETKA_HEAD
             --join SMETKA_LINE SL on SL.ID_SMETKA_HEAD = SH.ID_SMETKA_HEAD
             left outer join STOCK S on S.ID_STOCK = SL.ID_STOCK
             left outer join NOM_VAT_GROUP VG on VG.ID_VAT = S.ID_VAT_GROUP
      where  SH.ID_SMETKA_HEAD = :id_doc_head
             and SH.STATUS = 2
             and SL.STATUS > 0
      group by 1
      into   :fisc_grp, :transaction_value
    do begin
      fisc_qty = 1;
      fisc_price =  :transaction_value;
      suspend;
    end
  end else begin
    for
      select VG.VAT_CODE, S.STOCK_NAME, roundto(sum(SL.NETO_SUM), 0.01), sum(SL.QTY)
      from   SMETKA_HEAD SH
             join BILL_NETO SL on SL.ID_SMETKA_HEAD = SH.ID_SMETKA_HEAD
             --join SMETKA_LINE SL on SL.ID_SMETKA_HEAD = SH.ID_SMETKA_HEAD
             left outer join STOCK S on S.ID_STOCK = SL.ID_STOCK
             left outer join NOM_VAT_GROUP VG on VG.ID_VAT = S.ID_VAT_GROUP
      where  SL.ID_SMETKA_HEAD = :id_doc_head
             and SH.STATUS = 2
             and SL.STATUS > 0
      group by 1, 2
      into   :fisc_grp, :fisc_text, :transaction_value, :fisc_qty
    do begin
      fisc_price =  roundto(:transaction_value / :fisc_qty, 0.01);
      if (:FISC_PRICE <> 0) then suspend;
    end
  end

  fisc_price = null;
  select SH.SURCHARGE_SUM, D.DISCOUNT_NAME, 'SRV', SH.SURCHARGE_SUM, 1
  from   SMETKA_HEAD SH
         join NOM_DISCOUNT D on D.ID_DISCOUNT = SH.ID_SURCHARGE
  where  SH.ID_SMETKA_HEAD = :id_doc_head
  into   :fisc_price, :fisc_text, :fisc_grp, :transaction_value, :FISC_QTY;
  if (:FISC_PRICE <> 0) then suspend;
end
^

set term ;^


commit work;

