unit U_JYOrderCDListGH;

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;

type
  TfrmJYOrderCDListGH = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    conNO: TEdit;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    MJID: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label4: TLabel;
    MJType: TComboBox;
    v1Column13: TcxGridDBColumn;
    Label5: TLabel;
    MPRTCodeName: TEdit;
    Label6: TLabel;
    MJStr2: TComboBox;
    v1Column17: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Label12: TLabel;
    factoryName: TEdit;
    Filler: TComboBox;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    v1Column24: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    CDS_CD: TClientDataSet;
    RMCD: TRMGridReport;
    RMDBCD: TRMDBDataSet;
    CDS_LS: TClientDataSet;
    v1Column15: TcxGridDBColumn;
    v1MKXS: TcxGridDBColumn;
    Mjstr4: TEdit;
    PrtColor: TEdit;
    RM3: TRMGridReport;
    v1Column31: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column33: TcxGridDBColumn;
    KHConNO: TEdit;
    Label19: TLabel;
    v1Column34: TcxGridDBColumn;
    v1Column36: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    CDS_HZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    RMDBPRT: TRMDBDataSet;
    CDS_PRT: TClientDataSet;
    v1Column16: TcxGridDBColumn;
    Label1: TLabel;
    RKOrdID: TEdit;
    N3: TMenuItem;
    X1: TMenuItem;
    cxTabControl1: TcxTabControl;
    v1Column18: TcxGridDBColumn;
    lbl1: TLabel;
    APID: TEdit;
    Label2: TLabel;
    RCGangNo: TEdit;
    v1Column29: TcxGridDBColumn;
    Label11: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(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 OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure MJIDKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure conNOKeyPress(Sender: TObject; var Key: Char);
    procedure MJStr2Change(Sender: TObject);
    procedure MJTypeChange(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 cxTabControl1Change(Sender: TObject);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure InitForm();
    function Savedata(): Boolean;
    procedure GetCDSData();
    procedure SetStatus();
    procedure PrintCKMDG(MRKOrdID: string);
    { Private declarations }
  public
    FFInt: Integer;
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_OrderInPut, U_Fun, U_MJEdit, U_ZDYHelp, U_ProductOrderListSel,
  U_SysLogHelp,U_RTFun;

{$R *.dfm}
procedure TfrmJYOrderCDListGH.SetStatus();
begin
//  TBManage.Visible := False;
//  TBZF.Visible := False;
//  v1Column15.Options.Focusing := False;
//  ToolButton1.Visible := False;
//  TBSH.Visible := False;
//  TBHH.Visible := False;
//  if Trim(canshu1) = '管理' then
//  begin
//    TBManage.Visible := True;
//    TBZF.Visible := True;
//    v1Column15.Options.Focusing := True;
//    ToolButton1.Visible := true;
//  end;
//  if Trim(canshu1) = '次品管理' then
//  begin
//    TBSH.Visible := True;
//    TBHH.Visible := True;
//  end;
//  case cxTabControl1.TabIndex of
//    0:
//      begin
//        ToolButton8.Visible := true;
//        ToolButton4.Visible := false;
//      end;
//    1:
//      begin
//        ToolButton8.Visible := false;
//        ToolButton4.Visible := true;
//      end;
//    2:
//      begin
//        ToolButton8.Visible := false;
//        ToolButton4.Visible := true;
//      end;
//  end;
end;

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

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

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

procedure TfrmJYOrderCDListGH.PrintCKMDG(MRKOrdID: string);

begin
 

end;

procedure TfrmJYOrderCDListGH.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
//  if IsJYTime.Checked then
//  begin
//    fwhere := ' where Filltime>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
//    fwhere := fwhere + ' and Filltime<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
//  end;

  Pwhere := SGetFilters(Panel1, 1, 2);

  if trim(Pwhere) <> '' then
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          Pwhere := Pwhere + ' and MJStr2=''未入库'' ';
        end;
      1:
        begin
          Pwhere := Pwhere + ' and MJStr2<>''未入库'' ';
        end;
    end;
  end
  else
  begin
    case cxTabControl1.TabIndex of
      0:
        begin
          Pwhere := ' MJStr2=''未入库'' ';
        end;
      1:
        begin
          Pwhere := ' MJStr2<>''未入库'' ';
        end;
      2:
        begin
          Pwhere := ' 1=1 ';
        end;
    end;
  end;


  if trim(fwhere) <> '' then
  begin
    fwhere := fwhere + ' and ' + trim(Pwhere);
  end
  else
  begin
    fwhere := ' where ' + trim(Pwhere);
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_Page_ClothInfoGH ');
      sql.Add('@BegDate=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)));
      sql.Add(',@EndDate=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
//    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(Order_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
end;

procedure TfrmJYOrderCDListGH.InitForm();
var
  fsj: string;
begin

  ReadCxGridAll(trim(self.Caption), Tv1);
  fsj := 'select distinct(Filler) name from WFB_MJJY   ';
  SInitComBoxBySql(ADOQueryCmd, Filler, False, fsj);
  Filler.ItemIndex := Filler.Items.IndexOf(trim(DName));
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 14;
  //InitGrid();
end;

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

end;

procedure TfrmJYOrderCDListGH.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '检验报告');
end;

procedure TfrmJYOrderCDListGH.TBRafreshClick(Sender: TObject);
begin
  OrderNo.SetFocus;
  InitGrid();

end;

procedure TfrmJYOrderCDListGH.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 500;
  InitForm();

  SetStatus();
end;

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

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

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

function TfrmJYOrderCDListGH.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 TfrmJYOrderCDListGH.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(OrderNo.Text) < 4 then
      Exit;
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmJYOrderCDListGH.GetCDSData();
begin
  CDS_CD.FieldByName('MJXH').Value := CDS_LS.fieldbyname('MJXH').Value;
  CDS_CD.FieldByName('GangNo').Value := CDS_LS.fieldbyname('Aorddefstr1').Value;
  CDS_CD.FieldByName('PRTColor').Value := CDS_LS.fieldbyname('PRTColor').Value;
  CDS_CD.FieldByName('PRTEColor').Value := CDS_LS.fieldbyname('SOrddefstr4').Value;
  CDS_CD.FieldByName('MJLen').Value := CDS_LS.fieldbyname('MJLen').Value;
  CDS_CD.FieldByName('MJJZ').Value := CDS_LS.fieldbyname('MJQty4').Value;
  CDS_CD.FieldByName('MJMZ').Value := CDS_LS.fieldbyname('MJMaoZ').Value;
  CDS_CD.FieldByName('MJFK').Value := CDS_LS.fieldbyname('MJFK').Value;
  CDS_CD.FieldByName('KFQtyHZ').Value := CDS_LS.fieldbyname('KFQtyHZ').Value;
  CDS_CD.FieldByName('DengJi').Value := CDS_LS.fieldbyname('DengJi').Value;
  CDS_CD.FieldByName('PanDing').Value := CDS_LS.fieldbyname('PanDing').Value;
  CDS_CD.FieldByName('MJID').Value := CDS_LS.fieldbyname('MJID').Value;
end;

procedure TfrmJYOrderCDListGH.v1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with Order_Main do
  begin
    Edit;
    FieldByName('PanDing').Value := Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate WFB_MJJY Set PanDing=''' + Trim(mvalue) + '''');
    SQL.Add(' where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmJYOrderCDListGH.MJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(MJID.Text) < 4 then
      Exit;
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmJYOrderCDListGH.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 TfrmJYOrderCDListGH.conNOKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

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

procedure TfrmJYOrderCDListGH.MJTypeChange(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
end;

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

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

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

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

procedure TfrmJYOrderCDListGH.X1Click(Sender: TObject);
var
  str, SSubId, SGangNo: string;
  ZChiShu, DQChiShu: integer;
  ISSel: boolean;
begin
  ISSel := true;
  SSubId := trim(Order_Main.FieldByName('SubId').AsString);
  SGangNo := trim(Order_Main.FieldByName('Mjstr4').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)) and (SGangNo = trim(FieldByName('Mjstr4').AsString))) then
        begin
          edit;
          FieldByName('SSel').value := ISSel;
          DQChiShu := DQChiShu + 1;

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

end;

procedure TfrmJYOrderCDListGH.cxTabControl1Change(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();
  SetStatus();
end;

end.

