unit U_FinishedClothKCList;

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, Menus, cxCheckBox, Clipbrd, cxPC, Math,
  cxLookAndFeels, cxLookAndFeelPainters, dxBarBuiltInMenu, cxNavigator,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter, 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, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmFinishedClothKCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    MovePanel2: TMovePanel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label12: TLabel;
    Label14: TLabel;
    Label9: TLabel;
    Label13: TLabel;
    P_Color: TEdit;
    orderno: TEdit;
    JYID: TEdit;
    P_Code: TEdit;
    KHName: TEdit;
    JTH: TEdit;
    BatchNo: TEdit;
    Panel2: TPanel;
    Panel3: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    Label1: TLabel;
    LBCPAP: TLabel;
    tbOK: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Sel: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    cxgrdPS: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxgrdMJLen: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxgrdConNoPS: TcxGridDBColumn;
    cxgrdCKPS: TcxGridDBColumn;
    cxgrdWFPS: TcxGridDBColumn;
    Label11: TLabel;
    JYType: TComboBox;
    v1Column7: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Label2: TLabel;
    conno: TEdit;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure tbOKClick(Sender: TObject);
    procedure ordernoChange(Sender: TObject);
    procedure ordernoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    { Public declarations }
    FMainid, FSubId, FConNo, FColor, FCodeName: string;
    canshu1: string;
  end;

implementation

uses
  U_DataLink, U_Fun, U_SysLogHelp;

{$R *.dfm}
procedure TfrmFinishedClothKCList.SetStatus();
var
  i: Integer;
begin

  Panel6.Visible := False;
  case cxTabControl1.TabIndex of
    0:
      begin
        ClearOrHideControls(Panel1, '0', True, True);
        ClearOrHideControls(Panel1, '1', True, False);

        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        IsVisibleTV(Panel1, TV1, '0', True);
        cxgrdPS.Visible := True;
        cxgrdMJLen.Visible := True;
        cxgrdCKPS.Visible := True;
        cxgrdConNoPS.Visible := True;
        cxgrdWFPS.Visible := True;
        Tv1Column9.Visible := True;
        Tv1Column4.Visible := True;
        Tv1Column10.Visible := True;
        Tv1Column11.Visible := True;
        Tv1Column13.Visible := True;
        Tv1Column12.Visible := True;
//        Tv1Column5.Visible := True;
//        Tv1Column6.Visible := True;
      end;

    1:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;
        cxgrdPS.Visible := false;
        cxgrdCKPS.Visible := false;
        cxgrdConNoPS.Visible := false;
        cxgrdWFPS.Visible := false;
        Tv1Column9.Visible := false;
        Tv1Column11.Visible := false;
        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        Panel6.Visible := True;
      end;
  end;
end;

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

procedure TfrmFinishedClothKCList.InitGrid();
var
  fwhere, Pwhere, fFSId: string;
  Fwfps: Double;
begin
  Pwhere := SGetFilters(Panel1, 1, 2);
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' and ' + trim(Pwhere);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      case cxTabControl1.TabIndex of
        0:   /////////////////////////订单汇总//////////////////////////////
          begin
            sql.Add('exec P_CPCK_Get_KCHZ ');
            sql.Add('@WSql=' + quotedstr(fwhere));
          end;
        1:      /////////////////////////明细//////////////////////////////
          begin
            sql.Add('exec P_CPCK_Get_KC ');
            sql.Add('@pageIndex=' + inttostr(CurrentPage));
            sql.Add(',@pageSize=' + inttostr(RecordsNumber));
            sql.Add(',@criteria= ' + quotedstr(fwhere));
          end;
      end;
//      showmessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    if cxTabControl1.TabIndex = 1 then
      LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));

    if cxTabControl1.TabIndex = 0 then
    begin
      fFSId := CDS_Main.FieldByName('orderno').AsString;
      with CDS_Main do
      begin
        CDS_Main.DisableControls;
        First;
        while not eof do
        begin
          if (FieldByName('CKPS').Value <> 0) or (FieldByName('PS').Value <> 0) then
          begin
            Edit;
            Fwfps := FieldByName('ConNoPS').AsFloat - FieldByName('PS').AsFloat - FieldByName('CKPS').AsFloat;
            Post;
          end
          else
          begin
            Edit;
            FieldByName('CKPS').Value := 0;
            Fwfps := FieldByName('ConNoPS').AsFloat;
            Post;
          end;

          if Fwfps < 0 then
          begin
            Fwfps := 0;
          end;

            //ShowMessage(FloatToStr(Fwfsl));
          Edit;
          FieldByName('WFRKPS').Value := FloatToStr(Fwfps);
          Post;
          Next;
        end;

      end;
      CDS_Main.EnableControls;
      CDS_Main.Locate('orderno', fFSId, []);
    end;

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmFinishedClothKCList.TBRafreshClick(Sender: TObject);
begin
  orderNo.SetFocus;
  ToolBar1.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

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

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

procedure TfrmFinishedClothKCList.FormShow(Sender: TObject);
var
  fsj: string;
begin
  CurrentPage := 1;
  RecordsNumber := 500;

  fsj := 'select distinct Code=Null,Name=CRType from FinishedCloth_CR where CRFlag=''入库'' order by CRType ';
//  SInitComBoxBySql(ADOQueryTemp, CRType, False, fsj);
  ReadCxGrid(self.Caption, Tv1, '成品仓库');
  SetStatus();
  InitGrid();
end;

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

procedure TfrmFinishedClothKCList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmFinishedClothKCList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmFinishedClothKCList.ToolButton1Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmFinishedClothKCList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmFinishedClothKCList.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmFinishedClothKCList.Tv1DblClick(Sender: TObject);
var
  Tabint: integer;
begin
  Tabint := cxTabControl1.TabIndex;
  case Tabint of
    0:
      begin
        AssignmentControls(Panel1, CDS_Main, '0');
        cxTabControl1.TabIndex := 1;
      end;
  end;
end;

procedure TfrmFinishedClothKCList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmFinishedClothKCList.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmFinishedClothKCList.tbOKClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  ModalResult := 1;
end;

procedure TfrmFinishedClothKCList.ordernoChange(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 TfrmFinishedClothKCList.ordernoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmFinishedClothKCList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  case cxTabControl1.TabIndex of
    1:
      begin
        if (AViewInfo.GridRecord.Values[v1Column7.Index] = '次品') then
        begin
          ACanvas.Brush.Color := $008080FF
        end;
      end;
  end;

end;

end.

