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

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;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ClientDataSet3: TClientDataSet;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    ClientDataSet2: TClientDataSet;
    ToolButton5: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: 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;
    v2Column2: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    sdefstr6: TEdit;
    Label6: TLabel;
    v1DHQty: TcxGridDBColumn;
    v1MxQty: TcxGridDBColumn;
    TWC: TToolButton;
    TNOWC: TToolButton;
    v1Column9: TcxGridDBColumn;
    v1Column10: 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 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 ConNoMKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TWCClick(Sender: TObject);
    procedure TNOWCClick(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    function  DelData():Boolean;
    function YFData():Boolean;
    function DelYFData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothContractListDH: TfrmClothContractListDH;

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

{$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('坯布合同订单1',Tv1,'指示单管理');
  WriteCxGrid('坯布合同订单2',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))+''''
                                           +' and HTType=''采购合同''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothContractListDH.InitForm();
begin
  ReadCxGrid('坯布合同订单1',Tv1,'指示单管理');
  ReadCxGrid('坯布合同订单2',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;

function TfrmClothContractListDH.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 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.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;  }
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select Top 1 * from Contract_Main where ConNo='''+Trim(Order_Main.fieldbyname('ConNo').AsString)+'''');
    Open;
  end;
  ClientDataSet2.Append;
  ClientDataSet2.FieldByName('MXUnit').Value:=Trim(Order_Main.fieldbyname('C_Unit').AsString);
  ClientDataSet2.FieldByName('RKPlace').Value:='';
  ClientDataSet2.FieldByName('RKPLCode').Value:=Trim(ADOQueryTemp.fieldbyname('JHPlace').AsString);
  ClientDataSet2.Post;
end;

procedure TfrmClothContractListDH.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);
end;

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

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

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

procedure TfrmClothContractListDH.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;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存明细表
    with ClientDataSet2 do
    begin
      ClientDataSet2.DisableControls;
      First;
      while not eof do
      begin
        if Trim(ClientDataSet2.fieldbyname('MXId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'MX','Contract_Sub_Mx',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet2.EnableControls;
            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 Contract_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,'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
            if (Order_Main.fieldbyname('MFQty').AsFloat<>0) and (Order_Main.fieldbyname('KZQty').AsFloat<>0) 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;
          end
          else
          begin
            FieldByName('MxMQty').Value:=ClientDataSet2.fieldbyname('MxQty').Value;
          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 Contract_Cloth_DH where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          sql.Add(' and FirstName='''+Trim(ClientDataSet2.fieldbyname('RKPLace').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'DH','Contract_Cloth_DH',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            ClientDataSet2.EnableControls;
            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('PBName').Value:=Trim(Order_Main.fieldbyname('C_CodeName').AsString);
            FieldByName('PBSpec').Value:=Trim(Order_Main.fieldbyname('C_Spec').AsString);
            FieldByName('PBMF').Value:=Order_Main.fieldbyname('MFQty').AsFloat;
            FieldByName('PBKZ').Value:=Order_Main.fieldbyname('KZQty').AsFloat;
            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(',PBName='+quotedstr(Trim(Order_Main.fieldbyname('C_CodeName').AsString)));
            sql.Add(',PBSpec='+quotedstr(Trim(Order_Main.fieldbyname('C_Spec').AsString)));
            sql.Add(',PBMF='+floattostr(Order_Main.fieldbyname('MFQty').AsFloat));
            sql.Add(',PBKZ='+floattostr(Order_Main.fieldbyname('KZQty').AsFloat));
            sql.Add(' where DHID='''+Trim(ADOQueryTemp.fieldbyname('DHID').AsString)+'''');
            ExecSQL;
          end;
        end;
        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.RKPLCode=Contract_Cloth_DH.FirstNo)');
          sql.Add(',DHMQty=(select Sum(MxMQty) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
          sql.Add(' and A.RKPLCode=Contract_Cloth_DH.FirstNo)');
          sql.Add(', DHPS=(select Sum(Qty1) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
          sql.Add(' and A.RKPLCode=Contract_Cloth_DH.FirstNo)');
          sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
          sql.Add(' and FirstNo='''+Trim(ClientDataSet2.fieldbyname('RKPLCode').AsString)+'''');
          ExecSQL;
        end;
        Next;
      end;
    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)+'''');
      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)+'''');
      sql.Add(' select A.DHID from Contract_Cloth_LLMx A ');
      sql.Add(' inner join Contract_Cloth_LL B on B.DHID=A.DHID and B.OrdSubId=A.OrdSubId and B.LLID not like ''HL%'' ');
      sql.Add(' where not exists (select X.DHID from Contract_Cloth_DH X where X.DHId=A.DHID and X.FirstName=B.FirstName) ');
      sql.Add(' and A.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+''' ');
      Open;
    end;
    if not ADOQueryCmd.IsEmpty then
    begin
      ClientDataSet2.EnableControls;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('产生的领料数据将找不到对应的染厂，不能保存!','提示',0);
      //begin

      Exit;
      //end;
    end;
    ClientDataSet2.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
  except
    ClientDataSet2.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;
function TfrmClothContractListDH.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('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('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('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:='';
      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 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 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 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 TfrmClothContractListDH.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 TfrmClothContractListDH.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 TfrmClothContractListDH.ToolButton3Click(Sender: TObject);
begin
  //if FInt=2 then
  //begin
    if ClientDataSet2.IsEmpty then Exit;
    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.RKPLCode=Contract_Cloth_DH.FirstNo)');
        sql.Add(', DHMQty=(select isnull(Sum(MxMQty),0) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
        sql.Add(' and A.RKPLCode=Contract_Cloth_DH.FirstNo)');
        sql.Add(', DHPS=(select isnull(Sum(Qty1),0) from Contract_Sub_Mx A where A.SubId=Contract_Cloth_DH.SubId');
        sql.Add(' and A.RKPLCode=Contract_Cloth_DH.FirstNo)');
        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 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)+'''');
        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);
        ADOQueryCmd.Connection.RollbackTrans;
        Exit;
      end;
      DelYFData();
      ClientDataSet2.Delete;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;
end;

procedure TfrmClothContractListDH.v2Column3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
 { try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='Factory';
      flagname:='入库地点';
      //MainType:='RanFactory';
      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('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end; }

 try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        with ClientDataSet2 do
        begin
          Edit;
         // FieldByName('FactoryNoName').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
          FieldByName('RKPlace').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
          FieldByName('RKPlCode').Value:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmClothContractListDH.Button1Click(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmClothContractListDH.Button3Click(Sender: TObject);
begin
  ToolButton4.Click;
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)+'%'+''''
         +' and HTType=''采购合同''';
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmClothContractListDH.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 Contract_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 TfrmClothContractListDH.TWCClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Contract_Sub SET C_status=''2'' ');
      sql.Add('where subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      execsql;
    end;
    application.MessageBox('完成！','提示信息');
    TBRafresh.Click;
  except
   application.MessageBox('完成失败！','提示信息',0);
  end;
end;

procedure TfrmClothContractListDH.TNOWCClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then exit;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update Contract_Sub SET C_status=''1'' ');
      sql.Add('where subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      execsql;
    end;
    application.MessageBox('撤销成功！','提示信息');
    TBRafresh.Click;
  except
   application.MessageBox('撤销失败！','提示信息',0);
  end;
end;

procedure TfrmClothContractListDH.SetStatus();
begin

end;

procedure TfrmClothContractListDH.Tv1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  {with Order_Main do
  begin
    if (AViewInfo.GridRecord.Values[v1MXQty.Index]<
       AViewInfo.GridRecord.Values[v1PRTOrderQty.Index]*0.97) or
       (AViewInfo.GridRecord.Values[v1MXQty.Index]>
       AViewInfo.GridRecord.Values[v1PRTOrderQty.Index]*1.03) then
    begin
      ACanvas.Font.Color:=clRed;
    end;
    if AViewInfo.GridRecord.Values[v1DHQty.Index]<
       AViewInfo.GridRecord.Values[v1Column1.Index] then
    begin
      ACanvas.Font.Color:=clRed;
    end;
  end;}
end;

end.
