Exemple #1
0
      }//readInvoiceData

      //-------------------------------------------------------------------------------------------------------//

      /// <summary>
      /// Get Last price of the product that this customer bought.
      /// </summary>
      /// <param name="customerCode">Customer Code</param>
      /// <param name="productCode">Product Code</param>
      /// <returns></returns>
      public Sale GetLastPriceData(string customerCode, string productCode)
      {

         ///Start date is lookBackYrs year ago.
         int lookBackYrs = (int)Settings.Default.invoiceLookBackYrs;
         DateTime startDate = DateTime.Now.AddYears(-lookBackYrs);
         int invNum = -1;
         //string cusCode = String.Empty;
         double salePrice = -1;
         int currCode = -1;
         double xRate = -1;
         DateTime invDate = new DateTime();

         try
         {
            sdo = new SDOEngine();
            //Try a connection, will throw an exception if it fails
            ws = (WorkSpace)sdo.Workspaces.Add("App Server Update");
            ws.Disconnect();
            ws.Connect(sageUsrSet.sageDBDir, sageUsrSet.sageUsername, sageUsrSet.sagePassword, "UniqueUpdater");

            //Create instances of the objects
            invoiceRecord = (InvoiceRecord)ws.CreateObject("InvoiceRecord");
            invoiceItem = (InvoiceItem)ws.CreateObject("InvoiceItem");
            var cs = (string)SDOHelper.Read(invoiceRecord, "ACCOUNT_REF");

            SDOHelper.Write(invoiceRecord, "ACCOUNT_REF", customerCode);
            invoiceRecord.Find(false);
            invNum = (int)SDOHelper.Read(invoiceRecord, "INVOICE_NUMBER");
            //Start at last Invoice
            invoiceRecord.MoveLast();
            do
            {
               //Invoice info
               invDate = (DateTime)SDOHelper.Read(invoiceRecord, "INVOICE_DATE");
               cs =  (string)SDOHelper.Read(invoiceRecord, "ACCOUNT_REF");

               //Only read invoice details if it is recent enough.
               if (invDate < startDate)
                  break;

               invNum = (int)SDOHelper.Read(invoiceRecord, "INVOICE_NUMBER");
               currCode = (sbyte)SDOHelper.Read(invoiceRecord, "CURRENCY");
               xRate = (double)SDOHelper.Read(invoiceRecord, "FOREIGN_RATE");

               //Link Items to Record
               invoiceItem = invoiceRecord.Link;


               var lastPriceResult = invoiceItem.FindFirst("STOCK_CODE", productCode);

               if (lastPriceResult)
               {
                  double netAmount = (double)SDOHelper.Read(invoiceItem, "NET_AMOUNT");
                  double qty = (double)SDOHelper.Read(invoiceItem, "QTY_ORDER");
                  salePrice = netAmount / qty;
                  return new Sale(invDate, salePrice, productCode);
               }//If




            } while (invoiceRecord.MovePrev());

         }
         catch (Exception e)
         {
            string eString = "Problem reading Invoice Data"
                              + "\r\n    -----------------     \r\n"
                              + e.GetType() + "\r\n" + e.Message
                              + "\r\n    -----------------     \r\n"
                              + "\r\nInvoice No.: " + invNum + ", Product: " + productCode + ", Customer : " + customerCode
                              + "\r\nSale Price : " + salePrice + "\r\n";
            throw new Exception(eString);
         }
         finally
         {
            DestroyAllObjects();
         }//Finally

         return null;
         
      }//readInvoiceData