unit U_ShaKCHZList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmShaKCHZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    SPName: TEdit;
    ToolButton2: TToolButton;
    v1Column17: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    RMDBMain: TRMDBDataSet;
    ToolButton3: TToolButton;
    ADOQueryPrint: TADOQuery;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    ToolButton4: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    ToolButton5: TToolButton;
    Panel5: TPanel;
    Label2: TLabel;
    Button5: TButton;
    Button6: TButton;
    Edit2: TEdit;
    CheckBox2: TCheckBox;
    Label1: TLabel;
    FACTORYNAME: TEdit;
    Label4: TLabel;
    spspec: TEdit;
    Label5: TLabel;
    batchno: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1Column1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure CheckBox1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
    CANSHU1: string;
    FCHEJIAN: string;
  end;

var
  frmShaKCHZList: TfrmShaKCHZList;

implementation

uses
  U_DataLink, U_RTFun, U_SXCKInPut, U_ShaOutList, U_ShaKCList, U_ZDYHelp;

{$R *.dfm}

procedure TfrmShaKCHZList.FormDestroy(Sender: TObject);
begin
  frmShaKCHZList := nil;
end;

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

procedure TfrmShaKCHZList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select * from V_SX_YJ where 1=1');
      if CheckBox1.Checked = True then
      begin
        sql.Add('and DDYJ=1');
      end;
      SQL.Add('AND CHEJIAN=' + QuotedStr(FCHEJIAN));
//      ShowMessage(SQL.Text);
      if CheckBox2.Checked = False then
      begin
        sql.Add('and KCQty<>0');
//        SQL.Add('having Sum(isnull(CRQtyFlag*Qty,0))<>0');
      end;
//      SQL.Add('having SUM(CRQtyFlag*qty)>0');

//      ShowMessage(sql.Text);
      Open;

    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmShaKCHZList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmShaKCHZList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线库存汇总列表2', Tv1, '纱线仓库');
  Close;
end;

procedure TfrmShaKCHZList.FormShow(Sender: TObject);
begin
  ReadCxGrid('纱线库存汇总列表2', Tv1, '纱线仓库');
  if CANSHU1 = '管理' then
  begin
    ToolButton5.Visible := True;
  end
  else
  begin
    ToolButton5.Visible := False;
  end;
  //InitGrid();
end;

procedure TfrmShaKCHZList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmShaKCHZList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmShaKCHZList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmShaKCHZList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

procedure TfrmShaKCHZList.SXBatchNOChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmShaKCHZList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmShaKCHZList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmShaKCList := TfrmShaKCList.Create(Application);
    with frmShaKCList do
    begin
      frmShaKCList.Align := alClient;
      FDYFlag := '99';
      frmShaKCList.SPName.Text := Trim(Self.CDS_Main.fieldbyname('SPName').AsString);
      frmShaKCList.SPSpec.Text := Trim(Self.CDS_Main.fieldbyname('SPSpec').AsString);
      frmShaKCList.BatchNo.Text := Trim(Self.CDS_Main.fieldbyname('BatchNo').AsString);
      frmShaKCList.SXPinPai.Text := Trim(Self.CDS_Main.fieldbyname('SXPinPai').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmShaKCList.Free;
  end;
end;

procedure TfrmShaKCHZList.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  TcxGridToExcel('纱线库存汇总列表', cxGrid2);
end;

procedure TfrmShaKCHZList.ToolButton1Click(Sender: TObject);
var
  filepath: string;
begin

  try
    filepath := ExtractFilePath(Application.ExeName) + 'report\纱线库存报表.rmf';
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.Add(' select FactoryName,SPName,SPSpec,BatchNo,SXPinPai,SPNameGYS,SPColor');
      sql.Add(' ,RKPiQty=Sum(isnull(PiQty,0)),RKQty=Sum(isnull(Qty,0))');
      sql.Add(' ,KCPiQty=Sum(isnull(MXKCPiQty,0)),KCQty=Sum(isnull(MXKCQty,0))');
      sql.Add(' ,CKPiQty=Sum(isnull(PiQty,0))-Sum(isnull(MXKCPiQty,0)),CKQty=Sum(isnull(Qty,0))-Sum(isnull(MXKCQty,0))');
      sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''纱线'' ');
      SQL.Add(' and A.CRFlag=''入库'' and MXKCQty>0 and ZFFlag=0 ');
      SQL.Add(' Group by FactoryName,SPName,SPSpec,BatchNo,SXPinPai,SPNameGYS,SPColor');
      Open;
    end;
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;
  finally

  end;

end;

procedure TfrmShaKCHZList.ToolButton3Click(Sender: TObject);
var
  filepath: string;
begin
  try
    filepath := ExtractFilePath(Application.ExeName) + 'report\纱线汇总报表.rmf';
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.Add(' select FactoryName,SPName,SPSpec');
      sql.Add(' ,KCPiQty=Sum(isnull(MXKCPiQty,0)),KCQty=Sum(isnull(MXKCQty,0))');
      sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''纱线'' ');
      SQL.Add(' and A.CRFlag=''入库'' and MXKCQty>0 and ZFFlag=0 ');
      SQL.Add(' Group by FactoryName,SPName,SPSpec');
      Open;
    end;
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;
  finally

  end;

end;

procedure TfrmShaKCHZList.Tv1Column1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  F1, F2, f3: Double;
begin
  F1 := StrToFloatDef((AViewInfo.GridRecord.Values[Tv1Column1.Index]), 0);    //入库匹数
  F2 := StrToFloatDef((AViewInfo.GridRecord.Values[v2Column6.Index]), 0);
  f3 := StrToFloatDef((AViewInfo.GridRecord.Values[Tv1Column2.Index]), 0);

//  if F1 >= F2 then
//  begin
//    ACanvas.Brush.Color := clRed;
//  end;
  if f3 = 1 then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

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

procedure TfrmShaKCHZList.ToolButton4Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXYCLName';
      flagname := '纱线名称';
      fnote := True;
      V1Note.Caption := '预警数量';

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaKCHZList.ToolButton5Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty = True then
    Exit;
  Panel5.Visible := True;
  Edit2.Text := Trim(CDS_Main.fieldbyname('kcqty').AsString);
end;

procedure TfrmShaKCHZList.Button5Click(Sender: TObject);
var
  MAXNO: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    if GetLSNo(ADOQueryTemp, MAXNO, 'SR', 'CK_SXPB_CR', 3, 1) = False then
    begin

      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取纱线入库编号失败!', '提示', 0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR');
      sql.Add('where 1<>1');
      open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('SPID').Value := Trim(MAXNO);
      FieldByName('FZSPID').Value := Trim(MAXNO);
      FieldByName('CKName').Value := '纱线';
      FieldByName('crtime').Value := SGetServerDate(ADOQueryTemp);
      FieldByName('CRFlag').Value := '入库';
      FieldByName('CRtype').Value := '调整入库';
      FieldByName('CRQtyFlag').Value := 1;
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('QtyUnit').Value := 'KG';
      FieldByName('ToFactoryName').Value := '本厂仓库';

      FieldByName('SPName').Value := CDS_Main.fieldbyname('SPName').AsString;
      FieldByName('FACTORYNAME').Value := CDS_Main.fieldbyname('FACTORYNAME').AsString;
      FieldByName('spspec').Value := CDS_Main.fieldbyname('spspec').AsString;
      FieldByName('batchno').Value := CDS_Main.fieldbyname('batchno').AsString;

      FieldByName('QTY').Value := STRTOFLOATDEF(Edit2.Text, 0) - CDS_Main.fieldbyname('kcQty').AsFloat;

      Post;

    end;

    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      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('品名' + Trim(CDS_Main.fieldbyname('SPName').AsString) + '供应商' + Trim(CDS_Main.fieldbyname('FACTORYNAME').AsString) + '原库存:' + Trim(CDS_Main.fieldbyname('kcqty').AsString) + ';现库存:' + Trim(edit2.Text))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    with CDS_Main do
    begin
      Edit;
      CDS_Main.fieldbyname('kcqty').AsString := edit2.text;
      Post;
    end;

    CDS_Main.EnableControls;
    Panel5.Visible := False;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!', '提示', 0);
//    InitGrid();
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;

end;

procedure TfrmShaKCHZList.CheckBox2Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmShaKCHZList.Button6Click(Sender: TObject);
begin
  panel5.Visible := false;
end;

end.

