star-micronics / react-native-star-io10

react-native-star-io10 is a library for supporting application development for Star Micronics devices.
Other
90 stars 55 forks source link

MC-Print3 takes about 15 seconds to print #27

Closed Brettm12345 closed 1 year ago

Brettm12345 commented 3 years ago

There is about a 15 second delay after sending the print command. Is there any way to fix this? Here is my code

  const printOrder = async ({
    id,
    price,
    items,
    dispensary,
    pickup_time_end,
    payment_method,
    pickup_time_start,
    created_at,
    user,
    guest_user,
  }: OrderTicketFragment) => {
    if (!printer) {
      toast.show({
        title: 'No Printer connected',
      });
      return;
    }
    setPrinting(true);
    toast.show({
      title: 'Printing',
    });
    const builder = new StarXpandCommand.StarXpandCommandBuilder();
    const customer = user ?? guest_user;
    const columns = 48;
    const hr = `${'-'.repeat(columns)}\n`;
    const subtotal = items.reduce(
      (acc, item) => acc + item.price * item.quantity,
      0
    );
    const tax = (value: number): string => money(subtotal * (value / 100));

    const [salesTax, cannabisTax, localTax] = [
      dispensary.sales_tax,
      dispensary.marijuana_tax,
      dispensary.local_tax,
    ].map(tax);

    const subtotalText = `SUBTOTAL${' '.repeat(
      3 - (cannabisTax.length - money(subtotal).length)
    )}${money(subtotal)}`;
    const paymentText = `Payment ${
      payment_method === Payment_Methods_Enum.Card ? 'Card' : 'Cash'
    }`;

    builder.addDocument(
      new StarXpandCommand.DocumentBuilder().addPrinter(
        new StarXpandCommand.PrinterBuilder()
          .styleInternationalCharacter(
            StarXpandCommand.Printer.InternationalCharacterType.Usa
          )
          .styleAlignment(StarXpandCommand.Printer.Alignment.Center)
          .styleMagnification(new StarXpandCommand.MagnificationParameter(4, 4))
          .actionPrintText('YouGotBud\n')
          .styleMagnification(new StarXpandCommand.MagnificationParameter(2, 2))
          .actionPrintText(
            [pickup_time_start, pickup_time_end].map(formatTime).join('-') +
              '\n'
          )
          .actionPrintText(fullName(customer!).toUpperCase() + '\n')
          .styleMagnification(new StarXpandCommand.MagnificationParameter(1, 1))
          .actionPrintText(
            lines(
              ...([
                `Order #: ${id}`,
                customer?.phone_number
                  ? `Phone: ${customer?.phone_number}`
                  : null,
                customer?.email ? `Email: ${customer?.email}` : null,
              ].filter(value => value !== null) as string[])
            )
          )
          .styleAlignment(StarXpandCommand.Printer.Alignment.Left)
          .actionPrintText(hr)
          .actionPrintText(
            lines(
              ...items.map(({quantity, price, product_variant, product}) => {
                const pad = ' '.repeat(quantity.toString().length + 2);
                const productSection =
                  `${quantity}  ${product.brand!.name}${' '.repeat(
                    columns -
                      2 -
                      quantity.toString().length -
                      product.brand!.name.length -
                      money(price).length
                  )}${money(price * quantity)}` +
                  `${pad}${
                    product.name.split(' ').reduce(
                      (acc, word) => ({
                        length: acc.length + word.length,
                        value:
                          acc.value +
                          (acc.length % 8 === 0
                            ? acc.length === 0
                              ? word
                              : `\n${pad}${word}`
                            : ` ${word}`),
                      }),
                      {length: 0, value: ''}
                    ).value
                  }\n` +
                  (!!product_variant
                    ? `${pad}${product_variant!.name}\n\n`
                    : '\n');

                return productSection;
              })
            )
          )
          .actionPrintText(hr)
          .actionPrintText(
            `${paymentText}${' '.repeat(
              columns - paymentText.length - subtotalText.length
            )}${subtotalText}\n`
          )
          .styleAlignment(StarXpandCommand.Printer.Alignment.Right)
          .actionPrintText(
            `LOCAL TAX${' '.repeat(
              4 - (cannabisTax.length - localTax.length)
            )}${localTax}\n`
          )
          .actionPrintText(
            `SALES TAX${' '.repeat(
              4 - (cannabisTax.length - salesTax.length)
            )}${salesTax}\n`
          )
          .actionPrintText(`CANNABIS TAX ${cannabisTax}\n`)
          .styleAlignment(StarXpandCommand.Printer.Alignment.Left)
          .actionPrintText(hr)
          .styleMagnification(new StarXpandCommand.MagnificationParameter(2, 2))
          .styleAlignment(StarXpandCommand.Printer.Alignment.Right)
          .actionPrintText(`Order Total ${money(price)}`)
          .styleMagnification(new StarXpandCommand.MagnificationParameter(1, 1))
          .styleAlignment(StarXpandCommand.Printer.Alignment.Left)
          .actionPrintText(hr)
          .styleAlignment(StarXpandCommand.Printer.Alignment.Center)
          .actionPrintText(
            `Ordered: ${dayjs(created_at).format('MM/DD/YY - HH:mm a')} \n\n`
          )
          .actionPrintText(`Thank you for shopping at ${dispensary.name}`)
          .actionCut(StarXpandCommand.Printer.CutType.Full)
      )
    );
    await printer.print(await builder.getCommands());
    setPrinting(false);
  };
bandit-ibayashi commented 2 years ago

We are sorry not to response for a long time. If you continue to have this problem, please let our staff in your region know along with the model, interface, and device information from this page.

bandit-ibayashi commented 1 year ago

This is closed because a new version has been released since this Issue was posted. If you continue to experience similar issues, we would appreciate it if you could submit a new Issue.