unit U_ClothContractListLL;

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, cxDropDownEdit, cxCheckBox, cxCurrencyEdit, cxCalc;

type
  TfrmClothContractListLL = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNo: TEdit;
    Label5: TLabel;
    C_CodeName: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    C_Spec: TEdit;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ClientDataSet3: TClientDataSet;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    ClientDataSet2: TClientDataSet;
    ToolButton5: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBPRTColor: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    ToolBar2: TToolBar;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    ToolButton11: TToolButton;
    v1Column5: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label6: TLabel;
    FactoryNoName: TEdit;
    Label7: TLabel;
    FirstName: TEdit;
    v3Column1: TcxGridDBColumn;
    v3TPPS: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v3Column11: TcxGridDBColumn;
    v3Column12: TcxGridDBColumn;
    v3Column13: TcxGridDBColumn;
    v3Column14: TcxGridDBColumn;
    v3Column15: TcxGridDBColumn;
    v3Column16: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v3Column2PropertiesEditValueChanged(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure v3Column5PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    function  YFData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothContractListLL: TfrmClothContractListLL;

implementation
uses
  U_DataLink,U_ClothContractInPut,U_Fun,U_ProductOrderListSel,U_ZDYHelp;

{$R *.dfm}

procedure TfrmClothContractListLL.FormDestroy(Sender: TObject);
begin
  frmClothContractListLL:=nil;
end;

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

procedure TfrmClothContractListLL.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('坯布合同订单ll1',Tv1,'指示单管理');
  WriteCxGrid('坯布合同订单llr11',Tv3,'指示单管理');
end;

procedure TfrmClothContractListLL.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select ClothPS=A.DHPS-A.TPPSHZ,ClothQty=A.DHQty-A.TPQtyHZ, A.DHUnit,SXMainId='''',SXDHID='''',A.MainId,A.SubId');
      SQL.Add(' ,A.DHID,A.DHPS,A.TPPSHZ,A.TPQtyHZ,A.DHQty,A.FirstName,B.ConNo,C.C_CodeName,C.C_Spec,C.MFQty,C.KZQty,B.FactoryNoName ');
      sql.Add(' from Contract_Cloth_DH A inner join Contract_Main B on A.Mainid=B.Mainid');
      sql.Add(' inner join Contract_Sub C on A.Subid=C.Subid');
      SQL.Add(' where B.FillTime>='''+FormatDateTime('yyyy-MM-dd',BegDate.Date)+'''      ');
      sql.Add(' and B.Filltime<'''+FormatDateTime('yyyy-MM-dd',EndDate.Date+1)+'''');
      sql.add(' and isnull(A.DHType,'''')=''''');
      sql.Add(' union all');
      sql.Add('  select ClothPS=A.DHPS-A.TPPSHZ,ClothQty=A.DHQty-A.TPQtyHZ,A.DHUnit,A.SXMainId,A.SXDHID,A.MainId,A.SubId  ');
      SQL.Add(' ,A.DHID,A.DHPS,A.TPPSHZ,A.TPQtyHZ,A.DHQty,A.FirstName ');
      sql.Add(',ConNo=(select Top 1 ConNo from ContractSX_Main SM where SM.MainId=A.SXMainId)');
      sql.Add(',C_CodeName=A.PBName,C_Spec=A.PBSpec,MFQty=A.PBMF,KZQty=A.PBKZ');
      SQL.Add(',FactoryNoName=(select Top 1 FirstName from ContractSX_Cloth_DH SD where SD.DHID=A.SXDHID)');
      sql.Add(' from Contract_Cloth_DH A ');
      SQL.Add(' where exists(select * from ContractSX_Main B where B.MainId=A.SXMainId');
      SQL.Add(' and B.FillTime>='''+FormatDateTime('yyyy-MM-dd',BegDate.Date)+'''      ');
      sql.Add(' and B.Filltime<'''+FormatDateTime('yyyy-MM-dd',EndDate.Date+1)+''')');
      sql.Add('  and isnull(A.DHType,'''')<>'''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothContractListLL.InitForm();
begin
  
  ReadCxGrid('坯布合同订单ll1',Tv1,'指示单管理');
  ReadCxGrid('坯布合同订单llr11',Tv3,'指示单管理');
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
  InitGrid();
end;

procedure TfrmClothContractListLL.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;

function TfrmClothContractListLL.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

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

procedure TfrmClothContractListLL.ConNoChange(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 TfrmClothContractListLL.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmClothContractListLL.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  begin
    try
      frmProductOrderListSel:=TfrmProductOrderListSel.Create(Application);
      with frmProductOrderListSel do
      begin
        OrderNo.Text:=Trim(self.Order_Main.fieldbyname('ConNo').AsString);
        if ShowModal=1 then
        begin
          with frmProductOrderListSel.Order_Main do
          begin
            frmProductOrderListSel.Order_Main.First;
            while not frmProductOrderListSel.Order_Main.Eof do
            begin
              if frmProductOrderListSel.Order_Main.FieldByName('SSel').AsBoolean=True then
              begin
                with Self.ClientDataSet3 do
                begin
                  Self.ClientDataSet3.Append;
                  Self.ClientDataSet3.FieldByName('OrdSubId').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('SubId').AsString);
                  Self.ClientDataSet3.FieldByName('OrderNo').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderNo').AsString);
                  Self.ClientDataSet3.FieldByName('MPRTCodeName').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTCodeName').AsString);
                  Self.ClientDataSet3.FieldByName('MPRTSpec').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('MPRTSpec').AsString);
                  Self.ClientDataSet3.FieldByName('PRTColor').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTColor').AsString);
                  Self.ClientDataSet3.FieldByName('SOrddefstr1').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('SOrddefstr1').AsString);
                  Self.ClientDataSet3.FieldByName('PRTOrderKgQty').Value:=0;
                  Self.ClientDataSet3.FieldByName('PRTOrderQty').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTOrderQty').AsString);
                  Self.ClientDataSet3.FieldByName('OrderUnit').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('OrderUnit').AsString);
                  Self.ClientDataSet3.FieldByName('PRTHX').Value:=Trim(frmProductOrderListSel.Order_Main.fieldbyname('PRTHX').AsString);
                  Self.ClientDataSet3.FieldByName('TPUnit').Value:=Self.Order_Main.FieldByName('DHUnit').Value;
                  Self.ClientDataSet3.FieldByName('Qty2').Value:=0;
                end;
              end;
              frmProductOrderListSel.Order_Main.Next;
            end;
          end;
        end;
      end;
    finally
      frmProductOrderListSel.Free;
    end;
  end;
end;

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

procedure TfrmClothContractListLL.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 TfrmClothContractListLL.ToolButton5Click(Sender: TObject);
var
  maxno,LLIdx,FDW:string;
begin
  if ClientDataSet3.IsEmpty then Exit;
  ToolBar1.SetFocus;
  if ClientDataSet3.Locate('OrderNo',null,[]) then
  begin
    Application.MessageBox('指定订单编号不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('TPPS',null,[]) then
  begin
    Application.MessageBox('匹数不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('TPQty',null,[]) then
  begin
    Application.MessageBox('数量不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('Qty2',null,[]) then
  begin
    Application.MessageBox('数量公差不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('TPUnit',null,[]) then
  begin
    Application.MessageBox('数量单位不能为空！','提示',0);
    Exit;
  end;
  ClientDataSet3.DisableControls;
  with ClientDataSet3 do
  begin
    First;
    while not Eof do
    begin

      if Trim(FDW)='' then
      begin
        FDW:=Trim(ClientDataSet3.fieldbyname('TPUnit').AsString);
      end;
      if FDW<>Trim(ClientDataSet3.fieldbyname('TPUnit').AsString) then
      begin
        Application.MessageBox('领料数量单位不一致!','提示',0);
        Exit;
      end;
      Next;
    end;
  end;
  ClientDataSet3.EnableControls;
  if Trim(Order_Main.fieldbyname('DHUnit').AsString)='M' then
  begin
    if ClientDataSet3.Locate('TPUnit','Kg',[]) then
    begin
      Application.MessageBox('白坯合同数量单位为M,领料数量单位不能为Kg！','提示',0);
      Exit;
    end;
  end;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存去向表
    with ClientDataSet3 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet3.fieldbyname('MXId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'MX','Contract_Cloth_LLMx',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取去向流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(ClientDataSet3.fieldbyname('MXId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Cloth_LLMx where MXId='''+Trim(ClientDataSet3.fieldbyname('MXId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet3.fieldbyname('MXId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
          FieldByName('SXMainId').Value:=Trim(Order_Main.fieldbyname('SXMainId').AsString);
          FieldByName('SXDHId').Value:=Trim(Order_Main.fieldbyname('SXDHId').AsString);
          FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          FieldByName('DHId').Value:=Trim(Order_Main.fieldbyname('DHId').AsString);
          FieldByName('MXId').Value:=Trim(maxno);
          FieldByName('OrdSubId').Value:=Trim(ClientDataSet3.fieldbyname('OrdSubId').AsString);
          FieldByName('TPDate').Value:=ClientDataSet3.fieldbyname('TPDate').Value;
          FieldByName('TPPerson').Value:=ClientDataSet3.fieldbyname('TPPerson').Value;
          FieldByName('TPNote').Value:=ClientDataSet3.fieldbyname('TPNote').Value;
          if Trim(ClientDataSet3.fieldbyname('MXId').AsString)<>'' then
          begin
            if Trim(ADOQueryCmd.fieldbyname('TPUnit').AsString)<>Trim(ClientDataSet3.fieldbyname('TPUnit').AsString) then
            begin
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                sql.Add('select * from Contract_Cloth_LL A where A.DHIDHelp in(');
                sql.Add('select LLID from Contract_Cloth_LL B where B.DHID='''+Trim(ClientDataSet3.fieldbyname('DHID').AsString)+'''');
                sql.Add(' and OrdSubId='''+Trim(ClientDataSet3.fieldbyname('OrdSubId').AsString)+''' and JXJGFlag=1)');
                Open;
              end;
              if ADOQueryTemp.IsEmpty=False then
              begin
                ADOQueryCmd.Connection.RollbackTrans;
                Application.MessageBox('已外加工不能修改数量单位!','提示',0);
                Exit;
              end;
            end;
          end; 
          if Trim(ClientDataSet3.fieldbyname('MXId').AsString)<>'' then
          begin
            if Trim(ADOQueryCmd.fieldbyname('TPUnit').AsString)<>Trim(ClientDataSet3.fieldbyname('TPUnit').AsString) then
            begin
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                sql.Add('select * from JYOrder_Sub_AnPai A where SubId='''+Trim(ClientDataSet3.fieldbyname('OrdSubId').AsString)+'''');
                Open;
              end;
              if ADOQueryTemp.IsEmpty=False then
              begin
                ADOQueryCmd.Connection.RollbackTrans;
                Application.MessageBox('已回仓不能修改数量单位!','提示',0);
                Exit;
              end;
            end;
          end;


          FieldByName('TPUnit').Value:=ClientDataSet3.fieldbyname('TPUnit').Value;
          {if ClientDataSet3.FieldByName('JXJGFlag').AsBoolean=True then
          begin
            FieldByName('JXJGFlag').Value:=1;
          end else
          begin
            FieldByName('JXJGFlag').Value:=0;
          end; }
          if Trim(ClientDataSet3.fieldbyname('TPQty').AsString)<>'' then
            FieldByName('TPQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value
          else
            FieldByName('TPQty').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('JGPrice').AsString)<>'' then
            FieldByName('JGPrice').Value:=ClientDataSet3.fieldbyname('JGPrice').Value
          else
            FieldByName('JGPrice').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('GangFee').AsString)<>'' then
            FieldByName('GangFee').Value:=ClientDataSet3.fieldbyname('GangFee').Value
          else
            FieldByName('GangFee').Value:=0;
          FieldByName('TPPS').AsFloat:=ClientDataSet3.fieldbyname('TPPS').AsFloat;
          if Trim(ClientDataSet3.fieldbyname('Qty1').AsString)<>'' then
            FieldByName('Qty1').Value:=ClientDataSet3.fieldbyname('Qty1').Value
          else
            FieldByName('Qty1').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('Qty2').AsString)<>'' then
            FieldByName('Qty2').Value:=ClientDataSet3.fieldbyname('Qty2').Value
          else
            FieldByName('Qty2').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('Qty3').AsString)<>'' then
            FieldByName('Qty3').Value:=ClientDataSet3.fieldbyname('Qty3').Value
          else
            FieldByName('Qty3').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('TPUnit').AsString)='Kg' then
          begin
            FieldByName('TPMQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value*1.00*1000
                                       /(Order_Main.fieldbyname('MFQty').Value*1.00/100*Order_Main.fieldbyname('KZQty').Value);
          end else
          begin
            FieldByName('TPMQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value;
          end;
          if Trim(ClientDataSet3.fieldbyname('TPUnit').AsString)='Kg' then
          begin
            FieldByName('MQty2').Value:=ClientDataSet3.fieldbyname('Qty2').Value*1.00*1000
                                       /(Order_Main.fieldbyname('MFQty').Value*1.00/100*Order_Main.fieldbyname('KZQty').Value);
          end else
          begin
            FieldByName('MQty2').Value:=ClientDataSet3.fieldbyname('Qty2').Value;
          end;
          if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)=Trim(Order_Main.FieldByName('DHUnit').AsString) then
          begin
            FieldByName('TPYZQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value;
          end else
          begin
            if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)='M' then
            begin
              FieldByName('TPYZQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value
                                       *Order_Main.fieldbyname('MFQty').Value*1.00/100
                                       *Order_Main.fieldbyname('KZQty').Value*1.00/1000;
            end;
          end;
          if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)=Trim(Order_Main.FieldByName('DHUnit').AsString) then
          begin
            FieldByName('YZQty2').Value:=ClientDataSet3.fieldbyname('Qty2').Value;
          end else
          begin
            if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)='M' then
            begin
              FieldByName('YZQty2').Value:=ClientDataSet3.fieldbyname('Qty2').Value
                                       *Order_Main.fieldbyname('MFQty').Value*1.00/100
                                       *Order_Main.fieldbyname('KZQty').Value*1.00/1000;
            end;
          end;
          Post;
        end;
        with ClientDataSet3 do
        begin
          Edit;
          FieldByName('MXId').Value:=Trim(maxno);
          Post;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Contract_Cloth_DH Set TPMQtyHZ=(select Sum(TPMQty) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_DH.DHID)');
          if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)=Trim(Order_Main.FieldByName('DHUnit').AsString) then
          begin
            sql.Add(',TPQtyHZ=(select Sum(TPQty) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_DH.DHID)');
          end else
          begin
            if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)='M' then
            begin
              SQL.Add(',TPQtyHZ=(select sum(TPQty*(PBMF/100)*(PBKZ/1000)) from Contract_Cloth_LLMx A  ');
              sql.Add('  where A.DHID=Contract_Cloth_DH.DHID )');
            end;
          end;
          sql.Add(',TPPSHZ=(select Sum(TPPS) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_DH.DHID)');
          sql.Add(' where DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Cloth_LL where DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
          sql.Add(' and OrdSubId='''+Trim(ClientDataSet3.fieldbyname('OrdSubId').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
         { with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select isnull(Max(LLIdx),0) LLIdx from Contract_Cloth_LL');
            Open;
          end;
          LLIdx:=IntToStr(ADOQueryTemp.fieldbyname('LLIdx').AsInteger+1); }
          if GetLSNo(ADOQueryCmd,maxno,'LL','Contract_Cloth_LL',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取去向流水号失败！','提示',0);
            Exit;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from Contract_Cloth_LL where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('LLID').Value:=Trim(maxno);
            FieldByName('DHID').Value:=Trim(Order_Main.fieldbyname('DHID').AsString);
            FieldByName('SXDHID').Value:=Trim(Order_Main.fieldbyname('SXDHID').AsString);
            FieldByName('SXMainID').Value:=Trim(Order_Main.fieldbyname('SXMainID').AsString);
            FieldByName('FirstNo').Value:=Trim(Order_Main.fieldbyname('FirstName').AsString);
            FieldByName('FirstName').Value:=Trim(Order_Main.fieldbyname('FirstName').AsString);
            FieldByName('OrdSubId').Value:=Trim(ClientDataSet3.fieldbyname('OrdSubId').AsString);
            FieldByName('TPUnit').Value:=Trim(ClientDataSet3.fieldbyname('TPUnit').AsString);
            FieldByName('LLIdx').Value:=1;
            Post;
          end;

        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Contract_Cloth_LL Set TPUnit='''+Trim(ClientDataSet3.fieldbyname('TPUnit').AsString)+'''');
            sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
            ExecSQL;
          end;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Contract_Cloth_LL Set TPPS=(select sum(TPPS) from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.DHID');
          SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
          if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)=Trim(Order_Main.FieldByName('DHUnit').AsString) then
          begin
            sql.Add(',TPQty=(select Sum(TPQty) from Contract_Cloth_LLMx A  where A.DHID=Contract_Cloth_LL.DHID');
            SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
          end else
          begin
            if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)='M' then
            begin
              SQL.Add(',TPQty=(select sum(TPQty*(PBMF/100)*(PBKZ/1000)) from Contract_Cloth_LLMx A  ');
              sql.Add(' where A.DHID=Contract_Cloth_LL.DHID');
              SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
            end;
          end;
          if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)=Trim(Order_Main.FieldByName('DHUnit').AsString) then
          begin
            sql.Add(',BCPQty=(select Sum(Qty2) from Contract_Cloth_LLMx A  where A.DHID=Contract_Cloth_LL.DHID');
            SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
          end else
          begin
            if Trim(ClientDataSet3.FieldByName('TPUnit').AsString)='M' then
            begin
              SQL.Add(',BCPQty=(select sum(A.Qty2*(PBMF/100)*(PBKZ/1000)) from Contract_Cloth_LLMx A  ');
              sql.Add(' where A.DHID=Contract_Cloth_LL.DHID');
              SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
            end;
          end;
          sql.Add(', TPMQty=(select sum(TPMQty) from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.DHID');
          SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
          sql.Add(', BCPMQty=(select sum(MQty2)  from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.DHID');
          SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
          sql.Add('where DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
          SQL.Add(' and OrdSubId='''+Trim(ClientDataSet3.fieldbyname('OrdSubid').AsString)+'''');
          ExecSQL;
        end;
        Next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Cloth_LL where not exists(');
      sql.Add(' select * from Contract_Cloth_LLMX LM where LM.DHID=Contract_Cloth_LL.DHID  ');
      sql.Add(' and LM.OrdSubId=Contract_Cloth_LL.OrdSubId )');
      sql.Add(' and DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
      sql.Add(' and isnull(DHIDHelp,'''')='''' ');
      ExecSQL;
    end;
  {  if YFData()=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('更新缸费异常!','提示',0);
      Exit;
    end;   }
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;
function TfrmClothContractListLL.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou:String;
begin
  Result:=False;
 { with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where OrderNo='''+Trim(Order_Main.fieldbyname('ConNo').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin

    Application.MessageBox('订单未录入!','提示',0);
    Exit;
  end else
  begin
    if ADOQueryTemp.RecordCount>1 then
    begin
      
      Application.MessageBox('订单录入重复!','提示',0);
      Exit;
    end;
  end;  }
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where OrderNo='''+Trim(Order_Main.fieldbyname('ConNo').AsString)+'''');
    Open;
  end;
  OrdMainId:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select OrdDefStr2 from JYOrder_Main where Mainid='''+Trim(OrdMainId)+'''');
    Open;
  end;
  FComTaiTou:=Trim(ADOQueryTemp.fieldbyname('OrdDefStr2').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main.fieldbyname('FirstName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('FirstName').AsString);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(Order_Main.fieldbyname('FirstName').AsString)+'''');
    sql.Add(' and YFTypeId='''+Trim(Order_Main.fieldbyname('DHid').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin

    if GetLSNo(ADOQueryCmd,YFID,'GF','YF_Money_CR',3,1)=False then
    begin
      Application.MessageBox('取缸费应付最大号失败!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value:=Trim(YFID);
      FieldByName('YFTypeId').Value:=Trim(Order_Main.fieldbyname('DHid').AsString);
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('Filler').Value:=Trim(DName);
      FieldByName('CRType').Value:='应付款登记';
      FieldByName('CRFlag').Value:='应付付';
      FieldByName('QtyFlag').Value:=1;
      FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('FirstName').AsString);
      FieldByName('CRTime').Value:=ClientDataSet3.fieldbyname('TPDate').Value;
      FieldByName('YFType').Value:='自动生成';
      //FieldByName('Price').Value:=Order_Main.fieldbyname('Price').Value;
      FieldByName('HuiLv').Value:=1;
      FieldByName('BZType').Value:='￥';
      FieldByName('ComTaiTou').Value:='';
      FieldByName('QtyUnit').Value:='缸';
      FieldByName('YFName').Value:='缸费';
      FieldByName('MainId').Value:=Trim(OrdMainId);
      Post;
    end;
  end else
  begin
    YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set CRTime='''+Trim(ClientDataSet3.fieldbyname('TPDate').AsString)+'''');
      sql.Add(' where YFID='''+Trim(ADOQueryTemp.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update YF_Money_CR Set Qty=(select isnull(Count(*),0) from Contract_Cloth_LLMX A ');
    SQL.Add(' where A.DHId=YF_Money_CR.YFTypeId and A.GangFee>0)');
    sql.Add(',Money=(select isnull(sum(GangFee),0) from Contract_Cloth_LLMX A ');
    SQL.Add(' where A.DHId=YF_Money_CR.YFTypeId and A.GangFee>0)');
    sql.Add(',BBMoney=(select isnull(sum(GangFee),0) from Contract_Cloth_LLMX A ');
    SQL.Add(' where A.DHId=YF_Money_CR.YFTypeId and A.GangFee>0)');
    sql.Add(' where YFTypeId='''+Trim(Order_Main.fieldbyname('DHid').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFID='''+Trim(YFID)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    if ADOQueryTemp.FieldByName('Money').AsFloat=0 then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete YF_Money_CR where YFID='''+Trim(YFID)+'''');
        ExecSQL;
      end;
    end;
  end;
  Result:=True;
end;

procedure TfrmClothContractListLL.ToolButton3Click(Sender: TObject);
begin

  begin
    if ClientDataSet3.IsEmpty then Exit;
    if Trim(ClientDataSet3.fieldbyname('MXId').AsString)<>'' then
    begin
      if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add(' select * from  Contract_Cloth_LL where DHID='''+Trim(ClientDataSet3.fieldbyname('DHID').AsString)+'''');
      sql.Add(' and OrdSubId='''+Trim(ClientDataSet3.fieldbyname('OrdSubId').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.FieldByName('HCQty').AsFloat>0 then
    begin
      Application.MessageBox('已有回仓数据,不能删除!','提示',0);
      Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Cloth_LLMx where MXId='''+Trim(ClientDataSet3.fieldbyname('MXId').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update Contract_Cloth_DH Set TPQtyHZ=(select isnull(Sum(TPYZQty),0) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_DH.DHID)');
        sql.Add(',TPMQtyHZ=(select isnull(Sum(TPMQty),0) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_DH.DHID)');
        sql.Add(',TPPSHZ=(select isnull(Sum(TPPS),0) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_DH.DHID)');
        sql.Add(' where DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate Contract_Cloth_LL Set TPPS=(select isnull(sum(TPPS),0) from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.DHID');
        SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
        sql.Add(', TPQty=(select isnull(sum(TPYZQty),0) from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.DHID');
        SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
        sql.Add(', BCPQty=(select isnull(sum(YZQty2),0)  from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.DHID');
        SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
        sql.Add(', TPMQty=(select isnull(sum(TPMQty),0)  from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.DHID');
        SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
        sql.Add(', BCPMQty=(select isnull(sum(MQty2),0)  from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.DHID');
        SQL.Add(' and A.OrdSubId=Contract_Cloth_LL.OrdSubId)');
        sql.Add('where DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
        SQL.Add(' and OrdSubId='''+Trim(ClientDataSet3.fieldbyname('OrdSubid').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Cloth_LL where not exists(');
        sql.Add(' select * from Contract_Cloth_LLMX LM where LM.DHID=Contract_Cloth_LL.DHID  ');
        sql.Add(' and LM.OrdSubId=Contract_Cloth_LL.OrdSubId )');
        sql.Add(' and DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
        sql.Add(' and isnull(DHIDHelp,'''')='''' ');
        ExecSQL;
      end;
      if YFData()=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('更新缸费异常!','提示',0);
        Exit;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      ClientDataSet3.Delete;
    except;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('操作异常!','提示',0);
    end;

    {with Order_Main do
    begin
      Edit;
      if ClientDataSet3.IsEmpty=False then
        FieldByName('KCQty').Value:=Order_Main.fieldbyname('DHQty').Value-tv3.DataController.Summary.FooterSummaryValues[0]
      else
        FieldByName('KCQty').Value:=Order_Main.fieldbyname('DHQty').Value;
      Post;
    end; }
  end;
end;

procedure TfrmClothContractListLL.v2Column3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='RKPlace';
      flagname:='入库地点';
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          Edit;
          FieldByName('RKPlace').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractListLL.v3Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,fieldname,qty1,qty2:String;
begin
  If Trim(TcxTextEdit(Sender).EditingText)<>'' then
  begin
    mvalue:=TcxTextEdit(Sender).EditingText;
  end else
  begin
    mvalue:='0';
  end;
  fieldname:=Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=mvalue;
    Post;
  end;
  {with Order_Main do
  begin
    Edit;
    FieldByName('KCQty').Value:=Order_Main.fieldbyname('DHQty').Value-tv3.DataController.Summary.FooterSummaryValues[0];
    Post;
  end; }
  if Trim(ClientDataSet3.fieldbyname('TPQty').AsString)<>'' then
  begin
    qty1:=ClientDataSet3.fieldbyname('TPQty').AsString;
  end else
  begin
    qty1:='0';
  end;
  if Trim(ClientDataSet3.fieldbyname('Qty2').AsString)<>'' then
  begin
    Qty2:=ClientDataSet3.fieldbyname('Qty2').AsString;
  end else
  begin
    Qty2:='0';
  end;
  if StrToFloat(qty1)*StrToFloat(qty2)=0 then Exit;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName('qty3').Value:=(StrToFloat(qty1)-StrToFloat(qty2))*1.00*100/StrToFloat(qty1);
    Post;
  end;

end;

procedure TfrmClothContractListLL.ConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNo.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        //sql.Add('select ClothPS=A.DHPS-A.TPPSHZ,ClothQty=A.DHQty-A.TPQtyHZ,  ');
       // SQL.Add(' A.DHID,A.DHPS,A.TPPSHZ,A.TPQtyHZ,A.DHQty,A.DHUnit,A.FirstName,B.ConNo,C.C_CodeName,C.C_Spec,C.MFQty,C.KZQty,B.FactoryNoName ');
        sql.Add('select ClothPS=A.DHPS-A.TPPSHZ,ClothQty=A.DHQty-A.TPQtyHZ, A.DHUnit,SXMainId='''',SXDHID='''',A.MainId,A.SubId');
        SQL.Add(' ,A.DHID,A.DHPS,A.TPPSHZ,A.TPQtyHZ,A.DHQty,A.FirstName,B.ConNo,C.C_CodeName,C.C_Spec,C.MFQty,C.KZQty,B.FactoryNoName ');
        sql.Add(' from Contract_Cloth_DH A inner join Contract_Main B on A.Mainid=B.Mainid');
        sql.Add(' inner join Contract_Sub C on A.Subid=C.Subid');
        SQL.Add(' where B.ConNo like '''+'%'+Trim(ConNo.Text)+'%'+'''');
        sql.add(' and isnull(A.DHType,'''')=''''');
        sql.Add(' union all');

       // sql.Add('  select ClothPS=A.DHPS-A.TPPSHZ,ClothQty=A.DHQty-A.TPQtyHZ,  ');
        //SQL.Add(' A.DHID,A.DHPS,A.TPPSHZ,A.TPQtyHZ,A.DHQty,A.DHUnit,A.FirstName ');
        //sql.Add(',ConNo=(select Top 1 ConNo from ContractSX_Main SM where SM.MainId=A.SXMainId)');
        //sql.Add(',C_CodeName=A.PBName,C_Spec=A.PBSpec,MFQty=A.PBMF,KZQty=A.PBKZ');
        //SQL.Add(',FactoryNoName=(select Top 1 FirstName from ContractSX_Cloth_DH SD where SD.DHID=A.SXDHID)');
        sql.Add('  select ClothPS=A.DHPS-A.TPPSHZ,ClothQty=A.DHQty-A.TPQtyHZ,A.DHUnit,A.SXMainId,A.SXDHID,A.MainId,A.SubId  ');
        SQL.Add(' ,A.DHID,A.DHPS,A.TPPSHZ,A.TPQtyHZ,A.DHQty,A.FirstName ');
        sql.Add(',ConNo=(select Top 1 ConNo from ContractSX_Main SM where SM.MainId=A.SXMainId)');
        sql.Add(',C_CodeName=A.PBName,C_Spec=A.PBSpec,MFQty=A.PBMF,KZQty=A.PBKZ');
        SQL.Add(',FactoryNoName=(select Top 1 FirstName from ContractSX_Cloth_DH SD where SD.DHID=A.SXDHID)');
        sql.Add(' from Contract_Cloth_DH A ');
        SQL.Add(' where exists(select * from ContractSX_Main B where B.MainId=A.SXMainId');
        SQL.Add(' and B.ConNO like '''+'%'+Trim(ConNo.Text)+'%'+''')');
        sql.Add('  and isnull(A.DHType,'''')<>'''' ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmClothContractListLL.ToolButton1Click(Sender: TObject);
  var
    FColumn:String;
begin
  if ClientDataSet3.IsEmpty then Exit;
  ToolBar2.SetFocus;
  FColumn:=Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(FColumn)='OrderNo' then Exit;
  if Trim(FColumn)='PRTColor' then Exit;
  if Application.MessageBox('确定要进行一键替换吗？','提示',32+4)<>IDYES then Exit;
  OneKeyPost(Tv3,ClientDataSet3);
end;

procedure TfrmClothContractListLL.v3Column5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,fieldname,qty1,qty3:String;

begin
  If Trim(TcxTextEdit(Sender).EditingText)<>'' then
  begin
    mvalue:=TcxTextEdit(Sender).EditingText;
  end else
  begin
    mvalue:='0';
  end;
  fieldname:=Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=mvalue;
    Post;
  end;
  {with Order_Main do
  begin
    Edit;
    FieldByName('KCQty').Value:=Order_Main.fieldbyname('DHQty').Value-tv3.DataController.Summary.FooterSummaryValues[0];
    Post;
  end; }
  if Trim(ClientDataSet3.fieldbyname('TPQty').AsString)<>'' then
  begin
    qty1:=ClientDataSet3.fieldbyname('TPQty').AsString;
  end else
  begin
    qty1:='0';
  end;
  if Trim(ClientDataSet3.fieldbyname('qty3').AsString)<>'' then
  begin
    qty3:=ClientDataSet3.fieldbyname('qty3').AsString;
  end else
  begin
    qty3:='0';
  end;
  //if StrToFloat(qty1)*StrToFloat(qty2)=0 then Exit;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName('qty2').Value:=StrToFloat(qty1)*(1-StrToFloat(qty3)*1.00/100);
    Post;
  end;

end;

procedure TfrmClothContractListLL.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  if Order_Main.IsEmpty then exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,C.MPRTSpec,B.PRTColor,C.MPRTCodeName,C.OrderNo,B.SOrddefstr1,B.PRTHX,B.PRTOrderQty,B.OrderUnit, ');
    SQL.Add(' Case when B.OrderUnit=''M'' then Cast(dbo.F_Get_Order_MFKZ(A.MainId,''MF'') ');
    sql.Add(' *1.00/100*B.PRTOrderQty*dbo.F_Get_Order_MFKZ(A.MainId,''KZ'')/1000 as decimal(18,0))');
    SQL.Add(' when B.OrderUnit=''Y'' then Cast(dbo.F_Get_Order_MFKZ(A.MainId,''MF'')');
    sql.Add(' *1.00/100*B.PRTOrderQty*0.9144*dbo.F_Get_Order_MFKZ(A.MainId,''KZ'')/1000 As decimal(18,0)) ');
    sql.Add(' when B.OrderUnit=''Kg'' then B.PRTOrderQty   else Null end as PRTOrderKgQty');
    SQL.Add('from Contract_Cloth_LLMx 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.DHId='''+Trim(Order_Main.fieldbyname('DHId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
end;

end.
