unit U_FinishedClothZJing;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, MovePanel, cxTextEdit,
  cxContainer, cxCurrencyEdit, BtnEdit, RM_e_main, RM_e_htm, RM_e_Graphic,
  RM_e_Jpeg, RM_e_txt, RM_e_csv, RM_E_llPDF, Math, Clipbrd, cxPC, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, dxSkinsCore,
  dxSkinsDefaultPainters, dxSkinscxPCPainter;

type
  TfrmFinishedClothZJing = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_Main: TDataSource;
    v1Column4: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    MovePanel2: TMovePanel;
    v1Column32: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    N3: TMenuItem;
    X1: TMenuItem;
    Order_Main: TClientDataSet;
    v1Column1: TcxGridDBColumn;
    Panel3: TPanel;
    Panel2: TPanel;
    Panel4: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    Label1: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    v1Column2: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Panel1: TPanel;
    Label7: TLabel;
    Label5: TLabel;
    lab1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    UserName: TEdit;
    P_Code: TEdit;
    IsJYTime: TCheckBox;
    P_CodeName: TEdit;
    Label3: TLabel;
    CarNo: TEdit;
    Label4: TLabel;
    WBCode: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Label6: TLabel;
    KHName: TEdit;
    Label8: TLabel;
    ConNo: TEdit;
    ClType: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Label9: TLabel;
    chejian: TEdit;
    ToolButton2: TToolButton;
    Panel5: TPanel;
    Label10: TLabel;
    Edit1: TEdit;
    Label11: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure MJStr2Change(Sender: TObject);
    procedure KHConNOKeyPress(Sender: TObject; var Key: Char);
    procedure RKOrdIDKeyPress(Sender: TObject; var Key: Char);
    procedure N3Click(Sender: TObject);
    procedure X1Click(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure APIDKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    FDCheG, FPCode: string;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure InitForm();
    function Savedata(): Boolean;
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    procedure SetStatus();
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_Fun, U_MJEdit, U_ZDYHelp, U_SysLogHelp;

{$R *.dfm}

procedure TfrmFinishedClothZJing.SetStatus();
var
  i: Integer;
begin

  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);
        v1Column10.Visible := True;
        Tv1Column1.Visible := True;
        Tv1Column2.Visible := True;
        v1Column1.Visible := True;
      end;

    1:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;
        v1Column1.Visible := False;
        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
      end;
  end;
end;

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

procedure TfrmFinishedClothZJing.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmFinishedClothZJing.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption, Tv1, '检验管理');
  Close;
end;

procedure TfrmFinishedClothZJing.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      case cxTabControl1.TabIndex of
        0:   /////////////////////////订单汇总//////////////////////////////
          begin
            Pwhere := SGetFilters(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' where ' + trim(Pwhere);

            sql.Add('exec P_CK_Cloth_ZJHZ ');
            sql.add('@Begdate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
            sql.Add(',@enddate=' + QuotedStr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1))));
            sql.Add(',@WSql=' + QuotedStr(fwhere) + ' ');
          end;
        1:      /////////////////////////明细//////////////////////////////
          begin
            fwhere := ' where CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
            fwhere := fwhere + ' and CRTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
            fwhere := fwhere + ' and ClType = ''整经'' ';

            Pwhere := SGetFilters(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            sql.Add('exec P_CK_Cloth_ZJList ');
            sql.Add('@pageIndex=' + inttostr(CurrentPage));
            sql.Add(',@pageSize=' + inttostr(RecordsNumber));
            sql.Add(',@criteria= ' + quotedstr(fwhere));
          end;
      end;
      //ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
    if cxTabControl1.TabIndex = 1 then
      LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
end;

procedure TfrmFinishedClothZJing.InitForm();
var
  fsj: string;
begin
  ReadCxGrid(self.Caption, Tv1, '检验管理');
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
end;

procedure TfrmFinishedClothZJing.TBFindClick(Sender: TObject);
var
  fsj, fwhere: string;
begin

end;

procedure TfrmFinishedClothZJing.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  SetStatus();
  InitGrid();

end;

procedure TfrmFinishedClothZJing.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 500;
  InitForm();
  SetStatus();
  InitGrid();
end;

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

procedure TfrmFinishedClothZJing.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main, True);
end;

procedure TfrmFinishedClothZJing.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main, False);
end;

function TfrmFinishedClothZJing.Savedata(): Boolean;
var
  maxno, LLID: string;
  HXPS: Integer;
  HXQty, HXMQty: Double;
begin
  HXPS := 0;
  HXQty := 0;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1 * from Contract_Cloth_LL where OrdSubId=''' + Trim(Order_Main.fieldbyname('SubId').AsString) + '''');
    sql.Add(' and FirstName=''' + Trim(Order_Main.fieldbyname('AOrddefstr4').AsString) + '''');
    Open;
  end;
  LLID := Trim(ADOQueryTemp.fieldbyname('LLID').AsString);
  if Trim(LLID) = '' then
  begin
    Result := False;
    Application.MessageBox('没有相对应的加工厂!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd, maxno, 'HX', 'Contract_Cloth_BefChkHX', 2, 1) = False then
    begin
      Result := False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取回修最大号失败!', '提示', 0);
      Exit;
    end;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          HXPS := HXPS + 1;
          HXQty := HXQty + Order_Main.FieldByName('MJLen').Value;
        end;
        Next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('');
      Open;
    end;
  except
  end;
end;

procedure TfrmFinishedClothZJing.MJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmFinishedClothZJing.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  //Clipboard.SetTextBuf(PChar(Trim(Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmFinishedClothZJing.MJStr2Change(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmFinishedClothZJing.KHConNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmFinishedClothZJing.RKOrdIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmFinishedClothZJing.N3Click(Sender: TObject);
var
  SSubId, SGangNo: string;
begin

  with Order_Main do
  begin
    SSubId := trim(FieldByName('SubId').AsString);
    Order_Main.DisableControls;
    first;
    while not eof do
    begin
      if SSubId = trim(FieldByName('SubId').AsString) then
      begin
        edit;
        FieldByName('SSel').value := true;
        Post;
      end
      else
      begin
        edit;
        FieldByName('SSel').value := false;
        Post;
      end;
      Next;
    end;
    Order_Main.Locate('SubId', VarArrayOf([SSubId]), []);
    Order_Main.EnableControls;
  end;
end;

procedure TfrmFinishedClothZJing.X1Click(Sender: TObject);
var
  str, SSubId: string;
  ZChiShu, DQChiShu: integer;
  ISSel: boolean;
begin
  ISSel := true;
  SSubId := trim(Order_Main.FieldByName('SubId').AsString);
  str := '';
  if InputQuery('选择', '请输入数字', str) then
  begin
    ZChiShu := strtointdef(str, 0);
    if ZChiShu <= 0 then
      exit;

    with Order_Main do
    begin

      Order_Main.DisableControls;
      first;
      DQChiShu := 0;
      while not eof do
      begin

        if SSubId = trim(FieldByName('SubId').AsString) then
        begin
          edit;
          FieldByName('SSel').value := ISSel;
          DQChiShu := DQChiShu + 1;
          Post;
        end
        else
        begin
          edit;
          FieldByName('SSel').value := false;
          Post;
        end;
        if DQChiShu = ZChiShu then
          ISSel := false;
        Next;
      end;
      Order_Main.Locate('SubId', VarArrayOf([SSubId]), []);
      Order_Main.EnableControls;
    end;
  end;

end;

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

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

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

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

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

procedure TfrmFinishedClothZJing.APIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if ADOQueryMain.Active = False then
      Exit;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  end;
end;

procedure TfrmFinishedClothZJing.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('整经产量统计', cxGrid1);
end;

procedure TfrmFinishedClothZJing.ToolButton2Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex = 0 then
  begin

    Exit;
  end;
  Panel5.Visible := True;
  Edit2.Text := Order_Main.fieldbyname('CarNo').AsString;

end;

procedure TfrmFinishedClothZJing.Button2Click(Sender: TObject);
begin
  Panel5.Visible := false;
end;

procedure TfrmFinishedClothZJing.Button1Click(Sender: TObject);
begin
  if Edit1.Text = '' then
  begin
    Application.MessageBox('机台号不能为空!', '提示', 0);
    Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.add('update CK_YgCl set  carno= ' + QuotedStr(Trim(Edit1.Text)));
      sql.add('where clid=''' + trim(Order_Main.fieldbyname('clid').asstring) + ''' ');

      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(Order_Main.FieldByName('clid').AsString) + ' 原机台号：' + trim(edit2.Text) + ' 现机台号：' + trim(edit1.Text))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;

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

end.

