public void SaveOrUpdate(LaundryDetailDataEntity p_detail)
 {
     using(var session = NHibernateHelper.OpenSession())
     {
         using(var transaction = session.BeginTransaction())
         {
             try
             {
                 session.SaveOrUpdate(p_detail);
                 transaction.Commit();
             }
             catch(Exception ex)
             {
                 transaction.Rollback();
                 throw ex;
             }
         }
     }
 }
        public LaundryHeaderDataEntity ProcessHeaderDataEntity()
        {
            int totalItemQty = 0;
            int itemQty = 0;

            //TODO:calculate totalpayment;if claim then retrieve values from paymentdetail table else totalamtdue - balance
            //m_headerEntity.TotalPayment = decimal.Parse(this.txtamttender.Text);
            m_headerEntity.TotalAmountDue = decimal.Parse(txttotalamtdue.Text);

            m_headerEntity.ReceivedDate = dtrecieveDate.Value;
            m_headerEntity.DueDate = dtdueDate.Value;

            //m_headerEntity.ClaimFlag = btnclaim.Enabled;
            m_headerEntity.Customer = m_presenter.getCustomerByName(cmbCustomers.Text);
            m_headerEntity.DetailEntities = new List<LaundryDetailDataEntity>();
            foreach(DataGridViewRow row in this.dataGridView1.Rows){
                if(row.Cells[0].Value != null){
                    if(!string.IsNullOrEmpty(row.Cells[0].Value.ToString())){
                        LaundryDetailDataEntity detail = new LaundryDetailDataEntity();
                        detail.Header = m_headerEntity;
                        detail.Category = m_presenter.getCategoryByName(row.Cells[1].Value.ToString());
                        detail.Service = m_presenter.getServiceByName(row.Cells[0].Value.ToString());
                        detail.Kilo = double.Parse(row.Cells[2].Value.ToString());
                        itemQty = int.Parse(row.Cells[3].Value.ToString());
                        detail.ItemQty = itemQty;
                        totalItemQty += itemQty;
                        detail.Amount = decimal.Parse(row.Cells[4].Value.ToString());
                        m_headerEntity.DetailEntities.Add(detail);
                    }
                }
            }
            m_headerEntity.TotalItemQty = totalItemQty;
            m_headerEntity.TotalAmountDue = decimal.Parse(txttotalamtdue.Text);
            m_headerEntity.TotalCharge = decimal.Parse(txttotalcharges.Text);
            m_headerEntity.TotalDiscount = decimal.Parse(txttotaldiscount.Text);
            m_headerEntity.AmountDue = decimal.Parse(txtamtdue.Text);

            LaundryPaymentDetailDataEntity paymentdetail = new LaundryPaymentDetailDataEntity();

            if(decimal.Parse(txtamttender.Text) > 0){
                if(decimal.Parse(txtamttender.Text) >=  (decimal.Parse(txttotalamtdue.Text) - m_headerEntity.TotalPayment)){
                    paymentdetail.Amount = decimal.Parse(txttotalamtdue.Text) - m_headerEntity.TotalPayment;
                }else{
                    //paymentdetail.Amount = m_headerEntity.TotalAmountDue - decimal.Parse(txtbalance.Text);
                    paymentdetail.Amount = decimal.Parse(txtamttender.Text);
                }
            }else
                paymentdetail.Amount = 0;

            if(this.Text.Contains("NEW"))
                m_headerEntity.TotalPayment = paymentdetail.Amount;
            else
                m_headerEntity.TotalPayment += paymentdetail.Amount;

            paymentdetail.PaymentDate = Convert.ToDateTime(DateTime.Now);
            paymentdetail.Header = m_headerEntity;
            paymentdetail = paymentdetail.Amount > 0 ? paymentdetail : null;
            m_headerEntity.PaymentDetailEntities.Add(paymentdetail);
            m_headerEntity.JobChargeEntities = new List<LaundryJobChargesDataEntity>();
            foreach(object checkedItem in this.chkchargesList.CheckedItems){
                m_jobcharge = new LaundryJobChargesDataEntity();
                m_jobcharge.Charge = m_presenter.getJobChargeByName(checkedItem.ToString());
                m_jobcharge.Header = m_headerEntity;
                m_headerEntity.JobChargeEntities.Add(m_jobcharge);
            }

            m_headerEntity.PaidFlag = (decimal.Parse(txtbalance.Text) == 0) ? true : false;

            return m_headerEntity;
        }
        public void saveNewHeaderAndNewDaySummary()
        {
            // test for new header and new day
            // should create new record for daysummary
            LaundryHeaderDataEntity header = new LaundryHeaderDataEntity();
            LaundryDetailDataEntity detail = new LaundryDetailDataEntity();
            LaundryJobChargesDataEntity jobcharge = new LaundryJobChargesDataEntity();

            LaundryCategoryDataEntity category = new LaundryCategoryDao().GetByName("Colored Garments");
            LaundryServiceDataEntity service = new LaundryServiceDao().GetByName("Wash - Dry - Fold");
            LaundryPriceSchemeDataEntity pricescheme = new LaundryPriceSchemeDao().GetByCategoryService(service,category);

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

            header.Customer = customer;
            header.ReceivedDate = DateTime.Now;
            header.DueDate = DateTime.Now.AddDays(5); // add 5 days for due date

            detail.Header = header; // set header entity in detail for nhibernate to pickup and map
            detail.Category = category;
            detail.Service = service;
            detail.Kilo = 5;
            detail.Amount = pricescheme.Price * Convert.ToDecimal(detail.Kilo);
            detail.ItemQty = 20;

            jobcharge.Charge = new LaundryChargeDao().GetByName("Delivery");
            jobcharge.Header = header; // set header entity in jobcharge for nhibernate to pickup and map

            header.DetailEntities.Add(detail); // add detail to header details list
            header.JobChargeEntities.Add(jobcharge); // add charges to header charges list

            header.ClaimFlag = false;
            header.AmountDue = detail.Amount;
            header.TotalItemQty = detail.ItemQty;
            header.TotalCharge = jobcharge.Charge.Amount;
            header.TotalDiscount = 0;
            header.TotalAmountDue = (header.AmountDue + header.TotalCharge) - header.TotalDiscount;
            header.TotalPayment = header.TotalAmountDue;

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

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

            // set daysummary
            LaundryDaySummaryDataEntity daysummary = new LaundryDaySummaryDataEntity();
            daysummary.DayStamp = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            daysummary.TotalSales += header.TotalPayment;
            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
            // 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

            // FIX: save new header & new daysummary thru laundrydao instead of laundrydaysummary
            LaundryDao dao = new LaundryDao();
            dao.SaveOrUpdate(header);
        }
        public void saveNewHeaderAndUpdateDaySummary()
        {
            // test for new header but with existing daysummary
            // should not save new record for daysummary
            // should only update existing daysummary with transcount and sales

            DateTime sampleDay = Convert.ToDateTime(DateTime.Now.ToShortDateString()); // daystamp in daysummary should be date only (no time);

            LaundryDaySummaryDao summarydao = new LaundryDaySummaryDao();
            LaundryDaySummaryDataEntity summary = summarydao.GetByDay(sampleDay);
            if(summary!= null)
            {
                LaundryHeaderDataEntity header = new LaundryHeaderDataEntity();
                LaundryDetailDataEntity detail = new LaundryDetailDataEntity();
                LaundryJobChargesDataEntity jobcharge = new LaundryJobChargesDataEntity();

                LaundryCategoryDataEntity category = new LaundryCategoryDao().GetByName("White Garments");
                LaundryServiceDataEntity service = new LaundryServiceDao().GetByName("Wash - Dry - Press");
                LaundryPriceSchemeDataEntity pricescheme = new LaundryPriceSchemeDao().GetByCategoryService(service,category);

                CustomerDao custdao = new CustomerDao();
                CustomerDataEntity customer = custdao.GetByName("John Dee");
                if(customer == null)
                {
                    customer = new CustomerDataEntity();
                    customer.Name = "John Dee";
                    customer.Address = "Cebu";
                    customer.ContactNumber = "111-1111";
                }
                header.Customer = customer;
                header.ReceivedDate = DateTime.Now;
                header.DueDate = DateTime.Now.AddDays(5); // add 5 days for due date

                detail.Header = header; // set header entity in detail for nhibernate to pickup and map
                detail.Category = category;
                detail.Service = service;
                detail.Kilo = 100;
                detail.Amount = pricescheme.Price * Convert.ToDecimal(detail.Kilo);
                detail.ItemQty = 300;

                jobcharge.Charge = new LaundryChargeDao().GetByName("Delivery");
                jobcharge.Header = header; // set header entity in jobcharge for nhibernate to pickup and map

                header.DetailEntities.Add(detail); // add detail to header details list
                header.JobChargeEntities.Add(jobcharge); // add charges to header charges list

                header.ClaimFlag = false;
                header.AmountDue = detail.Amount;
                header.TotalItemQty = detail.ItemQty;
                header.TotalCharge = jobcharge.Charge.Amount;
                header.TotalDiscount = 50.00M;
                header.TotalAmountDue = (header.AmountDue + header.TotalCharge) - header.TotalDiscount;
                header.TotalPayment += 50.00M; // accumulate amount tender with current amount tender

                // TODO: should update paidflag in header if total balance = 0.

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

                summary.TransCount += 1;
                summary.TotalSales += header.TotalPayment;
                header.DaySummary = summary;

                // update daysummary with transcount and totalsales
                LaundryDaySummaryDao dao = new LaundryDaySummaryDao();
                dao.Update(summary);

                // save header,details,etc.
                LaundryDao ldao = new LaundryDao();
                ldao.SaveOrUpdate(header);
            }
        }
        private void SaveUpdateDetails()
        {
            //LaundryHeaderDataEntity headerData = m_laundryDao.GetByID(m_headerEntity.LaundryHeaderID);
            List<LaundryDetailDataEntity> detailData = m_detailDao.GetAllItemsByHeaderId(m_headerEntity.LaundryHeaderID) as List<LaundryDetailDataEntity>;

            var listToLookUp = detailData.ToLookup(entity => entity.Category.Name);
            var listToAdd = new_DetailEntities.Where(entity => (!listToLookUp.Contains(entity.Category.Name)));
            var listToUpdate = new_DetailEntities.Where(entity => listToLookUp.Contains(entity.Category.Name));
            List<LaundryDetailDataEntity> newDetailList = new List<LaundryDetailDataEntity>();

            foreach(LaundryDetailDataEntity detail in listToUpdate.ToList()){
                LaundryDetailDataEntity entity = new LaundryDetailDataEntity();
                LaundryDetailDataEntity m_entity = detailData.Find(x => x.Category.Name == detail.Category.Name && x.Service.Name == detail.Service.Name);
                if(m_entity != null){
                    entity.ItemQty = detail.ItemQty;
                    entity.Kilo = detail.Kilo;
                    entity.Amount = detail.Amount;
                    entity.Category = detail.Category;
                    entity.Service = detail.Service;
                    entity.Header = detail.Header;
                    entity.ID = m_entity.ID;
                    newDetailList.Add(entity);
                }
            }

            foreach(LaundryDetailDataEntity detail in listToAdd.ToList()){
                newDetailList.Add(detail);
            }

            m_OriginalHeaderEntity.DetailEntities = newDetailList;
        }