Exemplo n.º 1
0
        private void CorrectSalesOrderWithUnknownRef(long shopifyOrderId)
        {
            var shopifyOrderRecord = _syncOrderRepository.RetrieveShopifyOrder(shopifyOrderId);

            if (!shopifyOrderRecord.HasSyncWithUnknownNbr())
            {
                return;
            }

            var salesOrderRecord = shopifyOrderRecord.AcumaticaSalesOrder;

            var customerOrderRef = shopifyOrderRecord.ShopifyOrderId.ToString();
            var findOrders       = _salesOrderClient.FindSalesOrder(customerOrderRef);

            if (findOrders.Count == 0)
            {
                _logService.Log(LogBuilder.ClearingUnknownAcumaticaSalesOrderRef(shopifyOrderRecord));
                _acumaticaOrderRepository.DeleteSalesOrder(shopifyOrderRecord.AcumaticaSalesOrder);
                return;
            }

            // Heuristic for now
            //
            var salesOrder = findOrders.OrderBy(x => x.OrderNbr.value).First();

            salesOrderRecord.Ingest(salesOrder);
            _logService.Log(LogBuilder.FillingUnknownAcumaticaSalesOrderRef(shopifyOrderRecord, salesOrderRecord));
            _acumaticaOrderRepository.SaveChanges();
        }
        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);
        }