File "fees.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/seed/fees.php
File size: 2.46 KiB (2516 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
header('Content-Type: application/json');
require_once '../../config/config.php';

function ensureFeeType(PDO $pdo, string $name, string $desc=''): int {
  $s=$pdo->prepare('SELECT fee_type_id FROM fee_types WHERE fee_name=?');
  $s->execute([$name]);
  $id=(int)($s->fetchColumn()?:0);
  if($id){ $u=$pdo->prepare('UPDATE fee_types SET description=? WHERE fee_type_id=?'); $u->execute([$desc,$id]); return $id; }
  $i=$pdo->prepare('INSERT INTO fee_types (fee_name, description) VALUES (?,?)');
  $i->execute([$name,$desc]);
  return (int)$pdo->lastInsertId();
}

function findCourseId(PDO $pdo, string $code): int { $s=$pdo->prepare('SELECT course_id FROM courses WHERE course_code=?'); $s->execute([$code]); return (int)($s->fetchColumn()?:0); }

function ensureFeeStructure(PDO $pdo, int $courseId, int $feeTypeId, float $amount, string $from, ?string $to=null): int {
  $s=$pdo->prepare('SELECT fee_structure_id FROM course_fee_structure WHERE course_id=? AND fee_type_id=? AND effective_from=?');
  $s->execute([$courseId,$feeTypeId,$from]);
  $id=(int)($s->fetchColumn()?:0);
  if($id){ $u=$pdo->prepare('UPDATE course_fee_structure SET amount=?, effective_to=? WHERE fee_structure_id=?'); $u->execute([$amount,$to,$id]); return $id; }
  $i=$pdo->prepare('INSERT INTO course_fee_structure (course_id, fee_type_id, amount, effective_from, effective_to) VALUES (?,?,?,?,?)');
  $i->execute([$courseId,$feeTypeId,$amount,$from,$to]);
  return (int)$pdo->lastInsertId();
}

try{
  $pdo->beginTransaction();
  $examFee = ensureFeeType($pdo,'Exam Fee','Per paper exam fee');
  $revalFee = ensureFeeType($pdo,'Revaluation','Revaluation application fee');
  $lateFee = ensureFeeType($pdo,'Late Fee','Late application fee');

  $courses=['BE-ME','BE-CSE','ME-MFG','PHD-ME'];
  $from=date('Y-m-01');
  $created=0;
  foreach($courses as $code){
    $cid=findCourseId($pdo,$code); if($cid<=0) continue;
    ensureFeeStructure($pdo,$cid,$examFee,750.00,$from,null); $created++;
    ensureFeeStructure($pdo,$cid,$revalFee,1000.00,$from,null); $created++;
    ensureFeeStructure($pdo,$cid,$lateFee,250.00,$from,null); $created++;
  }
  $pdo->commit();
  echo json_encode(['status'=>'success','message'=>'Fee sample data seeded.','fee_types'=>[$examFee,$revalFee,$lateFee],'structures_created'=>$created]);
}catch(Throwable $e){ if($pdo->inTransaction()) $pdo->rollBack(); http_response_code(500); echo json_encode(['status'=>'error','message'=>$e->getMessage()]); }
?>


PHP File Manager