unit U_CPRklist;

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, RM_Common,
  RM_Class, RM_e_Xls, RM_GridReport, RM_System, RM_Dataset, cxPC, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, 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
  TfrmCPRklist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    v1CRNO: TcxGridDBColumn;
    v1CRID: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    P_CodeName: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1CPType: TcxGridDBColumn;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Code: TcxGridDBColumn;
    TBAddSM: TToolButton;
    v1INoutNo: TcxGridDBColumn;
    Label7: TLabel;
    inoutNo: TEdit;
    v1custName: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1money: TcxGridDBColumn;
    custName: TEdit;
    Label8: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    AdoPrint: TADOQuery;
    AdoPrint1: TADOQuery;
    RMDB_MD: TRMDBDataSet;
    RMDB_Main: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    cxTabControl1: TcxTabControl;
    TBCdel: TToolButton;
    v1Column4: TcxGridDBColumn;
    orderNo: TEdit;
    Label9: TLabel;
    P_Code: TEdit;
    Label11: TLabel;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    TV2Column14: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    TV2Column10: TcxGridDBColumn;
    TV2Column11: TcxGridDBColumn;
    TV2Column12: TcxGridDBColumn;
    TV2Column13: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    CDS_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuerySub: TADOQuery;
    TV2Column15: TcxGridDBColumn;
    TV2Column16: TcxGridDBColumn;
    TV2Column17: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure P_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 TBPrintClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBAddSMClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TBCdelClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private
    procedure InitGrid();
    procedure InitSub();
    procedure SetComboBox();
    procedure SetStatus();
    { Private declarations }
  public
    fCKName: string;                                 
    { Public declarations }
  end;

var
  frmCPRklist: TfrmCPRklist;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_BpRk, U_BpSmRk, U_CPRk;
{$R *.dfm}

procedure TfrmCPRklist.SetStatus();
begin
  TBedit.Visible := false;
  Tbdel.Visible := false;
  tbcdel.Visible := false;
  case cxTabControl1.TabIndex of
    0:
      begin
        TBedit.Visible := true;
        Tbdel.Visible := true;
      end;
    1:
      begin
        tbcdel.Visible := true;
      end;
  end;
end;

procedure TfrmCPRklist.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''CPCK'' ');
    if trim(fCKName) <> '' then
      sql.Add('and zdyName=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      ckName.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if ckName.Items.Count > 0 then
    ckName.ItemIndex := 0;

  CRType.Items.Clear;
  CRType.Items.Add('');
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''CPRKTYPE''');
    if trim(fCKName) <> '' then
      sql.Add('and note=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count > 0 then
    CRType.ItemIndex := 0;
end;

procedure TfrmCPRklist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add(' exec P_Get_CP_RK ''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''',''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''',''' + inttostr(cxTabControl1.TabIndex) + ''' ');

//      sql.Add('select A.*,');
//      sql.Add('orderNo=(select Top 1 orderNo from JYOrder_Main X where X.MainID=A.MainID )');
//
//      sql.Add('from CP_InOut A');
//      sql.Add('where CRTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
//      sql.Add('and CRTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
//      sql.Add('and CRFlag=''入库'' ');
//      if trim(FckName) <> '' then
//        sql.Add('and ckName=' + quotedstr(trim(fckName)));
//      if cxTabControl1.TabIndex = 0 then
//        sql.Add('and valid=''Y'' ');
//      if cxTabControl1.TabIndex = 1 then
//        sql.Add('and valid=''N'' ');
//      ShowMessage(sql.Text);
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
  finally
  end;
end;

procedure TfrmCPRklist.InitSub();
begin
  if CDS_Main.IsEmpty then
    Exit;
  try
    with ADOQuerySub do
    begin
      close;
      sql.Clear;
      sql.Add('exec P_Get_CP_RK_MX ''' + Cds_main.fieldbyname('P_CodeName').AsString + ''' ,''' + Cds_main.fieldbyname('P_Code').AsString + ''',''' + Cds_main.fieldbyname('CRNO').AsString + ''' ');
      sql.Add(' ,''' + cds_main.fieldbyname('DRCKNO').AsString + ''' ,''' + cds_main.fieldbyname('DRCKName').AsString + ''' ');
//     ShowMessage(sql.text);
      open;
    end;
    SCreateCDS20(ADOQuerySub, CDS_Sub);
    SInitCDSData20(ADOQuerySub, CDS_Sub);
  finally
  end;
end;

procedure TfrmCPRklist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption + '1', Tv1, '成品仓库');
  Action := cafree;
end;

procedure TfrmCPRklist.FormDestroy(Sender: TObject);
begin
  frmCpRklist := nil;
end;

procedure TfrmCPRklist.TBAddClick(Sender: TObject);
begin
  frmCpRk := TfrmCpRk.create(self);
  with frmCpRk do
  begin
    fkeyNo := '';
    fType := 0;
    fckName := self.fckName;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

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

procedure TfrmCPRklist.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption + '1', Tv1, '成品仓库');
  SetComboBox();
  SetStatus();
  InitGrid();
end;

procedure TfrmCPRklist.FormCreate(Sender: TObject);
begin
  BegDate.Date := DServerDate - 7;
  EndDate.Date := DServerDate;
  cxGrid1.Align := alclient;

end;

procedure TfrmCPRklist.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmCPRklist.TBEditClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  if Trim(CDS_Main.fieldbyname('CRType').AsString) = Trim('加工入库') then
  begin
    Application.MessageBox('此单是加工入库，请在坯布出库里面修改！', '提示', 0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from YF_Money_CR A');
    SQL.Add(' where A.mainid=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
    sql.Add(' and status>=''1'' ');
    Open;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    Application.MessageBox('应付款没有撤销审核，不能修改！', '提示', 0);
    Exit;
  end;
{
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_InOut A');
    SQL.Add('where A.kcid='''+Trim(CDS_Main.fieldbyname('kcid').AsString)+''' ');
    sQL.Add('and A.CRFlag=''出库''');
    Open;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    Application.MessageBox('已产生出库数据，不能修改！','提示',0);
    Exit;
  end;    }

  frmCPRk := TfrmCPRk.create(self);
  with frmCPRk do                                        
  begin
    fkeyNo := self.cds_main.fieldbyname('CRNO').AsString;
    FCode := self.cds_main.fieldbyname('P_Code').AsString;
    FCodeName := self.cds_main.fieldbyname('P_Codename').AsString;
    FDRCKNO := self.cds_main.fieldbyname('DRCKNO').AsString;
    FDRCKName := self.cds_main.fieldbyname('DRCKName').AsString;
    fType := 1;
    fckName := self.fckName;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmCPRklist.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
 { if Trim(CDS_Main.fieldbyname('Filler').AsString)<>Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!','提示',0);
    Exit;
  end; }
  if Trim(CDS_Main.fieldbyname('CRType').AsString) = Trim('加工入库') then
  begin
    Application.MessageBox('此单是加工入库，请在坯布出库里面作废！', '提示', 0);
    Exit;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from YF_Money_CR A');
    SQL.Add(' where A.mainid=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
    sql.Add(' and status>=''1'' ');
    Open;
  end;
  if not ADOQueryCmd.IsEmpty then
  begin
    Application.MessageBox('应付款没有撤销审核，不能作废！', '提示', 0);
    Exit;
  end;

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

  try

    with ADOQueryCmd do
    begin
      Close;                        
      sql.Clear;             
      sql.Add('delete from YF_Money_CR ');
      SQL.Add('where mainID=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
      sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');

      sql.Add('delete from CP_kc ');
      SQL.Add('where CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      SQL.Add('and ISNULL(P_Code,'''')=''' + trim(CDS_Main.fieldbyname('P_Code').AsString) + ''' ');
      SQL.Add('and ISNULL(P_CodeName,'''')=''' + trim(CDS_Main.fieldbyname('P_CodeName').AsString) + ''' ');
      SQL.Add('and ISNULL(DRCKNO,'''')=''' + trim(CDS_Main.fieldbyname('DRCKNO').AsString) + ''' ');
      SQL.Add('and ISNULL(DRCKName,'''')=''' + trim(CDS_Main.fieldbyname('DRCKName').AsString) + ''' ');

      sql.Add('update CP_InOut SET valid=''N'' ');
      SQL.Add('where  CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      SQL.Add('and ISNULL(P_Code,'''')=''' + trim(CDS_Main.fieldbyname('P_Code').AsString) + ''' ');
      SQL.Add('and ISNULL(P_CodeName,'''')=''' + trim(CDS_Main.fieldbyname('P_CodeName').AsString) + ''' ');
      SQL.Add('and ISNULL(DRCKNO,'''')=''' + trim(CDS_Main.fieldbyname('DRCKNO').AsString) + ''' ');
      SQL.Add('and ISNULL(DRCKName,'''')=''' + trim(CDS_Main.fieldbyname('DRCKName').AsString) + ''' ');
      execsql;
    end;
    TBRafresh.Click;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

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

procedure TfrmCPRklist.TBPrintClick(Sender: TObject);
var
  filepath: string;
begin
  if CDS_Main.IsEmpty then
    exit;
  if CDS_Main.FieldByName('CRType').AsString <> '退货入库' then
  begin
    application.MessageBox('不是退货入库，不能打印码单！', '提示信息', 0);
    exit;
  end;

  with AdoPrint do
  begin
    close;
    sql.Clear;
    sql.Add('select A.* from CP_InOut A');
    sql.Add('where inoutNO=' + quotedstr(trim(CDS_Main.fieldbyname('inoutNO').AsString)));
    open;
  end;

  with AdoPrint1 do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Print_cpCkMd ');
    sql.Add('@inoutNo=' + quotedstr(trim(CDS_Main.fieldbyname('inoutNO').AsString)));
    sql.Add(',@flag=' + quotedstr(trim('')));
    open;
  end;

  try
    filepath := ExtractFilePath(Application.ExeName) + 'report\退货码单.rmf';
    if not FileExists(Pchar(filepath)) then
    begin
      application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
      exit;
    end;
    RMVariables['dtxz'] := FormatDateTime('yyyy-mm-dd', date());
    RMVariables['zdr'] := trim(DName);
    RMVariables['Filler'] := trim(CDS_Main.fieldbyname('Filler').AsString);
    RMGridReport1.LoadFromFile(filepath);
    RMGridReport1.ShowReport;
  finally

  end;
end;

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

procedure TfrmCPRklist.TBAddSMClick(Sender: TObject);
begin
  frmBpSmRk := TfrmBpSmRk.create(self);
  with frmBpSmRk do
  begin
    fkeyNo := '';
    fType := 0;
    fckName := self.fckName;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
    //  self.CDS_Main.Locate('crNo',trim(fkeyNo),[]) ;
    end;
    free;
  end;
end;

procedure TfrmCPRklist.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmCPRklist.TBCdelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from CP_InOut where CRNO=''' + Trim(CDS_Main.fieldbyname('CRNO').AsString) + ''' ');
      SQL.Add('and ISNULL(P_Code,'''')=''' + trim(CDS_Main.fieldbyname('P_Code').AsString) + ''' ');
      SQL.Add('and ISNULL(P_CodeName,'''')=''' + trim(CDS_Main.fieldbyname('P_CodeName').AsString) + ''' ');
      SQL.Add('and ISNULL(DRCKNO,'''')=''' + trim(CDS_Main.fieldbyname('DRCKNO').AsString) + ''' ');
      SQL.Add('and ISNULL(DRCKName,'''')=''' + trim(CDS_Main.fieldbyname('DRCKName').AsString) + ''' ');
      execsql;
    end;
    TBRafresh.Click;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPRklist.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  initsub();
end;

end.

