unit U_ClothContractList;

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
  TfrmClothContractList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBPrint: 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;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    C_Spec: TEdit;
    ScrollBox1: TScrollBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    v1C_Qty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1PriceUnit: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton1: TToolButton;
    ClientDataSet3: TClientDataSet;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    ClientDataSet2: TClientDataSet;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    v1Qty1: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBPRTColor: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2ComeDate: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2BatchNo: TcxGridDBColumn;
    v2Qty1: TcxGridDBColumn;
    v2MxQty: TcxGridDBColumn;
    v2MxNote: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    CDS_PRT: TClientDataSet;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Tchk: TToolButton;
    TNochk: TToolButton;
    v1DHQty: TcxGridDBColumn;
    v1MXQty: TcxGridDBColumn;
    FactoryNoName: TEdit;
    Label6: TLabel;
    v1Column1: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(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 Button4Click(Sender: TObject);
    procedure v3Column2PropertiesEditValueChanged(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TchkClick(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure SetStatus();
    { Private declarations }
  public
    FType:string;
    { Public declarations }
  end;

var
  frmClothContractList: TfrmClothContractList;

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

{$R *.dfm}

procedure TfrmClothContractList.SetStatus();
begin
  TBAdd.Visible:=false;
  tchk.Visible:=false;
  tnochk.Visible:=false;
  tbedit.Visible:=false;
  tbdel.Visible:=false;
  IF FType=''then
  begin
    case cxTabControl1.TabIndex of
    0:begin
       tchk.Visible:=true;
       tbedit.Visible:=true;
       tbdel.Visible:=true;
       TBAdd.Visible:=true;
      end;
    1:begin
        tnochk.Visible:=true;
      end;
    2:begin

      end;
    end;
  end;
  IF FType='查询' then
  begin
    Tbadd.Visible:=false;
    TBPrint.Visible:=false;
    v1Price.Visible:=false;
    v1PriceUnit.Visible:=false;
    v1Money.Visible:=false;

    v1Price.Hidden:=true;
    v1PriceUnit.Hidden:=true;
    v1Money.Hidden:=true;
    cxTabControl1.TabIndex:=2;
    cxTabControl1.Visible:=false;
  end;
end;

procedure TfrmClothContractList.FormDestroy(Sender: TObject);
begin
  frmClothContractList:=nil;
end;

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

procedure TfrmClothContractList.FormCreate(Sender: TObject);
begin
  ScrollBox1.Align:=alClient;
end;

procedure TfrmClothContractList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption+tv1.Name,Tv1,'指示单管理');
end;

procedure TfrmClothContractList.InitGrid();
var strwhere: string;
begin
  strwhere:=' and QDTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
              +' and QDTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+''''
              +' and HTType=''采购合同''';
  IF cxTabControl1.TabIndex=0 then
  strwhere:=strwhere+' and isnull(C_status,''0'')=''0'' ';
  IF cxTabControl1.TabIndex=1 then
  strwhere:=strwhere+' and isnull(C_status,''0'')=''1'' ';
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec ClothContract_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value:=strwhere;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmClothContractList.InitForm();
begin
  ReadCxGrid(self.Caption+tv1.Name,Tv1,'指示单管理');
  cxTabControl1.TabIndex:=0;
  if Trim(DParameters1)='1' then
  begin
    TBPrint.Visible:=False;
  end else
  begin
 //   v1Column1.Visible:=True;
 //   v1Column1.Hidden:=False;
 //   v1PRTUnit.Visible:=True;
 //   v1PRTUnit.Hidden:=False;
  //  v1PRTQty.Visible:=True;
  //  v1PRTQty.Hidden:=False;
    TBPrint.Visible:=True;
  end;
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmClothContractList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmClothContractList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmClothContractInPutPB:=TfrmClothContractInPutPB.Create(Application);
    with frmClothContractInPutPB do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FConNo:=Trim(Self.Order_Main.fieldbyname('ConNoM').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmClothContractInPutPB.Free;
  end;
end;

procedure TfrmClothContractList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if ClientDataSet2.IsEmpty=false then
  begin
    Application.MessageBox('已到坯，不能删除坯布合同！','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //TBRafresh.Click;
    //TBFind.Click;
    Order_Main.Delete;
  end;
end;

function TfrmClothContractList.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 TfrmClothContractList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('坯布合同订单列表',cxGrid1);
end;

procedure TfrmClothContractList.TBPrintClick(Sender: TObject);
var
   fPrintFile,FConNoM:string;

begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\坯布订购合同.rmf' ;
  with ADOQueryTemp 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))+'''';
    Parameters.ParamByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
    Parameters.ParamByName('WSql').Value:='';
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_PRT);
  SInitCDSData20(ADOQueryTemp,CDS_PRT);
  FConNoM:=Trim(CDS_PRT.fieldbyname('ConNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\坯布订购合同.rmf'),'提示',0);
  end;
  //SDofilter(ADOQueryMain,'');
  //SCreateCDS20(ADOQueryMain,Order_Main);
  //SInitCDSData20(ADOQueryMain,Order_Main);
  //Order_Main.Locate('ConNoM',FConNoM,[]);
  //SelPrintData(TV4,ADOQueryMain,'合同查询报表');
end;

procedure TfrmClothContractList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmClothContractList.TBAddClick(Sender: TObject);
begin
  try
    frmClothContractInPutPB:=TfrmClothContractInPutPB.Create(Application);
    with frmClothContractInPutPB do
    begin
      PState:=0;
      FMainId:='';
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmClothContractInPutPB.Free;
  end;
end;

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

procedure TfrmClothContractList.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if FInt=2 then
  begin
    ClientDataSet2.Append;
    ClientDataSet2.Post;
  end else
  if FInt=3 then
  begin
    //ClientDataSet3.Append;
    //ClientDataSet3.Post;
    try
      frmProductOrderList:=TfrmProductOrderList.Create(Application);
      with frmProductOrderList do
      begin
        FFInt:=2;
        FCloth:=1;
        frmProductOrderList.TBAdd.Visible:=False;
        frmProductOrderList.TBEdit.Visible:=False;
        frmProductOrderList.TBDel.Visible:=False;
        frmProductOrderList.TBExport.Visible:=False;
        frmProductOrderList.TBPrint.Visible:=False;
        frmProductOrderList.ToolButton1.Visible:=False;
        frmProductOrderList.ToolButton2.Visible:=False;
        frmProductOrderList.ToolButton3.Visible:=True;
        frmProductOrderList.v1Column4.Visible:=True;
        if ShowModal=1 then
        begin
          with frmProductOrderList.Order_Main do
          begin
            frmProductOrderList.Order_Main.First;
            while not frmProductOrderList.Order_Main.Eof do
            begin
              if frmProductOrderList.Order_Main.FieldByName('SSel').AsBoolean=True then
              begin
                with Self.ClientDataSet3 do
                begin
                  Self.ClientDataSet3.Append;
                  Self.ClientDataSet3.FieldByName('OrdSubId').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('SubId').AsString);
                  Self.ClientDataSet3.FieldByName('OrderNo').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('OrderNo').AsString);
                  Self.ClientDataSet3.FieldByName('MPRTCodeName').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('MPRTCodeName').AsString);
                  Self.ClientDataSet3.FieldByName('MPRTSpec').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('MPRTSpec').AsString);
                  Self.ClientDataSet3.FieldByName('PRTColor').Value:=Trim(frmProductOrderList.Order_Main.fieldbyname('PRTColor').AsString);
                end;
              end;
              frmProductOrderList.Order_Main.Next;
            end;
          end;
        end;
      end;
    finally
      frmProductOrderList.Free;
    end;
  end;
end;

procedure TfrmClothContractList.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,KCSL=A.MXQty-(select isnull(Sum(TPQty),0) from Contract_Sub_Mxto B where B.MXID=A.MXID),');
    sql.Add(' KCPS=A.Qty1-(select isnull(Sum(Qty1),0) from Contract_Sub_Mxto B where B.MXID=A.MXID)');
    sql.Add('from Contract_Sub_Mx A');
    sql.Add(' where A.SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
  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 TfrmClothContractList.Tv2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FInt:=2;
end;

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

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

procedure TfrmClothContractList.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('MxQty',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','Contract_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 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',0);
          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;
          Post;
        end;
        with ClientDataSet2 do
        begin
          Edit;
          FieldByName('MxId').Value:=Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功！','提示',0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmClothContractList.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 TfrmClothContractList.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 TfrmClothContractList.ToolButton3Click(Sender: TObject);
begin
  if FInt=2 then
  begin
    if ClientDataSet2.IsEmpty then Exit;
    if ClientDataSet3.IsEmpty=false then
    begin
      Application.MessageBox('已产生坯布领料数据，不能删除到坯记录！','提示',0);
      Exit;
    end;
    if Trim(ClientDataSet2.fieldbyname('MxId').AsString)<>'' then
    begin
      if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub_Mx where MxId='''+Trim(ClientDataSet2.fieldbyname('MxId').AsString)+'''');
      ExecSQL;
    end;
    ClientDataSet2.Delete;
  end else
  if FInt=3 then
  begin
    if ClientDataSet3.IsEmpty then Exit;
    if Trim(ClientDataSet3.fieldbyname('ToId').AsString)<>'' then
    begin
      if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub_MxTo where ToId='''+Trim(ClientDataSet3.fieldbyname('ToId').AsString)+'''');
      ExecSQL;
    end;
    ClientDataSet3.Delete;
    with ClientDataSet2 do
    begin
      Edit;
      if ClientDataSet3.IsEmpty=False then
        FieldByName('KCSL').Value:=ClientDataSet2.fieldbyname('MxQty').Value-tv3.DataController.Summary.FooterSummaryValues[0]
      else
        FieldByName('KCSL').Value:=ClientDataSet2.fieldbyname('MxQty').Value;
      Post;
    end;
  end;
end;

procedure TfrmClothContractList.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 TfrmClothContractList.Button1Click(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

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

procedure TfrmClothContractList.Button4Click(Sender: TObject);
begin
  ToolButton5.Click;
end;

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

begin
  If Trim(TcxTextEdit(Sender).EditingText)<>'' then
  begin
    mvalue:=TcxTextEdit(Sender).EditingText;
  end else
  begin
    mvalue:='0';
  end;
  fieldname:=Tv3.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=mvalue;
    Post;
  end;
  with ClientDataSet2 do
  begin
    Edit;
    FieldByName('KCSL').Value:=ClientDataSet2.fieldbyname('MxQty').Value-tv3.DataController.Summary.FooterSummaryValues[0];
    Post;
  end;
  if Trim(ClientDataSet3.fieldbyname('TPQty').AsString)<>'' then
  begin
    qty1:=ClientDataSet3.fieldbyname('TPQty').AsString;
  end else
  begin
    qty1:='0';
  end;
  if Trim(ClientDataSet3.fieldbyname('Qty2').AsString)<>'' then
  begin
    Qty2:=ClientDataSet3.fieldbyname('Qty2').AsString;
  end else
  begin
    Qty2:='0';
  end;
  if StrToFloat(qty1)*StrToFloat(qty2)=0 then Exit;
  with ClientDataSet3 do
  begin
    Edit;
    FieldByName('qty3').Value:=(StrToFloat(qty1)-StrToFloat(qty2))*1.00*100/StrToFloat(qty1);
    Post;
  end;
end;

procedure TfrmClothContractList.conPress(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 TfrmClothContractList.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  If Order_Main.IsEmpty then Exit;
  try
    frmClothContractInPutPB:=TfrmClothContractInPutPB.Create(Application);
    with frmClothContractInPutPB do
    begin
      PState:=1;
      ToolBar1.Enabled:=false;
      ToolBar2.Enabled:=false;
      Tv1.OptionsData.Editing:=false;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FConNo:=Trim(Self.Order_Main.fieldbyname('ConNoM').AsString);
      if ShowModal=1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmClothContractInPutPB.Free;
  end;
end;

procedure TfrmClothContractList.TchkClick(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 TfrmClothContractList.TNochkClick(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=''0'' ');
      sql.Add('where subID='+quotedstr(trim(Order_Main.fieldbyname('subID').AsString)));
      execsql;
    end;
    application.MessageBox('撤销成功！','提示信息');
    TBRafresh.Click;
  except
   application.MessageBox('撤销失败！','提示信息',0);
  end;
end;

procedure TfrmClothContractList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  TBRafresh.Click;
end;

procedure TfrmClothContractList.Tv1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if cxTabControl1.TabIndex=0 then
  begin
    with Order_Main do
    begin
      if (AViewInfo.GridRecord.Values[v1MXQty.Index]<
         AViewInfo.GridRecord.Values[v1C_Qty.Index]*0.97) or
         (AViewInfo.GridRecord.Values[v1MXQty.Index]>
         AViewInfo.GridRecord.Values[v1C_Qty.Index]*1.03) then
      begin
        ACanvas.Font.Color:=clRed;
      end;
      if AViewInfo.GridRecord.Values[v1DHQty.Index]<
         AViewInfo.GridRecord.Values[v1Qty1.Index] then
      begin
        ACanvas.Font.Color:=clRed;
      end;
    end;
  end;
end;

end.
