Skip to content

Instantly share code, notes, and snippets.

@Mardiniii
Last active December 26, 2017 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mardiniii/057928518bdd6fb0ac1598c4505e3a67 to your computer and use it in GitHub Desktop.
Save Mardiniii/057928518bdd6fb0ac1598c4505e3a67 to your computer and use it in GitHub Desktop.
In this gist you will find the main scripts used by the finance team in its daily workflow. Feel free to add any comment or question.

Why we are doing this?

We want to have a centralized source where any person from the finance team or the product department can find easy and fast the scripts and code related to the tasks from our team.

What do you need?

You will need access to the staging and production consoles, if you are not available to got into this enviroments talk to the Systems Team in #dev-systems Slack channel.

Invoice Generation

If you want to see the current implementation for this process you can check this Flow Diagram any comment about this process ask in #dev-finance Slack channel.

For global - All agencies

You can launch the next jobs in order to start the globlal invoicing process for all the agencies, we have different billing periods so check the next snippet :

# For daily billing period
Invoices::GenerateDailyWorker.perform_async

# For weekly billing period
Invoices::GenerateWeeklyWorker.perform_async

# For biweekly billing period
Invoices::GenerateBiweeklyWorker.perform_async

# For Monthly billing period
Invoices::GenerateMonthlyWorker.perform_async

For one agency in specific billing period

If you need to run the invoicing process for a specific agency in some of the billing periods from the list below you can use the next command:

# Example for agency with monthly billing period

# Script info
agency_id = 'agency_id'
billing_period = :monthly

# Find date limit to fetch pending sales
date_limit = Date.today.prev_month.end_of_month # Date supposed to run the generation

# Trigger workers: this method will trigger one worker per each client with pending sales to generate invoices
Invoices::Generators.new(agency, billing_period, date_limit).start

For one client in specific billing period

If you need to run the invoicing process for a specific client in some of the billing periods from the list below you can use the next command:

# Example for client with biweekly billing period

# Script info
client_id = 'client_id'
agency_id = 'agency_id'
generation_date = Date.new(2018,1,16) # Date supposed to run the generation
billing_period = :biweekly

# Find date limit to fetch pending sales
first_fortnight = (1..15)
date_limit = if first_fortnight.include?(generation_date)
         Date.today.prev_month.end_of_month
       else
         Date.today.beginning_of_month + 14.days
       end

# Trigger worker
Invoices::Generators::Client.delay(queue: :billing).generate(client_id, agency_id, billing_period, date_limit)

Note

Be careful and take in consideration we use third-party apps and services for some countries, after running some of the commands above you should check the synchronization with this services is working too, a high number of invoices can collapse the rate limit. (For example in Mexico we use Facturama to validate legal documents)

Files

If you want to know more about this workflow you can check the next files in our Ruby repository:

Feel free to contact the channel #dev-finance in Slack if you have any doubt, comment, feedback, suggestion or question.

Company Invoice Generation

I'm waiting to update this documentation after the refactor is deployed. Check this PR fore more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment