#!/usr/bin/python

import sys, string, os, tempfile

from gnue.common.apps.GClientApp import GClientApp
from gnue.common.utils.TextUtils import dollarToText, comify
from gnue.common.datasources.GDataSource import DataSourceWrapper
from mx.DateTime import *

VERSION = "0.0.1"

#
# Datasource stuff
#

CONNECTION = 'market'

#
# Main code
#

FED_ID="20-???????"

ADDRESS="""\
Off the Shelf
dba/Old Towne Market
9120 Pigeon Roost Rd
Olive Branch MS 38654"""


def run(connections, year='2003', printer='hp0'):

  datasource = DataSourceWrapper(connections,
                   fields=('vendor','fed_id','fed_id_type','check_name',
                           'address1','address2','city',
                           'state','zip','amount'),
                   attributes={'connection': CONNECTION,
                               'name': 'dts1099s',
                               'table': 'v_1099s',
                               'order_by': 'vendor' } )

  resultset = datasource.createResultSet(conditions={"year": year})

  for rec in resultset:
    indent = "    "
    print "\n" * 2
    print "    " + ADDRESS.replace('\n','\n'+indent)
    print "\n" * 4

    if rec['fed_id_type'] == 'S':
      fed_id = '%s-%s-%s' % (
              rec['fed_id'][:3],
              rec['fed_id'][3:5],
              rec['fed_id'][5:] )
    elif rec['fed_id_type'] == 'F':
      fed_id = '%s-%s' % (
              rec['fed_id'][:2],
              rec['fed_id'][2:] )
    else:
      fed_id = ""

    print indent + FED_ID + " " * 7 + fed_id

    print
    print

    print indent + rec['check_name']
    print indent + " " * 34 + comify(rec['amount'],2)
    print "\n" * 1
    print indent + (rec['address1'] or '')
    print indent + (rec['address2'] or '')
#    print
    print indent + (rec['city'] or '') + ' '+ (rec['state'] or '') + ' '+ (rec['zip'] or '')

    print "\n" * 10


class Runner(GClientApp):
  def run(self):
   run(self.connections)


if __name__ == '__main__':
  Runner().run()


