コード例 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Take Thai print server.");

            while (true != false)
            {
                try
                {
                    //Console.WriteLine("Currently getting all orders.");
                    var todaysOrders = GetAllTodaysOrders();

                    var convertJsonToModels = new ConvertJsonToModels();
                    var orderModels         = convertJsonToModels.ConvertJsonToOrderModelsList(todaysOrders);
                    if (orderModels != null && orderModels.Count > 0)
                    {
                        //Console.WriteLine("Printing orders");

                        var pdfClass = new PDFClass();

                        foreach (var order in orderModels)
                        {
                            using (var context = new SQLiteDBContext())
                            {
                                //Console.WriteLine("All Tables");
                                //using (var command = context.Database.GetDbConnection().CreateCommand())
                                //{
                                //    command.CommandText = "SELECT name from sqlite_master WHERE type='table'";
                                //    context.Database.OpenConnection();
                                //    using (var result = command.ExecuteReader())
                                //    {
                                //        while (result.Read())
                                //        {
                                //            Console.WriteLine(result.GetString(0));
                                //        }
                                //    }
                                //}

                                //Console.WriteLine("Order stuff");
                                var currentOrder = context.PrintOrderModel.FirstOrDefaultAsync(x => x.OrderId == order.Id).Result;

                                if (currentOrder != null)
                                {
                                    //Console.WriteLine("Checking current order");
                                    if ((currentOrder.IsPrinted || (!currentOrder.IsComplete && order.PaymentMethod == null)) && currentOrder.OrderPlacedDate == order.OrderPlacedDate)
                                    {
                                        //Console.WriteLine($"Order ID: {currentOrder.Id} does not need to be printed");
                                        continue;
                                    }

                                    if (currentOrder.IsPrinted)
                                    {
                                        currentOrder.IsPrinted = false;
                                        context.Update(currentOrder);
                                        context.SaveChanges();
                                    }
                                }
                                else
                                {
                                    //Console.WriteLine("Checking current order");
                                    if (order.PaymentMethod == null)
                                    {
                                        //Console.WriteLine($"Order ID: {currentOrder.Id} does not need to be printed");
                                        continue;
                                    }

                                    currentOrder = new PrintOrderModel
                                    {
                                        OrderId         = order.Id,
                                        OrderPlacedDate = order.OrderPlacedDate,
                                        VatReceipt      = order.VatReceipt
                                    };
                                }

                                //Console.WriteLine("Creating Kitchen Receipt");
                                // Generate format ready to print
                                var pdfFilePath = pdfClass.CreateKitchenPdf(order);

                                //Console.WriteLine("Send Kitchen Receipt to printer");
                                var print = SendRequestToPrinter(pdfFilePath);

                                if (order.DeliveryOrCollection == "Delivery")
                                {
                                    //Console.WriteLine("Creating Delivery Receipt");
                                    pdfFilePath = pdfClass.CreateDeliveryPdf(order);
                                    //Console.WriteLine("Send Kitchen Receipt to printer");
                                    print = SendRequestToPrinter(pdfFilePath);
                                }


                                if (order.VatReceipt)
                                {
                                    //Console.WriteLine("Creating VAT Receipt");
                                    pdfFilePath = pdfClass.CreateVatPdf(order);
                                    //Console.WriteLine("Send VAT Receipt to printer");
                                    print = SendRequestToPrinter(pdfFilePath);
                                }

                                if (print)
                                {
                                    // Send request to confirm printed.
                                    currentOrder.IsPrinted  = true;
                                    currentOrder.IsComplete = true;
                                    context.Update(currentOrder);
                                    context.SaveChanges();
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine("An Error occurred printing order.");
                    Console.WriteLine(exception.Message);
                    Console.WriteLine(exception.StackTrace);
                    while (exception.InnerException != null)
                    {
                        exception = exception.InnerException;
                        Console.WriteLine(exception.StackTrace);
                    }
                }

                Thread.Sleep(3000);
            }
        }