gp_DailyReport_Result IHistoryDAO.DailyReport(String MerchantID, String ClerkID, String WhereFrom, String MerchSeqNum, String TerminalID, DateTime LocalTime)
        {
            InitializeConnection();
            gp_DailyReport_Result Res = GiftEntity.gp_DailyReport(MerchantID, ClerkID, "A", WhereFrom, MerchSeqNum, TerminalID, LocalTime).FirstOrDefault();

            return(Res);
        }
        // Yes, there was a stored procedure to do the detail report,
        // but it returns two result sets and the second one can have
        // a bunch of records in it.
        DetailHistory IHistoryDAO.DetailReport(String MerchantID, String ClerkID, String WhereFrom, String MerchSeqNum, String TerminalID, DateTime LocalTime)
        {
            InitializeConnection();
            // need to resolve any merchant sequence number issues



            DetailHistory Results = new DetailHistory();

            Results.DetailItems = new List <CardHistory>();


            gp_DailyReport_Result Res = GiftEntity.gp_DailyReport(MerchantID, ClerkID, "E", WhereFrom, MerchSeqNum, TerminalID, LocalTime).FirstOrDefault();

            if (Res.ResponseCode == "A")
            {
                Results.When         = DateTime.Now;
                Results.ResponseCode = Res.ResponseCode[0];
                Results.ErrorCode    = (string)Res.ErrorCode;
                Results.Summary      = new DailySalesInformation();
                DailyReportToWeb(Res, Results.Summary, LocalTime);


                // now get the details

                Merchant Merch = GiftEntity.Merchants.FirstOrDefault(d => d.MerchantID == MerchantID);

                var LastRun = (from h in GiftEntity.Histories
                               where h.WhichMerchantGUID == Merch.MerchantGUID &&
                               h.TransType == "CLOS"
                               orderby h.ID descending
                               select h.ID).FirstOrDefault();
                //if (LastRun == null)
                //    LastRun = 0;

                var DailyHistories = from h in GiftEntity.Histories
                                     where h.WhichMerchantGUID == Merch.MerchantGUID &&
                                     h.ID > LastRun &&
                                     h.ErrorCode == "APP  " &&
                                     h.TransType != "DYRP" && // keep the daily report out of this
                                     h.TransType != "SHIP"    // keep the card shipments out of the report
                                     orderby h.ID
                                                              //c.Card2 == WhichCard.ID
                                     select new
                {
                    ID               = h.ID,
                    CardGUID         = h.CardGUID,
                    TransType        = h.TransType,
                    When             = h.WhenHappened,
                    LocalTime        = h.LocalTime,
                    Amount           = h.Amount,
                    PointsGranted    = 0,
                    MerchWhere       = h.WhichMerchantGUID,
                    Clrk             = h.Clerk,
                    Text             = h.TransactionText,
                    CardGUID2        = h.CardGUID2,
                    CouponUsed       = 0,
                    CouponIssued     = 0,
                    PrizeAwardedGUID = Guid.NewGuid(),
                    InvoiceNumber    = h.InvoiceNumber
                };
                foreach (var his in DailyHistories)
                {
                    CardHistory nHistory = new CardHistory();
                    nHistory.ID         = his.ID;
                    nHistory.CardNumber = (from c in GiftEntity.Cards
                                           where c.CardGUID == his.CardGUID
                                           select c.CardNumLast4).FirstOrDefault().ToString();
                    nHistory.Amount     = his.Amount;
                    nHistory.Clerk      = his.Clrk;
                    nHistory.MerchWhere = (from m in GiftEntity.Merchants
                                           where m.MerchantGUID == his.MerchWhere
                                           select m.MerchantName).FirstOrDefault();
                    nHistory.TransType   = his.TransType;
                    nHistory.Transaction = ConvertTransactionType(his.TransType);
                    nHistory.When        = his.When;
                    if (his.LocalTime.HasValue)
                    {
                        nHistory.LocalTime = his.LocalTime.Value;
                    }
                    nHistory.Text          = his.Text;
                    nHistory.PointsGranted = his.PointsGranted;
                    if (his.CardGUID2 != null)
                    {
                        nHistory.Card2 = GiftEntity.Cards
                                         .FirstOrDefault(c => c.CardGUID == his.CardGUID2).CardNumLast4;
                    }
                    nHistory.InvoiceNumber = his.InvoiceNumber;

                    Results.DetailItems.Add(nHistory);
                }
            }
            else
            {
                Results.ResponseCode = Res.ResponseCode[0];
                Results.ErrorCode    = (string)Res.ErrorCode;
            }
            return(Results);
        }