Skip to content

Merciglobal Cloud ERP โ€“ E-Way Bill Help Page

๐Ÿ“˜ Overview

This help page explains the PHP code used in Merciglobal Cloud ERP for generating an E-Way Bill programmatically from a sales invoice.

The script:

  • Fetches invoice data
  • Calculates GST
  • Loads transporter details
  • Adds invoice items
  • Generates Government E-Way copy
  • Opens printable HTML automatically

๐Ÿ“‚ PHP Code

<?php
session_start();
chdir('..');
require "funcs.php";

$masterid = $_GET['id'];
$mode = $_GET['mode'];

$printcond = " a.id=$masterid";

$q = "SELECT a.billparty as partyid,a.serial as billno,a.date,a.netamt - a.gstamt as netpay,d.gstper as taxper,b.name as transport,
       b.gstin as trgstno,c.gstin as partygst,a.netamt,a.gstamt as taxamt,a.billparty as acid,a.vehicalno
       FROM ".tbl('trn_fac_sales')." a 
       LEFT JOIN ".tbl('TRANSPORT')." b on b.id = a.transport
       LEFT JOIN ".tbl('fasmast')." c on c.id = a.billparty
       LEFT JOIN ".tbl('trn_fac_salesg')." d on d.linkid = a.id
       WHERE a.id=$masterid ";

$res = myExecute($q);
$row = getrow($res);

$acid       = $row['acid'];
$acid       = $row['partyid'];
$invno      = $row['billno'];
$date       = $row['date'];
$netpay     = $row['netpay'];
$taxper     = $row['taxper'];
$transport  = $row['transport'];
$trpgstno   = $row['trgstno'];
$netamt     = $row['netamt'];
$igst       = $row['taxamt'];
$ptygstin   = $row['partygst'];
$vehicle    = $row['vehicleno'];

$CompState = CompStateCode();
$PtyState = substr($ptygstin,0,2);

if(a2c($CompState) == a2c($PtyState)){
    $cgstper = $taxper/2;
    $sgstper = $taxper/2;
    $igstper = 0;       
}
else{   
    $cgstper = 0;
    $sgstper = 0;
    $igstper = $taxper; 
}

$igstamt    = getgstamt('IGST',$igst,$acid);
$cgstamt    = getgstamt('CGST',$igst,$acid);
$sgstamt    = getgstamt('SGST',$igst,$acid);

$eWay = new EWayBill('trn_sales',$masterid, $acid, $trans_id=0, $docno=$invno, $row['date'],$netpay,$cgstamt,$sgstamt,$igstamt, $row['lrno'], $row['date'], 
$vehicle,$transport,$trpgstno,$netamt);

$cQI = " SELECT a.itemid,b.name as item,b.hsncode,
           CASE when d.saletype='GREY' then a.noofcarton
              when d.saletype='BLEACH' then a.noofcarton
              when d.saletype='BEAM' then a.noofbeam
              when d.saletype='YARN' then a.noofcarton END AS pcs,a.meter as mtr,a.weight AS weight,a.per,a.amount AS amt,c.email
         FROM ".tbl('trn_fac_salesg')." a 
         LEFT JOIN ".tbl('MST_FAC_ITEM')." b on b.id = a.itemid
         LEFT JOIN ".tbl('trn_fac_sales')." d on d.id = a.linkid
         LEFT JOIN ".tbl('fasmast')." c on c.id = d.billparty
         WHERE a.linkid=$masterid
";

$resI = myexecute($cQI);
while ($rowI = mysqli_fetch_array($resI,MYSQLI_ASSOC)){

    if($rowI['per'] == 'PCS'){
        $qty = $rowI['pcs'];
        $per = 'PCS';
    }

    if($rowI['per'] == 'MTR'){
        $qty = $rowI['mtr'];
        $per = 'SQM';
    }

    if($rowI['per'] == 'KG'){
        $qty = $rowI['weight'];
        $per = 'KGC';
    }

    $qty = 0;

    $eWay->addItem($rowI['item'],$rowI['item'],$rowI['hsncode'],$qty,$per,$rowI['amt'],$sgstper,$cgstper,$igstper,$cessrate=0,$cessadvol=0);
}

$html = $eWay->GovtCopy();
$html .= "<script> window.print(); </script>";

$log = '../common/'.$_SESSION['wa_project'].'.eway_'.rand().'.html';
file_put_contents($log, $html);

echo "<script> newWin('../$log, '', '_blank' ); </script>";

$html = "<script>
               var mywin = window.open('../'+'$log' ,'_blank');
               if (!mywin){
                  alert('Popups are Blocked in your browser./n Please Allow Popups');
               }
            </script>";

echo $html;
echo "<script> window.close(); </script>";

printed('trn_sales',$masterid);
?>

โš™๏ธ What This Code Does

Section Purpose
Session Start Starts ERP session
Invoice Query Fetches sales invoice data
GST Logic Calculates CGST/SGST/IGST
EWayBill Object Creates E-Way bill instance
Item Loop Adds invoice items
GovtCopy() Generates printable copy
HTML File Stores printable HTML
Print Window Opens print dialog
printed() Marks invoice as printed

๐Ÿงพ GST Logic

Intrastate

When company state and party state are same:

  • CGST Applied
  • SGST Applied
  • IGST = 0

Interstate

When states are different:

  • IGST Applied
  • CGST = 0
  • SGST = 0

๐Ÿ“ฆ Item Quantity Logic

Unit Converted To
PCS PCS
MTR SQM
KG KGC

โš ๏ธ Important Note

The following line resets quantity:

$qty = 0;

This may cause incorrect E-Way quantities.

Recommended:

// Remove qty reset

โœ… Output Generated

The script generates:

  • Printable Government E-Way Bill
  • GST Summary
  • Transport Details
  • Item Details
  • Browser Print Window
  • HTML Log File

๐Ÿญ Used In

This module in Merciglobal Cloud ERP is commonly used for:

  • Textile Industry
  • Yarn Trading
  • Fabric Manufacturing
  • Dispatch Management
  • GST Compliance
  • Logistics Automation

๐Ÿ“Œ Conclusion

This E-Way Bill module automates GST-compliant transport documentation inside Merciglobal Cloud ERP using PHP-based invoice processing and dynamic E-Way generation.