<?php
include("../../include/db.php");
/**********************************Main  ADD ***************************************/
if($_POST['action']=='Save')
{
$delivery_no=$_POST['delivery_no'];
$date_time=$_POST['date_time'];
$job_no=$_POST['job_no'];
$created_by=$_POST['created_by'];
$customer_name=$_POST['customer_name'];
$style_no=$_POST['style_no'];
$style_name=$_POST['style_name'];
$combo=$_POST['combo'];
$cust_po_no=$_POST['cust_po_no'];
$job_type=$_POST['job_type'];
$fabric_type=$_POST['fabric_type'];
$remarks=$_POST['remarks'];
$overal_qty=$_POST['overal_qty'];
$deliv_status=$_POST['deliv_status'];
$delivery_through=$_POST['delivery_through'];
$vehicle_no=$_POST['vehicle_no'];
$cr_dates=date('Y-m-d');
$acadamic_yr=explode("-",$cr_dates);
$acdmc_yr_res=$acadamic_yr[0];

$create_date=date('Y-m-d');
$explodes=explode("-",$create_date);
$year1=$explodes[0];
$year2=$explodes[1];
$year=$year1."-".$year2;

if(($year=='2014-04')||($year=='2014-05')||($year=='2014-06')||($year=='2014-07')||($year=='2014-08')||($year=='2014-09')||($year=='2014-10')||($year=='2014-11')||($year=='2014-12')||($year=='2015-01')||($year=='2015-02')||($year=='2015-03'))
{
$acc_year='2015';
}

if(($year=='2015-04')||($year=='2015-05')||($year=='2015-06')||($year=='2015-07')||($year=='2015-08')||($year=='2015-09')||($year=='2015-10')||($year=='2015-11')||($year=='2015-12')||($year=='2016-01')||($year=='2016-02')||($year=='2016-03'))
{
$acc_year='2016';
}

if(($year=='2016-04')||($year=='2016-05')||($year=='2016-06')||($year=='2016-07')||($year=='2016-08')||($year=='2016-09')||($year=='2016-10')||($year=='2016-11')||($year=='2016-12')||($year=='2017-01')||($year=='2017-02')||($year=='2017-03'))
{
$acc_year='2017';
}

if(($year=='2017-04')||($year=='2017-05')||($year=='2017-06')||($year=='2017-07')||($year=='2017-08')||($year=='2017-09')||($year=='2017-10')||($year=='2017-11')||($year=='2017-12')||($year=='2018-01')||($year=='2018-02')||($year=='2018-03'))
{
$acc_year='2018';
}


if(($year=='2018-04')||($year=='2018-05')||($year=='2018-06')||($year=='2018-07')||($year=='2018-08')||($year=='2018-09')||($year=='2018-10')||($year=='2018-11')||($year=='2018-12')||($year=='2019-01')||($year=='2019-02')||($year=='2019-03'))
{
$acc_year='2019';
}



if(($year=='2019-04')||($year=='2019-05')||($year=='2019-06')||($year=='2019-07')||($year=='2019-08')||($year=='2019-09')||($year=='2019-10')||($year=='2019-11')||($year=='2019-12')||($year=='2020-01')||($year=='2020-02')||($year=='2020-03'))
{
$acc_year='2020';
}

if(($year=='2020-04')||($year=='2020-05')||($year=='2020-06')||($year=='2020-07')||($year=='2020-08')||($year=='2020-09')||($year=='2020-10')||($year=='2020-11')||($year=='2020-12')||($year=='2021-01')||($year=='2021-02')||($year=='2021-03'))
{
$acc_year='2021';
}

if(($year=='2021-04')||($year=='2021-05')||($year=='2021-06')||($year=='2021-07')||($year=='2021-08')||($year=='2021-09')||($year=='2021-10')||($year=='2021-11')||($year=='2021-12')||($year=='2022-01')||($year=='2022-02')||($year=='2022-03'))
{
$acc_year='2022';
}

if(($year=='2022-04')||($year=='2022-05')||($year=='2022-06')||($year=='2022-07')||($year=='2022-08')||($year=='2022-09')||($year=='2022-10')||($year=='2022-11')||($year=='2022-12')||($year=='2023-01')||($year=='2023-02')||($year=='2023-03'))
{
$acc_year='2023';
}

if(($year=='2023-04')||($year=='2023-05')||($year=='2023-06')||($year=='2023-07')||($year=='2023-08')||($year=='2023-09')||($year=='2023-10')||($year=='2023-11')||($year=='2023-12')||($year=='2024-01')||($year=='2024-02')||($year=='2024-03'))
{
$acc_year='2024';
}

if(($year=='2024-04')||($year=='2024-05')||($year=='2024-06')||($year=='2024-07')||($year=='2024-08')||($year=='2024-09')||($year=='2024-10')||($year=='2024-11')||($year=='2024-12')||($year=='2025-01')||($year=='2025-02')||($year=='2025-03'))
{
$acc_year='2025';
}

$Insql=mysql_query("insert into delivery_challan_main(delivery_no,date_time,job_no,created_by,customer_name,style_no,style_name,combo,customer_po_no,job_type,fabric_type,remarks,total_qty,deliv_status,cr_date,acad_yr,delivery_through,vehicle_no)values('$delivery_no','$date_time','$job_no','$created_by','$customer_name','$style_no','$style_name','$combo','$cust_po_no','$job_type','$fabric_type','$remarks','$overal_qty','$deliv_status','$cr_dates','$acc_year','$delivery_through','$vehicle_no')");

if($deliv_status=='Completed')
{
$Insql1=mysql_query("update jobcard_main SET deliv_status='1' where  jobcard_no='$job_no'");
$Insql2=mysql_query("update jobcard_sublist SET deliv_status='1' where  jobcard_no='$job_no'");

$Insql3=mysql_query("update inward_main SET deliv_status='1' where  jobcard_no='$job_no'");
$Insql4=mysql_query("update inward_sub_list SET deliv_status='1' where  jobcard_no='$job_no'");

$Insql5=mysql_query("update fusing_production_main SET deliv_status='1' where  job_card_no='$job_no'");
$Insql6=mysql_query("update fusing_production_sublist SET deliv_status='1' where  job_card_no='$job_no'");

$Insql7=mysql_query("update printer_production_main SET deliv_status='1' where  job_card_no='$job_no'");
$Insql8=mysql_query("update printer_production_sublist SET deliv_status='1' where  job_card_no='$job_no'");
}

echo "Added Successfully";
}
/*********************************** Main Update  *********************************************/
if($_POST['action']=='Edit')
{
$edit_id=$_POST['edit_id'];
$delivery_no=$_POST['delivery_no'];
$date_time=$_POST['date_time'];
$job_no=$_POST['job_no'];
$created_by=$_POST['created_by'];
$customer_name=$_POST['customer_name'];
$style_no=$_POST['style_no'];
$style_name=$_POST['style_name'];
$combo=$_POST['combo'];
$cust_po_no=$_POST['cust_po_no'];
$job_type=$_POST['job_type'];
$fabric_type=$_POST['fabric_type'];
$remarks=$_POST['remarks'];
$overal_qty=$_POST['overal_qty'];
$deliv_status=$_POST['deliv_status'];
$cr_dates=date('Y-m-d');
$acadamic_yr=explode("-",$cr_dates);
$acdmc_yr_res=$acadamic_yr[0];
$delivery_through=$_POST['delivery_through'];
$vehicle_no=$_POST['vehicle_no'];
$Insql=mysql_query("update delivery_challan_main SET delivery_no='$delivery_no',date_time='$date_time',job_no='$job_no',created_by='$created_by',customer_name='$customer_name',style_no='$style_no',style_name='$style_name',combo='$combo',customer_po_no='$cust_po_no',job_type='$job_type',fabric_type='$fabric_type',remarks='$remarks',total_qty='$overal_qty',vehicle_no='$vehicle_no',delivery_through='$delivery_through',deliv_status='$deliv_status' where id='$_POST[edit_id]'");

if($deliv_status=='Completed')
{
$Insql1=mysql_query("update jobcard_main SET deliv_status='1' where  jobcard_no='$job_no'");
$Insql2=mysql_query("update jobcard_sublist SET deliv_status='1' where  jobcard_no='$job_no'");

$Insql3=mysql_query("update inward_main SET deliv_status='1' where  jobcard_no='$job_no'");
$Insql4=mysql_query("update inward_sub_list SET deliv_status='1' where  jobcard_no='$job_no'");

$Insql5=mysql_query("update fusing_production_main SET deliv_status='1' where  job_card_no='$job_no'");
$Insql6=mysql_query("update fusing_production_sublist SET deliv_status='1' where  job_card_no='$job_no'");

$Insql7=mysql_query("update printer_production_main SET deliv_status='1' where  job_card_no='$job_no'");
$Insql8=mysql_query("update printer_production_sublist SET deliv_status='1' where  job_card_no='$job_no'");

}
echo "Updated Successfully";
}
/********************************** Sublist Add ***************************************/
function get_inward1($delivery_no)
{
    $sql_user="select sum(qty) as qty from  delivery_challan_sub where delivery_no='$delivery_no' ";
	$rs_user=mysql_query($sql_user);
	while($rsdata_user=mysql_fetch_object($rs_user))
	{ 
	$qty=$rsdata_user->qty;
	}
	return $qty;
}

function get_job_nos($jobcard_no)
{
    $sql_user11="select sum(qty) as qty from  jobcard_sublist where jobcard_no='$jobcard_no' ";
	$rs_user11=mysql_query($sql_user11);
	while($rsdata_user11=mysql_fetch_object($rs_user11))
	{ 
	$qty=$rsdata_user11->qty;
	}
	return $qty;
}

if($_POST['action']=='ADD')
{
$delivery_no=$_POST['delivery_no'];
$sum_inward2=get_inward1($delivery_no);
$date_time=$_POST['date_time'];
$job_no=$_POST['job_no'];
$total_piece=get_job_nos($job_no);
$customer_name=$_POST['customer_name'];
$job_type=$_POST['job_type'];
$fabric_type=$_POST['fabric_type'];
$delivery_date=$_POST['delivery_date'];
$deliv_particular=$_POST['deliv_particular'];
$roll_width=$_POST['roll_width'];
$kg_height=$_POST['kg_height'];
$inward_qty=$_POST['inward_qty'];
$tot_qty=$_POST['tot_qty'];

$cr_dates=date('Y-m-d');
$acadamic_yr=explode("-",$cr_dates);
$acdmc_yr_res=$acadamic_yr[0];
if($total_piece!=$sum_inward2)
{
$Insql=mysql_query("insert into delivery_challan_sub(delivery_no,date_time,job_no,cust_name,job_type,fabric_type,delivery_date,particulars,rollno_width,kg_meter_height,qty,cr_date,acad_yr,qty_hide)values('$delivery_no','$date_time','$job_no','$customer_name','$job_type','$fabric_type','$delivery_date','$deliv_particular','$roll_width','$kg_height','$inward_qty','$cr_dates','$acdmc_yr_res','$tot_qty')");
}
else
{
}
include('delivery_challan_sublist.php');
}
/********************************** Sublist Edit ***************************************/
if($_POST['action']=='EDIT')
{
$delivery_no=$_POST['delivery_no'];
$date_time=$_POST['date_time'];
$job_no=$_POST['job_no'];
$customer_name=$_POST['customer_name'];
$job_type=$_POST['job_type'];
$fabric_type=$_POST['fabric_type'];
$delivery_date=$_POST['delivery_date'];
$deliv_particular=$_POST['deliv_particular'];
$roll_width=$_POST['roll_width'];
$kg_height=$_POST['kg_height'];
$inward_qty=$_POST['inward_qty'];
$tot_qty=$_POST['tot_qty'];

$cr_dates=date('Y-m-d');
$acadamic_yr=explode("-",$cr_dates);
$acdmc_yr_res=$acadamic_yr[0];

$Insql=mysql_query("update delivery_challan_sub SET delivery_no='$delivery_no',date_time='$date_time',job_no='$job_no',cust_name='$customer_name',job_type='$job_type',fabric_type='$fabric_type',delivery_date='$delivery_date',particulars='$deliv_particular',rollno_width='$roll_width',kg_meter_height='$kg_height',qty='$inward_qty',qty_hide='$tot_qty' where sub_id='$_POST[edit_id]' and job_no='$job_no'");
				 
include('delivery_challan_sublist.php');
}
/****************************** Sub Delete ******************************************/
if($_POST['action']=='del_deliv_sublist')
{
$Insql=mysql_query("delete from delivery_challan_sub where sub_id='$_POST[id]'");
include('delivery_challan_sublist.php');
}
/******************************* Main Delete *************************************************/
if($_POST['action']=='delete_deliv_main')
{
$Insql=mysql_query("delete from delivery_challan_main where id='$_POST[id]'");
$Insql=mysql_query("delete from delivery_challan_sub where delivery_no='$_POST[delivery_no]' and job_no='$_POST[job_no]' ");
}
?>