unit U_Bpkcsumlist_Sel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxCheckBox,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxTextEdit, cxCalendar,
  Menus;

type
  TfrmBpkcsumlist_sel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label2: TLabel;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TOk: TToolButton;
    v1Ssel: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    ckName: TComboBox;
    Label6: TLabel;
    v1Column1: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    Label8: TLabel;
    v1Column2: TcxGridDBColumn;
    Label9: TLabel;
    Edit1: TEdit;
    Label10: TLabel;
    VCfactoryName: TcxGridDBColumn;
    v1defnote5: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    v1kuwei: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    CheckBox3: TCheckBox;
    CYID: TEdit;
    Label3: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CheckBox4: TCheckBox;
    Tv1Column7: TcxGridDBColumn;
    P_Code: TEdit;
    Label4: TLabel;
    Label7: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    P_SPEC: TEdit;
    kuwei: TEdit;
    factoryName: TEdit;
    P_CF: TEdit;
    KHCPName: TEdit;
    P_CodeName: TEdit;
    P_Color: TEdit;
    sehao: TEdit;
    KHColor: TEdit;
    KHSeHao: TEdit;
    shazhi: TEdit;
    Label11: TLabel;
    Tv1Column2: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle3: TcxStyle;
    Tv1Column3: TcxGridDBColumn;
    Label21: TLabel;
    YPLX: TComboBox;
    ToolButton2: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure P_CodeChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure v1kuweiPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure CheckBox4Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetComboBox();
    { Private declarations }
  public
    fType: integer;
    fCKName: string;
    canshu2: string;
    CANSHU8: string;
    CANSHU1: string;
    { Public declarations }
  end;

var
  frmBpkcsumlist_sel: TfrmBpkcsumlist_sel;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_BpRk;
{$R *.dfm}

procedure TfrmBpkcsumlist_sel.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''YPCK'' ');
    if trim(fCKName) <> '' then
      sql.Add('and zdyName=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      ckName.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if ckName.Items.Count > 0 then
    ckName.ItemIndex := 0;

  CRType.Items.Clear;
  CRType.Items.Add('');
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''YPRKTYPE''');
    if trim(fCKName) <> '' then
      sql.Add('and note=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count > 0 then
    CRType.ItemIndex := 0;

  with adoqueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_Zdy where Type=''YPKCJYFLAG''');
    open;
    if not IsEmpty then
      Edit1.Text := trim(fieldbyname('zdyName').AsString);
  end;
end;

procedure TfrmBpkcsumlist_sel.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_View_YpKc3 ');
      sql.Add(' @yjNum=''' + trim(Edit1.text) + '''  ');
      if CheckBox1.Checked then
        sql.Add(',@iskc=''1''  ')
      else
        sql.Add(',@iskc=''0''  ');

      if CheckBox2.Checked then
        sql.Add(',@isyj=''1''  ')
      else
        sql.Add(',@isyj=''0''  ');
      if CheckBox4.Checked = True then
      begin
        sql.Add(',@criteria='' and kuwei='''''''' ''');
      end;
//      ShowMessage(SQL.Text);
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
  finally
  end;
end;

procedure TfrmBpkcsumlist_sel.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  if fType = 10 then
    Action := cahide
  else
    Action := cafree;
end;

procedure TfrmBpkcsumlist_sel.FormDestroy(Sender: TObject);
begin
  frmBpkcsumlist_Sel := nil;
end;

procedure TfrmBpkcsumlist_sel.P_CodeChange(Sender: TObject);
begin
  if ADOQueryTmp.Active = False then
    Exit;
  SDofilter(ADOQueryTmp, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryTmp, CDS_Main);
  SInitCDSData20(ADOQueryTmp, CDS_Main);
end;

procedure TfrmBpkcsumlist_sel.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption + '1', Tv1, '样品仓库7');
  if fType = 10 then
  begin
    TOk.Visible := true;
    v1Ssel.Visible := true;
  end;
  if CANSHU1 = '查询' then
  begin

    Tv1.OptionsData.Editing := False;
    ToolButton1.Visible := False;
  end
  else
  begin
    Tv1.OptionsData.Editing := True;
//    ToolButton1.Visible := True;

  end;
//  if canshu2 = '业务员' then
//  begin
//    v1factoryName.Visible := false;
//    v1factoryName.Hidden := true;
//  end
//  else
//  begin
//    v1factoryName.Visible := true;
//    v1factoryName.Hidden := false;
//  end;
  SetComboBox();

end;

procedure TfrmBpkcsumlist_sel.FormCreate(Sender: TObject);
begin
  BegDate.Date := DServerDate - 7;
  EndDate.Date := DServerDate;
  cxGrid1.Align := alclient;

end;

procedure TfrmBpkcsumlist_sel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + '1', Tv1, '样品仓库7');
  close;
end;

procedure TfrmBpkcsumlist_sel.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmBpkcsumlist_sel.TBPrintClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmBpkcsumlist_sel.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmBpkcsumlist_sel.TOkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

procedure TfrmBpkcsumlist_sel.CheckBox1Click(Sender: TObject);
begin
  if checkbox3.Checked = True then
  begin
    P_Code.Tag := 1;
  end
  else
  begin
    P_Code.Tag := 2;
  end;
  initgrid();
  if ADOQueryTmp.Active = False then
    Exit;
  SDofilter(ADOQueryTmp, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryTmp, CDS_Main);
  SInitCDSData20(ADOQueryTmp, CDS_Main);
//  ToolButton2.Click();
end;

procedure TfrmBpkcsumlist_sel.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
//  if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('kcQty').Index] < strtofloatdef(trim(edit1.Text), 0) then
//    ACanvas.Brush.Color := $008080FF;
end;

procedure TfrmBpkcsumlist_sel.CheckBox2Click(Sender: TObject);
begin
  initgrid();
end;

procedure TfrmBpkcsumlist_sel.ToolButton1Click(Sender: TObject);
begin
  frmZDYHelp := TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag := 'YPKCJYFLAG';
    caption := '样品库存预警设置';
    V1Name.Caption := '预警数量';
    showmodal;
    free;
  end;
  with adoqueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_Zdy where Type=''YPKCJYFLAG''');
    open;
    if not IsEmpty then
      Edit1.Text := trim(fieldbyname('zdyName').AsString);
  end;
  TBRafresh.Click;
end;

procedure TfrmBpkcsumlist_sel.FormActivate(Sender: TObject);
begin
  InitGrid();

  if CANSHU8 = '1' then
  begin
    SDofilter(ADOQueryTmp, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryTmp, CDS_Main);
    SInitCDSData20(ADOQueryTmp, CDS_Main);
  end;
end;

procedure TfrmBpkcsumlist_sel.Tv1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    InflateRect(FBounds, -1, -1);    //Platform   specific.   May   not   work   on   Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmBpkcsumlist_sel.v1kuweiPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update YP_InOut2 set  kuwei=' + quotedstr(Trim(mvalue)));
    sql.Add('where isnull(FROMOrderno,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('FROMOrderno').AsString)));
    sql.Add('and isnull(frommainid,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('frommainid').AsString)));
    sql.Add('and isnull(fromsubid,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('fromsubid').AsString)));
    sql.Add('and isnull(ywy,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('ywy').AsString)));
    sql.Add('and isnull(Price,0)=' + Trim(CDS_Main.fieldbyname('Price').AsString));
    sql.Add('and isnull(factoryname,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('factoryname').AsString)));
    sql.Add('and isnull(P_Code,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_Code').AsString)));
    sql.Add('and isnull(P_codename,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_codename').AsString)));
    sql.Add('and isnull(P_spec,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_spec').AsString)));
    sql.Add('and isnull(P_cf,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_cf').AsString)));
    sql.Add('and isnull(P_Color,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_Color').AsString)));
    sql.Add('and isnull(P_HX,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_HX').AsString)));
    sql.Add('and isnull(QtyUnit,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('QtyUnit').AsString)));
    sql.Add('and isnull(shazhi,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('shazhi').AsString)));
    sql.Add('and isnull(kuwei,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('kuwei').AsString)));
    sql.Add('and isnull(P_SKZ,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_SKZ').AsString)));
    sql.Add('and isnull(YPCFD,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('YPCFD').AsString)));
    sql.Add('and isnull(JuanHao,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('JuanHao').AsString)));
    sql.Add('and isnull(ISCGPZ,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('ISCGPZ').AsString)));
    sql.Add('and isnull(YPType,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('YPType').AsString)));
    ExecSQL;
  end;
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmBpkcsumlist_sel.Tv1DblClick(Sender: TObject);
begin
  if fType <> 10 then
    Exit;
  if CDS_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

procedure TfrmBpkcsumlist_sel.Button1Click(Sender: TObject);
var
  mvalue: string;
begin
  if Trim(Edit2.Text) = '' then
  begin
    application.MessageBox('库位不能为空！', '提示信息', 0);
    Exit;
  end;
  mvalue := Trim(Edit2.Text);
  while CDS_Main.Locate('SSel', True, []) do
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update YP_InOut2 set  kuwei=' + quotedstr(Trim(mvalue)));
      sql.Add('where isnull(FROMOrderno,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('FROMOrderno').AsString)));
      sql.Add('and isnull(frommainid,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('frommainid').AsString)));
      sql.Add('and isnull(fromsubid,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('fromsubid').AsString)));
      sql.Add('and isnull(ywy,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('ywy').AsString)));
      sql.Add('and isnull(Price,0)=' + Trim(CDS_Main.fieldbyname('Price').AsString));
      sql.Add('and isnull(factoryname,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('factoryname').AsString)));
      sql.Add('and isnull(P_Code,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_Code').AsString)));
      sql.Add('and isnull(P_codename,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_codename').AsString)));
      sql.Add('and isnull(P_spec,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_spec').AsString)));
      sql.Add('and isnull(P_cf,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_cf').AsString)));
      sql.Add('and isnull(P_Color,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_Color').AsString)));
      sql.Add('and isnull(P_HX,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_HX').AsString)));
      sql.Add('and isnull(QtyUnit,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('QtyUnit').AsString)));
      sql.Add('and isnull(shazhi,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('shazhi').AsString)));
      sql.Add('and isnull(kuwei,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('kuwei').AsString)));
      sql.Add('and isnull(P_SKZ,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('P_SKZ').AsString)));
      sql.Add('and isnull(YPCFD,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('YPCFD').AsString)));
      sql.Add('and isnull(JuanHao,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('JuanHao').AsString)));
      sql.Add('and isnull(ISCGPZ,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('ISCGPZ').AsString)));
      sql.Add('and isnull(YPType,'''')=' + quotedstr(Trim(CDS_Main.fieldbyname('YPType').AsString)));
      ExecSQL;
    end;

    Tv1.Controller.EditingController.ShowEdit();
    CDS_Main.Delete;
  end;
  InitGrid();
  if ADOQueryTmp.Active = False then
    Exit;
  SDofilter(ADOQueryTmp, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryTmp, CDS_Main);
  SInitCDSData20(ADOQueryTmp, CDS_Main);
end;

procedure TfrmBpkcsumlist_sel.N1Click(Sender: TObject);
begin

  SelOKNo(CDS_Main, True);
end;

procedure TfrmBpkcsumlist_sel.N2Click(Sender: TObject);
begin

  SelOKNo(CDS_Main, False);
end;

procedure TfrmBpkcsumlist_sel.CheckBox4Click(Sender: TObject);
begin

  initgrid();
  if ADOQueryTmp.Active = False then
    Exit;
  SDofilter(ADOQueryTmp, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryTmp, CDS_Main);
  SInitCDSData20(ADOQueryTmp, CDS_Main);
end;

procedure TfrmBpkcsumlist_sel.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryTmp.Active = False then
    Exit;
  SDofilter(ADOQueryTmp, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryTmp, CDS_Main);
  SInitCDSData20(ADOQueryTmp, CDS_Main);
end;

end.

