public Document Parse(string file, Document document) { var xdocument = XDocument.Load(file); if (document.Invoice == null) { document.SetInvoice(); } foreach (var element in xdocument.XPathSelectElements("NewDataSet/position")) { if (document.ProviderDocumentId == null) { document.ProviderDocumentId = element.XPathSelectElement("TTN").Value; } var docDate = element.XPathSelectElement("TTN_DATE") == null ? null : element.XPathSelectElement("TTN_DATE").Value; if (!String.IsNullOrEmpty(docDate)) { document.DocumentDate = Convert.ToDateTime(docDate); } var line = document.NewLine(); line.Product = element.XPathSelectElement("NAME_POST").Value; line.Producer = element.XPathSelectElement("PRZV_POST") == null ? null : element.XPathSelectElement("PRZV_POST").Value; line.SerialNumber = element.XPathSelectElement("SERIA") == null ? null : element.XPathSelectElement("SERIA").Value; line.Period = element.XPathSelectElement("SGODN") == null ? null : element.XPathSelectElement("SGODN").Value; line.Certificates = element.XPathSelectElement("SERT") == null ? null : element.XPathSelectElement("SERT").Value; line.CertificatesDate = element.XPathSelectElement("SERT_DATE") == null ? null : element.XPathSelectElement("SERT_DATE").Value; line.ProducerCostWithoutNDS = element.XPathSelectElement("PRCENA_BNDS") == null ? null : (decimal?)element.Get("PRCENA_BNDS"); line.Nds = element.XPathSelectElement("NDS") == null ? null : (uint?)SafeConvert.ToDecimalInvariant(element.XPathSelectElement("NDS").Value); line.Cipher = element.XPathSelectElement("SHIFR") == null ? null : element.XPathSelectElement("SHIFR").Value; line.TradeCost = element.XPathSelectElement("OPT_PRICE") == null ? null : (decimal?)element.Get("OPT_PRICE"); line.SaleCost = element.XPathSelectElement("OTP_CENA") == null ? null : (decimal?)element.Get("OTP_CENA"); line.RetailCost = element.XPathSelectElement("RCENA") == null ? null : (decimal?)element.Get("RCENA"); line.Quantity = Convert.ToUInt32(element.XPathSelectElement("KOL_TOV").Value); if (String.IsNullOrEmpty(document.Invoice.BuyerName)) { document.Invoice.BuyerName = element.XPathSelectElement("AGNABBR") == null ? null : element.XPathSelectElement("AGNABBR").Value; } if (document.Invoice.BuyerId == null) { document.Invoice.BuyerId = element.XPathSelectElement("TELEX") == null ? null : (int?)Convert.ToInt32(element.XPathSelectElement("TELEX").Value); } if (String.IsNullOrEmpty(document.Invoice.StoreName)) { document.Invoice.StoreName = element.XPathSelectElement("STORENAME") == null ? null : element.XPathSelectElement("STORENAME").Value; } } return(document); }
public BaseReport(MySqlConnection connection, DataSet dsProperties) : this() { Logger = LogManager.GetLogger(GetType()); _reportParams = new Dictionary <string, object>(); _dsReport = new DataSet(); Connection = connection; dtReportProperties = dsProperties.Tables["ReportProperties"]; dtReportPropertyValues = dsProperties.Tables["ReportPropertyValues"]; foreach (DataRow drProperty in dtReportProperties.Rows) { var currentPropertyName = drProperty[BaseReportColumns.colPropertyName].ToString(); if (!_reportParams.ContainsKey(currentPropertyName)) { _reportParamsIds.Add(currentPropertyName, Convert.ToUInt32(drProperty[BaseReportColumns.colPropertyID])); switch (drProperty[BaseReportColumns.colPropertyType].ToString()) { case "BOOL": try { _reportParams.Add(currentPropertyName, Convert.ToBoolean(Convert.ToByte(drProperty[BaseReportColumns.colPropertyValue]))); } catch (Exception ex) { throw new ReportException( $"Ошибка при конвертации параметра '{drProperty[BaseReportColumns.colPropertyType]}' из строки '{drProperty[BaseReportColumns.colPropertyValue]}'.", ex); } break; case "LIST": var listValues = new List <ulong>(); var drValues = dtReportPropertyValues.Select(BaseReportColumns.colReportPropertyID + "=" + drProperty[BaseReportColumns.colPropertyID].ToString()); foreach (DataRow drValue in drValues) { try { listValues.Add(Convert.ToUInt64(drValue[BaseReportColumns.colReportPropertyValue])); } catch (Exception ex) { throw new ReportException( $"Ошибка при конвертации параметра '{drProperty[BaseReportColumns.colPropertyType]}' из строки '{drValue[BaseReportColumns.colReportPropertyValue]}'.", ex); } } _reportParams.Add(currentPropertyName, listValues); break; case "FILE": case "STRING": _reportParams.Add(currentPropertyName, drProperty[BaseReportColumns.colPropertyValue].ToString()); break; case "DATETIME": try { if (drProperty[BaseReportColumns.colPropertyValue].ToString().Equals("NOW", StringComparison.OrdinalIgnoreCase)) { _reportParams.Add(currentPropertyName, DateTime.Now); } else { _reportParams.Add(currentPropertyName, DateTime.ParseExact(drProperty[BaseReportColumns.colPropertyValue].ToString(), MySqlConsts.MySQLDateFormat, null)); } } catch (Exception ex) { throw new ReportException( $"Ошибка при конвертации параметра '{drProperty[BaseReportColumns.colPropertyType]}' из строки '{drProperty[BaseReportColumns.colPropertyValue]}'.", ex); } break; case "INT": case "ENUM": try { string val = drProperty[BaseReportColumns.colPropertyValue].ToString(); if (!String.IsNullOrEmpty(val)) { _reportParams.Add(currentPropertyName, Convert.ToInt32(drProperty[BaseReportColumns.colPropertyValue].ToString())); } } catch (Exception ex) { throw new ReportException( $"Ошибка при конвертации параметра '{drProperty[BaseReportColumns.colPropertyType]}' из строки '{drProperty[BaseReportColumns.colPropertyValue]}'.", ex); } break; case "PERCENT": try { string val = drProperty[BaseReportColumns.colPropertyValue].ToString(); if (!String.IsNullOrEmpty(val)) { val = val.Replace(",", "."); _reportParams.Add(currentPropertyName, SafeConvert.ToDecimalInvariant(val)); } } catch (Exception ex) { throw new ReportException( $"Ошибка при конвертации параметра '{drProperty[BaseReportColumns.colPropertyType]}' из строки '{drProperty[BaseReportColumns.colPropertyValue]}'.", ex); } break; default: throw new ReportException( $"Неизвестный тип параметра : '{drProperty[BaseReportColumns.colPropertyType].ToString()}'."); } } else { throw new ReportException($"Параметр '{currentPropertyName}' задан дважды."); } } }