unit U_ClothContractListLLSX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxDropDownEdit, cxCheckBox;

type
  TfrmClothContractListLLSX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNo: TEdit;
    Label5: TLabel;
    C_CodeName: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    C_Spec: TEdit;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ClientDataSet3: TClientDataSet;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    ClientDataSet2: TClientDataSet;
    ToolButton5: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column6: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column65: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column3: TcxGridDBColumn;
    ToolBar2: TToolBar;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    ToolButton11: TToolButton;
    v1Column5: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label6: TLabel;
    FactoryNoName: TEdit;
    Label7: TLabel;
    FirstName: TEdit;
    v3Column1: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v3Column7: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    v3Column12: TcxGridDBColumn;
    v3Column13: TcxGridDBColumn;
    v3Column14: 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 Tv2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Tv2CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v3Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure v3Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v3Column14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    function  YFData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmClothContractListLLSX: TfrmClothContractListLLSX;

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

{$R *.dfm}

procedure TfrmClothContractListLLSX.FormDestroy(Sender: TObject);
begin
  frmClothContractListLLSX:=nil;
end;

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

procedure TfrmClothContractListLLSX.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('AA坯布合同订单ll1SX',Tv1,'指示单管理');
  WriteCxGrid('AA坯布合同订单llr1SX',Tv3,'指示单管理');
end;

procedure TfrmClothContractListLLSX.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=''前到后'' ');
      sql.Add(' where B.FillTime>=:begdate and B.Filltime<:enddate   ');
      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_Cloth_LLMx A ');
    sql.Add('where A.DHId='''+Trim(Order_Main.fieldbyname('DHId').AsString)+'''');
    //ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
end;

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

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

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

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

procedure TfrmClothContractListLLSX.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 TfrmClothContractListLLSX.ToolButton5Click(Sender: TObject);
var
  maxno,LLIdx,FDW:string;
begin
  if ClientDataSet3.IsEmpty then Exit;
  ToolBar1.SetFocus;
  if ClientDataSet3.Locate('PBName',null,[]) then
  begin
    Application.MessageBox('坯布名称不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('SXPS',null,[]) then
  begin
    Application.MessageBox('匹数不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('SXQty',null,[]) then
  begin
    Application.MessageBox('数量不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('JGPrice',null,[]) then
  begin
    Application.MessageBox('加工单价不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('TPQty',null,[]) then
  begin
    Application.MessageBox('纱线数量不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('SXUnit',null,[]) then
  begin
    Application.MessageBox('数量单位不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('PBMF',null,[]) then
  begin
    Application.MessageBox('门幅不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet3.Locate('PBKZ',null,[]) then
  begin
    Application.MessageBox('克重不能为空！','提示',0);
    Exit;
  end;
  ClientDataSet3.DisableControls;
  with ClientDataSet3 do
  begin
    First;
    while not Eof do
    begin

      if Trim(FDW)='' then
      begin
        FDW:=Trim(ClientDataSet3.fieldbyname('SXUnit').AsString);
      end;
      if FDW<>Trim(ClientDataSet3.fieldbyname('SXUnit').AsString) then
      begin
        Application.MessageBox('数量单位不一致!','提示',0);
        Exit;
      end;
      Next;
    end;
  end;
 { with ClientDataSet3 do
  begin
    First;
    while not Eof do
    begin

      if Trim(FDW)='' then
      begin
        FDW:=Trim(ClientDataSet3.fieldbyname('SXUnit').AsString);
      end;
      if FDW<>Trim(ClientDataSet3.fieldbyname('SXUnit').AsString) then
      begin
        Application.MessageBox('数量单位不一致!','提示',0);
        Exit;
      end;
      Next;
    end;
  end;}
  ClientDataSet3.EnableControls;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存去向表
    with ClientDataSet3 do
    begin
      First;
      while not eof do
      begin
        if Trim(ClientDataSet3.fieldbyname('MXId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'MX','ContractSX_Cloth_LLMx',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取去向流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(ClientDataSet3.fieldbyname('MXId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from ContractSX_Cloth_LLMx where MXId='''+Trim(ClientDataSet3.fieldbyname('MXId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(ClientDataSet3.fieldbyname('MXId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
          FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          FieldByName('DHId').Value:=Trim(Order_Main.fieldbyname('DHId').AsString);
          FieldByName('MXId').Value:=Trim(maxno);
          FieldByName('TPDate').Value:=ClientDataSet3.fieldbyname('TPDate').Value;
          FieldByName('TPNote').Value:=ClientDataSet3.fieldbyname('TPNote').Value;
          FieldByName('TPUnit').Value:='Kg';
          FieldByName('SXUnit').Value:=ClientDataSet3.fieldbyname('SXUnit').Value;
          FieldByName('PBName').Value:=ClientDataSet3.fieldbyname('PBName').Value;
          FieldByName('PBSpec').Value:=ClientDataSet3.fieldbyname('PBSpec').Value;
          FieldByName('PBMF').Value:=ClientDataSet3.fieldbyname('PBMF').Value;
          FieldByName('PBKZ').Value:=ClientDataSet3.fieldbyname('PBKZ').Value;
          FieldByName('ToName').Value:=ClientDataSet3.fieldbyname('ToName').Value;
          if Trim(ClientDataSet3.fieldbyname('SXUnit').AsString)='Kg' then
          begin
            FieldByName('SXMQty').Value:=ClientDataSet3.FieldByName('SXQty').Value*1.00
                                         /(ClientDataSet3.FieldByName('PBKZ').Value/1000)
                                         /(ClientDataSet3.FieldByName('PBMF').Value/100)
          end else
          begin
            FieldByName('SXMQty').Value:=ClientDataSet3.FieldByName('SXQty').Value;
          end;

          if Trim(ClientDataSet3.fieldbyname('TPQty').AsString)<>'' then
            FieldByName('TPQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value
          else
            FieldByName('TPQty').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('JGPrice').AsString)<>'' then
            FieldByName('JGPrice').Value:=ClientDataSet3.fieldbyname('JGPrice').Value
          else
            FieldByName('JGPrice').Value:=0;

          if Trim(ClientDataSet3.fieldbyname('SXPS').AsString)<>'' then
            FieldByName('SXPS').Value:=ClientDataSet3.fieldbyname('SXPS').Value
          else
            FieldByName('SXPS').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('SXQty').AsString)<>'' then
            FieldByName('SXQty').Value:=ClientDataSet3.fieldbyname('SXQty').Value
          else
            FieldByName('SXQty').Value:=0;
          if Trim(ClientDataSet3.fieldbyname('ZSXS').AsString)<>'' then
            FieldByName('ZSXS').Value:=ClientDataSet3.fieldbyname('ZSXS').Value
          else
            FieldByName('ZSXS').Value:=0;
          FieldByName('TPYZQty').Value:=ClientDataSet3.fieldbyname('TPQty').Value;
          Post;
        end;
        with ClientDataSet3 do
        begin
          Edit;
          FieldByName('MXId').Value:=Trim(maxno);
          Post;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' Update ContractSX_Cloth_DH Set ');
          sql.Add(' TPQtyHZ=(select Sum(TPQty) from ContractSX_Cloth_LLMx A where A.DHID=ContractSX_Cloth_DH.DHID)');
          sql.Add(' where DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Cloth_DH where SXDHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
          sql.Add(' and FirstName='''+Trim(ClientDataSet3.fieldbyname('ToName').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'SD','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('DHID').Value:=Trim(maxno);
            FieldByName('SXDHID').Value:=Trim(Order_Main.fieldbyname('DHID').AsString);
            FieldByName('SXMainID').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
            FieldByName('FirstNo').Value:=Trim(ClientDataSet3.fieldbyname('ToName').AsString);
            FieldByName('FirstName').Value:=Trim(ClientDataSet3.fieldbyname('ToName').AsString);
            FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
            FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
            FieldByName('DHUnit').Value:=Trim(ClientDataSet3.fieldbyname('SXUnit').AsString);
            FieldByName('PBName').Value:=Trim(ClientDataSet3.fieldbyname('PBName').AsString);
            FieldByName('PBSpec').Value:=Trim(ClientDataSet3.fieldbyname('PBSpec').AsString);
            FieldByName('PBKZ').Value:=Trim(ClientDataSet3.fieldbyname('PBKZ').AsString);
            FieldByName('PBMF').Value:=Trim(ClientDataSet3.fieldbyname('PBMF').AsString);
            FieldByName('DHType').Value:='纱线加工';
            Post;
          end;

        end else
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update Contract_Cloth_DH Set DHUnit='''+Trim(ClientDataSet3.fieldbyname('SXUnit').AsString)+'''');
            sql.Add(' ,PBName='''+Trim(ClientDataSet3.fieldbyname('PBName').AsString)+'''');
            sql.Add(' ,PBSpec='''+Trim(ClientDataSet3.fieldbyname('PBSpec').AsString)+'''');
            sql.Add(' ,PBMF='+Trim(ClientDataSet3.fieldbyname('PBMF').AsString));
            sql.Add(' ,PBKZ='+Trim(ClientDataSet3.fieldbyname('PBKZ').AsString));
            sql.Add(' ,SXMainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
            sql.add(',DHType=''纱线加工'' ');
            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 DHPS=(select isnull(sum(SXPS),0) from ContractSX_Cloth_LLMX A where A.DHID=Contract_Cloth_DH.SXDHID');
          SQL.Add(' and A.ToName=Contract_Cloth_DH.FirstName)');
          sql.Add(',SXTPQty=(select isnull(Sum(TPQty),0) from ContractSX_Cloth_LLMx A  where A.DHID=Contract_Cloth_DH.SXDHID');
          SQL.Add(' and A.ToName=Contract_Cloth_DH.FirstName)');
          sql.Add(',DHQty=(select isnull(Sum(SXQty),0) from ContractSX_Cloth_LLMx A  where A.DHID=Contract_Cloth_DH.SXDHID');
          SQL.Add(' and A.ToName=Contract_Cloth_DH.FirstName)');
          SQL.Add(',DHMQty=(select isnull(sum(SXMQty),0) from ContractSX_Cloth_LLMx A  ');
          SQL.Add('  where A.DHID=Contract_Cloth_DH.SXDHID');
          SQL.Add(' and A.ToName=Contract_Cloth_DH.FirstName)');
          sql.Add('where SXDHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
          SQL.Add(' and FirstName='''+Trim(ClientDataSet3.fieldbyname('ToName').AsString)+'''');
          ExecSQL;
        end;
        Next;
      end;
    end;
    if YFData()=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('更新加工费异常!','提示',0);
      Exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;
function TfrmClothContractListLLSX.YFData():Boolean;
var
  CRID,OrdMainId,YFID,FComTaiTou:String;
begin
  Result:=False;
{  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where OrderNo='''+Trim(Order_Main.fieldbyname('ConNo').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin

    Application.MessageBox('订单未录入!','提示',0);
    Exit;
  end else
  begin
    if ADOQueryTemp.RecordCount>1 then
    begin
      
      Application.MessageBox('订单录入重复!','提示',0);
      Exit;
    end;
  end;  }
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main where OrderNo='''+Trim(Order_Main.fieldbyname('ConNo').AsString)+'''');
    Open;
  end;
  OrdMainId:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select OrdDefStr2 from JYOrder_Main where Mainid='''+Trim(OrdMainId)+'''');
    Open;
  end;
  FComTaiTou:=Trim(ADOQueryTemp.fieldbyname('OrdDefStr2').AsString);

  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main.fieldbyname('FirstName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value:=StrToInt(CRID);
      FieldByName('FactoryName').Value:=Trim(Order_Main.fieldbyname('FirstName').AsString);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(Order_Main.fieldbyname('FirstName').AsString)+'''');
    sql.Add(' and YFTypeId='''+Trim(Order_Main.fieldbyname('DHid').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin

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

procedure TfrmClothContractListLLSX.ToolButton3Click(Sender: TObject);
begin

  begin
    if ClientDataSet3.IsEmpty then Exit;
    if Trim(ClientDataSet3.fieldbyname('MXId').AsString)<>'' then
    begin
      if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete ContractSX_Cloth_LLMx where MXId='''+Trim(ClientDataSet3.fieldbyname('MXId').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update ContractSX_Cloth_DH Set TPQtyHZ=(select isnull(Sum(TPYZQty),0) from ContractSX_Cloth_LLMx A where A.DHID=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('UPdate Contract_Cloth_DH Set DHPS=(select isnull(sum(SXPS),0) from ContractSX_Cloth_LLMX A where A.DHID=Contract_Cloth_DH.SXDHID');
        SQL.Add(' and A.ToName=Contract_Cloth_DH.FirstName)');
        sql.Add(', DHQty=(select isnull(sum(SXQty),0) from ContractSX_Cloth_LLMX A where A.DHID=Contract_Cloth_DH.SXDHID');
        SQL.Add(' and A.ToName=Contract_Cloth_DH.FirstName)');
        sql.Add(', DHMQty=(select isnull(sum(SXMQty),0)  from ContractSX_Cloth_LLMX A where A.DHID=Contract_Cloth_DH.SXDHID');
        SQL.Add(' and A.ToName=Contract_Cloth_DH.FirstName)');
        sql.Add('where SXDHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
        SQL.Add(' and FirstName='''+Trim(ClientDataSet3.fieldbyname('ToName').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Contract_Cloth_DH where not exists(');
        sql.Add(' select * from ContractSX_Cloth_LLMX LM where LM.DHID=Contract_Cloth_DH.SXDHID  ');
        sql.Add(' and LM.ToName=Contract_Cloth_DH.FirstName )');
        sql.Add(' and SXDHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''');
        SQL.Add(' and FirstName='''+Trim(ClientDataSet3.fieldbyname('ToName').AsString)+'''');
        ExecSQL;
      end;
      if YFData()=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('更新坯布加工费异常!','提示',0);
        Exit;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      ClientDataSet3.Delete;
    except;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('操作异常!','提示',0);
    end;

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

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

procedure TfrmClothContractListLLSX.v3Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,fieldname,qty1,qty2,qty3:String;

begin
  If Trim(TcxTextEdit(Sender).EditingText)<>'' then
  begin
    mvalue:=TcxTextEdit(Sender).EditingText;
  end else
  begin
    mvalue:='0';
  end;
  fieldname:=Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=mvalue;
    Post;
  end;
  if Trim(ClientDataSet3.fieldbyname('ZSXS').AsString)<>'' then
  begin
    Qty3:=ClientDataSet3.fieldbyname('ZSXS').AsString;
  end else
  begin
    Qty3:='0';
  end;
  if Trim(ClientDataSet3.fieldbyname('SXQty').AsString)<>'' then
  begin
    Qty1:=ClientDataSet3.fieldbyname('SXQty').AsString;
  end else
  begin
    Qty1:='0';
  end;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName('TPQty').Value:=StrToFloat(qty1)*StrToFloat(qty3);
    Post;
  end;

end;

procedure TfrmClothContractListLLSX.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if Order_Main.IsEmpty then exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* ');
    SQL.Add('from ContractSX_Cloth_LLMx A ');
    sql.Add('where A.DHId='''+Trim(Order_Main.fieldbyname('DHId').AsString)+'''');
    //ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);

end;

procedure TfrmClothContractListLLSX.ConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNo.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select 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 TfrmClothContractListLLSX.ToolButton1Click(Sender: TObject);
  var
    FColumn:String;
begin
  if ClientDataSet3.IsEmpty then Exit;
  ToolBar2.SetFocus;
  FColumn:=Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(FColumn)='OrderNo' then Exit;
  if Trim(FColumn)='PRTColor' then Exit;
  if Application.MessageBox('确定要进行一键替换吗？','提示',32+4)<>IDYES then Exit;
  OneKeyPost(Tv3,ClientDataSet3);
end;

procedure TfrmClothContractListLLSX.ToolButton8Click(Sender: TObject);
begin

  IF Order_Main.IsEmpty then exit;
  if ClientDataSet3.IsEmpty then
  begin

    with ClientDataSet3 do
    begin
      Append;
      FieldByName('TPDate').Value:=SGetServerDate(ADOQueryTemp);
      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;
      FieldByName('ToName').Value:=Trim(ADOQueryTemp.fieldbyname('RanFactory').AsString);
      Post;
    end;
  end else
  begin
    CopyAddRow(Tv3,ClientDataSet3);
    with ClientDataSet3 do
    begin
      Edit;
      FieldByName('MXID').Value:='';
      FieldByName('TPDate').Value:=null;
      FieldByName('SXPS').Value:=null;
      FieldByName('SXQty').Value:=null;
      FieldByName('TPQty').Value:=null;
      Post;
    end;
  end;

end;

procedure TfrmClothContractListLLSX.v3Column7PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='Cloth';
      flagname:='坯布名称';
      if ShowModal=1 then
      begin
        Self.ClientDataSet3.Edit;
        Self.ClientDataSet3.FieldByName('PBName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        //Self.ClientDataSet3.FieldByName('C_Code').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractListLLSX.v3Column14PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='Factory';
      flagname:='染厂';
      MainType:='RanFactory';
      if ShowModal=1 then
      begin
        Self.ClientDataSet3.Edit;
        self.ClientDataSet3.FieldByName('ToName').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
