Exemplo n.º 1
0
        // Push Order
        //
        private void CreateSalesOrder(long shopifyOrderId)
        {
            var shopifyOrderRecord = _syncOrderRepository.RetrieveShopifyOrder(shopifyOrderId);
            var acumaticaCustomer  = PushNonExistentCustomer(shopifyOrderRecord);

            var logContent = LogBuilder.CreateAcumaticaSalesOrder(shopifyOrderRecord);

            _logService.Log(logContent);

            // Write the Sales Order to Acumatica
            //
            var salesOrder = BuildNewSalesOrder(shopifyOrderRecord, acumaticaCustomer);

            // Create the Sync record *first*
            //
            var newRecord = new AcumaticaSalesOrder();

            newRecord.ShopifyOrderMonsterId    = shopifyOrderRecord.MonsterId;
            newRecord.ShopifyCustomerMonsterId = acumaticaCustomer.ShopifyCustomerMonsterId;
            newRecord.DateCreated = DateTime.UtcNow;
            newRecord.LastUpdated = DateTime.UtcNow;
            _acumaticaOrderRepository.InsertSalesOrder(newRecord);

            // Write to Acumatica Sales Order API
            //
            var resultJson = _salesOrderClient.WriteSalesOrder(salesOrder.SerializeToJson(), Expand.Totals);
            var newOrder   = resultJson.ToSalesOrderObj();

            newRecord.Ingest(newOrder);

            if (!newRecord.AcumaticaIsTaxValid)
            {
                shopifyOrderRecord.NeedsOrderPut = true;
            }
            else
            {
                shopifyOrderRecord.NeedsOrderPut = false;
            }

            shopifyOrderRecord.ErrorCount = 0;
            _syncOrderRepository.SaveChanges();
        }
Exemplo n.º 2
0
        public void UpdateCustomerInAcumatica(ShopifyCustomer shopifyRecord)
        {
            _logService.Log(LogBuilder.UpdateAcumaticaCustomer(shopifyRecord));

            using (var transaction = _acumaticaOrderRepository.BeginTransaction())
            {
                var acumaticaCustomer = BuildCustomer(shopifyRecord);
                var result            = _customerClient.WriteCustomer(acumaticaCustomer);

                var acumaticaRecord = shopifyRecord.AcumaticaCustomer;
                acumaticaRecord.AcumaticaMainContactEmail = acumaticaCustomer.MainContact.Email.value;
                acumaticaRecord.LastUpdated = DateTime.UtcNow;

                shopifyRecord.NeedsCustomerPut = false;
                _syncOrderRepository.SaveChanges();

                _acumaticaJsonService.Upsert(
                    AcumaticaJsonType.Customer, acumaticaRecord.AcumaticaCustomerId, result.SerializeToJson());
                transaction.Commit();
            }
        }
        private void PushPaymentAndWriteSync(ShopifyTransaction transactionRecord, PaymentWrite payment)
        {
            // Push to Acumatica
            //
            string resultJson;

            try
            {
                resultJson = _paymentClient.WritePayment(payment.SerializeToJson());
                _syncOrderRepository.ResetOrderErrorCount(transactionRecord.ShopifyOrderId);
            }
            catch (Exception ex)
            {
                _systemLogger.Error(ex);
                _logService.Log($"Encounter error syncing {transactionRecord.LogDescriptor()}");
                _syncOrderRepository.IncreaseOrderErrorCount(transactionRecord.ShopifyOrderId);
                return;
            }

            var resultPayment  = resultJson.DeserializeFromJson <PaymentWrite>();
            var existingRecord = _syncOrderRepository.RetreivePayment(transactionRecord.MonsterId);

            if (existingRecord == null)
            {
                // Create Monster Sync Record
                //
                var paymentRecord = new AcumaticaPayment();
                paymentRecord.ShopifyTransactionMonsterId = transactionRecord.MonsterId;

                if (resultPayment == null)
                {
                    // Workaround for Acumatica Bug
                    //
                    paymentRecord.AcumaticaRefNbr = AcumaticaSyncConstants.UnknownRefNbr;
                }
                else
                {
                    paymentRecord.AcumaticaRefNbr = resultPayment.ReferenceNbr.value;
                }

                paymentRecord.AcumaticaDocType        = payment.Type.value;
                paymentRecord.AcumaticaAmount         = (decimal)payment.PaymentAmount.value;
                paymentRecord.AcumaticaAppliedToOrder = (decimal)payment.AmountAppliedToOrder;

                if (transactionRecord.IsRefund())
                {
                    if (transactionRecord.NeedManualApply())
                    {
                        // Users are tasked with creating their Return for Credit
                        //
                        paymentRecord.NeedRelease     = false;
                        paymentRecord.NeedManualApply = true;
                    }
                    else
                    {
                        paymentRecord.NeedRelease     = true;
                        paymentRecord.NeedManualApply = false;
                    }
                }

                if (!transactionRecord.IsRefund())
                {
                    paymentRecord.NeedRelease     = true;
                    paymentRecord.NeedManualApply = false;
                }

                paymentRecord.DateCreated = DateTime.UtcNow;
                paymentRecord.LastUpdated = DateTime.UtcNow;
                _syncOrderRepository.InsertPayment(paymentRecord);
            }
            else
            {
                existingRecord.AcumaticaAppliedToOrder = (decimal)payment.AmountAppliedToOrder;
                existingRecord.LastUpdated             = DateTime.UtcNow;
                _syncOrderRepository.SaveChanges();
            }

            _syncOrderRepository.UpdateShopifyOriginalPaymentNeedPut(transactionRecord.ShopifyOrderMonsterId, false);
            _syncOrderRepository.ResetOrderErrorCount(transactionRecord.ShopifyOrderId);
        }