unit U_CGCX;

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, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxSkinsCore,    
   dxSkinDarkRoom,  
    
     
    
    
    
    
   
   dxSkinOffice2013White, 
     dxSkinSharpPlus,
   dxSkinSpringTime,  
   dxSkinsDefaultPainters, 
     dxSkinscxPCPainter;

type
  TfrmCGCX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Panel1: TPanel;
    Label9: TLabel;
    GYSName: TEdit;
    v1KHNo: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label2: TLabel;
    CGID: TEdit;
    v1Column1: TcxGridDBColumn;
    v1KHLXR: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Tv1Column1: TcxGridDBColumn;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CheckBox1: TCheckBox;
    ADOQueryTemp: TADOQuery;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DS_Sub: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    Tv2Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RMGridReport1: TRMGridReport;
    Tv1Column2: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv2Column2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column4: TcxGridDBColumn;
    Tv2Column5: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    Tv2Column7: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Tv2Column8: TcxGridDBColumn;
    Tv2Column9: TcxGridDBColumn;
    Tv2Column10: TcxGridDBColumn;
    Tv2Column11: 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 TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton4Click(Sender: TObject);
  private
    DQdate: TDateTime;
    XZKHTYpe, canshu1: string;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmCGCX: TfrmCGCX;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_SCPerson, U_PBGYSInPutMain, U_CGMian;

{$R *.dfm}

procedure TfrmCGCX.FormDestroy(Sender: TObject);
begin
  frmCGCX := nil;
end;

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

procedure TfrmCGCX.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;

end;

procedure TfrmCGCX.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption, Tv1, '贸易生产管理');

end;

procedure TfrmCGCX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select *  ');
      sql.Add(' from CG_Main A  ');
      sql.Add(' where A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCGCX.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  ReadCxGrid(self.Caption, Tv1, '贸易生产管理');
  InitGrid();

end;

procedure TfrmCGCX.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;

procedure TfrmCGCX.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(canshu1) = '' then
  begin
    if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
    begin
      Application.MessageBox('不能操作他人的数据!', '提示', 0);
      Exit;
    end;
  end;
end;

procedure TfrmCGCX.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(Order_Main.fieldbyname('Filler').AsString) <> Trim(DName) then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmCGCX.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update ZH_KH_Info Set Valid=''N'' where ZKId=''' + Trim(Order_Main.fieldbyname('ZKId').AsString) + '''');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

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

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

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

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

procedure TfrmCGCX.N1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf';
  SDofilter(ADOQueryMain, ' OrderNoM=''' + Trim(Order_Main.fieldbyname('OrderNoM').AsString) + '''');
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
  Porderno := Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf'), '提示', 0);
  end;
  SDofilter(ADOQueryMain, '');
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
  Order_Main.Locate('ordernoM', Porderno, []);
end;

procedure TfrmCGCX.N2Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf';
  SDofilter(ADOQueryMain, ' OrderNoM=''' + Trim(Order_Main.fieldbyname('OrderNoM').AsString) + '''');
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
  Porderno := Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf'), '提示', 0);
  end;
  SDofilter(ADOQueryMain, '');
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
  Order_Main.Locate('ordernoM', Porderno, []);
end;

procedure TfrmCGCX.ToolButton3Click(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmCGCX.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 TfrmCGCX.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    Sql.Add(' select A.*,B.*');
    sql.Add(' from CG_Main A inner join CG_Sub B on A.CGID=B.CGID ');
    sql.Add(' where A.CGID=''' + Trim(Order_Main.fieldbyname('CGID').AsString) + '''');
    open;
  end;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\棉采购单.rmf';
  if FileExists(fPrintFile) then
  begin
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(Order_Main.fieldbyname('CGID').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);
      exit;
    end;

    RMVariables['QRBARCODE'] := fImagePath;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\棉采购单.rmf'), '提示', 0);
  end;
end;

procedure TfrmCGCX.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  try
    ADOQuery1.DisableControls;
    with ADOQuery1 do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select *  ');
      sql.Add(',RKPS=(select sum(PS) from CK_MYSC_CR X where X.CGSID=A.CGSID )  ');
      sql.Add(',RKKG=(select sum(KgQty) from CK_MYSC_CR X where X.CGSID=A.CGSID )  ');
      sql.Add(',RKMS=(select sum(Qty) from CK_MYSC_CR X where X.CGSID=A.CGSID )  ');
      sql.Add(',WRKPS=PS-(select sum(PS) from CK_MYSC_CR X where X.CGSID=A.CGSID )  ');
      sql.Add(',WRKMS=Qty-(select sum(Qty) from CK_MYSC_CR X where X.CGSID=A.CGSID )  ');
      sql.Add(' from CG_sub A  ');
      sql.Add(' where A.CGID=''' + Trim(Order_Main.fieldbyname('CGID').AsString) + '''');
      Open;
    end;
    SCreateCDS20(ADOQuery1, CDS_Sub);
    SInitCDSData20(ADOQuery1, CDS_Sub);
  finally
    ADOQuery1.EnableControls;
  end;
end;

procedure TfrmCGCX.ToolButton2Click(Sender: TObject);
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CG_Main where CGID=''' + Trim(Order_Main.fieldbyname('CGID').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CG_Sub where CGID=''' + Trim(Order_Main.fieldbyname('CGID').AsString) + '''');
      ExecSQL;
    end;
    Order_Main.Delete;
    Application.MessageBox('数据成功！', '提示', 0);
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

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

procedure TfrmCGCX.ToolButton4Click(Sender: TObject);
begin
  frmCGMian := TfrmCGMian.create(self);
  with frmCGMian do
  begin
    PState := 1;
    FMainId := Trim(Order_Main.fieldbyname('CGID').AsString);
    if showmodal = 1 then
    begin
      InitGrid();
    end;
    free;
  end;
end;

end.

