unit U_CKProductBCPKCList;

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, MovePanel, cxCheckBox, Menus, cxCalendar, RM_Common, RM_Class,
  RM_GridReport, RM_System, RM_Dataset, BtnEdit, Math, Clipbrd, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, RM_e_Xls;

type
  TfrmCKProductBCPKCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    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;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    v1Column16: TcxGridDBColumn;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    RM3: TRMGridReport;
    RMDBHZ: TRMDBDataSet;
    RMDBPRT: TRMDBDataSet;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    v1Column18: TcxGridDBColumn;
    Panel1: TPanel;
    Label3: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label7: TLabel;
    Label4: TLabel;
    Label10: TLabel;
    Label15: TLabel;
    MPRTCodeName: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderNo: TEdit;
    MJID: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    CPType: TComboBox;
    RKplace: TEdit;
    PRTColor: TEdit;
    v1Column5: TcxGridDBColumn;
    ganghao: TEdit;
    v1Column10: TcxGridDBColumn;
    Label11: TLabel;
    ConNo: TEdit;
    v1Column13: TcxGridDBColumn;
    Label12: TLabel;
    RKOrdID: TEdit;
    Label14: TLabel;
    ISCRTime: TCheckBox;
    v1Column14: TcxGridDBColumn;
    Label1: TLabel;
    KuanHao: TEdit;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Label2: TLabel;
    Label13: TLabel;
    MPRTCode: TEdit;
    PRTHX: TEdit;
    Panel3: TPanel;
    Label26: TLabel;
    Label27: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    Label17: TLabel;
    Edit_RollNum_X: TEdit;
    Edit_Qty_M_X: TEdit;
    edit_Qty_Y_X: TEdit;
    Edit1: TEdit;
    v1Column20: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label16: TLabel;
    KuangHao: TEdit;
    ToolButton1: TToolButton;
    ComboBox1: TComboBox;
    CDS_Label: TClientDataSet;
    ToolButton2: TToolButton;
    RMXLSExport1: TRMXLSExport;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Label18: TLabel;
    BaoID: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure MPRTCodeNameKeyPress(Sender: TObject; var Key: Char);
    procedure CPTypeKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column12PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure JsNum();
    procedure InitGrid();
    { Private declarations }
  public
    issel: integer;
    { Public declarations }
  end;

var
  frmCKProductBCPKCList: TfrmCKProductBCPKCList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ProductOrderListSel;

{$R *.dfm}

procedure TfrmCKProductBCPKCList.JsNum();
var
  fMJID: string;
  fps: integer;
  fQty, fQty1, fqty2: double;
begin
  fMJID := CDS_Main.fieldbyName('MJID').AsString;
  fps := 0;
  fQty := 0;
  fQty1 := 0;
  fqty2 := 0;
  if CDS_Main.IsEmpty then
    exit;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      if FieldByName('ssel').AsBoolean then
      begin
        fps := fps + 1;
        fQty := fQty + FieldByName('KGQty').AsFloat;
        fQty1 := fQty1 + FieldByName('Qty').AsFloat;
        fqty2 := fqty2 + FieldByName('MJYLen').AsFloat;
     //   fMoney:=fMoney+FieldByName('money').AsFloat;
      end;
      next;
    end;
    First;
    EnableControls;
  end;
  Edit_RollNum_X.Text := inttostr(fps);
  Edit_Qty_M_X.Text := floattostr(fQty1);
  Edit_Qty_Y_X.Text := floattostr(fQty);
  Edit1.Text := floattostr(fqty2);
  CDS_Main.Locate('MJID', fMJID, []);
end;

procedure TfrmCKProductBCPKCList.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPKCList := nil;
end;

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

procedure TfrmCKProductBCPKCList.FormCreate(Sender: TObject);
begin
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 20;
end;

procedure TfrmCKProductBCPKCList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  Pwhere := SGetFilters(Panel1, 1, 2);

  if ISCRTime.Checked then
  begin
    fwhere := ' where CRTime>=' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.DateTime));
    fwhere := fwhere + ' and CRTime<' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1));
  end;

  if trim(Pwhere) <> '' then
  begin
    if fwhere <> '' then
      fwhere := fwhere + ' and ' + trim(Pwhere)
    else
      fwhere := ' where ' + trim(Pwhere)
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_Page_FinishClothKC ');
      sql.Add('@pageIndex=' + inttostr(CurrentPage));
      sql.Add(',@pageSize=' + inttostr(RecordsNumber));
      sql.Add(',@criteria= ' + quotedstr(fwhere));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
  JsNum();
end;

procedure TfrmCKProductBCPKCList.TBRafreshClick(Sender: TObject);
begin
  CurrentPage := 1;
  InitGrid();

end;

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

procedure TfrmCKProductBCPKCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption), Tv1, '成品仓库');
  Close;
end;

procedure TfrmCKProductBCPKCList.FormShow(Sender: TObject);
var
  fsj: string;
begin
  fsj := 'select distinct(LMName) name from Lbael_Map where LMType=''入库码单''  ';
  SInitComBoxBySql(ADOQueryCmd, ComboBox1, true, fsj);

  with ADOQueryTemp do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    Sql.Add('select * from Lbael_Map where LMType=''入库码单''');

    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Label);
  SInitCDSData20(ADOQueryTemp, CDS_Label);
  CurrentPage := 1;
  RecordsNumber := 500;
  ReadCxGrid(trim(self.Caption), Tv1, '成品仓库');

end;

procedure TfrmCKProductBCPKCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('库存列表', cxGrid2);
end;

procedure TfrmCKProductBCPKCList.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

procedure TfrmCKProductBCPKCList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

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

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

procedure TfrmCKProductBCPKCList.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmCKProductBCPKCList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmCKProductBCPKCList.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPKCList.MPRTCodeNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmCKProductBCPKCList.CPTypeKeyPress(Sender: TObject; var Key: Char);
begin
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPKCList.v1Column12PropertiesEditValueChanged(Sender: TObject);
begin
  JsNum();
end;

procedure TfrmCKProductBCPKCList.ToolButton1Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  FMainID := '';
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Main.fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;
  if CDS_Label.Locate('LMName', trim(ComboBox1.Text), []) then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec ' + trim(CDS_Label.fieldbyname('LMSql').AsString) + ' @Code=' + quotedstr(Trim(DCode)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryPrint, CDS_PRT);
    SInitCDSData20(ADOQueryPrint, CDS_PRT);
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKMD_DXHZ @code=' + quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryPrint, CDS_HZ);
    SInitCDSData20(ADOQueryPrint, CDS_HZ);
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCKProductBCPKCList.ToolButton2Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, fImagePath2: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTemp);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete TBSubID where DName=''' + Trim(DCode) + '''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('SELECT * FROM TBSubID where 1=2 ');
    open;
  end;
  FMainID := '';
  CDS_Main.DisableControls;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          ADOQueryCmd.append;
          ADOQueryCmd.fieldbyname('SubId').Value := Trim(CDS_Main.fieldbyname('MJID').AsString);
          ADOQueryCmd.fieldbyname('Dname').Value := Trim(DCode);
          ADOQueryCmd.post;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
  CDS_Main.EnableControls;
  if CDS_Label.Locate('LMName', trim(ComboBox1.Text), []) then
  begin
    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec ' + trim(CDS_Label.fieldbyname('LMSql').AsString) + ' @Code=' + quotedstr(Trim(DCode)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryPrint, CDS_PRT);
    SInitCDSData20(ADOQueryPrint, CDS_PRT);
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);

    fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label0001.xls';
    if FileExists(fImagePath2) then
      DeleteFile(fImagePath2);
    fImagePath2 := ExtractFilePath(Application.ExeName) + 'image\label.xls';
    RM1.PrepareReport;
    RM1.ExportTo(RMXLSExport1, fImagePath2);
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

end.

