public void Delete(RefillDaySummaryDataEntity p_daysummary)
 {
     using(var session = NHibernateHelper.OpenSession())
     {
         using(var transaction = session.BeginTransaction())
         {
             try
             {
                 session.Delete(p_daysummary);
                 transaction.Commit();
             }
             catch(Exception ex)
             {
                 transaction.Rollback();
                 throw ex;
             }
         }
     }
 }
 private void UpdateDaySummary(decimal amount, DateTime paymentDate, RefillHeaderDataEntity header)
 {
     try
     {
         RefillDaySummaryDataEntity daysummary = m_daysummaryDao.GetByDay(paymentDate);
         if(daysummary == null)
         {
             daysummary = new RefillDaySummaryDataEntity();
             daysummary.HeaderEntities.Add(header);
             daysummary.TotalSales += amount;
             daysummary.DayStamp = paymentDate;
         }else{
             daysummary.TotalSales += amount;
         }
         m_daysummaryDao.SaveOrUpdate(daysummary);
     }
     catch(Exception ex)
     {
         throw ex;
     }
 }
        public void saveNewHeaderAndNewDaySummary()
        {
            // test for new header and new day
            // should create new record for daysummary
            RefillHeaderDataEntity header = new RefillHeaderDataEntity();
            RefillDetailDataEntity detail = new RefillDetailDataEntity();

            RefillTransactionTypeDataEntity transactionType = new RefillTransactionTypeDao().GetByName("Delivery");
            RefillProductTypeDataEntity productType = new RefillProductTypeDao().GetByName("5 Gal at 25");

            CustomerDao custdao = new CustomerDao();
            CustomerDataEntity customer = custdao.GetByName("Vanessa Dee");
            if(customer == null)
            {
                customer = new CustomerDataEntity();
                customer.Name = "Vanessa Dee";
                customer.Address = "Cebu";
                customer.ContactNumber = "111-1111";
            }

            header.Date = DateTime.Now;
            header.TransactionType = transactionType;

            detail.Header = header; // set header entity in detail for nhibernate to pickup and map
            detail.ProductType = productType;
            detail.Qty = 10;
            detail.Amount = productType.Price * Convert.ToDecimal(detail.Qty);
            detail.StoreBottleQty = 10;
            detail.StoreCapQty = 10;

            // update main inventory
            // TODO: fix proper handling of inventory per type of bottle???
            RefillInventoryDao refillInvDao = new RefillInventoryDao();
            RefillInventoryHeaderDataEntity inv = new RefillInventoryHeaderDataEntity();
            //inv = refillInvDao.GetByName("Cap");
            //inv.TotalQty = 500;
            //inv.QtyOnHand -= detail.StoreCapQty;
            //inv.QtyReleased += detail.StoreCapQty;
            //refillInvDao.Update(inv);
            inv = refillInvDao.GetByName("5 Gal Bottle");
            inv.QtyOnHand -= detail.StoreBottleQty;
            inv.QtyReleased += detail.StoreBottleQty;
            refillInvDao.Update(inv);

            // update cust inventory
            RefillCustomerInventoryDao custInvDao = new RefillCustomerInventoryDao();
            RefillCustInventoryHeaderDataEntity custInv= new RefillCustInventoryHeaderDataEntity();
            custInv.Customer = customer;
            custInv.CapsOnHand += detail.StoreCapQty;
            custInv.BottlesOnHand += detail.StoreBottleQty;

            header.Customer = customer;

            header.DetailEntities.Add(detail); // add detail to header details list
            header.AmountDue = detail.Amount;
            header.TotalQty = detail.Qty;
            header.AmountTender = header.AmountDue;

            if(header.AmountTender == header.AmountTender){
                header.PaidFlag = true;
            }
            else{
                header.PaidFlag = false;
            }

            // set paymentdetail
            RefillPaymentDetailDataEntity paymentdetail = new RefillPaymentDetailDataEntity();
            paymentdetail.Amount = header.AmountTender;
            paymentdetail.PaymentDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            paymentdetail.Header = header;
            header.PaymentDetailEntities.Add(paymentdetail);

            // set daysummary
            RefillDaySummaryDataEntity daysummary = new RefillDaySummaryDataEntity();
            daysummary.DayStamp = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            daysummary.TotalSales += header.AmountTender;
            daysummary.TransCount += 1;
            daysummary.HeaderEntities.Add(header); // set header entity in daysummary for nhibernate to pickup and map
            header.DaySummary = daysummary; // set daysummary entity in header for nhibernate to pickup and map

            custdao.SaveOrUpdate(customer); // save or update customer
            custInvDao.SaveOrUpdate(custInv);
            // save daysummary record; no need to explicitly save header,detail,jobcharges,paymentdetail, etc for new daysummary record
            // this will handle the saving for the linked tables
            RefillDao ldao = new RefillDao();
            ldao.SaveOrUpdate(header);
        }
        private bool SaveDaySummary(RefillHeaderDataEntity headerEntity)
        {
            try{
                DateTime today = Convert.ToDateTime(DateTime.Now.ToShortDateString()); // daystamp in daysummary should be date only (no time);
                RefillDaySummaryDataEntity daySummary = m_summaryDao.GetByDay(today);
                if(daySummary != null)
                {
                    daySummary.TransCount += 1;
                    //TODO: totalsales should be totalamoutdue - balance
                    //daySummary.TotalSales += headerEntity.PaymentDetailEntities[0].Amount;
                    if(headerEntity.PaymentDetailEntities[headerEntity.PaymentDetailEntities.Count-1] != null)
                        daySummary.TotalSales += headerEntity.PaymentDetailEntities[headerEntity.PaymentDetailEntities.Count-1].Amount;
                    else
                        daySummary.TotalSales += 0;
                    headerEntity.DaySummary = daySummary;

                    // update daysummary with transcount and totalsales
                    m_summaryDao.Update(daySummary);
                }else{
                    // set daysummary
                    daySummary = new RefillDaySummaryDataEntity();
                    daySummary.DayStamp = Convert.ToDateTime(DateTime.Now.ToShortDateString());
                    //TODO: totalsales should be amounttender - amount change.

                    //daySummary.TotalSales +=  headerEntity.PaymentDetailEntities[0].Amount;
                    if(headerEntity.PaymentDetailEntities[headerEntity.PaymentDetailEntities.Count-1] != null)
                        daySummary.TotalSales += headerEntity.PaymentDetailEntities[headerEntity.PaymentDetailEntities.Count-1].Amount;
                    else
                        daySummary.TotalSales += 0;
                    daySummary.TransCount += 1;

                    // set header entity in daysummary for nhibernate to pickup and map
                    daySummary.HeaderEntities.Add(headerEntity);
                    // set daysummary entity in header for nhibernate to pickup and map
                    headerEntity.DaySummary = daySummary;
                    //m_chargeDao.SaveOrUpdate(headerEntity.
                    //m_customerDao.SaveOrUpdate(headerEntity.Customer);
                    // save daysummary record; no need to explicitly save header,detail,jobcharges,paymentdetail, etc for new daysummary record
                    // this will handle the saving for the linked tables
                }
                m_refillDao.SaveOrUpdate(headerEntity);
                return true;
            }catch(Exception ex){
                MessageService.ShowError("Unexpected exception occured while saving your entries.\nPlease see log file for technical details.","Error in Saving", ex);
            }
            return false;
        }