Cp1251
DOC 
foreground = lightgray
document_width = 900
document_height = 700
document_title = "ZetaSE.Autocomplect/ Документ Списание ТМЦ"
preloadscript="
(global.loading = 1)
(global.create = 0)
(global.d1 = '01.01.2000')
(global.get_num = 0)
(global.cur_id_active = -1)
(global.recalc = 0)
(global.changing = 0)
"
postloadscript="($X
	 	 ($IF (($g.D_HEAD@getvalue 'HEAD.ACCEPT') == 1)
		  ($g.progress@setValue 'Только для чтения')
		 )
		 ($debug '**********************************')
		 ($debug 'ARGUMENTS.0: ' + ARGUMENTS.0)
		 ($debug 'ARGUMENTS.1: ' + ARGUMENTS.1)
                 (global.no_combo_action = 1)
                 ($g.self@doAction 'retrieve combo1')
                 ($g.self@doAction 'retrieve combo2')
                 (global.no_combo_action = 0)

                 (sz = ($g.data_ds1@size))

                 ($if (global.create == 0)
                   (
                     # если данный документ уже существует, надо установить комбобоксы и галки в соответствии со значениями, сохраненными в этом документе
                     (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)

                     (i = 0) (found_i = 0)
                     ($while (i < sz)
                       (
                         ($g.data_ds2@setCurRow i)
                         ($if (($g.data_ds2@getValue 'id') == ($try ($g.d_head@getValue 'head.type1') $catch ('NullException')(-999)) )
                           (
                             ($g.combo2@setSelectedItem i)
                             (found_i = i)
                           )
                          )
                          (i += 1)
                       )
                     )
                     ($g.data_ds2@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)
                   )
                 )
                 (global.loading = 0)
		 ($ret '')
		)"

{dscollection
   alias = ds
   initQuery = "select head_sq.nextval,sysdate,trunc(sysdate, 'month') month1,id_personal from personal where upper(login)=user"
   aliases = "hpk,date,month1,idp"
   seqquery = "select spc_sq.nextval from dual"
   seqnames = "spk"
   initAction ="(global.create = 1)
                (global.get_num = 1)
                ($X 
		 ($G.get_type_doc_rod@retrieve)
		 ($IF (($G.get_type_doc_rod@getvalue 'ID_TYPE_DOC') != 54)
		   (($G.substore@retrieve)
                    ($if (($G.substore@size) !=0)
                      (
                       (tmp = ($try ($g.substore@getvalue 'SPC.ID_HEAD') $catch ('NullException')(0)))
                       ($debug ' parent doc: id_head = ' + tmp)
                       ($G.d_head@setvalue '' tmp)
 	               (sz = ($g.substore@size))
                       (i = 0)
	               ($while (i < sz)
                        (
                         ($g.substore@SetCurRow (i))
                         ($G.d_spc@newrow)
                         (tmp = ($try ($g.substore@getvalue 'SPC.ID_ACTIVE') $catch ('NullException')(0)))
                         ($G.d_spc@setvalue 'SPC.ID_ACTIVE' tmp)
                         (tmp = ($try ($g.substore@getvalue 'NAME_ACTIVE') $catch ('NullException')(' ')))
                         ($G.d_spc@setvalue 'ACTIVE.NAME_ACTIVE' tmp)
                         (tmp = ($try ($g.substore@getvalue 'SPC.ID_UNIT') $catch ('NullException')(0)))
                         ($G.d_spc@setvalue 'SPC.ID_UNIT' tmp)
                         (tmp = ($try ($g.substore@getvalue 'SPC.Q1_SPC') $catch ('NullException')(0)))
                         ($G.d_spc@setvalue 'SPC.Q1_SPC' tmp)
                         # если родительский документ - перемещение, нужно брать остаток по складу, куда перемещаем
                         # отстаток считаем на конец даты, в которой заводится документ
                         ($if (($G.get_type_doc_rod@getvalue 'ID_TYPE_DOC') == 96)
                            (tmp = ($try ($g.substore@getvalue 'REST2') $catch ('NullException')(0)))
                          $else
                            (tmp = ($try ($g.substore@getvalue 'REST1') $catch ('NullException')(0)))
                         )
                         ($G.d_spc@setvalue 'DOC_LIB.GETRESTBEFOREBEFORE(HEAD.ID_DEPARTMENT1,SPC.ID_ACTIVE,HEAD.SORT)' tmp)
                         (tmp = ($try ($g.substore@getvalue 'SPC.Q2_SPC') $catch ('NullException')(0)))
                         ($G.d_spc@setvalue 'SPC.Q2_SPC' tmp)
                         (tmp = ($try ($g.substore@getvalue 'SPC.PRICE1_SPC') $catch ('NullException')(0)))
                         ($G.d_spc@setvalue 'SPC.PRICE1_SPC' tmp)
                         (tmp = ($try ($g.substore@getvalue 'SPC.PRICE2_SPC') $catch ('NullException')(0)))
                         ($G.d_spc@setvalue 'SPC.PRICE2_SPC' tmp)
                         (tmp = ($try ($g.substore@getvalue 'OBOZN') $catch ('NullException')(' ')))
                         ($G.d_spc@setvalue 'ACTIVE_PSEUDO_UNIT.OBOZN' tmp)
                         (i = i + 1)
                        )
                       ) 
                      ) 
                    )
                    ($G.copy@retrieve)
                    ($if (($G.copy@size) !=0)
                      (
                       ($g.copy@SetCurRow (0))
                       (tmp = ($try ($g.copy@getvalue 'HEAD.ID_HEAD') $catch ('NullException')(0)))
                       ($debug ' parent doc: id_head = ' + tmp)
                       ($G.d_head@setvalue 'HEAD.PARENT_ID_HEAD' tmp)
                       ($debug '-- copying document, parent_id_head: ' + ($G.d_head@getvalue 'head.parent_id_head'))
                       ($if (($g.copy@getvalue 'HEAD.ID_TYPE_DOC') == 96)
                        (
                         (tmp = ($try ($g.copy@getvalue 'HEAD.ID_DEPARTMENT2') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.ID_DEPARTMENT1' tmp)
                         (tmp = ($try ($g.copy@getvalue 'DEPARTMENT2.NAME1_DEP') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'DEPARTMENT.NAME1_DEP' tmp)
                         (tmp = ($try ($g.copy@getvalue 'HEAD.NUM_SD') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.NUM_PER_SD' tmp)                         
                         (tmp = ($try ($g.copy@getvalue 'HEAD.DATE_SD') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.DATE_PER_SD' tmp)                          
                        )
                        $else
                        (
                         (tmp = ($try ($g.copy@getvalue 'HEAD.ID_DEPARTMENT1') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.ID_DEPARTMENT1' tmp)
                         (tmp = ($try ($g.copy@getvalue 'DEPARTMENT.NAME1_DEP') $catch ('NullException')('')))
                         ($G.d_head@setvalue 'DEPARTMENT.NAME1_DEP' tmp)
                         (tmp = ($try ($g.copy@getvalue 'HEAD.NUM_PER_SD') $catch ('NullException')('')))
                         ($G.d_head@setvalue 'HEAD.NUM_PER_SD' tmp)
                         (tmp = ($try ($g.copy@getvalue 'HEAD.DATE_PER_SD') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.DATE_PER_SD' tmp)
                         (tmp = ($try ($g.copy@getvalue 'HEAD.ID_CLIENT1') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.ID_CLIENT1' tmp)
                         (tmp = ($try ($g.copy@getvalue 'HEAD.COMMENT_SD') $catch ('NullException')('')))
                         ($G.d_head@setvalue 'HEAD.COMMENT_SD' tmp)
                         (tmp = ($try ($g.copy@getvalue 'CLIENT.NAME1_CLIENT') $catch ('NullException')('')))
                         ($G.d_head@setvalue 'CLIENT.NAME1_CLIENT' tmp)
                        )
                       ) 
                      )
                    )
		   )
		$else
		  (($G.substore3@retrieve)
                    ($if (($G.substore3@size) != 0)(
                         (tmp = ($try ($g.substore3@getvalue 'HEAD.ID_DEPARTMENT1') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.ID_DEPARTMENT1' tmp)
                         (tmp = ($try ($g.substore3@getvalue 'DEPARTMENT.NAME1_DEP') $catch ('NullException')('')))
                         ($G.d_head@setvalue 'DEPARTMENT.NAME1_DEP' tmp)
                         (tmp = ($try ($g.substore3@getvalue 'HEAD.NUM_SD') $catch ('NullException')('')))
                         ($G.d_head@setvalue 'HEAD.NUM_PER_SD' tmp)
                         (tmp = ($try ($g.substore3@getvalue 'HEAD.DATE_SD') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.DATE_SD' tmp)
                         ($G.d_head@setvalue 'HEAD.DATE_PER_SD' tmp)
                         (tmp = ($try ($g.substore3@getvalue 'HEAD.ID_CLIENT1') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.ID_CLIENT1' tmp)
                         (tmp = ($try ($g.substore3@getvalue 'HEAD.COMMENT_SD') $catch ('Any')(' ')))
                         ($if (tmp != 0)
                            ($G.d_head@setvalue 'HEAD.COMMENT_SD' tmp)
                         )
                         (tmp = ($try ($g.substore3@getvalue 'HEAD.ID_HEAD') $catch ('NullException')(0)))
                         ($G.d_head@setvalue 'HEAD.PARENT_ID_HEAD' tmp)
			)
		   )                     
                  ($G.substore2@retrieve)
                  (t = ($G.substore2@iterator))
                  ($while ($t@next)(
                      ($G.d_spc@newrow)
                         ($debug 'adding row')
		         (tmp = ($($t@value)@field 'ID_ACTIVE'))
                         ($G.d_spc@setvalue 'SPC.ID_ACTIVE' TMP)
                         (tmp = ($($t@value)@field 'NAME_ACTIVE'))
                         ($G.d_spc@setvalue 'ACTIVE.NAME_ACTIVE' tmp)
                         (tmp = ($($t@value)@field 'ID_UNIT'))
                         ($G.d_spc@setvalue 'SPC.ID_UNIT' tmp)
                         (tmp = ($($t@value)@field 'C')*(-1))
                         ($G.d_spc@setvalue 'SPC.Q1_SPC' tmp)
                         (tmp = ($($t@value)@field 'REST'))
                         ($G.d_spc@setvalue 'DOC_LIB.GETRESTBEFOREBEFORE(HEAD.ID_DEPARTMENT1,SPC.ID_ACTIVE,HEAD.SORT)' tmp)
                         (tmp = ($($t@value)@field 'PRICE1_SPC'))
                         ($G.d_spc@setvalue 'SPC.PRICE1_SPC' tmp)
                         (tmp = ($($t@value)@field 'OBOZN'))
                         ($G.d_spc@setvalue 'ACTIVE_PSEUDO_UNIT.OBOZN' tmp)
                        )
                       )
		     )
		  )
                ($G.d_head@notifyviews)          
                ($G.d_spc@notifyviews)
                ($debug '--end of postloadscript -- parent_id_head: ' + ($G.d_head@getvalue 'head.parent_id_head'))
                ($ret 1))"
}
{datastore  alias=doc_num
    query="select num_sd from (select 'Сп-'||(nvl(max(to_number(substr(num_sd, 4))), 0) + 1) num_sd
             from head
            where head.num_sd like 'Сп-%')"
  } // end of ds

{datastore  alias=ds_rest
    query="select rest
             from (
              select doc_lib.getRestBefore(
                                               ~($try (g.sklad_t) $catch ('NullException')(-1))~,
                                               ~global.cur_id_active~,
                                               ~($if (global.create == 0)
                                                    ($try ($g.d_head@getValue 'head.sort') $catch ('NullException')(-1))
                                                 $else 
                                                    ('(
                                                           select nvl(max(h.sort), 1e30)
                                                             from head h where trunc(h.date_sd, \'month\') = trunc(to_date(\'' + ($try (global.d1) $catch ('Any')('01.01.2000')) + '\', \'dd.mm.yyyy\'), \'month\') 
                                                                           and h.date_sd <= to_date(\'' + ($try (global.d1) $catch ('Any')('01.01.2000')) + '\', \'dd.mm.yyyy\')
                                                      )')
                                                )~
                                             ) rest
             from dual
                  )"
} // end of ds

  {datastore  
	alias=get_type_doc_rod
	query="select id_type_doc
                 from head
                where id_head=~($try (ARGUMENTS.1) $catch ('NullException') (-1) )~"
  } // end of ds
 {datastore
              alias = substore
              query = "select spc.id_active,spc.id_head,spc.id_unit,spc.q1_spc,spc.q2_spc,spc.price1_spc,spc.price2_spc,
                              name_active, obozn,
                              rest1,
                              rest2,
                              rest3
                       from   (
                       select spc.id_spc, spc.id_active,spc.id_head,spc.id_unit,spc.q1_spc,spc.q2_spc,spc.price1_spc,spc.price2_spc,
                              active.name_active,active_pseudo_unit.obozn,
                              doc_lib.getRestBefore(head.id_department1,active.id_active, head.sort)/active_pseudo_unit.koeff   rest1,
                              doc_lib.getRestBefore(head.id_department2,active.id_active, head.sort)/active_pseudo_unit.koeff   rest2,
                              doc_lib.getRestBefore(head.id_department2,active.id_active,
                                                      (
                                                           select nvl(max(h.sort), 1e30)
                                                             from head h where trunc(h.date_sd, 'month') = trunc(head.date_sd, 'month') 
                                                                           and h.date_sd <= to_date('~($try (global.d1) $catch ('Any')('01.01.2000'))~', 'dd.mm.yyyy')
                                                      )
                                                    )/active_pseudo_unit.koeff   rest3
                       from active,head,spc,active_pseudo_unit
                       where spc.id_head=~($try ($if ((ARGUMENTS.1) == 'NOTHING') (-1) $else (ARGUMENTS.1)) $catch ('NullException') (-1) )~
                             and spc.id_head = head.id_head
                             and spc.id_active = active.id_active 
                             and spc.id_unit = active_pseudo_unit.id_pseudo_unit (+)
                                 ) spc
                       order by spc.id_spc"
 }  
 {datastore
     alias = substore2
     query = "select id_active,id_unit,c,price1_spc,
                     name_active,obozn
                from (
	      select spc.id_active,spc.id_unit,spc.price1_spc,
		     spc.q1_spc -  doc_lib.getRestBefore(head.id_department1,active.id_active, head.sort)/active_pseudo_unit.koeff   c,
		     doc_lib.getRestBefore(head.id_department1,active.id_active,
                                                      (
                                                           select nvl(max(h.sort), 1e30)
                                                             from head h where trunc(h.date_sd, 'month') = trunc(head.date_sd, 'month') 
                                                                           and h.date_sd <= to_date('~($try (global.d1) $catch ('Any')('01.01.2000'))~', 'dd.mm.yyyy')
                                                      )
                                                    )/active_pseudo_unit.koeff rest,
                     active.name_active,active_pseudo_unit.obozn
                from active,spc,head,active_pseudo_unit
               where spc.id_head=~($try (ARGUMENTS.1) $catch ('NullException') (-1) )~
                 and head.id_head = spc.id_head
                 and spc.id_active = active.id_active 
                 and spc.id_unit = active_pseudo_unit.id_pseudo_unit 
                     )
		      where c < 0"
 }  
 {datastore
      alias = substore3
      query = "select head.id_head,
                      head.num_sd,
                      head.date_sd,
                      head.id_department1,department.name1_dep,
                      head.id_client1,head.id_client2,
                      head.comment_sd
                 from head,department
                where head.id_head=~($try (ARGUMENTS.1) $catch ('NullException') (-1) )~
                  and head.id_department1 = department.id_department "
 }
 {datastore
   alias = copy
   query = "select head.id_head,
                   head.num_sd,
                   head.date_sd,
                   head.id_type_doc,
                   head.num_per_sd,head.date_per_sd,
                   head.id_department1,department.name1_dep,head.id_department2,department2.name1_dep,
                   head.id_client1,head.id_client2,
                   head.comment_sd,
                           head.parent_id_head,
    	                   client.name1_client
            from head,department,client,department department2
            where head.id_head=~($try ($if ((ARGUMENTS.1) == 'NOTHING') (-1) $else (ARGUMENTS.1)) $catch ('NullException') (-1) )~
                  and head.id_department2 = department2.id_department (+)
                  and head.id_department1 = department.id_department (+)
                          and head.id_client1 = client.id_client (+)"
 }
{ datastore
  alias = check_month
  query = "select month1
             from (
                    select to_char(trunc(to_date('~global.d1~', 'dd.mm.yyyy'), 'month'), 'Month YYYY') month1
                      from dual
                  )
                  "
}


{toolbar   BACKGROUND = "LIGHTGRAY"
{splitpanel   percent=25
{scrollpane 
{form width=780 height=208       bg_color="#c0c0c0"   alias = fm
   {label
       value="N док."       
       left=11    top=8     height=20       width=40
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }  
   {field
       target=head.num_sd   border = box
       left=55     top=8    height=20       width=95
       alias=num_sd         font_size=12    font_family=0    bg_color="#ffffff"
       editable=readonly
   }  
   {label
       value="Дата док."    
       left=160    top=8    height=20       width=50
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }
   {field
       target=head.date_sd  border = box
       left=210    top=8    height=20       width=80
       alias=date_sd        font_size=12    font_family=0    bg_color="#ffffff"
       dep="ficd1"
   }
   {field 
       type = string
       left = 500  width = 70 height=25    
       alias=ficd1           visible=no
       exp="($X
                ($if (global.loading == 1) ($ret ''))
                (global.d1 = ($($@date g.date_sd)@toPLString))
                (r = ($g.d_spc@GetCurRow))
                # нужно пересчитать остатки на новую дату
                (i = 0)
                (sz = ($g.d_spc@size))
                ($while (i < sz)
                  (
                    ($g.d_spc@fastSetCurRow i)
                    (global.cur_id_active = ($g.d_spc@getValue 'spc.id_active'))
                    ($g.ds_rest@retrieve)
                    ($g.d_spc@setValue 'DOC_LIB.GETRESTBEFOREBEFORE(HEAD.ID_DEPARTMENT1,SPC.ID_ACTIVE,HEAD.SORT)' ($g.ds_rest@getValue 'rest'))
                    ($debug 'id_active: ' + global.cur_id_active + ', rest: ' + ($g.ds_rest@getValue 'rest'))
                    (g.diff = g.ost - g.q1_spc)

                    ($if (g.diff < 0)
                       ($g.gridspc@setcellbgcolor ($g.d_spc@getCurRow) 4 'RED')
                     $else
                       ($g.gridspc@setcellbgcolor ($g.d_spc@getCurRow) 4 'WHITE')
                    )
                    ( i += 1)
                  )
                )
                ($g.d_spc@setCurRow r)
                ($g.d_spc@notifyViews)
            )"
   }
   {field
       target=head.accept    border = box   visible=no
       left=200   top=83    height=20      width=40
       alias=accept         font_size=12    font_family=0    bg_color="#ffffff"
   }
   {label
       value="Склад"        
       left=320    top=8    height=20       width=40
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }
   {field
       target=head.id_department1
       visible=no           border = box
       left=350    top=8    height=20       width=200
       alias=sklad_t        font_size=12    font_family=0    bg_color="#ffffff"
   }
   {field
       target=department.name1_dep                 border = box
       left=355    top=8    height=20       width=200
       alias=sklad          font_size=12    font_family=0    bg_color="#ffffff"
       editable=handbook
       edit="opennew department_gr.rml "
       editexp="($X
                   (g.sklad=STORE.department.name1_dep)
                   (g.sklad_t=STORE.department.id_department)
                   ($if (global.get_num == 1)
                       (
                           ($g.doc_num@retrieve)
                           ($g.d_head@setValue 'head.num_sd'  ($try ($g.doc_num@GetValue 'NUM_SD') $catch ('Any')('')))
                           (g.num_sd = ($try ($g.doc_num@GetValue 'NUM_SD') $catch ('Any')('')))
                           (global.get_num = 0)
                       )
                   )
                   ($g.d_head@update)
                   ($g.self@doAction 'execute exec commit')
                   ($g.gridspc@retrieve)
                   ($ret '')
                 )"
   }
   {label
       value="Исполнитель"  
       left=570  top=8      height=20       width=70
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }
   {field
       target=personal.fname                border = box
       left=640    top=8    height=20       width=70
       alias=name           font_size=12    font_family=0    bg_color="#ffffff"
       editable = readonly
   }     
   {label
       value="Номер пер док."
       left=11    top=33     height=20       width=80
       font_size=12          font_family=0   bg_color="#c0c0c0"
   }
   {field
       target=head.num_per_sd            halignment = right
       left=95    top=33     height=20       width=70         border = box
       alias=num_per         font_size=12     font_family=0    bg_color="#ffffff"
   }
   {label
       value="Дата пер док."
       left=180    top=33     height=20       width=80
       font_size=12          font_family=0   bg_color="#c0c0c0"
   }
   {field
       target=head.date_per_sd            halignment = left
       left=250    top=33     height=20       width=85         border = box
       alias=dat_per         font_size=12     font_family=0    bg_color="#ffffff"
   }
   {label
       value="Период:"
       left=340    top=33    height=20       width=50
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }

   {combobox
      left = 390     top = 33     width = 150        height = 20 
      alias = combo1               border = box
      target = month
      action = "~($X
                  ($debug 'combo1')
                  ($if (global.no_combo_action == 0)
                    (
                      ($g.data_ds1@setCurRow ($g.combo1@getSelectedIndex))
                      ($g.d_head@setValue 'head.date1' ($g.data_ds1@getValue 'data1'))
                      ($debug 'combo1, index: ' + ($g.combo1@getSelectedIndex) + ', value: ' + ($g.data_ds1@getValue 'data1') + ' ' + ($g.data_ds1@getValue 'month'))
                    )
                  )
                  ($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=550    top=33    height=20       width=40
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }

   {combobox
      left = 590     top = 33     width = 150        height = 20 
      alias = combo2               border = box
      target = name
      action = "~($X
                  ($if (global.no_combo_action == 0)
                    (
                      ($g.data_ds2@setCurRow ($g.combo2@getSelectedIndex))
                      ($g.d_head@setValue 'head.type1' ($g.data_ds2@getValue 'id'))
                    )
                  )
                  ($ret '')
                )~"
      {datastore
       alias = data_ds2
       query = "select id, name
                  from type_act_rash"
      }
   }
   {label
       value="Организация"
       left=11     top=58   height=20        width=75
       font_size=12         font_family=0    bg_color="#c0c0c0"
   }
   {field
       target=head.id_client1                 halignment = right
       left=90     top=58   height=20        width=70
       alias=cli1_numb_client                border = box        
       font_size=12         font_family=0    bg_color="#ffffff"
       editable=handbook
       edit="opennew find_client.rml"
       editexp="cli1_numb_client=STORE.client.id_client"
       dep="name_client1"
   }
   {field
      target=client.name1_client              border = box
      left=165     top=58   height=20        width=270
      alias=name_client1    font_size=12     font_family=0    bg_color="#ffffff"
      editexp="name_client1=STORE.client.name1_client"
      editable = readonly
   }
   {label
       value="Примечание"    
       left=11    top=83     height=20      width=65
       font_size=12          font_family=0  bg_color="#c0c0c0"
   }
   {field
       target=head.comment_sd   border = box
       left=90    top=83    height=20      width=495
       alias=comment_sd         font_size=12   font_family=0    bg_color="#ffffff"
   }
   {label
    alias=progress
    left = 10  top = 105      width = 150     height = 20 
    font_size=17              font_family=1   font_color="#CC3300"
    bg_color="#c0c0c0"        border=none     
   }
{datastore 
        alias = d_head
        query = "select head.id_head, head.sort, head.id_type_doc, head.accept,
                        head.num_sd, head.date_sd,
                        head.num_per_sd, head.date_per_sd,
                        head.id_department1, department.name1_dep, head.id_department2,
                        head.id_client1, head.id_client2,
                        head.comment_sd,
			head.id_personal, personal.fname,
			head.parent_id_head, head.date1, head.type1,
			client.name1_client
                   from head,personal,department,client
                  where head.id_head = ~($try ($if (global.create == 0)
                                  	    	(ARGUMENTS.0)
	                                       $else (ds.hpk))
		                         $catch ('any') (-1))~
                   and head.id_department1 = department.id_department (+)
		   and head.id_client1 = client.id_client (+)
                   and head.id_personal = personal.id_personal(+)
                   and head.id_type_doc = 57"
              editable = yes
                  head = yes
                unique = "('head','head.id_head')"
               default = "head.id_head=~ds.hpk~,head.id_type_doc=~57~,
                          head.date_sd=~ds.date~,
                          head.id_personal=~ds.idp~,
                          head.accept=~0~,
                          head.num_sd=~'Сп-'~,
			  head.num_per_sd=~'.'~,head.date_per_sd=~ds.date~,
			  head.id_department1=~0~,
			  head.id_department2=~0~,
                          head.id_client1=~1~,
                          head.id_client2=~1~"
} // ds
} // form
} // scroll
{tabset labels = "перечень продукции"
 tabs_on_top=yes
{grid
editable = yes
add="opennew find_choose.rml 1, 0"
alias = GRIDSPC
buttonbar_font_family=0       buttonbar_font_size=12           
buttonbar_size = 30           titlebar_size = 18            rowsize=25     
addexp="($X
          ($debug 'global.changing: ' + global.changing)
          ($if (global.changing == 1)
             (
               ($debug '---changing---')
               ($g.d_spc@delRow ($g.d_spc@size) - 1)
               ($g.d_spc@notifyViews)
               (global.changing = 0)
               ($ret '')
             )
          )
          (g.war=store.active.id_active)
          (g.name_w=store.active.name_active)
          (g.id_un=store.id_pseudo_unit)
          (g.name_un=store.obozn)

          (cur_row = ($g.d_spc@getCurRow))

          # проверим, нет ли уже такого актива в спецификации
          (global.cur_id_active = store.active.id_active)
          (sz = ($g.d_spc@size) - 1) (i = 0) (found = 0)
          #($debug 'DS size: ' + (sz +  1) + ', checking: ' + sz + ' rows')
          ($while (i < sz)
            (
              ($g.d_spc@FastSetCurRow i)
              ($if (($g.d_spc@getValue 'spc.id_active') == global.cur_id_active) 
                 (
                   (found = 1)
                   (found_row = (i+1))
                   (i = sz)
                 )
              )
              (i += 1)
            )
          )
          ($if (found != 0)
            (
              (v = ($@validator))
              ($v@SetType 'NUMBER')
              ($v@SetMask '##0.')
              ($if (($g.self@sure 'Такой актив уже есть в спецификации, в строке ' + ($v@toString found_row) + '. Хотите добавить еще раз?' 0) == 0)
                (
                  ($g.d_spc@delRow ($g.d_spc@size)-1)
                  ($g.d_spc@notifyViews)
                  (cur_row = ($g.d_spc@size)-1)
                )
              )

            )
          )
          ($g.d_spc@setCurRow cur_row)
          (global.d1 = ($($@date g.date_sd)@toPLString))
          ($g.ds_rest@retrieve)
          ($g.d_spc@setValue 'DOC_LIB.GETRESTBEFOREBEFORE(HEAD.ID_DEPARTMENT1,SPC.ID_ACTIVE,HEAD.SORT)' ($try ($g.ds_rest@GetValue 'REST') $catch ('NullException')(0)))
          ($debug 'Остаток: ' + ($g.d_spc@getValue 'DOC_LIB.GETRESTBEFOREBEFORE(HEAD.ID_DEPARTMENT1,SPC.ID_ACTIVE,HEAD.SORT)'))
          (g.diff = 0)
          ($debug 'Diff: ' + g.diff)
          ($p.diff@calc)
          ($debug 'Diff: ' + g.diff)
          #($g.gridspc@repaint)
          ($ret '')
        )
        "
{menu
   {item label = "Поиск в справочнике" action="~($X (global.changing = 0) ($ret ''))~opennew find_choose.rml 1, 0"}
   {item label = "-"}
   {item label = "История изменений строки" action= "open reports/report_spc_log.rml ~($g.d_spc@getValue 'spc.id_spc')~"}
   {item label = "История изменений документа" action= "open reports/report_spc_log_doc.rml ~($g.d_spc@getValue 'spc.id_head')~"}
   {item label = "-"}
   {item label = "Аналоги" action= "opennew docs/material_analog_2.rml ~($($g.d_spc@getValue 'spc.id_active')@int)~, ~($($g.d_head@getValue 'head.id_department1')@int)~, ~($X (global.changing = 1) ($($g.self@getAliases)@put '_DS1' g.d_spc) ($ret ''))~ &_DS1")  }
}
 {column size=40  
     visible=no
     title="Номер"   
     target=spc.id_spc        title_font_family=2     halignment=left
     font_color="#000000"     font_size=12              font_family=0
 }
 {column size=110 
    visible=no
    title="Номер"   
    target=spc.id_head        title_font_family=2
    font_color="#000000"      font_size = 12            font_family=0
 }
 {column size=65
    title="Код"              font_family=0            editable = readonly 
    target=spc.id_active     title_font_family=2      halignment=center
    alias=war                font_color="#000000"     font_size=12 
 }
 {column size=250       title="Наименование"
    target=active.name_active  title_font_family=2     halignment=left
    font_color="#000000"      font_size=12              font_family=0
    alias=name_w              editable = readonly 
 }
 {column size=60     
    visible=no
    title="Код"              font_family=0
    target=spc.id_unit       title_font_family=2     halignment=center
    alias=id_un                font_color="#000000"     font_size=12   
    editexp = "id_un=STORE.active_pseudo_unit.id_pseudo_unit"
 }
 {column size=60       title="Ед. изм."
    target=active_pseudo_unit.obozn  title_font_family=2     halignment=center
    font_color="#000000"      font_size=12              font_family=0
    alias=name_un             
    editable = handbook 
    edit = "opennew psevdo_units.rml ~($try (war) $catch ('NullException') (-1) )~"
    editexp = "name_un=STORE.active_pseudo_unit.obozn"
    dep = "id_un"
 }
 {column size=100       title="Кол-во " 
    target=spc.q1_spc      title_font_family=2      halignment=right
    font_color="#000000"     font_size=12            font_family=0
    editmask = "# #0.00"    alias = q1_spc
    dep="diff"
 }
 {column size=70       title="Остаток" 
    target="DOC_LIB.GETRESTBEFOREBEFORE(HEAD.ID_DEPARTMENT1,SPC.ID_ACTIVE,HEAD.SORT)"
    title_font_family=2      halignment=right
    font_color="#000000"     font_size=12            font_family=0
    editmask = "# #0.000"    alias = ost
    editable = readonly			visible = yes
 }
 {column size=70       title="Разница" 
    type = number
    title_font_family=2      halignment=right
    font_color="#000000"     font_size=12            font_family=0
    editmask = "# #0.000"    alias = diff
    editable = readonly		visible = yes
    exp = "($X
               #($if (global.loading == 1) ($ret '') )
               ($debug 'recalc diff ' + global.recalc)

               (g.diff = g.ost - g.q1_spc)

               ($debug 'g.ost: ' + g.ost + ', g.q1_spc: ' + g.q1_spc + ', g.diff: ' + g.diff)

               ($if (g.diff < 0)
                  ($g.gridspc@setcellbgcolor ($g.d_spc@getCurRow) 4 'RED')
                $else
                  ($g.gridspc@setcellbgcolor ($g.d_spc@getCurRow) 4 'WHITE')
               )
               (global.recalc += 1)
               ($ret '')
           )"
 }
 {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.price1_spc,spc.price2_spc,
                      active.name_active,active_pseudo_unit.obozn,
                      head.id_department1,
                      doc_lib.getRestBeforeBefore(head.id_department1, spc.id_active, head.sort)

                 from head,spc,active,active_pseudo_unit
                where spc.id_head = ~($try (d_head.head.id_head) $catch ('NullException') (-1) )~ 
                  and spc.id_head = head.id_head
                  and spc.id_active = active.id_active 
                  and spc.id_unit = active_pseudo_unit.id_pseudo_unit (+)
                order by spc.id_spc"
             editable = yes
               unique = "('spc','spc.id_spc')" 
              default = "spc.id_head=~($try (d_head.head.id_head) $catch ('NullException') (-1) )~,
                         spc.id_spc=~ds.spk~,
			 spc.q1_spc=~0~,spc.q2_spc=~0~,
			 spc.price1_spc=~0~,spc.price2_spc=~0~"
	 
} // ds
} // grid1
} // tabset
} // split
{button
  left = 11  width = 50    height = 20
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  label = "Сохранить и выйти F3"
  action = "($X
	     ($if (($G.D_HEAD@getValue 'HEAD.ACCEPT') == 1)
		  (
		    ($g.self@actcancel)
		    ($ret '')
		  )
	     )
	     # проверяем, нет ли перемещений больше остатка
	     (found = 0)
	     (i = 0) (sz = ($g.d_spc@size))
	     ($while (i < sz)
	       (
	         ($g.d_spc@setCurRow i)
	         ($if (($try (g.diff) $catch ('NullException')(0)) < 0)
	           (
	             (found = 1)
	             (i = sz)
	           )
	         )
	         (i += 1)
	       )
	     )
	     ($if (found > 0)
               (
                 #($g.self@sure 'В документе есть строки с недостаточным остатком - отмечены красным цветом. Для сохранения документа исправьте эти строки.' 1)
                 #($ret '')
               )
             )
                        ($if (global.get_num == 1)
                            (
                                ($g.doc_num@retrieve)
                                d_head.head.num_sd = ($try ($g.doc_num@GetValue 'NUM_SD') $catch ('Any')(''))
                                g.num_sd = ($try ($g.doc_num@GetValue 'NUM_SD') $catch ('Any')(''))
                                (global.get_num = 0)
                            )
                        )
			    ($g.self@actdok)
             ($ret ''))"
  shortcut = "F3"
}
{button
  left = 11  width = 50    height = 20
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  label = "Запис F4"
  action = "($X
          ($if (($G.D_HEAD@getValue 'HEAD.ACCEPT') == 1)
		  (
		    ($g.self@actcancel)
		    ($ret '')
		  )
	     )
	    ($if (global.get_num == 1)
                (
                    ($g.doc_num@retrieve)
                    d_head.head.num_sd = ($try ($g.doc_num@GetValue 'NUM_SD') $catch ('Any')(''))
                    g.num_sd = ($try ($g.doc_num@GetValue 'NUM_SD') $catch ('Any')(''))
                    (global.get_num = 0)
                )
            )
            ($debug '0000000000000')
	     # проверяем, нет ли перемещений больше остатка
	     (found = 0)
	     (i = 0) (sz = ($g.d_spc@size))
	     ($while (i < sz)
	       (
	         ($g.d_spc@setCurRow i)
	         ($if (($try (g.diff) $catch ('NullException')(0)) < 0)
	           (
	             (found = 1)
	             (i = sz)
	           )
	         )
	         (i += 1)
	       )
	     )
	     ($if (found > 0)
               (
                 ($g.self@sure 'В документе есть строки с недостаточным остатком - отмечены красным цветом. Для сохранения документа исправьте эти строки.' 1)
                 ($ret '')
               )
             )
            #($if (($G.D_HEAD@getValue 'HEAD.ACCEPT') == 1)
		#  (
		    #($g.self@doAction 'execute exec doc_lib.de_accept(' + ($G.D_HEAD@getValue 'HEAD.ID_HEAD') + ');')
		    #($g.self@actsave)
		    #($g.self@doAction 'execute exec doc_lib.accept(' + ($G.D_HEAD@getValue 'HEAD.ID_HEAD') + ');')
		#    ($g.self@actcancel)
		#  )
		#  $else (
		    ($g.self@actsave)
		#  )
		#)
	)	
		"
  shortcut = "F4"
}
{button
  left = 72  width = 50    height = 20
  BACKGROUND = "LIGHTGRAY"    foreground="#000000"
  label = "Отмена ESC"
  action = "($X
             (tmp = 'Вы действительно хотите выйти без сохранения данных' + '')
             (m = ($g.self@sure tmp 0))
               ($if (m == 0)()
               $else ($g.self@actcancel)
               )
             ($ret ''))"
  shortcut = "escape"
}
} // toolbar

