unit U_ClothPurchaseEnterList;

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, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxPC, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseList, RM_e_Graphic, RM_e_Jpeg,
  RM_E_llPDF, Vcl.Clipbrd, dxScrollbarAnnotations, FrameDateSel, cxContainer,
  cxProgressBar, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmClothPurchaseEnterList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    Panel1: TPanel;
    Label4: TLabel;
    Label9: TLabel;
    BuyName: TcxTextEdit;
    PurNo: TcxTextEdit;
    cxTabControl1: TcxTabControl;
    CDS_1: TClientDataSet;
    Label3: TLabel;
    SellName: TcxTextEdit;
    pm1: TPopupMenu;
    ADOQueryImage: TADOQuery;
    ToolButton7: TToolButton;
    N3: TMenuItem;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    VC_SCSCode: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1PRTPrice: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    N5: TMenuItem;
    N6: TMenuItem;
    frmFrameDateSel1: TfrmFrameDateSel;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    Label1: TLabel;
    C_Name: TcxTextEdit;
    Panel2: TPanel;
    cxSplitter1: TcxSplitter;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    ADO_2: TADOQuery;
    GPM_2: TcxGridPopupMenu;
    DS_2: TDataSource;
    ToolBar2: TToolBar;
    ToolButton5: TToolButton;
    ToolButton1: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure BuyNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure PurNoPropertiesChange(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure cxButton4Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure cxButton5Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure cxButton3Click(Sender: TObject);
    procedure PurNoKeyPress(Sender: TObject; var Key: Char);
    procedure TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton1Click(Sender: TObject);
  private
    fFlileFlag: string;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitGrid2();
    { Private declarations }
  public

    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint, U_PurClothEnterInput,
  U_ClothPurchaseQtyInPut;

{$R *.dfm}
procedure TfrmClothPurchaseEnterList.InitGrid2();
var
  SqlStr: string;
begin

  if CDS_1.IsEmpty then
    EXIT;
  SqlStr := 'select A.BCIONO,IOTime=convert(varchar(10),A.IOTime,120) ,Qty=sum(A.Qty) ,Piece=sum(A.Piece)   ';
  SqlStr := SqlStr + 'from BS_Cloth_IO A                                                              ';
  SqlStr := SqlStr + 'where A.IOType=''采购入库'' and A.FromPurSId=' + quotedstr(trim(CDS_1.fieldbyname('PurSId').asstring));
  SqlStr := SqlStr + 'group by A.BCIONO,convert(varchar(10),A.IOTime,120)  ';

  InitAdoData(ADO_2, Tv2, SqlStr, '', '');

end;

procedure TfrmClothPurchaseEnterList.N1Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, True); //全选
end;

procedure TfrmClothPurchaseEnterList.N2Click(Sender: TObject);
begin
  SelOKNoFiler(Tv1, False); //全弃
end;

procedure TfrmClothPurchaseEnterList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmClothPurchaseEnterList.N5Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);
end;

procedure TfrmClothPurchaseEnterList.N6Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);

end;

procedure TfrmClothPurchaseEnterList.PurNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid();
end;

procedure TfrmClothPurchaseEnterList.PurNoPropertiesChange(Sender: TObject);
begin
  if Length(Trim(TcxTextEdit(Sender).Text)) < 4 then
    Exit;
  TBFind.Click;
end;

procedure TfrmClothPurchaseEnterList.SetStatus();
begin

end;

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

procedure TfrmClothPurchaseEnterList.FormCreate(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 90;

end;

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

procedure TfrmClothPurchaseEnterList.InitGrid();
var
  SqlStr: string;
begin

  SqlStr := ' select A.*,B.*';
  SqlStr := SqlStr + ',LJHCPiece= (select SUM(X.Piece) from BS_Cloth_IO X where X.FromPurSId = B.PurSId and X.IOType =''采购入库'' and STKName=''贸易布匹'')';
  SqlStr := SqlStr + ',LJHCQty= (select SUM(X.Qty)   from BS_Cloth_IO X where X.FromPurSId = B.PurSId and X.IOType =''采购入库'' and STKName=''贸易布匹'')';
  SqlStr := SqlStr + ' from Pur_ClothPlan_Main A inner join Pur_ClothPlan_sub B on A.PurMId=B.PurMId  ';
  SqlStr := SqlStr + 'where 1=1 ';

  case cxTabControl1.TabIndex of
    0:
      begin
        SqlStr := SqlStr + ' and  isnull(A.status,''0'')=''9''';
      end;
    1:
      begin
        SqlStr := SqlStr + ' and  isnull(A.status,''0'')=''10''';
        SqlStr := SqlStr + ' and A.PurDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''';
        SqlStr := SqlStr + ' and A.PurDate<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)) + '''';
      end;

  end;
  InitCDSData(ADOQueryMain, CDS_1, Tv1, SqlStr, SGetFilters(Panel1, 1, 2), 'PurNo');
end;

procedure TfrmClothPurchaseEnterList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

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

procedure TfrmClothPurchaseEnterList.FormShow(Sender: TObject);
begin
  inherited;
  fFlileFlag := UserDataFlag + 'HX';
  readCxGrid(trim(Self.Caption) + 'tv1', Tv1, '成品仓库管理');
  readCxGrid(trim(Self.Caption) + 'tv2', Tv2, '成品仓库管理');
  SetStatus();
  InitGrid();
end;

procedure TfrmClothPurchaseEnterList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmClothPurchaseEnterList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmClothPurchaseEnterList.BuyNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmClothPurchaseEnterList.cxButton1Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton1Click(Sender);
  InitGrid();
end;

procedure TfrmClothPurchaseEnterList.cxButton2Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton2Click(Sender);
  InitGrid();
end;

procedure TfrmClothPurchaseEnterList.cxButton3Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton3Click(Sender);
  InitGrid();
end;

procedure TfrmClothPurchaseEnterList.cxButton4Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton4Click(Sender);
  InitGrid();
end;

procedure TfrmClothPurchaseEnterList.cxButton5Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton5Click(Sender);
  InitGrid();
end;

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

procedure TfrmClothPurchaseEnterList.ToolButton1Click(Sender: TObject);
begin

  if ADO_2.IsEmpty then
    Exit;
  try
    frmClothPurchaseQtyInPut := TfrmClothPurchaseQtyInPut.Create(Application);
    with frmClothPurchaseQtyInPut do
    begin
      FBCIONO := Trim(ADO_2.fieldbyname('BCIONO').AsString);
      FQtyUnit := Trim(CDS_1.fieldbyname('QtyUnit').AsString);
      FPurMId := Trim(CDS_1.fieldbyname('PurMId').AsString);
      FPurSId := Trim(CDS_1.fieldbyname('PurSId').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmClothPurchaseQtyInPut.Free;
  end;

end;

procedure TfrmClothPurchaseEnterList.ToolButton5Click(Sender: TObject);
begin
  try
    frmClothPurchaseQtyInPut := TfrmClothPurchaseQtyInPut.Create(Application);
    with frmClothPurchaseQtyInPut do
    begin
      FBCIONO := '';
      FQtyUnit := Trim(CDS_1.fieldbyname('QtyUnit').AsString);
      FPurMId := Trim(CDS_1.fieldbyname('PurMId').AsString);
      FPurSId := Trim(CDS_1.fieldbyname('PurSId').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmClothPurchaseQtyInPut.Free;
  end;
end;

procedure TfrmClothPurchaseEnterList.ToolButton6Click(Sender: TObject);
var
  MPurMIdS: string;
begin
  if CDS_1.IsEmpty then
    exit;

  MPurMIdS := MPurMIdS + trim(CDS_1.fieldbyname('PurMId').AsString) + ',';

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Pur_ClothPlan_SetStatus ');
      sql.Add(' @PurMIdS=' + QuotedStr(MPurMIdS));
      sql.Add(',@Operation=''撤销完成'' ');
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('合同撤销完成')));
      sql.Add(',' + quotedstr(trim('采购计划ID：' + trim(MPurMIdS))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    application.MessageBox('撤销完成成功', '提示');
    InitGrid();
  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    InitGrid();
  end;

end;

procedure TfrmClothPurchaseEnterList.ToolButton7Click(Sender: TObject);
begin
  writeCxGrid(trim(Self.Caption) + 'tv1', Tv1, '成品仓库管理');
  writeCxGrid(trim(Self.Caption) + 'tv2', Tv2, '成品仓库管理');
end;

procedure TfrmClothPurchaseEnterList.TV1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGrid2();
end;

end.

