unit U_TradeMarketDisSel;

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, dxScrollbarAnnotations, cxImageList;

type
  TfrmTradeMarketDisSel = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADOQueryLabel: TADOQuery;
    ToolButton3: TToolButton;
    IdFTP1: TIdFTP;
    DS_MX: TDataSource;
    CDS_MX: TClientDataSet;
    Panel2: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1CustomerNoName: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    v1PRTRollNum: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    v1OrdDefStr1: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1JGFactoryName: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Panel3: TPanel;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Ssel: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1C_Color: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1KCKW: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    MovePanel2: TMovePanel;
    CDS_MXSel: TClientDataSet;
    DS_MXSel: TDataSource;
    ADOQueryMain: TADOQuery;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label7: TLabel;
    Label15: TLabel;
    Label1: TLabel;
    c_code: TcxTextEdit;
    C_color: TcxTextEdit;
    CPType: TcxComboBox;
    Button1: TButton;
    PHYG: TBtnEditA;
    CheckBox1: TCheckBox;
    batchno: TcxTextEdit;
    Label2: TLabel;
    C_Pattern: TcxTextEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure CustomerNoNameBtnDnClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure Tv3FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TBSaveClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    fCode, fColor, FPattern: string;
    function SaveData(): Boolean;
    procedure InitOrder();
    procedure InitGrid();

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

var
  frmTradeMarketDisSel: TfrmTradeMarketDisSel;
  newh: hwnd;

implementation

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


{$R *.dfm}

procedure TfrmTradeMarketDisSel.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradeMarketDisSel.TBFindClick(Sender: TObject);
begin
  inherited;
  if ADOQueryMain.Active then
  begin
    SCreateCDS(ADOQueryMain, cds_mx);
    SInitCDSData(ADOQueryMain, cds_mx);
  end;
end;

function TfrmTradeMarketDisSel.SaveData(): Boolean;
var
  phID: string;
  P_ID: integer;
begin
  Result := false;
  ADOCmd.Connection.BeginTrans;
  P_ID := 1;
  try
    with CDS_MXSel do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if GetLSNo(adotemp, phID, 'PH', 'Trade_Market_Distribution', 4, 1) = False then
        begin
          Application.MessageBox('取配货单号失败！', '提示', 0);
          Exit;
        end;
        with ADOCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from Trade_Market_Distribution ');
          sql.Add('where 1=2 ');
          open;
        end;
        ADOCmd.Append;
        ADOCmd.FieldByName('TMDID').Value := trim(phID);
        ADOCmd.FieldByName('TMMID').Value := trim(cds_1.fieldbyname('TMMID').AsString);
        ADOCmd.FieldByName('TMSID').Value := trim(cds_1.fieldbyname('TMSID').AsString);
        ADOCmd.FieldByName('STKID').Value := trim(cds_mxsel.fieldbyname('STKID').AsString);
        ADOCmd.FieldByName('Filler').Value := trim(DName);
        ADOCmd.Post;

        with adocmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from BS_Cloth_IO ');
          sql.Add('where STKID=' + quotedstr(trim(CDS_MXSel.fieldbyname('STKID').AsString)));
          open;
          edit;
          fieldbyname('DisStatus').Value := '1';
          post;
        end;

        next;
      end;
      first;
      EnableControls;

      with adocmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from Trade_Market_Sub ');
        sql.Add('where TMMID=' + quotedstr(trim(cds_1.fieldbyname('TMMID').AsString)));
        sql.Add('and TMSID=' + quotedstr(trim(cds_1.fieldbyname('TMSID').AsString)));
//            ShowMessage(sql.Text);
        open;
        edit;
        fieldbyname('substatus').Value := '2';
        post;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := true;
  except
    ADOCmd.Connection.RollbackTrans;
  end;
end;

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

end;

procedure TfrmTradeMarketDisSel.TBSaveClick(Sender: TObject);
begin
  if CDS_MXSel.IsEmpty then
    exit;
  Panel1.SetFocus;
  with CDS_MXSel do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      if (trim(fieldbyname('c_code').AsString) <> trim(cds_1.FieldByName('c_code').AsString)) or (uppercase(trim(fieldbyname('c_color').AsString)) <> uppercase(trim(cds_1.FieldByName('C_Color').AsString))) or (uppercase(trim(fieldbyname('C_Pattern').AsString)) <> uppercase(trim(cds_1.FieldByName('C_Pattern').AsString))) then
      begin
        application.MessageBox('产品编号,花型或者颜色不一样，不能配货！', '提示信息', 0);
        EnableControls;
        exit;
      end;
      next;
    end;
    first;
    EnableControls;
  end;

  if SaveData() then
  begin
    application.MessageBox('配货成功!', '提示信息');
    CDS_1.Delete;
    CDS_MXSel.EmptyDataSet;
   // TBRafresh.Click;
  end
  else
  begin
    application.MessageBox('配货失败!', '提示信息', 0);
  end;
end;

procedure TfrmTradeMarketDisSel.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  i: Integer;
begin
  if cds_mx.IsEmpty then
    Exit;
  CDS_MXSel.Append;
  for i := 0 to CDS_MXSel.FieldCount - 1 do
  begin
    CDS_MXSel.Fields[i].Value := cds_mx.Fields[i].Value;
  end;
  CDS_MXSel.Post;
  CDS_MX.Delete;
end;

procedure TfrmTradeMarketDisSel.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  i: Integer;
begin
  if CDS_MXSel.IsEmpty then
    Exit;
  CDS_MX.Append;
  for i := 0 to CDS_MX.FieldCount - 1 do
  begin
    CDS_MX.Fields[i].Value := CDS_MXSel.Fields[i].Value;
  end;
  CDS_MX.FieldByName('ssel').Value := false;
  CDS_MX.Post;
  CDS_MXSel.Delete;
end;

procedure TfrmTradeMarketDisSel.Tv3FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  inherited;
  if CDS_1.IsEmpty then
  begin
    fCode := '!##@';
    fColor := '!##@';
    FPattern := '!##@';
  end
  else
  begin
    FCode := trim(cds_1.FieldByName('C_Code').AsString);
    fColor := trim(cds_1.FieldByName('C_Color').AsString);
    FPattern := trim(cds_1.FieldByName('C_Pattern').AsString);
  end;
  InitGrid();
end;

procedure TfrmTradeMarketDisSel.Button1Click(Sender: TObject);
var
  i: integer;
  mfieldName: string;
begin
  if CDS_MX.IsEmpty then
    Exit;
  CDS_mxSel.DisableControls;
  with CDS_MX do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      if (CDS_MX.Locate('ssel', true, [])) and not CDS_mxSel.Locate('stkid', CDS_MX.fieldbyname('stkid').AsString, []) then
      begin
        CDS_mxSel.Append;
        for i := 0 to CDS_mxSel.FieldCount - 1 do
        begin
          mfieldName := trim(CDS_mxSel.fields[i].FieldName);
          CDS_mxSel.Fields[i].Value := CDS_MX.fieldbyName(mfieldName).Value;
        end;
        CDS_mxSel.Post;
        CDS_MX.Delete;
      end;
      next;
    end;
    EnableControls;
    First;
  end;
  with CDS_MX do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      if (CDS_MX.Locate('ssel', true, [])) and not CDS_mxSel.Locate('stkid', CDS_MX.fieldbyname('stkid').AsString, []) then
      begin
        CDS_mxSel.Append;
        for i := 0 to CDS_mxSel.FieldCount - 1 do
        begin
          mfieldName := trim(CDS_mxSel.fields[i].FieldName);
          CDS_mxSel.Fields[i].Value := CDS_MX.fieldbyName(mfieldName).Value;
        //  CDS_SubSel.Fields[i].Value:=CDS_Main.Fields[i].Value;
        end;
        CDS_mxSel.Post;
        CDS_MX.Delete;
      end;
      next;
    end;
    EnableControls;
    First;
  end;
  CDS_mxSel.EnableControls;
end;

procedure TfrmTradeMarketDisSel.CheckBox1Click(Sender: TObject);
begin
  inherited;
  if cds_mx.IsEmpty then
    exit;
  with cds_mx do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value := checkbox1.Checked;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

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

procedure TfrmTradeMarketDisSel.InitOrder();
begin
  try
    with ADOTemp do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from Trade_Market_Sub A ');
      sql.Add(' inner join Trade_Market_Main B on B.TMMID=A.TMMID');
      sql.Add(' and isnull(substatus,''0'')=''0''');
      sql.Add(' and A.TMMID=' + quotedstr(trim(fmainID)));
      open;
    end;
    SCreateCDS(ADOTemp, cds_1);
    SInitCDSData(ADOTemp, cds_1);
  finally
  //  Order_Main.EnableControls;
  end;
end;

procedure TfrmTradeMarketDisSel.FormShow(Sender: TObject);
begin
  inherited;
  TBRafresh.Click;
end;

procedure TfrmTradeMarketDisSel.InitGrid();
begin
  try
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('exec P_Trade_Dispatched_View');
      SQL.Add('@C_Code=' + quotedstr(trim(fCode)));
      SQL.Add(',@C_Color=' + quotedstr(trim(fColor)));
      SQL.Add(',@C_Pattern=' + quotedstr(trim(FPattern)));
//      ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, cds_mx);
    SInitCDSData(ADOQueryMain, cds_mx);
    SCreateCDS(ADOQueryMain, cds_mxSel);
  finally
    TBFind.Click;
  end;
end;

end.

