jobs_update.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. require_once __DIR__ . '/../lib/identity.php';
  3. require_once __DIR__ . '/../lib/jobs.php';
  4. [$actor, $vendor_id] = resolve_request_actor();
  5. $pdo = db();
  6. $job_id = (int) ($_POST['job_id'] ?? 0);
  7. $action = $_POST['action'] ?? '';
  8. $column = $_POST['column'] ?? '';
  9. $value = $_POST['value'] ?? '';
  10. if ($job_id <= 0) {
  11. http_response_code(400);
  12. echo 'Bad job_id';
  13. return;
  14. }
  15. $stmt = $pdo->prepare('SELECT * FROM jobs WHERE id = ?');
  16. $stmt->execute([$job_id]);
  17. $job = $stmt->fetch();
  18. if (!$job) {
  19. http_response_code(404);
  20. echo 'Job not found';
  21. return;
  22. }
  23. // Vendor-side requests are scoped to their own jobs.
  24. if ($actor !== 'ICG' && (int) $job['vendor_id'] !== $vendor_id) {
  25. http_response_code(403);
  26. echo 'Wrong vendor';
  27. return;
  28. }
  29. // --- Button-driven state transitions ---
  30. if ($action !== '') {
  31. $allowed = [
  32. 'acknowledge' => ['ack', '', ['vendor']],
  33. 'mark_finished' => ['status', 'Finished', ['vendor']],
  34. 'mark_shipped' => ['status', 'Shipped', ['vendor']],
  35. 'mark_received' => ['status', 'Received', ['ICG']],
  36. ];
  37. if (!isset($allowed[$action])) {
  38. http_response_code(400);
  39. echo 'Unknown action';
  40. return;
  41. }
  42. [$col, $new, $roles] = $allowed[$action];
  43. $role = $actor === 'ICG' ? 'ICG' : 'vendor';
  44. if (!in_array($role, $roles, true)) {
  45. http_response_code(403);
  46. echo 'Action not allowed for this role';
  47. return;
  48. }
  49. apply_job_change($job, $col, $new, $actor);
  50. echo 'Success';
  51. return;
  52. }
  53. // --- Field edits (ICG only) ---
  54. if ($actor !== 'ICG') {
  55. http_response_code(403);
  56. echo 'Edits restricted to ICG';
  57. return;
  58. }
  59. $editable = ['job', 'material', 'description', 'qty', 'due_date'];
  60. if (!in_array($column, $editable, true)) {
  61. http_response_code(400);
  62. echo 'Unknown column';
  63. return;
  64. }
  65. $value = trim($value);
  66. if ($column === 'qty') {
  67. if ($value === '' || !is_numeric($value)) {
  68. http_response_code(400);
  69. echo 'Qty must be a number';
  70. return;
  71. }
  72. $value = (string) (int) $value;
  73. } elseif ($column === 'due_date') {
  74. $value = parse_due_date($value);
  75. }
  76. apply_job_change($job, $column, $value, $actor);
  77. echo 'Success';