public static Boolean ProcessSalesOrder(string fileContent, out salesOrder document)
        {
            document = new salesOrder();

            DALPortalDataContext dc = new DALPortalDataContext();
            Boolean update;

            try
            {
                XDocument xmldoc = XDocument.Parse(fileContent);
                XElement docTypeElement = xmldoc.Element("ORDERS05");
                XElement iDocElement = docTypeElement.Element("IDOC");

                // Company
                XElement companyElement = iDocElement.Elements("E1EDK14").ToList().Where(c => c.Element("QUALF").Value.Equals("008")).FirstOrDefault();
                String sectionCode = companyElement.Element("ORGID").Value;
                String companyCode = CompanyClass.GetCompanyBasedOnSection(sectionCode, dc);

                String ecc6DocNum = RemoveLeadingZeros(iDocElement.Element("E1EDK01").Element("BELNR").Value);

                // Check if Sales Order already exists
                document = dc.salesOrders.Where(c => c.documentNumber.Equals(ecc6DocNum) && c.companyCode.Equals(companyCode)).FirstOrDefault();
                if (document == null)
                {
                    document = new salesOrder();
                    document.companyCode = companyCode;
                    document.documentNumber = ecc6DocNum;
                    document.docStatusCode = "O";
                    update = false;
                }
                else
                {
                    update = true;
                }

                // Document Header info
                String sapECC6BusinessPartnerCode = Convert.ToInt32(iDocElement.Element("E1EDK01").Element("RECIPNT_NO").Value).ToString();
                businessPartner bp = Objects.BusinessPartnerClass.GetBusinessPartner(sapECC6BusinessPartnerCode, document.companyCode, dc);
                if (bp == null)
                    throw new Exception(String.Format("BusinessPartner {0} does not exists.", sapECC6BusinessPartnerCode));

                document.businessPartnerId = bp.businessPartnerId;
                document.currencyCode = iDocElement.Element("E1EDK01").Element("CURCY").Value;
                document.currencyRate = Convert.ToDecimal(iDocElement.Element("E1EDK01").Element("WKURS").Value, NumberFormatInfo.InvariantInfo);

                // Customer Reference
                XElement custRef = iDocElement.Elements("E1EDK02").ToList().Where(c => c.Element("QUALF").Value.Equals("001")).FirstOrDefault();
                document.customerReference = custRef.Element("BELNR").Value;

                // Dates
                XElement delDateElement = iDocElement.Elements("E1EDK03").ToList().Where(c => c.Element("IDDAT").Value.Equals("002")).FirstOrDefault();
                document.deliveryDate = DateTime.ParseExact(delDateElement.Element("DATUM").Value, "yyyyMMdd", CultureInfo.CurrentCulture);

                XElement del2DateElement = iDocElement.Elements("E1EDK03").ToList().Where(c => c.Element("IDDAT").Value.Equals("012")).FirstOrDefault();
                document.docDate = DateTime.ParseExact(del2DateElement.Element("DATUM").Value, "yyyyMMdd", CultureInfo.CurrentCulture);

                XElement del3DateElement = iDocElement.Elements("E1EDK03").ToList().Where(c => c.Element("IDDAT").Value.Equals("025")).FirstOrDefault();
                document.createDate = DateTime.ParseExact(del3DateElement.Element("DATUM").Value, "yyyyMMdd", CultureInfo.CurrentCulture);

                // Sales Order lines
                foreach (var orderLineElement in iDocElement.Elements("E1EDP01").ToList())
                {
                    salesOrderLine line = new salesOrderLine();

                    Int32 lineNum = Convert.ToInt32(orderLineElement.Element("POSEX").Value);

                    Boolean newLine = false;
                    if (update && document.salesOrderLines.Any(c => c.lineNum.Equals(lineNum)))
                    {
                        line = document.salesOrderLines.Where(c => c.lineNum.Equals(lineNum)).FirstOrDefault();
                    }
                    else
                    {
                        line.lineNum = lineNum;
                        newLine = true;
                    }

                    // Get Item (remove leading zero's)
                    XElement ItemCodeElement = orderLineElement.Elements("E1EDP19").ToList().Where(c => c.Element("QUALF").Value.Equals("002")).FirstOrDefault();

                    String itemCode = RemoveLeadingZeros(ItemCodeElement.Element("IDTNR").Value);
                    String itemName = ItemCodeElement.Element("KTEXT").Value;

                    item item = ItemClass.GetItem(itemCode, companyCode, dc);
                    if (item == null)
                        item = ItemClass.CreateItem(itemCode, itemName, companyCode);

                    line.itemId = item.itemId;
                    line.lineNum = Convert.ToInt32(orderLineElement.Element("POSEX").Value);
                    line.uomCodeOrg = orderLineElement.Element("MENEE").Value;
                    line.price = Convert.ToDecimal(orderLineElement.Element("VPREI").Value, NumberFormatInfo.InvariantInfo);
                    line.itemDescription = itemName;

                    if (orderLineElement.Element("MENGE") != null)
                        line.quantity = Convert.ToDecimal(orderLineElement.Element("MENGE").Value, NumberFormatInfo.InvariantInfo);
                    else
                        line.quantity = 0;

                    if (orderLineElement.Element("NTGEW") != null)
                        line.weight = Convert.ToDecimal(orderLineElement.Element("NTGEW").Value, NumberFormatInfo.InvariantInfo);
                    else
                        line.weight = 0;

                    line.lineTypeCode = orderLineElement.Element("PSTYV").Value;
                    line.lineStatusCode = "O";

                    line.customerReference = String.Empty;
                    line.customerItemCode = String.Empty;

                    XElement customerItemCodeElement = orderLineElement.Elements("E1EDP19").ToList().Where(c => c.Element("QUALF").Value.Equals("001")).FirstOrDefault();
                    if (customerItemCodeElement != null)
                    {
                        XElement customerItemCodeElement2 = customerItemCodeElement.Element("IDTNR");
                        if (customerItemCodeElement2 != null)
                            line.customerItemCode = RemoveLeadingZeros(customerItemCodeElement2.Value);
                    }

                    XElement customerReference = orderLineElement.Elements("E1EDPT1").ToList().Where(c => c.Element("TDID").Value.Equals("0002")).FirstOrDefault();
                    if (customerReference != null)
                    {
                        XElement customerReference2 = customerReference.Element("E1EDPT2");
                        if (customerReference2 != null)
                            line.customerReference = customerReference2.Element("TDLINE") != null ? customerReference2.Element("TDLINE").Value : String.Empty;
                    }

                    // Line is cancelled when field ABGRU is filled
                    var lineStatusElement = orderLineElement.Element("ABGRU"); ;
                    if (lineStatusElement != null)
                        if (!String.IsNullOrWhiteSpace(lineStatusElement.Value))
                            line.lineStatusCode = "C";

                    // ShipDate
                    String deliveryDate = GetSOLineDeliveryDate(orderLineElement);
                    if (!String.IsNullOrEmpty(deliveryDate))
                        line.deliveryDate = DateTime.ParseExact(deliveryDate, "yyyyMMdd", CultureInfo.CurrentCulture);

                    // Certificate as Ordered
                    Boolean certAsOrdered = orderLineElement.Elements("E1EDP02").ToList().Any(c => c.Element("QUALF").Value.Equals("902") && c.Element("BELNR").Value.Equals("ZCPT"));
                    line.certOrdered = certAsOrdered;

                    if (newLine)
                        document.salesOrderLines.Add(line);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(String.Format("Error converting Sales Order {0} for company {1} from SAP ECC6 XML format. Error: {2}", document.documentNumber, document.companyCode, ex.Message), "ProcessSalesOrder");
                return false;
            }

            if (update)
            {
                try
                {
                    dc.SubmitChanges();
                    Trace.WriteLine(String.Format("Updating Sales Order {0} for company {1} Successfull.", document.documentNumber, document.companyCode), "ProcessSalesOrder");
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(String.Format("Error Updating Sales Order {0} for company {1}. Error: {2}", document.documentNumber, document.companyCode, ex.Message), "ProcessSalesOrder");
                    return false;
                }
            }
            else
            {
                try
                {
                    dc.salesOrders.InsertOnSubmit(document);
                    dc.SubmitChanges();
                    Trace.WriteLine(String.Format("Creating Sales Order {0} for company {1} Successfull.", document.documentNumber, document.companyCode), "ProcessSalesOrder");
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(String.Format("Error Creating Sales Order {0} for company {1}. Error: {2}", document.documentNumber, document.companyCode, ex.Message), "ProcessSalesOrder");
                    return false;
                }
            }

            return true;
        }
Exemple #2
0
partial         void UpdatesalesOrder(salesOrder instance)
        {
            instance.timeStampLastUpdate = DateTime.Now;
            this.ExecuteDynamicUpdate(instance);
        }
Exemple #3
0
partial         void InsertsalesOrder(salesOrder instance)
        {
            instance.timeStampCreated = DateTime.Now;
            instance.docType = "SO";
            this.ExecuteDynamicInsert(instance);
        }
		private void detach_salesOrders(salesOrder entity)
		{
			this.SendPropertyChanging();
			entity.document = null;
		}
        public Boolean ProcessDataFromXML(String databaseName, String fileContent)
        {
            Boolean returnValue = true;

              try
              {
            // Check xml type

            // SAPECC6
            if (fileContent.Contains("IDOC"))
            {
              businessPartner customer = new businessPartner();
              DALPortalDataContext dc = new DALPortalDataContext();
                    String companyCode;

              //Customer
              if (fileContent.Contains("DEBMAS03"))
              {
            if (!XMLSAPECC6Processor.ProcessCustomer(fileContent, out customer, out companyCode))
              return false;

            CreateUsers(customer, companyCode, dc);
              }
              //SalesOrder
              if (fileContent.Contains("ORDERS05"))
              {
            salesOrder document = new salesOrder();

            if (!XMLSAPECC6Processor.ProcessSalesOrder(fileContent, out document))
              return false;
              }
              //Delivery
              if (fileContent.Contains("DELVRY03"))
              {
            delivery document = new delivery();

            if (!XMLSAPECC6Processor.ProcessDelivery(fileContent, out document))
              return false;
              }

                    //Item
                    if (fileContent.Contains("MATMAS05"))
                    {
                        if (!XMLSAPECC6Processor.ProcessItem(fileContent))
                            return false;
                    }
            }
              }
              catch (Exception ex)
              {
            Trace.WriteLine(String.Format("Process XML file Failed. Error: {0}", ex.Message), "ProcessDataFromXML");
            return false;
              }

              return returnValue;
        }
		private void attach_salesOrders(salesOrder entity)
		{
			this.SendPropertyChanging();
			entity.docStatus = this;
		}
		private void attach_salesOrders(salesOrder entity)
		{
			this.SendPropertyChanging();
			entity.currency = this;
		}
 partial void DeletesalesOrder(salesOrder instance);
 partial void UpdatesalesOrder(salesOrder instance);
 partial void InsertsalesOrder(salesOrder instance);