unit U_CKProductBCPKCHZList;

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

type
  TfrmCKProductBCPKCHZListPB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    C_CodeName: TEdit;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label5: TLabel;
    ConNo: TEdit;
    v1Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Label1: TLabel;
    C_Code: TEdit;
    v1Column7: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Label6: TLabel;
    Label7: TLabel;
    YWY: TEdit;
    lidan: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label2: TLabel;
    APBatchNo: TEdit;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    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 C_CodeNameChange(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKProductBCPKCHZListPB: TfrmCKProductBCPKCHZListPB;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_JYMXInfo;

{$R *.dfm}

procedure TfrmCKProductBCPKCHZListPB.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPKCHZListPB := nil;
end;

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

procedure TfrmCKProductBCPKCHZListPB.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      {if CheckBox1.Checked=False then
      begin
        sql.Add('exec P_View_KuLin_HZPB   @Status=''0''  ');
      end
      else
      begin
        sql.Add('exec P_View_KuLin_HZPB   @Status=''1''  ');
      end;  }
      if CheckBox1.Checked then
      begin

        sql.Add('select A.* ,C.C_CodeName,C.C_Code,C.C_Color,C.ZhenCun,C.MF,C.KZ,C.C_Spec,C.KaiFu');
        sql.Add(' ,CM.ConNo,CM.lidan,CM.YWY');
        sql.Add(',Case when isnull(JQty,0)<>0 then Cast(KCKgQty*1.00/JQty as decimal(18,2)) else Null end as pjzl');
        sql.Add(' from(select Sum(KCKgQty) KCKgQty,count(*) JQty,A.MainId,A.SubId,A.KCCarNo,A.KCAPBatchNo,JT.MCCJ ');
        sql.Add(' from CK_PBCP_KC KC ');
        sql.Add(' inner join  CK_PBCP_CR A on KC.MJID=A.MJID and A.CRFlag=''入库'' ');
        sql.Add(' left join Machine JT on A.KCCarNo=JT.MCNO ');
        sql.Add(' where  KC.KCKGQty>0 group by A.MainId,A.SubId,A.KCCarNo,A.KCAPBatchNo,JT.MCCJ ) A');
        Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
        sql.Add(' inner join Cloth_Main CM on CM.MainId=C.MainId ');
        SQL.Add(' order by JQty desc ');
      end
      else
      begin
        if CheckBox2.Checked then
        begin
          sql.Add('select A.*,C.C_CodeName,C.C_Code,C.C_Color,C.ZhenCun,C.MF,C.KZ,C.C_Spec,C.KaiFu');
          sql.Add(' ,CM.ConNo,CM.lidan,CM.YWY');
          sql.Add(',Case when isnull(JQty,0)<>0 then Cast(KCKgQty*1.00/JQty as decimal(18,2)) else Null end as pjzl');
          sql.Add(' from(select Sum(KCKgQty) KCKgQty,count(*) JQty,A.MainId,A.SubId,A.KCAPBatchNo,JT.MCCJ ');
          sql.Add(' from CK_PBCP_KC KC ');
          sql.Add(' inner join  CK_PBCP_CR A on KC.MJID=A.MJID and A.CRFlag=''入库'' ');
          sql.Add(' left join Machine JT on A.KCCarNo=JT.MCNO ');
          sql.Add(' where  KC.KCKGQty>0 group by A.MainId,A.SubId,A.KCAPBatchNo,JT.MCCJ ) A');
          Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
          sql.Add(' inner join Cloth_Main CM on CM.MainId=C.MainId ');
          SQL.Add(' order by JQty desc ');
        end else
        begin
          sql.Add('select A.*,C.C_CodeName,C.C_Code,C.C_Color,C.ZhenCun,C.MF,C.KZ,C.C_Spec,C.KaiFu');
          sql.Add(' ,CM.ConNo,CM.lidan,CM.YWY');
          sql.Add(',Case when isnull(JQty,0)<>0 then Cast(KCKgQty*1.00/JQty as decimal(18,2)) else Null end as pjzl');
          sql.Add(' from(select Sum(KCKgQty) KCKgQty,count(*) JQty,A.MainId,A.SubId,A.KCAPBatchNo');
          sql.Add(' from CK_PBCP_KC KC ');
          sql.Add(' inner join  CK_PBCP_CR A on KC.MJID=A.MJID and A.CRFlag=''入库'' ');
          sql.Add(' left join Machine JT on A.KCCarNo=JT.MCNO ');
          sql.Add(' where  KC.KCKGQty>0 group by A.MainId,A.SubId,A.KCAPBatchNo ) A');
          Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
          sql.Add(' inner join Cloth_Main CM on CM.MainId=C.MainId ');
          SQL.Add(' order by JQty desc ');
        end;

      end;       
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmCKProductBCPKCHZListPB.TBCloseClick(Sender: TObject);
begin
  if Trim(DName)='ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption), Tv1,ADOQueryCmd,ADOQueryTemp);
  end;


  Close;
end;

procedure TfrmCKProductBCPKCHZListPB.FormShow(Sender: TObject);
begin

  ReadCxGridAll(trim(self.Caption), Tv1);
  //InitGrid();
end;

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

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

procedure TfrmCKProductBCPKCHZListPB.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 TfrmCKProductBCPKCHZListPB.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmJYMXInfo := TfrmJYMXInfo.Create(Application);
    with frmJYMXInfo do
    begin
      FFMainId := Trim(self.CDS_Main.fieldbyname('MainId').AsString);
//      FFClothType:=Trim(self.CDS_Main.fieldbyname('ClothType').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmJYMXInfo.Free;
  end;
end;

procedure TfrmCKProductBCPKCHZListPB.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked then
  begin
    CheckBox2.Checked:=False;
  end;
  InitGrid();
end;

procedure TfrmCKProductBCPKCHZListPB.CheckBox2Click(Sender: TObject);
begin
  if CheckBox2.Checked then
  begin
    CheckBox1.Checked:=False;
  end;
  InitGrid();
end;

end.

