<?php
/**
 * Invoice Trait
 *
 * @package Easy_Invoice
 * @subpackage Traits
 */

namespace EasyInvoice\Traits;

use EasyInvoice\Controllers\SettingsController;

/**
 * InvoiceTrait contains methods for invoice operations like generating invoice numbers, calculating due dates
 */
trait InvoiceTrait {
    /**
     * Generate invoice number based on settings
     * 
     * @param int $invoice_id The invoice ID
     * @return string The generated invoice number
     */
    protected function generateInvoiceNumber($invoice_id) {
        $settings_controller = new SettingsController();
        $settings = $settings_controller->getSettings();
        
        $prefix = $settings['invoice']['prefix'] ?: 'INV-';
        $starting_number = intval($settings['invoice']['starting_number']) ?: 1001;
        
        return $prefix . ($starting_number + $invoice_id - 1);
    }
    
    /**
     * Calculate due date based on issue date and settings
     * 
     * @param string $issue_date Issue date in Y-m-d format
     * @return string Due date in Y-m-d format
     */
    protected function calculateDueDate($issue_date) {
        $settings_controller = new SettingsController();
        $settings = $settings_controller->getSettings();
        
        $due_days = intval($settings['invoice']['due_days']) ?: 30;
        
        $issue_timestamp = strtotime($issue_date);
        $due_timestamp = strtotime("+{$due_days} days", $issue_timestamp);
        
        return date('Y-m-d', $due_timestamp);
    }
} 