unit U_TradeMarketInPut;

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

type
  TfrmTradeMarketInPut = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADOQueryLabel: TADOQuery;
    ADO_I1: TADOQuery;
    DS_I1: TDataSource;
    ToolButton3: TToolButton;
    IdFTP1: TIdFTP;
    ADO_I2: TADOQuery;
    DS_I2: TDataSource;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton5: TToolButton;
    Panel2: TPanel;
    Panel3: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column11: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    VC_PRTPs: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Panel4: TPanel;
    Panel5: TPanel;
    Label33: TLabel;
    Label38: TLabel;
    QtyMX: TcxTextEdit;
    GroupBox2: TGroupBox;
    cxGrid2: TcxGrid;
    TvMX: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    DS_MX: TDataSource;
    CDS_MX: TClientDataSet;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    Label25: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label14: TLabel;
    Label20: TLabel;
    Label19: TLabel;
    Label37: TLabel;
    Label18: TLabel;
    TMMNo: TcxTextEdit;
    TMMDate: TDateTimePicker;
    DLYDate: TDateTimePicker;
    OrdNote: TMemo;
    OrdType: TComboBox;
    OurCoName: TcxButtonEdit;
    CustName: TcxButtonEdit;
    ShippingAddress: TcxButtonEdit;
    CONTACT: TcxButtonEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ScrollBox1Click(Sender: TObject);
    procedure v1Column5PropertiesEditValueChanged(Sender: TObject);
    procedure HYTTBtnUpClick(Sender: TObject);
    procedure KYTTBtnUpClick(Sender: TObject);
    procedure edtGYBtnUpClick(Sender: TObject);
    procedure v1OrderUnitPropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxDBImage1DblClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TechnicsBtnUpClick(Sender: TObject);
    procedure TechnicsBtnDnClick(Sender: TObject);
    procedure PackNoteDblClick(Sender: TObject);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure QtyMXKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TvMXCellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure OurCoNameDblClick(Sender: TObject);
    procedure OurCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ShippingAddressPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    FSCXDH: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    function getTMMNo(fconNo: string): string;
    procedure InitImage1();
    procedure InitImage2();
    procedure LookImage(FileName: string);
    procedure InitMXGrid(MTMSId: string);
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FTMMID, FFTMMID, FTMMNo, MTMSId: string;
    FXS: Integer;
    canshu1: string;
    fFlileFlag: string;
    fordtype: string;
    { Public declarations }
  end;

var
  frmTradeMarketInPut: TfrmTradeMarketInPut;
  newh: hwnd;

implementation

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


{$R *.dfm}

procedure TfrmTradeMarketInPut.InitImage1();
begin

end;

procedure TfrmTradeMarketInPut.InitImage2();
begin

end;

function TfrmTradeMarketInPut.getTMMNo(fconNo: string): string;
var
  str1: string;
  i: integer;
begin
  str1 := trim(fconNo) + '01';
  with ADOTemp do
  begin
    close;
    sql.Clear;
    sql.Add('select TMMID from Trade_Plan_Main where TMMNo=' + quotedstr(trim(str1)));
    open;
  end;
  if not ADOTemp.IsEmpty then
  begin
    for i := 1 to 99 do
    begin
      str1 := trim(fconNo) + rightstr(inttostr(100 + i), 2);
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select TMMID from Trade_Plan_Main where TMMNo=' + quotedstr(trim(str1)));
        open;
      end;
      if ADOCmd.IsEmpty then
        break;
    end;
  end;
  result := str1;
end;

procedure TfrmTradeMarketInPut.TBCloseClick(Sender: TObject);
begin

  Close;

end;

procedure TfrmTradeMarketInPut.InitData();
var
  FDate: TDateTime;
begin
  FDate := SGetServerDateTime(ADOTemp);
  TMMDate.DateTime := FDate;
  DlyDate.DateTime := FDate;

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Trade_Market_Main where TMMID=''' + Trim(FTMMID) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);
  SCSHData(ADOTemp, ScrollBox1, 5);
  SCSHData(ADOTemp, ScrollBox1, 9);
  SCSHData(ADOTemp, ScrollBox1, 7);
  OurCoName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('OurCoNo').AsString);
  CustName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('CustNo').AsString);
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select *  from Trade_Market_Sub where 1=1 ');
    if PState = 1 then
    begin
      sql.Add(' and TMMID=''' + Trim(FTMMID) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' and 1<>1');
    end;
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_1);
  SInitCDSData(ADOTemp, CDS_1);

  if PState = 0 then
  begin
    TMMDate.DateTime := SGetServerDateTime(ADOTemp);
    DlyDate.DateTime := TMMDate.DateTime;
    TMMNo.Text := '';
    TMMNo.Enabled := True;
  end
  else
  begin
  end;
  if CopyInt = 99 then
  begin
    PState := 0;
    FTMMID := '';
    TMMNo.Text := '';
    TMMNo.Enabled := True;

    TMMDate.DateTime := FDate;

    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('TMMID').Value := '';
        FieldByName('TMSId').Value := '';
        Post;
        Next;
      end;
    end;

  end;
end;

procedure TfrmTradeMarketInPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
var
  FType, ZDYName, FText: string;
begin
end;

procedure TfrmTradeMarketInPut.FormShow(Sender: TObject);
var
  i: Integer;
begin
  inherited;
  readCxGrid(TRIM(SELF.Caption), Tv1, '生产指示单管理');
  TMMNo.Text := '';
  fFlileFlag := UserDataFlag + 'HX';
  InitData();
  ordtype.Items.Clear;
  ordtype.Items.Add(fordtype);
  ordtype.ItemIndex := 0;
  InitImage1();
  InitImage2();
end;

function TfrmTradeMarketInPut.SaveData(): Boolean;
var
  maxno, Smaxno, maxTMMNo, MaxFNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FTMMID) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'JM', 'Trade_Market_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败!');
    end
    else
    begin
      maxno := Trim(FTMMID);
    end;

    if TMMNo.Text = '' then
      TMMNo.Text := maxno;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Trade_Market_Main where TMMID=''' + Trim(FTMMID) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FTMMID) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd, 'Trade_Market_Main', ScrollBox1, 2);
      FieldByName('TMMID').Value := Trim(maxno);
      FieldByName('OurCoNo').Value := Trim(OurCoName.Properties.LookupItems.Text);
      FieldByName('CustNo').Value := Trim(CustName.Properties.LookupItems.Text);

      Post;
    end;

/////////////////////               保存子表            ///////////////////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('TMSId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_Market_Sub', 4, 1) = False then
            raise Exception.Create('取子流水号失败!');
        end
        else
        begin
          Smaxno := Trim(CDS_1.fieldbyname('TMSId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Trade_Market_Sub where TMMID=''' + Trim(maxno) + '''');
          sql.Add(' and TMSId=''' + Trim(Smaxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(CDS_1.fieldbyname('TMSId').AsString) = '' then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Trade_Market_Sub', 0);

          RTSetsavedata(ADOCmd, 'Trade_Market_Sub', ScrollBox1, 7);
          FieldByName('TMMID').Value := Trim(maxno);
          FieldByName('TMSId').Value := Trim(Smaxno);
//          FieldByName('ConSId').Value := Trim(CDS_1.fieldbyname('ConSId').AsString);
//          FieldByName('ConMId').Value := Trim(CDS_1.fieldbyname('ConMId').AsString);
          Post;
          with CDS_1 do
          begin
            Edit;
            fieldbyname('TMSId').Value := Trim(Smaxno);
            Post;
          end;

        end;
        Next;
      end;
    end;
/////////////////////               保存子表           ///////////////////////////
/////////////////////               保存加工厂            ///////////////////////////
//    with ADOCmd do
//    begin
//      Close;
//      sql.Clear;
//      SQL.Add('select * from Trade_Plan_Fty where TPFId=''' + Trim(FTMMID) + '''');
//      Open;
//    end;
//    with ADOCmd do
//    begin
//      if ADOCmd.IsEmpty then
//      begin
//        Append;
//      end
//      else
//        Edit;
//      RTSetsavedata(ADOCmd, 'Trade_Plan_Fty', ScrollBox1, 5);
//      FieldByName('TMMID').Value := Trim(FTMMID);
//      FieldByName('TPFId').Value := Trim(FTMMID);
//      Post;
//    end;
/////////////////////               保存加工厂           ///////////////////////////
///
///////////////       保存打卷设置       ///////////////
//    with ADOCmd do
//    begin
//      Close;
//      sql.Clear;
//      SQL.Add('select * from Trade_Cloth_Inspect_Rule where RuleID=''' + Trim(FTMMID) + '''');
//      Open;
//    end;
//    with ADOCmd do
//    begin
//      if ADOCmd.IsEmpty then
//      begin
//        Append;
//      end
//      else
//        Edit;
//      RTSetsavedata(ADOCmd, 'Trade_Cloth_Inspect_Rule', ScrollBox1, 9);
//      FieldByName('RuleID').Value := Trim(FTMMID);
//      Post;
//    end;
///////////////       保存打卷设置       ///////////////

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Trade_Market_Main where TMMNo=''' + Trim(TMMNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
      raise Exception.Create('订单号重复!');

    FTMMID := Trim(maxno);

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

    if pos('KEY 约束', Exception(ExceptObject).Message) > 0 then
    begin
      if GetLSNo(ADOCmd, Smaxno, 'JS', 'Trade_Market_Sub', 4, 1) = False then
      begin
        Application.MessageBox('重新取子流水号失败！', '提示', 0);
        Exit;
      end;
    end;
  end;
end;

procedure TfrmTradeMarketInPut.TBSaveClick(Sender: TObject);
var
  MAXNO1: string;
begin
  if PState <> 1 then
  begin
    TMMDate.SetFocus;
  end;

  if CDS_1.IsEmpty then
  begin
    Application.MessageBox('明细不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('TMSUnit', null, []) then
  begin
    Application.MessageBox('单位不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('TMSQty', null, []) then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;

//  if CDS_1.Locate('C_Color', null, []) then
//  begin
//    Application.MessageBox('颜色不能为空!', '提示', 0);
//    Exit;
//  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
//    ModalResult := 1;
  end;
end;

procedure TfrmTradeMarketInPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin
  if Trim(TMMNo.Text) = '' then
  begin
    Application.MessageBox('订单号不能为空!', '提示', 0);
    Exit;
  end;
  i := cds_1.RecordCount;
  i := i + 1;
  CopyAddRow(Tv1, cds_1);

  if GetLSNo(ADOTemp, maxno, 'JS', 'Trade_Market_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  with cds_1 do
  begin
    Edit;
    FieldByName('SerialNo').Value := IntToStr(i);
    FieldByName('TMSId').Value := Trim(maxno);
    FieldByName('TMSUnit').Value := 'M';
    Post;
  end;
end;

procedure TfrmTradeMarketInPut.ToolButton2Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除选择的数据吗？', '提示', 32 + 4) <> IDYES then
    exit;
  CDS_1.DisableControls;
  with CDS_1 do
  begin
    First;
    while Locate('SSel', True, []) do
    begin
      if Trim(CDS_1.fieldbyname('TMSId').AsString) <> '' then
      begin
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete Trade_Market_Sub where TMSId=''' + Trim(CDS_1.fieldbyname('TMSId').AsString) + '''');
          sql.Add('delete Trade_Market_Sub_Qty where TMSId=''' + Trim(CDS_1.fieldbyname('TMSId').AsString) + '''');
          ExecSQL;
        end;
      end;
      Delete;
    end;
  end;
  CDS_1.EnableControls;

end;

procedure TfrmTradeMarketInPut.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(TRIM(SELF.Caption), Tv1, '生产指示单管理');

end;

procedure TfrmTradeMarketInPut.ToolButton4Click(Sender: TObject);
var
  i: Integer;
  maxno: string;
begin
  inherited;
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin

      if ShowModal = 1 then
      begin



//       while CDS_1.locate('SSel', true, []) do
        begin

          if self.CDS_1.locate('C_Code', trim(CDS_1.Fieldbyname('C_Code').AsString), []) then
          begin
            CDS_1.Edit;
            CDS_1.fieldbyname('SSel').value := false;
//            continue;
          end;

          i := cds_1.RecordCount;

          with CDS_3 do
          begin

            DisableControls;
            First;

            while not eof do
            begin

              if FieldByName('SSEL').AsBoolean then
              begin
                i := i + 1;
                if GetLSNo(ADOTemp, maxno, 'JS', 'Trade_Market_Sub', 4, 1) = False then
                begin
                  ADOCmd.Connection.RollbackTrans;
                  Application.MessageBox('取子流水号失败！', '提示', 0);
                  Exit;
                end;
                with self.CDS_1 do
                begin
                  append;
                  FieldByName('SSel').Value := false;
                  FieldByName('SerialNo').Value := IntToStr(i);
                  FieldByName('TMSId').Value := Trim(maxno);
                  FieldByName('TMSUnit').Value := 'M';
                  Fieldbyname('C_Code').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
                  Fieldbyname('C_Name').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
                  Fieldbyname('C_Spec').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
                  Fieldbyname('C_Composition').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
                  Fieldbyname('C_Width').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
                  Fieldbyname('C_GramWeight').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);

//            Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
//            Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Color').AsString);
//            Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Pattern').AsString);

                  Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_ColorNo').AsString);
                  Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_Color').AsString);
                  Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_Pattern').AsString);
//                  Fieldbyname('C_StyleNo').value := trim(frmClothInfoSel.CDS_3.Fieldbyname('C_StyleNo').AsString);
//                  Fieldbyname('SerialNo').value := 999;
                end;
              end;
              Next;
            end;
            EnableControls;
          end;

//          with self.CDS_1 do
//          begin
//            append;
//            FieldByName('SSel').Value := false;
//            FieldByName('SerialNo').Value := IntToStr(i);
//            FieldByName('TMSId').Value := Trim(maxno);
//            FieldByName('TMSUnit').Value := 'M';
//            Fieldbyname('C_Code').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
//            Fieldbyname('C_Name').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
//            Fieldbyname('C_Spec').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
//            Fieldbyname('C_Composition').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
//            Fieldbyname('C_Width').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
//            Fieldbyname('C_GramWeight').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);
//
//            Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
//            Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Color').AsString);
//            Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Pattern').AsString);
//
//          end;
//          CDS_1.Edit;
//          CDS_1.fieldbyname('SSel').value := false;
        end;
      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;

end;

procedure TfrmTradeMarketInPut.edtGYBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
//  fsj := Trim(TBtnEditC(Sender).Hint);
//  FWZ := Pos('/', fsj);
//  i := 0;
//  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
//        TBtnEditC(Sender).text := '';
//        with ClientDataSet1 do
//        begin
//          First;
//          while not Eof do
//          begin
//            if FieldByName('SSel').AsBoolean = True then
//            begin
//              i := i + 1;
//              if TBtnEditC(Sender).text = '' then
//                TBtnEditC(Sender).text := FieldByName('ZDYName').AsString
//              else
//                TBtnEditC(Sender).text := TBtnEditC(Sender).text + ',' + FieldByName('ZDYName').AsString;   //inttostr(i)+'.'+
//            end;
//            Next;
//          end;
//        end;
//      end;
//    end;
//  finally
//    frmZDYHelpSel.Free;
//  end;
end;

procedure TfrmTradeMarketInPut.TechnicsBtnDnClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmTradeMarketInPut.TechnicsBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxTextEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxTextEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradeMarketInPut.CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        self.CustName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.CustName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeMarketInPut.cxDBImage1DblClick(Sender: TObject);
begin
  if TcxDBImage(Sender).Picture.Height = 0 then
    exit;
  LookImage(ADO_I1.fieldbyname(TcxDBImage(Sender).Hint).asstring);
end;

procedure TfrmTradeMarketInPut.LookImage(FileName: string);
var
  sFieldName: string;
begin

  sFieldName := leftbstr(ExtractFilePath(Application.ExeName), 1) + ':\图片查看';

  if not DirectoryExists(pchar(sFieldName)) then
    CreateDirectory(pchar(sFieldName), nil);

  sFieldName := sFieldName + '\' + trim(FileName);

  try
    IdFTP1.Host := ReadINIFileStr('SYSTEMSET.INI', 'SERVER', '服务器地址', '127.0.0.1');
    IdFTP1.Username := 'three';
    IdFTP1.Password := '641010';
    IdFTP1.Connect();
  except
    ;
  end;

  if IdFTP1.Connected then
  begin
    try
      IdFTP1.Get(fFlileFlag + '\' + Trim(FileName), sFieldName, true, false);
    except
      Application.MessageBox('客户图样文件不存在', '提示', MB_ICONWARNING);
      IdFTP1.Quit;
      Exit;
    end;
  end
  else
  begin
    Application.MessageBox('无法连接文件服务器', '提示', MB_ICONWARNING);
    IdFTP1.Quit;
    Exit;
  end;
  if IdFTP1.Connected then
    IdFTP1.Quit;
  ShellExecute(Handle, 'open', PChar(sFieldName), '', '', SW_SHOWNORMAL);
end;

procedure TfrmTradeMarketInPut.OurCoNameDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmTradeMarketInPut.OurCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '我司';
      if ShowModal = 1 then
      begin
        self.OurCoName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.OurCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeMarketInPut.v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  mvalues: string;
begin
  mvalues := tv1.Columns[tv1.Controller.FocusedColumn.index].DataBinding.FieldName;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrderUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName(mvalues).Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeMarketInPut.v1OrderUnitPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName, YValue: string;
  FDDQty, FCPCDXS, FQty, FDKCQty: Double;
begin
  mvalue := TcxComboBox(Sender).EditingText;                                           //变化后单位
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin

    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    YValue := FieldByName('DDQtyUnit').AsString;                                        //原单位=订单单位
    FDDQty := FieldByName('DDQty').AsFloat;                                            //订单数量
    FCPCDXS := FieldByName('CPCDXS').AsFloat;
    FDKCQty := FieldByName('DKCQty').AsFloat;
    FQty := FieldByName('PRTOrderQty').AsFloat;
  end;
  if mvalue = 'Kg' then
  begin
    if YValue = 'Y' then
    begin
      FQty := FDDQty * 0.9144 / FCPCDXS;
    end;
    if YValue = 'M' then
    begin
      FQty := FDDQty / FCPCDXS;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty;
    end;
  end;

  if mvalue = 'M' then
  begin
    if YValue = 'Y' then
    begin
      FQty := FDDQty * 0.9144;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty * FCPCDXS;
    end;
    if YValue = 'M' then
    begin
      FQty := FDDQty;
    end;
  end;
  if mvalue = 'Y' then
  begin
    if YValue = 'M' then
    begin
      FQty := FDDQty / 0.9144;
    end;
    if YValue = 'Kg' then
    begin
      FQty := FDDQty * FCPCDXS / 0.9144;
    end;
    if YValue = 'Y' then
    begin
      FQty := FDDQty;
    end;
  end;

  with CDS_1 do
  begin
    Edit;
    FieldByName('PRTOrderQty').Value := FQty - FDKCQty;
    Post;
  end;

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

procedure TfrmTradeMarketInPut.v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PriceUnit';
      flagname := '价格单位';
      if Trim(DParameters1) <> '高权限' then
      begin
        TBAdd.Visible := False;
        TBEdit.Visible := False;
        TBDel.Visible := False;
      end;

      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName('PriceUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeMarketInPut.ScrollBox1Click(Sender: TObject);
begin
  TMMDate.SetFocus;
end;

procedure TfrmTradeMarketInPut.ShippingAddressPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeMarketInPut.PackNoteDblClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
  i: integer;
begin
  fsj := Trim(TMemo(Sender).Hint);
  FWZ := Pos('/', fsj);
  i := 0;
  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
        TMemo(Sender).Lines.Clear;
        with ClientDataSet1 do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').AsBoolean = True then
            begin
              i := i + 1;
              TMemo(Sender).Lines.Add(FieldByName('ZDYName').AsString)      //inttostr(i)+'.'+
            end;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmTradeMarketInPut.QtyMXKeyPress(Sender: TObject; var Key: Char);
var
  MaxNo: string;
begin
  if Key = #13 then
  begin
    if Label38.Caption = '序号' then
    begin
      Application.MessageBox('请选择数据', '提示', 0);
      Exit;
    end;
    if StrTofloatDef(QtyMX.Text, 0) = 0 then
      Exit;
    if GetLSNo(ADOTemp, MaxNo, 'MX', 'Trade_Market_Sub_Qty', 4, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    try
      ADOCmd.Connection.BeginTrans;
      with ADOCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('insert into  Trade_Market_Sub_Qty (TMSQID,TMSId,Meter)');
        sql.Add('values(' + quotedstr(Trim(MaxNo)));
        sql.Add(',' + quotedstr(Trim(MTMSId)));
        sql.Add(',' + QtyMX.Text);
        sql.Add(')');
        ExecSQL;
      end;
      ADOCmd.Connection.CommitTrans;

      with CDS_MX do
      begin
        Append;
        FieldByName('TMSQID').Value := MaxNo;
        FieldByName('TMSId').Value := MTMSId;
        FieldByName('Meter').Value := QtyMX.Text;
        post;
      end;
    except
      ADOCmd.Connection.RollbackTrans;
      Application.MessageBox('添加失败!', '提示', 0);
    end;
    QtyMX.Text := '';
    TvMX.Controller.EditingController.ShowEdit();
  end;
  with cds_1 do
  begin
    edit;
    if CDS_MX.IsEmpty then
    begin
      Fieldbyname('TMSPiece').Value := 0;
      Fieldbyname('TMSQty').Value := 0;
    end
    else
    begin
      Fieldbyname('TMSPiece').Value := TvMX.DataController.Summary.FooterSummaryValues[1];
      Fieldbyname('TMSQty').Value := TvMX.DataController.Summary.FooterSummaryValues[0];
    end;

    post;
  end;
end;

procedure TfrmTradeMarketInPut.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
//      xzflag := 0;
//      kcflag := 1;
      if ShowModal = 1 then
      begin
        with Self.CDS_1 do
        begin
          edit;
          Fieldbyname('C_Code').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
          Fieldbyname('C_Name').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
          Fieldbyname('C_Spec').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
          Fieldbyname('C_Composition').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
          Fieldbyname('C_Width').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
          Fieldbyname('C_GramWeight').AsString := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);

          Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
          Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Color').AsString);
          Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Pattern').AsString);
          post;
        end;

      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;

end;

procedure TfrmTradeMarketInPut.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmPictureUpload := TfrmPictureUpload.create(self);
    with frmPictureUpload do
    begin
      fFlileFlag := self.fFlileFlag;
      FDataId := Trim(self.CDS_1.fieldbyname('AttName').AsString);
      FPictureName := Trim(self.CDS_1.fieldbyname('AttName').AsString);
      FWidth := 450;
      FHeight := 600;
      FTFType := 'HX';
      if showmodal = 1 then
      begin
        with self.CDS_1 do
        begin
          Edit;
          Fieldbyname('AttName').Value := frmPictureUpload.FPictureName;
          Post;
        end;
      end;
    end;
  finally
    frmPictureUpload.free;
  end;
end;

procedure TfrmTradeMarketInPut.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  inherited;
  Label38.Caption := '序号：' + Trim(cds_1.fieldbyname('SerialNo').AsString);
  MTMSId := Trim(cds_1.fieldbyname('TMSId').AsString);
  InitMXGrid(MTMSId);
end;

procedure TfrmTradeMarketInPut.TvMXCellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;
  if ToolBar2.Enabled = False then
    Exit;
  with ADOCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete Trade_Market_Sub_Qty   where TMSQID=' + quotedstr(Trim(CDS_MX.FieldByName('TMSQID').AsString)));
    ExecSQL;
  end;

//  with ADOCmd do
//  begin
//    close;
//    sql.Clear;
//    sql.Add(' exec P_KCSD_AllNew ');
//    sql.Add(' @Operation=''大货单明细删除'' ');
//    sql.Add(' ,@TMMNo=' + quotedstr(trim(TMMNo.Text)));
//    ExecSQL;
//  end;
  CDS_MX.Delete;
  with cds_1 do
  begin
    edit;
    if CDS_MX.IsEmpty then
    begin
      Fieldbyname('TMSPiece').Value := 0;
      Fieldbyname('TMSQty').Value := 0;
    end
    else
    begin
      Fieldbyname('TMSPiece').Value := TvMX.DataController.Summary.FooterSummaryValues[1];
      Fieldbyname('TMSQty').Value := TvMX.DataController.Summary.FooterSummaryValues[0];
    end;

    post;
  end;
end;

procedure TfrmTradeMarketInPut.InitMXGrid(MTMSId: string);
begin
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.* from Trade_Market_Sub_Qty A ');
    sql.Add('where TMSId=''' + Trim(MTMSId) + '''');
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_MX);
  SInitCDSData(ADOTemp, CDS_MX);
end;

procedure TfrmTradeMarketInPut.v1Column5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    FieldByName('MGTMMID').Value := '';
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmTradeMarketInPut.HYTTBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxTextEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxTextEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeMarketInPut.KYTTBtnUpClick(Sender: TObject);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxTextEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if Trim(flag) = 'OrdDefStr2' then
      begin
        V1Name.Caption := '中文名称';
        V1Note.Caption := '英文名称';
        fnote := True;
      end;
      if ShowModal = 1 then
      begin
        TcxTextEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

