Skip to content

V4 - dailySalesDataCollection

This workflow collects and stores daily sales data for entrepreneurs, calculating profit margins by combining sales figures with various cost categories and retrieving fixed costs from a user database. It serves as a data collection endpoint that can be called by other workflows to maintain comprehensive business tracking records.

Purpose

No business context provided yet — add a context.md to enrich this documentation.

How It Works

  1. Receives Input Data: The workflow is triggered by another workflow with daily business data including sales figures, costs, and metadata
  2. Calculates Profit: Automatically computes profit by subtracting all costs (stocking, transport, other, and daily fixed costs) from sales revenue
  3. Stores in Database: Inserts or updates the daily profit tracking record in PostgreSQL with upsert logic to handle duplicate entries
  4. Returns Key Metrics: Outputs the calculated profit, business date, and sales figures for use by calling workflows

Workflow Diagram

graph TD
    A[When Executed by Another Workflow] --> B[Insert rows in a table]
    B --> C[Edit Fields]

Trigger

Execute Workflow Trigger: This workflow is designed to be called by other workflows, not run independently. It expects the following input parameters:

  • date (string): Date of business activity
  • phoneNumber (string): Entrepreneur's phone number identifier
  • sales (number): Total sales revenue for the day
  • stockingCost (number): Cost of inventory/stock
  • transportCost (number): Transportation expenses
  • otherCost (number): Miscellaneous business costs
  • Micro actions (string): Description of micro-actions taken
  • isPractice? (boolean): Flag indicating if this is practice data

Nodes Used

Node Type Node Name Purpose
Execute Workflow Trigger When Executed by Another Workflow Receives input data from calling workflows
PostgreSQL Insert rows in a table Stores daily sales data and calculates profit with upsert logic
Set Edit Fields Formats output data with key metrics (profit, date, sales)

External Services & Credentials Required

  • PostgreSQL Database: Requires connection credentials for the database containing:
    • dailyProfitTracking table (main data storage)
    • youthEntrepreneursReal table (for retrieving daily fixed costs)

Environment Variables

No specific environment variables are configured in this workflow. Database connection details are managed through n8n's credential system.

Data Flow

Input Data: - Business date and entrepreneur phone number - Sales revenue and cost breakdown (stocking, transport, other) - Micro-actions description and practice flag

Processing: - Retrieves daily fixed costs from entrepreneur profile - Calculates profit: Sales - (Stocking + Transport + Other + Fixed Costs) - Handles data conflicts with upsert logic

Output Data: - profit: Calculated daily profit - dateOfBusiness: Business date - sales: Total sales amount

Error Handling

The workflow is configured with an error workflow (ID: cuHEGQjAfvuGwIOD) that will handle any execution failures. The PostgreSQL operation uses upsert logic to gracefully handle duplicate entries by updating existing records instead of failing.

Known Limitations

  • Phone numbers are trimmed of trailing commas, suggesting potential data quality issues in the source system
  • Fixed costs default to 0 if not found in the entrepreneur database
  • No validation of negative values or unrealistic cost ratios

This workflow is designed to be called by other workflows in the system. The error handling references workflow ID cuHEGQjAfvuGwIOD for failure management.

Setup Instructions

  1. Import Workflow: Import the JSON into your n8n instance
  2. Configure PostgreSQL Credentials:
    • Set up PostgreSQL connection in n8n credentials
    • Ensure access to dailyProfitTracking and youthEntrepreneursReal tables
  3. Database Schema: Verify the following table structure exists:
    1
    2
    3
    -- dailyProfitTracking table should have columns:
    -- phoneNumber, dateOfBusiness, sales, stockingCost, transportCost, 
    -- otherCost, dailyFixedCost, profit, microActions, isPractice
    
  4. Error Workflow: Ensure error workflow cuHEGQjAfvuGwIOD exists or update the reference
  5. Test Integration: Call this workflow from another workflow with sample data to verify functionality
  6. Activate: Enable the workflow once testing is complete

The workflow uses execution order v1 and restricts calls to workflows from the same owner for security.