items.php 12 KB

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