public PaymentWrite BuildPaymentForCreate(ShopifyTransaction transactionRecord)
        {
            var transaction = _shopifyJsonService.RetrieveTransaction(transactionRecord.ShopifyTransactionId);
            var gateway     = _settingsRepository.RetrievePaymentGatewayByShopifyId(transaction.gateway);

            // Locate the Acumatica Customer
            //
            var shopifyCustomerId = transactionRecord.CustomerId();
            var customer          = _syncOrderRepository.RetrieveCustomer(shopifyCustomerId);
            var acumaticaCustId   = customer.AcumaticaCustId();

            // Build the Payment Ref and Description
            //
            var orderRecord = _syncOrderRepository.RetrieveShopifyOrder(transactionRecord.OrderId());
            var order       = _shopifyJsonService.RetrieveOrder(orderRecord.ShopifyOrderId);

            // Create the payload for Acumatica
            //
            var payment = new PaymentWrite();

            payment.CustomerID = acumaticaCustId.ToValue();
            payment.Hold       = false.ToValue();
            payment.Type       = PaymentType.Payment.ToValue();
            payment.PaymentRef = $"{transaction.id}".ToValue();

            var createdAtUtc  = (transaction.created_at ?? order.created_at).UtcDateTime;
            var acumaticaDate = _acumaticaTimeZoneService.ToAcumaticaTimeZone(createdAtUtc);

            payment.ApplicationDate = acumaticaDate.Date.ToValue();

            // Amount computations
            //
            payment.PaymentAmount = ((double)transaction.amount).ToValue();
            var appliedToOrder = orderRecord.TheoreticalPaymentRemaining();


            // Applied to Documents
            //
            var acumaticaOrderRef = orderRecord.AcumaticaSalesOrderId();

            if (acumaticaOrderRef.HasValue() && orderRecord.IsNotCancelledOrAllRefunded())
            {
                payment.OrdersToApply =
                    PaymentOrdersRef.ForOrder(acumaticaOrderRef, SalesOrderType.SO, (double)appliedToOrder);
            }

            payment.PaymentMethod = gateway.AcumaticaPaymentMethod.ToValue();
            payment.CashAccount   = gateway.AcumaticaCashAccount.ToValue();
            payment.Description   = $"Payment for Shopify Order #{orderRecord.ShopifyOrderNumber}".ToValue();

            return(payment);
        }
Exemplo n.º 2
0
        private SalesOrder BuildNewSalesOrderHeader(
            ShopifyOrder shopifyOrderRecord, Order shopifyOrder, AcumaticaCustomer customer)
        {
            var transactionRecord = shopifyOrderRecord.PaymentTransaction();
            var payment           = _shopifyJsonService.RetrieveTransaction(transactionRecord.ShopifyTransactionId);

            var settings = _settingsRepository.RetrieveSettings();
            var gateway  = _settingsRepository.RetrievePaymentGatewayByShopifyId(payment.gateway);

            var salesOrder = new SalesOrder();

            salesOrder.Details = new List <SalesOrderDetail>();

            salesOrder.OrderType = SalesOrderType.SO.ToValue();

            var createdAtUtc  = shopifyOrder.created_at.UtcDateTime;
            var acumaticaDate = _acumaticaTimeZoneService.ToAcumaticaTimeZone(createdAtUtc);

            salesOrder.Date = acumaticaDate.Date.ToValue();

            salesOrder.CustomerOrder = $"{shopifyOrder.id}".ToValue();
            salesOrder.ExternalRef   = $"{shopifyOrder.id}".ToValue();
            salesOrder.Status        = SalesOrderStatus.Open.ToValue();
            salesOrder.Hold          = false.ToValue();
            salesOrder.Description   = $"Shopify Order #{shopifyOrder.order_number}".ToValue();
            salesOrder.CustomerID    = customer.AcumaticaCustomerId.ToValue();

            salesOrder.PaymentMethod = gateway.AcumaticaPaymentMethod.ToValue();
            salesOrder.CashAccount   = gateway.AcumaticaCashAccount.ToValue();

            // Shipping Settings
            //
            salesOrder.ShippingSettings = new ShippingSettings
            {
                ShipSeparately = true.ToValue(),
                ShippingRule   = ShippingRules.BackOrderAllowed.ToValue(),
            };


            // Freight Price and Taxes
            //
            salesOrder.FreightPrice = ((double)shopifyOrder.NetShippingPrice).ToValue();

            salesOrder.OverrideFreightPrice = true.ToValue();
            salesOrder.FreightTaxCategory   = shopifyOrder.IsShippingTaxable
                ? settings.AcumaticaTaxableCategory.ToValue()
                : settings.AcumaticaTaxExemptCategory.ToValue();

            // Taxes
            //
            salesOrder.FinancialSettings = new FinancialSettings()
            {
                OverrideTaxZone = true.ToValue(),
                CustomerTaxZone = settings.AcumaticaTaxZone.ToValue(),
                Branch          = _acumaticaHttpContext.AcumaticaBranch.ToValue()
            };

            var taxTransfer = shopifyOrder.ToSerializedAndZippedTaxTransfer();

            salesOrder.custom = new SalesOrderUsrTaxSnapshot(taxTransfer);

            return(salesOrder);
        }