Example #1
0
def reportCreation():
    error = None
    if 'token' in session:
        concur = ConcurClient()
        token = session['token']
        username = session['username']
        if request.method == 'POST':
            today = datetime.date.today()
            new_xml_update = {}
            new_xml_update['Report'] = {}
            #new_xml_update['Report']['ID'] = dict_resp_report['Report']['ID']
            new_xml_update['Report']['Name'] = request.form['Name']
            new_xml_update['Report']['Comment'] = request.form['Comment']

            xml_post = xmltodict.unparse(new_xml_update)

            content_type, resp_post = concur.validate_response(concur.api('v3.0/expense/reports', method='POST', params={'access_token': token},
                                                                            headers={'content-type': 'application/xml', 'accept':'*'}, data=xml_post))


            #print resp_post['Response']['ID']
            #print resp_post['Response']['URI']
            return render_template('reportCreationSuccess.html', username=username, reportURL=resp_post['Response']['URI'], reportID=resp_post['Response']['ID'], error=error)


        return render_template('reportCreation.html', username=username, error=error)
    else:
        return 'Invalid Token - Please Login /login'
Example #2
0
def receiptsInput():
    error = None
    if 'token' in session:
        concur = ConcurClient()
        token = session['token']
        username = session['username']
        #resp = concur.validate_response(concur.api('v3.0/expense/reports', params={'access_token': token}))
        content_type, userProfile = concur.validate_response(concur.api('user/v1.0/user', method='GET', params={'access_token': token, 'loginID': username}))
        prefix = 'ns0:'
        isExpenseApprover = True if userProfile[prefix + 'UserProfile'][prefix + 'ExpenseApprover'] == 'Y' else False
        isInvoiceApprover = True if userProfile[prefix + 'UserProfile'][prefix + 'InvoiceApprover'] == 'Y' else False

        if request.method == 'POST':
            receiptsNumbers = request.form['receiptsNumbers'].split('\n')

            counter = 1

            list_headers = ["Report ID","Status"]
            y = PrettyTable(list_headers)

            for rn in receiptsNumbers:
                if len(rn)>0:
                    #Retrieve The whole Report
                    response = concur.api('v3.0/expense/reports', method='GET', params={'access_token': token, 'id': rn.strip()})
                    resp_report = response.content
                    dict_resp_report = xmltodict.parse(resp_report)

                    #print dict_resp_report['Report']['Name']
                    #for k,v in dict_resp_report['Report'].iteritems():
                    #    print "%s:%s" % (k,v)

                    #if dict_resp_report['Report']['Custom15']['Type']=='List':
                    #    dict_resp_report['Report']['Custom15'] = dict_resp_report['Report']['Custom15']['ListItemID']


                    now = datetime.datetime.now()
                    new_xml_update = {}
                    new_xml_update['Report'] = {}
                    #new_xml_update['Report']['ID'] = dict_resp_report['Report']['ID']
                    new_xml_update['Report'][BOX_NUMBER_FIELD] = {}
                    new_xml_update['Report'][BOX_NUMBER_FIELD]['Value'] = 'Box Number:' + request.form['BoxNumber']
                    new_xml_update['Report']['Comment'] = now.strftime('Receipts Received at %m/%d/%Y %H:%M')
                    #new_xml_update['Report']['ReceiptsReceived'] = "Y"
                    #new_xml_update['Report']['Custom16'] = {}
                    #new_xml_update['Report']['Custom16']['Value'] = "true"
                    counter = counter + 1


                    b = bytearray(TextToImage(u'Country Code: ' + dict_resp_report['Report']['Country'] +
                            u'\nBox Number: ' + request.form['BoxNumber'] +
                            u'\nReport IDr: ' + dict_resp_report['Report']['ID'] +
                            u'\nYour Receipts Hardcopies have been Processed. ' +
                            u'\nAny Question Please Call: (555)555-5555',
                            16,
                            ))
                    '''
                    # Send dummy file
                    with open(UPLOAD_FOLDER + 'dummy_pic.png') as image_file:
                        f = image_file.read()

                        b = bytearray(text2png(u'Country Code: ' + dict_resp_report['Report']['Country'] +
                                u'\nBox Number: ' + request.form['BoxNumber'] +
                                u'\nReport IDr: ' + dict_resp_report['Report']['ID'] +
                                u'\nYour Receipts Hardcopies have been Processed. Any Question Please Call: (555)555-5555',
                                color = "#000",
                                bgcolor = "#FFF",
                                fontfullpath = None,
                                fontsize = 16,
                                leftpadding = 1,
                                rightpadding = 1,
                                width = 450,
                                ))
                    '''

                    #content_type, resp_file_post = concur.validate_response(concur.api('image/v1.0/report', method='POST', params={'access_token': token, 'id': rn.strip()},
                    content_type, resp_file_post = concur.validate_response(concur.api('image/v1.0/report/%s' % (rn.strip()), method='POST', params={'access_token': token},
                                                                                                    headers={'content-type': 'image/png', 'accept':'*'}, data=b))
                    print resp_file_post


                    #xml_update = xmltodict.unparse(dict_resp_report)
                    xml_update = xmltodict.unparse(new_xml_update)

                    print xml_update


                    #new report based on the last one
                    content_type, resp_update = concur.validate_response(concur.api('v3.0/expense/reports', method='PUT', params={'access_token': token, 'id': rn.strip()},
                                                                                    headers={'content-type': 'application/xml', 'accept':'*'}, data=xml_update))

                    #content_type, resp_update = concur.validate_response(concur.api('v3.0/expense/reports', method='POST', params={'access_token': token},
                    #                                                                headers={'content-type': 'application/xml', 'accept':'*'}, data=xml_post))

                    if resp_update['Response']=='no-content':
                        list_row = [ dict_resp_report['Report']['ID'],
                                     'Updated OK',
                                     ]
                    else:
                        list_row = [ dict_resp_report['Report']['ID'],
                                     'Updated FAIL',
                                     ]

                    y.add_row(list_row)


            y.sortby = "Report ID"
            y.sort_key = operator.itemgetter(1)
            #print y

            y_html_str = y.get_html_string(attributes = {"style":"border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;"}, border=True, header=True, padding_width=2).encode("utf8")
            y_html_str = y_html_str.replace('<th>','<th style="border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;">')
            y_html_str = y_html_str.replace('<td>','<td style="border-width: 1px;padding: 3px;border-style: solid;border-color: black;">')

            return render_template('receiptsInputResponse.html', reportsUpdatedList=y_html_str, username=username, error=error)

        return render_template('receiptsInput.html', username=username, isExpenseApprover=isExpenseApprover, isInvoiceApprover=isInvoiceApprover, error=error)
    else:
        return 'Invalid Token - Please Login /login'
Example #3
0
def reportsList():
    error = None
    if 'token' in session:
        concur = ConcurClient()
        token = session['token']
        username = session['username']

        content_type, reports = concur.validate_response(concur.api('v3.0/expense/reports',
                                                params={'access_token': token}))


        print token

        for (k, report_arr) in reports['Reports']['Items'].iteritems():
            # print '%s: %s' % (k, v)

            '''
            for rep in report_arr:
                print 'ID: %s' % rep['ID']
                print 'Name: %s' % rep['Name']
                print 'HasException: %s' % rep['HasException']
                print 'ReceiptsReceived: %s' % rep['ReceiptsReceived']
                print 'Total: %s' % rep['Total']
                print '===================='
                # for (k1, v1) in rep.iteritems():
                #    print '%s: %s' % (k1, v1)
            '''

            #sorted_x = sorted(report_arr[0].iteritems(), key=operator.itemgetter(1))

            list_headers = ["ID","Name","HasException","ReceiptsReceived","LastComment","Box Number","Total"]
            y = PrettyTable(list_headers)
            #get the first x for details:
            for rep in report_arr:
                for k1,v1 in rep.iteritems():
                    if str(k1).startswith('Custom'):
                        print k1,v1

                list_row = [ rep['ID'],
                             rep['Name'],
                             rep['HasException'],
                             rep['ReceiptsReceived'],
                             rep['LastComment'],
                             '' if not rep[BOX_NUMBER_FIELD].has_key('ListItemID') else rep[BOX_NUMBER_FIELD]['Value'],
                             rep['Total']]

                y.add_row(list_row)

            y.sortby = "ID"
            y.sort_key = operator.itemgetter(1)
            #print y

            y_html_str = y.get_html_string(attributes = {"style":"border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;"}, border=True, header=True, padding_width=2).encode("utf8")
            y_html_str = y_html_str.replace('<th>','<th style="border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;">')
            y_html_str = y_html_str.replace('<td>','<td style="border-width: 1px;padding: 3px;border-style: solid;border-color: black;">')

            #adding date in the beggining
            #y_html_str = '<h2 align="left"><b>From  %s  to  %s</b></h2></br>' % (periodFrom,periodTo) + y_html_str

            #br in the end
            y_html_str = y_html_str + '</br></br>'

            #y.format = True
            return render_template('reportsList.html', reportsList=y_html_str, username=username, error=error)




        return render_template('receiptsInput.html', username=username, isExpenseApprover=isExpenseApprover, isInvoiceApprover=isInvoiceApprover, error=error)
    else:
        return 'Invalid Token - Please Login /login'
def test():
    loginID = '*****@*****.**'

    access_token_full = \
        getTokenGivenUsernamePasswordAndConsumerKey(loginID, 'testtest1'
                                                    , '85Bvf1931AXmiNCfeB1O5L')
    print access_token_full
    if access_token_full.has_key('Error'):
        print 'Wrong Password'
    else:
        token = access_token_full['Access_Token']['Token']

        # concur = ConcurClient('85Bvf1931AXmiNCfeB1O5L','Lwq6xDnvWSN8YQHrw5WMYDyGa9I1FAxs',str(access_token['Access_Token']['Token']),False)

        concur = ConcurClient()

        reports = concur.validate_response(concur.api('v3.0/expense/reports',
                                                params={'access_token': token}))
        for (k, report_arr) in reports[1]['Reports']['Items'].iteritems():
            # print '%s: %s' % (k, v)
            for rep in report_arr:
                print 'ID: %s' % rep['ID']
                print 'Name: %s' % rep['Name']
                print 'HasException: %s' % rep['HasException']
                print 'ReceiptsReceived: %s' % rep['ReceiptsReceived']
                print 'Total: %s' % rep['Total']
                print '===================='
                # for (k1, v1) in rep.iteritems():
                #    print '%s: %s' % (k1, v1)

            #sorted_x = sorted(report_arr[0].iteritems(), key=operator.itemgetter(1))

            list_headers = ["ID","Name","HasException","ReceiptsReceived","Total"]
            y = PrettyTable(list_headers)
            #get the first x for details:
            for rep in report_arr:
                list_row = [ rep['ID'],rep['Name'],rep['HasException'],rep['ReceiptsReceived'],rep['Total']]
                y.add_row(list_row)

            y.sortby = "ID"
            y.sort_key = operator.itemgetter(1)
            #print y

            y_html_str = y.get_html_string(attributes = {"style":"border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;"}, border=True, header=True, padding_width=2).encode("utf8")
            y_html_str = y_html_str.replace('<th>','<th style="border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;">')
            y_html_str = y_html_str.replace('<td>','<td style="border-width: 1px;padding: 3px;border-style: solid;border-color: black;">')

            #adding date in the beggining
            #y_html_str = '<h2 align="left"><b>From  %s  to  %s</b></h2></br>' % (periodFrom,periodTo) + y_html_str

            #br in the end
            y_html_str = y_html_str + '</br></br>'

            #y.format = True
            print y_html_str

        resp = concur.validate_response(concur.api('user/v1.0/user',
                                                   params={'access_token': token, 'loginID': loginID}))

        dict_userProfile = resp[1]
        prefix = 'ns0:'
        isExpenseApprover = (True if dict_userProfile[prefix
                                                      + 'UserProfile'][prefix + 'ExpenseApprover'
                                                                       ] == 'Y' else False)
        isInvoiceApprover = (True if dict_userProfile[prefix
                                                      + 'UserProfile'][prefix + 'InvoiceApprover'
                                                                       ] == 'Y' else False)
        print isExpenseApprover
        print isInvoiceApprover