unit U_PBRklist;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, RM_System, RM_Dataset, RM_Common,
  RM_Class, RM_GridReport, cxPC, Menus, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, 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
  TfrmPBRklist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    CodeName: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Code: TcxGridDBColumn;
    v1INoutNo: TcxGridDBColumn;
    Label7: TLabel;
    CRNo: TEdit;
    v1Price: TcxGridDBColumn;
    v1money: TcxGridDBColumn;
    gangNo: TEdit;
    Label10: TLabel;
    RMDB_Main: TRMDBDataSet;
    v1Column4: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    Code: TEdit;
    Label15: TLabel;
    v1Column10: TcxGridDBColumn;
    Label17: TLabel;
    fromFactoryName: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column2: TcxGridDBColumn;
    ADOQueryMain: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column8: TcxGridDBColumn;
    Label4: TLabel;
    kuwei: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    CDS_PRT: TClientDataSet;
    ToolButton3: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    fCKName, canshu1: string;
    fCRType, FZZType: string;
    { Public declarations }
  end;

//var
//  frmPBRklist: TfrmPBRklist;

implementation

uses
  U_DataLink, U_Fun10, U_Fun, U_ZDYHelp, U_PBRk, U_FjList_RZ;
{$R *.dfm}

procedure TfrmPBRklist.SetStatus();
begin
//  TBEdit.Visible:=False;
//  ToolButton1.Visible:=False;
//  TBDel.Visible:=False;
//  TBPrint2.Visible:=False;
//  TBPrint1.Visible:=False;
//  ToolButton2.Visible:=False;
//  ToolButton3.Visible:=False;
//  if Trim(DParameters2)='高权限' then
//  begin
//    case cxTabControl1.TabIndex of
//    0:begin
//        TBEdit.Visible:=true;
//        ToolButton1.Visible:=true;
//        TBDel.Visible:=true;
//        ToolButton2.Visible:=true;
//        TBPrint1.Visible:=true;
//      end;
//    1:begin
//        TBPrint2.Visible:=true;
//        TBPrint1.Visible:=true;
//        ToolButton3.Visible:=true;
//      end;
//    2:begin
//
//      end;
//    end
//  end
//  else
//  begin
//    case cxTabControl1.TabIndex of
//    0:begin
//        TBEdit.Visible:=true;
//        ToolButton1.Visible:=true;
//        TBDel.Visible:=true;
//        TBPrint1.Visible:=true;
//      end;
//    1:begin
//        TBPrint2.Visible:=true;
//        TBPrint1.Visible:=true;
//      end;
//    2:begin
//
//      end;
//    end
//  end;
end;
                                        
procedure TfrmPBRklist.InitGrid();
begin
  try                                              
    with ADOQueryMain do
    begin
      close;                               
      sql.Clear;
      sql.Add('select A.*   ');
      SQL.add(',CKLX=(select top 1 CRType from CK_PB_CR X where X.CRFlag=''出库''  and X.Valid=''Y''   and  X.KCID=A.KCID )');
      SQL.add(',CKPS=(select sum(PS) from CK_PB_CR X where X.CRFlag=''出库''  and X.Valid=''Y''   and  X.KCID=A.KCID )');
      sql.Add(',Case when isnull((select count(*) from TP_File where WBID=A.CRNO and TFType=''坯布汇总附件'' ),0)>0 ');
      sql.Add('then Cast(1 as bit) else Cast(0 as bit) end as FJFlag');
      sql.Add('   from CK_PB_CR A ');
      sql.Add('where A.CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add('and  A.CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      sql.Add('and  A.CRFlag=''入库''  and A.Valid=''Y'' and CKName=''坯布仓库'' ');

//      if canshu1 = '录入' then
//      begin
//        Sql.Add(' and Filler=' + quotedstr(Trim(DName)));
//      end;
      sql.Add('order by A.CRNO,A.Code ');
//      ShowMessage(SQL.text);
      open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
    TBFind.Click;
  finally
  end;
end;

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

procedure TfrmPBRklist.FormDestroy(Sender: TObject);
begin
//  frmPBRklist:=nil;
end;

procedure TfrmPBRklist.TBAddClick(Sender: TObject);
begin
  frmPBRk := TfrmPBRk.create(self);
  with frmPBRk do
  begin
    fkeyNo := '';
    fType := 0;
    fCRType := self.fCRType;
    FZZType := self.FZZType;
    FactoryName.Text := '本厂';
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmPBRklist.CodeNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_Main);
  SInitCDSData20(ADOQueryMain, CDS_Main);
end;

procedure TfrmPBRklist.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption, Tv1, '面料仓库');
  SetStatus();
//  InitGrid();
end;

procedure TfrmPBRklist.FormCreate(Sender: TObject);
begin
  EndDate.Date := SGetServerDate10(ADOQueryTmp);
  BegDate.Date := EndDate.Date - 7;
  cxGrid1.Align := alclient;
end;

procedure TfrmPBRklist.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(self.Caption, Tv1, '面料仓库');
  close;
end;

procedure TfrmPBRklist.TBEditClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR_Sub where YFPZNO=''' + Trim(CDS_Main.fieldbyname('CRID').AsString) + '''');
    Open;
  end;
  if ADOQueryTmp.IsEmpty = False then
  begin
    Application.MessageBox('已经生成财务数据不能操作!', '提示', 0);
    Exit;
  end;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PB_CR where Valid=''Y'' and  CRFlag=''出库''');
    sql.Add('and KCID=' + QuotedStr(Trim(CDS_Main.fieldbyname('KCID').AsString)));
    Open;
  end;
  if ADOQueryTmp.IsEmpty = False then
  begin
    Application.MessageBox('已出库数据不能操作!', '提示', 0);
    Exit;
  end;
  frmPBRk := TfrmPBRk.create(self);
  with frmPBRk do
  begin
    fkeyNo := self.cds_main.fieldbyname('CRID').AsString;
    fType := 1;
    fckName := self.fckName;
    fCRType := self.fCRType;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('CRID', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmPBRklist.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PB_CR where Valid=''Y'' and  CRFlag=''出库''');
    sql.Add('and KCID=' + QuotedStr(Trim(CDS_Main.fieldbyname('KCID').AsString)));
    Open;
  end;
  if ADOQueryTmp.IsEmpty = False then
  begin
    Application.MessageBox('已出库数据不能操作!', '提示', 0);
    Exit;
  end;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR_Sub where YFPZNO=''' + Trim(CDS_Main.fieldbyname('CRID').AsString) + '''');
    Open;
  end;
  if ADOQueryTmp.IsEmpty = False then
  begin
    Application.MessageBox('已经生成财务数据不能操作!', '提示', 0);
    Exit;
  end;
  if (cds_main.fieldbyname('Filler').AsString <> trim(DName)) and (trim(canshu1) <> '高权限') then
  begin
    application.MessageBox('操作员不正确，不能修改！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    if Trim(CDS_Main.FieldByName('CRID').AsString) <> '' then
    begin
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('update CK_PB_CR set Valid=''N'' where CRID=' + quotedstr(Trim(CDS_Main.FieldByName('CRID').AsString)));
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec P_Up_PBKc ' + quotedstr(Trim(CDS_Main.FieldByName('KCID').AsString)));
        Open;
      end;
      if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('出库数量超出入库数量!', '提示', 0);
        Exit;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    end;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmPBRklist.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmPBRklist.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

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

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

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

procedure TfrmPBRklist.ToolButton1Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  try
    frmFjList_RZ := TfrmFjList_RZ.Create(Application);
    with frmFjList_RZ do
    begin
      fkeyNO := Trim(Self.CDS_Main.fieldbyname('CRNO').AsString);
      fType := '坯布汇总附件';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmFjList_RZ.Free;
  end;
end;

procedure TfrmPBRklist.ToolButton2Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName, WSql, MCKBM: string;
  i, j: Integer;
  OrderKg: Double;
begin
//  if not CDS_Main.Locate('SSel', True, []) then
//  begin
//    Application.MessageBox('请选择数据!', '提示', 0);
//    Exit;
//  end;

  LBName := '坯布入库单';
  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryTmp);

  with ADOQueryTmp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Company A  where A.CoType=''客户'' and A.Valid=''Y'' ');
    sql.Add(' and A.CoName=' + QuotedStr(CDS_Main.FieldByName('fromFactoryName').AsString));
    Open;
  end;
  if ADOQueryTmp.IsEmpty then
  begin
    Exit;
  end
  else
  begin
    if Trim(ADOQueryTmp.FieldByName('ISHS').AsString) = '是' then
    begin
      with ADOQueryTmp do
      begin
        Close;
        sql.Clear;
        sql.Add('select A.*  from CK_PB_CR A ');
        sql.Add('where   A.CRFlag=''入库''  and A.Valid=''Y'' ');
        sql.add('and isnull(CKBM,'''')='''' and CRNO=' + QuotedStr(CDS_Main.FieldByName('CRNO').AsString));
//        ShowMessage(SQL.text);
        Open;
      end;

      if not ADOQueryTmp.IsEmpty then
      begin
        if GetLSNo(ADOQueryTmp, MCKBM, 'Y' + formatdateTime('yyyy', SGetServerDate10(ADOQueryTmp)), 'CK_PB_CR', 6, 0) = False then
        begin
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' update  CK_PB_CR set CKBM=' + QuotedStr(MCKBM));
          sql.Add('where    CRNO=' + QuotedStr(CDS_Main.FieldByName('CRNO').AsString));
          ExecSQL;
        end;
      end;
    end;
  end;

  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* , nyr=cast( DATEPART(YEAR, CRTime) AS varchar)+ ''年''+cast( DATEPART(MONTH, CRTime) AS varchar) + ''月''+cast( DATEPART(DAY, CRTime) AS varchar)+ ''日''   ');
    sql.Add('   from CK_PB_CR A ');
    sql.Add('where   A.CRFlag=''入库''  and A.Valid=''Y'' ');
    sql.add('and   CRNO=' + QuotedStr(CDS_Main.FieldByName('CRNO').AsString));
//    ShowMessage(SQL.text);
    Open;
  end;
  SCreateCDS20(ADOQueryPrint, CDS_PRT);
  SInitCDSData20(ADOQueryPrint, CDS_PRT);

  for i := CDS_PRT.RecordCount mod 10 to 9 do
  begin
    with CDS_PRT do
    begin
      Append;
      FieldByName('PS').Value := 0;
      FieldByName('Qty').Value := 0;
      Post;
    end;
  end;

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RMVariables['SYRName'] := Trim(SYRName);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;
end;

procedure TfrmPBRklist.ToolButton3Click(Sender: TObject);
var
  MStrsql, MStrIn: string;
begin

  if CDS_Main.IsEmpty then
    Exit;
//  if cxTabControl1.TabIndex <> 2 then
//    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要修改数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
//  MovePanel2.Visible := True;
//  MovePanel2.Refresh;
  try
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      MStrsql := '';
      First;
      while not Eof do
      begin
        if CDS_Main.FieldByName('SSel').AsBoolean then
        begin
          if MStrsql = '' then
            MStrsql := Quotedstr(trim(CDS_Main.FieldByName('CRID').AsString))
          else
            MStrsql := MStrsql + ',' + Quotedstr(trim(CDS_Main.FieldByName('CRID').AsString));
        end;

        Next;
      end;
    end;
    CDS_Main.EnableControls;
//    MovePanel2.Visible := False;
  except
//    MovePanel2.Visible := False;
//    CDS_Main.EnableControls;
  end;

  if InputQuery('编号', '请输入', MStrIn) then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_PB_CR Set CKBM=' + QuotedStr(MStrIn));
      sql.Add(' where CRID in (' + MStrsql + ')');
//      showmessage(sql.text);
      ExecSQL;
    end;

  end;
  InitGrid();
end;

end.

