unit U_BDKuWei;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, StdCtrls, ExtCtrls, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, MovePanel, cxCheckBox, Menus, ComCtrls, BtnEdit, ShellAPI, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmBDKuWei = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v3Column2: TcxGridDBColumn;
    Panel2: TPanel;
    SMID: TEdit;
    Button2: TButton;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    Label12: TLabel;
    CDS_Main: TClientDataSet;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    DS_FHSQ: TDataSource;
    CDS_FHSQ: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Button3: TButton;
    btn1: TButton;
    lbl1: TLabel;
    edt1: TEdit;
    Tv3Column1: TcxGridDBColumn;
    ADOQueryTemp1: TADOQuery;
    Tv3Column2: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SMIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
    FSMStr, JXFlag, FSubId, FKWNo: string;
    FFHPS, FSQPS: integer;
    procedure InitFHMX();
    procedure SaveCK();
    procedure InitFHPS();
  public
    { Public declarations }
  end;

var
  frmBDKuWei: TfrmBDKuWei;

implementation

uses
  U_DataLink, U_Fun, MMSystem, U_CPFHSQListSel, U_FinishedClothKCList_SMCK,
  U_FinishedClothInList_KW, U_KuWeiManage_KW;        //, U_CKProductBCPKCList

//var
//  frmPBKCListSel: TfrmCKProductBCPKCListPB;
{$R *.dfm}

procedure TfrmBDKuWei.InitFHPS(); //已出库匹数
begin

  Edit1.Text := IntToStr(FFHPS);

end;

procedure TfrmBDKuWei.SaveCK();      //出库
var
  maxno, SMFlag, KYCKFlag: string;
  FFQty: Integer;
begin

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPDATE CK_Cloth_CR Set  ');
      sql.add(' KuWei= ''' + Trim(edt1.text) + '''');
      sql.add(' ,FKWNo= ''' + Trim(FKWNo) + '''');
      sql.Add(' where CK_Cloth_CR.JYID=' + Quotedstr(Trim(FSMStr)));
      sql.Add('and CK_Cloth_CR.CRFlag=''入库''  ');
//      showmessage(SQL.Text);
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    FFHPS := FFHPS + 1;

    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('绑定库位失败！', '提示信息', 0);

  end;
end;

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

procedure TfrmBDKuWei.FormDestroy(Sender: TObject);
begin
  frmBDKuWei := nil;
end;

procedure TfrmBDKuWei.FormShow(Sender: TObject);
begin
  SMID.SetFocus;
  ReadCxGrid('绑定库位', Tv3, '成品仓库');
end;

procedure TfrmBDKuWei.InitFHMX();   //查询发货明细
var
  MFHPS: integer;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * ');
    sql.Add(' from CK_Cloth_CR ');
    sql.Add(' where FKWNo=' + QuotedStr(FKWNo));
//    ShowMessage(sql.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MainSel);
  SInitCDSData20(ADOQueryTemp, CDS_MainSel);
  if CDS_MainSel.IsEmpty = False then
    MFHPS := Tv3.DataController.Summary.FooterSummaryValues[0]
  else
    MFHPS := 0;

  Edit1.Text := IntToStr(MFHPS);

end;

procedure TfrmBDKuWei.SMIDKeyPress(Sender: TObject; var Key: Char);  //扫描入口
begin
  if Key = #13 then
  begin
    FSMStr := Trim(SMID.Text);
    SMID.Text := '';

    SaveCK();
    InitFHPS();
  end;
end;

procedure TfrmBDKuWei.Button2Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('绑定库位', Tv3, '成品仓库');
end;

procedure TfrmBDKuWei.Button1Click(Sender: TObject);    //选申请单
var
  MKey: Char;
begin
  MKey := #13;
  try
    frmFHSQListSel := TfrmFHSQListSel.Create(self);
    with frmFHSQListSel do
    begin
      if ShowModal = 1 then
      begin
        SMID.Text := Trim(Order_Main.fieldbyname('FHMainId').AsString);
        SMIDKeyPress(SMID, MKey);
      end;
    end;
  finally
    frmFHSQListSel.Free;
  end;
end;

procedure TfrmBDKuWei.Button3Click(Sender: TObject);
var
  MKey: Char;
begin

  MKey := #13;
  if edt1.Text = '' then
  begin
    Application.MessageBox('请先选择库位！', '提示', 0);
    Exit;
  end;
  try
    frmFinishedClothInList_KW := TfrmFinishedClothInList_KW.Create(self);
    with frmFinishedClothInList_KW do
    begin

      if ShowModal = 1 then
      begin
        with frmFinishedClothInList_KW.CDS_Main do
        begin
          First;
          while not Eof do
          begin
            if Fieldbyname('ssel').AsBoolean then
            begin
              SMID.Text := Trim(Fieldbyname('JYID').AsString);
              SMIDKeyPress(SMID, MKey);
              Sleep(200);
            end;
            Next;
          end;
        end;
//      if ShowModal = 1 then
//      begin
//        while frmFinishedClothInList_KW.CDS_Main.Locate('SSel', true, []) do
//        begin
//          self.SMID.Text := Trim(frmFinishedClothInList_KW.CDS_Main.fieldbyname('JYID').AsString);
//          self.SMIDKeyPress(self.SMID, MKey);
//          frmFinishedClothInList_KW.CDS_Main.Delete;
//          sleep(500);
//        end;
      end;
    end;
  finally
    frmFinishedClothInList_KW.Free;
  end;
  InitFHPS();
  InitFHMX();

end;

procedure TfrmBDKuWei.btn1Click(Sender: TObject);
begin
  try
    frmKuWeiManage_KW := TfrmKuWeiManage_KW.Create(self);
    with frmKuWeiManage_KW do
    begin

      if ShowModal = 1 then
      begin
        while frmKuWeiManage_KW.CDS_HZ.Locate('SSel', true, []) do
        begin
          self.edt1.Text := Trim(frmKuWeiManage_KW.CDS_HZ.fieldbyname('KWNo').AsString);
          frmKuWeiManage_KW.CDS_HZ.Delete;
        end;
      end;
    end;
    if GetLSNo(ADOQueryTemp1, FKWNo, 'KW', 'CK_Cloth_CR', 4, 1) = False then
    begin
      Application.MessageBox('生成流水号异常！', '提示', 0);
      exit;
    end;
//    ShowMessage(FKWNo);
  finally
    frmKuWeiManage_KW.Free;
  end;
end;

end.

