public void save(DocumentoModel doc)
        {
            SQLiteConnection myConn = bd.ConnectSqlite();

            // myConn.Open();
            try
            {
                string sql = "INSERT INTO documento (" +
                             "TipoDTE," +
                             "Folio," +
                             "FchEmis," +
                             "RUTEmisor," +
                             "RUTRecep," +
                             "RznSoc," +
                             "MntNeto," +
                             "MntExe," +
                             "IVA," +
                             "tipoimp," +
                             "tasaimp," +
                             "montoimp," +
                             "MntTotal," +
                             "estado," +
                             "NombreXml" +
                             ") VALUES (" +
                             doc.TipoDTE + "," +
                             doc.Folio + ",'" +
                             Convert.ToDateTime(doc.FchEmis).ToString("yyyy-MM-dd") + "','" +
                             doc.RUTEmisor + "','" +
                             doc.RUTRecep + "','" +
                             doc.RznSoc + "'," +
                             doc.MntNeto + "," +
                             doc.MntExe + "," +
                             doc.IVA + ",'" +
                             doc.tipoimp + "'," +
                             doc.tasaimp + "," +
                             doc.montoimp + "," +
                             doc.MntTotal + ",'" +
                             doc.estado + "','" +
                             doc.NombreXml + "'" +
                             ")";

                SQLiteCommand command = new SQLiteCommand(sql, myConn);
                command.ExecuteNonQuery();
                Console.WriteLine(">>>>>>>> " + doc.Folio);
            }
            catch (Exception empUpdate)
            {
                Console.WriteLine("====================== ERROR: {0}" + empUpdate.ToString() + " ==========================");
                // MessageBox.Show("ERROR: {0}" + empUpdate.ToString());
            }
            myConn.Close();
        }
        public void serialize(DocumentoModel documento)
        {
            if (documento.TipoDTE != 802)
            {
                MemoryStream stream           = new MemoryStream();
                DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(DocumentoModel));
                ds.WriteObject(stream, documento);
                string jsonString = Encoding.UTF8.GetString(stream.ToArray());
                stream.Close();

                String json = jsonString.Replace("null", "\"\"");

                String fileNameJson = @"C:/IatFiles/file/" + documento.TipoDTE + "_" + documento.RUTEmisor + "_" + documento.Folio + ".json";

                using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileNameJson, false, Encoding.GetEncoding("ISO-8859-1")))
                {
                    file.WriteLine(json);
                }
            }
        }
Exemple #3
0
        public void addUltmoDte(DocumentoModel doc)
        {
            try
            {
                DateTime thisDay = DateTime.Now;
                String   fecha   = String.Format("{0:yyyyMMddTHHmmss}", thisDay);

                SQLiteConnection myConn = bd.ConnectSqlite();
                //myConn.Open();

                string sql = "insert into ultimodte (RutEmisor, RznSoc, CdgSIISucur, RutRecep, RznSocRecep, Folio, TipoDTE, fch) values ('" +
                             doc.RUTEmisor + "','" + doc.RznSoc + "'," + doc.CdgSIISucur + ",'" + doc.RUTRecep + "','" + doc.RznSocRecep + "'," + doc.Folio + "," + doc.TipoDTE + ",'" + fecha + "')";
                SQLiteCommand command = new SQLiteCommand(sql, myConn);
                command.ExecuteNonQuery();

                myConn.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: {0}", e.ToString());
            }
        }
Exemple #4
0
        public bool isValid(DocumentoModel doc)
        {
            bool valid = true;

            String xmlCaf = String.Empty;
            String cafDir = String.Empty;

            fileAdmin file = new fileAdmin();

            string rut = doc.RUTEmisor;

            try
            {
                switch (doc.TipoDTE)
                {
                case 33: cafDir = @"C:\AdmToSii\cafs\" + rut + @"\factura\";
                    break;

                case 61: cafDir = @"C:\AdmToSii\cafs\" + rut + @"\notacredito\";
                    break;

                case 56: cafDir = @"C:\AdmToSii\cafs\" + rut + @"\notadebito\";
                    break;

                case 52: cafDir = @"C:\AdmToSii\cafs\" + rut + @"\Guia\";
                    break;

                case 34: cafDir = @"C:\AdmToSii\cafs\" + rut + @"\facturaexenta\";
                    break;
                }

                xmlCaf = file.nextFile(cafDir, "*.xml");

                String xml = String.Empty;

                if (xmlCaf != null)
                {
                    StreamReader objReader = new StreamReader(xmlCaf, System.Text.Encoding.Default, true);
                    objReader.ToString();
                    xml = objReader.ReadToEnd();
                }


                int start = xml.IndexOf("<TD") + 4;
                int end   = xml.IndexOf("</TD>");
                int largo = end - start;

                // Valida tipo de documento
                String td = xml.Substring(start, largo);
                if (td != doc.TipoDTE.ToString())
                {
                    valid = false;
                }


                start = xml.IndexOf("<FA>") + 4;
                end   = xml.IndexOf("</FA>");
                largo = end - start;
                // Valida FECHA de documento
                String fch = xml.Substring(start, largo);

                DateTime fchCaf = DateTime.ParseExact(fch, "yyyy-MM-dd",
                                                      System.Globalization.CultureInfo.InvariantCulture);

                DateTime fEmis = DateTime.ParseExact(fch, "yyyy-MM-dd",
                                                     System.Globalization.CultureInfo.InvariantCulture);

                if (fEmis > fchCaf)
                {
                    valid = false;
                }


                start = xml.IndexOf("<D>") + 3;
                end   = xml.IndexOf("</D>");
                largo = end - start;
                String d = xml.Substring(start, largo);

                start = xml.IndexOf("<H>") + 3;
                end   = xml.IndexOf("</H>");
                largo = end - start;
                String h = xml.Substring(start, largo);


                // Valida Folio del documento dentro del rango CAF
                int ds = Convert.ToInt32(d);
                int hs = Convert.ToInt32(h);

                // TO DO: Descomentar esta linea para el proceso de producción
                //    if (!((folio < hs) && (folio >ds)) ) valid = false;


                // OTRAS VALIDACIONES
                if (doc.CiudadRecep == null || doc.CiudadRecep == String.Empty)
                {
                    valid = false;
                }
                if (doc.CmnaRecep == null || doc.CmnaRecep == String.Empty)
                {
                    valid = false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file CAF could not be read:");
                Console.WriteLine(e.Message);
            }


            return(valid);
        }
Exemple #5
0
        public void createJson(DocumentoModel documento)
        {
            RegistroWin regWin       = new RegistroWin();
            string      fileNameJson = string.Empty;

            if (documento.TipoDTE != 802)
            {
                regWin = regWin.getRegWin();
                try
                {
                    fileNameJson = @"" + regWin.rutaPrint + "\\" + documento.TipoDTE + "_" + documento.RUTEmisor + "_" + documento.Folio + ".json";
                }
                catch (Exception ex)
                {
                    Console.WriteLine("#####  error al guardar JSON #############" + ex);
                }

                using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileNameJson, false, Encoding.GetEncoding("ISO-8859-1")))
                {
                    file.WriteLine("{");
                    file.WriteLine("\"TipoDTE\": " + documento.TipoDTE + ",");
                    file.WriteLine("\"Folio\": " + documento.Folio + ",");
                    file.WriteLine("\"FchEmis\": \"" + documento.FchEmis + "\",");
                    file.WriteLine("\"IndNoRebaja\": " + documento.IndNoRebaja + ",");
                    file.WriteLine("\"TipoDespacho\": " + documento.TipoDespacho + ",");
                    file.WriteLine("\"IndTraslado\": " + documento.IndTraslado + ",");
                    file.WriteLine("\"TpoImpresion\": \"" + documento.TpoImpresion + "\",");
                    file.WriteLine("\"IndServicio\": " + documento.IndServicio + ",");
                    file.WriteLine("\"MntBruto\": " + documento.MntBruto + ",");
                    file.WriteLine("\"FmaPago\": " + documento.FmaPago + ",");
                    //for
                    file.WriteLine("  \"MntPagos\":");
                    file.WriteLine("  [");
                    file.WriteLine("  {\"FchPago\": \"\", \"MntPago\": 0}");
                    file.WriteLine("  ],");
                    file.WriteLine("\"MedioPago\": \"\",");
                    file.WriteLine("\"CondVenta\": \"\",");
                    file.WriteLine("\"FchVenc\": \"\",");
                    file.WriteLine("\"RUTEmisor\": \"" + documento.RUTEmisor + "\",");
                    file.WriteLine("\"RznSoc\": \"" + documento.RznSoc + "\",");
                    file.WriteLine("\"GiroEmis\": \"" + documento.GiroEmis + "\",");
                    file.WriteLine("\"CdgSIISucur\": " + documento.CdgSIISucur + ",");
                    file.WriteLine("\"DirOrigen\": \"" + documento.DirOrigen + "\",");
                    file.WriteLine("\"CmnaOrigen\": \"" + documento.CmnaOrigen + "\",");
                    file.WriteLine("\"CiudadOrigen\": \"" + documento.CiudadOrigen + "\",");
                    file.WriteLine("\"CdgVendedor\": \"" + documento.CdgVendedor + "\",");
                    file.WriteLine("\"NomVendedor\": \"" + documento.NomVendedor + "\",");
                    file.WriteLine("\"BodEmis\": \"\",");
                    file.WriteLine("\"RUTMandante\": \"\",");
                    file.WriteLine("\"RUTRecep\": \"" + documento.RUTRecep + "\",");
                    file.WriteLine("\"RznSocRecep\": \"" + documento.RznSocRecep + "\",");
                    file.WriteLine("\"GiroRecep\": \"" + documento.GiroRecep + "\",");
                    file.WriteLine("\"DirRecep\": \"" + documento.DirRecep + "\",");
                    file.WriteLine("\"CmnaRecep\": \"" + documento.CmnaRecep + "\",");
                    file.WriteLine("\"CiudadRecep\": \"" + documento.CiudadRecep + "\",");
                    file.WriteLine("\"TelRecep\": \"" + documento.TelRecep + "\",");
                    file.WriteLine("\"BodRecep\": \"\",");
                    file.WriteLine("\"RUTSolicita\": \"\",");
                    file.WriteLine("\"RUTTrans\": \"\",");
                    file.WriteLine("\"NombreChofer\": \"\",");
                    file.WriteLine("\"Patente\": \"\",");
                    file.WriteLine("\"DirDest\": \"\",");
                    file.WriteLine("\"CmnaDest\": \"\",");
                    file.WriteLine("\"NroOrdenCompra\": " + documento.NroOrdenCompra + ",");
                    file.WriteLine("\"NroCita\": " + documento.NroCita + ",");
                    file.WriteLine("\"Sello\": " + documento.Sello + ",");
                    file.WriteLine("\"MntNeto\": " + documento.MntNeto + ",");
                    file.WriteLine("\"MntExe\": " + documento.MntExe + ",");
                    file.WriteLine("\"MntBase\": 0,");
                    file.WriteLine("\"MntMargenCom\": 0,");
                    file.WriteLine("\"TasaIVA\": " + documento.TasaIVA + ",");
                    file.WriteLine("\"IVA\": " + documento.IVA + ",");
                    file.WriteLine("  \"imptoReten\":");
                    file.WriteLine("  [");
                    file.WriteLine("  ],");
                    file.WriteLine("\"IVANoRet\": 0,");
                    file.WriteLine("\"CredEC\": 0,");
                    file.WriteLine("\"GrntDep\": 0,");
                    file.WriteLine("\"ValComNeto\": 0,");
                    file.WriteLine("\"ValComExe\": 0,");
                    file.WriteLine("\"ValComIVA\": 0,");
                    file.WriteLine("\"MntTotal\": " + documento.MntTotal + ",");
                    file.WriteLine("\"MontoNF\": 0,");
                    file.WriteLine("  \"detalle\":");
                    file.WriteLine("  [");
                    //for each
                    int lineaFinalDetalle = 0;
                    foreach (var fila in documento.detalle)
                    {
                        file.Write("   {\"NroLinDet\": " + fila.NroLinDet + ", \"TpoCodigo\": \"PLU\", \"VlrCodigo\": " + fila.VlrCodigo + ", \"CodCatalog\": \"\", \"TpoDocLiq\": \"\", \"IndExe\": \"" + fila.IndExe + "\", \"NmbItem\": \"" + fila.NmbItem + "\", \"DscItem\": \"" + fila.DscItem + "\", \"UnmdItem\": \"" + fila.UnmdItem + "\", \"QtyItem\": " + fila.QtyItem + ", \"PrcItem\": " + fila.PrcItem + ", \"PrcBruItem\": " + fila.PrcBruItem + ", \"DescuentoPct\": " + fila.DescuentoPct + ", \"DescuentoMonto\": " + fila.DescuentoMonto + ", \"DescuentoBruMonto\": " + fila.DescuentoBruMonto + ", \"CodImpAdic\": \"" + fila.CodImpAdic + "\", \"MontoItem\": " + fila.MontoItem + ",\"MontoBruItem\": " + fila.MontoBruItem);
                        if (lineaFinalDetalle != documento.detalle.Count - 1)
                        {
                            file.WriteLine(" },");
                            lineaFinalDetalle = lineaFinalDetalle + 1;
                        }
                        else
                        {
                            file.WriteLine(" }");
                        }
                    }
                    //fin foreach
                    file.WriteLine("  ],");
                    file.WriteLine("  \"dscRcgGlobal\":");
                    file.WriteLine("  [");
                    //foreach
                    int lineaFinalDescuentos = 0;
                    foreach (var filaDes in documento.dscRcgGlobal)
                    {
                        file.Write("   {\"NroLinDR\": " + filaDes.NroLinDR + ", \"TpoMov\": \"" + filaDes.TpoMov + "\", \"GlosaDR\": \"" + filaDes.GlosaDR + "\", \"TpoValor\": \"" + filaDes.TpoValor + "\", \"ValorDR\": " + filaDes.ValorDR + ", \"IndExeDR\": " + filaDes.IndExeDR);
                        if (lineaFinalDescuentos != documento.dscRcgGlobal.Count - 1)
                        {
                            file.WriteLine(" },");
                            lineaFinalDescuentos = lineaFinalDescuentos + 1;
                        }
                        else
                        {
                            file.WriteLine(" }");
                        }
                    }
                    //fin foreach
                    file.WriteLine("  ],");
                    file.WriteLine("  \"Referencia\":");
                    file.WriteLine("  [");
                    //foreach
                    int lineaFinalRef = 0;
                    foreach (var filaRef in documento.Referencia)
                    {
                        file.Write("   { \"NroLinRef\": " + filaRef.NroLinRef + ", \"TpoDocRef\": \"" + filaRef.TpoDocRef + "\", \"IndGlobal\": " + filaRef.IndGlobal + ", \"FolioRef\": \"" + filaRef.FolioRef + "\", \"RutOtr\": \"" + filaRef.RUTOtr + "\", \"IdAdicOtr\": \"" + filaRef.IdAdicOtr + "\", \"FchRef\": \"" + filaRef.FchRef + "\", \"CodRef\": " + filaRef.CodRef + ", \"RazonRef\": \"" + filaRef.RazonRef + "\"");
                        if (lineaFinalRef != documento.Referencia.Count - 1)
                        {
                            file.WriteLine(" },");
                            lineaFinalRef = lineaFinalRef + 1;
                        }
                        else
                        {
                            file.WriteLine(" }");
                        }
                    }
                    //fin foreach
                    file.WriteLine("  ],");
                    file.WriteLine("  \"Comisiones\":");
                    file.WriteLine("  [");
                    file.WriteLine("   {\"NroLinCom\": 0, \"TipoMovim\": \"\", \"Glosa\": \"\", \"ValComNeto\": 0,\"ValComExe\": 0,\"ValComIVA\": 0}");
                    file.WriteLine("  ]");
                    file.WriteLine("}");
                }
            }
        }
Exemple #6
0
        public void buscaXmlProveedor()
        {
            string urlXml   = string.Empty;
            string fileName = nextFile(@"C:\AdmToSii\file\xml\proveedores", "*.xml");

            urlXml = @"C:\AdmToSii\file\xml\proveedores\" + fileName;
            //serializar xml
            XmlTextReader reader = new XmlTextReader(urlXml);

            if (fileName != null)
            {
                while (reader.ReadToFollowing("Documento"))
                {
                    DocumentoModel doc       = new DocumentoModel();
                    XmlReader      readerDoc = reader.ReadSubtree();
                    while (readerDoc.Read())
                    {
                        if (readerDoc.Name == "TipoDTE")
                        {
                            doc.TipoDTE = Convert.ToInt32(reader.ReadString());
                        }
                        if (readerDoc.Name == "Folio")
                        {
                            doc.Folio = Convert.ToInt32(reader.ReadString());
                        }
                        if (readerDoc.Name == "FchEmis")
                        {
                            doc.FchEmis = reader.ReadString();
                        }
                        if (readerDoc.Name == "RUTEmisor")
                        {
                            doc.RUTEmisor = reader.ReadString();
                        }
                        if (readerDoc.Name == "RznSoc")
                        {
                            doc.RznSoc = reader.ReadString();
                        }
                        if (readerDoc.Name == "GiroEmis")
                        {
                            doc.GiroEmis = reader.ReadString();
                        }
                        if (readerDoc.Name == "RUTRecep")
                        {
                            doc.RUTRecep = reader.ReadString();
                        }
                        if (readerDoc.Name == "RznSocRecep")
                        {
                            doc.RznSocRecep = reader.ReadString();
                        }
                        if (readerDoc.Name == "GiroRecep")
                        {
                            doc.GiroRecep = reader.ReadString();
                        }
                        if (readerDoc.Name == "MntNeto")
                        {
                            doc.MntNeto = Convert.ToInt32(reader.ReadString());
                        }
                        if (readerDoc.Name == "IVA")
                        {
                            doc.IVA = Convert.ToInt32(reader.ReadString());
                        }
                        if (readerDoc.Name == "MntTotal")
                        {
                            doc.MntTotal = Convert.ToInt32(reader.ReadString());
                        }
                        doc.estado    = "DISCHARGED";
                        doc.NombreXml = fileName;
                    }
                    if (doc.exist(doc.RUTEmisor, doc.TipoDTE.ToString(), doc.Folio.ToString()) == "False")
                    {
                        doc.save(doc);
                    }
                }
                reader.Close();
                //Muevo el archivo
                mvFile(fileName, @"C:\AdmToSii\file\xml\proveedores\", @"C:\AdmToSii\file\xml\proveedores\fileprocess\");
                //Actualizo grilla
            }
        }
        public DocumentoModel lectura(String fileJson, bool moveFile, String dirOrigen)
        {
            DocumentoModel doc      = new DocumentoModel();
            fileAdmin      file     = new fileAdmin();
            String         fileName = String.Empty;

            if (dirOrigen == "")
            {
                dirOrigen = @"C:\AdmToSii\file";
            }


            if (fileJson == "")
            {
                fileName = file.nextFile(dirOrigen, "*.json");
            }
            else
            {
                fileName = dirOrigen + fileJson;
            }


            if (fileName != null)
            {
                StreamReader objReader = new StreamReader(fileName, System.Text.Encoding.Default, true);
                objReader.ToString();
                String data = objReader.ReadToEnd();

                DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(DocumentoModel));

                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(data));

                try
                {
                    doc = (DocumentoModel)js.ReadObject(ms);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    // MessageBox.Show("Error de lectura JSON"+ e.Message);
                }

                // Datos del Emisor
                // Cargo datos en laclase Documento desde sqlite
                string oc = bd.GetOC();
                if (oc == "True")
                {
                    doc.NroOrdenCompra = Convert.ToInt32(getOrdenCompra(data));
                    doc.NroCita        = getNroCita(data);
                    doc.Sello          = getSello(data);
                }


                if (doc.RUTEmisor == null)
                {
                    try
                    {
                        SQLiteConnection myConn = bd.ConnectSqlite();
                        myConn.Open();

                        string           sql     = "select * from empresa";
                        SQLiteCommand    command = new SQLiteCommand(sql, myConn);
                        SQLiteDataReader reader  = command.ExecuteReader();
                        while (reader.Read())
                        {
                            doc.RUTEmisor         = reader["RutEmisor"].ToString();
                            doc.RznSoc            = reader["RznSoc"].ToString();
                            doc.GiroEmis          = reader["GiroEmis"].ToString();
                            doc.Telefono          = reader["Telefono"].ToString();
                            doc.CorreoEmisor      = reader["CorreoEmisor"].ToString();
                            doc.Acteco            = Convert.ToInt32(reader["Acteco"]);
                            doc.CdgSIISucur       = Convert.ToInt32(reader["CdgSIISucur"]);
                            doc.DirMatriz         = reader["DirMatriz"].ToString();
                            doc.CmnaOrigen        = reader["CmnaOrigen"].ToString();
                            doc.CiudadOrigen      = reader["CiudadOrigen"].ToString();
                            doc.DirOrigen         = reader["DirOrigen"].ToString();
                            doc.NombreCertificado = reader["NomCertificado"].ToString();
                            doc.SucurEmisor       = reader["SucurEmisor"].ToString();
                            doc.FchResol          = reader["FchResol"].ToString();
                            doc.RutEnvia          = reader["RutCertificado"].ToString();
                            doc.NumResol          = reader["NumResol"].ToString();
                            doc.CondEntrega       = reader["CondEntrega"].ToString();
                        }
                        myConn.Close();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("ERROR: {0}", e.ToString());
                    }
                }
                else
                {
                    try
                    {
                        SQLiteConnection myConn = bd.ConnectSqlite();
                        //myConn.Open();

                        string           sql     = "select * from empresa where empresa.RutEmisor = '" + doc.RUTEmisor.ToString() + "'";
                        SQLiteCommand    command = new SQLiteCommand(sql, myConn);
                        SQLiteDataReader reader  = command.ExecuteReader();
                        while (reader.Read())
                        {
                            doc.Telefono          = reader["Telefono"].ToString();
                            doc.CorreoEmisor      = reader["CorreoEmisor"].ToString();
                            doc.Acteco            = Convert.ToInt32(reader["Acteco"]);
                            doc.DirRegionalSII    = reader["sucurSII"].ToString();
                            doc.DirMatriz         = reader["DirMatriz"].ToString();
                            doc.NombreCertificado = reader["NomCertificado"].ToString();
                            doc.SucurEmisor       = reader["SucurEmisor"].ToString();
                            doc.FchResol          = reader["FchResol"].ToString();
                            doc.RutEnvia          = reader["RutCertificado"].ToString();
                            doc.NumResol          = reader["NumResol"].ToString();
                            doc.CondEntrega       = reader["CondEntrega"].ToString();
                            doc.PrnMtoNeto        = reader["PrnMtoNeto"].ToString();
                            doc.PrnTwoCopy        = reader["PrnTwoCopy"].ToString();
                        }

                        myConn.Close();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("ERROR: {0}", e.ToString());
                    }
                }

                objReader.Close();
                ms.Close();
                if (moveFile)
                {
                    file.mvFile(fileName, dirOrigen, "C:/AdmToSii/fileProcess/");
                }

                CafModel caf = new CafModel();

                if (!caf.isValid(doc))
                {
                    doc = null;
                }

                if (fileJson == "")
                {
                    doc.fileName = fileName;
                }
                else
                {
                    doc.fileName = fileJson;
                }
                return(doc);
            }
            else
            {
                return(null);
            }
        }
Exemple #8
0
 public void serialize(DocumentoModel documento)
 {
     new Json().createJson(documento);
 }