unit U_CPKCHZSel;

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, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, cxTextEdit;

type
  TfrmCPKCHZSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    v1Column3: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    PRTColor: TComboBox;
    RKplace: TEdit;
    Label13: TLabel;
    v1Column13: TcxGridDBColumn;
    Label10: TLabel;
    ConNo: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label11: TLabel;
    Label12: TLabel;
    PRTHX: TEdit;
    ywy: TEdit;
    v1Column11: TcxGridDBColumn;
    Label14: TLabel;
    PRTEColor: TEdit;
    Label15: TLabel;
    KuanHao: TEdit;
    v1Column12: TcxGridDBColumn;
    Label16: TLabel;
    MPRTCode: TEdit;
    v1Column14: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Label17: TLabel;
    lidan: TEdit;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    ToolButton1: TToolButton;
    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 TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure PRTColorChange(Sender: TObject);
    procedure Tv1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCPKCHZSel: TfrmCPKCHZSel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmCPKCHZSel.FormDestroy(Sender: TObject);
begin
  frmCPKCHZSel := nil;
end;

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

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

procedure TfrmCPKCHZSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select SUM(A.KGQty) KGQty,SUM(A.Qty) Qty,SUM(A.MJYLen) MJYLen,SUM(A.NetKGQty) NetKGQty,count(A.MJID) JQty,A.QtyUnit,A.CPType  ');
      sql.Add(' ,A.KuanHao,B.ConNo,B.OrderNo,B.lidan,B.ywy,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ  ');
      sql.Add(' ,C.PRTColor,C.PRTEColor,C.PRTHX,C.PrtKuanNo,C.KCNote,C.KCZTStatus,A.MainId,A.SubId ');
      sql.Add('from  CK_BanCP_CR A   inner join CK_BanCP_KC D on A.MJID=D.MJID inner join JYOrder_Main B on A.MainId=B.MainId  ');
      sql.Add('inner join JYOrder_Sub C on A.SubId=C.SubId   where A.CRFlag=''入库'' and D.KCValid=''Y'' ');
      sql.Add(' group by  A.QtyUnit,A.MainId,A.SubId,A.CPType,C.KCNote,C.KCZTStatus ,A.KuanHao,B.ConNo,B.OrderNo,B.lidan,B.ywy,B.MPRTCode,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ ,C.PRTColor,C.PRTEColor,C.PRTHX,C.PrtKuanNo');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  TBFind.Click;
end;

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

  MovePanel2.Visible := False;
end;

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

procedure TfrmCPKCHZSel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '成品仓库');
  Close;
end;

procedure TfrmCPKCHZSel.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(self.Caption), Tv1, '成品仓库');
end;

procedure TfrmCPKCHZSel.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 TfrmCPKCHZSel.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPKCHZSel.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 TfrmCPKCHZSel.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPKCHZSel.Tv1Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;

  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update JYOrder_Sub set  ' + FFieldName + '=' + quotedstr(Trim(mvalue)));
    sql.Add('where  SUBID=' + quotedstr(Trim(CDS_Main.fieldbyname('SUBID').AsString)));
    ExecSQL;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPKCHZSel.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  mvalue, FFieldName: string;
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'KCZTStatus';
      flagname := '库存状态';
      if ShowModal = 1 then
      begin
        mvalue := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  with CDS_Main do
  begin
    Edit;
    FieldByName('KCZTStatus').Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update JYOrder_Sub set  KCZTStatus=' + quotedstr(Trim(mvalue)));
    sql.Add('where  SUBID=' + quotedstr(Trim(CDS_Main.fieldbyname('SUBID').AsString)));
    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmCPKCHZSel.ToolButton1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmCPKCHZSel.Tv1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

end.

