<?php

//$page_security = $_POST['PARAM_0'] == $_POST['PARAM_1'] ?    'SA_SALESTRANSVIEW' : 'SA_SALESBULKREP_PI';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Print Invoices
// ----------------------------------------------------------------
global $path_to_root;
//$path_to_root="../../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
//add_access_extensions();

//----------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------

function print_invoices($invoice_id = 0)
{
    $html="
    <br>   
<h4 >Terms and Conditions<h4>
    <ol>
	
    <li style=\"text-align: justify;\"><span style=\"font-size:10px\">BUYER decides and advices the SELLER the choice or mode of Despatch. The packing, special markings and freight amounts will be charged at actuals. All emergency and Air mode despatch expenses will be borne by BUYER only
.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">The risk of loss or damages to the products in this invoice shall be on BUYER or CARRIER, whichever first occurs. SELLERS risk & responsibility ceases once the goods leave their premises..</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">It shall be the absolute responsibility of the BUYER to insure the goods with any insurance company of their choice, in order to secure the transit damages. And if the BUYER wants to insure the same through SELLER, then the BUYER shall inform & pay the necessary insurance premium charges in advance to the SELLER separately. Otherwise SELLER shall not be liable for any loss what-so-ever that caused during the transit.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">Transit delays caused by natural calamities, act of God, act of terrorism, carrier accidents, burning, multiple loadings & un-loadings, delivered at wrong location etc., do not attract any deductions in the invoiced amount what-so-ever.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">Transit claims, of any sort, will be lodged within 2 days by the BUYER from the date of receipt of the Materials with regard to short received or received not as ordered, damages etc. are subject to settlement by discussion between both the parties, but not meant for any deductions from the invoiced amount. But otherwise the SELLER will not entertain any claim, even if the amount is paid in full in the form of advance.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">All goods supplied by SELLER through this invoice are done so against confirmed purchase order, drawings and spec, approved rates, & agreed terms only. Hence goods once sold will not be taken back or exchanged.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">All product purchases incorporating variations from our standard catalogue are considered customised & special and are not subject to cancellation without prior acceptance by SELLER or all the developmental costs will be debited to the BUYER and recovered in full.</span></li>
<!--	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">All tax forms (Form 'D' or Form 'C') are due on demand, failing which, sales tax at full rate will be charged.</span></li> -->
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">Final amount due under this invoice is recoverable in full without any deductions for whatever reasons within the stipulated credit period. Failing which, the SELLER reserves the right to charge interest of 24% per annum for all the unpaid balance in this Invoice or accrued outstanding balance amounts.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">All payments are to be made as per the details given below in advance or before despatch from our premises by DD / RTGS / NEFT / Paytm / Google Pay etc.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\"><b>Beneficiary account: M/s.Geesys Technologies India Pvt. Ltd., (SELLER)</b></span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\"><b> Account Number     : Ac/No.054411100002573,</b></span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\"><b> Bank Name               : Andhra Bank,</b></span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\"><b> Branch Name           : Saidapet,</b></span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\"><b> IFSC Code                : ANDB0000544.</b></span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">Any PDCs will be accepted strictly against written acceptance on a promissory note well in advance of the actual despatch. The dishonouring of any of the PDC leads to a penalty of Rs.2,500/- per cheque. The settlement of such dishonoured amounts has to be settled immediately by RTGS/NEFT/Paytm/Google Pay etc only.  Even one such dishonour will lead to ceasing of PDC facility.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">Improper installation of equipment by third party, inexperience technician, not as advised by SELLER, not done as per approved drawing, wrong application and not connected to  suitable equipment will make our warranty NULL and VOID. The warranty also SPECIFICALLY EXCLUDES consequential damages. </span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">Unless agreed by SELLER in writing, no modification what-so-ever to the supplied material under this Invoice is permitted. If violated, for reasons beyond BUYERs control, the warranty will become null and void.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">For faster attention and resolution, please mark all your queries and correspondence to geesyscare@geesysindia.com only.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">The entire understanding between the parties hereto is set forth herein and any promises, representations, warranties or guarantees not herein contained shall have no force and effect unless in writing signed by both the parties.</span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">If any part hereof is contrary to, prohibited by, or deemed invalid under applicable laws or regulations, such provision shall be deemed inapplicable and omitted to the extent contrary, prohibited or invalid, but the remainder shall not be less invalid and shall be given effect so far as possible. </span></li>
	<li style=\"text-align: justify;\"><span style=\"font-size:10px\">All disputes with regard to supplies made under this Invoice, are subject to CHENNAI jurisdiction only.</span></li>
    
</ol>


";
    global $path_to_root, $alternative_tax_include_on_docs, $suppress_tax_rates;

    
    $show_this_payment = true; // include payments invoiced here in summary

    include_once($path_to_root . "/modules/KvcodesCRM/reports/pdf_report.inc");

    if($invoice_id > 0 || (isset($_POST['trans_no']) && $_POST['trans_no'] !='')){

        if(isset($_POST['trans_no']))
            $invoice_id = $_POST['trans_no'];
        $from = $to = $invoice_id;
        $email = 1;
        $currency = $pay_service = $comments = $customer = $orientation = '';
        //display_error($from);
        $_POST['REP_ID'] = '107';   

    
    $dec = user_price_dec();

    $fno = explode("-", $from);
    $tno = explode("-", $to);
    $cur = get_company_Pref('curr_default');
    $state_code = get_company_Pref('state_code');
    $state_flag=0;
    for ($i = $fno[0]; $i <= $tno[0]; $i++)
    {
        $myrow = get_customer_trans($i, ST_SALESINVOICE);
        $branch = get_branch($myrow["branch_code"]);
        $scode=get_state_code($branch['debtor_no']);

        if($scode['state_code']==$state_code)
        {
            $state_flag = 1;

        }
    }

//	$cols = array(4, 80, 225, 300, 325, 385, 450, 515);

    // $headers in doctext.inc
//    $cols2="";
//    $align2="";
//    $headers2=array();
//    $cols=array();
//    $align=array();
//    $headers=array();
    if($state_flag==1) {
        $cols2 = array(368, 395, 425, 460, 490);
        $aligns2 = array('center', 'center', 'center', 'center');

        $headers2 = array(_('Rate'), _('Amount'), _('Rate'), _('Amount'));
//
//    $aligns2 = array('left', 'left', 'left', 'left', 'left');
//    $cols = array(0, 50, 150, 210, 270,	330, 390, 450, 510,	570);
        //------------0--1---2----3----4----5----6----7----8--
//    $cols = array(0, 50, 150, 190,220, 270,300,340,380,440,500,570);

        $headers = array(_('Item Code'), _('Item Description'), _('Qty'), _('Credit'), _('Debit'),
            _('Credit'), _('Debit'), _('Credit'));
        $cols = array(0, 50, 150, 190, 225, 260, 300, 360, 440, 490, 570);

        $aligns = array('left', 'left', 'center', 'center', 'center', 'center', 'center', 'center', 'center', 'center');

        $params = array('comments' => $comments);
    }else
    {
        $cols2 = array(425, 460, 490);
        $aligns2 = array('center', 'center', 'center', 'center');

        $headers2 = array(_('Rate'), _('Amount'));
//
//    $aligns2 = array('left', 'left', 'left', 'left', 'left');
//    $cols = array(0, 50, 150, 210, 270,	330, 390, 450, 510,	570);
        //------------0--1---2----3----4----5----6----7----8--
//    $cols = array(0, 50, 150, 190,220, 270,300,340,380,440,500,570);

        $headers = array(_('Item Code'), _('Item Description'), _('Qty'), _('Credit'), _('Debit'),
            _('Credit'), _('Debit'), _('Credit'));
        $cols = array(0, 50, 190, 225, 260, 300, 360,440, 490, 570);

        $aligns = array('left', 'left', 'center', 'center', 'center', 'center', 'center', 'center', 'center', 'center');

        $params = array('comments' => $comments);
    }



    if ($email == 0)
    {

        $rep = new FrontReport(_('INVOICE'), "InvoiceBulk", user_pagesize());
        $rep->SetHeaderType('Header2');
        $rep->currency = $cur;
        $rep->Font();
//		$rep->Info($params, $cols, null, $aligns);
        $rep->Info($params, $cols, $headers, $aligns,$cols2,$headers2,$aligns2);
    }

    for ($i = $fno[0]; $i <= $tno[0]; $i++)
    {
        if (!exists_customer_trans(ST_SALESINVOICE, $i))
            continue;
        $sign = 1;
        $myrow = get_customer_trans($i, ST_SALESINVOICE);
        $baccount = get_default_bank_account($myrow['curr_code']);
        $params['bankaccount'] = $baccount['id'];

        $branch = get_branch($myrow["branch_code"]);
        $branch['disable_branch'] = (isset($paylink) ? $paylink : 0 ); // helper
        $sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
        if ($email == 1)
        {
            $rep = new FrontReport("", "", user_pagesize());
            $rep->SetHeaderType('Header2');
            $rep->currency = $cur;
            $rep->Font();
            $rep->title = _('INVOICE');
            $rep->filename = "Invoice" . $myrow['reference'] . ".pdf";
            $rep->Info($params, $cols, null, $aligns);
        }
        else
            $rep->title = _('INVOICE');
        $contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no']);

        $scode=get_state_code($branch['debtor_no']);
        if($scode['state_code']==$state_code)
            $rep->SetCommonData($myrow, $branch, $sales_order, $baccount, ST_SALESINVOICE, $contacts,1);
        else
            $rep->SetCommonData($myrow, $branch, $sales_order, $baccount, ST_SALESINVOICE, $contacts,0);
        $rep->NewPage();

        $result = get_customer_trans_details(ST_SALESINVOICE, $i);

        $SubTotal = 0;


        $tot_tax=0.0;
        //display_error(json_encode($myrow).'-'.$i);
        while ($myrow2=db_fetch($result))      {

            if ($myrow2["quantity"] == 0)
                continue;


            $Net = round2($sign * ((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]),
                user_price_dec());
            $SubTotal += $Net;
            $DisplayPrice = number_format2($myrow2["unit_price"],$dec);
            $DisplayQty = number_format2($sign*$myrow2["quantity"],get_qty_dec($myrow2['stock_id']));
//            $unittax=number_format2($myrow2['unittax'],$dec);
            $tot_tax+=$myrow2['unittax'];
            if($state_flag==1)
                $percen=round(($myrow2["unittax"]/$myrow2["unit_price"])*100*.5,2);
            else
                $percen=round(($myrow2["unittax"]/$myrow2["unit_price"])*100,2);


            $DisplayNet = number_format2($Net,$dec);
            if ($myrow2["discount_percent"]==0)
                $DisplayDiscount ="";
            else
                $DisplayDiscount = number_format2($myrow2["discount_percent"]*100,user_percent_dec()) . "%";

            if($state_flag==1) {
                $tax = number_format2($myrow2['unittax'] * .5 * $myrow2["quantity"]);
                $rep->TextCol(0, 1, $myrow2['stock_id'], -2);
                $oldrow = $rep->row;
                $rep->NewLine();
                $rep->TextCol(0, 1, "(" . $myrow2['itemid'] . ")", -2);
                $rep->row = $oldrow;
                $oldrow = $rep->row;
                $rep->TextColLines(1, 2, $myrow2['StockDescription'], -2);
                $newrow = $rep->row;
                $rep->row = $oldrow;
                $rep->TextCol(2, 3, $myrow2['hsnno'], -2);
                $rep->TextCol(3, 4, $myrow2['units'], -2);
                $rep->TextCol(4, 5, $DisplayQty, -2);
                $rep->TextCol(5, 6, $DisplayPrice, -2);
                $rep->TextCol2(0, 1, $percen . "%", -2);
                $rep->TextCol2(1, 2, $tax, -2);
                $rep->TextCol2(2, 3, $percen . "%", -2);
                $rep->TextCol2(3, 4, $tax, -2);
                $rep->TextCol(9, 10, ($myrow2['FullUnitPrice'] * $myrow2["quantity"]), -2);
                $rep->TextCol(6, 7, $DisplayNet, -2);
                $rep->row = $newrow;
                $rep->NewLine(1);
            }else
            {
                $tax = number_format2($myrow2['unittax'] *  $myrow2["quantity"]);
                $rep->TextCol(0, 1, $myrow2['stock_id'], -2);
                $oldrow = $rep->row;
                $rep->NewLine();
                $rep->TextCol(0, 1, "(" . $myrow2['itemid'] . ")", -2);
                $rep->row = $oldrow;
                $oldrow = $rep->row;
                $rep->TextColLines(1, 2, $myrow2['StockDescription'], -2);
                $newrow = $rep->row;
                $rep->row = $oldrow;
                $rep->TextCol(2, 3, $myrow2['hsnno'], -2);
                $rep->TextCol(3, 4, $myrow2['units'], -2);
                $rep->TextCol(4, 5, $DisplayQty, -2);
                $rep->TextCol(5, 6, $DisplayPrice, -2);
                $rep->TextCol2(0, 1, $percen . "%", -2);
                $rep->TextCol2(1, 2, $tax, -2);
//            $rep->TextCol2(2, 3, $percen . "%", -2);
//            $rep->TextCol2(3, 4, $tax, -2);
                $rep->TextCol(8, 9, ($myrow2['FullUnitPrice'] * $myrow2["quantity"]), -2);
                $rep->TextCol(6, 7, $DisplayNet, -2);
                $rep->row = $newrow;
                $rep->NewLine(1);
            }
            if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight)) {
                $rep->row=180;
                $rep->Font('bold');
                $rep->TextCol(1, 2, "[ Continue .... ]", -2);
                $rep->NewPage();
            }
        }

        $comments = get_comments(ST_SALESINVOICE, $i);
        if ($comments && db_num_rows($comments))
        {
            $rep->NewLine();
            //while ($comment=db_fetch($comments))
            //	$rep->TextColLines(0, 3, $comment['memo_'], -2);
            //$rep->TextWrapLines(0, 3, $comment['memo_'], -2);
        }

        $DisplaySubTot = number_format2($SubTotal,$dec);
        $DisplayFreight = number_format2($sign*$myrow["ov_freight"],$dec);

        $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
        $linetype = true;
        $doctype = ST_SALESINVOICE;
        include($path_to_root . "/reporting/includes/doctext.inc");
//        $myrep=$rep->row;
//        $rep->row=$rep->row-77;
//        $myrep_1=$rep->row-77;

        $temp=$rep->row;
        $rep->row=$rep->row-78;
        $rep->Font('bold');
        $rep->TextCol(6, 7,	$DisplaySubTot, -2);
        $rep->row=$temp;
//        $rep->TextCol2(2, 3,$tot_tax, -2);


//        $rep->TextCol(3, 6, $doc_Sub_total, -2);
//        $rep->TextCol(6, 7,	$DisplaySubTot, -2);
//        $rep->NewLine();
        $temp=$rep->row;
        $rep->row=$rep->row-20;
//        $rep->Font('bold');
        $rep->TextCol(1, 2, $doc_Shipping, -2);

        if($state_flag==1)
            $rep->TextCol(9, 10,	$DisplayFreight, -2);
        else
            $rep->TextCol(8, 9,	$DisplayFreight, -2);

        $rep->NewLine();
//        $rep->TextCol(1, 2, "Packaging & Forwarding ", -2);
//        $rep->TextCol(9, 10,	$DisplayFreight, -2);
//        $rep->NewLine();
//        $rep->TextCol(1, 2, "Insurance ", -2);
//        $rep->TextCol(9, 10,	$DisplayFreight, -2);
        $rep->NewLine();
        $rep->row=$temp;
        $rep->NewLine();
//        $rep->Font('');
        $tax_items = get_trans_tax_details(ST_SALESINVOICE, $i);
        $first = true;
//        $this->rectangle($this->leftMargin, $iline2+50, $right - $this->leftMargin, $iline2 - $iline3, "F", null, array(255, 0, 0));
//        $rep->rectangle($rep->leftMargin, 180,558,50, "F", null, array(224, 224, 224));
//        $rep->line($rep->leftMargin, 0,650 ,-150, "F", null, array(255, 255, 0));
//        $rep->LineTo($rep->leftMargin, 150 ,$rep->leftMargin+570, 150);
//        $rep->LineTo($rep->leftMargin, 192 ,586, 192);
//        $rep->LineTo($rep->leftMargin, 150 ,$rep->leftMargin+570, 150);
//        $rep->LineTo($rep->leftMargin, 150 ,$rep->leftMargin+570, 150);
        $cgst_flag=0;
        $tmp_row=$rep->row;
        $rep->row=$rep->row-66;
//        $rep->Font('bold');
        while ($tax_item = db_fetch($tax_items))
        {
//            $rep->row=$rep->row+10;
            if ($tax_item['amount'] == 0)
                continue;
            $DisplayTax = number_format2($sign*$tax_item['amount'], $dec);

            if (isset($suppress_tax_rates) && $suppress_tax_rates == 1)
                $tax_type_name = $tax_item['tax_type_name'];
            else
                $tax_type_name = $tax_item['tax_type_name']." (".$tax_item['rate']."%) ";

            if ($tax_item['included_in_price'])
            {
                if (isset($alternative_tax_include_on_docs) && $alternative_tax_include_on_docs == 1)
                {
                    if ($first)
                    {  $rep->Font('bold');
                        $rep->TextCol(3, 6, _("Total Tax Excluded"), -2);
                        $rep->TextCol(6, 7,	number_format2($sign*$tax_item['net_amount'], $dec), -2);
                        $rep->NewLine();
                    }
                    $rep->TextCol(3, 6, $tax_type_name, -2);
                    $rep->TextCol(6, 7,	$DisplayTax, -2);
                    $first = false;
                }
                else
                    $rep->TextCol(3, 7, $doc_Included . " " . $tax_type_name . $doc_Amount . ": " . $DisplayTax, -2);
            }
            else
            {
//echo $tax_type_name;
//                echo $cgst_flag;
//                echo $state_flag;

                if($cgst_flag==0) {
                    if($state_flag==1) {
                        $rep->TextCol2(0, 1, split_percentage($tax_type_name), -2);
//                    $rep->row=$tmp_row;
                        $rep->TextCol2(1, 2, $DisplayTax, -2);
                        $cgst_flag=1;
                    }
                    else
                    {
                        $rep->TextCol2(0, 1, (split_percentage($tax_type_name)), -2);
                        $rep->TextCol2(1, 2, $DisplayTax, -2);
                        $rep->row = $rep->row + 13;
//                        echo "TESTing";
                    }

                }
                else
                {

//                        $rep->TextCol2(3, 6, $tax_type_name, -2);
                    if($state_flag==1) {
                        $rep->TextCol2(2, 3, split_percentage($tax_type_name), -2);
                        $rep->TextCol2(3, 4, $DisplayTax, -2);
                        $cgst_flag = 0;
                        $rep->row = $rep->row + 13;
                    }
//                    $rep->NewLine();
//                    $rep->row=+5;
                }
//                exit;

            }
//            $rep->NewLine();
        }
        $rep->row=$tmp_row-78;
        $rep->NewLine();
        $DisplayTotal = number_format2($sign*($myrow["ov_freight"] + $myrow["ov_gst"] +
                $myrow["ov_amount"]+$myrow["ov_freight_tax"]),$dec);
        $rep->Font('bold');
//        $rep->TextCol(3, 6, $doc_TOTAL_INVOICE, - 2);
        $temp=$rep->row;
        $rep->row=$rep->row+25;
        if($state_flag==1)

        {

            $rep->TextCol(9, 10, $DisplayTotal, -2);
            $rep->NewLine();
            $rep->NewLine();
            $rep->TextCol(9, 10, "(E. & O.E)", -2);


        }
        else {
            $rep->TextCol(8, 9, $DisplayTotal, -2);
            $rep->NewLine();
            $rep->NewLine();
            $rep->TextCol(8, 9, "(E. & O.E)", -2);
        }

        $words = price_in_words($myrow['Total'], ST_SALESINVOICE);
        if ($words != "")
        {
//            $rep->NewLine(1);
            $rep->TextCol(0, 7, $myrow['curr_code'] . ": " . $words, - 2);
        }
        $rep->row=$temp;

        $rep->Font();
        // if ($email == 1)
        // {
        //     $myrow['dimension_id'] = (isset($paylink) ? $paylink : 0 ); // helper for pmt link
        //     $rep->End($email, $doc_Invoice_no . " " . $myrow['reference'], $myrow, ST_SALESINVOICE);
        // }
    }
//	if ($email == 0)
//		$rep->End();
    $rep->Font();
    $rep->SetHeaderType('');
    //   $tt="test";
    $rep->SetMargins(15,80, 25);
    $rep->NewPage();



//$rep->SetMargins(10, PDF_MARGIN_TOP, 10);
//$rep->Text(2,$terms);
//$logo = company_path() . "/images/" . $this->company['coy_logo'];
    $logo=$path_to_root."/company/0/images/header1.jpg";
//
//
    $rep->AddImage($logo, 0,696, 0, 98);
    $rep->AddImage($path_to_root."/company/0/images/footer.jpg", 0, 10, 0, 70);
    $rep->NewPage();
    $rep->writeHTML($html, true, 100, true, 0);
    if ($email == 1)
        return $rep->End($email);
    }
}

?>
