<?php 
session_start();
$iduser=$_SESSION['user_idd'];
$date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
$timenow=$date->format('Y-m-d H:i:s');
$timenow2=$date->format('Y-m-d');
$month=$date->format('M');
//$finaldate='Today: '.$date->format('l d M Y - H:i:s');

 include('../include/config.php');
 
 if(isset($_POST["idup"]))
 {
 $id=$_POST["idup"]; 
 $item=addslashes($_POST["item"]);
 $dat=addslashes($_POST["dat"]);
 $amt=addslashes($_POST["amt"]);
 $modd=addslashes($_POST["modd"]);
 $bnk=addslashes($_POST["bnk"]);
 $ftd=addslashes($_POST["ftd"]);

 $yrr=substr($dat, 0, 4);
 $mm=substr($dat, 5, 2);

if($mm=='01'){$mm='January';}
if($mm=='02'){$mm='February';}
if($mm=='03'){$mm='March';}
if($mm=='04'){$mm='April';}
if($mm=='05'){$mm='May';}
if($mm=='06'){$mm='June';}
if($mm=='07'){$mm='July';}
if($mm=='08'){$mm='August';}
if($mm=='09'){$mm='September';}
if($mm=='10'){$mm='October';}
if($mm=='11'){$mm='November';}
if($mm=='12'){$mm='December';}

$sql=mysqli_query($con,"UPDATE farminput SET Item='$item',Date='$dat',Amount='$amt',Paymode='$modd',Bank='$bnk',Month='$mm',Year='$yrr',Details='$ftd' where id='$id'");

if($sql)
{
$msgg='updated farm income details';
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msgg','$timenow','$timenow2','update','$month')");
if($sql3)
{ } 
echo "success";
}else
{
echo "Error occured while trying to update data!!";
}
 }
 else if(isset($_POST["idd"]))
 {
  $id=$_POST["idd"];
  $sqll=mysqli_query($con,"DELETE FROM farminput WHERE id='$id'");
  
  if($sqll)
  {
    $msgg='deleted a farm income entry';
    $sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msgg','$timenow','$timenow2','delete','$month')");
    if($sql3)
    { } 
    
   echo "deleted";  
  }else
  {
    echo 'Sorry, system un able to delete the records';
  }
 } else 
 { 
 $item=addslashes($_POST["item"]);
 $dat=addslashes($_POST["dat"]);
 $amt=addslashes($_POST["amt"]);
 $modd=addslashes($_POST["modd"]);
 $bnk=addslashes($_POST["bnk"]);
 $ftd=addslashes($_POST["ftd"]);

 $yrr=substr($dat, 0, 4);
 $mm=substr($dat, 5, 2);

if($mm=='01'){$mm='January';}
if($mm=='02'){$mm='February';}
if($mm=='03'){$mm='March';}
if($mm=='04'){$mm='April';}
if($mm=='05'){$mm='May';}
if($mm=='06'){$mm='June';}
if($mm=='07'){$mm='July';}
if($mm=='08'){$mm='August';}
if($mm=='09'){$mm='September';}
if($mm=='10'){$mm='October';}
if($mm=='11'){$mm='November';}
if($mm=='12'){$mm='December';}

$sql=mysqli_query($con,"INSERT INTO farminput(Item,Date,Amount,Paymode,Bank,EntryDate,Month,Year,User,Details) values ('$item','$dat','$amt','$modd','$bnk','$timenow2','$mm','$yrr','$iduser','$ftd')");

if($sql)
{
$msgg='added '.$amt.' as a farm income';
$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msgg','$timenow','$timenow2','update','$month')");
if($sql3)
{ } 
echo "success";
}else
{
echo "Error in saving data";
}
 }
?>