Create Purchase Order


Purchase order is a document created by the company to purchase products from suppliers. This document includes items and quantities required. Some times indicate the purchase value for supplier reference. In this case the company and supplier can identify price changes and track orders according to the purchase order number.

Creating a purchase order involves three files.


  1. purchase_order.php (to enter items and other details)
  2. func/close_po.php (to save purchase order details in the database)
  3. func/print_po.php?pid=2 (to print the purchase order)
Printing of a purchase order shows in a pop-up window. Many new browsers blocks this function and it is good to remove the pop-up printing window and instead open it as a new window or tab.



Source code of purchase_order.php file:

<?php include("inc/page_header.php"); ?>
<?php
 if(check('Add Stocks') != 'Valid'){
  header("Location: sys_home.php?pre=error");
  ob_end_flush();
 }
 /* +++++++++++++++ Form 1 (Submit 1) Handling ++++++++++++++++++++++ */
  if(isset($_POST['submit_1'])){
   $item_code = $_POST['item_code'];
   $part_no = $_POST['part_no'];
   $item_name = $_POST['item_name'];
   if($item_code == '' && $part_no == '' && $item_name == ''){
    $error = 'Please enter Item Code OR Part No OR Item Name to start!';
   }else{
    if($item_code != ''){
     // search by Item Code
     $q = mysql_query("SELECT * FROM items WHERE Item_Code = '$item_code'");
    }else if($part_no != ''){
     // search by Part No
     $q = mysql_query("SELECT * FROM items WHERE Item_Part_No = '$part_no'");
    }else{
     // search by Item Name
     $q = mysql_query("SELECT * FROM items WHERE Item_Name = '$item_name'");
    }
    // check item found in the inventory
    if(mysql_num_rows($q) >0){
     // item found and set variables for item code, part no and item name
     $r = mysql_fetch_assoc($q);
     $itemcode = $r['Item_Code'];
     $partno = $r['Item_Part_No'];
     $itemname = $r['Item_Name'];
     // get the current stock details
     $q = mysql_query("SELECT SUM(Stock_Qty), Stock_Purchase_Price, Stock_Retail_Price FROM stocks WHERE Stock_Item = '$itemcode' ORDER BY Stock_ID DESC");
     $r = mysql_fetch_assoc($q);
     $current_stock = $r['SUM(Stock_Qty)'];
     $purchase_price = $r['Stock_Purchase_Price'];
     $retail_price = $r['Stock_Retail_Price'];
    }else{
     // item not found and create error message
     $error = 'Item not found in the inventory!';
    }
   }
  }
 /* +++++++++++++++ // Form 1 Handling ++++++++++++++++++++++++++++++ */
 /* +++++++++++++++  Form 2 Handling ++++++++++++++++++++++++++++++ */
  if(isset($_POST['submit_2'])){
   // variables for form 2 display values
   $itemname = $_POST['item_name'];
   $itemcode = $_POST['item_code'];
   $partno = $_POST['part_no'];
   $current_stock = $_POST['current_stock'];
   $purchase_price = $_POST['pur_price'];
   $item_code = $_POST['item_code']; // this is a duplicate but keeps it because 
             // it uses by other elements in the form
   $qty = $_POST['qty'];
   $part_no = $_POST['part_no'];
   // validate Qty. Purchase Price NULL, Empty or Zero values are accepting
   if($qty == '' || $qty == 0){
    $error = 'Please enter a valid Qty!';
   }else{
    // add item details to the temp_po_items table
    mysql_query("INSERT INTO temp_po_items (Temp_PO_Item, Temp_PO_Qty, Temp_PO_Price) VALUES ('$itemcode', '$qty', '$purchase_price')");
   }
  }
 /* +++++++++++++++ // Form 2 Handling ++++++++++++++++++++++++++++++ */
?>
<script type="text/javascript" src="ajax/show_items_js.js"></script>
<script type="text/javascript" src="js/scripts.js"></script>
 <div class="wrapper">

  <div class="s60">
  <div class="boxtitle">Purchase Order</div>
    <p>Please enter Item Code OR Part No OR Item Name.</p>
    <div style="border:2px solid #6484F0; width:90%; margin:10px auto 10px auto; padding:5px; ">
     <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
      <table>
       <tr><td>ID:</td><td><input type="text" size="8" name="item_code" /></td>
       <td>Item Code / Part No:</td><td><input type="text" size="21" name="part_no" /></td></tr>
       <tr><td>Item Name:</td><td colspan="3"><input id="item_name" type="text" size="45" name="item_name" onKeyUp="showItems(this.value)" onFocus="showItemList()" onBlur="hideItemList()" /> <input id="submit_1" type="submit" name="submit_1" class="btn" value="&raquo;" /><br />
        <div id="item_list" style="position:absolute; z-index:2; width:294px; background-color:#FFFFCC; border:1px solid #999999; overflow:scroll; min-height:1px; visibility:hidden; "></div>
       </td></tr>
      </table>
     </form>
    </div>
    <?php if(isset($_POST['submit_1']) || (isset($_POST['submit_2']) && isset($error))){ // control the visibility of form 2 ?>
    <div style="border:2px solid #6484F0; width:90%; margin:10px auto 10px auto; padding:5px; ">
     <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
      <table width="100%">
       <tr id="sh5"><td>&nbsp;<b>Item Name:</b></td><td colspan="3"><input type="text" size="40" value="<?php echo $itemname; ?>" disabled /></td></tr>
       <tr id="sh3"><td>&nbsp;<b>Item Code:</b></td><td>&nbsp;<b>Part No:</b></td><td>&nbsp;<b>Current Stock:</b></td><td>&nbsp;<b>Purchase Price Rs:</b></td></tr>
       <tr id="sh3"><td><input type="text" size="8" value="<?php echo $itemcode; ?>" disabled /></td>
        <td><input type="text" size="20" value="<?php echo $partno; ?>" disabled /></td>
        <td><input type="text" size="10" value="<?php echo $current_stock; ?>" disabled /></td>
        <td><input type="text" name="pur_price" size="10" value="<?php echo $purchase_price; ?>" /></td></tr>
       <tr id="sh5"><td>&nbsp;<b>Qty:</b></td><td><input type="text" name="qty" size="10" /> *</td>
           <td colspan="2"><input type="submit" name="submit_2" value="Add +" class="btn" /></td></tr>
      </table>
      <input type="hidden" name="item_code" value="<?php echo $itemcode; ?>" />
      <input type="hidden" name="part_no" value="<?php echo $partno; ?>" />
      <input type="hidden" name="item_name" value="<?php echo $itemname; ?>" />
      <input type="hidden" name="current_stock" value="<?php echo $current_stock; ?>" />
     </form>
    </div>
    <?php } // end of form 2 visibility control ?>
     <table width="98%" bgcolor="#CCCCFF">
      <tr id="headrow"><td>&nbsp;Part No</td><td>&nbsp;Item Name</td><td>&nbsp;Price Rs.</td><td>&nbsp;Qty</td><td>&nbsp;Total Rs.</td><td>&nbsp;</td></tr>
      <?php
       // display items in the temp_po_items table
       $q = mysql_query("SELECT Temp_PO_ID, Temp_PO_Item, Temp_PO_Qty, Temp_PO_Price, Item_Name, Item_Part_No FROM temp_po_items, items WHERE Temp_PO_Item = Item_Code ORDER BY Temp_PO_ID");
       $gross_value = 0;
       while($r = mysql_fetch_assoc($q)){
        $i_id = $r['Temp_PO_ID'];
        $i_code = $r['Temp_PO_Item'];
        $i_no = $r['Item_Part_No'];
        $i_name = $r['Item_Name'];
        $i_price = $r['Temp_PO_Price'];
        $i_qty = $r['Temp_PO_Qty'];
        $i_total = $i_price * $i_qty;
        echo "<tr id='sh0'>
           <td>{$i_no}</td>
           <td>{$i_name}</td><td><div align='right'>{$i_price}</div></td>
           <td><div align='right'>{$i_qty}</div></td>
           <td><div align='right'>".number_format($i_total,2,'.','')."</div></td>
           <td><div align='right'><a href='func/remove_po_item.php?itm={$i_id}'>Remove</a></div></td>
         </tr>";
        $gross_value += $i_total;
       }
      ?>
     </table>
  </div>
  <div class="s40">
   <?php
    if(isset($error)){ echo "<div class='errordiv'>{$error}</div>"; }
    if(isset($noerror)){ echo "<div class='noerrordiv'>{$noerror}</div>"; }
   ?>
     <!-- reserved for future upgrade to multiple supplier selection
     <table width="97%" bgcolor="#FFFF99">
     </table>
     -->
     <div align="center" style="margin-top:20px; ">
      <form method="post" action="func/close_po.php">
       <input type="hidden" name="po_value" value="<?php echo $gross_value; ?>" />
       <p><strong>Supplier:</strong></p>
       <select size="1" name="po_supplier">
        <option value="0" selected>None</option>
        <?php
         $q = mysql_query("SELECT * FROM suppliers");
         while($r = mysql_fetch_assoc($q)){
          $supplier_id = $r['Supplier_ID'];
          $supplier_name = $r['Supplier_Name'];
          echo "<option value='$supplier_id'>$supplier_name</option>";
         }
        ?>
       </select><br /><br />
       <input type="checkbox" name="vat">&nbsp;Include VAT<br /><br />
       <strong>Custom Message:</strong><br />
       <textarea name="message" cols="35" rows="3"></textarea><br /><br />
       <input type="submit" value="Confirm &amp; Print" style="background-color:#990000; color:#FFFFFF; border:2px solid #660000; font-size:16px;" />
      </form>
     </div>
  </div>
  
 </div>
 
<?php include("inc/page_footer.php"); ?>


Source code of close_po.php file

<script type="text/javascript">
<!-- Please enable javascripts
 function newPopup(url){
  popUpWindow = window.open(url,'popUpWindow','height=700,width=650,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=no')
 }
-->
</script>
<?php
 include("../inc/db_connect.php");
  $supplier_id = $_POST['po_supplier'];
  $date = date('Y-m-d');
  $po_value = $_POST['po_value'];
  $message = $_POST['message'];
  // form validate
  if(isset($_POST['vat'])){
   $vat = 'Yes';
  }else{
   $vat = 'No';
  }
  // create a new po record
  mysql_query("INSERT INTO purchase_orders (Purchase_Order_Date, Purchase_Order_Value, Purchase_Order_Vat, Purchase_Order_Suppliers, Purchase_Order_Message) VALUES ('$date', '$po_value', '$vat', '$supplier_id', '$message')");
  // get the created po id
  $q = mysql_query("SELECT MAX(Purchase_Order_ID) FROM purchase_orders");
  $r = mysql_fetch_assoc($q);
  $po_id = $r['MAX(Purchase_Order_ID)'];
  // read from temp
  $q = mysql_query("SELECT * FROM temp_po_items");
  while($r = mysql_fetch_assoc($q)){
   $po_item = $r['Temp_PO_Item'];
   $po_qty = $r['Temp_PO_Qty'];
   $po_price = $r['Temp_PO_Price'];
   // insert into po items
   mysql_query("INSERT INTO po_items (PO_ID, PO_Item, PO_Qty, PO_Price) VALUES ('$po_id', '$po_item', '$po_qty', '$po_price')");
  }
  // empty the temp table
  mysql_query("TRUNCATE temp_po_items");
  // open the print po window
   echo "<script type=\"text/javascript\"> \n";
    echo "newPopup(\"print_po.php?pid={$po_id}\"); \n";
    echo "var t = setTimeout(\"window.location='../purchase_order.php'\",1000); \n";
   echo "</script>";
 mysql_close($conn);
?>


Source code of print_po.php file

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Invoice</title>
<link rel="stylesheet" type="text/css" href="../inc/page_style.css" />
</head>

<body style="font-size:11px; background-color:#FFFFFF; background-image:none; ">
<?php include("../inc/printer.php"); ?>
<?php include("../inc/db_connect.php"); ?>
<?php
 $po_id = $_GET['pid'];
 // get po details
 $q = mysql_query("SELECT * FROM purchase_orders WHERE Purchase_Order_ID = '$po_id'");
 $r = mysql_fetch_assoc($q);
 $supplier_id = $r['Purchase_Order_Suppliers'];
 $po_value = $r['Purchase_Order_Value'];
 $po_vat = $r['Purchase_Order_Vat'];
 $po_date = $r['Purchase_Order_Date'];
 $po_message = $r['Purchase_Order_Message'];
 // get supplier details
 $q = mysql_query("SELECT * FROM suppliers WHERE Supplier_ID = '$supplier_id'");
 $r = mysql_fetch_assoc($q);
 $supplier_name = $r['Supplier_Title'].', '.$r['Supplier_Name'];
 $supplier_address = $r['Supplier_Address'].', '.$r['Supplier_Address_2'].', '.$r['Supplier_City'];
 $supplier_telephone = $r['Supplier_Telephone'];
?>
<table border="0" width="100%">
 <tr>
  <td width="50%">
   <table border="0">
    <tr><td><strong>Supplier:</strong></td><td><?php echo $supplier_name; ?></td></tr>
    <tr><td><strong>Address:</strong></td><td><?php echo $supplier_address; ?></td></tr>
    <tr><td><strong>Telephone:</strong></td><td><?php echo $supplier_telephone; ?></td></tr>
   </table>
  </td>
  <td width="50%">
   <table border="0" width="95%">
    <tr><td><div align="right"><strong>Date:</strong> <?php echo $po_date; ?></div></td></tr>
    <tr><td><div align="right"><strong>Our Ref:</strong> <?php echo $po_id; ?></div></td></tr>
    <tr><td><strong>&nbsp;</strong></td></tr>
   </table>
  </td>
 </tr>
</table>
<h1 style="text-align:center; background-color:#666666; color:#FFFFFF;">PURCHASE ORDER</h1>
<table align="center" width="98%" border="1px" cellpadding="0" cellspacing="0" style="border-collapse:collapse; border:1px solid #333333;">
 <tr style="background-color:#666666; color:#FFFFFF; font-weight:bold; "><td>&nbsp;Item No.</td><td>&nbsp;Item Name</td><td>&nbsp;Qty</td><td>&nbsp;Unit Price Rs.</td><td>&nbsp;Total Price Rs.</td></tr>
 <?php
  $q = mysql_query("SELECT * FROM po_items, items WHERE PO_Item = Item_Code AND PO_ID = '$po_id'");
  while($r = mysql_fetch_assoc($q)){
   $item_no = $r['Item_Part_No'];
   $item_name = $r['Item_Name'];
   $qty = $r['PO_Qty'];
   $price = $r['PO_Price'];
   $total = $qty * $price;
   echo "<tr>
      <td>&nbsp;{$item_no}</td>
      <td>&nbsp;{$item_name}</td>
      <td><div align='right'>&nbsp;{$qty}</div></td>
      <td><div align='right'>&nbsp;{$price}</div></td>
      <td><div align='right'>&nbsp;".number_format($total,2,'.','')."</div></td>
     </tr>";
  }
   // calculate vat
   if($po_vat == 'Yes'){
    $vat_amount = ($po_value/100)*12;
   }else{
    $vat_amount = 0;
   }
 ?>
 <tr bgcolor="#CCCCCC"><td colspan="5">&nbsp;</td></tr>
 <tr><td colspan="4"><div align="right" style="font-size:12px; "><strong>Vat 12%</strong></div></td><td><div align="right" style="font-size:12px;"><strong><?php echo number_format(($vat_amount),2); ?></strong></div></td></tr>
 <tr><td colspan="4"><div align="right" style="font-size:14px; "><strong>Total Value Rs.</strong></div></td><td><div align="right" style="font-size:14px;"><strong><?php echo number_format(($po_value+$vat_amount),2); ?></strong></div></td></tr>
</table>
<p style="text-align:left;"><strong>NOTES:</strong><br /><?php echo $po_message; ?></p>
<table align="center" width="98%" border="1px" cellpadding="0" cellspacing="0" style="border-collapse:collapse; border:1px solid #333333;">
 <tr>
  <td align="center"><br /><br /><br />........................................................<br />Prepared By</td>
  <td align="center"><br /><br /><br />........................................................<br />Checked By</td>
  <td align="center"><br /><br /><br />........................................................<br />Approved By</td>
 </tr>
</table>
<?php mysql_close($conn); ?>
</body>
</html>

Comments