Cp1251
DOC
foreground = lightgray 
document_width = 750
document_height = 550
document_title = "ZetaSE.Autocomplect/ Заявки на выдачу материалов"
preloadscript="
(global.retr = 0)
(global.id_h = 0)
(global.show_all = 0)
"

postloadscript="($X
                   #($g.grid_group1@allign)

                   ($defun grid_color<arg>
                     ($X
                        (row = ($g.gridds01@getCurRow))
                        (i = 0)
                        ($while (i < ($g.gridds01@size))
                          (
                            ($g.gridds01@fastSetCurRow i)
                            ($debug 'row ' + i + ' ACCEPT1: ' + ($g.gridds01@getValue 'accept1'))
                            ($if (($try ($g.gridds01@getValue 'accept1') $catch ('NullException')(0)) == 'X')
                                  ($g.grid_group1@setRowBgColor i 'GREEN')
                             $else
                                  ($g.grid_group1@setRowBgColor i 'WHITE')
                            )
                            (i += 1)
                          )
                        )
                        ($g.gridds01@fastSetCurRow row)
                        ($ret '')
                      )
                   )

                   ($grid_color)

                   ($ret '')
                )"
{DSCollection
   alias = dscoll
   SeqQuery = "select to_char(sysdate) from dual"
   seqnames = "date"
}
{ datastore
  alias = d_spc_doc
  query = "select cnt from 
              (select count(1) cnt from spc where id_head = ~global.id_h~ and parent_id_spc is null)"
}

{ datastore
  alias = d_perem
  query = "select id_head from head where parent_id_head = ~global.id_h~"
}

{toolbar   BACKGROUND = "LIGHTGRAY"
{splitpanel type=vertical     percent=20
{treeview2
   alias=tree1  
   listAction="~($X
                 ($g.self@doAction 'retrieve grid_group1')
                 ($grid_color)
                 ($ret '')
               )~"
   nodeAction="~($X
                 ($g.self@doAction 'retrieve grid_group1')
                 ($grid_color)
                 ($ret '')
               )~"
   nodefont="Courier,bold|italic,12"      pointfont="Courier,italic,12"  
   hilite=gray,white
   node=blue   point=blue
   rootname="Заявки"
   hiliting=on
   storepath = yes

   {menu
        {item label = "Показать все документы"
         action="~($X
                    (global.show_all = 1)
                    ($g.self@doAction 'retrieve tree1')
                    ($ret '')
                  )~"
        }
   }

        {groupreport 
          alias = datastore1
          query = "select distinct to_char(date_sd,'yyyy') y, to_char(date_sd,'mm') m ,
                          to_char(date_sd,'dd') d, date_sd 
                     from head
                    where head.id_type_doc = 17
                          ~($if (global.show_all != 0)
                              ('')
                            $else
                              # показывать последние два года
                              (' and head.date_sd > trunc(add_months(sysdate, -12), \'year\')')
                           )~
                  "
                 grouping = "y;m;d"
                  sorting = "1;1;1"
        } // end of gr
} // end of tree
{grid
    alias = GRID_GROUP1
    buttonbar_font_family = 0              buttonbar_font_size = 12
    buttonbar_size = 30                    titlebar_size = 18
    multiselect = no                      editable = no
    rowsize = 25

    edit = "~($X
                   (global.id_h = ($($G.GRID_GROUP1@getDatastore)@getValue 'ID_HEAD'))
                   ($if (($g.gridds01@getValue 'type_zvm') == 1)
                     (
                       (r = 'open docs/material2.rml')
                       ($g.d_spc_doc@retrieve)
                       ($if (($g.d_spc_doc@getValue 'cnt') == 0)
                          (
                               ($if (($($G.GRID_GROUP1@getDatastore)@getValue 'id_department1') == 0)
                                 (
                                    ($g.self@sure 'Не указан склад, с которого произойдет списание по данной заявке.' 1)
                                    ($ret '')
                                 )
                               )
                               ($if (($try ($($G.GRID_GROUP1@getDatastore)@getValue 'dep1') $catch ('NullException')(0)) == 0)
                                 (
                                    ($g.self@sure 'У склада \"' + ($($G.GRID_GROUP1@getDatastore)@getValue 'name_sklad') + '\" нет соответствующего ему склада-цеха, на который произойдет перемещение материалов.' 1)
                                    ($ret '')
                                 )
                               )
                               ($g.self@doAction 'execute exec util.fill_zvm(' + global.id_h + ')')
                               ($g.self@doAction 'execute exec commit')
                          )
                       )
                     )
                    $else
                     (r = 'open docs/material.rml')
                   )
                   (r += ' ' + global.id_h)
                   ($ret r)
             )~"
    del = "~($X
               (tmp = 'Вы действительно хотите удалить документ №' + ($($G.GRID_GROUP1@getDatastore)@getValue 'NUM_SD' ) + '')
               (m = ($g.self@sure tmp 0))
               ($if (m == 0)
                   ($ret '')
               )
               ($g.self@doAction 'execute exec util.del_doc(' + ($($G.GRID_GROUP1@getDatastore)@getValue 'ID_HEAD') + '); retrieve tree1; retrieve grid_group1')
            ($ret ''))~"

   {menu
        {item label = "Добавить заявку на продукцию" action="createnew docs/zvm.rml -1 ; retrieve tree1; retrieve grid_group1; ~($X ($grid_color) ($ret ''))~"}
        {item label = "Добавить заявку на список продукции" action="createnew docs/zvm2.rml -1 ;  retrieve tree1; retrieve grid_group1; ~($X ($grid_color) ($ret ''))~"}
        {item label = "-"}
        {item label = "Статус: черновик"
           action = "~($X
	                  ($g.self@doAction 'execute exec update head set accept = 0 where id_head = ' + ($($G.GRID_GROUP1@getDatastore)@getValue 'ID_HEAD'))
                          ($g.self@actSave)
                          ($g.self@doAction 'execute exec commit')
	                  ($g.self@doAction 'retrieve grid_group1')
	                  ($grid_color)
	                  ($ret '')
	              )~"
	}
        {item label = "Статус: утверждена"
           action = "~($X
	                  ($g.self@doAction 'execute exec update head set accept = 1 where id_head = ' + ($($G.GRID_GROUP1@getDatastore)@getValue 'ID_HEAD'))
                          ($g.self@actSave)
                          ($g.self@doAction 'execute exec commit')
	                  ($g.self@doAction 'retrieve grid_group1')
	                  ($grid_color)
	                  ($ret '')
	              )~"
	}
        {item label = "-"}
        {item label = "Скопировать заявку"
           action="createnew docs/~($if (($g.gridds01@getValue 'type_zvm') == 1)
                                    ('zvm2.rml')
                                  $else
                                    ('zvm.rml')
                                 )~ ~($($GRID_GROUP1@getDatastore)@getValue 'ID_HEAD' )~ ;  retrieve tree1; retrieve grid_group1; ~($X ($grid_color) ($ret ''))~" 
        }
        {item label = "-"}
        {item label = "Вывести материалы"
          action="~($X
                   (global.id_h = ($($G.GRID_GROUP1@getDatastore)@getValue 'ID_HEAD'))
                   ($if (($g.gridds01@getValue 'type_zvm') == 1)
                     (
                       (r = 'open docs/material2.rml')
                       ($g.d_spc_doc@retrieve)
                       ($if (($g.d_spc_doc@getValue 'cnt') == 0)
                          (
                               ($if (($($G.GRID_GROUP1@getDatastore)@getValue 'id_department1') == 0)
                                 (
                                    ($g.self@sure 'Не указан склад, с которого произойдет списание по данной заявке.' 1)
                                    ($ret '')
                                 )
                               )
                               ($if (($try ($($G.GRID_GROUP1@getDatastore)@getValue 'dep1') $catch ('NullException')(0)) == 0)
                                 (
                                    ($g.self@sure 'У склада \"' + ($($G.GRID_GROUP1@getDatastore)@getValue 'name_sklad') + '\" нет соответствующего ему склада-цеха, на который произойдет перемещение материалов.' 1)
                                    ($ret '')
                                 )
                               )
                               ($g.self@doAction 'execute exec util.fill_zvm(' + global.id_h + ')')
                               ($g.self@doAction 'execute exec commit')
                          )
                       )
                     )
                    $else
                     (r = 'open docs/material.rml')
                   )
                   (r += ' ' + global.id_h)
                   ($ret r)
             )~"}

        {item label = "Невыданные материалы"
          action="~($X
                     (r = 'open docs/zvm_add.rml ')
                     ($if (($($G.GRID_GROUP1@getDatastore)@size) > 0)
                       (r += '\'' + ($($G.GRID_GROUP1@getDatastore)@getValue 'DATE_CHAR' ) +'\'')
                      $else
                       (r += '\'' + dscoll.date  +'\'')
                     )
                     ($ret r)
                  )~"
        }
        {item label = "Сменно-суточное задание"
          action="~($X
                     (doc = 'docs/ssz.rml')
                     ($if (($($G.GRID_GROUP1@getDatastore)@getValue 'CHILD_ID_HEAD' ) == -999)
                       ($ret 'createnew ' + doc + ' -1, ' + ($($G.GRID_GROUP1@getDatastore)@getValue 'ID_HEAD' ) + '; retrieve grid_group1')
                      $else
                       ($ret 'opennew ' + doc + ' ' + ($($G.GRID_GROUP1@getDatastore)@getValue 'CHILD_ID_HEAD' ) + ', -1; retrieve grid_group1')
                     )
                  )~"
        }
        {item label = "-"}
        {item label = "Обновить" action="retrieve tree1;retrieve grid_group1"}
        {item label = "-"}
        {item label = "Удалить"
           action = "~($X
                       ($IF (($try ($($G.GRID_GROUP1@getDatastore)@getValue 'ACCEPT1' ) $catch ('NullException')(0)) == 1)
		             ((tmp = 'Запрещено удалять проведенный документ')
            		      (m = ($g.self@sure tmp 1))
		             )
			$else
	                 (
	                  # проверка, нет ли перемещения
                          (global.id_h = ($($G.GRID_GROUP1@getDatastore)@getValue 'ID_HEAD'))
	                  (tmp = 'Вы действительно хотите удалить документ №' + ($($G.GRID_GROUP1@getDatastore)@getValue 'NUM_SD' ) + ' ?')
	                  ($g.d_perem@retrieve)
	                  ($if (($g.d_perem@size) > 0)
	                     (tmp += '%%По этой заявке создано перемещение. Оно тоже будет удалено.' 1)
	                  )

	                  (m = ($g.self@sure tmp 0))
	                  ($if (m == 0)
	                   ($ret '')
	                  )
	                  ($g.self@doAction 'execute exec util.del_doc(' + ($($G.GRID_GROUP1@getDatastore)@getValue 'ID_HEAD') + ');retrieve grid_group1')
			 )
		       )
		      ($ret ''))~"
        }
        {item label = "-"}
        {item label = "История изменений документа" action= "open reports/report_head_log.rml ~($($G.GRID_GROUP1@getDatastore)@getValue 'ID_HEAD')~"}
   }
   {column size=70             title="id_head"      alias=id_hd
        target=id_head        title_font_family=2     title_font_color="#000000"
        halignment=right           font_size = 12          font_family=0
        visible = no            alias=idh    editmask = "##0."
   }
   {column size=40             title="X"
        target=accept1         title_font_family=2     title_font_color="#000000"
                               font_color="#000000"     
        font_size = 12         font_family=0
        halignment=center
   }
   {column size=50             title="Выд."
        target=zvm_percent_moved     title_font_family=2     title_font_color="#000000"
        halignment=center            font_color="#000000"     
        font_size = 12             font_family=0
        halignment=center
   }
   {column size=60             title="N док."
        target=num_sd         title_font_family=2     title_font_color="#000000"
        halignment=left            font_color="#000000"     
        font_size = 12             font_family=0
   }
   {column size=70             title="Дата док."
        target=date_sd        title_font_family=2     title_font_color="#000000"
        halignment=center          font_size = 12          font_family=0
   }
   {column size=80             title="Месяц"
        target=month_sd        title_font_family=2     title_font_color="#000000"
        halignment=center          font_size = 12          font_family=0
   }
   {column size=120             title="Склад"
        target=name_sklad
        title_font_family=2        title_font_color="#000000"
        halignment=center            font_size = 12          font_family=0
   }
   {column size=80             title="Участок"
        target=name_work_area
        title_font_family=2        title_font_color="#000000"
        halignment=center            font_size = 12          font_family=0
   }
   {column size=350             title="Продукция"
        target=name_prod
        title_font_family=2        title_font_color="#000000"
        halignment=center            font_size = 12          font_family=0
   }
   {column size=170             title="Примечание"
        target=comment_sd
        title_font_family=2        title_font_color="#000000"
        halignment=left            font_size = 12          font_family=0
   }
   {column size=170             title="Автор"
        target=author        title_font_family=2     title_font_color="#000000"
        halignment=left          font_size = 12          font_family=0
        editable = readonly
   }
//   {column size=170             title="Последнее изменение"
//        target=last_changed        title_font_family=2     title_font_color="#000000"
//        halignment=left          font_size = 12          font_family=0
//        editable = readonly
//   }

   {datastore 
           alias = GRIDDS01
           query = "select id_head, num_sd, date_sd, date_char, month_sd, comment_sd, accept1, type_zvm, author, last_changed, name_sklad, name_prod, id_department1, dep1,
                           child_id_head, name_work_area, zvm_percent_moved
                      from (
                    select head.id_head, head.num_sd, head.date_sd, to_char(head.date_sd, 'dd.mm.yyyy') date_char, to_char(head.date1, 'Month YY') month_sd,
                           head.id_department1, department.name1_dep name_sklad, department.dep1, head.comment_sd,
                           head.id_department2, dep2.name1_dep,
                           nvl(head.id_contract, 0) type_zvm,
                           decode(head.id_contract, 1, util.zvm_prod_names(head.id_head), active.name_active) name_prod,
                           decode(head.accept, 0, '', 1, 'X', -1, '--', 'Z') accept1,
                           util.zvm_percent_moved(head.id_head) zvm_percent_moved,
                           nvl(head_child.id_head, -999) child_id_head,
                           document_author(head.id_head) author,
                           personal.sname||' '||personal.fname last_changed,
                           work_area.name_work_area
                      from head, department, department dep2, personal, active, head head_child, work_area
                     where head.date_sd ~ 
         ($if (($g.tree1@getLevel) == 0)
                   ($try ('=to_date(\''+
                   #  '01-01-0001' +
                   dscoll.date+                 
                  '\',\'dd-mm-yyyy\')') 
                $catch ('Any') ('=\'11-11-1999\'')
                    )
                $if (($g.tree1@getLevel) == 1)
                    ($try ('between
                            to_date(\'01-01-'+
                            datastore1.y+
             '\',\'dd-mm-yyyy\')
                         and
                            to_date(\'31-12-'+
                            datastore1.y+
             '\',\'dd-mm-yyyy\')')
                     $catch ('Any') ('=\'01-01-1999\'')
                     )               
                 $if (($g.tree1@getLevel) == 2)
                     ($if (($@number (datastore1.m)) == 12)
                          ($try ('between
                                  to_date(\'01-12-'+
                                  datastore1.y+
                   '\',\'dd-mm-yyyy\')
                                  and
                                  to_date(\'31-12-'+
                                  datastore1.y+
                   '\',\'dd-mm-yyyy\')')
                            $catch ('Any') ('=\'01-01-1999\'')
                            )                                  
                      $else ($try (' >= to_date(\'01-'+
                                      datastore1.m+                
                       '-'+
                       datastore1.y+
                       '\',\'dd-mm-yyyy\') 
                                  and 
                              head.date_sd < to_date(\'01-'+
                       ($(($@number (datastore1.m))+1)@int)+
                                      '-'+
                                      datastore1.y+
                       '\',\'dd-mm-yyyy\')')
                      $catch ('Any') ('=\'11-11-1999\'')
                      )  
                     )              
                 $else ($try ('=to_date(\''+
                  datastore1.d+
                  '-'+
                  datastore1.m+
                  '-'+
                  datastore1.y+
                  '\',\'dd-mm-yyyy\')') 
                    $catch ('Any') ('=\'11-11-1999\'')
                        )
                 ) ~ 
             and head.id_department1 = department.id_department (+)                 
             and head.id_department2 = dep2.id_department (+)                    
             and head.id_type_doc = 17
             and head.last_changed = personal.id_personal (+)
             and head.id_active = active.id_active
             and head.id_head = head_child.parent_id_head (+)
             and head_child.id_type_doc (+) = 7
             and head.type1 = work_area.id_work_area (+)
                    )
           order by date_sd, to_number(substr(num_sd, 5))

                  ~($X 
                        ($if (($g.gridds01@size) > 0)
                           (global.id_h = ($g.gridds01@getValue 'ID_HEAD' ))
                        ) 
                        ($ret '')
                   )~

           "
       {datastore
            alias = set_row_ds
            query = "select user from dual 
                     ~($X 
                        (t = ($g.gridds01@iterator))
                        (row = 0)
                        (f=0)
                        ($while ($t@next)
                          (
                             ($if (global.id_h != ($($t@value)@field 'ID_HEAD'))
                                   (row = row + 1)
                              $else 
                                (
                                   (f=1) 
                                   ($break)
                                )
                             )
                          )
                        )
                        ($if (f==1)
                          ($if (($g.gridds01@size) > 0 )
                            (
                              ($g.gridds01@setCurRow row)
                              ($g.gridds01@notifyviews)
                            )
                          )
                        )
                        (global.id_h = 0)
                        ($ret '')
                      )~"
            editable = no
       } // ds set_row_ds


   } // end of ds
} // end of grid
} // end of vertical split
{button
  left = 72  width = 50    height = 20
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  label = "Выход ESC"
  action = "($g.self@actcancel)"
  shortcut = "escape"
}

} // toolbar
