unit U_ClothContractListHZ;

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, cxLookAndFeels, cxLookAndFeelPainters,
  dxBarBuiltInMenu, cxNavigator;

type
  TfrmClothContractListHZ = 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;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxTabSheet3: TcxTabSheet;
    cxTabSheet4: TcxTabSheet;
    cxTabSheet5: TcxTabSheet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn38: TcxGridDBColumn;
    cxGridDBColumn39: TcxGridDBColumn;
    cxGridDBColumn40: TcxGridDBColumn;
    cxGridDBColumn41: TcxGridDBColumn;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridDBColumn43: TcxGridDBColumn;
    cxGridDBColumn44: TcxGridDBColumn;
    cxGridDBColumn45: TcxGridDBColumn;
    cxGridDBColumn46: TcxGridDBColumn;
    cxGridDBColumn47: TcxGridDBColumn;
    cxGridDBColumn48: TcxGridDBColumn;
    cxGridDBColumn49: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxTabSheet6: TcxTabSheet;
    cxGrid6: TcxGrid;
    Tv6: TcxGridDBTableView;
    cxGridDBColumn50: TcxGridDBColumn;
    cxGridDBColumn51: TcxGridDBColumn;
    cxGridDBColumn52: TcxGridDBColumn;
    cxGridDBColumn53: TcxGridDBColumn;
    cxGridDBColumn54: TcxGridDBColumn;
    cxGridDBColumn55: TcxGridDBColumn;
    cxGridDBColumn56: TcxGridDBColumn;
    cxGridDBColumn57: TcxGridDBColumn;
    cxGridDBColumn58: TcxGridDBColumn;
    cxGridDBColumn59: TcxGridDBColumn;
    cxGridDBColumn60: TcxGridDBColumn;
    cxGridDBColumn61: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1FactoryNo1Name: TcxGridDBColumn;
    v1PRTSpec: TcxGridDBColumn;
    v1Qty1: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTUnit: TcxGridDBColumn;
    v1PRTQty: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton2: TToolButton;
    v1Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    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 conPress(Sender: TObject; var Key: Char);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    { Public declarations }                        
  end;

var
  frmClothContractListHZ: TfrmClothContractListHZ;

implementation
uses
  U_DataLink,U_ClothContractInPutSX,U_Fun,U_ProductOrderList,U_ZDYHelp,U_ClothContractInPutSXMX
  ,U_ClothContractInPutHZ,U_ClothContractInPutPB, U_FjList_RZ;

{$R *.dfm}

procedure TfrmClothContractListHZ.FormDestroy(Sender: TObject);
begin
  frmClothContractListHZ:=nil;
end;

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

procedure TfrmClothContractListHZ.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('坯布合同订单列表SX',Tv1,'指示单管理');
  WriteCxGrid('纱线加工合同',Tv2,'指示单管理');
  WriteCxGrid('坯布采购合同',Tv3,'指示单管理');
  WriteCxGrid('染色合同',Tv4,'指示单管理');
  WriteCxGrid('后加工合同',Tv5,'指示单管理');
end;

procedure TfrmClothContractListHZ.InitGrid();
begin
  if cxPageControl1.ActivePageIndex=0 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec ClothContractSX_QryList :MainId,:WSql');
        Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                             +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''';
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select A.*,B.*,ConNoM=A.ConNo,Money=(B.Price*B.C_Qty) from ContractHZ_Main A inner join ContractHZ_Sub B on A.Mainid=B.MainId ');
        sql.Add(' where  A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
        SQL.Add(' and A.ConType=''纱线前加工'' ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select A.*,B.*,ConNoM=A.ConNo,Money=(B.Price*B.C_Qty) from ContractHZ_Main A inner join ContractHZ_Sub B on A.Mainid=B.MainId ');
        sql.Add(' where  A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
        SQL.Add(' and A.ConType=''纱线加工'' ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end else
  if cxPageControl1.ActivePageIndex=3 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('exec ClothContract_QryList :MainId,:WSql');
        Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                             +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''';
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end else
  if cxPageControl1.ActivePageIndex=4 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select A.*,B.*,ConNoM=A.ConNo,Money=(B.Price*B.C_Qty) from ContractHZ_Main A inner join ContractHZ_Sub B on A.Mainid=B.MainId ');
        sql.Add(' where  A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
        SQL.Add(' and A.ConType=''染色加工'' ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end else
  if cxPageControl1.ActivePageIndex=5 then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select A.*,B.*,ConNoM=A.ConNo,Money=(B.Price*B.C_Qty) from ContractHZ_Main A inner join ContractHZ_Sub B on A.Mainid=B.MainId ');
        sql.Add(' where  A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
        SQL.Add(' and A.ConType=''后加工'' ');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;

end;

procedure TfrmClothContractListHZ.InitForm();
begin
  ReadCxGrid('坯布合同订单列表SX',Tv1,'指示单管理');
  ReadCxGrid('纱线加工合同',Tv2,'指示单管理');
  ReadCxGrid('坯布采购合同',Tv3,'指示单管理');
  ReadCxGrid('染色合同',Tv4,'指示单管理');
  ReadCxGrid('后加工合同',Tv5,'指示单管理');
  if Trim(DParameters1)='1' then
  begin
    TBPrint.Visible:=False;
    v1Column1.Visible:=False;
    v1Column1.Hidden:=True;
    v1PRTUnit.Visible:=False;
    v1PRTUnit.Hidden:=True;
    v1PRTQty.Visible:=False;
    v1PRTQty.Hidden:=True;
  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 TfrmClothContractListHZ.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 TfrmClothContractListHZ.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    try
      frmClothContractInPutSXMX:=TfrmClothContractInPutSXMX.Create(Application);
      with frmClothContractInPutSXMX 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

        end;
      end;
    finally
      frmClothContractInPutSXMX.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
        CPFlag:='ClothSX';
        CPFlagName:='纱线名称';
        Caption:='染纱合同';
        FactoryFlag:='PBFactory';
        v1Column3.Caption:='加工厂';
        v1Price.Caption:='加工单价';
        FConType:='纱线前加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
        CPFlag:='ClothSX';
        CPFlagName:='纱线名称';
        Caption:='织造合同';
        FactoryFlag:='PBFactory';
        v1Column3.Caption:='加工厂';
        v1Price.Caption:='加工单价';
        FConType:='纱线加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=3 then
  begin
    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

        end;
      end;
    finally
      frmClothContractInPutPB.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=4 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
        CPFlag:='Cloth';
        CPFlagName:='坯布名称';
        Caption:='染色加工合同';
        FactoryFlag:='RanFactory';
        v1Column3.Caption:='染厂';
        v1Price.Caption:='染色单价';
        FConType:='染色加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=5 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
        CPFlag:='Cloth';
        CPFlagName:='坯布名称';
        Caption:='后加工合同';
        FactoryFlag:='HZLFactory';
        v1Column3.Caption:='加工厂';
        v1Price.Caption:='加工单价';
        FConType:='后加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end;

end;

procedure TfrmClothContractListHZ.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;

  if cxPageControl1.ActivePageIndex=0 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ContractSX_Sub_MX B inner join Contract_Sub C on B.SubId=C.SubId ');
      sql.Add(' where C.Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.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 else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ContractHZ_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ContractHZ_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 ContractHZ_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    Order_Main.Delete;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ContractHZ_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ContractHZ_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 ContractHZ_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    Order_Main.Delete;
  end else
  if cxPageControl1.ActivePageIndex=3 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Contract_Sub_MX B inner join Contract_Sub C on B.SubId=C.SubId ');
      sql.Add(' where C.Mainid='''+Trim(Order_Main.fieldbyname('Mainid').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已经产生到货数据不能删除!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    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;
    Order_Main.Delete;
  end else
  if cxPageControl1.ActivePageIndex=4 then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ContractHZ_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ContractHZ_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 ContractHZ_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    Order_Main.Delete;
  end else
  if cxPageControl1.ActivePageIndex=5 then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ContractHZ_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ContractHZ_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 ContractHZ_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    Order_Main.Delete;
  end;

end;

function TfrmClothContractListHZ.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete ContractSX_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from ContractSX_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 ContractSX_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 ContractSX_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 TfrmClothContractListHZ.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel('坯布合同订单列表',cxGrid1);
end;

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

begin
  if Order_Main.IsEmpty then Exit;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\纱线订购合同.rmf' ;
    with ADOQueryTemp do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec ClothContractSX_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);
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\纱线前加工合同.rmf' ;
    with ADOQueryTemp do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * from ContractHZ_Main A inner join ContractHZ_Sub B on A.MainId=B.MainId');
      SQL.Add(' where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_PRT);
    SInitCDSData20(ADOQueryTemp,CDS_PRT);
    FConNoM:=Trim(CDS_PRT.fieldbyname('ConNo').AsString);
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\纱线加工合同.rmf' ;
    with ADOQueryTemp do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * from ContractHZ_Main A inner join ContractHZ_Sub B on A.MainId=B.MainId');
      SQL.Add(' where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_PRT);
    SInitCDSData20(ADOQueryTemp,CDS_PRT);
    FConNoM:=Trim(CDS_PRT.fieldbyname('ConNo').AsString);
  end else
  if cxPageControl1.ActivePageIndex=3 then
  begin
    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);
  end else
  if cxPageControl1.ActivePageIndex=4 then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\染色合同.rmf' ;
    with ADOQueryTemp do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * from ContractHZ_Main A inner join ContractHZ_Sub B on A.MainId=B.MainId');
      SQL.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_PRT);
    SInitCDSData20(ADOQueryTemp,CDS_PRT);
    FConNoM:=Trim(CDS_PRT.fieldbyname('ConNo').AsString);
  end else
  if cxPageControl1.ActivePageIndex=5 then
  begin
    fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\后加工合同.rmf' ;
    with ADOQueryTemp do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * from ContractHZ_Main A inner join ContractHZ_Sub B on A.MainId=B.MainId');
      SQL.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_PRT);
    SInitCDSData20(ADOQueryTemp,CDS_PRT);
    FConNoM:=Trim(CDS_PRT.fieldbyname('ConNo').AsString);
  end;


  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;

end;

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

procedure TfrmClothContractListHZ.TBAddClick(Sender: TObject);
begin
  if cxPageControl1.ActivePageIndex=0 then
  begin
    try
      frmClothContractInPutSXMX:=TfrmClothContractInPutSXMX.Create(Application);
      with frmClothContractInPutSXMX do
      begin
        PState:=0;
        FMainId:='';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutSXMX.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=0;
        FMainId:='';
        CPFlag:='ClothSX';
        CPFlagName:='纱线名称';
        Caption:='染纱合同';
        FactoryFlag:='PBFactory';
        v1Column3.Caption:='加工厂';
        v1Price.Caption:='加工单价';
        FConType:='纱线前加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=0;
        FMainId:='';
        CPFlag:='ClothSX';
        CPFlagName:='纱线名称';
        Caption:='织造合同';
        FactoryFlag:='PBFactory';
        v1Column3.Caption:='加工厂';
        v1Price.Caption:='加工单价';
        FConType:='纱线加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=3 then
  begin
    try
      frmClothContractInPutPB:=TfrmClothContractInPutPB.Create(Application);
      with frmClothContractInPutPB do
      begin
        PState:=0;
        FMainId:='';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutPB.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=4 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=0;
        FMainId:='';
        CPFlag:='Cloth';
        CPFlagName:='坯布名称';
        Caption:='染色加工合同';
        FactoryFlag:='RanFactory';
        v1Column3.Caption:='染厂';
        v1Price.Caption:='染色单价';
        FConType:='染色加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=5 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=0;
        FMainId:='';
        CPFlag:='Cloth';
        CPFlagName:='坯布名称';
        Caption:='后加工合同';
        FactoryFlag:='HZLFactory';
        v1Column3.Caption:='加工厂';
        v1Price.Caption:='加工单价';
        FConType:='后加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end;

end;

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

procedure TfrmClothContractListHZ.conPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(ConNoM.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;

      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        if cxPageControl1.ActivePageIndex=0 then
        begin
          sql.Add('exec ClothContractSX_QryList :MainId,:WSql');
          Parameters.ParamByName('WSql').Value:=' and OM.conNo like '''+'%'+Trim(ConNoM.Text)+'%'+'''';
        end else
        if cxPageControl1.ActivePageIndex=1 then
        begin
          sql.Add('select A.*,B.*,Money=(B.Price*B.C_Qty) from ContractHZ_Main A inner join ContractHZ_Sub B on A.Mainid=B.MainId ');
          SQL.Add(' where A.ConNO like'''+'%'+Trim(ConNoM.Text)+'%'+'''');
          sql.Add(' and A.ConType=''纱线加工''');
        end else
        if cxPageControl1.ActivePageIndex=2 then
        begin
          sql.Add('exec ClothContract_QryList :MainId,:WSql');
          Parameters.ParamByName('WSql').Value:=' and OM.conNo like '''+'%'+Trim(ConNoM.Text)+'%'+'''';
        end else
        if cxPageControl1.ActivePageIndex=3 then
        begin
          sql.Add('select A.*,B.*,Money=(B.Price*B.C_Qty) from ContractHZ_Main A inner join ContractHZ_Sub B on A.Mainid=B.MainId ');
          SQL.Add(' where A.ConNO like'''+'%'+Trim(ConNoM.Text)+'%'+'''');
          sql.Add(' and A.ConType=''染色加工''');
        end else
        if cxPageControl1.ActivePageIndex=4 then
        begin
          sql.Add('select A.*,B.*,Money=(B.Price*B.C_Qty) from ContractHZ_Main A inner join ContractHZ_Sub B on A.Mainid=B.MainId ');
          SQL.Add(' where A.ConNO like'''+'%'+Trim(ConNoM.Text)+'%'+'''');
          sql.Add(' and A.ConType=''后加工''');
        end;

        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmClothContractListHZ.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmClothContractListHZ.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    try
      frmClothContractInPutSXMX:=TfrmClothContractInPutSXMX.Create(Application);
      with frmClothContractInPutSXMX do
      begin
        PState:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNoM').AsString);
        PCopyInt:=1;
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutSXMX.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=1;
        PCopyInt:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
        CPFlag:='ClothSX';
        CPFlagName:='纱线名称';
        Caption:='染纱合同';
        FactoryFlag:='PBFactory';
        v1Column3.Caption:='加工厂';
        v1Price.Caption:='加工单价';
        FConType:='纱线前加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=1;
        PCopyInt:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
        CPFlag:='ClothSX';
        CPFlagName:='纱线名称';
        Caption:='织造合同';
        FactoryFlag:='PBFactory';
        v1Column3.Caption:='加工厂';
        v1Price.Caption:='加工单价';
        FConType:='纱线加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=3 then
  begin
    try
      frmClothContractInPutPB:=TfrmClothContractInPutPB.Create(Application);
      with frmClothContractInPutPB do
      begin
        PState:=1;
        PCopyInt:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNoM').AsString);
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutPB.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=4 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=1;
        PCopyInt:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
        CPFlag:='Cloth';
        CPFlagName:='坯布名称';
        Caption:='染色加工合同';
        FactoryFlag:='RanFactory';
        v1Column3.Caption:='染厂';
        v1Price.Caption:='染色单价';
        FConType:='染色加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=5 then
  begin
    try
      frmClothContractInPutHZ:=TfrmClothContractInPutHZ.Create(Application);
      with frmClothContractInPutHZ do
      begin
        PState:=1;
        PCopyInt:=1;
        FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
        FConNo:=Trim(Self.Order_Main.fieldbyname('ConNo').AsString);
        CPFlag:='Cloth';
        CPFlagName:='坯布名称';
        Caption:='后加工合同';
        FactoryFlag:='HZLFactory';
        v1Column3.Caption:='加工厂';
        v1Price.Caption:='加工单价';
        FConType:='后加工';
        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmClothContractInPutHZ.Free;
    end;
  end;
end;

procedure TfrmClothContractListHZ.ToolButton2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmFjList_RZ:=TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
    //  PState:=1;
   //   if Trim(canshu1)<>'高权限'  then cxButton1.Visible:=false; 
      fkeyNO:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      fType:='布匹采购';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFjList_RZ.Free;
  end;
end;

end.
