from dbConn import conn, cursor
from custInfo import getID, getInv

address = "46523 INVERNESS"
date = '20140611'
getCCID = getID(address, date)
# if (getCCID == '') or (getCCID == None) or len(getCCID) < 2:
	# print 'not found'
# else:
	# print "'"+getID(address, date) + "'"

# return ('MS5618', '725908', 'HC')
print getInv("77433")
# return ('', 'JM7840', 'IW')
with open(readdir,'r') as getStuff:
	with open(writedir, 'w') as putStuff:
		manyStuff = getStuff.read().splitlines()
		
		# remove header
		manyStuff.pop(0)
		
		for stuff in range(0, len(manyStuff)):
			stuffList = manyStuff[stuff].split(',')
			ccAndSO = stuffList[2]
			mmddyyyy = stuffList[0]
			
			
			date = mmddyyyy[4:]+mmddyyyy[:4]
			salesOrder = ccAndSO[6:]
			customerCode = ccAndSO[:7]
			trackingNum = str(stuffList[3])
			
			invNum = ''
			
			getInvNum = getInv(salesOrder)
			
			if getInvNum == None :
				invNum = 'Not Found'
			else:
				invNum = getInvNum[0]
			
			result = '%s %s %s %s %s' % (salesOrder, trackingNum, customerCode, date, invNum)
			
			putStuff.writelines(result+'\n')
			print result
			
			# if '/' is found, separate the string
			elif '/' in record[0]:					
				tempList = record[0].split("/")
				# loop through list to validate SO
				pos = 0
				while pos < len(tempList):					
					# check if SO only contains digits
					if not tempList[pos].isdigit():
						tempList.pop(pos)
						pos-=1
					pos+=1
				
				for result in tempList:	
					#get invoice info 
					invNumber = getInv(result)
					
					if invNumber != None:
						invNumberFromInv =  str(invNumber[0])
						salesPersonFromInv = str(invNumber[2])					
				
					# check if no customerCode, then check database to retrieve one
					if record[2] == '': 
						address = str(record[8])
						date = str(record[16])
						getCCID = getID(address, date)
						
						if (getCCID == '') or (getCCID == None) or len(getCCID) < 2:
							# check invoice # if customerCode can be found for the last time!!
							if invNumber == None:
								invNumberResult = "CC Not Found!"