private PAP.GenericResponse CreatePackListsFromXml(IEnumerable <PackListReadInformation> listcollection)
        {
            var response = new PAP.GenericResponse();
            var userId   = Guid.Parse("9BB44614-26C8-4761-BD54-2204E0E76C2D");

            using (var context = new Entities())
            {
                foreach (var pl in listcollection)
                {
                    var isTransfer =
                        context.TransferSentHeads.FirstOrDefault(trans => trans.Id == Guid.Parse(pl.ContractId)) != null;
                    var plist = new PackListInformation
                    {
                        Id                 = Guid.Parse(pl.PackListId),
                        ContractHeadId     = isTransfer ? Guid.Empty : Guid.Parse(pl.ContractId),
                        TransferSentHeadId = isTransfer ? Guid.Parse(pl.ContractId) : Guid.Empty,
                        CreatedByUserId    = userId,
                        PackListNumber     = Convert.ToInt32(pl.PackListNumber),
                        DateCreated        = DateTime.Now,
                        SealNumber         = string.Empty,
                        Units              = 0,
                        PackListStatus     = (int)PackListStatus.Held,
                    };
                    var pkresponse = isTransfer ? UpdateTransferPackList(plist) : UpdatePackList(plist);
                    if (!pkresponse.Success)
                    {
                        response.FailureInformation += pkresponse.FailureInformation;
                    }
                }
            }

            return(response);
        }
        private PAP.GenericResponse ReadPapXml()
        {
            try
            {
                var response        = new PAP.GenericResponse();
                var xmlDoc          = XDocument.Load(@"Files\ReceivePAP.xml");
                var packxmldata     = new Packs();
                var packlistxmldata = new PackLists();
                var settings        = new XmlReaderSettings {
                    IgnoreWhitespace = true
                };
                var buffer    = Encoding.ASCII.GetBytes(xmlDoc.ToString());
                var xmlStream = new MemoryStream(buffer);
                using (var xmlReader = XmlReader.Create(xmlStream, settings))
                {
                    var packXmlSerializer     = new XmlSerializer(packxmldata.GetType());
                    var packlistXmlSerializer = new XmlSerializer(packlistxmldata.GetType());
                    packxmldata                  = (Packs)packXmlSerializer.Deserialize(xmlReader);
                    packlistxmldata              = (PackLists)packlistXmlSerializer.Deserialize(xmlReader);
                    response.FailureInformation  = CreatePacksFromXml(packxmldata.PackCollection).FailureInformation;
                    response.FailureInformation += CreatePackListsFromXml(packlistxmldata.PackListCollection);
                }

                return(response);
            }
            catch (Exception ex)
            {
                LoggingMediator.Log("ReadPapXml");
                LoggingMediator.Log(ex);
                throw;
            }
        }
        private static PAP.GenericResponse UpdatePack(PackInformation pkinfo)
        {
            try
            {
                var response = new PAP.GenericResponse();
                using (var context = new Entities())
                {
                    var yd = (from yard in context.Yards
                        where yard.Id == pkinfo.YardId
                        select yard.CustomerNumberPrefix).First() ?? string.Empty;
                    pkinfo.NumberPrefix = yd;
                    pkinfo.InternalPackNumber = yd.Trim() + pkinfo.TagNumber;

                    var pack = (from pk in context.Packs
                        where pk.Id == pkinfo.Id
                        select pk).FirstOrDefault();
                    if (pack == null)
                    {
                        var pk = new Pack();
                        Mapper.Map(pkinfo, pk);
                        context.Packs.AddObject(pk);
                    }
                    else Mapper.Map(pkinfo, pack);
                    context.SaveChanges();
                }
                response.Success = true;
                return response;
            }
            catch (Exception ex)
            {
                LoggingMediator.Log("UpdatePack");
                LoggingMediator.Log(ex);
                return new PAP.GenericResponse {Success = false, FailureInformation = "Error in PAPService:UpdatePack"};
            }
        }
        private PAP.GenericResponse WritePapXml()
        {
            try
            {
                var response = new PAP.GenericResponse();
                using (var context = new Entities())
                {
                    var availablePacks              = GetPacksByYard(context);
                    var availablePackLists          = GetPackListsByYard(context);
                    var availableTransfersPackLists = GetTransferPackListsByYard(context);
                    var availableContracts          = GetOpenContractsByYard(context);
                    var availableTransfers          = GetHeldTransferSentHeadByYard(context);
                    var availableInventory          = GetAvailableInventoryByYard(context);

                    var allAvailableContracts = availableContracts.Concat(availableTransfers).ToArray();
                    var allAvailablePacklists = availablePackLists.Concat(availableTransfersPackLists).ToArray();

                    var sendData = new Root
                    {
                        AvailableContracts = allAvailableContracts,
                        InventoryItems     = availableInventory,
                        PackLists          = allAvailablePacklists,
                        AvailablePacks     = availablePacks
                    };
                    var writer        = new XmlSerializer(typeof(Root));
                    var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                    var fullFileName  = Path.Combine(desktopFolder, "SendPAP.xml");
                    using (var fs = new FileStream(fullFileName, FileMode.Create))
                    {
                        writer.Serialize(fs, sendData);
                        fs.Close();
                    }
                }
                response.Success = true;
                return(response);
            }
            catch (Exception ex)
            {
                LoggingMediator.Log("ReadPapXml");
                LoggingMediator.Log(ex);
                throw;
            }
        }
        private static PAP.GenericResponse UpdatePack(PackInformation pkinfo)
        {
            try
            {
                var response = new PAP.GenericResponse();
                using (var context = new Entities())
                {
                    var yd = (from yard in context.Yards
                              where yard.Id == pkinfo.YardId
                              select yard.CustomerNumberPrefix).First() ?? string.Empty;
                    pkinfo.NumberPrefix       = yd;
                    pkinfo.InternalPackNumber = yd.Trim() + pkinfo.TagNumber;

                    var pack = (from pk in context.Packs
                                where pk.Id == pkinfo.Id
                                select pk).FirstOrDefault();
                    if (pack == null)
                    {
                        var pk = new Pack();
                        Mapper.Map(pkinfo, pk);
                        context.Packs.AddObject(pk);
                    }
                    else
                    {
                        Mapper.Map(pkinfo, pack);
                    }
                    context.SaveChanges();
                }
                response.Success = true;
                return(response);
            }
            catch (Exception ex)
            {
                LoggingMediator.Log("UpdatePack");
                LoggingMediator.Log(ex);
                return(new PAP.GenericResponse {
                    Success = false, FailureInformation = "Error in PAPService:UpdatePack"
                });
            }
        }
        private PAP.GenericResponse CreatePacksFromXml(IEnumerable <PackReadInformation> packcollection)
        {
            var response = new PAP.GenericResponse();
            var userId   = Guid.Parse("9BB44614-26C8-4761-BD54-2204E0E76C2D");

            foreach (var pk in packcollection)
            {
                var pack = new PackInformation
                {
                    Id                 = Guid.Parse(pk.Id),
                    GrossWeight        = Convert.ToDecimal(pk.GrossWeight),
                    TareWeight         = Convert.ToDecimal(pk.TareWeight),
                    ScaleGrossWeight   = Convert.ToDecimal(pk.ScaleGrossWeight),
                    ScaleTareWeight    = Convert.ToDecimal(pk.ScaleTareWeight),
                    TagNumber          = Convert.ToInt32(pk.TagNumber),
                    PrintDescription   = pk.PrintDescription,
                    UnitOfMeasure      = pk.UnitOfMeasure,
                    DateCreated        = Convert.ToDateTime(pk.DateCreated),
                    DateClosed         = Convert.ToDateTime(pk.DateClosed),
                    Quantity           = Convert.ToInt16(pk.Quantity),
                    CommodityType      = Convert.ToInt16(pk.CommodityType),
                    PackStatus         = Convert.ToInt16(pk.PackStatus),
                    InventoryId        = Guid.Parse(pk.InventoryId),
                    YardId             = YardId,
                    NumberPrefix       = string.Empty,
                    InternalPackNumber = string.Empty,
                    Cost               = Convert.ToDecimal(pk.Cost),
                    CreatedByUserId    = userId,
                };
                var pkresponse = UpdatePack(pack);
                if (!pkresponse.Success)
                {
                    response.FailureInformation += pkresponse.FailureInformation;
                }
            }
            return(response);
        }
 private static PAP.GenericResponse UpdateTransferPackList(PackListInformation listinfo)
 {
     try
     {
         var response = new PAP.GenericResponse();
         using (var context = new Entities())
         {
             var plist = (from list in context.TransferPackListHeads
                          where list.Id == listinfo.Id
                          select list).FirstOrDefault();
             if (plist == null)
             {
                 var list = new TransferPackListHead();
                 Mapper.Map(listinfo, list);
                 context.TransferPackListHeads.AddObject(list);
             }
             else
             {
                 Mapper.Map(listinfo, plist);
             }
             context.SaveChanges();
         }
         response.Success = true;
         return(response);
     }
     catch (Exception ex)
     {
         LoggingMediator.Log("UpdatePack");
         LoggingMediator.Log(ex);
         return(new PAP.GenericResponse
         {
             Success = false,
             FailureInformation = "Error in PAPService:UpdateTransferPackList"
         });
     }
 }
        private PAP.GenericResponse WritePapXml()
        {
            try
            {
                var response = new PAP.GenericResponse();
                using (var context = new Entities())
                {
                    var availablePacks = GetPacksByYard(context);
                    var availablePackLists = GetPackListsByYard(context);
                    var availableTransfersPackLists = GetTransferPackListsByYard(context);
                    var availableContracts = GetOpenContractsByYard(context);
                    var availableTransfers = GetHeldTransferSentHeadByYard(context);
                    var availableInventory = GetAvailableInventoryByYard(context);

                    var allAvailableContracts = availableContracts.Concat(availableTransfers).ToArray();
                    var allAvailablePacklists = availablePackLists.Concat(availableTransfersPackLists).ToArray();

                    var sendData = new Root
                    {
                        AvailableContracts = allAvailableContracts,
                        InventoryItems = availableInventory,
                        PackLists = allAvailablePacklists,
                        AvailablePacks = availablePacks
                    };
                    var writer = new XmlSerializer(typeof (Root));
                    var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                    var fullFileName = Path.Combine(desktopFolder, "SendPAP.xml");
                    using (var fs = new FileStream(fullFileName, FileMode.Create))
                    {
                        writer.Serialize(fs, sendData);
                        fs.Close();
                    }

                }
                response.Success = true;
                return response;
            }
            catch (Exception ex)
            {
                LoggingMediator.Log("ReadPapXml");
                LoggingMediator.Log(ex);
                throw;
            }
        }
        private PAP.GenericResponse CreatePackListsFromXml(IEnumerable<PackListReadInformation> listcollection)
        {
            var response = new PAP.GenericResponse();
            var userId = Guid.Parse("9BB44614-26C8-4761-BD54-2204E0E76C2D");
            using (var context = new Entities())
            {
                foreach (var pl in listcollection)
                {
                    var isTransfer =
                        context.TransferSentHeads.FirstOrDefault(trans => trans.Id == Guid.Parse(pl.ContractId)) != null;
                    var plist = new PackListInformation
                    {
                        Id = Guid.Parse(pl.PackListId),
                        ContractHeadId = isTransfer ? Guid.Empty : Guid.Parse(pl.ContractId),
                        TransferSentHeadId = isTransfer ? Guid.Parse(pl.ContractId) : Guid.Empty,
                        CreatedByUserId = userId,
                        PackListNumber = Convert.ToInt32(pl.PackListNumber),
                        DateCreated = DateTime.Now,
                        SealNumber = string.Empty,
                        Units = 0,
                        PackListStatus = (int) PackListStatus.Held,
                    };
                    var pkresponse = isTransfer ? UpdateTransferPackList(plist) : UpdatePackList(plist);
                    if (!pkresponse.Success) response.FailureInformation += pkresponse.FailureInformation;
                }
            }

            return response;
        }
 private PAP.GenericResponse CreatePacksFromXml(IEnumerable<PackReadInformation> packcollection)
 {
     var response = new PAP.GenericResponse();
     var userId = Guid.Parse("9BB44614-26C8-4761-BD54-2204E0E76C2D");
     foreach (var pk in packcollection)
     {
         var pack = new PackInformation
         {
             Id = Guid.Parse(pk.Id),
             GrossWeight = Convert.ToDecimal(pk.GrossWeight),
             TareWeight = Convert.ToDecimal(pk.TareWeight),
             ScaleGrossWeight = Convert.ToDecimal(pk.ScaleGrossWeight),
             ScaleTareWeight = Convert.ToDecimal(pk.ScaleTareWeight),
             TagNumber = Convert.ToInt32(pk.TagNumber),
             PrintDescription = pk.PrintDescription,
             UnitOfMeasure = pk.UnitOfMeasure,
             DateCreated = Convert.ToDateTime(pk.DateCreated),
             DateClosed = Convert.ToDateTime(pk.DateClosed),
             Quantity = Convert.ToInt16(pk.Quantity),
             CommodityType = Convert.ToInt16(pk.CommodityType),
             PackStatus = Convert.ToInt16(pk.PackStatus),
             InventoryId = Guid.Parse(pk.InventoryId),
             YardId = YardId,
             NumberPrefix = string.Empty,
             InternalPackNumber = string.Empty,
             Cost = Convert.ToDecimal(pk.Cost),
             CreatedByUserId = userId,
         };
         var pkresponse = UpdatePack(pack);
         if (!pkresponse.Success) response.FailureInformation += pkresponse.FailureInformation;
     }
     return response;
 }
        private PAP.GenericResponse ReadPapXml()
        {
            try
            {
                var response = new PAP.GenericResponse();
                var xmlDoc = XDocument.Load(@"Files\ReceivePAP.xml");
                var packxmldata = new Packs();
                var packlistxmldata = new PackLists();
                var settings = new XmlReaderSettings {IgnoreWhitespace = true};
                var buffer = Encoding.ASCII.GetBytes(xmlDoc.ToString());
                var xmlStream = new MemoryStream(buffer);
                using (var xmlReader = XmlReader.Create(xmlStream, settings))
                {
                    var packXmlSerializer = new XmlSerializer(packxmldata.GetType());
                    var packlistXmlSerializer = new XmlSerializer(packlistxmldata.GetType());
                    packxmldata = (Packs) packXmlSerializer.Deserialize(xmlReader);
                    packlistxmldata = (PackLists) packlistXmlSerializer.Deserialize(xmlReader);
                    response.FailureInformation = CreatePacksFromXml(packxmldata.PackCollection).FailureInformation;
                    response.FailureInformation += CreatePackListsFromXml(packlistxmldata.PackListCollection);
                }

                return response;
            }
            catch (Exception ex)
            {
                LoggingMediator.Log("ReadPapXml");
                LoggingMediator.Log(ex);
                throw;
            }

        }
 private static PAP.GenericResponse UpdateTransferPackList(PackListInformation listinfo)
 {
     try
     {
         var response = new PAP.GenericResponse();
         using (var context = new Entities())
         {
             var plist = (from list in context.TransferPackListHeads
                 where list.Id == listinfo.Id
                 select list).FirstOrDefault();
             if (plist == null)
             {
                 var list = new TransferPackListHead();
                 Mapper.Map(listinfo, list);
                 context.TransferPackListHeads.AddObject(list);
             }
             else Mapper.Map(listinfo, plist);
             context.SaveChanges();
         }
         response.Success = true;
         return response;
     }
     catch (Exception ex)
     {
         LoggingMediator.Log("UpdatePack");
         LoggingMediator.Log(ex);
         return new PAP.GenericResponse
         {
             Success = false,
             FailureInformation = "Error in PAPService:UpdateTransferPackList"
         };
     }
 }