/*ALIAS>prop_db*/


/*SQLStart>1506*/
alter TRIGGER INDETAIL_BI
AS
declare variable OpID integer;
declare variable OpUnitID integer;
declare variable PayDate date;
declare variable PayType char(1);
declare variable PayWay char(1);
declare variable Anul char(1);
declare variable DDS numeric(15,3);
declare variable DDSSum numeric(15, 3);
declare variable cnt integer;
BEGIN
    NEW.ID_NoFactorSum  = NEW.ID_Sum;

    select IN_OperatorID, IN_PayDate, IN_PayType, IN_PayDDS, IN_PayDDSSum, IN_OperatorUnitID, IN_PayWay, IN_Anul
    from Invoices 
    where IN_ID = NEW.ID_ID
    into :OpID, :PayDate, :PayType, :DDS, :DDSSum, :OpUnitID, :PayWay, :Anul;

    select count(*) from daypayments 
    where (dp_inid = NEW.ID_ID) and (DP_FLAG = "D")
    into :cnt;

    execute procedure DayPaymentsAdd(:OpID, :PayDate, NEW.ID_AccNo, NEW.ID_PartNo,
            New.ID_SUM, :PayType, NEW.ID_ID, NEW.ID_BIN, NEW.ID_UNITID, :PayWay, :Anul, :PayType, :OpUnitID);

    /* otrazqwa w daypayments DDSto */
    if ((New.ID_DDS <> 0) and (New.ID_DDS is not null) and (cnt = 0) ) then
      execute procedure DayPaymentsAdd(:OpID, :PayDate, NEW.ID_AccNo_D, NEW.ID_PartNo_D,
                                       DDS, 'D', NEW.ID_ID, NEW.ID_BIN_D, NEW.ID_UNITID_D, :PayWay, :Anul, :PayType, :OpUnitID);

    /* otrazqwa w daypayments lihvite */
    if ((New.ID_Lihvi <> 0) and (New.ID_Lihvi is not null)) then
      execute procedure DayPaymentsAdd(:OpID, :PayDate, NEW.ID_AccNo_L, NEW.ID_PartNo_L,
                                        New.ID_Lihvi, 'L', NEW.ID_ID, NEW.ID_BIN_L, NEW.ID_UNITID_L, :PayWay, :Anul, :PayType, :OpUnitID);

    if ( ( New.ID_DDS <> 0) and (DDSSum <> 0) ) then
    begin
      New.ID_DDS = (New.ID_SUM / DDSSum) * DDS;
      New.ID_ALL = (NEW.ID_SUM + NEW.ID_LIHVI + NEW.ID_DDS);
    end
END
/*SQLStop>1506*/

