Esempio n. 1
0
        /// <summary>
        /// Used to send invoices
        /// </summary>
        static bool SendInvoices()
        {
            Queue <int> queue = reception.GetNewlyCreatedOrders();

            if (queue.Count == 0)
            {
                return(false);
            }

            // Opens word
            if (!OpenWord())
            {
                return(false);
            }

            LabelInfo();
            Console.WriteLine("Læser fra invoice-queue.dat...");

            Queue <int>        failed       = new Queue <int>();
            List <Reservation> reservations = reception.GetReservations();

            LabelInfo();
            if (queue.Count == 1)
            {
                Console.WriteLine(queue.Count + " kunde er ved at få sendt en fakture...");
            }
            else
            {
                Console.WriteLine(queue.Count + " kunder er ved at få sendt en fakture...");
            }

            int index   = 0;
            int invalid = 0;

            // Incement all ordernumbers
            foreach (int orderNumber in queue)
            {
                LabelInfo();
                Console.WriteLine("Mails sendt: " + index + " / " + (queue.Count - invalid));

                Reservation reservation = null;

                // Trys to find ordernumber in list
                for (int i = 0; i < reservations.Count; i++)
                {
                    if (reservations[i].OrderNumber == orderNumber)
                    {
                        reservation = reservations[i];
                    }
                }

                // If there wasn't found any ordernumber that match up give a warning and continue
                if (reservation == null)
                {
                    invalid++;
                    LabelWarning();
                    Console.WriteLine("Reservationen " + orderNumber + " findes ikke i databasen.");
                    continue;
                }

                // If there was found any ordernumber that match up send invoice to customer
                try
                {
                    reception.SendInvoice(reservation, path, templateFilename);
                    index++;

                    LabelSuccess();
                    Console.WriteLine(reservation.Customer.FirstName + " " + reservation.Customer.LastName + " har modtaget fakturaen via: " + reservation.Customer.Email);
                }
                // If something went wrong give a error
                catch (Exception exception)
                {
                    failed.Enqueue(reservation.OrderNumber);

                    LabelError();
                    Console.WriteLine("En kunde fik ikke sin fakture! - " + orderNumber + " - " + exception.Message);
                }
            }

            // Puts back failed ordernumbers to file
            if (index != queue.Count - invalid)
            {
                LabelWarning();
                Console.WriteLine("Skriver de fejlede mail adresser tilbage til køen...");

                foreach (int orderNumber in failed)
                {
                    reception.AddNewlyCreatedOrder(orderNumber);
                }

                LabelWarning();
            }
            else
            {
                LabelSuccess();
            }

            Console.WriteLine("Mails sendt: " + index + " / " + (queue.Count - invalid));

            // Close word
            reception.CloseWord();

            return(true);
        }