Beispiel #1
0
        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            if (_invoices == null)
            {
                return;                    // Haven't parsed any invoices yet, or there was an error
            }
            sessionManager.BeginSession(COMPANY_FILE, ENOpenMode.omDontCare);
            _sessionBegun = true;
            _failed       = false;

            // Grab the list of unpaid invoices from the selected customer
            IMsgSetRequest messageSet   = sessionManager.CreateMsgSetRequest("CA", 12, 0);
            IInvoiceQuery  invoiceQuery = messageSet.AppendInvoiceQueryRq();

            invoiceQuery.ORInvoiceQuery.InvoiceFilter.PaidStatus.SetValue(ENPaidStatus.psNotPaidOnly);
            invoiceQuery.ORInvoiceQuery.InvoiceFilter.EntityFilter.OREntityFilter.FullNameList.Add(e.Argument.ToString());

            try
            {
                IMsgSetResponse responseSet = sessionManager.DoRequests(messageSet);

                IResponse      response;
                ENResponseType responseType;

                for (int i = 0; i < responseSet.ResponseList.Count; i++)
                {
                    response = responseSet.ResponseList.GetAt(i);

                    if (response.Detail == null)
                    {
                        continue;
                    }

                    responseType = (ENResponseType)response.Type.GetValue();
                    if (responseType == ENResponseType.rtInvoiceQueryRs)
                    {
                        IInvoiceRetList invoiceList = (IInvoiceRetList)response.Detail;

                        for (int invoiceIndex = 0; invoiceIndex < invoiceList.Count; invoiceIndex++)
                        {
                            IInvoiceRet invoice = (IInvoiceRet)invoiceList.GetAt(invoiceIndex);

                            if (invoice != null)
                            {
                                //unpaid_listbox.Items.Add(invoice.CustomerRef.FullName.GetValue() + " - " + invoice.RefNumber.GetValue() + " ( " + invoice.TxnID.GetValue() + " )");
                                _unpaidInvoices.Add(new Invoice(invoice.RefNumber.GetValue(), invoice.TxnID.GetValue()));
                                int index = _invoices.FindIndex(x => x.Probill == invoice.RefNumber.GetValue());

                                if (index >= 0)
                                {
                                    _invoices[index].TxnID = invoice.TxnID.GetValue();
                                    Trace.WriteLine("Applying TxnID " + invoice.TxnID.GetValue() + " to Invoice " + _invoices[index].Probill);
                                }
                            }
                        }
                    }
                }

                messageSet = sessionManager.CreateMsgSetRequest("CA", 12, 0);
                messageSet.Attributes.OnError = ENRqOnError.roeContinue;


                IReceivePaymentAdd payment = messageSet.AppendReceivePaymentAddRq();

                // Populate the request with all the details
                payment.CustomerRef.FullName.SetValue(e.Argument.ToString());
                payment.RefNumber.SetValue(_invoices.First().ChequeNumber);
                payment.PaymentMethodRef.FullName.SetValue("Cheque");
                IAppliedToTxnAdd txn;
                double           totalAmt = 0.0;
                int count = 0;

                foreach (Invoice i in _invoices)
                {
                    // Don't add payments that are negative (IE. back charges, etc)
                    if (i.Total < 0)
                    {
                        Trace.WriteLine(String.Format("Rejecting negative transaction {0} for ${1}", i.Probill, i.Total));
                        // Subtract it from our total
                        // TODO: add an expense?
                        //totalAmt -= i.Total;
                        continue;
                    }
                    if (i.Total == 0)
                    {
                        // Discard any empty entries
                        Trace.WriteLine("Rejecting empty entry");
                        continue;
                    }
                    if (i.TxnID == "")
                    {
                        // Discard any invalid entries (couldn't find an unpaid invoice that matches)
                        Trace.WriteLine("Rejecting entry with no TxnID");
                        continue;
                    }

                    txn = payment.ORApplyPayment.AppliedToTxnAddList.Append();
                    txn.PaymentAmount.SetValue(i.Total);
                    txn.TxnID.SetValue(i.TxnID);
                    totalAmt += i.Total;
                    count    += 1;
                    Trace.WriteLine("Adding Transaction of " + i.Total + " with TxnID " + i.TxnID + " ( " + count + " )");
                }

                Trace.WriteLine("Total amount received: " + totalAmt);
                // Round off the total to two decimal places, otherwise it will sometimes inexplicably fail
                totalAmt = Math.Round(totalAmt, 2);
                payment.TotalAmount.SetValue(totalAmt);
                // TODO: fix hardcoded version number
                payment.Memo.SetValue("Automatically generated by qbFixer 0.1 beta");
                //payment.TxnDate.SetValue(DateTime.Today);

                responseSet = sessionManager.DoRequests(messageSet);



                for (int i = 0; i < responseSet.ResponseList.Count; i++)
                {
                    response = responseSet.ResponseList.GetAt(i);

                    if (response.StatusCode > 0)
                    {
                        MessageBox.Show(response.StatusMessage);
                        return;
                    }
                }

                _bw.ReportProgress(100);
                _bw.CancelAsync();
            }
            catch (System.Runtime.InteropServices.COMException comEx)
            {
                MessageBox.Show(comEx.Message);
                _failed = true;
            }
            finally
            {
                sessionManager.EndSession();
                _sessionBegun = false;
            }
        }