Cp1251
DOC 
foreground = lightgray
document_width = 1000
document_height = 900
document_title = "Возврат нарезки"
style = "style/docs.st"
preloadscript="
(global.create = 0)
(global.d1 = ('\''+'01.01.2003'+'\'') )
(global.get_num = 0)
(global.cur_id_active = -1)
(global.recalc = 0)"

postloadscript="($X
	 	 ($IF (($g.D_HEAD@getvalue 'HEAD.ACCEPT') == 1)
		  ($g.progress@setValue 'Только для чтения')
		 )
		($ret ''))"

{dscollection
   alias = ds
   initQuery = "select head_sq.nextval,sysdate,id_personal from personal where upper(login)=user"
   aliases = "hpk,date,idp"
   seqquery = "select spc_sq.nextval from dual"
   seqnames = "spk"
   initAction ="($X
                  (global.create = 1)
                  (global.get_num = 1)

                  # копируем спецификацию

                  ($g.ds_narezka@retrieve)

                  ($if (($g.ds_narezka@size) == 0) ($ret '') )

                  # склады ставим наоборот

                  (g.sklad = ($g.ds_narezka@getValue 'dep2.name1_dep'))
                  (g.sklad_t = ($g.ds_narezka@getValue 'head.id_department2'))
                  (g.sklad2 = ($g.ds_narezka@getValue 'dep1.name1_dep'))
                  (g.sklad2_t = ($g.ds_narezka@getValue 'head.id_department1'))
                  ($g.d_head@setValue 'head.parent_id_head' ($g.ds_narezka@getValue 'head.id_head'))
                  (i = 0)
                  ($while (i < ($g.ds_narezka@size))
                    (
                      ($g.ds_narezka@setCurRow i)
                      ($g.d_spc@newRow)
                      ($g.d_spc@setValue 'spc.id_active' ($g.ds_narezka@getValue 'spc.id_active'))
                      ($g.d_spc@setValue 'active.name_active' ($g.ds_narezka@getValue 'active.name_active'))
                      ($g.d_spc@setValue 'spc.id_unit' ($g.ds_narezka@getValue 'spc.id_unit'))
                      ($g.d_spc@setValue 'active_pseudo_unit.obozn' ($g.ds_narezka@getValue 'active_pseudo_unit.obozn'))
                      ($g.d_spc@setValue 'spc.q1_spc' ($g.ds_narezka@getValue 'rest'))
                      ($g.d_spc@setValue 'DOC_LIB.GETRESTBEFOREBEFORE(HEAD.ID_DEPARTMENT1,SPC.ID_ACTIVE,1E20)' ($g.ds_narezka@getValue 'rest'))
                      
                      (i += 1)
                    )
                  )
                  ($g.d_spc@notifyviews)
                  ($ret '')
                )"
}

{datastore  alias=doc_num
    query="select num_sd from (select 'ВНАР-'||(nvl(max(to_number(substr(num_sd, 6))), 0) + 1) num_sd
             from head
            where head.num_sd like 'ВНАР-%')"
} // end of ds

{datastore  alias=get_active_details
    query="select doc_lib.getRestBeforeBefore(
                                               ~($try (g.sklad_t) $catch ('NullException')(-1))~,
                                               active.id_active,
                                               1e20
                                             ) rest,
                  price.price
             from active, price
            where active.id_active = ~global.cur_id_active~
              and active.id_active = price.id_active (+)
              and price.id_client (+) = ~($try (g.cli3_numb_client) $catch ('Any')(-1))~"
} // end of ds

{datastore  alias=ds_narezka
    query="select head.id_head, head.id_department1, dep1.name1_dep, head.id_department2, dep2.name1_dep,
                  spc.id_active, spc.id_unit, active.name_active, active_pseudo_unit.obozn,
                  doc_lib.getRestBeforeBefore(
                                               head.id_department2,
                                               active.id_active,
                                               1e20
                                             ) rest
             from spc, head, active, active_pseudo_unit, department dep1, department dep2
            where head.id_head = ~($try (ARGUMENTS.0) $catch ('NullException') (0) )~
              and head.id_department1 = dep1.id_department
              and head.id_department2 = dep2.id_department
              and spc.id_head = head.id_head
              and spc.id_active = active.id_active
              and spc.id_unit = active_pseudo_unit.id_pseudo_unit 
            "
} // end of ds

{datastore  alias=check_narezka
    query="select head.num_sd
             from spc, head
            where head.date_sd = trunc(sysdate)
              and head.id_type_doc = 8
              and head.id_head != ~($try (ARGUMENTS.0) $catch ('NullException') (0) )~
              and spc.id_head = head.id_head
              and spc.id_active = ~(global.cur_id_active)~
            "
} // end of ds

{datastore  alias=check_narezka_day
    query="select head.num_sd, active.name_active
             from spc, head, active
            where head.date_sd = trunc(sysdate)
              and head.id_type_doc = 8
              and head.id_head != ~($try (ARGUMENTS.0) $catch ('NullException') (0) )~
              and head.id_head != (select parent_id_head from head where id_head = ~($try (ARGUMENTS.0) $catch ('NullException') (0) )~)
              and head.num_sd like 'НАР-%'
              and spc.id_head = head.id_head
              and spc.id_active = ~(global.cur_id_active)~
              and active.id_active = spc.id_active
            "
} // end of ds


{toolbar   BACKGROUND = "LIGHTGRAY"
{splitpanel   percent=15
{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=155    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=70
       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="(ficd1 = global.d1)"
  }
   {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=300    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=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=350    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="sklad=STORE.department.name1_dep"
       dep="sklad_t"
   }
   {label
       value="Склад"
       left=300    top=40    height=20       width=40
       font_size=12         font_family=0   bg_color="#c0c0c0"
   }
   {field
       target=head.id_department2
       visible=no           border = box
       left=250    top=40    height=20       width=100
       alias=sklad2_t        font_size=12    font_family=0    bg_color="#ffffff"
       editexp="(sklad2_t=STORE.department.id_department)"
   }
   {field
       target=department2.name1_dep                 border = box
       left=350    top=40    height=20       width=200
       alias=sklad2          font_size=12    font_family=0    bg_color="#ffffff"
       editable=handbook
       edit="opennew department_gr.rml "
       editexp="sklad2=STORE.department.name1_dep"
       dep="sklad2_t"
   }
   {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=650    top=8    height=20       width=70
       alias=name           font_size=12    font_family=0    bg_color="#ffffff"
       editable = readonly
   }     
   {label
       value="Примечание"    
       left=11    top=70     height=20      width=65
       font_size=12          font_family=0  bg_color="#c0c0c0"
   }
   {field
       target=head.comment_sd   border = box
       left=80    top=70    height=20      width=495
       alias=comment_sd         font_size=12   font_family=0    bg_color="#ffffff"
   }
   {label
    alias=progress
    left = 630  top = 70      width = 150     height = 20 
    font_size=16              font_family=2   font_color="#000088"
    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, department2.name1_dep,
                        head.id_client1,client.name1_client,
			head.id_client2,client2.name1_client,
                        head.comment_sd,
			head.id_contract,contract.name_contract,
			head.id_personal,personal.fname,
			head.parent_id_head
                   from head,client,client client2,personal,department,department department2, contract
                  where head.id_head = ~($try ($if (global.create == 0)
                                  	    	(ARGUMENTS.0)
	                                       $else (ds.hpk))
		                         $catch ('any') (-1))~
                   and head.id_client1 = client.id_client (+)
                   and head.id_client2 = client2.id_client (+)
                   and head.id_contract = contract.id_contract (+)
                   and head.id_department1 = department.id_department (+)
                   and head.id_department2 = department2.id_department (+)
                   and head.id_personal = personal.id_personal(+)"
              editable = yes
                  head = yes
                unique = "('head','head.id_head')"
               default = "head.id_head=~ds.hpk~,head.id_type_doc=~8~,
                          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_contract=~1~,
                          head.id_client1=~1~,
                          client.name1_client=~'ФИРМА'~, 
                          head.id_client2=~1~,
                          client2.name1_client=~'ФИРМА'~"
} // 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=20     
addexp="($X

	(t = ($g.d_spc@iterator))
	(i = 0)
	(is_in = 1)
	($while ($t@next)
	    (
		(i += 1)
		($g.d_spc@SetCurRow (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)
                      ($g.d_spc@delRow (($g.d_spc@size) - 1))
                    )
                  )
		))
        ))

	
        (global.cur_id_active = store.active.id_active)
        ($g.check_narezka@retrieve)
        ($if (($g.check_narezka@size) > 0)
          (
            (msg = 'Этот материал уже есть в сегодняшней нарезке:%%')
            (i = 0)
            (sz = ($g.check_narezka@size))
            ($while (i < sz)
              (
                ($g.check_narezka@setCurRow i)
                (msg += ($g.check_narezka@getValue 'head.num_sd') + '%')
                (i += 1)
              )
            )

            (msg += '%%Все равно добавить его?')
            ($if (($g.self@sure msg 0) == 0)
              (
                (is_in = 0)
                ($g.d_spc@delRow (($g.d_spc@size) - 1))
              )
            )
          )
        )

	($if (is_in == 1) (
          
          (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)
          (global.cur_id_active = store.active.id_active)
          ($g.get_active_details@retrieve)
          (g.ost = ($try ($g.get_active_details@GetValue 'REST') $catch ('Any')('')))
          ($p.diff@calc)
        ))

        ($ret '')
        )
        "
{menu
   {item label = "Поиск в справочнике" action="opennew find_choose.rml 1, 0"}
   {item label = "История поступлений"
    action = "opennew history_prih.rml ~($g.d_spc@getValue 'spc.id_active')~"
   }
   {item label = "-"}

   {item label = "История изменений" action= "open reports/report_spc_log.rml ~($g.d_spc@getValue 'spc.id_spc')~"}

}
 {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=50     
    title="Код"              font_family=0	     editable = readonly   
    target=spc.id_active     title_font_family=2     halignment=left
    alias=war                font_color="#000000"    font_size=12   
    editmask = "##0."
 }
 {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=left
    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=70       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="summ,diff"
 }
 {column size=70       title="Остаток" 
    target="DOC_LIB.GETRESTBEFOREBEFORE(HEAD.ID_DEPARTMENT1,SPC.ID_ACTIVE,1E20)"
    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)
               ($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)
               ($debug 'recalc diff ' + global.recalc)
               ($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,
		      spc.id_nds,nds.proc_nds,
		      doc_lib.getRestBeforeBefore(head.id_department1, spc.id_active, 1e20)
                 from head,spc,active,active_pseudo_unit,nds
                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 
                  and spc.id_nds = nds.id_nds
             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=~1~,spc.q2_spc=~0~,
			 spc.id_nds=~2~,nds.proc_nds=~18~,
			 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
             ($debug '------------------------------------------ ')
	     ($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 '')
               )
             )

             # проверяем, есть ли возвращаемые материалы в других сегодняшних нарезках
	     (found = 0)
	     (i = 0) (sz = ($g.d_spc@size))
	     (str = '%')
	     ($while (i < sz)
	       (
	         ($debug '############################################### ' + i)
	         ($g.d_spc@setCurRow i)
	         (global.cur_id_active = ($g.d_spc@getValue 'spc.id_active'))
	         ($g.check_narezka_day@retrieve)
	         ($if (($g.check_narezka_day@size) > 0)
	           (
	             (found += 1)
	             (str += '%' + ($g.check_narezka_day@getValue 'head.num_sd') + ' - ' + ($g.check_narezka_day@getValue 'active.name_active'))
	           )
	         )
	         (i += 1)
	       )
	     )

	     (str = 'Следующие материалы есть в других сегодняшних нарезках.' + str + '%%Сохранить возврат?')
	     ($if (found > 0)
               ($if (($g.self@sure str 0) == 0)
                  ($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 = 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

