class CtrlClient(object): """docstring for CtrlClient.""" def __init__(self): super(CtrlClient, self).__init__() self.__conn = None self.__client = None self.__clients = [] self.__items = [] self.__loadClients() def __loadClients(self): try: self.__conn = Conn() flag, clients = self.__conn._selectAll(table="client") if not flag: self.__setClients() self.__items = clients except Exception as e: self.__setClients() def __setClients(self): name = "name text" email = "email text" points = "points integer" try: self.__conn = Conn() if not self.__conn._createTable(name, points, email, table="client"): self.__loadClients() except Exception as e: print(e) def getClients(self): if self.__items == None: self.__loadClients() for client in self.__items: self.__setClient(client) return self.__clients def getClient(self, *args): logger.debug(f'Arguments "{args}"') try: self.__conn = Conn() flag, client = self.__conn._selectEach(f'{args[0]}', f'{args[1]}', table="client") logger.debug(f'Return===>Client "{client}" / Flag "{flag}"') self.__client = Client(*client) logger.debug(f'Client===>ObjClient "{self.__client}"') return flag, self.__client except Exception as e: print("Exception GetClient", e) return flag, None def __setClient(self, client): item = Client(*client) self.__clients.append(item)
class CtrlProduct(object): """docstring for CtrlProduct.""" def __init__(self): super(CtrlProduct, self).__init__() self.__conn = None self.__products = [] self.__items = [] self.__loadProducts() def __loadProducts(self): try: self.__conn = Conn() flag, products = self.__conn._selectAll(table="product") if not flag: self.__setProducts() self.__items = products except Exception as e: self.__setProducts() def __setProducts(self): name = "name text" description = "description text" price = "price real" image = "image text" try: self.__conn = Conn() if not self.__conn._createTable(name, description, price, image, table="product"): self.__loadProducts() except Exception as e: print(e) def getProduct(self, name): try: self.__conn = Conn() flag, product = self.__conn._selectEach("name", name, table="product") product = Product(*product) return flag, product except Exception as e: print(e) return flag def getProducts(self): if self.__items == None: self.__loadProducts() for product in self.__items: self.__setProduct(product) return self.__products def __setProduct(self, product): item = Product(*product) self.__products.append(item)
class CtrlSale(object): """docstring for CtrlSale.""" def __init__(self): super(CtrlSale, self).__init__() self._id = None self.__con = None self.__sale = None self._sales = [] self.__loadSales() self.__status = False self.__ctrlProduct = CtrlProduct() self.__ctrlClient = CtrlClient() def _setShop(self, *args): self.__sale = Sale(*args) return self.__sale def _getClient(self, name): _, client = self.__ctrlClient.getClient('name', name) return client def _getProducts(self, sale): products = [] qtd = [] for item in sale: _, product = self.__ctrlProduct.getProduct(item[2]) qtd.append(item[3]) products.append(product) return products, qtd def __loadSales(self): try: self.__conn = Conn() flag, sales = self.__conn._selectAll(table="sale") if not flag: self.__setSales() self._sales = sales self._id = self._sales[-1][0] + 1 print(self._id) except Exception as e: print("EXCEPTION LOAD_SALES", e) self.__setSales() def _getSale(self, id): try: self.__conn = Conn() flag, sale = self.__conn._selectAllWhere("id", id, table="sale") return flag, sale except Exception as e: print(e) return flag def __setSales(self): id = "id integer" client = "client text" product = "product text" quantity = "quantity integer" value = "value real" try: self.__conn = Conn() if not self.__conn._createTable( id, client, product, quantity, value, table="sale"): self.__loadSales() except Exception as e: print("SET SALES", e) def __insertSale(self, *args): logger.debug(f'Return===>SALE "{args}"') try: self.__conn = Conn() self.__conn._insertItem(args[0], args[1], table="sale") except Exception as e: print("INSERT_SALE============>", e) def _setSale(self, sale): fields = "id,client,product,quantity,value" sale = json.loads(sale) if self._id == None: self.__loadSales() for i, _ in enumerate(sale['userBasket']): values = f"'{self._id}','{sale['userLogged']}','{sale['userBasket'][str(i)]['item']}','{sale['userBasket'][str(i)]['qtd']}','{sale['userBasket'][str(i)]['price']}'" try: self.__insertSale(fields, values) self.__status = True except Exception as e: self.__status = False print("INSERT_STATUS ============>", e) return self.__status def __repr__(self): return self.content