<hgroup>
<button caption="AddLine" onclick="zenPage.btAddLine()"/>
<button caption="Del Line" onclick="zenPage.btDelete()"/>
<button caption="Save" onclick="zenPage.btSave()"/>
</hgroup>

<jsonSQLProvider id="json" sql="select nvl(ArticleCost->Aka,'')||nvl(AddServiceLine->AddService->Service->Aka,''),IsNal,RubSum,Comment,round(nvl(AddServiceLine->StoimFact,0)+nvl(TakeSum,0),2),Id 
from doc.AddCost "  >
</jsonSQLProvider>
<dataGrid pageSize="20"  id="dataGrid" pagingMode="client" controllerId="json" 
sortMode="client" selectMode="cells"  valueColumn="5" >
 
 <columnDescriptor caption="Article" type="lookup" readOnly="false" width="250px" ongetlookupspec="return zenPage.fireLookupDataState();"/>
 <columnDescriptor caption="Nal" type="checkbox" readOnly="false" width="50px"/>
 <columnDescriptor caption="Cost" type="string" readOnly="false" width="150px"/>
 <columnDescriptor caption="Comment" type="string" readOnly="false" width="250px"/>
 <columnDescriptor caption="Dop" type="string" readOnly="false" width="250px"/>
 <columnDescriptor caption="Code" type="string" readOnly="true"  />
 </dataGrid>

ClientMethod btSave() [ Language = javascript ]
{
var dataGrid zen('dataGrid');
var rowCount dataGrid.getRowCount();
for(var row=0;row<rowCount;row++) {
zenPage.SaveRow(dataGrid.getCellValue(row,0),dataGrid.getCellValue(row,1),dataGrid.getCellValue(row,2),dataGrid.getCellValue(row,3),dataGrid.getCellValue(row,5));
}
window.location.reload();
}

Method SaveRow(aArticleCostAka = "", aIsNal = "", aRubSum = "", aComment = "", aId) As %Status [ ZenMethod ]
{
set articleCostId=""
&sql(select Id into :articleCostId from ent.ArticleCost where Aka =:aArticleCostAka)

&sql(update doc.AddCost(ArticleCost,IsNal,RubSum,Comment)
values(:articleCostId,:aIsNal,:aRubSum,:aComment) where Id=:aId)

quit $$$OK
}

ClientMethod btAddLine() [ Language = javascript ]
{
zenPage.btSave();
zenPage.AddLine();
window.location.reload();
}

Method AddLine() As %Status [ ZenMethod ]
{
&sql(insert doc.AddCost (ArticleCost) values (null) )
quit $$$OK
}

ClientMethod btDelete() [ Language = javascript ]
{
var dataGrid zen('dataGrid');
var cRow=dataGrid.getProperty('currRow');

if (confirm("You sure?")){
zenPage.DeleteRow(dataGrid.getCellValue(cRow-1,5));
}
window.location.reload();
}

Method DeleteRow(Id) As %Status [ ZenMethod ]
{

&sql(delete from doc.AddCost where Id=:Id)
quit $$$OK
}