<% 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) return jsonObject 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_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 USEsafe([Bill]) 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+'Bill.dbf'+[") order by vtr desc, ptransid desc into cursor xcursor] &tsql m_transid=ptransid ncntr=vtr Select [Bill] Do While .T. m_transid=Padr(Alltrim(Transform(ncntr)),9,[ ]) =Seek(m_transid,[Bill],[transid]) If Eof([Bill]) 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,[Bill],[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_date',m_date) AddProperty(ajaxresponse,'m_amount',m_amount) Replace Date With Ctod(m_date), modified With Datetime(), vendor With m_vendorid Set Date BRITISH AddProperty(ajaxresponse,'cols',headerText) tcounter=0 newitemIDSadded=0 newitemNAMESadded=0 tsql=[select top 1 * from (']+dbclocation+'BillData'+[') 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) trate=.Get(7) tdisc=.Get(8) ttotal=Val(Transform(tqty))*Val(Transform(trate)) tbatch=.Get(10) texpiry=.Get(11) tbonus=.Get(12) tmodified=.Get(14) gsql='set ' 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 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]) *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, ; sale With Val(tsaleintax), tax1 With Val(ttaxprate), qty With Val(tqty), rate With Val(trate), ; discount With Val(tdisc), Total With ttotal, Batch With tbatch, bonus With Val(tbonus), ; modified With Ctot(THEMODIFIEDSTR), 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() respmsg=respmsg+Transform(Lineno())+[ adding data record, yet to set expiry and modified FROM ]+THEMODIFIEDSTR USEsafe('BillData') Set Order To transid =Seek(m_transid,[BillData],[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,'MODIFIEDstr',THEMODIFIEDSTR) AddProperty(ajaxresponse,'MODIFIED',MODIFIEDSTR) AddProperty(ajaxresponse,'newids',newitemIDSadded) AddProperty(ajaxresponse,'newnames',newitemNAMESadded) ********************************************* Select [Bill] newnum=m_transid updatetransactions([Bill], newnum) If Not Empty(bk1loc) Use (bk1loc+[Bill]) In Select([Billbk]) Alias [Billbk] Again Use (bk1loc+[BillData]) In Select([BillDatabk]) Alias [BillDatabk] Again Use (bk1loc+[acctran]) In Select([acctranbk]) Alias [acctranbk] Again Use (bk1loc+[stock]) In Select([stockbk]) Alias [stockbk] Select [Billbk] Set Order To transid =Seek(newnum) Delete While transid=newnum Select [Bill] Set Order To transid =Seek(newnum) Do While Not Eof() And transid=newnum Scatter Name dtbk Select [Billbk] Append Blank Gather Name dtbk Select [Bill] Skip Enddo Use In Select([Billbk]) Select [BillDatabk] Set Order To transid =Seek(newnum) Delete While transid=newnum Select [BillData] Set Order To transid =Seek(newnum) Do While Not Eof() And transid=newnum Scatter Name dtbk Select [BillDatabk] Append Blank Gather Name dtbk Select [BillData] Skip Enddo Use In Select([BillDatabk]) Select [acctranbk] Set Order To transid =Seek(newnum+[Bill]) Delete While transid=newnum And vtype=[Bill] Select [acctran] Set Order To transid =Seek(newnum+[Bill]) Do While Not Eof() And transid=newnum And vtype=[Bill] 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+[Bill]) Delete While transid=newnum And vtype=[Bill] Select [stock] Set Order To transid =Seek(newnum+[Bill]) Do While Not Eof() And transid=newnum And vtype=[Bill] 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+[Bill]) In Select([Billbk]) Alias [Billbk] Again Use (bk2loc+[BillData]) In Select([BillDatabk]) Alias [BillDatabk] Again Use (bk2loc+[acctran]) In Select([acctranbk]) Alias [acctranbk] Again Use (bk2loc+[stock]) In Select([stockbk]) Alias [stockbk] Again Select [Billbk] Set Order To transid =Seek(newnum) Delete While transid=newnum Select [Bill] Set Order To transid =Seek(newnum) Do While Not Eof() And transid=newnum Scatter Name dtbk Select [Billbk] Append Blank Gather Name dtbk Select [Bill] Skip Enddo Use In Select([Billbk]) Select [BillDatabk] Set Order To transid =Seek(newnum) Delete While transid=newnum Select [BillData] Set Order To transid =Seek(newnum) Do While Not Eof() And transid=newnum Scatter Name dtbk Select [BillDatabk] Append Blank Gather Name dtbk Select [BillData] Skip Enddo Use In Select([BillDatabk]) Select [acctranbk] Set Order To transid =Seek(newnum+[Bill]) Delete While transid=newnum And vtype=[Bill] Select [acctran] Set Order To transid =Seek(newnum+[Bill]) Do While Not Eof() And transid=newnum And vtype=[Bill] 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+[Bill]) Delete While transid=newnum And vtype=[Bill] Select [stock] Set Order To transid =Seek(newnum+[Bill]) Do While Not Eof() And transid=newnum And vtype=[Bill] 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() %>