from datetime import datetime
from django.shortcuts import render, redirect, reverse
from django.template import loader
from django.http import HttpResponse
from sklearn.tree import DecisionTreeClassifier

from User.forms import MyForm,MyForm2,MyForm3,MyForm4,MyForm5,MyFormServices,MyFormNew
from django.contrib.auth import authenticate, login
from sklearn.linear_model import LogisticRegression
from User import models as Umodels
from django.http import HttpResponseRedirect
from django.contrib import messages
import json

# -------------------   EF
import pandas as pd
import numpy as np
import math
import sklearn
import matplotlib.pyplot as plt
from datetime import datetime
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
# -------------------   EF

# Demand Forecast
import statsmodels.api as sm
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.arima.model import ARIMA
from pandas.tseries.offsets import DateOffset

import warnings
warnings.filterwarnings("ignore")

# Start  CompanyHome
def Homepage(request):
    return render(request, 'Admin/Homepage.html');
# End  CompanyHome

# Employee_Efficiency
def Employee_Efficiency(request):
    if request.method == 'POST':
        myForm = MyForm5(request.POST)
        if myForm.is_valid():
            workid = myForm.cleaned_data['customerid']
            DateSet = pd.read_csv("Data/WorkHistory.csv", skipinitialspace=True)

            DateSet['JobDone'].replace({'Engine Repairering': 1, 'Painting': 2, 'Oil Changing': 3,
                                        'Interior Cleaning': 4, 'Body Wash': 5, 'Under Wash': 6}, inplace=True)
            DateSet = DateSet[(DateSet.WorkQuality > 1)]  # search for job need to be done
            DateSet = DateSet.drop(columns=['WorkQuality'])
            x = DateSet[['JobDone', 'TakenTime']]
            y = DateSet[['StaffID']]
            grouped_df = DateSet[(DateSet["JobDone"] == workid)]  # get selected work
            grouped_df = grouped_df.groupby("StaffID")  # group job staff
            grouped_df = grouped_df['TakenTime']  # get only taken time to complete work
            mean_df = grouped_df.mean()  # avarage time to complete work
            min_df = mean_df.min()  # get less time taken to complete work

            # x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=2)
            # lm = LinearRegression()
            # lm.fit(x_train, y_train)
            #
            # new_input = [[workid, min_df]]  # Best WorkQuality Work
            # pred = lm.predict(new_input)
            # workid = (int(pred.round())) # final employee ID by ML
            #
            # print("--------------Accuracy-----------------")
            # accuracy = lm.score(x_test, y_test)
            # print(accuracy)

            X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=2)

            clf = DecisionTreeClassifier()
            clf = clf.fit(X_train, y_train)
            y_pred = clf.predict(X_test)

            new_input = [[workid, min_df]]  # Best WorkQuality Work
            pred = clf.predict(new_input)
            workid = (int(pred.round()))  # final employee ID by ML

            print("Accuracy:", metrics.accuracy_score(y_test, y_pred))

            DateSetLoad = pd.read_csv("Data/WorkHistory.csv", skipinitialspace=True)

            data2 = pd.read_csv('Data/EmployeeStatus.csv')

            # using merge function by setting how='inner'
            output1 = pd.merge(DateSetLoad, data2,
                               on='StaffID',
                               how='inner')

            DateSetResult = DateSetLoad[(output1.StaffID == workid) & (output1.Availability == "Yes")]

            df = DateSetResult[:1]
            # data = data.to_html()

            # -----------------------------------------------------------------------  aaAccuracy section

            # -----------------------------------------------------------------------  aaAccuracy section
            json_records = df.reset_index().to_json(orient='records')
            arr = []
            arr = json.loads(json_records)
            if (arr == [] ):
                DateSetResult = DateSetLoad[(output1.Availability == "Yes")]
                df = DateSetResult[:1]
                # data = data.to_html()
                json_records = df.reset_index().to_json(orient='records')
                arr = []
                arr = json.loads(json_records)

            else:
                a = 2

            form = MyForm5()
            context = { 'form': form, 'd': arr}
            return render(request, 'Admin/Employee_Efficiency.html', context)

    else:
        form = MyForm5()
    return render(request, 'Admin/Employee_Efficiency.html', {'form': form});


def Employee_Data(request):
    data1 = pd.read_csv('Data/WorkHistory.csv')
    data2 = pd.read_csv('Data/EmployeeStatus.csv')
    DateSet = pd.merge(data1, data2,  on='StaffID',   how='inner')
    grouped_df = DateSet.groupby("StaffID" )  # group job staff
    finalpt = grouped_df.head(10) # date from two datasets
    json_records = finalpt.reset_index().to_json(orient='records')
    arr = []
    arr = json.loads(json_records)
    context = {'d': arr}
    return render(request, 'Admin/Employee_Data.html',context);

def EmployeeStatus_Update(request):
    if request.method == 'POST':
        myForm = MyForm3(request.POST)
        if myForm.is_valid():
            StaffID = myForm.cleaned_data['StaffID']
            Availability = myForm.cleaned_data['Availability']
            DateSet = pd.read_csv("Data/EmployeeStatus.csv")
            StaffID = (int(StaffID) - 1)
            DateSet.at[StaffID,'Availability']= Availability
            DateSet.to_csv("Data/EmployeeStatus.csv", index=False)
            messages.success(request, 'Employee status successfully! Updated.')
            return HttpResponseRedirect(request.path_info)
        else:
            form = MyForm3()
            return render(request, 'Admin/EmployeeStatus_Update.html', {'form':form});

    else:
        form = MyForm3()
    return render(request, 'Admin/EmployeeStatus_Update.html', {'form':form});

# Demand Forecasting History
def PurchaseHistory(request):
    data = pd.read_csv('Data/Purchasefile.csv')
    finalpt = data.head(121)
    json_records = finalpt.reset_index().to_json(orient='records')
    arr = []
    arr = json.loads(json_records)
    context = {'d': arr}
    return render(request, 'Admin/PurchaseHistory.html',context);

# start Demand Forecasting Machine Learning Model
def Forecasting(request):
    if request.method == 'POST':
        myForm = MyForm4(request.POST)
        if myForm.is_valid():
            Productid = myForm.cleaned_data['Productid']

            if (Productid == 1):  # load first product budget
                df = pd.read_csv('Data/Purchasefile.csv')
                df = df.drop(
                    ['VIC_Hino_Fuel_Filter', 'Price2', 'Substitude_Product_Price2', 'Production_cost_per_unit2',
                     'CALTEX_Texamatic_1888_Transmision_Oil', 'Price3', 'Substitude_Product_Price3',
                     'Production_cost_per_unit3'], axis=1)
                df.head()
                ## Cleaning up the data
                df.columns = ["Month", "CALTEX_Delo_Gold_Ultra", "Price1", "Substitude_Product_Price1",
                              "Production_cost_per_unit1"]
                rrr = df.head()
                ## Drop last 2 rows
                #  df.drop(106, axis=0, inplace=True)
                # df.tail()
                #   df.drop(105, axis=0, inplace=True)
                # df.tail()

                # Convert Month into Datetime
                df['Month'] = pd.to_datetime(df['Month'])
                # df.head()
                df.set_index('Month', inplace=True)
                # df.head()
                df.describe()
                # df.plot()
                # plt.show()
                ### Testing For Stationarity

                from statsmodels.tsa.stattools import adfuller
                test_result = adfuller(df['CALTEX_Delo_Gold_Ultra'])
                test_result2 = adfuller(df['Price1'])

                def adfuller_test(CALTEX_Delo_Gold_Ultra):
                    result = adfuller(CALTEX_Delo_Gold_Ultra)
                    labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                    for value, label in zip(result, labels):
                        print(label + ' : ' + str(value))

                def adfuller_test2(Price1):
                    result2 = adfuller(Price1)
                    labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                    for value, label in zip(result2, labels):
                        print(label + ' : ' + str(value))

                adfuller_test(df['CALTEX_Delo_Gold_Ultra'])
                adfuller_test2(df['Price1'])
                ##Differencing
                df['Product1 First Difference'] = df['CALTEX_Delo_Gold_Ultra'] - df['CALTEX_Delo_Gold_Ultra'].shift(1)
                df['Product1 First Difference2'] = df['Price1'] - df['Price1'].shift(1)
                #  df['Sales'].shift(1)
                df['Seasonal First Difference'] = df['CALTEX_Delo_Gold_Ultra'] - df['CALTEX_Delo_Gold_Ultra'].shift(12)
                df['Seasonal First Difference2'] = df['Price1'] - df['Price1'].shift(12)
                # df.head(14)

                ## Again test dickey fuller test
                adfuller_test(df['Seasonal First Difference'].dropna())
                adfuller_test2(df['Seasonal First Difference2'].dropna())

                import statsmodels.api as sm
                # For non-seasonal data
                # p=1, d=1, q=0 or 1
                from statsmodels.tsa.arima.model import ARIMA
                model = ARIMA(df['CALTEX_Delo_Gold_Ultra'], order=(1, 0, 0))
                model2 = ARIMA(df['Price1'], order=(1, 0, 0))
                model_fit = model.fit()
                print(model_fit.summary())
                model_fit2 = model2.fit()
                print(model_fit2.summary())
                # model_fit.summary()
                df['forecast'] = model_fit.predict(start=104, end=120, dynamic=True)
                df['forecastPrice'] = model_fit2.predict(start=104, end=120, dynamic=True)
                # df[['Product1','forecast']].plot(figsize=(12,8))
                model = sm.tsa.statespace.SARIMAX(df['CALTEX_Delo_Gold_Ultra'], order=(1, 1, 1),
                                                  seasonal_order=(1, 1, 1, 12))
                model2 = sm.tsa.statespace.SARIMAX(df['Price1'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))

                results = model.fit()

                results2 = model2.fit()

                # df['forecast']=results.predict(start=100,end=100,dynamic=True)
                # df[['Product1','forecast']].plot(figsize=(12,8))
                from pandas.tseries.offsets import DateOffset
                future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)]
                future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns)
                # future_datest_df.tail()
                future_df = pd.concat([df, future_datest_df])

                # work with date

                # work with date

                # find Date Space
                inputdatex = myForm.cleaned_data['inputdate']
                inputdate = datetime.strftime(inputdatex, "%m/%d/%Y")

                lastDate = '07/01/2020'

                inputdate = inputdate
                date_format = "%m/%d/%Y"
                a = datetime.strptime(lastDate, date_format)  # user input date
                b = datetime.strptime(inputdate, date_format)
                delta = b - a
                delta2 = delta.days
                # print(delta2)
                forcasting_date = int(delta2 / 30)
                newDate = forcasting_date + 102  # ending number

                #   print(forcasting_date)
                forecast = future_df['forecast'] = results.predict(start=104, end=newDate, dynamic=True)
                #    future_df[['Sales', 'forecast']].plot(figsize=(12, 8))

                forecastPrice = future_df['forecastPrice'] = results2.predict(start=104, end=newDate, dynamic=True)
                #   future_df[['Price1', 'forecastPrice']].plot(figsize=(12, 8))

                df3 = forecast[-1]
                df4 = forecastPrice[-1]
                print(int(df3))
                print(int(df4))

                arz = int(df3)
                arz2 = int(df4)
                arz3 = arz * arz2
                context = {'arz': arz, 'arz2': arz2, 'arz3': arz3, 'datagot': inputdate}

                template = loader.get_template('Admin/Result.html')
                # returing the template
                return HttpResponse(template.render(context, request))

            if (Productid == 2):  # load 2nd product budget
                df = pd.read_csv('Data/Purchasefile.csv')
                df = df.drop(
                    ['CALTEX_Delo_Gold_Ultra', 'Price1', 'Substitude_Product_Price1', 'Production_cost_per_unit1',
                     'CALTEX_Texamatic_1888_Transmision_Oil', 'Price3', 'Substitude_Product_Price3',
                     'Production_cost_per_unit3'], axis=1)
                df.head()
                df.columns = ["Month", "VIC_Hino_Fuel_Filter", "Price2", "Substitude_Product_Price2",
                              "Production_cost_per_unit2"]

                df['Month'] = pd.to_datetime(df['Month'])
                df.set_index('Month', inplace=True)

                from statsmodels.tsa.stattools import adfuller
                test_result = adfuller(df['VIC_Hino_Fuel_Filter'])
                test_result2 = adfuller(df['Price2'])

                def adfuller_test(VIC_Hino_Fuel_Filter):
                    result = adfuller(VIC_Hino_Fuel_Filter)
                    labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                    for value, label in zip(result, labels):
                        print(label + ' : ' + str(value))

                def adfuller_test2(Price2):
                    result2 = adfuller(Price2)
                    labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                    for value, label in zip(result2, labels):
                        print(label + ' : ' + str(value))

                adfuller_test(df['VIC_Hino_Fuel_Filter'])
                adfuller_test2(df['Price2'])

                df['Product1 First Difference'] = df['VIC_Hino_Fuel_Filter'] - df['VIC_Hino_Fuel_Filter'].shift(1)
                df['Product1 First Difference2'] = df['Price2'] - df['Price2'].shift(1)

                df['Seasonal First Difference'] = df['VIC_Hino_Fuel_Filter'] - df['VIC_Hino_Fuel_Filter'].shift(12)
                df['Seasonal First Difference2'] = df['Price2'] - df['Price2'].shift(12)

                adfuller_test(df['Seasonal First Difference'].dropna())
                adfuller_test2(df['Seasonal First Difference2'].dropna())

                import statsmodels.api as sm
                from statsmodels.tsa.arima.model import ARIMA

                model = ARIMA(df['VIC_Hino_Fuel_Filter'], order=(1, 0, 0))
                model2 = ARIMA(df['Price2'], order=(1, 0, 0))
                model_fit = model.fit()
                model_fit2 = model2.fit()

                df['forecast'] = model_fit.predict(start=104, end=120, dynamic=True)
                df['forecastPrice'] = model_fit2.predict(start=104, end=120, dynamic=True)
                model = sm.tsa.statespace.SARIMAX(df['VIC_Hino_Fuel_Filter'], order=(1, 1, 1),
                                                  seasonal_order=(1, 1, 1, 12))
                model2 = sm.tsa.statespace.SARIMAX(df['Price2'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))

                results = model.fit()
                results2 = model2.fit()

                from pandas.tseries.offsets import DateOffset
                future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)]
                future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns)
                # future_datest_df.tail()
                future_df = pd.concat([df, future_datest_df])

                inputdatex = myForm.cleaned_data['inputdate']
                inputdate = datetime.strftime(inputdatex, "%m/%d/%Y")

                lastDate = '07/01/2020'
                inputdate = inputdate
                date_format = "%m/%d/%Y"
                a = datetime.strptime(lastDate, date_format)  # user input date
                b = datetime.strptime(inputdate, date_format)
                delta = b - a
                delta2 = delta.days
                # print(delta2)
                forcasting_date = int(delta2 / 30)
                newDate = forcasting_date + 102  # ending number

                forecast = future_df['forecast'] = results.predict(start=104, end=newDate, dynamic=True)
                forecastPrice = future_df['forecastPrice'] = results2.predict(start=104, end=newDate, dynamic=True)

                df3 = forecast[-1]
                df4 = forecastPrice[-1]
                print(int(df3))
                print(int(df4))

                arz = int(df3)
                arz2 = int(df4)
                arz3 = arz * arz2
                context = {'arz': arz, 'arz2': arz2, 'arz3': arz3, 'datagot': inputdate}
                template = loader.get_template('Admin/Result.html')
                return HttpResponse(template.render(context, request))

            if (Productid == 3):  # load 3rd product budget
                df = pd.read_csv('Data/Purchasefile.csv')
                df = df.drop(
                    ['CALTEX_Delo_Gold_Ultra', 'Price1', 'Substitude_Product_Price1', 'Production_cost_per_unit1',
                     'VIC_Hino_Fuel_Filter', 'Price2', 'Substitude_Product_Price2', 'Production_cost_per_unit2'],
                    axis=1)
                df.head()
                df.columns = ["Month", "CALTEX_Texamatic_1888_Transmision_Oil", "Price3", "Substitude_Product_Price3",
                              "Production_cost_per_unit3"]

                df['Month'] = pd.to_datetime(df['Month'])
                df.set_index('Month', inplace=True)

                from statsmodels.tsa.stattools import adfuller
                test_result = adfuller(df['CALTEX_Texamatic_1888_Transmision_Oil'])
                test_result2 = adfuller(df['Price3'])

                def adfuller_test(CALTEX_Texamatic_1888_Transmision_Oil):
                    result = adfuller(CALTEX_Texamatic_1888_Transmision_Oil)
                    labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                    for value, label in zip(result, labels):
                        print(label + ' : ' + str(value))

                def adfuller_test2(Price3):
                    result2 = adfuller(Price3)
                    labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                    for value, label in zip(result2, labels):
                        print(label + ' : ' + str(value))

                adfuller_test(df['CALTEX_Texamatic_1888_Transmision_Oil'])
                adfuller_test2(df['Price3'])

                df['Product1 First Difference'] = df['CALTEX_Texamatic_1888_Transmision_Oil'] - df[
                    'CALTEX_Texamatic_1888_Transmision_Oil'].shift(1)
                df['Product1 First Difference2'] = df['Price3'] - df['Price3'].shift(1)

                df['Seasonal First Difference'] = df['CALTEX_Texamatic_1888_Transmision_Oil'] - df[
                    'CALTEX_Texamatic_1888_Transmision_Oil'].shift(12)
                df['Seasonal First Difference2'] = df['Price3'] - df['Price3'].shift(12)

                adfuller_test(df['Seasonal First Difference'].dropna())
                adfuller_test2(df['Seasonal First Difference2'].dropna())
                import statsmodels.api as sm

                from statsmodels.tsa.arima.model import ARIMA
                model = ARIMA(df['CALTEX_Texamatic_1888_Transmision_Oil'], order=(1, 0, 0))
                model2 = ARIMA(df['Price3'], order=(1, 0, 0))
                model_fit = model.fit()
                model_fit2 = model2.fit()

                df['forecast'] = model_fit.predict(start=104, end=120, dynamic=True)
                df['forecastPrice'] = model_fit2.predict(start=104, end=120, dynamic=True)
                model = sm.tsa.statespace.SARIMAX(df['CALTEX_Texamatic_1888_Transmision_Oil'], order=(1, 1, 1),
                                                  seasonal_order=(1, 1, 1, 12))
                model2 = sm.tsa.statespace.SARIMAX(df['Price3'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))

                results = model.fit()
                results2 = model2.fit()

                from pandas.tseries.offsets import DateOffset
                future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)]
                future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns)
                # future_datest_df.tail()
                future_df = pd.concat([df, future_datest_df])

                inputdatex = myForm.cleaned_data['inputdate']
                inputdate = datetime.strftime(inputdatex, "%m/%d/%Y")

                lastDate = '07/01/2020'
                inputdate = inputdate
                date_format = "%m/%d/%Y"
                a = datetime.strptime(lastDate, date_format)  # user input date
                b = datetime.strptime(inputdate, date_format)
                delta = b - a
                delta2 = delta.days
                # print(delta2)
                forcasting_date = int(delta2 / 30)
                newDate = forcasting_date + 102  # ending number

                forecast = future_df['forecast'] = results.predict(start=104, end=newDate, dynamic=True)
                forecastPrice = future_df['forecastPrice'] = results2.predict(start=104, end=newDate, dynamic=True)

                df3 = forecast[-1]
                df4 = forecastPrice[-1]
                print(int(df3))
                print(int(df4))

                arz = int(df3)
                arz2 = int(df4)
                arz3 = arz * arz2
                context = {'arz': arz, 'arz2': arz2, 'arz3': arz3, 'datagot': inputdate}
                template = loader.get_template('Admin/Result.html')
                return HttpResponse(template.render(context, request))
    else:
        form = MyForm4()
    # returning form
    return render(request, 'Admin/Forecasting.html', {'form': form});
# end Admin ML
# End Demand Forecasting Machine Learning Model

# start Admin ML
def HighestDemand(request):
     if request.method == 'POST':
        myForm = MyFormNew(request.POST)
        if myForm.is_valid():
            df = pd.read_csv('Data/Purchasefile.csv')
            df = df.drop(['Price1','Price2','Price3'], axis=1)
            df.head()
                ## Cleaning up the data
            df.columns = ["Month", "CALTEX_Delo_Gold_Ultra", "Substitude_Product_Price1",
                          "Production_cost_per_unit1", "VIC_Hino_Fuel_Filter",
                          "Substitude_Product_Price2", "Production_cost_per_unit2",
                          "CALTEX_Texamatic_1888_Transmision_Oil", "Substitude_Product_Price3",
                          "Production_cost_per_unit3"]
            rrr = df.head()

                # Convert Month into Datetime
            df['Month'] = pd.to_datetime(df['Month'])
            df.set_index('Month', inplace=True)
            df.describe()
            from statsmodels.tsa.stattools import adfuller
            test_result = adfuller(df['CALTEX_Delo_Gold_Ultra'])
            test_result2 = adfuller(df['VIC_Hino_Fuel_Filter'])
            test_result2 = adfuller(df['CALTEX_Texamatic_1888_Transmision_Oil'])

            def adfuller_test(CALTEX_Delo_Gold_Ultra):
                result = adfuller(CALTEX_Delo_Gold_Ultra)
                labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                for value, label in zip(result, labels):
                    print(label + ' : ' + str(value))

            def adfuller_test2(VIC_Hino_Fuel_Filter):
                result2 = adfuller(VIC_Hino_Fuel_Filter)
                labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                for value, label in zip(result2, labels):
                    print(label + ' : ' + str(value))

            def adfuller_test3(CALTEX_Texamatic_1888_Transmision_Oil):
                result3 = adfuller(CALTEX_Texamatic_1888_Transmision_Oil)
                labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                for value, label in zip(result3, labels):
                    print(label + ' : ' + str(value))




            adfuller_test(df['CALTEX_Delo_Gold_Ultra'])
            adfuller_test2(df['VIC_Hino_Fuel_Filter'])
            adfuller_test3(df['CALTEX_Texamatic_1888_Transmision_Oil'])
            df['Seasonal First Difference'] = df['CALTEX_Delo_Gold_Ultra'] - df['CALTEX_Delo_Gold_Ultra'].shift(1)
    #        df['Product1 First Difference2'] = df['Price1'] - df['Price1'].shift(1)

            df['Seasonal First Difference2'] = df['VIC_Hino_Fuel_Filter'] - df['VIC_Hino_Fuel_Filter'].shift(12)
   #         df['Seasonal First Difference2'] = df['Price1'] - df['Price1'].shift(12)

            df['Seasonal First Difference3'] = df['CALTEX_Texamatic_1888_Transmision_Oil'] - df['CALTEX_Texamatic_1888_Transmision_Oil'].shift(12)
   #         df['Seasonal First Difference2'] = df['Price1'] - df['Price1'].shift(12)

            adfuller_test(df['Seasonal First Difference'].dropna())
            adfuller_test2(df['Seasonal First Difference2'].dropna())
            adfuller_test3(df['Seasonal First Difference3'].dropna())
            import statsmodels.api as sm

            from statsmodels.tsa.arima.model import ARIMA
            model = ARIMA(df['CALTEX_Delo_Gold_Ultra'], order=(1, 0, 0))
            model2 = ARIMA(df['VIC_Hino_Fuel_Filter'], order=(1, 0, 0))
            model3 = ARIMA(df['CALTEX_Texamatic_1888_Transmision_Oil'], order=(1, 0, 0))
            model_fit = model.fit()
            model_fit2 = model2.fit()
            model_fit3 = model3.fit()

            df['forecast'] = model_fit.predict(start=104, end=120, dynamic=True)
            df['forecastP2'] = model_fit2.predict(start=104, end=120, dynamic=True)
            df['forecast3'] = model_fit3.predict(start=104, end=120, dynamic=True)
                # df[['Product1','forecast']].plot(figsize=(12,8))
            model = sm.tsa.statespace.SARIMAX(df['CALTEX_Delo_Gold_Ultra'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
            model2 = sm.tsa.statespace.SARIMAX(df['VIC_Hino_Fuel_Filter'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
            model3 = sm.tsa.statespace.SARIMAX(df['CALTEX_Texamatic_1888_Transmision_Oil'], order=(1, 1, 1),
                                               seasonal_order=(1, 1, 1, 12))

            results = model.fit()
            results2 = model2.fit()
            results3 = model3.fit()

            from pandas.tseries.offsets import DateOffset
            future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)]
            future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns)

            future_df = pd.concat([df, future_datest_df])


            inputdatex = myForm.cleaned_data['inputdate']
            inputdate = datetime.strftime(inputdatex, "%m/%d/%Y")

            lastDate = '07/01/2020'

            inputdate = inputdate
            date_format = "%m/%d/%Y"
            a = datetime.strptime(lastDate, date_format)  # user input date
            b = datetime.strptime(inputdate, date_format)
            delta = b - a
            delta2 = delta.days
                # print(delta2)
            forcasting_date = int(delta2 / 30)
            newDate = forcasting_date + 102 # ending number

                #   print(forcasting_date)
            forecast = future_df['forecast'] = results.predict(start=104, end=newDate, dynamic=True)

            forecastP2 = future_df['forecastP2'] = results2.predict(start=104, end=newDate, dynamic=True)
            forecastP3 = future_df['forecastP3'] = results3.predict(start=104, end=newDate, dynamic=True)


            df3 = forecast[-1] # 1st product
            df4 = forecastP2[-1] # 2nd product
            df5 = forecastP3[-1] # 3rd product
            print (int(df3))
            print(int(df4))
            print(int(df5))

            maxvalue = max(df3, df4, df5)


            # arz = int(maxvalue)

            if (maxvalue == df3 ):
                arz = "CALTEX_Delo_Gold_Ultra"
            if (maxvalue == df4 ):
                arz = "VIC_Hino_Fuel_Filter"
            if (maxvalue == df5):
                arz = "CALTEX_Texamatic_1888_Transmision_Oil"

            context = { 'arz': arz ,  'datagot': inputdate }

            template = loader.get_template('Admin/Result2.html')
                    #returing the template
            return HttpResponse(template.render(context, request))

     else:
         form = MyFormNew()
     #returning form
     return render(request, 'Admin/HighestDemand.html', {'form':form});
# end Admin ML

# Customer Data
def Customer_Data(request):
    data = pd.read_csv('Data/CustomerDatasetNew_4.csv')
    finalpt = data.head(500)
    json_records = finalpt.reset_index().to_json(orient='records')
    arr = []
    arr = json.loads(json_records)
    context = {'d': arr}
    return render(request, 'Admin/Customer_Data.html',context);

# Customer Frequency
def Customer_Frequency(request):
    if request.method == 'POST':
        myForm = MyForm(request.POST)
        if myForm.is_valid():
            # Loading the data
            df = pd.read_csv('Data/CustomerDatasetNew_4.csv')

            # Drop the missing data in dataframe
            df_data = df.dropna()

            # Convert the date field, InvoiceDate to datetime object
            df_data.InvoiceDate = pd.to_datetime(df_data.InvoiceDate)

            ctm_bhvr_dt = df_data[(df_data.InvoiceDate < pd.Timestamp(2021, 12, 1))  # dataset End date
                                  & (df_data.InvoiceDate >= pd.Timestamp(2021, 5, 1))].reset_index(
                drop=True)  # dataset start date

            ctm_next_quarter = df_data[(df_data.InvoiceDate < pd.Timestamp(2022, 3, 1))  # predict date Till
                                       & (df_data.InvoiceDate >= pd.Timestamp(2021, 12, 1))].reset_index(
                drop=True)  # dataset End date

            # Get the distinct customers in the dataframe ctm_bhvr_dt
            ctm_dt = pd.DataFrame(ctm_bhvr_dt['CustomerID'].unique())

            # Rename the column to CustomerID.
            ctm_dt.columns = ['CustomerID']

            # Create a dataframe with CustomerID and customers first purchase
            ctm_1st_purchase_in_next_quarter = ctm_next_quarter.groupby('CustomerID').InvoiceDate.min().reset_index()
            ctm_1st_purchase_in_next_quarter.columns = ['CustomerID', 'MinPurchaseDate']

            # Create a dataframe with CustomerID and customers last purchase
            ctm_last_purchase_bhvr_dt = ctm_bhvr_dt.groupby('CustomerID').InvoiceDate.max().reset_index()
            ctm_last_purchase_bhvr_dt.columns = ['CustomerID', 'MaxPurchaseDate']

            # Merge two dataframes
            ctm_purchase_dates = pd.merge(ctm_last_purchase_bhvr_dt, ctm_1st_purchase_in_next_quarter, on='CustomerID',
                                          how='left')

            # Get the difference in days from MinPurchaseDate and MaxPurchaseDate for each customer
            ctm_purchase_dates['NextPurchaseDay'] = (
                    ctm_purchase_dates['MinPurchaseDate'] - ctm_purchase_dates['MaxPurchaseDate']).dt.days

            # Update the dataframe ctm_dt by merging it with the NextPurchaseDay column of the dataframe ctm_purchase_dates
            ctm_dt = pd.merge(ctm_dt, ctm_purchase_dates[['CustomerID', 'NextPurchaseDay']], on='CustomerID',
                              how='left')
            # Fill all missing values in the dataset ctm_dt with the number 9999
            ctm_dt = ctm_dt.fillna('Will not return')

            # Feature Engineering
            # Recency
            # Get the maximum purchase date of each customer and create a dataframe with it together with the customer's id.
            ctm_max_purchase = ctm_bhvr_dt.groupby('CustomerID').InvoiceDate.max().reset_index()
            ctm_max_purchase.columns = ['CustomerID', 'MaxPurchaseDate']

            # Find the recency of each customer in days
            ctm_max_purchase['Recency'] = (
                    ctm_max_purchase['MaxPurchaseDate'].max() - ctm_max_purchase['MaxPurchaseDate']).dt.days

            # Find the recency in days
            ctm_max_purchase['Recency'] = (
                    ctm_max_purchase['MaxPurchaseDate'].max() - ctm_max_purchase['MaxPurchaseDate']).dt.days

            # Merge the dataframes ctm_dt and ctm_max_purchase[['CustomerID', 'Recency']] on the CustomerID column.
            ctm_dt = pd.merge(ctm_dt, ctm_max_purchase[['CustomerID', 'Recency']], on='CustomerID')
            Recency = ctm_dt.head()

            # Frequency
            # Get order counts for each user and create a dataframe with it
            ctm_frequency = df_data.groupby('CustomerID').InvoiceDate.count().reset_index()
            ctm_frequency.columns = ['CustomerID', 'Frequency']

            # Update the dataframe ctm_dt by merging it with the dataframe ctm_frequency
            ctm_dt = pd.merge(ctm_dt, ctm_frequency, on='CustomerID')
            Frequency = ctm_dt.head()
            print(Frequency)
            df_data['Revenue'] = df_data.ServicePrice

            # df_data['Revenue'] = df_data.UnitPrice * df_data.Quantity
            # Get the revenue from each customer and sum them.
            ctm_revenue = df_data.groupby('CustomerID').Revenue.sum() / df_data.groupby(
                'CustomerID').InvoiceDate.count()
            totalALlcustomervalue = ctm_revenue.sum()
            singlecustoemrvalue = ctm_revenue

            # ML Section Start
            pntge = (singlecustoemrvalue / totalALlcustomervalue) * 100
            sss = pd.DataFrame(pntge, columns=['persontageImpact'])
            xxx = sss.head()
            print(xxx)
            ctm_dt = pd.merge(ctm_dt, sss, on='CustomerID')
            Frequency = ctm_dt.head()
            df2 = pd.read_csv('Data/CustomerDatasetNew_4.csv')
            ctm_dt = pd.merge(ctm_dt, df2, on='CustomerID')

            df0 = pd.read_csv('Data/CustomerDatasetNew_4.csv')
            df0 = df0.groupby('CustomerID')[
                'ServicePrice', 'ServiceQualityRating', 'ResponseTimeRating', 'CustomerServiceRating'].sum()
            df0 = pd.merge(df0, Frequency, on='CustomerID')
            print(df0)
            x = df0[['ServicePrice', 'ServiceQualityRating', 'ResponseTimeRating', 'CustomerServiceRating']]
            y = df0[['Frequency']]
            # split dataset for train and test
            x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)
            # user LinearRegression

            from sklearn.linear_model import LinearRegression
            lm = LinearRegression()
            lm.fit(x_train, y_train)

            customerid = myForm.cleaned_data['customerid']
            dfz = ctm_dt[(ctm_dt["CustomerID"] == customerid)]
            Frequency = dfz.head(1)
            print(Frequency)
            df = Frequency
            json_records = df.reset_index().to_json(orient='records')
            arr = json.loads(json_records)
            form = MyForm()
            context = {'form': form, 'd': arr}
            return render(request, 'Admin/Customer_Frequency.html', context)

    else:
        form = MyForm()
    return render(request, 'Admin/Customer_Frequency.html', {'form': form});


# tensorflow sound detection model
# import tensorflow as tf
# import tensorflow_io as tfio
#
# import tensorflow as tf
# import tensorflow_io as tfio
#
# audio = tfio.audio.AudioIOTensor('gs://clouds-sample-tests/speech/brooklyn.flac')
#
# print(audio)
#
#
# # remove last dimension
# audio_tensor = tf.squeeze(audio_slice, axis=[-1])
#
# print(audio_tensor)
#
# from IPython.display import Audio
#
# Audio(audio_tensor.numpy(), rate=audio.rate.numpy())
#
#
# import matplotlib.pyplot as plt
#
#
# tensor = tf.cast(audio_tensor, tf.float32) / 32768.0
#
# plt.figure()
# plt.plot(tensor.numpy())


# pyaudio sound detection model
# import numpy as np
#
# import warnings
# warnings.filterwarnings("ignore")
#
# from pyaudioclassification import feature_extraction, train, predict, print_leaderboard
#
# parent_dir = '.'
#
#
# if np.DataSource().exists("./feat.npy") and np.DataSource().exists("./label.npy"):
#     features, labels = np.load('./feat.npy'), np.load('./label.npy')
# else:
#     features, labels = feature_extraction('./data/')
#     np.save('./feat.npy', features)
#     np.save('./label.npy', labels)
#
#
# if np.DataSource().exists("./model.h5"):
#     from keras.models import load_model
#     model = load_model('./model.h5')
# else:
#     model = train(features, labels, epochs=100)
#     model.save('./model.h5')


#Next Maintain Date

def NextMaintain(request):
    if request.method == 'POST':
        myForm = MyFormServices(request.POST)
        #myForm3 = MyForm3(request.POST or None)

#Next Service Date Prediction with logistic Regression
        if myForm.is_valid():

            customerid = myForm.cleaned_data['customerid']
            checkValid = pd.read_csv('Data/MaintainData.csv')
            checkValid = checkValid[(checkValid["Customer"] == customerid)]
            print("Validation----------------------")
            checkValid = (checkValid.shape[0])

            if (checkValid > 0):

                ServiceType = myForm.cleaned_data['ServiceType']
                customerid = myForm.cleaned_data['customerid']
                Vnumber = myForm.cleaned_data['Vnumber']

                df = pd.read_csv('Data/MaintainData.csv')
                df = df[(df["Customer"] == customerid)]
                df = df[(df["Service"] == ServiceType)]
                df = df[(df["VehicleNumber"] == Vnumber)]

                df.shape
                dataset = df.head()
                # print(dataset)
                # do same but attach it to the dataframe
                df['a'] = pd.to_datetime(df['Date']).diff()
                df['a'] = df['a'].astype(str).str.replace("days", "")
                df['a'] = df['a'].astype(str).str.replace("NaT", "30")
                print(df)

                X = df[['TakenTime', 'Price', 'MeterReading']]
                y = df['a']

                X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

                logreg = LogisticRegression()
                logreg.fit(X_train, y_train)
                Accuracy = logreg.score(X_test, y_test)
                print(Accuracy)

                MeterReading = df['MeterReading'].mean()
                # print(MeterReading)

                new_input = [[28, 200, MeterReading]]
                pred = logreg.predict(new_input)
                pred = int(pred)
                print("--------------Customer will come after days---------------------")
                # print(pred)
                startdate = "10/31/2021"
                enddate = pd.to_datetime(startdate) + pd.DateOffset(days=pred)
                print(enddate)


                df = df[:1]
                json_records = df.reset_index().to_json(orient='records')
                arr = []
                arr = json.loads(json_records)
                if (arr == [] ):
                    json_records = df.reset_index().to_json(orient='records')
                    arr = []
                    arr = json.loads(json_records)

                else:
                    a = 2
                form = MyFormServices()

                enddate = enddate.strftime("%d/%m/%Y")

#Price Prediction with ARIMA

                df2 = pd.read_csv('Data/MaintainData.csv')
                df2 = df2[(df2["Customer"] == customerid)]
                df2 = df2[(df2["Service"] == ServiceType)]
                df = df2[(df2["VehicleNumber"] == Vnumber)]

                #    df.drop('Service','ServiceName','TakenTime','Customer','VehicleType','VehicleMake','VehicleModel','VehicleNumber','MeterReading','VehicleManufacturedYear','AvergeUsage','mailID', axis=1, inplace=True)
                df = df.drop(columns=['Service', 'ServiceName', 'TakenTime', 'Customer', 'VehicleType', 'VehicleMake',
                                      'VehicleModel', 'VehicleNumber', 'MeterReading', 'VehicleManufacturedYear',
                                      'AvergeUsage', 'mailID', ])
                df.head()
                ## Cleaning up the data
                df.columns = ["Date", "Price"]
                rrr = df.head()

                # Convert Date into Datetime
                df['Date'] = pd.to_datetime(df['Date'])
                df.head()
                df.set_index('Date', inplace=True)
                df.head()
                df.describe()
                # df.plot()
                # plt.show()
                ##Testing For Stationarity

                from statsmodels.tsa.stattools import adfuller
                test_result = adfuller(df['Price'])

                def adfuller_test(Price):
                    result = adfuller(Price)
                    labels = ['ADF Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used']
                    for value, label in zip(result, labels):
                        print(label + ' : ' + str(value))
                    if result[1] <= 0.05:
                        print(
                            "strong evidence against the null hypothesis(Ho), reject the null hypothesis. Data has no unit root and is stationary")
                    else:
                        print(
                            "weak evidence against null hypothesis, time series has a unit root, indicating it is non-stationary ")

                adfuller_test(df['Price'])
                #Differencing
                df['Price First Difference'] = df['Price'] - df['Price'].shift(1)
                df['Price'].shift(1)
                df['Seasonal First Difference'] = df['Price'] - df['Price'].shift(12)

                #Again test dickey fuller test
                adfuller_test(df['Seasonal First Difference'].dropna())
                import statsmodels.api as sm
                from statsmodels.tsa.arima.model import ARIMA
                model = ARIMA(df['Price'], order=(1, 1, 1))
                model_fit = model.fit()
                model = sm.tsa.statespace.SARIMAX(df['Price'], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
                results = model.fit()

                from pandas.tseries.offsets import DateOffset
                future_dates = [df.index[-1] + DateOffset(months=x) for x in range(0, 150)]
                future_datest_df = pd.DataFrame(index=future_dates[1:], columns=df.columns)
                future_df = pd.concat([df, future_datest_df])


                Datefinding = int(df.shape[1] + 15)

                forecastedPrice = future_df['forecast'] = results.predict(start=Datefinding, end=Datefinding, dynamic=True)
                print("Arima output - Servport price prediction")

                forecastedPrice = str(forecastedPrice.to_list()[0])
                forecastedPrice = int(float(forecastedPrice))
                forecastedPrice = str(forecastedPrice)
                print(forecastedPrice)


            else:
                form = MyFormServices()
                context = {'form': form, 'd': "CUSTOMER NOT FOUND"}
                return render(request, 'Admin/NextMaintain.html', context)

            context = {'form': form, 'd': enddate, 'forecastedPrice': ' Rs ' + forecastedPrice}
            return render(request, 'Admin/NextMaintain.html', context)

    else:
        form = MyFormServices()


    return render(request, 'Admin/NextMaintain.html', {'form': form });


def MaintainData(request):
    data1 = pd.read_csv('Data/MaintainData.csv')
    finalpt = data1.head(518)
    json_records = finalpt.reset_index().to_json(orient='records')
    arr = []
    arr = json.loads(json_records)
    context = {'d': arr}
    return render(request, 'Admin/MaintainData.html',context);




def AdminLogin(request):
    if request.method == 'POST':
        myForm = MyForm2(request.POST)
        if myForm.is_valid():
            UserName = myForm.cleaned_data['UserName']
            Password = myForm.cleaned_data['Password']
            gotdata = Umodels.loginDataAdmin.objects.all().filter(UserName=UserName , Password=Password).count()
            if gotdata > 0:
                return render(request, 'Admin/AdminHome.html');
            else:
                messages.success(request, 'Login Info Not Found  , Please Check Your UserName and Password')
                return HttpResponseRedirect(request.path_info)
    else:
        form = MyForm2()
    return render(request, 'Admin/AdminLogin.html', {'form':form});


def AdminHome(request):
    return render(request,'Admin/AdminHome.html')






