views.py 4.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
import django
from django.db.models.manager import Manager
from pos.models import Bill, BillList, FeaturePic
from django import dispatch
from django.shortcuts import redirect, render
from management.models import EmployeeUser, POSManager, Product
import json
from allauth.account.forms import LoginForm
from django.views.decorators.csrf import csrf_protect
from django.contrib.auth import authenticate,login,logout
from django.contrib.auth.decorators import login_required
from datetime import *
import pytz
# from management.permission_page import allow_by_role 


django.utils.timezone.activate(pytz.timezone('Asia/Bangkok'))
# https://stackoverflow.com/questions/21925671/convert-django-model-object-to-dict-with-all-of-the-fields-intact
from itertools import chain

def to_dict(instance):
    opts = instance._meta
    data = {}
    for f in chain(opts.concrete_fields, opts.private_fields):
        if f.name == 'price' :
            data[f.name] = str(f.value_from_object(instance))
        else:
            data[f.name] = f.value_from_object(instance)
    for f in opts.many_to_many:
        data[f.name] = [i.id for i in f.value_from_object(instance)]
    return data



# Create your views here.
@login_required
# @allow_by_role(roles=['manager','employee'])
def pos_page(req):
    mgr_user = None
    if req.user.groups.filter(name = 'manager').exists():
        mgr_user = POSManager.objects.get(user=req.user)
    else :
        emp_user = EmployeeUser.objects.get(user=req.user)
        mgr_user = POSManager.objects.get(emp_user.create_by)
    if req.method == 'POST':
        list_productId = req.POST.getlist('list_productID')
        list_productQuantity = req.POST.getlist('list_productQuantity')
        
        bill = Bill(
            created=datetime.now(),
            user=req.user,
            manager=mgr_user)
        bill.save()
        for i in range(len(list_productId)):
            iproduct = Product.objects.get(id=list_productId[i])
            iproduct.quantity -= int(list_productQuantity[i])
            iproduct.save()
            BillList(
                product=Product.objects.get(id=list_productId[i]),
                quantity=list_productQuantity[i],
                bill=bill
                ).save()
            return redirect('pos')

    productCategory = Product.objects.filter(manager=mgr_user.user).values_list('category').distinct() 
    productsById = dict()

    productCategory = [i[0] for i in productCategory]

    productDict = {}
    for i in productCategory :
        k = Product.objects.filter(category=i,manager=req.user)

        kl = []
        for j in list(k):
            kl.append(to_dict(j))
            productsById[j.id] = to_dict(j)

        productDict[i]=kl

    return render(req,'pos_page.html',{'productCate':productCategory,'productDict':productDict,'productsById':productsById})

@csrf_protect
def home(req):
    login_form = LoginForm()
    mgr_user = None
    if req.user.groups.filter(name = 'manager').exists():
        try:
            mgr_user = POSManager.objects.get(user=req.user)
        except:
            POSManager(user=req.user).save()
    # if req.user.is_authenticated:
    # # Do something for authenticated users.
    #     print('logining')
    # else:
    #     if req.method == 'POST':
    #         form = LoginForm(req.POST, req.FILES)
    #         if form.is_valid():
    #             print(req.POST)
    #             print(req.POST['login'],req.POST['password'])
                
    #             user = authenticate(username=req.POST['login'], password=req.POST['password'])
    #             if user is not None:
    #                 login(req,user)
    #                 print('login have user')
    #                 # user.lo
    #                 # A backend authenticated the credentials
    #             else:
    #                 print('nope')
    #                 # No backend authenticated the credentials
    #             # username = req.FILES['']
    #             # password = 
    #             # newdoc.save()
    #             # print(request.FILES['docfile'])
    #             # print(newdoc.docfile)
                
    #             # Redirect to the document list after POST
    #             # return redirect('test')
    #         else:
    #             message = 'The form is not valid. Fix the following error:'
    #             print(message)
    #     else:
    #         print('ไม่มีpost')
        
    
    
    #     # Do something for anonymous users.
    featurePic = FeaturePic.objects.all().values_list('urlPic')

    return render(req,'home.html',{'login_form':login_form,'featurePic':featurePic})