PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.3.4
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.3.4
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Services / QuoteNumberService.php

QuoteNumberService.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.3.4, at includes/Services/QuoteNumberService.php

175 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Quote Number Service
4 *
5 * @package Easy_Invoice
6 * @subpackage Services
7 */
8
9 namespace EasyInvoice\Services;
10
11 /**
12 * QuoteNumberService Class
13 *
14 * Handles automatic generation of quote numbers based on settings.
15 */
16 class QuoteNumberService {
17
18 /**
19 * Generate the next quote number
20 *
21 * @return string The generated quote number
22 */
23 public function generateNextNumber(): string {
24 // Get settings
25 $prefix = get_option('easy_invoice_quote_prefix', 'QT-');
26
27 // Get the next number to use
28 $next_number = get_option('easy_invoice_next_quote_number', 1);
29
30 // Find the next unique number
31 $unique_number = $this->findNextUniqueNumber($next_number, $prefix);
32
33 // Update the counter to the number we actually used + 1 for next time
34 update_option('easy_invoice_next_quote_number', $unique_number + 1);
35
36 return $prefix . str_pad($unique_number, 6, '0', STR_PAD_LEFT);
37 }
38
39 /**
40 * Get the next quote number without incrementing
41 *
42 * @return string The next quote number
43 */
44 public function getNextNumber(): string {
45 $prefix = get_option('easy_invoice_quote_prefix', 'QT-');
46 $next_number = get_option('easy_invoice_next_quote_number', 1);
47
48 // Find what the next unique number would be
49 $unique_number = $this->findNextUniqueNumber($next_number, $prefix);
50
51 return $prefix . str_pad($unique_number, 6, '0', STR_PAD_LEFT);
52 }
53
54 /**
55 * Find the next unique number starting from the given number
56 *
57 * @param int $start_number The number to start checking from
58 * @param string $prefix The prefix to use for checking
59 * @return int The next unique number
60 */
61 private function findNextUniqueNumber(int $start_number, string $prefix): int {
62 $current_number = $start_number;
63 $max_attempts = 1000; // Prevent infinite loops
64 $attempts = 0;
65
66 while ($attempts < $max_attempts) {
67 $quote_number = $prefix . str_pad($current_number, 6, '0', STR_PAD_LEFT);
68
69 if (!$this->numberExists($quote_number)) {
70 return $current_number;
71 }
72
73 $current_number++;
74 $attempts++;
75 }
76
77 // If we can't find a unique number, add timestamp to ensure uniqueness
78 return $current_number + time();
79 }
80
81 /**
82 * Reset the quote number counter
83 *
84 * @param int $new_starting_number The new starting number
85 * @return void
86 */
87 public function resetCounter(int $new_starting_number = 1): void {
88 update_option('easy_invoice_next_quote_number', $new_starting_number);
89 }
90
91 /**
92 * Check if a quote number already exists
93 *
94 * @param string $quote_number The quote number to check
95 * @return bool True if the number exists, false otherwise
96 */
97 public function numberExists(string $quote_number): bool {
98 global $wpdb;
99
100 $result = $wpdb->get_var($wpdb->prepare(
101 "SELECT COUNT(*) FROM {$wpdb->postmeta}
102 WHERE meta_key = '_easy_invoice_quote_number'
103 AND meta_value = %s",
104 $quote_number
105 ));
106
107 return intval($result) > 0;
108 }
109
110 /**
111 * Generate a unique quote number (handles duplicates)
112 *
113 * @return string A unique quote number
114 */
115 public function generateUniqueNumber(): string {
116 // Get settings
117 $prefix = get_option('easy_invoice_quote_prefix', 'QT-');
118
119 // Get the next number to use from the current settings
120 $next_number = get_option('easy_invoice_next_quote_number', 1);
121
122 // Find the next unique number
123 $unique_number = $this->findNextUniqueNumber($next_number, $prefix);
124
125 // Update the counter to the number we actually used + 1 for next time
126 update_option('easy_invoice_next_quote_number', $unique_number + 1);
127
128 return $prefix . str_pad($unique_number, 6, '0', STR_PAD_LEFT);
129 }
130
131 /**
132 * Get the highest quote number from existing quotes
133 *
134 * @return int The highest quote number found
135 */
136 public function getHighestQuoteNumber(): int {
137 global $wpdb;
138
139 $prefix = get_option('easy_invoice_quote_prefix', 'QT-');
140
141 // Get all quote numbers from the database
142 $results = $wpdb->get_results($wpdb->prepare(
143 "SELECT meta_value FROM {$wpdb->postmeta}
144 WHERE meta_key = '_easy_invoice_quote_number'
145 AND meta_value LIKE %s
146 ORDER BY meta_value DESC
147 LIMIT 1",
148 $prefix . '%'
149 ));
150
151 if (empty($results)) {
152 return 0;
153 }
154
155 $highest_number = $results[0]->meta_value;
156
157 // Extract the number part (remove prefix and padding)
158 $number_part = str_replace($prefix, '', $highest_number);
159 $number_part = ltrim($number_part, '0');
160
161 return intval($number_part);
162 }
163
164 /**
165 * Format a quote number with custom formatting
166 *
167 * @param int $number The number to format
168 * @param string $prefix The prefix to use
169 * @param int $padding The number of digits to pad to
170 * @return string The formatted quote number
171 */
172 public function formatNumber(int $number, string $prefix = 'QT-', int $padding = 6): string {
173 return $prefix . str_pad($number, $padding, '0', STR_PAD_LEFT);
174 }
175 }