unit U_TradeMarketDisScan;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxDropDownEdit,
  RM_Common, RM_Preview, RM_System, RM_Class, RM_GridReport, RM_Dataset, Menus,
  cxSplitter, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCheckBox,
    dxDateRanges, dxBarBuiltInMenu, cxPC,
  System.ImageList, Vcl.ImgList, U_BaseInput, cxImage, cxDBEdit, IdBaseComponent,
  IdComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase,
  IdFTP, ShellAPI, MovePanel, RM_e_Xls, MMSystem, dxScrollbarAnnotations,
  cxImageList, dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmTradeMarketDisScan = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    GPM_1: TcxGridPopupMenu;
    ToolButton3: TToolButton;
    MovePanel2: TMovePanel;
    Panel1: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1P_Color: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1PHRollNum: TcxGridDBColumn;
    v1PHQty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    V2Column2: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    MovePanel1: TMovePanel;
    Label2: TLabel;
    Edit2: TcxTextEdit;
    Button4: TButton;
    Label1: TLabel;
    SmNO: TcxTextEdit;
    Label15: TLabel;
    PHYG: TBtnEditA;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    CDSOrder: TClientDataSet;
    DataSource1: TDataSource;
    ADOQueryTmp: TADOQuery;
    ADOQueryPrint: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    ADOQuery1: TADOQuery;
    CDSMJID: TClientDataSet;
    DataSource2: TDataSource;
    RMXLSExport2: TRMXLSExport;
    RM1: TRMGridReport;
    Tv1Column2: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    Button1: TButton;
    TV2Column2: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
    procedure TBSaveClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure PHYGBtnClick(Sender: TObject);
  private
    fCode, fColor, FPattern: string;
    function SaveData(): Boolean;
    procedure InitOrder();
    procedure InitMJID();
    procedure initGrid();

    { Private declarations }
  public
    FMainid: string;
    { Public declarations }
  end;

var
  frmTradeMarketDisScan: TfrmTradeMarketDisScan;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_ZDYHelpSel, U_RTFun, U_CompanySel, U_PictureUpload,
  U_ClothInfoSel, U_TradeDispatchedSel;


{$R *.dfm}

procedure TfrmTradeMarketDisScan.TBCloseClick(Sender: TObject);
begin

  Close;

end;

function TfrmTradeMarketDisScan.SaveData(): Boolean;
var
  phID: string;
  P_ID: integer;
begin

  Result := false;
  ADOQueryCmd.Connection.BeginTrans;
  P_ID := 1;
  try
    with CDSMJID do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if trim(CDSMJID.fieldbyname('SDefNote').AsString) = '扫描成功' then
        begin
          if GetLSNo(ADOQueryTmp, phID, 'PH', 'Trade_Market_Distribution', 4, 1) = False then
          begin
            Application.MessageBox('取配货单号失败！', '提示', 0);
            Exit;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from Trade_Market_Distribution ');
            sql.Add('where 1=2 ');
            open;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('TMDID').Value := trim(phID);
          ADOQueryCmd.FieldByName('TMMID').Value := trim(cdsmjid.fieldbyname('TMMID').AsString);
          ADOQueryCmd.FieldByName('TMSID').Value := trim(cdsmjid.fieldbyname('TMSID').AsString);
          ADOQueryCmd.FieldByName('STKID').Value := trim(cdsmjid.fieldbyname('STKID').AsString);
          ADOQueryCmd.FieldByName('Filler').Value := trim(DName);
          ADOQueryCmd.FieldByName('PHYG').Value := trim(PHYG.Text);
          ADOQueryCmd.FieldByName('TMDType').Value := '扫描配货';
          ADOQueryCmd.Post;

          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from Trade_Cloth_Stock ');
            sql.Add('where stkid=' + quotedstr(trim(CDSMJID.fieldbyname('stkid').AsString)));
            open;
            edit;
            fieldbyname('DISstatus').Value := '1';
            post;
          end;
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from Trade_Market_Sub ');
            sql.Add('where TMSID=' + quotedstr(trim(CDSMJID.fieldbyname('TMSID').AsString)));
            open;
            edit;
            fieldbyname('substatus').Value := '2';
            post;
          end;
        end;
        next;
      end;
      first;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmTradeMarketDisScan.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if Key = #13 then
  begin
    MovePanel1.Visible := false;
    if trim(smno.Text) = '' then
      exit;
    if uppercase(leftBstr(trim(smno.Text), 2)) = 'JM' then
    begin
      if not CDSMJID.IsEmpty then
      begin
        if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\数据未保存.wav')) then
          PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\数据未保存.wav'), 0, SND_ASYNC);
        exit;
      end;
      inItorder();
    end
    else
    begin
      if CDSOrder.IsEmpty then
      begin
        MovePanel1.Visible := true;
        Edit2.Text := SmNO.Text;
        Label2.Caption := '订单不能为空！';
        smno.Text := '';
        if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\订单不能为空.wav')) then
          PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\订单不能为空.wav'), 0, SND_ASYNC);
        exit;
      end;
      if not CDSMJID.Locate('CIID', trim(smNo.Text), []) then
      begin
        initMjid();
      end
      else
      begin
        if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\重复扫描.wav')) then
          PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\重复扫描.wav'), 0, SND_ASYNC);
      end;
    end;
    smno.Text := '';
    smno.SetFocus;
  end;
end;

procedure TfrmTradeMarketDisScan.TBRafreshClick(Sender: TObject);
begin
  inherited;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  initOrder();
  MovePanel2.Visible := False;

end;

procedure TfrmTradeMarketDisScan.TBSaveClick(Sender: TObject);
begin
  inherited;
  if CDSOrder.IsEmpty then
    exit;
  if CDSMJID.IsEmpty then
    exit;

  try
    if not SaveData() then
    begin
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\保存失败.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\保存失败.wav'), 0, SND_ASYNC);
      exit;
    end
    else
    begin
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\保存成功.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\保存成功.wav'), 0, SND_ASYNC);
      initGrid();
      exit;
    end;
  except
  end;
end;

procedure TfrmTradeMarketDisScan.TV2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
  inherited;
  var i: integer;
  begin
    i := tv2.GetColumnByFieldName('SDefNote').Index;
    if (AViewInfo.GridRecord.Values[i] <> '扫描成功') then
      ACanvas.Brush.Color := clred;
  end;
end;

procedure TfrmTradeMarketDisScan.Button1Click(Sender: TObject);
var
  key: Char;
begin
  inherited;
  key := #13;
  frmTradeDispatchedSel := TfrmTradeDispatchedSel.create(self);
  with frmTradeDispatchedSel do
  begin

    if showmodal = 1 then
    begin
      smno.Text := trim(Order_Main.FieldByName('TMMID').AsString);

      SmNOKeyPress(smno, key);
    end;
    free;
  end;
end;

procedure TfrmTradeMarketDisScan.Button4Click(Sender: TObject);
begin
  inherited;
  MovePanel1.Visible := False;
end;

procedure TfrmTradeMarketDisScan.CustomerNoNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmTradeMarketDisScan.InitOrder();
var
  fP_No: string;
begin
  MovePanel1.Visible := false;
  CDSMJID.EmptyDataSet;
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.add(' select   *  ');
    sql.Add(' ,PHRollNum=(select count(STKID) from Trade_Market_Distribution X where X.TMMID=A.TMMID and X.TMSID=B.TMSID),');
    sql.Add(' PHQty=(select sum(STKMeter) from Trade_Market_Distribution X inner join Trade_Cloth_Stock XX on X.stkid=XX.stkid where X.TMMID=A.TMMID and X.TMSID=B.TMSID)');
    sql.add(' from Trade_Market_Sub B');
    sql.add(' inner join Trade_Market_Main A on B.TMMID=A.TMMID ');
    SQL.Add(' where A.TMMID=' + quotedstr(trim(SmNO.text)));
    SQL.Add(' and isnull(B.substatus,''0'')<=''2'' ');
    Open;
  end;
  if ADOQueryMain.IsEmpty then
  begin
    MovePanel1.Visible := true;
    Edit2.text := SmNO.Text;
    Label2.Caption := '订单号未审核或已发货！';
    if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\订单不存在.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\订单不存在.wav'), 0, SND_ASYNC);
    exit;
  end
  else
  begin
    SCreateCDS(ADOQueryMain, CDSOrder);
    SInitCDSData(ADOQueryMain, CDSOrder);
    with CDSOrder do
    begin
      DisableControls;
      first;
      while not eof do
      begin

        next;
      end;
      first;
      EnableControls;
    end;
  end;

end;

procedure TfrmTradeMarketDisScan.PHYGBtnClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxTextEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxTextEdit(Sender).Text := ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmTradeMarketDisScan.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.add(' select   *  ');
    sql.Add(' ,PHRollNum=(select count(X.STKID) from Trade_Market_Distribution X where X.TMMID=A.TMMID and X.TMSID=B.TMSID),');
    sql.Add(' PHQty=(select sum(STKMeter) from Trade_Market_Distribution X inner join Trade_Cloth_Stock XX on X.stkid=XX.stkid where X.TMMID=A.TMMID and X.TMSID=B.TMSID)');
    sql.add(' from Trade_Market_Sub B');
    sql.add(' inner join Trade_Market_Main A on B.TMMID=A.TMMID ');
    SQL.Add(' where 1=2 ');
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDSOrder);
  SInitCDSData(ADOQueryMain, CDSOrder);

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.TMMID,TMSID,TMDID,cast(0 as int) as RollNum from Trade_Cloth_Stock A ');
    SQL.Add('inner join Trade_Market_Distribution B on B.STKID=A.STKID  ');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS(ADOQueryMain, CDSMJID);
  SInitCDSData(ADOQueryMain, CDSMJID);

end;

procedure TfrmTradeMarketDisScan.FormShow(Sender: TObject);
begin
  MovePanel1.Left := (Width - MovePanel1.Width) div 2;
  MovePanel1.top := (Height - MovePanel1.Height - 200) div 2;
  InitGrid();
end;

procedure TfrmTradeMarketDisScan.InitMJID();
var
  fP_No: string;
  i: integer;
begin
  MovePanel1.Visible := false;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select CIID from Trade_Cloth_Inspect A');
    SQL.Add('where A.CIID=' + quotedstr(trim(SmNO.text)));
    SQL.Add('and CIIOFlag=''未入库''');
    Open;
    if not IsEmpty then
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value := '此条码未入库';
      CDSMJID.FieldByName('CIID').Value := trim(SmNO.Text);
      CDSMJID.Post;
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\此条码未入库.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\此条码未入库.wav'), 0, SND_ASYNC);
      Exit;
    end;
  end;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select Stkid from Trade_Cloth_IO A');
    SQL.Add('where A.stkid=' + quotedstr(trim(SmNO.text)));
    SQL.Add('and IOFlag=''出库''');
    Open;
    if not IsEmpty then
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value := '此条码已出库';
      CDSMJID.FieldByName('Stkid').Value := trim(SmNO.Text);
      CDSMJID.Post;
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\此条码已出库.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\此条码已出库.wav'), 0, SND_ASYNC);
      Exit;
    end;
  end;

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from Trade_Cloth_Stock A');
    sql.add('left join  Trade_Cloth_IO B on B.stkid=A.stkid and B.IOFlag=''入库'' ');
    SQL.Add('where A.CIID=' + quotedstr(trim(SmNO.text)));
    sql.Add('and isnull(A.DisStatus,''0'')=''0''  ');
    sql.Add('and StkExists=1  ');
    Open;
  end;
  if ADOQueryMain.IsEmpty then
  begin
    CDSMJID.Append;
    CDSMJID.FieldByName('SDefNote').Value := '条码不存在或已经配货';
    CDSMJID.FieldByName('CIID').Value := trim(SmNO.Text);
    CDSMJID.Post;
    if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\扫描错误.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\扫描错误.wav'), 0, SND_ASYNC);
    exit;
  end;
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.STKID,B.TMMID,B.TMSID from Trade_Cloth_Stock A');
    sql.Add('inner join Trade_Market_Sub B on isnull(B.C_Color,'''')=isnull(A.C_Color,'''') and isnull(B.C_Pattern,'''')= isnull(A.C_Pattern,'''')  ');
    SQL.Add('where A.STKID=' + quotedstr(trim(SmNO.text)));
    SQL.Add('and B.TMMID=' + quotedstr(trim(CDSOrder.Fieldbyname('TMMID').AsString)));
 //   SQL.ADD('and isnull(B.PRTKZ,'''')<>'''' ');
    Open;
  end;

  if ADOQueryTmp.IsEmpty then
  begin
    CDSMJID.Append;
    CDSMJID.FieldByName('SDefNote').Value := '颜色和花型不对应';
    CDSMJID.FieldByName('STKID').Value := trim(SmNO.Text);
    CDSMJID.Post;
    if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\品名颜色不对应.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\品名颜色不对应.wav'), 0, SND_ASYNC);
    exit;
  end;
  i := 0;
  while not ADOQueryTmp.eof do
  begin
    if CDSOrder.Locate('TMSID', trim(ADOQueryTmp.fieldbyname('TMSID').AsString), []) then
    begin
      if (CDSOrder.fieldbyName('TMSPiece').AsInteger = CDSOrder.FieldByName('PHRollNum').AsInteger) and (CDSOrder.fieldbyName('TMSPiece').AsInteger > 0) then
      begin
      end
      else
      begin
        i := 1;
        CDSMJID.Append;
        CDSMJID.FieldByName('SDefNote').Value := '扫描成功';
        CDSMJID.FieldByName('CIID').Value := trim(ADOQueryMain.Fieldbyname('CIID').AsString);
        CDSMJID.FieldByName('stkid').Value := trim(ADOQueryMain.Fieldbyname('stkid').AsString);
        CDSMJID.FieldByName('C_Name').Value := trim(ADOQueryMain.Fieldbyname('C_Name').AsString);
        CDSMJID.FieldByName('C_Color').Value := trim(ADOQueryMain.Fieldbyname('C_Color').AsString);
        CDSMJID.FieldByName('Grade').Value := trim(ADOQueryMain.Fieldbyname('Grade').AsString);
        CDSMJID.FieldByName('RollNum').Value := 1;
        CDSMJID.FieldByName('stkmeter').Value := ADOQueryMain.Fieldbyname('stkmeter').AsFloat;
        CDSMJID.FieldByName('TMMID').Value := trim(ADOQueryTmp.Fieldbyname('TMMID').AsString);
        CDSMJID.FieldByName('TMSID').Value := trim(ADOQueryTmp.Fieldbyname('TMSID').AsString);
        CDSMJID.FieldByName('C_Width').Value := ADOQueryMain.Fieldbyname('C_Width').AsString;
        CDSMJID.FieldByName('C_GramWeight').Value := ADOQueryMain.Fieldbyname('C_GramWeight').AsString;
          //  CDSMJID.FieldByName('conNo').Value:=trim(ADOQueryMain.Fieldbyname('conNo').AsString);
        CDSMJID.Post;
        if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\正确.wav')) then
          PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\正确.wav'), 0, SND_ASYNC);
        CDSOrder.Edit;
        CDSOrder.fieldbyName('PHRollNum').Value := CDSOrder.fieldbyName('PHRollNum').AsInteger + 1;
        CDSOrder.fieldbyName('PHQty').Value := CDSOrder.fieldbyName('PHQty').AsFloat + ADOQueryMain.Fieldbyname('stkmeter').AsFloat;
        CDSOrder.Post;
        if tv1.DataController.Summary.FooterSummaryValues[0] <> 0 then
        begin
          if tv1.DataController.Summary.FooterSummaryValues[0] = tv2.DataController.Summary.FooterSummaryValues[0] then
          begin
            TBSave.Click;
            exit;
          end;
        end;
        exit;
      end;
    end;
    ADOQueryTmp.Next;
  end;

  if i = 0 then
  begin
    if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\超出下单匹数.wav')) then
      PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\超出下单匹数.wav'), 0, SND_ASYNC);

  end;
end;

end.

