unit U_KCPBCGRKList_RCDJ;

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, cxTextEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxSkinsCore,  
     dxSkinDarkRoom, 
    
     
     
    
    
    
    
  dxSkinOffice2013White,   
   dxSkinSharpPlus,  dxSkinSpringTime, 
    dxSkinsDefaultPainters,
     
  dxSkinscxPCPainter, BtnEdit, dxBarBuiltInMenu, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinPumpkin, dxSkinSeven,
  dxSkinSevenClassic, dxSkinSharp, dxSkinSilver, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmKCPBCGRKList_RCDJ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: 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;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    KHNameHZ: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    MYName: TEdit;
    v1Column2: TcxGridDBColumn;
    Label1: TLabel;
    Note: TEdit;
    labMYType: TLabel;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CheckBox1: TCheckBox;
    v1JCDanWeiJC: TcxGridDBColumn;
    Label3: TLabel;
    JCDanWeiJC: TEdit;
    v1filler: TcxGridDBColumn;
    Label4: TLabel;
    filler: TEdit;
    ToolButton2: TToolButton;
    ClientDataSet1: TClientDataSet;
    SpeedButton2: TSpeedButton;
    SpeedButton3: TSpeedButton;
    SpeedButton4: TSpeedButton;
    SpeedButton5: TSpeedButton;
    SpeedButton6: TSpeedButton;
    SpeedButton7: TSpeedButton;
    SpeedButton8: TSpeedButton;
    SpeedButton9: TSpeedButton;
    SpeedButton10: TSpeedButton;
    SpeedButton11: TSpeedButton;
    SpeedButton12: TSpeedButton;
    SpeedButton13: TSpeedButton;
    SpeedButton14: TSpeedButton;
    SpeedButton15: TSpeedButton;
    Image2: TImage;
    v1Column4: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label5: TLabel;
    MYTypeFlag: TComboBox;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Label6: TLabel;
    MLOrderNo: TEdit;
    Label7: TLabel;
    KHNameHZZ: TEdit;
    v1Column13: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label8: TLabel;
    PBBatchNo: TEdit;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Label9: TLabel;
    MYColor: TEdit;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Panel3: TPanel;
    Label10: TLabel;
    ComboBox1: TComboBox;
    Label11: TLabel;
    CDZSD: TBtnEditC;
    Button1: TButton;
    Button2: TButton;
    Tv1Column9: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ADOQueryPrint: TADOQuery;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Panel4: TPanel;
    Label12: TLabel;
    Label13: TLabel;
    Button3: TButton;
    Button4: TButton;
    GJ: TEdit;
    MS: TEdit;
    ToolButton6: TToolButton;
    ToolButton5: TToolButton;
    ZKZT: TBtnEditC;
    Label14: TLabel;
    Tv1Column2: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolButton7: TToolButton;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    Tv1Column10: TcxGridDBColumn;
    ToolButton8: TToolButton;
    Panel5: TPanel;
    Label15: TLabel;
    Button5: TButton;
    Button6: TButton;
    KW: 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 TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure v1Column17CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure v1Column3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure CDZSDBtnDnClick(Sender: TObject);
    procedure CDZSDBtnUpClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
//    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ZKZTBtnDnClick(Sender: TObject);
    procedure ZKZTBtnUpClick(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    FGXName: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure initGX();
    procedure SetStatus();
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1: string;

    { Public declarations }
  end;

var
  frmKCPBCGRKList_RCDJ: TfrmKCPBCGRKList_RCDJ;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LLRKInPutMH_RCCG, U_ZSDXZ;

{$R *.dfm}
procedure TfrmKCPBCGRKList_RCDJ.SetStatus();
begin
  ToolButton3.Visible := false;
  ToolButton7.Visible := false;
  ToolButton5.Visible := false;
  ToolButton6.Visible := false;
  case cxTabControl1.TabIndex of
    0:
      begin
        ToolButton3.Visible := true;
        ToolButton5.Visible := true;
        ToolButton6.Visible := true;
      end;
    1:
      begin
        ToolButton7.Visible := true;
      end;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.FormDestroy(Sender: TObject);
begin
  frmKCPBCGRKList_RCDJ := nil;
end;

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

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

procedure TfrmKCPBCGRKList_RCDJ.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('库存加工码单', Tv1, '库存贸易生产管理');
  Close;
end;

procedure TfrmKCPBCGRKList_RCDJ.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;      
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select AA.* ');
      sql.Add(',KHNameHZ=dbo.getpinyin(isnull(AA.KHNameJC,''''))+AA.GYSNo+isnull(AA.KHNameJC,'''')');
      sql.Add('from(');
      sql.Add(' select A.*,D.KGQTY as mxkgqty ,D.MQTY as mxmqty ,D.MXID,D.CDZSD,D.Fsubid,D.CMXID,D.ZKZT,D.JH,D.KW');
      sql.Add(',KHNameJC=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHNO=A.GYSNo)');
      sql.Add(',KHNameJCDH=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHNO=A.ToGYSNo)');
      sql.Add(',KHNameHZZ=dbo.getpinyin(isnull(KM.KHNameJC,''''))+B.KHNO+isnull(KM.KHNameJC,'''')');
      sql.Add(',KHNameJCZ=KM.KHNameJC,B.MLOrderNo,KHNOKH=B.KHNO');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' left join CK_MYSC_CR_MX D on D.MYID=A.MYID');
      sql.Add(' left join ML_OrderMainNew B on A.MainId=B.NewMLID');
      sql.Add(' left join KH_Main KM on KM.KHNO=B.KHNO ');
      sql.Add(' where 1=1  ');
      if CheckBox1.Checked = False then
      begin
        sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM', now) + '-01') + '''');
        sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', now + 1)) + '''');
        BegDate.Visible := false;
        EndDate.Visible := false;
      end
      else
      begin
        sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        BegDate.Visible := true;
        EndDate.Visible := true;
      end;
      sql.Add(' and isnull(CRType,'''')=''加工码单'' ');
      if cxTabControl1.TabIndex = 0 then
      begin
        sql.Add(' and isnull(D.ZKZT,'''')=''在库'' ');
      end
      else
      begin
        sql.Add(' and (isnull(D.ZKZT,'''')<> ''在库'' and isnull(D.ZKZT,'''')<> '''' ) ');
      end;
//      if canshu1<>'高权限' then
//      begin
//        sql.add(' and A.filler='''+trim(DName)+'''');
//      end;
      sql.Add(')AA');
   //   ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.InitForm();
var
  fsj: string;
begin
  BegDate.Date := StrToDate(formatdatetime('yyyy-MM', SGetServerDate(ADOQueryTemp)) + '-01');
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  ReadCxGrid('库存加工码单', Tv1, '库存贸易生产管理');
  fsj := 'select Name=ZDYName,Code=Null from KH_Zdy where Type=''JGGongXu'' ';
  SInitComBoxBySql(ADOQueryTemp, MYTypeFlag, False, fsj);
  SetStatus();
  InitGrid();
end;

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

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

procedure TfrmKCPBCGRKList_RCDJ.TBAddClick(Sender: TObject);
begin
  Panel2.Visible := True;
  initGX();
end;

procedure TfrmKCPBCGRKList_RCDJ.initGX();
var
  BB: array[0..100] of string;
  i, j: Integer;
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select ZDYName from KH_Zdy where Type=''JGGongXu''  '); //order by ZDYName
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('没有定义数据！', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    First;
    i := 0;
    while not Eof do
    begin
      BB[i] := Trim(fieldbyname('ZDYName').AsString);
      i := i + 1;
      Next;
    end;
  end;
  i := i - 1;
  if i > 14 then
  begin
    i := 14;
  end;
  for j := 0 to i do
  begin
    with Panel2 do
    begin
      TSpeedButton(Controls[j]).Visible := True;
      TSpeedButton(Controls[j]).Caption := BB[j];
    end;
  end;
end;

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

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

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

procedure TfrmKCPBCGRKList_RCDJ.ToolButton1Click(Sender: TObject);
var
  FFMYType: string;
begin
  FFMYType := Trim(Order_Main.fieldbyname('MYType').AsString);
  try
    frmLLRKInPutMH_RCCG := TfrmLLRKInPutMH_RCCG.Create(Application);
    with frmLLRKInPutMH_RCCG do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MYId').AsString);
      FMYType := Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag := Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      labMYType.Caption := Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLLRKInPutMH_RCCG.Free;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.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 TfrmKCPBCGRKList_RCDJ.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(canshu1) <> '高权限' then
  begin
//    if Order_Main.FieldByName('filltime').AsDateTime+1<Now then
//    begin
//      application.MessageBox('超过一天不能删除','提示');
//      Exit;
//    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId=''' + Trim(Self.Order_Main.fieldbyname('MYId').AsString) + '''');
    sql.Add(' and ChkStatusP=''已审核''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = false then
  begin
    Application.MessageBox('应付款已审核不能操作', '提示');
    exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId=''' + trim(Order_Main.fieldbyname('MYID').AsString) + '''');
    sql.Add(' and (isnull(ChkerP,'''')<>'''' ');
    sql.Add(' or isnull(ChkerQ,'''')<>'''') ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = false then
  begin
    Application.MessageBox('应付款已审核,不能删除', '提示');
    exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin

  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.N1Click(Sender: TObject);
var
  FFMYType: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(canshu1) <> '高权限' then
  begin
//    if Order_Main.FieldByName('filltime').AsDateTime+1<Now then
//    begin
//      application.MessageBox('超过一天不能修改','提示');
//      Exit;
//    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId=''' + Trim(Self.Order_Main.fieldbyname('MYId').AsString) + '''');
    sql.Add(' and ChkStatusP=''已审核''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = false then
  begin
    Application.MessageBox('应付款已审核不能操作', '提示');
    exit;
  end;
  FFMYType := Trim(Order_Main.fieldbyname('MYType').AsString); //加限制财务ChkerP=''已审核''
  try
    frmLLRKInPutMH_RCCG := TfrmLLRKInPutMH_RCCG.Create(Application);
    with frmLLRKInPutMH_RCCG do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MYId').AsString);
      FMYType := Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag := Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      labMYType.Caption := Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString) + '加工码单';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_RCCG.Free;
  end;
end;

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

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

procedure TfrmKCPBCGRKList_RCDJ.Tv1DblClick(Sender: TObject);
begin
//  ToolButton1.Click;
end;

procedure TfrmKCPBCGRKList_RCDJ.Image2Click(Sender: TObject);
begin
  Panel2.Visible := False;

end;

procedure TfrmKCPBCGRKList_RCDJ.SpeedButton1Click(Sender: TObject);
begin
  Panel2.Visible := False;
  FGXName := TSpeedButton(Sender).Caption;
  try
    frmLLRKInPutMH_RCCG := TfrmLLRKInPutMH_RCCG.Create(Application);
    with frmLLRKInPutMH_RCCG do
    begin
      PState := 0;
      FMainId := '';
      FMYType := FGXName;
      FMYTypeFlag := FGXName;
      labMYType.Caption := Trim(FGXName) + '加工码单';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_RCCG.Free;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.ToolButton2Click(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JGGongXu';
      flagname := '工序';
      //fnote:=True;
      //V1Note.Caption:='坯布联系号';
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.v1Column1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id, Id10: Integer;
begin
  Id := TV1.GetColumnByFieldName('PS').Index;
  Id10 := TV1.GetColumnByFieldName('MXHZPS').Index;
  if (AViewInfo.GridRecord.Values[Id]) <> (AViewInfo.GridRecord.Values[Id10]) then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.v1Column17CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id, Id10: Integer;
begin
  Id := TV1.GetColumnByFieldName('KgQty').Index;
  Id10 := TV1.GetColumnByFieldName('MXHZKgQty').Index;
  if (AViewInfo.GridRecord.Values[Id]) <> (AViewInfo.GridRecord.Values[Id10]) then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.v1Column3CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id, Id10: Integer;
begin
  Id := TV1.GetColumnByFieldName('Qty').Index;
  Id10 := TV1.GetColumnByFieldName('MXHZQty').Index;
  if (AViewInfo.GridRecord.Values[Id]) <> (AViewInfo.GridRecord.Values[Id10]) then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.ToolButton3Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  Panel3.Left := (self.Width - Panel3.Width) div 2;
  Panel3.Visible := true;
end;

procedure TfrmKCPBCGRKList_RCDJ.Button2Click(Sender: TObject);
begin
  Panel3.Visible := False;
end;

procedure TfrmKCPBCGRKList_RCDJ.CDZSDBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmKCPBCGRKList_RCDJ.CDZSDBtnUpClick(Sender: TObject);
begin
  try
    frmzsdxz := Tfrmzsdxz.Create(Application);
    with frmzsdxz do
    begin

      if ShowModal = 1 then
      begin
        Self.CDZSD.Text := Trim(frmzsdxz.Order_Main.fieldbyname('MLOrderNo').AsString);
        Self.CDZSD.TxtCode := Trim(frmzsdxz.Order_Main.fieldbyname('NewSubid').AsString);
      end;
    end;
  finally
    frmzsdxz.Free;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.Button1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
//  if Trim(ComboBox1.text) = '' then
//  begin
//    Application.MessageBox('布匹类型不能为空!', '提示', 0);
//    Exit;
//  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
//            sql.Add('Update CK_MYSC_CR_MX Set BPLX=''' + trim(ComboBox1.Text) + ''' ');
            sql.Add('Update CK_MYSC_CR_MX Set ');
            sql.Add('CDZSD=''' + trim(CDZSD.Text) + ''' ');
            sql.Add(', CDmainid=''' + trim(CDZSD.TxtCode) + ''' ');
            sql.Add(', ZKZT=''' + trim(ZKZT.Text) + ''' ');
            sql.Add('where MXID=''' + Trim(Order_Main.fieldbyname('MXID').AsString) + '''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！', '提示信息');
    Panel3.Visible := false;
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
  CurRow: Integer;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin
        CurRow := Tv1.Controller.FocusedRowIndex;
        with ADOQueryPrint do
        begin
          Close;
          SQL.Clear;
          sql.Add('select AA.* ');
          sql.Add(',KHNameHZ=dbo.getpinyin(isnull(AA.KHNameJC,''''))+AA.GYSNo+isnull(AA.KHNameJC,'''')');
          sql.Add('from(');
          sql.Add(' select A.*,D.KGQTY as mxkgqty ,D.MQTY as mxmqty ,D.MXID,D.KCCDZSD,D.ZKZT,D.JH');
          sql.Add(',KHNameJC=(select Top 1 B.KHNameJC from KH_Main B ');
          sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHNO=A.GYSNo)');
          sql.Add(',KHNameJCDH=(select Top 1 B.KHNameJC from KH_Main B ');
          sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHNO=A.ToGYSNo)');
          sql.Add(',KHNameHZZ=dbo.getpinyin(isnull(KM.KHNameJC,''''))+B.KHNO+isnull(KM.KHNameJC,'''')');
          sql.Add(',KHNameJCZ=KM.KHNameJC,B.MLOrderNo,KHNOKH=B.KHNO');
          sql.Add(' from CK_MYSC_CR A  ');
          sql.Add(' left join CK_MYSC_CR_MX D on D.MYID=A.MYID');
          sql.Add(' left join ML_OrderMainNew B on A.MainId=B.NewMLID');
          sql.Add(' left join KH_Main KM on KM.KHNO=B.KHNO ');
          sql.Add(' where 1=1 and isnull(D.ZKZT,'''')<>'''' ');
          if CheckBox1.Checked = False then
          begin
            sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM', now) + '-01') + '''');
            sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', now + 1)) + '''');
            BegDate.Visible := false;
            EndDate.Visible := false;
          end
          else
          begin
            sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
            sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
            BegDate.Visible := true;
            EndDate.Visible := true;
          end;
          sql.Add(' and isnull(CRType,'''')=''加工码单'' ');
          sql.Add('and D.MXID=''' + Trim(Order_Main.fieldbyname('MXID').AsString) + '''');
          sql.Add(')AA');
          Open;
        end;
        try
          Moudle := LoadLibrary('MakeQRBarcode.dll');
          @Makebar := GetProcAddress(Moudle, 'Make');
          @Mixtext := GetProcAddress(Moudle, 'MixText');
          Txt := Trim(ADOQueryPrint.fieldbyname('MXID').AsString);
          fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
          if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
            CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
          if FileExists(fImagePath) then
            DeleteFile(fImagePath);
          Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
        except
          application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
          Order_Main.EnableControls;
          exit;
        end;

//        if Trim(ADOQueryPrint.fieldbyname('SLbName').AsString) <> '' then
//        begin
//          ExportFtErpFile(Trim(ADOQueryPrint.fieldbyname('SLbName').AsString), ADOQueryCmd);
//          fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ADOQueryPrint.fieldbyname('SLbName').AsString);
//        end
//        else
//        begin
        ExportFtErpFile('通用标签.rmf', ADOQueryCmd);
        fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\面料库存标签.rmf';
//        end;

        if FileExists(fPrintFile) then
        begin
          RMVariables['QRBARCODE'] := fImagePath;
          RM2.LoadFromFile(fPrintFile);
          RM2.ShowReport;
//          RM2.PrintReport;
        end
        else
        begin
          Order_Main.EnableControls;
          Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\面料库存标签.rmf'), '提示', 0);
          Exit;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
  Tv1.Controller.FocusedRowIndex := CurRow;
end;

procedure TfrmKCPBCGRKList_RCDJ.ToolButton6Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
//  if Order_Main.IsEmpty then
//    Exit;
  Panel4.Left := (self.Width - Panel4.Width) div 2;
  Panel4.Visible := true;
end;

procedure TfrmKCPBCGRKList_RCDJ.Button4Click(Sender: TObject);
begin
  Panel4.Visible := False;
end;

procedure TfrmKCPBCGRKList_RCDJ.Button3Click(Sender: TObject);
var
  maxno: string;
  CRID, JH: Integer;
  ZL, CD: string;
  MaxCkNo, MaxCkSubNo: string;
begin

  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean = True then
      begin
        if GetLSNo(ADOQueryCmd, maxno, 'MX', 'CK_MYSC_CR_MX', 4, 1) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取出库最大号失败！', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_MYSC_CR_MX where 1<>1');
          Open;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select count(*) as js from CK_MYSC_CR_MX where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
          Open;
        end;
        JH := StrToInt(Trim(ADOQueryTemp.fieldbyname('js').AsString)) + 1;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('Filler').Value := Trim(DName);
          FieldByName('MYID').Value := Trim(Order_Main.fieldbyname('MYID').AsString);
          FieldByName('MXID').Value := Trim(maxno);
          FieldByName('CMXID').Value := Trim(Order_Main.fieldbyname('MXID').AsString);
          FieldByName('FSubId').Value := Trim(Order_Main.fieldbyname('FSubId').AsString);
          FieldByName('KCCDZSD').Value := Trim(Order_Main.fieldbyname('KCCDZSD').AsString);
          FieldByName('KgQty').Value := strtofloatdef(trim(GJ.Text), 0);
          FieldByName('MQty').Value := strtofloatdef(trim(MS.Text), 0);
          FieldByName('ZKZT').Value := '在库';
          FieldByName('JH').Value := JH;
          Post;
        end;
        ZL := floattostr(strtofloatdef(trim(GJ.Text), 0));
        CD := floattostr(strtofloatdef(trim(MS.Text), 0));
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_MYSC_CR_MX set KgQty=KgQty-''' + ZL + '''  ,MQty=MQty -''' + CD + '''  where MXID=''' + Trim(Order_Main.fieldbyname('MXID').AsString) + ''' ');
//          sql.Add(' UPdate CK_MYSC_CR Set MXHZQty=(select Sum(MQty) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
//          sql.Add(' ,MXHZKgQty=(select Sum(KgQty) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
//          sql.Add(' ,MXHZPS=(select Count(*) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
//          sql.Add(' where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
//          sql.Add(' Update CK_MYSC_CR Set QtyChaE=isnull(MXHZQty,0)-isnull(Qty,0),KgQtyChaE=isnull(MXHZKgQty,0)-isnull(KgQty,0),PSChaE=isnull(MXHZPS,0)-isnull(PS,0) ');
//          sql.Add(' where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
//          showmessage(sql.text);
          ExecSQL;
        end;

        application.ProcessMessages;

      end;

    end;

    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('拆匹成功！', '提示', 0);
    Panel4.Visible := False;
    TBRafresh.Click;
    Exit;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('拆匹异常！', '提示', 0);
    Panel4.Visible := False;
  end;

end;

procedure TfrmKCPBCGRKList_RCDJ.ToolButton5Click(Sender: TObject);
var
  ZL, CD: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MXID=''' + Trim(Order_Main.fieldbyname('MXID').AsString) + '''');
    Open;
  end;
  if Trim(Order_Main.fieldbyname('CMXID').AsString) = '' then
  begin
    Application.MessageBox('此匹不是拆匹布,不能撤销拆匹！', '提示');
    exit;
  end;

  try
//    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      ZL := floattostr(strtofloatdef(Trim(Order_Main.fieldbyname('mxKgQty').AsString), 0));
      CD := floattostr(strtofloatdef(Trim(Order_Main.fieldbyname('mxMQty').AsString), 0));
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update CK_MYSC_CR_MX set KgQty=KgQty+''' + ZL + '''  ,MQty=MQty +''' + CD + '''  where MXID=''' + Trim(Order_Main.fieldbyname('CMXID').AsString) + ''' ');
        sql.Add(' Delete CK_MYSC_CR_MX  where MXID=''' + Trim(Order_Main.fieldbyname('MXID').AsString) + '''');
//        sql.Add(' UPdate CK_MYSC_CR Set MXHZQty=(select Sum(MQty) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
//        sql.Add(' ,MXHZKgQty=(select Sum(KgQty) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
//        sql.Add(' ,MXHZPS=(select Count(*) from CK_MYSC_CR_MX A where A.MYID=CK_MYSC_CR.MYID) ');
//        sql.Add(' where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
//        sql.Add(' Update CK_MYSC_CR Set QtyChaE=isnull(MXHZQty,0)-isnull(Qty,0),KgQtyChaE=isnull(MXHZKgQty,0)-isnull(KgQty,0),PSChaE=isnull(MXHZPS,0)-isnull(PS,0) ');
//        sql.Add(' where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');

        ExecSQL;
      end;
      Order_Main.Delete;

    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
//    Result := True;
    Application.MessageBox('撤销拆匹成功！', '提示', 0);
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
//    Result := False;
    Application.MessageBox('撤销拆匹异常！', '提示', 0);
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.ZKZTBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmKCPBCGRKList_RCDJ.ZKZTBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ZKZT';
      flagname := '出库状态';
      V1Name.Caption := '出库状态';
      if ShowModal = 1 then
      begin
        Self.ZKZT.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.ToolButton7Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
//            sql.Add('Update CK_MYSC_CR_MX Set BPLX='''' ');
            sql.Add('Update CK_MYSC_CR_MX Set ');
            sql.Add(' CDZSD='''' ');
            sql.Add(', CDmainid='''' ');
            sql.Add(', ZKZT=''在库'' ');
            sql.Add('where MXID=''' + Trim(Order_Main.fieldbyname('MXID').AsString) + '''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;
end;

procedure TfrmKCPBCGRKList_RCDJ.MenuItem1Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, True);
end;

procedure TfrmKCPBCGRKList_RCDJ.MenuItem2Click(Sender: TObject);
begin
  SelOKNoFilter(Tv1, False);
end;

procedure TfrmKCPBCGRKList_RCDJ.ToolButton8Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  Panel5.Left := (self.Width - Panel5.Width) div 2;
  Panel5.Visible := true;
end;

procedure TfrmKCPBCGRKList_RCDJ.Button6Click(Sender: TObject);
begin
  Panel5.Visible := false;                 
end;

procedure TfrmKCPBCGRKList_RCDJ.Button5Click(Sender: TObject);
begin
if Order_Main.IsEmpty then
    Exit;
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    Order_Main.DisableControls;
    ADOQueryCmd.Connection.BeginTrans;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        if Order_Main.FieldByName('SSel').AsBoolean = True then
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_MYSC_CR_MX Set ');
            sql.Add('KW=''' + trim(KW.Text) + ''' ');
            sql.Add('where MXID=''' + Trim(Order_Main.fieldbyname('MXID').AsString) + '''');
            ExecSQL;
          end;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存成功！', '提示信息');
    Panel5.Visible := false;
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Order_Main.EnableControls;
    application.MessageBox('数据保存失败！', '提示信息', 0);
  end;
end;

end.

