unit U_MLSC;

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

type
  TfrmMLSC = 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;
    KHConNo: TEdit;
    v1KHNo: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label2: TLabel;
    ConNo: TEdit;
    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;
    ToolButton4: TToolButton;
    Tv2Column8: TcxGridDBColumn;
    Tv2Column9: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Label1: TLabel;
    Label3: TLabel;
    KHNo: TEdit;
    KHName: TEdit;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Label4: TLabel;
    Label5: TLabel;
    GYNoteZ: TEdit;
    Note: TEdit;
    Tv1Column6: TcxGridDBColumn;
    Tv2Column3: TcxGridDBColumn;
    Tv2Column4: 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);
    procedure Tv2DblClick(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
  frmMLSC: TfrmMLSC;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_SCPerson, U_PBGYSInPutMain, U_MLLR, U_SCDMX;

{$R *.dfm}

procedure TfrmMLSC.FormDestroy(Sender: TObject);
begin
  frmMLSC := nil;
end;

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

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

end;

procedure TfrmMLSC.TBCloseClick(Sender: TObject);
begin
  Close;
//  writeCxGrid(self.Caption + '11', Tv1, '贸易生产管理1');
//  writeCxGrid(self.Caption + '子表11', Tv2, '贸易生产管理1');
  writeCxGrid('面料生产单11', Tv1, '贸易生产管理1');
  writeCxGrid('面料生产单子表11', Tv2, '贸易生产管理1');
end;

procedure TfrmMLSC.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('select *  ');
      sql.Add(' from MLSC 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 TfrmMLSC.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
//  ReadCxGrid(self.Caption + '11', Tv1, '贸易生产管理1');
//  ReadCxGrid(self.Caption + '子表11', Tv2, '贸易生产管理1');
  ReadCxGrid('面料生产单11', Tv1, '贸易生产管理1');
  ReadCxGrid('面料生产单子表11', Tv2, '贸易生产管理1');
  InitGrid();

end;

procedure TfrmMLSC.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 TfrmMLSC.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 TfrmMLSC.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 TfrmMLSC.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 TfrmMLSC.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

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

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

procedure TfrmMLSC.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 TfrmMLSC.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 TfrmMLSC.ToolButton3Click(Sender: TObject);
begin
  frmMLLR := TfrmMLLR.create(self);
  with frmMLLR do
  begin
    PState := 0;
    FMainId := '';
    if showmodal = 1 then
    begin
      InitGrid();
    end;
    free;
  end;
end;

procedure TfrmMLSC.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 TfrmMLSC.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 MLSC A inner join MLSC_Sub B on A.MainID=B.MainID ');
    sql.Add(' where A.MainID=''' + Trim(Order_Main.fieldbyname('MainID').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('MainID').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 TfrmMLSC.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(',sl=(select sum(MQty) from CK_MYSC_CR_MX B where B.MYID=(select mainid from CK_MYSC_CR C where C.scdsubid=A.subid)) ');
//      sql.Add(',zl=(select sum(KgQty) from CK_MYSC_CR_MX B where B.MYID=(select mainid from CK_MYSC_CR C where C.scdsubid=A.subid))  ');
      sql.Add(',sl=(select sum(B.MQty) from CK_MYSC_CR C  INNER JOIN CK_MYSC_CR_MX B on B.MYID=C.MYID where B.Fsubid=A.subid AND ToGYSName=''云翔仓库'') ');
      sql.Add(',zl=(select sum(B.KgQty) from CK_MYSC_CR C  INNER JOIN CK_MYSC_CR_MX B on B.MYID=C.MYID where B.Fsubid=A.subid AND ToGYSName=''云翔仓库'')  ');
      sql.Add(' from MLSC_Sub A  ');
      sql.Add(' where A.MainID=''' + Trim(Order_Main.fieldbyname('MainID').AsString) + '''');
//      ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQuery1, CDS_Sub);
    SInitCDSData20(ADOQuery1, CDS_Sub);
  finally
    ADOQuery1.EnableControls;
  end;
end;

procedure TfrmMLSC.ToolButton2Click(Sender: TObject);
begin

  if Order_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete MLSC where MainID=''' + Trim(Order_Main.fieldbyname('MainID').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete MLSC_Sub where MainID=''' + Trim(Order_Main.fieldbyname('MainID').AsString) + '''');
      ExecSQL;
    end;
    Order_Main.Delete;
    Application.MessageBox('数据成功！', '提示', 0);
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

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

procedure TfrmMLSC.ToolButton4Click(Sender: TObject);
begin
  frmMLLR := TfrmMLLR.create(self);
  with frmMLLR do
  begin
    PState := 1;
    FMainId := Trim(Order_Main.fieldbyname('MainID').AsString);
    if showmodal = 1 then
    begin
      InitGrid();
    end;
    free;
  end;
end;

procedure TfrmMLSC.Tv2DblClick(Sender: TObject);
begin
  try
    frmSCDMX := TfrmSCDMX.Create(Application);
    with frmSCDMX do
    begin
//      PState := 0;
      Fsubid := Trim(CDS_Sub.fieldbyname('subid').AsString);
//      FMYType := FGXName;
//      FMYTypeFlag := FGXName;
//      labMYType.Caption := Trim(FGXName) + '加工码单';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmSCDMX.Free;
  end;
end;

end.

