unit U_PlanScanInEdit3;

interface

uses
  Windows, Messages, strUtils, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, StdCtrls, ExtCtrls, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, MMSystem,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid, MovePanel, cxCheckBox, Menus, ComCtrls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, U_BaseList, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, RM_Common, RM_Class,
  RM_GridReport, RM_Dataset, Vcl.ToolWin, dxSkinBasic, 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, dxSkinOffice2016Colorful, dxSkinOffice2016Dark,
  dxSkinOffice2019Black, dxSkinOffice2019Colorful, dxSkinOffice2019DarkGray,
  dxSkinOffice2019White, dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic,
  dxSkinSharp, dxSkinSharpPlus, dxSkinSilver, dxSkinSpringtime, dxSkinStardust,
  dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinTheBezier, dxSkinValentine,
  dxSkinVisualStudio2013Blue, dxSkinVisualStudio2013Dark,
  dxSkinVisualStudio2013Light, dxSkinVS2010, dxSkinWhiteprint, dxSkinWXI,
  dxSkinXmas2008Blue, dxScrollbarAnnotations, cxContainer, cxProgressBar,
  BtnEdit;

type
  TfrmPlanScanInEdit3 = class(TfrmBaseList)
    GPM_1: TcxGridPopupMenu;
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel1: TPanel;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    GPM_2: TcxGridPopupMenu;
    DS_3: TDataSource;
    CDS_3: TClientDataSet;
    GPM_3: TcxGridPopupMenu;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel3: TPanel;
    ToolBar3: TToolBar;
    ToolButton3: TToolButton;
    btnGetOutNo: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    edtScan: TEdit;
    Label6: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    wlCompany: TBtnEditA;
    Label5: TLabel;
    InNo: TEdit;
    Label4: TLabel;
    CRTime: TDateTimePicker;
    Label11: TLabel;
    CRType: TComboBox;
    carPlate: TBtnEditA;
    Tv3Column1: TcxGridDBColumn;
    Tqueren: TButton;
    Label3: TLabel;
    Driver: TBtnEditA;
    Driverid: TEdit;
    GroupBox1: TGroupBox;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Button1: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure btnGetOutNoClick(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure edtScanKeyPress(Sender: TObject; var Key: Char);
    procedure wlCompanyBtnClick(Sender: TObject);
    procedure carPlateBtnClick(Sender: TObject);
    procedure TquerenClick(Sender: TObject);
    procedure DriverBtnClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FScan, FSubIds, FZCID: string;
    procedure PlayWav(MWav: string);
    procedure InitKC();

    procedure InitZCD();
    procedure InitStatus(ZCID: string);
    procedure ScanKW();
    procedure ScanCIID();
    function SaveData(): Boolean;
  public

    { Public declarations }
  end;

var
  frmPlanScanInEdit3: TfrmPlanScanInEdit3;

implementation

uses
  U_DataLink, U_RTFun, U_LabelPrint, U_ZDYHelp, U_MachineManageSel, U_UserSel;  //U_TradeClothWaitStorage,  U_PositionSel

{$R *.dfm}

procedure TfrmPlanScanInEdit3.PlayWav(MWav: string);
begin
  if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\' + MWav + '.wav')) then
    PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\' + MWav + '.wav'), 0, SND_ASYNC);
end;

procedure TfrmPlanScanInEdit3.InitZCD();
begin

//  if carPlate.Text = '' then
//    exit;


  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_Market_CarTransport A  WHERE STATUS=''0''   ');

    sql.Add('AND   EXISTS  (SELECT  * FROM Trade_Market_CarTransportSUB B WHERE B.ZCID=A.ZCID)  ');

    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);

end;

procedure TfrmPlanScanInEdit3.InitKC();
begin

//  if carPlate.Text = '' then
//    exit;


  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_Market_Main A    ');

    sql.Add('where    ');    // ZCID =(select zcid from Trade_Market_Main b where
    sql.add('  a.tmmid=' + QuotedStr(TRIm(FScan)));

    SQL.Add('AND HDtime IS NULL ');
    sql.Add('and ISNULL(stATUS,''0'')=''9''');

    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('条码错误或已回单', '提示信息', 0);
    Exit;
  end
  else
  begin

    if CDS_3.Locate('TMMID', ADOQueryTemp.FieldByName('TMMID').AsString, []) then
    begin
      Application.MessageBox('该单据已在列表中，无需重复添加', '提示信息', 0);
      Exit; // 存在则直接退出，不执行 Append
    end;
    with CDS_3 do
    begin
      Append;
      FieldByName('TMMNo').VALUE := ADOQueryTemp.FieldByName('TMMNo').AsString;
      FieldByName('TMMID').VALUE := ADOQueryTemp.FieldByName('TMMID').AsString;
      FieldByName('CUSTNAME').VALUE := ADOQueryTemp.FieldByName('CUSTNAME').AsString;
      POST;
    end;
//
//    SCreateCDS(ADOQueryTemp, CDS_3);
//    SInitCDSData(ADOQueryTemp, CDS_3);
//    Driver.Text := ADOQueryTemp.FieldByName('Driver').AsString;
//    carPlate.Text := ADOQueryTemp.FieldByName('carPlate').AsString;

  end;

end;

procedure TfrmPlanScanInEdit3.ScanKW();
var
  MStkIds: string;
begin
  try
    with ADOQueryTemp do
    begin
      close;
      SQL.Clear;
      sql.Add('select * from Bs_Position where POSID =''' + trim(FScan) + ''' ');
      open;
    end;

    wlCompany.Text := ADOQueryTemp.FieldByName('posno').AsString;

    btnGetOutNo.Click;
  except

  end;

end;

procedure TfrmPlanScanInEdit3.ScanCIID();
var
  MStkIds: string;
begin
  try
    if Trim(wlCompany.Text) = '' then
      raise Exception.Create('库位不能为空!');
    if Trim(InNo.Text) = '' then
      raise Exception.Create('入库单号不能为空!');

    MStkIds := FScan;

    try
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        Sql.Add('exec P_Market_Position_In ');
        Sql.Add('@StkIds=' + quotedstr(trim(MStkIds)));
        Sql.Add(',@IOTime=' + Quotedstr(FormatDateTime('yyyy-MM-dd', CRTime.DateTime)));
        Sql.Add(',@IOType=' + Quotedstr(crtype.Text));
        Sql.Add(',@IONO=' + Quotedstr(InNo.Text));
        Sql.Add(',@StkPosition=' + Quotedstr(wlCompany.Text));
        Sql.Add(',@FillId=' + Quotedstr(DCode));
        Sql.Add(',@Filler=' + Quotedstr(DName));
//        ShowMessage(sql.Text);
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
      ADOQueryCmd.Connection.CommitTrans;
      PlayWav('正确');
    except
      ADOQueryCmd.Connection.RollbackTrans;
      raise Exception.Create(PChar(Exception(ExceptObject).Message));
    end;

  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
//    PlayWav('发货错误');
  end;

end;

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

procedure TfrmPlanScanInEdit3.FormDestroy(Sender: TObject);
begin
  inherited;
  frmPlanScanInEdit3 := nil;
end;

procedure TfrmPlanScanInEdit3.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.caption + 'Tv3', Tv3, '成品仓库');
//  InitKC();
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_Market_Main A    ');

    sql.Add('where 1<>1 ');

    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_3);
  SInitCDSData(ADOQueryTemp, CDS_3);
  initzcd();
end;

procedure TfrmPlanScanInEdit3.edtScanKeyPress(Sender: TObject; var Key: Char);
var
  maxno, SMFlag, KYCKFlag, FMainId, ff: string;
  FFQty: Integer;
begin
  // 长度 =13 时，自动模拟按回车
  if Length(edtScan.Text) = 12 then
  begin
    Key := #13; // 强制把当前按键变成回车
    edtScan.SelectAll; // 可选：选中全部方便下次覆盖扫码
  end;

  // 原来的回车逻辑
  if Key = #13 then
  begin
    if edtScan.Text = '' then
      EXIT;

    FScan := Trim(edtScan.Text);
    edtScan.Text := '';

    InitKC();
  end;
end;

procedure TfrmPlanScanInEdit3.ToolButton3Click(Sender: TObject);
begin
  if Trim(InNo.Text) = '' then
    Exit;

  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeClothIn1';
      FFiltration1 := Trim(CDS_1.FieldByName('zcid').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;

end;

procedure TfrmPlanScanInEdit3.wlCompanyBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := 'Cust';
      flag := 'AREA';
      flagname := '区域';
      if ShowModal = 1 then
      begin
        wlCompany.text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPlanScanInEdit3.Button1Click(Sender: TObject);
begin
  with CDS_3 do
  begin
    Delete;
  end;

end;

procedure TfrmPlanScanInEdit3.carPlateBtnClick(Sender: TObject);
begin
  inherited;
  try
    frmMachineManageSel := TfrmMachineManageSel.Create(Application);
    with frmMachineManageSel do
    begin
      if ShowModal = 1 then
      begin
        carPlate.text := Trim(CDS_1.fieldbyname('VNO').AsString);
      end;
    end;
  finally
    frmMachineManageSel.Free;
  end;
end;

procedure TfrmPlanScanInEdit3.DriverBtnClick(Sender: TObject);
begin

  try
    frmUserSel := TfrmUserSel.Create(Application);
    with frmUserSel do
    begin
      Fdept := '送货组';
      if ShowModal = 1 then
      begin
        self.Driverid.Text := Trim(CDS_1.fieldbyname('EECode').AsString);
        self.Driver.Text := Trim(CDS_1.fieldbyname('EEName').AsString);
      end;
    end;
  finally
    frmUserSel.Free;
  end;
end;

procedure TfrmPlanScanInEdit3.btnGetOutNoClick(Sender: TObject);
var
  maxNo: string;
begin
  if GetLSNo(ADOQueryCmd, maxNo, 'RK', 'Trade_Cloth_IO', 3, 1) = False then
  begin
    Application.MessageBox('取入库单号失败!', '提示', 0);
    Exit;
  end;
  InNo.Text := Trim(maxNo);
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);
  InitKC();
end;

procedure TfrmPlanScanInEdit3.ToolButton7Click(Sender: TObject);
begin
  tv3.Controller.EditingController.ShowEdit();
  Close;

end;

procedure TfrmPlanScanInEdit3.ToolButton8Click(Sender: TObject);
begin
  WriteCxGrid(self.caption + 'Tv3', Tv3, '成品仓库');
end;

procedure TfrmPlanScanInEdit3.TquerenClick(Sender: TObject);
begin
//  if Driver.Text = '' then
//  begin
//    Application.MessageBox('司机不能为空!', '提示', 0);
//    Exit;
//  end;
//  if carPlate.Text = '' then
//  begin
//    Application.MessageBox('车辆不能为空!', '提示', 0);
//    Exit;
//  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Trade_Market_Main A    ');

      sql.Add('where 1<>1 ');

      Open;
    end;
    SCreateCDS(ADOQueryTemp, CDS_3);
    SInitCDSData(ADOQueryTemp, CDS_3);
    initzcd();

  end;
end;
//procedure TfrmPlanScanInEdit.TquerenClick(Sender: TObject);

function TfrmPlanScanInEdit3.SaveData(): Boolean;
var
  MaxMid, MaxSid: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    with CDS_3 do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update  Trade_Market_Main   ');
          SQL.Add('set status=''10'', hdtime=GETDATE()');

          sql.Add('where TMMID=' + quotedstr(trim(CDS_3.fieldbyname('TMMID').AsString)));

          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('出车单号：' + trim(CDS_3.FieldByName('ZCID').AsString))));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          execsql;
        end;
        Next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;

    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    Result := False;
  end;
end;

procedure TfrmPlanScanInEdit3.InitStatus(ZCID: string);
begin

  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('update  Trade_Market_Main SET status=''9'' ');
    SQL.Add(',zctime=GETDATE()');
    SQL.Add(',Driver=' + QuotedStr(Driver.Text));
    SQL.Add(',carPlate= ' + QuotedStr(carPlate.Text));
    SQL.Add(',zcid= ' + QuotedStr(ZCID));
    sql.Add('where TMMId=' + quotedstr(trim(CDS_3.fieldbyname('TMMId').AsString)));

    sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails) ');
    sql.Add('values(''贸易布坯出库''    ');
    sql.Add(',' + quotedstr(trim(CDS_3.fieldbyname('TMMId').AsString)));
    sql.Add(',''完成''   ');
    sql.Add(',' + quotedstr(DName));
    sql.Add(',' + quotedstr(trim(self.Caption)));
    sql.Add(',' + quotedstr(trim(CDS_3.fieldbyname('TMMId').AsString)));
    sql.Add(')  ');

    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('计划单号：' + trim(CDS_3.FieldByName('TMMNO').AsString))));
    sql.Add(',' + quotedstr(trim('成功')));
    sql.Add(')');
    execsql;
  end;

end;

end.

