first upload to git

parents
Pipeline #1247 failed with stages
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class DashboardConfig(AppConfig):
name = 'dashboard'
from django.db import models
# Create your models here.
This diff is collapsed.
from django.test import TestCase
# Create your tests here.
from re import X
from xlsxwriter import workbook
import management
from time import sleep
from django.shortcuts import redirect, render
from datetime import date, datetime, timedelta
from pos.models import *
from management.models import Product
from django.contrib.auth.decorators import login_required
from management.permission_page import pos_manager_only
import xlsxwriter
import glob
from icecream import ic
# Create your views here.
# https://linuxhint.com/download-the-file-in-django/
import mimetypes
# import os module
import os
# Import HttpResponse module
from django.http.response import HttpResponse
def download_file(request, filename=''):
if filename == '':
filename = 'constFile/example_for_add_product.xlsx'
path = open(filename, 'rb')
# Set the mime type
mime_type, _ = mimetypes.guess_type(filename)
# Set the return value of the HttpResponse
response = HttpResponse(path, content_type=mime_type)
# Set the HTTP header for sending to browser
response['Content-Disposition'] = "attachment; filename=%s" % filename
# Return the response value
return response
@login_required
@pos_manager_only
def dashborad(req):
context=dict()
userlabel = str(req.user.id)+'_'+str(req.user)
print('------------------------------------xlsx file --------------------------------------------------')
for name in glob.glob('*.xlsx'):
print(name)
try:
if userlabel in name:
os.remove(name)
except:
print("can't delete filename:",name)
if req.method == 'POST':
#pick time range
print(req.POST)
context['mode'] = 'post'
else :
strnowtime = datetime.now().strftime("%Y%m%d%H%M%S")
labelfilexlsx = userlabel+'_'+strnowtime
today_filename = 'today_of_'+labelfilexlsx+'.xlsx'
last_week_filename = 'last_week_of_'+labelfilexlsx+'.xlsx'
last_month_filename = 'last_month_of_'+labelfilexlsx+'.xlsx'
context['today_filename'] = today_filename
context['last_week_filename'] = last_week_filename
context['last_month_filename'] = last_month_filename
context['today'] = billlistsNdays(filename=today_filename)
context['week'] = billlistinrange(date.today() - timedelta(days=7),date.today(),last_week_filename)
context['month'] = billlistinrange(date.today() - timedelta(days=30),date.today(),last_month_filename)
return render(req,'dashboard.html',context)
def billlistinrange(start,end,filename=''):
bills = Bill.objects.filter(created__gte=start).filter(created__lt=end)
data = dict()
soldValueInDay = dict()
dataset = dict()
nday = (end-start).days
sumSoldValue = 0
for i in bills:
listsInBill = BillList.objects.filter(bill=i)
datekey = i.created.strftime('%d/%m/%Y')
if soldValueInDay.get(datekey) == None : soldValueInDay[datekey] = 0
for j in listsInBill:
v = j.product.price * j.quantity
soldValueInDay[datekey] += v
sumSoldValue += v
if dataset.get(j.product.id) != None:
# print(dataset.get(j.product.id))
dataset[j.product.id]['sold']+=int(j.quantity)
else :
# id : autoincrement field in db
dataset[j.product.id] = {
'id':j.product.product_id,
'name':j.product.name,
'price':j.product.price,
'sold':j.quantity
}
if filename!='':
workbook = xlsxwriter.Workbook(filename)
sheet_by_day = workbook.add_worksheet('days')
sheet_by_product = workbook.add_worksheet('products')
columns_by_p = [
{'header':'id'},
{'header':'name'},
{'header':'price'},
{'header':'sold'},
{'header':'value'},
{'header':'sold/day'},
{'header':'value/day'},
]
columns_by_d = [
{'header':'day'},
{'header':'value'},
]
product_tableArea = 'A1:G'+str(len(dataset.keys())+1)
sheet_by_product.add_table(product_tableArea,{'columns':columns_by_p,'style':None})
row_counter = 2
for key in dataset.keys():
tmp = dataset[key]
val = tmp['price'] * tmp['sold']
tmp['value'] = val
tmp['soldperday'] = round(tmp['sold']/nday,2)
tmp['valueperday'] = round(val/nday,2)
dataset[key] = tmp
if filename!='':
cellstr = 'A'+str(row_counter)
sheet_by_product.write_row(cellstr,list(tmp.values()))
row_counter+=1
if filename!='':
keysol = list(soldValueInDay.keys())
day_tableArea = 'A1:B'+str(len(keysol)+1)
sheet_by_day.add_table(day_tableArea,{'columns':columns_by_d, 'style':None})
for i in range(len(keysol)) :
cellstr = 'A'+str(i+2)
sheet_by_day.write_row(cellstr,[keysol[i],soldValueInDay.get(keysol[i])])
workbook.close()
data['dataProduct'] = dataset
data['soldValueInDay'] = soldValueInDay
x_axis = []
y_axis = []
for i in range(nday):
k = (start+timedelta(days=i)).strftime('%d/%m/%Y')
x_axis.append(k)
y_axis.append(0) if soldValueInDay.get(k) == None else y_axis.append(float(soldValueInDay.get(k)))
data['avgSoldValue'] = [round(float(sumSoldValue)/nday,2)]*nday
data['labels'] = x_axis
data['data'] = y_axis
return data
def billlistsNdays(n=0,filename=''):
start_date = date.today() - timedelta(days=n)
bills = Bill.objects.filter(created__gte=start_date)
dataset = {}
for i in bills:
listsInBill = BillList.objects.filter(bill=i)
for j in listsInBill:
if dataset.get(j.product.id) != None:
# print(dataset.get(j.product.id))
dataset[j.product.id]['sold']+=int(j.quantity)
else :
# id : autoincrement field in db
dataset[j.product.id] = {
'id':j.product.product_id,
'name':j.product.name,
'price':j.product.price,
'sold':j.quantity
}
if filename!='':
workbook = xlsxwriter.Workbook(filename)
sheet = workbook.add_worksheet('products')
columns_table = [
{'header':'id'},
{'header':'name'},
{'header':'price'},
{'header':'sold'},
{'header':'value'}
]
tableArea = 'A1:E'+str(len(dataset.keys())+1)
sheet.add_table(tableArea,{'style':None,'columns':columns_table})
row_counter=2
for key in dataset.keys():
dataset[key]['value'] = dataset[key]['price'] * dataset[key]['sold']
if filename != '' :
cellstr = 'A'+str(row_counter)
sheet.write_row(cellstr,dataset[key].values())
if filename!='':
workbook.close()
return dataset
\ No newline at end of file
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'webproject.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
from django.contrib import admin
from .models import *
# Register your models here.
admin.site.register(Product)
admin.site.register(EmployeeUser)
admin.site.register(FileForSave)
admin.site.register(POSManager)
from django.apps import AppConfig
class ManagementConfig(AppConfig):
name = 'management'
from django import forms
from django.db.models import fields
from .models import Product #,EmployeeUser
from django.utils.translation import ugettext, ugettext_lazy as _
from pos.models import Bill
class FileForm(forms.Form):
docfile = forms.FileField(label='Select a file')
class ProductForm(forms.ModelForm):
class Meta:
model = Product
exclude = ['manager']
class BillForm(forms.ModelForm):
class Meta:
model = Bill
exclude = ['manager']
class EmployeeForm(forms.Form):
username = forms.CharField(label=_('username '), required=True)
password = forms.CharField(label=_('password '), required=True)
note = forms.CharField(label=_('note '), required=False)
class SendProductID(forms.Form):
pid = forms.CharField(required=False)
# Generated by Django 3.1.4 on 2021-04-16 01:10
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='FileForSave',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('docfile', models.FileField(upload_to='')),
],
),
migrations.CreateModel(
name='Manager',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('test', models.CharField(max_length=50)),
],
),
migrations.CreateModel(
name='Product',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('product_id', models.CharField(max_length=50)),
('name', models.CharField(max_length=50)),
('description', models.CharField(max_length=50)),
('price', models.DecimalField(decimal_places=2, max_digits=7)),
('quantity', models.IntegerField()),
('category', models.CharField(max_length=50)),
],
),
migrations.CreateModel(
name='SubUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('test', models.CharField(max_length=50)),
],
),
]
# Generated by Django 3.2 on 2021-04-16 07:14
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('management', '0001_initial'),
]
operations = [
migrations.DeleteModel(
name='Manager',
),
migrations.DeleteModel(
name='SubUser',
),
]
# Generated by Django 3.2 on 2021-04-17 07:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('management', '0002_auto_20210416_1414'),
]
operations = [
migrations.AddField(
model_name='product',
name='manager_username',
field=models.CharField(default=2, max_length=50),
preserve_default=False,
),
]
# Generated by Django 3.2 on 2021-04-21 13:33
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
('management', '0003_product_manager_username'),
]
operations = [
migrations.CreateModel(
name='EmployeeUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('username', models.CharField(max_length=50)),
('password', models.CharField(max_length=15)),
('manager_username', models.CharField(max_length=50)),
],
),
migrations.CreateModel(
name='Manager',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('email', models.EmailField(max_length=254, unique=True, verbose_name='email address')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]
# Generated by Django 3.2 on 2021-04-21 13:51
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('management', '0004_employeeuser_manager'),
]
operations = [
migrations.DeleteModel(
name='Manager',
),
]
# Generated by Django 3.2 on 2021-04-24 04:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('management', '0005_delete_manager'),
]
operations = [
migrations.DeleteModel(
name='EmployeeUser',
),
migrations.AlterField(
model_name='product',
name='product_id',
field=models.CharField(max_length=50, unique=True),
),
]
# Generated by Django 3.2 on 2021-04-24 06:42
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('management', '0006_auto_20210424_1149'),
]
operations = [
migrations.CreateModel(
name='EmployeeUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('note', models.CharField(max_length=15)),
('manager_username', models.CharField(max_length=150)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
# Generated by Django 3.2 on 2021-04-24 07:36
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('management', '0007_employeeuser'),
]
operations = [
migrations.RenameField(
model_name='employeeuser',
old_name='manager_username',
new_name='create_by',
),
]
# Generated by Django 3.2 on 2021-04-24 07:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('management', '0008_rename_manager_username_employeeuser_create_by'),
]
operations = [
migrations.AlterField(
model_name='employeeuser',
name='id',
field=models.AutoField(primary_key=True, serialize=False),
),
]
# Generated by Django 3.2 on 2021-04-24 07:42
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('management', '0009_alter_employeeuser_id'),
]
operations = [
migrations.AlterField(
model_name='employeeuser',
name='id',
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
# Generated by Django 3.2 on 2021-04-25 03:51
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('management', '0010_alter_employeeuser_id'),
]
operations = [
migrations.AlterField(
model_name='employeeuser',
name='create_by',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='Manager', to=settings.AUTH_USER_MODEL),
),
]
# Generated by Django 3.2 on 2021-04-25 03:54
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('management', '0011_alter_employeeuser_create_by'),
]
operations = [
migrations.AlterField(
model_name='employeeuser',
name='create_by',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to=settings.AUTH_USER_MODEL),
),
]
# Generated by Django 3.2 on 2021-04-25 03:58
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('management', '0012_alter_employeeuser_create_by'),
]
operations = [
migrations.AlterField(
model_name='employeeuser',
name='create_by',
field=models.ForeignKey(default=None, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='+', to=settings.AUTH_USER_MODEL),
),
]
# Generated by Django 3.2 on 2021-04-25 04:11
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('management', '0013_alter_employeeuser_create_by'),
]
operations = [
migrations.AlterField(
model_name='employeeuser',
name='create_by',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to='auth.user'),
),
]
# Generated by Django 3.2 on 2021-04-26 03:19
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('management', '0014_alter_employeeuser_create_by'),
]
operations = [
migrations.RemoveField(
model_name='product',
name='manager_username',
),
migrations.AddField(
model_name='product',
name='manager',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
# Generated by Django 3.2 on 2021-04-26 03:35
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('management', '0015_auto_20210426_1019'),
]
operations = [
migrations.AlterField(
model_name='employeeuser',
name='note',
field=models.CharField(max_length=50),
),
migrations.AlterField(
model_name='product',
name='manager',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='auth.user'),
),
migrations.CreateModel(
name='Man',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('note', models.CharField(max_length=150)),
('create_by', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to=settings.AUTH_USER_MODEL)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
# Generated by Django 3.2 on 2021-05-03 06:26
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('management', '0016_auto_20210426_1035'),
]
operations = [
migrations.AlterField(
model_name='product',
name='product_id',
field=models.CharField(max_length=50),
),
migrations.AlterUniqueTogether(
name='product',
unique_together={('product_id', 'manager')},
),
migrations.DeleteModel(
name='Man',
),
]
# Generated by Django 3.2 on 2021-05-20 11:35
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('management', '0017_auto_20210503_1326'),
]
operations = [
migrations.CreateModel(
name='POSManager',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('shop', models.CharField(max_length=150)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.urls import reverse
from django.contrib.auth.models import User,Group
from django.dispatch import receiver
from django.db.models.signals import post_save, pre_save,pre_delete
class FileForSave(models.Model):
docfile = models.FileField(upload_to='')
class EmployeeUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
note = models.CharField(max_length=50)
create_by = models.ForeignKey(User, on_delete=models.CASCADE,related_name='+')
def delete(self, *args, **kwargs):
self.user.delete()
return super(self.__class__, self).delete(*args, **kwargs)
def __str__(self):
return str(self.user)+' create_by '+str(self.create_by)
class POSManager(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
shop = models.CharField(max_length=150)
def delete(self, *args, **kwargs):
self.user.delete()
return super(self.__class__, self).delete(*args, **kwargs)
def __str__(self):
return str(self.user)+' manage '+str(self.shop)
class Product(models.Model):
product_id = models.CharField(max_length=50)
name = models.CharField(max_length=50)
description = models.CharField(max_length=50)
category = models.CharField(max_length=50)
price = models.DecimalField(max_digits=7,decimal_places=2)
quantity = models.IntegerField()
manager = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
unique_together = (("product_id","manager"),)
def __str__(self):
return str(self.manager)+' create ' +str(self.product_id)+' '+self.name+' '+str(self.price)
def get_absolute_url(self):
return reverse("show_product")
@receiver(pre_save, sender=User)
def update_username_from_email(sender, instance, **kwargs):
if instance.username.lower()==instance.first_name.lower() and instance.email!=None and instance.email!='' :
user_email = instance.email
username = user_email[:30]
n = 1
while User.objects.exclude(pk=instance.pk).filter(username=username).exists():
n += 1
username = user_email[:(29 - len(str(n)))] + '-' + str(n)
instance.username = username
@receiver(post_save,sender=User)
def addGroupsManager(sender, instance, **kwargs):
if instance.groups.exists() == False:
group_manager = Group.objects.get(name='manager')
instance.groups.add(group_manager)
@receiver(post_save,sender=EmployeeUser)
def setGroupEmployeeDeleteGroupManager(sender,instance, **kwargs):
group_employee = Group.objects.get(name='employee')
instance.user.groups.add(group_employee)
group_manager = Group.objects.get(name='manager')
instance.user.groups.remove(group_manager)
# edit เมื่อทำหน้าสมัครเอง
if POSManager.objects.filter(user=instance.create_by).count()==0:
POSManager(user=instance.create_by).save()
\ No newline at end of file
from django.shortcuts import redirect,render
def pos_manager_only(view_func):
def wrapper_function(request, *args, **kwargs):
group = None
if request.user.groups.exists():
group = request.user.groups.all()[0].name
if group == 'manager':
print('managerrrrrrrrrrrrrrrrrrrrrrrr')
return view_func(request,*args, **kwargs)
else :
return redirect('GaMaiMeeSit')
return wrapper_function
# def allow_by_role(roles=[]):
# def decorator(view_f):
# def wrapper(request, *args, **kwargs):
# group = None
# if request.user.groups.exists():
# group = request.user.groups.all()[0].name
# if group in roles:
# print('group in role')
# return view_f(request,*args, **kwargs)
# else :
# return redirect('GaMaiMeeSit')
# return wrapper
# return decorator
def gamaimeesit(request):
return render(request,'gamaimeesit.html')
\ No newline at end of file
{% extends 'pos_base.html' %}
{% block content %}
{% include 'navbar.html' %}
<!-- check message -->
{% if message != '' %}
<div class="modal fade" id="show_message" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
{{message}}
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
</div>
</div>
</div>
{% endif %}
<!-- Modal product -->
<div class="modal fade" id="product_Modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">กรุณายืนยัน</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% comment %} <div class="modal-body">
...
</div> {% endcomment %}
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">ปิด</button>
<button type="button" class="btn btn-primary" onclick='document.getElementById("product_form").submit();'>เพิ่มสินค้า</button>
</div>
</div>
</div>
</div>
<!-- Modal upload file -->
<div class="modal fade" id="upload_file_Modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">กรุณายืนยัน</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% comment %} <div class="modal-body">
<p>
นอนยันการอัพโหลด
</p>
</div> {% endcomment %}
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">ปิด</button>
<button type="button" class="btn btn-primary" onclick='document.getElementById("upload_form").submit();'>อัพโหลด</button>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-lg-6 p-0">
<ul class="list-group">
<li class="list-group-item list-group-item-light ">
<h2>เพิ่มสินค้า 1 รายการ</h2>
</li>
<li class="list-group-item">
<form id='product_form' name='product_form' action="{% url "add_product" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ product_form.as_p }}
</form>
<button class="btn btn-outline-success" data-toggle="modal" data-target="#product_Modal" >เพิ่มสินค้า</button>
</li>
</ul>
</div>
<div class="col-lg-6">
<ul class="list-group">
<li class="list-group-item list-group-item-info">
<h2>เพิ่มสินค้าโดยใช้ไฟล์ xlsx</h2>
</li>
<li class="list-group-item">
<a class="nav-link" href="{% url 'download' 'exampleAddProduct.xlsx'%}" id='download'>ตัวอย่างไฟล์</a>
<form id='upload_form' name='upload_form' action="{% url "add_product" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>{{ upload_form.non_field_errors }}</p>
<p>{{ upload_form.docfile.label_tag }} {{ upload_form.docfile.help_text }}</p>
<p>
{{ upload_form.docfile.errors }}
<input class='btn p-0' type="file" name="docfile" required="" id="id_docfile">
</p>
</form>
<button name='summit_upload_form' class="btn btn-outline-success" data-toggle="modal" data-target="#upload_file_Modal">อัพโหลด</button>
</li>
</ul>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(window).on('load', function() {
$('#show_message').modal('show');
});
</script>
{% endblock content %}
{% extends 'pos_base.html' %}
{% block content %}
<div class="container py-5">
<form align='center' action='.' method='POST'>{% csrf_token %}
<h5 class='text-primary'>ยืนยันการลบ</h5>
<p>
<b>รายการขาย สร้างเมื่อ</b> {{ object.created }} <b>บุคคลที่สร้าง</b> {{object.user}}
</p>
<button type="submit" class="btn btn-outline-danger mx-5" >ยืนยัน</button>
<a href='{% url "bills" %}'><button type="button" class="btn btn-outline-info mx-5" >ปิด</button></a>
</form>
</div>
{% endblock %}
\ No newline at end of file
{% extends 'pos_base.html' %}
{% block content %}
<div class="container py-5">
<form align='center' action='.' method='POST'>{% csrf_token %}
<h5 class='text-primary'>ยืนยันการลบ</h5>
<p>
<b>username</b> {{ object.user }} <b>note</b> {{object.note}}
</p>
<button type="submit" class="btn btn-outline-danger mx-5" >ยืนยัน</button>
<a href='{% url "show_product" %}'><button type="button" class="btn btn-outline-info mx-5" >ปิด</button></a>
</form>
</div>
{% endblock %}
\ No newline at end of file
{% extends 'pos_base.html' %}
{% block content %}
<div class="container py-5">
<form align='center' action='.' method='POST'>{% csrf_token %}
<h5 class='text-primary'>ยืนยันการลบ</h5>
<p>
สินค้ารหัส {{object.product_id}} ชื่อสินค้า {{object.name}}
</p>
<button type="submit" class="btn btn-outline-danger mx-5" >ยืนยัน</button>
<a href='{% url "show_product" %}'><button type="button" class="btn btn-outline-info mx-5" >ปิด</button></a>
</form>
</div>
{% endblock %}
\ No newline at end of file
{% extends 'pos_base.html' %}
{% block content %}
{% include 'navbar.html' %}
<div class="modal fade" id="product_Modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">กรุณายืนยัน</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">ปิด</button>
<button type="button" class="btn btn-primary" onclick='document.getElementById("product_form").submit();'>บันทึก</button>
</div>
</div>
</div>
</div>
<div class="container">
<form id='product_form'action='.' method='POST'>{% csrf_token %}
{{ form.as_p }}
</form>
<button class="btn btn-outline-success" data-toggle="modal" data-target="#product_Modal" >บันทึก</button>
</div>
{% endblock %}
\ No newline at end of file
{% extends 'pos_base.html' %}
{% block content %}
<h1>ไม่มีสิทธิ์เข้าถึงหน้านี้</h1>
<script>
window.onload = fiveSecondGoHomePage()
function fiveSecondGoHomePage(){
setTimeout(function(){
window.location.href = '{% url "home" %}'
}, 5000);
}
</script>
{% endblock content %}
\ No newline at end of file
{% extends 'pos_base.html' %}
{% block content %}
{% include 'navbar.html'%}
{% comment %} {{emps}} {% endcomment %}
<div class="container">
<form action="{% url "manage_employee" %}" method="post" class='text-center' >
{% csrf_token %}
{% comment %} &nbsp;&nbsp;&nbsp; {% endcomment %}
{{ sign_up_form }}
<button type='submit' >เพิ่ม</button>
</form>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th scope="col">username</th>
<th scope="col">Note</th>
<th scope='col' class='text-center'>ลบผู้ใช้</th>
</tr>
</thead>
<tbody>
{% for emp in emps %}
<tr>
<td >{{emp.username}} </td>
<td >{{emp.note}} </td>
<td scope='rowgroup' class='text-center'>
<a href='{% url "deleteemployee" emp.id %}'>
<button type="button" class="btn btn-small btn-outline-secondary" >
<i class="bi-trash"></i>
</button>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endblock content %}
{% extends 'pos_base.html' %}
{% block content %}
{% include 'navbar.html' %}
<div class="container">
<table class="table">
<thead>
<tr>
<th>เวลาที่สร้าง</th>
<th>สร้างโดย</th>
<th></th>
</tr>
</thead>
<tbody>
{% for i in bills%}
<tr>
<td scope="row">{{i.created}}</td>
<td>{{i.user}}</td>
<td><a href='{% url "deletebill" i.id %}'> <button type="button" class="btn btn-small btn-outline-secondary" ><i class="bi-trash"></i></button> </a></td>
</tr>
{% endfor %}
<tr>
<td scope="row"></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
{% endblock content %}
\ No newline at end of file
{% extends 'pos_base.html' %}
{% block content %}
{% include 'navbar.html' %}
<div class="container d-lg-none">
<h1>
ขนาดของจอเล็กเกินไป
</h1>
</div>
<div class='container d-none d-lg-block mt-5'>
<a name="" id="" class="btn btn-lg btn-primary align-content-center" href="{% url 'add_product' %}" role="button">เพิ่มสินค้า</a>
{% comment %} <button class='btn btn-outline-info'> </button> {% endcomment %}
{% comment %} unable to sort data in table have to fix it? {% endcomment %}
<ul class="list-group list-group-flush">
<li class="list-group-item m-0 p-1 list-group-item-primary">
<div class="row">
<div class='col-lg-2 '>product id</div>
<div class='col-lg-2 '>name</div>
<div class='col-xl-2 d-lg-none d-xl-block '>description</div>
<div class='col-lg-2 col-xl-1 text-center'>price</div>
<div class='col-lg-2 col-xl-1 text-center'>quantity</div>
<div class='col-lg-2 text-center'>category</div>
<div class='col-lg-2 '>management</div>
</div>
</li>
{% for product in products %}
<li class="list-group-item m-0 p-1">
<div class="row">
<div class='col-lg-2 '>{{product.product_id}}</div>
<div class='col-lg-2 text-truncate'>{{product.name}}</div>
<div class='col-lg-2 d-lg-none d-xl-block'><p class='m-0'>{{product.description}}
{% comment %} ครับ สำหรับท่านที่เดินผ่านไปผ่านมานะครับ วันนี้ เฉาก๊วยชากังราวของเรานะครับ
ก็ได้มาบริการท่านพ่อแม่พี่น้องกันอีกแล้วครับ อากาศร้อนๆ อย่างนี้นะครับ
สำหรับท่านที่เดินผ่านไปผ่านมา ลองมาแวะชิมเฉาก๊วยแท้ๆ กันก่อนนะครับ
เฉาก๊วยชากังราวของเราเป็นที่รู้จักไปทั่ว {% endcomment %}
</p></div>
<div class='col-lg-2 col-xl-1 text-center'>{{product.price}}</div>
<div class='col-lg-2 col-xl-1 text-center'>{{product.quantity}}</div>
<div class='col-lg-2 text-center'>{{product.category}}</div>
<div class='col-lg-2 '>
{% comment %} <a href='{% url "edit_product" %}'> {% endcomment %}
<button type="button" class="btn btn-small btn-outline-secondary" onclick='to_edit_page({{product.id}})'><i class="bi-list"></i></button>
{% comment %} </a> {% endcomment %}
<button type="button" class="btn btn-small" disabled></button>
{% comment %} <button type="button" class="btn btn-small btn-outline-secondary" data-toggle="modal" data-target="#confirm_delete" onclick='savePIDforDELETE({{product.id}})'><i class="bi-trash"></i></button> {% endcomment %}
<a href='{% url "deleteproduct" product.id %}'> <button type="button" class="btn btn-small btn-outline-secondary" ><i class="bi-trash"></i></button> </a>
</div>
</div>
</li>
{% endfor %}
</ul>
</div>
<script>
console.log('hi');
function to_edit_page(pid){
window.location.href = '{% url "home" %}'+'management/editproduct/'+pid;
}
</script>
{% endblock %}
\ No newline at end of file
from django.test import TestCase
# Create your tests here.
from django import urls
from django.shortcuts import redirect, render,get_object_or_404
import os
from django.urls import reverse
from .models import FileForSave, POSManager,Product,EmployeeUser
from .forms import FileForm,EmployeeForm,ProductForm,SendProductID
import pandas as pd
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.views.generic.edit import UpdateView,DeleteView
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from .permission_page import pos_manager_only
from pos.models import Bill,BillList
@login_required
@pos_manager_only
def show_bills(req):
context = dict()
manager = POSManager.objects.get(user=req.user)
context['bills'] = Bill.objects.filter(manager=manager).order_by('-created')
return render(req,'show_bills.html',context)
@login_required
@pos_manager_only
def show_products(request):
context = {}
context['products'] = Product.objects.filter(manager=request.user).order_by('quantity')
context['keepPID'] = SendProductID()
return render(request,'show_product.html',context)
class BillDeleteView(DeleteView):
template_name = 'delete_bill.html'
def get_object(self):
id_ = self.kwargs.get("id")
return get_object_or_404(Bill, id=id_)
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
success_url = self.get_success_url()
print(success_url)
self.object.delete()
success_url = self.get_success_url()
return HttpResponseRedirect(success_url)
def get_success_url(self):
return reverse('bills',kwargs=None)
@login_required
@pos_manager_only
def add_product(request):
message = ''
file_form = FileForm()
product_form = ProductForm()
if request.method == 'POST':
file_form = FileForm(request.POST, request.FILES)
product_form = ProductForm(request.POST)
if file_form.is_valid():
newdoc = FileForSave(docfile=request.FILES['docfile'])
newdoc.save()
try:
df = pd.read_excel (newdoc.docfile)
for i in df.values:
i = i.tolist()
for j in i:
print(type(j),end=' ')
print()
try:
product = Product.objects.get(manager=request.user,product_id=i[0])
product.quantity+=i[4]
product.save()
except:
try:
instance_product = Product(
product_id=str(i[0],),
name=i[1],
description=i[2],
price=i[3],
quantity=i[4],
category=i[5],
manager=request.user)
instance_product.save()
print(post_save(sender=Product,instance=instance_product))
except:
print('*--try level 3 error--* new_instance_product error')
print('*--try level 2 error--* no this product')
except:
print('*--try level 1 error--*')
message = 'เกิดข้อผิดพลาด'
allFile = FileForSave.objects.all()
for i in allFile:
f_path = i.docfile.path
try:
os.remove(f_path)
print(f_path+' deleted')
i.delete()
except:
print("can't delete "+f_path)
return redirect('show_product')
elif product_form.is_valid():
rp = request.POST
instance_product = Product(
product_id=rp.get('product_id'),
name=rp.get('name'),
description=rp.get('description'),
price=rp.get('price'),
quantity=rp.get('quantity'),
category=rp.get('category'),
manager=request.user)
instance_product.save()
return redirect('show_product')
else:
message = 'กรุณากรอกข้อมูลกรอกข้อมูล'
context = {'upload_form': file_form, 'message': message}
context['product_form']=product_form
return render(request, 'add_product.html', context)
@login_required
@pos_manager_only
def manage_employee(request):
sign_up_form = EmployeeForm()
if request.method == 'POST':
form = EmployeeForm(request.POST, request.FILES)
if form.is_valid():
user = User.objects.create(username=request.POST['username'])
user.set_password(raw_password=request.POST['password'])
user.save()
employee = EmployeeUser(user=user,create_by=request.user,note=request.POST['note'])
employee.save()
return redirect('manage_employee')
employees = EmployeeUser.objects.filter(create_by=request.user).values()
employees2 = []
for i in employees:
employees2.append({
'username':User.objects.get(id=i['user_id']).username,
'note':i['note'],
'id':i['id']
})
return render(request,'manage_employee.html',{'sign_up_form':sign_up_form,'emps':employees2,'emp1':employees})
@login_required
@pos_manager_only
def edit_product(request):
print('in function edit product ')
if request.method == 'POST':
print(request.POST)
context = {}
return render(request, 'edit_product.html' ,context)
else :
return redirect('show_product')
class ProductUpdateView(UpdateView):
model = Product
form_class = ProductForm
template_name = 'edit_product.html'
def get_object(self):
id_ = self.kwargs.get("id")
return get_object_or_404(Product, id=id_)
def form_valid(self, form):
return super().form_valid(form)
class ProductDeleteView(DeleteView):
template_name = "delete_product.html"
def get_object(self):
id_ = self.kwargs.get("id")
print(get_object_or_404(Product, id=id_))
return get_object_or_404(Product, id=id_)
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
manager = User.objects.get(id = self.object.manager_id)
success_url = self.get_success_url()
if request.user == manager:
success_url = self.get_success_url()
print('delete ok')
self.object.delete()
else:
print("you don't have permission")
return HttpResponseRedirect(success_url)
def get_success_url(self):
return reverse('show_product',kwargs=None)
class EmployeeDeleteView(DeleteView):
template_name = "delete_employee.html"
def get_object(self):
id_ = self.kwargs.get("id")
return get_object_or_404(EmployeeUser, id=id_ )
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.delete()
success_url = self.get_success_url()
return HttpResponseRedirect(success_url)
def get_success_url(self):
return reverse('manage_employee',kwargs=None)
from django.contrib import admin
from .models import *
# Register your models here.
admin.site.register(BillList)
admin.site.register(Bill)
admin.site.register(FeaturePic)
\ No newline at end of file
from django.apps import AppConfig
class PosConfig(AppConfig):
name = 'pos'
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment