unit U_YarnOutWBList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, cxSpinEdit,
  StdCtrls, ExtCtrls, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseInput, U_BaseList, System.ImageList,
  Vcl.ImgList, dxScrollbarAnnotations, cxContainer, dxCore, cxDateUtils,
  cxMaskEdit, cxPC, FrameDateSel, cxCheckBox, dxSkinsCore,
  dxSkinsDefaultPainters, cxProgressBar;

type
  TfrmYarnOutWBList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ToolButton1: TToolButton;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGrid3Level1: TcxGridLevel;
    ADO_2: TADOQuery;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    GPM_3: TcxGridPopupMenu;
    ADO_3: TADOQuery;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    TV3Column3: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    TV3Column5: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    TV3Column6: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    Panel4: TPanel;
    Panel5: TPanel;
    Label1: TLabel;
    panel1: TPanel;
    TV3Column8: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    lbl1: TLabel;
    lbl2: TLabel;
    IOTime: TcxDateEdit;
    ToCoName: TcxButtonEdit;
    Panel3: TPanel;
    Label3: TLabel;
    Label8: TLabel;
    Label2: TLabel;
    Y_Name: TcxTextEdit;
    Y_spec: TcxTextEdit;
    frmFrameDateSel1: TfrmFrameDateSel;
    cxTextEdit1: TcxTextEdit;
    Y_Code: TcxTextEdit;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    TV2Column10: TcxGridDBColumn;
    Label4: TLabel;
    Y_ColorNo: TcxTextEdit;
    TV3Column9: TcxGridDBColumn;
    TV2Column11: TcxGridDBColumn;
    TV2Column12: TcxGridDBColumn;
    ToolButton4: TToolButton;
    TV2Column13: TcxGridDBColumn;
    TV3Column7: TcxGridDBColumn;
    TV3Column10: TcxGridDBColumn;
    TV3Column11: TcxGridDBColumn;
    Label5: TLabel;
    Y_Name1: TcxTextEdit;
    Label6: TLabel;
    Y_Name2: TcxTextEdit;
    Label7: TLabel;
    Y_Name3: TcxTextEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Y_CodeKeyPress(Sender: TObject; var Key: Char);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TV3DblClick(Sender: TObject);
    procedure TV2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton4Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid2;
    procedure InitGrid3;
  public
    { Public declarations }
    FBCId, FStkName, FKHName: string;
  end;

var
  frmYarnOutWBList: TfrmYarnOutWBList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_YarnStkSel, U_CompanySel, U_YarnOutInput1;

{$R *.dfm}

procedure TfrmYarnOutWBList.InitGrid2;
begin
  with ADO_2 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select IO.TWIOID,IO.IOTime,IO.IOType,IO.Filler,IO.filltime,IO.Qty ,C.YarnQty,C.CycleYarnQty,A.WBCID,A.WB_Code,A.WB_Name ,Y_Code,C.Y_Name,C.Y_Spec,Y_Color ');
    sql.Add(' ,A.WB_YarnQty,A.WB_Width,A.WB_YarnDensity,A.WBFtyName,A.WB_YarnLen    ');

    sql.Add(' ,YarnKg=(select  sum( X.Qty)  from BS_Yarn_IO X   where  X.IOFlag=''出库'' and  X.WBCID =A.WBCID and  X.Y_Name =C.Y_Name )');
    sql.Add(' ,PJID=A.WBCID+C.Y_Name');
    sql.Add(' ,KKer =A.Filler');
    sql.Add(' from Tat_WB_IO IO inner join Tat_WB_Card A on IO.WBCID=A.WBCID');
    sql.Add(' inner join Tat_WB_Spec B on A.WB_Code=B.WB_Code  ');
    sql.Add(' inner join Tat_WB_Formula C on B.WBSID=C.WBSID');

    sql.Add(' where IO.IOType in(''外厂整经'',''整经完成'') and isnull(Y_Code,'''')<>'''' ');

    sql.add(' and IO.IOTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.BegDate.Date)) + '''');
    sql.Add(' and IO.IOTime<''' + Trim(FormatDateTime('yyyy-MM-dd', frmFrameDateSel1.enddate.Date + 1)) + '''');

    case cxTabControl1.TabIndex of
      0:
        begin
          sql.Add('  and (not exists(select   X.Qty  from BS_Yarn_IO X   where  X.IOFlag=''出库'' and  X.WBCID =A.WBCID and  X.Y_Name =C.Y_Name ) and isnull(CSFlag,0) = 0)');
        end;
      1:
        begin
          sql.Add('  and (exists(select   X.Qty  from BS_Yarn_IO X   where  X.IOFlag=''出库'' and  X.WBCID =A.WBCID and  X.Y_Name =C.Y_Name ) or isnull(CSFlag,0) = 1)');
        end;

    end;
//    ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS(ADO_2, CDS_2);
  SInitCDSData(ADO_2, CDS_2);
end;

procedure TfrmYarnOutWBList.InitGrid3();
begin
  with ADO_3 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,Y_Name2=Y_Name ');
    sql.Add(',YarnMeter=ROUND((case when isnull(TestDenier,0)>0 then StkQty*1000/(TestDenier/9000)/10000 else 0 end),2)  ');
    sql.Add(' from BS_Yarn_IO A where  A.IOFlag=''入库'' and  isnull(STkQty,0)>0   ');
    if Trim(y_code.text) <> '' then
      sql.Add(' and Y_Code =''' + y_code.text + '''');

    if Trim(Y_ColorNo.text) <> '' then
      sql.Add(' and Y_ColorNo =''' + Y_ColorNo.text + '''');

    if Trim(y_Name1.text) <> '' then
      sql.Add(' and y_Name =''' + y_Name1.text + '''');
//    ShowMessage(sql.text);
    Open;
  end;
  SCreateCDS(ADO_3, CDS_3);
  SInitCDSData(ADO_3, CDS_3);
end;

procedure TfrmYarnOutWBList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid2;
end;

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

procedure TfrmYarnOutWBList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmYarnOutWBList.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid2();
end;

procedure TfrmYarnOutWBList.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  IOTime.Date := SGetServerDateTime(ADOQueryTemp);
  frmFrameDateSel1.EndDate.Date := SGetServerDate(ADOQueryTemp);
  frmFrameDateSel1.BegDate.Date := frmFrameDateSel1.EndDate.Date - 30;

  ReadCxGrid(fSTKName + '出库登记Tv2', Tv2, '纱线仓库');
  ReadCxGrid(fSTKName + '出库登记Tv3', Tv3, '纱线仓库');

  InitGrid2();
  InitGrid3();
end;

procedure TfrmYarnOutWBList.ToCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.ToCoName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.ToCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmYarnOutWBList.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(fSTKName + '出库登记Tv2', Tv2, '纱线仓库');
  WriteCxGrid(fSTKName + '出库登记Tv3', Tv3, '纱线仓库');
end;

procedure TfrmYarnOutWBList.ToolButton2Click(Sender: TObject);
begin
  if ADO_2.Active then
  begin
    SDofilter(ADO_2, SGetFilters(Panel3, 1, 2));
    SCreateCDS(ADO_2, CDS_2);
    SInitCDSData(ADO_2, CDS_2);
  end;
end;

procedure TfrmYarnOutWBList.ToolButton3Click(Sender: TObject);
var
  Qty: Integer;
begin

  if Trim(ToCoName.Text) = '' then
  begin
    Application.MessageBox('出到单位不能为空!', '提示', 0);
    Exit;
  end;

  if not CDS_2.Locate('ssel', True, []) then
  begin
    Application.MessageBox('请选择轴卡数据!', '提示', 0);
    Exit;
  end;

  Qty := 0;
  with CDS_2 do
  begin
    First;
    while not Eof do
    begin
      if CDS_2.fieldbyname('SSel').AsBoolean = True then
      begin
        Qty := Qty + 1;
      end;
      next;
    end;
  end;

  if Qty > 1 then
  begin
    Application.MessageBox('不能多选打钩!', '提示', 0);
    Exit;
  end;

  if CDS_3.IsEmpty then
  begin
    Application.MessageBox('请选择纱线库存数据!', '提示', 0);
    Exit;
  end;

  try
    frmYarnOutInput1 := TfrmYarnOutInput1.Create(Application);
    with frmYarnOutInput1 do
    begin
      FPJID := SelCDSKey(Self.CDS_2, ['PJID'])[0];
//      FWBCID := CDS_2.FieldByName('WBCID').ASSTRING;
      FStkID := CDS_3.FieldByName('StkID').ASSTRING;
      FToCoNo := self.ToCoName.Properties.LookupItems.Text;
      FToCoName := self.ToCoName.Text;

      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmYarnOutInput1.Free;
  end;
end;

procedure TfrmYarnOutWBList.ToolButton4Click(Sender: TObject);
var
  MBYIOIDs: string;
begin
  MBYIOIDs := SelCDSKey(CDS_2, ['TWIOID'])[0];

  if MBYIOIDs = '' then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('update Tat_WB_IO set CSFlag= 1 ');
      sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(MBYIOIDs) + ','','') X where Tat_WB_IO.TWIOID=X.RTValue )  ');
//      ShowMessage(sql.text);
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(trim('点击已出纱')));
      sql.Add(',' + quotedstr(trim(MBYIOIDs)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid2();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmYarnOutWBList.TV2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Y_Name1.text := CDS_2.fieldbyname('Y_Name').AsString;

//  Y_Code.text := CDS_2.fieldbyname('Y_Code').AsString;
  InitGrid3();
end;

procedure TfrmYarnOutWBList.TV3DblClick(Sender: TObject);
begin
  ToolButton3.Click;
end;

procedure TfrmYarnOutWBList.Y_CodeKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
    InitGrid3();
end;

end.

