unit U_FinishedLSOutList;

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, RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System,
  RM_GridReport, cxCheckBox, Menus, MovePanel, BtnEdit, cxPC, Math,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmFinishedLSOutList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    TBCKCX: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    TBPrint: TToolButton;
    cxgrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxgrdMJLen: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxgrid1Level1: TcxGridLevel;
    MovePanel2: TMovePanel;
    v1Column8: TcxGridDBColumn;
    Label5: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Y_CodeName: TEdit;
    Y_Spec: TEdit;
    FactoryName: TEdit;
    BatchNo: TEdit;
    Edit1: TEdit;
    Y_CodeNameCP: TEdit;
    Label9: TLabel;
    KHName: TEdit;
    TBFind: TToolButton;
    v1Column6: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label10: TLabel;
    SQType: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    Tv1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    CDS_WXTS: TClientDataSet;
    cxTabControl1: TcxTabControl;
    ToolButton2: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBCKCXClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure KHNameChange(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Tv2DblClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure SetStatus();
    procedure SaveCKDataJTMCX();
    procedure SendMessage(Mfsid, MY_CodeNameCP, MT_Type,key2str: string);
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmFinishedLSOutList.SetStatus();
begin
  if canshu1='管理' then
  begin
    TBPrint.Visible:=True;
  end else
  begin
    TBPrint.Visible:=False;
    ToolBar1.Refresh;
  end;
end;

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

procedure TfrmFinishedLSOutList.FormCreate(Sender: TObject);
begin
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmFinishedLSOutList.InitGrid();
var
  fwhere, Pwhere: string;
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select * from SQ_FHYT_Sub A inner join SQ_FHYT_Main B on A.FHMainId=B.FHMainId ');
      sql.add('where B.FHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + ''' ');
      sql.Add('and B.FHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      if cxTabControl1.TabIndex=0 then
      begin
        SQL.Add(' and B.EndFlag=0');
      end else
      if cxTabControl1.TabIndex=1 then
      begin
        SQL.Add(' and B.EndFlag=1');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmFinishedLSOutList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption+'JTS', Tv1, 'JTS仓库');
  Close;
end;

procedure TfrmFinishedLSOutList.FormShow(Sender: TObject);
begin
  ReadCxGrid(Self.Caption+'JTS', Tv1, 'JTS仓库');
  SetStatus();
  InitGrid();
end;

procedure TfrmFinishedLSOutList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  SelExportData(Tv1, ADOQueryMain, '');
end;

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

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

procedure TfrmFinishedLSOutList.TBCKCXClick(Sender: TObject);
var
  FFMainId, FPrice, FHZ, FCKOrderNo: string;
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;

  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    with CDS_Main do
    begin
      First;
      while CDS_Main.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('EXEC  P_LSCK_UP_CKRE   @JYID=' + quotedstr(trim(CDS_Main.fieldbyname('LSID').asstring)));
            sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
            sql.Add(' ' + quotedstr(trim(DName)));
            sql.Add(',getdate() ');
            sql.Add(',' + quotedstr(trim(self.Caption)));
            sql.Add(',' + quotedstr(trim('加弹丝出库撤销')));
            sql.Add(',' + quotedstr(trim('条码：' + trim(CDS_Main.FieldByName('LSID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            ExecSQL;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('exec P_FHSQLS_UP_QTY  ' + quotedstr(trim(CDS_Main.fieldbyname('CKNo').asstring)));
            ExecSQL;
          end;
          CDS_Main.Delete;
        end;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible := False;
    Exit;
  except
    MovePanel2.Visible := False;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！', '提示', 0);
  end;

end;

procedure TfrmFinishedLSOutList.TBPrintClick(Sender: TObject);
var
  fPrintFile: string;
begin
  if CDS_Main.IsEmpty then  EXIT;
  if cxTabControl1.TabIndex=0 then
  begin
    Application.MessageBox('没有结束，不能打印码单！','提示',0);
    Exit;
  end;
  ExportFtErpFile('加弹丝码单.rmf', ADOQueryTemp);
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CKMDJTS ' + quotedstr(Trim(CDS_Main.fieldbyname('FHMainId').AsString)));
    Open;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\加弹丝码单.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.DefaultCopies := 2;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

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

procedure TfrmFinishedLSOutList.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_YT_CR where isnull(CKNO,'''')='''+Trim(CDS_Main.fieldbyname('FHMainId').AsString)+'''');
    if CDS_Main.IsEmpty=False then
    begin
      sql.Add(' and isnull(CKNO,'''')<>'''' ');
    end else
    begin
      sql.Add(' and 1=2 ');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;

procedure TfrmFinishedLSOutList.Tv2DblClick(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(canshu1)<>'管理' then Exit;
  if Application.MessageBox('确定要撤销出库数据吗？','提示',32+4)<>IDYES then Exit;
  SaveCKDataJTMCX();
end;
procedure TfrmFinishedLSOutList.SaveCKDataJTMCX();
var
  FSMStr,FCKOrdNo:string;
begin
  FSMStr:=Trim(ClientDataSet2.fieldbyname('JYID').AsString);
  FCKOrdNo:=Trim(CDS_Main.fieldbyname('FHMainId').AsString);
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update CK_YT_CR Set CRFlag=''待出库'',CRType=NULL,CRTime=NULL,CRNote=NULL,CKNo=NULL ');
      sql.Add(' where  CK_YT_CR.JYID=' + Quotedstr(Trim(FSMStr)));
      sql.Add(' and CRFlag=''出库''');

      sql.Add(' UPdate CK_YT_KC Set Valid=''Y'' ');
      sql.Add(' where  CK_YT_KC.JYID=' + Quotedstr(Trim(FSMStr)));
      sql.Add(' and Valid=''N'' ');

      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_FHSQYT_UP_QTY  ' + quotedstr(trim(FCKOrdNo)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(''));
      sql.Add(',' + quotedstr(trim('加弹丝出库撤销')));
      sql.Add(',' + quotedstr('条码：' + trim(FSMStr) + ' 出库单号：' + trim(FCKOrdNo)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    with CDS_Main do
    begin
      Edit;
      FieldByName('FHPiQty').Value:=FieldByName('FHPiQty').Value-1;
      FieldByName('FHQtyMZ').Value:=FieldByName('FHQtyMZ').Value-ClientDataSet2.fieldbyname('JYGW').Value;
      FieldByName('FHQty').Value:=FieldByName('FHQty').Value-ClientDataSet2.fieldbyname('JYNW').Value;
      Post;
    end;
    ClientDataSet2.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmFinishedLSOutList.SendMessage(Mfsid, MY_CodeNameCP, MT_Type,key2str: string);
var
  FDateTime:TDateTime;
begin
  FDateTime:=SGetServerDateTime(ADOQueryTemp);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' exec P_View_JTSOutTS  ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_WXTS);
  SInitCDSData20(ADOQueryTemp, CDS_WXTS);

  CDS_WXTS.First;
  while not CDS_WXTS.eof do
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from WeixinMessage where 1<>1');
      Open;
      Append;
      FieldByName('MessageType').Value := 'OrderProgress';
      FieldByName('MessageName').Value := '本厂加弹丝发货';
      FieldByName('Title').Value := '您有本厂加弹丝发货记录待确认'; //first data

      FieldByName('URL').Value :='subpages/yarn/fhConfirm?fhid=' + Trim(Mfsid);
      FieldByName('OpenId').Value := Trim(CDS_WXTS.FieldByName('wxid').AsString);
      FieldByName('Status').Value := '0';
      FieldByName('CreateDate').Value := FormatDateTime('yyyymmdd', FDateTime);
      FieldByName('CreateTime').Value := FormatDateTime('HH:mm:ss', FDateTime);
      FieldByName('BusinessId').Value := Trim(Mfsid); // 发货申请单号
      FieldByName('KeyWord1').Value := Trim(MY_CodeNameCP);   //产品名称
      FieldByName('KeyWord2').Value := Trim(MT_Type); //等级
      FieldByName('remark').Value :=key2str; //remark
      Post;
    end;
    CDS_WXTS.Next;
  end;

end;


procedure TfrmFinishedLSOutList.ToolButton1Click(Sender: TObject);
var
  FMfsid,FMY_CodeNameCP,FMY_Type,Fkey2str:string;
begin
  if CDS_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Application.MessageBox('确定要结束发货吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_Main.fieldbyname('SQTYpe').AsString)='本厂出库' then
  begin
    FMfsid:=Trim(CDS_Main.fieldbyname('FHMainId').AsString);
    FMY_CodeNameCP:=Trim(CDS_Main.fieldbyname('Y_CodeNameCP').AsString);
    FMY_Type:=Trim(CDS_Main.fieldbyname('Y_Type').AsString);
    Fkey2str:='箱数：'+Trim(CDS_Main.fieldbyname('FHPiQty').AsString)
              +' 净重：'+Trim(CDS_Main.fieldbyname('FHQty').AsString)
              +' 毛重：'+Trim(CDS_Main.fieldbyname('FHQtyMZ').AsString);
    SendMessage(FMfsid,FMY_CodeNameCP,FMY_Type,Fkey2str);
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update SQ_FHYT_Main Set EndFlag=1,EndTime=getdate(),EndPerson='''+Trim(DName)+'''');
    sql.Add(' where FHMainId='''+Trim(CDS_Main.fieldbyname('FHMainId').AsString)+'''');
    ExecSQL;
  end;
  CDS_Main.Delete;
  Application.MessageBox('结束成功!','提示',0);
end;

procedure TfrmFinishedLSOutList.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>1 then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from SQ_FHYT_Main  ');
    sql.Add(' where FHMainId='''+Trim(CDS_Main.fieldbyname('FHMainId').AsString)+'''');
    sql.Add(' and OKFlag=1 ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已确认不能结束撤销!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要撤销数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update SQ_FHYT_Main Set EndFlag=0,EndTime=getdate(),EndPerson='''+Trim(DName)+'''');
    sql.Add(' where FHMainId='''+Trim(CDS_Main.fieldbyname('FHMainId').AsString)+'''');
    ExecSQL;
  end;
  CDS_Main.Delete;
  Application.MessageBox('撤销成功!','提示',0);
end;

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

end.

