Help with auto calculating inventory

Help with auto calculating inventory

Hello, I am currently trying to create a program that will auto calculate a refill field and a threshold field and schedule it to run every night. We want to take 60 days worth of data and divide it by 2 to get an average for a month and then divide it by two again to get our threshold. I have not ran my code yet because I am nervous if it is wrong, it will change all the values to wrong. Any help/advice would be greatly appreciated. 

  1. //Fetching all records from Book and Invoice Tables
  2. Books = Books[ID != 0];
  3. Invoice_Data = Invoice_Data[ID != 0];
  4. //getting a value that is 2 months less than today's date
  5. two_months_ago = subMonth(zoho.currentdate,2);
  6. //If date field is in the last 60 days. 
  7. if(Invoice_Data.Date_field > two_months_ago)
  8. {
  9. for each  Each_Book in Invoice_Data
  10. {
  11. //Counting all the books in invoice in last 2 months and dividing by 2
  12. Total_Books = Invoice_Data[Book = Book].count();
  13. Total_Books2 = Total_Books / 2;
  14. Total_Books3 = Total_Books2 / 2;
  15. //converting decimal to Long
  16. Total_Books_Threshold = Total_Books3.toLong();
  17. Total_Books_Refill = Total_Books2.toLong();
  18. //Updating refill/threshold 
  19. Books.Refill_To=Total_Books_Refill;
  20. Books.Refill_Threshold=Total_Books_Threshold;
  21. }
  22. }