unit U_TatSCTLLIST;

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, cxCheckBox,
  BtnEdit, RM_BarCode, cxPC, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseList, Vcl.Menus, dxScrollbarAnnotations,
  cxContainer, dxCore, cxDateUtils, cxMaskEdit, cxDropDownEdit, dxSkinsCore,
  dxSkinsDefaultPainters, cxProgressBar, dxSkinOffice2013White, dxSkinSharpPlus,
  dxSkinWXI;

type
  TfrmSCTLLIST = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryPrint: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    ToolButton2: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton5: TToolButton;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    GPM_2: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v3Column8: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v3Column16: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    EndDate: TcxDateEdit;
    BegDate: TcxDateEdit;
    Label9: TLabel;
    Label1: TLabel;
    ConNo: TcxTextEdit;
    C_CODE: TcxTextEdit;
    Label4: TLabel;
    Tv1GKBH: TcxGridDBColumn;
    cxPageControl3: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    GroupBox1: TGroupBox;
    Y_Note_PLJ: TMemo;
    Panel2: TPanel;
    cxPageControl2: TcxPageControl;
    cxGrid2: TcxGrid;
    Tv3: TcxGridDBTableView;
    TV1Column7: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxTabSheet2: TcxTabSheet;
    GroupBox2: TGroupBox;
    Y_Note_PLW: TMemo;
    Panel3: TPanel;
    cxPageControl1: TcxPageControl;
    cxGrid3: TcxGrid;
    Tv2: TcxGridDBTableView;
    Tv2Column7: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    Tv2Column13: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    Tv2Column6: TcxGridDBColumn;
    Tv2Column14: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    Tv2Column2: TcxGridDBColumn;
    Tv2Column11: TcxGridDBColumn;
    Tv2Column8: TcxGridDBColumn;
    Tv2Column12: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    Tv3Y_Qty: TcxGridDBColumn;
    Tv2MeterQty: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    GPM_3: TcxGridPopupMenu;
    Tv1WSQty: TcxGridDBColumn;
    Tv1JSQty: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);

    procedure ToolButton5Click(Sender: TObject);
    procedure ConNoPropertiesChange(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private
    FInt, PFInt: Integer;
    FFMainID, BPFlag: string;
    procedure InitGrid();
    procedure InitClothYarn(MC_Code: string);
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint;

{$R *.dfm}

procedure TfrmSCTLLIST.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
  WriteCxGrid('DCG13', Tv1, '坯布生产');
end;

procedure TfrmSCTLLIST.FormCreate(Sender: TObject);
begin
  inherited;
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  BegDate.Date := EndDate.Date - 90;
end;

procedure TfrmSCTLLIST.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmSCTLLIST.InitGrid();
var
  mJSQty,mWSQty : double;
begin

  try
    ADOQueryMain.DisableControls;
    mJSQty := 0;
    mWSQty := 0;


    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('SELECT *');
      sql.Add(',JSQty = ( select ISNULL(SUM(X.MeterQty), 0) * A.Conqty/100 from BS_Cloth_Yarn X inner join Bs_Cloth_Info Y on Y.BCIID=X.BCIID ');
      sql.Add(' where X.YRType=''经丝'' and Y.C_Code=A.C_Code group by X.BCIID )');
      sql.Add(',WSQty = ( select ISNULL(SUM(X.MeterQty), 0) * A.Conqty/100 from BS_Cloth_Yarn X inner join Bs_Cloth_Info Y on Y.BCIID=X.BCIID ');
      sql.Add(' where X.YRType=''纬丝'' and Y.C_Code=A.C_Code group by X.BCIID )');
      sql.Add(' FROM V_CON_TL A');
      SQL.Add('WHERE  ConDate>' + quotedstr(FormatDateTime('yyyy-MM-dd', BegDate.Date)));
      SQL.Add('and   ConDate<=' + quotedstr(FormatDateTime('yyyy-MM-dd', enddate.Date + 1)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmSCTLLIST.InitClothYarn(MC_Code: string);
var
  ConqtyValue: string;
begin

  ConqtyValue := CDS_1.FieldByName('Conqty').AsString;
  if ConqtyValue = '' then
    ConqtyValue := '0';

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,MainId=cast('''' as varchar(30)),SubId=cast('''' as varchar(30)) ');
    SQL.Add(' ,allYarn=A.MeterQty*ISNULL(' + ConqtyValue + ',0)/100');
    sql.Add(' from BS_Cloth_Yarn A inner join Bs_Cloth_Info B on A.BCIID=B.BCIID');
    sql.Add(' where A.YRType=''经丝'' and B.C_Code=''' + Trim(MC_Code) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
  ClearCDSColumn(CDS_2, ['YRId', 'SubId', 'MainId']);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,MainId=cast('''' as varchar(30)),SubId=cast('''' as varchar(30))');
    SQL.Add(' ,allYarn=A.MeterQty*ISNULL(' + ConqtyValue + ',0)/100');
    sql.Add(' from BS_Cloth_Yarn A inner join Bs_Cloth_Info B on A.BCIID=B.BCIID');
    sql.Add(' where A.YRType=''纬丝'' and B.C_Code=''' + Trim(MC_Code) + '''');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_3);
  SInitCDSData(ADOQueryTemp, CDS_3);
  ClearCDSColumn(CDS_3, ['YRId', 'SubId', 'MainId']);

end;

procedure TfrmSCTLLIST.N1Click(Sender: TObject);
begin
  SelOKNo(cds_1, true)
end;

procedure TfrmSCTLLIST.N2Click(Sender: TObject);
begin
  SelOKNo(cds_1, false);
end;

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

procedure TfrmSCTLLIST.ToolButton1Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  Tv1.OnFocusedRecordChanged := nil;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := SelcdsKey(CDS_1, ['PCID'])[0];

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FPreviewPrint := False;
      FLMType := 'TatPlanCard';
      FFiltration1 := WSql;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update Tat_Plan_Card         ');
          sql.Add('set PRTer=' + quotedstr(DName));
          sql.Add(', PRTCount=PRTCount+1');
          sql.Add(', PRTDate=getdate()');
          sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(WSql) + ','','') X where Tat_Plan_Card.PCID=X.RTValue )  ');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmSCTLLIST.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + '1', Tv1, '生产投料计算表');
  WriteCxGrid(trim(self.Caption) + '1', Tv2, '生产投料计算表');
  WriteCxGrid(trim(self.Caption) + '1', Tv3, '生产投料计算表');

end;

procedure TfrmSCTLLIST.ToolButton3Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  Tv1.OnFocusedRecordChanged := nil;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := '';
  CDS_1.DisableControls;

  with CDS_1 do
  begin
    First;
    while not Eof do
    begin
      if CDS_1.fieldbyname('SSel').AsBoolean then
      begin
        if WSql <> '' then
        begin
          WSql := WSql + ',' + (Trim(CDS_1.fieldbyname('PCID').AsString));
        end
        else
        begin
          WSql := (Trim(CDS_1.fieldbyname('PCID').AsString));
        end;
      end;
      Next;
    end;
  end;

  CDS_1.Locate('SSel', True, []);
  CDS_1.EnableControls;

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Tat_Card_Del ');
      Sql.Add('@PCIDS=' + quotedstr(Trim(WSql)));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmSCTLLIST.ToolButton5Click(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmSCTLLIST.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  if CDS_1.IsEmpty then
  begin
    Exit;
  end;
  InitClothYarn(CDS_1.FieldByName('C_Code').AsString);
end;

procedure TfrmSCTLLIST.ConNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmSCTLLIST.FormShow(Sender: TObject);
begin
  inherited;

  ReadCxGrid(trim(self.Caption) + '1', Tv1, '生产投料计算表');
  ReadCxGrid(trim(self.Caption) + '1', Tv2, '生产投料计算表');
  ReadCxGrid(trim(self.Caption) + '1', Tv3, '生产投料计算表');

  InitGrid();
end;

procedure TfrmSCTLLIST.ConNoPropertiesChange(Sender: TObject);
begin
  ToolButton5.Click;
end;

procedure TfrmSCTLLIST.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

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

end.


