unit U_YSYGGJList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, MovePanel, cxButtonEdit, cxCalendar, cxPC, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, dxSkinsCore, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter, cxCheckBox;

type
  TfrmYSYGGJList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    MovePanel2: TMovePanel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    YWY: TEdit;
    OrdPerson1: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column3: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    filler: TEdit;
    Label5: TLabel;
    Label13: TLabel;
    GXNmae: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Panel4: TPanel;
    Label14: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column10: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    Label16: TLabel;
    wangno: TEdit;
    v1Column4: TcxGridDBColumn;
    huaname: TEdit;
    Label3: TLabel;
    Label17: TLabel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Label4: TLabel;
    Prtcode: TEdit;
    Tv1Column4: TcxGridDBColumn;
    Tch: TToolButton;
    tbdel: TToolButton;
    Tnoch: TToolButton;
    cxTabControl1: TcxTabControl;
    Tv1Column5: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Panel2: TPanel;
    Label19: TLabel;
    Label20: TLabel;
    Button4: TButton;
    Edit_GXPrice: TEdit;
    Edit_QTY: TEdit;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    Button9: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure PRTColorChange(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Image2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchClick(Sender: TObject);
    procedure TnochClick(Sender: TObject);
    procedure tbdelClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmYSYGGJList: TfrmYSYGGJList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmYSYGGJList.FormDestroy(Sender: TObject);
begin
  frmYSYGGJList := nil;
end;

procedure TfrmYSYGGJList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TfrmYSYGGJList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp) - 30;
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmYSYGGJList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do                              
    begin
      Close;
      sql.Clear;                                         
      Filtered := False;
      sql.Add('select A.*,B.PRTCodeName,B.prtcode,B.PRTColor,B.SOrddefstr5,');
      sql.Add(' isnull((A.GxPrice ),0)  as  gxdj ,isnull(((A.GxPrice)*qty ),0) as gxmoney ');
//      sql.Add('gxdj=case when  A.qty<=30 then isnull((select (A.GxPrice)*2 from JyOrder_Yield B where A.CLID=B.clid ),0)');
//      SQL.Add('else isnull((A.GxPrice ),0) end, ');
//      sql.Add('gxmoney=case when  A.qty<=30 then isnull((select ((A.GxPrice)*2)*B.qty from JyOrder_Yield B where A.CLID=B.clid ),0)');
//      SQL.Add(' else isnull(((A.GxPrice)*qty ),0) end ');
      sql.Add('from JyOrder_Yield A ');
      sql.Add('left join JYOrder_Sub  B on  A.mainid=B.mainid and A.subid=B.subid');
      sql.Add('where A.filltime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
      sql.Add(' and A.filltime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      if cxTabControl1.TabIndex < 2 then
        sql.Add(' and isnull(A.status,0)=''' + inttostr(cxTabControl1.TabIndex) + ''' ');
//      Sql.add('group by convert(char(10),A.FillTime,120),A.Filler,A.mainID,A.MjTypeOther,C.OrderNo,D.PRTCodeName,C.conNo,C.CustomerNoName,D.PrtColor');
//    ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYSYGGJList.TBRafreshClick(Sender: TObject);
begin
  //BegDate.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

procedure TfrmYSYGGJList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmYSYGGJList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库1');
  Close;
end;

procedure TfrmYSYGGJList.FormShow(Sender: TObject);
begin

  ReadCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库1');
  if Trim(DParameters2) = '管理' then
  begin
    //v1Column5.Options.Focusing:=True;
  end
  else
  begin
    //v1Column5.Options.Focusing:=False;
  end;
  //InitGrid();
end;

procedure TfrmYSYGGJList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('库存汇总列表', cxGrid2);
end;

procedure TfrmYSYGGJList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmYSYGGJList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYSYGGJList.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SOrdDefStr10';
      flagname := '库存存放地点';
      if ShowModal = 1 then
      begin
        with CDS_Main do
        begin
          Edit;
          FieldByName('SOrdDefStr10').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Sub Set SOrdDefStr10=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.Add(' where SubId=''' + Trim(Self.CDS_Main.fieldbyname('SubId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYSYGGJList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYSYGGJList.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmYSYGGJList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Left := FLeft;
  Panel4.Top := FTop + 110;
  Panel4.Visible := True;
  Panel10.Caption := Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text := CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
  application.ProcessMessages;
end;

procedure TfrmYSYGGJList.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmYSYGGJList.cxTabControl1Change(Sender: TObject);
begin
  case cxTabControl1.TabIndex of
    0:
      begin
//        tbedit.Visible := true;
        tbdel.Visible := true;
        Tch.Visible := true;
      end;
    1:
      begin
        Tnoch.Visible := true;
      end;
  end;
  TBRafresh.Click;
end;

procedure TfrmYSYGGJList.TchClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;

  if CDS_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update JyOrder_Yield SET status=''1'' ');
            sql.Add(',chker=' + quotedstr(trim(DName)));
            sql.Add(',chkTime=getdate() ');
            sql.Add('where CLID =' + quotedstr(trim(CDS_Main.fieldbyname('CLID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSYGGJList.TnochClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;

  if CDS_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update JyOrder_Yield SET status=''0'' ');
            sql.Add(',chker='''' ');
            sql.Add(',chkTime='''' ');
            sql.Add('where CLID =' + quotedstr(trim(CDS_Main.fieldbyname('CLID').AsString)));
            execsql;
          end;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmYSYGGJList.tbdelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;

  if CDS_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      DisableControls;
      First;
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.add('insert into JyOrder_Yield_Del select * from WFB_MJJY where mjid=''' + trim(CDS_Main.fieldbyname('CLID').AsString) + ''' ');
            sql.Add('update JyOrder_Yield_Del Set DelTime=Getdate(),Deler=''' + trim(DName) + ''' where CLID=''' + trim(CDS_Main.fieldbyname('CLID').AsString) + ''' ');
            sql.Add('delete JyOrder_Yield  where MJID=''' + Trim(CDS_Main.fieldbyname('CLID').AsString) + '''');
            ExecSQL;
          end;
          CDS_Main.Delete;
        end;
        next;
      end;
      First;
      EnableControls;
    end;

    InitGrid();
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;

end;

procedure TfrmYSYGGJList.ToolButton1Click(Sender: TObject);
begin
  Panel2.Left := (self.Width - Panel2.Width) div 2;
  Panel2.Visible := True;
end;

procedure TfrmYSYGGJList.Button4Click(Sender: TObject);
begin
  Panel2.Visible := False;
end;

procedure TfrmYSYGGJList.Button9Click(Sender: TObject);
begin
  if (trim(Edit_GXPrice.Text) = '') and checkbox4.Checked then
  begin
    application.MessageBox('单价不能为空！', '提示信息', 0);
    exit;
  end;
  if (trim(Edit_QTY.Text) = '') and checkbox5.Checked then
  begin
    application.MessageBox('数量不能为空！', '提示信息', 0);
    exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while CDS_Main.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
          sql.Add(' ' + quotedstr(trim(DName)));
          sql.Add(',getdate() ');
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(trim('员工工价')));
          sql.Add(',' + quotedstr(trim('修改前→条码:' + trim(CDS_Main.fieldbyname('CLID').AsString) + ',单价 ' + trim(CDS_Main.fieldbyname('GXPrice').AsString) + ',数量 ' + trim(CDS_Main.fieldbyname('QTY').AsString) + '修改后：' + ',单价 ' + trim(Edit_GXPrice.Text) + ',数量 ' + trim(Edit_QTY.text))));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          if CheckBox4.Checked then
          begin
            sql.Add('update JyOrder_Yield SET GXPrice=''' + trim(Edit_GXPrice.Text) + ''' where CLID=''' + trim(CDS_Main.fieldbyname('CLID').AsString) + ''' ');
          end;
          if CheckBox5.Checked then
          begin
            sql.Add('update JyOrder_Yield SET QTY=''' + trim(Edit_QTY.Text) + ''' where CLID=''' + trim(CDS_Main.fieldbyname('CLID').AsString) + ''' ');
          end;

          ExecSQL;
        end;
        Edit;
        FieldByName('SSel').Value := False;

        if CheckBox4.Checked then
        begin
          FieldByName('GXPrice').Value := Trim(Edit_GXPrice.Text);
        end;
        if CheckBox5.Checked then
        begin
          FieldByName('QTY').Value := Trim(Edit_QTY.Text);
        end;

        Post;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!', '提示', 0);
    Panel2.Visible := false;
    Edit_GXPrice.Text := '';
    Edit_QTY.Text := '';
    CheckBox4.Checked := false;
    CheckBox5.Checked := false;
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

end.

