Wednesday, July 4, 2018

Sales


This is the main function of the system - selling products. Since this system is intended to handle only installment payments, this system doesn't maintain an inventory. Product inventory will be an useful add-on for the system and if you want to improve the system it is good to develop an inventory and stock management part for this system.

When selling a product, following details need to feed to the system.


  • Date of selling
  • Customer's NIC number
  • Product name
  • Product description
  • Item value
  • Number of installments
  • Total interest in percentage
  • Value of an installment - This amount need to be calculated separately. It is up to you to improve these functions, if you are going to submit this as your project.
After posting the form - PHP script validate data for integrity. If customer NIC is not found in the customers table, error message displays. Other details also validated upon submission of the form.

There are lots of improvements need in this page and there are plenty of room for you to do additional developments here.

There are links to select and view sales according to the status of the sale.

  • All sales
  • Active sales
  • Completed sales
  • Cancelled / Suspended sales
Viewing of sales are handled by the view_sales.php page and we will discuss it in the next post.

Source Code for the Sales.php page


<?php include("header.php"); ?>
<div style="width:60%; float:left; margin-right:30px;">
<h3>New Sale</h3>
<?php
if(isset($_POST['submit'])){
$s_date = $_POST['date'];
$s_item = $_POST['item'];
$s_nic = $_POST['nic'];
$s_details = $_POST['details'];
$s_value = $_POST['value'];
$s_installments = $_POST['ins'];
$s_interest = $_POST['interest'];
$s_ins_value = $_POST['ivalue'];
if($s_date == '' || $s_item == '' || $s_nic == '' || $s_value == '' || $s_installments == '' || $s_interest == '' || $s_ins_value == ''){
$error = "Please enter all required fields.";
}
if(!isset($error)){
// no form errors. validate customer nic
$q = mysql_query("SELECT Cus_ID, Cus_Status FROM customers WHERE Cus_NIC = '$s_nic'");
if(mysql_num_rows($q) == 0){
$error = "Customer not found!";
}else{
$r = mysql_fetch_assoc($q);
$s_customer = $r['Cus_ID'];
$c_status = $r['Cus_Status'];
// validate status
if($c_status != "A"){
$error = "This customer is Black Listed or Suspended. Can not create new sale.";
}
}
}
if(!isset($error)){
mysql_query("INSERT INTO sales (S_Date, S_Customer, S_Item, S_Description, S_Value, S_Installments, S_Installment_Value, S_Interest) VALUES 
('$s_date', '$s_customer', '$s_item', '$s_details', '$s_value', '$s_installments', '$s_ins_value', '$s_interest')"); 
$noerror = "Sale created.";
}
}
// messages
if(isset($error)){
echo "<div class='error'><strong>Error:</strong> {$error}</div>";
}
if(isset($noerror)){
echo "<div class='noerror'><strong>Success:</strong> {$noerror}</div>";
}
?>

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table border="0">
<tr>
<td>Date *</td>
<td><input type="text" name="date" size="15" value="<?php if(isset($s_date)){ echo $s_date; }else{ echo $date; } ?>" /></td>
</tr>
<tr>
<td>Customer N.I.C. *</td>
<td><input type="text" name="nic" size="20" value="<?php if(isset($s_nic)){ echo $s_nic; } ?>" /></td>
</tr>
<tr>
<td>Item Name *</td>
<td><input type="text" name="item" size="50" value="<?php if(isset($s_item)){ echo $s_item; } ?>" /></td>
</tr>
<tr>
<td>Item Details</td>
<td><textarea name="details" cols="40" rows="4"><?php if(isset($s_details)){ echo $s_details; } ?></textarea></td>
</tr>
<tr>
<td>Item Value Rs. *</td>
<td><input type="text" name="value" size="12" value="<?php if(isset($s_value)){ echo $s_value; } ?>" /></td>
</tr>
<tr>
<td>No. of Installments *</td>
<td><input type="text" name="ins" size="10" value="<?php if(isset($s_installments)){ echo $s_installments; } ?>" /></td>
</tr>
<tr>
<td>Interest % *</td>
<td><input type="text" name="interest" size="10" value="<?php if(isset($s_interest)){ echo $s_interest; } ?>" /></td>
</tr>
<tr>
<td>Installment Value Rs. *</td>
<td><input type="text" name="ivalue" size="12" value="<?php if(isset($s_ins_value)){ echo $s_ins_value; } ?>" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</div>
<div style="width:36%; float:left;">
<h3>Search</h3>
<form method="post" action="search.php">
N.I.C. <input type="text" name="nic" size="20" /> <input type="submit" name="submit" value="Search" />
</form>
<h3>View Sales</h3>
<ul>
<li><a href="view_sales.php?t=all">All Sales</a></li><br />
<li><a href="view_sales.php?t=A">Active Sales</a></li><br />
<li><a href="view_sales.php?t=C">Completed Sales</a></li><br />
<li><a href="view_sales.php?t=S">Suspended / Cancelled Sales</a></li><br />
</ul>
</div>
<div style="clear:both;"></div>
<?php include("footer.php"); ?>

Download the complete project folder here.

Search Customers


This system search a customer by NIC number field in the customers database table. Customer search box is displayed in all pages except users. In all other pages, it is only the form with action location as search.php and the search query and display of search result is handled by the search.php page.

Source code of the search form

<form method="post" action="search.php">
    N.I.C. <input type="text" name="nic" size="20" /> 
    <input type="submit" name="submit" value="Search" />
</form>

When click on the search button it post the input value to the search.php page. There are two methods a form can send data "POST" and "GET". We uses POST method because posted data is invisible to the users and more secure than GET method. If you uses GET method, posted data is visible to the users in the URL. Most of the time in system developments, preferred method of a form is POST method.

Inline CSS styles used to show different colors according to customer's status. Red for black listed customers, Gray color for suspended customer.

Source code of "Search.php" page

<?php include("header.php"); ?>
<?php
if(isset($_GET['cid'])){
$cid = $_GET['cid'];
}
if(isset($_POST['submit'])){
$cnic = $_POST['nic'];
if($cnic == ''){
header("Location: customers.php");
ob_end_flush(0);
}
}
if(!isset($cid) && !isset($cnic)){
header("Location: customers.php");
ob_end_flush(0);
}
?>
<h3>Customer Details</h3>
<?php
// get customer details
if(isset($cnic)){
$q = mysql_query("SELECT * FROM customers WHERE Cus_NIC = '$cnic'");
}else{
$q = mysql_query("SELECT * FROM customers WHERE Cus_ID = '$cid'");
}
$r = mysql_fetch_assoc($q);
$c_id = $r['Cus_ID'];
$c_date = $r['Cus_Date'];
$c_name = $r['Cus_Name'];
$c_nic = $r['Cus_NIC'];
$c_add = $r['Cus_Add'];
$c_tel = $r['Cus_Tel'];
$c_sex = $r['Cus_Sex'];
$c_email = $r['Cus_Email'];
$c_status = $r['Cus_Status'];
if($c_status == "S"){
$status = "Suspended";
}else if($c_status == "B"){
$status = "Black Listed";
}else{
$status = "Active";
}
?>
<span style="float:right; background-color:#33CC99; border:1px solid #00CCCC; padding:3px 5px;"><a href="edit_customer.php?id=<?php echo $c_id; ?>" class="wht">Edit Customer Details</a></span>
<table width="100%" cellpadding="3px" cellspacing="1px" style="background-color:#00CCCC;">
<tr class="wht"><td><?php echo $c_name; ?></td></tr>
<tr class="wht"><td><strong>N.I.C.</strong> <?php echo $c_nic; ?></td></tr>
<tr class="wht"><td><?php echo $c_add; ?></td></tr>
<tr class="wht"><td><strong>Telephone:</strong> <?php echo $c_tel; ?></td></tr>
<tr class="wht"><td><strong>Email:</strong> <?php echo $c_email; ?></td></tr>
<tr class="wht"><td><strong>Sex:</strong> <?php echo $c_sex; ?></td></tr>
<tr class="
<?php
if($c_status == "B"){
echo "lred";
}else if($c_status == "S"){
echo "lblack";
}else{
echo "wht";
}
?>
"><td><strong>Status:</strong> <?php echo $status; ?></td></tr>
</table>

<h3>Sales</h3>
<table width="100%" style="background-color:#CC0000;" cellpadding="3px" cellspacing="1px">
<tr class="header">
<td>Date</td>
<td>Item Name</td>
<td>Details</td>
<td>Value Rs.</td>
<td>Installments</td>
<td>Interest</td>
<td>Inst. Value Rs.</td>
<td>Status</td>
<td>&nbsp;</td>
</tr>
<?php
$total_value= 0;
$q = mysql_query("SELECT * FROM sales WHERE S_Customer = '$c_id' ORDER BY S_Date DESC");
while($r = mysql_fetch_assoc($q)){
$s_id = $r['S_ID'];
$s_date = $r['S_Date'];
$s_item = $r['S_Item'];
$s_customer = $r['S_Customer'];
$s_details = $r['S_Description'];
$s_value = $r['S_Value'];
$total_value += $s_value;
$s_installments = $r['S_Installments'];
$s_interest = $r['S_Interest'];
$s_ins_value = $r['S_Installment_Value'];
$s_status = $r['S_Status'];
echo "<tr class='";
if($s_status == "C"){
echo "lgreen";
}else if($s_status == "S"){
echo "lred";
}else{
echo "wht";
}
echo "'>
<td>{$s_date}</td>
<td>{$s_item}</td>
<td title='{$s_details}' onMouseOver=\"this.bgColor='yellow'\"  onMouseOut=\"this.bgColor=''\">".substr($s_details,0,20)."...</td>
<td><div align='right'>{$s_value}</div></td>
<td>{$s_installments}</td>
<td>{$s_interest}%</td>
<td><div align='right'>{$s_ins_value}</div></td>
<td>{$s_status}</td>
<td><a href='new_payment.php?sid={$s_id}'>Payments</a></td>
</tr> \n";
}
?>
<tr class="lblack">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><div align="right"><strong>Total Value &nbsp;</strong></div></td>
<td><div align="right"><strong><?php echo number_format($total_value,2,'.',''); ?></strong></div></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>


<?php include("footer.php"); ?>

View Customers


Customers list is categorized according to the status of the customer as;

Here is the link to download the complete project folder.

Click to Download Project
  1. All Customers - shows all customers
  2. Suspended customers - shows only customer "status = S" is suspended
  3. Black Listed customer - shows only customer "status = B"
  4. Active Customers - shows customers with status = A
This is determined from the customers table status column. Color code is used to show different status in the table.

Source code of view_customers.php page


<?php include("header.php"); ?>
<h3>Customers</h3>
<p><strong>Filter:</strong> <a href="view_customers.php?t=A">Active</a> | <a href="view_customers.php?t=S">Suspended</a> | <a href="view_customers.php?t=B">Black Listed</a> | <a href="view_customers.php?t=all">All</a></p>
<table width="100%" style="background-color:#CC0000;" cellpadding="3px" cellspacing="1px">
<tr class="header">
<td>Date</td>
<td>Name</td>
<td>N.I.C.</td>
<td>Telephone</td>
<td>Address</td>
<td>Sex</td>
<td>Email</td>
<td>Stat.</td>
<td>&nbsp;</td>
</tr>
<?php
$status = $_GET['t'];
if($status != "all"){
$q = mysql_query("SELECT * FROM customers WHERE Cus_Status = '$status' ORDER BY Cus_Name");
}else{
$q = mysql_query("SELECT * FROM customers ORDER BY Cus_Name");
}
while($r = mysql_fetch_assoc($q)){
$cus_id = $r['Cus_ID'];
$cus_date = $r['Cus_Date'];
$cus_name = $r['Cus_Name'];
$cus_nic = $r['Cus_NIC'];
$cus_tel = $r['Cus_Tel'];
$cus_email = $r['Cus_Email'];
$sex = $r['Cus_Sex'];
$address = $r['Cus_Add'];
$c_status = $r['Cus_Status'];
echo "<tr class='";
if($c_status == "B"){
echo "lred";
}else if($c_status == "S"){
echo "lblack";
}else{
echo "wht";
}
echo "'>
<td>{$cus_date}</td>
<td>{$cus_name}</td>
<td>{$cus_nic}</td>
<td>{$cus_tel}</td>
<td>{$address}</td>
<td>{$sex}</td>
<td>{$cus_email}</td>
<td>{$c_status}</td>
<td><a href='edit_customer.php?id={$cus_id}'>Edit</a></td>
</tr> \n";
}
?>
</table>

<?php include("footer.php"); ?>

Customer Management


To create a new customer, we created a menu called "Customers" and linked to "customers.php" file. In the customers form, following fields are defined:


  1. Date - is the date of record creation
  2. Name - is the customer name
  3. NIC - is the customer's Identity card number, this is the unique number we use to cross check duplicated customers. Only one NIC number is allowed in the database (customers table).
  4. Telephone - This is also a mandatory field.
  5. Email - Optional input of customer's email address
  6. Sex - Male or Female
  7. Address - Postal address of the customer.
Except "Email" all other fields are mandatory. We check this in the PHP script after submit of the form. There is a list to view created customers grouped in to categories as follows:

  1. All Customers
  2. Black Listed Customers
  3. Suspended Customers
  4. Active Customers

We can edit customer's details and Status (Active, Black Listed, Suspended) by selecting appropriate link and view list of customers. There is a link to edit customer details.

"Customers.php" Source Code


<?php include("header.php"); ?>
<div style="width:60%; float:left; margin-right:30px;">
<h3>New Customer</h3>
<?php
if(isset($_POST['submit'])){
$cus_date = $_POST['date'];
$cus_name = $_POST['name'];
$cus_nic = $_POST['nic'];
$cus_tel = $_POST['tel'];
$cus_email = $_POST['email'];
$sex = $_POST['sex'];
$address = $_POST['address'];
if($cus_date == '' || $cus_name == '' || $cus_nic == '' || $cus_tel == '' || $address == ''){
$error = "Please enter all required fields.";
}
if(!isset($error)){
// no form errors. check for duplicates
$q = mysql_query("SELECT Cus_ID FROM customers WHERE Cus_NIC = '$cus_nic'");
if(mysql_num_rows($q) > 0){
$error = "This customer already created!";
}
}
if(!isset($error)){
mysql_query("INSERT INTO customers (Cus_Date, Cus_Name, Cus_NIC, Cus_Tel, Cus_Email, Cus_Sex, Cus_Add) VALUES ('$cus_date', '$cus_name', '$cus_nic', '$cus_tel', '$cus_email', '$sex', '$address')"); 
$noerror = "Customer has been added.";
}
}
// messages
if(isset($error)){
echo "<div class='error'><strong>Error:</strong> {$error}</div>";
}
if(isset($noerror)){
echo "<div class='noerror'><strong>Success:</strong> {$noerror}</div>";
}
?>

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<table border="0">
<tr>
<td>Date *</td>
<td><input type="text" name="date" size="15" value="<?php if(isset($cus_date)){ echo $cus_date; }else{ echo $date; } ?>" /></td>
</tr>
<tr>
<td>Name *</td>
<td><input type="text" name="name" size="50" value="<?php if(isset($cus_name)){ echo $cus_name; } ?>" /></td>
</tr>
<tr>
<td>N.I.C. *</td>
<td><input type="text" name="nic" size="20" value="<?php if(isset($cus_nic)){ echo $cus_nic; } ?>" /></td>
</tr>
<tr>
<td>Telephone *</td>
<td><input type="text" name="tel" size="20" value="<?php if(isset($cus_tel)){ echo $cus_tel; } ?>" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" size="50" value="<?php if(isset($cus_email)){ echo $cus_email; } ?>" /></td>
</tr>
<tr>
<td>Sex *</td>
<td>
<input type="radio" name="sex" value="M" <?php if(isset($sex)){ if($sex == "M"){ echo "checked"; }}else{ echo "checked"; } ?> /> Male &nbsp; 
<input type="radio" name="sex" value="F" <?php if(isset($sex) && $sex == "F"){ echo "checked"; } ?> /> Female
</td>
</tr>
<tr>
<td>Address *</td>
<td><textarea cols="40" rows="4" name="address"><?php if(isset($address)){ echo $address; } ?></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</div>
<div style="width:36%; float:left;">
<h3>Search</h3>
<form method="post" action="search.php">
N.I.C. <input type="text" name="nic" size="20" /> <input type="submit" name="submit" value="Search" />
</form>
<h3>View Customers</h3>
<ul>
<li><a href="view_customers.php?t=all">All Customers</a></li><br />
<li><a href="view_customers.php?t=B">Black Listed Customers</a></li><br />
<li><a href="view_customers.php?t=S">Suspended Customers</a></li><br />
<li><a href="view_customers.php?t=A">Active Customers</a></li><br />
</ul>
</div>
<div style="clear:both;"></div>
<?php include("footer.php"); ?>

Create, Edit and Delete Users (Manage Users)


These users are system users who can access to system and perform tasks. We created a menu called "users" to access this option and linked to "users.php" file. Users can also assign privileges that restrict operations in the system. There are three privileges;


  1. Manage Inventory
  2. Billing
  3. Manage Users
User must have at least one privilege or more. We record these privileges along with user name and password in the users table. Create, Edit and Delete operations are all handled in same "users.php" page.


<?php include("header.php"); ?>
<script type="text/javascript">
function delUser(id){
var r=confirm("Are you sure you want to delete this user? all data associated with this user will be deleted or become unavailable!");
if(r==true){
window.location="delete_user.php?id="+id;
}
}
function addnew(){
window.location="users.php";
}
</script>
<div style="width:60%; float:left; margin-right:30px;">
<h3>New User / Edit User</h3>
<?php
// form processing
if(isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];
if(!isset($_POST['chkbox'])){
$error = "Please select one or more privileges for this user!";
}else{
$privileges = $_POST['chkbox'];
$priv = "";
for($i=0; $i<count($privileges); $i++){
$priv .= $privileges[$i].", ";
}
}
if($username == ''){ $error = "Please enter User Name!"; }
if($password == ''){ $error = "Please enter a Password!"; }
// duplicate check
if(isset($_GET['act'])){
$uid = $_GET['id'];
$q = mysql_query("SELECT * FROM users WHERE User_Name = '$username' AND User_ID != '$uid'");
}else{
$q = mysql_query("SELECT * FROM users WHERE User_Name = '$username'");
}
if(mysql_num_rows($q) > 0){
$error = "Another user with the same name already exists in the database. Please select a different name!";
}
if(!isset($error)){
if(isset($_GET['act'])){ // user update request
$uid = $_GET['id'];
mysql_query("
UPDATE users SET User_Name = '$username', User_Password = '$password', User_Privileges = '$priv' 
WHERE User_ID = '$uid'
");
$noerror = "User details has been updated!";
}else{ // add new user request
mysql_query("
INSERT INTO users (User_Name, User_Password, User_Privileges) 
VALUES ('$username', '$password','$priv')
");
$noerror = "User has been added to the database!";
}
}
}
// end of form processing
if(isset($error)){
echo "<div class=\"error\">{$error}</div>";
}
if(isset($noerror)){
echo "<div class=\"noerror\">";
echo $noerror;
echo "</div>";
}
?>
<?php
// user editing
if(isset($_GET['act'])){
$uid = $_GET['id'];
$q = mysql_query("SELECT * FROM users WHERE User_ID = '$uid'");
$r = mysql_fetch_assoc($q);
function checkprev($str){
$uid = $_GET['id'];
$a = mysql_query("SELECT * FROM users WHERE User_ID = '$uid'");
$b = mysql_fetch_assoc($a);
$c = $b['User_Privileges'];
if(stristr($c, $str)){
return "Checked";
}
}
}
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>User Name:</td><td><input type="text" size="35" name="username" value="<?php if(isset($_GET['act'])){ echo $r['User_Name']; } ?>"></td>
</tr>
<tr>
<td>Password:</td><td><input type="password" size="10" name="password"></td>
</tr>
<tr>
<td>&nbsp;</td><td>&nbsp;</td>
</tr>
<tr>
<td colspan="2" style="background-color:#D2F2D3; border:1px solid #0033CC; padding-left:3px; "><strong>Previleges</strong></td>
</tr>
<tr>
<td colspan="2" style="border:1px solid #0033CC; ">
<table>
<tr>
<td><input type="checkbox" name="chkbox[]" value="Manage Inventory" <?php if(isset($_GET['act'])){ echo checkprev('Manage Inventory'); } ?>> Manage Inventory</td>
<td><input type="checkbox" name="chkbox[]" value="Billing" <?php if(isset($_GET['act'])){ echo checkprev('Billing'); } ?>> Billing</td>
<td><input type="checkbox" name="chkbox[]" value="Manage Users" <?php if(isset($_GET['act'])){ echo checkprev('Manage Users'); } ?>> Manage Users</td>
</tr>
</table>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td colspan="2"><input class="btn" type="submit" name="submit" value="<?php if(isset($_GET['act'])){ echo "Update User"; }else{ echo "Add User"; } ?>"> <?php if(isset($_GET['act'])){ echo "&nbsp; <input class='btn' type='button' value='&laquo; Back' onclick='addnew()' />"; } ?></td>
</tr>
</table>
</form>
</div>
<div style="width:36%; float:left;">
<h3>System Users</h3>
<table width="97.5%" cellpadding="3px" cellspacing="1px">
<tr id="header-row">
<td>User ID</td>
<td>User Name</td>
<td>Actions</td>
</tr>
<?php
$q = mysql_query("SELECT User_ID, User_Name FROM users");
while($r = mysql_fetch_assoc($q)){
echo "<tr id=\"sh1\"><td>{$r['User_ID']}</td><td>{$r['User_Name']}</td><td>";
echo "<a href=\"users.php?act=&id={$r['User_ID']}\">Edit</a> | <a href=\"javascript: delUser({$r['User_ID']})\">Delete</a>";
echo "</td></tr>";
}
?>
</table>
</div>
<div style="clear:both;"></div>
<?php include("footer.php"); ?>

Edit Profile (Company Details)


When customer need to update company name, address and telephone numbers, he can do it from the "Edit Profile" option. These information will be printed on invoices and payment receipts. These company details are stored in profile table in mysql database. We have to insert a record using "php my admin" and here we perform only UPDATE operation.

<?php include("header.php"); ?>
<h3>Edit Profile</h3>
<?php
$q = mysql_query("SELECT * FROM profile");
$r = mysql_fetch_assoc($q);
$name = $r['Name'];
$address = $r['Address'];
$telephone = $r['Telephone'];
if(isset($_POST['submit'])){
$name = $_POST['name'];
$address = $_POST['address'];
$telephone = $_POST['telephone'];
if(!empty($name) && !empty($address) && !empty($telephone)){
mysql_query("UPDATE profile SET Name = '$name', Address = '$address', Telephone = '$telephone'");
$noerror = "Profile updated!";
}else{
$error = "Please enter all details!";
}
}
if(isset($error)){
echo "<div class='error'>{$error}</div>";
}
if(isset($noerror)){
echo "<div class='noerror'>{$noerror}</div>";
}
?>
<form method="post" action="edit_profile.php">
<table border="0">
<tr>
<td>Name:</td><td><input type="text" size="40" name="name" value="<?php echo $name; ?>" /></td>
</tr>
<tr>
<td>Address:</td><td><textarea name="address" cols="30" rows="5"><?php echo $address; ?></textarea></td>
</tr>
<tr>
<td>Telephone:</td><td><input type="text" size="40" name="telephone" value="<?php echo $telephone; ?>" /></td>
</tr>
<tr>
<td>&nbsp;</td><td><input class="btn" type="submit" name="submit" value="Update" /></td>
</tr>
</table>
</form>
<?php include("footer.php"); ?>