test_item_creation.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. // Test script to verify item creation creates accounting entry
  3. header('Content-Type: application/json');
  4. require_once 'config/database.php';
  5. require_once 'models/Item.php';
  6. require_once 'models/AccountingEntry.php';
  7. try {
  8. $database = new Database();
  9. $db = $database->getConnection();
  10. if (!$db) {
  11. echo json_encode(['success' => false, 'message' => 'Database connection failed']);
  12. exit;
  13. }
  14. // Test data for item creation
  15. $test_item_data = [
  16. 'name' => 'Test Item for Accounting',
  17. 'description' => 'Test item to verify accounting entry creation',
  18. 'quantity' => 2,
  19. 'price' => 100.00,
  20. 'date_of_purchase' => '2024-06-15'
  21. ];
  22. // Create item
  23. $item = new Item($db);
  24. $item->name = $test_item_data['name'];
  25. $item->description = $test_item_data['description'];
  26. $item->quantity = $test_item_data['quantity'];
  27. $item->price = $test_item_data['price'];
  28. $item->date_of_purchase = $test_item_data['date_of_purchase'];
  29. if ($item->create()) {
  30. // Create corresponding accounting entry
  31. $accounting_entry = new AccountingEntry($db);
  32. // Calculate accounting entry fields
  33. $total_amount = floatval($test_item_data['price']) * intval($test_item_data['quantity']);
  34. $vat_percentage = 25.50;
  35. $net_amount = $total_amount / (1 + ($vat_percentage / 100));
  36. $vat_amount = $total_amount - $net_amount;
  37. $tax_free_amount = $net_amount;
  38. // Set accounting entry properties
  39. $accounting_entry->entry_date = $test_item_data['date_of_purchase'];
  40. $accounting_entry->description = $test_item_data['name'];
  41. $accounting_entry->entry_type = 'Kulu';
  42. $accounting_entry->category = '222';
  43. $accounting_entry->tax_free_amount = $tax_free_amount;
  44. $accounting_entry->vat_percentage = $vat_percentage;
  45. $accounting_entry->vat_25_5 = $vat_amount;
  46. $accounting_entry->vat_14 = 0;
  47. $accounting_entry->vat_10 = 0;
  48. $accounting_entry->total_amount = $total_amount;
  49. $accounting_entry->net_amount = $net_amount;
  50. $accounting_entry->vat_amount = $vat_amount;
  51. $accounting_entry->reference_number = '';
  52. // Create accounting entry
  53. if ($accounting_entry->create()) {
  54. echo json_encode([
  55. 'success' => true,
  56. 'message' => 'Item and accounting entry created successfully',
  57. 'test_data' => [
  58. 'total_amount' => $total_amount,
  59. 'net_amount' => $net_amount,
  60. 'vat_amount' => $vat_amount,
  61. 'tax_free_amount' => $tax_free_amount
  62. ]
  63. ]);
  64. } else {
  65. echo json_encode([
  66. 'success' => false,
  67. 'message' => 'Item created but accounting entry creation failed'
  68. ]);
  69. }
  70. } else {
  71. echo json_encode([
  72. 'success' => false,
  73. 'message' => 'Item creation failed'
  74. ]);
  75. }
  76. } catch (Exception $e) {
  77. echo json_encode([
  78. 'success' => false,
  79. 'message' => $e->getMessage()
  80. ]);
  81. }
  82. ?>