from django.core.management.base import BaseCommand, CommandError
from Item.models import Item
import pandas as pd
import numbers as np

class Command(BaseCommand):
    help = "Closes the specified poll for voting"
    def handle(self, *args, **options):
            print("fill Items command")

            # return
            # if request.method == 'POST' and request.FILES['myfile']:      
            #     myfile = request.FILES['myfile']
            #     fs = FileSystemStorage()
            filename = "Item/management/commands/Iteminventory20230417.xls"
            # filename = "Item/management/commands/Book2.xlsx"
            # filename = fs.save(myfile.name, myfile)
            # uploaded_file_url = fs.url(filename)              
            empexceldata = pd.read_excel(filename, converters={'ReorderPoint':int},  )

            dbframe = empexceldata.replace("NaN", "")
            dbframe = dbframe.fillna("")
            print(dbframe['PurchaseCost'])
            dbframe['PurchaseCost'] = dbframe['PurchaseCost'].apply(pd.to_numeric, downcast='float', errors='coerce')
            dbframe['AverageCost'] = dbframe['AverageCost'].apply(pd.to_numeric, downcast='float', errors='coerce')
            # dbframe['Reorderpoint'] = dbframe['ReorderPoint'].apply(pd.to_numeric, downcast='float', errors='coerce')
            # dbframe['ReorderPoint'] = dbframe['ReorderPoint'].astype(int)
            print (dbframe)
            dbframe['PurchaseCost'] = dbframe['PurchaseCost'].fillna(0)
            dbframe['AverageCost'] = dbframe['AverageCost'].fillna(0)
            print(dbframe['PurchaseCost'])
            # print(dbframe['NameFromTaco'])
            # print(dbframe['ReorderPoint'])
            print(dbframe['QuantityOnHand'])
            print (dbframe.dtypes)
            icount=0
            for db in dbframe.itertuples():
                obj, created = Item.objects.get_or_create(FullName=db.FullName)
                print(obj.FullName, obj.pk)
                # if created:
                obj.ItemType=1
                obj.Name=db.Name
                # obj.FullName=db.FullName
                # obj.# BarCodeValue=db.# BarCodeValue
                obj.SalesDesc=db.SalesDesc
                # obj.# SalesPrice=db.# SalesPrice
                obj.PurchaseDesc=db.PurchaseDesc
                obj.Sublevel=db.Sublevel
                obj.ManufacturerPartNumber=db.ManufacturerPartNumber
                obj.PurchaseCost=db.PurchaseCost
                # obj.ReorderPoint=db.ReorderPoint
                obj.QuantityOnHand=db.QuantityOnHand
                obj.AverageCost=db.AverageCost
                obj.QuantityOnOrder=db.QuantityOnOrder
                obj.QuantityOnSalesOrder=db.QuantityOnSalesOrder
                # obj.# UnitOfMeasureSetRefFullName=db.# UnitOfMeasureSetRefFullName
                obj.ParentRefFullName=db.ParentRefFullName
                # obj.# SalesTaxCodeRefFullName=db.# SalesTaxCodeRefFullName
                obj.IncomeAccountRefFullName=db.IncomeAccountRefFullName
                obj.COGSAccountRefFullName=db.COGSAccountRefFullName
                obj.PrefVendorRefFullName=db.PrefVendorRefFullName
                obj.AssetAccountRefFullName=db.AssetAccountRefFullName
                obj.IsActive=db.IsActive
                obj.NameFromTaco=db.NameFromTaco
                obj.CATEGORY=db.CATEGORY
                obj.Type=db.Type

                # obj = Item.objects.create(Name	=	db.Name,
                #             FullName	=	db.FullName,
                #             # BarCodeValue	=	db.# BarCodeValue,
                #             SalesDesc	=	db.SalesDesc,
                #             # SalesPrice	=	db.# SalesPrice,
                #             PurchaseDesc	=	db.PurchaseDesc,
                #             Sublevel	=	db.Sublevel,
                #             ManufacturerPartNumber	=	db.ManufacturerPartNumber,
                #             PurchaseCost	=	db.PurchaseCost,
                #             # ReorderPoint	=	db.ReorderPoint,
                #             QuantityOnHand	=	db.QuantityOnHand,
                #             AverageCost	=	db.AverageCost,
                #             QuantityOnOrder	=	db.QuantityOnOrder,
                #             QuantityOnSalesOrder	=	db.QuantityOnSalesOrder,
                #             # UnitOfMeasureSetRefFullName	=	db.# UnitOfMeasureSetRefFullName,
                #             ParentRefFullName	=	db.ParentRefFullName,
                #             # SalesTaxCodeRefFullName	=	db.# SalesTaxCodeRefFullName,
                #             IncomeAccountRefFullName	=	db.IncomeAccountRefFullName,
                #             COGSAccountRefFullName	=	db.COGSAccountRefFullName,
                #             PrefVendorRefFullName	=	db.PrefVendorRefFullName,
                #             AssetAccountRefFullName	=	db.AssetAccountRefFullName,
                #             IsActive	=	db.IsActive,
                #             NameFromTaco	=	db.NameFromTaco,
                #             CATEGORY	=	db.CATEGORY,
                #             Type	=	db.Type,
                # )
                obj.save()
                icount+=1
            print(f"{icount} Records updated/added")



            # return render(request, 'Import_excel_db.html', {
            #     'uploaded_file_url': uploaded_file_url
            # })   
            # return render(request, 'Import_excel_db.html',{})