Esempio n. 1
0
        private Document GetDocument(DataSets.Order.CalculatedOrdersRow row,
            DataSets.Order.CalculatedOrderDetailsDataTable detailDataTable,
            DataSets.Order.DeletedOrderDetailsDataTable deletedOrderDetailsDataTable,
            Token lastToken, NorthwindConfig config)
        {
            #region Declarations
            OrderDocument doc;
            string id;
            LogState logState = LogState.Updated;
            Document lineItemDoc;
            CountryCodes countryCodes = new CountryCodes();
            #endregion

            id = row.OrderID.ToString();

            if (lastToken.InitRequest)
                logState = LogState.Created;

            else if (row.IsCreateIDNull() || row.IsModifyIDNull()
                || row.IsCreateUserNull() || row.IsModifyUserNull())
                logState = LogState.Created;

            else if ((row.CreateID > lastToken.SequenceNumber)
                   && (row.CreateUser != config.CrmUser))
                logState = LogState.Created;

            else if ((row.CreateID == lastToken.SequenceNumber)
                && (row.CreateUser != config.CrmUser)
                && (id.CompareTo(lastToken.Id.Id) > 0))
                logState = LogState.Created;

            else if ((row.ModifyID >= lastToken.SequenceNumber) && (row.ModifyUser != config.CrmUser))
                logState = LogState.Updated;

            doc = new OrderDocument();
            doc.Id = id;
            doc.LogState = logState;

            doc.currency.Value = config.CurrencyCode;
            doc.pricinglistid.Value = Constants.DefaultValues.PriceList.ID;
            doc.reference.Value = id;

            if (row.IsCustomerIDNull())
                doc.accountid.Value = null;
            else
                doc.accountid.Value = Constants.CustomerIdPrefix + row.CustomerID;

            if (row.IsOrderDateNull())
                doc.opened.Value = null;
            else
                doc.opened.Value = row.OrderDate;

            if (row.IsShippedDateNull())
                doc.status.Value = Constants.OrderStatus.Active;
            else
                doc.status.Value = Constants.OrderStatus.Completed;

            //doc.DiscountPercentage.Value = new decimal(0);

            //doc.grossamt.Value = row.IsTotalQuotedPriceNull() ? new decimal(0) : Convert.ToDecimal(row.TotalQuotedPrice);
            doc.discountamt.Value = new decimal(0);
            doc.lineitemdisc.Value = row.IsDiscountAmountNull() ? new decimal(0) : Convert.ToDecimal(row.DiscountAmount);
            doc.nettamt.Value = row.IsTotalNetPriceNull() ? new decimal(0) : Convert.ToDecimal(row.TotalNetPrice);
            doc.freight.Value = row.IsFreightNull() ? new decimal(0) : row.Freight;

            doc.tax.Value = new decimal(0);
            doc.grossamt.Value = doc.nettamt.Value;

            if (row.IsRequiredDateNull())
                doc.deliverydate.Value = null;
            else
                doc.deliverydate.Value = row.RequiredDate;
            if (row.IsEmployeeIDNull())
                doc.salesrepr.Value = null;
            else
                doc.salesrepr.Value = Convert.ToString(row.EmployeeID);

            if (row.IsShipViaNull())
                doc.shippedvia.Value = null;
            else
                doc.shippedvia.Value = row.ShipVia;

            OrderAddress orderAddress = new OrderAddress();
            orderAddress.SetNorthwindAddress(row.IsShipAddressNull() ? "" : row.ShipAddress,
                row.IsShipCityNull() ? "" : row.ShipCity,
                row.IsShipPostalCodeNull() ? "" : row.ShipPostalCode,
                row.IsShipCountryNull() ? "" : row.ShipCountry);

            doc.shipaddress.Value = orderAddress.CrmOrderAddress;

            foreach (DataSets.Order.CalculatedOrderDetailsRow detailRow in detailDataTable.Rows)
            {
                lineItemDoc = GetDocumentLineItem(detailRow, lastToken, config);
                if ((doc.LogState != LogState.Created) && (lineItemDoc.HasNoLogStatus))
                    continue;

                doc.orderitems.Add(lineItemDoc);
            }

            foreach (DataSets.Order.DeletedOrderDetailsRow deletedRow in deletedOrderDetailsDataTable.Rows)
            {
                lineItemDoc = new LineItemDocument();
                lineItemDoc.Id = deletedRow[0].ToString();
                lineItemDoc.LogState = LogState.Deleted;
                doc.orderitems.Add(lineItemDoc);
            }

            return doc;
        }