unit U_YarnPurchasePlanSel;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ToolWin, ComCtrls, U_BaseHelp, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, DBClient, ADODB, ImgList, StdCtrls, ExtCtrls,
  cxTextEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu,
  System.ImageList, U_BaseInput, cxButtonEdit, cxCheckBox, Vcl.Menus, cxCalendar,
  cxPC, dxScrollbarAnnotations, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmYarnPurchasePlanSel = class(TfrmBaseHelp)
    ToolBar1: TToolBar;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolButton1: TToolButton;
    ADOConnection1: TADOConnection;
    Panel1: TPanel;
    Label1: TLabel;
    Y_Composition: TEdit;
    GPM_1: TcxGridPopupMenu;
    ImageList1: TImageList;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    SHuangSeCu: TcxStyle;
    Tv1Column1: TcxGridDBColumn;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Label2: TLabel;
    Y_Name: TEdit;
    Label3: TLabel;
    SellName: TEdit;
    Label4: TLabel;
    PurNo: TEdit;
    Label5: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    IsJYTime: TCheckBox;
    Tv1Column10: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolButton2: TToolButton;
    Tv1Column11: TcxGridDBColumn;
    Label6: TLabel;
    ZS: TEdit;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1ZS: TcxGridDBColumn;
    Label7: TLabel;
    Label8: TLabel;
    CPConNo: TEdit;
    ConNo: TEdit;
    Tv1CPConNo: TcxGridDBColumn;
    Tv1ConNo: TcxGridDBColumn;
    Tv1C_Degree: TcxGridDBColumn;
    Tv1Y_Color: TcxGridDBColumn;
    Tv1Y_Composition: TcxGridDBColumn;
    Tv1Denier: TcxGridDBColumn;
    Tv1FF: TcxGridDBColumn;
    Tv1Qty: TcxGridDBColumn;
    Tv1CYRKQty: TcxGridDBColumn;
    Tv1YRKPiece: TcxGridDBColumn;
    Tv1Piece: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TV1DblClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure PurNoKeyPress(Sender: TObject; var Key: Char);
  private
    procedure InitGrid();
    { Private declarations }
  public
    FSTKName, FAuthority, canshu1: string;
    { Public declarations }
  end;

var
  frmYarnPurchasePlanSel: TfrmYarnPurchasePlanSel;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmYarnPurchasePlanSel.FormCreate(Sender: TObject);
begin
  inherited;
  try
    with ADOConnection1 do
    begin
      Connected := false;
      ConnectionString := DConString;
      Connected := true;
    end;
    ADOQueryBaseCmd.Connection := ADOConnection1;
    ADOQueryBaseTemp.Connection := ADOConnection1;
  except
    application.MessageBox('网络连接失败！', '提示信息');
  end;
  EndDate.DateTime := SGetServerDate(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 90;
end;



procedure TfrmYarnPurchasePlanSel.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYarnPurchasePlanSel.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Y_Name.SetFocus;

  Action := cahide;
end;

procedure TfrmYarnPurchasePlanSel.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.* ');
//      sql.Add(' ,WDHQty=B.Qty-(select sum(Qty) from BS_Yarn_IO X where X.IOFlag=''入库''  and X.FromPurSId=B.PurSId)  ');
      sql.Add(' ,YRKQty=(select sum(Qty) from BS_Yarn_IO X where X.IOFlag=''入库''  and X.FromPurSId=B.PurSId)  ');
      sql.Add(' ,YRKPiece=(select sum(Piece) from BS_Yarn_IO X where X.IOFlag=''入库''  and X.FromPurSId=B.PurSId)  ');
//      sql.Add(' ,FromOrdNo=(select OrderNo from Tat_Plan_Main X inner join Tat_Plan_Sub Y on X.MainID=Y.MainID  where Y.ConNo=B.ConNo) ');
//      sql.Add(' ,FromMainId=(select MainID from Tat_Plan_Sub X where X.ConNo=B.ConNo) ');
//      sql.Add(' ,FromSubId=(select OrderNo from Tat_Plan_Sub X where X.ConNo=B.ConNo) ');
      sql.Add(' from Pur_YarnPlan_Main A');
      sql.Add(' inner join Pur_YarnPlan_sub B on A.PurMId=B.PurMId');
      sql.Add(' where  isnull(LX,'''') <> ''织造单''  and isnull(A.status,''0'')=''9''');
      sql.Add(' and  isnull(C_Degree,'''') = ''纱线''  ');
      sql.add(' and A.PurDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and A.PurDate<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' and not EXISTS (select X.FromSubID from BS_Yarn_IO X where X.FromPurSId=B.PurSId)  ');
          end;
        1:
          begin
            sql.Add(' and  EXISTS (select X.FromSubID from BS_Yarn_IO X where X.FromPurSId=B.PurSId)  ');
          end;

      end;
//      ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYarnPurchasePlanSel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, true);
end;

procedure TfrmYarnPurchasePlanSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);

end;

procedure TfrmYarnPurchasePlanSel.PurNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if ADOQueryMain.Active then
    begin
      SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
      SCreateCDS(ADOQueryMain, CDS_1);
      SInitCDSData(ADOQueryMain, CDS_1);
    end;
  end;

end;

procedure TfrmYarnPurchasePlanSel.FormShow(Sender: TObject);
begin
  inherited;

  InitGrid();

  ReadCxGrid(self.Caption + '1', TV1, '自定义数据');

  if canshu1 = '管理' then
  begin
//    Tv1Column23.Visible := true;
//    Tv1Column24.Visible := true;
//    Tv1Column25.Visible := true;
//    Tv1Column26.Visible := true;
//    Tv1Column27.Visible := true;
//    Tv1Column28.Visible := true;
//
//    Tv1Column23.Hidden := False;
//    Tv1Column24.Hidden := False;
//    Tv1Column25.Hidden := False;
//    Tv1Column26.Hidden := False;
//    Tv1Column27.Hidden := False;
//    Tv1Column28.Hidden := False;
  end
  else
  begin
//    Tv1Column23.Visible := False;
//    Tv1Column24.Visible := False;
//    Tv1Column25.Visible := False;
//    Tv1Column26.Visible := False;
//    Tv1Column27.Visible := False;
//    Tv1Column28.Visible := False;
//
//    Tv1Column23.Hidden := true;
//    Tv1Column24.Hidden := true;
//    Tv1Column25.Hidden := true;
//    Tv1Column26.Hidden := true;
//    Tv1Column27.Hidden := true;
//    Tv1Column28.Hidden := true;
  end;

end;

procedure TfrmYarnPurchasePlanSel.TBCloseClick(Sender: TObject);
begin
  inherited;
  Close;
end;

procedure TfrmYarnPurchasePlanSel.TBSaveClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + '1', TV1, '自定义数据');

end;

procedure TfrmYarnPurchasePlanSel.ToolButton1Click(Sender: TObject);
begin
  Y_Name.SetFocus;
  ModalResult := 1;
end;

procedure TfrmYarnPurchasePlanSel.ToolButton2Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYarnPurchasePlanSel.TV1DblClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmYarnPurchasePlanSel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmYarnPurchasePlanSel := nil;
end;

end.

