コード例 #1
0
        public async Task Run()
        {
            // if cash drawer is connected to the printer, the drawer will open now.
            await this.client.OpenDrawerAsync(CancellationToken.None);

            // lets print some nonfiscal text with custom text formatting.
            await this.PrintNonfiscalText();

            // now lets print receipt (with no options specified).
            CashRegisterReceipt receipt = this.CreateReceipt();

            await this.PrintPaperReceipt(receipt);

            // previous call was made with default options - paper receipt.
            // if we want to specify, how receipt should be issued (as paper, as PDF file or via email) we can compose printing options.
            // lets see examples below:

            // paper receipt
            CashRegisterReceipt anotherPaperReceipt = this.CreateReceipt();

            await this.PrintPaperReceiptWithCustomOptions(anotherPaperReceipt);

            // PDF receipt
            CashRegisterReceipt pdfReceipt = this.CreateReceipt();

            await this.PrintPdfReceipt(pdfReceipt);

            // email receipt
            CashRegisterReceipt emailReceipt = this.CreateReceipt();

            await this.PrintEmailReceipt(emailReceipt);

            // location registration
            await this.LocationRegistration();
        }
コード例 #2
0
        private async Task PrintPaperReceipt(CashRegisterReceipt receipt)
        {
            // prepare request object
            RegisterCashRegisterReceiptRequest receiptRequest = new RegisterCashRegisterReceiptRequest(receipt);

            // and register receipt
            RegisterReceiptResult result = await client.RegisterReceiptAsync(receiptRequest, CancellationToken.None);
        }
コード例 #3
0
        private async Task PrintPdfReceipt(CashRegisterReceipt receipt)
        {
            // there are no options available for pdf printer.

            // prepare print context object without additional options.
            RegisterReceiptPrintContext printContext = RegisterReceiptPrintContext.CreatePdf();

            // wrap receipt to request
            RegisterCashRegisterReceiptRequest receiptRequest = new RegisterCashRegisterReceiptRequest(receipt);

            // and register receipt
            RegisterReceiptResult result = await client.RegisterReceiptAsync(receiptRequest, printContext, CancellationToken.None);

            // PDF file is created now.
            // by default, its location is : "C:/ProgramData/NineDigit/Portos.eKasa/receipts"

            this.HandleReceiptRegistrationResult(result);
        }
コード例 #4
0
        private async Task PrintEmailReceipt(CashRegisterReceipt receipt)
        {
            // create printing options object from dictionary.
            EmailPrintingOptions printOptions = new EmailPrintingOptions()
            {
                // required parameter - recipients email address
                // please change this to some real email address
                To = "*****@*****.**",
                // optional recipient display name
                RecipientDisplayName = "John Brown",
                // optional parameter. this will override the "configuration.Printers.Email.Subject" for this specific receipt.
                Subject = "Your receipt, mr. Brown!",
                // optional parameter. This will override the "configuration.Printers.Email.Body" for this specific receipt.
                Body = "Thank you for your purchase."
            };

            // prepare print context object from printing options
            RegisterReceiptPrintContext printContext = RegisterReceiptPrintContext.CreateEmail(printOptions);

            // wrap receipt to request
            RegisterCashRegisterReceiptRequest receiptRequest = new RegisterCashRegisterReceiptRequest(receipt);

            // and register receipt
            RegisterReceiptResult result;

            try
            {
                result = await client.RegisterReceiptAsync(receiptRequest, printContext, CancellationToken.None);
            }
            catch (Exception ex)
            {
                throw new Exception("please setup your email configuration (SMTP server, ...) before sending emails.", ex);
            }

            this.HandleReceiptRegistrationResult(result);
        }
コード例 #5
0
        private async Task PrintPaperReceiptWithCustomOptions(CashRegisterReceipt receipt)
        {
            // we can override default client configuration with these options:
            PosPrintingOptions printOptions = new PosPrintingOptions()
            {
                // this will override the "configuration.Printers.Pos.Drawer.Enabled" for this specific receipt.
                OpenDrawer = false,
                // this will override the "configuration.Printers.Pos.Logo.Enabled" for this specific receipt.
                PrintLogo = true,
                // this will override the "configuration.Printers.Pos.Logo.MemoryAddress" for this specific receipt.
                LogoMemoryAddress = 2
            };

            // prepare print context object from printer name and printing options
            RegisterReceiptPrintContext printContext = RegisterReceiptPrintContext.CreatePos(printOptions);

            // wrap receipt to request
            RegisterCashRegisterReceiptRequest receiptRequest = new RegisterCashRegisterReceiptRequest(receipt);

            // and register receipt
            RegisterReceiptResult result = await client.RegisterReceiptAsync(receiptRequest, printContext, CancellationToken.None);

            this.HandleReceiptRegistrationResult(result);
        }
コード例 #6
0
        private CashRegisterReceipt CreateReceipt()
        {
            // lets create receipt object - the single required parameter is the cash registe code.
            // all other parameters are optional.

            CashRegisterReceipt receipt = new CashRegisterReceipt(this.cashRegisterCode)
            {
                // Optional text, printed between informations about company and items
                HeaderText = "Web: www.ninedigit.sk",
                // Optional text, that is printed at the end of the receipt
                FooterText = "Ďakujeme za nákup.",
            };

            // lets add some items (products) to the receipt - as receipt must have at least one.

            // helper object to contains receipt item data.
            ReceiptItemData itemData = new ReceiptItemData()
            {
                Type      = ReceiptItemType.Positive,
                Name      = "Banány voľné",
                UnitPrice = 1.123456m,                  // unit price can be specified up to 6 decimal places
                Quantity  = new Quantity(0.123m, "kg"), // quantity can be specified up to 3 decimal places
                Price     = 0.14m,                      // price must be equal to unitPrice * quantity, and can be specified up to 2 decimal places. Mathematical rounding is applied.
                VatRate   = VatRate.Free
            };

            // this data object has its validator, so we can check, whether our application composes receipt item correctly.
            ValidationResult itemDataValidationResult = itemData.Validate();

            if (!itemDataValidationResult.IsValid)
            {
                // object is invalid - that means, we did something wrong!

                // to see more details, inspect the errors collection.
                IEnumerable <MemberValidationFailure> errors = itemDataValidationResult.Errors;

                // lets pick first error and throw an exception.
                MemberValidationFailure firstError = errors.First();

                string errorMessage = $"Invalid composition of ticket item. {firstError.MemberName}: {firstError.Message}";
                throw new InvalidOperationException(errorMessage);
            }

            // after validation succeeds, we can create receipt item
            ReceiptItem receiptItem = new ReceiptItem(itemData);

            // add item to receipt.
            receipt.Items.Add(receiptItem);

            // we don't need to specify payments. But if we do, their amounts must be equal or greater than total amount of receipt.

            // prepare empty payments collection
            receipt.Payments = new ReceiptPayments();

            // and add some payments
            ReceiptPayment payment1 = new ReceiptPayment("Hotovosť", 1m); // "Hotovosť" means "cash"

            receipt.Payments.Add(payment1);

            // cash to return can be stated as another payment, with negative amount.
            ReceiptPayment payment2 = new ReceiptPayment("Hotovosť", -0.86m);

            receipt.Payments.Add(payment2);

            return(receipt);
        }