<?php
error_reporting(E_ALL);
date_default_timezone_set("Asia/Kolkata");
ob_start();
session_start();

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

$job_card_no = $_GET['job_card_no'];
$invoice_no = $_GET['invoice_no'];
$billing_type = $_GET['billing_type'];
$acc_year = $_GET['acc_year'];
$bill_date = $_GET['bill_date'];

$sql = "select * from jobcard_main where jobcard_no='$job_card_no' ";
$rs = mysql_query($sql);
$sno = 0;
$from = 0;
$data = array();
while ($rsdata = mysql_fetch_array($rs)) {
    $sno = $sno + 1;
    $from = $from + 1;
    $jobcard_no1 = $rsdata['jobcard_no'];
    $createdate = $rsdata['createdate'];
    $print_date = $rsdata['print_date'];
    $job_created = $rsdata['job_created'];
    $cust_name = $rsdata['cust_name'];
    $cust_po = $rsdata['cust_po'];
    $style_no = $rsdata['style_no'];
    $style_name = $rsdata['style_name'];
    $combo = $rsdata['combo'];
    $printed_qty = $rsdata['printed_qty'];
    $billed_status = $rsdata['billed_status'];
    $job_type = $rsdata['job_type'];
    $amount = $rsdata['amount'];
    $cust_po_no = $rsdata['cust_po_no'];
    $qty = $rsdata['qty'];
    $jobcard_values = $rsdata['jobcard_values'];

    $estimate_type = get_estimate_type($jobcard_no1);
    $invoice_no = get_invoice_no($jobcard_no1);
    $dates = get_created_dates($jobcard_no1);
    list($estimate_type, $remark, $desc) = get_estimate_type11($jobcard_no1);

    $data['jobcard_main'] = array(
        'jobcard_no' => $jobcard_no1,
        'createdate' => $createdate,
        'print_date' => $print_date,
        'job_created' => $job_created,
        'cust_name' => $cust_name,
        'cust_po' => $cust_po,
        'style_no' => $style_no,
        'style_name' => $style_name,
        'combo' => $combo,
        'printed_qty' => $printed_qty,
        'billed_status' => $billed_status,
        'job_type' => $job_type,
        'amount' => $amount,
        'cust_po_no' => $cust_po_no,
        'qty' => $qty,
        'jobcard_values' => $jobcard_values,
        'estimate_type' => $estimate_type,
        'invoice_no' => $invoice_no,
        'dates' => $dates,
        'remark' => $remark,
        'description' => $desc
    );
}

$sql_user = "select * from fusing_jobcard_sub where invoice_no='$invoice_no' and billing_type='$_GET[billing_type]' and acc_year='$_GET[acc_year]'";
$rs_user = mysql_query($sql_user);
$rscount_fuse = mysql_num_rows($rs_user);
if ($rscount_fuse != 0) {
    while ($rsdata_user = mysql_fetch_object($rs_user)) {
        $jobcard_nos = $rsdata_user->jobcard_no;
        $inward_dc_no = get_inward_dc_no($jobcard_nos);
    }
} else {
    $jobcard_no = $_GET['job_card_no'];
    $inward_dc_no = get_inward_dc_no($jobcard_no);
}

$sql = "select * from party_creation where party_name='$cust_name' and delete_status!='1' order by party_name ASC ";
$rs1 = mysql_query($sql);
while ($rsdata = mysql_fetch_array($rs1)) {
    $party_no = $rsdata['party_no'];
    $party_name1 = $rsdata['party_name'];
    $mobile_no = $rsdata['mobile_no'];
    $phone_no = $rsdata['phone_no'];
    $address = $rsdata['address'];
    $city = $rsdata['city'];
    $cst_no = $rsdata['cst_no'];
    $tin_no = $rsdata['tin_no'];
    $gst_no = $rsdata['gst_no'];

    $data['party_creation'] = array(
        'party_no' => $party_no,
        'party_name' => $party_name1,
        'mobile_no' => $mobile_no,
        'phone_no' => $phone_no,
        'address' => $address,
        'city' => $city,
        'cst_no' => $cst_no,
        'tin_no' => $tin_no,
        'gst_no' => $gst_no
    );
}

$output = json_encode($data);

header('Content-Type: application/json');
echo $output;

exit;

function get_dc_no($jobcard_no, $particulars)
{
    $sql_user = "select GROUP_CONCAT(delivery_no) as delivery_no  from delivery_challan_sub where job_no='$jobcard_no' and particulars='$particulars' union select delivery_no from sticker_delivery_challan_sub where job_no='$jobcard_no' and particulars='$particulars' ";
    $rs_user = mysql_query($sql_user);
    while ($rsdata_user = mysql_fetch_object($rs_user)) {
        $delivery_no = $rsdata_user->delivery_no;
    }
    return $delivery_no;
}

function get_estimate_type($jobcard_no1)
{
    $sql_user = "select billing_type as billing_type from fusing_production_main where job_card_no='$jobcard_no1' and billing_type!=''  union select billing_type as billing_type from printer_production_main where job_card_no='$jobcard_no1' and billing_type!=''";
    $rs_user = mysql_query($sql_user);
    while ($rsdata_user = mysql_fetch_object($rs_user)) {
        $billing_type = $rsdata_user->billing_type;
    }
    return $billing_type;
}

function get_invoice_no($jobcard_no1)
{
    $sql_user = "select invoice_no as invoice_no from fusing_production_main where job_card_no='$jobcard_no1' and invoice_no!='' union select invoice_no as invoice_no from printer_production_main where job_card_no='$jobcard_no1' and invoice_no!=''";
    $rs_user = mysql_query($sql_user);
    while ($rsdata_user = mysql_fetch_object($rs_user)) {
        $invoice_no = $rsdata_user->invoice_no;
    }
    return $invoice_no;
}

function get_created_dates($jobcard_no1)
{
    $sql_user = "select bill_date as bill_date from fusing_production_main where job_card_no='$jobcard_no1' and invoice_no!='' union select bill_date as bill_date from printer_production_main where job_card_no='$jobcard_no1' and invoice_no!=''";
    $rs_user = mysql_query($sql_user);
    while ($rsdata_user = mysql_fetch_object($rs_user)) {
        $bill_date = $rsdata_user->bill_date;
    }
    return $bill_date;
}

function get_estimate_type11($jobcard_no1)
{
    $sql_user = "select billing_type as billing_type,remarks as remarks,description as description from fusing_production_main where job_card_no='$jobcard_no1' and billing_type!=''  union select billing_type as billing_type,remarks as remarks,description as description from printer_production_main where job_card_no='$jobcard_no1' and billing_type!=''";
    $rs_user = mysql_query($sql_user);
    while ($rsdata_user = mysql_fetch_object($rs_user)) {
        $billing_type = $rsdata_user->billing_type;
        $remarks = $rsdata_user->remarks;
        $description = $rsdata_user->description;
    }
    return array($billing_type, $remarks, $description);
}

function get_inward_dc_no($jobcard_nos)
{
    $sql_user1 = "select customer_dc_no from inward_main where jobcard_no='$jobcard_nos' ";
    $rs_user11 = mysql_query($sql_user1);
    while ($rsdata_user11 = mysql_fetch_object($rs_user11)) {
        $customer_dc_no = $rsdata_user11->customer_dc_no . ",";
    }
    return $customer_dc_no;
}

function get_tax_type($job_card_no)
{
    $sql = "select tax_type as tax_type from printer_production_main where job_card_no='$job_card_no' union select tax_type as tax_type from fusing_production_main where job_card_no='$job_card_no'";
    $rs1 = mysql_query($sql);
    while ($rsdata = mysql_fetch_array($rs1)) {
        $tax_type = $rsdata['tax_type'];
    }
    return $tax_type;
}

function get_freight_charges($jobcard_no)
{
    $sql = "select frieght_charges as frieght from printer_production_main where job_card_no='$jobcard_no' union select frieght_charges as frieght from fusing_production_main where job_card_no='$jobcard_no'";
    $rs1 = mysql_query($sql);
    while ($rsdata = mysql_fetch_array($rs1)) {
        $frieght_charges = $rsdata['frieght'];
    }
    return $frieght_charges;
}
?>
