public CustomerInfo GetCustomerInfo(EntityRef <Customer> customer, bool getAllRentals)
        {
            if (customer.IsNull)
            {
                throw new ArgumentNullException("customer");
            }

            CustomerInfo customerInfo = new CustomerInfo {
                Customer = customer.GetVanillaEntity(), Rentals = new List <RentalInfo>()
            };

            foreach (Rental rental in StorageContext.Current.Find <Rental>(candidate => candidate.Customer == customer && (getAllRentals || !candidate.Closed)))
            {
                RentalInfo rentalInfo = new RentalInfo();
                rentalInfo.Book   = rental.Book.Entity;
                rentalInfo.Rental = rental;
                rentalInfo.Notes  = new List <Note>(StorageContext.Current.Find <Note>(candidate => candidate.Owner == rental));
                customerInfo.Rentals.Add(rentalInfo);
            }

            customerInfo.AccountLines = new List <CustomerAccountLine>(StorageContext.Current.Find <CustomerAccountLine>(
                                                                           candidate => candidate.Customer == customer));

            return(customerInfo);
        }
        public void ReportLostBook(EntityRef <Rental> rentalRef)
        {
            if (rentalRef.IsNull)
            {
                throw new ArgumentNullException("rentalRef");
            }

            Rental rental = rentalRef.GetVanillaEntity();
            Book   book   = rental.Book.GetVanillaEntity();

            // Check business rules.
            BusinessRulesManager.Assert("ReportLostBook", rental);

            // Apply the penalty.
            CustomerAccountLine penalty = new CustomerAccountLine
            {
                Customer    = rental.Customer,
                Employee    = this.Session.Employee,
                Rental      = rentalRef,
                Date        = DateTime.Today,
                Amount      = -book.LostPenalty,
                Description = string.Format(
                    "Lost of the book [{0}; {1}]",
                    book.Authors,
                    book.Title)
            };

            this.customerProcesses.AddCustomerAccountLine(penalty);

            // Create a note for the penalty.
            Note note = new Note
            {
                Owner    = rental,
                Employee = this.Session.Employee,
                Date     = DateTime.Now,
                Title    = "Penalty Applied",
                Text     = string.Format(
                    "A penalty of {2} EUR was applied for the lost of the book [{0}; {1}]",
                    book.Authors,
                    book.Title,
                    -penalty.Amount)
            };

            StorageContext.Current.Insert(note);

            // Update the rental.
            rental.ReturnDate = DateTime.Now;
            rental.Closed     = true;
            StorageContext.Current.Update(rental);

            // Delete the book.
            book.Deleted = true;
            StorageContext.Current.Update(book);
        }
        public void ReturnBook(EntityRef <Rental> rentalRef)
        {
            if (rentalRef.IsNull)
            {
                throw new ArgumentNullException("rentalRef");
            }

            Rental rental = rentalRef.GetVanillaEntity();

            // Check business rules.
            BusinessRulesManager.Assert("ReturnBook", rental);

            // Check if the book has been returned in time and apply penalty.
            if (DateTime.Today > rental.ScheduledReturnDate)
            {
                int delay = (int)Math.Ceiling((DateTime.Today - rental.ScheduledReturnDate).TotalDays);

                CustomerAccountLine penalty = new CustomerAccountLine
                {
                    Customer    = rental.Customer,
                    Employee    = this.Session.Employee,
                    Rental      = rentalRef,
                    Date        = DateTime.Today,
                    Amount      = -0.1M * delay,
                    Description = string.Format(
                        "Delay of {0} day(s) while returning the book [{1}; {2}]",
                        delay,
                        rental.Book.Entity.Authors,
                        rental.Book.Entity.Title)
                };

                this.customerProcesses.AddCustomerAccountLine(penalty);


                Note note = new Note
                {
                    Owner    = rental,
                    Title    = "Penalty Applied",
                    Employee = this.Session.Employee,
                    Date     = DateTime.Now,
                    Text     = string.Format(
                        "A penalty of {3} EUR was applied for a {0}-day(s) delay while returning the book [{1}; {2}]",
                        delay,
                        rental.Book.Entity.Authors,
                        rental.Book.Entity.Title,
                        -penalty.Amount)
                };
                StorageContext.Current.Insert(note);
            }

            rental.ReturnDate = DateTime.Now;
            rental.Closed     = true;
            StorageContext.Current.Update(rental);
        }
        public void ReportLostBook( EntityRef<Rental> rentalRef )
        {
            if ( rentalRef.IsNull )
                throw new ArgumentNullException( "rentalRef" );

            Rental rental = rentalRef.GetVanillaEntity();
            Book book = rental.Book.GetVanillaEntity();

            // Check business rules.
            BusinessRulesManager.Assert( "ReportLostBook", rental );

            // Apply the penalty.
            CustomerAccountLine penalty = new CustomerAccountLine
                                              {
                                                  Customer = rental.Customer,
                                                  Employee = this.Session.Employee,
                                                  Rental = rentalRef,
                                                  Date = DateTime.Today,
                                                  Amount = -book.LostPenalty,
                                                  Description = string.Format(
                                                      "Lost of the book [{0}; {1}]",
                                                      book.Authors,
                                                      book.Title )
                                              };
            this.customerProcesses.AddCustomerAccountLine( penalty );

            // Create a note for the penalty.
            Note note = new Note
                            {
                                Owner = rental,
                                Employee = this.Session.Employee,
                                Date = DateTime.Now,
                                Title = "Penalty Applied",
                                Text = string.Format(
                                    "A penalty of {2} EUR was applied for the lost of the book [{0}; {1}]",
                                    book.Authors,
                                    book.Title,
                                    -penalty.Amount )
                            };
            StorageContext.Current.Insert( note );

            // Update the rental.
            rental.ReturnDate = DateTime.Now;
            rental.Closed = true;
            StorageContext.Current.Update( rental );

            // Delete the book.
            book.Deleted = true;
            StorageContext.Current.Update( book );
        }
      //  [Transaction]
        public void DeleteCustomer( EntityRef<Customer> customerRef )
        {
            if ( customerRef.IsNull )
                throw new ArgumentNullException( "customerRef" );

            // Get a safe version of the customer.
            Customer customer = customerRef.GetVanillaEntity();

            // Check business rules.
            BusinessRulesManager.Assert( "DeleteCustomer", customer );

            // Update the 'Deleted' flag.
            customer.Deleted = true;
            StorageContext.Current.Update( customer );
        }
        //  [Transaction]
        public void DeleteCustomer(EntityRef <Customer> customerRef)
        {
            if (customerRef.IsNull)
            {
                throw new ArgumentNullException("customerRef");
            }

            // Get a safe version of the customer.
            Customer customer = customerRef.GetVanillaEntity();

            // Check business rules.
            BusinessRulesManager.Assert("DeleteCustomer", customer);

            // Update the 'Deleted' flag.
            customer.Deleted = true;
            StorageContext.Current.Update(customer);
        }
        public void ReturnBook( EntityRef<Rental> rentalRef )
        {
            if ( rentalRef.IsNull )
                throw new ArgumentNullException( "rentalRef" );

            Rental rental = rentalRef.GetVanillaEntity();

            // Check business rules.
            BusinessRulesManager.Assert( "ReturnBook", rental );

            // Check if the book has been returned in time and apply penalty.
            if ( DateTime.Today > rental.ScheduledReturnDate )
            {
                int delay = (int) Math.Ceiling( ( DateTime.Today - rental.ScheduledReturnDate ).TotalDays );

                CustomerAccountLine penalty = new CustomerAccountLine
                                                  {
                                                      Customer = rental.Customer,
                                                      Employee = this.Session.Employee,
                                                      Rental = rentalRef,
                                                      Date = DateTime.Today,
                                                      Amount = -0.1M*delay,
                                                      Description = string.Format(
                                                          "Delay of {0} day(s) while returning the book [{1}; {2}]",
                                                          delay,
                                                          rental.Book.Entity.Authors,
                                                          rental.Book.Entity.Title )
                                                  };

                this.customerProcesses.AddCustomerAccountLine( penalty );


                Note note = new Note
                                {
                                    Owner = rental,
                                    Title = "Penalty Applied",
                                    Employee = this.Session.Employee,
                                    Date = DateTime.Now,
                                    Text = string.Format(
                                        "A penalty of {3} EUR was applied for a {0}-day(s) delay while returning the book [{1}; {2}]",
                                        delay,
                                        rental.Book.Entity.Authors,
                                        rental.Book.Entity.Title,
                                        -penalty.Amount )
                                };
                StorageContext.Current.Insert( note );
            }

            rental.ReturnDate = DateTime.Now;
            rental.Closed = true;
            StorageContext.Current.Update( rental );
        }
        public CustomerInfo GetCustomerInfo( EntityRef<Customer> customer, bool getAllRentals )
        {
            if ( customer.IsNull )
                throw new ArgumentNullException( "customer" );

            CustomerInfo customerInfo = new CustomerInfo {Customer = customer.GetVanillaEntity(), Rentals = new List<RentalInfo>()};

            foreach ( Rental rental in StorageContext.Current.Find<Rental>( candidate => candidate.Customer == customer && (getAllRentals || !candidate.Closed) ) )
            {
                RentalInfo rentalInfo = new RentalInfo();
                rentalInfo.Book = rental.Book.Entity;
                rentalInfo.Rental = rental;
                rentalInfo.Notes = new List<Note>( StorageContext.Current.Find<Note>( candidate => candidate.Owner == rental ) );
                customerInfo.Rentals.Add( rentalInfo );
            }

            customerInfo.AccountLines = new List<CustomerAccountLine>( StorageContext.Current.Find<CustomerAccountLine>(
                                                                           candidate => candidate.Customer == customer ) );

            return customerInfo;
        }