#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

try:
    # Please do not use 'from scribus import *' . If you must use a 'from import',
    # Do so _after_ the 'import scribus' and only import the names you need, such
    # as commonly used constants.
    import scribus
except ImportError,err:
    print "This Python script is written for the Scribus scripting interface."
    print "It can only be run from within Scribus."
    sys.exit(1)

#########################
# YOUR IMPORTS GO HERE  #
#########################
# from scribus import Printer
from scribus import setText
from scribus import messageBox

def main(argv):
    """This is a documentation string. Write a description of what your code
    does here. You should generally put documentation strings ("docstrings")
    on all your Python functions."""
    #########################
    #  YOUR CODE GOES HERE  #
    #########################

    #
    #   This could use a GUI dialog box, but I simple edit the file and enter
    #   the two values here...

    startFactureNo=00
    endFactureNo=03

    #
    #   if you are printing a different number of tickets, you'll need to
    #   adjust the range() statement and include more/less calls to setText()
    #   to set the values of your named text fields.

    for idx in range(startFactureNo,endFactureNo,1):
        #
        #  Note: I wanted at least three positions to print, with leading
        #  zero-filled numbers.  If you want four positions, adjust
        #  the "00" string and the "-3" value to "000" and "-4".  The same
        #  logic applies to longer ticket numbers.
        #
        setText(unicode(("F4431780" + `(idx)`)[-3:], 'iso-8859-1'), "Fnbr1")
        setText(unicode(("F4431780" + `(idx)`)[-3:], 'iso-8859-1'), "Fnbr2")
        setText(unicode(("F4431780" + `(idx)`)[-3:], 'iso-8859-1'), "Fnbr3")
		# setText(unicode(("00" + `(idx)+0`)[-3:], 'iso-8859-1'), "num11")
        # p = Printer()
        #   you will need to substitute the name of your printer here.
        # p.printer = 'hp';
        # p.Print()
		filename = unicode(("F4431780" + `(idx)`)[-3:], 'iso-8859-1')
		pdf = scribus.PDFfile()
		pdf.file = filename+".pdf"
		pdf.save()
        # debugging 
        # result = messageBox('Note', "Just printed tickets " + str(idx) + " through " + str(idx+3) )

def main_wrapper(argv):
    """The main_wrapper() function disables redrawing, sets a sensible generic
    status bar message, and optionally sets up the progress bar. It then runs
    the main() function. Once everything finishes it cleans up after the main()
    function, making sure everything is sane before the script terminates."""
    try:
    #result = messageBox('Monkeys!', 'Something went ook!', ICON_WARNING, BUTTON_YES|BUTTONOPT_DEFAULT, BUTTON_NO, BUTTON_IGNORE|BUTTONOPT_ESCAPE)
    scribus.statusMessage("Running script...")
    scribus.progressReset()
    main(argv)
    finally:
    # Exit neatly even if the script terminated with an exception,
    # so we leave the progress bar and status bar blank and make sure
    # drawing is enabled.
    if scribus.haveDoc():
        scribus.setRedraw(True)
    scribus.statusMessage("")
    scribus.progressReset()

# This code detects if the script is being run as a script, or imported as a module.
# It only runs main() if being run as a script. This permits you to import your script
# and control it manually for debugging.
if __name__ == '__main__':
    main_wrapper(sys.argv)