unit U_ClothContractListDH;

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;

type
  TfrmClothContractListDH = 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;
    ConNoM: TEdit;
    Label5: TLabel;
    C_CodeNameM: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    C_Spec: TEdit;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1Qty1: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2ComeDate: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2MxQty: TcxGridDBColumn;
    v2MxNote: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v2Column1: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v2Column2: 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 ConNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure v2Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ConNoMKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function YFData():Boolean;
    function DelYFData():Boolean;
    procedure InitSubGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothContractListDH: TfrmClothContractListDH;

implementation
uses
  U_DataLink,U_ClothContractInPut,U_Fun,U_ProductOrderList,U_ZDYHelp,U_RTFun;

{$R *.dfm}

procedure TfrmClothContractListDH.FormDestroy(Sender: TObject);
begin
  frmClothContractListDH:=nil;
end;

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

procedure TfrmClothContractListDH.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('坯布合同订单dhLCK',Tv1,'指示单管理');
  WriteCxGrid('坯布合同订单dhrLCK',Tv2,'指示单管理');
end;

procedure TfrmClothContractListDH.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))+'''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothContractListDH.InitForm();
begin
  ReadCxGrid('坯布合同订单dhLCK',Tv1,'指示单管理');
  ReadCxGrid('坯布合同订单dhrLCK',Tv2,'指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

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

procedure TfrmClothContractListDH.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 TfrmClothContractListDH.FormShow(Sender: TObject);
begin
  InitForm();
end;
procedure TfrmClothContractListDH.v2Column3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add(' select * from JYOrder_Sub_AnPai where LLID in(');
    sql.Add('select B.LLID from Contract_Cloth_DH A inner join Contract_Cloth_LL B on A.DHID=B.DHID where A.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    sql.Add(' and A.Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
    sql.Add(' and A.FirstName='''+Trim(ClientDataSet2.fieldbyname('RKPlace').AsString)+''')');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已有回仓数据，不能更改存放地点!','提示',0);
    Exit;
  end;
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      //flag:='RKPlace';
      flag:='FactoryNo1Name';
      flagname:='入库地点';
      MainType:='染厂';
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          Edit;
          FieldByName('RKPlace').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
          FieldByName('RKPlCode').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractListDH.ConNoMKeyPress(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)+'%'+'''';
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;
procedure TfrmClothContractListDH.InitSubGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,KCSL=A.MXQty-(select isnull(Sum(TPQty),0) from Contract_Sub_Mxto B where B.MXID=A.MXID),');
    sql.Add(' KCPS=A.Qty1-(select isnull(Sum(Qty1),0) from Contract_Sub_Mxto B where B.MXID=A.MXID)');
    sql.Add('from Contract_Sub_Mx A');
    sql.Add(' where A.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;
procedure TfrmClothContractListDH.ToolButton10Click(Sender: TObject);
var
  maxno,MXMaxno,FMXID:string;
begin
  if ClientDataSet2.IsEmpty then Exit;
  if ClientDataSet2.Locate('ComeDate',null,[]) then
  begin
    Application.MessageBox('到货日期不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('RKPlace',null,[]) then
  begin
    Application.MessageBox('存放地点不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('RKPlace','',[]) then
  begin
    Application.MessageBox('存放地点不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('MxQty',null,[]) then
  begin
    Application.MessageBox('到货数量不能为空！','提示',0);
    Exit;
  end;
 { if ClientDataSet2.Locate('LCKNo',null,[]) then
  begin
    Application.MessageBox('流程卡号不能为空！','提示',0);
    Exit;
  end;  }
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    {with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Cloth_LLMX where DHID in(');
      sql.Add('select DHID from   Contract_Cloth_LLMX A ');
      sql.Add('where not exists(select *from Contract_Cloth_DH B where B.DHID=A.DHID)');
      sql.Add('and DHID  like ''DH%'')');
      ExecSQL;
    end;}
    //保存明细表
    with ClientDataSet2 do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Contract_Sub_Mx where MXID='''+Trim(ClientDataSet2.fieldbyname('MXID').AsString)+'''');
          Open;
        end;
        FMXID:=Trim(ADOQueryTemp.fieldbyname('MXID').AsString);
        if Trim(FMXID)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'MX','Contract_Sub_Mx',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取明细流水号失败！','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(FMXID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Sub_Mx where MxId='''+Trim(FMXID)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FMXID)='' then
            Append
          else
            Edit;
          FieldByName('MxId').Value:=Trim(maxno);
          FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,ClientDataSet2,'Contract_Sub_Mx',2);
          //SSetSaveDataCDSNew(ADOQueryCmd,Tv2,ClientDataSet2,'Contract_Sub_Mx',2);
          FieldByName('MXUnit').Value:=Trim(Order_Main.fieldbyname('C_Unit').AsString);
          FieldByName('RKPLCode').Value:=Trim(ClientDataSet2.fieldbyname('RKPLCode').AsString);
          if Trim(ClientDataSet2.fieldbyname('Qty1').AsString)='' then
          begin
            FieldByName('Qty1').Value:=0;
          end;
          if Trim(ClientDataSet2.fieldbyname('MxQty').AsString)='' then
          begin
            FieldByName('MxQty').Value:=0;
          end;
          if Trim(Order_Main.fieldbyname('C_Unit').AsString)='Kg' then
          begin
            FieldByName('MxMQty').Value:=ClientDataSet2.fieldbyname('MxQty').Value*1.00*1000
                                       /(Order_Main.fieldbyname('MFQty').Value*1.00/100*Order_Main.fieldbyname('KZQty').Value);
          end else
          begin
            FieldByName('MxMQty').Value:=ClientDataSet2.fieldbyname('MxQty').Value;
          end;
          Post;
        end;

        MXMaxno:=Trim(maxno);

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Cloth_DH where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          sql.Add(' and MXID='''+Trim(MXMaxno)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'DH','Contract_Cloth_DH',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_DH 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('DHId').Value:=Trim(maxno);
            FieldByName('FirstNo').Value:=Trim(ClientDataSet2.fieldbyname('RKPLCode').AsString);
            FieldByName('FirstName').Value:=Trim(ClientDataSet2.fieldbyname('RKPLace').AsString);
            FieldByName('DHUnit').Value:=Trim(Order_Main.fieldbyname('C_Unit').AsString);
            FieldByName('MXID').Value:=Trim(MXMaxno);
            Post;
          end;
        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Contract_Cloth_DH Set DHUnit='''+Trim(Order_Main.fieldbyname('C_Unit').AsString)+'''');
            sql.Add(' where DHID='''+Trim(ADOQueryTemp.fieldbyname('DHID').AsString)+'''');
            ExecSQL;
          end;
        end;
        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Contract_Cloth_DH where MXID='''+Trim(MXMaxno)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add(' Update Contract_Cloth_DH Set DHQty=(select MxQty from Contract_Sub_Mx A where A.MXId=Contract_Cloth_DH.MXId)');
            sql.Add(',DHMQty=(select MxMQty from Contract_Sub_Mx A where A.MXId=Contract_Cloth_DH.MXId)');
            sql.Add(', DHPS=(select Qty1 from Contract_Sub_Mx A where A.MXId=Contract_Cloth_DH.MXId)');
            sql.Add(' where MXID='''+Trim(MXMaxno)+'''');
            ExecSQL;
          end;
        end else
        begin
          //处理以前的数据
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Contract_Cloth_DH Set DHQty=(select Sum(MxQty) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
            sql.Add(' and A.RKPlace=Contract_Cloth_DH.FirstName)');
            sql.Add(',DHMQty=(select Sum(MxMQty) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
            sql.Add(' and A.RKPlace=Contract_Cloth_DH.FirstName)');
            sql.Add(', DHPS=(select Sum(Qty1) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
            sql.Add(' and A.RKPlace=Contract_Cloth_DH.FirstName)');
            sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
            sql.Add(' and FirstName='''+Trim(ClientDataSet2.fieldbyname('RKPlace').AsString)+'''');
            sql.Add(' and Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
            ExecSQL;
          end;
        end;

        if Trim(ClientDataSet2.fieldbyname('MXID').AsString)<>'' then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('delete Contract_Cloth_DH ');
            sql.Add('where not exists(select * from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId and A.RKPlace=Contract_Cloth_DH.firstName) ');
            sql.Add(' and SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
            sql.add(' and MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
            ExecSQL;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from Contract_Cloth_LLMX B where B.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
            sql.Add(' and B.Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
            sql.Add(' and  not exists(select * from Contract_Cloth_DH A where A.DHID=B.DHID ');
            SQL.Add(' and A.SubId=B.SubId ) and B.DHID not like'''+'LL%'+'''');
            Open;
          end;
          if ADOQueryTemp.IsEmpty=False then
          begin
            Application.MessageBox('产生的领料数据将找不到对应的染厂，不能保存!','提示',0);
            //begin
              ADOQueryCmd.Connection.RollbackTrans;
              Exit;
            //end;
          end;
        end;

        with ClientDataSet2 do
        begin
          Edit;
          FieldByName('MxId').Value:=Trim(MXMaxno);
          Post;
        end;
        YFData();
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where YFTypeID='''+Trim(MXMaxno)+'''');
          sql.Add(' and YFName=''坯布费用'' ' );
          Open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('生成应付款失败,请再次点击到坯确定!','提示',0);
          Exit;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmClothContractListDH.ToolButton8Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  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;
  ClientDataSet2.Append;
  ClientDataSet2.FieldByName('MXUnit').Value:=Trim(Order_Main.fieldbyname('C_Unit').AsString);
  ClientDataSet2.Post;
end;

procedure TfrmClothContractListDH.ToolButton9Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
    if Trim(ClientDataSet2.fieldbyname('MXID').AsString)<>'' then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_DH A inner join Contract_Cloth_LL B on A.DHID=B.DHID where A.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        sql.Add(' and A.Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
        sql.Add(' and A.FirstName='''+Trim(ClientDataSet2.fieldbyname('RKPlace').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        Application.MessageBox('已开卡，不能删除!','提示',0);
        Exit;
      end;
      if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
      try
        ADOQueryCmd.Connection.BeginTrans;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete Contract_Sub_Mx where MxId='''+Trim(ClientDataSet2.fieldbyname('MxId').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update Contract_Cloth_DH Set DHQty=(select isnull(Sum(MxQty),0) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
          sql.Add(' and A.RKPlace=Contract_Cloth_DH.FirstName)');
          sql.Add(',DHMQty=(select isnull(Sum(MxMQty),0) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
          sql.Add(' and A.RKPlace=Contract_Cloth_DH.FirstName)');
          sql.Add(', DHPS=(select isnull(Sum(Qty1),0) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
          sql.Add(' and A.RKPlace=Contract_Cloth_DH.FirstName)');
          sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
          sql.Add(' and FirstName='''+Trim(ClientDataSet2.fieldbyname('RKPlace').AsString)+'''');
          sql.Add(' and Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete Contract_Cloth_DH ');
          sql.Add('where not exists(select * from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId and A.RKPlace=Contract_Cloth_DH.firstName) ');
          sql.Add(' and SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          sql.add(' and MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Cloth_LLMX B where B.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          sql.Add(' and B.Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
          sql.Add(' and  not exists(select * from Contract_Cloth_DH A where A.DHID=B.DHID ');
          SQL.Add(' and A.SubId=B.SubId ) and B.DHID not like'''+'LL%'+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          Application.MessageBox('产生的领料数据将找不到对应的染厂，不能保存!','提示',0);
          ADOQueryCmd.Connection.RollbackTrans;
          Exit;
        end;
        DelYFData();
        ADOQueryCmd.Connection.CommitTrans;
      except
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('删除异常!','提示',0);
      end;
    end;
    ClientDataSet2.Delete;
end;

procedure TfrmClothContractListDH.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitSubGrid();
end;
function TfrmClothContractListDH.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName,FFCRID: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('FactoryNoName').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('FactoryNoName').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('FactoryNoName').AsString)+'''');
    sql.Add(' and YFTypeId='''+Trim(ClientDataSet2.fieldbyname('MXId').AsString)+'''');
    Open;
  end;
  begin
    if ADOQueryTemp.IsEmpty then
    begin
      if GetLSNo(ADOQueryCmd,YFID,'PF','YF_Money_CR',3,1)=False then
      begin
        Application.MessageBox('取坯布应付最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where YFID='''+Trim(YFID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if ADOQueryTemp.IsEmpty then
        Append
      else
        Edit;
      FieldByName('YFID').Value:=Trim(YFID);
      FieldByName('YFTypeId').Value:=Trim(ClientDataSet2.fieldbyname('MXId').AsString);
      FieldByName('ConSubId').Value:=Trim(ClientDataSet2.fieldbyname('SubId').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('FactoryNoName').AsString);
      FieldByName('CRTime').Value:=ClientDataSet2.fieldbyname('ComeDate').Value;
      FieldByName('Qty').Value:=ClientDataSet2.fieldbyname('MXQty').Value;
      FieldByName('Note').Value:=ClientDataSet2.fieldbyname('MxNote').Value;
      FieldByName('PS').Value:=ClientDataSet2.fieldbyname('Qty1').Value;
      FieldByName('YFType').Value:='自动生成';
      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('C_Unit').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 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;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where YFTypeId='''+Trim(ClientDataSet2.fieldbyname('MXId').AsString)+'''');
    sql.Add(' and CRID<>'+CRID);
    sql.Add(' and YFName=''坯布费用'' ');
    Open;
  end;
  FFCRID:=Trim(ADOQueryCmd.fieldbyname('CRID').AsString);
  if Trim(FCRID)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete YF_Money_CR where YFTypeId='''+Trim(ClientDataSet2.fieldbyname('MXId').AsString)+'''');
      sql.Add(' and CRID='+FFCRID);
      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='+FFCRID);
      ExecSQL;
    end;
  end;
  Result:=True;
end;
function TfrmClothContractListDH.DelYFData():Boolean;
var
  CRID,OrdMainId,YFID:String;
begin
  Result:=False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main.fieldbyname('FactoryNoName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end else
  begin
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete YF_Money_CR where ');
    sql.Add('  YFTypeId='''+Trim(ClientDataSet2.fieldbyname('MXId').AsString)+'''');
    sql.Add(' and YFID like '''+'PF%'+'''');
    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;

end.
