Exemplo n.º 1
0
        internal static CustomerOrderInfo GetInfoFromTransaction(CustomerOrderTransaction customerOrder)
        {
            CustomerOrderInfo parameters = new CustomerOrderInfo();
            //GRW
            DAC       odac = new DAC(SalesOrder.InternalApplication.Settings.Database.Connection);
            DataTable dt   = odac.GetContactData(customerOrder.TransactionId, customerOrder.TerminalId, customerOrder.StoreId);

            if (dt.Rows.Count > 0)
            {
                parameters.CustName  = dt.Rows[0]["NAME"].ToString();
                parameters.CustPhone = dt.Rows[0]["PHONE"].ToString();
                parameters.Email     = dt.Rows[0]["EMAIL"].ToString();
            }
            //GRW
            parameters.OrderType      = customerOrder.OrderType;
            parameters.Id             = customerOrder.OrderId;
            parameters.TransactionId  = customerOrder.TransactionId;
            parameters.QuotationId    = customerOrder.OrderId;
            parameters.AutoPickOrder  = false;
            parameters.WarehouseId    = customerOrder.WarehouseId;
            parameters.CurrencyCode   = customerOrder.StoreCurrencyCode;
            parameters.StoreId        = customerOrder.StoreId;
            parameters.TerminalId     = customerOrder.TerminalId;
            parameters.LocalHourOfDay = customerOrder.LocalHourOfDay;

            parameters.AddressRecordId    = (customerOrder.ShippingAddress != null) ? customerOrder.ShippingAddress.AddressRecId : string.Empty;
            parameters.CustomerAccount    = (customerOrder.Customer != null) ? customerOrder.Customer.CustomerId : string.Empty;
            parameters.SalespersonStaffId = (customerOrder.SalesPersonId) ?? string.Empty;

            // The format must match the expected format in AX RetailTransactionService.CreateCustomerOrder: "dd/MM/yyyy"
            parameters.ExpiryDateString            = customerOrder.ExpirationDate.ToString(FixedDateFormat);
            parameters.RequestedDeliveryDateString = customerOrder.RequestedDeliveryDate.ToString(FixedDateFormat);
            parameters.DeliveryMode = customerOrder.DeliveryMode != null ? customerOrder.DeliveryMode.Code : string.Empty;
            parameters.PrepaymentAmountOverridden = customerOrder.PrepaymentAmountOverridden;
            parameters.PrepaymentAmountApplied    = customerOrder.NetAmountWithTaxAndCharges - customerOrder.AmountDue;

            parameters.TotalManualDiscountAmount     = customerOrder.TotalManualDiscountAmount;
            parameters.TotalManualDiscountPercentage = customerOrder.TotalManualPctDiscount;

            //parameters.Email = customerOrder.ReceiptEmailAddress;
            parameters.Comment            = ((IPosTransactionV1)customerOrder).Comment;
            parameters.ReturnReasonCodeId = customerOrder.ReturnReasonCodeId;
            parameters.LoyaltyCardId      = (customerOrder.LoyaltyItem != null) ?
                                            customerOrder.LoyaltyItem.LoyaltyCardNumber : string.Empty;

            // If we do not have the channel reference identifier, we create a new receipt identifier instead.
            parameters.ChannelReferenceId = customerOrder.ChannelReferenceId;

            parameters.CreditCardToken = customerOrder.CreditCardToken;

            // Discount codes
            parameters.DiscountCodes = new Collection <string>();
            foreach (string code in customerOrder.DiscountCodes)
            {
                parameters.DiscountCodes.Add(code);
            }

            // Line Items
            parameters.Items = new Collection <ItemInfo>();
            foreach (SaleLineItem item in customerOrder.SaleItems)
            {
                if (!item.Voided)
                {
                    string deliveryMode = parameters.DeliveryMode;
                    if (item.DeliveryMode != null)
                    {
                        deliveryMode = item.DeliveryMode.Code;
                    }

                    string deliveryDateString = parameters.RequestedDeliveryDateString;
                    if (item.DeliveryDate.HasValue)
                    {
                        deliveryDateString = item.DeliveryDate.Value.ToString(FixedDateFormat);
                    }

                    Collection <ChargeInfo> lineChargeInfo = new Collection <ChargeInfo>();
                    foreach (Tax.MiscellaneousCharge charge in item.MiscellaneousCharges)
                    {
                        lineChargeInfo.Add(new ChargeInfo()
                        {
                            Amount        = charge.Amount,
                            Code          = charge.ChargeCode,
                            SalesTaxGroup = charge.SalesTaxGroupId,
                            TaxGroup      = charge.TaxGroupId
                        });
                    }

                    // If no line-level warehouse is specified, fall back to the header warehouse
                    string inventLocationId = string.IsNullOrWhiteSpace(item.DeliveryWarehouse) ? customerOrder.WarehouseId : item.DeliveryWarehouse;

                    // AX SO line stores discount amount per item, POS stores for whole line, calculate per item discount amount
                    decimal lineDiscount = (item.Quantity == 0M ? 0M : (item.TotalDiscount + item.LineDiscount + item.PeriodicDiscount) / (item.Quantity));

                    // Save all discount lines per sales line
                    Collection <DiscountInfo> lineDiscountInfo = new Collection <DiscountInfo>();
                    foreach (DiscountItem discountLine in item.DiscountLines)
                    {
                        DiscountInfo discountInfo = new DiscountInfo();
                        discountInfo.DiscountCode            = string.Empty;
                        discountInfo.PeriodicDiscountOfferId = string.Empty;

                        discountInfo.EffectiveAmount = discountLine.EffectiveAmount;
                        discountInfo.DealPrice       = discountLine.DealPrice;
                        discountInfo.Percentage      = discountLine.Percentage;
                        discountInfo.DiscountAmount  = discountLine.Amount;

                        LineDiscountItem     lineDiscountItem;
                        PeriodicDiscountItem periodicDiscountItem;
                        CustomerDiscountItem customerDiscountItem;

                        if ((lineDiscountItem = discountLine as LineDiscountItem) != null)
                        {
                            discountInfo.DiscountOriginType = (int)lineDiscountItem.LineDiscountType;

                            if ((periodicDiscountItem = discountLine as PeriodicDiscountItem) != null)
                            {
                                discountInfo.PeriodicDiscountOfferId = periodicDiscountItem.OfferId;
                                discountInfo.DiscountCode            = periodicDiscountItem.DiscountCode;
                            }
                            else if ((customerDiscountItem = discountLine as CustomerDiscountItem) != null)
                            {
                                discountInfo.CustomerDiscountType = (int)customerDiscountItem.CustomerDiscountType;
                            }
                            else
                            {
                                discountInfo.DiscountOriginType = (int)LineDiscountItem.DiscountTypes.Manual;
                                discountInfo.ManualDiscountType = (int)discountLine.GetManualDiscountType();
                            }
                        }

                        if (discountLine is TotalDiscountItem)
                        {
                            discountInfo.DiscountOriginType = (int)LineDiscountItem.DiscountTypes.Manual;
                            discountInfo.ManualDiscountType = (int)discountLine.GetManualDiscountType();
                        }

                        lineDiscountInfo.Add(discountInfo);
                    }

                    parameters.Items.Add(new ItemInfo()
                    {
                        RecId = item.OrderLineRecordId,

                        //quantity
                        ItemId   = item.ItemId,
                        Quantity = item.Quantity,
                        Unit     = item.SalesOrderUnitOfMeasure,

                        //pricing
                        Price         = item.Price,
                        Discount      = lineDiscount,
                        NetAmount     = item.NetAmount,
                        ItemTaxGroup  = item.TaxGroupId,
                        SalesTaxGroup = item.SalesTaxGroupId,
                        SalesMarkup   = item.SalesMarkup,

                        PeriodicDiscount             = item.PeriodicDiscount,
                        LineDscAmount                = item.LineDiscount,
                        LineManualDiscountAmount     = item.LineManualDiscountAmount,
                        LineManualDiscountPercentage = item.LineManualDiscountPercentage,
                        TotalDiscount                = item.TotalDiscount,
                        TotalPctDiscount             = item.TotalPctDiscount,
                        PeriodicPercentageDiscount   = item.PeriodicPctDiscount,

                        //Comment
                        Comment = item.Comment,

                        //delivery
                        WarehouseId                 = inventLocationId,
                        AddressRecordId             = item.ShippingAddress != null ? item.ShippingAddress.AddressRecId : null,
                        DeliveryMode                = deliveryMode,
                        RequestedDeliveryDateString = deliveryDateString,

                        //inventDim
                        BatchId   = item.BatchId,
                        SerialId  = item.SerialId,
                        VariantId = item.Dimension.VariantId,
                        ColorId   = item.Dimension.ColorId,
                        SizeId    = item.Dimension.SizeId,
                        StyleId   = item.Dimension.StyleId,
                        ConfigId  = item.Dimension.ConfigId,

                        //Return
                        InvoiceId     = item.ReturnInvoiceId,
                        InventTransId = item.ReturnInvoiceInventTransId,

                        //line-level misc. charges
                        Charges = lineChargeInfo,

                        //line-level discounts
                        Discounts = lineDiscountInfo,
                    });
                }
            }

            // Header level Misc Charges
            parameters.Charges = new Collection <ChargeInfo>();
            foreach (Tax.MiscellaneousCharge charge in customerOrder.MiscellaneousCharges)
            {
                parameters.Charges.Add(new ChargeInfo()
                {
                    Code          = charge.ChargeCode,
                    Amount        = charge.Amount,
                    SalesTaxGroup = charge.SalesTaxGroupId,
                    TaxGroup      = charge.TaxGroupId
                });
            }
            string CardType = "";

            // Payments
            parameters.Payments = new Collection <PaymentInfo>();
            foreach (ITenderLineItem tender in customerOrder.TenderLines)
            {
                CardType = "";
                if (!tender.Voided)
                {
                    ICardTenderLineItem cardTender = tender as ICardTenderLineItem;
                    if (cardTender != null && cardTender.EFTInfo.IsAuthOnly)
                    {
                        // This is a Pre-Authorization record.
                        IEFTInfo eft = cardTender.EFTInfo;
                        parameters.Preauthorization = new Preauthorization()
                        {
                            PaymentPropertiesBlob = eft.PaymentProviderPropertiesXML
                        };
                    }
                    else if (tender.Amount != decimal.Zero)
                    {
                        // This is an actual payment record.
                        DAC odac2 = new DAC(ApplicationSettings.Database.LocalConnection);
                        CardType = odac2.getCardType(customerOrder.TransactionId, customerOrder.TerminalId, customerOrder.StoreId, tender.LineId, tender.TenderTypeId);

                        parameters.Payments.Add(new PaymentInfo()
                        {
                            PaymentType = tender.TenderTypeId,
                            Amount      = string.IsNullOrEmpty(tender.CurrencyCode) ? tender.Amount : tender.ForeignCurrencyAmount,
                            Currency    = (tender.CurrencyCode) ?? string.Empty,
                            CardType    = string.IsNullOrEmpty(CardType) ? "":CardType
                        });
                    }
                }
            }

            // Affiliations
            parameters.Affiliations = new Collection <AffiliationInfo>();
            foreach (IAffiliation affiliation in customerOrder.AffiliationLines)
            {
                parameters.Affiliations.Add(new AffiliationInfo()
                {
                    AffiliationId = affiliation.RecId,
                    LoyaltyTierId = affiliation.LoyaltyTier
                });
            }

            return(parameters);
        }
        internal static CustomerOrderInfo GetInfoFromTransaction(CustomerOrderTransaction customerOrder)
        {
            CustomerOrderInfo parameters = new CustomerOrderInfo();

            parameters.OrderType      = customerOrder.OrderType;
            parameters.Id             = customerOrder.OrderId;
            parameters.QuotationId    = customerOrder.OrderId;
            parameters.AutoPickOrder  = false;
            parameters.WarehouseId    = customerOrder.WarehouseId;
            parameters.CurrencyCode   = customerOrder.StoreCurrencyCode;
            parameters.StoreId        = customerOrder.StoreId;
            parameters.TerminalId     = customerOrder.TerminalId;
            parameters.LocalHourOfDay = customerOrder.LocalHourOfDay;

            parameters.AddressRecordId    = (customerOrder.ShippingAddress != null) ? customerOrder.ShippingAddress.AddressRecId : string.Empty;
            parameters.CustomerAccount    = (customerOrder.Customer != null) ? customerOrder.Customer.CustomerId : string.Empty;
            parameters.SalespersonStaffId = (customerOrder.SalesPersonId) ?? string.Empty;

            // The format must match the expected format in AX RetailTransactionService.CreateCustomerOrder: "dd/MM/yyyy"
            parameters.ExpiryDateString            = customerOrder.ExpirationDate.ToString(FixedDateFormat);
            parameters.RequestedDeliveryDateString = customerOrder.RequestedDeliveryDate.ToString(FixedDateFormat);
            parameters.DeliveryMode = customerOrder.DeliveryMode != null ? customerOrder.DeliveryMode.Code : string.Empty;
            parameters.PrepaymentAmountOverridden = customerOrder.PrepaymentAmountOverridden;

            parameters.Email              = customerOrder.ReceiptEmailAddress;
            parameters.Comment            = ((IPosTransactionV1)customerOrder).Comment;
            parameters.ReturnReasonCodeId = customerOrder.ReturnReasonCodeId;
            parameters.LoyaltyCardId      = (customerOrder.LoyaltyItem != null && customerOrder.LoyaltyItem.UsageType == LSRetailPosis.Transaction.Line.LoyaltyItem.LoyaltyItemUsageType.UsedForLoyaltyRequest) ?
                                            customerOrder.LoyaltyItem.LoyaltyCardNumber : string.Empty;

            // If we do not have the channel reference identifier, we create a new receipt identifier instead.
            parameters.ChannelReferenceId = !string.IsNullOrWhiteSpace(customerOrder.ChannelReferenceId)
                ? customerOrder.ChannelReferenceId
                : SalesOrder.InternalApplication.Services.ApplicationService.GetNextReceiptId(customerOrder);

            parameters.CreditCardToken = customerOrder.CreditCardToken;

            // Discount codes
            parameters.DiscountCodes = new Collection <string>();
            foreach (string code in customerOrder.DiscountCodes)
            {
                parameters.DiscountCodes.Add(code);
            }

            // Line Items
            parameters.Items = new Collection <ItemInfo>();
            foreach (SaleLineItem item in customerOrder.SaleItems)
            {
                if (!item.Voided)
                {
                    string deliveryMode = parameters.DeliveryMode;
                    if (item.DeliveryMode != null)
                    {
                        deliveryMode = item.DeliveryMode.Code;
                    }

                    string deliveryDateString = parameters.RequestedDeliveryDateString;
                    if (item.DeliveryDate.HasValue)
                    {
                        deliveryDateString = item.DeliveryDate.Value.ToString(FixedDateFormat);
                    }

                    Collection <ChargeInfo> lineChargeInfo = new Collection <ChargeInfo>();
                    foreach (Tax.MiscellaneousCharge charge in item.MiscellaneousCharges)
                    {
                        lineChargeInfo.Add(new ChargeInfo()
                        {
                            Amount        = charge.Amount,
                            Code          = charge.ChargeCode,
                            SalesTaxGroup = charge.SalesTaxGroupId,
                            TaxGroup      = charge.TaxGroupId
                        });
                    }

                    // If no line-level warehouse is specified, fall back to the header warehouse
                    string inventLocationId = string.IsNullOrWhiteSpace(item.DeliveryWarehouse) ? customerOrder.WarehouseId : item.DeliveryWarehouse;

                    //AX SO line stores discount amount per item, POS stores for whole line, calculate per item discount amount
                    decimal lineDiscount = (item.Quantity == 0M ? 0M : (item.TotalDiscount + item.LineDiscount + item.PeriodicDiscount) / (item.Quantity));

                    parameters.Items.Add(new ItemInfo()
                    {
                        RecId = item.OrderLineRecordId,

                        //quantity
                        ItemId   = item.ItemId,
                        Quantity = item.Quantity,
                        Unit     = item.SalesOrderUnitOfMeasure,

                        //pricing
                        Price         = item.Price,
                        Discount      = lineDiscount,
                        NetAmount     = item.NetAmount,
                        ItemTaxGroup  = item.TaxGroupId,
                        SalesTaxGroup = item.SalesTaxGroupId,
                        SalesMarkup   = item.SalesMarkup,

                        //delivery
                        WarehouseId                 = inventLocationId,
                        AddressRecordId             = item.ShippingAddress != null ? item.ShippingAddress.AddressRecId : null,
                        DeliveryMode                = deliveryMode,
                        RequestedDeliveryDateString = deliveryDateString,

                        //inventDim
                        BatchId   = item.BatchId,
                        SerialId  = item.SerialId,
                        VariantId = item.Dimension.VariantId,
                        ColorId   = item.Dimension.ColorId,
                        SizeId    = item.Dimension.SizeId,
                        StyleId   = item.Dimension.StyleId,
                        ConfigId  = item.Dimension.ConfigId,

                        //Return
                        InvoiceId     = item.ReturnInvoiceId,
                        InventTransId = item.ReturnInvoiceInventTransId,

                        //line-level misc. charges
                        Charges = lineChargeInfo,
                    });
                }
            }

            // Header level Misc Charges
            parameters.Charges = new Collection <ChargeInfo>();
            foreach (Tax.MiscellaneousCharge charge in customerOrder.MiscellaneousCharges)
            {
                parameters.Charges.Add(new ChargeInfo()
                {
                    Code          = charge.ChargeCode,
                    Amount        = charge.Amount,
                    SalesTaxGroup = charge.SalesTaxGroupId,
                    TaxGroup      = charge.TaxGroupId
                });
            }

            // Payments
            parameters.Payments = new Collection <PaymentInfo>();
            foreach (ITenderLineItem tender in customerOrder.TenderLines)
            {
                if (!tender.Voided)
                {
                    ICardTenderLineItem cardTender = tender as ICardTenderLineItem;
                    if (cardTender != null && cardTender.EFTInfo.IsAuthOnly)
                    {
                        // This is a Pre-Authorization record.
                        IEFTInfo eft = cardTender.EFTInfo;
                        parameters.Preauthorization = new Preauthorization()
                        {
                            PaymentPropertiesBlob = eft.PaymentProviderPropertiesXML
                        };
                    }
                    else if (tender.Amount != decimal.Zero)
                    {
                        // This is an actual payment record.
                        parameters.Payments.Add(new PaymentInfo()
                        {
                            PaymentType = tender.TenderTypeId,
                            Amount      = tender.Amount,
                            Currency    = (tender.CurrencyCode) ?? string.Empty
                        });
                    }
                }
            }
            return(parameters);
        }