items.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. header("Content-Type: application/json; charset=UTF-8");
  4. header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
  5. header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
  6. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  7. exit(0);
  8. }
  9. require_once __DIR__ . '/../config/database.php';
  10. require_once __DIR__ . '/../models/Item.php';
  11. $database = new Database();
  12. $db = $database->getConnection();
  13. $item = new Item($db);
  14. $request_method = $_SERVER['REQUEST_METHOD'];
  15. switch($request_method) {
  16. case 'GET':
  17. if(isset($_GET['id'])) {
  18. $item->id = $_GET['id'];
  19. $item->readOne();
  20. if($item->name != null) {
  21. // Fix picture URL if it contains incorrect path
  22. $picture_url = $item->picture;
  23. if ($picture_url && strpos($picture_url, '/var/www/html/') !== false) {
  24. // Remove /var/www/html/ from the path
  25. $picture_url = str_replace('/var/www/html/', '', $picture_url);
  26. }
  27. if ($picture_url && strpos($picture_url, '/api/') !== false) {
  28. // Remove /api/ from the beginning if present
  29. $picture_url = str_replace('/api/', '', $picture_url);
  30. }
  31. // Ensure proper URL format
  32. if ($picture_url && !preg_match('/^https?:\/\//', $picture_url)) {
  33. $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
  34. $picture_url = $baseUrl . '/' . ltrim($picture_url, '/');
  35. }
  36. $item_arr = array(
  37. "id" => $item->id,
  38. "name" => $item->name,
  39. "description" => $item->description,
  40. "serial_number" => $item->serial_number,
  41. "picture" => $picture_url,
  42. "quantity" => $item->quantity,
  43. "price" => $item->price,
  44. "date_of_purchase" => $item->date_of_purchase,
  45. "created_at" => $item->created_at,
  46. "updated_at" => $item->updated_at
  47. );
  48. http_response_code(200);
  49. echo json_encode($item_arr);
  50. } else {
  51. http_response_code(404);
  52. echo json_encode(array("message" => "Item not found."));
  53. }
  54. } else {
  55. $stmt = $item->read();
  56. $num = $stmt->rowCount();
  57. if($num > 0) {
  58. $items_arr = array();
  59. $items_arr["records"] = array();
  60. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  61. extract($row);
  62. // Fix picture URL if it contains incorrect path
  63. $picture_url = $picture;
  64. if ($picture && strpos($picture, '/var/www/html/') !== false) {
  65. // Remove /var/www/html/ from the path
  66. $picture_url = str_replace('/var/www/html/', '', $picture);
  67. }
  68. if ($picture && strpos($picture, '/api/') !== false) {
  69. // Remove /api/ from the beginning if present
  70. $picture_url = str_replace('/api/', '', $picture);
  71. }
  72. // Ensure proper URL format
  73. if ($picture_url && !preg_match('/^https?:\/\//', $picture_url)) {
  74. $baseUrl = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
  75. $picture_url = $baseUrl . '/' . ltrim($picture_url, '/');
  76. }
  77. $item_item = array(
  78. "id" => $id,
  79. "name" => $name,
  80. "description" => $description,
  81. "serial_number" => $serial_number,
  82. "picture" => $picture_url,
  83. "quantity" => $quantity,
  84. "price" => $price,
  85. "date_of_purchase" => $date_of_purchase,
  86. "created_at" => $created_at,
  87. "updated_at" => $updated_at
  88. );
  89. array_push($items_arr["records"], $item_item);
  90. }
  91. http_response_code(200);
  92. echo json_encode($items_arr);
  93. } else {
  94. http_response_code(200);
  95. echo json_encode(array("records" => array()));
  96. }
  97. }
  98. break;
  99. case 'POST':
  100. $data = json_decode(file_get_contents("php://input"));
  101. if(!empty($data->name) && !empty($data->quantity) && !empty($data->price)) {
  102. $item->name = $data->name;
  103. $item->description = $data->description ?? '';
  104. $item->serial_number = $data->serial_number ?? '';
  105. $item->picture = $data->picture ?? '';
  106. $item->quantity = $data->quantity;
  107. $item->price = $data->price;
  108. $item->date_of_purchase = $data->date_of_purchase ?? null;
  109. if($item->create()) {
  110. // Create corresponding accounting entry
  111. require_once __DIR__ . '/../models/AccountingEntry.php';
  112. $accounting_entry = new AccountingEntry($db);
  113. // Calculate accounting entry fields
  114. $total_amount = floatval($data->price) * intval($data->quantity);
  115. $vat_percentage = 25.50;
  116. $net_amount = $total_amount / (1 + ($vat_percentage / 100));
  117. $vat_amount = $total_amount - $net_amount;
  118. $tax_free_amount = $net_amount;
  119. // Set accounting entry properties
  120. $accounting_entry->entry_date = $data->date_of_purchase ?? date('Y-m-d');
  121. $accounting_entry->description = $data->name;
  122. $accounting_entry->entry_type = 'Kulu';
  123. $accounting_entry->category = '333';
  124. $accounting_entry->tax_free_amount = $tax_free_amount;
  125. $accounting_entry->vat_percentage = $vat_percentage;
  126. $accounting_entry->vat_25_5 = $vat_amount;
  127. $accounting_entry->vat_14 = 0;
  128. $accounting_entry->vat_10 = 0;
  129. $accounting_entry->total_amount = $total_amount;
  130. $accounting_entry->net_amount = $net_amount;
  131. $accounting_entry->vat_amount = $vat_amount;
  132. $accounting_entry->reference_number = '';
  133. // Create accounting entry
  134. if($accounting_entry->create()) {
  135. http_response_code(201);
  136. echo json_encode(array("message" => "Item and accounting entry were created."));
  137. } else {
  138. http_response_code(201);
  139. echo json_encode(array("message" => "Item was created but accounting entry failed."));
  140. }
  141. } else {
  142. http_response_code(503);
  143. echo json_encode(array("message" => "Unable to create item."));
  144. }
  145. } else {
  146. http_response_code(400);
  147. echo json_encode(array("message" => "Unable to create item. Data is incomplete."));
  148. }
  149. break;
  150. case 'PUT':
  151. $data = json_decode(file_get_contents("php://input"));
  152. if(!empty($data->id) && !empty($data->name) && !empty($data->quantity) && !empty($data->price)) {
  153. $item->id = $data->id;
  154. $item->name = $data->name;
  155. $item->description = $data->description ?? '';
  156. $item->serial_number = $data->serial_number ?? '';
  157. $item->picture = $data->picture ?? '';
  158. $item->quantity = $data->quantity;
  159. $item->price = $data->price;
  160. $item->date_of_purchase = $data->date_of_purchase ?? null;
  161. if($item->update()) {
  162. // Update or create corresponding accounting entry
  163. require_once __DIR__ . '/../models/AccountingEntry.php';
  164. $accounting_entry = new AccountingEntry($db);
  165. // Calculate accounting entry fields
  166. $total_amount = floatval($data->price) * intval($data->quantity);
  167. $vat_percentage = 25.50;
  168. $net_amount = $total_amount / (1 + ($vat_percentage / 100));
  169. $vat_amount = $total_amount - $net_amount;
  170. $tax_free_amount = $net_amount;
  171. // Set accounting entry properties
  172. $accounting_entry->entry_date = $data->date_of_purchase ?? date('Y-m-d');
  173. $accounting_entry->description = $data->name;
  174. $accounting_entry->entry_type = 'Kulu';
  175. $accounting_entry->category = '333';
  176. $accounting_entry->tax_free_amount = $tax_free_amount;
  177. $accounting_entry->vat_percentage = $vat_percentage;
  178. $accounting_entry->vat_25_5 = $vat_amount;
  179. $accounting_entry->vat_14 = 0;
  180. $accounting_entry->vat_10 = 0;
  181. $accounting_entry->total_amount = $total_amount;
  182. $accounting_entry->net_amount = $net_amount;
  183. $accounting_entry->vat_amount = $vat_amount;
  184. $accounting_entry->reference_number = '';
  185. // Try to find existing accounting entry for this item
  186. $existing_entry_query = "SELECT id FROM accounting_entries WHERE description = ? AND entry_type = 'Kulu' AND category = '333' ORDER BY entry_date DESC LIMIT 1";
  187. $stmt = $db->prepare($existing_entry_query);
  188. $stmt->execute([$data->name]);
  189. $existing_entry = $stmt->fetch(PDO::FETCH_ASSOC);
  190. if ($existing_entry) {
  191. // Update existing entry
  192. $accounting_entry->id = $existing_entry['id'];
  193. if($accounting_entry->update()) {
  194. http_response_code(200);
  195. echo json_encode(array("message" => "Item and accounting entry were updated."));
  196. } else {
  197. http_response_code(200);
  198. echo json_encode(array("message" => "Item was updated but accounting entry update failed."));
  199. }
  200. } else {
  201. // Create new entry
  202. if($accounting_entry->create()) {
  203. http_response_code(200);
  204. echo json_encode(array("message" => "Item was updated and new accounting entry was created."));
  205. } else {
  206. http_response_code(200);
  207. echo json_encode(array("message" => "Item was updated but accounting entry creation failed."));
  208. }
  209. }
  210. } else {
  211. http_response_code(503);
  212. echo json_encode(array("message" => "Unable to update item."));
  213. }
  214. } else {
  215. http_response_code(400);
  216. echo json_encode(array("message" => "Unable to update item. Data is incomplete."));
  217. }
  218. break;
  219. case 'DELETE':
  220. if(isset($_GET['id'])) {
  221. $item->id = $_GET['id'];
  222. if($item->delete()) {
  223. http_response_code(200);
  224. echo json_encode(array("message" => "Item was deleted."));
  225. } else {
  226. http_response_code(503);
  227. echo json_encode(array("message" => "Unable to delete item."));
  228. }
  229. } else {
  230. http_response_code(400);
  231. echo json_encode(array("message" => "Unable to delete item. ID is missing."));
  232. }
  233. break;
  234. default:
  235. http_response_code(405);
  236. echo json_encode(array("message" => "Method not allowed."));
  237. break;
  238. }
  239. ?>