def Authenticate(acc_info: LoginReq): account = models.Accounts.query.filter_by( account_name=acc_info.user_name).first() if not account: raise ErrorRsp(code=400, message='Tài khoản không tồn tại', msg='Tài khoản không tồn tại') if str(hashlib.md5(acc_info.password.encode( 'utf-8')).hexdigest()) != account.account_password: raise ErrorRsp(code=400, message='Mật khẩu không chính xác', msg='Mật khẩu không chính xác') return account.serialize()
def CreateOrder(order: CreateOrderReq): create_order = models.Orders(customer_id=order.customer_id, employee_id=order.employee_id, order_date=order.order_date, type=order.type, total=order.total, note=order.note, delete_at=order.delete_at) db.session.add(create_order) db.session.commit() for order_detail in order.order_detail_list: order_book = models.Books.query.get(order_detail['book_id']) order_book.new_amount -= order_detail['quantity'] if order_book.new_amount < 0: raise ErrorRsp(code=400, message='Số lượng sách tồn kho đã hết.') new_order_detail = models.Orderdetails( order_id=create_order.serialize()['order_id'], book_id=order_detail['book_id'], retail_price=order_book.serialize()['retail_price'], discount=order_book.serialize()['discount'], total=(1 - order_book.serialize()['discount']) * (order_book.serialize()['retail_price'] * order_detail['quantity']), quantity=order_detail['quantity']) create_order.orderdetails.append(new_order_detail) db.session.commit() return create_order.serialize()
def CreateBorrowTicket(req: CreateBorrowTicketReq): create_borrow_ticket = models.Borrowtickets( customer_id=req.customer_id, employee_id=req.employee_id, quantity=len(req.borrow_book_ids), borrow_date=datetime.now(), appointment_date=datetime.now() + timedelta(days=14), note=req.note) db.session.begin_nested() db.session.add(create_borrow_ticket) db.session.commit() print(create_borrow_ticket.serialize()) count = 0 for borrow_ticket_detail in req.borrow_book_ids: borrow_book = models.Books.query.get(borrow_ticket_detail) count += 1 if borrow_book and count <= 3: models.Books.old_amount -= 1 new_borrow_ticket_detail = models.Borrowticketdetails( book_id=borrow_ticket_detail, borrow_ticket_id=create_borrow_ticket.serialize() ['borrow_ticket_id']) create_borrow_ticket.borrow_ticket_detail.append( new_borrow_ticket_detail) else: db.session.rollback() raise ErrorRsp(code=400, message='nd', msg='sss') db.session.commit() return create_borrow_ticket.serialize()
def CreateEmployeeAccount(req: CreateEmployeeAccountReq): is_exist_account_name_employee = models.Accounts.query.filter( models.Accounts.account_name == req.account_name).first() is_exist_email_phone_employee = models.Employees.query.filter( or_(models.Employees.email == req.email, models.Employees.phone == req.phone, models.Employees.identity_id == req.identity_id)).first() if is_exist_account_name_employee: raise ErrorRsp(code=400, message='Tài khoản tồn tại', msg='Tài khoản tồn tại') hashed_password = hashlib.md5( req.account_password.encode('utf-8')).hexdigest() create_account = models.Accounts(account_name=req.account_name, account_password=hashed_password, note=req.note, role_id=req.role_id) db.session.begin_nested() db.session.add(create_account) db.session.commit() if is_exist_email_phone_employee: db.session.rollback() raise ErrorRsp( code=400, message='số điện thoại, chứng minh nhân dân hoặc email đẫ tồn tại', msg='số điện thoại, chứng minh nhân dân hoặc email đẫ tồn tại') create_employee = models.Employees(identity_id=req.identity_id, account_id=create_account.account_id, last_name=req.last_name, first_name=req.first_name, phone=req.phone, email=req.email, birth_date=req.birth_date, hire_date=req.hire_date, address=req.address, gender=req.gender, image=req.image, basic_rate=req.basic_rate, note=req.note) db.session.add(create_employee) db.session.commit() return create_account.serialize(), create_employee.serialize()
def CreateCustomerAccount(req: CreateCustomerAccountReq): is_exist_account_name_customer = models.Accounts.query.filter( models.Accounts.account_name == req.account_name).first() is_exist_email_phone_customer = models.Customers.query.filter( or_(models.Customers.email == req.email, models.Customers.phone == req.phone)).first() if is_exist_account_name_customer: raise ErrorRsp(code=400, message='Tài khoản tồn tại', msg='Tài khoản tồn tại') hashed_password = hashlib.md5( req.account_password.encode('utf-8')).hexdigest() create_account = models.Accounts(account_name=req.account_name, account_password=hashed_password, role_id=req.role_id) db.session.begin_nested() db.session.add(create_account) db.session.commit() if is_exist_email_phone_customer: db.session.rollback() raise ErrorRsp( code=400, message='số điện thoại, chứng minh nhân dân hoặc email đẫ tồn tại', msg='số điện thoại, chứng minh nhân dânhoặc email đẫ tồn tại') create_customer = models.Customers(identity_id=req.identity_id, account_id=create_account.account_id, last_name=req.last_name, first_name=req.first_name, phone=req.phone, student_code=req.student_code, birth_date=req.birth_date, address=req.address, gender=req.gender, email=req.email) db.session.add(create_customer) db.session.commit() return create_account.serialize(), create_customer.serialize()
def ChangePassword(req: ChangePasswordReq): account = models.Accounts.query.get(int(req.account_id)) hashed_new_password = str( hashlib.md5(req.new_password.strip().encode("utf-8")).hexdigest()) hashed_current_password = str( hashlib.md5(req.current_password.strip().encode("utf-8")).hexdigest()) if account.account_password == hashed_current_password: account.account_password = hashed_new_password db.session.commit() else: raise ErrorRsp(code=400, message='Mật khẩu không chính xác', msg="Mật khẩu không chính xác") return account.serialize()
def GetBooksByPage(req): has_next, has_prev, books = BookRep.GetBooksByPage(req) result = {"has_next": has_next, "has_prev": has_prev, "books": books} if (req.per_page == 0): raise ErrorRsp(code=400, message='Per page không được bằng 0') return result