unit U_ClothContractInPutPB;

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, cxDropDownEdit;

type
  TfrmClothContractInPutPB = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    ConNo: TEdit;
    Label4: TLabel;
    DeliveryDate: TDateTimePicker;
    Label5: TLabel;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1PRTSpec: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1ClothQty: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1ClothUnit: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    FactoryNoName: TcxButtonEdit;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Label2: TLabel;
    QDTime: TDateTimePicker;
    Label3: TLabel;
    CompanyName: TcxButtonEdit;
    Label6: TLabel;
    QDPalce: TEdit;
    Label8: TLabel;
    JHPlace: TcxButtonEdit;
    Label7: TLabel;
    ConTk2: TcxButtonEdit;
    Label10: TLabel;
    Label11: TLabel;
    ConTK4: TcxButtonEdit;
    Label12: TLabel;
    Label13: TLabel;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label15: TLabel;
    ConTk7: TEdit;
    Label9: TLabel;
    v1Column7: TcxGridDBColumn;
    Label14: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    LXPerson: TEdit;
    bankNo: TEdit;
    LXTel: TEdit;
    bankName: TEdit;
    LXaddress: TEdit;
    ConTK1: TEdit;
    ConTK3: TEdit;
    ConTK5: TEdit;
    ConTK6: TEdit;
    ADOQuery1: TADOQuery;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FactoryNoNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PRTMFPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1OrderQtyPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1ClothQtyPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure CompanyNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure JHPlacePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ConTK3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ConTK4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ConTk2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure FormCreate(Sender: TObject);
    procedure v1ClothUnitPropertiesEditValueChanged(Sender: TObject);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FMainId,FConNo:String;
    { Public declarations }
  end;

var
  frmClothContractInPutPB: TfrmClothContractInPutPB;

implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun, U_ProductOrder_CX, U_ZdyAttachGYS;

{$R *.dfm}

procedure TfrmClothContractInPutPB.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmClothContractInPutPB.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*');
    sql.Add('from Contract_sub A');
    sql.Add('where A.MainID='''+FMainId+'''') ;
    Open;
  end;
  SCreateCDS20(ADOQuery1,Order_Sub);
  SInitCDSData20(ADOQuery1,Order_Sub);
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*');
    sql.Add('from Contract_Main A');
    sql.Add('where A.MainID='''+FMainId+'''') ;
    Open;
  end;
  SCSHData(ADOQuery1,ScrollBox1,0);
end;

procedure TfrmClothContractInPutPB.ZDYHelp(FButn:TcxButtonEdit;LType:string);

begin

end;

procedure TfrmClothContractInPutPB.FormShow(Sender: TObject);
begin

  InitData();
  if PCopyInt=1 then
  begin
    FMainId:='';
    FConNo:='';
    ConNo.Text:='';
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('SubId').Value:='';
        Post;
        Next;
      end;
    end;
  end;
end;

function TfrmClothContractInPutPB.SaveData():Boolean;
var
  maxno,maxSubNo:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表\
    if Trim(FMainId)='' then
    begin
     if GetLSNo(ADOCmd,maxno,'PM','Contract_Main',2,1)=False then
     begin
       ADOCmd.Connection.RollbackTrans;
       Application.MessageBox('生成流水号异常！','提示',0);
       exit;
       end;
     end
     else
     begin
       maxno:=Trim(FMainId);
     end;

        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from Contract_Main where MainId='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then  Append else edit;
          FieldByName('MainId').Value:=Trim(maxno);
          FieldByName('HTType').Value:='采购合同';
          SSetsaveSql(ADOCmd,'Contract_Main',ScrollBox1,0);
          if Trim(FMainId)='' then
          begin
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
          end;
          Post;
        end;

    //FMainId:=Trim(maxno);
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('SubId').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxSubNo,'PS','Contract_Sub',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxSubNo:=Trim(Order_Sub.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Contract_Sub where MainId='''+Trim(maxno)+'''');
          sql.Add(' and SubId='''+Trim(maxSubNo)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then  Append else edit;
          SSetSaveDataCDSNew(ADOCmd,Tv1,Order_Sub,'Contract_Sub',0);
          FieldByName('MainId').Value:=Trim(maxno);
          FieldByName('SubId').Value:=Trim(maxSubNo);
          FieldByName('Qty').Value:=Order_Sub.fieldbyname('Qty').AsFloat;
          FieldByName('RollNum').Value:=Order_Sub.fieldbyname('RollNum').AsFloat;
          FieldByName('Price').Value:=Order_Sub.fieldbyname('Price').AsFloat;
          FieldByName('money').Value:=Order_Sub.fieldbyname('money').AsFloat;
       //   FieldByName('C_status').Value:='0';
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SubId').Value:=Trim(maxSubNo);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmClothContractInPutPB.TBSaveClick(Sender: TObject);
begin
  DeliveryDate.SetFocus;
  if Trim(ConNo.Text)='' then
  begin
    Application.MessageBox('合同编号不能为空！','提示',0);
    Exit;
  end;

  if Trim(FactoryNoName.Text)='' then
  begin
    Application.MessageBox('供方不能为空！','提示',0);
    Exit;
  end;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if Order_Sub.Locate('Qty',null,[]) then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if Order_Sub.Locate('QtyUnit',null,[]) then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;



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

procedure TfrmClothContractInPutPB.ToolButton1Click(Sender: TObject);
begin
  with Order_Sub do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmClothContractInPutPB.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  if Trim(Order_Sub.fieldbyname('SubId').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Contract_Sub where SubId='''+Trim(Order_Sub.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmClothContractInPutPB.FactoryNoNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin

          FactoryNoName.text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmClothContractInPutPB.v1Column1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='Cloth';
      flagname:='坯布名称';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('C_CodeName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.Order_Sub.FieldByName('C_Code').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.v1PRTMFPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KZ';
      flagname:='克重单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('KZUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.v1OrderQtyPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MF';
      flagname:='门幅单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('MFUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.v1ClothQtyPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PriceUnit';
      flagname:='计价单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PriceUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.v1Column2PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CGUnit';
      flagname:='单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('QtyUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.CompanyNamePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdDefStr2';
      flagname:='需方';
      if ShowModal=1 then
      begin
        FXS:=99;
        CompanyName.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.JHPlacePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='JHPlace';
      flagname:='交货地点';
      if ShowModal=1 then
      begin
        JHPlace.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.ConTK3PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ConTK3';
      flagname:='包装要求';
      if ShowModal=1 then
      begin
        ConTK3.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.ConTK4PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ConTK4';
      flagname:='结算方式';
      if ShowModal=1 then
      begin
        ConTK4.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.ConTk2PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ConTK2';
      flagname:='运输方法';
      if ShowModal=1 then
      begin
        ConTK2.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.ConNoKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin
    if Trim(ConNo.Text)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select Top 1* from JYOrder_Main where OrderNo='''+Trim(ConNo.Text)+'''');
        Open;
      end;
      FactoryNoName.Text:=Trim(ADOTemp.fieldbyname('PBFactory').AsString);
    end;
  end;
end;

procedure TfrmClothContractInPutPB.v1Column10PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
   try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='Factory';
      flagname:='供方';
      MainType:='PBFactory';
      if ShowModal=1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('Sdefstr2').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmClothContractInPutPB.FormCreate(Sender: TObject);
begin
  QDTime.DateTime:=SGetServerDate(ADOTemp);
  DeliveryDate.DateTime:=SGetServerDate(ADOTemp);
end;

procedure TfrmClothContractInPutPB.v1ClothUnitPropertiesEditValueChanged(
  Sender: TObject);
var
  fieldname:string;
begin
  fieldname:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with Order_Sub do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=TcxTextEdit(Sender).EditingText;
    FieldByName('Money').Value:=strtofloat(format('%0.2f',[FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]));
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();
end;

end.
