Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi there,

i am creating restaurant software.. they need datewise bill number.
every day bill number should be start from Bill No : 1

Like today is 9/18/2015. today total bill generated 1,2,3,4,5,6.

now i want 1 number in textbox if time is 12:00 AM on 9/19/2015.
Posted
Comments
Leo Chapiro 18-Sep-15 7:22am    
Show the corresponding source code of your project please! Where are you stuck?

Store the last bill number together with its own date. Then, if you need a new bill number compare the current date with last bill number's one. If they are the same then new bill number = last bill number + 1, otherwise new bill numbwer = 1.
 
Share this answer
 

If you wish to update the TextBox as soon as the day changes, do something like this.


C#
using System.Windows.Threading;//reference WindowsBase
//Inside the class declaration
private readonly DispatcherTimer timer = new DispatcherTimer();
private int lastDay;
private void timer_Tick(object sender, EventArgs e)
 {
   if (DateTime.Now.Date.Day!=this.lastDay)
     {
      this.lastDay = DateTime.Now.Date.Day;
      setBillTextBoxToOne() ;//Your update TextBox method
     }
 }

//in the class constructor
 this.timer.Interval = TimeSpan.FromSeconds(15);
 this.timer.Tick += this.timer_Tick;
 this.lastDay = DateTime.Now.Date.Day;
 this.timer.Start();
 
Share this answer
 
If the bill number is only needed for reporting or receipt printing and bill information isn't deleted then I wouldn't bother storing it.

If you derive then it you don't run the risk of accidentally writing the same number twice (or more times) to the DB nor do you have to reset a pre-assigned number if a bill doesn't get created when, for example, a customer complains and the manager writes off the cost by way of compensation.

A notional table...
SQL
create table bill
(
  issueDate datetime not null 
  constraint df_bill_issued default getdate(),
  -- A computed column that groups all bills by issue date.
  issueDay as floor(cast(issueDate as float)),
  totalNoTax decimal(5,2) not null,
  taxRate decimal(3,2) not null
  constraint df_bill_tax default(0)
)



Set up some noddy data...
SQL
insert into bill(issueDate, totalNoTax) values (getdate(), 0.75)
insert into bill(issueDate, totalNoTax) values (dateadd(hh, 1, getdate()), 1.50)
insert into bill(issueDate, totalNoTax) values (dateadd(hh, 2, getdate()), 2.50)
insert into bill(issueDate, totalNoTax) values (dateadd(hh, 3, getdate()), 3.50)
insert into bill(issueDate, totalNoTax) values (dateadd(hh, 4, getdate()), 4.50)


Write a query to get the bill information with a derived bill number..

SQL
set ANSI_WARNINGS OFF  -- Squelch null suppression warning.
select 
  count(counter.issueDay)+1 as billNumber,
  bill.issueDate,
  bill.totalNoTax,
  bill.taxRate
from bill
left join (select issueDay, issueDate from bill) as counter
      on counter.issueDay= bill.Issueday
      and counter.issueDate < bill.IssueDate
group by 
  bill.issueDate,
  bill.totalNoTax,
  bill.taxRate


SQL
-- billNumber  issueDate               totalNoTax
-- ----------- ----------------------- ---------------------------------------
-- 1           2015-09-18 20:56:38.563 0.75
-- 2           2015-09-18 21:56:38.580 1.50
-- 3           2015-09-18 22:56:38.583 2.50
-- 4           2015-09-18 23:56:38.583 3.50
-- 1           2015-09-19 00:56:38.587 4.50


If you are going to be doing a lot of work where you need repeated access to bill number then consider converting the query to a view...

SQL
create view indexedBill 
as
 
select 
  count(counter.issueDay)+1 as billNumber,
  bill.issueDate,
  bill.totalNoTax,
  bill.taxRate
from bill
left join (select issueDay, issueDate from bill) as counter
      on counter.issueDay= bill.Issueday
      and counter.issueDate < bill.IssueDate
group by 
  bill.issueDate,
  bill.totalNoTax,
  bill.taxRate



...then pretend you've got a new table. For example...

SQL
select * from indexedBill where billNumber = 1

-- billNumber  issueDate               totalNoTax                              
-- 1           2015-09-18 21:02:07.037 0.75      
-- 1           2015-09-19 00:02:07.040 3.50      


SQL
select * from indexedBill where issueDate < '18 september 2015 23:59:00'

-- billNumber  issueDate               totalNoTax                              
-- 1           2015-09-18 21:02:07.037 0.75           
-- 2           2015-09-18 22:02:07.040 1.50           
-- 3           2015-09-18 23:02:07.040 2.50           


And if you really must display a bill number when creating a new bill (why?) then a query along the following lines ought to be adequate if there isn't much concurrent bill creation.

SQL
select max(billNumber) + 1 from indexedBill 
where issueDate = floor(cast(getdate() as float))
 
Share this answer
 
v2
Based on this[^] i'd strongly recommend to create custom class collection. Then you'll be able to filter collection on SellDate to get max[^] of BillNo via using Linq[^] query.

Steps to do:

  1. Create new Console project[^]
  2. Add new Class file (CTRL+SHIFT+A)
  3. Copy and paste below code

    VB
    'Bill class to store single Bill object
    Public Class Bill
    	Private aSellDate As DateTime
    	Private aBillNo As Integer = 0
    
    	Public Sub New(_SellDate As DateTime)
    		aSellDate = _SellDate
    	End Sub
    
    	Public Sub New(_SellDate As DateTime, _BillNo As Integer)
    		aSellDate = _SellDate
    		aBillNo = _BillNo
    	End Sub
    	
    	Public Property SellDate As Datetime
    		Get
    			Return aSellDate
    		End Get
    		Set (value As DateTime)
    			aSellDate = value
    		End Set
    	End Property
    	
    	Public Property BillNo As Integer
    		Get
    			Return aBillNo
    		End Get
    		Set(value As Integer)
    			aBillNo = value
    		End Set
    	End Property
    	
    End Class

  4. Add another Class file (CTRL+SHIFT+A)
  5. Copy and paste below code

    VB
    'Bills class is a collection of Bills
    Public Class Bills
        Inherits CollectionBase
    
        'default constructor
        Public Sub New()
            '
        End Sub
    
            'TO DO:
            'Add Item method
            'Add Remove method
            'etc.
    
        Public Function Add(b As Bill) As Bill
            'List is embeded object, it's a generic List 
            'get index of newly added Bill object
            Dim index As Integer = List.Add(b)
            'change BillNo from default (0) to value corresponding to the current day
            'use Linq query
            'NOTE: we need to cast List to specific type via using <code>Cast(Of Type</code>) or <code>OfType(Type)</code> method
            List(index).BillNo = List.Cast(of Bill).Where(Function(a) a.SellDate = b.SellDate).Max(Function(a) a.BillNo) + 1
            Return CType(List(index), Bill)
        End Function
    
    End Class
  6. Usage: (Main method)

    VB
    Sub Main()
    
       'declare variables
        Dim firstDay As Integer = 18
        Dim lastDay As Integer = 20
        Dim colBills As Bills
        Dim b As Bill
    
        'create collection of Bills
        colBills = new Bills()
    
        'add 15 Bills between 18. and 20. September 2015
        For i As Integer = 0 To 14
            Dim randomDay As Integer = CInt(Math.Floor((lastDay - firstDay + 1) * Microsoft.VisualBasic.Rnd())) + firstDay
            b = new Bill(new DateTime(2015,9,randomDay))
            colBills.Add(b)
        Next i
    
        Dim orderedList = colBills.Cast(of Bill).OrderBy(Function(a) a.SellDate).ThenBy(Function(a) a.BillNo)
        Console.WriteLine("Sell date {0} Bill number", "                   - ")
        For Each a As Bill in orderedList
            Console.WriteLine("{0} - {1}", a.SellDate, a.BillNo)
        Next
    
    
    End Sub


Result:
Sell date           - Bill number
2015-09-18 00:00:00 - 1
2015-09-18 00:00:00 - 2
2015-09-18 00:00:00 - 3
2015-09-18 00:00:00 - 4
2015-09-19 00:00:00 - 1
2015-09-19 00:00:00 - 2
2015-09-19 00:00:00 - 3
2015-09-20 00:00:00 - 1
2015-09-20 00:00:00 - 2
2015-09-20 00:00:00 - 3
2015-09-20 00:00:00 - 4
2015-09-20 00:00:00 - 5
2015-09-20 00:00:00 - 6
2015-09-20 00:00:00 - 7
2015-09-20 00:00:00 - 8



For further information, please see:
Creating Classes in Visual Basic .NET[^]
Walkthrough: Defining Classes (Visual Basic)[^]
Enumerable.Cast(Of TResult) Method (IEnumerable)[^]
Enumerable.OfType(Of TResult) Method (IEnumerable)[^]
Introduction to LINQ in Visual Basic[^]
Writing Your First LINQ Query (Visual Basic)[^]
LINQ - Sample Queries[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900