def getMachines(): app.logger.info("In getMachines API, retrieving machines ordered by customers or ca or available with csp") try: _id = request.args['inputId'] _role = request.args['inputRole'] _ca_id = request.args['inputCaId'] if _id and _ca_id and _role == "customer": results = {'results': sql_select('select m.*, ord.ca_id from order_customer ord join machine_customer m on ord.order_id=m.order_id where ord.customer_id="'+_id+'" and ord.ca_id="'+ _ca_id +'" and order_end_date is null')} app.logger.debug("In getMachines API, retrieved machines ordered by customers:", results) return json.dumps(results) elif _id and _ca_id and _role == "csp": results = {'results': sql_select('select m.* from machine m where m.csp_id="' + _id + '";')} app.logger.debug("In getMachines API, retrieved machines available with csp:", results) return json.dumps(results) elif _id and _ca_id and _role == "ca": results = {'results': sql_select('select m.*, ord.customer_id from order_ ord join machine m on ord.order_id=m.order_id where ord.ca_id="' + _ca_id + '" and order_end_date is null')} # print('select * from order_ ord join machine m on ord.order_id=m.order_id where ord.ca_id="' + _ca_id + '" and order_end_date is null') app.logger.debug("In getMachines API, retrieved machines ordered by ca:", results) return json.dumps(results) else: app.logger.debug("In getMachines API: Missing mandatory parameters") return json.dumps({"results": "Missing mandatory parameters"}) except Exception as e: app.logger.error("In getMachines API: Error:", str(e)) return json.dumps({'error': str(e)})
def revenue_history(): app.logger.info( "In revenue_history API, retrieving revenue history for csp or ca") try: _id = request.args['inputId'] _role = request.args['inputRole'] if _id and _role == "ca": results = { 'results': sql_select('select * from customer_bill where ca_id="' + _id + '" and is_paid is True;') } app.logger.debug( "In revenue_history API, retrieved revenue history for ca:", results) # print('select * from customer_bill where customer_id="'+_id+'"') return json.dumps(results) elif _id and _role == "csp": results = { 'results': sql_select('select * from ca_bill where csp_id="' + _id + '" and is_paid is True;') } app.logger.debug( "In revenue_history API, retrieved revenue history for csp:", results) return json.dumps(results) else: app.logger.debug( "In revenue_history API: Missing mandatory parameters") return json.dumps({"results": "Missing mandatory parameters"}) except Exception as e: app.logger.error("In revenue_history API: Error:", str(e)) return json.dumps({'error': str(e)})
def orderHistory(): app.logger.info("In orderHistory API, retrieving order history of customer or csp or ca") try: _id = request.args['inputId'] _role = request.args['inputRole'] _ca_id = request.args['inputCaId'] if _id and _ca_id and _role == "customer": results = {'results': sql_select('select * from order_customer where customer_id="'+_id+'" and ca_id="'+ _ca_id +'" and order_end_date is not null')} app.logger.debug("In orderHistory API, retrieved order history of customer:", results) return json.dumps(results) elif _id and _ca_id and _role == "csp": results = {'results': sql_select('select * from order_csp where csp_id="' + _id + '" and ca_id="' + _ca_id + '" and order_end_date is not null')} app.logger.debug("In orderHistory API, retrieved order history of csp:", results) return json.dumps(results) elif _id and _ca_id and _role == "ca": results = {'results': sql_select('select * from order_ o join receives r on o.order_id=r.order_id where o.ca_id="' + _ca_id + '" and o.order_end_date is not null')} app.logger.debug("In orderHistory API, retrieved order history of ca:", results) # print('select * from order_ o join receives r on o.order_id=r.order_id where o.ca_id="' + _ca_id + '" and o.order_end_date is not null') return json.dumps(results) else: app.logger.debug("In orderHistory API: Missing mandatory parameters") return json.dumps({"results": "Missing mandatory parameters"}) except Exception as e: app.logger.error("In orderHistory API: Error:", str(e)) return json.dumps({'error': str(e)})
def current_bill(): app.logger.info( "In current_bill API, retrieving unpaid current bills for customer or ca" ) try: _id = request.args['inputId'] _role = request.args['inputRole'] if _id and _role == "customer": results = { 'results': sql_select('select * from customer_bill where customer_id="' + _id + '" and is_paid is False;') } app.logger.debug( "In current_bill API, retrieved unpaid current bills for customer:", results) # print('select * from customer_bill where customer_id="'+_id+'"') return json.dumps(results) elif _id and _role == "ca": results = { 'results': sql_select('select * from ca_bill where ca_id="' + _id + '" and is_paid is False;') } app.logger.debug( "In current_bill API, retrieved unpaid current bills for ca:", results) return json.dumps(results) else: app.logger.debug( "In current_bill API: Missing mandatory parameters") return json.dumps({"results": "Missing mandatory parameters"}) except Exception as e: app.logger.error("In current_bill API: Error:", str(e)) return json.dumps({'error': str(e)})
def myCSPs(): app.logger.info("In myCSPs API, retrieving csp's for given ca") try: _ca_id = request.args['inputCaId'] if _ca_id: results = { 'resultsAvailable': sql_select( 'select * from csp c join csp_contracts o on c.csp_id=o.csp_id where o.ca_id=' + _ca_id + ' and c.csp_id not in (select m.csp_id from machine m where m.order_id is not null)' ), 'resultsOccupied': sql_select( 'select * from csp c join csp_contracts o on c.csp_id=o.csp_id where o.ca_id=' + _ca_id + ' and c.csp_id in (select m.csp_id from machine m where m.order_id is not null)' ) } app.logger.debug("In myCSPs API, retrieved csp's for given ca:", results) return json.dumps(results) else: app.logger.debug("In myCSPs API: Missing mandatory parameters") return json.dumps({"results": "Missing mandatory parameters"}) except Exception as e: app.logger.error("In myCSPs API: Error:", str(e)) return json.dumps({'error': str(e)})
def getOffer(): app.logger.info("In getOffer API, retrieving csp's for given ca") try: _ca_id = request.args['inputCaId'] _customer_id = request.args['inputId'] _role = request.args['inputRole'] if _ca_id and _role: if _role == 'ca': return json.dumps({ 'results': sql_select('select * from offer where ca_id = ' + _ca_id + ';') }) if _role == 'customer': return json.dumps({ 'results': sql_select( 'select * from customer c join offer o on c.customer_offer_id=o.offer_id where c.customer_id = ' + _customer_id + ';') }) else: app.logger.debug("In getOffer API: Missing mandatory parameters") return json.dumps({"results": "Missing mandatory parameters"}) except Exception as e: app.logger.error("In current_revenue API: Error:", str(e)) return json.dumps({'error': str(e)})
def login(): app.logger.info("In login API, logging in for ca or csp or customer") # print(request.form) try: _email = request.form['inputEmailLogin'] _password = request.form['inputPasswordLogin'] _role = request.args['inputRole'] if _email and _role == "customer": userRow = sql_select( 'select customer_id, customer_email_id, customer_name, customer_password, customer_bank_account from customer where customer_email_id="' + _email + '"') if check_password_hash(userRow[0][3], _password): results = {'results': userRow} app.logger.debug("In login API, customer - logged in:", results) return json.dumps(results) else: app.logger.error("In login API : Error:", {'error': 'Invalid password'}) return json.dumps({'error': 'Invalid password'}), 500 elif _email and _role == "ca": userRow = sql_select( 'select ca_id, ca_email_id, ca_name, ca_password, ca_bank_account_number from ca where ca_email_id="' + _email + '"') if check_password_hash(userRow[0][3], _password): results = {'results': userRow} app.logger.debug("In login API, ca - logged in:", results) return json.dumps(results) else: app.logger.error("In login API : Error:", {'error': 'Invalid password'}) return json.dumps({'error': 'Invalid password'}), 500 elif _email and _role == "csp": userRow = sql_select( 'select csp_id, csp_email_id, csp_name, csp_password, csp_bank_account_number from csp where csp_email_id="' + _email + '"') if check_password_hash(userRow[0][3], _password): results = {'results': userRow} app.logger.debug("In login API, csp - logged in:", results) return json.dumps(results) else: app.logger.error("In login API : Error:", {'error': 'Invalid password'}) return json.dumps({'error': 'Invalid password'}), 500 else: app.logger.debug("In login API: Missing mandatory parameters") return json.dumps({"results": "Missing mandatory parameters"}), 500 except Exception as e: app.logger.error("In login API: Error:", str(e)) return json.dumps({'error': str(e)})
def generate_bill(): app.logger.info("In generate_bill API, generating bills for ca or customer") try: _id = request.args['inputId'] _role = request.args['inputRole'] conn = mysql.connect() cursor = conn.cursor() messages = [] if _id and _role == "ca": # print(_id, _role) for ca_id, customer_id in sql_select('select order_.ca_id, order_.customer_id from onboards join order_ on order_.customer_id = onboards.customer_id and order_.ca_id = onboards.ca_id where order_.ca_id="' + _id + '";'): # print("Generating bill for customer_id:", customer_id) cursor.callproc('sp_generate_bill_ca', (datetime.now().day, datetime.now().month, datetime.now().year, ca_id, customer_id)) message = cursor.fetchall() if len(message): # print(message[0]) messages.append(message[0]) app.logger.debug("In generate_bill API, generated bill for customer:", message[0]) else: app.logger.error("In generate_bill API: Error in sp_generate_bill_ca:", str(messages[0])) return json.dumps({'Error': str(messages[0])}), 500 conn.commit() return json.dumps({'message': messages}) elif _id and _role == "csp": for csp_id, ca_id in sql_select('select receives.csp_id, order_.ca_id from receives join order_ on order_.order_id = receives.order_id where receives.csp_id="' + _id + '";'): # print("Generating bill for ca_id:", ca_id) cursor.callproc('sp_generate_bill_csp', (datetime.now().day, datetime.now().month, datetime.now().year, csp_id, ca_id)) message = cursor.fetchall() if len(message): # print(message[0]) messages.append(message[0]) app.logger.debug("In generate_bill API, generated bill for ca:", message[0]) else: app.logger.error("In generate_bill API: Error in sp_generate_bill_csp:", str(messages[0])) return json.dumps({'Error': str(messages[0])}), 500 conn.commit() return json.dumps({'message': messages}) else: app.logger.debug("In generate_bill API: Missing mandatory parameters") return json.dumps({"results": "Missing mandatory parameters"}) except Exception as e: app.logger.error("In generate_bill API: Error:", str(e)) return json.dumps({'error': str(e)})
def myCustomers(): app.logger.info("In myCustomers API, retrieving customers's for given ca") try: _ca_id = request.args['inputCaId'] if _ca_id: results = {'results': sql_select('select * from customer c join onboards o on c.customer_id=o.customer_id where c.customer_isDelete=0 and o.ca_id='+_ca_id)} app.logger.debug("In myCustomers API, retrieved customers's for given ca:", results) return json.dumps(results) else: app.logger.debug("In myCustomers API: Missing mandatory parameters") return json.dumps({"results": "Missing mandatory parameters"}) except Exception as e: app.logger.error("In myCustomers API: Error:", str(e)) return json.dumps({'error': str(e)})