unit U_ClothCLList;

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

type
  TfrmClothCLList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Label2: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    ConNo: TEdit;
    Label5: TLabel;
    C_CodeName: TEdit;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column6: TcxGridDBColumn;
    RMDBPRT: TRMDBDataSet;
    RMDBPZ: TRMDBDataSet;
    CDS_PZ: TClientDataSet;
    CDS_SJ: TClientDataSet;
    RMDBSJ: TRMDBDataSet;
    v1Column21: TcxGridDBColumn;
    RMDBDataSet1: TRMDBDataSet;
    ClientDataSet1: TClientDataSet;
    CDS_WJG: TClientDataSet;
    RMDBWJG: TRMDBDataSet;
    Label6: TLabel;
    BatchNoHZ: TEdit;
    v1Column31: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyleRepository2: TcxStyleRepository;
    cxStyle2: TcxStyle;
    cxStyle3: TcxStyle;
    cxStyleRepository3: TcxStyleRepository;
    cxStyle4: TcxStyle;
    KHName: TEdit;
    Label4: TLabel;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    DDZT: TComboBox;
    Label7: TLabel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column3: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure conPress(Sender: TObject; var Key: Char);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure RTAPJTInfoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure DDZTChange(Sender: TObject);
    procedure v1Column1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure Tv1Column1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure v1Column9CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
  private
    FInt, PFInt: Integer;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitButton();
    { Private declarations }
  public
    canshu1, FCheJian: string;
    { Public declarations }
  end;

var
  frmClothCLList: TfrmClothCLList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ClothCLCXList;
 //U_ClothSCInPutSC ,U_ClothSCInPut ,U_SCPerson



{$R *.dfm}

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

procedure TfrmClothCLList.InitButton();
begin

end;

procedure TfrmClothCLList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('进度统计查询', Tv1, '坯布生产管理');
end;

procedure TfrmClothCLList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' exec P_View_DDJD1 ''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' ,''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1)) + '''');
//      sql.Add(',''' + trim(RTAPJTInfo.text) + '''');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmClothCLList.InitForm();
begin
  InitButton();
  ReadCxGrid('进度统计查询', Tv1, '坯布生产管理');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 7;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmClothCLList.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 TfrmClothCLList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Cloth_Sub where SubId=''' + Trim(Order_Main.fieldbyname('SubId').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Cloth_Sub where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Cloth_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
        ExecSQL;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString) = '' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Cloth_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
        ExecSQL;
      end;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Cloth_PaiZhen where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Cloth_YCL_PB where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmClothCLList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel('坯布生产单', cxGrid1);
end;

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

procedure TfrmClothCLList.ConNoChange(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 TfrmClothCLList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmClothCLList.conPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(Trim(ConNo.Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        //sql.Add('exec Cloth_QryList :MainId,:WSql');
       // Parameters.ParamByName('WSql').Value:=' and OM.conNo like '''+'%'+Trim(ConNoM.Text)+'%'+''''
                                             // +' and ConType=''生产'' ';
        sql.Add('select * from Cloth_Main A inner join CLoth_Sub B on A.Mainid=B.MainId');
        sql.Add(' where A.ConNo like ''' + '%' + Trim(ConNo.Text) + '%' + '''');

        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

procedure TfrmClothCLList.FormDestroy(Sender: TObject);
begin
  frmClothCLList := nil;
end;

procedure TfrmClothCLList.Tv1DblClick(Sender: TObject);
begin
  try
    frmClothCLCXList := TfrmClothCLCXList.Create(Application);
    with frmClothCLCXList do
    begin
      FMainID := self.Order_Main.fieldbyname('MainID').asstring;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmClothCLCXList.Free;
  end;
end;

procedure TfrmClothCLList.RTAPJTInfoKeyPress(Sender: TObject; var Key: Char);
begin
  InitGrid();
end;

procedure TfrmClothCLList.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  F1, F2, f3: Double;
begin
//  F1 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column1.Index]), 0);    //入库匹数
//  F2 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column6.Index]), 0);   //订单匹数
//  f3 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column3.Index]), 0);   //出库匹数
//  if F1 >= F2 * 0.8 then
//  begin
//    ACanvas.Brush.Color := clRed;
//  end;
//  if f3 >= F2 * 0.8 then
//  begin
//    ACanvas.Brush.Color := clYellow;
//  end;
//  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('DDZT').Index] = null then
//    Exit;
//
//  FDDZT := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('DDZT').Index];
//
//  if FDDZT = '已开始' then
//    ACanvas.Font.Color := $FF0000;
//  if FDDZT = '已结束' then
//    ACanvas.Font.Color := $0000FF;

end;

procedure TfrmClothCLList.DDZTChange(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 TfrmClothCLList.v1Column1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  F1, F2, f3: Double;
begin
  F1 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column1.Index]), 0);    //入库匹数
  F2 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column6.Index]), 0);   //订单匹数
  f3 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column3.Index]), 0);   //出库匹数
  if F1 >= F2 * 0.8 then
  begin
    ACanvas.Brush.Color := clRed;
  end;
//  if f3 >= F2 * 0.8 then
//  begin
//    ACanvas.Brush.Color := clYellow;
//  end;
//  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('DDZT').Index] = null then
//    Exit;
//  if AViewInfo.GridRecord.Values[id] = True then
//  begin
//    ACanvas.Brush.Color := clRed;
//  end;
end;

procedure TfrmClothCLList.Tv1Column1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  F1, F2, f3: Double;
begin
  F1 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column1.Index]), 0);    //入库匹数
  F2 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column6.Index]), 0);   //订单匹数
  f3 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column3.Index]), 0);   //出库匹数
  if F1 >= F2 * 0.8 then
  begin
    ACanvas.Brush.Color := clRed;
  end;
end;

procedure TfrmClothCLList.v1Column9CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  F1, F2, f3: Double;
begin
  F1 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column1.Index]), 0);    //入库匹数
  F2 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column6.Index]), 0);   //订单匹数
  f3 := StrToFloatDef((AViewInfo.GridRecord.Values[v1Column3.Index]), 0);   //出库匹数
  if f3 >= F2 * 0.8 then
  begin
    ACanvas.Brush.Color := clYellow;
  end;
end;

procedure TfrmClothCLList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  if Application.MessageBox('确定要执行结束操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate Cloth_Main Set Endtime=getdate(),EndPerson=''' + Trim(DName) + ''',EndFlag=1');
        sql.Add(' where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
        sql.Add('UPdate  Cloth_JiTai SET StopFlag=1,DOTime=getdate(),DOPerson=''' + Trim(DName) + '''');
        sql.Add(',TYTime=getdate(),TYPerson=''' + Trim(DName) + '''');
        sql.Add(' where MainId=' + quotedstr(trim(Order_Main.fieldbyname('MainId').AsString)));
        sql.Add(' and StopFlag=0');
        ExecSQL;
      end;
//      UpdateJTInfo(Trim(Order_Main.fieldbyname('MainId').AsString));

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update Cloth_Sub Set RTAPJTQty=(select Count(*) from Cloth_JiTai A where A.MainId=Cloth_Sub.MainId )');
        sql.Add(',RTAPJTInfo=dbo.F_Get_Order_SubStr_PB(Cloth_Sub.MainId,''RTAPJTInfo'')');
        sql.Add(',RTQYJTQty=(select Count(*) from Cloth_JiTai A where A.MainId=Cloth_Sub.MainId and StopFlag=0 )');
        sql.Add(',RTQYJTInfo=dbo.F_Get_Order_SubStr_PB(Cloth_Sub.MainId,''RTQYJTInfo'')');
        sql.Add(',RTTYJTQty=(select Count(*) from Cloth_JiTai A where A.MainId=Cloth_Sub.MainId and StopFlag=1 )');
        sql.Add(',RTTYJTInfo=dbo.F_Get_Order_SubStr_PB(Cloth_Sub.MainId,''RTTYJTInfo'')');
        sql.Add(' where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
    //Parameters.ParamByName('MainId').Value:=Trim(RTMainId);
        ExecSQL;
      end;

    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!', '提示', 0);
    initgrid();
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

end.

