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);
        }
        public void saveNewPaymentDetail()
        {
            // test for saving new paymentdetail for partial payments
            RefillHeaderDataEntity header = new RefillDao().GetByID(2);

            header.AmountTender += 10.00M; // accumulate amount tender with current payment
            // set paymentdetail
            RefillPaymentDetailDataEntity paymentdetail = new RefillPaymentDetailDataEntity();
            paymentdetail.Amount = 10.00M; // current amount tender or payment
            paymentdetail.PaymentDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            paymentdetail.Header = header;

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

            // update daysummary
            DateTime daysummaryDay = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            RefillDaySummaryDataEntity daysummary = new RefillDaySummaryDao().GetByDay(daysummaryDay);
            if(daysummary != null)
            {
                daysummary.TotalSales += paymentdetail.Amount;
                daysummary.DayStamp = daysummaryDay;
            }
            RefillDaySummaryDao dao = new RefillDaySummaryDao();
            dao.SaveOrUpdate(daysummary);

            // update header, save payment detail etc.
            header.PaymentDetailEntities.Add(paymentdetail);
            RefillDao ldao = new RefillDao();
            ldao.Update(header);
        }
        public RefillHeaderDataEntity ProcessHeaderDataEntity()
        {
            int totalQty = 0;
            RefillHeaderDataEntity m_headerEntity = new RefillHeaderDataEntity();
            m_headerEntity.Date = dtDate.Value;
            m_headerEntity.Customer = m_presenter.getCustomerByName(cmbCustomers.Text);
            m_headerEntity.TransactionType = m_presenter.getTransactionTypeByName(cmbtransTypes.Text);
            m_headerEntity.AmountDue = decimal.Parse(txtamtdue.Text);
            //m_headerEntity.AmountTender = decimal.Parse(txtamttender.Text);
            List<RefillDetailDataEntity> refillDetails = new List<RefillDetailDataEntity>();
            foreach(DataGridViewRow row in this.dataGridView1.Rows){
                if(row.Cells[0].Value != null){
                    if(!string.IsNullOrEmpty(row.Cells[0].Value.ToString())){
                        RefillDetailDataEntity detail = new RefillDetailDataEntity();
                        detail.Header = m_headerEntity;
                        detail.ProductType = m_presenter.getProductByName(row.Cells[0].Value.ToString());
                        detail.StoreBottleQty = int.Parse(row.Cells[1].Value.ToString());
                        detail.StoreCapQty = int.Parse(row.Cells[2].Value.ToString());
                        detail.Qty = int.Parse(row.Cells[3].Value.ToString());
                        totalQty += detail.Qty;
                        detail.Amount = decimal.Parse(row.Cells[4].Value.ToString());
                        refillDetails.Add(detail);
                    }
                }
            }
            m_headerEntity.DetailEntities = refillDetails;
            m_headerEntity.PaidFlag = chkunpaid.Checked;
            RefillPaymentDetailDataEntity paymentDetail = new RefillPaymentDetailDataEntity();
            m_headerEntity.PaidFlag = false;

            if(decimal.Parse(txtamttender.Text) > 0){
                if(decimal.Parse(txtamttender.Text) >= m_headerEntity.AmountDue){
                    paymentDetail.Amount = m_headerEntity.AmountDue;
                    m_headerEntity.PaidFlag = true;
                }else{
                    paymentDetail.Amount = decimal.Parse(txtamttender.Text);
                }
            }else
                paymentDetail.Amount = 0M;

            m_headerEntity.AmountTender = paymentDetail.Amount;
            m_headerEntity.TotalQty = totalQty;
            paymentDetail.PaymentDate = Convert.ToDateTime(DateTime.Now);
            paymentDetail.Header = m_headerEntity;
            paymentDetail = paymentDetail.Amount > 0 ? paymentDetail : null;
            m_headerEntity.PaymentDetailEntities.Add(paymentDetail);

            return m_headerEntity;
        }
        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);

            RefillDaySummaryDao summarydao = new RefillDaySummaryDao();
            RefillDaySummaryDataEntity summary = summarydao.GetByDay(sampleDay);
            if(summary!= null)
            {
                RefillHeaderDataEntity header = new RefillHeaderDataEntity();
                RefillDetailDataEntity detail = new RefillDetailDataEntity();

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

                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.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 = 20;
                detail.Amount = productType.Price * Convert.ToDecimal(detail.Qty);
                detail.StoreBottleQty = 10;
                detail.StoreCapQty = 10;

                header.DetailEntities.Add(detail); // add detail to header details list
                header.AmountDue = detail.Amount;
                header.TotalQty = detail.Qty;
                header.AmountTender += 25.00M; // accumulate amount tender with current amount tender

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

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

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

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

                // save header,details,etc.
                RefillDao ldao = new RefillDao();
                ldao.SaveOrUpdate(header);
            }
        }
 private RefillPaymentDetailDataEntity CreateNewPayment(decimal amount, RefillHeaderDataEntity header, DateTime paymentDate)
 {
     RefillPaymentDetailDataEntity payment = new RefillPaymentDetailDataEntity();
     payment.Header = header;
     payment.Amount = amount;
     payment.PaymentDate = paymentDate;
     return payment;
 }