unit U_CKProductCKHZList;

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, cxButtonEdit, cxCalendar, RM_System, RM_Common,
  RM_Class, RM_GridReport, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  RM_Dataset, RM_e_Xls, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint,
  dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinOffice2013White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringTime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

type
  TfrmCKProductCKHZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    PRTCodeName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Label5: TLabel;
    orderNo: TEdit;
    Label6: TLabel;
    PRTColor: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    MovePanel2: TMovePanel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    YWY: TEdit;
    OrdPerson1: TEdit;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    CKno: TEdit;
    Label4: TLabel;
    Panel4: TPanel;
    Label13: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column10: TcxGridDBColumn;
    SOrddefstr1: TEdit;
    Label14: TLabel;
    KHCONNO: TEdit;
    Label15: TLabel;
    RMGridReport1: TRMGridReport;
    ToolButton1: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    ToolButton2: TToolButton;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    ToolButton3: TToolButton;
    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 TBFindClick(Sender: TObject);
    procedure PRTCodeNameChange(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure PRTColorChange(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TBPrintClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    FLeft, FTop: Integer;
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKProductCKHZList: TfrmCKProductCKHZList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_JYOrderCDOne;

{$R *.dfm}

procedure TfrmCKProductCKHZList.FormDestroy(Sender: TObject);
begin
  frmCKProductCKHZList := nil;
end;

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

procedure TfrmCKProductCKHZList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductCKHZList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      Filtered := False;    //D.PRTCodeName,D.PrtColor,D.PrtHX,D.SOrddefstr4,D.SOrddefstr1,
      sql.Add('select CKNO,A.MainId,A.MJTypeother as QtyUnit,C.OrderNo,C.ConNO,D.Sorddefstr5,count(distinct baoid) as RKbao, ');
      sql.Add('count(A.MainId) as JQty,SUM(A.MJLen) as Qty,SUM(A.MJMaoZ) as KGQty,SUM(A.MJQty4) as CKJZ,round(SUM(cast([dbo].[Get_ShuZi](A.MJFK) as float)*cast(A.MJSJKZ as float)*MJLEN/1000),1) as JSJZ');
      SQL.Add(',round(SUM(CASE WHEN A.ckflag=''已出库'' THEN (cast([dbo].[Get_ShuZi](A.MJFK) as float)*cast(A.MJSJKZ as float)*A.MJLen/1000) ELSE 0 END),1) as CKSL');
      SQL.Add(',SUM(CASE WHEN A.ckflag=''已出库'' THEN A.MJLen ELSE 0 END) as SCKROLL');
      SQL.Add(',SUM(CASE WHEN A.ckflag=''未出库'' THEN A.MJLen ELSE 0 END) as SkcROLL');
      SQL.Add(',SUM(CASE WHEN A.ckflag=''已出库'' THEN 1 ELSE 0 END) as CKROLL');
      SQL.Add(',SUM(CASE WHEN A.ckflag=''未出库'' THEN 1 ELSE 0 END) as KCROLL');
      sql.Add(',CKZbao=(select count(distinct baoid) from WFB_MJJY X where X.CKNO=A.CKNO and  X.ckflag=''已出库''),');
      sql.Add('CKbao=(select count(distinct baoid) from WFB_MJJY X where X.CKNO=A.CKNO and   X.mainid=A.mainid    and  X.ckflag=''已出库'')');
      sql.Add('from WFB_MJJY A ');
      sql.Add('inner join JYOrder_Main C on C.MainId=A.MainId ');
      sql.Add('inner join JYOrder_sub D on D.subID=A.subID ');
      Sql.add('where A.ckno<>'''' ');
//      Sql.add(' A.fillTime>='''+formatdateTime('yyyy-MM-dd',begdate.Date)+'''and A.fillTime<'''+formatdateTime('yyyy-MM-dd',enddate.Date+1)+''' ');
      Sql.add('group by CKNO,A.MainId,A.MJTypeother,C.OrderNo,C.ConNO,D.Sorddefstr5');
//        ShowMessage(SQL.Text);
      Open;

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

procedure TfrmCKProductCKHZList.TBRafreshClick(Sender: TObject);
begin
  //BegDate.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

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

procedure TfrmCKProductCKHZList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');
  Close;
end;

procedure TfrmCKProductCKHZList.FormShow(Sender: TObject);
begin

  ReadCxGrid(self.Caption + tv1.Name, Tv1, '成品仓库');
  if Trim(DParameters2) = '管理' then
  begin
    //v1Column5.Options.Focusing:=True;
  end
  else
  begin
    //v1Column5.Options.Focusing:=False;
  end;
  //InitGrid();
end;

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

procedure TfrmCKProductCKHZList.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 TfrmCKProductCKHZList.PRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductCKHZList.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SOrdDefStr10';
      flagname := '库存存放地点';
      if ShowModal = 1 then
      begin
        with CDS_Main do
        begin
          Edit;
          FieldByName('SOrdDefStr10').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Sub Set SOrdDefStr10=''' + Trim(ClientDataSet1.fieldbyname('ZdyName').AsString) + '''');
          sql.Add(' where SubId=''' + Trim(Self.CDS_Main.fieldbyname('SubId').AsString) + '''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCKProductCKHZList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductCKHZList.Image2Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmCKProductCKHZList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  Panel4.Left := FLeft;
  Panel4.Top := FTop + 110;
  Panel4.Visible := True;
  Panel10.Caption := Trim(TV1.Controller.FocusedColumn.Caption);
  RichEdit1.Text := CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString;
  application.ProcessMessages;
end;

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

procedure TfrmCKProductCKHZList.TBPrintClick(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
  sqlStr: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\出库码单.rmf';

//  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;
//  sqlStr := '';
//  FMainID := '';
//  Order_Main.DisableControls;
//  try
//    ADOQueryCmd.Connection.BeginTrans;
//    with Order_Main do
//    begin
//      First;
//      while not Eof do
//      begin
//        if Fieldbyname('Ssel').AsBoolean then
//        begin
//          if FMainID = '' then
//          begin
//            FMainID := Trim(Order_Main.fieldbyname('CKNO').AsString);
//          end
//          else
//          begin
//            if Trim(Order_Main.fieldbyname('CKNO').AsString) <> FMainID then
//            begin
//              application.MessageBox('选择的不是同一个出库单，不能一起打印！', '提示信息', 0);
//              ADOQueryCmd.Connection.RollbackTrans;
//              EnableControls;
//              exit;
//            end;
//          end;
//          sqlStr := sqlStr + 'insert into TBSubID(SubId,Dname) ';
//          sqlStr := sqlStr + ' values( ';
//          sqlStr := sqlStr + ' ''' + Trim(Order_Main.fieldbyname('CKNO').AsString) + ''', ';
//          sqlStr := sqlStr + ' ''' + Trim(DCode) + ''' ';
//          sqlStr := sqlStr + ' ) ';
//        end;
//        Next;
//      end;
//    end;
//    ADOQueryCmd.Connection.CommitTrans;
//  except
//    ADOQueryCmd.Connection.RollbackTrans;
//  end;
//  Order_Main.EnableControls;
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add(sqlStr);
////    showmessage(sql.text);
//    ExecSQL;
//  end;

  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_PrintMd10 ''' + CDS_Main.fieldbyname('CKNO').AsString + ''' ');
//      sql.add('exec P_Do_PrintMd10 ');
//      sql.add('@mainID=' + quotedstr(Trim('')));
//      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      Open;
//      ShowMessage(SQL.text);
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
  end;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCKProductCKHZList.ToolButton1Click(Sender: TObject);
begin
  if cds_main.IsEmpty then
    Exit;
  frmJYOrderCDOne := TfrmJYOrderCDOne.Create(Application);
  with frmJYOrderCDOne do
  begin
    orderno.Text := trim(self.CDS_Main.fieldbyname('orderno').asstring);
    gangno.Text := trim(self.CDS_Main.fieldbyname('MJStr4').asstring);
    PRTColor.Text := trim(self.CDS_Main.fieldbyname('PRTColor').asstring);
    if ShowModal = 1 then
    begin
    //     InitGrid();
    end;
    free;
  end;
end;

procedure TfrmCKProductCKHZList.ToolButton2Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
  sqlStr: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\出库英文品名码单.rmf';

  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_PrintMd10_PMYW  ');  //''' + CDS_Main.fieldbyname('CKNO').AsString + '''
      sql.add('@mainID=''' + CDS_Main.fieldbyname('CKNO').AsString + '''');
      sql.add(',@orderNo=''' + CDS_Main.fieldbyname('mainid').AsString + '''');
      sql.add(',@Sorddefstr5=''' + CDS_Main.fieldbyname('Sorddefstr5').AsString + '''');
      Open;
//      ShowMessage(SQL.text);
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
  end;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCKProductCKHZList.ToolButton3Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
  sqlStr: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\混色码单.rmf';

  begin
    with ADOQueryTemp do
    begin                                             
      Close;
      sql.Clear;
      sql.Add('exec P_Do_PrintMd10_HS  ');  //''' + CDS_Main.fieldbyname('CKNO').AsString + '''
      sql.add('@CKNO=''' + CDS_Main.fieldbyname('CKNO').AsString + '''');
      sql.add(',@mainID=''' + CDS_Main.fieldbyname('mainid').AsString + '''');
      Open;
//      ShowMessage(SQL.text);
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
  end;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

end.

