unit U_MCGKCListNew;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxButtonEdit,
  cxDropDownEdit, cxPC, Buttons, cxContainer, cxGroupBox, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdFTP, ShellAPI, ImgList, StrUtils,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmMCGKCListNew = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    CheckBox1: TCheckBox;
    Label3: TLabel;
    JCDanWeiJC: TEdit;
    Button1: TButton;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N3: TMenuItem;
    MenuItem2: TMenuItem;
    N4: TMenuItem;
    Label1: TLabel;
    Note: TEdit;
    ImageList1: TImageList;
    KHNo: TEdit;
    JCKHNo: TEdit;
    Label5: TLabel;
    CGName: TEdit;
    v1MYMF: TcxGridDBColumn;
    Label7: TLabel;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    N1: TMenuItem;
    DataSource2: TDataSource;
    CDS_Sub: TClientDataSet;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DataSource3: TDataSource;
    CDS_3: TClientDataSet;
    Tv1Column4: TcxGridDBColumn;
    Label2: TLabel;
    Panel3: TPanel;
    Label23: TLabel;
    NowQty: TEdit;
    Label4: TLabel;
    AfterQty: TEdit;
    Button2: TButton;
    Button3: TButton;
    Tv1Column1: TcxGridDBColumn;
    MYKZ: TEdit;
    Tv2Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure MYNameChange(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure MYKZChange(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    canshu1, FFKHName: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure Initimage();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    FMYName: string;
    { Public declarations }
  end;

var
  frmMCGKCListNew: TfrmMCGKCListNew;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmMCGKCListNew.FormDestroy(Sender: TObject);
begin
  frmMCGKCListNew := nil;
end;

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

procedure TfrmMCGKCListNew.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  canshu1 := Trim(DParameters1);
end;

procedure TfrmMCGKCListNew.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('棉库存1', Tv1, '生产指示单管理');
  WriteCxGrid('棉库存2', Tv2, '生产指示单管理');
  Close;
end;

procedure TfrmMCGKCListNew.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_MainKC2 ');
      sql.Add('@WSql=' + quotedstr(Trim(FMYName)));
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  FFKHName := '';

end;

procedure TfrmMCGKCListNew.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  readCxGrid('棉库存1', Tv1, '生产指示单管理');
  readCxGrid('棉库存2', Tv2, '生产指示单管理');
  InitGrid();
end;

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

function TfrmMCGKCListNew.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' delete from YS_Money_CR where YFTypeId=''' + trim(Order_Main.fieldbyname('MYID').AsString) + ''' ');
          sql.Add(' insert into CK_MYSC_CR_Log  ');
          sql.Add(' select * from CK_MYSC_CR where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
          sql.Add(' UPdate CK_MYSC_CR_Log Set DelTime=getdate(),DelerCode=''' + Trim(DCode) + ''',Deler=''' + Trim(DName) + '''');
          sql.Add(' where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
          sql.Add(' Delete from CK_MYSC_CR  where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmMCGKCListNew.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

procedure TfrmMCGKCListNew.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
end;

procedure TfrmMCGKCListNew.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmMCGKCListNew.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMCGKCListNew.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

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

procedure TfrmMCGKCListNew.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  with Order_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := false;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmMCGKCListNew.N1Click(Sender: TObject);
begin
  NowQty.text := Trim(CDS_Sub.fieldbyname('Qty').AsString);
  Panel3.Visible := true;
  cxGrid1.Enabled := False;
  cxGrid2.Enabled := False;
end;

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

procedure TfrmMCGKCListNew.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMCGKCListNew.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(Order_Main.fieldbyname('Chker').AsString) = '' then
    exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\检测报告.rmf';
  RMXLSExport2 := TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Test_Report where TRId=''' + Trim(Order_Main.fieldbyname('TRId').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['OrderUnit']:=Order_Main.fieldbyname('OrderUnit').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\检测报告.rmf'), '提示', 0);
  end;
end;

procedure TfrmMCGKCListNew.Button2Click(Sender: TObject);
var
  FReal, JSQty: Double;
  maxno, Flag: string;
begin
  if TryStrToFloat(AfterQty.Text, FReal) = False then
  begin
    Application.MessageBox('米数非法数字!', '提示', 0);
    Exit;
  end;
  GetLSNo(ADOQueryCmd, maxno, 'MH', 'CK_MYSC_CR', 3, 1);
  JSQty := StrToFloat(AfterQty.Text) - StrToFloat(NowQty.Text);
  if JSQty > 0 then
  begin
    Flag := '加';
  end
  else
  begin
    Flag := '减';
    JSQty := JSQty * - 1;
  end;
  with ADOQueryTemp do
  begin
    close;
    sql.Clear;
    sql.Add('insert into CK_MYSC_CR');
    sql.Add('(MYType,MYTypeFlag,MYID,CRTime,CRQtyFlag,CRFlag,CRType,MYCode,MYName,MYMF,MYKZ,Qty');
    sql.Add(',KHNo,KHName,Filler,FillerCode,Filltime,JCDanWei,JCNo,SJLX) values(');
    sql.Add(quotedstr(trim('盘点棉')));
    sql.Add(',' + quotedstr(trim('CGH')));
    sql.Add(',' + quotedstr(trim(maxno)));
    sql.Add(',CONVERT(VARCHAR(10),getdate(),120)  ');
    if Flag = '加' then
    begin
      sql.Add(',1');
      sql.Add(',' + quotedstr(trim('入库')));
      sql.Add(',' + quotedstr(trim('盘点入库')));
    end
    else
    begin
      sql.Add(',-1');
      sql.Add(',' + quotedstr(trim('出库')));
      sql.Add(',' + quotedstr(trim('盘点出库')));
    end;
    sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('MYCode').AsString)));
    sql.Add(',' + quotedstr(trim(FMYName)));
    sql.Add(',' + quotedstr(trim('')));
    sql.Add(',' + quotedstr(trim(Order_Main.fieldbyname('MYKZ').AsString)));
    sql.Add(',' + FloatToStr(JSQty));
    sql.Add(',' + quotedstr(trim(CDS_Sub.fieldbyname('KHNo').AsString)));
    sql.Add(',' + quotedstr(trim(CDS_Sub.fieldbyname('KHName').AsString)));
    sql.Add(', ' + quotedstr(trim(DName)));
    sql.Add(', ' + quotedstr(trim(DCode)));
    sql.Add(',getdate() ');
    sql.Add(',' + quotedstr(trim('绍兴宇昊绣品有限公司')));
    sql.Add(',' + quotedstr(trim('GS02')));
    sql.Add(',' + quotedstr(trim('新')));
    sql.Add(')');
//    showmessage(sql.text);
    execsql;
  end;

  CDS_Sub.Edit;
  CDS_Sub.fieldbyname('Qty').value := AfterQty.Text;

  Order_Main.Edit;
  if Flag = '加' then
  begin
    Order_Main.fieldbyname('Qty').value := Order_Main.fieldbyname('Qty').value + JSQty;
  end
  else
  begin
    Order_Main.fieldbyname('Qty').value := Order_Main.fieldbyname('Qty').value - JSQty;
  end;

  NowQty.text := '';
  AfterQty.text := '';
  Panel3.Visible := false;
  cxGrid1.Enabled := true;
  cxGrid2.Enabled := true;
end;

procedure TfrmMCGKCListNew.Initimage();
begin

end;

procedure TfrmMCGKCListNew.MYNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMCGKCListNew.Button3Click(Sender: TObject);
begin
  NowQty.text := '';
  AfterQty.text := '';
  Panel3.Visible := false;
  cxGrid1.Enabled := true;
  cxGrid2.Enabled := true;
end;

procedure TfrmMCGKCListNew.MYKZChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMCGKCListNew.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  try
    ADOQueryTemp.DisableControls;
    with ADOQueryTemp do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_MainKC33 ');
      sql.Add(' @MYKZ=' + quotedstr(Trim(Order_Main.fieldbyname('MYKZ').AsString)));
      sql.Add(',@MYName=' + quotedstr(Trim(FMYName)));
      sql.Add(',@MYCode=' + quotedstr(Trim(Order_Main.fieldbyname('MYCode').AsString)));
//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_Sub);
    SInitCDSData20(ADOQueryTemp, CDS_Sub);
  finally
    ADOQueryTemp.EnableControls;
  end;
end;

procedure TfrmMCGKCListNew.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ModalResult := 1;
end;

end.

