예제 #1
0
        /// <summary>
        /// Prints Ticket collection
        /// </summary>
        private void ProcessPrint(List <Spot> spots)
        {
            if (spots.Count == 0)
            {
                return;
            }

            Spot spotBlank = spots.FirstOrDefault(x => x.Blank == null);

            if (spotBlank != null)
            {
                StatusText = OperationResult = "Недостаточно бланков для печати. Печать отменена";
                return;
            }

            spots.Sort();

            _printProgress = new PrintProgress
            {
                Message = "Идет печать билетов..."
            };

            _printProgress.Cancel += CancelProcess;

            _worker = new BackgroundWorker {
                WorkerSupportsCancellation = true
            };
            Dispatcher dispatcher   = _progressDialog.Dispatcher;
            int        ticketsCount = spots.Count;
            int        printedCount = 0;

            _worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                decimal percent  = 100 / (ticketsCount);
                int     progress = 0;

                for (int i = 1; i <= ticketsCount; i++)
                {
                    if (_worker.CancellationPending)
                    {
                        args.Cancel = true;
                        return;
                    }

                    Spot spot = spots[i - 1];
                    if (spot.Status == ItemStatus.Sold)
                    {
                        string msg = string.Format("{0} из {1} - печать билета № {2} на бланке {3}", i, ticketsCount, spot.Id, spot.Blank.Key);
                        _updatePrintDelegate = UpdatePrintText;
                        dispatcher.BeginInvoke(_updatePrintDelegate, progress, msg);

                        byte[] bytes = GetTicketImage(Customer.Order.Id, spot.Id, 2).ToArray();
                        //Thread.Sleep(500);

                        //bool result = _printRequest.Print(bytes);
                        bool result = true;
                        if (!result)
                        {
                            PrinterStatus = string.Format("Ошибка печати билета № {0} ", spot.Id);
                            _worker.CancelAsync();
                        }
                        else
                        {
                            Ticket ticket = new Ticket
                            {
                                Id      = spot.Id,
                                BlankId = spot.Blank.Id,
                                Status  = ItemStatus.Printed
                            };
                            ticket = Access.SetTicketPrinted(ticket);
                            if (ticket.BlankId == spot.Blank.Id)
                            {
                                spot.Status = ItemStatus.Printed;
                            }
                            else
                            {
                                //t.Status = ItemStatus.Printed;
                            }
                        }
                    }

                    progress = (int)(progress + percent);
                    printedCount++;
                }
            };
            _worker.RunWorkerCompleted += delegate
            {
                _printProgress.Close();
                _worker = null;
            };
            _worker.RunWorkerAsync();
            _printProgress.ShowDialog();

            StatusText          =
                OperationResult =
                    String.Format("Печать завершена. Напечатано {0} из {1} билетов.", printedCount, ticketsCount);

            BaseData.ClearCache();
        }