private bool InvoiceCompletion(List <JobLog> jobLogs)
        {
            int  trials        = 2;
            bool status        = true;
            var  invoiceDao    = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Invoice>();
            var  completionDao = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Completion>();
            var  attachmentDao = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Attachment>();
            var  deliveryDao   = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Delivery>();

            var customerDao = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Customer>();

            var path = @"Data/CompletionStorage/";


            var invoices = invoiceDao.GetCollection(t => !t.IsCompleted, false);

            foreach (var invoice in invoices)
            {
                try
                {
                    var serverFilename      = Guid.NewGuid().ToString();
                    var attachmentCompletes = new List <AttachmentComplete>();

                    var attachments = attachmentDao.GetCollection(t => t.OrderNumber.Equals(invoice.OrderNumber));
                    if (attachments == null || !attachments.Any())
                    {
                        ++invoice.CompletionCounter;
                        if (invoice.CompletionCounter < trials)
                        {
                            continue;
                        }
                    }
                    var invoiceIncluded         = invoiceDao.GetSingle(t => t.Id.Equals(invoice.Id));
                    var invoicePdfDocumentArray = GetInvoicePdfDocument(invoiceIncluded);
                    var attachmentFiles         = GetAttachmentsFile(attachments);

                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    CompletionPdf(invoicePdfDocumentArray, attachmentFiles, $"{path}{serverFilename}");

                    if (attachmentFiles != null && attachmentFiles.Any())
                    {
                        attachmentCompletes.AddRange(attachments.Select(t => new AttachmentComplete()
                        {
                            AttachmentId = t.Id
                        }));
                    }

                    var completion = new Completion()
                    {
                        CustomerId          = invoice.CustomerId,
                        InvoiceId           = invoice.Id,
                        OrderId             = invoice.OrderId,
                        OrderNumber         = invoice.OrderNumber,
                        InvoiceNumber       = invoice.InvoiceNumber,
                        InvoiceDate         = invoice.InvoiceDate,
                        CompleteFileName    = $"{invoice.InvoiceNumber}.pdf",
                        ServerFileName      = serverFilename,
                        AttachmentCompletes = attachmentCompletes
                    };
                    completionDao.AddItem(completion);
                    invoice.IsCompleted = true;

                    foreach (var attachment in attachments)
                    {
                        attachment.IsCompleted = true;
                    }

                    var deliveries = deliveryDao.GetCollection(t => t.OrderNumber.Equals(invoice.OrderNumber));
                    foreach (var delivery in deliveries)
                    {
                        delivery.IsCompleted = true;
                    }

                    attachmentDao.UpdateRangeItems(attachments);
                    deliveryDao.UpdateRangeItems(deliveries);

                    var customer = customerDao.GetSingle(t => t.Id.Equals(completion.CustomerId));
                    completion.Customer = customer;
                    _emailSendGridBlProvider.SendInvoiceCompleteNotification(completion);
                }
                catch (Exception e)
                {
                    status = false;
                    jobLogs.Add(GetLogException(invoice.InvoiceNumber, e));
                }
            }

            try
            {
                invoiceDao.UpdateRangeItems(invoices);
            }
            catch (Exception e)
            {
                status = false;
                jobLogs.Add(GetLogException("InvoiceCompleteUpdateRange", e));
            }

            return(status);
        }
        private bool ProcessAttachmentScans(string filePath, List <JobLog> jobLogs)
        {
            var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

            var fileName = Path.GetFileNameWithoutExtension(filePath);

            var fileNameWithExtension = Path.GetFileName(filePath);

            var deliveryNumberString = string.Empty;

            var status = false;

            try
            {
                var deliveryNumbers = _attachmentScanHelper.GetBarcodeFromStream(stream);
                var deliveryDao     = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Delivery>();
                foreach (var deliveryNumber in deliveryNumbers)
                {
                    var delivery = deliveryDao.GetSingle(t => t.DeliveryNumber.Equals(deliveryNumber));
                    if (delivery != null)
                    {
                        deliveryNumberString = delivery.DeliveryNumber;
                        var pdfStream = _attachmentScanHelper.ConvertImageToPdf(stream);
                        var entity    = new Attachment()
                        {
                            OrderNumber    = delivery.OrderNumber,
                            DeliveryId     = delivery.Id,
                            DeliveryNumber = delivery.DeliveryNumber,
                            FileType       = "pdf",
                            FileName       = $"{fileName}.pdf"
                        };

                        UploadAttachment(pdfStream, entity);
                        status = true;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                jobLogs.Add(GetLogException(fileNameWithExtension, e));
                stream.Dispose();
            }
            finally
            {
                stream.Dispose();
                if (status)
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(@filePath);
                    }
                    jobLogs.Add(GetLogException(fileNameWithExtension, new Exception($"Add as attachment for delivery: {deliveryNumberString}")));
                }
                else
                {
                    jobLogs.Add(GetLogException(fileNameWithExtension, new Exception("Barcode not exist or not match to any delivery number.")));
                }
            }

            return(status);
        }
        private bool GetDataFromXml(string fileName, List <JobLog> jobLogs)
        {
            var invoiceDao  = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Invoice>();
            var customerDao = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Customer>();
            var orderDao    = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Order>();
            var deliveryDao = new BaseDaoFactory(new EfzDbContextFactory().Create()).GetDao <Delivery>();

            var status = false;

            var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            try
            {
                var serializer = new XmlSerializer(typeof(XmlModel.XmlModel));

                serializer.UnknownNode      += new XmlNodeEventHandler(serializer_UnknownNode);
                serializer.UnknownAttribute += new
                                               XmlAttributeEventHandler(serializer_UnknownAttribute);

                var xml = (XmlModel.XmlModel)serializer.Deserialize(stream);


                if (xml.Invoices != null)
                {
                    foreach (var invoice in xml.Invoices)
                    {
                        if (invoiceDao.GetSingle(t => t.OrderNumber.Equals(invoice.OrderNumber)) != null)
                        {
                            jobLogs.Add(GetLogException(fileName, new Exception($"Invoice with order number:{invoice.OrderNumber} existing.")));
                            continue;
                        }
                        var orderId = orderDao.GetSingle(t => t.OrderNumber.Equals(invoice.OrderNumber), false)?.Id;
                        invoice.OrderId      = orderId;
                        invoice.InvoiceItems = invoice.XmlInvoiceItems?.ToList();
                        invoiceDao.AddItem(invoice);
                    }
                }

                if (xml.Orders != null)
                {
                    foreach (var order in xml.Orders)
                    {
                        if (orderDao.GetSingle(t => t.OrderNumber.Equals(order.OrderNumber)) != null)
                        {
                            jobLogs.Add(GetLogException(fileName, new Exception($"Order with order number:{order.OrderNumber} existing.")));
                            continue;
                        }
                        order.OrderItems = order.XmlOrderItems?.ToList();
                        var entity  = orderDao.AddItem(order);
                        var invoice = invoiceDao.GetSingle(t => t.OrderNumber.Equals(entity.OrderNumber), false);
                        if (invoice.OrderId != entity.Id)
                        {
                            invoice.OrderId = entity.Id;
                            invoiceDao.UpdateItem(invoice);
                        }

                        var delivery = deliveryDao.GetSingle(t => t.OrderNumber.Equals(entity.OrderNumber), false);
                        if (delivery.OrderId != entity.Id)
                        {
                            delivery.OrderId = entity.Id;
                            deliveryDao.UpdateItem(delivery);
                        }
                    }
                }

                if (xml.Deliveries != null)
                {
                    foreach (var delivery in xml.Deliveries)
                    {
                        if (deliveryDao.GetSingle(t => t.OrderNumber.Equals(delivery.OrderNumber)) != null)
                        {
                            jobLogs.Add(GetLogException(fileName, new Exception($"Delivery with order number:{delivery.OrderNumber} existing.")));
                            continue;
                        }
                        var orderId = orderDao.GetSingle(t => t.OrderNumber.Equals(delivery.OrderNumber), false)?.Id;
                        delivery.OrderId = orderId;
                        deliveryDao.AddItem(delivery);
                    }
                }

                status = true;
                stream.Dispose();
            }
            catch (Exception e)
            {
                jobLogs.Add(GetLogException(fileName, e));
                stream.Dispose();
            }
            finally
            {
                stream.Dispose();
                if (status)
                {
                    if (File.Exists(fileName))
                    {
                        File.Delete(@fileName);
                    }
                }
            }

            return(status);
        }