from pos.models import MenuItem, Store, Setting, Table, BookingTable, Order, OrderItem, UIMenu, Ingredient, StoreFile, MenuFile, Post, PostFile, Customer, Code, Profile, PostCategory, Message
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth import authenticate
from django.contrib.auth.models import User
from datetime import date
from .admin import SettingAdminForm

from .forms import formMap, OrderForm, OrderFilter, OrderItemFormSet, StoreFilter, StoreForm, MenuItemForm, \
    MenuItemFilter, TableForm, TableFilter, BookingForm, BookingFilter, PostForm, PostFilter, CustomerFilter, CustomerForm, CodeForm, UserFilter, MessageForm, MessageFilter, \
    UIMenuForm, UIMenuFilter
from django.core.paginator import Paginator
from django.forms import inlineformset_factory, JSONField, TextInput
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django_json_widget.widgets import JSONEditorWidget

from django_admin_hstore_widget.forms import HStoreFormField
from django.contrib.postgres.fields import HStoreField, ArrayField

class Kitchen:
    name = "kitchen"
    def __init__(self, store):
        self.store = store

    def acceptOrder(self, order):
        order.inProcess = Kitchen.name
        order.status = "process"
        order.save()

        for oi in order.orderitem_set.all():
            oi.status = "process"
            oi.save()

    def listItems(self, order):
        return order.orderitem_set.all().order_by('-created_at')

    def finishItem(self, orderItem):
        orderItem.status = "fn"
        orderItem.save()

    def createNote(self, order, text):
        order.note = text
        order.save()

class BookingSys:
    name = "bookingSys"
    formClass = BookingForm
    #menuItemFormset = inlineformset_factory(MenuItem, Ingredient, fields=('name', 'qty', 'unit', 'comment',))
    modelClass = BookingTable
    displayList  = {"table": "Table", "onDate": "Date",  "onTime": "Time", "note": "Note" }
    filterClass = BookingFilter
    PS = 25

    def __init__(self, store):
        self.store = store

    def list(self):
        return self.store.bookingtable_set.all().order_by('-created_at')

    def filter(self, request):
        page = request.GET.get('page', 1)
        f = self.filterClass(request.GET, queryset=self.list())
        p = Paginator(f.qs, self.PS)
        #print(p)
        return (f, p.get_page(page))

    def delete(self, oid):
        return self.modelClass.objects.filter(store=self.store, pk=oid).delete()

    def create(self, code, nseats):
        table = self.modelClass(code=code, nSeats=nseats)
        table.store = self.store
        table.save()
        return table


    def get(self, code):
        return self.modelClass.objects.get(code = code, store=self.store)

    def form(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.formClass
        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)

class PostSys:
    name = "postSys"
    formClass = PostForm
    #menuItemFormset = inlineformset_factory(MenuItem, Ingredient, fields=('name', 'qty', 'unit', 'comment',))
    fileFormset = inlineformset_factory(Post, PostFile, fields=('file', 'tags',))
    modelClass = Post
    displayList  = {"title": "Title", "cats": "Category" }
    sideForms = ['category', 'relateMenus', 'tags', ]
    filterClass = PostFilter
    PS = 25



    def __init__(self, store):
        self.store = store

    def list(self, **kwargs):
        return self.store.post_set.filter(**kwargs).order_by("-created_at")

    def filter(self, request):
        page = request.GET.get('page', 1)
        f = self.filterClass(request.GET, queryset=self.list())
        p = Paginator(f.qs, self.PS)
        #print(p)
        return (f, p.get_page(page))

    def delete(self, oid):
        return self.modelClass.objects.filter(store=self.store, pk=oid).delete()

    def create(self, code, nseats):
        obj = self.modelClass(code=code, nSeats=nseats)
        obj.store = self.store
        obj.save()
        return obj


    def get(self, pid):
        return self.modelClass.objects.get(pk=pid, store=self.store)

    def form(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.formClass

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            f = f(instance=instance)
            f.fields['relateMenus'].queryset = self.store.menuitem_set.all()
            return f

    def sideFields(self, form):
        return [ form[f] for f in self.sideForms]



    def formset(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.fileFormset

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            #store = instance.store
            f0 =  f(instance=instance)
            '''
            for f1 in f0.forms:
                f1.fields['item'].queryset = self.store.menuitem_set.all()
            '''
            return f0

    def getPosts(self, **kwargs):
        cat = kwargs.get('cat', None)
        try:
            if cat:
                pc = PostCategory.objects.get(name=cat)
                return pc.post_set.filter(store=self.store)
        except:
            return None

    @classmethod
    def gGetPosts(self, **kwargs):
        cat = kwargs.get('cat', None)
        if cat:
            pc = PostCategory.objects.get(name=cat)
            return pc.post_set.filter(store=self.store)
        return None

class UserSys:
    name = "userSys"
    formClass = UserCreationForm
    profileFormset = inlineformset_factory(User, Profile, fields=('location', 'birthdate', 'role',))
    modelClass = User
    displayList  = {"username": "User Name", "email":"Email", "first_name": "First Name", "last_name": "Last Name", "profile.role": "Role" }
    filterClass = UserFilter
    PS = 25
    sideForms = [ ]


    def __init__(self, store):
        self.store = store

    def list(self):
        return User.objects.filter(profile__store=self.store)

    def filter(self, request):
        page = request.GET.get('page', 1)
        if self.list():
            f = self.filterClass(request.GET, queryset=self.list())
            p = Paginator(f.qs, self.PS)
            #print(p)
            return (f, p.get_page(page))
        else:
            return (None, None)

    def delete(self, oid):
        return self.modelClass.objects.filter(store=self.store, pk=oid).delete()


    def form(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.formClass
        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)

    def form2(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = UserChangeForm
        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)

    def sideFields(self, form):
        return [ form[f] for f in self.sideForms]



    def formset(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.profileFormset

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            #store = instance.store
            f0 =  f(instance=instance)
            '''
            for f1 in f0.forms:
                f1.fields['item'].queryset = self.store.menuitem_set.all()
            '''
            return f0
class CustomerSys:
    name = "customerSys"
    formClass = CustomerForm
    #menuItemFormset = inlineformset_factory(MenuItem, Ingredient, fields=('name', 'qty', 'unit', 'comment',))
    modelClass = Customer
    displayList  = {"fullName": "Full Name", "lineId": "LINE ID", "email": "Email", "store": "Store" }
    filterClass = CustomerFilter
    PS = 25
    sideForms = [ ]


    def __init__(self, store):
        self.store = store

    def list(self):
        return self.store.customer_set.all().order_by('-created_at')

    def filter(self, request):
        page = request.GET.get('page', 1)
        f = self.filterClass(request.GET, queryset=self.list())
        p = Paginator(f.qs, self.PS)
        #print(p)
        return (f, p.get_page(page))

    def delete(self, oid):
        return self.modelClass.objects.filter(store=self.store, pk=oid).delete()


    def form(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.formClass
        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)

    def sideFields(self, form):
        return [ form[f] for f in self.sideForms]



    def formset(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.fileFormset

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            #store = instance.store
            f0 =  f(instance=instance)
            '''
            for f1 in f0.forms:
                f1.fields['item'].queryset = self.store.menuitem_set.all()
            '''
            return f0

class MessageSys:
    name = "messageSys"
    formClass = MessageForm
    #menuItemFormset = inlineformset_factory(MenuItem, Ingredient, fields=('name', 'qty', 'unit', 'comment',))
    modelClass = Message
    displayList  = {"subject": "Subject", "email": "Email", "tel": "Tel.", "body": "Body", "reply": "Reply", "parent": "Parent" }
    filterClass = MessageFilter
    PS = 25

    def __init__(self, store):
        self.store = store

    def list(self):
        return self.store.message_set.all().order_by("-created_at")

    def filter(self, request):
        page = request.GET.get('page', 1)
        f = self.filterClass(request.GET, queryset=self.list())
        p = Paginator(f.qs, self.PS)
        #print(p)
        return (f, p.get_page(page))

    def delete(self, oid):
        return self.modelClass.objects.filter(store=self.store, pk=oid).delete()

    def create(self, code, nseats):
        table = self.modelClass(code=code, nSeats=nseats)
        table.store = self.store
        table.save()
        return table


    def get(self, code):
        return self.modelClass.objects.get(code = code, store=self.store)

    def form(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.formClass
        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)

class TableSys:
    name = "tableSys"
    formClass = TableForm
    #menuItemFormset = inlineformset_factory(MenuItem, Ingredient, fields=('name', 'qty', 'unit', 'comment',))
    modelClass = Table
    displayList  = {"code": "Code", "nSeats": "Seats", "occupy": "Occupy" }
    filterClass = TableFilter
    PS = 25

    def __init__(self, store):
        self.store = store

    def list(self):
        return self.store.table_set.all().order_by("-created_at")

    def filter(self, request):
        page = request.GET.get('page', 1)
        f = self.filterClass(request.GET, queryset=self.list())
        p = Paginator(f.qs, self.PS)
        #print(p)
        return (f, p.get_page(page))

    def delete(self, oid):
        return self.modelClass.objects.filter(store=self.store, pk=oid).delete()

    def create(self, code, nseats):
        table = self.modelClass(code=code, nSeats=nseats)
        table.store = self.store
        table.save()
        return table

    def bookingTbl(self, code, onDate = date.today(), note=""):
        tbl = self.modelClass.objects.get(code=code)
        booking = BookingTable(table=tbl, onDate=onDate, note=note)
        booking.save()
        return booking

    def get(self, code):
        return self.modelClass.objects.get(code = code, store=self.store)

    def form(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.formClass
        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)


class BaseSys:

    def __init__(self, store):
        self.store = store

    def list(self, **kwargs):
        return self.model.order_set.all().order_by('-created_at')

    def filter(self, request):
        page = request.GET.get('page', 1)
        f = self.filterClass(request.GET, queryset=self.list())
        p = Paginator(f.qs, self.PS)
        #print(p)
        return (f, p.get_page(page))

    def delete(self, oid):
        return self.model.objects.filter(store=self.store, pk=oid).delete()

    '''
    def create(self, tblCode, note=""):
        tbl = self.tableSys.get(code=tblCode)
        order = Order(table=tbl, note=note, store=self.store)
        order.save()
        return order
    '''

class OrderSys:
    name = "orderSys"
    formClass = OrderForm
    orderItemFormset = inlineformset_factory(Order, OrderItem, fields=('item','qty', 'note', ))

    displayList = { "table": "Table", "note": "Note"}
    filterClass = OrderFilter
    PS = 25

    def __init__(self, store):
        self.store = store
        self.tableSys = TableSys(self.store)
        self.menuSys = MenuSys(self.store)





    def list(self, **kwargs):
        return self.store.order_set.all().order_by('-created_at')

    def filter(self, request):
        page = request.GET.get('page', 1)
        f = self.filterClass(request.GET, queryset=self.list())
        p = Paginator(f.qs, self.PS)
        #print(p)
        return (f, p.get_page(page))

    def delete(self, oid):
        return Order.objects.filter(store=self.store, pk=oid).delete()

    def create(self, tblCode, note=""):
        tbl = self.tableSys.get(code=tblCode)
        order = Order(table=tbl, note=note, store=self.store)
        order.save()
        return order

    def putItem(self, orderId, itemId, qty, note=""):
        order = Order.objects.get(pk=orderId)
        item = self.menuSys.get(itemId)
        oi = OrderItem.objects.create(order=order, item=item, qty=qty, note=note)
        return oi

    def cancelItem(self, itemId):
        return OrderItem.objects.get(pk=itemId).delete()

    def getStatus(self, orderId):
        order = Order.objects.get(pk=orderId)
        return {'owner': order.owner, 'inProcess': order.inProcess, 'status': order.status }

    def purchaseOrder(self, orderId):
        order = Order.objects.get(pk=orderId)
        order.status = Order.PURCHASE
        order.save()

        self.printReceipt(order)

    def printReceipt(order):
        print(f"Receipt {order.id}")


    def form(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = Order.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = OrderSys.formClass

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)

    def formset(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = Order.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.orderItemFormset

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            #store = instance.store
            f0 =  f(instance=instance)
            for f1 in f0.forms:
                f1.fields['item'].queryset = self.store.menuitem_set.all()
            return f0

class MenuSys:
    name = "menuSys"
    formClass = MenuItemForm
    menuItemFormset = inlineformset_factory(MenuItem, Ingredient, fields=('name', 'qty', 'unit', 'comment',))
    menuFileFormset = inlineformset_factory(MenuItem, MenuFile, fields=('menuFile', 'tags',))
    modelClass = MenuItem
    displayList  = {"name": "Name" }
    filterClass = MenuItemFilter
    PS = 25

    def __init__(self, store):
        self.store = store

    def list(self, **kwargs):
        return self.store.menuitem_set.filter(**kwargs).order_by('-created_at')

    def filter(self, request):
        page = request.GET.get('page', 1)
        f = self.filterClass(request.GET, queryset=self.list())
        p = Paginator(f.qs, self.PS)
        print(p)
        return (f, p.get_page(page))

    def naMenus(self):
        pass

    def avMenus(self):
        pass

    def search(self, kw):
        return MenuItem.objects.filter(name=kw)

    def get(self, objId):
        return MenuItem.objects.get(pk = objId)

    def create(self, name, tagline=""):
        return MenuItem.objects.create(name=name, tagline=tagline,store=self.store)


    def delete(self, oid):
        return self.modelClass.objects.filter(store=self.store, pk=oid).delete()

    def deleteMenu(self, pk):
        return MenuItem.objects.delete(pk=pk)

    def form(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.formClass
        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)


    def formset(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.menuItemFormset

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            #store = instance.store
            f0 =  f(instance=instance)
            '''
            for f1 in f0.forms:
                f1.fields['item'].queryset = self.store.menuitem_set.all()
            '''
            return f0

    def formset2(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.menuFileFormset

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            #store = instance.store
            f0 =  f(instance=instance)
            '''
            for f1 in f0.forms:
                f1.fields['item'].queryset = self.store.menuitem_set.all()
            '''
            return f0

class MarketingSys:
    name = "marketingSys"

    def __init__(self, store):
        self.store = store

    def popMenu(self, period="today"):
        pass

    def createRating(self, menuId, rating):
        pass

    def createComment(self, menuId, comment, byWho):
        pass

    def createStoreRating(self, rating):
        pass

    def createStoreComment(self, comment):
        pass

    def getComments(self, obj):
        pass

    def getRatings(self, obj):
        pass



class UISys:
    name = "uiSys"
    filterClass = UIMenuFilter
    formClass = UIMenuForm
    modelClass = UIMenu

    displayList  = {"name": "Name", "parent": "Parent", "group": "Group", "url": "URL", "icon": "Icon", "nOrder": "Order"}
    PS = 25

    def __init__(self, store):
        self.store = store

    def list(self):
        return self.store.uimenu_set.all().order_by("-created_at")

    def filter(self, request):
        page = request.GET.get('page', 1)
        f = self.filterClass(request.GET, queryset=self.list())
        p = Paginator(f.qs, self.PS)
        #print(p)
        return (f, p.get_page(page))

    def menu(self, group):
        #return UIMenu.objects.filter(group=group, store=self.store).order_by('-nOrder')
        return UIMenu.objects.filter(group=group, store=self.store)

    def search(self, style):
        pass

    def form(self, oid=None,  request=None):
        instance = None
        if oid:
            instance = self.modelClass.objects.get(pk=oid)
        #f = formMap.get(formId)
        f = self.formClass
        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)

    def delete(self, oid):
        return self.modelClass.objects.filter(store=self.store, pk=oid).delete()

    def createMenu(self, name, parentId, group, url):
        parent = UIMenu.objects.get(pk=parentId)
        return UIMenu.objects.create(name=name, parent=parent, group=group, url=url)

    def deleteMenu(self, menuId):
        return UIMenu.objects.delete(pk=menuId)

    def updateMenu(self, menuId, name, parentId, group, url):
        parent = UIMenu.objects.get(pk=parentId)
        menu  = UIMenu.objects.get(pk=menuId)

        menu.name = name
        menu.parent = parent
        menu.group = group
        menu.url = url
        menu.save()

    def renderStoreInfo(self):
        return f"""
    <h1>{self.store.name}</h1>
    """


class CodeSys:
    #model = Store
    name = "codeSys"
    formClass = CodeForm
    modelClass = Code

    def __init__(self, store):
        self.store = store


    def form(self,  request=None):
        try:
            instance = self.store.code
        except Store.code.RelatedObjectDoesNotExist:
            c = Code(store = self.store)
            c.save()
            instance = self.store.code

        f = self.formClass

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)


#class StoreSys(BaseSys):
class StoreSys:
    #model = Store
    name = "storeSys"
    filterClass = StoreFilter
    formClass = StoreForm
    modelClass = Store

    storeFileFormset = inlineformset_factory(Store, StoreFile, fields=('storeFile', 'tags',))
    settingFormset = inlineformset_factory(Store, Setting, fields=('data', 'note',), widgets = { HStoreField: {'widget': JSONEditorWidget} })
    #settingFormset = inlineformset_factory(Store, Setting, fields=('data', 'note',), form=SettingAdminForm)

    def __init__(self, store):
        self.store = store


    def form(self,  request=None):
        instance = self.store
        f = self.formClass

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            return f(instance=instance)


    def formset(self, oid=None,  request=None):
        instance = self.store
        f = self.storeFileFormset

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            #store = instance.store
            f0 =  f(instance=instance)
            '''
            for f1 in f0.forms:
                f1.fields['item'].queryset = self.store.menuitem_set.all()
            '''
            return f0

    def formset2(self, oid=None,  request=None):
        instance = self.store
        f = self.settingFormset

        if request:
            return f(request.POST, request.FILES, instance=instance)
        else:
            #store = instance.store
            f0 =  f(instance=instance)
            '''
            for f1 in f0.forms:
                f1.fields['item'].queryset = self.store.menuitem_set.all()
            '''
            return f0







class PosSys:
    def __init__(self, store):
        #self.restoreStore(store)
        self.store = store
        self.kitchen = Kitchen(self.store)
        self.settingSys = SettingSys(self.store)
        self.tableSys = TableSys(self.store)
        self.bookingSys = BookingSys(self.store)
        self.orderSys = OrderSys(self.store)
        self.menuSys = MenuSys(self.store)
        self.uiSys = UISys(self.store)
        self.storeSys = StoreSys(self.store)
        self.postSys = PostSys(self.store)
        self.customerSys = CustomerSys(self.store)
        self.codeSys = CodeSys(self.store)
        self.userSys = UserSys(self.store)
        self.messageSys = MessageSys(self.store)

    @classmethod
    def initStore(self, name, tags):
        s = Store(name=name, tagline=tags)
        s.save()

    @classmethod
    def initFromId(self, storeId):
        store = Store.objects.get(pk=storeId)
        return PosSys(store)



    @classmethod
    def restoreStore(cls, user):
        #self.store = store
        try:
            store = Store.objects.get(owner = user)
        except ObjectDoesNotExist:
            s = Store(owner=user)
            s.save()
            store = s

        posSys = PosSys(store)

        return posSys
        #self.kitchen = Kitchen(self.store)




    @classmethod
    def login(cls, username, password):
        user = authenticate(username=username, password=password)
        if user is not None:
            return cls.restoreStore(user)
            #store  = Store.objects.get(owner = user)
            #return PosSys(store)

        else:
            return None

    @classmethod
    def regis(cls, username, email, password, confirmPassword):
        if password == confirmPassword:
            user = User.objects.create_user(username, email, password)
            return user
        else:
            return None

    def bookingTable(self, tbl, note, whenDt):
        if tbl.store == self.store:
            b = BookingTable(table=tbl, note=note, onDate=whenDt)
            return b.save()
        else:
            return None

    def listMenus(self):
        return self.store.menuitem_set.all()

    def printReciept(self, orderId):
        order = Order.get(pk=orderId)
        return order.orderitem_set.all()

    def addItemToOrder(self, order, item, qty):
        o = OrderItem(order=order, item=item, qty = qty)
        return o.save()

    def sentOrder(self, order):
        self.kitchen.acceptOrder(order)

    def viewOrderStatus(self, order):
        return order.currentStatus()


    def createOrder(self, tblId):
        o = Order(tbl=tbl)
        return o.save()




    def createSampleMenu(self):
        m = MenuItem(name='sample Menu', tagline='Wow', store=self.store)
        m.save()


class SettingSys:
    def __init__(self, store):
        self.store = store
        if hasattr(self.store, 'setting'):
            #posSys.settingSys = SettingSys(store.setting)
            self.setting = self.store.setting
        else:
            setting = Setting(store = self.store, data={'firstTime': True})
            setting.save()
            self.setting = setting

    def get(self, key):
        return self.setting.data.get(key, None)

    def set(self,dicts):
        for k,v in dicts.items():
            self.setting.data[k] = v

    def commit(self):
        self.setting.save()


if __name__ == "__main__":
    Pos.register(email="xxx", passwd="", confirmPasswd="")
    pos = Pos.login(email="xxx", passwd="")

    settings = pos.settings
    settings.name = ""
    settings.address = ""
    settings.currency = "USD"


    menus = pos.menus
    menus.add(MenuItem(name='xxx', cost=10, price=15))
    menus.add(MenuItem(name='xyy', cost=10, price=15))
    menus.add(MenuItem(name='kkk', cost=10, price=15))

    mi = menus.find(name='xxx')
    mi2 = menus.find(name='kkk')
    mi.active = False

    cust = Customer(name='xxx', email='xxx', line='xxx')

    orders = pos.orders

    order = orders.createOrder()
    order.items.add(OrderItem(menu = mi, qty=3, comment='ll'))
    order.items.add(OrderItem(menu = mi2, qty=1, comment='uuu'))
    order.comment = 'xxxxx'
    order.commit()

    pr = order.printReceipt()
    order.clear()

    cust.logOrders.add(order)
    cust.logOrders.add(order)
    cust.logOrders.add(order)

    reports = pos.reports

    daily = reports.daily
    daily.groupBy(category=True)
    daily.print()

    weekly = reports.weekly
    weekly.groupBy(day=True)
    weekly.print()
    pos.logout()











