 
     # First request to get the cookie
     request options, (err, res, body) ->
-        return next err if err?
-
-        loginOptions =
-            method: 'POST'
-            form: form
-            jar: true
-            strictSSL: false
-            url: submitUrl
-            headers:
-                'Cookie': res.headers['set-cookie']
-                'Referer': 'https://assure.ameli.fr/PortailAS/appmanager/' + \
-                           'PortailAS/assure?_nfpb=true&_pageLabel=' + \
-                           'as_login_page'
-
-        # Second request to authenticate
-        request loginOptions, (err, res, body) ->
-
-            isNotLogedIn = body.indexOf('Connexion à mon compte') > -1
-
-            if err or isNotLogedIn
-                log.error "Authentification error"
-                next 'bad credentials'
-            else
-                reimbursementOptions =
-                    method: 'GET'
-                    jar: true
-                    strictSSL: false
-                    url: reimbursementUrl
-
-                # Last request to get the reimbursements
-                request reimbursementOptions, (err, res, body) ->
-                    if err then next err
-                    else
-                        data.html = body
-                        next()
+        if err
+            log.error err
+            return next 'request error'
+        else
+            loginOptions =
+                method: 'POST'
+                form: form
+                jar: true
+                strictSSL: false
+                url: submitUrl
+                headers:
+                    'Cookie': res.headers['set-cookie']
+                    'Referer': refererUrl
+
+            # Second request to authenticate
+            request loginOptions, (err, res, body) ->
+                if err
+                    log.error err
+                    next 'bad credentials'
+                else if body.indexOf('Connexion à mon compte') > -1
+                    log.error 'Authentication error'
+                    next 'bad credentials'
+                else
+                    reimbursementOptions =
+                        method: 'GET'
+                        jar: true
+                        strictSSL: false
+                        headers:
+                            'Cookie': res.headers['set-cookie']
+                            'Referer': refererUrl
+                        url: reimbursementUrl
+
+                    # Last request to get the reimbursements page
+                    request reimbursementOptions, (err, res, body) ->
+                        if err
+                            log.error err
+                            return next 'request error'
+                        else
+                            data.html = body
+                            next()
 
 
 # Parse the fetched page to extract bill data.


    $ = cheerio.load data.html

    $('#tabDerniersPaiements tbody tr').each ->
        date = $($(this).find('td').get(0)).text()
        subtype = $($(this).find('td').get(1)).text()

        amount = $($(this).find('td').get(2)).text()
        amount = amount.replace(' euros', '').replace(',','.')
        amount = parseFloat amount

        # Get the details url
        detailsUrl = $($(this).find('td a').get(1)).attr('href')
        # Remove the unecessary port to avoid buggy request
        detailsUrl = detailsUrl.replace(':443', '')

        bill =
            amount: amount
            type: 'health'
            subtype: subtype
            date: moment date, 'DD/MM/YYYY'
            vendor: 'Ameli'
            detailsUrl: detailsUrl

        healthBills.fetched.push bill if bill.amount?

    # For each bill, get the pdf
    async.each healthBills.fetched, getPdf, (err) ->
        next err


# Retrieve pdf url
getPdf = (bill, callback) ->
    detailsUrl = bill.detailsUrl

    options =
        method: 'GET'
        jar: true
        strictSSL: false
        url: detailsUrl

    # Request to get the pdf url
    request options, (err, res, body) ->
        if err? or res.statusCode isnt 200
            callback new Error 'Pdf not found'
        else
            html = cheerio.load body
            pdfUrl = "https://assure.ameli.fr"
            pdfUrl += html('.r_lien_pdf').attr('href')
            # Remove all the dirty escape characters...
            pdfUrl = pdfUrl.replace(/(?:\r\n|\r|\n|\t)/g, '')
            bill.pdfurl = pdfUrl
            callback null


buildNotification = (requiredFields, healthBills, data, next) ->


    next()


customLinkBankOperation = (requiredFields, healthBills, data, next) ->
    identifier = 'C.P.A.M.'
    bankIdentifier = requiredFields.bank_identifier
    identifier = bankIdentifier if bankIdentifier? and bankIdentifier isnt ""

    linkBankOperation(
        log: log
