def get(self): claims = get_jwt_claims() if not claims['active']: return { 'message': 'Error # 34 in Product Resource, You have not been activated by the admin' }, 400 current_user = UserModel.find_by_user(get_jwt_identity()) if not claims['is_superuser']: approved_zid_list = VbusinessModel.find_all_business_list() business_Id_list = [current_user.businessId] if current_user.businessId not in approved_zid_list: return { 'message': 'Error # 182 in Customer Resource, You have not been authorized to use this business' }, 400 else: business_Id_list = VbusinessModel.find_all_business_list() category_list = CategoryModel.find_all_category_list() all_items = [ item.json() for item in CaitemModel.find_by_zid_category( business_Id_list, category_list) ] product_count = len(all_items) return {'rowcount': product_count}, 200
def get(self): claims = get_jwt_claims() if not claims['active']: return { 'message': 'Error # 34 in Product Resource, You have not been activated by the admin' }, 400 current_user = UserModel.find_by_user(get_jwt_identity()) if not claims['is_superuser']: approved_zid_list = VbusinessModel.find_all_business_list() business_Id_list = [current_user.businessId] if current_user.businessId not in approved_zid_list: return { 'message': 'Error # 182 in Customer Resource, You have not been authorized to use this business' }, 400 else: business_Id_list = VbusinessModel.find_all_business_list() category_list = CategoryModel.find_all_category_list() all_items = [{ 'product_Code': item.json()['product_Code'], 'product_Name': item.json()['product_Name'], 'product_Category': item.json()['product_Category'], 'Sales_Price': item.json()['Sales_Price'], 'Unit': item.json()['Unit'] } for item in CaitemModel.find_by_zid_category( business_Id_list, category_list)] all_priceCat = [{ 'productCatCode': cat.json()['productCatCode'], 'sp_priceQty': cat.json()['sp_priceQty'], 'discountAmount': cat.json()['discountAmount'] } for cat in OpspprcModel.find_by_priceCat_all(business_Id_list)] for i in all_items: i['sp_priceQty'] = 0 i['discountAmount'] = 0 for j in all_priceCat: if i['product_Code'] == j['productCatCode']: i['sp_priceQty'] = j['sp_priceQty'] i['discountAmount'] = j['discountAmount'] return all_items, 200
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 delete(self, businessId, customerId): claims = get_jwt_claims() if not claims['is_admin']: return { 'message': 'Error # 151 in Customer Resource, admin previlige required' }, 400 approved_zid_list = VbusinessModel.find_all_business_list() if businessId not in approved_zid_list: return { 'message': 'Error # 156 in Customer Resource, You have not been authorized to use this business' }, 400 customerDetail = CacusModel.find_by_customerId(businessId, customerId) if customerDetail: customerDetail.delete_from_db() return { 'message': 'Response # 163 in Customer Resources, Customer has been deleted' }, 200
def get(self, businessId): businessIdList = VbusinessModel.find_all_business_list() if businessId not in businessIdList: return {'message':'This business has not been Validated by the super user for you to use'},400 data = [empCode.json() for empCode in HrmstModel.find_by_zid(businessId)] return data,200
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()) if not claims['is_superuser']: approved_zid_list = VbusinessModel.find_all_business_list() business_Id_list = [current_user.businessId] if current_user.businessId not in approved_zid_list: return { 'message': 'Error # 182 in Customer Resource, You have not been authorized to use this business' }, 400 else: business_Id_list = VbusinessModel.find_all_business_list() category_list = CategoryModel.find_all_category_list() all_category = [{ 'businessId': category.json()['businessId'], 'product_Category': category.json()['product_Category'] } for category in CaitemModel.find_product_category(business_Id_list)] approved_category = [ category.json() for category in CategoryModel.find_all_category() ] non_approved_category = [ i for i in all_category if i not in approved_category ] return { 'all_category': all_category, 'approved_category': approved_category, 'non_approved_category': non_approved_category }, 200
def post(self): claims = get_jwt_claims() if not claims['active']: return { 'message': 'Error # 25 in Location 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 locationDetail = LocationModel(ztime=data['ztime'], zid=username.businessId, xemp=username.employeeCode, xlat=data['xlat'], xlong=data['xlong']) try: locationDetail.save_to_db() return {'message': 'Location Saved'}, 200 except: return {'message': 'Something went wrong'}, 400
def get(self, businessId, customerId): claims = get_jwt_claims() if not claims['active']: return { 'message': 'Error # 21 in Customer Resource, You have not been activated by the admin' }, 400 approved_zid_list = VbusinessModel.find_all_business_list() if businessId not in approved_zid_list: return { 'message': 'Error # 24 in Customer Resource, You have not been authorized to use this business' }, 400 customer = CacusModel.find_by_customerId(businessId, customerId) if customer: return customer.json(), 200 return { 'message': 'Response # 30 in Customer Resource, Customer not found' }, 400
def delete(self, businessId, approvedCategory): claims = get_jwt_claims() if not claims['is_admin']: return { 'message': 'Error # 213 in Product Resource, admin prevelige required' }, 400 approved_zid_list = VbusinessModel.find_all_business_list() current_user = UserModel.find_by_user(get_jwt_identity()) if (current_user.businessId not in approved_zid_list) or (businessId not in approved_zid_list): return { 'message': 'Error # 180 in Product Resource, You have not been authorized to use this business' }, 400 if not CaitemModel.find_by_zid_category([businessId], [approvedCategory]): return { 'message': 'Error # 131 in Product Resources, this category or business ID does not exist in our System' }, 400 categoryDetail = CategoryModel.find_by_zid_category( current_user.businessId, approvedCategory) if categoryDetail: categoryDetail.delete_from_db() return { 'message': 'Response # 225 in Product Resources, Category has been deleted' }, 200
def post(self, businessId, customerId): claims = get_jwt_claims() if not claims['is_admin']: return { 'message': 'Error # 41 in Customer Resource, admin previlege required' }, 400 approved_zid_list = VbusinessModel.find_all_business_list() if businessId not in approved_zid_list: return { 'message': 'Error # 46 in Customer Resource, You have not been authorized to use this business' }, 400 customerId = str( db.session.query(func.max( CacusModel.xcus)).filter_by(zid=businessId).first()) customerId = increment(customerId) if CacusModel.find_by_customerId(businessId, customerId): return { 'message': "Error # 54 in Customer Resource, An item with name '{}' already exists." .format(customerId) }, 400 json_data = request.get_json() if not json_data: return { 'message': 'Error # 59 in Customer Resource, No input data provided' }, 400 try: data = cacusSchema.load(json_data).data except ValidationError as err: return err.messages, 400 ztime = datetime.datetime.now() zutime = datetime.datetime.now() xdate = datetime.datetime.today() customerDetail = CacusModel(zid=businessId, ztime=ztime, zutime=zutime, xcus=customerId, xshort=data['xshort'], xadd1=data['xadd1'], xadd2=data['xadd2'], xcity=data['xcity'], xmobile=data['xmobile'], xsp=data['xsp']) try: customerDetail.save_to_db() except Exception as e: print(e) return { "message": "Error # 86 in Customer Resource, An error occured inserting the customer" }, 400 return customerDetail.json(), 200
def put(self, businessId, customerId): claims = get_jwt_claims() if not claims['is_admin']: return { 'message': 'Error # 94 in Customer Resource, admin previlige required' }, 400 approved_zid_list = VbusinessModel.find_all_business_list() if businessId not in approved_zid_list: return { 'message': 'Error # 99 in Customer Resource, You have not been authorized to use this business' }, 400 json_data = request.get_json() if not json_data: return { 'message': 'Error # 104 in Customer Resource, No input data provided' }, 400 try: data = cacusUpdateSchema.load(json_data).data except ValidationError as err: return { 'message': 'Error # 109 in Customer Resource, Validation error in Schemas' }, 400 ztime = datetime.datetime.now() zutime = datetime.datetime.now() xdate = datetime.datetime.now() customerDetail = CacusModel.find_by_customerId(businessId, customerId) if customerDetail is None: customerDetail = CacusModel(zid=businessId, ztime=ztime, zutime=zutime, xcus=customerId, xshort=data['xshort'], xadd1=data['xadd1'], xadd2=data['xadd2'], xcity=data['xcity'], xmobile=data['xmobile'], xsp=data['xsp']) else: customerDetail.zid = businessId customerDetail.zutime = zutime customerDetail.xdate = xdate customerDetail.xcus = customerId customerDetail.xshort = data['xshort'] customerDetail.xadd1 = data['xadd1'] customerDetail.xadd2 = data['xadd2'] customerDetail.xcity = data['xcity'] customerDetail.xmobile = data['xmobile'] customerDetail.xsp = data['xsp'] try: customerDetail.save_to_db() except Exception as e: print(e) return { "message": "Error # 145 in Customer Resource, An error update the customer" }, 400 return customerDetail.json(), 200
def post(self, businessId): claims = get_jwt_claims() if not claims['is_admin']: return {'message': 'admin previlege required'}, 400 approved_zid_list = VbusinessModel.find_all_business_list() current_user = UserModel.find_by_user(get_jwt_identity()) if (current_user.businessId not in approved_zid_list) or (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': 'Error # 186 in Product Resource, No input data provided' }, 400 try: data = categorySchema.load(json_data).data except ValidationError as err: return err.messages, 400 data['approvedCategory'] = html.unescape(data['approvedCategory']) if not CaitemModel.find_by_zid_category([businessId], [data['approvedCategory']]): return { 'message': 'Error # 131 in Product Resources, this category or business ID does not exist in our System' }, 400 if CategoryModel.find_by_zid_category(current_user.businessId, data['approvedCategory']): return { 'message': 'Error # 194 in Product Resources, this category has already been approved' }, 400 categoryDetail = CategoryModel( zid=businessId, approvedCategory=data['approvedCategory'], xtra1=None, xtra2=None, xtra3=None, xtra4=None, xtra5=None) try: categoryDetail.save_to_db() except Exception as e: print(e) return { "message": "Error # 205 in Product Resource, An error occured while saving the product category" }, 400 return categoryDetail.json(), 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 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