Note that there are some explanatory texts on larger screens.

plurals
  1. POBulk Database Entries - Calculate Commission
    primarykey
    data
    text
    <p>I am writing an MVC 3 project and I'm using entity framework. I've written a method that generates bulk database entries, and this is for client's Invoices. The system is for a college and each student is enrolled with a specific tutor in the Enrollments class. The monthly fee of this enrollment is stored in Enrollments. The invoice method works correctly for generating invoices, but now I am looking to generate database entries for commission slips for the tutors of the college. The method would need to find all the students enrolled with each tutor (through the Enrollments class) and accumulate this amount. This total amount would then be multiplied by the Commission percentage the tutors receive. This would then be CommissionAmount. I am unsure how to write the code for this. These are the relevant model classes:</p> <pre><code>public class Enrollment { [Key] [Display(Name = "Enrollment ID Number")] public long EnrollmentIDNumber { get; set; } [Display(Name = "Client ID Number")] public long ClientNumberID { get; set; } [Display(Name = "Tutor ID Number")] public long TutorNoID { get; set; } [Display(Name = "Course Name")] public string CourseName { get; set; } [Display(Name = "Lesson Time")] public string LessonTime { get; set; } [Display(Name = "Lesson Day")] public string LessonDay { get; set; } [Display(Name = "Lesson Location")] public string LessonLocation { get; set; } [Display(Name = "Lesson Type")] public string LessonType { get; set; } [Display(Name = "Lesson Level")] public string LessonLevel { get; set; } [Display(Name = "Monthly Fee")] public long MonthlyFee { get; set; } public virtual Client Client { get; set; } public virtual Tutor Tutor { get; set; } } public class TutorCommission { [Key] [Display(Name = "Commission ID")] public long CommissionID { get; set; } [Display(Name = "Commission Month")] public string CommissionMonth {get; set;} [Display(Name = "Commission Amount")] public long CommissionAmount { get; set; } [Display(Name = "Commission Status")] public string CommissionStatus { get; set; } [Display(Name = "Tutor ID Number")] public long TutorNoID { get; set; } public virtual Tutor Tutor { get; set; } public virtual ICollection&lt;CommissionPayments&gt; CommissionPayments { get; set; } } public class TutorCommissionPercentage { [Key] public int TutorCommissionID { get; set; } public long TutorNoID { get; set; } [Range(0, 100, ErrorMessage="Percentage must be between 0 and 100")] [Display(Name="Commission Percentage")] public decimal CommissionPercentage { get; set; } public virtual Tutor Tutor { get; set; } } </code></pre> <p>The code for the generate Invoices is the following:</p> <pre><code> public ActionResult CreateBulkInvoices() { var month = DateTime.Now.ToString("MMMM"); var enrolments = db.Enrollments.ToList(); var newInvoices = from enrolment in enrolments select new Invoices() { InvoiceAmount = enrolment.MonthlyFee, InvoiceMonth = month, // string constant InvoiceStatus = "Unpaid", ClientNumberID = enrolment.ClientNumberID }; foreach (var newInvoice in newInvoices) { db.Invoice.Add(newInvoice); db.SaveChanges(); } return RedirectToAction("Index"); } public class Tutor { [Key] [Display(Name = "Tutor ID Number")] public long TutorNoID { get; set; } [Required] [StringLength(50, ErrorMessage="First name must be less than 50 characters")] [Display(Name = "First Name")] public string TutorFirstName { get; set; } [StringLength(50, ErrorMessage = "Last name must be less than 50 characters")] [Display(Name = "Last Name")] public string TutorLastName { get; set; } [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] [Display(Name = "Birth Date")] public DateTime? TutorBirthDate { get; set; } [Display(Name = "Cellphone Number")] public string TutorCellphoneNumber { get; set; } [Display(Name = "Home Number")] public string TutorHomeNumber { get; set; } [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage = "Not a valid email address")] [Display(Name = "Email Address")] public string TutorEmailAddress { get; set; } [Display(Name = "Street Address")] public string TutorStreetAddress { get; set; } [Display(Name = "Suburb")] public string TutorSuburb { get; set; } [Display(Name = "City")] public string TutorCity { get; set; } [Display(Name = "Postal Code")] public string TutorPostalCode { get; set; } [Display(Name="Full Name")] public string FullName { get { return TutorFirstName + " " + TutorLastName; } } [Display(Name="Commission Percentage")] [Required] public double TutorCommissionPercentage { get; set; } public virtual ICollection&lt;Enrollment&gt; Enrollments { get; set; } public virtual ICollection&lt;TutorCommission&gt; TutorCommissions { get; set; } } </code></pre> <p>The purpose of these bulk database entry methods is so that the manager can click one link and generate Invoices and Commission Slips for all clients and tutors in the database.</p> <p>Thanks Amy</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload