/** * Copyright (c) 2015 Mustafa DUMLUPINAR, mdumlupinar@gmail.com * * This file is part of seyhan project. * * seyhan 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. * * This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */ package controllers.invoice.reports; import static play.data.Form.form; import java.util.Date; import java.util.LinkedHashMap; import java.util.Map; import models.Contact; import models.GlobalPrivateCode; import models.GlobalTransPoint; import models.InvoiceTransSource; import models.InvoiceTransStatus; import models.SaleSeller; import models.StockDepot; import play.data.Form; import play.data.format.Formats.DateTime; import play.i18n.Messages; import play.mvc.Controller; import play.mvc.Result; import reports.ReportParams; import reports.ReportService; import reports.ReportService.ReportResult; import utils.AuthManager; import utils.CacheUtils; import utils.DateUtils; import utils.GlobalCons; import utils.InstantSQL; import views.html.invoices.reports.invoice_list; import controllers.global.Profiles; import enums.ReportUnit; import enums.Right; import enums.RightLevel; /** * @author mdpinar */ public class InvoiceList extends Controller { private final static Right RIGHT_SCOPE = Right.FATR_FATURA_LISTESI; private final static String REPORT_NAME = "InvoiceList"; private final static Form<InvoiceList.Parameter> parameterForm = form(InvoiceList.Parameter.class); public static class Parameter { public String orderby; public ReportUnit unit; public Contact contact; public GlobalTransPoint transPoint = Profiles.chosen().gnel_transPoint; public GlobalPrivateCode privateCode = Profiles.chosen().gnel_privateCode; public Right transType; public String transNo; public InvoiceTransSource transSource; public InvoiceTransStatus status; @DateTime(pattern = "dd/MM/yyyy") public Date startDate = DateUtils.getFirstDayOfMonth(); @DateTime(pattern = "dd/MM/yyyy") public Date endDate = new Date(); @DateTime(pattern = "dd/MM/yyyy") public Date deliveryDate; public StockDepot depot; public SaleSeller seller; public Boolean isCash; public String showType; public static Map<String, String> options() { LinkedHashMap<String, String> options = new LinkedHashMap<String, String>(); options.put("t.trans_date", Messages.get("date")); options.put("t.contact_name, t.trans_date", Messages.get("contact.name")); options.put("t.delivery_date", Messages.get("date.delivery") + " " + Messages.get("ascending")); options.put("t.delivery_date desc", Messages.get("date.delivery") + " " + Messages.get("descending")); return options; } public static Map<String, String> showTypes() { LinkedHashMap<String, String> options = new LinkedHashMap<String, String>(); options.put("Summary", Messages.get("report.show.summary")); options.put("Detailed", Messages.get("report.show.detail")); return options; } } private static String getQueryString(Parameter params) { StringBuilder queryBuilder = new StringBuilder(""); queryBuilder.append(" and t.workspace = " + CacheUtils.getWorkspaceId()); if (params.contact != null && params.contact.id != null) { queryBuilder.append(" and t.contact_id = "); queryBuilder.append(params.contact.id); } if (params.startDate != null) { queryBuilder.append(" and t.trans_date >= "); queryBuilder.append(DateUtils.formatDateForDB(params.startDate)); } if (params.endDate != null) { queryBuilder.append(" and t.trans_date <= "); queryBuilder.append(DateUtils.formatDateForDB(params.endDate)); } if (params.deliveryDate != null) { queryBuilder.append(" and t.delivery_date = "); queryBuilder.append(DateUtils.formatDateForDB(params.deliveryDate)); } if (params.depot != null && params.depot.id != null) { queryBuilder.append(" and t.depot_id = "); queryBuilder.append(params.depot.id); } if (params.seller != null && params.seller.id != null) { queryBuilder.append(" and t.seller_id = "); queryBuilder.append(params.seller.id); } if (params.transType != null) { queryBuilder.append(" and t._right = '"); queryBuilder.append(params.transType); queryBuilder.append("'"); } if (params.isCash != null && params.isCash) { queryBuilder.append(" and t.is_cash = " + GlobalCons.TRUE); } if (params.transNo != null && ! params.transNo.isEmpty()) { queryBuilder.append(" and t.trans_no = '"); queryBuilder.append(params.transNo); queryBuilder.append("'"); } if (params.transSource != null && params.transSource.id != null) { queryBuilder.append(" and t.trans_source_id = "); queryBuilder.append(params.transSource.id); } if (params.status != null && params.status.id != null) { queryBuilder.append(" and t.status_id = "); queryBuilder.append(params.status.id); } return queryBuilder.toString(); } public static Result generate() { Result hasProblem = AuthManager.hasProblem(RIGHT_SCOPE, RightLevel.Enable); if (hasProblem != null) return hasProblem; Form<InvoiceList.Parameter> filledForm = parameterForm.bindFromRequest(); if(filledForm.hasErrors()) { return badRequest(invoice_list.render(filledForm)); } else { Parameter params = filledForm.get(); ReportParams repPar = new ReportParams(); repPar.modul = RIGHT_SCOPE.module.name(); repPar.reportNameExtra = "List"; repPar.reportName = REPORT_NAME + params.showType; repPar.reportUnit = params.unit; repPar.query = getQueryString(params); repPar.orderBy = params.orderby; /* * Parametrik degerlerin gecisi */ repPar.paramMap.put("TRANS_POINT_SQL", ""); if (params.transPoint != null && params.transPoint.id != null) { repPar.paramMap.put("TRANS_POINT_SQL", InstantSQL.buildTransPointSQL(params.transPoint.id)); } repPar.paramMap.put("PRIVATE_CODE_SQL", ""); if (params.privateCode != null && params.privateCode.id != null) { repPar.paramMap.put("PRIVATE_CODE_SQL", InstantSQL.buildPrivateCodeSQL(params.privateCode.id)); } String par1 = (params.transType != null ? Messages.get(params.transType.key) + ", " : ""); String par2 = "(" + DateUtils.formatDateStandart(params.startDate) + " - " + DateUtils.formatDateStandart(params.endDate) +")"; repPar.paramMap.put("REPORT_INFO", par1 + " - " + par2); ReportResult repRes = ReportService.generateReport(repPar, response()); if (repRes.error != null) { flash("warning", repRes.error); return ok(invoice_list.render(filledForm)); } else if (ReportService.isToDotMatrix(repPar)) { flash("success", Messages.get("printed.success")); } return ReportService.sendReport(repPar, repRes, invoice_list.render(filledForm)); } } public static Result index() { Result hasProblem = AuthManager.hasProblem(RIGHT_SCOPE, RightLevel.Enable); if (hasProblem != null) return hasProblem; return ok(invoice_list.render(parameterForm.fill(new Parameter()))); } }