fork download
  1. <?php
  2.  
  3. function calculateMonthlyPayroll(PDO $pdo, string $employeeId, int $year, int $month): float
  4. {
  5. if ($month < 1 || $month > 12) {
  6. throw new InvalidArgumentException('Month must be between 1 and 12');
  7. }
  8.  
  9. // Get all active contracts for the employee in this month
  10. $contracts = getActiveContracts($pdo, $employeeId, $year, $month);
  11.  
  12. $totalAmount = 0.0;
  13.  
  14. // Calculate previous month for revenue share considerations
  15. $previousMonth = $month - 1;
  16. $previousYear = $year;
  17. if ($previousMonth < 1) {
  18. $previousMonth = 12;
  19. $previousYear = $year - 1;
  20. }
  21.  
  22. // Process each contract based on role
  23. foreach ($contracts as $contract) {
  24. $roleCategory = $contract['role_category'];
  25.  
  26. switch ($roleCategory) {
  27. case 'actor':
  28. // Fixed fee installment
  29. if ($contract['fixed_fee_total'] && $contract['filming_duration_months']) {
  30. $totalAmount += $contract['fixed_fee_total'] / $contract['filming_duration_months'];
  31. }
  32.  
  33. // Calculate revenue share for the previous month
  34. if ($contract['revenue_share_percentage']) {
  35. $revenue = getFilmRevenue($pdo, $contract['film_id'], $previousYear, $previousMonth);
  36. if ($revenue) {
  37. $totalAmount += $revenue * ($contract['revenue_share_percentage'] / 100);
  38. }
  39. }
  40. break;
  41.  
  42. case 'production_staff':
  43. // Production Staff: Monthly fixed amount only
  44. if ($contract['monthly_amount']) {
  45. $totalAmount += $contract['monthly_amount'];
  46. }
  47. break;
  48.  
  49. case 'senior_staff':
  50. // Monthly fixed amount
  51. if ($contract['monthly_amount']) {
  52. $totalAmount += $contract['monthly_amount'];
  53. }
  54.  
  55. // Ongoing revenue share from PREVIOUS month
  56. if ($contract['revenue_share_percentage']) {
  57. $revenue = getFilmRevenue($pdo, $contract['film_id'], $previousYear, $previousMonth);
  58. if ($revenue) {
  59. $totalAmount += $revenue * ($contract['revenue_share_percentage'] / 100);
  60. }
  61. }
  62. break;
  63. }
  64. }
  65.  
  66. return $totalAmount;
  67. }
  68.  
  69. // Get active contracts for an employee in a specific month.
  70. function getActiveContracts(PDO $pdo, string $employeeId, int $year, int $month): array
  71. {
  72. // Calculate the first day of the month for contract validity check
  73. $calculationDate = sprintf('%04d-%02d-01', $year, $month);
  74.  
  75. $stmt = $pdo->prepare("
  76. SELECT
  77. c.id,
  78. c.fixed_fee_total,
  79. c.monthly_amount,
  80. c.filming_duration_months,
  81. c.revenue_share_percentage,
  82. r.category as role_category,
  83. f.id as film_id
  84. FROM contract c
  85. INNER JOIN role r ON c.role_id = r.id
  86. INNER JOIN film f ON c.film_id = f.id
  87. WHERE c.employee_id = :employee_id
  88. AND c.start_date <= :calculation_date
  89. AND c.end_date >= :calculation_date
  90. AND c.status = 'active'
  91. ");
  92.  
  93. $stmt->execute([
  94. 'employee_id' => $employeeId,
  95. 'calculation_date' => $calculationDate
  96. ]);
  97.  
  98. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  99. }
  100.  
  101. // Get film revenue for a specific month.
  102. function getFilmRevenue(PDO $pdo, string $filmId, int $year, int $month): ?float
  103. {
  104. $stmt = $pdo->prepare("
  105. SELECT revenue_amount
  106. FROM film_revenue
  107. WHERE film_id = :film_id
  108. AND year = :year
  109. AND month = :month
  110. ");
  111.  
  112. $stmt->execute([
  113. 'film_id' => $filmId,
  114. 'year' => $year,
  115. 'month' => $month
  116. ]);
  117.  
  118. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  119. return $result ? (float) $result['revenue_amount'] : null;
  120. }
  121.  
Success #stdin #stdout 0.04s 25768KB
stdin
Standard input is empty
stdout
Standard output is empty