<?php
include '../Auth.php';
include '../../class/ClassForm.php';

$inv_no = isset($_POST['inv_no']) ? $_POST['inv_no'] : '';
$inv_date = isset($_POST['inv_date']) ? $_POST['inv_date'] : '';
$bill_name = isset($_POST['bill_name']) ? $_POST['bill_name'] : '';
$bill_addr = $_POST['bill_addr'];
$bill_gstin = isset($_POST['bill_gstin']) ? $_POST['bill_gstin'] : '';
$bill_pan = isset($_POST['bill_pan']) ? $_POST['bill_pan'] : '';
$inv_pos = isset($_POST['inv_pos']) ? $_POST['inv_pos'] : '';
$inv_statecode = isset($_POST['inv_statecode']) ? $_POST['inv_statecode'] : '';
$inv_poref = isset($_POST['inv_poref']) ? $_POST['inv_poref'] : '';
$inv_irn = isset($_POST['inv_irn']) ? $_POST['inv_irn'] : '';
$inv_Particulars = $_POST['inv_Particulars'];
$inv_Hsn = $_POST['inv_Hsn'];
$inv_Amount = $_POST['inv_Amount'];
$inv_netVal = isset($_POST['inv_netVal']) ? $_POST['inv_netVal'] : '';
$inv_taxCGST = isset($_POST['inv_taxCGST']) ? $_POST['inv_taxCGST'] : '';
$inv_taxSGST = isset($_POST['inv_taxSGST']) ? $_POST['inv_taxSGST'] : '';
$inv_taxIGST = isset($_POST['inv_taxIGST']) ? $_POST['inv_taxIGST'] : '';
$inv_reverseCharge = isset($_POST['inv_reverseCharge']) ? $_POST['inv_reverseCharge'] : '';
$taxCGST_val = isset($_POST['taxCGST_val']) ? $_POST['taxCGST_val'] : '';
$taxSGST_val = isset($_POST['taxSGST_val']) ? $_POST['taxSGST_val'] : '';
$taxIGST_val = isset($_POST['taxIGST_val']) ? $_POST['taxIGST_val'] : '';
$roundOff_val = isset($_POST['roundOff_val']) ? $_POST['roundOff_val'] : '';
$inv_total = isset($_POST['inv_total']) ? $_POST['inv_total'] : '';

$templ_id = $_POST['templ_id'];

$entry = array(
    'id' => 0,
    'inv_id' => $inv_no,
    'biller_Name' => $bill_name,
    'biller_Addr1' => $bill_addr[0],
    'biller_Addr2' => $bill_addr[1],
    'biller_Addr3' => $bill_addr[2],
    'biller_Addr4' => $bill_addr[3],
    'biller_GSTIN' => $bill_gstin,
    'biller_PAN' => $bill_pan,
    'biller_Email' => '',
    'biller_Mobile' => '',    
    'extra' => '', 
    'status_' => 1
);
$status = $ClassForm->f_mysql_insert_array('biller_details', $entry);
if(!$status['mysqli_error']){
    $biller_id = $status['mysqli_insert_id'];
    $Inv_entry = array(
        'id' => 0,
        'inv_no' => $inv_no,
        'inv_date' => $inv_date,
        'inv_pos' => $inv_pos,
        'inv_sc' => $inv_statecode,
        'inv_poref' => $inv_poref,
        'inv_irn' => $inv_irn,
        'inv_qrLoc' => '',
        'inv_reverseCharge' => $inv_reverseCharge,
        'billed_to' => $biller_id,
        'tax_id' => 0,    
        'template_id' => $templ_id, 
        'inv_signedDetails' => 0,    
        'extra' => '', 
        'status_' => 1
    );
    $inv_status = $ClassForm->f_mysql_insert_array('domestic_invS', $Inv_entry);
    if(!$inv_status['mysqli_error']){
        $inv_id = $status['mysqli_insert_id'];
        $slno = 0;
        $i = 0;
        foreach($inv_Particulars as $particular){
            $slno ++;
            $pro_entry = array(
                'id' => 0,
                'inv_id' => $inv_id,
                'slno' => $slno,
                'particulars' => $particular,
                's_code' => $inv_Hsn[$i],
                'amount' => $inv_Amount[$i],
                'extra' => $inv_no,                 
                'status_' => 1
            );
            $pro_status = $ClassForm->f_mysql_insert_array('domestic_Service', $pro_entry);
            $i++;
        }

        $tax_entry = array(
            'id' => 0,
            'inv_id' => $inv_id,
            'taxable_amt' => $inv_netVal,
            'cgst_per' => $inv_taxCGST,
            'sgst_per' => $inv_taxSGST,
            'igst_per' => $inv_taxIGST,
            'cgst_val' => $taxCGST_val,
            'sgst_val' => $taxSGST_val,
            'igst_val' => $inv_taxIGST,
            'roindOff' => $roundOff_val,
            'total' => $inv_total,    
            'extra' => '',             
            'status_' => 1
        );
        $tax_status = $ClassForm->f_mysql_insert_array('domestic_taxDetails', $tax_entry);
        if(!$tax_status['mysqli_error']){
            $tax_id = $tax_status['mysqli_insert_id'];

            $result = array(
                error_code => '0',
                error_derail => 'no error found',
                success => true    
            );
            echo json_encode($result);
        }
        else{
            $result = array(
                error_code => '1',
                error_derail => 'Please Check Tax Details or Contact administraor',
                success => false    
            );
            echo json_encode($result);
        }
    }
    else{
        $result = array(
            error_code => '1',
            error_derail => 'Please Check Invoice Details or Contact administraor',
            success => false    
        );
        echo json_encode($result);
    }
}
else{
    $result = array(
        error_code => '1',
        error_derail => 'Please Check Biller Details or Contact administraor',
        success => false    
    );
    echo json_encode($result);
}







?>