unit U_QryBSPrtMachOutList;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ToolWin, cxGraphics,
  cxControls, cxLookAndFeels,
  cxContainer, cxEdit, dxLayoutcxEditAdapters, dxBarBuiltInMenu, cxPC,
  dxLayoutContainer, cxClasses, cxTextEdit, dxLayoutControl, cxStyles,
  cxCustomData, cxFilter, cxData, cxDataStorage, cxNavigator, dxDateRanges,
  dxScrollbarAnnotations, Data.DB, cxDBData, cxCalendar, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGridCustomView,
  cxGrid, U_BaseList, Data.Win.ADODB, cxGridCustomPopupMenu, cxGridPopupMenu,
  Datasnap.DBClient, cxCheckBox, cxButtonEdit, cxSplitter, dxCore, cxDateUtils,
  cxMaskEdit, cxDropDownEdit,  cxProgressBar, Vcl.Menus,
  RM_Common, RM_Class, RM_GridReport, RM_Dataset, cxLookAndFeelPainters,
  Vcl.ExtCtrls, dxSkinsCore, dxSkinsDefaultPainters, dxLayoutControlAdapters,
  FrameDateSel10, Vcl.StdCtrls, MovePanel, BtnEdit, cxCurrencyEdit, U_WindowFormdesign,
  dxSkinWXI;

type
  TfrmQryBSPrtMachOutList = class(TfrmBaseList)
    ToolBar3: TToolBar;
    Trefresh: TToolButton;
    TsaveGrid: TToolButton;
    Tclose: TToolButton;
    DataSource1: TDataSource;
    ADOQueryList: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    dxLayoutControl_query: TdxLayoutControl;
    dxLayoutControl_queryGroup_Root: TdxLayoutGroup;
    frmFrameDateSel1: TfrmFrameDateSel10;
    CDS_List: TClientDataSet;
    cxProgressBar1: TMovePanel;
    ADOQueryCmd: TADOQuery;
    TBADD: TToolButton;
    TBdel: TToolButton;
    TBedit: TToolButton;
    TBexcpet: TToolButton;
    TBprint: TToolButton;
    TgridSet: TToolButton;
    BPIOID: TcxTextEdit;
    dxLayoutItem1: TdxLayoutItem;
    P_name: TcxTextEdit;
    dxLayoutItem2: TdxLayoutItem;
    P_spec: TcxTextEdit;
    dxLayoutItem4: TdxLayoutItem;
    ADOQueryTemp: TADOQuery;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    procedure TrefreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TcloseClick(Sender: TObject);
    procedure TsaveGridClick(Sender: TObject);
    procedure dxLayoutControl_queryDblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBADDClick(Sender: TObject);
    procedure TBdelClick(Sender: TObject);
    procedure TBeditClick(Sender: TObject);
    procedure TBexcpetClick(Sender: TObject);
    procedure TV1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure TBprintClick(Sender: TObject);
    procedure TgridSetClick(Sender: TObject);
    procedure cxButton4Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure cxButton5Click(Sender: TObject);
    procedure BPIOIDKeyPress(Sender: TObject; var Key: Char);

  private
    fWindowDesign: TWindowFormdesign;
    fDesignCode:string;
    procedure initGrid();
    procedure initForm();
  protected
  public
    canshu1, FStkName, canshu3: string;
    FFSPID: string;

  end;

var
  frmQryBSPrtMachOutList: TfrmQryBSPrtMachOutList;

implementation
uses
  U_RTFun, U_globalVar, U_dataLink,U_FormLayOutDesign,
  U_LabelPrint,U_cxGridCustomSet, U_ZDYHelp, U_QryBSPrtMachOutEdit;
{$R *.dfm}

procedure TfrmQryBSPrtMachOutList.TsaveGridClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
  if gIsCanDesign then
  begin
    saveLayOut(application, dxLayoutControl_query, ADOQueryCmd,PWideChar( fDllFileName + '|' + Self.Name + '|' + dxLayoutControl_query.Name + '.ini'));
  end;
end;

procedure TfrmQryBSPrtMachOutList.TV1Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  with CDS_List do
  begin
    Edit;
    FieldByName('PH').Value := Trim(mvalue);
    Post;
  end;

  try
    with AdoqueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update Dye_Formula_Sample_Sub SET PH=''' + trim(mvalue) + ''' where DFSMID=''' + trim(CDS_List.fieldbyName('DFSMID').AsString) + '''');
      sql.Add('and fsNo=''' + trim(CDS_List.fieldbyName('fsNo').AsString) + '''');
      execsql;
    end;
  except

  end;

  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmQryBSPrtMachOutList.TBADDClick(Sender: TObject);
begin
  try
    frmQryBSPrtMachOutEdit := TfrmQryBSPrtMachOutEdit.Create(Application);
    with frmQryBSPrtMachOutEdit do
    begin
      FBCId := '';
      FStkName := Self.FStkName;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmQryBSPrtMachOutEdit.Free;
  end;
end;

procedure TfrmQryBSPrtMachOutList.TBdelClick(Sender: TObject);
var
  MBPIOID: string;
begin
  if CDS_List.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  if CDS_List.IsEmpty then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''机物料出库''    ');
      sql.Add(',' + quotedstr(Trim(CDS_List.fieldbyname('BPIOID').AsString)));
      sql.Add(',''删除''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(Trim(CDS_List.fieldbyname('BPIOID').AsString)));
      sql.Add(')  ');
      Sql.Add('exec P_BS_Product_Out_Del ');
      Sql.Add(' @BPIOIDS=' + quotedstr(Trim(CDS_List.fieldbyname('BPIOID').AsString)));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    ADOQueryCmd.Connection.CommitTrans;
    CDS_List.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmQryBSPrtMachOutList.TBeditClick(Sender: TObject);
begin
  if CDS_List.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(Trim(CDS_List.fieldbyname('BPIOID').AsString)));
    Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
    Sql.Add(',@DName=' + quotedstr(Trim(DName)));
    Open;
  end;
  if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
  begin
    Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
    exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    Sql.Clear;
    Sql.Add('exec P_BS_Product_Out_Edit ');
    Sql.Add(' @BPIOIDS=' + quotedstr(Trim(CDS_List.fieldbyname('BPIOID').AsString)));
    Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
    Sql.Add(',@DName=' + quotedstr(Trim(DName)));
    Open;
  end;
  if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
  begin
    Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
    exit;
  end;
  try
    frmQryBSPrtMachOutEdit := TfrmQryBSPrtMachOutEdit.Create(Application);
    with frmQryBSPrtMachOutEdit do
    begin
      FBCId := Trim(CDS_List.fieldbyname('BPIOID').AsString);
      TBDel.Visible := False;
      TBAdd.Visible := False;
      FStkName := Self.FStkName;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmQryBSPrtMachOutEdit.Free;
  end;

end;

procedure TfrmQryBSPrtMachOutList.TBexcpetClick(Sender: TObject);
begin
  if ADOQueryList.IsEmpty then
    exit;
  TcxGridToExcel(FStkName + '出库列表', cxGrid2);
end;

procedure TfrmQryBSPrtMachOutList.TBprintClick(Sender: TObject);
var
  WSQL: string;
begin
  inherited;
  if CDS_List.IsEmpty then
    Exit;

  WSQL := CDS_List.fieldbyname('ZIONO').AsString;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'JHLCK';
      FFiltration1 := WSQL;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

procedure TfrmQryBSPrtMachOutList.TcloseClick(Sender: TObject);
begin
   close;
end;

procedure TfrmQryBSPrtMachOutList.TgridSetClick(Sender: TObject);
begin
  fWindowDesign.OpenGridDesignWin10(fDesignCode,'cxgrid2',tv1);
end;

procedure TfrmQryBSPrtMachOutList.TrefreshClick(Sender: TObject);
begin
  inherited;
  initGrid();
end;

procedure TfrmQryBSPrtMachOutList.BPIOIDKeyPress(Sender: TObject;
  var Key: Char);
begin
  inherited;
  if key = #13 then
  begin
    initGrid();
  end;
end;

procedure TfrmQryBSPrtMachOutList.cxButton1Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton1Click(Sender);
  initGrid;
end;

procedure TfrmQryBSPrtMachOutList.cxButton4Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton4Click(Sender);
  initGrid;
end;

procedure TfrmQryBSPrtMachOutList.cxButton5Click(Sender: TObject);
begin
  inherited;
  frmFrameDateSel1.cxButton5Click(Sender);
  initGrid;
end;

procedure TfrmQryBSPrtMachOutList.cxTabControl1Change(Sender: TObject);
begin
  inherited;
  initGrid();
end;

procedure TfrmQryBSPrtMachOutList.dxLayoutControl_queryDblClick(
  Sender: TObject);
begin
  layoutDesign(TdxLayoutControl(Sender),ADOQueryCmd,PWideChar(dcode));
end;

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

procedure TfrmQryBSPrtMachOutList.FormCreate(Sender: TObject);
begin
  inherited;
  cxgrid2.Align:=alClient;
  canshu1 := Trim(self.fParameters1);
  FStkName := Trim(self.fParameters2);
  canshu3 := Trim(self.fParameters3);
  frmFrameDateSel1.enddate.Date:=SGetServerDate(ADOQueryBaseTemp);
  frmFrameDateSel1.begdate.Date:=frmFrameDateSel1.enddate.Date - 30;
end;

procedure TfrmQryBSPrtMachOutList.FormDestroy(Sender: TObject);
begin
  inherited;
  frmQryBSPrtMachOutList := nil;
end;

procedure TfrmQryBSPrtMachOutList.FormShow(Sender: TObject);
begin
  inherited;
  InitForm();
end;

procedure TfrmQryBSPrtMachOutList.InitForm();
begin
  inherited;
  addQryContionByLay(ADOQueryBaseTemp,fformId,'cxgrid2',dxLayoutControl_query,7);
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, gDllFileCaption);
  InitGrid();
end;

procedure TfrmQryBSPrtMachOutList.InitGrid();
var
  mSqlWhere: string;
begin
  mSqlWhere := SLGetFilters(dxLayoutControl_query, 1, 2);
  if trim(mSqlWhere) <> '' then
  begin
    mSqlWhere := ' and ' + trim(mSqlWhere);
  end;
  try
    ADOQueryList.DisableControls;
    with ADOQueryList do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      SQL.Add(',CKPRICE=(SELECT PRICE FROM BS_Product_IO E WHERE A.STKID=E.BPIOID)');
      SQL.Add(',CKMONEY=Qty*(SELECT PRICE FROM BS_Product_IO E WHERE A.STKID=E.BPIOID)');
      sql.Add(' from BS_Product_IO A');
      sql.add(' where A.IOTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''');
      sql.Add(' and A.IOTime<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.EndDate.Date + 1)) + '''');
      sql.Add(' and isnull(StkName,'''')=''' + Trim(FStkName) + '''');
      SQL.Add(' and ioFlag=''出库''  ');
      sql.Add(Trim(mSqlWhere));
      sql.Add('order by FILLTIME desc');
      Open;
    end;
    SCreateCDS(ADOQueryList, CDS_List);
    SInitCDSData(ADOQueryList, CDS_List);
  finally
    ADOQueryList.EnableControls;
  end;
end;
end.
