FillBy() private méthode

private FillBy ( Order dataTable, global OrderID ) : int
dataTable Order
OrderID global
Résultat int
        public override SdataTransactionResult Delete(string id)
        {
            SdataTransactionResult tmpTransactionResult;
            DataSets.OrderTableAdapters.Order_DetailsTableAdapter detailsTableAdapter = new Order_DetailsTableAdapter();
            DataSets.Order order = new DataSets.Order();

            int productId;
            int orderId;
            if (GetLocalIds(id, out orderId, out productId))
            {
                using (OleDbConnection connection = new OleDbConnection(_context.Config.ConnectionString))
                {
                    try
                    {
                        detailsTableAdapter.Connection = connection;
                        int recordCount = detailsTableAdapter.FillBy(order.Order_Details, orderId);
                        if (recordCount == 0)
                        {
                            tmpTransactionResult = new SdataTransactionResult();
                            tmpTransactionResult.LocalId = id;
                            tmpTransactionResult.HttpMethod = "DELETE";
                            tmpTransactionResult.ResourceKind = _resourceKind;
                            tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                            tmpTransactionResult.HttpMessage = ("salesorder not found");
                            return tmpTransactionResult;
                        }

                        foreach (Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order.Order_DetailsRow row in order.Order_Details.Rows)
                        {
                            if (row.ProductID == productId)
                            {
                                row.Delete();
                                break;
                            }
                        }

                        detailsTableAdapter.Update(order.Order_Details);
                        tmpTransactionResult = new SdataTransactionResult();
                        tmpTransactionResult.LocalId = id;
                        tmpTransactionResult.HttpMethod = "DELETE";
                        tmpTransactionResult.ResourceKind = _resourceKind;
                        tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.OK;
                        return tmpTransactionResult;
                    }
                    catch (Exception e)
                    {
                        tmpTransactionResult = new SdataTransactionResult();
                        tmpTransactionResult.LocalId = id;
                        tmpTransactionResult.HttpMethod = "DELETE";
                        tmpTransactionResult.ResourceKind = _resourceKind;
                        tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                        tmpTransactionResult.HttpMessage = e.Message;
                        return tmpTransactionResult;
                    }

                }

            }
            tmpTransactionResult = new SdataTransactionResult();
            tmpTransactionResult.LocalId = id;
            tmpTransactionResult.HttpMethod = "DELETE";
            tmpTransactionResult.ResourceKind = _resourceKind;
            tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
            tmpTransactionResult.HttpMessage = "Not found";
            return tmpTransactionResult;
        }
        public override SdataTransactionResult Update(PayloadBase payload, List<SyncFeedEntryLink> links)
        {
            SdataTransactionResult tmpTransactionResult;

            if (!(payload is SalesOrderLinePayload))
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = "PUT";
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = ("salesorder payload missing");
                return tmpTransactionResult;
            }
            salesOrderLinetype soLine = (payload as SalesOrderLinePayload).SalesOrderLinetype;
            DataSets.OrderTableAdapters.Order_DetailsTableAdapter detailsTableAdapter = new Order_DetailsTableAdapter();
            DataSets.Order order = new DataSets.Order();

            int productId;
            int orderId;
            if (GetLocalIds(payload.LocalID, out orderId, out productId))
            {
                using (OleDbConnection connection = new OleDbConnection(_context.Config.ConnectionString))
                {
                    try
                    {
                        detailsTableAdapter.Connection = connection;
                        int recordCount = detailsTableAdapter.FillBy(order.Order_Details, orderId);
                        if (recordCount == 0)
                        {
                            tmpTransactionResult = new SdataTransactionResult();
                            tmpTransactionResult.LocalId = payload.LocalID;
                            tmpTransactionResult.HttpMethod = "PUT";
                            tmpTransactionResult.ResourceKind = _resourceKind;
                            tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                            tmpTransactionResult.HttpMessage = ("salesorder not found");
                            return tmpTransactionResult;
                        }

                        foreach (Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order.Order_DetailsRow detailsRow in order.Order_Details.Rows)
                        {
                            if (detailsRow.ProductID == productId)
                            {
                                detailsRow.ModifyUser = _context.Config.CrmUser;
                                detailsRow.ModifyID = _context.Config.SequenceNumber;
                                if (soLine.quantitySpecified)
                                    detailsRow.Quantity = Convert.ToInt16(soLine.quantity);
                                else
                                    detailsRow.Quantity = 0;

                                if (soLine.initialPriceSpecified)
                                    detailsRow.UnitPrice = (Decimal)soLine.initialPrice;
                                else
                                    detailsRow.UnitPrice = 0;

                                if ((!soLine.discountTotalSpecified) || (detailsRow.Quantity == 0) || (detailsRow.UnitPrice == 0))
                                {
                                    detailsRow.Discount = (float)0;
                                }
                                else
                                {
                                    // discountPC = discountsum / qunatity * listprice
                                    //detailRow.Discount = Convert.ToSingle((decimal)lineItemDoc.discountsum.Value / ((decimal)detailRow.Quantity * detailRow.UnitPrice));
                                    float discount = Convert.ToSingle((decimal)soLine.discountTotal / (detailsRow.UnitPrice));
                                    if (discount > 1)
                                        discount = 0;
                                    detailsRow.Discount = discount;
                                }

                                break;
                            }
                        }

                        detailsTableAdapter.Update(order.Order_Details);
                        tmpTransactionResult = new SdataTransactionResult();
                        tmpTransactionResult.LocalId = payload.LocalID;
                        tmpTransactionResult.HttpMethod = "PUT";
                        tmpTransactionResult.ResourceKind = _resourceKind;
                        tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.OK;
                        return tmpTransactionResult;
                    }
                    catch (Exception e)
                    {
                        tmpTransactionResult = new SdataTransactionResult();
                        tmpTransactionResult.LocalId = payload.LocalID;
                        tmpTransactionResult.HttpMethod = "PUT";
                        tmpTransactionResult.ResourceKind = _resourceKind;
                        tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                        tmpTransactionResult.HttpMessage = e.Message;
                        return tmpTransactionResult;
                    }

                }

            }
            tmpTransactionResult = new SdataTransactionResult();
            tmpTransactionResult.LocalId = payload.LocalID;
            tmpTransactionResult.HttpMethod = "PUT";
            tmpTransactionResult.ResourceKind = _resourceKind;
            tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
            tmpTransactionResult.HttpMessage = "Not found";
            return tmpTransactionResult;
        }
        public override SdataTransactionResult Delete(string localID)
        {
            SdataTransactionResult tmpTransactionResult;
            DataSets.OrderTableAdapters.OrdersTableAdapter tableAdapter = new OrdersTableAdapter();
            DataSets.OrderTableAdapters.Order_DetailsTableAdapter detailsTableAdapter = new Order_DetailsTableAdapter();
            DataSets.Order order = new DataSets.Order();

            int id;
            if (!(Int32.TryParse(localID, out id)))
                id = 0;
            int recordCount;

            using (OleDbConnection connection = new OleDbConnection(_context.Config.ConnectionString))
            {
                try
                {
                    tableAdapter.Connection = connection;
                    recordCount = tableAdapter.FillBy(order.Orders, id);
                    if (recordCount == 0)
                    {
                        tmpTransactionResult = new SdataTransactionResult();
                        tmpTransactionResult.LocalId = localID;
                        tmpTransactionResult.HttpMethod = "DELETE";
                        tmpTransactionResult.ResourceKind = _resourceKind;
                        tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                        tmpTransactionResult.HttpMessage = ("salesorder not found");
                        return tmpTransactionResult;
                    }

                    detailsTableAdapter.Connection = connection;
                    detailsTableAdapter.FillBy(order.Order_Details, id);
                    foreach (DataRow row in order.Order_Details.Rows)
                    {
                        row.Delete();
                    }
                    order.Orders[0].Delete();
                    detailsTableAdapter.Update(order.Order_Details);
                    tableAdapter.Update(order.Orders);
                    tmpTransactionResult = new SdataTransactionResult();
                    tmpTransactionResult.LocalId = localID;
                    tmpTransactionResult.HttpMethod = "DELETE";
                    tmpTransactionResult.ResourceKind = _resourceKind;
                    tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.OK;
                    return tmpTransactionResult;
                }
                catch (Exception e)
                {
                    tmpTransactionResult = new SdataTransactionResult();
                    tmpTransactionResult.LocalId = localID;
                    tmpTransactionResult.HttpMethod = "DELETE";
                    tmpTransactionResult.ResourceKind = _resourceKind;
                    tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                    tmpTransactionResult.HttpMessage = e.Message;
                    return tmpTransactionResult;
                }

            }
        }
        public override SdataTransactionResult Update(PayloadBase payload, List<SyncFeedEntryLink> links)
        {
            SdataTransactionResult tmpTransactionResult;
            salesOrdertype salesorder = null;

            #region check input values
            if (!(payload is SalesOrderPayload))
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = "PUT";
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = ("salesorder payload missing");
                return tmpTransactionResult;
            }
            salesorder = (payload as SalesOrderPayload).SalesOrdertype;

            if (salesorder == null)
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = "PUT";
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = ("salesorder payload missing");
                return tmpTransactionResult;

            }
            foreach (SyncFeedEntryLink link in links)
            {
                if (link.LinkRel != RelEnum.related.ToString())
                    continue;
                string localID = string.Empty;
                if (link.Href.StartsWith(_context.DatasetLink, StringComparison.InvariantCultureIgnoreCase))
                    if (link.Href.LastIndexOf("('") < link.Href.LastIndexOf("')"))
                        localID = link.Href.Substring(link.Href.LastIndexOf("('") + 2,
                            link.Href.LastIndexOf("')") - link.Href.LastIndexOf("('") - 2);

                SupportedResourceKinds reskind = ResourceKindHelpers.GetResourceKind(link.PayloadPath);

                if (localID == string.Empty)
                    localID = GetLocalId(link.Uuid, reskind);

                payload.ForeignIds.Add(link.PayloadPath, localID);
            }

            if (payload == null)
                return null;

            string customerID;

            if (!payload.ForeignIds.TryGetValue("tradingAccount", out customerID))
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = "PUT";
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = ("Trading Acount Id missing");
                return tmpTransactionResult;
            }

            if (!customerID.StartsWith(Sage.Integration.Northwind.Application.API.Constants.CustomerIdPrefix))
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = "PUT";
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = ("Salesorder submission is only supported by customers");
                return tmpTransactionResult;
            }
            #endregion

            DataSets.OrderTableAdapters.OrdersTableAdapter tableAdapter = new OrdersTableAdapter();
            DataSets.OrderTableAdapters.Order_DetailsTableAdapter detailsTableAdapter=new Order_DetailsTableAdapter();

            DataSets.Order order = new DataSets.Order();

            int id;
            if (!(Int32.TryParse(payload.LocalID, out id)))
                id = 0;
            int recordCount;

            using (OleDbConnection connection = new OleDbConnection(_context.Config.ConnectionString))
            {
                tableAdapter.Connection = connection;
                recordCount = tableAdapter.FillBy(order.Orders, id);
                if (recordCount == 0)
                    return null;

                detailsTableAdapter.Connection = connection;
                detailsTableAdapter.FillBy(order.Order_Details, id);

            }

            DataSets.Order.OrdersRow row = (DataSets.Order.OrdersRow)order.Orders[0];

            #region fill dataset from document
            try
            {
                if (!salesorder.dateSpecified)
                    row.SetOrderDateNull();
                else
                    row.OrderDate = salesorder.date;

                if (!salesorder.dueDateSpecified)
                    row.SetRequiredDateNull();
                else
                    row.RequiredDate = (DateTime)salesorder.dueDate;

                //if (orderDoc.shippedvia.IsNull)
                //    newOrder.SetShipViaNull();
                //else
                //    newOrder.ShipVia = (int)orderDoc.shippedvia.Value;

                if (salesorder.postalAddresses == null || salesorder.postalAddresses.Length == 0)
                {
                    row.SetShipAddressNull();
                    row.SetShipCityNull();
                    row.SetShipCountryNull();
                    row.SetShipPostalCodeNull();
                }
                else
                {
                    postalAddresstype postadress = salesorder.postalAddresses[0];
                    row.ShipAddress = postadress.address1;
                    row.ShipCity = postadress.townCity;
                    row.ShipPostalCode = postadress.zipPostCode;
                    row.ShipCountry = postadress.country;

                }

                if (!salesorder.carrierTotalPriceSpecified)
                    row.Freight = (decimal)0;
                else
                    row.Freight = (decimal)salesorder.carrierTotalPrice;

                //row.CreateUser = _context.Config.CrmUser;
                row.ModifyUser = _context.Config.CrmUser;
                //row.CreateID = _context.Config.SequenceNumber;
                row.ModifyID = _context.Config.SequenceNumber;

                Dictionary<Guid, SyncFeedEntryLink> itemLinks = new Dictionary<Guid, SyncFeedEntryLink>();
                GuidConverter converter = new GuidConverter();
                 Guid itemUuid;

                foreach (SyncFeedEntryLink link in links)
                {
                    SupportedResourceKinds resKind = ResourceKindHelpers.GetResourceKind(link.PayloadPath);
                    if (resKind != SupportedResourceKinds.salesOrderLines)
                        continue;
                    try
                    {

                        itemUuid = (Guid)converter.ConvertFromString(link.Uuid);
                        if (!Guid.Empty.Equals(itemUuid))
                            itemLinks.Add(itemUuid, link);
                    }
                    catch (Exception)
                    {
                    }
                }
                List<DataSets.Order.Order_DetailsRow> rowsToDelete = new List<DataSets.Order.Order_DetailsRow>();

                //foreach (DataSets.Order.Order_DetailsRow detailsRow in rowsToDelete)
                //{
                //    detailsRow.Delete();
                //}

                foreach (DataSets.Order.Order_DetailsRow detailsRow in order.Order_Details)
                {
                    string itemId = detailsRow.OrderID.ToString() + "-" + detailsRow.ProductID.ToString();
                    itemUuid = GetUuid(itemId, "", SupportedResourceKinds.salesOrderLines);
                    SyncFeedEntryLink itemlink;
                    if (itemLinks.TryGetValue(itemUuid, out itemlink))
                    {
                        string lineNumberstring = itemlink.PayloadPath.Substring(itemlink.PayloadPath.IndexOf("[") + 1, itemlink.PayloadPath.IndexOf("]") - itemlink.PayloadPath.IndexOf("[") - 1);
                        int lineNumber;
                        if (Int32.TryParse(lineNumberstring, out lineNumber))
                        {
                            if (lineNumber < salesorder.salesOrderLines.Length)
                            {
                                salesOrderLinetype soLine = salesorder.salesOrderLines[lineNumber];
                                detailsRow.ModifyUser = _context.Config.CrmUser;
                                detailsRow.ModifyID = _context.Config.SequenceNumber;
                                if (soLine.quantitySpecified)
                                    detailsRow.Quantity = Convert.ToInt16(soLine.quantity);
                                else
                                    detailsRow.Quantity = 0;

                                if (soLine.initialPriceSpecified)
                                    detailsRow.UnitPrice = (Decimal)soLine.initialPrice;
                                else
                                    detailsRow.UnitPrice = 0;

                                if ((!soLine.discountTotalSpecified) || (detailsRow.Quantity == 0) || (detailsRow.UnitPrice == 0))
                                {
                                    detailsRow.Discount = (float)0;
                                }
                                else
                                {
                                    // discountPC = discountsum / qunatity * listprice
                                    //detailRow.Discount = Convert.ToSingle((decimal)lineItemDoc.discountsum.Value / ((decimal)detailRow.Quantity * detailRow.UnitPrice));
                                    float discount = Convert.ToSingle((decimal)soLine.discountTotal / (detailsRow.UnitPrice));
                                    if (discount > 1)
                                        discount = 0;
                                    detailsRow.Discount = discount;
                                }

                            }
                        }
                        itemLinks.Remove(itemUuid);
                    }
                    else
                    {
                        //delete item
                        rowsToDelete.Add(detailsRow);
                    }

                }

                foreach (SyncFeedEntryLink itemlink in itemLinks.Values)
                {
                    {
                        try
                        {
                            int productID;
                            string productIdString;
                            string productIdPayload = String.Format("{0}/{1}", itemlink.PayloadPath, "commodity");

                            if (payload.ForeignIds.TryGetValue(productIdPayload, out productIdString))
                            {
                                try
                                {
                                    productID = Convert.ToInt32(productIdString);
                                }
                                catch (Exception)
                                {
                                    continue;
                                }
                            }
                            else
                                continue;

                            string lineNumberstring = itemlink.PayloadPath.Substring(itemlink.PayloadPath.IndexOf("[") + 1, itemlink.PayloadPath.IndexOf("]") - itemlink.PayloadPath.IndexOf("[") - 1);
                            int lineNumber;
                            if (Int32.TryParse(lineNumberstring, out lineNumber))
                            {
                                if (lineNumber < salesorder.salesOrderLines.Length)
                                {
                                    salesOrderLinetype soLine = salesorder.salesOrderLines[lineNumber];

                                    DataSets.Order.Order_DetailsRow detailRow = order.Order_Details.NewOrder_DetailsRow();
                                    soLine.applicationID = payload.LocalID + "-" + productID.ToString();
                                    detailRow.OrderID = Convert.ToInt32(payload.LocalID);
                                    detailRow.ProductID = productID;
                                    if (soLine.quantitySpecified)
                                        detailRow.Quantity = Convert.ToInt16(soLine.quantity);
                                    else
                                        detailRow.Quantity = 0;

                                    if (soLine.initialPriceSpecified)
                                        detailRow.UnitPrice = (Decimal)soLine.initialPrice;
                                    else
                                        detailRow.UnitPrice = 0;

                                    if ((!soLine.discountTotalSpecified) || (detailRow.Quantity == 0) || (detailRow.UnitPrice == 0))
                                    {
                                        detailRow.Discount = (float)0;
                                    }
                                    else
                                    {
                                        // discountPC = discountsum / qunatity * listprice
                                        //detailRow.Discount = Convert.ToSingle((decimal)lineItemDoc.discountsum.Value / ((decimal)detailRow.Quantity * detailRow.UnitPrice));
                                        float discount = Convert.ToSingle((decimal)soLine.discountTotal / (detailRow.UnitPrice));
                                        if (discount > 1)
                                            discount = 0;
                                        detailRow.Discount = discount;
                                    }

                                    detailRow.CreateUser = _context.Config.CrmUser;
                                    detailRow.ModifyUser = _context.Config.CrmUser;
                                    detailRow.CreateID = _context.Config.SequenceNumber;
                                    detailRow.ModifyID = _context.Config.SequenceNumber;
                                    order.Order_Details.AddOrder_DetailsRow(detailRow);
                                }
                            }
                        }
                        // this error occours in case of invalid data types
                        catch (Exception e)
                        {

                            tmpTransactionResult = new SdataTransactionResult();
                            tmpTransactionResult.HttpMethod = "POST";
                            tmpTransactionResult.ResourceKind = _resourceKind;
                            tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                            tmpTransactionResult.HttpMessage = e.Message;
                            return tmpTransactionResult;

                        }

                    }

                }

                using (OleDbConnection connection = new OleDbConnection(_context.Config.ConnectionString))
                {
                    OleDbTransaction transaction = null;
                    try
                    {
                        connection.Open();
                        transaction = connection.BeginTransaction();

                        tableAdapter.Connection = connection;
                        detailsTableAdapter.Connection = connection;

                        tableAdapter.SetTransaction(transaction);
                        detailsTableAdapter.SetTransaction(transaction);

                        foreach (DataSets.Order.Order_DetailsRow detailsRow in rowsToDelete)
                        {
                            detailsTableAdapter.Delete(detailsRow.OrderID,
                                detailsRow.ProductID,
                                detailsRow.UnitPrice, detailsRow.Quantity,
                                detailsRow.Discount, detailsRow.CreateID, detailsRow.CreateUser, detailsRow.ModifyID, detailsRow.ModifyUser);
                        }

                        tableAdapter.Update(order.Orders);
                        detailsTableAdapter.Update(order.Order_Details);

                        transaction.Commit();

                        tmpTransactionResult = new SdataTransactionResult();
                        tmpTransactionResult.HttpMethod = "PUT";
                        tmpTransactionResult.ResourceKind = _resourceKind;
                        tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.OK;
                        tmpTransactionResult.LocalId = payload.LocalID;
                        return tmpTransactionResult;

                    }
                    catch (Exception transactionException)
                    {
                        if (transaction != null)
                            transaction.Rollback();
                        throw;
                    }

                }
            }
            catch (Exception e)
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = "PUT";
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = e.ToString();
                return tmpTransactionResult;
            }

            #endregion
        }
        public override SdataTransactionResult Update(Sage.Common.Syndication.FeedEntry payload)
        {
            SdataTransactionResult tmpTransactionResult;
            SalesOrderFeedEntry salesorder = null;

            #region check input values
            if (!(payload is SalesOrderFeedEntry))
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = HttpMethod.PUT;
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = ("salesorder payload missing");
                return tmpTransactionResult;
            }
            salesorder = (payload as SalesOrderFeedEntry);

            if (salesorder == null)
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = HttpMethod.PUT;
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = ("salesorder payload missing");
                return tmpTransactionResult;
            }

            string customerID = "";

            if (salesorder.tradingAccount != null)
                customerID = GetLocalId(salesorder.tradingAccount.UUID, SupportedResourceKinds.tradingAccounts);

            if (String.IsNullOrEmpty(customerID))
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = HttpMethod.PUT;
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = ("Trading Acount Id missing");
                return tmpTransactionResult;
            }

            if (!customerID.StartsWith(Sage.Integration.Northwind.Application.API.Constants.CustomerIdPrefix))
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = HttpMethod.PUT;
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = ("Salesorder submission is only supported by customers");
                return tmpTransactionResult;
            }
            #endregion

            Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.OrderTableAdapters.OrdersTableAdapter tableAdapter = new OrdersTableAdapter();
            Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.OrderTableAdapters.Order_DetailsTableAdapter detailsTableAdapter = new Order_DetailsTableAdapter();

            Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order order = new Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order();

            int id;
            if (!(Int32.TryParse(payload.Key, out id)))
                id = 0;
            int recordCount;

            using (OleDbConnection connection = new OleDbConnection(_context.Config.ConnectionString))
            {
                tableAdapter.Connection = connection;
                recordCount = tableAdapter.FillBy(order.Orders, id);
                if (recordCount == 0)
                    return null;

                detailsTableAdapter.Connection = connection;
                detailsTableAdapter.FillBy(order.Order_Details, id);

            }

            Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order.OrdersRow row = (Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order.OrdersRow)order.Orders[0];

            #region fill dataset from document
            try
            {
                if (!salesorder.IsPropertyChanged("date"))
                    row.SetOrderDateNull();
                else
                    row.OrderDate = salesorder.date;

                if (!salesorder.IsPropertyChanged("dueDate"))
                    row.SetRequiredDateNull();
                else
                    row.RequiredDate = (DateTime)salesorder.dueDate;

                //if (orderDoc.shippedvia.IsNull)
                //    newOrder.SetShipViaNull();
                //else
                //    newOrder.ShipVia = (int)orderDoc.shippedvia.Value;

                if (salesorder.postalAddresses == null || salesorder.postalAddresses.Entries.Count == 0)
                {
                    row.SetShipAddressNull();
                    row.SetShipCityNull();
                    row.SetShipCountryNull();
                    row.SetShipPostalCodeNull();
                }
                else
                {
                    PostalAddressFeedEntry postadress = salesorder.postalAddresses.Entries[0];
                    row.ShipAddress = postadress.address1;
                    row.ShipCity = postadress.townCity;
                    row.ShipPostalCode = postadress.zipPostCode;
                    row.ShipCountry = postadress.country;

                }

                if (!salesorder.IsPropertyChanged("carrierTotalPrice"))
                    row.Freight = (decimal)0;
                else
                    row.Freight = (decimal)salesorder.carrierTotalPrice;

                //row.CreateUser = _context.Config.CrmUser;
                row.ModifyUser = _context.Config.CrmUser;
                //row.CreateID = _context.Config.SequenceNumber;
                row.ModifyID = _context.Config.SequenceNumber;

                Guid itemUuid;

                List<Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order.Order_DetailsRow> rowsToDelete = new List<Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order.Order_DetailsRow>();
                List<Guid> itemUuids = new List<Guid>();
                if (salesorder.salesOrderLines != null)
                {
                    foreach (SalesOrderLineFeedEntry soLine in salesorder.salesOrderLines.Entries)
                    {
                        if ((soLine.UUID != null && soLine.UUID != Guid.Empty) && (!itemUuids.Contains(soLine.UUID)))
                            itemUuids.Add(soLine.UUID);
                    }
                }

                foreach (Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order.Order_DetailsRow detailsRow in order.Order_Details)
                {
                    string itemId = detailsRow.OrderID.ToString() + "-" + detailsRow.ProductID.ToString();
                    itemUuid = GetUuid(itemId, "", SupportedResourceKinds.salesOrderLines);
                    if (itemUuids.Contains(itemUuid))
                    {
                        foreach (SalesOrderLineFeedEntry soLine in salesorder.salesOrderLines.Entries)
                        {
                            if (soLine.UUID.Equals(itemUuid))
                            {
                                if (soLine.IsDeleted)
                                {
                                    rowsToDelete.Add(detailsRow);
                                    break;
                                }
                                /*if (soLine.IsEmpty)
                                {
                                    break;
                                }*/
                                detailsRow.ModifyUser = _context.Config.CrmUser;
                                detailsRow.ModifyID = _context.Config.SequenceNumber;
                                if (soLine.IsPropertyChanged("quantity"))
                                    detailsRow.Quantity = Convert.ToInt16(soLine.quantity);
                                else
                                    detailsRow.Quantity = 0;

                                if (soLine.IsPropertyChanged("initialPrice"))
                                    detailsRow.UnitPrice = (Decimal)soLine.initialPrice;
                                else
                                    detailsRow.UnitPrice = 0;

                                if ((!soLine.IsPropertyChanged("discountTotal")) || (detailsRow.Quantity == 0) || (detailsRow.UnitPrice == 0))
                                {
                                    detailsRow.Discount = (float)0;
                                }
                                else
                                {
                                    // discountPC = discountsum / qunatity * listprice
                                    //detailRow.Discount = Convert.ToSingle((decimal)lineItemDoc.discountsum.Value / ((decimal)detailRow.Quantity * detailRow.UnitPrice));
                                    float discount = Convert.ToSingle((decimal)soLine.discountTotal / (detailsRow.UnitPrice));
                                    if (discount > 1)
                                        discount = 0;
                                    detailsRow.Discount = discount;
                                }
                                break;
                            }
                        }
                        itemUuids.Remove(itemUuid);
                    }
                    else
                    {
                        //delete item
                        rowsToDelete.Add(detailsRow);
                    }

                }

                if (salesorder.salesOrderLines != null)
                {
                    foreach (SalesOrderLineFeedEntry soLine in salesorder.salesOrderLines.Entries)
                    {
                        Guid soUuid = soLine.UUID;
                        if (itemUuids.Contains(soUuid))
                            itemUuids.Remove(soUuid);
                        else
                            continue;

                        try
                        {

                            Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order.Order_DetailsRow detailRow = order.Order_Details.NewOrder_DetailsRow();
                            Guid productUuid = soLine.commodity.UUID;
                            string productIdString = GetLocalId(productUuid, SupportedResourceKinds.commodities);

                            int productID;
                            if (!int.TryParse(productIdString, out productID))
                                continue;

                            string sorderID = payload.Key + "-" + productID.ToString();
                            detailRow.OrderID = Convert.ToInt32(payload.Key);
                            detailRow.ProductID = productID;
                            if (soLine.IsPropertyChanged("quantity"))
                                detailRow.Quantity = Convert.ToInt16(soLine.quantity);
                            else
                                detailRow.Quantity = 0;

                            if (soLine.IsPropertyChanged("initialPrice"))
                                detailRow.UnitPrice = (Decimal)soLine.initialPrice;
                            else
                                detailRow.UnitPrice = 0;

                            if ((!soLine.IsPropertyChanged("discountTotal")) || (detailRow.Quantity == 0) || (detailRow.UnitPrice == 0))
                            {
                                detailRow.Discount = (float)0;
                            }
                            else
                            {
                                // discountPC = discountsum / qunatity * listprice
                                //detailRow.Discount = Convert.ToSingle((decimal)lineItemDoc.discountsum.Value / ((decimal)detailRow.Quantity * detailRow.UnitPrice));
                                float discount = Convert.ToSingle((decimal)soLine.discountTotal / (detailRow.UnitPrice));
                                if (discount > 1)
                                    discount = 0;
                                detailRow.Discount = discount;
                            }

                            detailRow.CreateUser = _context.Config.CrmUser;
                            detailRow.ModifyUser = _context.Config.CrmUser;
                            detailRow.CreateID = _context.Config.SequenceNumber;
                            detailRow.ModifyID = _context.Config.SequenceNumber;
                            order.Order_Details.AddOrder_DetailsRow(detailRow);

                        }
                        // this error occours in case of invalid data types
                        catch (Exception e)
                        {

                            tmpTransactionResult = new SdataTransactionResult();
                            tmpTransactionResult.HttpMethod = HttpMethod.POST;
                            tmpTransactionResult.ResourceKind = _resourceKind;
                            tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                            tmpTransactionResult.HttpMessage = e.Message;
                            return tmpTransactionResult;

                        }

                    }
                }

                using (OleDbConnection connection = new OleDbConnection(_context.Config.ConnectionString))
                {
                    OleDbTransaction transaction = null;
                    try
                    {
                        connection.Open();
                        transaction = connection.BeginTransaction();

                        tableAdapter.Connection = connection;
                        detailsTableAdapter.Connection = connection;

                        tableAdapter.SetTransaction(transaction);
                        detailsTableAdapter.SetTransaction(transaction);

                        foreach (Sage.Integration.Northwind.Adapter.Data.SalesOrders.DataSets.Order.Order_DetailsRow detailsRow in rowsToDelete)
                        {
                            detailsTableAdapter.Delete(detailsRow.OrderID,
                                detailsRow.ProductID,
                                detailsRow.UnitPrice, detailsRow.Quantity,
                                detailsRow.Discount, detailsRow.CreateID, detailsRow.CreateUser, detailsRow.ModifyID, detailsRow.ModifyUser);
                        }

                        tableAdapter.Update(order.Orders);
                        detailsTableAdapter.Update(order.Order_Details);

                        transaction.Commit();

                        tmpTransactionResult = new SdataTransactionResult();
                        tmpTransactionResult.HttpMethod = HttpMethod.PUT;
                        tmpTransactionResult.ResourceKind = _resourceKind;
                        tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.OK;
                        tmpTransactionResult.LocalId = payload.Key;
                        return tmpTransactionResult;

                    }
                    catch (Exception transactionException)
                    {
                        if (transaction != null)
                            transaction.Rollback();
                        throw;
                    }

                }
            }
            catch (Exception e)
            {
                tmpTransactionResult = new SdataTransactionResult();
                tmpTransactionResult.HttpMethod = HttpMethod.PUT;
                tmpTransactionResult.ResourceKind = _resourceKind;
                tmpTransactionResult.HttpStatus = System.Net.HttpStatusCode.BadRequest;
                tmpTransactionResult.HttpMessage = e.ToString();
                return tmpTransactionResult;
            }

            #endregion
        }