unit U_ClothSCListZDSel;

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, BtnEdit;

type
  TfrmClothSCListZDSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNoM: TEdit;
    Label5: TLabel;
    C_CodeNameM: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    C_Color: TEdit;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Qty1: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    RM2: TRMGridReport;
    RMDBPRT: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    cxSplitter1: TcxSplitter;
    DSAnPai: TDataSource;
    CDS_AnPai: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Button1: TButton;
    Button2: TButton;
    v2Column2: TcxGridDBColumn;
    PS: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    SCPerson: TBtnEditA;
    CarNo: TBtnEditA;
    ZhuanQty: TEdit;
    Label9: TLabel;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    Label2: TLabel;
    MainIdTM: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure SCPersonBtnClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure MainIdTMKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    function  SaveData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothSCListZDSel: TfrmClothSCListZDSel;

implementation
uses
  U_DataLink,U_ClothSCInPut,U_Fun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmClothSCListZDSel.FormDestroy(Sender: TObject);
begin
  frmClothSCListZDSel:=nil;
end;

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

procedure TfrmClothSCListZDSel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('车间生产计划单',Tv1,'指示单管理');
  WriteCxGrid('车间生产计划单AP',Tv2,'指示单管理');
end;

procedure TfrmClothSCListZDSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec ClothContract_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                           +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+''''
                                           +' and ConType=''生产'' ';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothSCListZDSel.InitForm();
begin
  ReadCxGrid('车间生产计划单',Tv1,'指示单管理');
  ReadCxGrid('车间生产计划单AP',Tv2,'指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
  {if Trim(DParameters1)='高权限' then
  begin
    TBPrintAgn.Visible:=True;
  end;}
end;

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

procedure TfrmClothSCListZDSel.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmClothSCInPut:=TfrmClothSCInPut.Create(Application);
    with frmClothSCInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FConNo:=Trim(Self.Order_Main.fieldbyname('ConNoM').AsString);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothSCInPut.Free;
  end;
end;

procedure TfrmClothSCListZDSel.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Sub_MX where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已到货不能删除数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //TBRafresh.Click;
    //TBFind.Click;
    Order_Main.Delete;
  end;
end;

function TfrmClothSCListZDSel.DelData():Boolean;
begin
end;

procedure TfrmClothSCListZDSel.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('坯布合同订单列表',cxGrid1);
end;

procedure TfrmClothSCListZDSel.TBPrintClick(Sender: TObject);
var
   fPrintFile,FConNoM:string;

begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\坯布生产单.rmf' ;
  with ADOQueryTemp do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add('exec ClothContract_QryList :MainId,:WSql');
    Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                         +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''';
    Parameters.ParamByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
    Parameters.ParamByName('WSql').Value:='';
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  FConNoM:=Trim(CDS_PRT.fieldbyname('ConNoM').AsString);
  //SDofilter(ADOQueryMain,' ConNoM='''+Trim(Order_Main.fieldbyname('ConNoM').AsString)+'''');
  //SCreateCDS20(ADOQueryMain,Order_Main);
  //SInitCDSData20(ADOQueryMain,Order_Main);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\坯布生产单.rmf'),'提示',0);
  end;
  //SDofilter(ADOQueryMain,'');
  //SCreateCDS20(ADOQueryMain,Order_Main);
  //SInitCDSData20(ADOQueryMain,Order_Main);
  //Order_Main.Locate('ConNoM',FConNoM,[]);
  //SelPrintData(TV4,ADOQueryMain,'合同查询报表');
end;

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

procedure TfrmClothSCListZDSel.TBAddClick(Sender: TObject);
begin
  try
    frmClothSCInPut:=TfrmClothSCInPut.Create(Application);
    with frmClothSCInPut do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmClothSCInPut.Free;
  end;
end;

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

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

procedure TfrmClothSCListZDSel.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*');
    sql.Add('from JYCon_Sub_AnPai A');
    sql.Add(' where A.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_AnPai);
  SInitCDSData20(ADOQueryTemp,CDS_AnPai);
end;

procedure TfrmClothSCListZDSel.Tv2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FInt:=2;
end;

procedure TfrmClothSCListZDSel.Tv3MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FInt:=3;
end;

procedure TfrmClothSCListZDSel.Tv2CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  {with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,C.MPRTSpec,B.PRTColor,C.MPRTCodeName,C.OrderNo ,');
    sql.Add('C_Unit=(select Top 1 C_Unit from Contract_Sub AA,Contract_Sub_Mx BB where AA.SubId=BB.SubId and BB.MXid=A.Mxid)');
    SQL.Add(' from Contract_Sub_MxTo A inner join JYOrder_Sub B on A.OrdSubId=B.SubId ');
    SQL.Add(' inner join JYOrder_Main C on C.MainId=B.MainId ');
    sql.Add('where A.MxId='''+Trim(ClientDataSet2.fieldbyname('MxId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);   }
end;

procedure TfrmClothSCListZDSel.conPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNoM.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec ClothContract_QryList :MainId,:WSql');
        Parameters.ParamByName('WSql').Value:=' and OM.conNo like '''+'%'+Trim(ConNoM.Text)+'%'+''''
                                              +' and ConType=''生产'' ';
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmClothSCListZDSel.SCPersonBtnClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(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
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothSCListZDSel.Button2Click(Sender: TObject);
var
  FFDS:Integer;
begin
  if Trim(PS.Text)='' then
  begin
    Application.MessageBox('匹数不能为空!','提示',0);
    Exit;
  end;
  if TryStrToInt(Trim(PS.Text),FFDS)=False then
  begin
    Application.MessageBox('匹数非法','提示',0);
    Exit;
  end;
  if Trim(SCPerson.Text)='' then
  begin
    Application.MessageBox('员工不能为空!','提示',0);
    Exit;
  end;
  if Trim(CarNo.Text)='' then
  begin
    Application.MessageBox('车号不能为空!','提示',0);
    Exit;
  end;
  if Trim(ZhuanQty.Text)='' then
  begin
    Application.MessageBox('转数不能为空!','提示',0);
    Exit;
  end;
  if TryStrToInt(Trim(ZhuanQty.Text),FFDS)=False then
  begin
    Application.MessageBox('转数非法','提示',0);
    Exit;
  end;
 { with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select isnull(Count(*),0) APPS from JYCon_Sub_AnPai where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if (ADOQueryTemp.FieldByName('APPS').AsInteger+strtoint(PS.Text))>Order_Main.fieldbyname('Qty1').AsInteger then
  begin
    Application.MessageBox('安排匹数过多，不允许操作!','提示',0);
    Exit;
  end; }
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  SaveData();
end;
function TfrmClothSCListZDSel.SaveData():Boolean;
var
  maxno,fPrintFile,TaiQty,TaiQtyMax,TaiQtyMin:String;
  i:Integer;
  FDate:string;
begin
  FDate:=FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOQueryTemp));
  try
    ADOQueryCmd.Connection.BeginTrans;
    ///保存主表

    for i:=1 to StrToInt(PS.Text) do
    begin
      if GetLSNo(ADOQueryCmd,maxno,'','JYCon_Sub_AnPai',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('生成坯布安排流水号异常！','提示',0);
        exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from JYCon_Sub_AnPai where 1<>1');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
        FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
        FieldByName('APId').Value:=Trim(maxno);
        FieldByName('SCPerson').Value:=Trim(SCPerson.Text);
        FieldByName('CarNo').Value:=Trim(CarNo.Text);
        FieldByName('ZhuanQty').Value:=Trim(ZhuanQty.Text);
        FieldByName('PRTCount').Value:=1;
        FieldByName('APDate').Value:=FDate;
        FieldByName('Filler').Value:=Trim(DName);
        Post;
      end;
      //更新加工单价
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add(' select Count(*) TaiQty from ');
        sql.Add('(select distinct(CarNo) CarNo from [JYCon_Sub_AnPai] where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        sql.Add(' and SCPerson='''+Trim(SCPerson.Text)+'''');
        SQL.Add(' and APDate='''+Trim(FDate)+''') AA');
        //ShowMessage(sql.Text);
        Open;
      end;
      TaiQty:=Trim(ADOQueryCmd.fieldbyname('TaiQty').AsString);
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add(' select isnull(Max(DEFFlt1),0) TaiQtyMax,isnull(Min(DEFFlt1),0) TaiQtyMin ');
        sql.Add(' from [KH_Zdy_Attachment] where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.FieldByName('TaiQtyMax').AsInteger=0 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('坯布加工单价未定义!','提示',0);
        Exit;
      end else
      begin
        TaiQtyMax:=ADOQueryTemp.fieldbyname('TaiQtyMax').AsString;
        TaiQtyMin:=ADOQueryTemp.fieldbyname('TaiQtyMin').AsString;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select Top 1 * from [KH_Zdy_Attachment] where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
        sql.Add(' and DEFFlt1='+TaiQty);
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYCon_Sub_AnPai Set JGPrice='+ADOQueryTemp.fieldbyname('DEFFlt3').AsString);
          sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
          sql.Add(' and SCPerson='''+Trim(SCPerson.Text)+'''');
          SQL.Add(' and APDate='''+Trim(FDate)+'''');
          ExecSQL;
        end;
      end else
      begin
        if StrToInt(TaiQty)<StrToInt(TaiQtyMin) then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYCon_Sub_AnPai Set JGPrice=');
            sql.Add('(select Top 1 DEFFlt3 from KH_Zdy_Attachment where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
            sql.Add(' and DEFFlt1='+TaiQtyMin);
            sql.Add(' )where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
            sql.Add(' and SCPerson='''+Trim(SCPerson.Text)+'''');
            SQL.Add(' and APDate='''+Trim(FDate)+'''');
            ExecSQL;
          end;
        end else
        if StrToInt(TaiQty)<StrToInt(TaiQtyMax) then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYCon_Sub_AnPai Set JGPrice=');
            sql.Add('(select Top 1 DEFFlt3 from KH_Zdy_Attachment where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
            sql.Add(' and DEFFlt1>'+TaiQty);
            sql.Add(' )where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
            sql.Add(' and SCPerson='''+Trim(SCPerson.Text)+'''');
            SQL.Add(' and APDate='''+Trim(FDate)+'''');
            ExecSQL;
          end;
        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYCon_Sub_AnPai Set JGPrice=');
            sql.Add('(select Top 1 DEFFlt3 from KH_Zdy_Attachment where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
            sql.Add(' and DEFFlt1='+TaiQtyMax);
            sql.Add(' )where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
            sql.Add(' and SCPerson='''+Trim(SCPerson.Text)+'''');
            SQL.Add(' and APDate='''+Trim(FDate)+'''');
            ExecSQL;
          end;
        end;
      end;
      //更新加工单价
      with CDS_AnPai do
      begin
        Append;
        FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
        FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
        FieldByName('APId').Value:=Trim(maxno);
        FieldByName('SCPerson').Value:=Trim(SCPerson.Text);
        FieldByName('CarNo').Value:=Trim(CarNo.Text);
        FieldByName('ZhuanQty').Value:=Trim(ZhuanQty.Text);
        FieldByName('APDate').Value:=FDate;
        FieldByName('PRTCount').Value:=1;
        Post;
      end;
      fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\坯布检验指示单标签.rmf';
      if FileExists(fPrintFile) then
      begin
        with ADOQueryPrint do
        begin
          Close;
          sql.Clear;
          sql.Add('select B.ConNo ,A.C_CodeName,A.C_Color,A.ConDefStr1,A.ConDefStr2,A.ConDefStr3,A.ConDefStr4, ');
          sql.Add(' AA.APID,AA.ZhuanQty,AA.SCPerson,AA.CarNo,A.ConDefStr5,A.KZQty ');
          sql.Add(' from JYCon_Sub_AnPai AA');
          sql.Add(' inner join Contract_Sub A on AA.SubId=A.SubId ');
          sql.Add(' inner join Contract_Main B on A.MainId=B.MainId ');
          sql.Add(' where AA.APId='''+Trim(CDS_AnPai.fieldbyname('APID').AsString)+'''');
          Open;
        end;
        {CDS_AnPai.Edit;
        CDS_AnPai.FieldByName('PRTFlag').Value:=1;
        CDS_AnPai.FieldByName('PRTCount').Value:=2;
        CDS_AnPai.Post;}
       { with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYCon_Sub_AnPai Set PRTFlag=1,PRTCount=2 ');
          sql.Add(' where APId='''+Trim(CDS_AnPai.fieldbyname('APID').AsString)+'''');
          ExecSQL;
        end; }
        //RMVariables['begindate']:=begindate.DateTime;
        //RMVariables['enddate']:=enddate.DateTime;
        //RMVariables['printtime']:=Now;
        //RMVariables['printer']:=Trim(gUserName);
        {RM2.LoadFromFile(fPrintFile);
        RM2.ShowReport;
        with ADOQueryPrint do
        begin
          Close;
          sql.Clear;
          sql.Add('select B.ConNo ,A.C_CodeName,A.C_Color,A.ConDefStr1,A.ConDefStr2,A.ConDefStr3,A.ConDefStr4, ');
          sql.Add(' AA.APID,AA.ZhuanQty,AA.SCPerson,AA.CarNo ');
          sql.Add(' from JYCon_Sub_AnPai AA');
          sql.Add(' inner join Contract_Sub A on AA.SubId=A.SubId ');
          sql.Add(' inner join Contract_Main B on A.MainId=B.MainId ');
          sql.Add(' where AA.APId='''+Trim(maxno)+'''');
          Open;
        end; }
        //RMVariables['begindate']:=begindate.DateTime;
        //RMVariables['enddate']:=enddate.DateTime;
        //RMVariables['printtime']:=Now;
        //RMVariables['printer']:=Trim(gUserName);
        RM2.LoadFromFile(fPrintFile);
        //RM2.ShowReport;
        RM2.PrintReport;
      end else
      begin
        Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\坯布检验指示单标签.rmf'),'提示',0);
        Exit;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmClothSCListZDSel.Button1Click(Sender: TObject);
var
  TaiQtyMax,TaiQtyMin,TaiQty:string;
begin
  if CDS_AnPai.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if CDS_AnPai.Locate('SSel;JYFlag',VarArrayOf([True,True]),[]) then
  begin
    Application.MessageBox('已检验不能作废!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要作废数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_AnPai.DisableControls;
    with CDS_AnPai do
    begin
      while CDS_AnPai.FieldByName('SSel').AsBoolean=True do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete JYCon_Sub_AnPai  ');
          sql.Add(' where APId='''+Trim(CDS_AnPai.fieldbyname('APID').AsString)+'''');
          ExecSQL;
        end;
        //更新加工单价
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add(' select Count(*) TaiQty from ');
          sql.Add('(select distinct(CarNo) CarNo from [JYCon_Sub_AnPai] where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
          sql.Add(' and SCPerson='''+Trim(CDS_AnPai.fieldbyname('SCPerson').AsString)+'''');
          SQL.Add(' and APDate='''+Trim(CDS_AnPai.fieldbyname('APDate').AsString)+''') AA');
          //ShowMessage(sql.Text);
          Open;
        end;
        TaiQty:=Trim(ADOQueryCmd.fieldbyname('TaiQty').AsString);
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add(' select isnull(Max(DEFFlt1),0) TaiQtyMax,isnull(Min(DEFFlt1),0) TaiQtyMin ');
          sql.Add(' from [KH_Zdy_Attachment] where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('TaiQtyMax').AsInteger=0 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          CDS_AnPai.EnableControls;
          Application.MessageBox('坯布加工单价未定义!','提示',0);
          Exit;
        end else
        begin
          TaiQtyMax:=ADOQueryTemp.fieldbyname('TaiQtyMax').AsString;
          TaiQtyMin:=ADOQueryTemp.fieldbyname('TaiQtyMin').AsString;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select Top 1 * from [KH_Zdy_Attachment] where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
          sql.Add(' and DEFFlt1='+TaiQty);
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update JYCon_Sub_AnPai Set JGPrice='+ADOQueryTemp.fieldbyname('DEFFlt3').AsString);
            sql.Add(' where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
            sql.Add(' and SCPerson='''+Trim(CDS_AnPai.fieldbyname('SCPerson').AsString)+'''');
            SQL.Add(' and APDate='''+Trim(CDS_AnPai.fieldbyname('APDate').AsString)+'''');
            ExecSQL;
          end;
        end else
        begin
          if StrToInt(TaiQty)<StrToInt(TaiQtyMin) then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update JYCon_Sub_AnPai Set JGPrice=');
              sql.Add('(select Top 1 DEFFlt3 from KH_Zdy_Attachment where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
              sql.Add(' and DEFFlt1='+TaiQtyMin);
              sql.Add(' )where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
              sql.Add(' and SCPerson='''+Trim(CDS_AnPai.fieldbyname('SCPerson').AsString)+'''');
              SQL.Add(' and APDate='''+Trim(CDS_AnPai.fieldbyname('APDate').AsString)+'''');
              ExecSQL;
            end;
          end else
          if StrToInt(TaiQty)<StrToInt(TaiQtyMax) then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update JYCon_Sub_AnPai Set JGPrice=');
              sql.Add('(select Top 1 DEFFlt3 from KH_Zdy_Attachment where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
              sql.Add(' and DEFFlt1>'+TaiQty);
              sql.Add(' )where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
              sql.Add(' and SCPerson='''+Trim(CDS_AnPai.fieldbyname('SCPerson').AsString)+'''');
              SQL.Add(' and APDate='''+Trim(CDS_AnPai.fieldbyname('APDate').AsString)+'''');
              ExecSQL;
            end;
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('Update JYCon_Sub_AnPai Set JGPrice=');
              sql.Add('(select Top 1 DEFFlt3 from KH_Zdy_Attachment where ZdyName='''+Trim(Order_Main.fieldbyname('C_CodeName').AsString)+'''');
              sql.Add(' and DEFFlt1='+TaiQtyMax);
              sql.Add(' )where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
              sql.Add(' and SCPerson='''+Trim(CDS_AnPai.fieldbyname('SCPerson').AsString)+'''');
              SQL.Add(' and APDate='''+Trim(CDS_AnPai.fieldbyname('APDate').AsString)+'''');
              ExecSQL;
            end;
          end;
        end;
        //更新加工单价
        CDS_AnPai.Delete;
      end;
    end;
    CDS_AnPai.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_AnPai.EnableControls;
    Application.MessageBox('作废失败!','提示',0);
    Exit;
  end;
end;

procedure TfrmClothSCListZDSel.MainIdTMKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec ClothContract_QryList :MainId,:WSql');
        Parameters.ParamByName('WSql').Value:=' and substring(OM.MainId,3,10)='''+Trim(MainIdTM.Text)+'''';
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    MainIdTM.Text:='';
  end;
end;

procedure TfrmClothSCListZDSel.ToolButton1Click(Sender: TObject);
begin
  ModalResult:=1;
end;

end.
