public void Save(EventCustomersFindingEditModel model)
        {
            using (var adapter = PersistenceLayer.GetDataAccessAdapter())
            {
                adapter.DeleteEntitiesDirectly(typeof(CustomerEventTestPhysicianNoteEntity),
                                               new RelationPredicateBucket(CustomerEventTestPhysicianNoteFields.EventId ==
                                                                           model.EventId));

                adapter.DeleteEntitiesDirectly(typeof(CustomerEventTestFindingEntity),
                                               new RelationPredicateBucket(CustomerEventTestFindingFields.EventId ==
                                                                           model.EventId));

                var entities = new EntityCollection <CustomerEventTestFindingEntity>();
                var physicianNotesEntities = new EntityCollection <CustomerEventTestPhysicianNoteEntity>();
                foreach (var customer in model.Customers)
                {
                    if (!string.IsNullOrEmpty(customer.Notes))
                    {
                        physicianNotesEntities.Add(new CustomerEventTestPhysicianNoteEntity(model.EventId,
                                                                                            customer.CustomerId)
                        {
                            PhysicianNotes = customer.Notes
                        });
                    }

                    foreach (var test in customer.Tests)
                    {
                        entities.Add(new CustomerEventTestFindingEntity(model.EventId, customer.CustomerId,
                                                                        (long)test.TestType)
                        {
                            Finding = (int)test.Finding
                        });
                    }
                }

                using (var scope = new TransactionScope())
                {
                    if (entities.Count > 0)
                    {
                        if (adapter.SaveEntityCollection(entities) == 0)
                        {
                            throw new PersistenceFailureException();
                        }
                    }

                    if (physicianNotesEntities.Count > 0)
                    {
                        if (adapter.SaveEntityCollection(physicianNotesEntities) == 0)
                        {
                            throw new PersistenceFailureException();
                        }
                    }

                    scope.Complete();
                }
            }
        }
        public EventCustomersFindingEditModel Create(Event selectedEvent, IEnumerable <CustomerItemizedReceiptModel> reciepts, IEnumerable <EventTest> eventTests,
                                                     IEnumerable <CustomerEventTestFindingEntity> entities, IEnumerable <CustomerEventTestPhysicianNoteEntity> physicianNoteEntities, IEnumerable <Customer> customers, IEnumerable <Pod> pod)
        {
            var model = new EventCustomersFindingEditModel
            {
                EventDate                        = selectedEvent.EventDate,
                EventId                          = selectedEvent.Id,
                EventTests                       = eventTests != null?eventTests.Select(et => et.Test).ToArray() : null,
                                            Host = reciepts != null && reciepts.Count() > 0 ? reciepts.First().Host : string.Empty,
                                            Pod  = string.Join(",", pod.Select(pn => pn.Name))
            };

            var customerFindingEditModels = reciepts.Select(reciept =>
            {
                var customer = customers.Where(c => c.CustomerId == reciept.CustomerId).FirstOrDefault();

                var physicianNote =
                    physicianNoteEntities.Where(pe => pe.EventId == reciept.EventId && pe.CustomerId == reciept.CustomerId).FirstOrDefault();

                string phone = customer.HomePhoneNumber != null
                                                                                       ? customer.HomePhoneNumber.
                               ToString()
                                                                                       : (customer.OfficePhoneNumber !=
                                                                                          null
                                                                                              ? customer.
                                                                                          OfficePhoneNumber.
                                                                                          ToString()
                                                                                              : (customer.
                                                                                                 MobilePhoneNumber !=
                                                                                                 null
                                                                                                     ? customer.
                                                                                                 MobilePhoneNumber
                                                                                                 .ToString()
                                                                                                     : string.Empty));

                return(new CustomerTestFindingEditModel
                {
                    CustomerId = reciept.CustomerId,
                    CustomerName = reciept.CustomerName,
                    Phone = phone,
                    Notes = physicianNote != null ? physicianNote.PhysicianNotes : string.Empty,
                    Tests = reciept.Items.Where(i => i.ItemType == OrderItemType.EventPackageItem || i.ItemType == OrderItemType.EventTestItem)
                            .Select(i => new TestFindingEditModel()
                    {
                        Finding = GetifCustomerFindingforTestisAbnormal(selectedEvent.Id, reciept.CustomerId, i.ItemId, entities),
                        TestType = (TestType)i.ItemId
                    }).ToArray()
                });
            }).ToList();


            model.Customers = customerFindingEditModels.OrderBy(cf => cf.CustomerName);
            return(model);
        }