items.php 10 KB

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