def viewComplete(jobID):

    page = "JOB RECORD"
    oplist = DB.getActiveOperators()
    typeList = DB.getJobType()
    workcellList = DB.getWorkCells()
    result = DB.setUpdateForm(jobID)
    endDate = result[0][15]
    endDate = endDate[0:10]

    job = {
        'job ID': result[0][0],
        'job name': result[0][1],
        'work order': result[0][2],
        'cell': result[0][3],
        'cell ID': result[0][4],
        'status': result[0][5],
        'status ID': result[0][6],
        'type': result[0][7],
        'type ID': result[0][8],
        'weight': result[0][9],
        'activity ID': result[0][10],
        'operator': result[0][11],
        'operator ID': result[0][12],
        'last op': result[0][13],
        'notes': result[0][14],
        'last activity': endDate
    }

    if request.method == 'POST':
        now = datetime.now()
        time = now.strftime('%I:%M %p')
        date = now.strftime('%Y-%m-%d')
        dbNotes = job['notes']
        pageNotes = request.form['notes']

        if (util.textHasChanged(dbNotes, pageNotes)):
            alias = "SYS"
            notes = pageNotes
            addNote = util.appendTimeStamp(alias, notes)
        else:
            addNote = dbNotes

        details = {'jobID': job['job ID'], 'notes': addNote}

        DB.updateJobNotes(details)

        return redirect(url_for('completedJobs'))

    else:
        return render_template('job_record.html',
                               job=job,
                               oplist=oplist,
                               page=page)

    return render_template('job_record.html',
                           job=job,
                           oplist=oplist,
                           page=page)
def updateJob(jobID):
    page = "UPDATE WORK IN PROCESS"
    oplist = DB.getActiveOperators()
    typeList = DB.getJobType()
    workcellList = DB.getWorkCells()
    result = DB.setUpdateForm(jobID)
    job = {
        'job ID': result[0][0],
        'job name': result[0][1],
        'work order': result[0][2],
        'cell': result[0][3],
        'cell ID': result[0][4],
        'status': result[0][5],
        'status ID': result[0][6],
        'type': result[0][7],
        'type ID': result[0][8],
        'weight': result[0][9],
        'activity ID': result[0][10],
        'operator': result[0][11],
        'operator ID': result[0][12],
        'last op': result[0][13],
        'notes': result[0][14],
        'last activity': result[0][15],
    }

    operationValues = util.getAvailableOperationValues(job['last op'])
    # #to be passed to the next operation combo box

    errors = True

    if request.method == 'POST':
        errors = False

        if not request.form['action']:
            errors = True

        if not request.form['operator']:
            errors = True

        if not request.form['next-operation']:
            errors = True

        if not errors:

            now = datetime.now()
            time = now.strftime('%I:%M %p')
            date = now.strftime('%Y-%m-%d')
            dbNotes = job['notes']
            pageNotes = request.form['notes']
            OperatorID = request.form['operator']
            nextOperation = request.form['next-operation']
            action = request.form['action']
            status = action

            if action == "7":
                status = 1

            if action == "2":
                nextOperation = DB.getFinalOperationByID(job['job ID'])
                alias = util.getUserAlias(OperatorID)
                notes = pageNotes
                addNote = util.appendTimeStamp(alias, notes, 'Job Completed')

            # Note Handling
            if (util.textHasChanged(dbNotes, pageNotes)):
                alias = util.getUserAlias(OperatorID)
                notes = pageNotes
                if action == "2":
                    addNote = util.appendTimeStamp(alias, notes,
                                                   'Job Completed')
                else:
                    addNote = util.appendTimeStamp(alias, notes)
            else:
                if action == "2":
                    alias = util.getUserAlias(OperatorID)
                    addNote = util.appendTimeStamp(alias, dbNotes,
                                                   'Job Completed')
                else:
                    addNote = dbNotes

            details = {
                'jobID': job['job ID'],
                'status': status,
                'activity': action,
                'notes': addNote,
                'time': time,
                'date': date,
                'operation': nextOperation,
                'operator': request.form['operator']
            }

            DB.logActivity(details)
            if not (request.form['action']
                    == '2') and not (request.form['action'] == '5'):
                time = ''
                date = ''

            DB.updateJobRecord(details)

            details['job'] = job['job name']
            details['work order'] = job['work order']
            details['operator'] = DB.getOperatorByID(details['operator'])
            details['status'] = DB.getStatusByID(details['status'])

            page = "UPDATE SUCCESS"
            return render_template('update_job_success.html',
                                   details=details,
                                   page=page)

        #return redirect(url_for('index'))

    else:
        return render_template('update_job.html',
                               job=job,
                               oplist=oplist,
                               page=page,
                               operationValues=operationValues)

    return render_template('update_job.html',
                           job=job,
                           oplist=oplist,
                           page=page,
                           operationValues=operationValues)
def newJobform():
    page = 'START A NEW JOB'
    oplist = DB.getActiveOperators()
    typeList = DB.getJobType()
    workcellList = DB.getWorkCells()

    if request.method == 'POST':
        errors = False

        if not request.form['operator']:
            errors = True

        if not request.form['job_name']:
            errors = True

        if not request.form['work_order']:
            errors = True

        if not request.form['work_cell']:
            errors = True

        if not request.form['job_type']:
            errors = True

        if not request.form['job_weight']:
            errors = True

        if not request.form['total_operations']:
            errors = True

        try:
            ck_ipt = request.form['in_process_testing']
        except:
            ck_ipt = '0'

        try:
            ck_pre = request.form['predjustments']
        except:
            ck_pre = '0'

        if not errors:
            now = datetime.now()
            time = now.strftime('%I:%M %p')
            date = now.strftime('%Y-%m-%d')

            alias = util.getUserAlias(request.form['operator'])
            notes = request.form['notes']
            addNote = util.appendTimeStamp(alias, notes, 'Job Started')

            newRecord = {
                'operator': request.form['operator'],
                'job': request.form['job_name'].upper(),
                'workOrder': request.form['work_order'],
                'workCell': request.form['work_cell'],
                'jobType': request.form['job_type'],
                'jobWeight': request.form['job_weight'],
                'totalOperatiions': request.form['total_operations'],
                'inProcessTesting': ck_ipt,
                'preAdjustments': ck_pre,
                'notes': addNote,
                'jobStatus':
                1,  # Job status is 'In Progress' as a starting condtion (job status table)
                'startTime': time,
                'startDate': date,
                'lastOperation': 10,  #jobs start at operation 10
                'Activity':
                4  #the value 4 represents 'starting' from the Activity_Action table
            }

            DB.startJob(newRecord)
            page = "JOB ADDED"

            newRecord['operator'] = DB.getOperatorByID(newRecord['operator'])

            #return redirect(url_for('index'))
            return render_template('new_job_success.html',
                                   newRecord=newRecord,
                                   page=page)

        else:
            return render_template('new_job.html',
                                   oplist=oplist,
                                   typeList=typeList,
                                   workcellList=workcellList,
                                   page=page)

    return render_template('new_job.html',
                           oplist=oplist,
                           typeList=typeList,
                           workcellList=workcellList,
                           page=page)