<?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");
}

?>