0

I designed an invoice system that add rows automatically and computes the results, print it in another file that can be downloaded by other users.can i get help on how to multiply the price and quantity to get amount?

this is my code:

<SCRIPT LANGUAGE="JavaScript">
function showamt() {

//var unitprice = document.form1.unitprice[].value;
//var qty = document.form1.qty[].value;
//amount = unitprice*qty;
//document.form1.amount[].value = amount;

var a= document.getElementById('unitprice[this]');
var b= document.getElementById('qty[this]');
var c = Math.ceil(a.value*b.value);
alert(b);
document.getElementById('amount[this]').value =c;
}

</script>
    <SCRIPT language="javascript">
        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var colCount = table.rows[0].cells.length;

            for(var i=0; i<colCount; i++) {

                var newcell = row.insertCell(i);

                newcell.innerHTML = table.rows[0].cells[i].innerHTML;
                //alert(newcell.childNodes);
                switch(newcell.childNodes[0].type) {
                    case "text":
                            newcell.childNodes[0].value = "";
                            break;
                    case "checkbox":
                            newcell.childNodes[0].checked = false;
                            break;
                    case "select-one":
                            newcell.childNodes[0].selectedIndex = 0;
                            break;
                }

                switch(newcell.childNodes[0].name) {
                    case "s_no[]":
                            newcell.childNodes[0].value = rowCount + 1; 
                            break;
                }
            }
        }

        function deleteRow(tableID) {
              var tbl = document.getElementById(tableID);
              var lastRow = tbl.rows.length;
              if (lastRow > 2) tbl.deleteRow(lastRow - 1);

        }

    </SCRIPT>

          <form id="myTable" name="form1" method="post" action="">
            <table width="444" height="268" border="0" cellpadding="0" cellspacing="2">
              <tr>
                <td><?php 

                if ($_POST['submit']){
$result = mysql_query("SELECT MAX(id) as id FROM invoices");
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    $last_id = $row['id'];

} 
$userid = $last_id + 1;


//$datecreated = date ('m-d-Y');    
$compname = $_POST['compname'];
$compid = $_POST['compid'];
$amount = $_POST['amount'];
$amountpaid = $_POST['amountpaid'];
$quantity = $_POST['qty'];
$address = $_POST['address'];
$unitprice = $_POST['unitprice'];
$transid = "INV00".$userid;
$balance = $_POST['balance'];
$initiator = $_POST['initiator'];
$descript = $_POST['descript'];
$location  = $_POST['location'];
$product = $_POST['product'];

$edate  = $_POST['date'];

//$edate =substr($edate,10);
$edate = new DateTime($edate);
$edate= date_format($edate, 'Y-m-d');

//$id = $_GET['id'];

$unitprice = $_POST['unitprice'];
$amount = $_POST['amount'];
$qty = $_POST['qty'];
$s_no = $_POST['s_no'];
$product = $_POST['product'];
$location  = $_POST['location'];
$compname = $_POST['compname'];

    $date_array = explode("-",$edate); // split the array

        $var_year = $date_array[0];
        $var_month = $date_array[1];
        $var_day = $date_array[2];
     $firstdate = $var_year ."-". $var_month ."-". $var_day;
     $displaydate = mktime(12,0,0,$var_month,$var_day,$var_year);
    $displaydate = date("l, jS F Y",$displaydate);

//$displaydate = date("l, jS F Y",$edate);

// START THE CREATION OF INVOICE FOR DOWNLOAD

$data .=   "<style type=text/css>
<!--
body,td,th {
    font-family: Tahoma, Geneva, sans-serif;
    font-weight: bold;
}
.look {
    font-size: 12px;
}
.top {
    font-size: 24px;
    font-weight: bold;
    color: #C63;
}
.side {
    color: #C60;
    font-size:12px;
}
.down {
    font-weight: normal;
    font-size: 12px;
}
.addr {
    font-size: 12px;
    font-weight: normal;
    text-align: left;
}
.confirm {
    font-size: 14px;
    font-weight: normal;
}
-->
</style>";


$data.= "<table width='800' border='0' cellspacing='0' cellpadding='0'>
  <tr>
    <td height='58'><table width='800' border='0' cellspacing='0' cellpadding='0'>
      <tr>
        <td width='98' height='83' rowspan='3' align='center'><img src='file:///c:\Program Files\logo.png' width='82' height='72' /></td>
        <td width='702' align='left'><p class='top'>&nbsp;&nbsp;&nbsp;&nbsp;OLITE MANUFACTURING COMPANY LIMITED</p></td>
      </tr>
      <tr>
        <td align='left' valign='top'><span class='side'>&nbsp;&nbsp;&nbsp;&nbsp;Makers of Hair And Body Care Cosmetics, House Hold Chemicals, Laundary Chemicals &amp; Soap</span></td>
      </tr>
      <tr>
        <td align='left' valign='top'><table align='left' cellpadding='0' cellspacing='0' class='topleft'>
          <tr>
            <td width='564' valign='top' class='addr'>No 1 Nwabueze Street, via Caretimes/Olite Road Off Achalla Ibusa Road(off Benin-Onitsha Exp. Way) Asaba, Delta State<br />
              <br/></td>
          </tr>
          <tr>
            <td valign='top' class='addr'>P.O Box 2125 Asaba, Nigeria </td>
          </tr>
          <tr>
            <td valign='top' class='addr'>Phone: 056-2826625, 08033870990  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class='confirm'><span class='down'>Email</span></span>:info@oliteng.com</td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height='36' align='center' valign='middle'>CASH/CREDIT SALES INVOICE</td>
  </tr>
  <tr>
    <td height='30' align='left'  valign='middle'>CUSTOMER'S NAME: <span class='confirm'>$compname </span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BRANCH: <span class='confirm'>$location</span></td>
  </tr>
  <tr>
        <td height='36' align='left' valign='middle'>CUSTOMER'S ADDRESS: <span class='confirm'>$address </span></td>  
  </tr>  
  <tr>
    <td height='78'><table width='800' border='0' cellspacing='3' cellpadding='0'>
      <tr>
        <td width='14'>&nbsp;</td>
        <td width='174' class='side'><strong>INVOICE NUMBER</strong></td>
        <td width='600'><span class='confirm'>$transid</span></td>
      </tr>
      <tr>
        <td rowspan='2'>&nbsp;</td>
        <td class='side'><strong>INVOICE DATE</strong></td>
        <td><span class='confirm'>$displaydate</span></td>
      </tr>
      <tr>
        <td height='46' colspan='2' class='side'><p>&nbsp;</p><table width='800' border='0' cellspacing='0' cellpadding='0'>
          <tr>
            <td width='22' height='23'>&nbsp;</td>
            <td width='778'><table width='618' border='0'>
              <tr class='title'>                <td width='44' align=center>S/No</td>               
                <td width='193' align=center>Product</td>
                <td width='119' align=center>Quantity</td>
                <td width='110' align=center>Unit Price</td>
                <td width='110' align=center>Amount(=N=)</td>
</tr>
              </table>";       foreach($amount as $a => $b){
              $data .=  "<table id=dataTable class='confirm' width=618 border=0>
                <tr>
                  <td width=44 align=center>$s_no[$a]</td>
                  <td width=193 align=center>$product[$a]</td>
                  <td width=119 align=center>$qty[$a]</td>
                  <td width=110 align=center>$unitprice[$a]</td>
                  <td width=110 align=center>$amount[$a]</td>
</tr>
                </table>";
                 $dataproduct .= $product[$a]. '<br/>';
                 $dataqty .= $qty[$a]. '<br/>';
                 $dataunitpri .= $unitprice[$a]. '<br/>';
                 $dataamount .= $amount[$a]. '<br/>';

              }
            $total = array_sum($amount);
             //initialize
//$totalfig=$myConverter->convert($total); //pass the number you want to convert
 //number_word($totalfig, "Naira", "Kobo");
 $totalfig = convert_number($total); 

    $data .="</td>
            </tr>
          </table></td>
      </tr>
      <tr>
        <td height='67' rowspan='2'><p>&nbsp;<p></td>
        <td class='side'><strong>AMOUNT IN WORDS</strong></td>
        <td><span class='confirm'>$totalfig Naira only</span></td>
      </tr>
      <tr>
        <td class='side'><strong>AMOUNT IN FIGURES</strong></td>
        <td><span class='confirm'>";  //$total = array_sum($amount);

 $data .="=N=   $total

  </span></td>
      </tr>
    </table></td>
  </tr>
<tr>
    <td height=79><p class=confirm>Kindly confirm the amount written on this invoice before leaving the Cash Point. Thank You.</p>
    <p>&nbsp;</p>
    <p>_________________________ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    _________________________<br/>
      <span class=down>Signed For: Management</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=down>Customer's Signature</span></p></td>
  </tr>

  </table>";

                 $dataproduct .= $product[$a]. '<br/>';
                 $dataqty .= $qty[$a]. '<br/>';
                 $dataunitpri .= $unitprice[$a]. '<br/>';
                 $dataamount .= $amount[$a]. '<br/>';

$sql = mysql_query("INSERT INTO invoices(compname,custaddress,transid,amount,unitprice,quantity,goodsbought,descript,initiator,branch,amountpaid,balance,datecreated) VALUES ('$compname','$address','$transid','$dataamount','$dataunitpri','$dataqty','$dataproduct','$descript','$initiator','$location','$amountpaid','$balance','$edate')");

if ($sql){
    echo "<span class=error>Invoice Uploaded Successfully.</span>";
//$sql = mysql_query("INSERT INTO payments(customer,amountbought,amountpaid,branch,datepaid) VALUES ('$compname','$total','$amountpaid','$branch','$edate')");

        $sql1 = mysql_query("INSERT INTO logs (staffname,info,date) VALUES ('$adminname', 'Staff has added invoice record','$curTime')");

}


//foreach($amount as $a => $b)
//  echo "$amount[$a]  -  $unitprice[$a]  -  $qty[$a] <br />";

$invoice = substr($transid, 0, strlen($transid)-4);  

$ourFileName = "../../files/formatted_".$transid.".html";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
//echo $data = htmlspecialchars_decode($data);
fwrite($ourFileHandle, $data);

fclose($ourFileHandle);


                }
?></td>
              </tr>
              <tr>
                <td height="5"><table width="629" border="0">
                  <tr>
                    <td width="247"><strong>Initiator:</strong>
                      <input name="initiator" type="text" id="initiator" size="35" /></td>
                    <td width="29">&nbsp;</td>
                    <td width="339" valign="middle"><strong>Date:</strong>
                      <input id="date" name="date" type="text" size="35" />
                      <a href="javascript: NewCssCal('date','yyyymmdd','dropdown',true,12)"><img
                                                        src="../../images/cal.gif" width="16" height="16" alt="Pick a date" border="0" /></a></td>
                  </tr>
                  <tr>
                    <td height="20" valign="top"><strong>Branch:</strong>
                      <select name="location" onchange="showUser(this.value)">
                        <option>Select A Branch</option>
                        <option value="Benin">Benin</option>
                        <option value="Warri">Warri </option>
                        <option value="Kaduna">Kaduna</option>
                        <option value="Port Harcourt">Port Harcourt </option>
                        <option value="Abuja">Abuja </option>
                        <option value="Asaba">Asaba/Onitsha</option>
                        <option value="Lagos">Lagos </option>
                        <option value="Sapele">Sapele </option>
                        <option value="Jos">Jos </option>
                        <option value="Aba">Aba </option>
                        <option value="Calabar">Calabar </option>
                        <option value="Makurdi">Makurdi </option>
                      </select></td>
                    <td>&nbsp;</td>
                    <td><?php if ($custgot='1'){ ?>
                      <div id="txtHint"><b>Customer Name:  Please Select a Branch First.</b></div>
                      <?php }else{ ?>
                      Customer Name:
                      <select name="compname" id="compname" >
                        <option selected="selected">-- Select Customer --</option>
                        <?php $sql1=mysql_query("SELECT * FROM customer");
        while($fet=mysql_fetch_array($sql1)){
        $compname=$fet['compname'];
        //$uname=$fet['uname'];
        echo "<option value='$compname'> $compname </option>";

        }

                    }?>
                      </select></td>
                  </tr>
                  <tr>
                    <td height="12">&nbsp;</td>
                    <td rowspan="2">&nbsp;</td>
                    <td rowspan="2" valign="top"><table width="335" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td width="90" valign="top"><strong>Customer Address:</strong></td>
                        <td width="245"><textarea name="address" cols="34" rows="5" id="address"></textarea></td>
                      </tr>
                    </table></td>
                  </tr>
                  <tr>
                    <td height="12">&nbsp;</td>
                  </tr>
                </table>
                  <br/>
                  <table width="628" border="0">
                    <tr class="title">
                      <td width="37">S/No</td>
                      <td width="244">Product</td>
                      <td width="97">Quantity</td>
                      <td width="77">Unit Price</td>
                      <td width="151" align="center">Amount(=N=)</td>
                    </tr>
                  </table>
                  <table id="dataTable" width="597" border="0">
                    <tr>
                      <td width="20"><input name="s_no[]" readonly="readonly"  type="text" value="1" size="2"/></td>
                      <td width="114"><select name="product[]" id="product[]">
                        <option selected="selected">-- Select Product --</option>
                        <?php $sql1=mysql_query("SELECT * FROM products");
        while($fet=mysql_fetch_array($sql1)){
        $productname=$fet['productname'];
        //$uname=$fet['uname'];
        echo "<option value='$productname'> $productname </option>";

        }?>
                      </select></td>
                      <td width="144"><input name="qty[]" type="text" size="10"/></td>
                      <td width="144"><input  name="unitprice[]" type="text" onmouseout="javascript:showamt();" size="10"/></td>
                      <td width="153"><input name="amount[]" type="text" onclick="showContent()" size="15"/></td>
                    </tr>
                  </table>
                  <input type="button" value="Add Row" onclick="addRow('dataTable')" />
                  <input type="button" value="Delete Row" onclick="deleteRow('dataTable')" /></td>
              </tr>
              <tr>
                <td height="37" align="left"><br/>
                  <span class="title">TOTAL AMOUNT</span>:&nbsp;&nbsp;&nbsp;=N=
                  <?php $total = array_sum($amount);
   print $total;?>
                  <br/></td>
              </tr>
              <tr>
                <td height="24"><input type="submit" name="submit" value="Submit" />
                  <input type="reset" name="Submit2" value="Cancel" /></td>
              </tr>
            </table>
            <p>&nbsp;</p>
          </form>

Edited by Dani: Formatting fixed

4
Contributors
9
Replies
14
Views
6 Years
Discussion Span
Last Post by utpal23
0

Please use code tags for showing your code, so others can easily go through it.
Please point for a peice of required code.
You have already coded it, then please come with your current output that your code has given..

0

Thanks for the answer. this is the code i need changes on. Thanks for anticipated reply

<SCRIPT LANGUAGE="JavaScript">
function showamt() {

//var unitprice = document.form1.unitprice[].value;
//var qty = document.form1.qty[].value;
//amount = unitprice*qty;
//document.form1.amount[].value = amount;

var a= document.getElementById('unitprice[this]');
var b= document.getElementById('qty[this]');
var c = Math.ceil(a.value*b.value);
alert(b);
document.getElementById('amount[this]').value =c;
}

</script>
<SCRIPT language="javascript">
function addRow(tableID) {

var table = document.getElementById(tableID);

var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var colCount = table.rows[0].cells.length;

for(var i=0; i<colCount; i++) {

var newcell = row.insertCell(i);

newcell.innerHTML = table.rows[0].cells.innerHTML;
//alert(newcell.childNodes);
switch(newcell.childNodes[0].type) {
case "text":
newcell.childNodes[0].value = "";
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
newcell.childNodes[0].selectedIndex = 0;
break;
}

switch(newcell.childNodes[0].name) {
case "s_no[]":
newcell.childNodes[0].value = rowCount + 1;
break;
}
}
}

function deleteRow(tableID) {
var tbl = document.getElementById(tableID);
var lastRow = tbl.rows.length;
if (lastRow > 2) tbl.deleteRow(lastRow - 1);

}

</SCRIPT>

0

First off, you have no 'ids' on you page.
var a= document.getElementById('unitprice[this]');
var b= document.getElementById('qty[this]');

you need to add id elements to your input tags or switch the 'ElementById' call to be getElementByTagName('name') like:
<input name='unitprice' id='unitprice' value=''>
also, I find it very strange that everything is named with []?
Like they should all be arrays, this shouldn't apply in most cases.
<td width="144"><input name="qty[]" type="text" size="10"/></td>
<td width="144"><input name="unitprice[]" type="text" onmouseout="javascript:showamt();" size="10"/></td>
<td width="153"><input name="amount[]" type="text" onclick="showContent()" size="15"/></td>
these are some of the lines that would need an 'id=' added to them.
The actual code you have should work, you just aren't getting the elements because of the lack of ids.

0

some of it does, at least the part you asked for:
lines 379 - 381 need to add id= to each line.

<td width="144"><input id="qty[]" name="qty[]" type="text" size="10"/></td>
<td width="144"><input id="unitprice[]" name="unitprice[]" type="text" onmouseout="javascript:showamt();" size="10"/></td>
<td width="153"><input id="amount[]" name="amount[]" type="text" onclick="showContent()" size="15"/></td>

also, your showamt code is wrong:

function showamt() {
	var a= document.getElementById('unitprice[]');
	var b= document.getElementById('qty[]');
	var c = Math.ceil(a.value*b.value);
	alert(b);
	document.getElementById('amount[]').value =c;
}

get rid of all of the [this] that you have in the js call.
I added the id= , and removed the 'this' from your showamt call.
It worked fine for me.

0

Thanks a lot, it seems to work for a line but i need it to work for different rows in my table. How can i get it done. i really appreciate your help so far

0

can i send the full page code to you. it has the first post i made. it also contains the code that generates the table and rows.Thanks for your help.

Edited by Nollyvenon: n/a

0

I have also worked this type of project . Very simple logic I have used. May be it will help you Brother.......

<script language="javascript" type="text/javascript">
function lastPrice(qID,sID,upID,tpID){
	
var quantity = document.getElementById(qID).value;
var stich = document.getElementById(sID).value;	
var unitPrice = document.getElementById(upID).value;


		var totalPrice=quantity*unitPrice;
		document.getElementById(tpID).value=Math.round(totalPrice);
		return true;
	

}
</script>

<tr>
				<td>1</td>
				<td><div align="center">
				  <input type="text" maxlength="30"  name="p1" value="" class=""/>
				  </div></td>
				<td><div align="center">
				  <input type="text" maxlength="30"  name="q1" value="" class="integer" id="q1" 
					onkeyup="lastPrice('q1','s1','up1','tp1');"/>
				  </div></td>
				<td><div align="center">
				  <input type="text" maxlength="30"  name="s1" value="" class="integer" id="s1" 
				onkeyup="lastPrice('q1','s1','up1','tp1');" />
				  </div></td>
				<td><div align="center">
				  <input type="text" maxlength="30"  name="up1" value="" class="numeric" 
				    onkeyup="lastPrice('q1','s1','up1','tp1');" id="up1"/>
				  </div></td>
				<td><div align="center">
				  <input type="text" maxlength="30"  name="tp1" value="" class="" id="tp1" readonly="1"/>
				  </div></td>
		   </tr>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.