unit U_ClothContractListDHSXQJG;

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
  TfrmClothContractListDHSXQJG = 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_CodeNameM: 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;
    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;
    v2Column2: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v2Column1: 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 Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv3MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ToolButton4Click(Sender: TObject);
    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 Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    function YFData():Boolean;
    function DelYFData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothContractListDHSXQJG: TfrmClothContractListDHSXQJG;

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

{$R *.dfm}

procedure TfrmClothContractListDHSXQJG.FormDestroy(Sender: TObject);
begin
  frmClothContractListDHSXQJG:=nil;
end;

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

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

procedure TfrmClothContractListDHSXQJG.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select ClothQty=DHQty-TPQtyHZ,  ');
      sql.Add(' A.*,B.ConNo,C.C_CodeName,C.C_Spec,C.MFQty,C.KZQty,B.FactoryNoName from  ContractSX_Cloth_DH A ');
      sql.add(' inner join ContractSX_Main B on A.Mainid=B.Mainid');
      sql.Add('inner join ContractSX_Sub C on A.Subid=C.Subid');
      sql.Add(' where B.FillTime>=:begdate and B.Filltime<:enddate  and A.DHType=''前加工'' ');
      Parameters.ParamByName('begdate').Value:=BegDate.Date;
      Parameters.ParamByName('enddate').Value:=EndDate.Date+1;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  IF Order_Main.IsEmpty then exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*');
    sql.Add('from ContractSX_Sub_Mx A');
    sql.Add(' where A.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    sql.Add(' and A.DHType=''前到后'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

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

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

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

procedure TfrmClothContractListDHSXQJG.ToolButton2Click(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; }
  if ClientDataSet2.Active=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select Top 1 * from JYOrder_Main where OrderNo='''+Trim(Order_Main.fieldbyname('ConNo').AsString)+'''');
    Open;
  end;
  ClientDataSet2.Append;
  ClientDataSet2.FieldByName('MXUnit').Value:=Trim(Order_Main.fieldbyname('DHUnit').AsString);
  ClientDataSet2.Post;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1 * from ContractHZ_Main A inner join ContractHZ_Sub B on A.MainId=B.MainId');
    sql.Add(' where A.ConNo='''+Trim(Order_Main.fieldbyname('ConNo').AsString)+'''');
    sql.Add(' and A.ConType=''纱线前加工''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    with ClientDataSet2 do
    begin
      Edit;
      FieldByName('RKPlace').Value:=ADOQueryTemp.fieldbyname('FactoryNoName').Value;
      FieldByName('MXPrice').Value:=ADOQueryTemp.fieldbyname('Price').Value;
      Post;
    end;
  end;
end;

procedure TfrmClothContractListDHSXQJG.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*');
    sql.Add('from ContractSX_Sub_Mx A');
    sql.Add(' where A.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  {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 TfrmClothContractListDHSXQJG.Tv2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FInt:=2;
end;

procedure TfrmClothContractListDHSXQJG.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FInt:=1;
end;

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

procedure TfrmClothContractListDHSXQJG.ToolButton4Click(Sender: TObject);
var
  maxno: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('MXPrice',null,[]) then
  begin
    Application.MessageBox('染纱单价不能为空！','提示',0);
    Exit;
  end;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存明细表
    with ClientDataSet2 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('MXId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'MX','ContractSX_Sub_Mx',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取明细流水号失败！','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(ClientDataSet2.fieldbyname('MXId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from ContractSX_Sub_Mx where MxId='''+Trim(ClientDataSet2.fieldbyname('MXId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet2.fieldbyname('MXId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MxId').Value:=Trim(maxno);
          FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          SSetSaveDataCDSNew(ADOQueryCmd,Tv2,ClientDataSet2,'ContractSX_Sub_Mx',2);
          FieldByName('MXUnit').Value:=Trim(Order_Main.fieldbyname('DHUnit').AsString);
          FieldByName('RKPLCode').Value:=Trim(ClientDataSet2.fieldbyname('RKPlace').AsString);
          FieldByName('QJGDHID').Value:=Trim(Order_Main.fieldbyname('DHID').AsString);
          FieldByName('DHTYpe').Value:='前到后';
          if Trim(ClientDataSet2.fieldbyname('MxQty').AsString)='' then
          begin
            FieldByName('MxQty').Value:=0;
          end;
          if Trim(ClientDataSet2.fieldbyname('MxPrice').AsString)='' then
          begin
            FieldByName('MxPrice').Value:=0;
          end;
          Post;
        end;
        with ClientDataSet2 do
        begin
          Edit;
          FieldByName('MxId').Value:=Trim(maxno);
          Post;
        end;

        if YFData()=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Exit;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from ContractSX_Cloth_DH where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          sql.Add(' and FirstName='''+Trim(ClientDataSet2.fieldbyname('RKPlace').AsString)+'''');
          sql.Add(' and DHTYpe=''前到后'' ');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'SD','ContractSX_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 ContractSX_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('RKPLace').AsString);
            FieldByName('FirstName').Value:=Trim(ClientDataSet2.fieldbyname('RKPLace').AsString);
            FieldByName('DHUnit').Value:=Trim(Order_Main.fieldbyname('DHUnit').AsString);
            FieldByName('DHType').Value:='前到后';
            Post;
          end;

        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update ContractSX_Cloth_DH Set DHUnit='''+Trim(Order_Main.fieldbyname('DHUnit').AsString)+'''');
            sql.Add(' where DHID='''+Trim(ADOQueryTemp.fieldbyname('DHID').AsString)+'''');
            ExecSQL;
          end;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update ContractSX_Cloth_DH Set DHQty=(select Sum(MxQty) from ContractSX_Sub_Mx A where A.SubId=ContractSX_Cloth_DH.SubId');
          sql.Add(' and A.RKPLace=ContractSX_Cloth_DH.FirstName and A.DHTYpe=''前到后'' )');
          sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
          sql.Add(' and FirstName='''+Trim(ClientDataSet2.fieldbyname('RKPLace').AsString)+'''');
          sql.Add(' and DHType=''前到后'' ');
          ExecSQL;
        end;

        Next;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate ContractSX_Cloth_DH Set TPQtyHZ=(select sum(MXQty) from ContractSX_Sub_Mx A where A.QJGDHID=ContractSX_Cloth_DH.DHID)');
      sql.Add(' where DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ContractSX_Cloth_DH ');
      sql.Add('where not exists(select * from ContractSX_Sub_Mx A where A.SubId=ContractSX_Cloth_DH.SubId  ');
      SQL.Add('and A.RKPlace=ContractSX_Cloth_DH.firstName and A.DHType=''前到后'')');
      sql.Add(' and SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and firstName='''+Trim(ClientDataSet2.fieldbyname('RKPLace').AsString)+'''');
      sql.Add(' and DHType=''前到后''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Cloth_LLMX B where not exists(select * from ');
      sql.Add(' Contract_Cloth_DH A where A.DHID=B.DHID ');
      SQL.Add(' and A.SubId=B.SubId ) and B.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      Open;
    end;
    if ADOQueryCmd.IsEmpty=False then
    begin
      Application.MessageBox('产生的领料数据将找不到对应的染厂，不能保存!','提示',0);
      //begin
        ADOQueryCmd.Connection.RollbackTrans;
        Exit;
      //end;
    end;
    {with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete YFMoney');
      ExecSQL;
    end;}
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;
function TfrmClothContractListDHSXQJG.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou,FCRID,FFactoryName: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(ClientDataSet2.fieldbyname('MXId').AsString)+'''');
    Open;
  end;
  //if ADOQueryTemp.IsEmpty then
  begin
    if ADOQueryTemp.IsEmpty then
    begin
      if GetLSNo(ADOQueryCmd,YFID,'SQ','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('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:=ClientDataSet2.fieldbyname('ComeDate').Value;
      FieldByName('Qty').Value:=ClientDataSet2.fieldbyname('MXQty').Value;
      //FieldByName('PS').Value:=ClientDataSet2.fieldbyname('Qty1').Value;
      FieldByName('YFType').Value:='自动生成';
      if Trim(ClientDataSet2.fieldbyname('MXPrice').AsString)<>'' then
        FieldByName('Price').Value:=ClientDataSet2.fieldbyname('MXPrice').Value
      else
        FieldByName('Price').Value:=0;
      FieldByName('HuiLv').Value:=1;
      FieldByName('BZType').Value:='￥';
      FieldByName('ComTaiTou').Value:='';
      FieldByName('QtyUnit').Value:=Trim(Order_Main.fieldbyname('DHUnit').AsString);
      //FieldByName('YFName').Value:='纱线前加工费';
      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(Sum(MXQty),0) from Contract_Sub_MX A inner join');
    SQL.Add('Contract_Sub B on A.SubId=B.SubId where B.MainId=YF_Money_CR.YFTypeId)');
    sql.Add(',PS=(select isnull(Sum(A.Qty1),0) from Contract_Sub_MX A inner join');
    SQL.Add('Contract_Sub B on A.SubId=B.SubId where B.MainId=YF_Money_CR.YFTypeId)');
    sql.Add(' where YFTypeId='''+Trim(Order_Main.fieldbyname('Mainid').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 TfrmClothContractListDHSXQJG.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 FactoryName='''+Trim(Order_Main.fieldbyname('FactoryNoName').AsString)+'''');
    sql.Add(' and YFTypeId='''+Trim(ClientDataSet2.fieldbyname('MXId').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;
  Result:=True;
end;
procedure TfrmClothContractListDHSXQJG.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 TfrmClothContractListDHSXQJG.ToolButton5Click(Sender: TObject);
var
  maxno:string;
begin
  if ClientDataSet3.IsEmpty then Exit;
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(ClientDataSet2.fieldbyname('MXId').AsString)='' then
  begin
    Application.MessageBox('到货数量未确定！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('OrderNo',null,[]) then
  begin
    Application.MessageBox('指定订单编号不能为空！','提示',0);
    Exit;
  end;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存去向表
    with ClientDataSet3 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet3.fieldbyname('ToId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'To','Contract_Sub_MxTo',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取去向流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(ClientDataSet3.fieldbyname('ToId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Sub_MxTo where ToId='''+Trim(ClientDataSet3.fieldbyname('ToId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet3.fieldbyname('ToId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MxId').Value:=Trim(ClientDataSet2.fieldbyname('MxId').AsString);
          FieldByName('ToId').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('TPQty').AsString)<>'' then
            FieldByName('TPQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value
          else
            FieldByName('TPQty').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;
          Post;
        end;
        with ClientDataSet3 do
        begin
          Edit;
          FieldByName('ToId').Value:=Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmClothContractListDHSXQJG.ToolButton3Click(Sender: TObject);
begin
  //if FInt=2 then
  //begin
    if ClientDataSet2.IsEmpty then Exit;
    if Trim(ClientDataSet2.FieldByName('MxId').AsString)='' then
    begin
      ClientDataSet2.Delete;
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete ContractSX_Sub_Mx where MxId='''+Trim(ClientDataSet2.fieldbyname('MxId').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update ContractSX_Cloth_DH Set DHQty=(select isnull(Sum(MxQty),0) from ContractSX_Sub_Mx A where A.SubId=ContractSX_Cloth_DH.SubId');
        sql.Add(' and A.RKPLace=ContractSX_Cloth_DH.FirstName)');
        sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
        sql.Add(' and FirstNo='''+Trim(ClientDataSet2.fieldbyname('RKPLCode').AsString)+'''');
        ExecSQL;
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete ContractSX_Cloth_DH ');
        sql.Add('where not exists(select * from ContractSX_Sub_Mx A where A.SubId=ContractSX_Cloth_DH.SubId and A.RKPlace=ContractSX_Cloth_DH.firstName) ');
        sql.Add(' and SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ContractSX_Cloth_LLMX B where not exists(select * from ');
        sql.Add(' ContractSX_Cloth_DH A where A.DHID=B.DHID ');
        SQL.Add(' and A.SubId=B.SubId ) and B.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        Open;
      end;
      if ADOQueryCmd.IsEmpty=False then
      begin
        Application.MessageBox('已产生的数据，不能删除!','提示',0);
        //begin
          ADOQueryCmd.Connection.RollbackTrans;
          Exit;
        //end;
      end;
      DelYFData();
      ClientDataSet2.Delete;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;
  //end;
end;

procedure TfrmClothContractListDHSXQJG.v2Column3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      //flag:='RKPlace';
      flag:='Factory';
      flagname:='入库地点';
      MainType:='PBFactory';
      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 TfrmClothContractListDHSXQJG.Button1Click(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmClothContractListDHSXQJG.Button2Click(Sender: TObject);
begin
  ToolButton3.Click;
end;

procedure TfrmClothContractListDHSXQJG.Button3Click(Sender: TObject);
begin
  ToolButton4.Click;
end;

procedure TfrmClothContractListDHSXQJG.ConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNo.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select ClothQty=DHQty-TPQtyHZ,  ');
        sql.Add(' A.*,B.ConNo,C.C_CodeName,C.C_Spec,C.MFQty,C.KZQty,B.FactoryNoName from  ContractSX_Cloth_DH A ');
        sql.add(' inner join ContractSX_Main B on A.Mainid=B.Mainid');
        sql.Add('inner join ContractSX_Sub C on A.Subid=C.Subid');
        sql.Add(' where B.ConNO like '''+'%'+Trim(ConNo.Text)+'%'+'''  and A.DHType=''前加工'' ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmClothContractListDHSXQJG.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*');
    sql.Add('from ContractSX_Sub_Mx A');
    sql.Add(' where A.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    sql.Add(' and A.DHType=''前到后'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

end.
