SELECT OUTACCT.FYEAR, OUTACCT.FPERIOD, Material.FMasterID, process.FENTRYID fprocessentryid, seq.FEntryId fseqentryid, seq.FSEQ,
CASE seq.FBILLTYPEID WHEN 'd040f688e69b406186aecfb5d9a86101' THEN '0' ELSE seq.FINOUTINDEX END finoutindex, FOUTINSTOCKTYPE,
' ' fvoucherbillno, seq.FBILLTYPEID fsrcbilltypeid, BillType_L.FName fsrcbilltype, FBILLFROMID, FBILLID, FBillEntryId, seq.FBILLNO,
FBILLSEQID, bom.FNumber fbomid, dim.FMtoNo fplantrack, dim.FPROJECTNO fitemnumber, seq.FBASEUNITID, unitL.FName funitid,
 ISNULL(batch.FNumber, ' ') flotno, dim.FAuxPropId fassipropertyid, dim.FCARGOOWNERTYPE fownertype, N' ' fseqno, seq.FQTY fbaseqty, FACCTGPRICE fprice,
  CASE seq.FBILLTYPEID WHEN 'd040f688e69b406186aecfb5d9a86101' THEN (0 - FACCTGAMOUNT) ELSE FACCTGAMOUNT END famount,
  acctgRange.FNumber facctgrangeno, acctgRangeL.FName facctgrangename, dim.FAuxPropId faidpropertyid, FBillDate fbusinessdate,
  Material.Fnumber fmaterialno, MaterialL.FName fmaterialname, MaterialL.FSPECIFICATION fmodel, OwnerOrgs.FNumber fownerno,
   OwnerOrgsL.FName fownername, StockOrgs.FNumber fstockorgno, StockOrgsL.FName fstockorgname, stock.FNumber fstockno,
    stockL.FName fstockname, dim.FStockLocId fstockplaceid, bin.FNumber fstockplaceno, binL.FName fstockplacename,
 enumItemL.FCAPTION fdocumentstatus, N' ' faidpropertyname, seq.FTransferBizType, seq.FTransferDirect, stockdim.FStockState,
 dim.FSTOCKSTATUSID, cald.FACCTSYSTEMID facctsystemid, cald.FFinOrgID facctorgid, cald.FACCTPOLICYID facctpolicyid,
  0 fisvoucher, seq.FTransferDirect, unit.FPRECISION fqtydigits, seq.FGiveAway, '0' fhasadjustbill, ' ' fadjustbillno,
  ISNULL(dept_L.FName, N' ') fdeptid, ISNULL(gory_L.FName, N' ') fcategoryid, '0' forderby, seq.FISSETTLE
  FROM T_HS_OutInStockSeq seq
  INNER JOIN T_HS_InivStockDimension dim ON seq.FDimeEntryId = dim.FEntryId
  LEFT OUTER JOIN T_BD_DEPARTMENT_L dept_L ON (seq.FDEPTID = dept_L.FDEPTID AND dept_L.FLOCALEID = 2052)
   INNER JOIN T_HS_AcctgProcess process ON process.FEntryId = seq.FPROCESSENTRYID
   INNER JOIN T_HS_StockDimension stockDim ON process.FDimeEntryId = stockDim.FEntryId
   INNER JOIN T_HS_OUTACCTG outacct ON outacct.FID = process.FID
   INNER JOIN T_HS_CALDIMENSIONS cald ON outacct.FDIMENSIONID = cald.FDIMENSIONID
   INNER JOIN T_HS_ACCTGRANGE acctgRange ON acctgRange.FACCTGRANGEID = stockDim.FAcctgRangeId
   LEFT OUTER JOIN T_HS_ACCTGRANGE_L acctgRangeL ON (acctgRangeL.FACCTGRANGEID = stockDim.FAcctgRangeId AND acctgRangeL.FLOCALEID = 2052)
    LEFT OUTER JOIN T_BAS_BILLTYPE_L BillType_L ON (BillType_L.FBILLTYPEID = seq.FBILLTYPEID AND BillType_L.FLOCALEID = 2052)
    INNER JOIN t_bd_Material material ON material.fmaterialID = dim.fmaterialID
    INNER JOIN t_BD_MaterialBase materialBase ON materialBase.FMaterialID = material.FMaterialID
    INNER JOIN T_BD_MATERIALCATEGORY gory ON materialBase.FCATEGORYID = gory.FCATEGORYID
     LEFT OUTER JOIN T_BD_MATERIALCATEGORY_L gory_L ON (gory.FCATEGORYID = gory_L.FCATEGORYID AND gory_L.FLOCALEID = 2052)
   LEFT OUTER JOIN t_bd_Material_l materialL ON (materialL.FMaterialID = Material.FMATERIALID AND MaterialL.FLOCALEID = 2052)
   LEFT OUTER JOIN T_ORG_ORGANIZATIONS StockOrgs ON StockOrgs.FORGID = dim.FStockOrgId
   LEFT OUTER JOIN T_ORG_ORGANIZATIONS_L StockOrgsL ON (StockOrgsL.FORGID = StockOrgs.FORGID AND StockOrgsL.FLOCALEID = 2052)
    LEFT OUTER JOIN T_ORG_ORGANIZATIONS OwnerOrgs ON OwnerOrgs.FORGID = dim.FCargoOwnerId
    LEFT OUTER JOIN T_ORG_ORGANIZATIONS_L OwnerOrgsL ON (OwnerOrgsL.FORGID = dim.FCargoOwnerId AND OwnerOrgsL.FLOCALEID = 2052)
     LEFT OUTER JOIN T_BD_LOTMASTER batch ON batch.FLOTID = dim.FLOT LEFT OUTER JOIN t_BD_Stock stock ON stock.FStockId = dim.FStockID
     LEFT OUTER JOIN t_BD_Stock_L stockL ON (stockL.FStockId = stock.FStockId AND stockL.FLOCALEID = 2052)
     LEFT OUTER JOIN T_BD_UNIT unit ON unit.funitid = seq.FBASEUNITID
     LEFT OUTER JOIN T_BD_UNIT_L unitL ON (unitL.funitid = seq.FBASEUNITID AND unitL.FLOCALEID = 2052)
     LEFT OUTER JOIN T_BAS_FLEXVALUES bin ON bin.FID = dim.FSTOCKLOCID
      LEFT OUTER JOIN T_BAS_FLEXVALUES_L binL ON (binL.FID = bin.FID AND binL.FLocaleID = 2052)
   LEFT OUTER JOIN t_eng_bom bom ON bom.FID = dim.FBOMID
   INNER JOIN T_META_FORMENUMITEM enumItem ON (enumItem.fid = '868a81ef-bd35-4f98-a172-4e9293655411' AND enumItem.FVALUE = seq.FDocumentStatus)
   LEFT OUTER JOIN T_META_FORMENUMITEM_L enumItemL ON (enumItemL.FENUMID = enumItem.FENUMID AND enumItemL.FLOCALEID = 2052) 
  
   WHERE (((((((((  cald.FACCTPOLICYID = 1) AND OUTACCT.FYEAR = 2017) AND OUTACCT.FPERIOD = 6)
   )) AND (seq.FAcctgAmount >= 0)) AND (seq.FAcctgAmount <= 0))
    AND seq.FDocumentStatus = 'C')
    ---- AND
    --- ((seq.FBillTypeId IN ('73383412199a402bb58439509e089077', 'CEB80356E70D492F9DD7C5E9CD37107E')
     AND (((seq.FACCTGPRICE <= 0) AND seq.FGIVEAWAY = '0') OR ((seq.FACCTGPRICE < 0) AND seq.FGIVEAWAY = '1')))
     AND (seq.FBILLFROMID <> 'HS_AdjustmentBill')