//    uniCenta oPOS  - Touch Friendly Point Of Sale
//    Copyright (c) 2009-2018 uniCenta & previous Openbravo POS works
//    https://unicenta.com
//
//    This file is part of uniCenta oPOS
//
//    uniCenta oPOS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//   uniCenta oPOS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with uniCenta oPOS.  If not, see <http://www.gnu.org/licenses/>
// 

report = new com.unicenta.pos.reports.PanelReportBean();

report.setTitleKey("Menu.CategorySales1");
report.setReport("/com/unicenta/reports/sales_categorysales_1");
report.setResourceBundle("com/unicenta/reports/sales_messages");

report.setSentence("SELECT " +
        "MIN(R.datenew), " +
        "MIN(C.id) AS CATID, " +
        "C.name AS CATNAME, " +
        "MIN((CASE WHEN C_1.NAME='' THEN C.NAME ELSE C_1.name END)) AS PARENTCAT, " +
        "P.name AS PRODNAME, " +
        "SUM(TL.price * TL.units) AS BASE, " +
        "SUM((TL.price * TL.units) * TX.rate) AS AMOUNT, " +
        "SUM((TL.PRICE + (TL.PRICE * TX.RATE)) * TL.units) AS TOTAL, " +
        "SUM(TL.units) AS UNITS " +
    "FROM ((((((products P " +
           "INNER JOIN categories C ON (P.category = C.id)) " +
          "INNER JOIN ticketlines TL ON (TL.product = P.id)) " +
         "INNER JOIN taxes TX ON (TL.taxid = TX.id)) " +
        "INNER JOIN tickets T ON (TL.ticket = T.id)) " +
       "INNER JOIN receipts R ON (T.id = R.id)) " +
      "INNER JOIN taxlines TXL ON (TXL.receipt = R.id) AND (TXL.taxid = TX.id)) " +
     "INNER JOIN categories C_1 ON (C_1.id = C.id) " +
    "WHERE ?(QBF_FILTER) " +
    "GROUP BY C.name, P.name");

report.addParameter("R.DATENEW");
report.addParameter("R.DATENEW");

paramdates = new com.unicenta.pos.reports.JParamsDatesInterval();
paramdates.setStartDate(com.unicenta.beans.DateUtils.getToday());
paramdates.setEndDate(com.unicenta.beans.DateUtils.getTodayMinutes());
report.addQBFFilter(paramdates);

report.addParameter("C.id");
report.addQBFFilter(new com.unicenta.pos.ticket.CategoryFilter());

report.addField("DATENEW", com.unicenta.data.loader.Datas.TIMESTAMP);
report.addField("CATID", com.unicenta.data.loader.Datas.STRING);
report.addField("CATNAME", com.unicenta.data.loader.Datas.STRING);
report.addField("PARENTCAT", com.unicenta.data.loader.Datas.STRING);
report.addField("PRODNAME", com.unicenta.data.loader.Datas.STRING);
report.addField("BASE", com.unicenta.data.loader.Datas.DOUBLE);
report.addField("AMOUNT", com.unicenta.data.loader.Datas.DOUBLE);
report.addField("TOTAL", com.unicenta.data.loader.Datas.DOUBLE);
report.addField("UNITS", com.unicenta.data.loader.Datas.DOUBLE);

report;