<?php

error_reporting(0);
include("../../include/db.php");

function get_jobtype($job_card_no)
{
	 	$sql_user="select *	from  jobcard_main where jobcard_no='$job_card_no'";
		$rs_user=mysql_query($sql_user);
			while($rsdata_user=mysql_fetch_object($rs_user))
			{ 
			$job_type=$rsdata_user->job_type;
			}
return $job_type;
	
}
function get_job_types($job_card_no)
{
	 	$sql_user="select *	from  jobcard_main where jobcard_no='$job_card_no'";
		$rs_user=mysql_query($sql_user);
			while($rsdata_user=mysql_fetch_object($rs_user))
			{ 
			$job_type=$rsdata_user->job_type;
			}
return $job_type;
}

function get_fabricstype($job_card_no)
{
	 	$sql_user="select *	from  jobcard_main where jobcard_no='$job_card_no'";
		$rs_user=mysql_query($sql_user);
			while($rsdata_user=mysql_fetch_object($rs_user))
			{ 
			$fabric_type=$rsdata_user->fabric_type;
			}
return $fabric_type;
}

if ($_POST['action'] == "delete_invoice") {

    $invoice_no = $_POST['invoice_no'];
    $p_id = $_POST['p_id'];
    $billing_type = $_POST['billing_type'];
    $today = date('Y-m-d');

    function insertDeletedRecord($data, $type) {
        $job_card_no = $data['job_card_no'];
        $print_date = $data['print_date'];
        $cust_name = $data['cust_name'];
        $job_created = $data['job_created'];
        $cust_po = $data['cust_po'];
        $style_no = $data['style_no'];
        $style_name = $data['style_name'];
        $combo = $data['combo'];
        $printed_qty = $data['printed_qty'];
        $invoice_no = $data['invoice_no'];
        $bill_date = $data['bill_date'];
        $payment_terms = $data['payment_terms'];
        $billing_type = $data['billing_type'];
        $cr_date = date('Y-m-d');

        if ($type == 'fusing') {
            $fusing_status = $data['fusing_status'];
            $fabric_type = $data['fabric_type'];
            $job_type = get_job_types($job_card_no);
            $refer = 'Fusing';

            $sql = "INSERT INTO deleted_production_main (job_card_no, print_date, cust_name, job_created, cust_po, style_no, style_name, combo, printed_qty, printer_status, job_type, refer, cr_date, fabric_type, billing_type, invoice_no, bill_date, payment_terms) 
                    VALUES ('$job_card_no', '$print_date', '$cust_name', '$job_created', '$cust_po', '$style_no', '$style_name', '$combo', '$printed_qty', '$fusing_status', '$job_type', '$refer', '$cr_date', '$fabric_type', '$billing_type', '$invoice_no', '$bill_date', '$payment_terms')";
        } else {
            $printer_status = $data['printer_status'];
            $job_type = get_jobtype($job_card_no);
            $fabric_type = get_fabricstype($job_card_no);
            $refer = 'Printer';

            $sql = "INSERT INTO deleted_production_main (job_card_no, print_date, cust_name, job_created, cust_po, style_no, style_name, combo, printed_qty, refer, job_type, fabric_type, printer_status, cr_date, billing_type, invoice_no, bill_date, payment_terms) 
                    VALUES ('$job_card_no', '$print_date', '$cust_name', '$job_created', '$cust_po', '$style_no', '$style_name', '$combo', '$printed_qty', '$refer', '$job_type', '$fabric_type', '$printer_status', '$cr_date', '$billing_type', '$invoice_no', '$bill_date', '$payment_terms')";
        }

        mysql_query($sql);
    }

    // Fetch & Insert from fusing_production_main
    $res = mysql_query("SELECT * FROM fusing_production_main WHERE invoice_no='$invoice_no' AND p_id='$p_id' AND billing_type='$billing_type'");
    while ($row = mysql_fetch_array($res)) {
        insertDeletedRecord($row, 'fusing');
    }

    // Fetch & Insert from printer_production_main
    $res = mysql_query("SELECT * FROM printer_production_main WHERE invoice_no='$invoice_no' AND p_id='$p_id' AND billing_type='$billing_type'");
    while ($row = mysql_fetch_array($res)) {
        insertDeletedRecord($row, 'printer');
    }

    // Calculate Accounting Year
    $month_year = date('Y-m', strtotime($today));
    list($y, $m) = explode('-', $month_year);
    $acc_year = ($m < 4) ? $y : $y + 1;

    // Reset billed values
    mysql_query("UPDATE fusing_production_main 
                    SET billed_status='0', billing_type='', payment_terms='', tax_type='', frieght_charges='', invoice_no='', bill_date=''
                    WHERE invoice_no='$invoice_no' AND billing_type='$billing_type' AND p_id='$p_id'");

    mysql_query("UPDATE printer_production_main 
                    SET billed_status='0', billing_type='', payment_terms='', tax_type='', frieght_charges='', invoice_no='', bill_date=''
                    WHERE invoice_no='$invoice_no' AND billing_type='$billing_type'");

    // Delete from jobcard sub
    mysql_query("DELETE FROM fusing_jobcard_sub 
                    WHERE invoice_no='$invoice_no' AND acc_year='$acc_year' AND billing_type='$billing_type'");

    include("billing_unbill_entry_list.php");
}

if($_POST['action']=="update" && isset($_POST['invoice_no'])) {
    $invoice_no = $_POST['invoice_no'];
    $job_card_no = $_POST['job_card_no'];

    $update_sql = "UPDATE printer_production_main SET
        bill_date='{$_POST['entry_date']}',
        billing_type='{$_POST['billing_type']}',
        frieght_charges='{$_POST['frieght']}',
        payment_terms='{$_POST['payment_terms']}',
        tax_type='{$_POST['tax_type']}',
        remarks='{$_POST['remarks']}',
        hsn_code='{$_POST['hsn_code']}',
        description='{$_POST['description']}',
        net_total='{$_POST['net_amt']}'
        WHERE invoice_no='$invoice_no' AND job_card_no='$job_card_no'";
    $res_update = mysql_query($update_sql);

    if(mysql_affected_rows()==0){
        // If not found in printer table, try fusing
        $update_sql2 = "UPDATE fusing_production_main SET
            bill_date='{$_POST['entry_date']}',
            billing_type='{$_POST['billing_type']}',
            frieght_charges='{$_POST['frieght']}',
            payment_terms='{$_POST['payment_terms']}',
            tax_type='{$_POST['tax_type']}',
            remarks='{$_POST['remarks']}',
            hsn_code='{$_POST['hsn_code']}',
            description='{$_POST['description']}',
            net_total='{$_POST['net_amt']}'
            WHERE invoice_no='$invoice_no' AND job_card_no='$job_card_no'";
        mysql_query($update_sql2);
    }
    echo "Billing updated successfully.";
    exit;
}

// function get_check_item_jobcard_sub($jobcard_number2,$acc_year)
// {
//   $sql_query="select * from fusing_jobcard_sub where jobcard_no='$jobcard_number2' and acc_year='$acc_year' ";
//   $rs=mysql_query($sql_query);
//   $rscount=mysql_num_rows($rs);
// 	return $rscount;
//  }

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

// $year = date('Y', strtotime($create_date));
// $month = date('m', strtotime($create_date));


// $acc_year = ($month >= 4) ? ($year + 1) : $year;

// 	$check_item_jobcard = get_check_item_jobcard_sub($_POST['jobcard_no_billing'], $acc_year);

// $create_date = date('Y-m-d');

// if ($check_item_jobcard == 0) {
//     // Insert new record
//     $Insql2 = mysql_query("
//         INSERT INTO fusing_jobcard_sub (jobcard_no, created_date, jobcard_number, acc_year)
//         VALUES ('$_POST[jobcard_no_billing]', '$create_date', '$_POST[jobcard_number]', '$acc_year')
//     ");

//     echo "Inserted Successfully: INSERT INTO fusing_jobcard_sub (jobcard_no, created_date, jobcard_number, acc_year)
//           VALUES ('$_POST[jobcard_no_billing]', '$create_date', '$_POST[jobcard_number]', '$acc_year')";
//           exit();
// } else {
//     // Update existing record
//     $Upsql2 = mysql_query("
//         UPDATE fusing_jobcard_sub 
//         SET created_date = '$create_date',
//             jobcard_number = '$_POST[jobcard_number]',
//             acc_year = '$acc_year'
//         WHERE jobcard_no = '$_POST[jobcard_no_billing]'
//     ");

//     echo "Updated Successfully: UPDATE fusing_jobcard_sub 
//           SET created_date = '$create_date', jobcard_number = '$_POST[jobcard_number]', acc_year = '$acc_year'
//           WHERE jobcard_no = '$_POST[jobcard_no_billing]'";
// }

// include("jobcard_sublist.php");

// }

?>