// Copyright (c) 1993-2005 "Компания БЭСТ". Все права сохраняются.

#include "s_public.ch"
#include "inkey.ch"

// Изменение 3.30
#DEFINE DEF_PRINT {PRN0_TNAME, PRN0_TDEVICE, PRN0_TVIEW,PRN0_TNAST,PRN0_TFILE,PRN0_TERROR,PRN0_TCOPY,PRN0_TCODE,PRN0_TSHAB,PRN0_TWIN}
#DEFINE DEF_INIT(aInit)  (PRN0_TNAME := aInit[1], PRN0_TDEVICE := aInit[2], PRN0_TVIEW := aInit[3],PRN0_TNAST := aInit[4],PRN0_TFILE := aInit[5],PRN0_TERROR := aInit[6],PRN0_TCOPY := aInit[7],PRN0_TCODE := IF(LEN(aInit) > 8 .AND. ValType(aInit[8]) = 'A',aInit[8],PRN0_TCODE),PRN0_TSHAB := IF(LEN(aInit) > 8 .AND. ValType(aInit[9]) = "L",aInit[9],PRN0_TSHAB),PRN0_TWIN :=IF(LEN(aInit) > 9 .AND. ValType(aInit[10]) = 'L',aInit[10],PRN0_TWIN))


STATIC nDemo_Ver:= 0, lSetTerminal := .F.

FUNCTION IdentUser(cIdent)
// Возвращает или выставляет уникальный код пользователя
IF cIdent = NIL
  IF EMPTY((cIdent := GETENV('TERMINAL')))
     cIdent := PADL('', 8, '0' )+PADL(LTRIM( UPPER(NNETSTAID())), 12, '0' )
  ELSE
     cIdent := PADL(cIdent,20,'0')
     lSetTerminal := .T.
  ENDIF
ELSE
  IDENT_USER := cIdent
ENDIF
RETURN cIdent

      // Изменение 3.30
FUNCTION IsLegal(cPrintCode)
  // cPrintCode - код принтера
  PLOCAL cPath := '',i
  PLOCAL aTPrint, n
  IF(ValType(cPrintCode) != "C",cPrintCode := "1",NIL)

  EVAL( {|| i:=GetAnalize( GlobalTask ), EVAL({||nDemo_Ver:=i[1]}) } )
  PUBLIC IDENT_USER := IdentUser()


  PUBLIC ST_FILE:=LEFT(IDENT_USER,5)+SUBSTR(IDENT_USER,9,5) //Считаем сетевой, чтобы на одной станции работать

  BEGIN SEQUENCE
  IF FILE(_PRO_PATH+'STATION.PRO')
    USE ( _PRO_PATH+'STATION.PRO' ) ALIAS STATION NEW SHARED VIA 'DBFCDX'

    IF !USED()
      MyExit()
    ENDIF
    // Ишем принтер по умолчанию
    LOCATE FOR STATION->NUM_DISK+STATION->NUM_NET==IDENT_USER .AND. ;
               STATION->NUM_PRINT == ' 0'
    IF !FOUND() .AND. !lSetTerminal
      LOCATE FOR IDENT_USER = STATION->NUM_DISK .AND. STATION->NUM_PRINT == ' 0'
      IF FOUND()
        IdentUser(STATION->NUM_DISK+STATION->NUM_NET)
      ENDIF
    ENDIF
    IF !FOUND()
      Registr( LEFT(IDENT_USER,8), RIGHT(IDENT_USER,12) )
    ELSE
      INST_COLOR  := STATION->COLOR
      PRN0_TYPE   := ALLTRIM(STATION->TYPE)
      PRN0_WIDTH  := STATION->WIDTH
      PRN0_PAPER  := STATION->PAPER
      PRN0_STR    := STATION->STR
      PRN0_PROBROS:= STATION->PROBROS
      PRN0_PAUSE  := STATION->PAUSE
      PRN0_DEVICE := STATION->DEVICE
      PRN0_PSEVDO := STATION->PSEVDO
      PRN0_E_YES  := STATION->E_YES
      PRN0_E_PATH := ALLTRIM(STATION->E_PATH)
      PRN0_E_FILE := ALLTRIM(STATION->E_FILE)
      PRN0_WTYPE  := ALLTRIM(STATION->WTYPE)
      PRN0_FONT   := ALLTRIM(STATION->FONT)
      PRN0_HEAD   := STATION->PRN_HEAD
      PRN0_NUMSTR := STATION->PRN_NUMSTR
      PRN0_BEGSTR := STATION->PRN_BEGSTR
      PRN0_LCODE := STATION->PRN_LCODE
      PRN0_BCODE := STATION->PRN_BCODE
      PRN0_ECODE := STATION->PRN_ECODE
      // Изменение 3.60
      IF FieldPos("PRN_PTYPE") != 0
        PRN0_PTYPE   := STATION->PRN_PTYPE
      ENDIF
      IF STATION->(FieldPos("PRN_LSUM")) != 0
        PRN0_LSUM := STATION->PRN_LSUM
      ENDIF
      IF STATION->(FieldPos("PRN_TPRINT")) != 0
         IF ValType(STATION->PRN_TPRINT) = "A"
           aTPrint := STATION->PRN_TPRINT
         ENDIF
         IF (n := ASCAN(aTPrint,{|x| x[1] = cPrintCode })) = 0
            DEF_INIT(DEF_PRINT)
         ELSE
            DEF_INIT(aTPrint[n,2])
         ENDIF
      ENDIF
      IF FieldPos("PRN_WIN") != 0
        PRN0_WIN   := STATION->PRN_WIN
      ENDIF
      IF FieldPos("PRN_ORINT") != 0
        PRN0_ORINT   := STATION->PRN_ORINT
      ENDIF
      IF FieldPos("PRN_OTSTUP") != 0
        PRN0_OTSTUP   := STATION->PRN_OTSTUP
      ENDIF
      IF FieldPos("PRN_BEST_P") != 0
        PRN0_BEST_PRINT   := ALLTRIM(STATION->PRN_BEST_P)
      ENDIF
      // Alex2
      IF FieldPos("PRN_CDTIME") != 0
        CURR_DTIME    := STATION->PRN_CDTIME
      ENDIF
      IF FieldPos("PRN_CFTIME") != 0
        CURR_FTIME    := STATION->PRN_CFTIME
      ENDIF
      //Тип WP печати
      IF FieldPos("PRN_WPTYPE") != 0
//        PRN0_WIN_PRINT := STATION->PRN_WPTYPE
          PRN0_WIN_PRINT := STATION->PRN_WPTYPE
      ENDIF
      //Имя принтера WP
      IF FieldPos("PRN_WPNAME") != 0
        PRN0_WPRINTER := STATION->PRN_WPNAME
      ENDIF
      cPath := ALLTRIM( STATION->LBL_PATH )
    ENDIF
    // DIM88
    InitStation()
    STATION->( dbCloseArea() )
  ENDIF
  END SEQUECE
RETURN( cPath )

      // Изменение 3.30
STATIC FUNCTION Registr( cCode1, cCode2, cPrintCode )
  // cPrintCode - код принтера
  PLOCAL nHandle,nHandle1
IF FILE(_PRO_PATH+'STATION.PRO').AND.!('ANALIZ.EXE' $ Upper(ExeName()))
  IF ALERT(" Рабочее место не зарегистрировано ",{" Регистрация "," Выход "})==2.OR.;
    LASTKEY()=K_ESC
    MyExit()
  ENDIF
  IF(ValType(cPrintCode) != "C",cPrintCode := "1",NIL)
  IF SELECT('STATION') > 0
    PutNetAdd({ cCode1, cCode2, ' 0', '', INST_COLOR,;
                PRN0_TYPE,    PRN0_WIDTH,    PRN0_PAPER,    PRN0_STR,;
                PRN0_PROBROS, PRN0_PAUSE,    PRN0_DEVICE,   PRN0_PSEVDO,;
                PRN0_E_YES,   PRN0_E_PATH,   PRN0_E_FILE,   PRN0_WTYPE, ;
                PRN0_FONT,    '' } )
    IF FieldPos("PRN_LSUM") != 0 .AND. RecLock()
       STATION->PRN_LSUM := PRN0_LSUM
       dbUnLock()
    ENDIF
    IF FieldPos("PRN_TPRINT") != 0 .AND. RecLock()
       STATION->PRN_TPRINT := {{cPrintCode,DEF_PRINT}}
       dbUnLock()
    ENDIF
      // Изменение 3.60
    IF FieldPos("PRN_PTYPE") != 0 .AND. RecLock()
       STATION->PRN_PTYPE := PRN0_PTYPE
       dbUnLock()
    ENDIF
    IF FieldPos("PRN_WIN") != 0 .AND. RecLock()
       STATION->PRN_WIN := PRN0_WIN
       dbUnLock()
    ENDIF
    IF FieldPos("PRN_WPTYPE") != 0 .AND. RecLock()
       STATION->PRN_WPTYPE := PRN0_WIN_PRINT
       dbUnLock()
    ENDIF
    IF FieldPos("PRN_WPNAME") != 0 .AND. RecLock()
       STATION->PRN_WPNAME := PRN0_WPRINTER
       dbUnLock()
    ENDIF
  ENDIF

  #ifdef KPM_EXE
  #else
     ALERT("Регистрация произведена",{' ENTER '})
  #endif

ENDIF
RETURN (NIL)

PROCEDURE SavePathStation( cPath )

IF FILE(_PRO_PATH+'STATION.PRO')
  USE (_PRO_PATH+'STATION.PRO') ALIAS STATION NEW SHARED VIA 'DBFCDX'

  IF USED()
    // Ишем принтер по умолчанию
    LOCATE FOR STATION->NUM_DISK+STATION->NUM_NET==IDENT_USER .AND. ;
               STATION->NUM_PRINT == ' 0'
    IF FOUND()
      IF RecLock( .T.  )
        STATION->LBL_PATH := cPath
        dbUnlock()
      ENDIF
    ENDIF
  ENDIF
  STATION->( dbCloseArea() )
ENDIF
RETURN

      // Изменение 3.30
PROCEDURE SaveStation(aSave,cPrintCode)
  // Если указан aSave, то записываем только то что указано
  // aSave {{имя поля,значение},{}......}
  // cPrintCode - код принтера
PLOCAL aTPrint := {}, n
IF FILE(_PRO_PATH+'STATION.PRO')
  USE ( _PRO_PATH+'STATION.PRO' ) ALIAS STATION NEW SHARED VIA 'DBFCDX'
  IF(ValType(cPrintCode) != "C",cPrintCode := "1",NIL)
  IF USED()
    // Ишем принтер по умолчанию
    LOCATE FOR STATION->NUM_DISK+STATION->NUM_NET==IDENT_USER .AND. ;
               STATION->NUM_PRINT == ' 0'
    IF !FOUND()
      Registr( LEFT(IDENT_USER,8), RIGHT(IDENT_USER,12) )
    ELSEIF ValType(aSave) != "A"
      PutNetRepl({ LEFT(IDENT_USER,8), RIGHT(IDENT_USER,12), ' 0', '', INST_COLOR,;
                 PRN0_TYPE,    PRN0_WIDTH,    PRN0_PAPER,    PRN0_STR,;
                 PRN0_PROBROS, PRN0_PAUSE,    PRN0_DEVICE,   PRN0_PSEVDO,;
                 PRN0_E_YES,   PRN0_E_PATH,   PRN0_E_FILE,   PRN0_WTYPE,;
                 PRN0_FONT,STATION->LBL_PATH,STATION->CODE,PRN0_HEAD, ;
                 PRN0_NUMSTR, PRN0_BEGSTR } )
      IF FieldPos("PRN_LSUM") != 0 .AND. RecLock()
         STATION->PRN_LSUM := PRN0_LSUM
         dbUnLock()
      ENDIF
      IF FieldPos("PRN_TPRINT") != 0 .AND. RecLock()
         IF ValType(STATION->PRN_TPRINT) = "A"
           aTPrint := STATION->PRN_TPRINT
         ENDIF
         IF (n := ASCAN(aTPrint,{|x| x[1] = cPrintCode })) = 0
            AADD(aTPrint,NIL)
            n := LEN(aTPrint)
         ENDIF
         aTPrint[n] := {cPrintCode,DEF_PRINT}
         STATION->PRN_TPRINT := aTPrint
         dbUnLock()
      ENDIF
      // Изменение 3.60
      IF FieldPos("PRN_PTYPE") != 0 .AND. RecLock()
         STATION->PRN_PTYPE := PRN0_PTYPE
         dbUnLock()
      ENDIF
      IF FieldPos("PRN_WIN") != 0 .AND. RecLock()
         STATION->PRN_WIN := PRN0_WIN
         dbUnLock()
      ENDIF
      IF FieldPos("PRN_ORINT") != 0 .AND. RecLock()
         STATION->PRN_ORINT := PRN0_ORINT
         dbUnLock()
      ENDIF
      IF FieldPos("PRN_OTSTUP") != 0 .AND. RecLock()
         STATION->PRN_OTSTUP := PRN0_OTSTUP
         dbUnLock()
      ENDIF
      IF FieldPos("PRN_BEST_P") != 0 .AND. RecLock()
         STATION->PRN_BEST_P := PRN0_BEST_PRINT
         dbUnLock()
      ENDIF
      // Alex2
      IF FieldPos("PRN_CDTIME") != 0 .AND. RecLock()
         STATION->PRN_CDTIME  := CURR_DTIME
         DBUNLOCK()
      ENDIF
      IF FieldPos("PRN_CFTIME") != 0 .AND. RecLock()
         STATION->PRN_CFTIME  := CURR_FTIME
         DBUNLOCK()
      ENDIF
      IF FieldPos("PRN_WPTYPE") != 0 .AND. RecLock()
         STATION->PRN_WPTYPE  := PRN0_WIN_PRINT
         DBUNLOCK()
      ENDIF
      IF FieldPos("PRN_WPNAME") != 0 .AND. RecLock()
         STATION->PRN_WPNAME  := PRN0_WPRINTER
         DBUNLOCK()
      ENDIF
    ELSEIF RecLock()
      AEVAL(aSave,{|x| EVAL(FieldBlock(x[1]),x[2]) })
      dbUnLock()
    ENDIF
    STATION->( dbCloseArea() )
  ENDIF
ENDIF
RETURN

FUNCTION IsDemo()
RETURN(.T.)

FUNCTION DemoVer()
RETURN .F.

FUNCTION MyExit()
WSELECT(0)
SET COLOR TO
SETCURSOR(1)
CLS
QUIT
RETURN(NIL)


  Function PR4_CheckIt()
  Return .t.

  Function PR4_IsUpgrade()
  Return .t.

  Function PR4_IsValid()
  Return .t.

  Function PR4_DateVer(x)
  Return Date()

  Function PR4_YearSer()
  Return(4)

  Function PR4_MonthSer()
  Return(10)

  Function PR4_YearB4()
  Return(4)

  Function PR4_MonthB4()
  Return(10)


  Function ShowKeyID()
  Return('Отладка')

  Function TypeKey()
  Return 2

  Function GetID()
  Return (PadR("", 10, "0"))

  FUNCTION MyGetID()
  Return(0)

  Function GetAnalize(cTask, lShow, nNewKey, aNewArm, nNewUnit  )
  RETURN ({2,.T.,777,1})

  Function Finish()
  Return NIL

  Function RainConnect()
  Return NIL

  Function RainInfo( nMax,nCount )
     nMax:=255
     nCount:=1
