unit U_MPZKCListNew;

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
  TfrmMPZKCListNew = 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;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    labMYType: TLabel;
    PopupMenu1: TPopupMenu;
    CheckBox1: TCheckBox;
    Label3: TLabel;
    JCDanWeiJC: TEdit;
    Button1: TButton;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    N3: TMenuItem;
    MenuItem2: TMenuItem;
    N4: TMenuItem;
    Label2: TLabel;
    Label1: TLabel;
    Note: TEdit;
    ImageList1: TImageList;
    KHNo: TEdit;
    JCKHNo: TEdit;
    Label5: TLabel;
    CGName: TEdit;
    v1MYName: TcxGridDBColumn;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    N1: TMenuItem;
    N2: TMenuItem;
    DataSource2: TDataSource;
    CDS_Sub: TClientDataSet;
    DataSource3: TDataSource;
    CDS_3: TClientDataSet;
    Tv1Column4: TcxGridDBColumn;
    MYName: TComboBox;
    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 Button3Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure MYNameKeyPress(Sender: TObject; var Key: Char);
    procedure MYNameChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    canshu1, FFKHName: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure Initimage();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    FMianQty: Double;
    FName, FCode, FKZ, FGYS, FGYSNO: string;
    { Public declarations }
  end;

var
  frmMPZKCListNew: TfrmMPZKCListNew;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_MCGKCListNew, U_BPZdy_LRM;

{$R *.dfm}

procedure TfrmMPZKCListNew.FormDestroy(Sender: TObject);
begin
  frmMPZKCListNew := nil;
end;

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

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

procedure TfrmMPZKCListNew.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('棉品名库存', Tv1, '生产指示单管理');
  Close;
end;

procedure TfrmMPZKCListNew.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_MainKC1 ');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  FFKHName := '';

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select distinct(isnull(MYName,'''')) AS name  from CK_MYSC_CR  where isnull(MYType,'''') in (''采购棉'',''盘点棉'') ');
    sql.Add(' and CRTime>=''2023-06-01'' and CRFLag=''入库''     ');
    Open;
  end;
  MYName.Items.Clear; //清空
  MYName.Items.Add(Trim(''));
  while not ADOQueryCmd.eof do
  begin
    MYName.Items.Add(ADOQueryCmd.fieldbyname('name').AsString);
    ADOQueryCmd.next;
  end;

end;

procedure TfrmMPZKCListNew.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  ReadCxGrid('棉品名库存', Tv1, '生产指示单管理');
  InitGrid();
end;

procedure TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

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

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

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

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

procedure TfrmMPZKCListNew.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 TfrmMPZKCListNew.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 TfrmMPZKCListNew.N1Click(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 := true;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

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

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

procedure TfrmMPZKCListNew.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 TfrmMPZKCListNew.Button3Click(Sender: TObject);
begin
  frmBPZDY_LRM := TfrmBPZDY_LRM.create(self);
  with frmBPZDY_LRM do
  begin
    flag := 'MName';
    flagName := '棉';
    ViewFlag := true;
    Panel4.Align := alClient;
    ToolBar3.Visible := false;
    Button1.Visible := False;
    if ShowModal = 1 then
    begin
      MYName.text := Trim(ClientDataSet1.fieldbyname('BPName').AsString);
    end;
    free;
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_View_MainKC ');
      sql.Add('@WSql=' + quotedstr(Trim('where MYName=''' + Trim(MYName.text) + '''')));
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
  FFKHName := '';
  TBFind.Click;
end;

procedure TfrmMPZKCListNew.Initimage();
begin

end;

procedure TfrmMPZKCListNew.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_MianKC_MXNew ');
      sql.Add(' @MYKZ=' + quotedstr(Trim(Order_Main.fieldbyname('MYKZ').AsString)));
      sql.Add(',@MYName=' + quotedstr(Trim(Order_Main.fieldbyname('MYName').AsString)));
//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_Sub);
    SInitCDSData20(ADOQueryTemp, CDS_Sub);
  finally
    ADOQueryTemp.EnableControls;
  end;

end;

procedure TfrmMPZKCListNew.MYNameKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = #13) and (Trim(MYName.text) <> '') then
  begin
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        sql.Add(' select isnull(MYKZ,'''') AS MYKZ,isnull(MYName,'''') AS MYName,isnull(KHName,'''') AS KHName,isnull(KHNameJC,'''') AS KHNameJC,sum(PS) AS PS,Sum(Qty) AS Qty from ( ');
        sql.Add('select  AA.*,KHNameJC=(select Top 1 B.KHNameJC from KH_Main B ');
        sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=AA.KHName),LX=''真实库存'' ');
        sql.Add(' from ( ');
        sql.Add(' select isnull(MYKZ,'''') AS MYKZ, isnull(MYName,'''') AS MYName,isnull(KHName,'''') AS KHName,sum(CRQtyFlag*PS) AS PS,sum(CRQtyFlag*Qty) AS Qty ');
        sql.Add(' from CK_MYSC_CR A ');
        sql.Add(' where 1=1     ');
        sql.Add(' and isnull(MYType,'''') in (''采购棉'',''盘点棉'')  ');
        sql.Add(' and A.CRTime>=''2023-02-28'' ');
        sql.Add(' and A.MYName=''' + trim(MYName.text) + '''');
        sql.Add(' GROUP BY isnull(MYKZ,'''') ,isnull(MYName,''''),isnull(KHName,'''')  ');
        sql.Add(' )AA     ');

        sql.Add(' UNION all   ');

        sql.Add(' select isnull(AA.MYKZ,'''') AS MYKZ,isnull(AA.MYName,'''') AS MYName,isnull(AA.KHName,'''') AS KHName,PS=0,Qty=sum(AA.Qty),isnull(AA.KHNameJC,'''') AS KHNameJC,LX=''虚拟库存'' from (  ');
        sql.Add(' select MYKZ=B.MLKZ,MYName=B.MLName,KHName=(select Top 1 X.KHName from KH_Main X  ');
        sql.Add(' where isnull(X.KHFlag,'''')=''GYS'' and isnull(X.Valid,'''')=''Y'' and X.KHNameJC=B.GYSName),PS=0,Qty=(select Sum(X.Qty)*-1 from ML_OrderSubidNew X where X.NewMLID=A.NewMLID) ');
        sql.Add(' ,KHNameJC=B.GYSName,MLOrderNo');
        sql.Add(' from ML_OrderMainNew A INNER join ML_OrderZuHe B on A.NewMLID=B.NewMLID and MLType=''棉'' and SSGS =''自己'' --and isnull(MLName,'''')<>''''    ');
        sql.Add(' where A.OrdDate >=''2023-02-28''  ');
        sql.Add(' and B.MLName=''' + trim(MYName.text) + '''');
        sql.Add(' and not exists(select JYID from JTJY_New X where X.NewMLID=A.NewMLID and JYType=''成品'')  ');
        sql.Add(' )AA      ');
        sql.Add(' GROUP BY isnull(AA.MYKZ,''''),isnull(AA.MYName,''''),isnull(AA.KHName,''''),isnull(AA.KHNameJC,'''') ');
        sql.Add(')AAA GROUP BY isnull(MYKZ,''''),isnull(MYName,''''),isnull(KHName,''''),isnull(KHNameJC,'''')   ');
//        ShowMessage(sql.text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    FFKHName := '';
    TBFind.Click;
  end
  else if (Key = #13) and (Trim(MYName.text) = '') then
  begin
    TBRafresh.Click;
  end;
end;

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

procedure TfrmMPZKCListNew.Tv1DblClick(Sender: TObject);
begin
  try
    frmMCGKCListNew := TfrmMCGKCListNew.Create(Self);
    with frmMCGKCListNew do
    begin
      FMYName := Trim(self.Order_Main.fieldbyname('MYName').AsString);
      Label2.Caption := Trim(self.Order_Main.fieldbyname('MYName').AsString);
      if ShowModal = 1 then
      begin
        FName := Trim(self.Order_Main.fieldbyname('MYName').AsString);
        FCode := Trim(frmMCGKCListNew.Order_Main.fieldbyname('MYCode').AsString);
        FKZ := Trim(frmMCGKCListNew.Order_Main.fieldbyname('MYKZ').AsString);
        FGYS := Trim(frmMCGKCListNew.CDS_Sub.fieldbyname('KHNameJC').AsString);
        FGYSNO := Trim(frmMCGKCListNew.CDS_Sub.fieldbyname('KHNo').AsString);
        FMianQty := frmMCGKCListNew.CDS_Sub.fieldbyname('Qty').asfloat;
        self.ModalResult := 1;
      end;
    end;
  finally
    frmMCGKCListNew.Free;
  end;
end;

end.

