app_customer.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from consumer.connect import DataConsumer
  2. from consumer.credentials import getCredentials
  3. from basic_display import BasicDisplay
  4. from cli.select import Selecter
  5. from cli.pass_asker import PassAsker
  6. from cli.customer_action_asker import CustomerActionAsker
  7. from cli.book_asker import BookAsker
  8. from cli.product_select import ProductSelecter
  9. from cli.house_asker import HouseAsker
  10. from cli.confirm import Confirmer
  11. from cli.quantity_asker import QuantityAsker
  12. from datetime import date, timedelta
  13. display = BasicDisplay()
  14. display.title('Customer application')
  15. credentials = getCredentials()
  16. consumer = DataConsumer(credentials)
  17. companyAsker = Selecter('Choose your company')
  18. passAsker = PassAsker()
  19. actionAsker = CustomerActionAsker()
  20. bookAsker = BookAsker()
  21. productAsker = ProductSelecter()
  22. houseAsker = HouseAsker()
  23. confirmAsker = Confirmer()
  24. quantityAsker = QuantityAsker()
  25. # Login phase
  26. compList = consumer.getCompanyNames()
  27. compList.append('dev')
  28. login = companyAsker.selectInto(compList)
  29. pwd = passAsker.ask()
  30. # Dummy password check
  31. if pwd != "":
  32. print('Successfully logged as', login)
  33. else:
  34. print('Name', login, 'not recognized.')
  35. print('(dummy password check : enter an non-empty password)')
  36. exit()
  37. # Action phase
  38. running = True
  39. while running:
  40. ans = actionAsker.ask()
  41. action = ans['action']
  42. if action == 'quit':
  43. running = False
  44. elif action == 'list orders':
  45. bookList = consumer.getOrdersOfCompany(login)
  46. if len(bookList) == 0:
  47. display.error('No book order for', login, 'yet.')
  48. else:
  49. bookId = bookAsker.select(bookList)
  50. itemList = consumer.getItemsOfOrder(bookId)
  51. if len(itemList) == 0:
  52. display.error('No item found for order', bookId)
  53. else:
  54. display.orderedItemList(itemList)
  55. elif action == 'list warehouses':
  56. houseList = consumer.getWarehousesOfCompany(login)
  57. if len(houseList) == 0:
  58. display.error('No warehouse for', login, 'yet.')
  59. else:
  60. display.warehouseList(houseList)
  61. elif action == 'new order':
  62. houseList = consumer.getWarehousesOfCompany(login)
  63. targetHouseId = houseAsker.select(houseList)
  64. proList = consumer.getProductSet()
  65. proSelectNameList = productAsker.selectInto(proList)
  66. productQuantityList = quantityAsker.askForEachOf(proSelectNameList)
  67. dueDate = date.today() + timedelta(15)
  68. if (confirmAsker.confirm()):
  69. consumer.addOrder(login, targetHouseId, proSelectNameList, productQuantityList, dueDate)
  70. consumer.commit()
  71. else:
  72. display.error('Order creation aborted')
  73. else:
  74. display.notImplemented(action)
  75. display.end()