unit U_CKProductBCPInList;

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, cxCheckBox, RM_Common,
  RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, Menus,
  cxCalendar;

type
  TfrmCKProductBCPInList = 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;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Label5: TLabel;
    orderNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType: TComboBox;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label10: TLabel;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    TBRKCX: TToolButton;
    v1Column4: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column11: TcxGridDBColumn;
    PRTColor: TComboBox;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    AOrdDefStr1: TComboBox;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    ComboBox1: TComboBox;
    MJXHM: TEdit;
    Label11: TLabel;
    CheckBox1: TCheckBox;
    Label12: TLabel;
    RKOrdID: TEdit;
    Panel4: TPanel;
    Label13: TLabel;
    Panel10: TPanel;
    Image2: TImage;
    RichEdit1: TRichEdit;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    conNo: TEdit;
    Label14: TLabel;
    KHConNo: TEdit;
    Label15: TLabel;
    APID: TEdit;
    Label16: TLabel;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    Label17: TLabel;
    PRTKuanNO: TEdit;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    Label18: TLabel;
    RKPlace: TEdit;
    Label19: TLabel;
    ISbao: TComboBox;
    RMDBPRT: TRMDBDataSet;
    PRTHX: TEdit;
    Label20: TLabel;
    v1Column24: TcxGridDBColumn;
    Label21: TLabel;
    SOrddefstr1: TEdit;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    Label22: TLabel;
    MJStr2: 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 TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBRKCXClick(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure PRTColorChange(Sender: TObject);
    procedure AOrdDefStr1Change(Sender: TObject);
    procedure CheckBox1Click(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 ISbaoChange(Sender: TObject);

  private
    procedure InitGrid();
    { Private declarations }
  public
      FLeft,FTop:Integer;
    { Public declarations }
  end;

var
  frmCKProductBCPInList: TfrmCKProductBCPInList;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmCKProductBCPInList.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPInList:=nil;
end;


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

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

procedure TfrmCKProductBCPInList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,cast(A.baono as int) intbaono,B.OrderNo,B.conNo,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.prtkuanNo,C.PRTColor,C.SOrddefstr1,C.SOrddefstr4,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTHX,D.MJQty3,D.MJQty4');
      sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
      sql.Add(',KHConNo=(select Top 1 KHConNo from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
      sql.Add(',MPRTECodeName=(select Top 1 MPRTCodeName from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
      sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM ,D.MJStr2');
      sql.add('from CK_BanCP_CR A ');
      Sql.add(' inner join JYOrder_Main B on A.MainId=B.MainId');
      Sql.add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
      Sql.add(' inner join WFB_MJJY D on A.MJId=D.MJId');
 //     sql.Add(' inner join JYOrder_Sub_AnPai E on A.APID=E.APID');
      sql.add(' where A.CRTime>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+''' ');
      sql.Add(' and A.CRTime<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+''' ');
      SQL.Add(' and CRFlag=''入库'' ');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCPInList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

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

procedure TfrmCKProductBCPInList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('半成品仓库入库',Tv1,'半成品仓库');
  Close;
end;

procedure TfrmCKProductBCPInList.FormShow(Sender: TObject);
begin

  ReadCxGrid('半成品仓库入库',Tv1,'半成品仓库');
  if Trim(DParameters2)='管理' then
  begin
    TBRKCX.Visible:=True;
  end else
  begin
  //  v1Column4.Visible:=False;

  end;
  //InitGrid();
end;

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

procedure TfrmCKProductBCPInList.TBFindClick(Sender: TObject);
var
  fsj,fwhere:String;
begin
  if ADOQueryMain.Active=False then Exit;

  fwhere:=SGetFilters(Panel1,1,2);
  fsj:='';
  if trim(ISbao.Text)='已打包' then
  begin
    fsj:=fsj+' baoNo>''0'' ';
  end else
  if trim(ISbao.Text)='未打包' then
  begin
    fsj:=fsj+' baoNo='''' ';
  end;

  IF trim(fsj)<>'' then
  begin
    IF fwhere='' then fwhere:=fsj
    else fwhere:=fwhere+' and '+fsj;
  end;

  SDofilter(ADOQueryMain,fwhere);
  SCreateCDS20(ADOQueryMain,CDS_Main);
  SInitCDSData20(ADOQueryMain,CDS_Main);
end;

procedure TfrmCKProductBCPInList.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPInList.TBPrintClick(Sender: TObject);
var
  fPrintFile,fPrintFile10,FMainID:String;
begin
  if CDS_Main.IsEmpty then Exit;
  IF trim(ComboBox1.Text)='' then  exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据','提示',0);
    Exit;
  end;
//
  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
        IF FMainID='' then
        begin
          FMainID:=Trim(CDS_Main.fieldbyname('mainID').AsString);
        end
        else
        begin
          IF Trim(CDS_Main.fieldbyname('mainID').AsString)<>FMainID then
          begin
            application.MessageBox('选择的不是同一个指示单，不能一起打印！','提示信息',0);
            ADOQueryCmd.Connection.RollbackTrans;
            EnableControls;
            exit;
          end;
        end;
          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 trim(ComboBox1.Text)='入库明细码单' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Print_RKMD'+quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_PRT);
    SInitCDSData20(ADOQueryTemp,CDS_PRT);
  end;
   IF trim(ComboBox1.Text)='入库成品码单' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Print_RKMD1'+quotedstr(Trim(DCode)));
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,CDS_PRT);
    SInitCDSData20(ADOQueryTemp,CDS_PRT);
  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 TfrmCKProductBCPInList.orderNoChange(Sender: TObject);
begin
//  if Length(Trim(orderNo.Text))<4 then Exit;
  TBFind.Click;
end;

procedure TfrmCKProductBCPInList.orderNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Length(Trim(orderNo.Text))<3 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.OrderNo,B.conNo,B.MPRTCodeName,B.MPRTSpec,B.MprtkuanNo,C.prtkuanNo,C.PRTColor,C.SOrddefstr1,C.SOrddefstr4,B.MPRTMF,B.MPRTKZ,D.MJXH,C.PRTHX,D.MJQty3,D.MJQty4');
        sql.Add(',isnull(customerNoName,B.OrderNo) KHName');
        sql.Add(',KHConNo=(select Top 1 KHConNo from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
        sql.Add(',MPRTECodeName=(select Top 1 MPRTCodeName from JYOrderCon_Main JCM where JCM.ConNo=B.OrderNo)');
        sql.Add(',D.mjstr4 as AOrdDefStr1,cast(D.MJXH as varchar) as MJXHM');
        sql.add('from CK_BanCP_CR A ');
        Sql.add(' inner join JYOrder_Main B on B.MainId=A.MainId');
        Sql.add(' inner join JYOrder_Sub C on C.mainID=A.mainID and C.SubId=A.SubId');
        Sql.add(' inner join WFB_MJJY D on D.MJId=A.MJId');
        sql.add(' where B.OrderNo like '+quotedstr('%'+trim(orderNo.Text)+'%'));
        SQL.Add(' and CRFlag=''入库'' ');
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

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

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

procedure TfrmCKProductBCPInList.TBRKCXClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行操作吗？','提示',32+4)<>IDYES then Exit;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete TBValueWhere where DName='+quotedstr(Trim(DName)));
      SQL.Add('and MainType='+quotedstr('撤销入库') );
      ExecSQL;
    end;
    with CDS_Main do
    begin
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_CR where CRID='+Trim(CDS_Main.fieldbyname('CRID').AsString));
          sql.Add('and CRFlag in(''入库'',''出库'') ');
          Open;
        end;
        if ADOQueryTemp.RecordCount>1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已出库不能撤销入库!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into TBValueWhere(DName,MainType,wsql1) ');
          sql.Add('values('+quotedstr(Trim(DName))+','+quotedstr('撤销入库')
          +','+quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString))+')');
          ExecSQL;
        end;

        CDS_Main.Delete;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_BanCP_CR SET CK_BanCP_CR.CRFlag=''待入库'',CK_BanCP_CR.CRType='''',CK_BanCP_CR.FillTime=''''');
      sql.Add(',CK_BanCP_CR.CRTime=null,CK_BanCP_CR.RKOrdId='''' ,CK_BanCP_CR.RKPlace='''' ,CK_BanCP_CR.Filler='''' ');
      sql.Add('FROM TBValueWhere A where A.wsql1=CK_BanCP_CR.MJID and CK_BanCP_CR.CRFlag=''入库'' and A.DName='+quotedstr(Trim(DName))+' and MainType='+quotedstr('撤销入库'));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update WFB_MJJY Set MJStr2=''未入库''where not exists(select MJID from CK_BanCP_CR X where X.MJID=WFB_MJJY.MJID and X.CRFlag=''入库'' and MJStr2<>''未入库'')');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Update_Order_JD_ByOther @MainType=''撤销入库'' ');
      sql.Add(',@DName='+quotedstr(Trim(DName)));
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
   
    CDS_Main.EnableControls;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销失败!','提示',0);
  end;
end;

procedure TfrmCKProductBCPInList.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_Main.IsEmpty=False then
  begin
    InitOrderColor(Trim(CDS_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
    InitBCGangNo(Trim(CDS_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  end;
end;

procedure TfrmCKProductBCPInList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
  if CDS_Main.IsEmpty=False then
  begin
    //InitOrderColor(Trim(CDS_Main.fieldbyname('MainId').AsString),PRTColor,ADOQueryTemp);
    InitBCGangNo(Trim(CDS_Main.fieldbyname('SubId').AsString),AOrdDefStr1,ADOQueryTemp);
  end;
end;

procedure TfrmCKProductBCPInList.AOrdDefStr1Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPInList.CheckBox1Click(Sender: TObject);
begin
    SelOKNo(CDS_Main,CheckBox1.Checked);
end;

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

procedure TfrmCKProductBCPInList.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 TfrmCKProductBCPInList.Tv1MouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft:=X;
  FTop:=Y;
end;

procedure TfrmCKProductBCPInList.ISbaoChange(Sender: TObject);
begin
  Tbfind.Click;
end;

end.
