import java.io.*; import java.net.*; import java.util.*; import java.text.DecimalFormat; import java.text.NumberFormat; import com.openbravo.pos.forms.DataLogicSales; import com.openbravo.pos.forms.DataLogicSystem; import com.openbravo.pos.erp.sync.DataLogicIntegration; import com.openbravo.pos.ticket.ProductInfoExt; import com.openbravo.data.loader.Session; import com.openbravo.pos.ticket.TicketLineInfo; import java.math.*; import java.util.Properties; import com.openbravo.pos.payment.PaymentInfo; import com.openbravo.pos.ticket.TicketInfo; Session session = new Session(dbURL, dbUser, dbPassword); DataLogicSales logicsale = new DataLogicSales(); logicsale.init(session); DataLogicSystem logicsystem = new DataLogicSystem(); logicsystem.init(session); DataLogicIntegration logicintegration = new DataLogicIntegration(); logicintegration.init(session); File file=null; File fileStatus=null; File fileStatus2=null; if(ticket.ticketType==0){ String auxname=null; String auxaddress="Barcelona"; try{ //eliminamos el status anterior si existe fileStatus = new File("C:/IntTFHKA/Status_Error.txt"); if(fileStatus.exists()) fileStatus.delete(); file = new File("C:/factura.txt"); if(file.exists()) file.delete(); file.createNewFile(); // creates a FileWriter Object FileWriter writer = new FileWriter(file); //formateamos la caracteres especiales auxname=ticket.getCustomer().printName().replace("&", "&").replace(""", "\"").replace("'", "\'"); //formateamos para que la descripcion del cliente no pase de 40 caracteres int cstrlength=39; int cstrlength2=79; int cstrlength3=119; int cstrlength4=159; if(auxname.length()<=cstrlength) cstrlength=auxname.length(); writer.write("i00Nombre: " + auxname.substring(0,cstrlength) + "\n"); if((auxname.length())>cstrlength) writer.write("i01 " + auxname.substring(cstrlength,((auxname.length()>cstrlength2)?cstrlength2:auxname.length())) + "\n"); if((auxname.length())>cstrlength2) writer.write("i02 " + auxname.substring(cstrlength2,((auxname.length()>cstrlength3)?cstrlength3:auxname.length())) + "\n"); if((auxname.length())>cstrlength3) writer.write("i03 " + auxname.substring(cstrlength3,((auxname.length()>cstrlength4)?cstrlength4:auxname.length())) + "\n"); writer.write("i04C.I/RIF: " + ticket.getCustomer().printTaxid() + "\n"); if(ticket.getCustomer().getAddress()!=null){ if(ticket.getCustomer().getAddress().trim().compareTo("")!=0) auxaddress=ticket.getCustomer().getAddress().trim(); } if(auxaddress.length()<=cstrlength) cstrlength=auxaddress.length(); writer.write("i05Direccion: " + auxaddress.substring(0,cstrlength) + "\n"); if((auxaddress.length())>cstrlength) writer.write("i06 " + auxaddress.substring(cstrlength,((auxaddress.length()>cstrlength2)?cstrlength2:auxaddress.length())) + "\n"); if((auxaddress.length())>cstrlength2) writer.write("i07 " + auxaddress.substring(cstrlength2,((auxaddress.length()>cstrlength3)?cstrlength3:auxaddress.length())) + "\n"); if((auxaddress.length())>cstrlength3) writer.write("i08 " + auxaddress.substring(cstrlength3,((auxaddress.length()>cstrlength4)?cstrlength4:auxaddress.length())) + "\n"); writer.write("i09Orden Nro.: " + ticket.printId() + "\n"); length=ticket.getLinesCount(); String aux=null; NumberFormat f = NumberFormat.getInstance(Locale.ENGLISH); if (f instanceof DecimalFormat) ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true); DecimalFormat fo = new DecimalFormat("00000000.00"); DecimalFormat fcant = new DecimalFormat("00000.000"); BigDecimal bd =null; ProductInfoExt product = null; int pstrlength=40; String desc=null; Double totalLines=0; for (int i = 0; i < length; i++) { line = ticket.getLine(i); desc=line.printName(); if(desc.length()<=pstrlength) pstrlength=desc.length(); desc=desc.substring(0,pstrlength); product = logicsale.getProductInfo(line.getProductID()); writer.write("@Codigo: "+ product.getReference() +"\n"); bd = new BigDecimal(line.getPrice()); bd = bd.setScale(2, RoundingMode.DOWN); aux=fo.format(bd.doubleValue()); writer.write("!"+aux.replace(".","").replace(",","")); aux=fcant.format(line.getMultiply()); writer.write(aux.replace(".","").replace(",","")); writer.write(desc + "\n"); totalLines+=(line.getPrice()*line.getMultiply()*(1+line.getTaxRate())); } DecimalFormat formatPayment = new DecimalFormat("0000000000.00"); System.out.println(ticket.getPayments()); Iterator i = ticket.getPayments().iterator(); Double montoefectivo=0; Double totalPayment=0; while (i.hasNext()){ PaymentInfo paymentline = i.next(); bd = new BigDecimal(paymentline.getTotal()); bd = bd.setScale(2, RoundingMode.HALF_UP); aux=formatPayment.format(bd.doubleValue()); if (paymentline.getName().compareTo("cheque")==0){ writer.write("205"+aux.replace(".","").replace(",","")+ "\n"); }else if (paymentline.getName().compareTo("magcard")==0){ if(paymentline.getCarType().compareTo("Debito")==0) writer.write("202"+aux.replace(".","").replace(",","")+ "\n"); else writer.write("203"+aux.replace(".","").replace(",","")+ "\n"); }else if (paymentline.getName().compareTo("transfer")==0){ writer.write("206"+aux.replace(".","").replace(",","")+ "\n"); }else if (paymentline.getName().compareTo("debt")==0){ writer.write("204"+aux.replace(".","").replace(",","")+ "\n"); }else{ montoefectivo+=paymentline.getTotal(); } totalPayment+=bd.doubleValue(); } bd = new BigDecimal(totalLines-totalPayment); bd = bd.setScale(2, RoundingMode.HALF_UP); if((bd.doubleValue())!=0.00){ aux=formatPayment.format(bd.doubleValue()); writer.write("213"+aux.replace(".","").replace(",","")+ "\n"); } if(montoefectivo!=0) writer.write("101"); writer.flush(); writer.close(); String filePath = "C:/IntTFHKA/IntTFHKA.exe SendFileCmd(C:/factura.txt)"; Process p = Runtime.getRuntime().exec(filePath); p.waitFor(); //usamos filereader para extraer la informacion de si se imprimio exitosamente sino se envia un mensaje Integer found = 1; boolean checkprinter=false; boolean StatusErrorExist=false; while(!StatusErrorExist){ fileStatus = new File("C:/IntTFHKA/Status_Error.txt"); StatusErrorExist=fileStatus.exists(); } if(StatusErrorExist){ // creates a FileReader Object FileReader reader = new FileReader(fileStatus); BufferedReader br = new BufferedReader(reader); String status; String[] statusError; while((status = br.readLine()) != null) { statusError=status.split("\\t"); if(statusError[1].compareTo("0")==0) checkprinter=true; } reader.close(); } if(!checkprinter){ JOptionPane.showMessageDialog(null, "No se pudo imprimir el ticket correctamente", "POS", JOptionPane.PLAIN_MESSAGE); logicsale.deleteTicket(ticket,"0"); return found; } //generamos el estatus de informacion para leer la informacin fiscal y ejecutamos el proceso //Borramos el ultimo status error fileStatus = new File("C:/IntTFHKA/Status_Error.txt"); if(fileStatus.exists()) fileStatus.delete(); //Borramos el ultimo status fileStatus2 = new File("C:/IntTFHKA/Status.txt"); if(fileStatus2.exists()) fileStatus2.delete(); boolean StatusExist=false; StatusErrorExist=false; while(!StatusExist){ String filePath2 = "C:/IntTFHKA/IntTFHKA.exe UploadStatusCmd(S1)"; Process p1 = Runtime.getRuntime().exec(filePath2); p1.waitFor(); while(!StatusErrorExist){ fileStatus = new File("C:/IntTFHKA/Status_Error.txt"); StatusErrorExist=fileStatus.exists(); } fileStatus2 = new File("C:/IntTFHKA/Status.txt"); StatusExist=fileStatus2.exists(); } //usamos filereader para extraer la informacion e insertarla en el ticket if(!logicintegration.updateTicketFiscalTheFactory(ticket,fileStatus2)){ JOptionPane.showMessageDialog(null, "Ocurrio un problema actualizando la informacion fiscal", "POS", JOptionPane.PLAIN_MESSAGE); return found; } }catch(Exception e){ e.printStackTrace(); } }