Cp1251
DOC 
foreground = lightgray
document_width = 1000
document_height = 700
document_title = "ZetaSE.Autocomplect/ Материалы для производства продукции"

preloadscript="

(global.sklad = '465')
(global.sklad_tseh = '445')
(global.id_head = -1)
(global.id_head_move = -1)
(global.id_head_spis = -1)
(global.id_active = -1)
(global.cur_active = -1)
(global.id_active_row = -1)
(global.spc_operation = 'no')
(global.row = -1)
(global.loading = 1)
(global.create = 0)
(global.no_combo_action = 0)
(global.parent_id_spc2 = -999)

($defun paint1<arg>
  ($X
    ($debug 'paint')
    (i = 0)
    (sz = ($g.d_spc@size))
    (rrr = ($g.d_spc@getCurRow))
    ($while (i < sz)
     (
       ($g.d_spc@fastSetCurRow i)
       #($debug 'paint: row ' + ($i@int) + ' color: ' + ($try ($g.d_spc@getValue 'color') $catch ('NullException')(0)))
       ($if (($try ($g.d_spc@getValue 'color') $catch ('NullException')(0)) == 0)
             ($g.gridspc@setRowBgColor i 'WHITE')
          $else
             ($g.gridspc@setRowBgColor i ($g.d_spc@getValue 'color'))
       )
       (i += 1)
     )
    )
    ($g.d_spc@setCurRow rrr)
    ($ret '')
  )
)
($defun set_row<arg>
  ($X
                    ($debug 'set_row,   find row with id_active: ' + global.id_active_row)
                    (i = 0)
                    ($while (i < ($g.d_spc@size))
                      (
                        ($g.d_spc@fastSetCurRow i)
                        ($if (($g.d_spc@getValue 'spc.id_active') == global.id_active_row)
                          (
                            #($debug 'found in row : ' + i)
                            ($g.d_spc@notifyViews)
                            (i = ($g.d_spc@size))
                          )
                        )
                        (i += 1)
                      )
                    )
                    ($ret '')
  )
)
"

postloadscript = "($X

                 (global.id_head = ($try (ARGUMENTS.0) $catch ('any') (-1)))

                 #($g.child_doc@retrieve)
                 #($if (($g.child_doc@size) > 0)
                 #  ($if (($g.child_doc@getValue 'accept2') == 1)
                 #    ($g.check_spis@setValue 'accepted')
                 #  )
                 #)

                 # этот флаг нужен, чтобы не выполнялся action у комбобокса, иначе он запишет в датасторю документа первое значение комбобокса,
                 # перезатерев существующее
                 (global.no_combo_action = 1)
                 ($g.self@doAction 'retrieve combo1')
                 (global.no_combo_action = 0)

                 ($if (global.create == 0)
                   (
                     # если данный документ уже существует, надо установить комбобоксы и галки в соответствии со значениями, сохраненными в этом документе
                     (sz = ($g.data_ds1@size))
                     (i = 0) (found_i = 0)
                     ($while (i < sz)
                       (
                         ($g.data_ds1@setCurRow i)
                         ($debug 'i = ' + i + ', datastore data1 = ' + ($g.data_ds1@getValue 'data1') + ', document date1 = ' + ($try ($g.d_head@getValue 'head.date1') $catch ('NullException')(-999)) )
                         ($if (($g.data_ds1@getValue 'data1') == ($try ($g.d_head@getValue 'head.date1') $catch ('NullException')(-999)) )
                           (
                             ($debug 'setSelectedItem ' + i)
                             ($g.combo1@setSelectedItem i)
                             (found_i = i)
                           )
                          )
                          (i += 1)
                       )
                     )
                     ($g.data_ds1@setCurRow found_i)

                   
                   )
                  $else
                   (
                     # Документ новый, ставим значения по умолчанию
                     (sz = ($g.data_ds1@size))
                     (i = 0) (found_i = 0)
                     ($while (i < sz)
                       (
                         ($g.data_ds1@setCurRow i)
                         ($debug 'i = ' + i + ', datastore data1: ' + ($g.data_ds1@getValue 'data1') + ', sysdate: ' + ds.month1 )
                         ($if (($g.data_ds1@getValue 'data1') == ds.month1 )
                           (
                             ($debug 'setSelectedItem ' + i)
                             ($g.combo1@setSelectedItem i)
                             (found_i = i)
                           )
                          )
                          (i += 1)
                       )
                     )
                     ($g.data_ds1@setCurRow found_i)
                   )
                 )
                     
                     
                ($paint1)
                ($g.gridspc@repaint)
                (global.loading = 0)
                ($Ret '')

             )"

{ datastore
  alias = child_doc
  query = "select id_head1, id_head2, accept2
             from (
           select head1.id_head id_head1, nvl(head2.id_head, 0) id_head2, nvl(head2.accept, 0) accept2
             from head head1, head head2
            where head1.parent_id_head = ~global.id_head~
              and head1.id_type_doc = 96
              and head2.parent_id_head = ~global.id_head~
              and head2.id_type_doc = 57
                  )"
}

{ datastore
  alias = check_q5
  query = "select count(1) cnt
             from spc
            where id_head = ~global.id_head~
              and spc.id_active
                  in (
                      select active.id_active
                        from active
                     connect by prior id_active = parent_act_id
                       start with id_active = ~($try ($g.gr@getValue 'id_active') $catch ('Any')(-1000000))~
                     )
              and q5_spc > 0"
}

{ datastore
  alias = get_rest
  query = "select rest1, rest2, q_dep1, q_dep11, q_dep2, q_dep22
             from (
           select doc_lib.getRestAnalogBeforeAll(~global.id_active~,
                                                 ~($g.d_head@getValue 'head.sort')~,
                                                 '~global.sklad~'
                                                ) rest1, 

                  doc_lib.getRestAnalogBeforeAll(~global.id_active~,
                                                 ~($g.d_head@getValue 'head.sort')~,
                                                 '~global.sklad_tseh~'
                                                ) rest2,
                  doc_lib.getRestAnalogBeforeAll(~global.id_active~,
                                                 1e30,
                                                 '206,306,209'
                                                ) q_dep1, 
                  doc_lib.getRestAnalogBeforeAll(~global.id_active~,
                                                 1e30,
                                                 '210'
                                                ) q_dep11, 


                  doc_lib.getRestAnalogBeforeAll(~global.id_active~,
                                                 1e30,
                                                 '208'
                                                ) q_dep2, 
                  doc_lib.getRestAnalogBeforeAll(~global.id_active~,
                                                 1e30,
                                                 '225'
                                                ) q_dep22


           from dual
                  )"
}

{datastore  alias=check_month
    query = "select check_zvm_month
               from (
                      select util.check_zvm_month(~(global.id_active)~, ~($try (ARGUMENTS.0) $catch ('NullException') (0) )~) check_zvm_month
                        from dual
                    )"
} // end of ds

{ datastore
  alias = analog_cnt
  query = "select count(1) cnt from active where analog_id = ~global.id_active~ or id_active in (select analog_id from active where id_active = ~global.id_active~)"
}

{datastore  alias=ds_original
    query = "select name_active, q2_spc
               from (
                      select active.name_active, spc.q2_spc
                        from active, spc
                       where spc.id_spc = ~(global.parent_id_spc2)~
                         and spc.id_active = active.id_active
                    )"
} // end of ds

{datastore  alias=ds_sum_change
    query = "select sum(spc.q2_spc) sum_change, count(spc.parent_id_spc) cnt_change
               from spc
              where spc.id_head = ~($g.d_head@getValue 'head.id_head')~
                and ( spc.id_active = ~global.cur_active~ or spc.parent_id_spc = ~global.cur_active~)"
}

{datastore alias = ds_new_date
    query = "select new_date from 
                    (
             select to_char(h.date1, 'yyyy-mm') || '-' || to_char(h.date_sd, 'dd') new_date
               from head h where h.id_head = ~global.id_head~
                    )" 
}

{dscollection
   alias = ds
   seqquery = "select spc_sq.nextval from dual"
   seqnames = "spk"
}


{toolbar   BACKGROUND = "LIGHTGRAY"
{splitpanel type = horizontal  percent=20
{splitpanel type = vertical percent = 35
{form width=780 height=100       bg_color="#c0c0c0"   alias = fm

   {label
       value="Склад"        
       left=5    top=5    height=20       width=50
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }
   {field
       target=head.id_department1
       visible=no           border = box
       left=50    top=70    height=20       width=100
       alias=sklad_t        font_size=12    font_family=0    bg_color="#ffffff"
       editexp="(sklad_t=STORE.department.id_department)"
   }
   {field
       target=department.name1_dep                 border = box
       left=60    top=5    height=25       width=170
       alias=sklad          font_size=12    font_family=0    bg_color="#ffffff"
       editable=readonly
   }

   {label
       value="N док."        
       left=5    top=35    height=20       width=50
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }
   {field
       target=head.num_sd               border = box
       left=60    top=35    height=25       width=100
       alias=f_num_sd        font_size=12    font_family=0    bg_color="#ffffff"
       editable=readonly
   }
   {label
       value="производство"
       left=235    top=5    height=20       width=80
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }
   {combobox
      left = 315     top = 5      width = 100        height = 20 
      alias = combo1               border = box
      target = month
      action = "~($X
               
                  ($debug 'combo1')
                  ($debug 'global.loading: ' + global.loading)

                  ($if (global.loading == 1)
                     (
                       ($ret '')
                     )
                  )
                  ($if (global.no_combo_action == 0)
                    (
                      ($g.data_ds1@setCurRow ($g.combo1@getSelectedIndex))
                      ($g.d_head@setValue 'head.date1' ($g.data_ds1@getValue 'data1'))
                      ($g.d_head@setValue 'head.date_sd' ($g.data_ds1@getValue 'data1'))


                      ($debug 'combo1, index: ' + ($g.combo1@getSelectedIndex) + ', value: ' + ($g.data_ds1@getValue 'data1') + ' ' + ($g.data_ds1@getValue 'month'))
                      ($g.d_head@update)
                      # Меняется месяц производства, надо сменить даты зависимых документов
                      (global.id_head = ($try (ARGUMENTS.0) $catch ('any') (-1)))
                      ($g.child_doc@retrieve)
                      ($if (($g.child_doc@size) == 0)
                         (
                           ($g.self@sure 'Ошибка в алгоритме. Не найдены документы перемещения и списания.' 1)
                           ($ret '')
                         )
                      )
                      (global.id_head_move = ($g.child_doc@getValue 'id_head1'))
                      (global.id_head_spis = ($g.child_doc@getValue 'id_head2'))
                      ($g.self@doAction 'execute exec doc_lib.de_accept( ' + global.id_head_move + ')')
                      ($g.ds_new_date@retrieve)
                      ($debug ' moving to date: ' + ($g.ds_new_date@getValue 'new_date'))

                      ($g.self@doAction 'execute exec update head set date_sd = date \'' + ($g.ds_new_date@getValue 'new_date') + '\' where id_head = ' + global.id_head_move)
                      ($g.self@doAction 'execute exec update head set date1 = trunc(date_sd, \'month\') where id_head = ' + global.id_head_move)
                      ($g.self@doAction 'execute exec doc_lib.accept( ' + global.id_head_move + ')')

                      ($g.self@doAction 'execute exec doc_lib.de_accept( ' + global.id_head_spis + ')')
                      ($g.self@doAction 'execute exec update head set date_sd = date \'' + ($g.ds_new_date@getValue 'new_date') + '\' where id_head = ' + global.id_head_spis)
                      ($g.self@doAction 'execute exec update head set date1 = trunc(date_sd, \'month\') where id_head = ' + global.id_head_spis)
                      ($g.self@doAction 'execute exec doc_lib.accept( ' + global.id_head_spis + ')')

                      ($g.self@doAction 'execute exec commit')
                      ($g.gridspc@retrieve)
                    )
                  )
                  ($ret '')
                )~"
      {datastore
       alias = data_ds1
       query = "select data1, month
                  from (
                select add_months(trunc(sysdate, 'month'), rownum - 6) data1,
                       to_char(add_months(trunc(sysdate, 'month'), rownum - 6), 'Month YYYY') month
                  from dual
               connect by level <= 12
                       )
                 order by data1"
      }
   }

   {label
       value="участок"
       left=235    top=40    height=20       width=80
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }
   {field
      left = 315     top = 40      width = 100        height = 20 
      border = box
      target = work_area.name_work_area
      editable = no
      tooltiptext = "Участок изменить нельзя, он указывается при создании заявки"
   }


{button
  left = 150  top = 90 width = 80    height = 53
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  icon = "images/defsit.jpg"
  tooltiptext = "Дефицит в цехе"
  action = "($X
               (sz = ($g.d_spc@size))
               (rrr = ($g.d_spc@getCurRow))
               (i = 0)
               ($while (i < sz)
                 (
                   ($g.d_spc@fastSetCurRow i)
                   ($if (($try ($g.d_spc@getValue 'spc.child_spc') $catch ('NullException')(0)) == 0)
                     (
                        (q = ($g.d_spc@getValue 'spc.q1_spc') + ($g.d_spc@getValue 'q_zvm_required') - ($g.d_spc@getValue 'q_zvm_sent'))
                        ($if (q < 0) (q = 0) )
                        ($debug 'row ' + ($i@int) + ' q: ' + q)
                        ($if (($g.ds_prod@getValue 'id_active') == 0)
                           (
                             ($g.self@doAction 'execute exec update spc set spc.q2_spc = ' + q  + ' where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' + ($g.d_spc@getValue 'spc.id_active'))
                             ($g.d_spc@setValue 'spc.q2_spc' q)
                           )
                         $else
                           ($g.d_spc@setValue 'spc.q2_spc' q)
                        )
                     )
                    $else
                     (
                       ($debug 'row ' + ($i@int) + ' skipped.')
                     )
                   )
                   (i += 1)
                 )
               )
               ($g.d_spc@update)
               ($g.self@doAction 'execute exec commit')
               ($g.d_spc@setCurRow rrr)
               ($g.d_spc@NOtifyViews)
               ($ret '')
            )"
}

{button
  left = 250  top = 95 width = 40    height = 40
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  icon = "images/box.jpg"
  tooltiptext = "Учесть дефицит"
  action = "($X
               ($g.check_q5@retrieve)
               ($if (($g.check_q5@getValue 'cnt') > 0)
                 (
                   ($g.self@sure 'Учесть остаток в цехе можно 1 раз.' 1)
                   ($ret '')
                 )
               )
               (sz = ($g.d_spc@size))
               (rrr = ($g.d_spc@getCurRow))
               (i = 0)
               ($while (i < sz)
                (
                  ($g.d_spc@fastSetCurRow i)
                  ($if (($try ($g.d_spc@getValue 'spc.child_spc') $catch ('NullException')(0)) == 0)
                    (


                       ($if (($g.d_spc@getValue 'q_rest_plan') >= ($g.d_spc@getValue 'spc.q1_spc')) 
                           (q = 0)
                        $else
                           (q = ($g.d_spc@getValue 'spc.q1_spc') -  ($g.d_spc@getValue 'q_rest_plan'))
                       )


                       ($debug 'row ' + ($i@int) + ' spc.q1_spc: ' + ($g.d_spc@getValue 'spc.q1_spc') + ', q_rest_plan: ' + ($g.d_spc@getValue 'q_rest_plan') + ', q: ' + q)

                       ($g.self@doAction 'execute exec  util.update_zvm(' + ($g.d_spc@getValue 'spc.id_head') + ', ' + ($g.d_spc@getValue 'spc.id_active') + ', ' + q + ')')
                       ($g.self@doAction 'execute exec  update spc set q5_spc = 1 where id_head = ' + ($g.d_spc@getValue 'spc.id_head') + ' and id_active = ' + ($g.d_spc@getValue 'spc.id_active'))
                    )
                   $else
                    (
                      ($debug 'row ' + ($i@int) + ' skipped.')
                    )
                  )
                  
                  (i += 1)
                )
               )
               ($g.self@doAction 'execute exec commit')
               ($g.gridspc@retrieve)
               ($paint1 0)
               ($g.gridspc@repaint)
               ($g.d_spc@setCurRow rrr)
               ($g.d_spc@NOtifyViews)
               ($ret '')
            )"
}

{button
  left = 300  top = 95 width = 40    height = 40
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  icon = "images/box_cancel.jpg"
  tooltiptext = "Вернуться к требуемому кол-ву"
  action = "($X
               (sz = ($g.d_spc@size))
               (rrr = ($g.d_spc@getCurRow))
               (i = 0)
               ($while (i < sz)
                (
                  ($g.d_spc@fastSetCurRow i)
                  ($g.self@doAction 'execute exec  update spc set q5_spc = 0, q2_spc = q1_spc where id_head = ' + ($g.d_spc@getValue 'spc.id_head') + ' and id_active = ' + ($g.d_spc@getValue 'spc.id_active'))
                  (i += 1)
                )
               )
               ($g.self@doAction 'execute exec commit')
               ($g.gridspc@retrieve)
               ($paint1 0)
               ($g.gridspc@repaint)
               ($g.d_spc@setCurRow rrr)
               ($g.d_spc@NOtifyViews)
               ($ret '')
            )"
}

{datastore 
        alias = d_head
        query = "select head.id_head, head.id_department1, head.sort, head.num_sd, 
                        head.date_sd, head.date1, head.type1, work_area.name_work_area,
                        department.name1_dep, department.dep1
                   from head, department, work_area
                  where head.id_head = ~($try (ARGUMENTS.0) $catch ('any') (-1))~
                    and head.id_department1 = department.id_department
                    and head.type1 = work_area.id_work_area (+)"
              editable = yes
                  head = yes
                unique = "('head','head.id_head')"
} // ds
} // form
{splitpanel type = vertical  percent=70
 { splitpanel type = vertical percent = 60
{grid
editable = no
alias = GRIDPROD
buttonbar_font_family=0       buttonbar_font_size=12           
buttonbar_size = 1           titlebar_size = 18            rowsize=20
titlebar_height = 30

del = ""

addexp="($X
          ($ret '')

	)"



 {column size=150       title="Продукция"
    target=name_active  title_font_family=0     halignment=left
    font_color="#000000"      font_size=12              font_family=0 font_face = "Arial narrow"
    editable = readonly 
 }
 {column size=50       title="Кол-во"
    target=q1_spc  title_font_family=0     halignment=center
    font_color="#000000"      font_size=12              font_family=0 font_face = "Arial narrow"
    editable = readonly       editmask = "# #0."
 }
 {column size=100     
    title="QQQ"              font_family=0            editable = readonly 
    target=q_material    title_font_family=0      halignment=center
    font_color="#000000"     font_size=12
    editmask = "##0.000"
    alias = COLUMN_NAME_MATERIAL
 }

 {datastore 
      alias = ds_prod
      query = "select id_active, name_active, q1_spc, name_active_material, q_material
                 from (
               select spc.id_active, active.name_active, spc.q1_spc, active_material.name_active name_active_material,
                      (
                        select s.q1_spc
                          from spc s
                         where s.id_head = active.id_head2
                           and s.id_active = ~global.id_active~
                      ) q_material
                 from spc, active, active active_material
                where spc.id_head = ~($try (d_head.head.id_head) $catch ('NullException') (-1) )~
                  and spc.parent_id_spc = 0
                  and active.id_active = spc.id_active
                  and active_material.id_active = ~global.id_active~
                      )
                order by name_active
                  "


} // ds
} // grid
  {grid
      alias = GRID_WORK_STAGE
      buttonbar_font_family = 0              buttonbar_font_size = 12
      buttonbar_size = 1                    titlebar_size = 18
      multiselect = no                      editable = no
      rowsize = 20
                del = "~($X
                           ($ret '') 
                        )~"

     {column size=100             title="work_stage.id_work_stage"  visible = no
          target=work_stage.id_work_stage
     }                  
     {column size=100             title="Этап сборки"
          target=work_stage.name_work_stage
          title_font_family=2        title_font_color="#000000"
          halignment=left            font_size = 12          font_family=0
     }                  

    {datastore 
             alias = GRIDDS_WORK_STAGE
             query = "select work_stage.id_work_stage, work_stage.name_work_stage
                        from work_stage
                       where work_stage.id_work_stage not in (0, 6)
                       union all

                      select -999 id_work_stage, '-- все этапы --' name_work_stage
                        from dual

                       order by name_work_stage"
                editable = no

                selaction = "~($X
                                 ($g.gridspc@retrieve)

                                 ($paint1 0)

                                 ($ret '')
                               )~"

     } // end of ds
  } // end of grid
} // vertical split
  {grid
   editable = no
   alias = GRID1
   buttonbar_font_family=0       buttonbar_font_size=12           
   buttonbar_size = 1           titlebar_size = 18            rowsize=25     
   titlebar_height = 0

    {column size=150       title="Группа"
       target=name_active  title_font_family=2     halignment=left
       font_color="#000000"      font_size=12              font_family=0
       alias=name_w              editable = readonly 
    }

   {datastore
    alias = gr
    query = "select id_active, name_active
               from (
             select -1000000 id_active, '/   показать все' name_active
               from dual
              union all
             select distinct util.rootActiveID(id_active) id_active, util.rootActiveName(id_active) name_active
               from (
                      select distinct active_material.id_active
                        from spc, active active_prod, active active_material, head head_spec, spc spc_spec
                       where spc.id_head = ~($try (ARGUMENTS.0) $catch ('NullException') (0) )~
                         and spc.parent_id_spc = 0
                         and spc.id_active = active_prod.id_active
                         and active_prod.id_head2 = head_spec.id_head
                         and head_spec.id_head = spc_spec.id_head
                         and spc_spec.id_active = active_material.id_active
                                  ~($if (($g.gridds_work_stage@size) > 0)
                                                         ($if (($g.gridds_work_stage@getValue 'work_stage.id_work_stage') != -999)
                                                            (' and spc_spec.work_type = ' + ($g.gridds_work_stage@getValue 'work_stage.id_work_stage'))
                                                          $else ('
                                                          -- all work stages
                                                          ')
                                                         )

                                                        $else ('
                                                          -- all work stages
                                                          ')

                                    )~
                    )
                    )
              order by name_active"

                 selaction = "~($X
                                  ($if (global.loading == 1)
                                    ($ret '')
                                  )
                                  ($g.gridspc@retrieve)
                                  ($paint1 0)
                                  ($g.gridspc@repaint)
                                  ($ret '')
                               )~"
   } // datastore
  } // 
} // vertical split
} // vertical split
 {tabset labels = "материалы"

{grid
editable = yes
alias = GRIDSPC
buttonbar_font_family=0       buttonbar_font_size=12           
buttonbar_size = 30           titlebar_size = 18            rowsize=20
titlebar_height = 110

del = "~($X
            
            ($ret '')
        )~"

addexp="($X

          ($debug ' =========================================== add exp =================================')
          ($debug 'global.spc_operation:  ' + global.spc_operation)

          # если идет замена, на повтор не проверяем
          (is_in = 1)
          ($if (global.spc_operation != 'change')
            (
              (rrr = ($g.d_spc@getCurRow))
              (t = ($g.d_spc@iterator))
              (i = 0)
              (is_in = 1)
              ($while ($t@next)
                (
                  (i += 1)
                  ($g.d_spc@FastSetCurRow (i-1))
                  (l_id_active = ($try ($g.d_spc@GetValue 'SPC.ID_ACTIVE') $catch ('Any')(-1)))
                  ($if (l_id_active == store.active.id_active) (
                    ($if (($g.self@sure 'Материал \"'+store.active.name_active+'\" уже есть в спецификации! Добавить еще раз?' 0) == 0)
                      (is_in = 0)
                     $else
                      (is_in = 1)
                    )
                  ))
                )
              )
              ($g.d_spc@setCurRow rrr)
            )
          )
          #($if (is_in == 1)
          #  (
          #    (global.id_active = store.active.id_active)
          #    ($g.check_month@retrieve)
          #    ($if (($g.check_month@getValue 'check_zvm_month') != '-999-')
          #      (
          #        (msg = 'Этот материал уже использовался в текущем месяце для следующей готовой продукции:')
          #        (msg += ($g.check_month@getValue 'check_zvm_month'))
          #        (msg += '%%Все равно добавить его?')
          #        ($if (($g.self@sure msg 0) == 0)
          #          (is_in = 0)
          #        )
          #      )
          #    )
          #  )
          #)
          ($if (is_in == 1)
            (
              ($if (global.spc_operation == 'change')
                (
                  ($g.d_spc@delRow ($g.d_spc@getCurRow))
                  ($g.d_spc@fastSetCurRow global.row)
                  ($debug 'current row: '+ global.row)
                  (global.id_active = store.active.id_active)

                  # Кол-во к выдаче в цех материала, идущего на замену : _Кол-во для производства_ - _Выдать в цех_.
                  (q1 = 0)
                  (q2 = 0)
                  (i = 0)
                  ($while (i < ($g.d_spc@size))
                    (
                      ($g.d_spc@fastSetCurRow i)
                      ($if (($g.d_spc@GetValue 'spc.id_active') == store.active.id_active)
                        (
                          (q1 = ($g.d_spc@GetValue 'spc.q1_spc'))
                          (q2 = ($g.d_spc@GetValue 'spc.q2_spc'))
                        )
                      )
                      (i += 1)
                    )

                  )
                  ($debug 'q1 : ' + q1)
                  ($debug 'q2 : ' + q2)


                  #($if (q1 > q2)
                  #   (q1 = q1 - q2)
                  # $else
                  #   (q1 = 0)
                  #)

                  #($debug 'q1 : ' + q1)

                  #($if ((q1 + q2) > 0)
                  #  ($if (($g.self@sure store.active.name_active + ' уже есть в этой заявке, Кол-во для производства: ' + q1 + ', Выдать в цех: ' + q2 + '. Учесть это кол-во для данной замены?' 0) == 0)
                  #    ($debug 'Не учитываем замену')
                  #   $else 
                  #    ($g.self@sure 'Учитываем по формуле Выдать в цех в данной строке = Остаток в той строке - Выдать в цех в той строке ... ???' 1)
                  #  )
                  #)

                  ($g.d_spc@fastSetCurRow global.row)

                  ($g.self@doAction 'execute exec update spc set parent_id_spc = ' + store.active.id_active + '  where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' + ($g.d_spc@getValue 'spc.id_active'))
                  # заменяющий материал, выдать в цех увеличивается на кол-во заменяемого
                  ($g.self@doAction 'execute exec update spc set q2_spc = q2_spc + (select sum(q2_spc) from spc where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' +  ($g.d_spc@getValue 'spc.id_active') + ')  where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' + store.active.id_active)
                  # заменяемый материал, выдать в цех = 0
                  ($g.self@doAction 'execute exec update spc set q2_spc = 0  where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' + ($g.d_spc@getValue 'spc.id_active'))

                  ($g.d_spc@notifyViews)
                )
               $else
                (
                 ($g.d_spc@setValue 'SPC.ID_ACTIVE' store.active.id_active)
                 ($g.d_spc@setValue 'NAME_ACTIVE' store.active.name_active)
                 ($g.d_spc@setValue 'SPC.ID_UNIT' store.id_pseudo_unit)
                 ($g.d_spc@setValue 'OBOZN' store.obozn)
                 (global.id_active = store.active.id_active)
                 ($g.get_rest@retrieve)
                 ($g.d_spc@setValue 'spc.q2_spc' 0)
                )
              )
            )
            $else (
              ($g.d_spc@delRow ($g.d_spc@getCurRow))
            )
          )
          ($g.d_spc@update)
          ($g.self@doAction 'execute exec commit')
          ($g.gridspc@retrieve)
          ($paint1 0)
          ($g.d_spc@fastSetCurRow global.row)
          ($g.gridspc@repaint)
          ($ret '')

	)"



{menu
   {item label = "Добавить (Ctrl+Ins)" action="~($X 
   
                                        (global.spc_operation = 'add') ($ret 'opennew find_choose.rml 1, 0')
                                      )~"}
   {item label = "-"}
   {item label = "Удалить" action="~($X
                                        
                                        ($g.self@doAction 'execute exec delete from spc where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' + ($g.d_spc@getValue 'spc.id_active'))
                                        ($g.self@doAction 'execute exec commit')
                                        ($($g.gridspc@getDatastore)@DelRow ($($g.gridspc@getDatastore)@getCurRow))
                                        ($($g.gridspc@getDatastore)@NotifyViews)
                                        ($paint1 0)
                                        ($g.gridspc@repaint)
                                        ($ret '')
                                    )~"
   }
   {item label = "-"}
   {item label = "Замена" action="~($X 
                                         (global.spc_operation = 'change')
                                         (global.row = ($g.d_spc@getCurRow))
                                         (global.parent_id_active = ($g.d_spc@getValue 'spc.id_active'))
                                         (global.name_active = ($g.d_spc@getValue 'name_active'))
                                         ($ret 'opennew find_choose.rml 1, 0') 
                                     )~
                                    "
   }
   {item label = "Удалить замену" action="~($X 
                                         ($debug '111111111111111')
                                         (r = ($g.d_spc@getCurRow))

                                         (global.cur_active = ($g.d_spc@getValue 'spc.parent_id_spc'))

                                         ($debug '2222222222222')

                                         ($g.self@doAction 'execute exec update spc set parent_id_spc = null where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' + ($g.d_spc@getValue 'spc.id_active'))

                                         ($debug '3333333333333333333')
                                         # заменяемый материал, выдать в цех = кол-во для производства
                                         ($g.self@doAction 'execute exec update spc set q2_spc = q1_spc  where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' + ($g.d_spc@getValue 'spc.id_active'))
                                         ($debug '444444444444444444444444444')

                                         # заменяющий материал, выдать в цех уменьшается на кол-во заменяемого
                                         ($g.self@doAction 'execute exec update spc set q2_spc = q2_spc - (select sum(q2_spc) from spc where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' +  ($g.d_spc@getValue 'spc.id_active') + ')  where id_head = ' + ($g.d_spc@getValue 'spc.id_head') +' and id_active = ' + global.cur_active )

                                         ($g.self@doAction 'execute exec commit')

                                         ($debug '55555555555555555555')
                                         ($g.gridspc@retrieve)
                                         ($paint1 0)
                                         ($g.gridspc@repaint)
                                         ($g.d_spc@fastSetCurRow r)
                                         ($g.d_spc@notifyViews)
                                         ($debug '666666666666666666666666')

                                         ($ret '') 
                                     )~"
      {item label = "-"}
      {item label = "Перемещения"
       action = "~($X
                     ($debug 'ARGUMENTS.0: ' + ($g.d_spc@getValue 'spc.id_active') + ' (' + ($($g.d_spc@getValue 'spc.id_active')@type) + ')')
                     ($debug 'ARGUMENTS.1: ' + ($g.d_spc@getValue 'dat1') + ' (' + ($($g.d_spc@getValue 'dat1')@type) + ')')
                     ($ret 'opennew history_move.rml ' + ($g.d_spc@getValue 'spc.id_active') + ', ' + ($g.d_spc@getValue 'dat1'))
                   )~"
      }
   }
                                    
   {item label = "-"}
   {item label = "История перемещений"
    action = "~($X
                  ($ret 'opennew history_move.rml ' + ($g.d_spc@getValue 'spc.id_active') + ', ' + ($g.d_spc@getValue 'dat1'))
                )~"
   }
   {item label = "История заявок"
    action = "~($X
                  ($ret 'opennew history_zvm.rml ' + ($g.d_spc@getValue 'spc.id_active') + ', ' + ($g.d_spc@getValue 'dat1'))
                )~"
   }
   {item label = "Оборотная ведомость"
    action = "~($X
                  ($ret 'open reports/report_sklad_ved2.rml ' + ($g.d_spc@getValue 'spc.id_active') + ', NOTHING')
                )~"
   }
   {item label = "Остатки по аналогам"
    action = "~($X
                  (global.spc_operation = 'change')
                  (global.row = ($g.d_spc@getCurRow))
                  (global.parent_id_active = ($g.d_spc@getValue 'spc.id_active'))
                  (global.name_active = ($g.d_spc@getValue 'name_active'))
                  ($ret 'opennew reports/report_analog_rests.rml ' + ($g.d_spc@getValue 'spc.id_active') + ', \'210\'')
                )~"
   }
   {item label = "Выгрузка в Excel"
    action = "~($X
                   
                  (global.grid_name = 'GRIDSPC')
                  ($g.self@calculate 'js:js_file:/docs/print_grid.js')
                  ($ret '')
                )~"
   }
}

 {column size=50     
    title="id_Spc"              font_family=0            editable = readonly 
    target=spc.id_spc     title_font_family=0      halignment=center
    font_color="#000000"     font_size=12
    editmask = "##0."
    alias = column_id_spc
    visible = no
 }
 {column size=50     
    title="Код"              font_family=0            editable = readonly 
    target=spc.id_active     title_font_family=0      halignment=center
    font_color="#000000"     font_size=12
    editmask = "##0."
    alias = column_id_active
 }
 {column size=200       title="Наименование"
    target=name_active  title_font_family=0     halignment=left
    font_color="#000000"      font_size=12              font_family=0 font_face = "Tahoma"
    editable = readonly 
 }
 {column size=80       title="<html>Остаток<br>Ренталком</html>" 
    target=q_dep11     title_font_family=0      halignment=right
    font_color="#000000"     font_size=12            font_family=0  font_face = "Tahoma"
    editmask = "# #0.000"   editable = readonly
 }
 {column size=150       title="Замена"
    target=replace_material  title_font_family=0     halignment=left
    font_color="#000000"      font_size=12              font_family=0 font_face = "Tahoma"
    editable = readonly 
    alias = column_replace
    //visible = no
 }
 {column size=50       title="Код"
    target=spc.parent_id_spc  title_font_family=0     halignment=left
    font_color="#000000"      font_size=12              font_family=0 font_face = "Tahoma"
    editable = readonly       editmask = "##0."
    alias = column_replace_id_spc
    visible = no

 }
 {column size=70       title="<html>Кол-во<br>для 1 ед.<BR>продукции</html>" 
    target=spc.price1_spc      title_font_family=0      halignment=right
    font_color="#000000"     font_size=12            font_family=0  font_face = "Tahoma"
    editmask = "# #0.000"    alias = q1_spc
    editable = readonly
 }
 {column size=90       title="<html>Кол-во<br>для произ-<BR>водства по <BR>данной заявке</html>" 
    target=spc.q1_spc      title_font_family=0      halignment=right
    font_color="#000000"     font_size=12            font_family=0   font_face = "Tahoma"
    editmask = "# #0.000"   editable = readonly
    alias = total_q_spc
 }
 {column size=90       title="<html>Кол-во<br>для произ-<BR>водства по <BR>пред. заявкам</html>" 
    target=q_zvm_required     title_font_family=0      halignment=right
    font_color="#000000"     font_size=12            font_family=0   font_face = "Tahoma"
    editmask = "# #0.000"   editable = readonly
 }
 {column size=90       title="<html>Перемещено<BR>за месяц<br>не по заявкам</html>" 
    target=q_not_zvm_sent      title_font_family=0      halignment=right
    font_color="#000000"     font_size=12            font_family=0   font_face = "Tahoma"
    editmask = "# #0.000"   editable = readonly
    alias = q_not_zvm_sent
 }
 {column size=90       title="<html>Перемещено<BR>за месяц<br>по заявкам</html>" 
    target=q_zvm_sent      title_font_family=0      halignment=right
    font_color="#000000"     font_size=12            font_family=0   font_face = "Tahoma"
    editmask = "# #0.000"   editable = readonly
    alias = q_zvm_sent
 }
 {column size=90       title="<html>Выдать<br>в цех</html>" 
    target=spc.q2_spc      title_font_family=0      halignment=right  font_face = "Tahoma"
    font_color="#000000"     font_size=12            font_family=0
    editmask = "# #0.000"
    alias = sent_q_spc
    dep = "q_rest_plan"
    editexp = "($X 
                   ($debug '===============================')
                   ($debug 'введено: ' + g.sent_q_spc)
                   #(q = 'execute exec update spc set q2_spc = ' + g.sent_q_spc ' where id_head = ' + ($g.d_spc@getValue 'spc.id_head') + ' and id_active =  ' + ($g.d_spc@getValue 'spc.id_active'))
                   #($debug 'запрос: ' + q)
                   #($g.self@doAction q)
                   ($g.d_spc@update)
                   ($g.self@doAction 'execute exec commit')


                   ($ret '')




                   # если редактируется материал, который используется в замену другим, или заменяемые материалы, надо пересчитать его кол-во

                   (global.cur_active = ($g.d_spc@getValue 'spc.id_active'))
                   ($g.ds_sum_change@retrieve)
                   ($debug 'row: ' + ($g.d_spc@getCurRow) + ' id_active: ' + global.cur_active + ' cnt_change: ' + ($g.ds_sum_change@getValue 'cnt_change') + 'parent_id_spc: ' + ($try ($g.d_spc@getValue 'spc.parent_id_spc') $catch ('NullException')(0)))

                   (v_retrieve = 0)

                   ($if (($try ($g.d_spc@getValue 'spc.parent_id_spc') $catch ('NullException')(0)) != 0)
                     (v_retrieve = 1)
                   )

                   ($if (($g.ds_sum_change@getValue 'cnt_change') > 0)
                     (v_retrieve = 1)
                   )

                   ($if (v_retrieve == 1)
                     (
                       (r = ($g.d_spc@getCurRow))
                       ($g.gridspc@retrieve)
                       ($paint1 0)
                       ($g.gridspc@repaint)
                       ($g.d_spc@fastSetCurRow r)
                       ($g.d_spc@notifyViews)
                     )
                   )

                   ($debug 'spc.q2_spc: ' + ($g.d_spc@getValue 'spc.q2_spc'))
                   ($debug '===============================')
                   ($ret '') 
               )"

 }
 {column size=90       title="<html>Ост.в цехе<br>после выполнения<br>пред. заявок<br>(дефицит)</html>" 
    target = q_rest_plan      title_font_family=0      halignment=right
    font_color="#000000"     font_size=12            font_family=0  font_face = "Tahoma"
    editmask = "# #0.000"    editable = readonly 
    alias = q_rest_plan
 }

 
 {column size=80       title="<html>Остаток<br>ЦЕХ текущий</html>" 
    target=q_dep22      title_font_family=0      halignment=right
    font_color="#000000"     font_size=12            font_family=0  font_face = "Tahoma"
    editmask = "# #0.000"    editable = readonly 
    alias = q_rest_semenov
 }
// {column size=150       title="<html>Примечание</html>" 
//    target=spc.comments      title_font_family=0      halignment=left  font_face = "Tahoma"
//    font_color="#000000"     font_size=12            font_family=0
//    alias = column_comments
//    visible = no
// }

 {datastore 
      alias = d_spc
      query = "select spc.id_spc, spc.id_head, spc.id_active, spc.id_unit,
                      spc.q1_spc, spc.q2_spc, spc.q5_spc, spc.price1_spc, spc.price2_spc,
                      spc.parent_id_spc, spc.child_spc,
                      dat1,
                      name_active, analog, analog_id, name_original_active,
                      obozn, spc.comments, replace_material, date_sd, num_sd, kod_prod, name_prod, comment_sd,
                      q_dep11, q_moved, q_dep22, perem, q_not_zvm_sent, q_zvm_sent, q_zvm_required,
                      type_zvm,
                      q_zvm_sent - q_zvm_required   q_rest_plan,
                      q_rest_current,
                      color, parent_id_spc2,
                      0, 0
                   from (

                       
                       ~($X
                           # для быстроты открытия ставим фильтр по материалам не на первую строку _все_
                           ($if (global.loading == 1)
                             (
                               ($if (($g.gr@size) > 2)
                                  ($g.gr@FastSetCurRow 3)
                                $else
                                  ($g.gr@FastSetCurRow 1)
                               )
                               ($g.gr@notifyViews)
                              )
                           )
                           ($ret '')
                        )~

                       select date_sd, num_sd, id_spc, id_head, id_unit,
                                   id_active_material id_active, name_active, analog, analog_id, name_original_active,
                                   obozn,
                                   q1_spc,

                                   case      --- данный материал не используется в заменах - просто выводим его кол-во [выдать в цех] 
                                        when (select count(1) from spc sz where sz.id_head = head.id_head and sz.parent_id_spc = head.id_active_material) = 0
                                        then q2_spc

                                        -- [выдать в цех] по заменяющему материалу считаем со всеми заменами, где он используется
                                   else (select sum(sss.q2_spc) from spc sss where sss.id_head = head.id_head and (sss.parent_id_spc = head.id_active_material or sss.id_active = head.id_active_material) )
                                   end q2_spc, 

                                   q5_spc,
                                   price1_spc,
                                   price2_spc,
                                   0 q_zvm_treb,
                                   big3.comment_spec(id_active_material, NULL, ~($try (ARGUMENTS.0) $catch ('NullException') (0) )~) comments,
                                   sum1_sd,
                                   comment_sd,
                                   type_zvm,
                                   work_type, kod_prod, name_prod,
                                   util.moved_to_production_analog_2(nvl(id_active_parent, id_active_material),  trunc(head.date1, 'month'), last_day(trunc(head.date1, 'month')), '~global.sklad~') perem,
                                   doc_lib.getRestAnalogBeforeAll(nvl(id_active_parent, id_active_material),
                                                                  1e30,
                                                                  '~global.sklad~'
                                                                 ) q_dep11, 


                                   doc_lib.getRestAnalogBeforeAll(nvl(id_active_parent, id_active_material),
                                                                  1e30,
                                                                  '~global.sklad_tseh~'
                                                                 ) q_dep22, 

                                   doc_lib.getRestAnalogBeforeAll(nvl(id_active_parent, id_active_material),
                                                                  1e30,
                                                                  '225'
                                                                 ) - q1_spc q_rest_current,
                                   util.zvm_required(nvl(id_active_parent, id_active_material), head.id_head ) q_zvm_required, 
                                   util.not_zvm_sent(nvl(id_active_parent, id_active_material), head.id_head ) q_not_zvm_sent, 
                                   util.zvm_sent(nvl(id_active_parent, id_active_material), head.id_head ) q_zvm_sent, 

                                  q_moved,
                                  parent_id_spc,
                                  parent_id_spc2,
                                  ( select sum(s.q1_spc) from spc s, head h
                                     where s.id_head = h.id_head
                                       and h.parent_id_head = head.id_head
                                       and s.id_active = head.id_active_material
                                       and h.id_type_doc in(96, 57)
                                       and s.q1_spc!= 0
                                  ) child_spc,
                                  replace_material,

                                  ------ подсветка --------

                                  -- перемещено?
                                  case when
                                       ( select sum(s.q1_spc) from spc s, head h
                                         where s.id_head = h.id_head
                                           and h.parent_id_head = head.id_head
                                           and s.id_active = head.id_active_material
                                           and h.id_type_doc in(96, 57)
                                          and s.q1_spc!= 0
                                        ) > 0
                                  then 'GREEN'
                                    else
                                      -- есть аналоги?
                                      case when (select count(1) from active a
                                                  where a.analog_id = nvl(id_active_parent, id_active_material)
                                                     or a.id_active = (select analog_id from active where id_active = nvl(id_active_parent, id_active_material))
                                                ) > 0
                                        then '#FFCCCC'
                                      else
                                         -- есть замена?
                                         case when replace_material is not null
                                            then 
                                               'MAGENTA'
                                            else
                                              NULL
                                         end
                                      end
                                  end color,

                                  ------ подсветка --------


                                  trunc (head.date1,'month') dat1
                             from (
                                  
                            select to_char(head.date_sd, 'dd.mm.yyyy') date_sd, head.date_sd date_sd_date, head.date1, head.num_sd, head.sort,
                                   spc.id_spc, spc.id_head, spc.id_unit,
                                   active_material.id_active id_active_material, active_parent.id_active id_active_parent, active_material.name_active,
                                   nvl(active_material.analog_id, 0) analog,
                                   nvl(active_material.analog_id, active_material.id_active) analog_id, active_original.name_active name_original_active,
                                   active_pseudo_unit.obozn,
                                   spc.q1_spc,
                                   spc.q2_spc,
                                   spc.q5_spc,
                                   spc.price1_spc,
                                   spc.price2_spc,
                                   null sum1_sd, ' ' comment_sd,
                                   1 type_zvm,
                                   null work_type, 0 kod_prod, ' ' name_prod,
                                  0 q_moved,
                                  spc.parent_id_spc,
                                  active_parent.name_active replace_material,
                                  spc.parent_id_spc2
                              from spc, active active_material, active_pseudo_unit, head, department, active active_parent, active active_original
                             where spc.id_head = ~($try (ARGUMENTS.0) $catch ('NullException') (0) )~
                               and head.id_department1 = department.id_department
                               and spc.id_head = head.id_head
                               and active_material.id_Active = spc.id_active
                               and spc.id_unit = active_pseudo_unit.id_pseudo_unit 
                               and nvl(spc.parent_id_spc, 1) != 0
                               and spc.parent_id_spc = active_parent.id_active (+)
                               -- кроме группы Прочие материалы
                               --and spc.id_active not in ( select id_active from active connect by prior id_active = parent_act_id start with id_active = 13292 )
                               and active_material.analog_id = active_original.id_active (+)
                                  ~($if (($g.gridds_work_stage@size) > 0)
                                       ($if (($g.gridds_work_stage@getValue 'work_stage.id_work_stage') != -999)
                                          (' and spc.work_type = ' + ($g.gridds_work_stage@getValue 'work_stage.id_work_stage'))
                                        $else ('
                                        -- all work stages
                                        ')
                                       )

                                    $else ('
                                        -- all work stages
                                        ')

                                    )~

                                   ~($if (($try ($g.gr@getValue 'id_active') $catch ('Any')(-1000000)) == -1000000)
                                       ( ' ' )
                                    $else
                                       ('
                                         and spc.id_active
                                          in (
                                              select active.id_active
                                                from active
                                             connect by prior id_active = parent_act_id
                                               start with id_active = ' + ($try ($g.gr@getValue 'id_active') $catch ('Any')(-1000000)) + '
                                             )
                                        ')
                                    )~
                                   ) head


                        ) spc

                  -- чтобы если есть замена, эта строка вставала рядом с тем, что заменяют
                  order by decode(replace_material, null, name_active, replace_material)
                  "
             editable = yes
               unique = "('spc','spc.id_spc')" 
              default = "spc.id_head=~($g.d_spc@getValue 'spc.id_head')~,
                         spc.id_spc=~ds.spk~,
			 spc.q1_spc=~0~,
			 spc.q2_spc=~0~"  


			 selaction = "~($X
			                 (global.id_active = ($g.d_spc@getValue 'spc.id_active'))
			                 ($debug 'global.id_active: ' + global.id_active)
			                 ($g.gridprod@retrieve)
			                 ($g.gridprod@setColumnTitle 'COLUMN_NAME_MATERIAL' ($($g.gridprod@GetDatastore)@getValue 'name_active_material'))
			                 ($ret '')
			               )~"


} // ds
} // grid
 } // tabset
} // horizontal split
{button
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  label = "Заполнить по спецификации"      font_size = 14
  action = "($X
                ($if (($g.d_spc@size) > 0)
                   ($if (($g.self@sure 'Перезаполнение документа по спецификации удалит все существующие строки документа. Продолжить?' 0) == 0) ($ret '') )
                )
                ($g.self@doAction 'execute exec util.fill_zvm(' + (ARGUMENTS.0) + ')')
                ($g.self@doAction 'execute exec commit')
                (global.loading = 1)
                ($g.grid1@retrieve)
                ($g.gridspc@retrieve)
                (global.loading = 0)
                ($paint1 0)
                ($ret '')
            )"
}
{button
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  label = "Удалить выделенные строки" font_size = 14
   action="($X
                ($debug '******** selection: ' + ($g.GRIDSPC@selectionValues 'column_id_spc'))

                ($if (($g.GRIDSPC@selectionValues 'column_id_spc') == 'NIL') (  ($g.self 'Не выделено ни одной строки.' 0) ($ret '') ) )

                (a = ($g.GRIDSPC@selectionValues 'column_id_spc'))
                (a_sz = ($a@size))
                (sz = ($g.d_spc@size))


                ($if (($g.self@sure 'Удалить выделенные строки (' + ($a_sz@int) + ' шт.) ?' 0) == 0)  ($ret '') )

                (j = 0)

                ($while (j < a_sz)
                  (
                    ($g.self@doAction 'execute exec delete from spc where id_spc = ' + ($a@ j))
                    ($g.self@doAction 'execute exec commit')
                    (k = 0)
                    ($while (k < sz)
                      (
                        ($($g.GRIDSPC@getDatastore)@fastSetCurRow k)
                        ($if ( ($($g.GRIDSPC@getDatastore)@getValue 'spc.id_spc') == ($a@ j) )
                          ($($g.GRIDSPC@getDatastore)@delRow k)
                        )
                        (k += 1)
                      )
                    )
                    (j += 1)
                  )
                )
                ($($g.GRIDSPC@getDatastore)@notifyViews)
                #($g.gridspc@retrieve)
                ($paint1 0)
                ($ret '')
            )"
 }
{button
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  label = "Печать (Ctrl+P)"     font_size = 14
  action = "($X
                ($($g.self@getAliases)@put '_DS' g.d_spc) 
                ($g.self@doAction 'open pech_form/pech_form_zvm2.rml &_DS')
                ($ret '')
            )"
  shortcut = "Ctrl+P"
}
{button
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  label = "Выход (ESC)"       font_size = 14
  action = "($g.self@actcancel)"
  shortcut = "escape"
}
} // toolbar

