unit U_SMPBKcList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxCheckBox,
  RM_Common, RM_Class, RM_GridReport, RM_System, RM_Dataset, MMSystem, Menus,
  BtnEdit, U_SLT, jpeg, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  cxTextEdit;

type
  TfrmSMPBKcList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1CRTime: TcxGridDBColumn;
    Label3: TLabel;
    P_CodeName: TEdit;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TOk: TToolButton;
    v1Ssel: TcxGridDBColumn;
    Label7: TLabel;
    P_Color: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    kuwei: TEdit;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    ADOQueryPrint: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM1: TRMGridReport;
    v1Column8: TcxGridDBColumn;
    ADOQueryMain: TADOQuery;
    Label13: TLabel;
    v1Column9: TcxGridDBColumn;
    P_Code: TEdit;
    Label18: TLabel;
    Label31: TLabel;
    FactoryName: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column2: TcxGridDBColumn;
    Panel2: TPanel;
    Label1: TLabel;
    Button1: TButton;
    edtKuWei: TBtnEditA;
    ToolButton1: TToolButton;
    GroupBox1: TGroupBox;
    ScrollBox1: TScrollBox;
    v1Column10: TcxGridDBColumn;
    adoqueryPicture: TADOQuery;
    ToolButton3: TToolButton;
    v1Column11: TcxGridDBColumn;
    lbl1: TLabel;
    yptype: TEdit;
    btn1: TToolButton;
    Edit1: TEdit;
    lbl2: TLabel;
    btn2: TButton;
    Label2: TLabel;
    OrderNo: TEdit;
    v1Column12: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure edtKuWeiBtnClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure v1NotePropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column4PropertiesEditValueChanged(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    procedure InitImage();
    { Private declarations }
  public
    fType: integer;
    fCKName: string;
    CANSHU1: string;
    
    { Public declarations }
  end;

var
  frmSMPBKcList: TfrmSMPBKcList;

var
  Mach: array of TfrmSlt;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp;
{$R *.dfm}

procedure TfrmSMPBKcList.InitImage();
var
  i, j: integer;
  jpg: TJpegImage;
  myStream: TADOBlobStream;
begin

  j := length(Mach);
  if j > 0 then
  begin
    for i := 0 to j - 1 do
    begin
      Mach[i].free;
    end;
  end;
  SetLength(Mach, 0);
  if CDS_Main.IsEmpty then
    exit;
  try
    with adoqueryPicture do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.TFID,A.WBID,A.FilesOther,B.FileName from TP_File A,XD_File B  where A.TFID =B.XFID ');
      if CDS_Main.IsEmpty = False then
      begin
        sql.Add('and A.WBID=' + quotedstr(trim(CDS_Main.fieldbyname('MJID').AsString)));
      end
      else
      begin
        sql.add(' and 1=2');
      end;
      open;
    end;
    j := adoqueryPicture.RecordCount;
    if j < 1 then
      exit;
    adoqueryPicture.DisableControls;
    adoqueryPicture.First;
    SetLength(Mach, j);
    jpg := TJpegImage.Create();
    for i := 0 to j - 1 do
    begin
      if triM(adoqueryPicture.fieldbyname('FilesOther').AsString) <> '' then
      begin
        myStream := tadoblobstream.Create(tblobfield(adoqueryPicture.fieldbyname('FilesOther')), bmread);
        jpg.LoadFromStream(myStream);
        Mach[i] := TfrmSlt.Create(Self);
        Mach[i].Name := trim(adoqueryPicture.fieldbyname('TFID').AsString);
        Mach[i].Parent := ScrollBox1;
        Mach[i].Left := 0 + i * 165;
        Mach[i].Init(adoqueryPicture.fieldbyname('TFID').AsString, adoqueryPicture.fieldbyname('FileName').AsString, jpg);
      end;
      adoqueryPicture.Next;
    end;
    adoqueryPicture.EnableControls;
  finally
    jpg.free;
    application.ProcessMessages;
  end;
end;

procedure TfrmSMPBKcList.SetStatus();
begin
  if fType = 10 then
  begin
    TOk.Visible := true;
    v1Ssel.Visible := true;
    ToolButton1.Visible := True;
  end;
  v1Qty.Visible := False;
  v1Qty.Hidden := True;
  ToolButton1.Visible := False;
  if Trim(CANSHU1) <> '客服' then
  begin
    v1Qty.Visible := true;
    v1Qty.Hidden := False;
    ToolButton1.Visible := True;
  end;

end;

procedure TfrmSMPBKcList.InitGrid();
begin
  try
    with adoqueryMain do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.* ');
      SQL.Add(' from CK_Sample_CR A ');
      sql.Add(' where A.kcQty<>0 and CKName=' + quotedstr(trim(fCKName)));
      sql.ADD(' and  A.Valid=''Y'' and A.CRFlag=''入库'' ');
      sql.Add(' order by A.CRTime desc,A.CRNO desc');
      open;
    end;
    SCreateCDS20(adoqueryMain, CDS_Main);
    SInitCDSData20(adoqueryMain, CDS_Main);
  finally
  end;
end;

procedure TfrmSMPBKcList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption + tv1.Name, Tv1, '面料仓库');
  if fType = 10 then
    Action := cahide
  else
    Action := cafree;
end;

procedure TfrmSMPBKcList.FormDestroy(Sender: TObject);
begin
  frmsmpbKcList := nil;
end;

procedure TfrmSMPBKcList.P_CodeNameChange(Sender: TObject);
begin
  if adoqueryMain.Active = False then
    Exit;
  SDofilter(adoqueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(adoqueryMain, CDS_Main);
  SInitCDSData20(adoqueryMain, CDS_Main);
end;

procedure TfrmSMPBKcList.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption + tv1.Name, Tv1, '面料仓库');
  SetStatus();
  TBRafresh.Click();
end;

procedure TfrmSMPBKcList.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmSMPBKcList.TBRafreshClick(Sender: TObject);
begin
  with adoqueryMain do
  begin
    close;
    sql.Clear;
    sql.Add(' select A.* ');
    SQL.Add(',CKQTY=(SELECT SUM(QTY) FROM CK_Sample_CR B WHERE B.MJID=A.MJID AND B.VALID=''Y'' AND B.crflag=''出库'' )');
    if CANSHU1 = '客服' then
    begin
      sql.Add(',XSKC=(CASE WHEN ISNULL(XSKCQTY,0)=0 THEN kcQty ELSE XSKCQTY END )');
    end
    else
    begin
      sql.Add(',XSKC=XSKCQTY');
    end;
    SQL.Add(' from CK_Sample_CR A ');
    sql.Add(' where A.kcQty<>0 and CKName=' + quotedstr(trim(fCKName)));
    sql.ADD(' and  A.Valid=''Y'' and A.CRFlag=''入库'' ');
    if Trim(P_Code.Text) <> '' then
    begin
      SQL.Add('AND P_CODE LIKE  ' + quotedstr('%' + trim(P_Code.Text) + '%'));
    end;
    if Trim(P_Color.Text) <> '' then
    begin
      SQL.Add('AND P_Color LIKE  ' + quotedstr('%' + trim(P_Color.Text) + '%'));
    end;
    if CANSHU1 = '客服' then
    begin
      sql.add('and XSKCQTY>=0');
    end;
    sql.Add(' order by A.CRTime desc,A.CRNO desc');
    open;
  end;
  SCreateCDS20(adoqueryMain, CDS_Main);
  SInitCDSData20(adoqueryMain, CDS_Main);
end;

procedure TfrmSMPBKcList.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmSMPBKcList.TOkClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

procedure TfrmSMPBKcList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, true);
end;

procedure TfrmSMPBKcList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, false);

end;

procedure TfrmSMPBKcList.edtKuWeiBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'mykuwei';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        edtKuWei.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmSMPBKcList.ToolButton2Click(Sender: TObject);
begin
  if adoqueryMain.Active = False then
    Exit;
  SDofilter(adoqueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(adoqueryMain, CDS_Main);
  SInitCDSData20(adoqueryMain, CDS_Main);
end;

procedure TfrmSMPBKcList.ToolButton1Click(Sender: TObject);
begin
  if Panel2.Visible = False then
  begin
    Panel2.Visible := True;
    Edit1.Visible := False;
    btn2.Visible := False;
    lbl2.Visible := False;
    Button1.Visible := True;
  end

  else
    Panel2.Visible := False;
end;

procedure TfrmSMPBKcList.Button1Click(Sender: TObject);
begin

  if trim(edtKuWei.Text) = '' then
    exit;

  try
    ADOQueryCmd.Connection.BeginTrans;
    while CDS_Main.Locate('SSel', true, []) do
    begin
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update CK_Sample_CR set kuwei=' + quotedstr(trim(edtKuWei.Text)));
        sql.Add(' where CRNO=' + quotedstr(trim(CDS_Main.FieldByName('CRNO').AsString)));
        execsql;
      end;
      with CDS_Main do
      begin
        Edit;
        FieldByName('SSel').Value := false;
        FieldByName('KuWei').Value := trim(edtKuWei.Text);

      end;

    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end
end;

procedure TfrmSMPBKcList.Tv1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  InitImage();
end;

procedure TfrmSMPBKcList.ToolButton3Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_Up_SampleKcAll');
    execsql;
  end;
end;

procedure TfrmSMPBKcList.btn2Click(Sender: TObject);
var
  maxno, maxno2: string;
  F1: string;
begin
  if trim(Edit1.Text) = '' then
    exit;
  if trim(edtKuWei.Text) = '' then
    exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    F1 := CDS_Main.FieldByName('CRNO').AsString;
    while CDS_Main.Locate('SSel', true, []) do
    begin
      if F1 <> trim(CDS_Main.FieldByName('CRNO').AsString) then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('不能选择多个库存！', '提示', 0);
        Exit;

      end;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from CK_Sample_CR where crno= ' + quotedstr(trim(CDS_Main.FieldByName('CRNO').AsString)));

        Open;
      end;
      if (StrToFloat(Edit1.Text) < (ADOQueryCmd.FieldByName('kcqty').AsFloat)) then
      begin
        if GetLSNo(ADOQueryTmp, maxno, 'SPR', 'CK_Sample_CR', 3, 1) = False then   //ADOQueryTmp yaoduiyingma
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;

        if GetLSNo(ADOQueryTmp, maxno2, 'SPC', 'CK_Sample_CR', 3, 1) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('exec P_Up_BPCP  @rkID=' + quotedstr(trim(CDS_Main.fieldbyname('crno').AsString)));
          sql.Add(',@maxno=' + quotedstr(trim(maxno)));
          SQL.Add(',@fqty=' + quotedstr(trim(Edit1.Text)));

          SQL.Add(',@kuwei=' + quotedstr(Trim(edtkuwei.text)));
          sql.Add(',@maxno2=' + quotedstr(trim(maxno2)));
          sql.Add(',@filler=' + quotedstr(trim(DName)));
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add('exec P_Up_SampleKc ' + quotedstr(trim(CDS_Main.fieldbyname('MJID').AsString)));
          Open;
        end;

        if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
        begin
          CDS_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('出库数量超出库存数量!', '提示', 0);
          Exit;
        end;
        with CDS_Main do
        begin
          Edit;
          FieldByName('SSel').Value := false;

        end;

      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('分匹成功!', '提示', 0);
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end

end;

procedure TfrmSMPBKcList.btn1Click(Sender: TObject);
begin
  if Panel2.Visible = False then
  begin
    Panel2.Visible := True;
    lbl2.Visible := True;
    Edit1.Visible := True;
    btn2.visible := True;

    Button1.Visible := False;
  end
  else
    Panel2.Visible := False;
end;

procedure TfrmSMPBKcList.OrderNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    TBRafresh.Click();
    ToolButton2.Click();

  end;

end;

procedure TfrmSMPBKcList.Tv1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if CANSHU1 = '客服' then
    Exit;
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_Sample_CR set  XSKCQTY=' + (Trim(mvalue)));
    sql.Add('where CRNO=' + quotedstr(Trim(CDS_Main.fieldbyname('CRNO').AsString)));

    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmSMPBKcList.CheckBox1Click(Sender: TObject);
begin
  if checkbox1.Checked = True then
  begin
    P_Code.Tag := 1;
  end
  else
  begin
    P_Code.Tag := 2;
  end;
  initgrid();
  ToolButton2.Click();
end;

procedure TfrmSMPBKcList.Tv1DblClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  ModalResult := 1;
end;

procedure TfrmSMPBKcList.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_Sample_CR set  kcQty=' + (Trim(mvalue)));
    SQL.Add(',QTY=ISNULL((SELECT SUM(QTY) FROM  CK_Sample_CR B WHERE B.MJID=CK_Sample_CR.MJID AND B.VALID=''Y'' and crflag=''出库'' ),0)+' + (Trim(mvalue)));
    sql.Add('where CRNO=' + quotedstr(Trim(CDS_Main.fieldbyname('CRNO').AsString)));

    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmSMPBKcList.v1NotePropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  if CANSHU1 = '客服' then
    Exit;
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_Sample_CR set  Note=' + QuotedStr(Trim(mvalue)));
    sql.Add('where CRNO=' + quotedstr(Trim(CDS_Main.fieldbyname('CRNO').AsString)));

    ExecSQL;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmSMPBKcList.Tv1Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_Sample_CR set  CGPZ=' + quotedstr(Trim(mvalue)));
    sql.Add('where CRNO=' + quotedstr(Trim(CDS_Main.fieldbyname('CRNO').AsString)));

    ExecSQL;
  end;
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmSMPBKcList.Tv1Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update CK_Sample_CR set  SJBDH=' + quotedstr(Trim(mvalue)));
    sql.Add('where CRNO=' + quotedstr(Trim(CDS_Main.fieldbyname('CRNO').AsString)));

    ExecSQL;
  end;
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
  end;

  Tv1.Controller.EditingController.ShowEdit();
end;

end.

