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, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, RM_System, RM_Dataset, RM_Common,
  RM_Class, RM_GridReport, cxPC, Menus, Clipbrd;

type
  TfrmCPRklist = 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;
    v1P_SPEC: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    v1Filler: TcxGridDBColumn;
    v1FillTime: TcxGridDBColumn;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    P_CodeName: TEdit;
    P_Color: TEdit;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1MJID: TcxGridDBColumn;
    ckName: TComboBox;
    Label6: TLabel;
    v1P_Color: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1INoutNo: TcxGridDBColumn;
    Label7: TLabel;
    inoutNo: TEdit;
    v1Price: TcxGridDBColumn;
    v1money: TcxGridDBColumn;
    kuwei: TEdit;
    Label9: TLabel;
    gangNo: TEdit;
    Label10: TLabel;
    v1Column3: TcxGridDBColumn;
    RMDB_Main: TRMDBDataSet;
    v1Column4: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    OrderNo: TEdit;
    Label14: TLabel;
    P_Code: TEdit;
    Label15: TLabel;
    v1Column10: TcxGridDBColumn;
    Label17: TLabel;
    FactoryName: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Label8: TLabel;
    fromFactoryName: TEdit;
    Label11: TLabel;
    ganghao: TEdit;
    N3: TMenuItem;
    cxTabControl1: TcxTabControl;
    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 TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure N3Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    fCKName, canshu1: string;
    fCRType: string;
    { Public declarations }
  end;

var
  frmCPRklist: TfrmCPRklist;

implementation

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

procedure TfrmCPRklist.SetStatus();
begin
  TBEdit.Visible := False;
  TBDel.Visible := False;

  if Trim(canshu1) = '高权限' then
  begin
    TBEdit.Visible := true;
    TBDel.Visible := true;
  end
  else
  begin

  end;
end;

procedure TfrmCPRklist.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* from CK_ML_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 CKName=''成品'' and A.CRFlag=''入库''  and A.Valid=''Y''  ');

      if cxTabControl1.TabIndex <> 2 then
      begin
        if cxTabControl1.TabIndex = 0 then
          SQL.Add(' and not exists(select YFPZNO from YF_Money_CR_Sub X where X.YFPZNO=A.CRNO ) ')
        else
          SQL.Add(' and  exists(select YFPZNO from YF_Money_CR_Sub X where X.YFPZNO=A.CRNO )');
      end;
      sql.Add(' order by ISRCCK desc  ');
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
    TBFind.Click;
  finally
  end;
end;

procedure TfrmCPRklist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  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;
    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, Tv1, '面料仓库');
  SetStatus();
  InitGrid();
end;

procedure TfrmCPRklist.FormCreate(Sender: TObject);
begin
  EndDate.Date := SGetServerDate10(ADOQueryTmp);
  BegDate.Date := EndDate.Date - 30;
  canshu1 := trim(DParameters1);
end;

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

procedure TfrmCPRklist.TBEditClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  if cds_main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if trim(cds_main.fieldbyname('ISRCCK').AsString) <> '染厂发货' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.* ');
      sql.Add('from CK_ML_CR A');
      SQL.Add('inner join CK_ML_KC B on B.MJID=A.MJID  ');
      SQL.Add('where A.CRNO=''' + trim(cds_main.fieldbyname('CRNO').AsString) + ''' ');
      SQL.Add('and B.Qty<>A.Qty and A.valid=''Y'' ');
      Open;
    end;
    if not ADOQueryCmd.IsEmpty then
    begin
      Application.MessageBox('此单已经产生出库，不能修改！', '提示', 0);
      Exit;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_ML_CR set ISXG=1  where CRNO=' + quotedstr('R' + Trim(CDS_Main.FieldByName('MJID').AsString)));
    ExecSQL;
  end;
  frmCPRk := TfrmCPRk.create(self);
  with frmCPRk do
  begin
    fkeyNo := self.cds_main.fieldbyname('CRNO').AsString;
    fType := 1;
    fckName := self.fckName;
    fCRType := self.fCRType;
    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 CDS_Main.Locate('ssel', true, []) = false then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Trim(CDS_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    if Trim(CDS_Main.FieldByName('CRNO').AsString) <> '' then
    begin
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('update CK_ML_CR set Valid=''N'' where CRNO=' + quotedstr(Trim(CDS_Main.FieldByName('CRNO').AsString)));
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        SQL.Add('exec P_Up_MlKc ' + quotedstr(Trim(CDS_Main.FieldByName('MJID').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 TfrmCPRklist.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

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

procedure TfrmCPRklist.TBFindClick(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.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

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

procedure TfrmCPRklist.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('ISRCCK').index] = '染厂发货' then
  begin
    ACanvas.Font.Color := $0000FF;
    if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('Qty').index] <= 0 then
    begin
      ACanvas.Brush.Color := $00FFFF;
    end;
    if AViewInfo.GridRecord.Values[tv1.GetColumnByFieldName('ISXG').index] = 1 then
    begin
      ACanvas.Brush.Color := $F0CAA6;
    end;

  end;
end;

procedure TfrmCPRklist.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

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

end.

