unit U_PBKcList;

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,
  RM_Common, RM_Class, RM_GridReport, RM_System, RM_Dataset, MMSystem, Menus,
  BtnEdit, Clipbrd;

type
  TfrmPBKcList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    Label3: TLabel;
    P_CodeName: TEdit;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TOk: TToolButton;
    v1Ssel: TcxGridDBColumn;
    v1MJID: TcxGridDBColumn;
    Label7: TLabel;
    P_Color: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    kuwei: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    orderNo: TEdit;
    Label10: TLabel;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM1: TRMGridReport;
    v1Column8: TcxGridDBColumn;
    ADOQueryMain: TADOQuery;
    Label13: TLabel;
    v1Column9: TcxGridDBColumn;
    P_Code: TEdit;
    Label18: TLabel;
    v1Column16: TcxGridDBColumn;
    Label31: TLabel;
    FactoryName: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column2: TcxGridDBColumn;
    Panel2: TPanel;
    Label1: TLabel;
    Button1: TButton;
    edtKuWei: TBtnEditA;
    ToolButton1: TToolButton;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    pp: TcxGridDBColumn;
    N3: TMenuItem;
    cv1Column11: TcxGridDBColumn;
    lbl1: TLabel;
    INoutNo: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure edtKuWeiBtnClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    fType: integer;
    fCKName: string;
    
    { Public declarations }
  end;

var
  frmPBKcList: TfrmPBKcList;

implementation

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

procedure TfrmPBKcList.SetStatus();
begin
  if fType = 10 then
  begin
    TOk.Visible := true;
    v1Ssel.Visible := true;
    ToolButton1.Visible := True;
  end;
end;

procedure TfrmPBKcList.InitGrid();
begin
  try
    with adoqueryMain do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' ,RKQty=(select Qty from CK_ML_CR X where X.MJID=A.MJID and X.CRFLAG=''入库'') ');
      sql.Add(' , pp=(select MIN(CRQtyFlag) from CK_ML_cr  B where B.MJID=A.MJID  AND B.CKName=''坯布'' and B.Valid=''Y'')');
      SQL.Add(' from CK_ML_KC A ');
      sql.Add(' where A.Qty<>0 and CKName=''坯布''  ');
      sql.ADD(' and exists(select * from CK_ML_CR X where X.Valid=''Y''   and X.MJID=A.MJID )');
      sql.Add(' order by A.CRTime desc,A.CRNO desc');
      open;
    end;
    SCreateCDS20(adoqueryMain, CDS_Main);
    SInitCDSData20(adoqueryMain, CDS_Main);
  finally
  end;
end;

procedure TfrmPBKcList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption + tv1.Name, Tv1, '面料仓库');
  if fType = 10 then
    Action := cahide
  else
    Action := cafree;
end;

procedure TfrmPBKcList.FormDestroy(Sender: TObject);
begin
  frmPBKcList := nil;
end;

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

procedure TfrmPBKcList.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption + tv1.Name, Tv1, '面料仓库');
  SetStatus();
  InitGrid();
end;

procedure TfrmPBKcList.TBCloseClick(Sender: TObject);
begin
  close;
end;

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

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

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

procedure TfrmPBKcList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('isph').index] = '1' then
    ACanvas.Brush.Color := clred;
  if (AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('pp').index] = '-1') and (StrToFloat(AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('qty').index]) > 0) then
    ACanvas.Brush.Color := $F0CAA6;

  if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('RKQty').index] <= 0 then
  begin
    ACanvas.Brush.Color := $00FFFF;
  end;

end;

procedure TfrmPBKcList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, true);
end;

procedure TfrmPBKcList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, false);

end;

procedure TfrmPBKcList.edtKuWeiBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'kuwei';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        edtKuWei.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

procedure TfrmPBKcList.ToolButton1Click(Sender: TObject);
begin
  if Panel2.Visible = False then
    Panel2.Visible := True
  else
    Panel2.Visible := False;
end;

procedure TfrmPBKcList.Button1Click(Sender: TObject);
begin
  if trim(edtKuWei.Text) = '' then
    exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    while CDS_Main.Locate('SSel', true, []) do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update CK_ML_KC set kuwei=' + quotedstr(trim(edtKuWei.Text)));
        sql.Add(' where mjid=' + quotedstr(trim(CDS_Main.FieldByName('mjid').AsString)));
        execsql;
      end;
      with CDS_Main do
      begin
        Edit;
        FieldByName('SSel').Value := false;
        FieldByName('KuWei').Value := trim(edtKuWei.Text);

      end;

    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end
end;

procedure TfrmPBKcList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

end.

