Q: $q24;
";;
}//else{echo "ok";}
while($ginvmast=sqlsrv_fetch_array($get_inv_det, SQLSRV_FETCH_ASSOC)){
$inv_no_db =$ginvmast["V_NO"];
// $inv_date_db =$ginvmast["VOU_DATE"];//YYYY-MM-DD
$inv_date_db =$ginvmast["INV_DATE"];//YYYY-MM-DD
// $inv_date_disp= $inv_date;
$vehicle_no=$ginvmast["VEHICLE_NO"];
$transporter_no=$ginvmast["TRANSPORTER_NO"];
$delivery_to=$ginvmast["DELIVERY_TO"];
$ship_to=$ginvmast["SHIP_TO"];
$consignee_code=$ginvmast["CONSIGNEE_CODE"];
$inv_date=date("d/m/Y",strtotime($inv_date_db));
// $sale_amt =$ginvmast["TOTAL_AMT"];
$tot_igst_amt =$ginvmast["AddIGST"];
$tot_cgst_amt =$ginvmast["AddCGST"];
$tot_sgst_amt =$ginvmast["AddSGST"];
$tax_amt =$ginvmast["TOTAL_AMT_GST"];
// $discount =$ginvmast["discount"];
$inv_amt_tot =$ginvmast["TOTAL_INVOICE_AMT"];
$account_id =$ginvmast["DRCR_CODE"];
$roundoff=$ginvmast["TOTAL_ROUNDING"];
$tot_freight=$ginvmast["TOTAL_FREIGHT"];
$tot_insurance=$ginvmast["TOTAL_INSURANCE"];
$company_code=$ginvmast["COMPANY_CODE"];
$lrrr_no=$ginvmast["LRRR_NO"];
$lrrr_date=$ginvmast["LRRR_DATE"];
}
$q59="SELECT * FROM [COMPANY] WHERE [COMPANY_CODE]='$company_code'";
$gcd=sqlsrv_query($conn,$q59);
if(!$gcd){$filecontents.="\n========ERR ? sellerDtls ?========\n$q59\n"; }
while($gcr=sqlsrv_fetch_array($gcd,SQLSRV_FETCH_ASSOC)) {
$co_gstin=$gcr[""];
$co_name=$gcr["COMPANY_NAME"];
$co_add1=$gcr["ADD_1"];
$co_add2=$gcr["ADD_2"];
$co_loc=$gcr["ADD_3"];
$co_pin=$gcr["PIN"];
$co_state_code=$gcr["ADD_4"];
$co_ph=$gcr["PHONE"];
$co_em=$gcr["EMAIL"];
}
$sellerDtls='
"gstin": "'.$co_gstin.'",
"lglNm": "'.$co_name.'",
"trdNm": "'.$co_name.'",
"addr1": "'.$co_add1.'",
"addr2": "'.$co_add2.'",
"loc" : "'.$co_address.'",
"pin" : "'.$co_pin.'",
"stcd": "'.$co_state_code.'",
"ph": "'.$co_ph.'",
"em": "'.$co_em.'"
';
$filecontents.="\n========sellerDtls ========\n$sellerDtls\n\n";
$q72="SELECT TOP (1) [DRCR_NAME] FROM [ACCOUNT_MASTER] WHERE [DRCR_CODE]='$consignee_code' ";
$filecontents.=$q72.";\n";
$gam=sqlsrv_query($conn,$q72);
while($gar=sqlsrv_fetch_array($gam,SQLSRV_FETCH_ASSOC)) {
$buyer_name=$gar["DRCR_NAME"];
$q88="SELECT * FROM [CUS_SUP_ADDRESS_D] WHERE [DRCR_CODE]='$account_id' AND [SL_NO]='$delivery_to'";
$gamres=sqlsrv_query($conn, $q88);// customer
$filecontents.=$q88."\n";
//$gamres=sqlsrv_query($conn, "SELECT * FROM [CUS_SUP_ADDRESS_D] WHERE [DRCR_CODE]='$consignee_code' AND [SL_NO]='$ship_to'"); // ship to
while($gamr=sqlsrv_fetch_array($gamres,SQLSRV_FETCH_ASSOC)) {
$address1=$gamr["ADD1"].$gamr["ADD2"];
$address2=$gamr["ADD3"].$gamr["ADD4"];
$address3=$gamr["ADD5"];
$city=$gamr["LOCATION"];
$pin=$gamr["PINCODE"];
$phone1=$gamr["PHONE_NO"];
// $phone2=$gamr["phone2"];
$mob1=$gamr["MOBILE_NO"];
// $mob2=$gamr["mob2"];
$gstin=$gamr["GST_NO"];
$buyer_state_code=$gamr["STATE"];
// if(strtoupper($gstin)!="URP"){
// $buyer_state_code=substr($gstin,0,2);
// }else{
// $buyer_state_code=96; // For Exports
// }
//$buyer_state_code=substr($gstin,0,2);
}
}
$transactionType="1";// Transaction type 1 : Regular
if($ship_to!=$delivery_to){
$transactionType="2";// Transaction type 2 : Shipto
$q72st="SELECT TOP (1) [DRCR_NAME] FROM [ACCOUNT_MASTER] WHERE [DRCR_CODE]='$consignee_code' ";
$filecontents.=$q72st.";\n";
$gamst=sqlsrv_query($conn,$q72st);
while($garst=sqlsrv_fetch_array($gamst,SQLSRV_FETCH_ASSOC)) {
$shipToTradeName==$garst["DRCR_NAME"];
}
$q124="SELECT * FROM [CUS_SUP_ADDRESS_D] WHERE [DRCR_CODE]='$consignee_code' AND [SL_NO]='$ship_to'";
$gamresshipto=sqlsrv_query($conn, $q124); // ship to
$filecontents.=$q124."\n";
while($gamrsr=sqlsrv_fetch_array($gamresshipto,SQLSRV_FETCH_ASSOC)) {
// $address1st=$gamrsr["ADD1"].$gamrsr["ADD2"];
// $address2st=$gamrsr["ADD3"].$gamrsr["ADD4"];
// $address3st=$gamrsr["ADD5"];
// $cityst=$gamrsr["LOCATION"];
// $pinst=$gamrsr["PINCODE"];
// $phone1st=$gamrsr["PHONE_NO"];
// $mob1st=$gamrsr["MOBILE_NO"]; // Modified 20-Jun-2023
$address1=$gamrsr["ADD1"].$gamrsr["ADD2"];
$address2=$gamrsr["ADD3"].$gamrsr["ADD4"];
$address3=$gamrsr["ADD5"];
$city=$gamrsr["LOCATION"];
$pin=$gamrsr["PINCODE"];
$phone1st=$gamrsr["PHONE_NO"];
$mob1st=$gamrsr["MOBILE_NO"];
$shipto_gstin=$gamrsr["GST_NO"];
$temp_sup_type=$gamr["GST"]; // Added 20-Jun-2024
$buyer_state_code=$gamrsr["STATE"];// Added 23-Jun-2023 /1500IST
// Added 20-Jun-2024
if($temp_sup_type=='A'){$sup_type="B2B";}
elseif($temp_sup_type=="C"){$sup_type="EXPWP";}
elseif($temp_sup_type=="I"){$sup_type="EXPWOP";}
elseif($temp_sup_type=="D"){$sup_type="SEZWP";}
elseif($temp_sup_type=="K"){$sup_type="SEZWOP";}
elseif($temp_sup_type=="J"){$sup_type="DEXP";}
// if(strtoupper($gstin)!="URP"){
// $buyer_state_code=substr($shipto_gstin,0,2);
// }else{
// $buyer_state_code=96; // For Exports
// }
}
}
// $pin="260301";
// $gstin="05AAACG4414B1ZE";
// $buyer_state_code="05";
//exit("exiint $gstin");
// $buyerDtls='
// "gstin": "'.$gstin.'",
// "lglNm": "'.$buyer_name.'",
// "trdNm": "'.$buyer_name.'",
// "addr1": "'.$address1.'",
// "addr2": "'.$address2.'",
// "loc": "'.$city.'",
// "pin": "'.$pin.'",
// "stcd": "'.$buyer_state_code.'",
// "pos": "'.$buyer_state_code.'",
// "ph": "'.$phone1.'",
// "em": "test@einv.com"
// ';
// $docDtls='
// "typ": "INV",
// "no": "'.$inv_no_db.'",
// "dt": "'.$inv_date.'"
// ';
$doc_details='
"supplyType": "O",
"subSupplyType": "1",
"subSupplyDesc": "",
"docType": "INV",
"docNo": "'.$inv_no_db.'",
"docDate": "'.$inv_date.'"
';
/*
//subSupplyType
1 Supply
2 Import
3 Export
4 Job Work
5 For Own Use
6 Job work Returns
7 Sales Return
8 Others
9 SKD/CKD
10 Line Sales
11 Recipient Not Known
12 Exhibition or Fairs
//docType
INV Tax Invoice
BIL Bill of Supply
BOE Bill of Entry
CHL Delivery Challan
CNT Credit Note
OTH Others
//Transportation Mode
1 Road
2 Rail
3 Air
4 Ship
*/
//"loc" : "'.$co_address.'",
$from_details='
"fromGstin": "'.$co_gstin.'",
"fromTrdName": "'.$co_name.'",
"fromAddr1": "'.$co_add1.'",
"fromAddr2": "'.$co_add2.'",
"fromPlace": "'.$co_address.'",
"fromPincode": "'.$co_pin.'",
"actFromStateCode": "'.$co_state_code.'",
"fromStateCode": "'.$co_state_code.'"
';
$to_details='
"toGstin": "'.$gstin.'",
"toTrdName": "'.$buyer_name.'",
"toAddr1": "'.$address1.'",
"toAddr2": "'.$address2.'",
"toPlace": "'.$city.'",
"toPincode": "'.$pin.'",
"actToStateCode":"'.$buyer_state_code.'",
"toStateCode": "'.$buyer_state_code.'"
';
$filecontents.="=====EWB JSON Generated : ".date('dMY His')."=====\n\n";
$filecontents.="=====fromDtls=====\n".$from_details."\n";
$filecontents.="=====docDtls=====\n".$doc_details."
";
$filecontents.="=====toDtls=====\n".$to_details."\n";
// $shipto_gstin=$gstin;
// $shipto_gstin="";
// $shipto_name="";
//Ship to details
//!Shipt to details
$txn_details='
"transactionType":"'.$transactionType.'",
"dispatchFromGSTIN":"'.$co_gstin.'",
"dispatchFromTradeName":"'.$co_name.'",
"shipToGSTIN":"'.$shipto_gstin.'",
"shipToTradeName":"'.$shipToTradeName.'"
';
/*
//TransactionType
1 Regular
2 Bill To Ship To
3 Bill From Dispatch From
4 Combination of 2 and 3
For type 2, shipToGSTIN, , shipToTradeName may be passed, for type 3,
dispatchFromGSTIN , dispatchFromTradeName may be passed and for type 4, both may
be passed
*/
$q159="SELECT * FROM [SALES_D] WHERE [DOC_NO]='$inv_id'";
$filecontents.=$q159."\n";
$git=sqlsrv_query($conn,$q159);
if(!$git){echo "Err: ";}
while($gitr=sqlsrv_fetch_array($git,SQLSRV_FETCH_ASSOC)){
$temp_item_id=$gitr["I_CODE"];
$temp_qty=$gitr["BALANCEAMOUNT"];
$temp_rate=$gitr["RATE"];
$discount_amt=$gitr["DISCOUNTAMT"];
$temp_item_unit=$gitr["QTY_UNIT"];
$other_charges=$gitr["OTHERSAMT"];// others amt
$qyt_disc_amt=$gitr["QTY_DISC_VALUE"];
$tot_other_charges+=sprintf('%01.2f',$other_charges);
// $temp_item_name1=$gitr["item_name"];
$amt_before_tax=$gitr["TOTAL_BEFORE_TAX"];
//Discount Application
//$amt_before_tax
//!Discount Application
$total_before_tax+=$amt_before_tax;
$temp_tax_per=($gitr["IGST"]+$gitr["CGST"]+$gitr["SGST"]);
if((substr($gstin,0,2))==(substr($co_gstin,0,2))){
// $cgst_amt=$temp_qty*$temp_rate*$temp_tax_per/200;//cmmented
// $sgst_amt=$temp_qty*$temp_rate*$temp_tax_per/200;cmmented
// $igst_amt=0;
// $temp_tax_per=$temp_tax_per_c+$temp_tax_per_s;cmmented
// echo "Intrastate";
$cgst_rate=$temp_tax_per/2;
$sgst_rate=$temp_tax_per/2;
$igst_rate=0;
}else{
// $cgst_amt=0;cmmented
// $sgst_amt=0;cmmented
// // $temp_tax_per=$gitr["IGST"];
// $igst_amt=$temp_qty*$temp_rate*$temp_tax_per/100;
// echo "Interstate";
$cgst_rate=0;
$sgst_rate=0;
$igst_rate=$temp_tax_per;
}
// echo "
TTP$tax_amt $temp_tax_per SGSTA $sgst_amt, CGSTA$cgst_amt , IGST A$igst_amt
";
// $temp_discount_amt=$gitr["discount_amt"];
// $gitdet=sqlsrv_query($conn, "SELECT [I_NAME],[UNIT],[HSN_SAC_CODE] FROM [ITEM_MASTER] WHERE [I_CODE]='$temp_item_id'");
$q272="SELECT [I_NAME],[ISSUE_UNIT],[RDPF],[HSN_SAC_CODE] FROM [ITEM_MASTER] WHERE [I_CODE]='$temp_item_id'";
$filecontents.=$q272."\n";
//echo "$q272;
";
$gitdet=sqlsrv_query($conn,$q272);
while($gitdetr=sqlsrv_fetch_array($gitdet,SQLSRV_FETCH_ASSOC)){
$temp_item_name=$gitdetr["I_NAME"];
//$temp_item_unit=$gitdetr["UNIT"];
$temp_vat_code=$gitdetr["HSN_SAC_CODE"];
$rdpf=$gitdetr["RDPF"];
if($rdpf=='F'){
$temp_item_unit="NOS";
}else{
$temp_item_unit=$gitdetr["ISSUE_UNIT"];
}
}
// echo "iid $temp_item_id $temp_vat_code $temp_item_unit
";
//$temp_item_unit=$mysqli->result($gitdet,0,"unit");
// if($tt==0){}
if($tt>0){
$item_data_details.=",";
}
$tt++;
$temp_line=sprintf('%01.2f',($temp_qty*$temp_rate));
$temp_tax_amt=sprintf('%01.2f',($temp_qty*$temp_rate*$temp_tax_per/100));
$sub_total=$sub_total+$temp_line;
$ald=sprintf('%01.2f',($temp_line-$temp_discount_amt));
$tot_ald+=$ald;
//$ald=sprintf('%01.2f',$ald);
$tot_tax_amt=$tot_tax_amt+$temp_tax_amt;
// $tot_discount=$tot_discount+$temp_discount_amt;
$l_st=$ald+$temp_tax_amt;
$tot_st=$tot_st+$l_st;
// $tot_cgst_amt+=$cgst_amt;;
// $tot_sgst_amt+=$sgst_amt;
// $tot_igst_amt+=$igst_amt;;
$totItemVal=sprintf('%01.2f',(($temp_qty*$temp_rate)+($temp_qty*$temp_rate*$temp_tax_per/100)+$other_charges));
$item_data_details.=' {
"productName":"'.ucwords(strtolower($temp_item_name)).'",
"productDesc":"'.ucwords(strtolower($temp_item_name)).'",
"hsnCode": "'.$temp_vat_code.'",
"quantity": "'.$temp_qty.'",
"qtyUnit": "'.strtoupper($temp_item_unit).'",
"cgstRate": "'.$cgst_rate.'",
"sgstRate": "'.$sgst_rate.'",
"igstRate": "'.$igst_rate.'",
"cessRate": "0",
"cessNonAdvol":"0",
"taxableAmount":"'.sprintf('%01.2f',($amt_before_tax)).'"
}';
// $itemList.='{
// "slNo": "'.$tt.'",
// "prdDesc": "'.ucwords(strtolower($temp_item_name)).'",
// "isServc": "N",
// "hsnCd": "'.$temp_vat_code.'",
// "qty": "'.sprintf('%01.3f',$temp_qty).'",
// "unit": "'.strtoupper($temp_item_unit).'",
// "unitPrice": "'.$temp_rate.'",
// "totAmt": "'.(sprintf('%01.2f',$ald)).'",
// "discount": "'.sprintf('%01.2f',$temp_discount_amt).'",
// "assAmt": "'.(sprintf('%01.2f',$ald)-sprintf('%01.2f',$temp_discount_amt)).'",
// "gstRt": "'.sprintf('%01.2f',$temp_tax_per).'",
// "igstAmt": "'.sprintf('%01.2f',$igst_amt).'",
// "cgstAmt":"'.sprintf('%01.2f',$cgst_amt).'",
// "sgstAmt":"'.sprintf('%01.2f',$sgst_amt).'",
// "cesRt": 0,
// "cesAmt": 0,
// "cesNonAdvlAmt": 0,
// "stateCesRt": 0,
// "stateCesAmt": 0,
// "stateCesNonAdvlAmt": 0,
// "othChrg": "'.sprintf('%01.2f',$other_charges).'",
// "totItemVal":"'.$totItemVal.'"
// }';
}
$filecontents.="=====itemList=====\n".$itemList."\n";
//echo "GST TYPE =$gst_type
$inv_date>= $gst_imple_date";
$sub_total4=$sub_total-$tot_discount+$fwd+$tot_tax_amt+$fwd_tax_amt+$tot_other_charges+$roundoff;
// $roundoff=$inv_amt-$sub_total4;
// $roundoff=round($inv_amt)-$inv_amt;
// echo "tot_st : $tot_st Sto 4 $sub_total4, $roundoff roundoff=$inv_amt-$sub_total4
";
//$inv_amt_tot
// $inv_amt_tot=round($inv_amt)+$roundoff;
// echo "
$tot_ald+$tot_other_charges+$roundoff
";
// $valDtls='
// "assVal": '.sprintf('%01.2f',($tot_ald)).',
// "cgstVal":'.sprintf('%01.2f',$tot_cgst_amt).',
// "sgstVal":'.sprintf('%01.2f',$tot_sgst_amt).',
// "igstVal":'.sprintf('%01.2f',$tot_igst_amt).',
// "cesVal": 0,
// "stCesVal": 0,
// "totInvVal": '.sprintf('%01.2f',($sub_total4+$roundoff)).',
// "rndOffAmt": '.sprintf('%01.2f',$roundoff).'';
// $filecontents.="=====valDtls=====\n".$valDtls."\n
";
//"otherValue":"0",
$value_details='
"otherValue":"'.sprintf('%01.2f',($tot_other_charges+$roundoff+$tot_freight+$tot_insurance)).'",
"totalValue": "'.sprintf('%01.2f',$total_before_tax).'",
"cgstValue": "'.sprintf('%01.2f',$tot_cgst_amt).'",
"sgstValue": "'.sprintf('%01.2f',$tot_sgst_amt).'",
"igstValue": "'.sprintf('%01.2f',$tot_igst_amt).'",
"cessValue": "0",
"cessNonAdvolValue":"0",
"totInvValue": "'.sprintf('%01.2f',$inv_amt_tot).'"
';
/*
GETTING TRANSPORTER NAME
*/
$q363="SELECT * FROM [TRANSPORTER_H] WHERE [TRANSPORTER_NO]='$transporter_no'";
$filecontents.=$q363."\n\n";
$get_tr_det=sqlsrv_query($conn, $q363);
if(!$get_tr_det){
$filecontents.= "APE : Error in Getting Transporter details \n using $q363 \n\n.";;
}//else{echo "ok";}
while($gtransmast=sqlsrv_fetch_array($get_tr_det, SQLSRV_FETCH_ASSOC)){
$transporter_name =$gtransmast["TRANSPORTER_NAME"];
$transporter_gstin =$gtransmast["GST_NO"];
}
//$transporter_doc_no="";//to be fetched
//$transporter_doc_date="";//to be fetched
$transporter_details='
"transporterId":"'.$transporter_gstin.'",
"transporterName": "'.$transporter_name.'",
"transDocNo": "'.$lrrr_no.'",
"transMode": "1",
"transDistance": "'.$transport_distance.'",
"transDocDate": "'.$lrrr_date.'",
"vehicleNo": "'.$vehicle_no.'",
"vehicleType": "'.$vehicle_type.'"
';
//TranMode
$filecontents.="=====TxnDtls=====\n".$txn_details."\n";
$filecontents.="=====valDtls=====\n".$value_details."\n";
$filecontents.="=====transporterDtls=====\n$q363\n".$transporter_details."\n";
$filecontents.="\n========End of File ========\n";
//saving JSON
//$file_path ='json_files'.DIRECTORY_SEPARATOR.date('Ym').DIRECTORY_SEPARATOR.date('d');
$file_path ='json_files/inv/'.date('Ymd');
//$set_qr_code_path = 'qr_assets/';
// If directory is not created, the create a new directory
if(!file_exists($file_path)){
mkdir($file_path);
}
//Set a file name
$filename = $file_path."/EWB-INV-".$inv_id."-".time().'.log';
append_file($filename,$filecontents);
function write_file($filename,$newdata)
{
$f=fopen($filename,"w");
fwrite($f,$newdata);
fclose($f);
}
function append_file($filename,$newdata)
{
$f=fopen($filename,"a");
fwrite($f,$newdata);
fclose($f);
}
function read_file($filename)
{
$f=fopen($filename,"r");
$data=fread($f,filesize($filename));
fclose($f);
return $data;
}
// echo "here 359";
//!saving JSON
?>