如何在CRM系統(tǒng)中實(shí)現(xiàn)單據(jù)格式的自動(dòng)編號(hào)
在CRM系統(tǒng)的使用及實(shí)施過程中,經(jīng)常會(huì)用到報(bào)價(jià)單,合同,銷售單,采購(gòu)單等單據(jù),這些單據(jù)有一個(gè)共同點(diǎn)是都會(huì)有一個(gè)單據(jù)編號(hào)的字段,如下圖所示的合同編號(hào):
如圖,合同編號(hào)的初始化規(guī)則是:“C-” + “年月日-” + 順序號(hào),該表信息為:
英文表名 |
db_contractinfo |
中文表名 |
合同信息表 |
物理表名 |
|
表編號(hào) |
29 |
主鍵 |
ci_pkid |
合同編號(hào) |
ci_contractid |
我們?cè)诒韱卧O(shè)置中,模塊編程里建立一段功能程序
作用表名 |
db_contractinfo |
Script說明 |
模塊啟動(dòng)時(shí)初始化合同編號(hào) |
Script代碼 |
|
以上代碼實(shí)現(xiàn):"C-" + "年月日-",下面代碼實(shí)現(xiàn)順序號(hào):
到服務(wù)器打開Microsoft SQL Server Management Studio,然后找到需要找的物理表。這里,我們的物理表名是:db_contractinfo_init,故找到:dbo. db_contractinfo_init。
:
如果是其它表的此類功能,只需把下表中的紅色部分字段做相應(yīng)替換即可,同時(shí),在JS里的字段是11位,也是取其11位,如是其它位數(shù),在下面程序中相應(yīng)把11替換成其它位數(shù)。
英文表名 |
db_contractinfo |
中文表名 |
合同信息表 |
物理表名 |
db_contractinfo_init |
表編號(hào) |
29 |
主鍵 |
ci_pkid |
合同編號(hào) |
ci_contractid |
ALTER TRIGGER [ht_tg_db_contractinsert] ON [dbo].[db_contractinfo_init]
FOR INSERT /* ht_tg_db_contractinsert是當(dāng)前觸發(fā)器名字,只要不跟之前名字一樣,可以隨便取,它有中括號(hào)是因?yàn)?/SPAN> 系統(tǒng)自動(dòng)生成的方便好看 */
AS
begin
declare @contractid varchar(20) /*首先要定義一個(gè)變量來存他插入的編號(hào)信息 */
select @contractid=ci_contractid from inserted /*這個(gè)就是獲取當(dāng)前插入的編號(hào),存到變量 */
declare @contractcount numeric(10)
declare @flow varchar(20)
--select @contractcount=count(*) from db_contractinfo_init where left(ci_contractid,11)= @contractid
select @contractid=left(@contractid,11) /*獲取插入編號(hào)的前11位。在這里,js生成的就是11位 */
select top 1 @flow=ci_contractid from db_contractinfo_init where left(ci_contractid,11)=@contractid and ci_contractid<>@contractid order by ci_contractid desc /* 這一句 就是查詢當(dāng)前11位編號(hào)相同的編號(hào)在數(shù)據(jù)庫(kù)中有多少個(gè),如我這個(gè)編號(hào)是 C20141028-,則查詢有多少個(gè)同樣編號(hào)的,如果有3個(gè)這樣的,最新這個(gè)就是004 */
if (@flow is not null ) /*該if語句查詢存在多少個(gè)是保存在變量@flow里面的*/
select @contractcount=convert(int,right(@flow,3))+1
else
select @contractcount='1'
/*下面幾句用來判斷編號(hào)的位數(shù),如為一位,就要在前面加00,2位就加0 三位就直接加上那個(gè)數(shù)字。
if (len(@contractcount)=1 )
begin
select @flow='00'+convert(varchar(1),@contractcount)
end
if (len(@contractcount)=2 )
begin
select @flow='0'+convert(varchar(2),@contractcount)
end
if (len(@contractcount)=3 )
begin
select @flow=convert(varchar(3),@contractcount)
end
update db_contractinfo_init set ci_contractid=ci_contractid+@flow where ci_pkid=@@identity
/* select @contractmoney=ci_money from inserted
update db_custominfo set ci_purchaseamount=ci_purchaseamount+ @contractmoney,ci_customkind=3 where ci_pkid=(select ci_customid from inserted)
*/
end