unit U_MaDanManageST;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxCalendar, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  cxCheckBox, cxButtonEdit, cxDropDownEdit, BtnEdit, cxGridBandedTableView,
  cxGridDBBandedTableView, cxContainer, cxTextEdit, cxMemo, cxRichEdit,
  DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxPC, RM_Common,
  RM_Class, RM_GridReport, RM_System, RM_Dataset, Menus, cxSplitter;

type
  TfrmMaDanManageST = class(TForm)
    Panel1: TPanel;
    Label3: TLabel;
    OrderNo: TEdit;
    CDS_OrderNo: TClientDataSet;
    DS_OrderNo: TDataSource;
    ADOQuery1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    ClientDataSet5: TClientDataSet;
    DataSource5: TDataSource;
    ClientDataSet3: TClientDataSet;
    DataSource3: TDataSource;
    ClientDataSet6: TClientDataSet;
    DataSource6: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGridPopupMenu5: TcxGridPopupMenu;
    ScrollBox3: TScrollBox;
    ScrollBox4: TScrollBox;
    Panel5: TPanel;
    Label9: TLabel;
    Panel4: TPanel;
    Label12: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label25: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    DLYDate: TDateTimePicker;
    MPRTCodeName: TEdit;
    MPRTSpec: TEdit;
    MPRTCF: TEdit;
    MPRTMF: TEdit;
    MPRTKZ: TEdit;
    MPRTKZNote: TEdit;
    LiDanPerson: TEdit;
    PFGenDanPerson: TEdit;
    MPRTCode: TEdit;
    MPRTGYHZ: TEdit;
    ScrollBox6: TScrollBox;
    Panel3: TPanel;
    Label13: TLabel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel2: TPanel;
    Label21: TLabel;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    v5Column8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    v5Column2: TcxGridDBColumn;
    v5Column7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v5Column3: TcxGridDBColumn;
    v5Column4: TcxGridDBColumn;
    v5Column5: TcxGridDBColumn;
    v5Column1: TcxGridDBColumn;
    v5Column9: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    ScrollBox8: TScrollBox;
    Panel7: TPanel;
    Label23: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid6: TcxGrid;
    TvOrdeNo: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGrid4Level1: TcxGridLevel;
    Button9: TButton;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    Panel9: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel10: TPanel;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    cxPageControl2: TcxPageControl;
    cxTabSheet3: TcxTabSheet;
    cxTabSheet4: TcxTabSheet;
    ScrollBox2: TScrollBox;
    Label4: TLabel;
    Label5: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label14: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label22: TLabel;
    MFChk: TComboBox;
    KZChk: TComboBox;
    SLDChk: TComboBox;
    SGChk: TComboBox;
    TSYQChk: TComboBox;
    ChkResult: TComboBox;
    Button8: TButton;
    Button7: TButton;
    Label24: TLabel;
    CJNote: TcxRichEdit;
    v5Column6: TcxGridDBColumn;
    RMDBMain: TRMDBDataSet;
    RM1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    Button10: TButton;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v3Column9: TcxGridDBColumn;
    v5Column10: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    ChkNote: TMemo;
    ChuanYangQty: TEdit;
    Label26: TLabel;
    ChuanYangUnit: TComboBox;
    Button11: TButton;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    Label27: TLabel;
    LingBuQty: TEdit;
    LingBuUnit: TComboBox;
    Label28: TLabel;
    DaKuaiQty: TEdit;
    DaKuaiUnit: TComboBox;
    ClientDataSet8: TClientDataSet;
    v3Column12: TcxGridDBColumn;
    Button15: TButton;
    ScrollBox1: TScrollBox;
    cxGrid4: TcxGrid;
    Tv6: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    Tv6Column1: TcxGridDBBandedColumn;
    Tv6Column2: TcxGridDBBandedColumn;
    v6Column1: TcxGridDBBandedColumn;
    v6Column2: TcxGridDBBandedColumn;
    v6Column3: TcxGridDBBandedColumn;
    v6Column4: TcxGridDBBandedColumn;
    v6Column5: TcxGridDBBandedColumn;
    v6Column6: TcxGridDBBandedColumn;
    v6Column9: TcxGridDBBandedColumn;
    v6Column10: TcxGridDBBandedColumn;
    v6Column11: TcxGridDBBandedColumn;
    v6Column12: TcxGridDBBandedColumn;
    v6Column14: TcxGridDBBandedColumn;
    v6Column7: TcxGridDBBandedColumn;
    v6Column13: TcxGridDBBandedColumn;
    v6Column15: TcxGridDBBandedColumn;
    v6Column8: TcxGridDBBandedColumn;
    v6Column16: TcxGridDBBandedColumn;
    cxGridLevel3: TcxGridLevel;
    v6Column17: TcxGridDBBandedColumn;
    Label29: TLabel;
    LiuYangQty: TEdit;
    LiuYangUnit: TComboBox;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v3Column13: TcxGridDBColumn;
    v5Column12: TcxGridDBColumn;
    v3Column14: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure TvOrdeNoDblClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure v2Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v5Column8PropertiesEditValueChanged(Sender: TObject);
    procedure Tv3Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure v6Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v6Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v6Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Button8Click(Sender: TObject);
    procedure Tv3FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure v1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Tv6MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv3MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormShow(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Tv3Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v6Column8PropertiesEditValueChanged(Sender: TObject);
    procedure v6Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure v6Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure v6Column16PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v6Column17PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxTabControl1Change(Sender: TObject);
  private
    { Private declarations }
     FMainId,SParameters1:String;

     procedure InitGrid();
     function UpdateTJCKQty(FFPFID:string):Boolean;
  public
    { Public declarations }
  end;

var
  frmMaDanManageST: TfrmMaDanManageST;

implementation
uses
  U_DataLink,U_RTFun, U_GYSList,U_ModuleNote,U_ZDYHelp, U_ZDYHelpSel,U_OrderInPutPrice,
  U_GYSSelList;

{$R *.dfm}

procedure TfrmMaDanManageST.FormDestroy(Sender: TObject);
begin
  frmMaDanManageST:=nil;

end;

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

procedure TfrmMaDanManageST.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('Tv1',Tv1,'实体码单');
  WriteCxGrid('Tv2',Tv2,'实体码单');
  WriteCxGrid('Tv3',Tv3,'实体码单');
  WriteCxGrid('Tv5',Tv5,'实体码单');
  WriteCxBandedGrid('Tv6',Tv6,'实体码单');
  close;
end;

procedure TfrmMaDanManageST.OrderNoChange(Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=Trim(OrderNo.Text);
  if Length(Trim(mvalue))<2 then
  begin
    cxGrid6.Visible:=False;
    Exit;
  end;
  mvalue:='%'+Trim(mvalue)+'%';
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select OrderNo,MPRTCodeName,A.MainId from JYOrder_Main A');
    sql.Add(' where A.orderno like :orderno ');
    //sql.Add(' and A.MainId in(select MainId from JYOrder_Sub B where B.Mainid=A.MainId and B.subId in(select OrdSubId from Contract_Cloth_LL))');
    Parameters.ParamByName('orderno').Value:=mvalue;
    Open;
  end;
  SCreateCDS20(ADOQuery1,CDS_OrderNo);
  SInitCDSData20(ADOQuery1,CDS_OrderNo);
  if CDS_OrderNo.IsEmpty then cxGrid6.Visible:=False else cxGrid6.Visible:=True;
end;
function TfrmMaDanManageST.UpdateTJCKQty(FFPFID:string):Boolean;
var
  FAPQty,FMDQty,FPS,FQty:Double;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from JYOrder_Main_MD_Out where PFID='''+Trim(FFPFID)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Result:=True;
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_PCS_Sub where PFID='''+Trim(FFPFID)+'''');
    Open;
  end;
  FAPQty:=ADOQueryTemp.fieldbyname('PFTPQty').Value;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(Qty),0) MDQty from JYOrder_Main_MD where PFID='''+Trim(FFPFID)+'''');
    sql.Add(' and isnull(CKName,'''')<>''调节仓库'' ');
    Open;
  end;
  FMDQty:=ADOQueryTemp.fieldbyname('MDQty').Value;
  if FMDQty>FAPQty then
  begin
    if Application.MessageBox('出库数量大于调用申请数量,确定要录入码单吗？','提示',32+4)<>IDYES then
    begin
      Result:=False;
      Exit;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(sum(PS),0) PS,isnull(Sum(Qty),0) Qty from JYOrder_Main_MD where PFID='''+Trim(FFPFID)+'''');
    sql.Add(' and isnull(CKName,'''')<>''调节仓库''');
    Open;
  end;
  FPS:=ADOQueryTemp.fieldbyname('PS').Value;
  FQty:=ADOQueryTemp.fieldbyname('Qty').Value;
  if FQty=0 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Main_MD_Out Set SJCKPS=0,SJCKQty=0');
      sql.Add(' where PFID='''+Trim(FFPFID)+'''');
      sql.Add('Update JYOrder_Main_MD Set CKPS=');
      sql.Add('(select isnull(sum(SJCKPS),0) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID) ');
      sql.Add(',CKQty=(select isnull(sum(SJCKQty),0) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID) ');
      sql.Add(' where MDID in');
      sql.Add('(select MDID from JYOrder_Main_MD_Out JMO where JMO.PFID='''+Trim(FFPFID)+''')');
      ExecSQL;
    end;
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD_Out Set SJCKPS=0,SJCKQty=0');
    sql.Add(' where PFID='''+Trim(FFPFID)+'''');
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_Out where PFID='''+Trim(FFPFID)+'''');
    sql.Add(' order by CKQty');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet8);
  SInitCDSData20(ADOQueryTemp,ClientDataSet8);

  with ClientDataSet8 do
  begin
    First;
    while not Eof do
    begin
      if ClientDataSet8.FieldByName('CKQty').Value<=FQty then
      begin
        if ClientDataSet8.RecordCount>1 then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' Update JYOrder_Main_MD_Out Set SJCKPS=CKPS,SJCKQty=CKQty ');
            sql.Add(' where CKID='''+Trim(ClientDataSet8.fieldbyname('CKID').AsString)+'''');
            ExecSQL;
          end;
          FPS:=FPS-ClientDataSet8.FieldByName('CKPS').Value;
          FQty:=FQty-ClientDataSet8.FieldByName('CKQty').Value;
          ClientDataSet8.Delete;
        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' Update JYOrder_Main_MD_Out Set SJCKPS='+floattostr(FPS)+',SJCKQty='+floattostr(FQty));
            sql.Add(' where CKID='''+Trim(ClientDataSet8.fieldbyname('CKID').AsString)+'''');
            ExecSQL;
          end;
          FPS:=0;
          FQty:=0;
          ClientDataSet8.Delete;
        end;

      end else
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' Update JYOrder_Main_MD_Out Set SJCKPS='+floattostr(FPS) );
          sql.Add(' ,SJCKQty='+floattostr(FQty) );
          sql.Add(' where CKID='''+Trim(ClientDataSet8.fieldbyname('CKID').AsString)+'''');
          ExecSQL;
        end;
        ClientDataSet8.Last;
      end;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Main_MD Set CKPS=');
    sql.Add('(select isnull(sum(SJCKPS),0) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID) ');
    sql.Add(',CKQty=(select isnull(sum(SJCKQty),0) from JYOrder_Main_MD_Out A where A.MDID=JYOrder_Main_MD.MDID) ');
    sql.Add(' where MDID in');
    sql.Add('(select MDID from JYOrder_Main_MD_Out JMO where JMO.PFID='''+Trim(FFPFID)+''')');
    ExecSQL;
  end;
  Result:=True;
end;
procedure TfrmMaDanManageST.TvOrdeNoDblClick(Sender: TObject);
var
  fsj:string;
begin
  OrderNo.Text:=CDS_OrderNo.fieldbyname('OrderNo').AsString;
  cxGrid6.Visible:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(',PFGenDanPerson=dbo.F_Get_Order_SubStr(A.MainId,''PFGenDanPerson'')');
    sql.Add(' from JYOrder_Main A');
    sql.Add(' where Orderno='''+Trim(OrderNo.Text)+'''');
    Open;
  end;
  if ADOQueryTemp.FieldByName('JSFlag').AsBoolean=True then
  begin
    Panel10.Visible:=False;
    Panel9.Visible:=False;

    Button11.Visible:=False;
    Button15.Visible:=False;
  end else
  begin
    Panel10.Visible:=True;
    Panel9.Visible:=True;

    Button11.Visible:=True;
    Button15.Visible:=True;
  end;
  CJNote.Text:=Trim(ADOQueryTemp.fieldbyname('MPRTMFNote').AsString)+#13+
               Trim(ADOQueryTemp.fieldbyname('MPRTKZNote').AsString)+#13+
               Trim(ADOQueryTemp.fieldbyname('CJNote').AsString);
  SCSHData(ADOQueryTemp,Panel4,2);
  MPRTCode.Text:=Trim(ADOQueryTemp.fieldbyname('MPRTCode').AsString);
  FMainId:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  ChuanYangQty.Text:=Trim(ADOQueryTemp.fieldbyname('ChuanYangQty').AsString);
  ChuanYangUnit.Text:=Trim(ADOQueryTemp.fieldbyname('ChuanYangUnit').AsString);
  LiuYangQty.Text:=Trim(ADOQueryTemp.fieldbyname('LiuYangQty').AsString);
  LiuYangUnit.Text:=Trim(ADOQueryTemp.fieldbyname('LiuYangUnit').AsString);
  LingBuQty.Text:=Trim(ADOQueryTemp.fieldbyname('LingBuQty').AsString);
  LingBuUnit.Text:=Trim(ADOQueryTemp.fieldbyname('LingBuUnit').AsString);
  DaKuaiQty.Text:=Trim(ADOQueryTemp.fieldbyname('DaKuaiQty').AsString);
  DaKuaiUnit.Text:=Trim(ADOQueryTemp.fieldbyname('DaKuaiUnit').AsString);
  fsj:='select Code=FactoryNo,Name=FactoryName from JYOrder_PCS_Sub where MainId='''+Trim(FMainId)+''''+
       ' and isnull(FactoryNo,'''')<>'''' group by FactoryNo,FactoryName';
  SInitCxGridComboBoxBySql(ADOQuery1,Tv3Column1,fsj,1,True,'');
  //SInitCxBandGridComboBoxBySql(ADOQuery1,v6Column8,fsj,1,True,'');
  SInitCxBandGridComboBoxBySql(ADOQuery1,v6Column2,fsj,1,True,'');
  fsj:='select Code=PFGenDanPerson,Name=PFGenDanPerson from JYOrder_PCS_Sub where MainId='''+Trim(FMainId)+''''+
       ' and isnull(PFGenDanPerson,'''')<>'''' group by PFGenDanPerson';
  SInitCxBandGridComboBoxBySql(ADOQuery1,v6Column1,fsj,1,True,'');
  fsj:='';
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Process where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  with ADOQueryTemp do
  begin
    First;
    while not Eof do
    begin
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select AA=dbo.F_Get_Order_SubStr(:PSID,''PFGenDanPerson'')');
        Parameters.ParamByName('PSID').Value:=Trim(ADOQueryTemp.fieldbyname('PSID').AsString);
        open;
      end;
      if Trim(fsj)='' then
      begin
        fsj:=Trim(ADOQueryTemp.fieldbyname('PSName').AsString)+':'+Trim(ADOQuery1.fieldbyname('AA').AsString);
      end else
        fsj:=fsj+'  '+Trim(ADOQueryTemp.fieldbyname('PSName').AsString)+':'+Trim(ADOQuery1.fieldbyname('AA').AsString);
      Next;
    end;
  end;
  PFGenDanPerson.Text:=Trim(fsj);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PRTHX,B.PRTColorNo,PRTColor=isnull(B.PRTColor,'''')+'' ''+isnull(B.PRTColorNo,''''),B.PRTOrdQty,B.OrderUnit,B.ColorNote,JP.PSName,B.FZGYNote  ');
    sql.Add(',Case when isnull(A.CKName,'''')='''' then ''正常'' else ''调用'' end as APType ');
    sql.Add(' ,orderNo=(select OrderNo from JYOrder_Main JM where JM.MainId=(select ORDMainid from JYOrder_Main_MD MD where MD.MDID=A.DiaoYongMDID))');
    //sql.Add(',Case when A.PFTPMQty<>0 then Cast((A.WCMQty-A.TJMQty)*100*1.00/A.PFTPMQty as decimal(18,2)) else 0 end as WCL');
    sql.Add(' from JYOrder_PCS_Sub A');
    sql.Add(' inner join JYOrder_Process JP on A.PSID=JP.PSID');
    sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
    sql.Add(' where A.MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,  ');
    sql.Add(' Case when A.MainId not like ''CM%'' then Cast(1 as bit ) else Cast(0 as bit ) end as DYFlag  ');
    sql.Add(' from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId');
    sql.Add(' where A.ConNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet5);
  SInitCDSData20(ADOQueryTemp,ClientDataSet5);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Process where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  InitGrid();
end;
procedure TfrmMaDanManageST.InitGrid();
var
  FPSID,FOrdSubId,FConSubId,FPFID:String;
begin
  if ClientDataSet2.Locate('SSel',True,[])=True then
  begin
    FPSID:=Trim(ClientDataSet2.fieldbyname('PSID').AsString);
  end else
  begin
    FPSID:='';
  end;
  if ClientDataSet5.Locate('SSel',True,[])=True then
  begin
    FConSubId:=Trim(ClientDataSet5.fieldbyname('SubId').AsString);
  end else
  begin
    FConSubId:='';
  end;
  if ClientDataSet1.Locate('SSel',True,[])=True then
  begin
    FOrdSubId:=Trim(ClientDataSet1.fieldbyname('SubId').AsString);
    FPFID:=Trim(ClientDataSet1.fieldbyname('PFID').AsString);
  end else
  begin
    FOrdSubId:='';
    FPFID:='';
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PSName OrdPSName,C.ConNo,C.FactoryNoName,D.Price,JS.PRTHX,JS.ColorNote ');
    sql.Add(',PRTColor=isnull(JS.PRTColor,'''')+'' ''+isnull(JS.PRTColorNo,'''')');
    sql.Add(' from JYOrder_Main_MD  A');
    sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
    sql.Add(' inner join JYOrder_PCS_Sub JPS on A.PFID=JPS.PFID ');
    sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
    sql.Add(' left join Contract_Main C on A.ConMainid=C.MainID');
    sql.Add(' left join Contract_Sub D on A.ConSubid=D.SubID');
    sql.Add(' where A.ORDMainId='''+Trim(FMainId)+'''');
    if Trim(FPSID)<>'' then
    begin
      sql.Add(' and A.PSID='''+Trim(FPSID)+'''');
    end;
    if Trim(FConSubId)<>'' then
    begin
      sql.Add(' and A.ConSubID='''+Trim(FConSubId)+'''');
    end;
    if Trim(FOrdSubId)<>'' then
    begin
      sql.Add(' and A.OrdSubID='''+Trim(FOrdSubId)+'''');
    end;
    if Trim(FPFID)<>'' then
    begin
      sql.Add(' and A.PFID='''+Trim(FPFID)+'''');
    end;
    {if Trim(SParameters1)<>'高权限' then
    begin
      SQL.Add(' and A.Filler='''+Trim(DName)+'''');
    end; }
    sql.Add(' and A.MDType=''实体'' ');
    SQL.Add(' and isnull(A.CKName,'''')<>''调节仓库'' ');
    Open;
  end;
  
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PSName OrdPSName,C.ConNo,C.FactoryNoName,D.Price,JS.PRTHX,JS.ColorNote ');
    sql.Add(',PRTColor=isnull(JS.PRTColor,'''')+'' ''+isnull(JS.PRTColorNo,'''')');
    sql.Add(' from JYOrder_Main_MD  A');
    sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
    sql.Add(' inner join JYOrder_PCS_Sub JPS on A.PFID=JPS.PFID ');
    sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
    sql.Add(' left join Contract_Main C on A.ConMainid=C.MainID');
    sql.Add(' left join Contract_Sub D on A.ConSubid=D.SubID');
    sql.Add(' where A.ORDMainId='''+Trim(FMainId)+'''');
    if Trim(FPSID)<>'' then
    begin
      sql.Add(' and A.PSID='''+Trim(FPSID)+'''');
    end;
    if Trim(FConSubId)<>'' then
    begin
      sql.Add(' and A.ConSubID='''+Trim(FConSubId)+'''');
    end;
    if Trim(FOrdSubId)<>'' then
    begin
      sql.Add(' and A.OrdSubID='''+Trim(FOrdSubId)+'''');
    end;
    if Trim(FPFID)<>'' then
    begin
      sql.Add(' and A.PFID='''+Trim(FPFID)+'''');
    end;
    {if Trim(SParameters1)<>'高权限' then
    begin
      SQL.Add(' and A.Filler='''+Trim(DName)+'''');
    end;  }
    sql.Add(' and A.MDType=''实体'' ');
    SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');
    {if cxTabControl1.TabIndex=0 then
    begin
      sql.Add(' and isnull(ClotheType,'''')<>''次品'' ');
      v6Column7.Options.Focusing:=True;
    end else
    begin
      v6Column7.Options.Focusing:=False;
      sql.Add(' and isnull(ClotheType,'''')=''次品'' ');
    end; }
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet6);
  SInitCDSData20(ADOQuery1,ClientDataSet6);
end;

procedure TfrmMaDanManageST.Button1Click(Sender: TObject);
begin
  if Trim(FMainId)='' then
  begin
    Application.MessageBox('没有选择订单!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择生产流程!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择安排信息!','提示',0);
    Exit;
  end;
  ClientDataSet2.Locate('SSel',True,[]);
  ClientDataSet1.Locate('SSel',True,[]);

  if  Trim(ClientDataSet1.fieldbyname('APType').AsString)<>'调用' then
  begin
    if ClientDataSet5.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择采购加工合同!','提示',0);
      Exit;
    end;
  end else
  begin
    if Trim(ClientDataSet1.fieldbyname('DataType').AsString)='外围' then
    begin
      Application.MessageBox('外围调用数据，不能手工增加实体码单!','提示',0);
      Exit;
    end;
  end;




  with ClientDataSet3 do
  begin
    Append;
    if  Trim(ClientDataSet1.fieldbyname('APType').AsString)<>'调用' then
    begin
      ClientDataSet5.Locate('SSel',True,[]);
    end;
    ClientDataSet2.Locate('SSel',True,[]);
    ClientDataSet1.Locate('SSel',True,[]);
    if  Trim(ClientDataSet1.fieldbyname('APType').AsString)<>'调用' then
    begin
      FieldByName('ConNo').Value:=ClientDataSet5.fieldbyname('ConNo').Value;
      FieldByName('FactoryNoName').Value:=ClientDataSet5.fieldbyname('FactoryNoName').Value;
      FieldByName('Price').Value:=ClientDataSet5.fieldbyname('Price').Value;
    end;
    FieldByName('MDDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOQueryTemp)));
    FieldByName('FactoryNo').Value:=ClientDataSet1.fieldbyname('FactoryNo').Value;
    FieldByName('FactoryName').Value:=ClientDataSet1.fieldbyname('FactoryName').Value;
    FieldByName('PSName').Value:=ClientDataSet2.fieldbyname('PSName').Value;
    FieldByName('QtyUnit').Value:=ClientDataSet1.fieldbyname('PFTPUnit').Value;
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
    FieldByName('PRTHX').Value:=ClientDataSet1.fieldbyname('PRTHX').Value;
    FieldByName('PRTColor').Value:=ClientDataSet1.fieldbyname('PRTColor').Value;
    FieldByName('BatchNo').Value:=ClientDataSet1.fieldbyname('BatchNo').Value;
    Post;
  end;
end;

procedure TfrmMaDanManageST.v2Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  PSID,ConType,fsj:String;
  Mvalue:Boolean;
begin
  Mvalue:=TcxCheckBox(Sender).EditingValue;
  PSID:=Trim(ClientDataSet2.fieldbyname('PSID').AsString);
  with ClientDataSet2 do
  begin
    while  ClientDataSet2.Locate('SSel',True,[])=True do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    ClientDataSet2.Locate('PSID',PSID,[]);
    Edit;
    FieldByName('SSel').Value:=Mvalue;
    Post;
  end;
  if Mvalue=True then
  begin
    fsj:='select Code=FactoryNo,Name=FactoryName from JYOrder_PCS_Sub A'+
         ' inner join JYOrder_Process B on A.PSID=B.PSID'+
         ' where A.MainId='''+Trim(FMainId)+''' and B.PSXH in('+
         Trim(ClientDataSet2.fieldbyname('PSXH').AsString)+','+
         inttostr(ClientDataSet2.fieldbyname('PSXH').AsInteger+1)+
         ' ) group by FactoryNo,FactoryName'; //and isnull(FactoryNo,'''')<>''''
    //ShowMessage(fsj);
    SInitCxGridComboBoxBySql(ADOQuery1,Tv3Column1,fsj,1,True,'');

  end;

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PRTHX,B.PRTColorNo,PRTColor=isnull(B.PRTColor,'''')+'' ''+isnull(B.PRTColorNo,''''),B.PRTOrdQty,B.OrderUnit,B.ColorNote,JP.PSName,B.FZGYNote  ');
    sql.Add(',Case when isnull(A.CKName,'''')='''' then ''正常'' else ''调用'' end as APType ');
    sql.Add(' ,orderNo=(select OrderNo from JYOrder_Main JM where JM.MainId=(select ORDMainid from JYOrder_Main_MD MD where MD.MDID=A.DiaoYongMDID))');
    //sql.Add(',Case when A.PFTPMQty<>0 then Cast((A.WCMQty-A.TJMQty)*100*1.00/A.PFTPMQty as decimal(18,2)) else 0 end as WCL');
    sql.Add(' from JYOrder_PCS_Sub A');
    sql.Add(' inner join JYOrder_Process JP on A.PSID=JP.PSID');
    sql.Add(' left join JYOrder_Sub B on A.SubId=B.SubId');
    if Mvalue=True then
      sql.Add('where A.PSID='''+Trim(ClientDataSet2.fieldbyname('PSID').AsString)+'''')
    else
      sql.Add(' where A.MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet1);
  SInitCDSData20(ADOQuery1,ClientDataSet1);
  InitGrid();
  if Mvalue=True then
  begin
    ClientDataSet2.Locate('SSel',True,[]);
    ConType:=Trim(ClientDataSet2.fieldbyname('PSName').AsString);
  end else
  begin
    ConType:='';
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,  ');
    sql.Add(' Case when A.MainId not like ''CM%'' then Cast(1 as bit ) else Cast(0 as bit ) end as DYFlag , ');
    Sql.Add(' Case when isnull(A.JYType,'''')<>'''' then Cast(1 as bit) else Cast(0 as bit) end as JYFlag ');
    sql.Add(' from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId');
    sql.Add(' where A.ConNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
    if Trim(ConType)<>'' then
    begin
      sql.Add(' and A.ConType='''+Trim(ConType)+'''');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet5);
  SInitCDSData20(ADOQueryTemp,ClientDataSet5);
end;

procedure TfrmMaDanManageST.v5Column8PropertiesEditValueChanged(
  Sender: TObject);
var
  PSID:String;
  Mvalue:Boolean;
begin
  Mvalue:=TcxCheckBox(Sender).EditingValue;
  PSID:=Trim(ClientDataSet5.fieldbyname('SubID').AsString);
  with ClientDataSet5 do
  begin
    while  ClientDataSet5.Locate('SSel',True,[])=True do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    ClientDataSet5.Locate('SubID',PSID,[]);
    Edit;
    FieldByName('SSel').Value:=Mvalue;
    Post;
  end;
  InitGrid();
end;

procedure TfrmMaDanManageST.Tv3Column1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.ClientDataSet3 do
        begin
          Edit;
          FieldByName('FromFactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('FromFactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmMaDanManageST.Button3Click(Sender: TObject);
var
  maxno,ConMainId,ConSubId,PSID,OrdSubId,PFID,maxCJNO,FAPType:string;
begin
  Panel9.SetFocus;
  if ClientDataSet3.IsEmpty then Exit;
  if Tv3.GroupedItemCount<>0 then
  begin
    Application.MessageBox('此格式不能保存数据!请把汇总框中的项目拖下!','提示',0);
    Exit;
  end;
  //if Trim(ClientDataSet3.FieldByName('MDId').AsString)='' then
  begin
    if ClientDataSet2.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择生产流程!','提示',0);
      Exit;
    end;
    if ClientDataSet1.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择安排信息!','提示',0);
      Exit;
    end;
    if Trim(ClientDataSet1.fieldbyname('APType').AsString)<>'调用' then
    begin
      if ClientDataSet5.Locate('SSel',True,[])=False then
      begin
        Application.MessageBox('没有选择加工合同!','提示',0);
        Exit;
      end;
    end;
  end;
  if ClientDataSet3.Locate('QtyUnit',null,[])=True then
  begin
    Application.MessageBox('单位不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('MDDate',null,[])=True then
  begin
    Application.MessageBox('码单日期不能为空!','提示',0);
    Exit;
  end;
  {f ClientDataSet3.Locate('MDDate','',[])=True then
  begin
    Application.MessageBox('码单日期不能为空!','提示',0);
    Exit;
  end;}
  if ClientDataSet3.Locate('FromFactoryName',null,[])=True then
  begin
    Application.MessageBox('下道供应商不能为空!','提示',0);
    Exit;
  end;
  {if ClientDataSet3.Locate('SelfGangNo',null,[])=True then
  begin
    Application.MessageBox('本厂缸号不能为空!','提示',0);
    Exit;
  end;}
  if Trim(SParameters1)='高权限' then
  begin
    if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  end else
  begin
    if Application.MessageBox('确定要保存数据吗?数据保存后将不能修改！','提示',32+4)<>IDYES then Exit;
  end;
  ClientDataSet1.Locate('SSel',True,[]);
  ConMainId:=Trim(ClientDataSet5.fieldbyname('MainId').AsString);
  ConSubId:=Trim(ClientDataSet5.fieldbyname('SubId').AsString);
  PSID:=Trim(ClientDataSet2.fieldbyname('PSID').AsString);
  OrdSubId:=Trim(ClientDataSet1.fieldbyname('SubId').AsString);
  PFID:=Trim(ClientDataSet1.fieldbyname('PFID').AsString);
  FAPType:=Trim(ClientDataSet1.fieldbyname('APType').AsString);
  if Trim(ClientDataSet3.fieldbyname('ConMainId').AsString)<>'' then
  begin
    ConMainId:=Trim(ClientDataSet3.fieldbyname('ConMainId').AsString);
  end;
  if Trim(ClientDataSet3.fieldbyname('ConSubId').AsString)<>'' then
  begin
    ConSubId:=Trim(ClientDataSet3.fieldbyname('ConSubId').AsString);
  end;
  if Trim(ClientDataSet3.fieldbyname('OrdSubId').AsString)<>'' then
  begin
    OrdSubId:=Trim(ClientDataSet3.fieldbyname('OrdSubId').AsString);
  end;
  if Trim(ClientDataSet3.fieldbyname('PSID').AsString)<>'' then
  begin
    PSID:=Trim(ClientDataSet3.fieldbyname('PSID').AsString);
  end;
  if Trim(ClientDataSet3.fieldbyname('PFID').AsString)<>'' then
  begin
    PFID:=Trim(ClientDataSet3.fieldbyname('PFID').AsString);
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet3.DisableControls;
    with ClientDataSet3 do
    begin
      First;
      while not Eof do
      begin
        if Trim(SParameters1)<>'高权限' then
        begin
          {if Trim(ClientDataSet3.fieldbyname('MDID').AsString)<>'' then
          begin
            Next;
            Continue;
          end; }
        end;

        if Trim(ClientDataSet3.fieldbyname('MDId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'','JYOrder_Main_MD',4,1)=False then
          begin
            ClientDataSet3.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取码单流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(ClientDataSet3.fieldbyname('MDId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet3.fieldbyname('MDId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet3.fieldbyname('MDId').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('MDID').Value:=Trim(maxno);
          FieldByName('OrdMainID').Value:=Trim(FMainId);
          FieldByName('ConMainID').Value:=Trim(ConMainId);
          FieldByName('ConSubID').Value:=Trim(ConSubId);
          FieldByName('OrdSubId').Value:=Trim(OrdSubId);
          FieldByName('PSID').Value:=Trim(PSID);
          FieldByName('PFID').Value:=Trim(PFID);
          FieldByName('MDType').Value:='实体';
          RTSetSaveDataCDS(ADOQueryCmd,Tv3,ClientDataSet3,'JYOrder_Main_MD',2);
          if Trim(ClientDataSet3.fieldbyname('FZQty').AsString)='' then
          begin
            FieldByName('FZQty').Value:=0;
          end else
          begin
            FieldByName('FZQty').Value:=ClientDataSet3.fieldbyname('FZQty').Value;
          end;

          if ClientDataSet3.FieldByName('NotPrint').AsBoolean=True then
          begin
            FieldByName('NotPrint').Value:=1;
          end else
          begin
            FieldByName('NotPrint').Value:=0;
          end;
          if ClientDataSet3.FieldByName('FirstGangFlag').AsBoolean=True then
          begin
            FieldByName('FirstGangFlag').Value:=1;
          end else
          begin
            FieldByName('FirstGangFlag').Value:=0;
          end;
          if ClientDataSet3.FieldByName('HXFlag').AsBoolean=True then
          begin
            FieldByName('HXFlag').Value:=1;
          end else
          begin
            FieldByName('HXFlag').Value:=0;
          end;
          if Trim(FAPType)='调用' then
          begin
            FieldByName('MDNO').Value:='DC'+Trim(maxno);
          end else
          begin
            FieldByName('MDNO').Value:='MD'+Trim(maxno);
          end;
          FieldByName('FactoryNo').Value:=Trim(ClientDataSet3.fieldbyname('FactoryNo').AsString);
          FieldByName('FromFactoryNo').Value:=Trim(ClientDataSet3.fieldbyname('FromFactoryNo').AsString);
          FieldByName('BatchNo').Value:=Trim(ClientDataSet3.fieldbyname('BatchNo').AsString);
          if ClientDataSet5.Locate('SSel',True,[])=True then
          begin
            FieldByName('ConCode').Value:=ClientDataSet5.fieldbyname('C_Code').Value;
            FieldByName('ConName').Value:=ClientDataSet5.fieldbyname('C_CodeName').Value;
            FieldByName('ConSpec').Value:=ClientDataSet5.fieldbyname('C_Spec').Value;
            FieldByName('ConCF').Value:=ClientDataSet5.fieldbyname('C_CF').Value;
            FieldByName('ConMF').Value:=ClientDataSet5.fieldbyname('MFQty').AsString;
            FieldByName('ConKZ').Value:=ClientDataSet5.fieldbyname('KZQty').AsString;
            FieldByName('ConPrice').Value:=ClientDataSet5.fieldbyname('Price').Value;
          end;
          Post;
        end;
        if Trim(ClientDataSet1.fieldbyname('BatchNo').AsString)='' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYOrder_Main_MD Set ');
            sql.Add('ConName=(select ConName from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet1.fieldbyname('DiaoYongMDID').AsString)+''')');
            sql.Add(',ConSpec=(select ConSpec from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet1.fieldbyname('DiaoYongMDID').AsString)+''')');
            sql.Add(',ConCF=(select ConCF from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet1.fieldbyname('DiaoYongMDID').AsString)+''')');
            sql.Add(',ConMF=(select ConMF from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet1.fieldbyname('DiaoYongMDID').AsString)+''')');
            sql.Add(',ConKZ=(select ConKZ from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet1.fieldbyname('DiaoYongMDID').AsString)+''')');
            //sql.Add('ConPrice=(select ConName from JYOrder_Main_MD A where A.MDID='''+Trim(ClientDataSet5.fieldbyname('DiaoYongMDID').AsString)+''')');
            SQL.Add(' where MDID='''+Trim(maxno)+'''');
            ExecSQL;
          end;
        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYOrder_Main_MD Set ');
            sql.Add('ConName=(select Top 1 SPName from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet1.fieldbyname('BatchNO').AsString)+''')');
            sql.Add(',ConSpec=(select Top 1 SPSpec from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet1.fieldbyname('BatchNO').AsString)+''')');
            sql.Add(',ConCF=(select  Top 1 SPCF from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet1.fieldbyname('BatchNO').AsString)+''')');
            sql.Add(',ConMF=(select  Top 1 SPMF from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet1.fieldbyname('BatchNO').AsString)+''')');
            sql.Add(',ConKZ=(select  Top 1 SPKZ from CK_SXPB_CR A where A.BatchNO='''+Trim(ClientDataSet1.fieldbyname('BatchNO').AsString)+''')');
            sql.Add(',ConPrice=(select Top 1 CostPrice from CK_SXPB_CR A ');
            sql.Add('         where A.BatchNO='''+Trim(ClientDataSet1.fieldbyname('BatchNO').AsString)+''' order by CRTime desc)');
            SQL.Add(' where MDID='''+Trim(maxno)+'''');
            ExecSQL;
          end;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD where OrdSubId='''+Trim(OrdSubId)+'''');
          sql.Add(' and SelfGangNo='''+Trim(ClientDataSet3.fieldbyname('SelfGangNo').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.RecordCount>1 then
        begin
          if Application.MessageBox(Pchar('本厂缸号<'+Trim(ClientDataSet3.fieldbyname('SelfGangNo').AsString)+'>重复，确定要保存数据吗？'),'提示',32+4)<>IDYES then
          begin
            ClientDataSet3.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Exit;
          end;
        end;
        with ADOQueryTemp do
        begin
          close;
          sql.Clear;
          sql.Add('select QtyUnit from JYOrder_Main_MD where PFID='''+Trim(PFID)+'''');
          sql.Add(' and isnull(CKName,'''')='''' ');
          sql.Add(' Group by QtyUnit');
          Open;
        end;
        if ADOQueryTemp.RecordCount>1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          ClientDataSet3.EnableControls;
          Application.MessageBox('单位不一致，不能保存数据!','提示',0);
          Exit;
        end;
        
        Edit;
        FieldByName('MDID').Value:=Trim(maxno);
        if Trim(FAPType)='调用' then
        begin
          FieldByName('MDNO').Value:='DC'+Trim(maxno);
        end else
        begin
          FieldByName('MDNO').Value:='MD'+Trim(maxno);
        end;
        FieldByName('OrdMainID').Value:=Trim(FMainId);
        FieldByName('ConMainID').Value:=Trim(ConMainId);
        FieldByName('ConSubID').Value:=Trim(ConSubId);
        FieldByName('OrdSubId').Value:=Trim(OrdSubId);
        FieldByName('PSID').Value:=Trim(PSID);
        FieldByName('PFID').Value:=Trim(PFID);
        Post;
        Next;
      end;
    end;

    if Trim(ClientDataSet1.fieldbyname('APType').AsString)='调用' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_PCS_Sub Set DiaoYongCKPS=(select isnull(Sum(PS),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID )');
        sql.Add(',DiaoYongCKQty=(select isnull(Sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID )');
        sql.Add(' where PFID='''+Trim(PFID)+'''');
        ExecSQL;
      end;
      if Trim(ClientDataSet1.fieldbyname('DiaoYongMDID').AsString)<>'' then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Main_MD Set CKPS=(select isnull(Sum(DiaoYongCKPS),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
          sql.Add(',CKQty=(select isnull(Sum(DiaoYongCKQty),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
          sql.Add(' where MDID='''+Trim(ClientDataSet1.fieldbyname('DiaoYongMDID').AsString)+'''');
          ExecSQL;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrder_Main_MD ');
        sql.Add(' where MDID='''+Trim(ClientDataSet1.fieldbyname('DiaoYongMDID').AsString)+'''');
        sql.Add(' and Qty<CKQty');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        ClientDataSet3.EnableControls;
        Application.MessageBox('出库数量大于库存数量，不能保存数据!','提示',0);
        Exit;
      end;
    end;
    {if UpdateTJCKQty(PFID)=false then  //更新调节仓库中的库存数量。(自动核销)
    begin
      ClientDataSet3.EnableControls;
      ADOQueryCmd.Connection.RollbackTrans;
      Exit;
    end;}

    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet3.EnableControls;
  except
    ClientDataSet3.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmMaDanManageST.FormCreate(Sender: TObject);
begin
  SParameters1:=Trim(DParameters1);
end;

procedure TfrmMaDanManageST.Button2Click(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
  if Trim(ClientDataSet3.fieldbyname('MDID').AsString)<>'' then
  begin
    if ClientDataSet3.FieldByName('HCFlag').AsBoolean=True then
    begin
      Application.MessageBox('已核查，不能删除!','提示',0);
      Exit;
    end;
    if Trim(SParameters1)<>'高权限' then
    begin
      Application.MessageBox('已保存的数据不能删除!','提示',0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from WFB_MJJY where APID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已检验不能删除数据!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Main_MD where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
        ExecSQL;
      end;
      {if Trim(ClientDataSet3.fieldbyname('QtyUnit').AsString)<>'M' then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_PCS_Sub Set WCMQty=');
          sql.Add('(select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')='''')*');
          sql.Add('(select KgZM from JYOrder_Process A where A.PSID=JYOrder_PCS_Sub.PSID)');
          sql.Add(' where PFID='''+Trim(ClientDataSet3.fieldbyname('PFID').AsString)+'''');
          ExecSQL;
        end;
      end else
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_PCS_Sub Set WCMQty=');
          sql.Add('(select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')='''')');
          sql.Add(' where PFID='''+Trim(ClientDataSet3.fieldbyname('PFID').AsString)+'''');
          ExecSQL;
        end;
      end;  }
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrder_PCS_Sub where PFID='''+Trim(ClientDataSet3.fieldbyname('PFID').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        if Trim(ADOQueryTemp.fieldbyname('CKName').AsString)<>'' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYOrder_PCS_Sub Set DiaoYongCKPS=(select isnull(Sum(PS),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID )');
            sql.Add(',DiaoYongCKQty=(select isnull(Sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID )');
            sql.Add(' where PFID='''+Trim(ClientDataSet3.fieldbyname('PFID').AsString)+'''');
            ExecSQL;
          end;
          if Trim(ADOQueryTemp.fieldbyname('DiaoYongMDID').AsString)<>'' then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('UPdate JYOrder_Main_MD Set CKPS=(select isnull(Sum(DiaoYongCKPS),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
              sql.Add(',CKQty=(select isnull(Sum(DiaoYongCKQty),0) from JYOrder_PCS_Sub A where isnull(A.DiaoYongMDID,'''')=JYOrder_Main_MD.MDID)');
              sql.Add(' where MDID='''+Trim(ADOQueryTemp.fieldbyname('DiaoYongMDID').AsString)+'''');
              ExecSQL;
            end;
          end;

        end;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      ClientDataSet3.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('操作异常!','提示',0);
      Exit;
    end;
  end else
  begin
    ClientDataSet3.Delete;
  end;
end;

procedure TfrmMaDanManageST.Button4Click(Sender: TObject);
begin
  if Trim(FMainId)='' then
  begin
    Application.MessageBox('没有选择订单!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择生产流程!','提示',0);
    Exit;
  end;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择安排信息!','提示',0);
    Exit;
  end;
  ClientDataSet2.Locate('SSel',True,[]);
  ClientDataSet1.Locate('SSel',True,[]);
  if  Trim(ClientDataSet1.fieldbyname('APType').AsString)<>'调用' then
  begin
    if ClientDataSet5.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择采购加工合同!','提示',0);
      Exit;
    end;
  end;
  with ClientDataSet6 do
  begin
    Append;
    if  Trim(ClientDataSet1.fieldbyname('APType').AsString)<>'调用' then
    begin
      ClientDataSet5.Locate('SSel',True,[]);
    end;

    ClientDataSet2.Locate('SSel',True,[]);
    ClientDataSet1.Locate('SSel',True,[]);
    if  Trim(ClientDataSet1.fieldbyname('APType').AsString)<>'调用' then
    begin
      FieldByName('ConNo').Value:=ClientDataSet5.fieldbyname('ConNo').Value;
      FieldByName('FactoryName').Value:=ClientDataSet5.fieldbyname('FactoryNoName').Value;
      FieldByName('Price').Value:=ClientDataSet5.fieldbyname('Price').Value;
      FieldByName('FactoryNoName').Value:=ClientDataSet5.fieldbyname('FactoryNoName').Value;
    end else
    begin
      FieldByName('FactoryNo').Value:=ClientDataSet5.fieldbyname('FactoryNo').Value;
      FieldByName('FactoryName').Value:=ClientDataSet5.fieldbyname('FactoryNoName').Value;
    end;



    FieldByName('PSName').Value:=ClientDataSet2.fieldbyname('PSName').Value;
    FieldByName('QtyUnit').Value:=ClientDataSet1.fieldbyname('PFTPUnit').Value;
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('MDDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOQueryTemp)));
    FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
    FieldByName('PRTHX').Value:=ClientDataSet1.fieldbyname('PRTHX').Value;
    FieldByName('PRTColor').Value:=ClientDataSet1.fieldbyname('PRTColor').Value;
    FieldByName('BatchNo').Value:=ClientDataSet1.fieldbyname('BatchNo').Value;
   { if cxTabControl1.TabIndex=0 then
    begin
      v6Column7.Options.Focusing:=True;
    end else
    begin
      v6Column7.Options.Focusing:=False;
      FieldByName('ClotheType').Value:='次品';
    end;}
    Post;
  end;
end;

procedure TfrmMaDanManageST.Button5Click(Sender: TObject);
begin

  if ClientDataSet6.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD_Out where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已经调用不能删除数据!','提示',0);
    Exit;
  end;
  if Trim(ClientDataSet6.fieldbyname('MDID').AsString)<>'' then
  begin
    if ClientDataSet6.FieldByName('HCFlag').AsBoolean=True then
    begin
      Application.MessageBox('已核查，不能删除!','提示',0);
      Exit;
    end;
    if Trim(SParameters1)<>'高权限' then
    begin
      Application.MessageBox('已保存的数据不能删除!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
      ExecSQL;
    end;
    if Trim(ClientDataSet6.fieldbyname('QtyUnit').AsString)<>'M' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_PCS_Sub Set TJMQty=');
        sql.Add('(select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')<>'''')*');
        sql.Add('(select KgZM from JYOrder_Process A where A.PSID=JYOrder_PCS_Sub.PSID)');
        sql.Add(' where PFID='''+Trim(ClientDataSet6.fieldbyname('PFID').AsString)+'''');
        ExecSQL;
      end;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_PCS_Sub Set TJMQty=');
        sql.Add('(select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')<>'''')');
        sql.Add(' where PFID='''+Trim(ClientDataSet6.fieldbyname('PFID').AsString)+'''');
        ExecSQL;
      end;
    end;
  end;
  ClientDataSet6.Delete;
end;

procedure TfrmMaDanManageST.Button6Click(Sender: TObject);
var
  maxno,ConMainId,ConSubId,PSID,OrdSubId,PFID:string;
begin
  if ClientDataSet6.IsEmpty then Exit;
  if Tv6.GroupedItemCount<>0 then
  begin
    Application.MessageBox('此格式不能保存数据!请把汇总框中的项目拖下!','提示',0);
    Exit;
  end;
  //if Trim(ClientDataSet6.FieldByName('MDId').AsString)='' then
  begin
    if ClientDataSet2.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择生产流程!','提示',0);
      Exit;
    end;
   { if ClientDataSet5.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择加工合同!','提示',0);
      Exit;
    end;  }
    if ClientDataSet1.Locate('SSel',True,[])=False then
    begin
      Application.MessageBox('没有选择安排信息!','提示',0);
      Exit;
    end;
    if Trim(ClientDataSet1.fieldbyname('APType').AsString)<>'调用' then
    begin
      if ClientDataSet5.Locate('SSel',True,[])=False then
      begin
        Application.MessageBox('没有选择加工合同!','提示',0);
        Exit;
      end;
    end;
  end;
  if ClientDataSet6.Locate('ToFactoryName',null,[])=True then
  begin
    Application.MessageBox('存放地点不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet6.Locate('MDDate',null,[])=True then
  begin
    Application.MessageBox('码单日期不能为空!','提示',0);
    Exit;
  end;
  {if ClientDataSet6.Locate('MDDate','',[])=True then
  begin
    Application.MessageBox('码单日期不能为空!','提示',0);
    Exit;
  end;  }
  if ClientDataSet6.Locate('ToFactoryName','',[])=True then
  begin
    Application.MessageBox('存放地点不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet6.Locate('QtyUnit',null,[])=True then
  begin
    Application.MessageBox('单位不能为空!','提示',0);
    Exit;
  end;
  if ClientDataSet6.Locate('DutyReason',null,[])=True then
  begin
    Application.MessageBox('入库原因不能为空!','提示',0);
    Exit;
  end;
  if Trim(SParameters1)='高权限' then
  begin
    if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  end else
  begin
    if Application.MessageBox('确定要保存数据吗?数据保存后将不能修改！','提示',32+4)<>IDYES then Exit;
  end;
  //if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  //ClientDataSet1.Locate('SSel',True,[]);
  ConMainId:=Trim(ClientDataSet5.fieldbyname('MainId').AsString);
  ConSubId:=Trim(ClientDataSet5.fieldbyname('SubId').AsString);
  PSID:=Trim(ClientDataSet2.fieldbyname('PSID').AsString);
  OrdSubId:=Trim(ClientDataSet1.fieldbyname('SubId').AsString);
  PFID:=Trim(ClientDataSet1.fieldbyname('PFID').AsString);
  if Trim(ClientDataSet6.fieldbyname('ConMainId').AsString)<>'' then
  begin
    ConMainId:=Trim(ClientDataSet6.fieldbyname('ConMainId').AsString);
  end;
  if Trim(ClientDataSet6.fieldbyname('ConSubId').AsString)<>'' then
  begin
    ConSubId:=Trim(ClientDataSet6.fieldbyname('ConSubId').AsString);
  end;
  if Trim(ClientDataSet6.fieldbyname('OrdSubId').AsString)<>'' then
  begin
    OrdSubId:=Trim(ClientDataSet6.fieldbyname('OrdSubId').AsString);
  end;
  if Trim(ClientDataSet6.fieldbyname('PSID').AsString)<>'' then
  begin
    PSID:=Trim(ClientDataSet6.fieldbyname('PSID').AsString);
  end;
  if Trim(ClientDataSet6.fieldbyname('PFID').AsString)<>'' then
  begin
    PFID:=Trim(ClientDataSet6.fieldbyname('PFID').AsString);
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet6.DisableControls;
    with ClientDataSet6 do
    begin
      First;
      while not Eof do
      begin
        if Trim(SParameters1)<>'高权限' then
        begin
          {if Trim(ClientDataSet6.fieldbyname('MDID').AsString)<>'' then
          begin
            Next;
            Continue;
          end; }
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
          Open;
        end;
        if Trim(ADOQueryTemp.fieldbyname('CRFlag').AsString)='入库' then
        begin
          Next;
          Continue;
        end;
        if Trim(ClientDataSet6.fieldbyname('MDId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'','JYOrder_Main_MD',4,1)=False then
          begin
            ClientDataSet6.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取码单流水号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(ClientDataSet6.fieldbyname('MDId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet6.fieldbyname('MDId').AsString)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('MDID').Value:=Trim(maxno);
          FieldByName('OrdMainID').Value:=Trim(FMainId);
          FieldByName('ConMainID').Value:=Trim(ConMainId);
          FieldByName('ConSubID').Value:=Trim(ConSubId);
          FieldByName('OrdSubId').Value:=Trim(OrdSubId);
          FieldByName('PSID').Value:=Trim(PSID);
          FieldByName('PFID').Value:=Trim(PFID);
          FieldByName('MDType').Value:='实体';
          RTSetSaveDataCDSBand(ADOQueryCmd,Tv6,ClientDataSet6,'JYOrder_Main_MD',2);
          FieldByName('MDNO').Value:='TJ'+Trim(maxno);
          FieldByName('FactoryNo').Value:=Trim(ClientDataSet6.fieldbyname('FactoryNo').AsString);
          FieldByName('FromFactoryNo').Value:=Trim(ClientDataSet6.fieldbyname('FromFactoryNo').AsString);
          FieldByName('ToFactoryNo').Value:=Trim(ClientDataSet6.fieldbyname('ToFactoryNo').AsString);
          FieldByName('DutyFactoryNo').Value:=Trim(ClientDataSet6.fieldbyname('DutyFactoryNo').AsString);
          FieldByName('CKName').Value:='调节仓库';
          if ClientDataSet6.FieldByName('OtherOrdFlag').AsBoolean=True then
          begin
            FieldByName('OtherOrdFlag').Value:=1;
          end else
          begin
            FieldByName('OtherOrdFlag').Value:=0;
          end;
          if ClientDataSet6.FieldByName('CiPinFlag').AsBoolean=True then
          begin
            FieldByName('CiPinFlag').Value:=1;
          end else
          begin
            FieldByName('CiPinFlag').Value:=0;
          end;
          FieldByName('RKDate').Value:=ClientDataSet6.fieldbyname('MDDate').Value;
          FieldByName('RKPS').Value:=ClientDataSet6.fieldbyname('PS').Value;
          FieldByName('RKQty').Value:=ClientDataSet6.fieldbyname('Qty').Value;
          FieldByName('RKQtyUnit').Value:=ClientDataSet6.fieldbyname('QtyUnit').Value;
          if Trim(ClientDataSet6.fieldbyname('ClotheType').AsString)<>'次品' then
          begin
            FieldByName('Status').Value:='已确认';
            FieldByName('LiYongXS').Value:=1;
          end;
          FieldByName('BatchNo').Value:=ClientDataSet6.fieldbyname('BatchNo').Value;
          if ClientDataSet5.Locate('SSel',True,[])=True then
          begin
            FieldByName('ConCode').Value:=ClientDataSet5.fieldbyname('C_Code').Value;
            FieldByName('ConName').Value:=ClientDataSet5.fieldbyname('C_CodeName').Value;
            FieldByName('ConSpec').Value:=ClientDataSet5.fieldbyname('C_Spec').Value;
            FieldByName('ConCF').Value:=ClientDataSet5.fieldbyname('C_CF').Value;
            FieldByName('ConMF').Value:=ClientDataSet5.fieldbyname('MFQty').AsString;
            FieldByName('ConKZ').Value:=ClientDataSet5.fieldbyname('KZQty').AsString;
            FieldByName('ConPrice').Value:=ClientDataSet5.fieldbyname('Price').Value;
          end;
          Post;
        end;
        with ADOQueryTemp do
        begin
          close;
          sql.Clear;
          sql.Add('select QtyUnit from JYOrder_Main_MD where PFID='''+Trim(PFID)+'''');
          sql.Add(' and isnull(CKName,'''')<>'''' ');
          sql.Add(' Group by QtyUnit');
          Open;
        end;
        if ADOQueryTemp.RecordCount>1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          ClientDataSet6.EnableControls;
          Application.MessageBox('单位不一致，不能保存数据!','提示',0);
          Exit;
        end;
        Edit;
        FieldByName('MDID').Value:=Trim(maxno);
        FieldByName('MDNO').Value:='TJ'+Trim(maxno);
        FieldByName('OrdMainID').Value:=Trim(FMainId);
        FieldByName('ConMainID').Value:=Trim(ConMainId);
        FieldByName('ConSubID').Value:=Trim(ConSubId);
        FieldByName('OrdSubId').Value:=Trim(OrdSubId);
        FieldByName('PSID').Value:=Trim(PSID);
        FieldByName('PFID').Value:=Trim(PFID);
        Post;
        Next;
      end;
    end;
    if Trim(ClientDataSet6.fieldbyname('QtyUnit').AsString)<>'M' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_PCS_Sub Set TJMQty=');
        sql.Add('(select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')<>'''')*');
        sql.Add('(select KgZM from JYOrder_Process A where A.PSID=JYOrder_PCS_Sub.PSID)');
        sql.Add(' where PFID='''+Trim(PFID)+'''');
        ExecSQL;
      end;
    end else
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update JYOrder_PCS_Sub Set TJMQty=');
        sql.Add('(select isnull(sum(Qty),0) from JYOrder_Main_MD A where A.PFID=JYOrder_PCS_Sub.PFID and isnull(CKName,'''')<>'''')');
        sql.Add(' where PFID='''+Trim(PFID)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet6.EnableControls;
  except
    ClientDataSet6.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmMaDanManageST.v6Column8PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.ClientDataSet6 do
        begin
          Edit;
          FieldByName('FromFactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('FromFactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmMaDanManageST.v6Column2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      if ShowModal=1 then
      begin
        with Self.ClientDataSet6 do
        begin
          Edit;
          FieldByName('DutyFactoryNo').Value:=Trim(frmGYSList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('DutyFactoryName').Value:=Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmMaDanManageST.v6Column14PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmGYSSelList:=TfrmGYSSelList.Create(Application);
    with frmGYSSelList do
    begin
      if ShowModal=1 then
      begin
        with Self.ClientDataSet6 do
        begin
          Edit;
          FieldByName('ToFactoryNo').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('ZKID').AsString) ;
          FieldByName('ToFactoryName').Value:=Trim(frmGYSSelList.Order_Main.fieldbyname('KHNameJC').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmGYSSelList.Free;
  end;
end;

procedure TfrmMaDanManageST.Button8Click(Sender: TObject);
var
  maxCJNO:String;
begin
  if ClientDataSet3.IsEmpty then Exit;
  if Trim(ClientDataSet3.fieldbyname('MDID').AsString)='' then
  begin
    Application.MessageBox('码单数据未保存!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存初检数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Edit;
      RTSetsavedata(ADOQueryCmd,'JYOrder_Main_MD',ScrollBox2,2);
      FieldByName('ChkResultPerson').Value:=Trim(DName);
      FieldByName('ChkResultDateTime').Value:=SGetServerDateTime(ADOQueryTemp);
      FieldByName('ChkNote').Value:=Trim(ChkNote.Text);
      Post;
    end;
    //安排表插入数据
    if Trim(ChkResult.Text)='合格' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from JYOrder_Main_MD_CJ where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,maxCJNO,'CJ','JYOrder_Main_MD_CJ',4,1)=False then
        begin
          ClientDataSet3.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取初检规则流水号失败!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from JYOrder_Main_MD_CJ where CJID='''+Trim(maxCJNO)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('CJID').Value:=Trim(maxCJNO);
          FieldByName('MDID').Value:=Trim(ClientDataSet3.fieldbyname('MDID').AsString);
          FieldByName('Filler').Value:=Trim(DName);
          Post;
        end;
      end;

    end;
    //安排表插入数据
    ADOQueryCmd.Connection.CommitTrans;
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('ChkResult').Value:=Trim(ChkResult.Text);
      Post;
    end;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('初检数据保存异常!','提示',0);
  end;
end;

procedure TfrmMaDanManageST.Tv3FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
var
  i:Integer;
begin
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD');
    sql.Add(' where MDID='''+Trim(ClientDataSet3.fieldbyname('MDId').AsString)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,ScrollBox2,2);
  with ScrollBox2 do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TComboBox then
      begin
        if Trim(ADOQuery1.fieldbyname(TComboBox(Controls[i]).Name).AsString)<>'' then
        begin
          TComboBox(Controls[i]).ItemIndex:=TComboBox(Controls[i]).Items.IndexOf(ADOQuery1.fieldbyname(TComboBox(Controls[i]).Name).AsString)
        end else
        begin
          TComboBox(Controls[i]).ItemIndex:=-1;
        end;
      end;
    end;
  end; 

  ChkNote.Text:=Trim(ADOQuery1.fieldbyname('ChkNote').AsString);

end;

procedure TfrmMaDanManageST.v1Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  PFID:String;
  Mvalue:Boolean;
begin
  Mvalue:=TcxCheckBox(Sender).EditingValue;
  PFID:=Trim(ClientDataSet1.fieldbyname('PFID').AsString);
  with ClientDataSet1 do
  begin
    while  ClientDataSet1.Locate('SSel',True,[])=True do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
    end;
    ClientDataSet1.Locate('PFID',PFID,[]);
    Edit;
    FieldByName('SSel').Value:=Mvalue;
    Post;
  end;
  if Mvalue=True then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,  ');
      sql.Add(' Case when A.MainId not like ''CM%'' then Cast(1 as bit ) else Cast(0 as bit ) end as DYFlag,  ');
      Sql.Add(' Case when isnull(A.JYType,'''')<>'''' then Cast(1 as bit) else Cast(0 as bit) end as JYFlag ');
      sql.Add(' from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId');
      sql.Add(' where A.ConNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
      sql.Add(' and A.ConType='''+Trim(ClientDataSet1.fieldbyname('PSName').AsString)+'''');
      sql.Add(' and A.FactoryNoName='''+Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)+'''');
      if Trim(ClientDataSet1.fieldbyname('PSName').AsString)<>'检验包装' then
      begin
        if Trim(ClientDataSet1.fieldbyname('APType').AsString)='调用' then
        begin
          sql.Add(' and (A.MainId not like ''CM%'' or isnull(A.JYType,'''')<>'''') ');
        end else
        begin
          //sql.Add(' and A.MainId like ''CM%'' ');
        end;
      end;

      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet5);
    SInitCDSData20(ADOQueryTemp,ClientDataSet5);
  end else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,  ');
      sql.Add(' Case when A.MainId not like ''CM%'' then Cast(1 as bit ) else Cast(0 as bit ) end as DYFlag,  ');
      Sql.Add(' Case when isnull(A.JYType,'''')<>'''' then Cast(1 as bit) else Cast(0 as bit) end as JYFlag ');
      sql.Add(' from Contract_Main A inner join Contract_Sub B on A.MainId=B.MainId');
      sql.Add(' where A.ConNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''');
      sql.Add(' and A.ConType='''+Trim(ClientDataSet1.fieldbyname('PSName').AsString)+'''');
      //sql.Add(' and A.FactoryNoName='''+Trim(ClientDataSet1.fieldbyname('FactoryName').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,ClientDataSet5);
    SInitCDSData20(ADOQueryTemp,ClientDataSet5);
  end;
  InitGrid();
end;

procedure TfrmMaDanManageST.Tv6MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 { with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet6.fieldbyname('MDID').AsString)+'''');
    Open;
  end;   }
  if ClientDataSet6.IsEmpty then Exit;
  if ClientDataSet6.fieldbyname('HCFlag').AsBoolean=True then
  begin
    Tv6.OptionsSelection.CellSelect:=False;
    Exit;
  end else
  begin
    Tv6.OptionsSelection.CellSelect:=True;
  end;
  if Trim(ClientDataSet6.fieldbyname('CRFlag').AsString)='入库' then
  begin
    Tv6.OptionsSelection.CellSelect:=False;
  end else
  begin
    Tv6.OptionsSelection.CellSelect:=True;
  end;
  if Trim(SParameters1)='高权限' then Exit;
  if Trim(ClientDataSet6.fieldbyname('MDID').asstring)<>'' then
  begin
    Tv6.OptionsSelection.CellSelect:=False;
  end else
  begin
    Tv6.OptionsSelection.CellSelect:=True;
  end;
end;

procedure TfrmMaDanManageST.Tv3MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  i:Integer;
begin
  if ClientDataSet3.fieldbyname('HCFlag').AsBoolean=True then
  begin
    v3Column1.Options.Focusing:=False;
    cxGridDBColumn2.Options.Focusing:=False;
    cxGridDBColumn3.Options.Focusing:=False;
    cxGridDBColumn7.Options.Focusing:=False;
    cxGridDBColumn11.Options.Focusing:=False;
    cxGridDBColumn14.Options.Focusing:=False;
    cxGridDBColumn12.Options.Focusing:=False;
    Tv3Column1.Options.Focusing:=False;
    //v3Column5.Options.Focusing:=False;
   // v3Column7.Options.Focusing:=False;
    Exit;
  end else
  begin
    v3Column1.Options.Focusing:=True;
    cxGridDBColumn2.Options.Focusing:=True;
    cxGridDBColumn3.Options.Focusing:=True;
    cxGridDBColumn7.Options.Focusing:=True;
    cxGridDBColumn11.Options.Focusing:=True;
    cxGridDBColumn14.Options.Focusing:=True;
    cxGridDBColumn12.Options.Focusing:=True;
    Tv3Column1.Options.Focusing:=True;
    v3Column5.Options.Focusing:=True;
    v3Column7.Options.Focusing:=True;
  end;
  if Trim(SParameters1)='高权限' then Exit;
  if ClientDataSet3.IsEmpty then Exit;

  if Trim(ClientDataSet3.fieldbyname('MDID').asstring)<>'' then
  begin
    v3Column1.Options.Focusing:=False;
    cxGridDBColumn2.Options.Focusing:=False;
    cxGridDBColumn3.Options.Focusing:=False;
    cxGridDBColumn7.Options.Focusing:=False;
    cxGridDBColumn11.Options.Focusing:=False;
    cxGridDBColumn14.Options.Focusing:=False;
    cxGridDBColumn12.Options.Focusing:=False;
    Tv3Column1.Options.Focusing:=False;
    //v3Column5.Options.Focusing:=False;
    //v3Column7.Options.Focusing:=False;
  end else
  begin
    v3Column1.Options.Focusing:=True;
    cxGridDBColumn2.Options.Focusing:=True;
    cxGridDBColumn3.Options.Focusing:=True;
    cxGridDBColumn7.Options.Focusing:=True;
    cxGridDBColumn11.Options.Focusing:=True;
    cxGridDBColumn14.Options.Focusing:=True;
    cxGridDBColumn12.Options.Focusing:=True;
    Tv3Column1.Options.Focusing:=True;
    v3Column5.Options.Focusing:=True;
    v3Column7.Options.Focusing:=True;
  end;
end;

procedure TfrmMaDanManageST.FormShow(Sender: TObject);
begin
  ReadCxGrid('Tv111',Tv1,'实体码单');
  ReadCxGrid('Tv2',Tv2,'实体码单');
  ReadCxGrid('Tv3',Tv3,'实体码单');
  ReadCxGrid('Tv5',Tv5,'实体码单');
  ReadCxBandedGrid('Tv6',Tv6,'实体码单');
  cxPageControl1.ActivePageIndex:=0;
  cxPageControl2.ActivePageIndex:=0;
end;

procedure TfrmMaDanManageST.Button9Click(Sender: TObject);
begin
  WriteCxGrid('Tv111',Tv1,'实体码单');
  WriteCxGrid('Tv2',Tv2,'实体码单');
  WriteCxGrid('Tv3',Tv3,'实体码单');
  WriteCxGrid('Tv5',Tv5,'实体码单');
  WriteCxBandedGrid('Tv6',Tv6,'实体码单');
  close;
end;

procedure TfrmMaDanManageST.Tv3Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,fsj:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName('FromFactoryName').Value:=Trim(mvalue);
    fsj:=TA((Tv3Column1.Properties as TcxComboBoxProperties).Items.Objects[(Tv3Column1.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    FieldByName('FromFactoryNo').Value:=Trim(fsj);
    Post;
  end;

end;

procedure TfrmMaDanManageST.v6Column8PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,fsj:string;
begin
 { mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet6 do
  begin
    Edit;
    FieldByName('FromFactoryName').Value:=Trim(mvalue);
    fsj:=TA((v6Column8.Properties as TcxComboBoxProperties).Items.Objects[(v6Column8.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    FieldByName('FromFactoryNo').Value:=Trim(fsj);
    Post;
  end;  }

end;

procedure TfrmMaDanManageST.v6Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,fsj:string;
begin
  mvalue:=TcxComboBox(Sender).EditingText;
  with ClientDataSet6 do
  begin
    Edit;
    FieldByName('DutyFactoryName').Value:=Trim(mvalue);
    fsj:=TA((v6Column2.Properties as TcxComboBoxProperties).Items.Objects[(v6Column2.Properties as TcxComboBoxProperties).Items.IndexOf(Trim(mvalue))]).S;
    FieldByName('DutyFactoryNo').Value:=Trim(fsj);
    Post;
  end;

end;

procedure TfrmMaDanManageST.Button7Click(Sender: TObject);
var
  fPrintFile:String;
begin
  if ClientDataSet3.IsEmpty then Exit;
  if ClientDataSet3.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据！','提示',0);
    Exit;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验指示单标签.rmf' ;
  ClientDataSet3.DisableControls;
  with ClientDataSet3 do
  begin
    First;
    while not eof do
    begin
      if ClientDataSet3.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
          Open;
        end;
        if Trim(ADOQueryTemp.fieldbyname('ChkResult').AsString)<>'合格' then
        begin
          ClientDataSet3.EnableControls;
          Application.MessageBox('检验不合格不能打印缸条码!','提示',0);
          Exit;
        end;
        if ADOQueryTemp.FieldByName('NotPrint').AsBoolean=True then
        begin
          ClientDataSet3.EnableControls;
          Application.MessageBox('不能打印《不打印》的缸条码!','提示',0);
          Exit;
        end;
        if FileExists(fPrintFile) then
        begin
          with ADOQueryPrint do
          begin
            Close;
            sql.Clear;
            sql.Add('select B.OrderNo,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ,C.PRTHX,C.PRTColorNo,C.PRTColor,GangNo=A.GangNo,A.PS,A.Qty,A.QtyUnit,A.MDID ');
            SQL.Add(',B.MPRTCode,B.MPRTGY,C.PRTColorEng,A.SelfGangNo');
            sql.Add('from JYOrder_Main_MD A');
            sql.Add('inner join JYOrder_Main B on A.OrdMainid=B.Mainid');
            sql.Add(' inner join JYOrder_Sub C on A.OrdSubid=C.SubId');
            sql.Add(' where A.MDID='''+Trim(ClientDataSet3.fieldbyname('MDID').AsString)+'''');
            Open;
          end;
          RM1.LoadFromFile(fPrintFile);
          //RM1.ShowReport;
          RM1.PrintReport;
        end else
        begin
          ClientDataSet3.DisableControls;
          Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验指示单标签.rmf'),'提示',0);
          Exit;
        end;
      end;

      Next;
    end;
  end;
  ClientDataSet3.EnableControls;
end;

procedure TfrmMaDanManageST.Button10Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='仓库码单录入';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmMaDanManageST.v6Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DutyReason';
      flagname:='入库原因';
      if ShowModal=1 then
      begin
        with Self.ClientDataSet6 do
        begin
          Edit;
          FieldByName('DutyReason').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMaDanManageST.N1Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet3,True);
end;

procedure TfrmMaDanManageST.N2Click(Sender: TObject);
begin
  SelOKNo(ClientDataSet3,False);
end;

procedure TfrmMaDanManageST.Button11Click(Sender: TObject);
var
  FReal:Double;
begin
  if Trim(FMainId)='' then Exit;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(ChuanYangQty.Text)='' then
  begin
    ChuanYangQty.Text:='0';
  end;
  if TryStrToFloat(ChuanYangQty.Text,FReal)=False then
  begin
    Application.MessageBox('船样数量非法!','提示',0);
    Exit;
  end;
  if Trim(LiuYangQty.Text)='' then
  begin
    LiuYangQty.Text:='0';
  end;
  if TryStrToFloat(LiuYangQty.Text,FReal)=False then
  begin
    Application.MessageBox('留样数量非法!','提示',0);
    Exit;
  end;
  if Trim(LingBuQty.Text)='' then
  begin
    LingBuQty.Text:='0';
  end;
  if TryStrToFloat(LingBuQty.Text,FReal)=False then
  begin
    Application.MessageBox('零布数量非法!','提示',0);
    Exit;
  end;
  if Trim(DaKuaiQty.Text)='' then
  begin
    DaKuaiQty.Text:='0';
  end;
  if TryStrToFloat(DaKuaiQty.Text,FReal)=False then
  begin
    Application.MessageBox('大块数量非法!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('Update JYOrder_Main Set ChuanYangQty='+Trim(ChuanYangQty.Text));
      sql.Add(',ChuanYangUnit='''+Trim(ChuanYangUnit.Text)+'''');
      sql.Add(',LiuYangQty='+Trim(LiuYangQty.Text));
      sql.Add(',LiuYangUnit='''+Trim(LiuYangUnit.Text)+'''');
      sql.Add(',LingBuQty='+Trim(LingBuQty.Text));
      sql.Add(',LingBuUnit='''+Trim(LingBuUnit.Text)+'''');
      sql.Add(',DaKuaiQty='+Trim(DaKuaiQty.Text));
      sql.Add(',DaKuaiUnit='''+Trim(DaKuaiUnit.Text)+'''');
      sql.Add(' where Mainid='''+Trim(FMainId)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmMaDanManageST.Button15Click(Sender: TObject);
begin
  if Trim(OrderNo.Text)='' then Exit;
  try
    frmOrderInPutPrice:=TfrmOrderInPutPrice.Create(Application);
    with frmOrderInPutPrice do
    begin
      PState:=1;
      FMainId:=Trim(Self.FMainId);
      FOrderNo:=Trim(Self.OrderNo.Text);
      {cxTabSheet1.Caption:='';
      cxTabSheet1.Enabled:=False;
      cxTabSheet2.Caption:='';
      cxTabSheet2.Enabled:=False; }
      FST:='仓库';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPutPrice.Free;
  end;
end;

procedure TfrmMaDanManageST.v6Column16PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='RKYongTu';
      flagname:='用途';
      if ShowModal=1 then
      begin
        with Self.ClientDataSet6 do
        begin
          Edit;
          FieldByName('RKYongTu').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMaDanManageST.v6Column17PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CDName';
      flagname:='疵点名称';
      if ShowModal=1 then
      begin
        with Self.ClientDataSet6 do
        begin
          Edit;
          FieldByName('CDName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMaDanManageST.cxTabControl1Change(Sender: TObject);
var
  FPSID,FOrdSubId,FConSubId,FPFID:String;
begin
  if ClientDataSet2.Locate('SSel',True,[])=True then
  begin
    FPSID:=Trim(ClientDataSet2.fieldbyname('PSID').AsString);
  end else
  begin
    FPSID:='';
  end;
  if ClientDataSet5.Locate('SSel',True,[])=True then
  begin
    FConSubId:=Trim(ClientDataSet5.fieldbyname('SubId').AsString);
  end else
  begin
    FConSubId:='';
  end;
  if ClientDataSet1.Locate('SSel',True,[])=True then
  begin
    FOrdSubId:=Trim(ClientDataSet1.fieldbyname('SubId').AsString);
    FPFID:=Trim(ClientDataSet1.fieldbyname('PFID').AsString);
  end else
  begin
    FOrdSubId:='';
    FPFID:='';
  end;
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PSName OrdPSName,C.ConNo,C.FactoryNoName,D.Price,JS.PRTHX,JS.PRTColor,JS.PRTColorNo,JS.ColorNote ');
    sql.Add(' from JYOrder_Main_MD  A');
    sql.Add(' inner join JYOrder_Process B on A.PSID=B.PSID');
    sql.Add(' inner join JYOrder_PCS_Sub JPS on A.PFID=JPS.PFID ');
    sql.Add(' left join JYOrder_Sub JS on A.OrdSubId=JS.SubId');
    sql.Add(' inner join Contract_Main C on A.ConMainid=C.MainID');
    sql.Add(' inner join Contract_Sub D on A.ConSubid=D.SubID');
    sql.Add(' where A.ORDMainId='''+Trim(FMainId)+'''');
    if Trim(FPSID)<>'' then
    begin
      sql.Add(' and A.PSID='''+Trim(FPSID)+'''');
    end;
    if Trim(FConSubId)<>'' then
    begin
      sql.Add(' and A.ConSubID='''+Trim(FConSubId)+'''');
    end;
    if Trim(FOrdSubId)<>'' then
    begin
      sql.Add(' and A.OrdSubID='''+Trim(FOrdSubId)+'''');
    end;
    if Trim(FPFID)<>'' then
    begin
      sql.Add(' and A.PFID='''+Trim(FPFID)+'''');
    end;
    sql.Add(' and A.MDType=''实体'' ');
    SQL.Add(' and isnull(A.CKName,'''')=''调节仓库'' ');
    {if cxTabControl1.TabIndex=0 then
    begin
      sql.Add(' and isnull(A.ClotheType,'''')<>''次品'' ');
      v6Column7.Options.Focusing:=True;
    end else
    begin
      v6Column7.Options.Focusing:=False;
      sql.Add(' and isnull(A.ClotheType,'''')=''次品'' ');
    end; }
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet6);
  SInitCDSData20(ADOQuery1,ClientDataSet6);
end;

end.
