unit U_ClothContractListHelp;

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
  TfrmClothContractListHelp = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNoM: TEdit;
    Label5: TLabel;
    C_CodeNameM: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    C_Spec: TEdit;
    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;
    ClientDataSet3: TClientDataSet;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    ClientDataSet2: TClientDataSet;
    v1Qty1: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    CDS_PRT: TClientDataSet;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1DHQty: TcxGridDBColumn;
    v1MXQty: TcxGridDBColumn;
    FactoryNoName: TEdit;
    Label6: TLabel;
    v1Column1: TcxGridDBColumn;
    Label2: TLabel;
    TBOK: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure TBOKClick(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;

    { Private declarations }
  public
    FType:string;
    { Public declarations }
  end;

var
  frmClothContractListHelp: TfrmClothContractListHelp;

implementation
uses
  U_DataLink,U_Fun, U_ZDYHelp;

{$R *.dfm}



procedure TfrmClothContractListHelp.FormDestroy(Sender: TObject);
begin
  frmClothContractListHelp:=nil;
end;

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

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

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

procedure TfrmClothContractListHelp.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 TfrmClothContractListHelp.InitForm();
begin
  ReadCxGrid(self.Caption+tv1.Name,Tv1,'指示单管理');
  cxTabControl1.TabIndex:=0;
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

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

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

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

end;

procedure TfrmClothContractListHelp.ToolButton5Click(Sender: TObject);
var
  maxno:string;
begin

end;

procedure TfrmClothContractListHelp.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 TfrmClothContractListHelp.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmClothContractListHelp.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;

procedure TfrmClothContractListHelp.TBOKClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  ModalResult:=1;
end;

procedure TfrmClothContractListHelp.Tv1CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  TBOK.Click;
end;

end.
