package it.acxent.pg; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.Font; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.Barcode128; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; import it.acxent.contab.RigaDocumento; import it.acxent.db.ApplParmFull; import it.acxent.db.DBAdapter; import it.acxent.db.ResParm; import it.acxent.db.WcString; import it.acxent.util.FileWr; import it.acxent.util.Vectumerator; import java.awt.Color; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.Serializable; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Timestamp; import java.util.Calendar; public class Coupon extends _PgAdapter implements Serializable { private static final long serialVersionUID = 1453818572610L; private long id_coupon; private String codice; private long nFoto; private String serie; private Timestamp printTmst; private long totCoupon; private long id_rigaDocumento; private RigaDocumento rigaDocumento; public Coupon(ApplParmFull newApplParmFull) { super(newApplParmFull); } public Coupon() {} public void setId_coupon(long newId_coupon) { this.id_coupon = newId_coupon; } public void setCodice(String newCodice) { this.codice = newCodice; } public void setNFoto(long newNFoto) { this.nFoto = newNFoto; } public long getId_coupon() { return this.id_coupon; } public String getCodice() { return (this.codice == null) ? "" : this.codice.trim(); } public long getNFoto() { return this.nFoto; } public boolean isValido() { if (getId_coupon() > 0L) { if (getId_rigaDocumento() == 0L) return true; return false; } return false; } protected ResParm checkDeleteCascade() { return new ResParm(true); } protected void deleteCascade() {} public Vectumerator findByCR(CouponCR CR, int pageNumber, int pageRows) { String s_Sql_Find = "select A.* from COUPON AS A"; String s_Sql_Order = ""; WcString wc = new WcString(); if (!CR.getSerie().isEmpty()) wc.addWc("A.serie like '%" + CR.getSerie() + "%'"); if (!CR.getCodice().isEmpty()) { if (CR.getCodice().indexOf("'") > 0) CR.setCodice(CR.getCodice().replace("'", "-")); wc.addWc("A.codice='" + CR.getCodice() + "'"); } if (CR.getFlgAttivi() == 0L) { wc.addWc("(A.id_rigaDocumento is null or A.id_rigaDocumento=0)"); } else if (CR.getFlgAttivi() == 1L) { wc.addWc("A.id_rigaDocumento >0"); } if (CR.getFlgUtilizzato() == 0L) { wc.addWc("(A.flgUtilizzato is null or A.flgUtilizzato=0)"); } else if (CR.getFlgUtilizzato() > 0L) { wc.addWc("A.flgUtilizzato =" + CR.getFlgUtilizzato()); } try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt, pageNumber, pageRows); } catch (SQLException e) { removeCPConnection(); handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public String getSerie() { return (this.serie == null) ? "" : this.serie.trim(); } public void setSerie(String serie) { this.serie = serie; } public void findByCodice(String l_coupon) { String s_Sql_Find = "select A.* from COUPON AS A"; String s_Sql_Order = " order by A.serie"; WcString wc = new WcString(); wc.addWc("A.codice='" + l_coupon + "'"); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); findFirstRecord(stmt); } catch (SQLException e) { removeCPConnection(); handleDebug(e); } } public String getDescrizione() { return getSerie() + " " + getSerie(); } public Vectumerator findSerieByGaraCompact(long l_id_gara, int pageNumber, int pageRows) { String s_Sql_Find = "select A.serie, A.nFoto, A.nFotoWeb, A.id_gara , count(A.id_gara) as totCoupon from COUPON AS A group by A.serie, A.nFoto, A.nFotoWeb, A.id_gara "; String s_Sql_Order = " order by A.serie"; WcString wc = new WcString(); wc.addHavingWc("A.id_gara=" + l_id_gara); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt, pageNumber, pageRows); } catch (SQLException e) { removeCPConnection(); handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public long getTotCoupon() { return this.totCoupon; } public void setTotCoupon(long totCoupon) { this.totCoupon = totCoupon; } public Vectumerator findBySerie(String l_serie, int pageNumber, int pageRows) { String s_Sql_Find = "select A.* from COUPON AS A"; String s_Sql_Order = " order by A.serie"; WcString wc = new WcString(); wc.addWc("A.serie ='" + l_serie + "'"); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt, pageNumber, pageRows); } catch (SQLException e) { removeCPConnection(); handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public Vectumerator findByGara(long l_id_gara) { String s_Sql_Find = "select A.* from COUPON AS A"; String s_Sql_Order = " order by A.serie"; WcString wc = new WcString(); wc.addWc("A.id_gara=" + l_id_gara); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); return findRows(stmt); } catch (SQLException e) { removeCPConnection(); handleDebug(e); return AB_EMPTY_VECTUMERATOR; } } public long getId_rigaDocumento() { return this.id_rigaDocumento; } public RigaDocumento getRigaDocumento() { this.rigaDocumento = (RigaDocumento)getSecondaryObject((DBAdapter)this.rigaDocumento, RigaDocumento.class, getId_rigaDocumento()); return this.rigaDocumento; } public void setId_rigaDocumento(long newId_rigaDocumento) { this.id_rigaDocumento = newId_rigaDocumento; setRigaDocumento(null); } public void setRigaDocumento(RigaDocumento newRigaDocumento) { this.rigaDocumento = newRigaDocumento; } public ResParm creaCoupon(String serie, long nFoto, long nCoupon) { ResParm rp; try { for (int i = 0; (long)i < nCoupon; i++) { Coupon c = new Coupon(getApFull()); c.setNFoto(nFoto); c.setSerie(serie); String codice = String.valueOf(Calendar.getInstance().getTimeInMillis()) + String.valueOf(Calendar.getInstance().getTimeInMillis()); System.out.println(codice); codice = codice.substring(0, 4) + "-" + codice.substring(0, 4) + "-" + codice.substring(4, 8) + "-" + codice.substring(8, 12) + "-" + codice.substring(12, 16); System.out.println("" + i + " " + i); c.setCodice(codice); c.save(); } rp = new ResParm(true, "Creati " + nCoupon + " coupon"); } catch (Exception e) { e.printStackTrace(); rp = new ResParm(false, e); } return rp; } public ByteArrayOutputStream stampaCouponA4Pdf(String l_serie, long blankLabels) { long pHMarg = 6L; long l_indent = 4L; ByteArrayOutputStream ba = new ByteArrayOutputStream(); try { this.document = new Document(PageSize.A4, 0.0F, 0.0F, 0.0F, 0.0F); this.writer = PdfWriter.getInstance(this.document, ba); this.document.open(); long labelNumber = 0L; int nCol = 3; int nRow = 8; float[] widths = { 0.33F, 0.33F, 0.33F }; this.pdfPcorpo = new PdfPTable(widths); this.pdfPcorpo.setWidthPercentage(100.0F); if (blankLabels > 0L) { PdfPCell cell = new PdfPCell(); cell.setFixedHeight((PageSize.A4.getHeight() - (float)pHMarg) / (float)nRow); cell.setIndent((float)l_indent); cell.setBorder(0); for (int i = 0; (long)i < blankLabels; i++) { labelNumber++; this.pdfPcorpo.addCell(cell); } } Vectumerator vec = findBySerie(l_serie, 0, 0); while (vec.hasMoreElements()) { Coupon rowBean = (Coupon)vec.nextElement(); labelNumber += (long)rowBean.addUnCouponA4Pdf(DBAdapter.convertHtmlToString(rowBean.getSerie()), nRow, 1L, rowBean.getCodice(), rowBean.getNFoto()); } if (labelNumber == 0L) { Phrase ph = new Phrase("Attenzione! Non ci sono etichette da stampare!"); PdfPCell cell = new PdfPCell(ph); cell.setFixedHeight((PageSize.A4.getHeight() - (float)pHMarg) / (float)nRow); cell.setIndent((float)l_indent); cell.setBorder(0); cell.setColspan(nCol); this.pdfPcorpo.addCell(cell); } else { long numberBlankCell = (long)nCol - labelNumber % (long)nCol; PdfPCell cell = new PdfPCell(); cell.setFixedHeight((PageSize.A4.getHeight() - (float)pHMarg) / (float)nRow); cell.setIndent((float)l_indent); cell.setBorder(0); if (numberBlankCell != (long)nCol) for (int i = 0; (long)i < numberBlankCell; i++) this.pdfPcorpo.addCell(cell); } this.document.add((Element)this.pdfPcorpo); this.document.close(); this.document = null; } catch (Exception e) { e.printStackTrace(); } return ba; } public int addUnCouponA4PdfCodBarre(String titolo, int nRow, long numberOflabel, String l_descrizione) { long pHMarg = 0L; long l_indent = 4L; int totNumbOflabel = 0; try { int margineTop = 40; int altezzaCod = 60; int larghezzaCod = 160; PdfContentByte cb = this.writer.getDirectContent(); Barcode128 codeBar = new Barcode128(); codeBar.setCodeType(9); for (int i = 0; (long)i < numberOflabel; i++) { totNumbOflabel++; PdfPCell cell = new PdfPCell(); cell.setBorder(0); codeBar.setCode(getCodice()); Image imgBarcode = codeBar.createImageWithBarcode(cb, null, null); imgBarcode.scaleToFit((float)larghezzaCod, (float)altezzaCod); cell.addElement(new Chunk(" " + l_descrizione, PDF_fMedioB)); cell.addElement(new Chunk(imgBarcode, 10.0F, (float)(-margineTop + 10))); cell.setVerticalAlignment(4); cell.setFixedHeight((PageSize.A4.getHeight() - (float)pHMarg) / (float)nRow); cell.setIndent((float)l_indent); this.pdfPcorpo.addCell(cell); } } catch (Exception e) { e.printStackTrace(); } return totNumbOflabel; } public Timestamp getPrintTmst() { return this.printTmst; } public void setPrintTmst(Timestamp printTmst) { this.printTmst = printTmst; } public String getPrintTmstS() { return convertTimestampToString(getPrintTmst()); } public int addUnCouponA4Pdf(String l_serie, int nRow, long numberOflabel, String l_codice, long l_nFoto) { long pHMarg = 2L; long l_indent = 4L; boolean border = false; Font fontCodice = new Font(2, 15.0F, 1, Color.DARK_GRAY); Font fontSerie = new Font(2, 12.0F, 1, Color.DARK_GRAY); Font fontWww = new Font(2, 10.0F, 1, Color.DARK_GRAY); Font fontNumero = new Font(2, 7.0F, 1, Color.DARK_GRAY); l_serie = "REGALAMI UN SORRISO"; int totNumbOflabel = 0; try { String logoFileName = getDocBase() + "/images/layout/regalami-un-sorriso.png"; int margineTop = 40; int altezzaCod = 60; int larghezzaCod = 160; float imgLogoWidth = 40.0F; Image imgLogo = Image.getInstance(logoFileName); imgLogo.scaleToFit(imgLogoWidth, imgLogoWidth); imgLogo.setAlignment(5); for (int i = 0; (long)i < numberOflabel; i++) { totNumbOflabel++; PdfPCell cell = new PdfPCell(); if (border) { cell.setBorderColor(Color.gray); cell.setBorderWidth(2.0F); } else { cell.setBorder(0); } cell.addElement(new Chunk(imgLogo, 2.0F, -20.0F)); cell.addElement(new Chunk(" " + l_serie + " ", fontSerie)); cell.addElement(new Chunk(" " + l_codice, fontCodice)); cell.addElement(new Chunk(" #" + l_nFoto + "# ", fontNumero)); cell.addElement(new Chunk(" www.regalamiunsorriso.it ", fontWww)); cell.setVerticalAlignment(4); cell.setHorizontalAlignment(1); cell.setFixedHeight((PageSize.A4.getHeight() - (float)pHMarg) / (float)nRow); cell.setIndent((float)l_indent); this.pdfPcorpo.addCell(cell); } } catch (Exception e) { e.printStackTrace(); } return totNumbOflabel; } public boolean hasCouponRegaloByCliente(long l_id_clifor) { String s_Sql_Find = "select A.* from COUPON AS A inner join RIGA_DOCUMENTO AS B ON A.id_rigaDocumento=B.id_rigaDocumento inner join DOCUMENTO AS C on B.id_documento=C.id_documento"; String s_Sql_Order = ""; WcString wc = new WcString(); wc.addWc("A.nFoto<100"); wc.addWc("C.id_clifor=" + l_id_clifor); try { PreparedStatement stmt = getConn().prepareStatement(s_Sql_Find + s_Sql_Find + wc.toString()); Vectumerator vec = findRows(stmt, 1, 1); if (vec.hasMoreElements()) return true; return false; } catch (SQLException e) { removeCPConnection(); handleDebug(e); return false; } } public void creaFileCvs(CouponCR CR) { try { DBAdapter.printDebug("--------------------------------- creaFileCsv: " + CR.getDescrizioneCR()); Vectumerator list = findByCR(CR, 0, 0); CR.setFileName(getPathTmp() + "reportCoupon_" + getPathTmp() + "_" + getLastUpdTmstForFiles() + "_" + CR.getId_users() + ".csv"); String theCvsFile = getDocBase() + getDocBase(); String SEP = ";"; new File(theCvsFile).delete(); FileWr outCvsFile = new FileWr(theCvsFile, false); String s1 = "Criteri di ricerca: " + CR.getDescrizioneCR() + "\nCoupon"; outCvsFile.writeLine(s1); while (list.hasMoreElements()) { Coupon row = (Coupon)list.nextElement(); s1 = row.getCodice(); outCvsFile.writeLine(s1); } outCvsFile.closeFile(); } catch (Exception e) { handleDebug(e); } } }