Esempio n. 1
0
        //Renews all Items in a loan as a new loan
        public void RenewLoan(LoansClientsInventoryDTO renewedDTO)
        {
            var loanNumber = renewedDTO.LoanNumber;

            using (var context = new SingularityDBContext())
            {
                var loan = context.LoanMasters.FirstOrDefault(
                    lm => string.Equals(lm.LoanNumber, loanNumber));

                if (loan != null)  // Always check for Null
                {
                    loan.IsActive = false;

                    loan.ClientOutcome = renewedDTO.ClientOutcome;
                    loan.LoanNotes     = renewedDTO.LoanNotes;
                    //do not worry about item damages for now
                }

                context.SaveChanges();

                foreach (var itemId in renewedDTO.InventoryItems)
                {
                    var item = context.InventoryItems.FirstOrDefault(
                        i => i.InventoryItemId == itemId.InventoryItemId);

                    if (item != null)
                    {
                        item.Availability = true;
                    }

                    context.SaveChanges();
                }
            }
        }
        public void SaveClient(Client client)
        {
            using (var context = new SingularityDBContext())
            {
                client.DateCreated = DateTime.Now;  // Manipulating the client object is done before saving to Db

                context.Clients.Add(client);

                context.SaveChanges();

                // No Any() needed null check covers it

                if (client.DisabilityIds != null)

                // what was the thing that checked for null before Any()? default type for Enum?

                {
                    var clientDisabilities = client.DisabilityIds
                                             .Select(disabilityId => new ClientDisability
                    {
                        ClientId = client.ClientID, DisabilityCategoryId = disabilityId
                    });

                    context.ClientDisabilities.AddRange(clientDisabilities);

                    context.SaveChanges();
                }
            }
        }
Esempio n. 3
0
        public void EditLoan(LoansClientsInventoryDTO loanSubmission)
        {
            //have client id
            using (var context = new SingularityDBContext())
            {
                IEnumerable <LoanDetail> query = from itemId in loanSubmission.InventoryItemIds
                                                 select new LoanDetail
                {
                    InventoryItemId = itemId,
                    LoanMasterId    = loanSubmission.LoanMasterId,
                    Purpose         = loanSubmission.Purpose,
                    PurposeType     = loanSubmission.PurposeType
                };
                List <LoanDetail> loanDetailsList = query.ToList();

                ////Update New Inventory Items' Availability
                foreach (var itemId in query)
                {
                    var item = (from ii in context.InventoryItems
                                where ii.InventoryItemId == itemId.InventoryItemId
                                select ii).FirstOrDefault();

                    if (item != null)
                    {
                        item.Availability = false;
                    }

                    context.SaveChanges();
                }
                //now we can add a range of loan details to the loan details table
                context.LoanDetails.AddRange(loanDetailsList);
                context.SaveChanges();
            }
        }
Esempio n. 4
0
        public void CreateRenewedLoan(LoansClientsInventoryDTO loanSubmission)
        {
            using (var context = new SingularityDBContext())
            {
                var loanNumIncrement = LoanIncrement();

                var newLoan = new LoanMaster
                {
                    ClientId    = loanSubmission.ClientId,
                    DateCreated = DateTime.Now,
                    LoanNumber  = loanNumIncrement,
                    IsActive    = true,
                    IsDeleted   = false
                };

                context.LoanMasters.Add(newLoan);
                context.SaveChanges();

                LoanDetail[] itemsListed = new LoanDetail[loanSubmission.InventoryItems.Count];

                for (int i = 0; i < itemsListed.Length; i++)
                {
                    foreach (var itemId in loanSubmission.InventoryItems)
                    {
                        //if (loanSubmission.Availability)
                        //{
                        itemsListed[i] = new LoanDetail
                        {
                            InventoryItemId = itemId.InventoryItemId,
                            LoanMasterId    = newLoan.LoanMasterId,
                            Purpose         = loanSubmission.Purpose,
                            PurposeType     = loanSubmission.PurposeType
                        };
                        i++;
                        //};
                    }
                    ;
                    //break;
                }
                List <LoanDetail> loanDetailsList = itemsListed.ToList();

                //now we can add a range of loan details to the loan details table
                context.LoanDetails.AddRange(loanDetailsList);
                context.SaveChanges();

                //Update Inventory Items' Availability
                var itemIds = GetInventoryItemIdsByLoanNumber(newLoan.LoanNumber);
                MarkInventoryItemsAsNotAvailable(context, itemIds);
            }
        }
Esempio n. 5
0
        public void CheckInLoan_Nick(LoansClientsInventoryDTO checkInDTO) // copied into RenewLoan below
        {
            var loanNumber = checkInDTO.LoanNumber;

            using (var context = new SingularityDBContext())
            {
                var loan = context.LoanMasters.FirstOrDefault(
                    lm => string.Equals(lm.LoanNumber, loanNumber));

                if (loan != null)
                {
                    loan.IsActive = false;

                    loan.ClientOutcome = checkInDTO.ClientOutcome;
                    loan.LoanNotes     = checkInDTO.LoanNotes;
                    //loan.Damages = checkInDTO.Damages;
                }

                context.SaveChanges();

                var itemIds = GetInventoryItemIdsByLoanNumber(loanNumber);

                MarkInventoryItemsAsAvailable(context, itemIds);
            }
        }
        //Update existing client info
        public void EditClientDetails(Client client, IEnumerable <int> DisabilityIds)
        {
            using (var context = new SingularityDBContext())
            {
                context.Clients.Attach(client);

                var entry = context.Entry(client);

                entry.State = EntityState.Modified;

                context.SaveChanges();



                if (DisabilityIds != null)  // null check required to cover posting on null DisabilityIds

                {
                    var oldClientDisabilities = context.ClientDisabilities.Where(cd => cd.ClientId == client.ClientID);

                    context.ClientDisabilities.RemoveRange(oldClientDisabilities);

                    context.SaveChanges();


                    var clientDisabilities = DisabilityIds                                //Access the IEnumerable<int> of DisabilityIds
                                             .Select(DisabilityId => new ClientDisability //Foreach create a new cd object
                    {
                        ClientId = client.ClientID, DisabilityCategoryId = DisabilityId
                    });

                    // After the ClientDisability Ojects are created and added to var, they get added to the table

                    context.ClientDisabilities.AddRange(clientDisabilities);

                    context.SaveChanges();
                }

                else
                {
                    var oldClientDisabilities = context.ClientDisabilities.Where(cd => cd.ClientId == client.ClientID);

                    context.ClientDisabilities.RemoveRange(oldClientDisabilities);

                    context.SaveChanges();
                }
            }
        }
Esempio n. 7
0
        //Updates the Inventory CheckIn DB fields
        public void CheckInLoanInventoryItem(LoansClientsInventoryDTO loan)
        {
            using (var context = new SingularityDBContext())
            {
                var loanNum = loan.LoanNumber;
                var itemIds = GetInventoryItemIdsByLoanNumber(loanNum);

                //itemIds is IEnumerable<int>
                //Update Item
                if (itemIds.Count() > 1)
                {
                    var itemId = (from ii in context.InventoryItems
                                  where ii.InventoryItemId == loan.InventoryItemId
                                  select ii).FirstOrDefault();

                    if (itemId != null)
                    {
                        itemId.Availability = true;
                        //itemId.Damages = loan.Damages;
                        //do not worry about item damages for now
                    }
                    context.SaveChanges();
                }
                else
                {
                    //CheckInLoan
                    var loanNumber = loan.LoanNumber;

                    var query = context.LoanMasters.FirstOrDefault(
                        lm => string.Equals(lm.LoanNumber, loanNumber));

                    if (query != null)
                    {
                        query.IsActive = false;
                    }

                    context.SaveChanges();

                    var itemIds2 = GetInventoryItemIdsByLoanNumber(loanNumber);

                    MarkInventoryItemsAsAvailable(context, itemIds2);
                }
            }
        }
Esempio n. 8
0
        public void RemoveSingleItemFromLoanByItemEdit(string loanNum)
        {
            using (var context = new SingularityDBContext())
            {
                var itemIds = GetInventoryItemIdsByLoanNumber(loanNum);

                if (itemIds.Count() > 1)
                {
                    //get inv item to available
                    if (itemIds != null)
                    {
                        MarkInventoryItemsAsAvailable(context, itemIds);
                    }
                    var loanId = (from ld in context.LoanDetails
                                  join lm in context.LoanMasters
                                  on ld.LoanMasterId equals lm.LoanMasterId
                                  where lm.LoanNumber == loanNum
                                  select ld).FirstOrDefault();


                    //remove the loan detail itself
                    context.LoanDetails.Remove(loanId);
                    context.SaveChanges();
                }
                else
                {
                    var loanNumber = loanNum;

                    var query = context.LoanMasters.FirstOrDefault(
                        lm => string.Equals(lm.LoanNumber, loanNumber));

                    if (query != null)
                    {
                        query.IsActive = false;
                    }

                    context.SaveChanges();

                    var itemIds2 = GetInventoryItemIdsByLoanNumber(loanNumber);

                    MarkInventoryItemsAsAvailable(context, itemIds2);
                }
            }
        }
        public void ChangeStatusDeleted(int id)
        {
            using (var context = new SingularityDBContext())
            {
                var client = context.Clients.FirstOrDefault(x => x.ClientID == id);

                client.IsDeleted = true;

                context.SaveChanges();
            }
        }
Esempio n. 10
0
        //add new inventory item
        public void SaveNewItem(InventoryItem item)
        {
            using (var context = new SingularityDBContext())
            {
                item.DatePurchased = DateTime.Now;  // Manipulating the item object is done before saving to Db

                context.InventoryItems.Add(item);

                context.SaveChanges();
            }
        }
Esempio n. 11
0
        //edit existing item>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        public void EditExistingItem(InventoryItem item)
        {
            using (var context = new SingularityDBContext())
            {
                context.InventoryItems.Attach(item);

                var entry = context.Entry(item);

                entry.State = EntityState.Modified;

                context.SaveChanges();
            }
        }
Esempio n. 12
0
        private void MarkInventoryItemsAsAvailable(SingularityDBContext context, IEnumerable <int> itemIds)
        {
            foreach (var itemId in itemIds)
            {
                var item = context.InventoryItems.FirstOrDefault(
                    i => i.InventoryItemId == itemId);

                if (item != null)
                {
                    item.Availability = true;
                }

                context.SaveChanges();
            }
        }
Esempio n. 13
0
        //Deletes Item - used in Update Inventory Items form
        public void DeleteItem(int id)
        {
            using (var context = new SingularityDBContext())
            {
                var item = context.InventoryItems.FirstOrDefault(x => x.InventoryItemId == id);

                if (item != null)
                {
                    item.IsDeleted = true;

                    context.InventoryItems.Attach(item);
                    var entry = context.Entry(item);
                    entry.State = EntityState.Modified;
                    context.SaveChanges();
                }
            }
        }
Esempio n. 14
0
        public void DeleteLoanByLoanNumber(string loanNumber)
        {
            using (var context = new SingularityDBContext())
            {
                var loanToDelete = context.LoanMasters.FirstOrDefault(
                    loanMaster =>
                    string.Equals(loanMaster.LoanNumber, loanNumber.ToString())
                    );

                if (loanToDelete != null)
                {
                    loanToDelete.IsDeleted = true;
                    loanToDelete.IsActive  = false;
                }
                context.SaveChanges();

                var itemIds = GetInventoryItemIdsByLoanNumber(loanNumber);

                MarkInventoryItemsAsAvailable(context, itemIds);
            }
        }
Esempio n. 15
0
        public void CreateLoan(LoanSubmission loanSubmission)
        {
            //have client id
            using (var context = new SingularityDBContext())
            {
                var loanNumIncrement = LoanIncrement();

                var newLoan = new LoanMaster
                {
                    ClientId    = loanSubmission.ClientId,
                    DateCreated = DateTime.Now,    //can go into the constructor of LoanMaster  -- haven't done this yet
                    //IsActive = loanSubmission.IsActive //can probably be defaulted
                    LoanNumber = loanNumIncrement, //we can create a utility class that auto increments this - see above
                    IsActive   = true
                };

                context.LoanMasters.Add(newLoan);
                context.SaveChanges(); //this line actually writes the record to the db

                //after writing the record the newLoan object will have a populated Id that matches the db
                //in this way we can add LoanDetails that reference the correct LoanMaster

                //have item Ids

                //we can map over the itemIds enumerable and map a new list of LoanDetails -- functional!

                //LINQ query syntax


                //Create new LoanDetail for each item in InventoryItemIds list
                IEnumerable <LoanDetail> query = from itemId in loanSubmission.InventoryItemIds
                                                 select new LoanDetail
                {
                    InventoryItemId = itemId,
                    LoanMasterId    = newLoan.LoanMasterId,
                    Purpose         = loanSubmission.Purpose,
                    PurposeType     = loanSubmission.PurposeType
                };

                //LINQ method syntax - would be same outcome as above query
                //IEnumerable<LoanDetail> methodQuery =
                //    loanSubmission.InventoryItemIds.Select(id => new LoanDetail
                //    {
                //        InventoryItemId = id,
                //        LoanMasterId = newLoan.LoanMasterId
                //    });

                //both are viable, sometimes query syntax feels more natural on joins and such
                //while method syntax can output some nice oneliners.
                //the point is that we use each item in a collection to create new LoanDetail
                //entities, mapping the properties we want over to the newly created object.

                //regardless, these queries have only returned something respects IEnumerable contract
                //we need concrete objects

                List <LoanDetail> loanDetailsList = query.ToList();

                //now we can add a range of loan details to the loan details table
                context.LoanDetails.AddRange(loanDetailsList);
                context.SaveChanges();


                ////Update Inventory Items' Availability
                var itemIds = GetInventoryItemIdsByLoanNumber(newLoan.LoanNumber);
                MarkInventoryItemsAsNotAvailable(context, itemIds);
                //or does this need to be like context.LoanDetails.AddRange(loanDetailsList);
            }
        }
Esempio n. 16
0
        public int RemoveSingleItemFromLoanByLoanNumber(int invItem)
        {
            int loanIdpassed = 0;

            using (var context = new SingularityDBContext())
            {
                var loanNumber = GetLoanNumberByInventoryItemId(invItem);
                var itemIds    = GetInventoryItemIdsByLoanNumber(loanNumber.FirstOrDefault());
                var loanId     = (from ld in context.LoanDetails
                                  join lm in context.LoanMasters
                                  on ld.LoanMasterId equals lm.LoanMasterId
                                  where lm.IsActive == true && (ld.InventoryItemId == invItem)
                                  select ld).FirstOrDefault(); //where (ld.InventoryItemId == invItem) &&

                //if there is more than one item in the loan, check in
                if (itemIds.Count() > 1)
                {
                    var item = context.InventoryItems.FirstOrDefault(
                        i => i.InventoryItemId == invItem);

                    if (item != null)
                    {
                        item.Availability = true;
                    }
                    context.SaveChanges();

                    //remove Loan Detail with item, from Loan
                    context.LoanDetails.Remove(loanId);
                    context.SaveChanges();

                    loanIdpassed = loanId.LoanMasterId;
                }
                else   //if there's only one item in loan, and it's about to be deleted
                {
                    //CheckInLoan_Nick(loan); - no because this is a delete when a mistake has been made. Item may never have left the building.

                    //remove the loan detail with that item from the loan
                    var loanId2 = (from ld in context.LoanDetails
                                   join lm in context.LoanMasters
                                   on ld.LoanMasterId equals lm.LoanMasterId
                                   where lm.IsActive == true && (ld.InventoryItemId == invItem)
                                   select ld).FirstOrDefault();

                    context.LoanDetails.Remove(loanId2);
                    context.SaveChanges();

                    //update Loan information in LoanMaster
                    var query = (from lm in context.LoanMasters
                                 where lm.LoanMasterId == loanId.LoanMasterId
                                 select lm).FirstOrDefault();

                    if (query != null)
                    {
                        query.IsActive  = false;
                        query.IsDeleted = true;
                    }
                    context.SaveChanges();

                    //Update inventory item availability
                    var itemIds2 = GetInventoryItemIdsByLoanNumber(query.LoanNumber);
                    MarkInventoryItemsAsAvailable(context, itemIds2);

                    //pass value back for page load
                    loanIdpassed = loanId2.LoanMasterId;
                }
                return(loanIdpassed);
            }
        }