def get(self): claims = get_jwt_claims() if not claims['active']: return { 'message': 'Error # 171 in Customer Resource, You have not been activated by the admin' }, 400 username = UserModel.find_by_user(get_jwt_identity()) if not claims['is_superuser']: approved_zid_list = VbusinessModel.find_all_business_list() if username.businessId not in approved_zid_list: return { 'message': 'Error # 182 in Customer Resource, You have not been authorized to use this business' }, 400 else: employee_code_list = HrmstModel.find_all_employee_list() return { 'Number of Customers': len([ cus.json() for cus in CacusModel.find_customers_by_sp( employee_code_list) ]) }, 200 try: child_list = HierarchyModel.find_by_child_of_code_single_user( username.employeeCode) child_list = [hier.json()['employee_code'] for hier in child_list] except Exception as e: print(e) if len(child_list) == 0: final_list = [username.employeeCode] else: try: full_list = HierarchyModel.find_all_hierarchy() full_list = [{ 'child': hier.json()['employee_code'], 'parent': hier.json()['child_of_code'] } for hier in full_list] except Exception as e: print(e) final_list = [username.employeeCode] for i in final_list: for j in full_list: if i == j['parent']: final_list.append(j['child']) return { 'Number of Customers': len([ cus.json() for cus in CacusModel.find_customers_by_sp(final_list) ]) }, 200
def get(self): claims = get_jwt_claims() if not claims['active']: return { 'message': 'Error # 25 in Order Resource, You have not been activated by the admin' }, 400 username = UserModel.find_by_user(get_jwt_identity()) ztime = datetime.datetime.now().date() ztime_31 = ztime - datetime.timedelta(31) try: child_list = HierarchyModel.find_by_child_of_code_single_user( username.employeeCode) child_list = [hier.json()['employee_code'] for hier in child_list] except Exception as e: print(e) if len(child_list) == 0: final_list = [username.employeeCode] else: try: full_list = HierarchyModel.find_all_hierarchy() full_list = [{ 'child': hier.json()['employee_code'], 'parent': hier.json()['child_of_code'] } for hier in full_list] except Exception as e: print(e) final_list = [username.employeeCode] for i in final_list: for j in full_list: if i == j['parent']: final_list.append(j['child']) terminal_list = UserModel.find_by_user_list(final_list) terminal_list = [term.json()['terminal'] for term in terminal_list] try: confirmedOrders = OpmobModel.find_confirmed( terminal_list, ztime_31) except Exception as e: print(e) return {'message': 'No orders created under your name'}, 400 invoice_no = '' count = 0 for orders in confirmedOrders: if invoice_no != orders.json()['invoice_no']: count += 1 invoice_no = orders.json()['invoice_no'] else: continue return {'Number_of_confirmedOrders': count}, 200
def get(self): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'admin previlege required'}, 400 parent_list = [ parent.json() for parent in HierarchyModel.find_all_hierarchy() ] return parent_list, 200
def get(self): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'admin previlege required'}, 400 parent_list = [ parent.json()['child_of_code'] for parent in HierarchyModel.find_all_hierarchy() ] total_hierarchy_employee_code_list = [ total.json()['employee_code'] for total in HierarchyModel.find_all_hierarchy() ] non_parent = list( set(total_hierarchy_employee_code_list) - set(parent_list)) total_hierarchy = [ nparent.json() for nparent in HierarchyModel.find_all_hierarchy() if nparent.json()['employee_code'] in non_parent ] return total_hierarchy, 200
def delete(self, username): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'admin previlege required'}, 400 current_user = UserModel.find_by_user(get_jwt_identity()) user = UserModel.find_by_user(username) if not user: return {'message': 'This user does not exist in our system'}, 400 if current_user.username == HierarchyModel.find_by_hierarchy( username).username: return {'message': 'You are not allowed to delete yourself'}, 400 if not current_user.businessId == HierarchyModel.find_by_hierarchy( username).business_Id: return {'message': 'You are not the admin for this user'}, 400 parent_list = [ parent.json()['child_of_code'] for parent in HierarchyModel.find_all_hierarchy() ] if not user.employeeCode in parent_list: hierarchyDetail = HierarchyModel.find_by_hierarchy(username) hierarchyDetail.delete_from_db() userDetail = UserModel.find_by_user(username) userDetail.status = 'inactive' userDetail.save_to_db() return { 'message': 'User has been deleted from your hierarchy' }, 200 else: return { 'message': 'You cannot delete this user, please replace child first' }
def delete(self, username): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'admin previlege required'},400 if not UserModel.find_by_user(username): return {'message':'This user does not exist in our system'},400 parent_list = [parent.json()['child_of_code'] for parent in HierarchyModel.find_all_hierarchy()] if not username in parent_list: userDetail = UserModel.find_by_user(username) userDetail.delete_from_db() return {'message':'User has been deleted from our system'},200 return {'message':'User still has children please assign children to other parent and then delete this user'},400
def get(self): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'admin previlege required'},400 current_user = UserModel.find_by_user(get_jwt_identity()) zid = current_user.businessId dataActive = [statusL.json() for statusL in UserModel.find_by_status(zid,'active')] dataHierarchy = [hierarchy.json() for hierarchy in HierarchyModel.find_all_hierarchy()] for i in dataActive: i['child_of_code'] = '' i['child_of_name'] = '' for j in dataHierarchy: if i['employeeCode'] == j['employee_code']: i['child_of_code'] = j['child_of_code'] i['child_of_name'] = j['child_of_name'] return dataActive,200
def put(self): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'admin previlege required'}, 400 current_user = UserModel.find_by_user(get_jwt_identity()) json_data = request.get_json() if not json_data: return {'message': 'No input data provided'}, 400 try: data = hierarchyUpdateSchema.load(json_data).data except ValidationError as err: return err.messages, 400 print(data) if not current_user.businessId == data['business_Id']: return {'message': 'You are not the admin for this user'}, 400 employee_to_change = HierarchyModel.find_by_employee_code( data['employee_code']) if employee_to_change.child_of_code == data['child_of_code']: return { 'message': 'You cannot change the parent to the already existing parent' }, 400 employee_to_change.child_of_code = data['child_of_code'] employee_to_change.child_of_name = data['child_of_name'] try: employee_to_change.save_to_db() return {'message': 'Employee hierarchy has been updated'}, 200 except: return { 'message': 'Something went wrong while updating information to your server' }, 400
def get(self): claims = get_jwt_claims() if not claims['active']: return { 'message': 'Error # 25 in Order Resource, You have not been activated by the admin' }, 400 username = UserModel.find_by_user(get_jwt_identity()) ztime = datetime.datetime.now().date() ztime_31 = ztime - datetime.timedelta(31) try: child_list = HierarchyModel.find_by_child_of_code_single_user( username.employeeCode) child_list = [hier.json()['employee_code'] for hier in child_list] except Exception as e: print(e) if len(child_list) == 0: final_list = [username.employeeCode] else: try: full_list = HierarchyModel.find_all_hierarchy() full_list = [{ 'child': hier.json()['employee_code'], 'parent': hier.json()['child_of_code'] } for hier in full_list] except Exception as e: print(e) final_list = [username.employeeCode] for i in final_list: for j in full_list: if i == j['parent']: final_list.append(j['child']) terminal_list = UserModel.find_by_user_list(final_list) terminal_list = [term.json()['terminal'] for term in terminal_list] try: notConfirmedOrders = OpmobModel.find_not_confirmed( terminal_list, ztime_31) except Exception as e: print(e) return {'message': 'No orders created under your name'}, 400 reOrders = [] invoice_no = '' for orders in notConfirmedOrders: print(invoice_no) if invoice_no != orders.json()['invoice_no']: newOrderDict = {} newOrderDict['Entry_Date'] = orders.json()['Entry_Date'] newOrderDict['employeeCode'] = orders.json()['employeeCode'] newOrderDict['businessId'] = orders.json()['businessId'] newOrderDict['invoice_no'] = orders.json()['invoice_no'] newOrderDict['customerCode'] = orders.json()['customerCode'] newOrderDict['customerName'] = orders.json()['customerName'] products = [] orderTotal = 0 for ordersProduct in OpmobModel.find_by_invoiceno( orders.json()['invoice_no']): orderTotal += ordersProduct.json()['orderLineTotal'] invoice_product = { 'productCode': ordersProduct.json()['productCode'], 'productName': ordersProduct.json()['productName'], 'orderQty': ordersProduct.json()['orderQty'], 'orderPrice': ordersProduct.json()['orderPrice'], 'orderLineTotal': ordersProduct.json()['orderLineTotal'] } products.append(invoice_product) newOrderDict['orderTotal'] = orderTotal newOrderDict['products'] = products invoice_no = orders.json()['invoice_no'] reOrders.append(newOrderDict) else: continue return reOrders, 200
def post(self): claims = get_jwt_claims() if not claims['active']: return { 'message': 'Error # 25 in Order Resource, You have not been activated by the admin' }, 400 username = UserModel.find_by_user(get_jwt_identity()) approved_zid_list = VbusinessModel.find_all_business_list() if username.businessId not in approved_zid_list: return { 'message': 'Error # 182 in Customer Resource, You have not been authorized to use this business' }, 400 json_data = request.get_json() if not json_data: return {'message': 'No input data provided'}, 400 try: data = opmobSchemas.load(json_data).data except ValidationError as err: return err.messages, 400 try: child_list = HierarchyModel.find_by_child_of_code_single_user( username.employeeCode) child_list = [hier.json()['employee_code'] for hier in child_list] except Exception as e: print(e) if len(child_list) == 0: final_list = [username.employeeCode] else: try: full_list = HierarchyModel.find_all_hierarchy() full_list = [{ 'child': hier.json()['employee_code'], 'parent': hier.json()['child_of_code'] } for hier in full_list] except Exception as e: print(e) final_list = [username.employeeCode] for i in final_list: for j in full_list: if i == j['parent']: final_list.append(j['child']) for d in data: cacusSp = CacusModel.find_by_customerId(d['zid'], d['xcus']).json() sp_list = [ cacusSp['cus_salesman'], cacusSp['cus_salesman1'], cacusSp['cus_salesman2'], cacusSp['cus_salesman3'] ] if len(set(sp_list).intersection(set(final_list))) == 0: return { 'message': 'You are not allowed to place an order for this customer' }, 400 ztime = datetime.datetime.now() xdate = datetime.datetime.now().date() xsl = clean(str(OpmobModel.find_last_xsl().xsl)) if xsl == 'None': xsl = 0 else: xsl = int(xsl) invoicesl = clean(str(OpmobModel.find_last_invoicesl().invoicesl)) if invoicesl == 'None': invoicesl = 0 else: invoicesl = int(invoicesl) mainList = [] for d in data: invoicesl = invoicesl + 1 xroword = 1 for i in (d['order']): #update all static values i['xcus'] = d['xcus'] try: i['xlat'] = d['xlat'] except: i['xlat'] = 0 try: i['xlong'] = d['xlong'] except: i['xlong'] = 0 approved_zid_list = VbusinessModel.find_all_business_list() if d['zid'] not in approved_zid_list: return { 'message': 'Error # 182 in Customer Resource, You have not been authorized to use this business' }, 400 i['zid'] = d['zid'] i['ztime'] = self.myconverter(ztime) i['zutime'] = self.myconverter(ztime) i['xdate'] = self.myconverter2(xdate) i['username'] = username.username i['xterminal'] = username.terminal i['xroword'] = xroword xroword = xroword + 1 xsl = xsl + 1 i['xsl'] = xsl i['invoicesl'] = invoicesl i['invoiceno'] = str(username.terminal) + str(invoicesl) # i['xemp'] = [item['xemp'] for item in busIdempCodeList if item.get('zid','') == i['zid']][0] i['xemp'] = username.employeeCode i['xcusname'] = CacusModel.query.filter_by( zid=i['zid']).filter_by(xcus=i['xcus']).first().xorg i['xcusadd'] = CacusModel.query.filter_by( zid=i['zid']).filter_by(xcus=i['xcus']).first().xadd1 i['xdesc'] = CaitemModel.query.filter_by( zid=i['zid']).filter_by(xitem=i['xitem']).first().xdesc xstdprice = CaitemModel.query.filter_by( zid=i['zid']).filter_by(xitem=i['xitem']).first().xstdprice xpricecat = CaitemModel.query.filter_by( zid=i['zid']).filter_by(xitem=i['xitem']).first().xpricecat print(xstdprice, 'xstdprice') print(xpricecat, 'xpricecat') try: xqtycat = OpspprcModel.query.filter_by( zid=i['zid']).filter_by( xpricecat=xpricecat).first().xqty except: xqtycat = 0 try: xdisc = OpspprcModel.query.filter_by( zid=i['zid']).filter_by( xpricecat=xpricecat).first().xdisc except: xdisc = 0 print(xqtycat, 'xqtycat') print(xdisc, 'xdisc') if i['xqty'] >= xqtycat: i['xprice'] = xstdprice - xdisc else: i['xprice'] = xstdprice i['xlinetotal'] = i['xprice'] * i['xqty'] print(i['xprice'], 'xprice') print(i['xqty'], 'xqty') print(i['xlinetotal'], 'xlinetotal') i['xstatusord'] = "New" i['xordernum'] = "" mainList.append(i) ######################################### orders_json_list = [] ######################################### for orders in mainList: orderDetail = OpmobModel(zid=orders['zid'], ztime=orders['ztime'], zutime=orders['zutime'], invoiceno=orders['invoiceno'], invoicesl=orders['invoicesl'], username=orders['username'], xemp=orders['xemp'], xcus=orders['xcus'], xcusname=orders['xcusname'], xcusadd=orders['xcusadd'], xitem=orders['xitem'], xdesc=orders['xdesc'], xqty=orders['xqty'], xprice=orders['xprice'], xstatusord=orders['xstatusord'], xordernum=orders['xordernum'], xroword=orders['xroword'], xterminal=orders['xterminal'], xdate=orders['xdate'], xsl=orders['xsl'], xlat=orders['xlat'], xlong=orders['xlong'], xlinetotal=orders['xlinetotal'], xtra1=None, xtra2=None, xtra3=None, xtra4=None, xtra5=None) try: orderDetail.save_to_db() orders_json_list.append(orderDetail.get_json_for_celery_db()) except Exception as e: print(e) return { "message": "An error occured inserting the customer" }, 400 #################################### add_all_rows_to_client_db_by_celery.delay(orderDetail.__tablename__, orders_json_list) #################################### return mainList, 200
def delete(self, invoiceno): claims = get_jwt_claims() if not claims['active']: return { 'message': 'Error # 25 in Order Resource, You have not been activated by the admin' }, 400 username = UserModel.find_by_user(get_jwt_identity()) try: child_list = HierarchyModel.find_by_child_of_code_single_user( username.employeeCode) child_list = [hier.json()['employee_code'] for hier in child_list] except Exception as e: print(e) if len(child_list) == 0: final_list = [username.employeeCode] else: try: full_list = HierarchyModel.find_all_hierarchy() full_list = [{ 'child': hier.json()['employee_code'], 'parent': hier.json()['child_of_code'] } for hier in full_list] except Exception as e: print(e) final_list = [username.employeeCode] for i in final_list: for j in full_list: if i == j['parent']: final_list.append(j['child']) terminal_list = UserModel.find_by_user_list(final_list) terminal_list = [term.json()['terminal'] for term in terminal_list] if OpmobModel.find_by_invoiceno( invoiceno)[0].xterminal not in terminal_list: return {'message': 'You are not allowed to delete this order'}, 400 orderNum = [ ordernum.xordernum for ordernum in OpmobModel.find_by_invoiceno(invoiceno) ] if '' not in orderNum: return { 'message': 'You cannot delete this Order as it has already been confirmed' }, 400 orderDetail = OpmobModel.find_by_invoiceno(invoiceno) for orders in orderDetail: orders.delete_from_db() #################################### delete_key_value_pair_list = [('invoiceno', invoiceno)] #################################### delete_from_client_db_with_custom_key_by_celery.delay( OpmobModel.__tablename__, delete_key_value_pair_list) return {'message': 'Your order has been deleted'}, 200
def post(self): json_data = request.get_json() print(json_data,'json_data') if not json_data: return {'message': 'Error # 27 User Resources, No input data provided'},400 try: data = userRegSchema.load(json_data).data except ValidationError as err: return err.messages,400 if data['is_admin'] != '': if UserModel.verify_secret_key(data['is_admin']) == 'is_superuser': pass elif UserModel.verify_secret_key(data['is_admin']) == 'is_admin': pass else: return {'message':'Please provide the correct encryption key'},400 print(data,'data') if UserModel.find_by_user(data['username']): return {'message':'Response # 35 User Resources, User {} already exists'. format(data['username'])},400 if not UserModel.verify_secret_key(data['is_admin']) == 'is_superuser': approved_zid_list = VbusinessModel.find_all_business_list() approved_zid_length = len(approved_zid_list) if approved_zid_length == 0: return {'message':'Error # 44 in User Resources, Super user has not registered any business for you to use'},400 if (data['businessId'] != 0 and data['employeeCode'] != "" and approved_zid_length > 0): if data['businessId'] not in approved_zid_list: return {'message':'Error # 56 User Resources, This business is not authorized in your system please talk to your IT administrator'},400 if UserModel.find_by_busIdempCode(data['username'],data['businessId'],data['employeeCode']): return {'message': 'Error # 59 User Resources, This Business ID and Employee Code already exists talk to your adminstrator to Provide you with a new businessId'},400 if not HrmstModel.find_by_EmployeeDetail(data['businessId'],data['employeeCode']): return {'message':'Error # 62 User Resources, Your Employee Code for Business ID provided does not exist in our system or does not match!'},400 terminalMax = str(db.session.query(func.max(UserModel.terminal)).first()) terminalMax = re.sub('[(",)]','',terminalMax) terminalMax = terminalMax.replace("'","") if terminalMax == 'Super': terminalId = 'T0001' else: terminalId = str(terminalMax) terminalId = increment(terminalId) employee_name = HrmstModel.find_by_EmployeeDetail(data['businessId'],data['employeeCode']).xname else: data['username'] = '******' employee_name = 'Superuser' data['businessId'] = 1 data['employeeCode'] = 'Super' terminalId = 'Super' new_user = UserModel( username = data['username'], password = UserModel.generate_hash(data['password']), employee_name = employee_name, email = data['email'], mobile = data['mobile'], businessId = data['businessId'], employeeCode = data['employeeCode'], terminal = terminalId, is_admin = UserModel.verify_secret_key(data['is_admin']), status = UserModel.verify_active_user(data['is_admin']) ) try: new_user.save_to_db() if UserModel.verify_secret_key(data['is_admin']) == 'is_admin': adminHierarchyDetail = HierarchyModel( username=data['username'], business_Id=data['businessId'], employee_code = data['employeeCode'], employee_name = employee_name, child_of_code = 'Super', child_of_name = 'Superuser' ) adminHierarchyDetail.save_to_db() access_token = create_access_token(identity = data['username']) refresh_token = create_refresh_token(identity = data['username']) current_user = UserModel.find_by_user(data['username']) return { 'message': 'Response # 148 User Resources, User {} was created'.format(data['username']), 'access_token':access_token, 'refresh_token':refresh_token, 'businessId': current_user.businessId, 'employeeCode':current_user.employeeCode, 'userRole': current_user.is_admin },200 except Exception as err: return {'message':'Error # 155 User Resources, Issues with saving to database'},400
def post(self): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'admin previlege required'}, 400 current_user = UserModel.find_by_user(get_jwt_identity()) approved_zid_list = VbusinessModel.find_all_business_list() if (current_user.businessId not in approved_zid_list): return { 'message': 'Error # 180 in Product Resource, You have not been authorized to use this business' }, 400 json_data = request.get_json() if not json_data: return {'message': 'No input data provided'}, 400 try: data = hierarchySchema.load(json_data).data except ValidationError as err: return err.messages, 400 if not UserModel.find_by_user(data['username']): return {'message': 'This user has not registered yet'}, 400 if not HrmstModel.find_by_EmployeeDetail(data['business_Id'], data['employee_code']): return { 'message': 'The employee code you provided does not exist in our system' }, 400 if not current_user.businessId == data['business_Id']: return {'message': 'You are not the admin for this user'}, 400 if HierarchyModel.find_by_hierarchy(data['username']): return { 'message': 'This user name has already been activated by the admin' }, 400 new_user = HierarchyModel(username=data['username'], business_Id=data['business_Id'], employee_code=data['employee_code'], employee_name=data['employee_name'], child_of_code=data['child_of_code'], child_of_name=data['child_of_name']) try: new_user.save_to_db() activeUser = UserModel.find_by_user(data['username']) activeUser.status = 'active' activeUser.save_to_db() return { 'message': 'User has been added to hierarchy and activated' }, 200 except: return {'message': 'Something went wrong'}, 400