| 1 |
<?php |
| 2 |
/** |
| 3 |
* Invoice Item Repository Class |
| 4 |
* |
| 5 |
* @package Easy_Invoice |
| 6 |
* @subpackage Repositories |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace EasyInvoice\Repositories; |
| 10 |
|
| 11 |
use EasyInvoice\Constants\InvoiceItemFields; |
| 12 |
use EasyInvoice\Interfaces\InvoiceItemRepositoryInterface; |
| 13 |
|
| 14 |
/** |
| 15 |
* InvoiceItemRepository Class |
| 16 |
* |
| 17 |
* Handles data access for invoice items stored as meta fields. |
| 18 |
* Invoice items are stored as '_easy_invoice_items' meta field in the invoice post. |
| 19 |
*/ |
| 20 |
class InvoiceItemRepository implements InvoiceItemRepositoryInterface { |
| 21 |
|
| 22 |
/** |
| 23 |
* The meta key for storing invoice items |
| 24 |
*/ |
| 25 |
const ITEMS_META_KEY = '_easy_invoice_items'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Get all items for a specific invoice |
| 29 |
* |
| 30 |
* @param int $invoice_id The invoice ID |
| 31 |
* @return array Array of invoice item data |
| 32 |
*/ |
| 33 |
public function findByInvoice($invoice_id) { |
| 34 |
$items_data = get_post_meta($invoice_id, self::ITEMS_META_KEY, true); |
| 35 |
|
| 36 |
if (empty($items_data) || !is_array($items_data)) { |
| 37 |
return []; |
| 38 |
} |
| 39 |
|
| 40 |
return $items_data; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get a specific item by index |
| 45 |
* |
| 46 |
* @param int $invoice_id The invoice ID |
| 47 |
* @param int $item_index The item index (0-based) |
| 48 |
* @return array|null The item data or null if not found |
| 49 |
*/ |
| 50 |
public function find($invoice_id, $item_index = null) { |
| 51 |
$items = $this->findByInvoice($invoice_id); |
| 52 |
|
| 53 |
if ($item_index === null) { |
| 54 |
return $items; |
| 55 |
} |
| 56 |
|
| 57 |
return isset($items[$item_index]) ? $items[$item_index] : null; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get all items (this method doesn't make sense for meta-based items, but kept for interface compatibility) |
| 62 |
* |
| 63 |
* @param array $args Optional arguments (ignored for meta-based items) |
| 64 |
* @return array Array of invoice item data |
| 65 |
*/ |
| 66 |
public function all($args = []) { |
| 67 |
// This method doesn't make sense for meta-based items |
| 68 |
// Return empty array as items are tied to specific invoices |
| 69 |
return []; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Add a new item to an invoice |
| 74 |
* |
| 75 |
* @param int $invoice_id The invoice ID |
| 76 |
* @param array $data The item data |
| 77 |
* @return bool True if successful, false otherwise |
| 78 |
*/ |
| 79 |
public function create($invoice_id, $data) { |
| 80 |
$items = $this->findByInvoice($invoice_id); |
| 81 |
$items[] = $data; |
| 82 |
$result = update_post_meta($invoice_id, self::ITEMS_META_KEY, $items); |
| 83 |
return $result !== false; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Update an existing item |
| 88 |
* |
| 89 |
* @param int $invoice_id The invoice ID |
| 90 |
* @param int $item_index The item index (0-based) |
| 91 |
* @param array $data The item data |
| 92 |
* @return bool True if successful, false otherwise |
| 93 |
*/ |
| 94 |
public function update($invoice_id, $item_index, $data) { |
| 95 |
$items = $this->findByInvoice($invoice_id); |
| 96 |
if (!isset($items[$item_index])) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
$items[$item_index] = $data; |
| 100 |
$result = update_post_meta($invoice_id, self::ITEMS_META_KEY, $items); |
| 101 |
return $result !== false; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Delete an item |
| 106 |
* |
| 107 |
* @param int $invoice_id The invoice ID |
| 108 |
* @param int $item_index The item index (0-based) |
| 109 |
* @return bool True if successful, false otherwise |
| 110 |
*/ |
| 111 |
public function delete($invoice_id, $item_index) { |
| 112 |
$items = $this->findByInvoice($invoice_id); |
| 113 |
|
| 114 |
if (!isset($items[$item_index])) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
// Remove the item |
| 119 |
unset($items[$item_index]); |
| 120 |
|
| 121 |
// Re-index the array |
| 122 |
$items = array_values($items); |
| 123 |
|
| 124 |
// Update the meta |
| 125 |
$result = update_post_meta($invoice_id, self::ITEMS_META_KEY, $items); |
| 126 |
|
| 127 |
return $result !== false; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Delete all items for an invoice |
| 132 |
* |
| 133 |
* @param int $invoice_id The invoice ID |
| 134 |
* @return bool True if successful, false otherwise |
| 135 |
*/ |
| 136 |
public function deleteAll($invoice_id) { |
| 137 |
$result = delete_post_meta($invoice_id, self::ITEMS_META_KEY); |
| 138 |
|
| 139 |
return $result !== false; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Count items for an invoice |
| 144 |
* |
| 145 |
* @param int $invoice_id The invoice ID |
| 146 |
* @return int The count of items |
| 147 |
*/ |
| 148 |
public function count($invoice_id) { |
| 149 |
$items = $this->findByInvoice($invoice_id); |
| 150 |
|
| 151 |
return count($items); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Get total amount for all items in an invoice |
| 156 |
* |
| 157 |
* @param int $invoice_id The invoice ID |
| 158 |
* @return float The total amount |
| 159 |
*/ |
| 160 |
public function getTotalAmount($invoice_id) { |
| 161 |
$items = $this->findByInvoice($invoice_id); |
| 162 |
$total = 0; |
| 163 |
|
| 164 |
foreach ($items as $item) { |
| 165 |
$quantity = isset($item['quantity']) ? floatval($item['quantity']) : 0; |
| 166 |
$price = isset($item['price']) ? floatval($item['price']) : 0; |
| 167 |
$total += $quantity * $price; |
| 168 |
} |
| 169 |
|
| 170 |
return $total; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Replace all items for an invoice |
| 175 |
* |
| 176 |
* @param int $invoice_id The invoice ID |
| 177 |
* @param array $items Array of item data |
| 178 |
* @return bool True if successful, false otherwise |
| 179 |
*/ |
| 180 |
public function replaceAll($invoice_id, $items) { |
| 181 |
$result = update_post_meta($invoice_id, self::ITEMS_META_KEY, $items); |
| 182 |
return $result !== false; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Sanitize item data |
| 187 |
* |
| 188 |
* @param array $data The raw item data |
| 189 |
* @return array The sanitized item data |
| 190 |
*/ |
| 191 |
protected function sanitizeItemData($data) { |
| 192 |
return [ |
| 193 |
'name' => isset($data['name']) ? sanitize_text_field($data['name']) : '', |
| 194 |
'description' => isset($data['description']) ? sanitize_textarea_field($data['description']) : '', |
| 195 |
'quantity' => isset($data['quantity']) ? floatval($data['quantity']) : 0, |
| 196 |
'price' => isset($data['price']) ? floatval($data['price']) : 0, |
| 197 |
'tax_rate' => isset($data['tax_rate']) ? floatval($data['tax_rate']) : 0, |
| 198 |
'taxable' => isset($data['taxable']) ? (string) $data['taxable'] : '0', |
| 199 |
'discount' => isset($data['discount']) ? floatval($data['discount']) : 0, |
| 200 |
'discount_type' => isset($data['discount_type']) ? sanitize_text_field($data['discount_type']) : 'percentage', |
| 201 |
]; |
| 202 |
} |
| 203 |
} |