public void AddCustomerAccountLine( CustomerAccountLine accountLine )
 {
     Customer customer = accountLine.Customer.GetVanillaEntity();
     customer.CurrentBalance += accountLine.Amount;
     StorageContext.Current.Update( customer );
     StorageContext.Current.Insert( accountLine );
 }
        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 AcceptCustomerPayment( EntityRef<Customer> customer, decimal amount )
        {
            CustomerAccountLine accountLine = new CustomerAccountLine
                                                  {
                                                      Amount = amount,
                                                      Customer = customer,
                                                      Date = DateTime.Now,
                                                      Description = "Customer Payment",
                                                      Employee = this.Session.Employee
                                                  };

            this.AddCustomerAccountLine( accountLine );

            CashboxOperation cashboxOperation = new CashboxOperation
                                                    {
                                                        Amount = amount,
                                                        Cashbox = this.Session.Cashbox,
                                                        Date = DateTime.Now,
                                                        Description = string.Format( "Customer Payment: {0} ({1} {2})",
                                                                                     customer.Entity.CustomerId, customer.Entity.FirstName,
                                                                                     customer.Entity.LastName ),
                                                        Employee = this.Session.Employee
                                                    };

            this.cashboxProcesses.RegisterCashboxOperation( cashboxOperation );
        }