Cp1251
DOC
foreground = lightgray 
document_width = 750
document_height = 550
document_title = "Калькулятор бюджета потребностей"
preloadscript="
(global.retr = 0)
(global.id_head = 0)
(global.id_active = -1)
(global.num_sd = 'Новый')
(global.no_selaction = 1)
(global.visible = 'no')
(global.recalc = 0)
(global.loading = 1)
(global.recalc = 0)
(global.cur_row = 0)
"
postloadscript = "($X

($defun itogi<arg>
  ($X

    (global.cur_row = ($g.gridds03@getCurRow))
    ($g.gridds03@setCurRow ($g.gridds03@size)-1)
    ($g.gridds03@setValue 'q_proiz' 0)
    ($g.gridds03@setValue 'q_proiz' ($g.gridds03@sum 'q_proiz'))
    ($g.gridds03@setValue 'proiz_sum' 0)
    ($g.gridds03@setValue 'proiz_sum' ($g.gridds03@sum 'proiz_sum'))
    ($g.gridds03@setValue 'ost' 0)
    ($g.gridds03@setValue 'ost' ($g.gridds03@sum 'ost'))
    ($g.gridds03@setValue 'ost_sum' 0)
    ($g.gridds03@setValue 'ost_sum' ($g.gridds03@sum 'ost_sum'))
    ($g.gridds03@setValue 'def' 0)
    ($g.gridds03@setValue 'def' ($g.gridds03@sum 'def'))
    ($g.gridds03@setValue 'def_sum' 0)
    ($g.gridds03@setValue 'def_sum' ($g.gridds03@sum 'def_sum'))
    ($g.gridds03@setCurRow global.cur_row)
    ($g.gridds03@notifyViews)
    ($ret '')
  )
)


($defun refresh<arg>
  ($X
    (global.recalc = 0)
    ($g.grid3@retrieve)
    ($g.gridds03@newRow)
    (i = 0)
    ($while (i < 9)
      (
        ($g.grid3@setcellbgcolor ($g.gridds03@getCurRow) i '#FFCCCC')
        (i += 1)
      )
    )
    ($g.gridds03@setValue 'name_active' 'Итого:')

    ($g.gridds03@setCurRow 0)
    (global.cur_row = 0)


    # итоги
    ($itogi)
    (global.recalc = 1)

    ($ret '')
  )
)


(global.loading = 0)

($refresh)



($ret '')

)"

{toolbar   BACKGROUND = "LIGHTGRAY"
{splitpanel type=horizonal     percent=15
{form
  {label left = 5 top = 10 width = 54 height = 30 font_size = 14 value = "Месяц" }
  { field left = 50 top = 10 width = 100 height = 30 font_size = 16 alias = dat1  target = month_beginning
    dep = "n_dat1"
  }
  {field  visible=yes   alias=n_dat1  type=string 
    left = 50 top = 40 width = 100 height = 30 font_size = 16	visible = no
    exp="($X
           (g.n_dat1 = ($($@date g.dat1)@toPLString))
           ($debug 'n_dat1 = ' + g.n_dat1)
           ($ret '')
         )"
  }
  { button left = 160 top = 10 width = 100 height = 30 label = "Обновить" foreground = "#000000"
    action = "($X
                 ($refresh)
                 ($if (($($g.grid3@getDatastore)@size) == 0)
                    ($g.info@setValue 'В этом месяце нет утвержденного плана производства.')
                  $else
                    ($g.info@setValue '')
                 )
                 ($ret '')
              )"
  }
  {label left = 5 top = 50 width = 900 height = 30 font_size = 16 font_color = "#0000FF" value = "" alias = info }
      {datastore  alias=date_ds
        query="select month_beginning, month_end
                 from (
               select to_date('01.'||to_char(sysdate, 'mm.yyyy'), 'dd.mm.yyyy') month_beginning,
                      last_day(to_date('01.'||to_char(sysdate, 'mm.yyyy'), 'dd.mm.yyyy')) month_end
                 from dual
                      )"
        
      }
}  // form
{grid
    alias = GRID3
    buttonbar_font_family = 0              buttonbar_font_size = 12
    buttonbar_size = 30                    titlebar_size = 18   titlebar_height = 40
    multiselect = no                      editable = yes
    rowsize = 20
          add=""
       addexp=""
          del=""

   {column size=50             title="Код"
        target=id_active
        alias = id_active_component
        font_size = 12          font_family=0
   }                  
   {column size=300             title="Материал"
        target=name_active
        alias = name_active_component
        title_font_family=2        title_font_color="#000000"
        halignment=left            font_size = 12          font_family=0
        editable = readonly
   }                  
   {column size=100             title="<html>цена<br>закупки</html>"
        target=price
        title_font_family=2        title_font_color="#000000"
        halignment=right            font_size = 12          font_family=0 editmask = "# #0.00"
        editable = yes
        alias = price
        dep = "proiz_sum, ost_sum, def_sum, recalc"
   }
   {columnset
    title="Потребность"
      {column size=100             title="кол-во"
           target=q_proiz
           title_font_family=2        title_font_color="#000000"
           halignment=right            font_size = 12          font_family=0 editmask = "# #0.00"
           editable = readonly
           alias = q_proiz
           dep = "proiz_sum"
      }
      {column size=100             title="сумма"
           target=proiz_sum
           title_font_family=2        title_font_color="#000000"
           halignment=right            font_size = 12          font_family=0 editmask = "# #0.00"
           editable = readonly
           alias = proiz_sum
           exp = "proiz_sum = ($try (q_proiz) $catch ('NullException')(0)) * ($try (price) $catch ('NullException')(0))"
      }
   }
   {columnset
    title="Остаток"
     {column size=70             title="кол-во"
          target=ost
          title_font_family=2        title_font_color="#000000"
          halignment=right            font_size = 12          font_family=0 editmask = "# #0.00"
          editable = readonly
          alias = ost
     }                  
      {column size=100             title="сумма"
           target=ost_sum
           title_font_family=2        title_font_color="#000000"
           halignment=right            font_size = 12          font_family=0 editmask = "# #0.00"
           editable = readonly
           alias = ost_sum
           exp = "ost_sum = ($try (ost) $catch ('NullException')(0)) * ($try (price) $catch ('NullException')(0))"
      }
   }
   {column size=70             title="Хитрый колумн"
        type = string
        title_font_family=2        title_font_color="#000000"
        halignment=right            font_size = 12          font_family=0 editmask = "# #0.00"
        editable = readonly
        alias = hidden1
        visible = no
   }                  
   {columnset
    title="Дефицит"
     {column size=70             title="кол-во"
          target = def
          title_font_family=2        title_font_color="#000000"
          halignment=right            font_size = 12          font_family=0 editmask = "# #0.00"
          editable = readonly
          alias = def
     }                  
      {column size=100             title="сумма"
           target=def_sum
           title_font_family=2        title_font_color="#000000"
           halignment=right            font_size = 12          font_family=0 editmask = "# #0.00"
           editable = readonly
           alias = def_sum
           exp = "def_sum = ($try (def) $catch ('NullException')(0)) * ($try (price) $catch ('NullException')(0))"
      }
   }
      {column size=100             title="сумма"
           type = number
           visible = no
           title_font_family=2        title_font_color="#000000"
           halignment=right            font_size = 12          font_family=0 editmask = "# #0.00"
           editable = readonly
           alias = recalc
           exp = "($X
                     ($if (global.recalc == 0) ($ret ''))
                     ($itogi)
                     ($debug 'recalc times: ' + global.recalc)
                     (global.recalc += 1)
                     ($ret '')
                  )"
      }

   {datastore 
           alias = GRIDDS03
           query = "select id_active, name_active, price, q_proiz, proiz_sum, ost, ost_sum, zak, def, def_sum
                      from (
                    select id_active, name_active, price, q_proiz, q_proiz*price proiz_sum, ost, ost*price ost_sum, zak,
                           decode(sign(q_proiz - ost), 1, q_proiz - ost, 0) def, decode(sign(q_proiz - ost), 1, q_proiz - ost, 0)*price def_sum
                      from (
                    select id_active, name_active,
                           q_proiz,
                           doc_lib.getRestBeforeAll(id_active,
                                                      -- на конец месяца
                                                      (
                                                       select max(sort)
                                                        from head
                                                       where date_sd between to_date('01.'||to_char(to_date('~g.n_dat1~', 'dd.mm.yyyy'), 'mm.yyyy'), 'dd.mm.yyyy')
                                                                         and last_day(to_date('~g.n_dat1~', 'dd.mm.yyyy'))
                                                      )
                                                    )
                           ost,
							
                           util.activeInDocuments (id_active,
                                                   to_date('01.'||to_char(to_date('~g.n_dat1~', 'dd.mm.yyyy'), 'mm.yyyy'), 'dd.mm.yyyy'),
                                                   last_day(to_date('~g.n_dat1~', 'dd.mm.yyyy')),
                                                   13
                                                  )
                           zak,

                           doc_lib.getPriceBefore(id_active, sortNum) price


                      from (
                    select active.id_active, active.name_active, active.analog, nvl(active.analog_id, 0) analog_id,
                           sum(spc_proiz.q1_spc*util.getSpecQuantity(head_proiz.sort, spc_proiz.id_active, spc_spec.id_active)) q_proiz,
                           max(head_proiz.sort) sortNum
                      from head head_proiz, spc spc_proiz, head  head_spec, spc spc_spec, active
                     where ~global.loading~ = 0
                       and head_proiz.date_sd between to_date('01.'||to_char(to_date('~g.n_dat1~', 'dd.mm.yyyy'), 'mm.yyyy'), 'dd.mm.yyyy')
                                                  and last_day(to_date('~g.n_dat1~', 'dd.mm.yyyy')) 
                       and head_proiz.id_type_doc = 14
                       and head_proiz.accept = 1
                       and head_proiz.id_head = spc_proiz.id_head
                       and head_spec.id_head = util.getLastSpec(spc_proiz.id_active, to_date('01.01.5000', 'dd.mm.yyyy'))
                       and spc_spec.id_head = head_spec.id_head
                       and spc_spec.id_active = active.id_active
                       and spc_spec.work_type = 2
                     group by active.id_active, active.name_active, active.analog, nvl(active.analog_id, 0)
                           )
                           )
                           )
                     order by name_active"


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

} // toolbar
