unit U_ClothContractListWJG;

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;

type
  TfrmClothContractListWJG = 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;
    v3Column6: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    ToolBar2: TToolBar;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    ToolButton11: TToolButton;
    v1Column5: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label6: TLabel;
    FactoryNoName: TEdit;
    Label7: TLabel;
    FirstName: TEdit;
    v3Column1: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    v1LLidx: TcxGridDBColumn;
    v3JGType: TcxGridDBColumn;
    v1JGType: TcxGridDBColumn;
    v1gangNO: TcxGridDBColumn;
    v3gangNO: 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 Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure v3Column10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridWhere(fsj:string);
    function  YFData():Boolean;
    function  YFDataDel():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothContractListWJG: TfrmClothContractListWJG;

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

{$R *.dfm}

procedure TfrmClothContractListWJG.FormDestroy(Sender: TObject);
begin
  frmClothContractListWJG:=nil;
end;

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

procedure TfrmClothContractListWJG.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('后加工1',Tv1,'指示单管理');
  WriteCxGrid('后加工2',Tv3,'指示单管理');
end;

procedure TfrmClothContractListWJG.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add(' select A.*,B.ConNo,D.gangNO,C.C_CodeName,B.FactoryNoName,C.C_Spec,C.MFQty,C.KZQty,C.MainId,C.SubId,A.tpunit as dhUnit ');
      sql.Add(',Price=(select Top 1 JGPrice from Contract_Cloth_LLMX AAA where AAA.DHID=A.LLId )');
    //  sql.Add(',JGType=(select Top 1 JGType from Contract_Cloth_LLMX AAA where AAA.DHID=A.LLId )');
      sql.Add(',KCPS=A.TPPS-A.HCPS,KCQty=A.BCPQty-A.HCQty');
      sql.Add(',APFlag=(select Count(*) from JYOrder_Sub_AnPai JAP where JAP.LLID=A.LLID)');
      sql.Add(' from Contract_Cloth_LL A ');
      sql.Add(' inner join Contract_Cloth_LLMX D on D.MXID=A.LLID');
      sql.Add(' inner join Contract_Main B on D.Mainid=B.Mainid');
      sql.Add(' inner join Contract_Sub C on D.Subid=C.Subid');
      sql.Add(' where D.JGType<>''退货出库'' ');
      sql.Add(' and D.TPDate>=:begdate and D.TPDate<:enddate ');
      Parameters.ParamByName('begdate').Value:=BegDate.Date;
      Parameters.ParamByName('enddate').Value:=EndDate.Date+1;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  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 ');
    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('LLId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
  finally
    ADOQueryMain.EnableControls;


  end;
end;
procedure TfrmClothContractListWJG.InitGridWhere(fsj:string);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add(' select A.*,B.ConNo,C.C_CodeName,B.FactoryNoName,C.C_Spec,C.MFQty,C.KZQty,C.MainId,D.DHUnit,C.SubId ');
      sql.Add(' ,JM.OrderNo,JS.PRTColor');
      sql.Add(',Price=(select Top 1 JGPrice from Contract_Cloth_LLMX AAA where AAA.DHID=A.DHID )');
      sql.Add(',KCPS=TPPS-HCPS,KCQty=BCPQty-HCQty');
      sql.Add(',APFlag=(select Count(*) from JYOrder_Sub_AnPai JAP where JAP.LLID=A.LLID)');
      sql.Add(' from Contract_Cloth_LL A ');
      sql.Add(' inner join Contract_Cloth_DH D on A.DHID=D.DHID ');
      sql.Add(' inner join Contract_Main B on D.Mainid=B.Mainid');
      sql.Add(' inner join Contract_Sub C on D.Subid=C.Subid');
      sql.Add(' inner join JYOrder_Sub JS on A.OrdSubId=JS.SubId ');
      sql.Add(' inner join JYOrder_Main JM on JS.Mainid=JM.MainId');
      //sql.Add(' where isnull(DHIDHelp,'''')='''' ');
      sql.Add(' where 1=1');
      sql.Add(fsj);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothContractListWJG.InitForm();
begin
  
  ReadCxGrid('后加工1',Tv1,'指示单管理');
  ReadCxGrid('后加工2',Tv3,'指示单管理');
  EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
  InitGrid();
end;

procedure TfrmClothContractListWJG.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 TfrmClothContractListWJG.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 TfrmClothContractListWJG.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmClothContractListWJG.ToolButton2Click(Sender: TObject);
begin
  with ClientDataSet3 do
  begin
    Append;
//    FieldByName('OrderNo').Value:=Trim(Order_Main.fieldbyname('OrderNo').AsString);
//    FieldByName('PRTColor').Value:=Trim(Order_Main.fieldbyname('PRTColor').AsString);
    FieldByName('TPDate').Value:=SGetServerDate(ADOQueryTemp);
    FieldByName('TPUnit').Value:=Self.Order_Main.FieldByName('DHUnit').Value;
    FieldByName('JGType').Value:='正常加工';
    FieldByName('Qty2').Value:=0;
    Post;
  end;
end;

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

procedure TfrmClothContractListWJG.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 TfrmClothContractListWJG.ToolButton5Click(Sender: TObject);
var
  maxno,LLIdx,FDW,FFLLID:string;
begin
  if ClientDataSet3.IsEmpty then Exit;
 { if ClientDataSet3.Locate('OrderNo',null,[]) then
  begin
    Application.MessageBox('指定订单编号不能为空！','提示',0);
    Exit;
  end;    }
  if ClientDataSet3.Locate('ToName',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;
        if trim(ClientDataSet3.fieldbyname('JGTYpe').AsString)='' then
        begin
          ClientDataSet3.Edit;
          ClientDataSet3.fieldbyname('JGTYpe').AsString:=trim('正常加工');
          ClientDataSet3.Post;
        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('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(Order_Main.fieldbyname('OrdSubId').AsString);
          FieldByName('TPDate').Value:=ClientDataSet3.fieldbyname('TPDate').Value;
          FieldByName('TPPerson').Value:=ClientDataSet3.fieldbyname('TPPerson').Value;
          FieldByName('TPNote').Value:=ClientDataSet3.fieldbyname('TPNote').Value;
          FieldByName('JGTYpe').Value:=ClientDataSet3.fieldbyname('JGTYpe').Value;
           FieldByName('gangNo').Value:=ClientDataSet3.fieldbyname('gangNo').Value;
          if ClientDataSet3.FieldByName('HCFlag').AsBoolean=True then
          begin
            FieldByName('HCFlag').Value:=1
          end else
          begin
            FieldByName('HCFlag').Value:=0;
          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;
          FieldByName('ToNo').Value:=ClientDataSet3.fieldbyname('ToNo').Value;
          FieldByName('ToName').Value:=ClientDataSet3.fieldbyname('ToName').Value;
          if Trim(ClientDataSet3.fieldbyname('TPQty').AsString)<>'' then
            FieldByName('TPQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value
          else
            FieldByName('TPQty').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('TPPS').AsString)<>'' then
            FieldByName('TPPS').Value:=ClientDataSet3.fieldbyname('TPPS').Value
          else
            FieldByName('TPPS').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('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;
          Post;
        end;
        with ClientDataSet3 do
        begin
          Edit;
          FieldByName('MXId').Value:=Trim(maxno);
          Post;
        end;
        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Contract_Cloth_LL where DHIdHelp='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
          sql.Add(' and firstName='''+Trim(ClientDataSet3.fieldbyname('ToName').AsString)+'''');
          sql.Add(' and JGType='''+Trim(ClientDataSet3.fieldbyname('JGType').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
        {  if GetLSNo(ADOQueryCmd,FFLLID,'HL','Contract_Cloth_LL',2,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('DHID').Value:=Trim(Order_Main.fieldbyname('DHID').AsString);
            FieldByName('OrdSubId').Value:=Trim(Order_Main.fieldbyname('OrdSubId').AsString);
            FieldByName('LLId').Value:=Trim(maxNo);
            FieldByName('DHIdHelp').Value:=Trim(Order_Main.fieldbyname('LLID').AsString);
            FieldByName('llidx').Value:=Order_Main.fieldbyname('llidx').AsInteger+1;
            FieldByName('FirstNo').Value:=Trim(ClientDataSet3.fieldbyname('ToNo').AsString);
            FieldByName('FirstName').Value:=Trim(ClientDataSet3.fieldbyname('ToName').AsString);
            FieldByName('TPUnit').Value:=Trim(ClientDataSet3.fieldbyname('TPUnit').AsString);
            FieldByName('JGType').Value:=Trim(ClientDataSet3.fieldbyname('JGType').AsString);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            SQL.Clear;
            sql.Add('Update Contract_Cloth_LL Set JXJGFlag=1 ');
            sql.Add(',ToNo='''+Trim(ClientDataSet3.fieldbyname('ToNo').AsString)+'''');
            sql.Add(',ToName='''+Trim(ClientDataSet3.fieldbyname('ToName').AsString)+'''');
            sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
            ExecSQL;
          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;
     {   if Order_Main.FieldByName('APFlag').Value=0 then
        begin
          if YFData()=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('更新染费失败!','提示',0);
            Exit;
          end;
        end else
        begin
          if YFDataDel()=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('更新染费失败!','提示',0);
            Exit;
          end;
        end;  }
        Next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update Contract_Cloth_LL Set TPMQty=(select Sum(TPMQty) from Contract_Cloth_LLMx A where A.MXID=Contract_Cloth_LL.LLID)');
   //   SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
      sql.Add(',TPPS=(select Sum(TPPS) from Contract_Cloth_LLMx A where A.MXID=Contract_Cloth_LL.LLID)');
    //  SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
      sql.Add(',TPQty=(select Sum(TPQty) from Contract_Cloth_LLMx A where A.MXID=Contract_Cloth_LL.LLID)');
    //  SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
      sql.Add(',BCPQty=(select Sum(Qty2) from Contract_Cloth_LLMx A where A.MXID=Contract_Cloth_LL.LLID)');
    //  SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
      sql.Add(',BCPMQty=(select Sum(MQty2) from Contract_Cloth_LLMx A where A.MXID=Contract_Cloth_LL.LLID)');
    //  SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
      sql.Add(' where LLID='''+Trim(ClientDataSet3.fieldbyname('MXID').AsString)+'''');
      ExecSQL;
    end;
    if Order_Main.FieldByName('APFlag').Value=0 then
    begin

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate Contract_Cloth_LL Set HCPS=(select isnull(Sum(TPPS),0) from Contract_Cloth_LL A where A.dhidhelp=Contract_Cloth_LL.LLID)');
        sql.Add(', HCQty=(select isnull(Sum(TPQty),0) from Contract_Cloth_LL A where A.dhidhelp=Contract_Cloth_LL.LLID)');
        sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate Contract_Cloth_LL Set HCMQty=HCQty*(TPMQty*1.00/TPQty)');
        sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
        ExecSQL;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;                
    Application.MessageBox('操作异常！','提示',0);
  end;
end;
function TfrmClothContractListWJG.YFData():Boolean;
var
  CRID,OrdMainId,YFID,Price,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(ClientDataSet3.fieldbyname('MXID').AsString)+'''');
    Open;
  end;
  YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  //if ADOQueryTemp.IsEmpty then
  begin
    if Trim(YFID)='' then
    begin
      if GetLSNo(ADOQueryCmd,YFID,'RF','YF_Money_CR',3,1)=False then
      begin
        Application.MessageBox('取染费应付最大号失败!','提示',0);
        Exit;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(YFID)='' then
        Append
      else
        Edit;
      FieldByName('YFID').Value:=Trim(YFID);
      FieldByName('YFTypeId').Value:=Trim(ClientDataSet3.fieldbyname('MXID').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;
      if Trim(ClientDataSet3.fieldbyname('TPQty').AsString)<>'' then
        FieldByName('Qty').Value:=ClientDataSet3.fieldbyname('TPQty').Value
      else
        FieldByName('Qty').Value:=0;
      if Trim(ClientDataSet3.fieldbyname('TPPS').AsString)<>'' then
        FieldByName('PS').Value:=ClientDataSet3.fieldbyname('TPPS').Value
      else
        FieldByName('PS').Value:=0;
      FieldByName('YFType').Value:='自动生成';
      if Trim(YFID)='' then
        FieldByName('Price').Value:=Order_Main.fieldbyname('Price').Value;
      FieldByName('HuiLv').Value:=1;
      FieldByName('BZType').Value:='人民币';
      FieldByName('ComTaiTou').Value:=Trim(FComTaiTou);
      FieldByName('QtyUnit').Value:=Trim(Order_Main.fieldbyname('TPUnit').AsString);
      FieldByName('YFName').Value:='染费';
      FieldByName('MainId').Value:=Trim(OrdMainId);
      Post;
    end;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update YF_Money_CR Set Qty=(select isnull(HCQty,0) from Contract_Cloth_LL A ');
    SQL.Add(' where A.LLId=YF_Money_CR.YFTypeId )');
    sql.Add(',PS=(select isnull(HCPS,0) from Contract_Cloth_LL A ');
    SQL.Add(' where A.LLId=YF_Money_CR.YFTypeId )');
    sql.Add(' where YFTypeId='''+Trim(Order_Main.fieldbyname('LLid').AsString)+'''');
    ExecSQL;
  end; }
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    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;
  Result:=True;
end;
function TfrmClothContractListWJG.YFDataDel():Boolean;
var
  CRID,OrdMainId,YFID,Price,FComTaiTou:String;
begin
  Result:=False;
  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
    Exit;
  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(ClientDataSet3.fieldbyname('MXID').AsString)+'''');
    Open;
  end;
  YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete YF_Money_CR ');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    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;
  Result:=True;
end;

procedure TfrmClothContractListWJG.ToolButton3Click(Sender: TObject);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Cloth_LL where DHIDHelp='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
    SQL.Add(' and FirstName='''+Trim(ClientDataSet3.fieldbyname('ToName').AsString)+'''');
    sql.Add(' and HCPS>0 ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已有回仓数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    begin
      if ClientDataSet3.IsEmpty then Exit;
      if Trim(ClientDataSet3.fieldbyname('MXId').AsString)<>'' then
      begin
        if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Exit;
        end;
      end;
      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_LL Set TPMQty=(select isnull(Sum(TPMQty),0) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_LL.DHIDHelp');
        SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
        sql.Add(',TPPS=(select isnull(Sum(TPPS),0) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_LL.DHIDHelp');
        SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
        sql.Add(',TPQty=(select isnull(Sum(TPQty),0) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_LL.DHIDHelp');
        SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
        sql.Add(',BCPQty=(select isnull(Sum(Qty2),0) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_LL.DHIDHelp');
        SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
        sql.Add(',BCPMQty=(select isnull(Sum(MQty2),0) from Contract_Cloth_LLMx A where A.DHID=Contract_Cloth_LL.DHIDHelp');
        SQL.Add('                                       and A.ToName=Contract_Cloth_LL.FirstName)');
        sql.Add(' where DHIDHelp='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
        ExecSQL;
      end;
      if Order_Main.FieldByName('APFlag').Value=0 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Contract_Cloth_LL Set HCPS=(select isnull(Sum(TPPS),0) from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.LLID)');
          sql.Add(', HCQty=(select isnull(Sum(TPQty),0) from Contract_Cloth_LLMX A where A.DHID=Contract_Cloth_LL.LLID)');
          sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate Contract_Cloth_LL Set HCMQty=HCQty*(TPMQty*1.00/TPQty)');
          sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
          ExecSQL;
        end;
      end;

      ClientDataSet3.Delete;
    end;
    if ClientDataSet3.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Cloth_LL where DHIDHelp='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
        sql.Add('Update Contract_Cloth_LL Set ToNo='''' ,ToName='''' ,JXJGFlag=0 ');
        sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
        ExecSQL;
      end;
    end;
    if YFDataDel()=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('更新染费失败!','提示',0);
      Exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);

  end;
end;

procedure TfrmClothContractListWJG.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 TfrmClothContractListWJG.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 TfrmClothContractListWJG.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: 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 ');
    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('LLId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);

end;

procedure TfrmClothContractListWJG.v3Column10PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      //flag:='RKPlace';
      flagname:='加工厂';
      flag:='FactoryNo1Name';
      MainType:='染厂';
      if ShowModal=1 then
      begin
        ClientDataSet3.Edit;
        ClientDataSet3.FieldByName('ToName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        ClientDataSet3.FieldByName('ToNo').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractListWJG.ConNoKeyPress(Sender: TObject;
  var Key: Char);
var
  fsj:string;
begin
  if Length(ConNo.Text)<3 then Exit;
  if Key=#13 then
  begin
    fsj:=' and B.ConNo like'''+'%'+Trim(ConNo.Text)+'%'+'''';
    InitGridWhere(fsj);
  end;
end;

end.
