unit U_MCGCKList;

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, dxBarBuiltInMenu,
  cxTextEdit, dxSkinsCore,   
    dxSkinDarkRoom, 
    
     
     
    
    
    
   
   dxSkinOffice2013White, 
     dxSkinSharpPlus,
   dxSkinSpringTime,  
   dxSkinsDefaultPainters, 
     dxSkinscxPCPainter;

type
  TfrmMCGCKList = 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;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    v1Column9: TcxGridDBColumn;
    KHNameJC: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column3: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    CheckBox1: TCheckBox;
    Button2: TButton;
    Label3: TLabel;
    JCDanWeiJC: TEdit;
    Button1: TButton;
    Label4: TLabel;
    filler: TEdit;
    v1filler: TcxGridDBColumn;
    ODPat: TOpenDialog;
    IdFTP1: TIdFTP;
    PopupMenu2: TPopupMenu;
    Label2: TLabel;
    Label1: TLabel;
    MYName: TEdit;
    Note: TEdit;
    ImageList1: TImageList;
    KHNo: TEdit;
    JCKHNo: TEdit;
    Label5: TLabel;
    CGName: TEdit;
    v1MYName: TcxGridDBColumn;
    v1MYMF: TcxGridDBColumn;
    v1MYKZ: TcxGridDBColumn;
    N5: TMenuItem;
    Label6: TLabel;
    Label7: TLabel;
    MYMF: TEdit;
    MYKZ: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Label9: TLabel;
    MLOrderNo: TEdit;
    Tv1Column2: TcxGridDBColumn;
    Label10: TLabel;
    CRType: TComboBox;
    Tv1Column3: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    Tv1Column6: TcxGridDBColumn;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuerySub: TADOQuery;
    N2: TMenuItem;
    N3: TMenuItem;
    ToolBar2: TToolBar;
    ToolButton12: TToolButton;
    ToolButton1: TToolButton;
    ADOCmd: TADOQuery;
    N4: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    ISMS: TCheckBox;
    MS: TEdit;
    Label8: TLabel;
    Qty: TComboBox;
    Label11: TLabel;
    KHJC: TEdit;
    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 cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton6Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure N6Click(Sender: TObject);
    procedure v1MYNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
    procedure v1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure cxGridDBColumn10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure N3Click(Sender: TObject);
    procedure cxGridDBColumn4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure Panel2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure N7Click(Sender: TObject);
    procedure MSKeyPress(Sender: TObject; var Key: Char);
    procedure N8Click(Sender: TObject);
    procedure Tv1Column1CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure QtyChange(Sender: TObject);
  private
    canshu1: string;
    DQdate: TDateTime;
    procedure InitGrid();
    function SaveData(): Boolean;
    procedure InitSubGrid(FMYID: string);
    procedure InitSubGrid2(FMainID: string);
    procedure InitStatus();
    procedure InitForm();
    function DelData(): Boolean;
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmMCGCKList: TfrmMCGCKList;

implementation

uses
  U_DataLink, U_RTFun, U_QCRKInPut, U_ZDYHelp, U_LLRKInPut, U_GYSList,
  U_LLRKInPutMH_MCG, U_LLRKInPutMH, U_LLRKInPutX, U_LLRKInPut_CG,
  U_LLRKInPutMH_CG, U_BPZdy_LRM, U_LLRKInPutMH_MTHCK, U_LLRKInPutMH_MPD, U_MCP,
  U_ZSDSeList, U_MPZKCListNew;

{$R *.dfm}

procedure TfrmMCGCKList.FormDestroy(Sender: TObject);
begin
  frmMCGCKList := nil;
end;

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

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

procedure TfrmMCGCKList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('棉出库管理', Tv1, '贸易生产管理');
  Close;
end;

procedure TfrmMCGCKList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      case cxTabControl1.TabIndex of
        0:
          begin
            SQL.Add('exec P_View_Mian_WCK2 ');
          end;
        1:
          begin
            SQL.Add('exec P_View_Mian_CK2 ');
          end;
      end;

      if CheckBox1.Checked = False then
      begin
        sql.Add(' @begdate=' + quotedstr(Trim('2000-01-01')));
        sql.Add(',@enddate=' + quotedstr(Trim('2100-01-01')));
        BegDate.Visible := false;
        EndDate.Visible := false;

        if cxTabControl1.TabIndex = 0 then
        begin
          if ISMS.Checked = false then
          begin
            sql.Add(' ,@ISMS=' + quotedstr(Trim('0')));
          end
          else
          begin
            sql.Add(' ,@ISMS=' + quotedstr(Trim('1')));
          end;
          sql.Add(',@MS=' + Trim(MS.text));
        end
        else if cxTabControl1.TabIndex = 1 then
        begin
          sql.Add(',@Qty=' + quotedstr(Trim(Qty.text)));
        end;
      end
      else
      begin
        sql.Add(' @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date))));
        sql.Add(',@enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1))));
        BegDate.Visible := true;
        EndDate.Visible := true;

        if cxTabControl1.TabIndex = 0 then
        begin
          if ISMS.Checked = false then
          begin
            sql.Add(' ,@ISMS=' + quotedstr(Trim('0')));
          end
          else
          begin
            sql.Add(' ,@ISMS=' + quotedstr(Trim('1')));
          end;
          sql.Add(',@MS=' + Trim(MS.text));
        end
        else if cxTabControl1.TabIndex = 1 then
        begin
          sql.Add(',@Qty=' + quotedstr(Trim(Qty.text)));
        end;

      end;
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmMCGCKList.InitSubGrid(FMYID: string);
begin
  try
    ADOQuerySub.DisableControls;
    with ADOQuerySub do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add(' select KHJC=(select X.KHNameJC from  KH_Main X  where X.KHNo =B.KHNo) ');
      SQL.Add(' ,GYSJC=(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(' ,JHQty=(select Sum(X.Qty) from ML_OrderSubidNew X where X.NewMLID=B.NewMLID) ');
      SQL.Add(' ,A.*,B.MLOrderNo,SJQty=A.Qty ');
      SQL.Add(' from CK_MYSC_CR A ');
      SQL.Add(' left JOIN ML_OrderMainNew B on A.MainID=B.NewMLID ');
      SQL.Add(' where A.MYID=''' + Trim(FMYID) + '''');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQuerySub, Order_Sub);
    SInitCDSData20(ADOQuerySub, Order_Sub);
  finally
    ADOQuerySub.EnableControls;
  end;
end;

procedure TfrmMCGCKList.InitSubGrid2(FMainID: string);
begin
  try
    ADOQuerySub.DisableControls;
    with ADOQuerySub do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add(' select KHJC=(select X.KHNameJC from  KH_Main X  where X.KHNo =B.KHNo) ');
      SQL.Add(' ,GYSJC=(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(' ,JHQty=(select Sum(X.Qty) from ML_OrderSubidNew X where X.NewMLID=B.NewMLID) ');
      SQL.Add(' ,A.*,B.MLOrderNo,SJQty=A.Qty ');
      SQL.Add(' from CK_MYSC_CR A ');
      SQL.Add(' left JOIN ML_OrderMainNew B on A.MainID=B.NewMLID ');
      SQL.Add(' where A.MainID=''' + Trim(FMainID) + '''');
      SQL.Add(' and isnull(MYType,'''') in (''采购棉'') ');
      SQL.Add(' and CRFLag=''出库''  ');
      SQL.Add(' and A.SJLX=''新''   ');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS20(ADOQuerySub, Order_Sub);
    SInitCDSData20(ADOQuerySub, Order_Sub);
  finally
    ADOQuerySub.EnableControls;
  end;
end;

procedure TfrmMCGCKList.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  ReadCxGrid('棉出库管理', Tv1, '贸易生产管理');
  InitStatus();
  InitGrid();
end;

procedure TfrmMCGCKList.InitStatus();
begin
  ToolButton3.Visible := false;
  ToolButton5.Visible := false;
  ToolButton6.Visible := false;
  ToolButton7.Visible := false;
  Tv1Column6.Visible := false;
  N1.Visible := false;
  N2.Visible := false;

  N6.Visible := false;
  N7.Visible := false;

  ISMS.Visible := false;
  MS.Visible := false;

  Label8.Visible := false;
  Qty.Visible := false;

  case cxTabControl1.TabIndex of
    0:
      begin
        ToolButton3.Visible := true;
        Tv1Column6.Visible := true;

        N6.Visible := true;
        N7.Visible := true;

        ISMS.Visible := true;
        MS.Visible := true;

      end;
    1:
      begin

        N1.Visible := true;
        N2.Visible := true;

        Label8.Visible := true;
        Qty.Visible := true;

  
//        ToolButton5.Visible := true;
//        ToolButton6.Visible := true;
//        ToolButton7.Visible := true;
      end;
  end;
end;

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

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

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

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

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

procedure TfrmMCGCKList.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 TfrmMCGCKList.cxTabControl1Change(Sender: TObject);
begin
  InitStatus();
  InitGrid();
end;

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

procedure TfrmMCGCKList.SpeedButton2Click(Sender: TObject);
begin
  try
    frmLLRKInPut_CG := TfrmLLRKInPut_CG.Create(Application);
    with frmLLRKInPut_CG do
    begin
      PState := 0;
      FMainID := '';
      FMYType := '采购面料';
      FMYTypeFlag := 'CGM';
      labMYType.Caption := '(面料)采购';
      FML := '面料';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CG.Free;
  end;
end;

procedure TfrmMCGCKList.SpeedButton4Click(Sender: TObject);
begin

  try
    frmLLRKInPutMH_CG := TfrmLLRKInPutMH_CG.Create(Application);
    with frmLLRKInPutMH_CG do
    begin
      PState := 0;
      FMainID := '';
      FMYType := '采购棉';
      FMYTypeFlag := 'CGH';
      labMYType.Caption := '(棉)采购';
      FML := '棉';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_CG.Free;
  end;
end;

procedure TfrmMCGCKList.SpeedButton5Click(Sender: TObject);
begin

  try
    frmLLRKInPutMH_CG := TfrmLLRKInPutMH_CG.Create(Application);
    with frmLLRKInPutMH_CG do
    begin
      PState := 0;
      FMainID := '';
      FMYType := '采购无纺布';
      FMYTypeFlag := 'CGW';
      labMYType.Caption := '(无纺布)采购';
      FML := '无纺布';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_CG.Free;
  end;
end;

procedure TfrmMCGCKList.SpeedButton3Click(Sender: TObject);
begin

  try
    frmLLRKInPut_CG := TfrmLLRKInPut_CG.Create(Application);
    with frmLLRKInPut_CG do
    begin
      PState := 0;
      FMainID := '';
      FMYType := '采购线';
      FMYTypeFlag := 'CGX';
      labMYType.Caption := '(线)采购';
      FML := '线';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CG.Free;
  end;
end;

procedure TfrmMCGCKList.SpeedButton6Click(Sender: TObject);
begin

  try
    frmLLRKInPut_CG := TfrmLLRKInPut_CG.Create(Application);
    with frmLLRKInPut_CG do
    begin
      PState := 0;
      FMainID := '';
      FMYType := '采购成品';
      FMYTypeFlag := 'CGC';
      labMYType.Caption := '采购(成品)';
      FML := '成品';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPut_CG.Free;
  end;
end;

procedure TfrmMCGCKList.Button2Click(Sender: TObject);
begin
  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text := '棉厂';
      KHType.TxtCode := '棉厂';
      if ShowModal = 1 then
      begin
        Self.KHNameJC.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
        TBFind.Click;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

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

procedure TfrmMCGCKList.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;
end;

procedure TfrmMCGCKList.N6Click(Sender: TObject);
begin
  SelOKNo(Order_Main, true);
end;

procedure TfrmMCGCKList.v1MYNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmMCP := TfrmMCP.create(self);
  with frmMCP do
  begin
    Flag := 'MName';
    flagName := '棉';
    ViewFlag := true;
    Panel4.Align := alClient;
    ToolBar3.Visible := false;
    ToolBar2.Enabled := false;
    Button1.Visible := False;
    if ShowModal = 1 then
    begin
      with Order_Main do
      begin
        Edit;
        FieldByName('MYName').Value := Trim(ClientDataSet1.fieldbyname('BPName').AsString);
        FieldByName('MYCode').Value := Trim(ClientDataSet1.fieldbyname('BPCode').AsString);
        FieldByName('MYMF').Value := Trim(ClientDataSet1.fieldbyname('BPMF').AsString);
        FieldByName('MYKZ').Value := Trim(ClientDataSet1.fieldbyname('BPKZ').AsString);
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add(' select B.KHName,B.KHNameJC,B.KHNo from M_Tree A INNER JOIN KH_Main B on A.CPName =B.KHNameJC where CPID =(');
          sql.Add(' select CPParent from M_Tree where CPID =(select CPID from M_ZDY where BPID =''' + Trim(ClientDataSet1.fieldbyname('BPID').AsString) + '''');
          sql.Add(' ))');
          open;
        end;
        FieldByName('GYSJC').Value := Trim(ADOQueryTemp.fieldbyname('KHNameJC').AsString);

        try
          ADOQueryCmd.Connection.BeginTrans;
          with Order_Main do
          begin

            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('update CK_MYSC_CR set MYName =''' + Trim(ClientDataSet1.fieldbyname('BPName').AsString) + '''');
              sql.Add(', MYCode =''' + Trim(ClientDataSet1.fieldbyname('BPCode').AsString) + '''');
              sql.Add(', MYMF =''' + Trim(ClientDataSet1.fieldbyname('BPMF').AsString) + '''');
              sql.Add(', MYKZ =''' + Trim(ClientDataSet1.fieldbyname('BPKZ').AsString) + '''');
              sql.Add(', KHName =''' + Trim(ADOQueryTemp.fieldbyname('KHName').AsString) + '''');
              sql.Add(' where MYID=' + quotedstr(Trim(Order_Main.FieldByName('MYID').AsString)));
//              ShowMessage(sql.text);
              execsql;
            end;
          end;
          ADOQueryCmd.Connection.CommitTrans;
        except
          ADOQueryCmd.Connection.RollbackTrans;
        end;

      end;
    end;
    free;
  end;
end;

procedure TfrmMCGCKList.ToolButton3Click(Sender: TObject);
var
  maxmxno: string;
begin

  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  if Application.MessageBox('确定要确认出库吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin

      while Locate('SSel', True, []) do
      begin

        if GetLSNo(ADOQueryCmd, maxmxno, 'SC', 'CK_MYSC_CR', 3, 1) = False then
        begin
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('insert into CK_MYSC_CR');
          sql.Add('(MainID,CRType,CRFlag,CRQtyFlag,MYType,MYTypeFlag,MYID,MYCode,MYName,MYMF,MYKZ,Qty,Filler,Filltime,CRTime,KHName,SJLX');
          sql.Add(') values(');
          sql.Add(quotedstr(Trim(Order_Main.fieldbyname('MainID').AsString)));
          sql.Add(',' + quotedstr(trim('生产出库')));
          sql.Add(',' + quotedstr(trim('出库')));
          sql.Add(',-1 ');
          sql.Add(',' + quotedstr(trim('采购棉')));
          sql.Add(',' + quotedstr(trim('CGM')));
          sql.Add(',' + quotedstr(trim(maxmxno)));
          sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('MYCode').AsString)));
          sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('MYName').AsString)));
          sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('MYMF').AsString)));
          sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('MYKZ').AsString)));
          sql.Add(',' + Trim(Order_Main.fieldbyname('JHQty').AsString));
          sql.Add(', ' + quotedstr(trim(DName)));
          sql.Add(',getdate() ');
          sql.Add(',CONVERT(VARCHAR(10),getdate(),120)');
          sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('GYSQC').AsString)));
          sql.Add(',' + quotedstr(trim('新')));
          sql.Add(')');
          execsql;
        end;

        Order_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

end;

procedure TfrmMCGCKList.v1Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with Order_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_MYSC_CR set Qty = ' + mvalue);
    sql.Add(' where MYID=' + quotedstr(Trim(Order_Main.FieldByName('MYID').AsString)));
//    ShowMessage(sql.text);
    ExecSQL;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmMCGCKList.ToolButton7Click(Sender: TObject);
begin

  if Application.MessageBox('确定要删除吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add(' delete from  CK_MYSC_CR ');
        sql.Add(' where MYID=' + quotedstr(Trim(Order_Main.FieldByName('MYID').AsString)));
//        ShowMessage(sql.text);
        execsql;
      end;
    end;
    Order_Main.Delete;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmMCGCKList.ToolButton5Click(Sender: TObject);
var
  maxmxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin

      if GetLSNo(ADOQueryCmd, maxmxno, 'SD', 'CK_MYSC_CR', 3, 1) = False then
      begin
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('insert into CK_MYSC_CR');
        sql.Add('(CRType,CRFlag,CRQtyFlag,MYType,MYTypeFlag,MYID,Qty,Filler,Filltime,CRTime,SJLX');
        sql.Add(') values(');
        sql.Add(quotedstr(trim('手动出库')));
        sql.Add(',' + quotedstr(trim('出库')));
        sql.Add(',-1 ');
        sql.Add(',' + quotedstr(trim('采购棉')));
        sql.Add(',' + quotedstr(trim('CGM')));
        sql.Add(',' + quotedstr(trim(maxmxno)));
        sql.Add(',' + Trim('0'));
        sql.Add(', ' + quotedstr(trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',CONVERT(VARCHAR(10),getdate(),120)');
        sql.Add(',' + quotedstr(trim('新')));
        sql.Add(')');
//        ShowMessage(sql.text);
        execsql;
      end;
    end;
    InitGrid();
    TBFind.Click;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmMCGCKList.ToolButton6Click(Sender: TObject);
var
  maxmxno: string;
begin
  if Application.MessageBox('确定要复制吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin

      if GetLSNo(ADOQueryCmd, maxmxno, 'FZ', 'CK_MYSC_CR', 3, 1) = False then
      begin
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;

      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('insert into CK_MYSC_CR');
        sql.Add('(MainID,CRType,CRFlag,CRQtyFlag,MYType,MYTypeFlag,MYID,MYCode,MYName,MYMF,MYKZ,Qty,Filler,Filltime,CRTime,KHName,SJLX');
        sql.Add(') values(');
        sql.Add(quotedstr(Trim(Order_Main.fieldbyname('MainID').AsString)));
        sql.Add(',' + quotedstr(trim('手动出库')));
        sql.Add(',' + quotedstr(trim('出库')));
        sql.Add(',-1 ');
        sql.Add(',' + quotedstr(trim('采购棉')));
        sql.Add(',' + quotedstr(trim('CGM')));
        sql.Add(',' + quotedstr(trim(maxmxno)));
        sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('MYCode').AsString)));
        sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('MYName').AsString)));
        sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('MYMF').AsString)));
        sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('MYKZ').AsString)));
        sql.Add(',' + Trim('0'));
        sql.Add(', ' + quotedstr(trim(DName)));
        sql.Add(',getdate() ');
        sql.Add(',CONVERT(VARCHAR(10),getdate(),120)');
        sql.Add(',' + quotedstr(Trim(Order_Main.fieldbyname('GYSQC').AsString)));
        sql.Add(',' + quotedstr(trim('新')));
        sql.Add(')');
//        ShowMessage(sql.text);
        execsql;
      end;
      InitGrid();
      TBFind.Click;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;

end;

procedure TfrmMCGCKList.N5Click(Sender: TObject);
begin
  ReadCxGrid('棉出库管理Tv2', Tv2, '贸易生产管理');
  Panel2.Visible := True;
  cxTabControl1.Enabled := False;
  cxGrid1.Enabled := False;
  InitSubGrid('');
end;

procedure TfrmMCGCKList.N1Click(Sender: TObject);
begin
  if cxTabControl1.TabIndex = 0 then
  begin
    Exit;
  end;
  ReadCxGrid('棉出库管理Tv2', Tv2, '贸易生产管理');
  cxGrid1.Enabled := False;
  cxTabControl1.Enabled := False;
  Panel2.Visible := True;
  if Trim(Order_Main.fieldbyname('MainID').AsString) <> '' then
  begin
    InitSubGrid2(Trim(Order_Main.fieldbyname('MainID').AsString));
  end
  else
  begin
    InitSubGrid(Trim(Order_Main.fieldbyname('MYID').AsString));
  end;

end;

procedure TfrmMCGCKList.N2Click(Sender: TObject);
begin

  if cxTabControl1.TabIndex = 0 then
  begin
    Exit;
  end;

  if Application.MessageBox('确定要删除吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add(' delete from  CK_MYSC_CR ');
        sql.Add(' where MYID=' + quotedstr(Trim(Order_Main.FieldByName('MYID').AsString)));
        execsql;
      end;
    end;
    Order_Main.Delete;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmMCGCKList.cxGridDBColumn10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZSDSeList := TfrmZSDSeList.Create(Application);
    with frmZSDSeList do
    begin
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          Self.Order_Sub.FieldByName('MLOrderNo').Value := Trim(frmZSDSeList.Order_Main.FieldByName('MLOrderNo').AsString);
          Self.Order_Sub.FieldByName('KHJC').Value := Trim(frmZSDSeList.Order_Main.FieldByName('KHNameJC').AsString);
          Self.Order_Sub.FieldByName('MainID').Value := Trim(frmZSDSeList.Order_Main.FieldByName('NewMLID').AsString);

          Self.Order_Sub.FieldByName('MYCode').Value := Trim(frmZSDSeList.Order_Main.FieldByName('MLCode').AsString);
          Self.Order_Sub.FieldByName('MYName').Value := Trim(frmZSDSeList.Order_Main.FieldByName('MLName').AsString);
          Self.Order_Sub.FieldByName('MYMF').Value := Trim(frmZSDSeList.Order_Main.FieldByName('MLMF').AsString);
          Self.Order_Sub.FieldByName('MYKZ').Value := Trim(frmZSDSeList.Order_Main.FieldByName('MLKZ').AsString);

          Self.Order_Sub.FieldByName('GYSJC').Value := Trim(frmZSDSeList.Order_Main.FieldByName('GYSName').AsString);

          Self.Order_Sub.FieldByName('JHQty').Value := frmZSDSeList.Order_Main.FieldByName('ZQty').AsFloat;
          Self.Order_Sub.FieldByName('SJQty').Value := frmZSDSeList.Order_Main.FieldByName('ZQty').AsFloat;
        end;
      end;
    end;
  finally
    frmZSDSeList.Free;
  end;
end;

procedure TfrmMCGCKList.N3Click(Sender: TObject);
begin
  with Order_Sub do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmMCGCKList.cxGridDBColumn4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmMPZKCListNew := TfrmMPZKCListNew.Create(Self);
    with frmMPZKCListNew do
    begin
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          Self.Order_Sub.FieldByName('MYCode').Value := Trim(frmMPZKCListNew.FCode);
          Self.Order_Sub.FieldByName('MYName').Value := Trim(frmMPZKCListNew.FName);
          Self.Order_Sub.FieldByName('MYMF').Value := '153';
          Self.Order_Sub.FieldByName('MYKZ').Value := Trim(frmMPZKCListNew.FKZ);
          Self.Order_Sub.FieldByName('GYSJC').Value := Trim(frmMPZKCListNew.FGYS);
          Self.Order_Sub.FieldByName('SJQty').Value := frmMPZKCListNew.FMianQty;
        end;
      end;
    end;
  finally
    frmMPZKCListNew.Free;
  end;
end;

procedure TfrmMCGCKList.ToolButton12Click(Sender: TObject);
begin
  WriteCxGrid('棉出库管理Tv2', Tv2, '贸易生产管理');
  Panel2.Visible := False;
  cxGrid1.Enabled := true;
  cxTabControl1.Enabled := true;
end;

procedure TfrmMCGCKList.ToolButton1Click(Sender: TObject);
begin

  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('出库数据为空不能保存!', '提示', 0);
    Exit;
  end;

  if Order_Sub.Locate('SJQty', null, []) = True then
  begin
    Application.MessageBox('出库米数不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('SJQty', 0, []) = True then
  begin
    Application.MessageBox('出库米数不能为0!', '提示', 0);
    Exit;
  end;

  if Order_Sub.Locate('MYName', null, []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;

  if Order_Sub.Locate('MYName', '', []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    Panel2.Visible := False;
    cxGrid1.Enabled := true;
    cxTabControl1.Enabled := true;
    InitGrid();
    TBFind.Click;
  end;
end;

function TfrmMCGCKList.SaveData(): Boolean;
var
  maxno, MXmaxno, forderNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('MYID').AsString) = '' then
        begin
          if GetLSNo(ADOQueryTemp, maxno, 'SD', 'CK_MYSC_CR', 3, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('MYID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add(' select * from CK_MYSC_CR ');
          sql.Add(' where MYID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('MYID').AsString) = '' then
          begin
            Append;
            fieldbyname('Filler').Value := Trim(DName);
            FieldByName('FillTime').Value := SGetServerDateTime(ADOQueryTemp);
            FieldByName('CRTime').Value := SGetServerDate(ADOQueryTemp);
            fieldbyname('CRType').Value := '手动出库';
            fieldbyname('CRFlag').Value := '出库';
            fieldbyname('CRQtyFlag').Value := -1;
            fieldbyname('MYType').Value := '采购棉';
            fieldbyname('MYTypeFLag').Value := 'CGM';
            fieldbyname('SJLX').Value := '新';
          end
          else
          begin
            Edit;
          end;
          FieldByName('MYID').Value := Trim(maxno);

          with ADOQueryTemp do
          begin
            Close;
            SQL.Clear;
            sql.Add(' select KHName from KH_Main ');
            sql.Add(' where KHNameJC=''' + Trim(Order_Sub.FieldByName('GYSJC').AsString) + '''');
            Open;
          end;

          fieldbyname('KHName').Value := Trim(ADOQueryTemp.FieldByName('KHName').AsString);
          fieldbyname('MYCode').Value := Trim(Order_Sub.FieldByName('MYCode').AsString);
          fieldbyname('MYName').Value := Trim(Order_Sub.FieldByName('MYName').AsString);
          fieldbyname('MYMF').Value := Trim(Order_Sub.FieldByName('MYMF').AsString);
          fieldbyname('MYKZ').Value := Trim(Order_Sub.FieldByName('MYKZ').AsString);
          fieldbyname('MainID').Value := Trim(Order_Sub.FieldByName('MainID').AsString);
          fieldbyname('Qty').Value := Order_Sub.FieldByName('SJQty').AsFloat;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('MYID').Value := Trim(maxno);
        Next;
      end;
    end;

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmMCGCKList.N4Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  CopyAddRowCDS(Order_Sub);

  with Order_Sub do
  begin
    Edit;
    FieldByName('MYID').Value := null;
    Post;
  end;
end;

procedure TfrmMCGCKList.Panel2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
  SendMessage(Panel2.Handle, wm_SysCommand, $F012, 0);
end;

procedure TfrmMCGCKList.N7Click(Sender: TObject);
begin
  SelOKNo(Order_Main, false);
end;

procedure TfrmMCGCKList.MSKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmMCGCKList.N8Click(Sender: TObject);
begin
//  if Application.MessageBox('确定要删除吗？', '提示', 32 + 4) <> IDYES then
//    Exit;
//
//  try
//    ADOQueryCmd.Connection.BeginTrans;
//    with Order_Sub do
//    begin
//      with ADOQueryCmd do
//      begin
//        close;
//        sql.Clear;
//        sql.Add(' delete from  CK_MYSC_CR ');
//        sql.Add(' where MYID=' + quotedstr(Trim(Order_Sub.FieldByName('MYID').AsString)));
//        ShowMessage(sql.text);
//        execsql;
//      end;
//    end;
//    Order_Sub.Delete;
//    ADOQueryCmd.Connection.CommitTrans;
//  except
//    ADOQueryCmd.Connection.RollbackTrans;
//  end;
end;

procedure TfrmMCGCKList.Tv1Column1CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if ARow1.Values[Tv1Column1.Index] = ARow2.Values[Tv1Column1.Index] then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmMCGCKList.QtyChange(Sender: TObject);
begin
  InitGrid();
end;

end.

