def UodateSize(request):
    aResponse = ResultResponse()
    received_json_data = json.loads(request.body.decode("utf-8"))
    theRequest = request.POST.copy()
    theRequest.__mutable = True
    theRequest.POST = received_json_data

    sizeName = received_json_data.get('ItemSizeName')

    active = received_json_data.get('IsActive')

    sizeId = received_json_data.get('id')
    aResponse = ResultResponse()

    aSize = ItemSize.objects.get(id=int(sizeId))
    masterForm = ItemSizeForm(theRequest.POST, instance=aSize)
    if masterForm.is_valid():

        aSize.ItemSizeName = sizeName
        aSize.UpdateBy = 'admin'
        aSize.IsActive = active
        aSize.save()
        aResponse.IsSuccess = True

        return JsonResponse(json.dumps(aResponse.__dict__), safe=False)
    else:
        appError = AppError()

        template = render_to_string('BasicSettings/_sizeForm.html',
                                    {'masterForm': masterForm})
        appError.TheData = str(template)
        return JsonResponse(json.dumps(appError.__dict__), safe=False)
def addUomDetailsFormRow(request):
    aResponse = ResultResponse()
    received_json_data = json.loads(request.body.decode("utf-8"))
    theRequest = request.POST.copy()
    theRequest.__mutable = True
    theRequest.POST = received_json_data
    trCount = received_json_data.get('trCount')

    detailForm = UomDetailsForm(theRequest.POST)

    if detailForm.is_valid():
        aResponse.IsSuccess = True
        context = {'detailForm': detailForm, 'trCount': trCount}
        template = render_to_string('BasicSettings/_uomDetailsFromRow.html',
                                    context)
        aResponse.TheData = str(template)
        #return HttpResponse(template)
        return JsonResponse(json.dumps(aResponse.__dict__), safe=False)
    else:
        appError = AppError()

        template = render_to_string('BasicSettings/_uomDetailsForm.html',
                                    {'detailForm': detailForm})
        appError.TheData = str(template)
        return JsonResponse(json.dumps(appError.__dict__), safe=False)
def SaveSize(request):
    aResponse = ResultResponse()
    received_json_data = json.loads(request.body.decode("utf-8"))
    theRequest = request.POST.copy()
    theRequest.__mutable = True
    theRequest.POST = received_json_data

    sizeName = received_json_data.get('ItemSizeName')

    active = received_json_data.get('IsActive')

    masterForm = ItemSizeForm(theRequest.POST)
    if masterForm.is_valid():

        size = ItemSize(ItemSizeName=sizeName,
                        IsActive=active,
                        CreateBy='Admin')
        size.save()
        if size.pk > 0:
            aResponse.IsSuccess = True
            aResponse.PK = size.pk
        return JsonResponse(json.dumps(aResponse.__dict__), safe=False)
    else:
        appError = AppError()
        template = render_to_string('BasicSettings/_sizeForm.html',
                                    {'masterForm': masterForm})
        appError.TheData = str(template)
        return JsonResponse(json.dumps(appError.__dict__), safe=False)
def UpdateCategory(request):
    aResponse = ResultResponse()
    received_json_data = json.loads(request.body.decode("utf-8"))
    theRequest = request.POST.copy()
    theRequest.__mutable = True
    theRequest.POST = received_json_data

    group = received_json_data.get('ItemGroup')
    name = received_json_data.get('ItemCategory')
    shortName = received_json_data.get('CategoryShortName')
    active = received_json_data.get('IsActive')
    categoryId = received_json_data.get('id')

    aResponse = ResultResponse()

    category = ItemCategory.objects.get(id=int(categoryId))

    masterForm = ItemCategoryForm(theRequest.POST, instance=category)

    if masterForm.is_valid():
        category.ItemCategory = name
        category.CategoryShortName = shortName
        category.ItemGroup = ItemGroup.objects.get(id=int(group))
        category.IsActive = active
        category.UpdateBy = 'Admin'
        category.save()
        aResponse.IsSuccess = True
        return JsonResponse(json.dumps(aResponse.__dict__), safe=False)
    else:
        appError = AppError()

        template = render_to_string('BasicSettings/_itemCategoryForm.html',
                                    {'masterForm': masterForm})
        appError.TheData = str(template)
        return JsonResponse(json.dumps(appError.__dict__), safe=False)
def SaveProduct(request):
    aResponse = ResultResponse()
    received_json_data = json.loads(request.body.decode("utf-8"))
    theRequest = request.POST.copy()
    theRequest.__mutable = True
    theRequest.POST = received_json_data

    name = received_json_data.get('ProductName')
    description = received_json_data.get('ProductDescripTion')
    country = received_json_data.get('ProductCountry')
    category = received_json_data.get('ProductCategory')
    brand = received_json_data.get('ProductBrand')
    color = received_json_data.get('ProductColor')
    size = received_json_data.get('ProductSize')
    uom = received_json_data.get('ProductUOM')

    masterForm = ProductForm(theRequest.POST)

    if masterForm.is_valid():
        productGenerate = _aProductHelper.GenerateProduct(
            name, description, category, country, brand, uom, color, size)
        finalProd = productGenerate[0]
        productColor = productGenerate[1]
        productsize = productGenerate[2]
        # need to add trunsection process
        finalProd.save()

        if productColor is not None:
            finalProd.ProductColor.add(*productColor)
        if productsize is not None:
            finalProd.ProductSize.add(*productsize)

        finalProd.ProductCode = _aProductHelper.GenerateProductCode(
            category=category, productid=finalProd.pk)
        finalProd.save()
        if finalProd.pk > 0:
            aResponse.IsSuccess = True
        return JsonResponse(json.dumps(aResponse.__dict__), safe=False)
    else:
        appError = AppError()
        template = render_to_string('ProductSetup/_productForm.html',
                                    {'masterForm': masterForm})
        appError.TheData = str(template)
        return JsonResponse(json.dumps(appError.__dict__), safe=False)
def SaveUom(request):
    aResponse = ResultResponse()

    received_json_data = json.loads(request.body.decode("utf-8"))

    details = received_json_data.pop('UomDetails' or None)
    theRequest = request.POST.copy()
    theRequest.__mutable = True
    theRequest.POST = received_json_data

    name = received_json_data.get('UomMasterName')
    shortName = received_json_data.get('UomMasterShortName')
    active = received_json_data.get('IsActive')

    CreateBy = 'admin'

    masterForm = UomMasterForm(theRequest.POST or None)
    if masterForm.is_valid():
        aResponse.IsSuccess = False
        amaster = UomMaster(UomMasterName=name,
                            UomMasterShortName=shortName,
                            IsActive=active,
                            CreateBy=CreateBy)
        with transaction.atomic():
            try:
                amaster.save()
                for theDetails in details:
                    uomDetails = UomDetails.objects.create(
                        UomMaster=amaster,
                        UomDetailsName=theDetails['UomDetailsName'],
                        UomDetailsShortName=theDetails['UomDetailsShortName'],
                        ConversionValue=theDetails['ConversionValue'],
                        EqualToMaster=theDetails['EqualToMaster'],
                        IsActive=True)
                    uomDetails.save()
                aResponse.IsSuccess = True
            except Exception as ex:
                aResponse.IsSuccess = False
                aResponse.ErrorMessage = ex.args
                transaction.set_rollback(True)
        return JsonResponse(json.dumps(aResponse.__dict__), safe=False)

    else:

        appError = AppError()

        template = render_to_string('BasicSettings/_uomMasterForm.html',
                                    {'masterForm': masterForm})
        appError.TheData = str(template)

        return JsonResponse(json.dumps(appError.__dict__), safe=False)
def UpdateUom(request):
    aResponse = ResultResponse()

    received_json_data = json.loads(request.body.decode("utf-8"))

    newDetails = received_json_data.pop('UomDetails' or None)

    theRequest = request.POST.copy()
    theRequest.__mutable = True
    theRequest.POST = received_json_data

    name = received_json_data.get('UomMasterName')
    uomid = received_json_data.get('id')
    shortName = received_json_data.get('UomMasterShortName')
    active = received_json_data.get('IsActive')

    uomMaster = UomMaster.objects.get(pk=int(uomid))
    uomMaster.UomMasterName = name
    uomMaster.UomMasterShortName = shortName
    uomMaster.IsActive = active
    uomMaster.save()

    detailsList = UomDetails.objects.filter(UomMaster=int(uomid))
    updateList = []
    uomDetailsList = []
    for details in detailsList:
        update = UomDetailsUpdateLog(
            UomDetails=details,
            UomMaster=uomMaster,
            UomDetailsName=details.UomDetailsName,
            UomDetailsShortName=details.UomDetailsShortName,
            ConversionValue=details.ConversionValue,
            EqualToMaster=details.EqualToMaster,
            IsActive=details.IsActive,
            CreateBy='admin')

        updateList.append(update)

    for theDetails in newDetails:
        uomDetails = UomDetails.objects.create(
            UomMaster=uomMaster,
            UomDetailsName=theDetails['UomDetailsName'],
            UomDetailsShortName=theDetails['UomDetailsShortName'],
            ConversionValue=theDetails['ConversionValue'],
            EqualToMaster=True
            if theDetails['EqualToMaster'] == "True" else False,
            IsActive=True)
        uomDetailsList.append(uomDetails)
    masterForm = UomMasterEditForm(theRequest.POST, instance=uomMaster)
    if masterForm.is_valid():
        UomDetailsUpdateLog.objects.bulk_create(updateList)
        UomDetails.objects.filter(UomMaster=int(uomid)).delete()
        UomDetails.objects.bulk_create(uomDetailsList)
        aResponse.IsSuccess = True
        return JsonResponse(json.dumps(aResponse.__dict__), safe=False)
    else:

        appError = AppError()

        template = render_to_string('BasicSettings/_uomMasterEditForm.html',
                                    {'masterEditForm': masterForm})
        appError.TheData = str(template)

        return JsonResponse(json.dumps(appError.__dict__), safe=False)