main.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. from asyncio.windows_events import NULL
  2. from time import sleep
  3. from app_logging import logger_name, init as init_logging
  4. import logging
  5. logger = logging.getLogger(logger_name)
  6. import requests,json
  7. import datetime
  8. import argparse
  9. init_logging()
  10. logger.debug(__name__)
  11. logger.info("")
  12. logger.info("")
  13. logger.info("")
  14. logger.info("------- welcome to Slash-Dolistripe -------")
  15. # Method to read config file settings
  16. logger.info(" --- -------------------------------------------------------------------------------- ---")
  17. logger.info(" --- ---------------------------------- ARG PHASE ----------------------------------- ---")
  18. logger.info(" --- -------------------------------------------------------------------------------- ---")
  19. parser = argparse.ArgumentParser()
  20. parser.add_argument("-v","--verbosity", help="increase output verbosity",action="store_true")
  21. parser.add_argument("-d","--dry", help="perform a dry run",action="store_true")
  22. parser.add_argument("-m","--mail", help="send invoice per mail to client (you can add a contact mail copy in the reference.conf file)",action="store_true")
  23. parser.add_argument("-p","--planned", help="trigger planned work to ",action="store_true")
  24. args = parser.parse_args()
  25. if args.verbosity:
  26. logger.info("Args : Debug Verbose Enabled")
  27. logger.setLevel(logging.DEBUG)
  28. else :
  29. logger.setLevel(logging.INFO)
  30. args = parser.parse_args()
  31. if args.dry:
  32. logger.info("Args : Dry Run Enabled")
  33. args = parser.parse_args()
  34. if args.mail:
  35. logger.info("Args : send invoice per Mails Enabled")
  36. logger.info(" --- -------------------------------------------------------------------------------- ---")
  37. logger.info(" --- ---------------------------------- CONF PHASE ---------------------------------- ---")
  38. logger.info(" --- -------------------------------------------------------------------------------- ---")
  39. logger.info("Reading Reference ConfIguration File")
  40. import configparser
  41. config = configparser.ConfigParser()
  42. config.optionxform = str # to make the read Case Sensitive
  43. config.read('references.conf')
  44. dolibarr_url = config["credentials"]["dolibarr_url"]
  45. dolibarr_username = config["credentials"]["dolibarr_username"]
  46. dolibarr_password = config["credentials"]["dolibarr_password"]
  47. if args.planned :
  48. dolibarr_planned_work_key = config["credentials"]["planned_work_key"]
  49. dolibarr_planned_work_cron_job_id = config["credentials"]["cron_job_id"]
  50. contact_mail = None
  51. if config.has_option("credentials", "contact_mail") :
  52. contact_mail = config["credentials"]["contact_mail"]
  53. logger.debug("contact mail in configuration is : " + contact_mail)
  54. logger.info(" --- -------------------------------------------------------------------------------- ---")
  55. logger.info(" --- -------------------------------- READ CRM PHASE -------------------------------- ---")
  56. logger.info(" --- -------------------------------------------------------------------------------- ---")
  57. from selenium import webdriver
  58. from selenium.webdriver.common.keys import Keys
  59. from selenium.webdriver.common.by import By
  60. import logging
  61. from selenium.webdriver.remote.remote_connection import LOGGER
  62. LOGGER.setLevel(logging.CRITICAL)
  63. logger.info("CRM Login")
  64. driver = webdriver.Edge()
  65. driver.get(dolibarr_url + "/index.php")
  66. assert "Identifiant" in driver.title
  67. driver.find_element(By.NAME,"username").send_keys(str(dolibarr_username))
  68. pass_field = driver.find_element(By.NAME,"password")
  69. pass_field.send_keys(str(dolibarr_password))
  70. pass_field.send_keys(Keys.RETURN)
  71. logger.info(driver.title)
  72. assert "Accueil" in driver.title
  73. logger.info("login successful")
  74. current_date = datetime.datetime.now()
  75. driver.get(dolibarr_url + "//compta/facture/list.php?leftmenu=customers_bills")
  76. # iterating over links63
  77. logger.info("iterating over invoice")
  78. invoices_table = driver.find_elements(By.XPATH,"//div[contains(@id,'contrat-lines-container')]/div")
  79. invoices_table = driver.find_element(By.CLASS_NAME,'liste')
  80. invoices_entries = invoices_table.find_elements(By.CLASS_NAME,"oddeven")
  81. class invoice_to_process :
  82. link : str
  83. name : str
  84. amount : float
  85. invoices_to_process = set()
  86. logger.info("checkin all invoices to found unpaid ones with amount 0...")
  87. for invoice_entry in invoices_entries :
  88. inv = invoice_to_process()
  89. logger.debug("----------")
  90. item = invoice_entry.find_element(By.CLASS_NAME,"classfortooltip")
  91. inv.name = item.text
  92. inv.link = item.get_attribute("href")
  93. logger.debug("invoice name : " + inv.name)
  94. logger.debug("invoice link : " + inv.link)
  95. inv.amount = float(invoice_entry.find_element(By.CLASS_NAME,"amount").text.replace(',','.').replace(' ',''))
  96. logger.debug("invoice amount : " + str(inv.amount))
  97. status = invoice_entry.find_element(By.CLASS_NAME,"badge-status").text
  98. logger.debug("status : " + str(status))
  99. if str(status) == "Impayée" and inv.amount == 0.0:
  100. logger.info("## An invoice is suitable for processing ! : " + inv.name)
  101. invoices_to_process.add(inv)
  102. logger.debug("----------")
  103. if args.dry:
  104. driver.close()
  105. logger.info("dry run enabled, exiting here...")
  106. exit(0)
  107. logger.info("processing target invoices...")
  108. invoice : invoice_to_process
  109. for invoice in invoices_to_process :
  110. logger.info("processing invoice " + invoice.name + "...")
  111. driver.get(invoice.link + "&action=paid")
  112. buttons = driver.find_element(By.CLASS_NAME,"ui-dialog-buttonset")
  113. logger.debug(buttons.get_attribute("innerHTML"))
  114. buttons.find_element(By.XPATH,'button[contains(text(), "Oui")]').click()
  115. exit(0)