<?php
/**
 * Helper class for common POS operations
 */

class POSHelper {
    
    /**
     * Format currency to LKR
     */
    public static function formatLKR($amount) {
        return number_format($amount, 2, '.', ',') . ' LKR';
    }
    
    /**
     * Generate unique transaction ID
     */
    public static function generateTransactionID() {
        return 'INV-' . date('YmdHis') . substr(uniqid(), -4);
    }
    
    /**
     * Generate unique credit note ID
     */
    public static function generateCreditNoteID() {
        return 'CN-' . date('YmdHis') . substr(uniqid(), -4);
    }
    
    /**
     * Generate unique expense ID
     */
    public static function generateExpenseID() {
        return 'EXP-' . date('YmdHis');
    }
    
    /**
     * Generate unique supplier payment ID
     */
    public static function generateSupplierPaymentID() {
        return 'SP-' . date('YmdHis') . substr(uniqid(), -4);
    }
    
    /**
     * Calculate loyalty points earned
     */
    public static function calculateLoyaltyPoints($totalAmount) {
        // 1 point per 100 LKR
        return floor($totalAmount / 100);
    }
    
    /**
     * Validate email format
     */
    public static function validateEmail($email) {
        return filter_var($email, FILTER_VALIDATE_EMAIL);
    }
    
    /**
     * Validate phone number (Sri Lanka format)
     */
    public static function validatePhoneNumber($phone) {
        // Sri Lanka phone patterns: 07X-XXXXXXX, 011-XXXXXXX, +94XXXXXXXXX
        return preg_match('/^(\+94|0)(7\d{8}|1{1}\d{9})$/', str_replace('-', '', $phone));
    }
    
    /**
     * Parse date to standard format
     */
    public static function parseDate($dateString) {
        $date = \DateTime::createFromFormat('Y-m-d', $dateString);
        return $date ? $date->format('Y-m-d') : null;
    }
    
    /**
     * Parse datetime to standard format
     */
    public static function parseDateTime($dateTimeString) {
        $date = \DateTime::createFromFormat('Y-m-d H:i:s', $dateTimeString);
        return $date ? $date->format('Y-m-d H:i:s') : null;
    }
    
    /**
     * Get date range for period
     */
    public static function getDateRange($period) {
        $today = date('Y-m-d');
        
        switch($period) {
            case 'today':
                return ['start' => $today, 'end' => $today];
            case 'week':
                $start = date('Y-m-d', strtotime('-7 days'));
                return ['start' => $start, 'end' => $today];
            case 'month':
                $start = date('Y-m-01');
                return ['start' => $start, 'end' => $today];
            case 'quarter':
                $start = date('Y-m-d', strtotime('-3 months'));
                return ['start' => $start, 'end' => $today];
            case 'year':
                $start = date('Y-01-01');
                return ['start' => $start, 'end' => $today];
            default:
                return ['start' => $today, 'end' => $today];
        }
    }
    
    /**
     * Calculate profit for transaction
     */
    public static function calculateProfit($items) {
        $totalCost = 0;
        $totalRevenue = 0;
        
        foreach ($items as $item) {
            $totalCost += ($item['cost'] * $item['quantity']);
            $totalRevenue += $item['line_total'];
        }
        
        return $totalRevenue - $totalCost;
    }
    
    /**
     * Check if product is low on stock
     */
    public static function isLowStock($currentStock, $threshold) {
        return $currentStock <= $threshold;
    }
    
    /**
     * Format date for display
     */
    public static function formatDateForDisplay($dateString) {
        $date = new \DateTime($dateString);
        return $date->format('M d, Y');
    }
    
    /**
     * Get days until date
     */
    public static function getDaysUntilDate($dateString) {
        $date = new \DateTime($dateString);
        $today = new \DateTime();
        $interval = $today->diff($date);
        return $interval->days;
    }
}
?>
