unit U_YarnPurchasePlanPut_ZZD;

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,
  cxDropDownEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxDateRanges, dxBarBuiltInMenu,
  dxDateTimeWheelPicker, dxGDIPlusClasses, Vcl.Menus, MovePanel, cxCheckBox,
  cxImage, cxDBEdit, Vcl.DBCtrls, U_BaseInput, System.ImageList, Vcl.ImgList,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdExplicitTLSClientServerBase, IdFTP, ShellAPI, dxScrollbarAnnotations, dxCore,
  cxDateUtils, cxImageList, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinOffice2013White, dxSkinSharpPlus, dxSkinWXI;

type
  TfrmYarnPurchasePlanPut_ZZD = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    Order_Sub: TClientDataSet;
    cxgrdpmn1: TcxGridPopupMenu;
    pm_sub: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column7: TcxGridDBColumn;
    v1XHNo: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column2: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    lbl5: TLabel;
    lbl6: TLabel;
    lbl7: TLabel;
    lbl8: TLabel;
    lbl1: TLabel;
    lbl3: TLabel;
    PurNo: TcxTextEdit;
    Sellname: TcxButtonEdit;
    BuyName: TcxButtonEdit;
    PurDate: TcxDateEdit;
    Payment: TcxButtonEdit;
    ShipMent: TcxButtonEdit;
    Label23: TLabel;
    PurType: TcxComboBox;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    ADO_2: TADOQuery;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column2: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    TV2Column4: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Label1: TLabel;
    StkName: TcxButtonEdit;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Label2: TLabel;
    ShuiLv: TcxTextEdit;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Label3: TLabel;
    ADOQuery1: TADOQuery;
    Label4: TLabel;
    Tv1Column8: TcxGridDBColumn;
    Label5: TLabel;
    OrderNo: TcxButtonEdit;
    Label6: TLabel;
    C_Code: TcxTextEdit;
    Label7: TLabel;
    C_Name: TcxTextEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SalesmanBtnUpClick(Sender: TObject);
    procedure v1Column15PropertiesEditValueChanged(Sender: TObject);
    procedure v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ShippMentKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure BuyNameDblClick(Sender: TObject);
    procedure ShipMentPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SellnamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure BuyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SellnameClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure Tv1Column18PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ShuiLvPropertiesEditValueChanged(Sender: TObject);
    procedure IsTaxPropertiesChange(Sender: TObject);
    procedure Tv1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    FLeft, FTop: Integer;
    fuserName, Fstatus: string;
    procedure JSMoney();
    procedure JSMoney2();
    procedure InitData();
    function TestData(): Boolean;
    function SaveData(): Boolean;
    procedure SetXH();

    { Private declarations }
  public
    CopyInt: Integer;
    PState: string;
    fFlileFlag: string;
    FPurMId: string;
    FAuthority, FGSTT: string;
    FShuilv: Double;
    { Public declarations }
  end;

var
  frmYarnPurchasePlanPut_ZZD: TfrmYarnPurchasePlanPut_ZZD;
  newh: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_ClothInfoSel, U_YarnInfoSel,
  U_ProductInfoSel, U_SalesContract_ZZDSXSel, U_SalesContract_SXSel;

{$R *.dfm}

procedure TfrmYarnPurchasePlanPut_ZZD.JSMoney2();
var
  FPieceKG, FPiece, FQty, FPrice, FAmount, FPrice2, FAmount2, FSL: Double;        //个数，单价，
begin
  if Order_Sub.IsEmpty then
    exit;
  with Order_Sub do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      FPiece := Fieldbyname('Piece').AsFloat;
      FPieceKG := Fieldbyname('PieceKG').AsFloat;
      FPrice := Fieldbyname('Price').AsFloat;      //含税单价
      FSL := StrToFloatdef(ShuiLv.text, 0);
      with Order_Sub do
      begin
        FQty := RoundFloat(FPieceKG * FPiece, 2);

        FAmount := RoundFloat(FQty * FPrice, 2);      //含税金额
        FAmount2 := RoundFloat(FQty * FPrice2 * FSL, 2);     //不含税金额


        with Order_Sub do
        begin
          Edit;
          FieldByName('PieceKG').Value := FPieceKG;
          FieldByName('Piece').Value := FPiece;
          FieldByName('Qty').Value := FQty;

          FieldByName('Price').Value := FPrice;

          FieldByName('Amount').Value := FAmount;
          FieldByName('Amount2').Value := FAmount2;
          Post;
        end;
      end;

      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmYarnPurchasePlanPut_ZZD.SellnameClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Properties.OnButtonClick(Sender, 0);
end;

procedure TfrmYarnPurchasePlanPut_ZZD.SellnamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.Sellname.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.Sellname.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoName').AsString);

        if Trim(frmCompanySel.CDS_1.fieldbyname('Shuilv').AsString) <> '' then
        begin
          self.Shuilv.text := Trim(frmCompanySel.CDS_1.fieldbyname('Shuilv').AsString);
        end;

      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmYarnPurchasePlanPut_ZZD.SetXH();
var
  i: Integer;
begin
  with Order_Sub do
  begin
    First;
    i := 1;
    while not Eof do
    begin
      with Order_Sub do
      begin
        Edit;
        FieldByName('XHNoTemp').Value := i;
        Post;
      end;
      i := i + 1;
      Next;
    end;
  end;

  with Order_Sub do
  begin
    Order_Sub.IndexFieldNames := '';
    First;
    i := 1;
    while not Eof do
    begin
      with Order_Sub do
      begin
        Edit;
        FieldByName('SerialNo').Value := FieldByName('XHNoTemp').AsInteger;
        Post;
      end;
      i := i + 1;
      Next;
    end;
    Order_Sub.IndexFieldNames := 'SerialNo';
  end;
end;

procedure TfrmYarnPurchasePlanPut_ZZD.ShipMentPropertiesButtonClick(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
      MainType := 'Pur';
      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 TfrmYarnPurchasePlanPut_ZZD.ShippMentKeyPress(Sender: TObject; var Key: Char);
begin

  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
  begin
    Key := #0;
  end
  else if (pos('.', TcxTextEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;
end;

procedure TfrmYarnPurchasePlanPut_ZZD.ShuiLvPropertiesEditValueChanged(Sender: TObject);
begin
  JSMoney2();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.JSMoney();
var
  FConQty, FConPrice, FC_Money: Double;
begin
  with Order_Sub do
  begin
    FConQty := Fieldbyname('ConQty').AsFloat;
    FConPrice := Fieldbyname('ConPrice').AsFloat;
  end;
  //计算金额，保留2位小数
  FC_Money := RoundFloat(FConQty * FConPrice, 2);
  with Order_Sub do
  begin
    Edit;
    FieldByName('ConTotalAmount').Value := FC_Money;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

function TfrmYarnPurchasePlanPut_ZZD.TestData(): Boolean;
begin
  try
    if Trim(PurNo.Text) = '' then
      raise Exception.Create('采购单号不能为空！');

    if Trim(OrderNo.Text) = '' then
      raise Exception.Create('织造单号不能为空！');

//    if Trim(SellName.Text) = '' then
//      raise Exception.Create('供应商不能为空！');
//    if Trim(BuyName.Text) = '' then
//      raise Exception.Create('我方单位不能为空！');

    if Order_Sub.IsEmpty then
      raise Exception.Create('明细不能为空！');

//    if Order_Sub.Locate('Y_Name', null, []) then
//      raise Exception.Create('品名不能为空！');
//    if Order_Sub.Locate('Y_Name', '', []) then
//      raise Exception.Create('品名不能为空！');
//    if Order_Sub.Locate('Price', null, []) then
//      raise Exception.Create('单价不能为空！');
//    if Order_Sub.Locate('Qty', null, []) then
//      raise Exception.Create('数量不能为空！');
//    if Order_Sub.Locate('Qty', 0, []) then
//      raise Exception.Create('数量不能为0！');
//    if Order_Sub.Locate('QtyUnit', null, []) then
//      raise Exception.Create('数量单位不能为空！');
//    if Order_Sub.Locate('QtyUnit', '', []) then
//      raise Exception.Create('数量单位不能为空！');

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

procedure TfrmYarnPurchasePlanPut_ZZD.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmYarnPurchasePlanPut_ZZD.InitData();
var
  maxno: string;
begin
  //刷新产品信息
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Pur_YarnPlan_Sub ');
    sql.Add('where PurMId=''' + Trim(FPurMId) + '''');
    sql.Add(' order by SerialNo ');
    Open;
  end;
  SCreateCDS(ADOTemp, Order_Sub);
  SInitCDSData(ADOTemp, Order_Sub);
   //刷新条款信息
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Pur_YarnPlan_Main where PurMId=''' + Trim(FPurMId) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);

  BuyName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('BuyNo').AsString);
  Sellname.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('SellNo').AsString);
  StkName.Properties.LookupItems.Text := Trim(ADOTemp.FieldByName('StkNo').AsString);

  Fstatus := Trim(ADOTemp.FieldByName('status').AsString);

  if PState = '新增' then
  begin
    PurDate.Date := SGetServerDateTime(ADOTemp);
//    DlyDate.Date := PurDate.Date;
    PurNo.Text := '自动生成';
    Shuilv.text := '0.96';
  end;

  if PState = '复制' then
  begin
    PState := '新增';
    FPurMId := '';
    PurNo.Text := '自动生成';

    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if GetLSNo(ADOCmd, maxno, 'SCS', 'Pur_YarnPlan_Sub', 4, 1) = False then
        begin
          Application.MessageBox('取子流水号失败！', '提示', 0);
          Exit;
        end;
        Edit;
        FieldByName('PurMId').Value := '';
        FieldByName('PurSId').Value := maxno;
        Post;
        Next;
      end;
    end;
  end;

end;

procedure TfrmYarnPurchasePlanPut_ZZD.IsTaxPropertiesChange(Sender: TObject);
begin

//  if IsTax.text = '是' then
//  begin
  ShuiLv.text := FloatToStr(FShuilv);
//  end
//  else
//  begin
//    ShuiLv.text := '';
//  end;

end;

procedure TfrmYarnPurchasePlanPut_ZZD.N1Click(Sender: TObject);
var
  FValue, FFValue, FColumn, FFColumn, MHXSID: string;
  MXHNO: Integer;
begin
  if Fstatus = '1' then
  begin
    application.MessageBox('合同已审核，无法操作！', '提示信息', 0);
    exit;
  end;
  MXHNO := Order_Sub.FieldByName('XHNo').ASInteger;
  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FValue := Trim(Order_Sub.fieldbyname(FColumn).AsString);
  MHXSID := Trim(Order_Sub.fieldbyname('HXSID').AsString);
  with Order_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean then
      begin
        Edit;

        if FValue = '' then
        begin
          Order_Sub.FieldByName(FColumn).Value := null;
        end
        else
        begin
          Order_Sub.FieldByName(FColumn).Value := FValue;
        end;
        if FColumn = 'C_HX' then
        begin
          Order_Sub.FieldByName('HXSID').Value := MHXSID;
        end;
        Post;
        JSMoney();
      end;
      Next;
    end;
    EnableControls;
  end;
  Order_Sub.Locate('XHNO', MXHNO, []);
end;

procedure TfrmYarnPurchasePlanPut_ZZD.N2Click(Sender: TObject);
var
  FValue, MC_Code, MC_Craft, MC_Name, MC_Spec, MC_CF, MC_Color, MC_MF, MC_KZ, MC_Note, MC_Unit, MGcHuaNo, MSuoLv, MC_EName: string;
  MXHNO: Integer;
begin
  if Fstatus = '1' then
  begin
    application.MessageBox('合同已审核，无法操作！', '提示信息', 0);
    exit;
  end;

  MXHNO := Order_Sub.FieldByName('XHNo').ASInteger;
  MC_Code := Order_Sub.fieldbyname('C_Code').AsString;
  MC_Craft := Order_Sub.fieldbyname('C_Craft').AsString;
  MC_Name := Order_Sub.fieldbyname('C_Name').AsString;
  MC_Spec := Order_Sub.fieldbyname('C_Spec').AsString;
  MC_CF := Order_Sub.fieldbyname('C_CF').AsString;
  MC_Color := Order_Sub.fieldbyname('C_Color').AsString;
  MC_MF := Order_Sub.fieldbyname('C_MF').AsString;
  MC_KZ := Order_Sub.fieldbyname('C_KZ').AsString;
  MC_Note := Order_Sub.fieldbyname('C_Note').AsString;
  MC_Unit := Order_Sub.fieldbyname('C_Unit').AsString;
  MSuoLv := Order_Sub.fieldbyname('SuoLv').AsString;
  MGcHuaNo := Order_Sub.fieldbyname('GcHuaNo').AsString;
  MC_EName := Order_Sub.fieldbyname('C_EName').AsString;

  with Order_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if FieldByName('SSel').AsBoolean then
      begin
        Edit;
        Order_Sub.fieldbyname('C_Code').Value := MC_Code;
        Order_Sub.fieldbyname('C_Craft').Value := MC_Craft;
        Order_Sub.fieldbyname('C_Name').Value := MC_Name;
        Order_Sub.fieldbyname('C_Spec').Value := MC_Spec;
        Order_Sub.fieldbyname('C_CF').Value := MC_CF;
        Order_Sub.fieldbyname('C_Color').Value := MC_Color;
        Order_Sub.fieldbyname('C_MF').Value := MC_MF;
        Order_Sub.fieldbyname('C_KZ').Value := MC_KZ;
        Order_Sub.fieldbyname('C_Note').Value := MC_Note;
        Order_Sub.fieldbyname('C_Unit').Value := MC_Unit;
        Order_Sub.fieldbyname('SuoLv').Value := MSuoLv;
        Order_Sub.fieldbyname('GcHuaNo').Value := MGcHuaNo;
        Order_Sub.fieldbyname('C_EName').Value := MC_EName;
        Post;

      end;
      Next;
    end;
    EnableControls;
  end;
  Order_Sub.Locate('XHNO', MXHNO, []);
end;

procedure TfrmYarnPurchasePlanPut_ZZD.OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  i, SZ: Integer;
  maxno, MAX_BCPH, ZM: string;
begin

  if GetLSNo(ADOCmd, maxno, 'PS', 'Pur_YarnPlan_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := Order_Sub.RecordCount + 1;      //当前的dataset有多少行记录


  try
    frmSalesContract_ZZDSXSel := TfrmSalesContract_ZZDSXSel.Create(Application);
    with frmSalesContract_ZZDSXSel do
    begin
      if ShowModal = 1 then
      begin
        Self.OrderNo.text := CDS_1.fieldbyname('OrderNo').value;
        Self.C_Code.text := CDS_1.fieldbyname('C_Code').value;
        Self.C_Name.text := CDS_1.fieldbyname('C_Name').value;
        with CDS_1 do
        begin
          First;
          while not Eof do
          begin
            if CDS_1.fieldbyname('SSel').AsBoolean then
            begin
              with Self.Order_Sub do
              begin
                Append;
                FieldByName('SSEL').Value := False;
                FieldByName('SerialNo').Value := i;
                FieldByName('PurSId').Value := maxno + INTTOSTR(i);

                FieldByName('Y_Code').Value := CDS_1.fieldbyname('Y_Code').value;
                FieldByName('Y_Name').Value := CDS_1.fieldbyname('Y_Name').value;
                FieldByName('Y_Spec').Value := CDS_1.fieldbyname('Y_Spec').value;
                FieldByName('Y_Composition').Value := CDS_1.fieldbyname('Y_Composition').value;
                FieldByName('Y_Color').Value := Cds_1.FieldByName('Y_Color').Value;
                FieldByName('Denier').Value := CDS_1.fieldbyname('Denier').value;

                FieldByName('FF').Value := CDS_1.fieldbyname('FF').value;
                FieldByName('ND').Value := Cds_1.FieldByName('ND').Value;

                FieldByName('Qty').Value := Cds_1.FieldByName('SXQty').Value;
                FieldByName('QtyUnit').Value := 'Kg';

              end;
            end;
            i := i + 1;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmSalesContract_ZZDSXSel.Free;
  end;
  SetXH();
end;

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

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

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmYarnPurchasePlanPut_ZZD.FormShow(Sender: TObject);
begin
  inherited;
  readCxGrid(trim(self.Caption) + 'Tv1', Tv1, '销售合同管理');
  readCxGrid(trim(self.Caption) + 'Tv2', Tv2, '销售合同管理');
  fFlileFlag := UserDataFlag + 'HX';
  InitData();

end;

function TfrmYarnPurchasePlanPut_ZZD.SaveData(): Boolean;
var
  maxno: string;
  MPurNo, fDate: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
////////////////       保存主表          //////////////////////
    if Trim(FPurMId) = '' then
    begin
      if GetLSNo(ADOTemp, maxno, 'SQ', 'Pur_YarnPlan_Main', 3, 1) = False then
        raise Exception.Create('取最大号失败！');

      fDate := FormatDateTime('yy', SGetServerDate(ADOTemp));

      if trim(PurNo.Text) = '自动生成' then
      begin
        if GetLSNo(ADOTemp, MPurNo, '', 'Pur_YarnPlan_Main', 4, 1) = False then
          raise Exception.Create('取最新采购单号失败！');
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add(' select xx=dbo.GetPinYin(' + quotedstr(Sellname.Text) + ')');
          Open;
        end;
        PurNo.Text := 'HHD' + MPurNo + trim(ADOTemp.FieldByName('xx').AsString);
      end;
    end
    else
    begin
      maxno := Trim(FPurMId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Pur_YarnPlan_Main where PurMId=''' + Trim(FPurMId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FPurMId) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('status').Value := '0';
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      FieldByName('PurMId').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'Pur_YarnPlan_Main', ScrollBox1, 2);
      FieldByName('LX').Value := '织造单';
      FieldByName('BuyNo').Value := Trim(BuyName.Properties.LookupItems.Text);
      FieldByName('SellNo').Value := Trim(Sellname.Properties.LookupItems.Text);
      FieldByName('StkNo').Value := Trim(StkName.Properties.LookupItems.Text);
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Pur_YarnPlan_Main where PurNo=''' + Trim(PurNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
      raise Exception.Create('采购单号重复！');

    FPurMId := Trim(maxno);
////////////////       保存主表          //////////////////////
///
////////////////       保存子表          //////////////////////
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin

        maxno := Trim(Order_Sub.fieldbyname('PurSId').AsString);

        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Pur_YarnPlan_Sub    ');
          sql.Add('where   PurSId=''' + Trim(maxno) + '''');
          SQL.Add('and PurMId=''' + Trim(FPurMId) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then
            Append
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'Pur_YarnPlan_Sub', 0);
          FieldByName('PurMId').Value := Trim(FPurMId);
          FieldByName('PurSId').Value := Trim(maxno);
          FieldByName('StkNo2').Value := Order_Sub.fieldbyname('StkNo2').Value;
          Post;
        end;
        with Order_Sub do
        begin
          Edit;
          FieldByName('PurSId').Value := Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
////////////////       保存子表          //////////////////////

    ADOCmd.Connection.CommitTrans;
    Result := True;
  except

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

procedure TfrmYarnPurchasePlanPut_ZZD.TBSaveClick(Sender: TObject);
begin
  PurDate.SetFocus;

  if TestData() = false then
    exit;

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

procedure TfrmYarnPurchasePlanPut_ZZD.v1PRTOrderQtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FPieceKG, FPiece, FQty, FPrice, FAmount, FPrice2, FAmount2, FSL, FKeKG, FSJPrice, FSJMoney: Double;        //个数，单价，
  FKeShu: Integer;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);

  with Order_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;

    FQty := Fieldbyname('Qty').AsFloat;
    FPrice := Fieldbyname('Price').AsFloat;      //单价
  end;

  FAmount := RoundFloat(FQty * FPrice, 2);   //金额

  with Order_Sub do
  begin
    Edit;
    FieldByName('Qty').Value := FQty;
    FieldByName('Price').Value := FPrice;
    FieldByName('Amount').Value := FAmount;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.ToolButton1Click(Sender: TObject);  //纱线档案
var
  i, SZ: Integer;
  maxno, MAX_BCPH, ZM: string;
begin

  if GetLSNo(ADOCmd, maxno, 'PS', 'Pur_YarnPlan_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := Order_Sub.RecordCount + 1;      //当前的dataset有多少行记录


  try
    frmYarnInfoSel := TfrmYarnInfoSel.Create(Application);
    with frmYarnInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with CDS_1 do
        begin
          First;
          while not Eof do
          begin
            if CDS_1.fieldbyname('SSel').AsBoolean then
            begin
              with Self.Order_Sub do
              begin
                Append;
                FieldByName('SSEL').Value := False;
                FieldByName('SerialNo').Value := i;
                FieldByName('PurSId').Value := maxno + INTTOSTR(i);

                FieldByName('C_Degree').Value := '纱线';
                FieldByName('Y_Code').Value := CDS_1.fieldbyname('Y_Code').value;
                FieldByName('Y_Name').Value := CDS_1.fieldbyname('Y_Name').value;
                FieldByName('Y_Spec').Value := CDS_1.fieldbyname('Y_Spec').value;
                FieldByName('Y_Composition').Value := CDS_1.fieldbyname('Y_Composition').value;

                FieldByName('Denier').Value := CDS_1.fieldbyname('Denier').value;
                FieldByName('ZS').Value := CDS_1.fieldbyname('ZS').value;

                FieldByName('FF').Value := CDS_1.fieldbyname('FF').value;
                FieldByName('ND').Value := Cds_1.FieldByName('ND').Value;

                FieldByName('Y_Color').Value := Cds_1.FieldByName('Y_Color').Value;

                FieldByName('Qty').Value := 0;
                FieldByName('Price').Value := 0;
                FieldByName('QtyUnit').Value := 'Kg';

              end;
            end;
            i := i + 1;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmYarnInfoSel.Free;
  end;
  SetXH();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  while Order_Sub.Locate('SSel', True, []) do
  begin
//    with ADOTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from  Trade_Plan_Sub where PurSId=''' + Trim(Order_Sub.fieldbyname('PurSId').AsString) + '''');
//      open;
//    end;
//    if ADOTemp.IsEmpty = false then
//    begin
//      exit;
//      application.MessageBox('该明细已经在生产！', '提示信息');
//    end;
    if Trim(Order_Sub.fieldbyname('PurSId').AsString) <> '' then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete Pur_YarnPlan_Sub where PurSId=''' + Trim(Order_Sub.fieldbyname('PurSId').AsString) + '''');
        ExecSQL;
      end;
    end;
    Order_Sub.Delete;
  end;

  SetXH();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.ToolButton3Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '销售合同管理');
  WriteCxGrid(trim(self.Caption) + 'Tv2', Tv2, '销售合同管理');
end;

procedure TfrmYarnPurchasePlanPut_ZZD.ToolButton4Click(Sender: TObject);
var
  i, SZ: Integer;
  maxno, MAX_BCPH, ZM: string;
begin

  if GetLSNo(ADOCmd, maxno, 'PS', 'Pur_YarnPlan_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := Order_Sub.RecordCount + 1;      //当前的dataset有多少行记录


  try
    frmProductInfoSel := TfrmProductInfoSel.Create(Application);
    with frmProductInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with CDS_1 do
        begin
          First;
          while not Eof do
          begin
            if CDS_1.fieldbyname('SSel').AsBoolean then
            begin
              with Self.Order_Sub do
              begin
                Append;
                FieldByName('SSEL').Value := False;
                FieldByName('SerialNo').Value := i;
                FieldByName('PurSId').Value := maxno + INTTOSTR(i);

                FieldByName('C_Degree').Value := '机物料';
                FieldByName('Y_Code').Value := CDS_1.fieldbyname('P_Code').value;
                FieldByName('Y_Name').Value := CDS_1.fieldbyname('P_Name').value;
                FieldByName('Y_Spec').Value := CDS_1.fieldbyname('P_Spec').value;

                FieldByName('Qty').Value := 0;
                FieldByName('Price').Value := CDS_1.fieldbyname('Price').value;
                FieldByName('QtyUnit').Value := 'Kg';

              end;
            end;
            i := i + 1;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmProductInfoSel.Free;
  end;
  SetXH();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.ToolButton5Click(Sender: TObject);  //合同增加
var
  i, SZ: Integer;
  maxno, MAX_BCPH, ZM: string;
begin

  if GetLSNo(ADOCmd, maxno, 'PS', 'Pur_YarnPlan_Sub', 4, 1) = False then
  begin
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('取子流水号失败！', '提示', 0);
    Exit;
  end;
  i := Order_Sub.RecordCount + 1;      //当前的dataset有多少行记录


  try
    frmSalesContract_SXSel := TfrmSalesContract_SXSel.Create(Application);
    with frmSalesContract_SXSel do
    begin
      if ShowModal = 1 then
      begin
        with CDS_1 do
        begin
          First;
          while not Eof do
          begin
            if CDS_1.fieldbyname('SSel').AsBoolean then
            begin
              with Self.Order_Sub do
              begin
                Append;
                FieldByName('SSEL').Value := False;
                FieldByName('SerialNo').Value := i;
                FieldByName('PurSId').Value := maxno + INTTOSTR(i);

                FieldByName('C_Degree').Value := '纱线';
                FieldByName('Y_Code').Value := CDS_1.fieldbyname('Y_Code').value;
                FieldByName('Y_Name').Value := CDS_1.fieldbyname('Y_Name').value;
                FieldByName('Y_Spec').Value := CDS_1.fieldbyname('Y_Spec').value;
                FieldByName('Y_Composition').Value := CDS_1.fieldbyname('Y_Composition').value;
                FieldByName('Y_Color').Value := Cds_1.FieldByName('Y_Color').Value;
                FieldByName('Denier').Value := CDS_1.fieldbyname('Denier').value;

                FieldByName('FF').Value := CDS_1.fieldbyname('FF').value;
                FieldByName('ND').Value := Cds_1.FieldByName('ND').Value;

                FieldByName('Qty').Value := Cds_1.FieldByName('SXQty').Value;
                FieldByName('Price').Value := 0;
                FieldByName('QtyUnit').Value := 'Kg';

              end;
            end;
            i := i + 1;
            Next;
          end;
        end;
      end;
    end;
  finally
    frmSalesContract_SXSel.Free;
  end;
  SetXH();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.Tv1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'GX';
      flagname := '工序';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('GX').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.Tv1Column18PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('StkNo2').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('StkName2').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmYarnPurchasePlanPut_ZZD.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
//  with ADO_2 do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add(' select top 20 A.Sellname,A.PurDate,B.Y_Name,B.Price');
//    sql.Add(' from Pur_YarnPlan_Main A inner join Pur_YarnPlan_Sub B on A.PurMId=B.PurMId  ');
//    sql.Add('where Y_Name=' + quotedstr(Trim(Order_Sub.fieldbyname('Y_Name').AsString)));
//    sql.Add(' order by A.PurDate desc');
//    Open;
//  end;
//  SCreateCDS(ADO_2, CDS_2);
//  SInitCDSData(ADO_2, CDS_2);

  with ADOQuery1 do
  begin
//    Close;
//    sql.Clear;
//    sql.Add(' select Sum(stkqty) AS stkqty, Sum(stkpiece) AS stkpiece');
//    sql.Add(' from BS_Yarn_IO A    ');
//    SQL.Add(' where A.IOFlag=''入库'' and  isnull(StkQty,0)>0   ');
//    SQL.Add(' where A.IOFlag=''入库'' and  isnull(Stkpiece,0)>0   ');
//    sql.Add(' and isnull(stkName,'''')=''' + Trim('纱线') + '''');
//    sql.Add(' and Y_Code=' + quotedstr(Trim(Order_Sub.fieldbyname('Y_Code').AsString)));
//    Open;
    Close;
    sql.Clear;
    sql.Add(' select Sum(stkqty) AS qty, Sum(stkpiece) AS piece');
    sql.Add(' from BS_Yarn_IO A    ');
    SQL.Add(' where A.IOFlag=''入库'' and (isnull(Qty,0)>0 or isnull(piece,0)>0)');
    sql.Add(' and isnull(stkName,'''')=''' + Trim('纱线') + '''');
    sql.Add(' and Y_Code=' + quotedstr(Trim(Order_Sub.fieldbyname('Y_Code').AsString)));
    Open;
  end;
  Label3.Caption := '库存数量:' + Trim(ADOQuery1.fieldbyname('qty').AsString) + '    ' + 'Kg';
  Label4.Caption := '库存件数:' + Trim(ADOQuery1.fieldbyname('piece').AsString);
end;

procedure TfrmYarnPurchasePlanPut_ZZD.v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MPRTCodeName';
      flagname := '产品名称';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('PRTCodeName').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Order_Sub.fieldbyname('PRTCode').Value := Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_MF';
      flagname := '门幅信息';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('C_MF').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_KZ';
      flagname := '克重信息';
      if ShowModal = 1 then
      begin
        Order_Sub.Edit;
        Order_Sub.fieldbyname('C_KZ').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
     //   Order_Sub.fieldbyname('PRTCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Order_Sub.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmYarnPurchasePlanPut_ZZD.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  MCYNo: string;
begin
  frmClothInfoSel := TfrmClothInfoSel.Create(self);
  with frmClothInfoSel do
  begin
    if showmodal = 1 then
    begin
      Order_Sub.Edit;
      Order_Sub.fieldbyname('C_Code').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Code').asstring;

      Order_Sub.fieldbyname('C_Name').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Name').asstring;
      Order_Sub.fieldbyname('C_EName').Value := frmClothInfoSel.CDS_1.fieldbyname('C_EName').asstring;
      Order_Sub.fieldbyname('C_Spec').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Spec').asstring;
      Order_Sub.fieldbyname('C_Composition').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Composition').asstring;
      Order_Sub.fieldbyname('C_Color').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Color').asstring;
      Order_Sub.fieldbyname('C_Width').Value := frmClothInfoSel.CDS_1.fieldbyname('C_Width').asstring;
      Order_Sub.fieldbyname('C_GramWeight').Value := frmClothInfoSel.CDS_1.fieldbyname('C_GramWeight').asstring;
      Order_Sub.Post;
    end;
    free;
  end;

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

procedure TfrmYarnPurchasePlanPut_ZZD.SalesmanBtnUpClick(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 TfrmYarnPurchasePlanPut_ZZD.v1Column15PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;

  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with Order_Sub do
  begin
    Edit;
    FieldByName('YongJinPrice').Value := mvalue;
    FieldByName('YongJin').Value := FieldByName('YongJinPrice').AsFloat * FieldByName('PRTOrderQty').AsFloat;
    Post;
  end;
end;

end.

