#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <EditConstants.au3>
#include <ComboConstants.au3>
#include <Word.au3>
#include <TabConstants.au3>
#include <File.au3>
#include <WinAPIEx.au3>;Yashied, http://autoit-script.ru/index.php/topic,47.0.html
Opt('MustDeclareVars', 1)
Opt('TrayMenuMode', 1)
Global $sIniDir = @ScriptDir & '\polic', $sIni = $sIniDir & '\' & @YEAR & '\' & @MON & '.ini', _
$hGui, $nCity, $nStreet, $aInfo, $fNotSave, $nTab, $iX, $vY = 95, $vStyle = -1, _
$aInput[8] = [7, 'Фамилия', 'Имя', 'Отчество', 'Дом', 'Корпус', 'Квартира'], _
$aButton[6] = [5, 'Принять', 'Просмотр', 'Печать', 'Сохранить', 'Сброс'], _
$aCombo[4] = [3, 'Год', 'Месяц', 'Страхователь'], _
$aInput_1[4] = [3, 'Дата полиса', 'ФИО страхователя', 'Адрес страхователя'], _
$aButton_1[3] = [2, 'Просмотр', 'Печать'], $fYear, $sTitle = 'ООО СК «Благовест»', $sWordExt
If WinExists('{[/@$@\]}' & $sTitle) Then Exit
AutoItWinSetTitle('{[/@$@\]}' & $sTitle)
If Not FileExists($sIniDir & '\' & @YEAR) _
Or Not StringInStr(FileGetAttrib($sIniDir & '\' & @YEAR), 'd') Then
DirCreate($sIniDir & '\' & @YEAR)
EndIf
$hGui = GUICreate($sTitle, 360, 290)
$nTab = GUICtrlCreateTab(2, 15, 356, 273)
GUICtrlSetCursor(-1, 0)
GUICtrlCreateTabItem('Новый полис')
GUICtrlCreateInput(_ToDayFormatRu(), 100, 65, 160, 20, BitOR($ES_CENTER, $ES_READONLY))
GUICtrlCreateLabel('Дата выдачи полиса', 100, 50, 160, 15, $SS_CENTER)
GUICtrlCreateLabel('А д р е с р е г и с т р а ц и и', 10, 140, 340, 15, $SS_CENTER)
For $i = $nTab + 2 To $nTab + 4
GUICtrlSetBkColor($i, 0xFFFFFF)
Next
For $i = 1 To $aInput[0] - 1
GUICtrlCreateLabel($aInput[$i], 10 + $iX * 115, $vY, 110, 15, $SS_CENTER)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$aInput[$i] = GUICtrlCreateInput('', 10 + $iX * 115, $vY + 15, 110, 20, $vStyle)
$iX += 1
If $iX = 3 Then
$iX = 0
$vY += 90
$vStyle = $ES_CENTER
If Not $aInput[$aInput[0]] Then
$aInput[$aInput[0]] = GUICtrlCreateInput('', 240, 155, 110, 20)
EndIf
EndIf
Next
$iX = 0
$vY = ''
$vStyle = ''
$nCity = GUICtrlCreateCombo('', 10, 155, 110, 21, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, 'Москва|Зеленоград', 'Москва')
$nStreet = GUICtrlCreateCombo('', 125, 155, 110, 20)
GUICtrlSetData(-1, 'Проспект|Улица|Переулок|Проезд|Шоссе', 'Улица')
For $i = 1 To $aButton[0]
$aButton[$i] = GUICtrlCreateButton($aButton[$i], 10 + ($i - 1) * 70, 240, 60, 30)
GUICtrlSetCursor(-1, 0)
Next
For $i = 2 To $aButton[0] - 1
GUICtrlSetState($aButton[$i], $GUI_DISABLE)
Next
GUICtrlCreateTabItem('Архив полисов')
$iX = 75
$vY = 70
For $i = 1 To $aCombo[0]
If $i > 2 Then
$vY = 190
EndIf
GUICtrlCreateLabel($aCombo[$i], 10 + ($i - 1) * $iX, 50, $vY, 15, $SS_CENTER)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$aCombo[$i] = GUICtrlCreateCombo('', 10 + ($i - 1) * $iX, 65, $vY, 20, $CBS_DROPDOWNLIST)
GUICtrlSetState(-1, $GUI_DISABLE)
Next
GUICtrlSendMsg($aCombo[$aCombo[0]], $CB_SETDROPPEDWIDTH, 250, 0)
$iX = 0
$vY = ''
For $i = 1 To $aInput_1[0]
GUICtrlCreateLabel($aInput_1[$i], 20, 100 + ($i - 1) * 40, 320, 15, $SS_CENTER)
GUICtrlSetBkColor(-1, 0xFFFFFF)
$aInput_1[$i] = GUICtrlCreateInput('', 20, 115 + ($i - 1) * 40, 320, 20, _
BitOR($ES_CENTER, $ES_MULTILINE))
GUICtrlSetFont($aInput_1[$i], 10)
GUICtrlSetBkColor(-1, 0xFFFFFF)
Next
GUICtrlSetPos($aInput_1[$aInput_1[0]], -1, -1, -1, 40)
For $i = 1 To $aButton_1[0]
$aButton_1[$i] = GUICtrlCreateButton($aButton_1[$i], 60 + ($i - 1) * 140, 246, 100, 30)
GUICtrlSetState(-1, $GUI_DISABLE)
GUICtrlSetCursor(-1, 0)
Next
GUICtrlCreateTabItem('')
GUISetState()
_WinAPI_EmptyWorkingSet()
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
If $fNotSave Then
If MsgBox(36, $sTitle, 'Имеются несохраненные данные.' & @LF & @LF & _
@TAB & 'Сохранить их?', 0, $hGui) = 6 Then _Save($aInfo)
EndIf
DirRemove(@ScriptDir & '\template\_temp', 1)
Exit
Case $nTab
$iX = GUICtrlSendMsg($nTab, $TCM_GETCURSEL, 0, 0)
If $iX Then
If Not $fYear Then
$vY = _GetYearToCombo()
If @error Then ContinueLoop
GUICtrlSetData($aCombo[1], $vY)
GUICtrlSetState($aCombo[1], $GUI_ENABLE)
$fYear = True
$vY = ''
EndIf
EndIf
$iX = 0
Case $aButton_1[1]
Dim $vStyle[5] = [4]
For $i = 1 To $aInput_1[0]
$vY = 1
$vStyle[$i] = GUICtrlRead($aInput_1[$i])
If Not $vStyle[$i] Then ExitLoop
$vY = 0
Next
If $vY Then
$vStyle = 0
ContinueLoop
EndIf
$vY = GUICtrlRead($aCombo[3])
$vY = StringRegExpReplace($vY, '^(.*?)\h\d.*', '$1')
If @extended <> 1 Then
$vStyle = 0
ContinueLoop
EndIf
$vStyle[3] = StringReplace($vStyle[3], @CRLF, ' ')
$vStyle[4] = $vY
$vY = ''
If Not $sWordExt Then
_GetWordExt($sWordExt)
If @error Then
$vStyle = 0
MsgBox(16, $sTitle, 'Ошибка определения версии Word', 0, $hGui)
ContinueLoop
EndIf
EndIf
_ReplaceWord($vStyle)
$vStyle = 0
If @error Then
MsgBox(16, $sTitle, 'Ошибка экспорта в Word', 0, $hGui)
Else
_Preview()
GUICtrlSetState($aButton_1[2], $GUI_ENABLE)
EndIf
_WinAPI_EmptyWorkingSet()
Case $aCombo[1]
$vY = GUICtrlRead($aCombo[1])
If Not $vY Then ContinueLoop
$vStyle = _GetMonthToCombo($vY)
If @error Then ContinueLoop
For $i = 2 To $aCombo[0]
GUICtrlSetData($aCombo[$i], '')
Next
GUICtrlSetData($aCombo[2], $vStyle)
GUICtrlSetState($aCombo[2], $GUI_ENABLE)
GUICtrlSetState($aCombo[3], $GUI_DISABLE)
For $i = 1 To $aInput_1[0]
GUICtrlSetData($aInput_1[$i], '')
Next
For $i = 1 To $aButton_1[0]
GUICtrlSetState($aButton_1[$i], $GUI_DISABLE)
Next
$vY = ''
$vStyle = ''
Case $aCombo[2]
$vY = GUICtrlRead($aCombo[2])
If Not $vY Then ContinueLoop
$vStyle = _GetInfoToCombo($vY)
If @error Then ContinueLoop
GUICtrlSetData($aCombo[3], '')
GUICtrlSetData($aCombo[3], $vStyle)
GUICtrlSetState($aCombo[3], $GUI_ENABLE)
For $i = 1 To $aInput_1[0]
GUICtrlSetData($aInput_1[$i], '')
Next
For $i = 1 To $aButton_1[0]
GUICtrlSetState($aButton_1[$i], $GUI_DISABLE)
Next
$vY = ''
$vStyle = ''
Case $aCombo[3]
$vY = GUICtrlRead($aCombo[3])
If Not $vY Then ContinueLoop
$vStyle = _GetInfoToInput($vY)
If @error Then ContinueLoop
For $i = 1 To $aInput_1[0]
GUICtrlSetStyle($aInput_1[$i], BitOR($ES_CENTER, $ES_MULTILINE, $ES_READONLY))
GUICtrlSetData($aInput_1[$i], $vStyle[$i])
Next
GUICtrlSetState($aButton_1[1], $GUI_ENABLE)
$vY = ''
$vStyle = ''
Case $aButton[1]
$aInfo = _GetInfo()
If @error Then
MsgBox(16, $sTitle, $aInfo, 0, $hGui)
ContinueLoop
EndIf
If Not $sWordExt Then
_GetWordExt($sWordExt)
If @error Then
MsgBox(16, $sTitle, 'Ошибка определения версии Word', 0, $hGui)
ContinueLoop
EndIf
EndIf
_ReplaceWord($aInfo)
If @error Then
MsgBox(16, $sTitle, 'Ошибка экспорта в Word', 0, $hGui)
Else
For $i = 2 To $aButton[0] - 1
GUICtrlSetState($aButton[$i], $GUI_ENABLE)
Next
$fNotSave = True
EndIf
_WinAPI_EmptyWorkingSet()
Case $aButton[2]
If Not $sWordExt Then
_GetWordExt($sWordExt)
If @error Then
MsgBox(16, $sTitle, 'Ошибка определения версии Word', 0, $hGui)
ContinueLoop
EndIf
EndIf
_Preview()
Case $aButton[3], $aButton_1[2]
If Not $sWordExt Then
_GetWordExt($sWordExt)
If @error Then
MsgBox(16, $sTitle, 'Ошибка определения версии Word', 0, $hGui)
ContinueLoop
EndIf
EndIf
_Print()
If @error Then
MsgBox(16, $sTitle, 'Ошибка печати полиса', 0, $hGui)
EndIf
_WinAPI_EmptyWorkingSet()
Case $aButton[4]
_Save($aInfo)
If Not @error Then
$fNotSave = False
GUICtrlSetState($aButton[4], $GUI_DISABLE)
EndIf
Case $aButton[5]
For $i = 1 To $aInput[0]
GUICtrlSetData($aInput[$i], '')
Next
For $i = 2 To $aButton[0] - 1
GUICtrlSetState($aButton[$i], $GUI_DISABLE)
Next
$aInfo = 0
$fNotSave = False
EndSwitch
WEnd
Func _GetInfoToInput($s_Key)
Local $s_String, $s_Temp, $s_Value, $a_Result
$s_String = GUICtrlRead($aCombo[1])
If Not $s_String Then Return SetError(1, 0, '')
$s_Temp = GUICtrlRead($aCombo[2])
If Not $s_Temp Then Return SetError(1, 0, '')
$s_Temp = _DateToMonthRu($s_Temp, 1)
If @error Then Return SetError(1, 0, '')
$s_Value = IniRead($sIniDir & '\' & $s_String & '\' & $s_Temp & '.ini', 'Polis', $s_Key, 'Error')
If $s_Value == 'Error' Then SetError(1, 0, '')
$a_Result = StringSplit($s_Value, '|')
If $a_Result[0] <> 3 Then SetError(1, 0, '')
$a_Result[$a_Result[0]] = StringReplace($a_Result[$a_Result[0]], ' дом', @CRLF & 'дом')
Return $a_Result
EndFunc ;==>_GetInfoToInput
Func _GetInfoToCombo($s_Mon)
Local $a_Section, $s_String
$s_Mon = _DateToMonthRu($s_Mon, 1)
If @error Then Return SetError(1, 0, '')
$s_String = GUICtrlRead($aCombo[1])
If Not $s_String Then Return SetError(1, 0, '')
$a_Section = IniReadSection($sIniDir & '\' & $s_String & '\' & $s_Mon & '.ini', 'Polis')
If @error Then Return SetError(1, 0, '')
$s_String = ''
For $i = 1 To $a_Section[0][0]
$s_String &= $a_Section[$i][0] & '|'
Next
If $s_String Then
Return StringTrimRight($s_String, 1)
EndIf
Return SetError(1, 0, '')
EndFunc ;==>_GetInfoToCombo
Func _GetMonthToCombo($s_DirYear)
Local $a_File, $s_String, $v_Temp
$a_File = _FileListToArray($sIniDir & '\' & $s_DirYear, '*.ini', 1)
If @error Then Return SetError(1, 0, '')
For $i = 1 To $a_File[0]
$v_Temp = StringRegExpReplace($a_File[$i], '\..*$', '')
$v_Temp = Int($v_Temp)
If $v_Temp > 0 And $v_Temp < 13 Then
$s_String &= _DateToMonthRu($v_Temp) & '|'
EndIf
Next
If $s_String Then
Return StringTrimRight($s_String, 1)
EndIf
Return SetError(1, 0, '')
EndFunc ;==>_GetMonthToCombo
Func _GetYearToCombo()
Local $a_Dir, $s_String
$a_Dir = _FileListToArray($sIniDir, '*', 2)
If @error Then Return SetError(1, 0, '')
For $i = 1 To $a_Dir[0]
If StringRegExp($a_Dir[$i], '^\d{4}$') Then
$s_String &= $a_Dir[$i] & '|'
EndIf
Next
If $s_String Then
Return StringTrimRight($s_String, 1)
EndIf
Return SetError(1, 0, '')
EndFunc ;==>_GetYearToCombo
Func _GetInfo()
Local $a_Replace[5] = [4], $s_FIO_Long, $s_Address, $s_FIO_Short, $s_Temp, $a_Temp, $s_Delim
For $i = 1 To 3
$s_Temp = GUICtrlRead($aInput[$i])
If Not $s_Temp Then Return SetError(1, 0, 'Не введены фамилия, имя или отчество')
If $i < 2 Then
$s_Temp = StringStripWS($s_Temp, 7)
If StringInStr($s_Temp, '-') Then
$s_Delim = '-'
ElseIf StringInStr($s_Temp, ' ') Then
$s_Delim = ' '
Else
$s_Delim = ''
EndIf
If $s_Delim Then
$a_Temp = StringSplit($s_Temp, $s_Delim)
$s_Temp = ''
For $j = 1 To $a_Temp[0]
$a_Temp[$j] = StringStripWS($a_Temp[$j], 8)
$s_Temp &= StringUpper(StringLeft($a_Temp[$j], 1)) & _
StringLower(StringTrimLeft($a_Temp[$j], 1)) & $s_Delim
Next
$s_FIO_Long = StringTrimRight($s_Temp, 1)
Else
$s_FIO_Long = StringUpper(StringLeft($s_Temp, 1)) & StringLower(StringTrimLeft($s_Temp, 1))
EndIf
$s_FIO_Short = $s_FIO_Long & ' '
Else
$s_Temp = StringStripWS($s_Temp, 8)
$s_FIO_Long &= ' ' & StringUpper(StringLeft($s_Temp, 1)) & StringLower(StringTrimLeft($s_Temp, 1))
$s_FIO_Short &= StringUpper(StringLeft($s_Temp, 1)) & '.'
EndIf
Next
$s_Address = GUICtrlRead($nCity) & ', '
$s_Temp = GUICtrlRead($nStreet)
If Not $s_Temp Then Return SetError(1, 0, 'Не выбран тип улицы')
$s_Temp = StringStripWS($s_Temp, 8)
$s_Address &= StringLower($s_Temp) & ' '
$s_Temp = GUICtrlRead($aInput[$aInput[0]])
If Not $s_Temp Then Return SetError(1, 0, 'Не введено название улицы')
$s_Temp = StringStripWS($s_Temp, 7)
If StringInStr($s_Temp, '-') Then
$s_Delim = '-'
ElseIf StringInStr($s_Temp, ' ') Then
$s_Delim = ' '
Else
$s_Delim = ''
EndIf
If $s_Delim Then
$a_Temp = StringSplit($s_Temp, $s_Delim)
$s_Temp = ''
For $i = 1 To $a_Temp[0]
$a_Temp[$i] = StringStripWS($a_Temp[$i], 8)
$s_Temp &= StringUpper(StringLeft($a_Temp[$i], 1)) & _
StringLower(StringTrimLeft($a_Temp[$i], 1)) & $s_Delim
Next
$s_Address &= StringTrimRight($s_Temp, 1) & ', '
Else
$s_Address &= StringUpper(StringLeft($s_Temp, 1)) & StringLower(StringTrimLeft($s_Temp, 1)) & ', '
EndIf
For $i = 4 To $aInput[0] - 1
$s_Temp = StringStripWS(GUICtrlRead($aInput[$i]), 8)
If Not Mod($i, 2) Then
If Not $s_Temp Then Return SetError(1, 0, 'Не введены номер дома или квартиры')
$s_Address &= StringLower(GUICtrlRead($aInput[$i] - 1)) & ' ' & $s_Temp & ', '
Else
If $s_Temp Then $s_Address &= StringLower(GUICtrlRead($aInput[$i] - 1)) & ' ' & $s_Temp & ', '
EndIf
Next
$a_Replace[1] = _ToDayFormatRu()
$a_Replace[2] = $s_FIO_Long
$a_Replace[3] = StringTrimRight($s_Address, 2) & '.'
$a_Replace[4] = $s_FIO_Short
Return SetError(0, 0, $a_Replace)
EndFunc ;==>_GetInfo
Func _Save($a_Array)
Local $s_Value, $sDate = StringFormat(' %02d.%02d.%02d %02d:%02d:%02d', @MDAY, @MON, _
StringRight(@YEAR, 2), @HOUR, @MIN, @SEC), $i_Res
For $i = 1 To $a_Array[0] - 1
$s_Value &= $a_Array[$i] & '|'
Next
$s_Value = StringTrimRight($s_Value, 1)
$i_Res = IniWrite($sIni, 'Polis', $a_Array[4] & $sDate, $s_Value)
If $i_Res Then
$a_Array = 0
EndIf
Return SetError($i_Res - 1)
EndFunc ;==>_Save
Func _Preview()
Local $s_File = @ScriptDir & '\template\_temp\_temp' & $sWordExt
If Not FileExists($s_File) Then Return
ShellExecute($s_File)
EndFunc ;==>_Preview
Func _Print($i_Copies = 2)
Local $s_File = @ScriptDir & '\template\_temp\_temp' & $sWordExt, $o_Word, $o_Doc, $i_Error
If Not FileExists($s_File) Then Return SetError(1)
$o_Word = _WordCreate($s_File, 0, 0)
If @error Then Return SetError(1)
$o_Doc = _WordDocGetCollection($o_Word, 0)
If @error Then Return SetError(1)
_WordDocPrint($o_Doc, 0, $i_Copies)
$i_Error = @error
_WordQuit($o_Word)
Return SetError($i_Error)
EndFunc ;==>_Print
Func _ReplaceWord($a_Replace)
Local $a_Find[5] = [4, 'date_long', 'fio_long', 'address', 'fio_short'], _
$s_Template = @ScriptDir & '\template\template' & $sWordExt, _
$s_File = @ScriptDir & '\template\_temp\_temp' & $sWordExt, $o_Word, $o_Doc, $i_Error
If Not FileCopy($s_Template, $s_File, 9) Then Return SetError(1)
$o_Word = _WordCreate($s_File, 0, 0)
If @error Then Return SetError(1, 0, 0)
For $j = 1 To 1
$o_Doc = _WordDocGetCollection($o_Word, 0)
If @error Then ExitLoop
For $i = 1 To $a_Find[0]
_WordDocFindReplace($o_Doc, $a_Find[$i], $a_Replace[$i])
If @error Then ExitLoop 2
Next
Next
If @error Then
_WordDocClose($o_Doc, 0)
_WordQuit($o_Word)
FileDelete($s_File)
Return SetError(1)
EndIf
_WordDocSave($o_Doc)
$i_Error = @error
_WordQuit($o_Word)
Return SetError($i_Error, 0, 0)
EndFunc ;==>_ReplaceWord
Func _ToDayFormatRu()
Local $a_Mon[13] = [12, 'января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', _
'сентября', 'октября', 'ноября', 'декабря']
Return StringFormat(Chr(171) & '%02d' & Chr(187) & ' %s %04d года', @MDAY, $a_Mon[@MON], @YEAR)
EndFunc ;==>_ToDayFormatRu
Func _DateToMonthRu($v_Mon, $i_Flag = 0)
Local $a_Mon[13] = [12, 'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', _
'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь']
If $i_Flag Then
For $i = 1 To $a_Mon[0]
If $v_Mon == $a_Mon[$i] Then
Return StringFormat('%02d', $i)
EndIf
Next
Return SetError(1, 0, '')
Else
$v_Mon = Abs(Int($v_Mon))
If $v_Mon > 0 And $v_Mon < 13 Then
Return $a_Mon[$v_Mon]
EndIf
Return SetError(1, 0, '')
EndIf
EndFunc ;==>_DateToMonthRu
Func _GetWordExt(ByRef $s_Ext)
Local $o_Word, $i_Ver
$o_Word = _WordCreate('', 0, 0)
If @error Then Return SetError(1)
$i_Ver = _WordPropertyGet($o_Word, 'version')
_WordQuit($o_Word, 0)
Sleep(250)
If Not $i_Ver Then Return SetError(1)
If $i_Ver > 11 Then
$s_Ext = '.docx'
Else
$s_Ext = '.doc'
EndIf
EndFunc ;==>_GetWordExt