narsil/Tanshu.Accounts.Repository/DbValues.cs
tanshu 69617949bd Important! : Need to update to new schema using SQL Scripts
Important! : This version will not work.  It is pre-alpha and saved in case of catastrophic failure
Refactor: Remove dependency on Fluent Nhibernate.
Refactor: All Primary keys are now Guids.
Refactor: Class Mappings changed from AutoMap to Explicit Mappings.
Breakage: All Cascading is now disabled and entities must be explicitly saved/updated/deleted
Breakage: Auto Commiting is now off and "SaveChanges()" needs to be called on all BIs.
Refactor: Changed the pattern where all relevant db code for an operation is basically in the same function.
Chore: Removed Advance and Payments options.
2014-10-12 15:11:45 +05:30

78 lines
3.1 KiB
C#

using System;
using Tanshu.Accounts.Entities;
namespace Tanshu.Accounts.Repository
{
public static class DbValues
{
public static DateTime Date
{
get
{
using (var session = SessionManager.Session)
{
var query = session.CreateSQLQuery("SELECT Getdate();");
return (DateTime)query.UniqueResult();
}
}
}
public static string KotID
{
get
{
using (var session = SessionManager.Session)
{
const string query = @"SELECT ISNULL('K-' + CAST(MAX(CAST(SUBSTRING(KotID, 3,8) AS int)) + 1 AS nvarchar(8)), 'K-1') FROM Vouchers";
var sqlQuery = session.CreateSQLQuery(query);
return (string)sqlQuery.UniqueResult();
}
}
}
public static string KotCode
{
get
{
using (var session = SessionManager.Session)
{
const string query = @"SELECT ISNULL('S-' + CAST(MAX(CAST(SUBSTRING(Code, 3,8) AS int)) + 1 AS nvarchar(8)), 'S-1') FROM Kots";
var sqlQuery = session.CreateSQLQuery(query);
return (string)sqlQuery.UniqueResult();
}
}
}
public static string BillID(VoucherType voucherType)
{
using (var session = SessionManager.Session)
{
var query = "";
switch (voucherType)
{
case VoucherType.Regular:
case VoucherType.TakeAway:
query = @"
DECLARE @BillID nvarchar(10)
SELECT @BillID = ISNULL(CAST(MAX(CAST(REPLACE(BillID, '-', '') AS int)) + 1 AS nvarchar(9)), '010001') FROM Vouchers WHERE BillID LIKE '__-____'
AND BillID NOT LIKE 'NC-%' AND BillID NOT LIKE 'ST-%'
IF LEN(@BillID) = 5
SET @BillID = '0' + @BillID
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-' + SUBSTRING(@BillID, 3, 7)
IF SUBSTRING(@BillID,3,7) = '-0000'
SET @BillID = SUBSTRING(@BillID, 1, 2) + '-0001'
SELECT @BillID";
break;
case VoucherType.NoCharge:
query = @"SELECT ISNULL('NC-' + CAST(MAX(CAST(SUBSTRING(BillID, 4,9) AS int)) + 1 AS nvarchar(9)), 'NC-1') FROM Vouchers WHERE BillID LIKE 'NC-%'";
break;
case VoucherType.Staff:
query = @"SELECT ISNULL('ST-' + CAST(MAX(CAST(SUBSTRING(BillID, 4,9) AS int)) + 1 AS nvarchar(9)), 'ST-1') FROM Vouchers WHERE BillID LIKE 'ST-%'";
break;
default:
throw new ArgumentOutOfRangeException("voucherType");
}
var sqlQuery = session.CreateSQLQuery(query);
return (string)sqlQuery.UniqueResult();
}
}
}
}