| 1 |
<?php |
| 2 |
/** |
| 3 |
* Invoice Trait |
| 4 |
* |
| 5 |
* @package Easy_Invoice |
| 6 |
* @subpackage Traits |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace EasyInvoice\Traits; |
| 10 |
|
| 11 |
use EasyInvoice\Controllers\SettingsController; |
| 12 |
|
| 13 |
/** |
| 14 |
* InvoiceTrait contains methods for invoice operations like generating invoice numbers, calculating due dates |
| 15 |
*/ |
| 16 |
trait InvoiceTrait { |
| 17 |
/** |
| 18 |
* Generate invoice number based on settings |
| 19 |
* |
| 20 |
* @param int $invoice_id The invoice ID |
| 21 |
* @return string The generated invoice number |
| 22 |
*/ |
| 23 |
protected function generateInvoiceNumber($invoice_id) { |
| 24 |
$settings_controller = new SettingsController(); |
| 25 |
$settings = $settings_controller->getSettings(); |
| 26 |
|
| 27 |
$prefix = $settings['invoice']['prefix'] ?: 'INV-'; |
| 28 |
$starting_number = intval($settings['invoice']['starting_number']) ?: 1001; |
| 29 |
|
| 30 |
return $prefix . ($starting_number + $invoice_id - 1); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Calculate due date based on issue date and settings |
| 35 |
* |
| 36 |
* @param string $issue_date Issue date in Y-m-d format |
| 37 |
* @return string Due date in Y-m-d format |
| 38 |
*/ |
| 39 |
protected function calculateDueDate($issue_date) { |
| 40 |
$settings_controller = new SettingsController(); |
| 41 |
$settings = $settings_controller->getSettings(); |
| 42 |
|
| 43 |
$due_days = intval($settings['invoice']['due_days']) ?: 30; |
| 44 |
|
| 45 |
$issue_timestamp = strtotime($issue_date); |
| 46 |
$due_timestamp = strtotime("+{$due_days} days", $issue_timestamp); |
| 47 |
|
| 48 |
return date('Y-m-d', $due_timestamp); |
| 49 |
} |
| 50 |
} |