Beispiel #1
0
 def Post(name):
     exercise = ExerciseType.NullObject()
     if ExerciseType.Count(name) == 0:
         exercise = ExerciseType(name=name.lower())
         Session.add(exercise)
         Session.commit()
     else:
         exercise = ExerciseType.GetByName(name)
     return exercise
Beispiel #2
0
class PurchaseLogic(object):
    COL_ID = 0
    COL_ITEM_ID = 1
    COL_ITEM_BCODE = 2
    COL_ITEM_DESC = 3
    COL_QTY = 4
    COL_ITEM_UNIT = 5
    COL_COST = 6
    COL_ITEM_SELLING = 7
    COL_EXPIRY = 8
    #COL_ITEM_TAX_CAT = 9

    SetSupplierList = None
    SetTaxCategoryList = None
    GetSupplier = None
    GetTaxCategory = None
    SetSupplier = None
    SetTaxCategory = None
    SetListValue = None
    GetListValue = None
    SetRow = None
    Lock = None
    GetPurchaseBillId = None
    HasItems = None
    ShowNavBar = None
    HideNavBar = None
    PurchaseBillIdError = None

    def __init__(self, userSession, peripherals):
        self.userSession = userSession
        self.peripherals = peripherals

        self.session = Session()

    def __del__(self):
        self.session.close()

    def Start(self):
        self.SetSupplierList(Supplier)
        self.SetTaxCategoryList(TaxCategory)
        try:
            query = self.session.query(PurchaseBill)
            last = query[-1]
            self.PurchaseBill = last
            self.Display()
        except IndexError:
            self.PurchaseBill = None
            self.New()

    def OnBack(self, event):
        if self.PurchaseBill == None:
            try:
                query = self.session.query(PurchaseBill)
                last = query[-1]
                self.PurchaseBill = last
                self.Display()
            except IndexError:
                pass
        else:
            try:
                self.PurchaseBill = self.session.query(PurchaseBill).\
                                    filter(PurchaseBill.id == (self.PurchaseBill.id - 1)).one()
                self.Display()
            except NoResultFound:
                print "No more behind"

    def OnForward(self, event):
        if self.PurchaseBill != None:
            try:
                self.PurchaseBill = self.session.query(PurchaseBill).\
                                    filter(PurchaseBill.id == (self.PurchaseBill.id + 1)).one()
                self.Display()
            except NoResultFound:
                self.PurchaseBill = None
                self.New()

    def GoToPurchaseBill(self, billid):
        try:
            self.PurchaseBill = self.session.query(PurchaseBill).\
                                    filter(PurchaseBill.id == (billid)).one()
            self.Display()
        except NoResultFound:
            if self.PurchaseBill != None:
                self.SetPurchaseBillId(str(self.PurchaseBill.id))
            else:
                self.New()
            self.PurchaseBillIdError()

    def New(self):
        self.ClearAll()
        self.SetPurchaseBillId('New')
        self.SetDate(datetime.now())
        self.SetSupplier(0)
        self.SetTaxCategory(0)

    def Display(self):
        self.ClearAll()
        if self.PurchaseBill == None:
            return

        self.SetPurchaseBillId(str(self.PurchaseBill.id))
        self.SetDate(self.PurchaseBill.time)
        self.SetSupplier(self.PurchaseBill.supplier.id)

        for purchase in self.PurchaseBill.purchases:
            row = self.AppendRow()
            self.SetRow(row, [
                purchase.id, purchase.item.id, purchase.item.bcode,
                purchase.item.desc, purchase.qty, purchase.item.unit,
                purchase.cost, purchase.item.selling, purchase.expiry
            ])
            self.SetForExistingItem(row)
            self.SetTaxCategory(purchase.item.taxCategory.id)

    def Save(self, event):
        try:
            billId = int(self.GetPurchaseBillId())
        except ValueError:
            billId = 0

        if self.PurchaseBill == None:
            self.SaveNew()
        else:
            self.OverWrite()

    def AddNewItem(self, row):
        item = Item(bcode=self.GetListValue(row, self.COL_ITEM_BCODE),
                    desc=self.GetListValue(row, self.COL_ITEM_DESC),
                    unit=self.GetListValue(row, self.COL_ITEM_UNIT),
                    selling=self.GetListValue(row, self.COL_ITEM_SELLING),
                    stockStart=0,
                    stockIn=0,
                    stockOut=0,
                    taxCategory_id=self.GetTaxCategory())

        self.session.add(item)
        self.session.flush()

        if item.bcode == '':
            item.bcode = 'S' + (str(item.id)).upper().zfill(4)

        return item

    def OverWrite(self):
        print "OverWriting"
        try:
            self.PurchaseBill.time = self.GetDate()
            self.PurchaseBill.supplier_id = self.GetSupplier()
            self.PurchaseBill.user_id = self.userSession.user.id

            for row in range(self.GetRowCount()):
                purchaseid = self.GetListValue(row, self.COL_ID)
                if purchaseid == 0:
                    print "saving new"
                    self.SaveNewPurchase(row, self.PurchaseBill)
                else:
                    print "saving old"
                    self.OverWritePurchase(purchaseid, row, self.PurchaseBill)

        except Exception, err:
            self.session.rollback()
            print('ERROR: %s\n' % str(err))

            print "error bill not saved"
        else:
Beispiel #3
0
class SessionManager(object):
    user = None

    def __init__(self):
        self.dbsession = Session()

    def __del__(self):
        self.dbsession.close()

    def IsLoggedIn(self):
        if self.user == None:
            return False

        return True

    def Login(self, userid, password):
        user = self.dbsession.query(User).filter(User.id == userid).first()

        print userid
        print user

        if user != None:
            if user.password == password:
                if 'LOGIN' in user.privilages:
                    self.user = user
                    self.loginSession()
                    return True
        return False

    def Logout(self):
        self.logoutSession()
        self.user = None

    def loginSession(self):
        now = datetime.now()

        self.userSession = UserSession(user_id=self.user.id,
                                       time=now,
                                       host='127.0.0.1',
                                       port=0)

        self.dbsession.add(self.userSession)
        self.dbsession.commit()

        sessionlog = UserSessionLog(user_id=self.user.id,
                                    time=now,
                                    host=self.userSession.host,
                                    port=self.userSession.port,
                                    action='LOGIN')

        self.dbsession.add(sessionlog)
        self.dbsession.commit()

    def logoutSession(self):
        if self.userSession != None:
            now = datetime.now()

            sessionlog = UserSessionLog(user_id=self.user.id,
                                        time=now,
                                        host=self.userSession.host,
                                        port=self.userSession.port,
                                        action='LOGOUT')

            self.dbsession.add(sessionlog)

            self.dbsession.delete(self.userSession)

            self.dbsession.commit()

            self.userSession = None