Ejemplo n.º 1
0
        /// <summary>
        /// Submit button is clicked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_SubmitAddNewUser_Click_1(object sender, EventArgs e)
        {
            if(validate())
            return; //Validation fails.

            //Validation successful.

            //Create a new CustomerDetail object, fetch form-field-values and insert into the DB.
            CustomerDetail customerDetail = new CustomerDetail();
            int lastInsertedCustomerId = db.CustomerDetails.Count();
            customerDetail.CustomerId = lastInsertedCustomerId + 1; //New Customer's id.

            fetchFormValues(customerDetail);

            bool exceptionOccured = false;
            try
            {
                bool success = false;

                /* For every new Customer inserted into the DB, entries are made in 2 tables - CustomerDetails and CustomerDues.
                 * Hence a transaction is required. */
                using (TransactionScope transaction = new TransactionScope())
                {
                    try
                    {
                        db.CustomerDetails.AddObject(customerDetail);

                        //Pending Balance should be inserted in the CustomerDues table.
                        CustomerDue dues = new CustomerDue();
                        dues.CustomerId = customerDetail.CustomerId;
                        int dueAmount = 0;
                        Int32.TryParse(textBox_currentBalance.Text.Trim(), out dueAmount);
                        dues.DueAmount = dueAmount;

                        db.CustomerDues.AddObject(dues);

                        db.SaveChanges();
                        transaction.Complete();
                        success = true;
                    }
                    catch (Exception ex)
                    {
                        success = false;
                    }
                }
                if (success)
                    db.AcceptAllChanges(); //Transaction was successful, commit all changes.
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                exceptionOccured = true;
            }

            if (!exceptionOccured)
            {
                MessageBox.Show("User added successfully.", "Success");
            }
            else
            {
                MessageBox.Show("Something went wrong.", "Error");
            }
        }
 /// <summary>
 /// Create a new CustomerDue object.
 /// </summary>
 /// <param name="customerId">Initial value of the CustomerId property.</param>
 /// <param name="dueAmount">Initial value of the DueAmount property.</param>
 /// <param name="carryforwardAmount">Initial value of the CarryforwardAmount property.</param>
 public static CustomerDue CreateCustomerDue(global::System.Int32 customerId, global::System.Int32 dueAmount, global::System.Int32 carryforwardAmount)
 {
     CustomerDue customerDue = new CustomerDue();
     customerDue.CustomerId = customerId;
     customerDue.DueAmount = dueAmount;
     customerDue.CarryforwardAmount = carryforwardAmount;
     return customerDue;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the CustomerDues EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToCustomerDues(CustomerDue customerDue)
 {
     base.AddObject("CustomerDues", customerDue);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Based on the changes made/not made to the current bill being displayed, updates the Customer's dues.
        /// </summary>
        /// <param name="customerId">The customerId of the Customer whose dues have to be recalculated.</param>
        public static void updateCustomerDues(int customerId)
        {
            Int32 totalPayableAmount;
            try
            {
                totalPayableAmount = db.MonthlyBills.Where(x => x.CustomerId == customerId).Sum(x => x.LunchAmount + x.DinnerAmount - x.DailyPayment);
            }
            catch (Exception ex)
            {
                totalPayableAmount = 0;
            }

            Int32 totalPaidAmount;
            try
            {
                totalPaidAmount = db.CustomerPaymentHistories.Where(x => x.CustomerId == customerId).Sum(x => x.PaidAmount);
            }
            catch (Exception ex)
            {
                totalPaidAmount = 0;
            }

            Int32 dabbawalaCharges;
            try
            {
                dabbawalaCharges = db.ExtraCharges.Where(x => x.CustomerId == customerId).Sum(x => x.DabbawalaCharges);
            }
            catch (Exception ex)
            {
                dabbawalaCharges = 0;
            }

            Int32 deliveryCharges;
            try
            {
                deliveryCharges = db.ExtraCharges.Where(x => x.CustomerId == customerId).Sum(x => x.DeliveryCharges);
            }
            catch (Exception ex)
            {
                deliveryCharges = 0;
            }

            Int32 initialBalance = db.CustomerDetails.Where(x => x.CustomerId == customerId && x.isDeleted.Equals("N")).First().InitialBalance;

            Int32 totalDueAmount = totalPayableAmount + dabbawalaCharges + deliveryCharges + initialBalance - totalPaidAmount;

            //Put values in CustomerDues tables
            CustomerDue updatedDues = db.CustomerDues.Where(x => x.CustomerId == customerId).FirstOrDefault();

            if (updatedDues == null)
                updatedDues = new CustomerDue();

            updatedDues.CustomerId = customerId;
            if (totalDueAmount < 0)
            {
                updatedDues.CarryforwardAmount = -1 * totalDueAmount;
                updatedDues.DueAmount = 0;
            }
            else
            {
                updatedDues.DueAmount = totalDueAmount;
                updatedDues.CarryforwardAmount = 0;
            }

            if (db.CustomerDues.Where(x => x.CustomerId == customerId).Count() == 0)
            {
                db.CustomerDues.AddObject(updatedDues);
            }

            db.SaveChanges();
        }