unit U_PlanCardList;

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, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxCheckBox, cxPC,
  Menus, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu,
  cxContainer, cxMaskEdit, Math, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinscxPCPainter;

type
  TfrmPlanCardList = class(TForm)
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_Plan: TDataSource;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    RMDBPRT: TRMDBDataSet;
    Panel2: TPanel;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Label1: TLabel;
    Label5: TLabel;
    Label10: TLabel;
    Label6: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    P_CodeName: TEdit;
    P_Code: TEdit;
    BatchNo: TEdit;
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    Panel4: TPanel;
    Panel5: TPanel;
    edtPiE: TEdit;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label4: TLabel;
    cxTabControl1: TcxTabControl;
    Label8: TLabel;
    edtTouWen: TEdit;
    Label11: TLabel;
    edtLen: TEdit;
    DS_Card: TDataSource;
    CDS_Card: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Button3: TButton;
    CDS_Prt: TClientDataSet;
    ADOQueryPrint: TADOQuery;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    APMachineNo: TEdit;
    Button4: TButton;
    Button1: TButton;
    ToolButton1: TToolButton;
    N2: TMenuItem;
    N3: TMenuItem;
    v1Column8: TcxGridDBColumn;
    Label12: TLabel;
    edtShangP: TEdit;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    Label13: TLabel;
    edtKaNo: TEdit;
    V2Column1: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    CDS_PJS: TClientDataSet;
    RMDB_JS: TRMDBDataSet;
    CDS_PWS: TClientDataSet;
    RMDB_WS: TRMDBDataSet;
    CDS_CZ: TClientDataSet;
    RMDB_CZ: TRMDBDataSet;
    CDS_QZ: TClientDataSet;
    RMDB_QZ: TRMDBDataSet;
    Button2: TButton;
    Button5: TButton;
    Tv1Column1: TcxGridDBColumn;
    Label21: TLabel;
    ZJPiLen: TEdit;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    Tv1Column2: TcxGridDBColumn;
    ToolButton4: TToolButton;
    RMDB_Main: TRMDBDataSet;
    CDS_PM: TClientDataSet;
    CDS_YarnJX: TClientDataSet;
    DS_YarnJX: TDataSource;
    CDS_YarnWX: TClientDataSet;
    DS_YarnWX: TDataSource;
    CDS_PLAN: TClientDataSet;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    Label2: TLabel;
    P_Code1: TEdit;
    edtQJingG: TEdit;
    Label3: TLabel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    Label7: TLabel;
    jcfactoryname: TEdit;
    Tv1Column4: TcxGridDBColumn;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle3: TcxStyle;
    TV2Column5: TcxGridDBColumn;
    N4: TMenuItem;
    Tv1Column9: TcxGridDBColumn;
    Label9: TLabel;
    conno: TEdit;
    Tv1Column10: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    Label14: TLabel;
    edZDFNo: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure P_CodeChange(Sender: TObject);
    procedure cxGridDBColumn11PropertiesEditValueChanged(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure cbbSYYLPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure TV3Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV4Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure edtLenChange(Sender: TObject);
    procedure edtPiEExit(Sender: TObject);
    procedure Tv1Column8PropertiesChange(Sender: TObject);
    procedure edtQJingGClick(Sender: TObject);
    procedure P_Code1KeyPress(Sender: TObject; var Key: Char);
    procedure jcfactorynameClick(Sender: TObject);
    procedure N4Click(Sender: TObject);
  private
    FInt, PFInt: Integer;
    FMainId, FS, FFPCId: string;
    procedure InitGrid();
    procedure InitForm();
    function SaveData(): Boolean;
    procedure PrtDate(MPCid: string);
    procedure DelCard(MPCid: string);
    procedure InitCard(MSubid: string);
    { Private declarations }
  public
    canshu1, FCheJian: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZdyMapSel, U_ZdyAttachGYS, U_ZDYHelp2,
  U_ClothSCDCGAnPaiMX;

{$R *.dfm}
procedure TfrmPlanCardList.InitCard(MSubid: string);
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,yapps=ISNULL((select count(*) from PD_Plan_AnPai E WHERE E.PCID=A.PCID),0)  ');
      SQL.Add(',gytype=(SELECT gytype FROM PD_PLAN_MAIN C WHERE C.MAINID=B.MAINID)');
      sql.Add(' from PD_Plan_Card A inner join PD_Plan_Card_MS B on A.PCID=B.PCID');
      sql.Add(' where B.Subid=' + quotedstr(Trim(MSubid)));
      Open;
    end;

    SCreateCDS20(ADOQueryTemp, CDS_Card);
    SInitCDSData20(ADOQueryTemp, CDS_Card);

  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmPlanCardList.DelCard(MPCid: string);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from PD_Plan_AnPai  where PCID=' + quotedstr(Trim(MPCid)));
    Open;
  end;
  if ADOQueryCmd.IsEmpty = False then
  begin
    Application.MessageBox('已打印布票，不能删除!', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from CK_WarpBeam_CR  where CRQtyFlag=-1 AND  PCID=' + quotedstr(Trim(MPCid)));
    SQL.Add('AND VALID=''Y''');
    Open;
  end;
  if ADOQueryCmd.IsEmpty = False then
  begin
    Application.MessageBox('经轴已出库，不能删除!', '提示', 0);
    Exit;
  end;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;

//      sql.Add(' Update PD_Plan_Card Set DelTime=getdate(),DelName=''' + Trim(DName) + ''',DelCode=''' + Trim(DCode) + '''');
//      sql.Add(' where PCID=' + quotedstr(Trim(MPCid)));
      sql.Add(' delete PD_Plan_Card where PCID=' + quotedstr(Trim(MPCid)));
      sql.Add('delete CK_WarpBeam_CR where valid=''Y'' and JZRKID=' + quotedstr(Trim(MPCid)));

//      sql.Add(' Update PD_Plan_Card_MS Set DelTime=getdate(),DelName=''' + Trim(DName) + ''',DelCode=''' + Trim(DCode) + '''');
//      sql.Add(' where PCID=' + quotedstr(Trim(MPCid)));
      sql.Add(' delete PD_Plan_Card_MS where PCID=' + quotedstr(Trim(MPCid)));
      sql.Add(' delete PD_Plan_Yarn_CAR where PCID=' + quotedstr(Trim(MPCid)));
//      ShowMessage(sql.text );
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('删除失败!', '提示', 0);
  end;

end;

procedure TfrmPlanCardList.PrtDate(MPCid: string);
var
  fPrintFile, FFCYID: string;
  DPS, i, j: Integer;
  Txt, fImagePath, Txt2, fImagePath2: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin

  ExportFtErpFile('轴卡标签.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\轴卡标签.rmf';

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update PD_Plan_Card set PrtFlag=PrtFlag+1 ');
    sql.Add(' where PCID=' + quotedstr(MPCid));
    ExecSQL;
  end;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select D.*,A.*,B.*,d.filltime as filltime2');
    sql.Add(',P_Code2=(STUFF(( SELECT  '','' +convert(VARCHAR, P_Code) FROM PD_Plan_sub e ');
    sql.Add('inner join  PD_Plan_Card_MS f  on e.Subid=f.Subid and f.PCID=' + quotedstr(MPCid));
    sql.Add('FOR XML PATH('''')), 1, 1, '''') )');
    sql.Add(',KHEWM=cast(dbo.getNum(p_code) as varchar)+''-''+cast(CAST(C_PiE AS INT) as varchar)+''-''+Datename(month,d.filltime) +RIGHT(''0'' + CAST(DATEPART(DAY,d.filltime) AS VARCHAR(2)), 2)+''-''+APMachineNo');
    sql.Add(' from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId');
    sql.Add(' inner join  PD_Plan_Card_MS C  on C.Subid=B.Subid  ');
    sql.Add(' inner join  PD_Plan_Card D  on C.PCID=D.PCID  ');
    sql.Add(' where D.PCID=' + quotedstr(MPCid));
    //ShowMessage(SQL.Text);
    Open;
  end;
  //+CONVERT(varchar(5),D.FILLTIME,101)
  SCreateCDS20(ADOQueryPrint, CDS_PM);
  SInitCDSData20(ADOQueryPrint, CDS_PM);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Y.* from PD_Plan_Yarn Y INNER JOIN PD_Plan_Card_MS M ON M.MainId=Y.MainId where Y.PYType=''经丝'' and M.mainid=' + Quotedstr(Trim(CDS_PLAN.fieldbyname('mainid').AsString)));
    sql.Add(' and M.PCID=' + quotedstr(MPCid));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PJS);
  SInitCDSData20(ADOQueryTemp, CDS_PJS);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Y.* from PD_Plan_Yarn Y INNER JOIN PD_Plan_Card_MS M ON M.MainId=Y.MainId where Y.PYType=''纬丝'' and M.mainid=' + Quotedstr(Trim(CDS_PLAN.fieldbyname('mainid').AsString)));
    sql.Add(' and M.PCID=' + quotedstr(MPCid));
    //ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PWS);
  SInitCDSData20(ADOQueryTemp, CDS_PWS);

  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(MPCid);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\' + MPCid + '.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;

  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt2 := Trim(CDS_PM.FIELDBYNAME('KHEWM').AsString);
    fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\' + Txt2 + '.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath2) then
      DeleteFile(fImagePath2);
    Makebar(pchar(Txt2), Length(Txt2), 3, 3, 0, PChar(fImagePath2), 3);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;

  if FileExists(fPrintFile) then
  begin
    RMVariables['QRBARCODE'] := fImagePath;
    RMVariables['khewm'] := fImagePath2;
    //RM1.ShowPrintDialog := False;
    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := 1;
    RM1.printreport;

  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
    Exit;
  end;
  if FileExists(fImagePath) then
    DeleteFile(fImagePath);
  if FileExists(fImagePath2) then
    DeleteFile(fImagePath2);
end;

function TfrmPlanCardList.SaveData(): Boolean;
var
  maxno, maxsubno, maxpbno, maxpzno, maxsjno: string;
begin
  try

    ADOQueryCmd.Connection.BeginTrans;
///////////////////////////////////保存轴卡//////////////////////////////
    if GetLSNo(ADOQueryCmd, maxno, 'PC', 'PD_Plan_Card', 4, 1) = False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('生成流水号异常！', '提示', 0);
      exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from PD_Plan_Card where 1=2 ');
      Open;
    end;

    with ADOQueryCmd do
    begin
      Append;
      FieldByName('PCid').Value := Trim(maxno);
      FieldByName('C_TouWen').Value := Trim(edtTouWen.Text);
      FieldByName('ZJPiLen').Value := Trim(ZJPiLen.Text);
      FieldByName('C_QJingG').Value := Trim(edtQJingG.Text);
      FieldByName('C_PiE').Value := Trim(edtPiE.Text);
      FieldByName('C_Len').Value := Trim(edtLen.Text);
      FieldByName('ZJLen').Value := Trim(edtLen.Text);
      FieldByName('C_ShangPai').Value := Trim(edtShangP.Text);
      FieldByName('APMachineNo').Value := Trim(APMachineNo.Text);
      FieldByName('edtKaNo').Value := Trim(edtKaNo.Text);
      FieldByName('edZDFNo').Value := Trim(edZDFNo.Text);
      FieldByName('jcfactoryname').Value := Trim(jcfactoryname.Text);
      FieldByName('Filler').Value := Trim(DName);

      Post;
    end;


///////////////////////////////////保存轴卡//////////////////////////////
///////////////////////////////////保存计划单//////////////////////////////
    ///////////////////////////////////经丝组合//////////////////////////////
    {
    with CDS_YarnJX do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_YarnJX.fieldbyname('PYId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'PY', 'PD_Plan_Yarn_CAR', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('经丝组合流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_YarnJX.fieldbyname('PYId').AsString);
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from PD_Plan_Yarn_Car where ');
          sql.Add('  PYId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_YarnJX.fieldbyname('PYId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('PYType').Value := '经丝';
          FieldByName('MainId').Value := Trim(CDS_Plan.FieldByName('MainId').Value);
          FieldByName('SubId').Value := Trim(CDS_Plan.FieldByName('subid').Value);

          FieldByName('PCID').Value := Trim(maxno);
          FieldByName('PYID').Value := Trim(maxpbno);
          FieldByName('Y_Note_PL').Value := Trim(Y_Note_PLJ.Text);
          FieldByName('Y_Note_PLQJ').Value := Trim(Y_Note_PLQJ.Text);
          RTSetSaveDataCDS(ADOQueryCmd, Tv3, CDS_YarnJX, 'PD_Plan_Yarn_CAR', 0);
          Post;
        end;
        CDS_YarnJX.Edit;
        CDS_YarnJX.FieldByName('SubId').Value := Trim(CDS_Plan.FieldByName('subid').Value);
        CDS_YarnJX.FieldByName('MainId').Value := Trim(CDS_Plan.FieldByName('MainId').Value);
        CDS_YarnJX.FieldByName('PYId').Value := Trim(maxpbno);
        Next;
      end;
    end;
///////////////////////////////////经丝组合//////////////////////////////
///////////////////////////////////纬丝组合//////////////////////////////
    with CDS_YarnWX do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_YarnWX.fieldbyname('PYId').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxpbno, 'PY', 'PD_Plan_Yarn_CAR', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('纬丝组合流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxpbno := Trim(CDS_YarnWX.fieldbyname('PYId').AsString);
        end;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from PD_Plan_Yarn_CAR where ');
          sql.Add('  PYId=''' + Trim(maxpbno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_YarnWX.fieldbyname('PYId').AsString) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;

          FieldByName('PCID').Value := Trim(maxno);
          FieldByName('PYType').Value := '纬丝';
          FieldByName('MainId').Value := Trim(CDS_Plan.FieldByName('MainId').Value);
          FieldByName('SubId').Value := Trim(CDS_Plan.FieldByName('subid').Value);
          FieldByName('PYID').Value := Trim(maxpbno);
          FieldByName('Y_Note_PL').Value := Trim(Y_Note_PLW.Text);
          RTSetSaveDataCDS(ADOQueryCmd, Tv4, CDS_YarnWX, 'PD_Plan_Yarn_CAR', 0);
          Post;
        end;
        CDS_YarnwX.Edit;
        CDS_YarnwX.FieldByName('SubId').Value := Trim(CDS_Plan.FieldByName('subid').Value);
        CDS_YarnwX.FieldByName('MainId').Value := Trim(CDS_Plan.FieldByName('mainid').Value);
        CDS_YarnwX.FieldByName('PYId').Value := Trim(maxpbno);
        Next;
      end;
    end;
///////////////////////////////////纬丝组合//////////////////////////////
}
///////////////////////////////////保存计划单//////////////////////////////
    ///////////////////////////////////更新是否开轴//////////////////////////////
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('UPDATE Contract_Sub SET SFPaiZ= ''已排轴'' ');
      SQL.Add(' FROM Contract_Sub A inner join Contract_Main B on B.ConMainId=A.ConMainId  ');
      SQL.Add(' WHERE A.P_Code=' + QuotedStr(Trim(CDS_Plan.FieldByName('P_Code').AsString)));
      SQL.Add(' and B.ConNo=' + QuotedStr(Trim(CDS_Plan.FieldByName('ConNo').AsString)));
        //ShowMessage(SQL.Text);
      ExecSQL;
    end;
    if jcfactoryname.Text <> '' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('UPDATE PD_Plan_Main SET jcfactoryname=' + QuotedStr(jcfactoryname.Text));

        SQL.Add(' WHERE mainid=' + QuotedStr(Trim(CDS_Plan.FieldByName('mainid').AsString)));

        //ShowMessage(SQL.Text);
        ExecSQL;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from PD_Plan_Card_MS where 1=2');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('MainId').Value := CDS_Plan.FieldByName('MainId').Value;
      FieldByName('SubId').Value := CDS_Plan.FieldByName('SubId').Value;
      FieldByName('PCId').Value := Trim(maxno);
      Post;

    end;
//    with CDS_Plan do
//    begin
//      while Locate('SSel', True, []) do
//      begin
//        with ADOQueryCmd do
//        begin
//          Close;
//          SQL.Clear;
//          sql.Add('select * from PD_Plan_Card_MS where 1=2');
//          Open;
//        end;
//        with ADOQueryCmd do
//        begin
//          Append;
//          FieldByName('MainId').Value := CDS_Plan.FieldByName('MainId').Value;
//          FieldByName('SubId').Value := CDS_Plan.FieldByName('SubId').Value;
//          FieldByName('PCId').Value := Trim(maxno);
//          Post;
//
//        end;
//        CDS_Plan.Edit;
//        CDS_Plan.FieldByName('ssel').AsBoolean := false;
//      end;
//    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('exec P_Do_Insert_CAR_JZ @PCid=' + QuotedStr(Trim(maxno)));
      ExecSQL;
    end;
    /////////////////////////////////更新是否开轴//////////////////////////////
    ADOQueryCmd.Connection.CommitTrans;
    PrtDate(maxno);
    Result := True;
  except
    Result := False;

    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

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

procedure TfrmPlanCardList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,APPS=(select SUM(c_pie) from PD_Plan_Card x INNER JOIN  PD_Plan_Card_MS y ON x.PCID=y.PCID and y.mainid=a.mainid)  ');
      sql.Add(',SCZT=(case when (select count(*) from PD_Plan_Yarn_CAR E WHERE  E.SUBID=B.SUBID  )>0 THEN ''1'' ELSE ''0'' END )');
      sql.Add(' from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' where ISNULL(A.ConNoStatu, '''') <>''已取消'' and  not exists( select B.SubId from PD_Plan_Card X inner join PD_Plan_Card_MS Y on X.PCId=Y.PCId where Y.SubId=B.SubId )');
//            SQL.Add('AND isnull((select SUM(c_pie) from PD_Plan_Card x INNER JOIN  PD_Plan_Card_MS y ON x.PCID=y.PCID and y.mainid=a.mainid),0) =0 ');
          end;
        1:
          begin
            sql.Add(' where ISNULL(A.ConNoStatu, '''') <>''已取消'' and A.FillTIme>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
            sql.Add(' and A.FillTIme<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''');
            sql.Add(' and exists(select B.SubId from PD_Plan_Card X inner join  PD_Plan_Card_MS Y on X.PCId=Y.PCId where Y.SubId=B.SubId )');
            SQL.Add('AND isnull((select SUM(c_pie) from PD_Plan_Card x INNER JOIN  PD_Plan_Card_MS y ON x.PCID=y.PCID and y.mainid=a.mainid),0) <PiQty ');
            SQL.Add('AND ISNULL(ordtype,'''')<>''外加工''');
          end;

        2:
          begin
            sql.Add(' where ISNULL(A.ConNoStatu, '''') <>''已取消'' and A.FillTIme>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
            sql.Add(' and A.FillTIme<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''');
            sql.Add(' and exists(select B.SubId from PD_Plan_Card X inner join  PD_Plan_Card_MS Y on X.PCId=Y.PCId where Y.SubId=B.SubId )');
            SQL.Add('AND isnull((select SUM(c_pie) from PD_Plan_Card x INNER JOIN  PD_Plan_Card_MS y ON x.PCID=y.PCID and y.mainid=a.mainid),0) <PiQty ');
            SQL.Add('AND ISNULL(ordtype,'''')=''外加工''');
          end;

        3:
          begin
            sql.Add(' where ISNULL(A.ConNoStatu, '''') <>''已取消'' and A.FillTIme>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
            sql.Add(' and A.FillTIme<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''');
            sql.Add(' and exists(select B.SubId from PD_Plan_Card X inner join  PD_Plan_Card_MS Y on X.PCId=Y.PCId where Y.SubId=B.SubId )');
            SQL.Add('AND isnull((select SUM(c_pie) from PD_Plan_Card x INNER JOIN  PD_Plan_Card_MS y ON x.PCID=y.PCID and y.mainid=a.mainid),0) >=PiQty ');

          end;
      end;
      sql.Add('and a.valid=''Y''');
//      ShowMessage(SQL.Text);
      Open;
    end;

    SCreateCDS20(ADOQueryMain, CDS_Plan);
    SInitCDSData20(ADOQueryMain, CDS_Plan);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmPlanCardList.InitForm();
var
  f1, f2: string;
begin

  ReadCxGrid(Trim(self.Caption) + 'TV1', Tv1, '坯布生产管理1');
  ReadCxGrid(Trim(self.Caption), Tv2, '坯布轴卡');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 90;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  InitGrid();

end;

procedure TfrmPlanCardList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmPlanCardList.Button1Click(Sender: TObject);
var
  i: Integer;
  FReal: double;
begin
  cxGrid1.Enabled := False;
//  if Trim(edZDFNo.Text) = '' then
//  begin
//    Application.MessageBox('经线批号不能为空！', '提示', 0);
//    Exit;
//  end;
  if TryStrToFloat(edtLen.Text, FReal) = False then
    raise Exception.Create('轴长非法数字!');
  if TryStrToFloat(edtPiE.Text, FReal) = False then
    raise Exception.Create('匹额非法数字!');
  if TryStrToFloat(ZJPiLen.Text, FReal) = False then
    raise Exception.Create('匹长非法数字!');
  if CDS_Plan.Locate('SSel', True, []) = False then
  begin
    cxGrid1.Enabled := True;
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,APPS=(select SUM(c_pie) from PD_Plan_Card x INNER JOIN  PD_Plan_Card_MS y ON x.PCID=y.PCID and y.mainid=a.mainid)  ');
    sql.Add(',SCZT=(case when (select count(*) from PD_Plan_Yarn_CAR E WHERE  E.SUBID=B.SUBID  )>0 THEN ''1'' ELSE ''0'' END )');
    sql.Add(' from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId');

    sql.Add('and a.valid=''Y''');
    sql.Add('and a.mainid=' + QuotedStr(CDS_Plan.FieldByName('mainid').AsString));
//      ShowMessage(SQL.Text);
    Open;
  end;
  if (ADOQueryTemp.FieldByName('apps').AsFloat + StrToFloatDef(edtPiE.Text, 0)) > ADOQueryTemp.FieldByName('PiQty').AsFloat then
  begin
    cxGrid1.Enabled := True;
    Application.MessageBox('安排匹数超过订单匹数!', '提示', 0);
    Exit;
  end;

//  for i := 1 to StrToIntDef(edtFS.Text, 0) do
//  begin
  if SaveData() then
  begin

  end;
//  end;
  InitCard(CDS_Plan.FieldByName('Subid').AsString);
  cxGrid1.Enabled := True;
end;

procedure TfrmPlanCardList.Button3Click(Sender: TObject);
begin
  while CDS_Card.Locate('SSel', True, []) do
  begin
    PrtDate(Trim(CDS_Card.FieldByName('PCID').AsString));
    CDS_Card.Edit;
    CDS_Card.FieldByName('SSel').Value := False;
  end;
end;

procedure TfrmPlanCardList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_Plan);
  SInitCDSData20(ADOQueryMain, CDS_Plan);
end;

procedure TfrmPlanCardList.N1Click(Sender: TObject);
begin
  if CDS_Card.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  while CDS_Card.Locate('SSel', True, []) do
  begin
    DelCard(Trim(CDS_Card.FieldByName('PCID').AsString));

    CDS_Card.Delete;
  end;
  InitCard(CDS_Plan.FieldByName('Subid').AsString);
end;

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

procedure TfrmPlanCardList.Button4Click(Sender: TObject);
begin
  try
    frmZdyMapSel := TfrmZdyMapSel.Create(self);
    with frmZdyMapSel do
    begin
      FInitSql := 'select MachNo Note,MachNo ZDYName  from BS_Machine where MachType=''扦经机'' order by MachNo ';
      if ShowModal = 1 then
      begin
        APMachineNo.Text := frmZdyMapSel.FZdyName;

      end;
    end;
  finally
    frmZdyMapSel.Free;
  end;
end;

procedure TfrmPlanCardList.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(Trim(self.Caption) + 'TV1', Tv1, '坯布生产管理1');
  WriteCxGrid(Trim(self.Caption), Tv2, '坯布轴卡');
  Close;
end;

procedure TfrmPlanCardList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Card, True);
end;

procedure TfrmPlanCardList.N3Click(Sender: TObject);
begin
  SelOKNo(CDS_Card, False);
end;

procedure TfrmPlanCardList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
//  InitCard(CDS_Plan.FieldByName('Subid').AsString);
//
//  edtTouWen.Text := Trim(CDS_Plan.FieldByName('JX_Wai').AsString);
//  ZJPiLen.Text := Trim(CDS_Plan.FieldByName('ZJPiLen').AsString);
//  with ADOQuery1 do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select top 1 A.*,B.*  ');
//    sql.Add(' from PD_Plan_Card A inner join PD_Plan_Card_MS B on A.PCID=B.PCID');
//    sql.Add(' where B.Subid=' + quotedstr(Trim(CDS_Plan.FieldByName('Subid').AsString)));
//    sql.Add(' order by A.filltime desc  ');
//    Open;
//  end;
//  edZDFNo.text := Trim(ADOQuery1.FieldByName('edZDFNo').AsString);
//  APMachineNo.text := Trim(ADOQuery1.FieldByName('APMachineNo').AsString);
//  edtKaNo.text := Trim(ADOQuery1.FieldByName('edtKaNo').AsString);
//  edtQJingG.text := Trim(ADOQuery1.FieldByName('C_QJingG').AsString);
//  edtPiE.text := Trim(ADOQuery1.FieldByName('C_PiE').AsString);
//  edtLen.text := Trim(ADOQuery1.FieldByName('C_Len').AsString);
//  edtShangP.text := Trim(ADOQuery1.FieldByName('C_ShangPai').AsString);
//  edtTiaoshu.text := Trim(ADOQuery1.FieldByName('TiaoShu').AsString);
//
//  edtgenshu.text := Trim(ADOQuery1.FieldByName('genshu').AsString);
//
//  edtMenFu.text := Trim(ADOQuery1.FieldByName('MenFu').AsString);
//  edtTiaoKuan.text := Trim(ADOQuery1.FieldByName('TiaoKuan').AsString);
//  edtYiSiLiang.text := Trim(ADOQuery1.FieldByName('YiSiLiang').AsString);
//  edtGangKou.text := Trim(ADOQuery1.FieldByName('GangKou').AsString);
//  cbbSYYL.text := Trim(ADOQuery1.FieldByName('SYYL').AsString);
end;

procedure TfrmPlanCardList.P_CodeChange(Sender: TObject);
begin
  if ADOQuerymain.Active = False then
    Exit;
  SDofilter(ADOQuerymain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQuerymain, CDS_Plan);
  SInitCDSData20(ADOQuerymain, CDS_Plan);
  CDS_PLAN.First;
  Tv1Column8PropertiesChange(Sender);
end;

procedure TfrmPlanCardList.cxGridDBColumn11PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Card do
    begin
      Edit;
      FieldByName(FFieldName).Value := Trim(mvalue);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate PD_Plan_Card ');
      sql.Add(' Set ' + FFieldName + '=''' + Trim(mvalue) + '''');
      sql.Add(' where PCid=' + quotedstr(CDS_Card.fieldbyname('PCid').AsString));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    tv2.Controller.EditingController.ShowEdit();

  except
    tv2.Controller.EditingController.ShowEdit();
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end
end;

procedure TfrmPlanCardList.Button2Click(Sender: TObject);
var
  i: Integer;
begin
  if CDS_Card.IsEmpty then
    Exit;

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

  i := 0;
  with CDS_Card do
  begin
    while CDS_Card.Locate('SSel', True, []) = True do
    begin
      i := i + 1;
      Edit;
      CDS_Card.FieldByName('SSel').Value := False;
      Post;
      if i > 1 then
      begin
        Application.MessageBox('不能选择多个数据！', '提示', 0);

        APMachineNo.Text := '';
        edtKaNo.Text := '';
        edtTouWen.Text := '';

        edtPiE.Text := '';
        edtLen.Text := '';
        edtShangP.Text := '';
        FFPCId := '';
        Exit;
        Break;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from CK_WarpBeam_CR where crflag=''出库'' and VALID=''Y'' AND  pcid=' + QuotedStr(CDS_Card.fieldbyname('PCid').AsString));
        open;
      end;
      if ADOQueryTemp.IsEmpty = FALSE then
      begin
        Application.MessageBox('经轴已出库禁止修改！', '提示', 0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.* ');
        sql.Add(' from PD_Plan_Card A ');
        sql.Add(' where A.PCId=' + quotedstr(Trim(CDS_Card.fieldbyname('PCid').AsString)));
        Open;
      end;
      if CDS_Card.IsEmpty = False then
      begin

        APMachineNo.Text := Trim(ADOQueryCmd.fieldbyname('APMachineNo').AsString);
        edtKaNo.Text := Trim(ADOQueryCmd.fieldbyname('edtKaNo').AsString);
        edtTouWen.Text := Trim(ADOQueryCmd.fieldbyname('C_TouWen').AsString);

        edtPiE.Text := Trim(ADOQueryCmd.fieldbyname('C_PiE').AsString);
        edtLen.Text := Trim(ADOQueryCmd.fieldbyname('C_Len').AsString);
        edtShangP.Text := Trim(ADOQueryCmd.fieldbyname('C_ShangPai').AsString);
        FFPCId := Trim(CDS_Card.fieldbyname('PCid').AsString);

        ZJPiLen.Text := Trim(ADOQueryCmd.fieldbyname('ZJPiLen').AsString);
        edtTouWen.Text := Trim(ADOQueryCmd.fieldbyname('C_TouWen').AsString);
        jcfactoryname.Text := Trim(ADOQueryCmd.fieldbyname('jcfactoryname').AsString);
        edZDFNo.Text := trim(ADOQueryCmd.fieldbyname('edZDFNo').AsString);
      end;
      Next;
    end;

  end;
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from PD_Plan_Yarn_CAR where PYType=''经丝'' and  SubID=' + quotedstr(Trim(CDS_Card.fieldbyname('SubID').AsString)));
////    ShowMessage(sql.text);
//    Open;
//  end;
//  SCreateCDS20(ADOQueryTemp, CDS_YarnJX);
//  SInitCDSData20(ADOQueryTemp, CDS_YarnJX);
//
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from PD_Plan_Yarn_CAR where PYType=''纬丝'' and  SubID=' + quotedstr(Trim(CDS_Card.fieldbyname('SubID').AsString)));
//
//    Open;
//  end;
//  SCreateCDS20(ADOQueryTemp, CDS_YarnWX);
//  SInitCDSData20(ADOQueryTemp, CDS_YarnWX);

  Button1.Enabled := False;
  Button3.Enabled := False;

end;

procedure TfrmPlanCardList.Button5Click(Sender: TObject);
begin
//  if Trim(edZDFNo.Text) = '' then
//  begin
//    Application.MessageBox('经线批号不能为空！', '提示', 0);
//    Exit;
//  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate PD_Plan_Card ');

      sql.Add(' set APMachineNo=' + quotedstr(Trim(APMachineNo.Text)));
      sql.Add(' ,edtKaNo=' + quotedstr(Trim(edtKaNo.Text)));
      sql.Add(' ,C_TouWen=' + quotedstr(Trim(edtTouWen.Text)));

      sql.Add(' ,C_ShangPai=' + quotedstr(Trim(edtShangP.Text)));

      sql.Add(' ,C_PiE=' + quotedstr(Trim(edtPiE.Text)));
      sql.Add(' ,C_Len=' + quotedstr(Trim(edtLen.Text)));

      sql.Add(' ,ZJPiLen=' + quotedstr(Trim(ZJPiLen.Text)));

      sql.Add(' ,Editer=' + quotedstr(Trim(DName)));
      sql.Add(' ,Edittime=GETDATE()');

      sql.Add(' ,edZDFNo=' + quotedstr(Trim(edZDFNo.Text)));

      sql.Add(' where PCid=' + quotedstr(Trim(FFPCId)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('exec P_Do_Insert_CAR_JZ @PCid=' + QuotedStr(Trim(FFPCId)));
      ExecSQL;
    end;
///////////////////////////////////经丝组合//////////////////////////////
///////////////////////////////////纬丝组合//////////////////////////////

  finally
    ADOQueryMain.EnableControls;
    Application.MessageBox('修改成功！', '提示', 0);
    Button1.Enabled := True;
    Button3.Enabled := True;

    APMachineNo.Text := '';
    edtKaNo.Text := '';
    edtTouWen.Text := '';
    edZDFNo.Text := '';
    edtPiE.Text := '';
    edtLen.Text := '';
    edtShangP.Text := '';
    FFPCId := '';
    InitCard(CDS_Plan.FieldByName('Subid').AsString);
  end

end;

procedure TfrmPlanCardList.cbbSYYLPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);

      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmPlanCardList.ToolButton4Click(Sender: TObject);
var
  fPrintFile, fPrintFileQZ, fPrintFileYWB: string;
  i, j, k: integer;
begin
  if CDS_Plan.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\整经工艺单.rmf';
  ExportFtErpFile('整经工艺单.rmf', ADOQueryTemp);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from PD_Plan_Main A inner join PD_Plan_Sub B on A.Mainid=B.MainId where A.MainId=' + Quotedstr(Trim(CDS_Plan.fieldbyname('MainId').AsString)));
    //ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PM);
  SInitCDSData20(ADOQueryTemp, CDS_PM);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from PD_Plan_QZ where MainId=' + Quotedstr(Trim(CDS_Plan.fieldbyname('MainId').AsString)));
    //ShowMessage(SQL.Text);
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    SCreateCDS20(ADOQueryTemp, CDS_QZ);
    SInitCDSData20(ADOQueryTemp, CDS_QZ);

  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from PD_Plan_Yarn_car where PYType=''经丝'' and MainId=' + Quotedstr(Trim(CDS_Plan.fieldbyname('MainId').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PJS);
  SInitCDSData20(ADOQueryTemp, CDS_PJS);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from PD_Plan_Yarn_car where PYType=''纬丝'' and MainId=' + Quotedstr(Trim(CDS_Plan.fieldbyname('MainId').AsString)));
    //ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PWS);
  SInitCDSData20(ADOQueryTemp, CDS_PWS);

  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
    if FileExists(fPrintFileQZ) then
    begin
      RM1.LoadFromFile(fPrintFileQZ);
      RM1.ShowReport;
    end;

    if FileExists(fPrintFileYWB) then
    begin
      RM1.LoadFromFile(fPrintFileYWB);
      RM1.ShowReport;
    end;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmPlanCardList.TV3Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try

    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with CDS_YarnJX do
        begin
          Self.CDS_YarnJX.Edit;
          Self.CDS_YarnJX.FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('Name').AsString);
          Post;
        end;

      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmPlanCardList.cxGridDBColumn3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp2 := TfrmZDYHelp2.Create(Application);
    with frmZDYHelp2 do
    begin
      flag := 'SXYCLName_JS';
      flagname := '经纬丝名称';
      fnote := True;
      V1Note.Caption := '类别';
      if ShowModal = 1 then
      begin
        Self.CDS_YarnJX.Edit;
        Self.CDS_YarnJX.FieldByName('Y_Name').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp2.Free;
  end;
end;

procedure TfrmPlanCardList.TV4Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try

    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with CDS_YarnWX do
        begin
          Self.CDS_YarnWX.Edit;
          Self.CDS_YarnWX.FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('Name').AsString);
          Post;
        end;

      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmPlanCardList.cxGridDBColumn8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp2 := TfrmZDYHelp2.Create(Application);
    with frmZDYHelp2 do
    begin
      flag := 'SXYCLName_JS';
      flagname := '经纬丝名称';
      fnote := True;
      V1Note.Caption := '类别';
      if ShowModal = 1 then
      begin
        Self.CDS_YarnWX.Edit;
        Self.CDS_YarnWX.FieldByName('Y_Name').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp2.Free;
  end;
end;

procedure TfrmPlanCardList.ToolButton5Click(Sender: TObject);
var
  I: Integer;
  Column: TcxGridDBColumn;
  FieldNames: TStringList;
begin
  FieldNames := TStringList.Create;
  try
    for I := 0 to Tv2.ColumnCount - 1 do
    begin
      Column := Tv2.Columns[I];
      FieldNames.Add(Column.Caption + ':' + Column.DataBinding.FieldName);
    end;
    ShowMessage(FieldNames.Text);
  finally
    FieldNames.Free;
  end;

end;

procedure TfrmPlanCardList.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_PLAN.IsEmpty then
    Exit;
  InitCard(CDS_Plan.FieldByName('Subid').AsString);
  P_CODE1.Text := '';
  APMachineNo.Text := '';
  edtKaNo.Text := '';
  edtTouWen.Text := '';

  edtPiE.Text := '';
  edtLen.Text := '';
//  edtFS.Text := '';
  edtShangP.Text := '';
  jcfactoryname.Text := '';
  ZJPiLen.Text := '';

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from PD_Plan_Main A inner join PD_Plan_sub b on a.mainid=b.mainid where a.mainid=' + QuotedStr(CDS_Plan.fieldbyname('mainid').AsString));
//    ShowMessage(SQL.Text);
    open;
  end;
  if ADOQueryTemp.IsEmpty = false then
  begin
    edtTouWen.Text := ADOQueryTemp.fieldByname('JX_Wai').asstring;

    ZJPiLen.Text := ADOQueryTemp.fieldByname('PILEN').asstring;
    P_CODE1.Text := ADOQueryTemp.fieldByname('P_CODE').asstring;
    edtLen.Text := ADOQueryTemp.fieldByname('ZJPiLen').asstring;
    jcfactoryname.Text := ADOQueryTemp.fieldByname('jcfactoryname').asstring;
//    edZDFNo.Text := ADOQueryTemp.fieldByname('C_edZDFNo').asstring;
//    APMachineNo.Text := ADOQueryTemp.fieldByname('C_APMachineNo').asstring;
//    edtKaNo.Text := ADOQueryTemp.fieldByname('C_edtKaNo').asstring;
//    edtTouWen.Text := ADOQueryTemp.fieldByname('C_edtTouWen').asstring;
//    edtQJingG.Text := ADOQueryTemp.fieldByname('C_edtQJingG').asstring;
//    edtPiE.Text := ADOQueryTemp.fieldByname('C_edtPiE').asstring;
//    edtLen.Text := ADOQueryTemp.fieldByname('C_edtLen').asstring;
//    edtFS.Text := ADOQueryTemp.fieldByname('C_edtFS').asstring;
//    edtShangP.Text := ADOQueryTemp.fieldByname('C_edtShangP').asstring;
//    edtTiaoshu.Text := ADOQueryTemp.fieldByname('C_edtTiaoshu').asstring;
//    edtgenshu.Text := ADOQueryTemp.fieldByname('C_edtgenshu').asstring;
//    edtMenFu.Text := ADOQueryTemp.fieldByname('C_edtMenFu').asstring;
//    edtTiaoKuan.Text := ADOQueryTemp.fieldByname('C_edtTiaoKuan').asstring;
//    edtYiSiLiang.Text := ADOQueryTemp.fieldByname('C_edtYiSiLiang').asstring;
//    edtsjwm.Text := ADOQueryTemp.fieldByname('SJ_WM').asstring;
//
//    edtGangKou.Text := ADOQueryTemp.fieldByname('C_edtGangKou').asstring;
//    cbbSYYL.Text := ADOQueryTemp.fieldByname('C_cbbSYYL').asstring;
//    ZJPiLen.Text := ADOQueryTemp.fieldByname('C_ZJPiLen').asstring;
//    SXCS.ItemIndex := SXCS.Items.IndexOf(trim(ADOQueryTemp.fieldbyname('C_SXCS').AsString));
//
//    Note.Text := ADOQueryTemp.fieldByname('GYNote').asstring;
//    ZZCHEJIAN.Text := ADOQueryTemp.fieldByname('CHEJIAN').asstring;
//    MKCR.Text := ADOQueryTemp.fieldByname('C_MKCR').asstring;

  end;

end;

procedure TfrmPlanCardList.edtLenChange(Sender: TObject);
var
  f1: double;
  f2, f3, f5: double;
  f4: Integer;
  s, tmpStr: string;
  I: Integer;
begin

//  if (strToFloatDef(edtLen.Text, 0) <> 0) and (StrToFloatDef(ZJPiLen.Text, 0) <> 0) then
//  begin
//    f2 := StrToFloatDef(edtLen.Text, 0);
//    f3 := StrToFloatDef(ZJPiLen.Text, 0);
//    f1 := f2 / f3;
//
//    f4 := floor(f1);
//    f5 := f2 - f3 * f4;
//    if f5 <> 0 then
//    begin
//      edtPiE.Text := FloatToStr(f4) + '匹+' + FloatToStr(f5) + '米';
//    end
//    else
//    begin
//      edtPiE.Text := FloatToStr(f4) + '匹';
//    end;
//
//  end;
end;

procedure TfrmPlanCardList.edtPiEExit(Sender: TObject);
begin
//  if (edtPiE.Text <> '') and (ZJPiLen.text <> '') then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('exec  P_Do_JSZC @PIE=' + QuotedStr(Trim(edtPiE.Text)));
//      sql.Add(',@ZJPICHANG=' + QuotedStr(Trim(ZJPiLen.Text)));
//      open;
//    end;
//    if ADOQueryTemp.IsEmpty = False then
//    begin
//      edtLen.Text := ADOQueryTemp.fieldbyname('zs').AsString;
//    end;
//  end;

end;

procedure TfrmPlanCardList.Tv1Column8PropertiesChange(Sender: TObject);
begin
  if CDS_PLAN.IsEmpty then
    Exit;
  InitCard(CDS_Plan.FieldByName('Subid').AsString);
  APMachineNo.Text := '';
  edtKaNo.Text := '';
  edtTouWen.Text := '';

  edtPiE.Text := '';
  edtLen.Text := '';
//  edtFS.Text := '';
  edtShangP.Text := '';

  ZJPiLen.Text := '';
  jcfactoryname.Text := '';
  P_CODE1.Text := '';
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from PD_Plan_Main A inner join PD_Plan_sub b on a.mainid=b.mainid where a.mainid=' + QuotedStr(CDS_Plan.fieldbyname('mainid').AsString));
//    ShowMessage(SQL.Text);
    open;
  end;
  if ADOQueryTemp.IsEmpty = false then
  begin
    edtTouWen.Text := ADOQueryTemp.fieldByname('JX_Wai').asstring;
    edtLen.Text := ADOQueryTemp.fieldByname('ZJPiLen').asstring;
    ZJPiLen.Text := ADOQueryTemp.fieldByname('ZJPiLen').asstring;
    P_CODE1.Text := ADOQueryTemp.fieldByname('P_CODE').asstring;
    jcfactoryname.Text := ADOQueryTemp.fieldByname('jcfactoryname').asstring;
//    edZDFNo.Text := ADOQueryTemp.fieldByname('C_edZDFNo').asstring;
//    APMachineNo.Text := ADOQueryTemp.fieldByname('C_APMachineNo').asstring;
//    edtKaNo.Text := ADOQueryTemp.fieldByname('C_edtKaNo').asstring;
//    edtTouWen.Text := ADOQueryTemp.fieldByname('C_edtTouWen').asstring;
//    edtQJingG.Text := ADOQueryTemp.fieldByname('C_edtQJingG').asstring;
//    edtPiE.Text := ADOQueryTemp.fieldByname('C_edtPiE').asstring;
//    edtLen.Text := ADOQueryTemp.fieldByname('C_edtLen').asstring;
//    edtFS.Text := ADOQueryTemp.fieldByname('C_edtFS').asstring;
//    edtShangP.Text := ADOQueryTemp.fieldByname('C_edtShangP').asstring;
//    edtTiaoshu.Text := ADOQueryTemp.fieldByname('C_edtTiaoshu').asstring;
//    edtgenshu.Text := ADOQueryTemp.fieldByname('C_edtgenshu').asstring;
//    edtMenFu.Text := ADOQueryTemp.fieldByname('C_edtMenFu').asstring;
//    edtTiaoKuan.Text := ADOQueryTemp.fieldByname('C_edtTiaoKuan').asstring;
//    edtYiSiLiang.Text := ADOQueryTemp.fieldByname('C_edtYiSiLiang').asstring;
//    edtsjwm.Text := ADOQueryTemp.fieldByname('SJ_WM').asstring;
//
//    edtGangKou.Text := ADOQueryTemp.fieldByname('C_edtGangKou').asstring;
//    cbbSYYL.Text := ADOQueryTemp.fieldByname('C_cbbSYYL').asstring;
//    ZJPiLen.Text := ADOQueryTemp.fieldByname('C_ZJPiLen').asstring;
//    SXCS.ItemIndex := SXCS.Items.IndexOf(trim(ADOQueryTemp.fieldbyname('C_SXCS').AsString));
//
//    Note.Text := ADOQueryTemp.fieldByname('GYNote').asstring;
//    ZZCHEJIAN.Text := ADOQueryTemp.fieldByname('CHEJIAN').asstring;
//    MKCR.Text := ADOQueryTemp.fieldByname('C_MKCR').asstring;

  end;
end;

procedure TfrmPlanCardList.edtQJingGClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'edtQJingG';
      flagname := '扦经工';
      if ShowModal = 1 then
      begin
        edtQJingG.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPlanCardList.P_Code1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    SelectNext(ActiveControl as TWinControl, True, True);
  end;
end;

procedure TfrmPlanCardList.jcfactorynameClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'jcfactoryname';
      flagname := '加工厂';
      if ShowModal = 1 then
      begin
        jcfactoryname.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPlanCardList.N4Click(Sender: TObject);
begin
  try
    frmClothSCDCGAnPaiMX := TfrmClothSCDCGAnPaiMX.Create(Application);
    with frmClothSCDCGAnPaiMX do
    begin
      PCID.Text := CDS_Card.fieldbyname('pcid').AsString;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmClothSCDCGAnPaiMX.Free;
  end;
end;

end.

