public void IntegrateOrderToQuickBooks(List <OrderMaster> omList)
        {
            bool sendToQb = false;

            if (context.RequestMessage.MessageDataExtension.ContainsKey(MessageDataExtensionKeys.SEND_TO_QB))
            {
                bool.TryParse(context.RequestMessage.MessageDataExtension[MessageDataExtensionKeys.SEND_TO_QB], out sendToQb);
            }

            List <QuickBooksInvoiceLog> logs = new List <QuickBooksInvoiceLog>();
            long cnt = 0;

            using (QuickBooksInvoiceAgent agent = new QuickBooksInvoiceAgent(context))
            {
                foreach (OrderMaster om in omList)
                {
                    cnt++;
                    string status = sendToQb ? (string.IsNullOrWhiteSpace(om.IntegrationStatus) ? QbIntegrationLogStatus.OK : QbIntegrationLogStatus.REVOKED) : QbIntegrationLogStatus.WAITING;

                    QuickBooksInvoiceLog log = CreateInvoiceLog(om, status);
                    context.TransactionObject = om;
                    string qbInvoiceId = string.Empty;
                    string qbTxnId     = string.Empty;
                    try
                    {
                        agent.Export();
                        if (context.TransactionObject != null)
                        {
                            if (context.TransactionObject is Tuple <string, string> )
                            {
                                Tuple <string, string> tuple = context.TransactionObject as Tuple <string, string>;
                                qbInvoiceId = tuple.Item1;
                                qbTxnId     = tuple.Item2;
                            }
                            else
                            {
                                qbInvoiceId = context.Bag["REF_NUMBER"].ToString();
                                qbTxnId     = context.Bag["TXN_ID"].ToString();
                            }
                        }
                        log.QuickBooksInvoiceId = qbInvoiceId;
                        log.QuickBooksTxnId     = qbTxnId;
                        log.IntegrationStatus   = QbIntegrationLogStatus.OK;
                    }
                    catch (Exception ex)
                    {
                        string message = string.Format("Exception occured while integrating Order {0} to Quickbooks.", om.Id);
                        logger.Error(ex, message);
                        log.IntegrationStatus = QbIntegrationLogStatus.ERROR;
                        log.ErrorLog          = ex.ToString();
                        context.Warnings.Add(message);
                    }

                    InsertIntegrationLog(log);

                    logs.Add(log);

                    string eventMessage = string.Format("Order `{0}` integration complete. Status:{1}, QB Invoice Id:{2}", log.OrderId, log.IntegrationStatus, log.QuickBooksInvoiceId);

                    this.OnTransactionProgress(new TransactionProgressEventArgs(omList.Count, cnt, eventMessage));
                }
            }

            context.TransactionObject = logs;
        }
Exemplo n.º 2
0
        protected override void ExportQb()
        {
            string invoiceIdString = request.MessageDataExtension["INVOICE_LIST"];

            string[] invoiceIdListStr = invoiceIdString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            List <long> invoiceIdList = invoiceIdListStr.Select(i => long.Parse(i)).ToList();

            OrderManager orderMan = new OrderManager(Context);

            List <QuickBooksInvoiceLog> list = invMan.List(invoiceIdList);

            List <OrderMaster> orders = orderMan.GetOrders(list.Select(l => l.OrderId).ToList());

            orders = orders.Where(om => om.IntegrationStatus == QbIntegrationLogStatus.WAITING || om.IntegrationStatus == QbIntegrationLogStatus.ERROR).ToList();
            long   counter     = 0;
            long   batchId     = CreateBatch(orders.Select(o => o.Id).ToList());
            string batchStatus = QbIntegrationLogStatus.OK;

            using (qbAgent = new QuickBooksInvoiceAgent(Context))
            {
                counter++;
                foreach (OrderMaster orderMaster in orders)
                {
                    Context.TransactionObject      = orderMaster;
                    orderMaster.InvoiceLog.BatchId = batchId;
                    string eventMessage = string.Empty;
                    try
                    {
                        qbAgent.Export();
                        orderMaster.InvoiceLog.IntegrationStatus   = orderMaster.InvoiceLog.IntegrationStatus == QbIntegrationLogStatus.ERROR ? QbIntegrationLogStatus.REVOKED : QbIntegrationLogStatus.OK;
                        orderMaster.InvoiceLog.ErrorLog            = string.Empty;
                        orderMaster.InvoiceLog.QuickBooksTxnId     = Context.Bag["TXN_ID"].ToString();
                        orderMaster.InvoiceLog.QuickBooksInvoiceId = Context.Bag["REF_NUMBER"].ToString();
                        orderMaster.OrderStatus = OrderStatus.INTEGRATED;
                        eventMessage            = string.Format("Invoice nr {0} created for order {1}", orderMaster.InvoiceLog.QuickBooksInvoiceId, orderMaster.Id);
                    }
                    catch (Exception ex)
                    {
                        string msg = string.Format("Exception occured while integrationg order to Quickbooks. Order id:{0}", orderMaster.Id);
                        logger.Error(ex, msg);
                        warnings.Add(msg);
                        orderMaster.InvoiceLog.ErrorLog          = ex.ToString();
                        orderMaster.InvoiceLog.IntegrationStatus = QbIntegrationLogStatus.ERROR;
                        orderMaster.OrderStatus = OrderStatus.ERROR;
                        batchStatus             = QbIntegrationLogStatus.ERROR;
                        eventMessage            = msg;
                    }
                    finally
                    {
                        invMan.UpdateInvoiceLog(orderMaster.InvoiceLog);
                        orderMan.UpdateOrder(orderMaster, false);
                    }
                    responseMessage += eventMessage + Environment.NewLine;

                    OnTransactionProgress(new TransactionProgressEventArgs(orders.Count, counter, eventMessage));
                }
            }

            invMan.UpdateBatch(batchId, batchStatus);
        }