unit U_MCGRKList_HBKC_CX;

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;

type
  TfrmMCGRKList_HBKC_CX = 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;
    v1MXKCPiQty: TcxGridDBColumn;
    Label34: TLabel;
    KHName: TEdit;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1MXKCKgQty: TcxGridDBColumn;
    v1MYName: TcxGridDBColumn;
    v1MXKCQty: TcxGridDBColumn;
    labMYType: TLabel;
    Button2: TButton;
    Label3: TLabel;
    JCDanWei: TEdit;
    Button1: TButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    PB_MX: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    v1MYMF: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxJCDanWeiJC: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cKUwei: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    v1MYKZ: TcxGridDBColumn;
    V2MYColor: TcxGridDBColumn;
    V2MYColorNo: TcxGridDBColumn;
    TCK: TToolButton;
    V2KHNameJC: TcxGridDBColumn;
    v1KUWei: 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 cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure KHNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure MYCodeChange(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure TCKClick(Sender: TObject);
    procedure TV2DblClick(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    FMYCode,FMYName,FKUWei:string;
    { Public declarations }
  end;

var
  frmMCGRKList_HBKC_CX: TfrmMCGRKList_HBKC_CX;

implementation
uses
  U_DataLink,U_RTFun, U_QCRKInPut,U_ZDYHelp,U_LLRKInPut,U_GYSList,U_RCCKInPut,
  U_LLRKInPutMH,U_LLRKInPutX,U_LLRKInPut_CG,U_LLRKInPutMH_CG,U_LLRKInPutMH_PBCG,
  U_LLRKInPutMH_PBKCCG,U_BPZdy_LRPB,U_LLRKInPutMH_MCG,U_BPZdy_LRM,U_LLRKInPutMH_XCG,U_QCRKInPut_CPNo;

{$R *.dfm}

procedure TfrmMCGRKList_HBKC_CX.FormDestroy(Sender: TObject);
begin
  frmMCGRKList_HBKC_CX:=nil;
end;

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

procedure TfrmMCGRKList_HBKC_CX.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
end;

procedure TfrmMCGRKList_HBKC_CX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('库存查询',Tv2,'贸易生产管理');
  WriteCxGrid('库存查询明细11',Tv1,'贸易生产管理');
  Close;
end;

procedure TfrmMCGRKList_HBKC_CX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.MYKZ,A.MYCode,A.MYName,A.MYMF,B.KUWei,Sum(MXKCPiQty) MXKCPiQty ');
      SQL.add(',Sum(MXKCKgQty) MXKCKgQty,Sum(MXKCQty) MXKCQty');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' inner join CK_MYSC_CR_MX B on B.MYID=A.MYID');
      sql.Add(' where A.MYCode='''+trim(FMYCode)+''' ');
      sql.Add(' and A.MYName='''+trim(FMYName)+''' ');
      sql.Add(' and B.KUWei='''+trim(FKUWei)+''' ');
      sql.Add(' and isnull(CRFlag,'''')=''入库'' ');
      sql.Add(' and isnull(A.MXKCPiQty,0)>0');
      sql.Add(' group by A.MYKZ,A.MYCode,A.MYName,A.MYMF,B.KUWei');
      sql.add(' order by cast(A.MYKZ as decimal(18,2)),cast(A.MYMF as decimal(18,2))');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    if Order_Main.Locate('MXKCKgQty',null,[])=false then
    begin
      v1MXKCKgQty.Visible:=true;
      cxGridDBColumn7.Visible:=true;
    end
    else
    begin
      v1MXKCKgQty.Visible:=false;
      cxGridDBColumn7.Visible:=false;
    end;
    if Order_Main.Locate('MXKCQty',null,[])=false then
    begin
      v1MXKCQty.Visible:=true;
      cxGridDBColumn8.Visible:=true;
    end
    else
    begin
      v1MXKCQty.Visible:=false;
      cxGridDBColumn8.Visible:=false;
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmMCGRKList_HBKC_CX.InitForm();
begin
  ReadCxGrid('库存查询',Tv2,'贸易生产管理');
  ReadCxGrid('库存查询明细11',Tv1,'贸易生产管理');
  InitGrid();
end;

procedure TfrmMCGRKList_HBKC_CX.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 TfrmMCGRKList_HBKC_CX.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(' 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 CK_MYSC_CR  where MYId='''+Trim(Order_Main.fieldbyname('MYId').AsString)+'''');
          sql.Add(' delete from TP_File where WBID='''+trim(Order_Main.fieldbyname('MYId').AsString)+''' ');
          sql.Add(' and TFType=''采购线''');
          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 TfrmMCGRKList_HBKC_CX.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid1);
end;

procedure TfrmMCGRKList_HBKC_CX.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

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

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

procedure TfrmMCGRKList_HBKC_CX.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 TfrmMCGRKList_HBKC_CX.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MYID='''+Trim(Order_Main.fieldbyname('MYID').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生明细数据，不能删除!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

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

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

procedure TfrmMCGRKList_HBKC_CX.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 TfrmMCGRKList_HBKC_CX.Button2Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='线厂';
      KHType.TxtCode:='线厂';
      if ShowModal=1 then
      begin
        Self.KHName.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHName').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmMCGRKList_HBKC_CX.Button1Click(Sender: TObject);
begin
  try
    frmGYSList:=TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text:='绣花厂';
      KHType.TxtCode:='绣花厂';
      if ShowModal=1 then
      begin
        Self.JCDanWei.Text:=Trim(frmGYSList.Order_Main.fieldbyname('KHName').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmMCGRKList_HBKC_CX.KHNameChange(Sender: TObject);
begin
  SDofilter(ADOQueryMain,SGetFilters(Panel1,3,4));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

procedure TfrmMCGRKList_HBKC_CX.N1Click(Sender: TObject);
begin
  frmRCCKInPut:=TfrmRCCKInPut.Create(self);
  with frmRCCKInPut do
  begin
    if ShowModal=1 then
    begin

    end;
    free;
  end;
end;

procedure TfrmMCGRKList_HBKC_CX.MYCodeChange(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 TfrmMCGRKList_HBKC_CX.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryCmd do
  begin
    Filtered:=False;
    Close;
    sql.Clear;
    sql.Add(' select A.RKNo,A.CRTime,A.MYColor,A.MYColorNo,A.KHName,A.CRType,A.MYID,Count(B.JH) MXKCPiQty,Sum(B.KgQty) MXKCKgQty,Sum(B.MQty) MXKCQty,B.KuWei ');
    sql.Add(',A.MYType,JCDanWeiJC=isnull((select Top 1 B.KHNameJC from KH_Main B ');
    sql.Add(' where isnull(B.KHFlag,'''')=''GS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.JCDanWei),''期初入库'')');
    sql.Add(',KHNameJC=isnull((select Top 1 B.KHNameJC from KH_Main B ');
    sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.KHName),''期初入库'')');
    sql.Add(' from CK_MYSC_CR A  ');
    sql.Add(' inner join CK_MYSC_CR_MX B on B.MYID=A.MYID');
    sql.Add(' where A.MYCode='''+trim(Order_Main.fieldbyname('MYCode').AsString)+''' ');
    sql.Add(' and A.MYName='''+trim(Order_Main.fieldbyname('MYName').AsString)+''' ');
    sql.Add(' and isnull(A.MYMF,'''')='''+trim(Order_Main.fieldbyname('MYMF').AsString)+'''');
    sql.Add(' and isnull(A.MYKZ,'''')='''+trim(Order_Main.fieldbyname('MYKZ').AsString)+'''');
    sql.Add(' and isnull(B.KUWei,'''')='''+trim(Order_Main.fieldbyname('KUWei').AsString)+'''');
    sql.Add(' and isnull(A.CRFlag,'''')=''入库'' ');
    sql.Add(' group by A.RKNo,A.CRTime,A.MYColor,A.MYColorNo,A.KHName,A.CRType,A.MYID,B.KuWei,A.MYType,A.JCDanWei');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd,PB_MX);
  SInitCDSData20(ADOQueryCmd,PB_MX);
  if PB_MX.Locate('MYColor',NULL,[])=false then
  begin
    V2MYColor.Visible:=true;
  end
  else
  begin
    V2MYColor.Visible:=false;
  end;
  if PB_MX.Locate('MYColorNo',NULL,[])=false then
  begin
    V2MYColorNo.Visible:=true;
  end
  else
  begin
    V2MYColorNo.Visible:=false;
  end;
end;

procedure TfrmMCGRKList_HBKC_CX.TCKClick(Sender: TObject);
begin
  if PB_MX.IsEmpty then Exit;
  if PB_MX.FieldByName('CRType').AsString<>'期初入库' then
  begin
    Exit;
  end;
  if PB_MX.FieldByName('MYType').AsString='染色面料' then
  begin
    try
      frmQCRKInPut:=TfrmQCRKInPut.Create(Application);
      with frmQCRKInPut do
      begin
        PState:=1;
        FMainId:=Trim(Self.PB_MX.fieldbyname('RKNO').AsString);
        TBSave.Visible:=False;
        FRead:='';
        BtnDel.Visible:=False;
        cxGroupBox1.Visible:=False;
        if self.cxGridDBColumn7.Visible=True then
        begin
          UnitKG.Text:='√';
        end
        else
        begin
          UnitKG.Text:='';
        end;
        if cxGridDBColumn8.Visible=True then
        begin
          UnitM.Text:='√';
        end
        else
        begin
          UnitM.Text:='';
        end;
        if ShowModal=1 then
        begin
          InitGrid();
        end;
      end;
    finally
      frmQCRKInPut.Free;
    end;
  end
  else
  begin
    try
      frmQCRKInPut_CPNo:=TfrmQCRKInPut_CPNo.Create(Application);
      with frmQCRKInPut_CPNo do
      begin
        PState:=1;
        FMainId:=Trim(Self.PB_MX.fieldbyname('RKNO').AsString);
        TBSave.Visible:=False;
        FRead:='';
        BtnDel.Visible:=False;
        cxGroupBox1.Visible:=False;
        if self.cxGridDBColumn7.Visible=True then
        begin
          UnitKG.Text:='√';
        end
        else
        begin
          UnitKG.Text:='';
        end;
        if cxGridDBColumn8.Visible=True then
        begin
          UnitM.Text:='√';
        end
        else
        begin
          UnitM.Text:='';
        end;
        if ShowModal=1 then
        begin
          InitGrid();
        end;
      end;
    finally
      frmQCRKInPut_CPNo.Free;
    end;
  end;
end;

procedure TfrmMCGRKList_HBKC_CX.TV2DblClick(Sender: TObject);
begin
  TCK.Click;
end;

end.
