unit U_JYOrderCDOne;

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, cxPC,
  cxContainer, cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookAndFeels,
  cxLookAndFeelPainters, dxCore, cxDateUtils, cxNavigator, dxBarBuiltInMenu;

type
  TfrmJYOrderCDOnePB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    Label7: TLabel;
    C_Code: TEdit;
    Label8: TLabel;
    Label10: TLabel;
    MJID: TEdit;
    Label2: TLabel;
    CDQK: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    Label5: TLabel;
    C_CodeName: TEdit;
    Label6: TLabel;
    MJStr2: TComboBox;
    v1Column17: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    Filler: TComboBox;
    TBManage: TToolButton;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBZF: TToolButton;
    TBPrint: TToolButton;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    MovePanel2: TMovePanel;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    Label1: TLabel;
    SCPerson: TComboBox;
    v1Column19: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    Label9: TLabel;
    v1Column12: TcxGridDBColumn;
    Label4: TLabel;
    JXPerson: TComboBox;
    v1Column18: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    TBCL: TToolButton;
    TBCancel: TToolButton;
    Label12: TLabel;
    ClothType: TComboBox;
    v1Column29: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    Label13: TLabel;
    MJStr3: TComboBox;
    v1Column31: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    CDS_Print: TClientDataSet;
    v1Column16: TcxGridDBColumn;
    Label14: TLabel;
    ZZPerson: TEdit;
    v1Column25: TcxGridDBColumn;
    v1Column33: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Label15: TLabel;
    CarNo: TEdit;
    cxTabControl2: TcxTabControl;
    Panel4: TPanel;
    Label11: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    Order_Main: TClientDataSet;
    ADOQueryMain: TADOQuery;
    Panel2: TPanel;
    Label17: TLabel;
    YeShu: TEdit;
    Label18: TLabel;
    JQty: TEdit;
    Label19: TLabel;
    MJJingZ: TEdit;
    Label20: TLabel;
    MJMaoZ: TEdit;
    Label21: TLabel;
    DelMoney: TEdit;
    Label22: TLabel;
    AddMoney: TEdit;
    Label23: TLabel;
    CDGS: TEdit;
    Label24: TLabel;
    Label25: TLabel;
    CDHZQty: TEdit;
    Label26: TLabel;
    CDHZKgQty: TEdit;
    Label27: TLabel;
    Button1: TButton;
    v1Column14: TcxGridDBColumn;
    Label28: TLabel;
    MJStr4: TComboBox;
    v1Column34: TcxGridDBColumn;
    Label29: TLabel;
    CheckBox2: TCheckBox;
    v1Column36: TcxGridDBColumn;
    v1Column37: TcxGridDBColumn;
    Label16: TLabel;
    GCCode: TEdit;
    v1Column35: TcxGridDBColumn;
    Label30: TLabel;
    APBatchNo: TEdit;
    ToolButton1: TToolButton;
    v1Column39: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    v1Column41: TcxGridDBColumn;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    v1Column38: TcxGridDBColumn;
    CheJian: TComboBox;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label31: TLabel;
    C_Spec: TEdit;
    Tv1Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Image2Click(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TBManageClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBZFClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBCLClick(Sender: TObject);
    procedure TBCancelClick(Sender: TObject);
    procedure C_CodeKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl2Change(Sender: TObject);
    procedure ZhenCunTypeChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    procedure InitGridYe();
    procedure IntTab();
    procedure InitHZ();
    procedure InitForm();
    function Savedata(): Boolean;
    procedure InitButton();
    procedure PrintData(FFFMJID: string);
    function ConnData(): Boolean;
    { Private declarations }
  public
    FFInt: Integer;
    { Public declarations }
  end;

//var
 // frmJYOrderCDOnePB: TfrmJYOrderCDOnePB;

implementation

uses
  U_DataLink, U_Fun, U_MJEdit;     //,U_OrderInPut,U_JFMoneyList

{$R *.dfm}

procedure TfrmJYOrderCDOnePB.FormDestroy(Sender: TObject);
begin
  //frmJYOrderCDOnePB:=nil;
end;

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

procedure TfrmJYOrderCDOnePB.InitButton();
begin
  if Trim(Self.Caption) = '检验信息查询' then
  begin
    TBZF.Visible := False;
    TBManage.Visible := False;
    TBCL.Visible := False;
    TBCancel.Visible := False;
    cxTabControl1.Visible := False;
    cxTabControl1.TabIndex := 2;
  end;
end;

procedure TfrmJYOrderCDOnePB.FormCreate(Sender: TObject);
begin
  DQdate := SGetServerDate(ADOQueryTemp);
end;

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

procedure TfrmJYOrderCDOnePB.InitGridYe();
var
  fsj: string;
begin

  OrderNo.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_Viwe_JYInfo_Ye :begdate,:enddate,:Ye,:WSql ');
      if CheckBox1.Checked = True then
      begin
        if cxTabControl1.TabIndex <> 0 then
        begin
          Parameters.ParamByName('begdate').Value := BegDate.Text;
          Parameters.ParamByName('enddate').Value := EndDate.Text;
        end
        else if cxTabControl1.TabIndex = 0 then
        begin
          Parameters.ParamByName('begdate').Value := '';
          Parameters.ParamByName('enddate').Value := '';
        end;
      end
      else
      begin
        Parameters.ParamByName('begdate').Value := '';
        Parameters.ParamByName('enddate').Value := '';
      end;

      Parameters.ParamByName('Ye').Value := cxTabControl2.TabIndex;
      fsj := SGetFilters(Panel1, 1, 2);
      if Trim(fsj) <> '' then
      begin
        fsj := ' and ' + fsj;
      end
      else
      begin
        fsj := '';
      end;
      if Self.Caption <> '检验信息查询' then
      begin
        if cxTabControl1.TabIndex = 0 then
        begin
          fsj := fsj + ' and ChuLiFlag=0 ';

        end
        else if cxTabControl1.TabIndex = 1 then
        begin
          fsj := fsj + ' and ChuLiFlag=1 ';
        end;
      end;
      fsj := fsj + ' and isnull(JTType,'''')<>''挂码''  ';
      if CheckBox2.Checked then
      begin
        fsj := fsj + 'and isnull(CDQK,'''')<>'''' ';
      end;
      Parameters.ParamByName('WSql').Value := fsj;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  MovePanel2.Visible := False;
end;

procedure TfrmJYOrderCDOnePB.InitHZ();
var
  fsj: string;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Viwe_JYInfo_Ye_HZ :begdate,:enddate,:WSql');
    if CheckBox1.Checked = True then
    begin
      if cxTabControl1.TabIndex <> 0 then
      begin
        Parameters.ParamByName('begdate').Value := BegDate.Text;
        Parameters.ParamByName('enddate').Value := enddate.Text;
      end
      else if cxTabControl1.TabIndex = 0 then
      begin
        Parameters.ParamByName('begdate').Value := '';
        Parameters.ParamByName('enddate').Value := '';
      end;
    end
    else
    begin
      Parameters.ParamByName('begdate').Value := '';
      Parameters.ParamByName('enddate').Value := '';
    end;
    {if cxTabControl1.TabIndex<>0 then
    begin
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
    end else
    begin
      Parameters.ParamByName('begdate').Value:='2000-01-01';
      Parameters.ParamByName('enddate').Value:='2500-01-01';
    end; }
    fsj := SGetFilters(Panel1, 1, 2);
    if Trim(fsj) <> '' then
    begin
      fsj := ' and ' + fsj;
    end
    else
    begin
      fsj := '';
    end;
    fsj := fsj + ' and isnull(JTType,'''')<>''挂码''  ';
    if CheckBox2.Checked then
    begin
      fsj := fsj + 'and isnull(CDQK,'''')<>'''' ';
    end;
    if Self.Caption <> '检验信息查询' then
    begin
      if cxTabControl1.TabIndex = 0 then
      begin
        Parameters.ParamByName('WSql').Value := fsj + ' and ChuLiFlag=0 ';
      end
      else if cxTabControl1.TabIndex = 1 then
      begin
        Parameters.ParamByName('WSql').Value := fsj + ' and ChuLiFlag=1 ';
      end
      else
      begin
        Parameters.ParamByName('WSql').Value := fsj;
      end;
    end
    else
    begin
      Parameters.ParamByName('WSql').Value := fsj;
    end;
//    ShowMessage(sql.text);
    Open;
  end;
  YeShu.Text := IntToStr(cxTabControl2.Tabs.Count);
  JQty.Text := Trim(ADOQueryTemp.fieldbyname('JQty').AsString);
  MJJingZ.Text := Trim(ADOQueryTemp.fieldbyname('MJJingZ').AsString);
  MJMaoZ.Text := Trim(ADOQueryTemp.fieldbyname('MJMaoZ').AsString);
  CDGS.Text := Trim(ADOQueryTemp.fieldbyname('CDGS').AsString);
  CDHZQty.Text := Trim(ADOQueryTemp.fieldbyname('CDHZQty').AsString);
  CDHZKgQty.Text := Trim(ADOQueryTemp.fieldbyname('CDHZKgQty').AsString);
  AddMoney.Text := Trim(ADOQueryTemp.fieldbyname('AddMoney').AsString);
  DelMoney.Text := Trim(ADOQueryTemp.fieldbyname('DelMoney').AsString);
  if Trim(SCPerson.Text) <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Viwe_JYInfo_Ye_HZ_JF :begdate,:enddate,:WSql');
      if CheckBox1.Checked = True then
      begin
        if cxTabControl1.TabIndex <> 0 then
        begin
          Parameters.ParamByName('begdate').Value := BegDate.Text;
          Parameters.ParamByName('enddate').Value := enddate.Text;
        end
        else if cxTabControl1.TabIndex = 0 then
        begin
          Parameters.ParamByName('begdate').Value := '';
          Parameters.ParamByName('enddate').Value := '';
        end;
      end
      else
      begin
        Parameters.ParamByName('begdate').Value := '';
        Parameters.ParamByName('enddate').Value := '';
      end;
      {if cxTabControl1.TabIndex<>0 then
      begin
        Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
        Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      end else
      begin
        Parameters.ParamByName('begdate').Value:='2000-01-01';
        Parameters.ParamByName('enddate').Value:='2500-01-01';
      end;  }
      fsj := '';
      fsj := SGetFilters(Panel1, 1, 2);
      if Trim(fsj) <> '' then
      begin
        fsj := ' and ' + fsj;
      end
      else
      begin
        fsj := '';
      end;
      if Self.Caption <> '检验信息查询' then
      begin
        if cxTabControl1.TabIndex = 0 then
        begin
          Parameters.ParamByName('WSql').Value := fsj + ' and ChuLiFlag=0 ';
        end
        else if cxTabControl1.TabIndex = 1 then
        begin
          Parameters.ParamByName('WSql').Value := fsj + ' and ChuLiFlag=1 ';
        end
        else
        begin
          Parameters.ParamByName('WSql').Value := fsj;
        end;
      end
      else
      begin
        Parameters.ParamByName('WSql').Value := fsj;
      end;
      Open;
    end;
    AddMoney.Text := Trim(ADOQueryTemp.fieldbyname('AddMoney').AsString);
    DelMoney.Text := Trim(ADOQueryTemp.fieldbyname('DelMoney').AsString);
  end;
end;

procedure TfrmJYOrderCDOnePB.InitForm();
var
  fsj: string;
begin
  ReadCxGrid(Self.Caption, Tv1, '检验管理1');
  fsj := 'select distinct(Filler) name from WFB_MJJY_PB   ';
  SInitComBoxBySql(ADOQueryCmd, Filler, False, fsj);
//  fsj := 'select name=ZdyName  from KH_Zdy where Type=''SCPerson'' order by ZdyName  ';
  fsj := 'select  UserName name from SY_User where Udept=''挡车工'' order by UserID  ';
  SInitComBoxBySql(ADOQueryCmd, SCPerson, False, fsj);
  fsj := 'select name=ZdyName  from KH_Zdy where Type=''JXPerson'' order by ZdyName   ';
  SInitComBoxBySql(ADOQueryCmd, JXPerson, False, fsj);
  fsj := 'select name=ZdyName  from KH_Zdy where Type=''ClothType'' order by ZdyName   ';
  SInitComBoxBySql(ADOQueryCmd, ClothType, False, fsj);
  EndDate.Text := FormatDateTime('yyyy-MM-dd', SGetServerDate10(ADOQueryTemp) + 1) + ' 08:00:00';
  BegDate.Text := FormatDateTime('yyyy-MM-dd', SGetServerDate10(ADOQueryTemp) - 1) + ' 08:00:00';
  InitButton();
  //InitGrid();
end;

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

function TfrmJYOrderCDOnePB.ConnData(): Boolean;
begin
  if not Assigned(DataLink_WeavingSchedule) then
    DataLink_WeavingSchedule := TDataLink_WeavingSchedule.Create(Application);
  try
    with DataLink_WeavingSchedule.ADOLink do
    begin
      //if not Connected then
      begin
        Connected := false;
        ConnectionString := DConString;
        LoginPrompt := false;
        Connected := true;
      end;
    end;
    Result := true;
  except
    Result := false;
    application.MessageBox('数据库连接失败！', '错误', mb_Ok + MB_ICONERROR);
  end;

end;

procedure TfrmJYOrderCDOnePB.TBRafreshClick(Sender: TObject);
begin
  IntTab();
  InitGridYe();
  InitHZ();
end;

procedure TfrmJYOrderCDOnePB.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmJYOrderCDOnePB.Panel10MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  TWinControl(Panel4).Perform(WM_SYSCOMMAND, $F012, 0);
end;

procedure TfrmJYOrderCDOnePB.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

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

procedure TfrmJYOrderCDOnePB.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Left := FLeft;
  Panel4.Top := FTop + 110;
  Panel4.Visible := True;
  Panel4.Refresh;
  Panel10.Caption := Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text := Order_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
end;

procedure TfrmJYOrderCDOnePB.TBManageClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PBCP_CR where CRFlag=''出库'' and MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已产生出库数据不能删除!', '提示', 0);
    Exit;
  end;
  try
    frmMJEditPB := TfrmMJEditPB.Create(Application);
    with frmMJEditPB do
    begin
      MJID.Text := Trim(Self.Order_Main.fieldbyname('MJID').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmMJEditPB.Free;
  end;
end;

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

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

procedure TfrmJYOrderCDOnePB.TBZFClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要作废数据吗？作废后数据不能恢复。', '提示', 32 + 4) <> IDYES then
    Exit;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_PBCP_CR where CRFlag=''入库'' and MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty = False then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('已产生入库数据不能删除!', '提示', 0);
          Exit;
        end
        else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete WFB_MJJY_PB_DelLog where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            SQL.Add('insert into WFB_MJJY_PB_DelLog ');
            sql.Add('select * from WFB_MJJY_PB where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY_PB_DelLog Set Note=Isnull(Note,'''')+''→删除人:''+''' + Trim(DName) + '''');
            sql.Add('+'' ''+Convert(Varchar(20),getdate(),120)');
            sql.Add(' where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete WFB_MJJY_PB where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete WFB_MJJY_CD_PB where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete CK_PBCP_CR where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            sql.Add('delete CK_PBCP_kc where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            SQL.Add('Update JYCon_Sub_AnPai Set JYFlag=0 where APID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
        end;
        Order_Main.Delete;
      end
      else
        Next;
    end;
  end;
  Order_Main.EnableControls;

end;

procedure TfrmJYOrderCDOnePB.PrintData(FFFMJID: string);
var
  fPrintFile: string;
  i: Integer;
  FDate: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\重打布票标签.rmf';
  if FileExists(fPrintFile) then
  begin
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(FFFMJID);
      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
      if FileExists(fImagePath) then
        DeleteFile(fImagePath);
      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
    except
      application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Print_PBLab :MJID');
      Parameters.ParamByName('MJID').Value := Trim(FFFMJID);
      Open;
    end;
    SCreateCDS20(ADOQueryPrint, CDS_Print);
    SInitCDSData20(ADOQueryPrint, CDS_Print);
    RM2.LoadFromFile(fPrintFile);
    RMVariables['QRBARCODE'] := fImagePath;
    RM2.ShowReport;
//    RM2.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\重打布票标签.rmf'), '提示', 0);
    Exit;
  end;
end;

procedure TfrmJYOrderCDOnePB.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while Order_Main.Locate('SSel', True, []) do
    begin
      PrintData(Trim(Order_Main.FieldByName('MJID').AsString));
      Edit;
      FieldByName('SSel').Value := false;
    end;
  end;
  Order_Main.EnableControls;
end;

function TfrmJYOrderCDOnePB.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 TfrmJYOrderCDOnePB.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    IntTab();
    InitGridYe();
    InitHZ();
  end;
end;

procedure TfrmJYOrderCDOnePB.cxTabControl1Change(Sender: TObject);
begin
  if Self.Caption = '检验信息查询' then
    Exit;
  IntTab();
  InitGridYe();
  InitHZ();
end;

procedure TfrmJYOrderCDOnePB.TBCLClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 0 then
    Exit;
  if Order_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要处理数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel', True, []) do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' UPdate WFB_MJJY_PB Set ChuLiFlag=1,ChuLiDate=getdate(),ChuLiPerson=''' + Trim(DName) + '''');
            sql.Add(' where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
          Order_Main.Delete;
        end;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmJYOrderCDOnePB.TBCancelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if cxTabControl1.TabIndex <> 1 then
    Exit;
  if Order_Main.Locate('ssel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销处理吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel', True, []) do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' UPdate WFB_MJJY_PB Set ChuLiFlag=0,ChuLiDate=getdate(),ChuLiPerson=''' + Trim(DName) + '''');
            sql.Add(' where MJID=''' + Trim(Order_Main.fieldbyname('MJID').AsString) + '''');
            ExecSQL;
          end;
          Order_Main.Delete;
        end;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmJYOrderCDOnePB.C_CodeKeyPress(Sender: TObject; var Key: Char);
begin
  {if Key=#13 then
  begin
    if Length(C_Code.Text)<3 then Exit;
    MovePanel2.Visible:=True;
    MovePanel2.Refresh;
    try
      Order_Main.DisableControls;
      with Order_Main do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec P_Viwe_JYInfo :begdate,:enddate,:PState,:FFSql ');

        Parameters.ParamByName('PState').Value:=3;
        if cxTabControl1.TabIndex=0 then
        begin
          Parameters.ParamByName('FFSql').Value:=' and BB.C_Code='''+Trim(C_Code.Text)+'''   and ChuLiFlag=0 ';
        end else
        if cxTabControl1.TabIndex=1 then
        begin
          Parameters.ParamByName('FFSql').Value:=' and BB.C_Code='''+Trim(C_Code.Text)+'''   and ChuLiFlag=1 ';
        end else
        begin
          Parameters.ParamByName('FFSql').Value:=' and BB.C_Code='''+Trim(C_Code.Text)+'''';
        end;
        if CheckBox1.Checked=False then
        begin
          Parameters.ParamByName('begdate').Value:='';
          Parameters.ParamByName('enddate').Value:='';
        end else
        begin
          if cxTabControl1.TabIndex<>0 then
          begin
            Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
            Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
          end else
          begin
            Parameters.ParamByName('begdate').Value:='2000-01-01';
            Parameters.ParamByName('enddate').Value:='2500-01-01';
          end;
        end;
        Open;
      end;
      //SCreateCDS20(ADOQueryMain,Order_Main);
     // SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      Order_Main.EnableControls;
    end;
    MovePanel2.Visible:=False;
  end; }
end;

procedure TfrmJYOrderCDOnePB.IntTab();
var
  JLInt, YeInt, i: Integer;
  fsj, fsj10: string;
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select JLInt=isnull(Count(*),0) from (');
    sql.add('select AA.* from (');
    sql.Add('select C.*,CM.ConNo Orderno,BB.C_Code,BB.C_CodeName,BB.BatchNoHZ,BB.C_Spec,BB.C_Color ');
    sql.Add('from WFB_MJJY_PB C ');
    sql.Add(' inner join Cloth_Sub BB on C.MainId=BB.MainId ');
    sql.Add(' inner join Cloth_Main CM on BB.MainId=CM.MainId');

    if cxTabControl1.TabIndex = 0 then
    begin
      sql.Add(' where C.FillTime>=''' + Trim('2000-01-01') + '''');
      sql.Add(' and C.FillTime<''' + Trim('2500-01-01') + '''');
    end
    else
    begin
      if CheckBox1.Checked = True then
      begin
        sql.Add(' where C.FillTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and C.FillTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      end
      else
      begin
        sql.Add(' where C.FillTime>=''' + Trim('2000-01-01') + '''');
        sql.Add(' and C.FillTime<''' + Trim('2500-01-01') + '''');
      end;
    end;
    if cxTabControl1.TabIndex = 0 then
    begin
      sql.add(' and ChuLiFlag=0 ');
    end
    else if cxTabControl1.TabIndex = 1 then
    begin
      sql.add(' and ChuLiFlag=1 ');
    end;

    sql.Add(' and isnull(JTType,'''')<>''挂码'' ');
    if CheckBox2.Checked then
    begin
      sql.Add(' and isnull(CDQK,'''')<>'''' ');
    end;
    fsj10 := SGetFilters(Panel1, 1, 2);
    if Trim(fsj10) <> '' then
    begin
      fsj10 := ' and ' + fsj10;
    end
    else
    begin
      fsj10 := '';
    end;
    SQL.Add(' ) AA Where 1=1 ');
    sql.Add(fsj10);
    SQL.Add(') AAA');
//    ShowMessage(SQL.Text);
    open;
  end;
  JLInt := ADOQueryTemp.fieldbyname('JLInt').AsInteger;
  YeInt := JLInt div 500;
  YeInt := YeInt + 1;
  cxTabControl2.Tabs.Clear;
  for i := 1 to YeInt do
  begin
    fsj := '第' + IntToStr(i) + '页';
    cxTabControl2.Tabs.Add(fsj);
  end;

end;

procedure TfrmJYOrderCDOnePB.cxTabControl2Change(Sender: TObject);
begin
  InitGridYe();
  InitHZ();
end;

procedure TfrmJYOrderCDOnePB.ZhenCunTypeChange(Sender: TObject);
begin
  IntTab();
  InitGridYe();
  InitHZ();
end;

procedure TfrmJYOrderCDOnePB.Button1Click(Sender: TObject);
begin
//  try
//    frmJFMoneyList:=TfrmJFMoneyList.Create(Application);
//    with frmJFMoneyList do
//    begin
//
//      frmJFMoneyList.Align:=alClient;
//      if ShowModal=1 then
//      begin
//
//      end;
//    end;
//  finally
//    frmJFMoneyList.Free;
//  end;
end;

procedure TfrmJYOrderCDOnePB.ToolButton1Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
begin
  if Order_Main.IsEmpty then
    Exit;

  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  FMainID := '';
  Order_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(Order_Main.fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  Order_Main.EnableControls;

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select distinct CONVERT(varchar(100), Filltime, 23) from WFB_MJJY_PB A');
    sql.Add(' where  A.MJID in (select SubId from TBSubID where DName=' + quotedstr(Trim(DName)) + ') ');
    Open;

  end;

  if ADOQueryTemp.RecordCount > 1 then
  begin
    Application.MessageBox('日期不是同一天', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Do_Print_JYBBZZ ');
    sql.add(' @DName=' + quotedstr(Trim(DCode)));
    Open;
  end;
  SCreateCDS20(ADOQueryCmd, CDS_Print);
  SInitCDSData20(ADOQueryCmd, CDS_Print);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\针织检验报告.rmf';
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmJYOrderCDOnePB.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  FMJStr2, FMJStr4: string;
begin

//  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('MJStr2').Index] = null then
//    Exit;
//  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('MJStr4').Index] = null then
//    Exit;
  FMJStr2 := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('MJStr2').Index];
  FMJStr4 := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('MJStr4').Index];

//  if FMJStr2 = '已入库' then
//    ACanvas.Font.Color := $7280FA;
  if FMJStr4 = '未出库' then
    ACanvas.Font.Color := $FF0000;
  if FMJStr4 = '已出库' then
    ACanvas.Font.Color := $0000FF;

end;

end.

