Skip to content

Salman-Ahamed/Dairy-Farm-Management-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

44 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ„ Dairy Farm Management System - Next.js

A modern, full-stack web application for managing dairy farm operations, built with Next.js 14, TypeScript, Prisma, and MySQL. This comprehensive system helps dairy farmers manage every aspect of their operations, from animal care to financial tracking, with advanced automation features.

โœจ Core Features

๐Ÿ” Authentication & User Management

  • Secure login and registration with NextAuth.js
  • Role-based access control (Admin/Manager/User)
  • Password encryption with bcrypt
  • Session management with JWT

๐Ÿฎ Animal Records Management

  • Complete animal profiles with unique tag numbers
  • Track breed, age, gender, and health status
  • Record purchase information and current weight
  • View complete animal history and timeline
  • Full CRUD operations (Create, Read, Update, Delete)
  • Individual animal view with detailed information
  • Animal status tracking (Active, Sold, Deceased, Transferred)

โค๏ธ Health Management System

  • Record health examinations and treatments
  • Track diseases, symptoms, and medications
  • Assign veterinarians and record costs
  • Schedule follow-up checkups
  • Health status monitoring (Healthy, Under Treatment, Critical, Recovered)
  • Complete health history per animal

โš–๏ธ Weight Tracking

  • Monitor animal growth and body condition
  • Track weight changes over time
  • Record height and body condition scores
  • Visual weight progression charts
  • Growth analytics and trends

๐Ÿ‘ถ Breeding Management

  • Manage breeding cycles (Natural & Artificial Insemination)
  • Track male and female animals in breeding
  • Expected and actual delivery dates
  • Record breeding outcomes (Successful, Failed, Aborted, Pending)
  • Offspring tracking and breeding costs

๐Ÿฅ› Milk Production Tracking

  • Daily milk production recording (Morning, Afternoon, Evening)
  • Automatic total calculation
  • Quality assessment (Excellent, Good, Average, Poor)
  • Fat content tracking
  • Per-animal production tracking
  • Production reports and analytics

๐Ÿ’ฐ Advanced Milk Sales System

  • Customer Management Integration
    • Complete customer database with contact details
    • Default price per customer
    • Purchase history tracking
  • Smart Payment System
    • Track total amount vs. amount paid
    • Customer Balance Management (Credit/Due tracking)
    • Auto-payment from customer credit balance
    • Support for advance payments
    • Payment status tracking (Paid, Pending, Overdue)
  • Automatic Financial Integration
    • Auto-create income records when payment status is PAID
    • Track cash payments separately
    • Detailed transaction history
  • Auto-Settle Pending Dues
    • One-click settlement of pending dues from customer credit
    • Intelligent partial payment handling
    • Oldest dues settled first
    • Automatic status updates

๐Ÿ“ฆ Milk Stock Management

  • Real-time stock calculation (Production - Sales)
  • Current milk stock display on dashboard
  • Color-coded stock alerts (Low, Medium, High)
  • Weekly and monthly stock analysis
  • Dedicated milk stock page with detailed analytics
  • Recent activity tracking (Production & Sales)

๐Ÿ‘ฅ Customer Management

  • Complete customer database
  • Contact information (Phone, Email, Address)
  • Default pricing per customer
  • Customer Balance Tracking
    • Positive balance = Advance/Credit
    • Negative balance = Due/Pending payment
  • Total purchases tracking
  • Last purchase date
  • Full purchase history with due amounts
  • Customer-wise sales reports

๐ŸŒพ Stock Feed Inventory

  • Feed type management (Concentrate, Roughage, Supplements)
  • Purchase tracking with supplier information
  • Expiry date monitoring
  • Current stock levels
  • Minimum stock alerts
  • Cost tracking per unit
  • Auto-integration with Finance (Expense tracking)

๐Ÿ‘ฅ Employee Management

  • Employee records with unique IDs
  • Position and department tracking
  • Contact information
  • Salary management
  • Join/leave date tracking
  • Employee status (Active, Inactive, Terminated, Resigned)

๐Ÿ“Š Financial Management System

  • Comprehensive income tracking
    • Milk sales (automatic)
    • Balance adjustments
    • Other income sources
  • Expense tracking
    • Stock feed purchases (automatic)
    • Health treatments
    • Employee salaries
    • Other expenses
  • Advanced Features
    • Automatic integration with Milk Sales
    • Automatic integration with Stock Feed
    • Transaction categorization
    • Reference number tracking
    • Detailed descriptions with breakdowns
    • Payment method tracking
    • Pagination (5 records per page)
    • Date-wise filtering
  • Financial Analytics
    • Income vs. Expense comparison
    • Monthly/Yearly summaries
    • Profitability tracking

๐Ÿ“ˆ Intelligent Dashboard

  • Real-time Statistics
    • Total animals count
    • Health alerts
    • Today's milk production
    • Current milk stock with alerts
    • Active employees
    • Monthly sales
  • Quick Actions (One-click shortcuts)
    • Add Milk Production
    • Add Milk Sale
    • Add Health Record
    • Add Stock Feed
  • Recent Activity Feed
    • Latest milk production records
    • Recent milk sales
    • New health reports
    • Newly added animals
    • Recent stock feed purchases
    • Time-ago formatting
    • Color-coded activity icons

๐Ÿ”„ Key Workflows

1. ๐Ÿฅ› Milk Sale Workflow (with Auto-Payment)

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    MILK SALE WORKFLOW                        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                              โ”‚
โ”‚  1. Select Customer                                          โ”‚
โ”‚     โ”œโ”€ Shows customer's available credit (if any)          โ”‚
โ”‚     โ”œโ”€ Shows customer's pending dues (if any)              โ”‚
โ”‚     โ””โ”€ Auto-fills default price                            โ”‚
โ”‚                                                              โ”‚
โ”‚  2. Enter Sale Details                                       โ”‚
โ”‚     โ”œโ”€ Quantity (Liters)                                    โ”‚
โ”‚     โ”œโ”€ Price per Liter                                      โ”‚
โ”‚     โ””โ”€ Total Amount = Quantity ร— Price                     โ”‚
โ”‚                                                              โ”‚
โ”‚  3. Auto-Payment Calculation                                 โ”‚
โ”‚     โ”œโ”€ System checks customer's credit balance             โ”‚
โ”‚     โ”œโ”€ Auto-applies credit to current sale                 โ”‚
โ”‚     โ””โ”€ Shows remaining due (if any)                        โ”‚
โ”‚                                                              โ”‚
โ”‚  4. Additional Payment (Optional)                            โ”‚
โ”‚     โ””โ”€ Customer can pay extra cash                         โ”‚
โ”‚                                                              โ”‚
โ”‚  5. Payment Status (Auto-Updated)                           โ”‚
โ”‚     โ”œโ”€ PAID: If fully paid (credit + cash)                โ”‚
โ”‚     โ””โ”€ PENDING: If partial payment                         โ”‚
โ”‚                                                              โ”‚
โ”‚  6. Automatic Backend Actions                                โ”‚
โ”‚     โ”œโ”€ Update customer balance                             โ”‚
โ”‚     โ”œโ”€ Record milk sale                                    โ”‚
โ”‚     โ”œโ”€ Create finance income record (if PAID)             โ”‚
โ”‚     โ””โ”€ Update customer purchase history                    โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Example Scenarios:

Scenario A: Customer with Credit

  • Customer Balance: +เงณ500 (Advance)
  • New Sale: 10L ร— เงณ80 = เงณ800
  • Auto-payment: เงณ500 from credit
  • Customer pays: เงณ300 cash
  • Result: Balance = เงณ0, Status = PAID โœ“

Scenario B: Customer with Insufficient Credit

  • Customer Balance: +เงณ200
  • New Sale: 10L ร— เงณ80 = เงณ800
  • Auto-payment: เงณ200 from credit
  • Customer pays: เงณ0 cash
  • Result: Balance = -เงณ600, Status = PENDING (เงณ600 due)

2. ๐Ÿ’ณ Auto-Settle Pending Dues Workflow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              AUTO-SETTLE PENDING DUES WORKFLOW               โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                              โ”‚
โ”‚  1. Customer Gives Advance Payment                           โ”‚
โ”‚     โ””โ”€ Add เงณ2000 to customer balance                        โ”‚
โ”‚                                                              โ”‚
โ”‚  2. System Detects Pending Dues                              โ”‚
โ”‚     โ”œโ”€ Checks all customer's sales                         โ”‚
โ”‚     โ”œโ”€ Identifies sales with due amounts                   โ”‚
โ”‚     โ””โ”€ Shows "Auto-Settle" card on customer detail page    โ”‚
โ”‚                                                              โ”‚
โ”‚  3. View Settlement Preview                                  โ”‚
โ”‚     โ”œโ”€ Shows number of pending sales                       โ”‚
โ”‚     โ”œโ”€ Shows total due amount                              โ”‚
โ”‚     โ”œโ”€ Shows available credit                              โ”‚
โ”‚     โ””โ”€ Indicates full/partial settlement possibility       โ”‚
โ”‚                                                              โ”‚
โ”‚  4. Click "Auto-Settle Dues" Button                          โ”‚
โ”‚     โ””โ”€ Confirmation dialog appears                          โ”‚
โ”‚                                                              โ”‚
โ”‚  5. Automatic Settlement Process (Oldest First)              โ”‚
โ”‚     โ”œโ”€ Get all sales with due amounts                      โ”‚
โ”‚     โ”œโ”€ Sort by date (oldest first)                         โ”‚
โ”‚     โ”œโ”€ Pay each sale using available credit                โ”‚
โ”‚     โ”œโ”€ Update sale status to PAID (if fully paid)         โ”‚
โ”‚     โ”œโ”€ Create finance income records                       โ”‚
โ”‚     โ””โ”€ Update customer's remaining balance                 โ”‚
โ”‚                                                              โ”‚
โ”‚  6. Settlement Complete                                      โ”‚
โ”‚     โ”œโ”€ Shows success message                               โ”‚
โ”‚     โ”œโ”€ Displays settled count and amount                   โ”‚
โ”‚     โ””โ”€ Shows remaining balance                             โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Example:

  • Customer Balance: +เงณ2000
  • Pending Sales:
    • Oct 1: เงณ500 due โ†’ Paid โœ“
    • Oct 3: เงณ800 due โ†’ Paid โœ“
    • Oct 5: เงณ400 due โ†’ Paid โœ“
  • Total Settled: เงณ1700
  • Remaining Balance: เงณ300

3. ๐Ÿ“ฆ Milk Stock Tracking Workflow

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚               MILK STOCK TRACKING WORKFLOW                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                              โ”‚
โ”‚  Real-time Calculation:                                      โ”‚
โ”‚  โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•                                        โ”‚
โ”‚                                                              โ”‚
โ”‚  Current Stock = Total Production - Total Sales              โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”‚
โ”‚  โ”‚ Milk Production  โ”‚         โ”‚   Milk Sales     โ”‚        โ”‚
โ”‚  โ”‚   Record Added   โ”‚    โ†’    โ”‚  Record Added    โ”‚   โ†’    โ”‚
โ”‚  โ”‚  (+50L today)    โ”‚         โ”‚  (-30L today)    โ”‚        โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ”‚
โ”‚                                                              โ”‚
โ”‚           โ†“                              โ†“                   โ”‚
โ”‚                                                              โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”‚
โ”‚  โ”‚     Current Stock = 50L - 30L = 20L            โ”‚        โ”‚
โ”‚  โ”‚                                                  โ”‚        โ”‚
โ”‚  โ”‚     Status: ๐Ÿ”ด Low Stock (< 50L)              โ”‚        โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ”‚
โ”‚                                                              โ”‚
โ”‚  Dashboard Display:                                          โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                          โ”‚
โ”‚  - Current Stock with color alerts                          โ”‚
โ”‚  - Weekly production vs. sales                              โ”‚
โ”‚  - Monthly trends                                           โ”‚
โ”‚  - Recent activities                                        โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

4. ๐Ÿฆ Automatic Finance Integration

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚          AUTOMATIC FINANCE INTEGRATION WORKFLOW              โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                              โ”‚
โ”‚  INCOME (Automatic):                                         โ”‚
โ”‚  โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•                                           โ”‚
โ”‚                                                              โ”‚
โ”‚  Milk Sale (PAID)          โ†’    Finance Record Created      โ”‚
โ”‚  โ”œโ”€ Amount: Cash payment only                              โ”‚
โ”‚  โ”œโ”€ Category: "Milk Sale"                                  โ”‚
โ”‚  โ”œโ”€ Description: Details with breakdown                    โ”‚
โ”‚  โ””โ”€ Reference: MILK-SALE-{id}                              โ”‚
โ”‚                                                              โ”‚
โ”‚  Customer Balance Adjustment  โ†’    Finance Record Created   โ”‚
โ”‚  โ”œโ”€ Amount: Adjustment amount                              โ”‚
โ”‚  โ”œโ”€ Category: "Balance Adjustment"                         โ”‚
โ”‚  โ””โ”€ Reference: BALANCE-ADJ-{id}                            โ”‚
โ”‚                                                              โ”‚
โ”‚                                                              โ”‚
โ”‚  EXPENSE (Automatic):                                        โ”‚
โ”‚  โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•                                          โ”‚
โ”‚                                                              โ”‚
โ”‚  Stock Feed Purchase    โ†’    Finance Record Created         โ”‚
โ”‚  โ”œโ”€ Amount: Total cost                                     โ”‚
โ”‚  โ”œโ”€ Category: "Stock Feed Purchase"                        โ”‚
โ”‚  โ”œโ”€ Description: Feed details                              โ”‚
โ”‚  โ””โ”€ Reference: STOCKFEED-{id}                              โ”‚
โ”‚                                                              โ”‚
โ”‚  Actions that Sync Finance:                                 โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                   โ”‚
โ”‚  โœ“ Create  โ†’ Finance record created                        โ”‚
โ”‚  โœ“ Update  โ†’ Finance record updated                        โ”‚
โ”‚  โœ“ Delete  โ†’ Finance record deleted                        โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

5. ๐Ÿ‘ค Complete Customer Journey

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚              COMPLETE CUSTOMER JOURNEY                       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                              โ”‚
โ”‚  Day 1: Customer Registration                                โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                  โ”‚
โ”‚  - Add new customer: "Karim"                                โ”‚
โ”‚  - Set default price: เงณ80/L                                 โ”‚
โ”‚  - Add contact info                                         โ”‚
โ”‚  - Initial balance: เงณ0                                      โ”‚
โ”‚                                                              โ”‚
โ”‚  Day 2: First Sale (Credit Sale)                            โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                               โ”‚
โ”‚  - Sale: 20L ร— เงณ80 = เงณ1600                                 โ”‚
โ”‚  - Customer pays: เงณ0                                        โ”‚
โ”‚  - Balance: -เงณ1600 (Due)                                   โ”‚
โ”‚  - Status: PENDING                                          โ”‚
โ”‚                                                              โ”‚
โ”‚  Day 3: Second Sale (More Debt)                             โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                โ”‚
โ”‚  - Sale: 15L ร— เงณ80 = เงณ1200                                 โ”‚
โ”‚  - Customer pays: เงณ0                                        โ”‚
โ”‚  - Balance: -เงณ2800 (Total Due)                             โ”‚
โ”‚  - Status: PENDING                                          โ”‚
โ”‚                                                              โ”‚
โ”‚  Day 5: Customer Gives Advance                              โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                 โ”‚
โ”‚  - Customer pays: เงณ5000 (Advance for future)               โ”‚
โ”‚  - Balance: -เงณ2800 + เงณ5000 = +เงณ2200                       โ”‚
โ”‚                                                              โ”‚
โ”‚  Day 5: Auto-Settle Pending Dues                            โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                               โ”‚
โ”‚  - Click "Auto-Settle" button                              โ”‚
โ”‚  - System settles 2 pending sales (เงณ2800)                 โ”‚
โ”‚  - Both marked as PAID                                      โ”‚
โ”‚  - Finance records created                                  โ”‚
โ”‚  - New Balance: +เงณ2200 - เงณ2800 = -เงณ600 (Adjusted)        โ”‚
โ”‚                                                              โ”‚
โ”‚  Day 7: New Sale (Auto-Payment)                             โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                                โ”‚
โ”‚  - Sale: 10L ร— เงณ80 = เงณ800                                  โ”‚
โ”‚  - Auto-payment from credit: เงณ600                          โ”‚
โ”‚  - Customer pays cash: เงณ200                                โ”‚
โ”‚  - Balance: เงณ0                                              โ”‚
โ”‚  - Status: PAID โœ“                                          โ”‚
โ”‚                                                              โ”‚
โ”‚  Day 10: New Sale (Advance Payment)                         โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€                           โ”‚
โ”‚  - Sale: 5L ร— เงณ80 = เงณ400                                   โ”‚
โ”‚  - Customer pays: เงณ1000                                    โ”‚
โ”‚  - Balance: +เงณ600 (Credit for next time)                   โ”‚
โ”‚  - Status: PAID โœ“                                          โ”‚
โ”‚                                                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

6. ๐Ÿ“Š Daily Farm Operations Flow

Morning:
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1. Login to Dashboard
2. Check health alerts
3. Record morning milk production
4. Check milk stock levels
5. If low stock alert โ†’ Plan sales

Midday:
โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1. Record any health issues
2. Add animal weight records
3. Check breeding schedules
4. Record afternoon milk production

Evening:
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1. Record evening milk production
2. Process milk sales
   - Select customer
   - Auto-payment applied
   - Update balances
3. Check pending dues
4. Settle dues if customer paid
5. Update stock feed inventory
6. Review financial summary

Weekly:
โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1. Analyze milk production trends
2. Review customer balances
3. Settle pending dues
4. Check stock feed levels
5. Employee attendance/salary
6. Financial reports

Monthly:
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1. Complete financial analysis
2. Customer payment review
3. Health record summary
4. Breeding outcomes
5. Profitability assessment

๐Ÿ› ๏ธ Tech Stack

  • Framework: Next.js 14 (App Router)
  • Language: TypeScript
  • Database: PostgreSQL with Prisma ORM ๐Ÿ˜
  • Authentication: NextAuth.js v4
  • UI Components: Radix UI + Tailwind CSS
  • Styling: Tailwind CSS
  • Form Handling: React Hook Form + Zod
  • Charts: Recharts
  • Date Handling: date-fns
  • Icons: Lucide React

๐Ÿ“‹ Prerequisites

Before you begin, ensure you have the following installed:

  • Node.js 18.x or higher
  • npm or yarn or pnpm
  • PostgreSQL 14+ (or use cloud PostgreSQL like Supabase, Neon, Railway)
  • Git

๐Ÿš€ Getting Started

1. Clone the Repository

cd next.js

2. Install Dependencies

npm install
# or
yarn install
# or
pnpm install

3. Set Up Environment Variables

Create a .env file in the root directory:

cp .env.example .env

Then update the .env file with your configuration:

# Database (PostgreSQL)
DATABASE_URL="postgresql://username:password@localhost:5432/dairy_farm"

# NextAuth
NEXTAUTH_URL="http://localhost:3000"
NEXTAUTH_SECRET="your-secret-key-here-change-in-production"

# Email (Optional)
EMAIL_SERVER_HOST="smtp.gmail.com"
EMAIL_SERVER_PORT=587
EMAIL_SERVER_USER="your-email@gmail.com"
EMAIL_SERVER_PASSWORD="your-app-password"
EMAIL_FROM="noreply@dairyfarm.com"

# App
NEXT_PUBLIC_APP_NAME="Dairy Farm Management System"

4. Set Up the Database

Create PostgreSQL Database

Option 1: Using psql (Command Line)

psql -U postgres
CREATE DATABASE dairy_farm;
\q

Option 2: Using pgAdmin (GUI)

  1. Open pgAdmin
  2. Right-click on "Databases"
  3. Select "Create" โ†’ "Database"
  4. Enter database name: dairy_farm
  5. Click "Save"

Option 3: Cloud Database (Recommended for Production)

  • Supabase: Free PostgreSQL hosting with dashboard
  • Neon: Serverless PostgreSQL
  • Railway: One-click PostgreSQL deployment
  • Render: Managed PostgreSQL

Run Prisma Migrations

npx prisma generate
npx prisma db push

This will create all tables in your PostgreSQL database.

5. Create Initial User (Optional)

You can create an initial admin user by signing up through the application, or use Prisma Studio:

npx prisma studio

6. Run the Development Server

npm run dev
# or
yarn dev
# or
pnpm dev

Open http://localhost:3000 in your browser.

๐Ÿ“ Usage Guide

First Time Setup

  1. Navigate to http://localhost:3000
  2. Click on "Sign Up" to create your account
  3. Fill in your details and select your role (Admin/Manager/User)
  4. Login with your credentials
  5. Start managing your dairy farm!

๐Ÿš€ Quick Start Guide

Once logged in, follow these steps to get started:

Step 1: Add Your Animals

  1. Go to Animals page
  2. Click "Add New Animal"
  3. Fill in details (Tag Number, Breed, Gender, Date of Birth, etc.)
  4. Save the animal

Step 2: Set Up Customers

  1. Go to Customers page
  2. Click "Add New Customer"
  3. Enter customer name, contact info
  4. Set default price per liter (optional)
  5. Save customer

Step 3: Record Milk Production

  1. Go to Milk Production page
  2. Click "Add New Record"
  3. Select animal
  4. Enter production amounts (Morning, Afternoon, Evening)
  5. Total is calculated automatically
  6. Save record

Step 4: Make a Milk Sale

  1. Go to Milk Sales page
  2. Click "Add New Sale"
  3. Select customer from dropdown (or enter new buyer name)
  4. Enter quantity and price
  5. Payment Features:
    • System shows customer's available credit
    • Credit is automatically applied to sale
    • Enter additional cash payment (if any)
    • Payment status auto-updates if fully paid
  6. Save sale

Result:

  • Milk stock automatically updated โœ…
  • Customer balance updated โœ…
  • Finance income record created (if PAID) โœ…

Step 5: Monitor Dashboard

  1. Go to Dashboard
  2. View:
    • Total animals
    • Today's milk production
    • Current milk stock (with color alerts)
    • Recent activities
  3. Use Quick Actions for fast data entry:
    • Add Milk Production
    • Add Milk Sale
    • Add Health Record
    • Add Stock Feed

Step 6: Manage Customer Balances

  1. Go to Customers page
  2. Click on a customer to view details
  3. View:
    • Current balance (Credit/Due)
    • Complete purchase history
    • Pending dues
  4. Balance Actions:
    • Adjust Balance: Manually add/deduct amount
    • Auto-Settle Dues: One-click to settle all pending dues from credit

Step 7: Track Finances

  1. Go to Finance page
  2. View all income and expenses
  3. Automatic Records:
    • Milk sales (when PAID)
    • Stock feed purchases
    • Customer balance adjustments
  4. Filter by date and type
  5. View total income vs. expenses

Step 8: Monitor Milk Stock

  1. Check Dashboard for current stock
  2. Go to Milk Stock page for detailed analysis:
    • Current stock with alerts
    • Weekly/Monthly production vs. sales
    • Recent activities
    • Quick action buttons

Step 9: Add Stock Feed

  1. Go to Stock Feed page
  2. Click "Add New Stock Feed"
  3. Enter feed details (Type, Quantity, Supplier, Cost)
  4. Save record

Result:

  • Finance expense record automatically created โœ…

Step 10: Track Animal Health

  1. Go to Health Management page
  2. Click "Add New Health Record"
  3. Select animal
  4. Enter disease, symptoms, treatment, medication
  5. Add veterinarian name and cost
  6. Set next checkup date
  7. Save record

๐Ÿ’ก Pro Tips

For Milk Sales

  • Always select an existing customer to automatically apply credit
  • The system shows a real-time payment summary
  • Green badge = Credit, Red badge = Due
  • If customer has credit, it's automatically applied to new sales

For Customer Management

  • Set default price per customer to save time
  • Use "Auto-Settle Dues" when customer pays advance
  • Orange highlighted rows in purchase history = Has pending dues
  • Customer balance: + (Credit) / - (Due)

For Finance Tracking

  • No need to manually add milk sale income (automatic)
  • No need to manually add stock feed expense (automatic)
  • Use pagination to view older records (5 per page)
  • Check reference numbers to trace back to original transactions

For Milk Stock

  • Red alert (<50L) = Low stock, need to reduce sales
  • Orange alert (<100L) = Medium stock
  • Cyan/Green (โ‰ฅ100L) = Good stock
  • Stock calculation: Production - Sales (automatic)

For Dashboard

  • Use Quick Actions for fast data entry
  • Recent Activity shows last 5 actions across all modules
  • Color-coded icons help identify activity type
  • Stock alert visible at a glance

๐Ÿ—‚๏ธ Project Structure

next.js/
โ”œโ”€โ”€ prisma/
โ”‚   โ”œโ”€โ”€ schema.prisma                      # Database schema with 11 models
โ”‚   โ””โ”€โ”€ migrations/                        # Database migration files
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ app/
โ”‚   โ”‚   โ”œโ”€โ”€ api/                          # API routes (RESTful endpoints)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ auth/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ [...nextauth]/route.ts   # NextAuth handler
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ signup/route.ts          # User registration
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ animals/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts                 # GET, POST /api/animals
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/route.ts            # GET, PUT, DELETE /api/animals/:id
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ health/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts                 # Health records listing/create
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/route.ts            # Individual record operations
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ weight/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/route.ts
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ breeding/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/route.ts
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ milk/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/route.ts
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ milk-sales/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts                 # + Auto-payment + Finance integration
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/route.ts            # + Balance sync + Status updates
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ customers/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts                 # Customer CRUD
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/
โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ route.ts             # Customer details
โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ adjust-balance/route.ts   # Manual balance adjustment
โ”‚   โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ settle-dues/route.ts      # Auto-settle pending dues
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ stockfeed/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts                 # + Auto finance expense
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/route.ts            # + Finance sync on edit/delete
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ employees/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ route.ts
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/route.ts
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ finance/
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ route.ts                 # With pagination support
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ [id]/route.ts
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”œโ”€โ”€ dashboard/                    # Protected dashboard pages
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx                     # Dashboard home (Stats + Quick Actions)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ layout.tsx                   # Dashboard layout with Sidebar
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ animals/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx                 # Animals list
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ new/page.tsx             # Add new animal
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/
โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ page.tsx             # View animal details
โ”‚   โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ edit/page.tsx        # Edit animal
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ health/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx                 # Health records list
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ new/page.tsx             # Add health record
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/
โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ page.tsx             # View health record
โ”‚   โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ edit/page.tsx        # Edit health record
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ weight/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ new/page.tsx
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/ (view + edit)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ breeding/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ new/page.tsx
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/ (view + edit)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ milk/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ new/page.tsx
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/ (view + edit)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ milk-sales/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx                 # Sales list
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ new/page.tsx             # + Payment summary + Auto-payment
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/
โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ page.tsx             # View with finance link
โ”‚   โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ edit/page.tsx        # Edit with payment tracking
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ customers/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx                 # Customers list with balance
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ new/page.tsx             # Add new customer
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/
โ”‚   โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ page.tsx             # + Purchase history + Auto-settle
โ”‚   โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ edit/page.tsx        # Edit customer
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ milk-stock/
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ page.tsx                 # Stock analysis + Alerts
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ stockfeed/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ new/page.tsx             # + Finance integration info
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/ (view + edit)
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ employees/
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx
โ”‚   โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ new/page.tsx
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ [id]/ (view + edit)
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ finance/
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ page.tsx                 # With pagination (5 per page)
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ new/page.tsx
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ [id]/ (view + edit)
โ”‚   โ”‚   โ”‚
โ”‚   โ”‚   โ”œโ”€โ”€ login/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ page.tsx                     # Login form
โ”‚   โ”‚   โ”œโ”€โ”€ signup/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ page.tsx                     # Registration form
โ”‚   โ”‚   โ”œโ”€โ”€ page.tsx                         # Landing page
โ”‚   โ”‚   โ”œโ”€โ”€ layout.tsx                       # Root layout
โ”‚   โ”‚   โ””โ”€โ”€ globals.css                      # Global styles
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ components/
โ”‚   โ”‚   โ”œโ”€โ”€ layout/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ Sidebar.tsx                  # Navigation with all modules
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ Header.tsx                   # User menu + logout
โ”‚   โ”‚   โ””โ”€โ”€ ui/                              # Shadcn/ui components
โ”‚   โ”‚       โ”œโ”€โ”€ button.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ card.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ input.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ label.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ select.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ table.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ badge.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ tabs.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ dialog.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ toast.tsx
โ”‚   โ”‚       โ”œโ”€โ”€ toaster.tsx
โ”‚   โ”‚       โ””โ”€โ”€ use-toast.ts
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ lib/
โ”‚       โ”œโ”€โ”€ prisma.ts                        # Prisma client singleton
โ”‚       โ”œโ”€โ”€ auth.ts                          # NextAuth configuration
โ”‚       โ””โ”€โ”€ utils.ts                         # Utility functions
โ”‚                                             # (formatDate, formatCurrency, etc.)
โ”‚
โ”œโ”€โ”€ public/                                   # Static assets
โ”œโ”€โ”€ .env                                      # Environment variables
โ”œโ”€โ”€ package.json                              # Dependencies
โ”œโ”€โ”€ tsconfig.json                             # TypeScript config
โ”œโ”€โ”€ tailwind.config.ts                        # Tailwind CSS config
โ”œโ”€โ”€ next.config.js                            # Next.js config
โ”œโ”€โ”€ README.md                                 # This file
โ””โ”€โ”€ SETUP_GUIDE.md                           # Detailed setup instructions

Key File Highlights

Smart API Routes:

  • milk-sales/route.ts: Auto-payment logic, customer balance updates, finance integration
  • customers/[id]/settle-dues/route.ts: Automatic pending dues settlement
  • stockfeed/route.ts: Auto finance expense creation

Interactive Pages:

  • milk-sales/new/page.tsx: Real-time payment summary with credit auto-application
  • customers/[id]/page.tsx: Customer details with auto-settle card
  • dashboard/page.tsx: Live stats with quick actions and recent activity

Utility Functions:

  • formatDate(): Consistent date formatting
  • formatCurrency(): Taka (เงณ) formatting
  • calculateAge(): Animal age calculation
  • getAgeString(): Human-readable age

๐Ÿ”ง Available Scripts

  • npm run dev - Start development server
  • npm run build - Build for production
  • npm run start - Start production server
  • npm run lint - Run ESLint
  • npx prisma studio - Open Prisma Studio (database GUI)
  • npx prisma generate - Generate Prisma Client
  • npx prisma db push - Push schema changes to database

๐ŸŒŸ Key Features Explained

Animal Management

  • Add, edit, and track animals with unique tag numbers
  • Record breed, age, gender, and health status
  • Track purchase information and current weight
  • View complete animal history

Health Tracking

  • Record health examinations and treatments
  • Track diseases, symptoms, and medications
  • Schedule follow-up checkups
  • Monitor health costs

Milk Production

  • Log daily milk production (morning, afternoon, evening)
  • Track milk quality and fat content
  • Generate production reports
  • Identify top-producing animals

Financial Management

  • Track all farm income and expenses
  • Categorize transactions
  • View financial summaries
  • Monitor profitability

๐Ÿ” Security Features

  • Secure password hashing with bcrypt
  • JWT-based session management
  • Protected API routes
  • Role-based access control (RBAC) ready
  • SQL injection prevention with Prisma

๐Ÿš€ Deployment

Deploy to Vercel

Deploy with Vercel

  1. Push your code to GitHub
  2. Import your repository to Vercel
  3. Add environment variables
  4. Deploy!

Deploy to Other Platforms

The application can be deployed to any platform that supports Next.js:

  • Netlify
  • Railway
  • DigitalOcean App Platform
  • AWS Amplify
  • Google Cloud Run

๐Ÿ“š API Documentation

Authentication

POST   /api/auth/signup              - Register new user
POST   /api/auth/signin              - User login
POST   /api/auth/signout             - User logout
GET    /api/auth/session             - Get current session

Animals

GET    /api/animals                  - Get all animals (with filters)
POST   /api/animals                  - Create new animal
GET    /api/animals/:id              - Get animal details by ID
PUT    /api/animals/:id              - Update animal
DELETE /api/animals/:id              - Delete animal

Health Records

GET    /api/health                   - Get all health records
POST   /api/health                   - Create health record
GET    /api/health/:id               - Get health record by ID
PUT    /api/health/:id               - Update health record
DELETE /api/health/:id               - Delete health record

Weight Records

GET    /api/weight                   - Get all weight records
POST   /api/weight                   - Create weight record
GET    /api/weight/:id               - Get weight record by ID
PUT    /api/weight/:id               - Update weight record
DELETE /api/weight/:id               - Delete weight record

Breeding Records

GET    /api/breeding                 - Get all breeding records
POST   /api/breeding                 - Create breeding record
GET    /api/breeding/:id             - Get breeding record by ID
PUT    /api/breeding/:id             - Update breeding record
DELETE /api/breeding/:id             - Delete breeding record

Milk Production

GET    /api/milk                     - Get all milk production records
POST   /api/milk                     - Create milk record
GET    /api/milk/:id                 - Get milk record by ID
PUT    /api/milk/:id                 - Update milk record
DELETE /api/milk/:id                 - Delete milk record

Milk Sales

GET    /api/milk-sales               - Get all milk sales
POST   /api/milk-sales               - Create milk sale
                                       โ€ข Auto-applies customer credit
                                       โ€ข Creates finance record (if PAID)
                                       โ€ข Updates customer balance
                                       โ€ข Auto-creates customer if new

GET    /api/milk-sales/:id           - Get milk sale by ID
PUT    /api/milk-sales/:id           - Update milk sale
                                       โ€ข Updates finance record
                                       โ€ข Updates customer balance
                                       โ€ข Handles status changes

DELETE /api/milk-sales/:id           - Delete milk sale
                                       โ€ข Deletes finance record
                                       โ€ข Reverses customer balance

Customers

GET    /api/customers                - Get all customers
POST   /api/customers                - Create new customer
GET    /api/customers/:id            - Get customer details
                                       โ€ข Includes purchase history
                                       โ€ข Shows balance
                                       โ€ข Shows pending dues

PUT    /api/customers/:id            - Update customer
DELETE /api/customers/:id            - Delete customer

POST   /api/customers/:id/adjust-balance
                                     - Manually adjust customer balance
                                       โ€ข Creates finance record
                                       โ€ข Updates balance

POST   /api/customers/:id/settle-dues
                                     - Auto-settle pending dues
                                       โ€ข Oldest dues first
                                       โ€ข Creates finance records
                                       โ€ข Updates payment status

Stock Feed

GET    /api/stockfeed                - Get all stock feed records
POST   /api/stockfeed                - Create stock feed
                                       โ€ข Auto-creates finance expense

GET    /api/stockfeed/:id            - Get stock feed by ID
PUT    /api/stockfeed/:id            - Update stock feed
                                       โ€ข Updates finance record

DELETE /api/stockfeed/:id            - Delete stock feed
                                       โ€ข Deletes finance record

Employees

GET    /api/employees                - Get all employees
POST   /api/employees                - Create employee
GET    /api/employees/:id            - Get employee by ID
PUT    /api/employees/:id            - Update employee
DELETE /api/employees/:id            - Delete employee

Finance

GET    /api/finance                  - Get all finance records
                                       โ€ข Supports pagination
                                       โ€ข Date filtering

POST   /api/finance                  - Create finance record
GET    /api/finance/:id              - Get finance record by ID
PUT    /api/finance/:id              - Update finance record
DELETE /api/finance/:id              - Delete finance record

๐Ÿ’พ Database Schema

Key Models

model User {
  id        String   @id @default(cuid())
  name      String
  email     String   @unique
  password  String
  role      String   @default("USER")
  createdAt DateTime @default(now())
}

model Animal {
  id            String    @id @default(cuid())
  tagNumber     String    @unique
  name          String?
  breed         String
  dateOfBirth   DateTime
  gender        Gender
  purchaseDate  DateTime?
  purchasePrice Float?
  currentWeight Float?
  status        AnimalStatus @default(ACTIVE)
  // Relations
  healthRecords HealthRecord[]
  weightRecords WeightRecord[]
  milkRecords   MilkRecord[]
  breedingMale  Breeding[]    @relation("MaleAnimal")
  breedingFemale Breeding[]   @relation("FemaleAnimal")
}

model Customer {
  id                    String     @id @default(cuid())
  name                  String
  phone                 String?
  email                 String?
  address               String?
  defaultPricePerLiter  Float?
  balance               Float      @default(0)  // + = Credit, - = Due
  lastPurchaseDate      DateTime?
  totalPurchases        Float      @default(0)
  notes                 String?
  // Relations
  milkSales             MilkSale[]
}

model MilkSale {
  id              String        @id @default(cuid())
  customerId      String?
  customer        Customer?     @relation(fields: [customerId])
  saleDate        DateTime
  quantity        Float
  pricePerLiter   Float
  totalAmount     Float
  amountPaid      Float         @default(0)
  buyer           String?
  paymentStatus   PaymentStatus @default(PENDING)
  paymentMethod   String?
  notes           String?
}

model Finance {
  id              String       @id @default(cuid())
  date            DateTime
  type            FinanceType  // INCOME or EXPENSE
  category        String
  description     String
  amount          Float
  paymentMethod   String?
  referenceNumber String?      @unique
  notes           String?
}

model MilkRecord {
  id            String   @id @default(cuid())
  animalId      String
  animal        Animal   @relation(fields: [animalId])
  date          DateTime
  morningAmount Float    @default(0)
  afternoonAmount Float  @default(0)
  eveningAmount Float    @default(0)
  totalAmount   Float
  quality       MilkQuality?
  fatContent    Float?
}

model HealthRecord {
  id                String       @id @default(cuid())
  animalId          String
  animal            Animal       @relation(fields: [animalId])
  checkupDate       DateTime
  disease           String?
  symptoms          String?
  treatment         String?
  medication        String?
  veterinarian      String?
  cost              Float?
  nextCheckupDate   DateTime?
  status            HealthStatus
  notes             String?
}

model WeightRecord {
  id              String   @id @default(cuid())
  animalId        String
  animal          Animal   @relation(fields: [animalId])
  recordDate      DateTime
  weight          Float
  height          Float?
  bodyCondition   Float?
  notes           String?
}

model Breeding {
  id                 String         @id @default(cuid())
  maleAnimalId       String
  femaleAnimalId     String
  maleAnimal         Animal         @relation("MaleAnimal", fields: [maleAnimalId])
  femaleAnimal       Animal         @relation("FemaleAnimal", fields: [femaleAnimalId])
  breedingDate       DateTime
  breedingType       BreedingType
  expectedDelivery   DateTime?
  actualDelivery     DateTime?
  outcome            BreedingOutcome?
  notes              String?
}

model StockFeed {
  id              String   @id @default(cuid())
  feedType        String
  quantity        Float
  unit            String
  supplier        String?
  purchaseDate    DateTime
  expiryDate      DateTime?
  costPerUnit     Float
  totalCost       Float
  currentStock    Float
  minimumStock    Float?
  notes           String?
}

model Employee {
  id          String         @id @default(cuid())
  employeeId  String         @unique
  name        String
  position    String
  department  String?
  phone       String?
  email       String?
  address     String?
  salary      Float?
  joinDate    DateTime
  leaveDate   DateTime?
  status      EmployeeStatus @default(ACTIVE)
  notes       String?
}

// Enums
enum Gender { MALE FEMALE }
enum AnimalStatus { ACTIVE SOLD DECEASED TRANSFERRED }
enum HealthStatus { HEALTHY UNDER_TREATMENT CRITICAL RECOVERED }
enum MilkQuality { EXCELLENT GOOD AVERAGE POOR }
enum PaymentStatus { PAID PENDING OVERDUE }
enum BreedingType { NATURAL ARTIFICIAL_INSEMINATION }
enum BreedingOutcome { SUCCESSFUL FAILED ABORTED PENDING }
enum FinanceType { INCOME EXPENSE }
enum EmployeeStatus { ACTIVE INACTIVE TERMINATED RESIGNED }

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request. Whether it's bug fixes, new features, or documentation improvements, your contribution is valued.

How to Contribute

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐ŸŒŸ Key Automation Features

This system includes several smart automation features that save time and reduce errors:

๐Ÿค– Automatic Finance Tracking

  • Milk Sales (PAID) โ†’ Auto-creates income record
  • Stock Feed Purchases โ†’ Auto-creates expense record
  • Customer Balance Adjustments โ†’ Auto-creates finance record
  • Edit/Delete โ†’ Finance records automatically sync

๐Ÿ’ฐ Smart Payment System

  • Auto-payment: Credit balance automatically applied to new sales
  • Real-time calculation: Shows exactly how much is covered by credit
  • Status auto-update: Payment status automatically set to PAID when fully covered
  • Balance tracking: Positive (Credit) / Negative (Due) tracking

๐Ÿ”„ Auto-Settle Dues

  • One-click settlement: Settle all pending dues at once
  • Intelligent payment: Oldest dues paid first
  • Partial settlement: Works even if credit doesn't cover all dues
  • Auto-status update: Sales marked as PAID automatically

๐Ÿ“ฆ Real-time Stock Tracking

  • Live calculation: Stock = Production - Sales
  • Color alerts: Low (Red), Medium (Orange), Good (Cyan)
  • Dashboard display: Always see current stock at a glance

๐Ÿ‘ค Smart Customer Management

  • Auto-registration: New buyers automatically added as customers
  • Price memory: Default price auto-filled for existing customers
  • Balance display: Shows available credit or pending dues
  • Purchase history: Complete transaction history with each customer

๐Ÿ“Š System Capabilities Summary

Module Create Read Update Delete Auto-Integration Special Features
๐Ÿฎ Animals โœ… โœ… โœ… โœ… - View page, Status tracking
โค๏ธ Health โœ… โœ… โœ… โœ… - Per-animal history
โš–๏ธ Weight โœ… โœ… โœ… โœ… - Growth tracking
๐Ÿ‘ถ Breeding โœ… โœ… โœ… โœ… - Outcome tracking
๐Ÿฅ› Milk Production โœ… โœ… โœ… โœ… Stock Calculation 3-session tracking
๐Ÿ’ฐ Milk Sales โœ… โœ… โœ… โœ… Finance + Balance Auto-payment, Auto-customer
๐Ÿ‘ฅ Customers โœ… โœ… โœ… โœ… Finance (Adjust) Balance tracking, Settle dues
๐Ÿ“ฆ Milk Stock - โœ… - - Production/Sales Real-time calculation
๐ŸŒพ Stock Feed โœ… โœ… โœ… โœ… Finance (Expense) Expiry tracking
๐Ÿ‘ฅ Employees โœ… โœ… โœ… โœ… - Status tracking
๐Ÿฆ Finance โœ… โœ… โœ… โœ… - Pagination, Categories

๐ŸŽฏ Who Should Use This System?

This Dairy Farm Management System is perfect for:

  • Small to Medium Dairy Farms (10-200 animals)
  • Farm Managers who want to digitize operations
  • Farm Owners who need real-time insights
  • Dairy Cooperatives managing multiple farmers
  • Agricultural Students learning farm management
  • Veterinarians tracking animal health

๐Ÿ’ก Why Choose This System?

โœ… Comprehensive

  • Covers ALL aspects of dairy farm management
  • From animal health to financial tracking

โœ… Smart Automation

  • Auto-payment from customer credit
  • Auto-finance integration
  • Auto-settle pending dues
  • Real-time stock tracking

โœ… User-Friendly

  • Clean, modern interface
  • Intuitive navigation
  • Color-coded alerts
  • Quick action shortcuts

โœ… Reliable

  • Built with enterprise-grade tech stack
  • Secure authentication
  • Data validation
  • Error handling

โœ… Scalable

  • Handles growing farm operations
  • Pagination for large datasets
  • Optimized database queries
  • Fast performance

๐Ÿ”œ Future Enhancements

  • Mobile app (React Native)
  • Real-time notifications
  • Advanced analytics with charts
  • Export to PDF/Excel
  • Multi-language support (Bengali, English)
  • Barcode/QR code scanning for animals
  • Weather integration for milk production correlation
  • Automated backup system
  • SMS notifications for payment reminders
  • Integration with IoT devices (milk meters)
  • Offline mode support
  • Multi-farm support
  • WhatsApp integration for customer notifications
  • Inventory forecasting
  • Feed efficiency calculator

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ™ Acknowledgments

Built with modern web technologies to help dairy farmers manage their operations more efficiently. Special thanks to:

  • Next.js team for the amazing framework
  • Prisma for the excellent ORM
  • Shadcn/ui for beautiful components
  • The open-source community

๐Ÿ“ž Support & Contact

For questions, issues, or feature requests:

  • ๐Ÿ“ง Email: support@dairyfarm.com
  • ๐Ÿ› Issues: Open an issue on GitHub
  • ๐Ÿ’ฌ Discussions: Join our community discussions

๐Ÿ„ Made with โค๏ธ for Dairy Farmers

Dairy Farm Management System - Modernizing Traditional Farming

โญ Star this repo | ๐Ÿด Fork it | ๐Ÿ“– Read the docs

Releases

No releases published

Packages

 
 
 

Contributors

Languages