2014年11月5日 星期三

[AP] 透過web service,由外界來開立notes文件

'//透過web service,由其他平台來開立notes文件
'// URL: http://cmos27/ap/office/ap410.nsf/ap410?wsdl
server name: cmos27
path:ap/office/ap410.nsf
字串: ap410?wsdl
若是要call 別人的service,就必須到Web Service Client建立連線資訊
在ACL的部份,需設定Anonymous為Unspecified,Depositor
'//

%REM
'//讓MES自動開立NG單 (IP),狀態為preparing
1. B_PNR(para1,para2,….): Build
2. 讓SPC建立新的PNR文件
%END REM
Dim Session As notessession
Dim sourcedb As notesdatabase
Dim doc As notesdocument

Public Class ReturnData
Public ercode As String
Public ermsg As String
Sub new()
End Sub
End Class

Public Class ReceiptInfo
'請依據實際狀況更新參數後才使用之
Public ALARM_ID As String '1.ALARM ID(多重值)
Public ALARM_other As String '2.ALARM ID other(多重值)
'
Public WaferNo As String '3.Wafer No(多重值)
Public ProblemDesc As String '4.異常情形描述
Public Empno As String '5.工號
Public EmpName As String '6.中文名
Public O_Dept As String '7.發現部門代碼
Public O_Dept_1 As String '8.發現部門
Public Source_Type As String '9.產品版別
Public Fab As String ' '10.Line別 :LIN1/LINE2/LINE3
Public StationID As String '11.站別:BH/BS/BB...
Public Deviation_ID As String  '12.異常代碼
Public Deviation_Name As String  '13.異常中文名稱
Public Deviation As String  '14.異常英文名稱
Public Cus As String   '15.Cust 代碼
Public Cus_1 As String '16.Cust名稱
Public LotNo As String '17.批號
Public XLotNo As String '18.客戶批號
Public PartNo As String  '19.型號
Public TQty As Double '20.進貨數量
Public DefQty As Double '21.當站整批數量
Public O_Station As String '22.站別:8728
Public MC_NO As String '23.機台號碼
Public DefQty_1 As Double '24.不良數量

Sub new 
End Sub
End Class
Public Class PNRObj
Sub new()
Set session=New notessession
End Sub
Private Function GetDbName()As String
GetDbName=session.CurrentDatabase.Title
End Function
Private Sub throwFault (fault As WS_FAULT, faultText As String)
Call fault.setFault(True)
Call fault.setFaultString(faultText)
'** do any other error logging things here...
Print faultText
End Sub

Private Sub GetEmpData(SourceDoc As NotesDocument)
Dim SourceDB As NotesDatabase
Dim PersonDB As NotesDatabase

Dim PersonDoc As NotesDocument
Dim PersonView As NotesView
Dim ArriveDate As Variant
Dim strEmpNo As String

strEmpNo = Format(SourceDoc.EmpNo(0), ">")

If strEmpNo = "" Then          
Exit Sub
End If

Set SourceDB = SourceDoc.ParentDatabase
Set PersonDB = New NotesDatabase(SourceDB.Server, "ap\sys\sys0001.nsf")
Set PersonView = PersonDB.GetView("Empnoview")
Set PersonDoc = PersonView.GetDocumentByKey(strEmpNo)

If PersonDoc Is Nothing Then
Messagebox "Error :Server index not found", 16, "Input Validation Error"
Else
SourceDoc.Authority = PersonDoc.Level
SourceDoc.EmpName = PersonDoc.Cname
SourceDoc.EmpEname = PersonDoc.tFullName
SourceDoc.EmpSex = PersonDoc.KSex
SourceDoc.DeptNo = PersonDoc.Dept_No
SourceDoc.DeptName = PersonDoc.Divison
SourceDoc.Company  = PersonDoc.Company
SourceDoc.EmpShift  = PersonDoc.Shift
If PersonDoc.kDir_Code(0) = "I" Then
SourceDoc.IDL = "間接"
Else
SourceDoc.IDL = "直接"
End If     
SourceDoc.Job_Title = PersonDoc.Title
SourceDoc.ID_NO = PersonDoc.id
SourceDoc.Shift_Desc = PersonDoc.Shift_Desc
End If
End Sub

Private Sub GetSerialNumber(NewDoc As NotesDocument)
'S/N
Dim PNRDB As NotesDatabase
Dim NumberDoc As NotesDocument
Set PNRDB=NewDoc.ParentDatabase
Set TempDoc = New NotesDocument(PNRDB)

yymmdd = Format(TempDoc.Created, "yymmdd")
KindHead$ = "PNR" + yymmdd
Set PNRView=PNRDB.GetView("View-AutoNumber")
Set NumberDoc=PNRView.GetDocumentByKey(KindHead$)
If NumberDoc Is Nothing Then
Set NumberDoc=New NotesDocument(PNRDB)
NumberDoc.Type = "PNR"
NumberDoc.Year=Left(yymmdd,2)
NumberDoc.Month=Mid(yymmdd,3,2)
NumberDoc.Day=Right(yymmdd,2)
Call NumberDoc.ReplaceItemValue("Author","*")
Set AuthorItem=NumberDoc.Getfirstitem("Author")
AuthorItem.Isauthors=True
NumberDoc.Form="Form-AutoNumber"
Call NumberDoc.ComputeWithForm(True, True)
SerianNumber=0
Else
SerialNumber=NumberDoc.SerialNumber(0)
End If
SerialNumber=SerialNumber+1
NumberDoc.SerialNumber=SerialNumber
Call NumberDoc.Save(True,True)

NewDoc.SerialNumber = "O" + yymmdd + Format(SerialNumber,"00")
End Sub
'***************************
'*1.Build all new PNR*
'***************************
Function B_PNR(SPCdata  As ReceiptInfo,returnFault As WS_FAULT)  As ReturnData

On Error Goto processError
Set sourcedb=session.CurrentDatabase
Set doc=New notesdocument(sourcedb)
doc.form="Form-Main"
doc.ALARM_ID=SPCdata.ALARM_ID
doc.ALARM_other=SPCdata.ALARM_other
doc.WaferNo=SPCdata.WaferNo
doc.ProblemDesc=SPCData.ProblemDesc '異常情形
doc.Empno=SPCdata.EmpNo
doc.EmpName=SPCdata.EmpName
'Call GetEmpData(doc)
doc.O_Dept=SPCdata.O_Dept
doc.O_Dept_1=SPCdata.O_Dept_1
doc.Source_Type=SPCData.Source_Type '產品版別
doc.Fab=SPCData.Fab 'LINE1/LINE2...
doc.StationID=SPCData.StationID 'BS..
doc.Deviation_ID=SPCdata.Deviation_ID
doc.Deviation_Name=SPCdata.Deviation_Name
doc.Deviation=SPCdata.Deviation
doc.Cus =SPCData.Cus   'Cust 代碼
doc.Cus_1=SPCData.Cus_1  'Cust名稱
doc.LotNo=SPCdata.LotNo
doc.XLotNo=SPCData.XLotNo '客戶批號
doc.PartNo=SPCData.PartNo '型號
doc.TQty=SPCData.TQty '進貨數量
doc.DefQty=SPCData.DefQty '當站整批數量
doc.O_Station=SPCData.O_Station '站別 8728
doc.MC_NO=SPCData.MC_NO '機台號碼
doc.DefQty_1=SPCData.DefQty_1 '不良數
Print doc.fab(0)+"-"+doc.O_station(0)
Call GetChkData(doc.fab(0)+"-"+doc.O_station(0),doc)
doc.ImportFrom="SPC"
doc.From=doc.ChkEName
'====================================================
Dim UserDoc As NotesDocument
Set UserDoc =sourcedb.createDocument()
Call UserDoc.replaceItemValue("Form", "Form-SwitchUserName")
Call UserDoc.replaceItemValue("UserName", doc.getItemValue("ChkEname"))
Call UserDoc.computeWithForm(True, True)
Dim AuthorItem As notesitem
Set AuthorItem =  doc.replaceItemValue( "Authors", UserDoc.getItemValue("CanonicalizeUserName") )
AuthorItem.IsAuthors=True
'====================================================
If bolGetSerialNo(Doc) = 0 Then
Exit Function
End If
Call doc.ComputeWithForm(True,False)
Call doc.save(True,False)
Set B_PNR=New ReturnData
B_PNR.ercode="S"
B_PNR.ermsg=doc.SerialNumber(0)
Exit Function

processError:
Call throwFault(returnFault, "Error code("&Cstr(Err) &") At Line " &Cstr(Erl)&": " & Error)

Exit Function

End Function
Private Function bolGetSerialNo(SourceDoc As NotesDocument) As Boolean
Dim db As NotesDatabase 
Dim docTmp As NotesDocument
Dim docQuery As NotesDocument
Dim docQuery_BK As NotesDocument
Dim agTask As NotesAgent 
Dim view As Notesview

Set db = SourceDoc.ParentDatabase 
Set view = db.getview("ByNoteId")
Set docQuery = New NotesDocument (db)
docQuery.Form = "QueryForm"
docQuery.O_Dept = SourceDoc.O_Dept

Call docQuery.Save(True,False)

Set agTask = db.GetAgent ("T1. GetSerialNumber")'
If agTask.RunonServer (docQuery.NoteID  ) = 0 Then
Call view.refresh
Set docQuery_BK = view.getdocumentbykey(docQuery.UniversalID ,True)
If Not docQuery_BK Is Nothing Then
If docQuery_BK.SerialNumber(0) = "" Then
Messagebox "抓取Serial Number 失敗 !!",MB_OK+MB_ICONSTOP,db.Title
bolGetSerialNo = False
Else
SourceDoc.SerialNumber = docQuery_BK.SerialNumber
bolGetSerialNo = True
End If
Else
Messagebox "抓取Serial Number 失敗 !!",MB_OK+MB_ICONSTOP,db.Title
bolGetSerialNo = False
End If
End If
End Function
Private Function GetChkData(key As String,doc As NotesDocument)
'找不到精確的改找模糊的
Set db=doc.ParentDatabase
Set view=db.getview("View-SystemSetup22")
Print view.name
Set profileDoc=view.getdocumentbykey(key,True)
If  profileDoc Is Nothing Then
key1= doc.fab(0) + "-*"
Set profileDoc=view.getdocumentbykey(key1,True)
If  profileDoc Is Nothing Then
Call Doc.replaceItemValue("ChkNo", "")
Call Doc.replaceItemValue("ChkName","")
Call Doc.replaceItemValue("ChkEname", "")
Else
Call Doc.replaceItemValue("ChkNo", profileDoc.getItemValue("EngNo"))
Call Doc.replaceItemValue("ChkName", profileDoc.getItemValue("EngCName"))
Call Doc.replaceItemValue("ChkEname", profileDoc.getItemValue("EngEName"))
End If
Else
Call Doc.replaceItemValue("ChkNo", profileDoc.getItemValue("EngNo"))
Call Doc.replaceItemValue("ChkName", profileDoc.getItemValue("EngCName"))
Call Doc.replaceItemValue("ChkEname", profileDoc.getItemValue("EngEName"))
End If
End Function

End Class

沒有留言:

張貼留言