Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I am making an accounting software. In accounting, each record has two or more lines/rows such as:

-----------------
Sales | 500,000 |
Cash | -500,000|
----------------

There are two rows in above accounting. similarly another accounting record my contain four rows such as:

-----------------
Sales | 500,000 |
Discount | -100,000|
Cash | -400,000|
----------------

My problem is, how to give a unique id to a particular accounting record such as:

-----------------
1 |Sales | 500,000 |
1 |Cash | -500,000|
2 |Sales | 500,000 |
2 |Discount | -100,000|
2 |Cash | -400,000|
----------------

THAT I CAN DISTINGUISH BETWEEN TWO ACCOUNTING RECORDS

and when user saves a new accounting record with 2 or more rows. Mysql automatically give '3' to all those two or more rows.

IS IT POSSIBLE?
Posted
Comments
Richard C Bishop 30-May-13 14:54pm    
Im sure it is. I know in MS SQL you can just use "IsIdentity" and it will auto-increment your record number. Try and find something like that in MySql.
Bernhard Hiller 31-May-13 2:56am    
You also ought to learn some basics of your domain, e.g. double-entry bookkeeping...

SQL
Hello,

You will have to to this at the time of insert. First add a column say record_id INTEGER in the table and have a primary key as record_id + account_id (column containing the vlaues Sales, Discount, Cash etc). 
Then define following table in your database <pre lang="SQL">CREATE TABLE sequence_data (
    sequence_name       varchar(100) NOT NULL,
    sequence_increment  int(11) unsigned NOT NULL DEFAULT 1,
    sequence_min_value  int(11) unsigned NOT NULL DEFAULT 1,
    sequence_max_value  bigint(20) unsigned NOT NULL DEFAULT 18446744073709551615,
    sequence_cur_value  bigint(20) unsigned DEFAULT 1,
    sequence_cycle      boolean NOT NULL DEFAULT FALSE,
    PRIMARY KEY (sequence_name)
)

Then define following function to generate the sequence number
SQL
CREATE FUNCTION nextval (seq_name varchar(100))
RETURNS bigint(20) NOT DETERMINISTIC
BEGIN
    DECLARE cur_val bigint(20);
 
    SELECT
        sequence_cur_value INTO cur_val
    FROM
        sequence.sequence_data
    WHERE
        sequence_name = seq_name;
 
    IF cur_val IS NOT NULL THEN
        UPDATE
            sequence.sequence_data
        SET
            sequence_cur_value = IF (
                (sequence_cur_value + sequence_increment) > sequence_max_value,
                IF (
                    sequence_cycle = TRUE,
                    sequence_min_value,
                    NULL
                ),
                sequence_cur_value + sequence_increment
            )
        WHERE
            sequence_name = seq_name;
    END IF;
     RETURN cur_val;
END;

Finally in your code first call the above function to get the unique identifier and then insert data in to your accounting table. Following code should get you started.
C#
public void createAcTxn(List<AcEntry> entries) {
    string strCon = @"server=localhost;userid=userid;password=pasword;database=mydb";
    int retVal = -1;
    MySqlCommand seqCmd = null;
    MySqlCommand insCmd = null;
    MySqlConnection conn = null;

    try
    {
        conn = new MySqlConnection(cs);
        conn.Open();
        seqCmd = new MySqlCommand("CALL nextval(@seq_name);", conn);
        seqCmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.Add("@seq_name", System.Data.SqlDbType.VarChar, 40).Value = "ACTXN_SEQ";
        
        retVal = (int) seqCmd.ExecuteScalar();
        insCmd = new MySqlCommand("INSERT actxn(record_id,  account_id, txn_amount) VALUES(@recId, @acId, @amnt)", conn);
        insCmd.Prepare();
        insCmd.Parameters.Add("@recId", SqlDbType.Int);
        insCmd.Parameters.Add("@acId", SqlDbType.VarChar);
        insCmd.Parameters.Add("@amnt", SqlDbType.Decimal);

        foreach(AcEntry entry in entries) {
            incCmd.Parameters(0).Value = retVal;
            incCmd.Parameters(1).Value = entry.AccountId;
            incCmd.Parameters(2).Value = entry.Amount;
            insCmd.ExecuteNonQuery();
        }
    } catch (MySqlException ex) {
        Console.WriteLine("Error: {0}",  ex.ToString());
    } finally {
        doClose(conn);  // A Helper method to cleanup & release the connection
    }
}

Note: I have not tested the above code, so there might be some type/compilation errors. You should be able to fix those in IDE

Regards,
 
Share this answer
 
You will need sequence generation package which will return unique sequence number for each sequence-key.

A) Record_Key
B) Journal_Key

Before insert you will have to write logic to insert these 2 generated keys into the record. Record_Key sequence has to be fetched every time an insert into the accounting table is done. Journal_key to be fetched every time the accounting legs are identified for insert. In your case 1 and 2 are Journal_Key and record_key can be a primary key for your accounting table.
 
Share this answer
 

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