<% set date ymd set century on set hours to 24 set mark to [-] expires=-1 ajaxresponse=CREATEOBJECT('empty') oJSON=NEWOBJECT('json','json.fxp') jsondata=orequest.form('data-area') STRTOFILE(jsondata,addbs(oprop.appstartpath)+[temp\jsondata.json]) set procedure to (addbs(oprop.appstartpath)+'json_new.fxp') additive jsonObject=json_decode(jsondata) headerText=[] if NOT type([jsonObject._1])=[O] ADDPROPERTY(ajaxresponse,'tmessage','Can not save empty bill') ADDPROPERTY(ajaxresponse,'terror','true') oresponse.write(ojson.stringify(ajaxresponse)) oresponse.flush() RETURN '' ENDIF respmsg=transform(lineno())+[ started. ] m_transid=UPPER(ALLTRIM(NVL(orequest.form('m_transid'),''))) m_vendorid=UPPER(ALLTRIM(orequest.form('m_vendorid'))) m_vendorname=UPPER(ALLTRIM(TRANSFORM(orequest.form('m_vendorname')))) m_depositid=UPPER(ALLTRIM(TRANSFORM(orequest.form('m_depositid')))) m_depositname=UPPER(ALLTRIM(orequest.form('m_depositname'))) m_date=orequest.form('m_date') m_amount=orequest.form('m_amount') m_discount=orequest.form('m_discount') errormessage=[] IF m_vendorid==m_vendorname && Add new vendor USEsafe([accounts]) FOR i=1 TO 9999 newvendor=[06]+PADL(ALLTRIM(TRANSFORM(i)),4,[0]) IF NOT SEEK(newvendor,[accounts],[accountid]) APPEND BLANK replace accountid WITH newvendor, name WITH m_vendorname exit endif endfor endif IF m_depositid==m_depositname && Add new vendor USEsafe([accounts]) FOR i=1 TO 9999 newdeposit=[06]+PADL(ALLTRIM(TRANSFORM(i)),4,[0]) IF NOT SEEK(newdeposit,[accounts],[accountid]) APPEND BLANK replace accountid WITH newdeposit, name WITH m_depositname exit endif endfor ENDIF usesafe([CashSale]) IF EMPTY(m_transid) tsql=[select top 1 transid,IIF(VAL(transid)=0,VAL(SUBSTR(transid,2)),VAL(transid)) AS VTR, padl(allt(transid),9) as ptransid ] tsql=tsql+[ from ("]+dbclocation+'CashSale.dbf'+[") order by vtr desc, ptransid desc into cursor xcursor] &tsql m_transid=ptransid ncntr=vtr SELECT [CashSale] DO WHILE .t. m_transid=PADR(ALLTRIM(TRANSFORM(ncntr)),9,[ ]) =SEEK(m_transid,[CashSale],[transid]) IF EOF([CashSale]) EXIT ENDIF ncntr=ncntr+1 ENDDO APPEND blank replace dtime WITH DATETIME(),transid WITH m_transid SKIP ENDIF m_transid=PADR(ALLTRIM(TRANSFORM(m_transid)),9,[ ]) =SEEK(m_transid,[CashSale],[transid]) ADDPROPERTY(ajaxresponse,'isadding',orequest.form('isadding')) ADDPROPERTY(ajaxresponse,'m_transid',m_transid) ADDPROPERTY(ajaxresponse,'m_vendorid',m_vendorid) ADDPROPERTY(ajaxresponse,'m_vendorname',m_vendorname) ADDPROPERTY(ajaxresponse,'m_depositid',m_depositid) ADDPROPERTY(ajaxresponse,'m_depositname',m_depositname) ADDPROPERTY(ajaxresponse,'m_date',m_date) ADDPROPERTY(ajaxresponse,'m_amount',m_amount) replace date WITH CTOD(m_date), modified WITH DATETIME(), customer WITH m_vendorid, depositid WITH m_depositid SET DATE BRITISH ADDPROPERTY(ajaxresponse,'cols',headerText) tcounter=0 newitemIDSadded=0 newitemNAMESadded=0 tsql=[select top 1 * from (']+dbclocation+'CashData'+[') where allt(transid)=='xxxxxx' order by transid into cursor griddatacursor readwrite] &tsql if type([jsonObject._1])=[O] respmsg=respmsg+transform(lineno())+[ created cursor for data. ] do while .t. tcounter=tcounter+1 c_param=[jsonObject._]+allt(transform(tcounter)) IF type(c_param)<>[O] exit endif with &c_param titemid=allt(upper(.get(1))) tname=ALLTRIM(upper(.get(2))) tsaleintax=.get(3) ttaxprate=.get(4) tsaleexctax=.get(5) tqty=.get(6) tdisc=.get(7) ttotal=val(transform(tqty))*val(transform(tsaleintax)) tbatch=.get(9) texpiry=.get(10) tbonus=.get(11) ENDWITH *Check if itemid exists. If not add the next itemid USEsafe('asctable') usesafe('itemmast') thisitemid=PADR(ALLTRIM(titemid),LEN(itemid)) itemnotfound=.f. DO case CASE SEEK(thisitemid,[asctable],[bar_code]) titemid=asctable.itemid CASE SEEK(thisitemid,[itemmast],[bar_code]) titemid=itemmast.itemid CASE SEEK(thisitemid,[itemmast],[itemid]) titemid=itemmast.itemid OTHERWISE itemnotfound=.t. endcase SELECT [itemmast] itisownitem=.f. if itemnotfound *first to check if itemid is a number itisanumber=.t. for tc=1 to len(titemid) if not between(asc(subst(titemid,tc,1)),48,57) itisanumber=.f. exit endif endfor willadd=.t. if not itisanumber tsql=[select itemid from itemmast where allt(name)==allt(tname) into cursor x] &tsql if _tally>0 titemid=itemid else newitemidsadded=newitemidsadded+1 *** If Not Empty(xcompany.itemstrt) cbstart=xcompany.itemstrt Else cbstart=100 Endif cbend=9999999999999 If Empty(xcompany.secstart) secstart='' Else secstart=ALLTRIM(xcompany.secstart) Endif * secstart='' lentcode=Len(secstart+Alltrim(Str(cbend,16,0))) Local myI SELECT [itemmast] SET ORDER TO valitemid DESCENDING GO top LOCATE FOR isown IF NOT EOF() vitem=VAL(itemid) else SELECT TOP 1 itemid, VAL(itemid) as tvitem FROM itemmast WHERE isown ORDER BY tvitem DESC INTO CURSOR xc vitem=tvitem endif IF EMPTY(vitem) secstart=[] ENDIF IF VAL(secstart)>0 secstart=[] endif cbstart=MAX(100,vitem,cbstart) USE IN SELECT('xc') SELECT [itemmast] For myI=cbstart To cbend newbarCode=secstart+Alltrim(Str(myI,lentcode,0)) If Not Seek(newbarCode,[itemmast],"itemid") And Not Seek(newbarCode,[itemmast],"Bar_code") Locate For Val(ItemID)=Val(newbarCode) And Val(newbarCode)<>0 If Not Eof() Loop Endif newisown=newbarcode Exit Endif ENDFOR *** titemid=newbarcode itisownitem=.t. ENDIF endif select [itemmast] if not seek(titemid,[itemmast],[itemid]) respmsg=respmsg+transform(lineno())+[ adding new item to data. ]+titemid+[ ] append blank replace itemid with titemid, name with tname, sale with val(tsaleintax), taxprate with val(ttaxprate),; cost with val(trate), pcost with val(trate), type with 'Inventory',; saleac with '090001', costac with '120001', stockac with '020001', isown WITH itisownitem else replace itemid with titemid, name with tname, sale with val(tsaleintax), taxprate with val(ttaxprate),; pcost with val(trate), type with 'Inventory',; saleac with '090001', costac with '120001', stockac with '020001', isown WITH itisownitem endif use in select([itemmast]) ENDIF *Now adding data *try *THEMODIFIEDSTR=SUBSTR(TMODIFIED,1,10)+[ ]+SUBSTR(TMODIFIED,12,8) *MODIFIEDSTR=CTOT(THEMODIFIEDSTR) SELECT [griddatacursor] APPEND BLANK replace sr_no WITH tcounter, transid with m_Transid, itemid WITH titemid , desc WITH tname, ; rate WITH VAL(tsaleintax), tax1 WITH VAL(ttaxprate), qty WITH VAL(tqty), ; discount WITH VAL(tdisc), total WITH ttotal, batch WITH tbatch, bonus WITH VAL(tbonus), ; EXPIRY WITH convert2date(TEXPIRY) *CATCH * haserror=.t. * errormessage=[ ERROR above ]+TRANSFORM(LINENO()) *endtry *replace modified WITH CTOT(tmodified), expiry with convert2date(texpiry) *End adding enddo ENDIF SELECT [griddatacursor] thislink=DBF() usesafe('CashData') SET ORDER TO transid =SEEK(m_transid,[CashData],[transid]) ADDPROPERTY(ajaxresponse,'deleting',m_transid) DO WHILE NOT EOF() AND ALLTRIM(transid)==ALLTRIM(m_transid) DELETE ADDPROPERTY(ajaxresponse,'deleted',itemid) SKIP enddo APPEND FROM (thislink) IF EMPTY(errormessage) ADDPROPERTY(ajaxresponse,'terror','') ELSE ADDPROPERTY(ajaxresponse,'terror','true') endif ADDPROPERTY(ajaxresponse,'tmessage',errormessage+respmsg) ADDPROPERTY(ajaxresponse,'newids',newitemidsadded) ADDPROPERTY(ajaxresponse,'newnames',newitemnamesadded) ********************************************* select [CashSale] newnum=m_transid updatetransactions([CashSale], newnum) IF NOT EMPTY(bk1loc) USE (bk1loc+[CashSale]) IN SELECT([CashSalebk]) ALIAS [CashSalebk] AGAIN USE (bk1loc+[CashData]) IN SELECT([CashDatabk]) ALIAS [CashDatabk] again USE (bk1loc+[acctran]) IN SELECT([acctranbk]) ALIAS [acctranbk] AGAIN USE (bk1loc+[stock]) IN SELECT([stockbk]) ALIAS [stockbk] SELECT [CashSalebk] SET ORDER TO transid =SEEK(newnum) DELETE while transid=newnum SELECT [CashSale] SET ORDER TO transid =SEEK(newnum) DO WHILE NOT EOF() AND transid=newnum SCATTER NAME dtbk SELECT [CashSalebk] APPEND BLANK GATHER name dtbk SELECT [CashSale] skip ENDDO USE IN SELECT([CashSalebk]) SELECT [CashDatabk] SET ORDER TO transid =SEEK(newnum) DELETE while transid=newnum SELECT [CashData] SET ORDER TO transid =SEEK(newnum) DO WHILE NOT EOF() AND transid=newnum SCATTER NAME dtbk SELECT [CashDatabk] APPEND BLANK GATHER name dtbk SELECT [CashData] skip ENDDO USE IN SELECT([CashDatabk]) SELECT [acctranbk] SET ORDER TO transid =SEEK(newnum+[CashSale]) DELETE while transid=newnum AND vtype=[CashSale] SELECT [acctran] SET ORDER TO transid =SEEK(newnum+[CashSale]) DO WHILE NOT EOF() AND transid=newnum AND vtype=[CashSale] SCATTER NAME dtbk SELECT [acctranbk] APPEND BLANK gather name dtbk SELECT [acctran] skip ENDDO USE IN SELECT([acctranbk]) SELECT [stockbk] SET ORDER TO transid =SEEK(newnum+[CashSale]) DELETE while transid=newnum AND vtype=[CashSale] SELECT [stock] SET ORDER TO transid =SEEK(newnum+[CashSale]) DO WHILE NOT EOF() AND transid=newnum AND vtype=[CashSale] SCATTER NAME dtbk SELECT [stockbk] APPEND BLANK gather name dtbk SELECT [stock] skip ENDDO USE IN SELECT([stockbk]) endif IF NOT EMPTY(bk2loc) USE (bk2loc+[CashSale]) IN SELECT([CashSalebk]) ALIAS [CashSalebk] AGAIN USE (bk2loc+[CashData]) IN SELECT([CashDatabk]) ALIAS [CashDatabk] again USE (bk2loc+[acctran]) IN SELECT([acctranbk]) ALIAS [acctranbk] AGAIN USE (bk2loc+[stock]) IN SELECT([stockbk]) ALIAS [stockbk] AGAIN SELECT [CashSalebk] SET ORDER TO transid =SEEK(newnum) DELETE while transid=newnum SELECT [CashSale] SET ORDER TO transid =SEEK(newnum) DO WHILE NOT EOF() AND transid=newnum SCATTER NAME dtbk SELECT [CashSalebk] APPEND BLANK GATHER name dtbk SELECT [CashSale] skip ENDDO USE IN SELECT([CashSalebk]) SELECT [CashDatabk] SET ORDER TO transid =SEEK(newnum) DELETE while transid=newnum SELECT [CashData] SET ORDER TO transid =SEEK(newnum) DO WHILE NOT EOF() AND transid=newnum SCATTER NAME dtbk SELECT [CashDatabk] APPEND BLANK GATHER name dtbk SELECT [CashData] skip ENDDO USE IN SELECT([CashDatabk]) SELECT [acctranbk] SET ORDER TO transid =SEEK(newnum+[CashSale]) DELETE while transid=newnum AND vtype=[CashSale] SELECT [acctran] SET ORDER TO transid =SEEK(newnum+[CashSale]) DO WHILE NOT EOF() AND transid=newnum AND vtype=[CashSale] SCATTER NAME dtbk SELECT [acctranbk] APPEND BLANK gather name dtbk SELECT [acctran] skip ENDDO USE IN SELECT([acctranbk]) SELECT [stockbk] SET ORDER TO transid =SEEK(newnum+[CashSale]) DELETE while transid=newnum AND vtype=[CashSale] SELECT [stock] SET ORDER TO transid =SEEK(newnum+[CashSale]) DO WHILE NOT EOF() AND transid=newnum AND vtype=[CashSale] SCATTER NAME dtbk SELECT [stockbk] APPEND BLANK gather name dtbk SELECT [stock] skip ENDDO USE IN SELECT([stockbk]) endif TheResponse=[TRANSID:]+PADR(ALLTRIM(TRANSFORM(NewNum)),9,[ ]) ********************************************* ADDPROPERTY(ajaxresponse,'updateresponse',theresponse) oresponse.write(ojson.stringify(ajaxresponse)) oresponse.flush() %>