public bool InsertInDatabase(DBDocument Document, Extras.Common.Method.ListUtilClass List, string TypeRC) { bool insert = false; int IDC = 0; MS.GeneraLog.IngresoBD01("---------- INICIO DE INGRESO A BD ----------"); MS.GeneraLog.IngresoBD01("Nombre del Archivo a guardar: " + MS.FileLocation); if (!MS.DocumentType.StartsWith("R")) { if (DBConnection.SP_ValidarDocumentoExiste(MS.DocumentName)) { MS.GeneraLog.IngresoBD01("Documento " + MS.DocumentName + " ya fue ingresado a la Base de Datos"); MS.GeneraLog.IngresoBD01("---------- FIN DE INGRESO A BD ----------"); MS.GeneraLog.RegistraError(MS.FileName); return(false); } try { IDC = DBConnection.SP_InsertaCabeceraDocumento(Document.Cabecera); if (IDC > 0) { string IdDetail = DBConnection.SP_InsertaDetalleDocumento(Document, IDC); if (IdDetail != "1") { insert = false; MS.GeneraLog.RegistraError(MS.FileName); } else { insert = true; } } else { insert = false; MS.GeneraLog.RegistraError(MS.FileName); } } catch (Exception e) { insert = false; MS.GeneraLog.IngresoBD01("Error al ingresar a Base de Datos: " + e.Message); MS.GeneraLog.RegistraError(MS.FileName); } } else { if (MS.DocumentType == "RC") { if (Document.RBD.Count > 0) { Document.RBC.NUM_CPE = MS.DocumentName; Document.RBC.NUM_SEC = MS.SummaryNumber; //IDC = DBConnection.SP_InsertaCabeceraRC(Document.RBC, List); IDC = DBConnection.SP_InsertaCabeceraRC(Document.RBC, List, TypeRC); //nuevo resumen Document.RBC.ID_RBC = IDC; if (IDC > 0) { //DBConnection.SP_InsertaDetalleRC(IDC, Document.RBD); comment //DBConnection.SP_InsertaDetalleRC(IDC, Document.RBD); ya no inserta detalle insert = true; } } } if (MS.DocumentType == "RA" || MS.DocumentType == "RR") { if (Document.RAD.Count > 0) { Document.RAC.NUM_CPE = MS.DocumentName; Document.RAC.NUM_SEC = MS.SummaryNumber; IDC = DBConnection.SP_InsertaCabeceraRA(Document.RAC); Document.RAC.ID_RAC = IDC; if (IDC > 0) { DBConnection.SP_InsertaDetalleRA(Document.RAD, Document.RAC); insert = true; } } } } MS.GeneraLog.IngresoBD01("---------- FIN DE INGRESO A BD ----------: " + insert); return(insert); }
//public bool GenerateXML() public Extras.Common.Method.ListUtilClass GenerateXML(string TypeRC) { //ListDocs.ListBoletas = new List<RBoletasDetalle>(); ListDocs = new Extras.Common.Method.ListUtilClass(); bool isGenerated = false; //InsertaTC(); exchange rate RBD = ordenaResumen(TypeRC); var temp = new List <RBoletasDetalle>(); var temp_XGN = new List <RBoletasDetalle>(); var temp_NCD = new List <RBoletasDetalle>(); var temp_ANS = new List <RBoletasDetalle>(); foreach (var doc in RBD) { if (doc.SYSTEM_STATUS == "XGN") { if (doc.SUMMARY == 0) { temp_XGN.Add(doc); } } else if (doc.SYSTEM_STATUS == "ANS") { if (doc.VOIDED == 0) { temp_ANS.Add(doc); } if (doc.SUMMARY == 0) { temp_XGN.Add(doc); } } if (doc.TPO_CPE.Contains("07") || doc.TPO_CPE.Contains("08")) { temp_NCD.Add(doc); } } if (TypeRC == "ANS") { //temp = temp_ANS; foreach (var ans in temp_ANS) { if (ans.STATUS_RC_DOC != 3) { temp.Add(ans); } } } else if (TypeRC == "XGN") { foreach (var d in temp_XGN) { if (!d.TPO_CPE.Contains("07") && !d.TPO_CPE.Contains("08") && d.STATUS_RC_DOC < 1) { temp.Add(d); } } if (temp_NCD.Count > 0) { ListDocs.ListNotesDoc = temp_NCD; } //if(temp_ANS.Count > 0) //{ // ListDocs.ListBoletasLow = temp_ANS; //} //ListDocs.ListBoletasLow = temp_ANS; ListDocs.ListBoletasLow.Add(new RBoletasDetalle() { NUM_CPE = "" }); } else { foreach (var ncd in temp_NCD) { if (ncd.STATUS_RC_DOC <= 1) { temp.Add(ncd); } } //temp = temp_NCD; //if (temp_ANS.Count > 0) //{ // ListDocs.ListBoletasLow = temp_ANS; //} //ListDocs.ListBoletasLow = temp_ANS; ListDocs.ListBoletasLow.Add(new RBoletasDetalle() { NUM_CPE = "" }); } #region old ////if (TypeRC == "ANS") ////{ //// foreach (var b in RBD) //// { //// if (b.SYSTEM_STATUS.Contains(TypeRC) && b.VOIDED == 0) { temp.Add(b); } //// } ////} ////else if (TypeRC == "NCD") ////{ //// foreach (var b in RBD) //// { //// if (b.SYSTEM_STATUS.ToUpper().Contains("ANS")) //// { //// ListDocs.ListBoletasLow.Add(b); //// } //// if (!b.TPO_CPE.Contains("03") && (b.SUMMARY <= 0)) //// { //// temp.Add(b); //// } //// } ////} ////else ////{ //// foreach (var b in RBD) //// { //// if (b.SYSTEM_STATUS.ToUpper().Contains("ANS")) //// { //// ListDocs.ListBoletasLow.Add(b); //// } //// if (b.TPO_CPE == "07" || b.TPO_CPE == "08") //// { //// ListDocs.ListNotesDoc.Add(b); //// } //// if (b.SUMMARY == 0 && !b.TPO_CPE.Contains("07") && !b.TPO_CPE.Contains("08")) //// { //// temp.Add(b); //// } //// } ////} //if (TypeRC == "ANS") //{ // foreach (var b in RBD) // { // if (b.SYSTEM_STATUS.Contains(TypeRC) && b.VOIDED == 0) // { // temp.Add(b); // } // } //} //else //{ // foreach (var b in RBD) // { // if (b.SYSTEM_STATUS.ToUpper().Contains("ANS")) // { // ListDocs.ListBoletasLow.Add(b); // } // if (b.SUMMARY == 0) // { // temp.Add(b); // } // if (b.TPO_CPE == "07" || b.TPO_CPE == "08") // { // ListDocs.ListNotesDoc.Add(b); // } // } //} //foreach (var b in RBD) //{ //if (b.SYSTEM_STATUS.ToUpper().Contains("ANS")) //{ // ListDocs.ListBoletasLow.Add(b); //} //if (b.SYSTEM_STATUS.ToUpper().Contains(TypeRC)) //{ // temp.Add(b); //} //} #endregion #region old cant //if (temp.Count > 1000) //{ // var tmp = new List<RBoletasDetalle>(); // for (int i = 0; i < 1000; i++) // { // tmp.Add(temp[i]); // } // RBD = tmp; // ListDocs.CantDocs = temp.Count - 1000; //} //else //{ // RBD = temp; //} #endregion if (temp.Count > 500) { var tmp = new List <RBoletasDetalle>(); for (int i = 0; i < 500; i++) { tmp.Add(temp[i]); } RBD = tmp; ListDocs.CantDocs = temp.Count - 500; } else { RBD = temp; } if (RBD.Count == 0) { MS.GeneraLog.IngresoBD01($"No se obtuvieron boletas y/o documentos asociados de la fecha {MS.SummaryDate}"); //return false; return(ListDocs); } else { if (!DBConnection.ExchangueRateExists && DBConnection.DocumentDolarExists) { MS.GeneraLog.IngresoBD01($"No se ha configurado el tipo de cambio para el día {MS.SummaryDate}"); //return false; return(ListDocs); } } getDocumentName(); MS.getFileNames(); try { MS.GeneraLog.GeneracionXML("----- Inicio de Generación de Archivo: " + MS.XMLIFileLocation); XmlSerializer mySerializer = new XmlSerializer(typeof(SummaryDocumentsType)); XmlSerializerNamespaces ns = getNamespaces(); XmlWriterSettings setting = getSettings(); SummaryDocumentsType sum = new SummaryDocumentsType(); sum.AccountingSupplierParty = getSupplierPartyType(); sum.Signature = getSignatureType(); sum.UBLVersionID = getUBLVersionID(); sum.CustomizationID = getCustomizationID(); sum.ID = getID(); sum.IssueDate = getIssueDate(); sum.ReferenceDate = getReferenceDate(); sum.Note = getNote(); sum.SummaryDocumentsLine = getSummaryDocumentsLine(1, "", TypeRC); sum.UBLExtensions = getUBLExtensions(); StringWriterWithEncoding iso88591StringWriter = new StringWriterWithEncoding(ISO_8859_1); XmlSerializer serializer = new XmlSerializer(typeof(SummaryDocumentsType)); XmlWriter writerx = XmlWriter.Create(iso88591StringWriter, setting); serializer.Serialize(writerx, sum, ns); XMLI = iso88591StringWriter.ToString(); XMLI = XMLI.Replace("<", "<"); XMLI = XMLI.Replace(">", ">"); XMLI = XMLI.Replace("&", "&"); using (StreamWriter sw = new StreamWriter(MS.XMLIFileLocation, false, ISO_8859_1)) { sw.Write(XMLI); } SXMLI = GenerateStreamFromString(XMLI); isGenerated = true; } catch (Exception ex) { MS.GeneraLog.GeneracionXML("Ocurrió un error al momento de generar el XML Inicial"); MS.GeneraLog.GeneracionXML("Error: " + ex.Message); } //return isGenerated; return(ListDocs); }
//public bool Generate() //public object Generate(string TypeRC) public Extras.Common.Method.ListUtilClass Generate(string TypeRC) { Extras.Common.Method.ListUtilClass ListResult = new Extras.Common.Method.ListUtilClass(); //object obj = new object(); bool isGenerated = false; bool isSigned = false; DateTime Actual = DateTime.Now; #region Generacion y Firma del XML switch (MS.DocumentType) { case "01": case "03": EInvoice inv = new EInvoice(MS, VD); if (inv.GenerateXML()) { if (Sign(inv.SXMLI)) { isSigned = true; } } break; case "07": ECreditNote cre = new ECreditNote(MS, VD); if (cre.GenerateXML()) { if (Sign(cre.SXMLI)) { isSigned = true; } } break; case "08": EDebitNote deb = new EDebitNote(MS, VD); if (deb.GenerateXML()) { if (Sign(deb.SXMLI)) { isSigned = true; } } break; case "20": ERetention ret = new ERetention(MS, VD); if (ret.GenerateXML()) { if (Sign(ret.SXMLI)) { isSigned = true; } } break; case "40": break; case "RC": DocumentTypesNew.ESummaryDocumentsNew summ = new DocumentTypesNew.ESummaryDocumentsNew(MS); ListResult = summ.GenerateXML(TypeRC); ////if (summ.GenerateXML()) if (ListResult.Count > 0) { MS = summ.MS; if (Sign(summ.SXMLI)) { Document = new DBDocument(); Document.RBC = summ.RBC; Document.RBD = summ.RBD; Document.RBC.FEC_ENV = DateTime.Parse($"{Actual.Year}-{Actual.Month.ToString().PadLeft(2, '0')}-{Actual.Day.ToString().PadLeft(2, '0')}"); Document.RBC.FEC_CAD = Actual.ToString(); Document.RBC.VAR_FIR = GetBytes(MS.XMLS); isSigned = true; } } //ESummaryDocuments sum = new ESummaryDocuments(MS); //if (sum.GenerateXML()) //{ // MS = sum.MS; // if (Sign(sum.SXMLI)) // { // Document = new DBDocument(); // Document.RBC = sum.RBC; // Document.RBD = sum.RBD; // Document.RBC.FEC_ENV = DateTime.Parse($"{Actual.Year}-{Actual.Month.ToString().PadLeft(2, '0')}-{Actual.Day.ToString().PadLeft(2, '0')}"); // Document.RBC.FEC_CAD = Actual.ToString(); // Document.RBC.VAR_FIR = GetBytes(MS.XMLS); // isSigned = true; // } //} break; case "RA": case "RR": EVoidedDocuments voi = new EVoidedDocuments(MS); if (voi.GenerateXML()) { MS = voi.MS; if (Sign(voi.SXMLI)) { Document = new DBDocument(); Document.RAC = voi.RAC; Document.RAD = voi.RAD; Document.RAC.FEC_ENV = DateTime.Parse($"{Actual.Year}-{Actual.Month.ToString().PadLeft(2, '0')}-{Actual.Day.ToString().PadLeft(2, '0')}"); Document.RAC.FEC_ANU = DateTime.Parse($"{Actual.Year}-{Actual.Month.ToString().PadLeft(2, '0')}-{Actual.Day.ToString().PadLeft(2, '0')}"); Document.RAC.FEC_REF = DateTime.Parse(MS.SummaryDate); Document.RAC.FEC_CAD = Actual.ToString(); Document.RAC.VAR_FIR = GetBytes(MS.XMLS); Document.RAC.TIPO = MS.DocumentType; isSigned = true; } } break; default: MS.GeneraLog.GeneracionXML($"El Tipo de Documento {MS.DocumentType} no está permitido"); MS.GeneraLog.RegistraError(MS.DocumentName); isSigned = false; break; } #endregion if (isSigned && !MS.DocumentType.StartsWith("R")) { if (FillDocument()) { isGenerated = true; //return isGenerated; ListResult.Generate = true; return(ListResult); } } if (isSigned && MS.DocumentType.StartsWith("R")) { isGenerated = true; //obj = new Extras.Common.Method.ListUtilClass(); ListResult.Generate = true; //obj = ListResult; return(ListResult); } //return isGenerated; return(ListResult); }