# #####################################################################################
# Thema		:	Metafora axiografwn
#				To module ayto metaferei stin trexousa etairia ta axiografa poy den
#				exoyn exoflithi apo	mia alli etairia
#
# Objects	:	Application
#
# Author	:	George Tegos
# Version   :   1.0
# #####################################################################################


import string
import win32api
import modules.gui.vsflexgrid as vsflexgrid
import atlas
import atlasLang
import modules.db.ctree as ctree
import modules.tools.dt as dt
import win32traceutil

gRoundDecimals = 0



def Application_RaiseEvent (pDisp, param1, param2):
	global gGrid
	currCmpPath = atlas.String8(Company.FullPath (""))
	
	if param1 == "DocLoaded":
		Document_Calculate ()
		
	return None

def Document_Calculate():
	#
	# Get lists
	#
	sourcecomp = atlas.String8(Document.PropertyString('sourceComp'))
	sourceCompPath = atlas.GetCompanyPath (sourcecomp, False)
	destCompPath   = atlas.String8(Company.FullPath(''))
	session = atlas.AtlasSession()
	session.Init()
	
	sourcePagIfil = atlas.LogPagioIfil(session,sourceCompPath)
	sourcePelIfil = atlas.LogPelIfil (session,sourceCompPath)
	destPagIfil   = atlas.LogPagioIfil(session,destCompPath)
	destPelIfil   = atlas.LogPelIfil(session,destCompPath)

	try:
		sourcePagIfil.Open()
		destPagIfil.Open()
		
		sourcePelIfil.Open ()
		destPelIfil.Open ()

	except ctree.CtreeException, ctExcept:
		win32api.MessageBox (0, 'ctree error %d' % ctExcept.error)
		session.Close()
		return None
	else:
		pelMap={}
		for pel in sourcePelIfil:
			pelMap[pel.seqNr]=pel.code
		pagMap={}
		for pag in sourcePagIfil:
			if pag.ktisi.promSeqNr:
				pagMap[pag.code]=pelMap.get(pag.ktisi.promSeqNr,'')
		pelMap={}		
		for pel in destPelIfil:
			pelMap[pel.code]=pel.seqNr
		session.BeginTransaction ()
		for pag in destPagIfil:
			pag.ktisi.promSeqNr=pelMap.get(pagMap.get(pag.code,''),pag.ktisi.promSeqNr)
			pag.Modify()
		session.CommitTransaction ()

		sourcePagIfil.Close()
		destPagIfil.Close()
		sourcePelIfil.Close ()
		destPelIfil.Close ()
		session.Close()
	return None

def Document_InitializeScript ():
	# write html string to temporary file, and navigate to it
	global gHtmlString
	pagePath = Document.CreateTempFile ()
	pageFile = file (pagePath, "w+")
	pageFile.write (gHtmlString)
	pageFile.close ()
	WebBrowser.Navigate (pagePath)
	
	
	
	#htmlPath = Application.Path (7)	
	#WebBrowser.Navigate (htmlPath+"CostEndo.htm")
	global companyCode
	companyCode=Company.Code+Company.Etos		
	prevCompanyEtos=string.atoi(Company.Etos)
	prevCompanyEtos-=1
	if prevCompanyEtos>9:
		prevCompanyCode= Company.Code + str(prevCompanyEtos)
	else:
		prevCompanyCode= Company.Code + '0' + str(prevCompanyEtos)
	#
	# Setup from properties
	#
	Document.DeleteFormProperties ()
	Document.AddFormProperty ("sourceComp", _("From"), "", 150, prevCompanyCode, "", "", "")
	Document.DisplayFormProperties ()

	return None



# The HTML string
gHtmlString = """
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1253">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<style TYPE="text/css">
	P {text-align:justify}
	TD {text-align:justify}
</style>
<title>Διορθωση παγιων</title>
</head>


<table border="0" cellpadding="1" width="95%" cellspacing="0">
  <tr>
    <td valign="center" width="80%" bgcolor="#004080">
    <p align="left"><font size="3" color="#FFFFFF" face="Arial Greek"><strong>
    Διορθωση παγιων</strong></font></td>
    </tr>
    <tr>
    <td><font  face="Arial Greek" size="2" color="#000000">
	<br>Με την εργασια αυτη διορθωνονται οι κωδικοι προμηθευτη
    των παγιων που μεταφερθηκαν απο αλλη χρηση.
    </font></td>
    </tr>
    <td><font  face="Arial Greek" size="2" color="#000000">
    Στο πεδίο "ΑΠΟ" δηλώστε την εταιρία απ’ όπου ειχατε μεταφερει τα παγια.
    </font></td>
    </tr>
	<tr>
	<td><font  face="Arial Greek" size="2" color="#000000">
	Πατήστε το πληκτρο <strong>"Calculate"</strong> στην γραμμή εργαλείων ή απο το πληκτρολογιο το <strong>F9</strong>.
	</font></td>
	</tr>
</table>
<SCRIPT Language="JavaScript" FOR="VSFlexGrid1" EVENT="KeyDown(key,shift)">
	row = VSFlexGrid1.Row
	col = VSFlexGrid1.Col
	/* Start editing with F2 only for ApoCrel rows and for quantity and apotimisi columns */
	if (key == 113 && VSFlexGrid1.RowOutlineLevel (row) == 1 && col > 3)
		VSFlexGrid1.EditCell();
</SCRIPT>
<SCRIPT Language="VBScript">
Sub CollapseAll ()
	for row=1  to VSFlexGrid1.Rows-1 step 1
		level = VSFlexGrid1.RowOutlineLevel (row)
		if level = 0 then
			if not VSFlexGrid1.IsCollapsed(row) then
				VSFlexGrid1.IsCollapsed (row) = 2
            end if
        end if
    next
end sub
Sub ExpandAll ()
	for row=1  to VSFlexGrid1.Rows-1 step 1
		level = VSFlexGrid1.RowOutlineLevel (row)
		if level = 0 then
			if VSFlexGrid1.IsCollapsed(row) then
				VSFlexGrid1.IsCollapsed (row) = 0
            end if
        end if
    next
end sub
</SCRIPT>
<form method="POST" action="--WEBBOT-SELF--">
  <!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" --><p align="left">
  <p>
  </P>
</form>
</body>
</html>

"""

