<?php
  require_once('includes/load.php');
  // Checkin What level user has permission to view this page
  page_require_level(1);
?>
<?php
  $plan = find_by_id_new('plandetails',(int)$_GET['PlanId'],'PlanId');
  if(!$plan){
    $session->msg("d","Missing Article id.");
    redirect('plan.php');
  }
  
   $salesplandetailscheck = find_by_column("salesplandetails",(int)$_GET['PlanId'],"PlanId");

  if(isset($salesplandetailscheck['PlanId'])) {
    $delete_ids = delete_all('salesplandetails',(int)$plan['PlanId'],'PlanId');
  if($delete_ids){
      $session->msg("s","Plan deleted.");
      //redirect('plan.php');
  } else {
      $session->msg("d","Plan deletion failed due to existing records in sales plan.");
      redirect('plan.php');
  }
  }
  
?>
<?php
  $delete_idd = delete_all('draftplandetails',(int)$plan['PlanId'],'PlanId');
  $delete_ids = delete_by_id_new('salesplandetails',(int)$plan['PlanId'],'PlanId');
  $delete_idb = delete_all('tempbom',(int)$plan['PlanId'],'PlanId');
  $delete_idb = delete_all('intermediatearticles',(int)$plan['PlanId'],'PlanId');
  $delete_idp = delete_all('planarticlemapping',(int)$plan['PlanId'],'PlanId');
  $delete_idp = delete_all('planpomapping',(int)$plan['PlanId'],'PlanId');
  $delete_id = delete_by_id_new('plandetails',(int)$plan['PlanId'],'PlanId');
  if($delete_id){
      $session->msg("s","Plan deleted.");
      redirect('plan.php');
  } else {
      $session->msg("d","Plan deletion failed.");
      redirect('plan.php');
  }
?>
