unit U_CPAddPN;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ADODB, DBClient, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, ComCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, cxTextEdit,
  cxButtonEdit, StdCtrls, ExtCtrls, cxCurrencyEdit, BtnEdit, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmCPAddPN = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label7: TLabel;
    CYNo: TEdit;
    CYNote: TMemo;
    Label5: TLabel;
    CYName: TEdit;
    Label15: TLabel;
    CYMF: TEdit;
    Label22: TLabel;
    CYKZ: TEdit;
    Label23: TLabel;
    Label2: TLabel;
    CYColor: TEdit;
    Label4: TLabel;
    CYHX: TEdit;
    Label10: TLabel;
    CYPrice: TEdit;
    CYID: TEdit;
    Label3: TLabel;
    Label6: TLabel;
    ZhenXing: TEdit;
    Label8: TLabel;
    ShaChang: TEdit;
    Label9: TLabel;
    CarType: TEdit;
    CYSpec: TMemo;
    Label13: TLabel;
    CYCL: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label12: TLabel;
    CYZhuanSu: TEdit;
    Label14: TLabel;
    WZCYPrice: TEdit;
    ForCustomer: TBtnEditA;
    Label11: TLabel;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
    procedure CYNoKeyPress(Sender: TObject; var Key: Char);
    procedure ForCustomerBtnClick(Sender: TObject);
  private
    canshu1: string;
    Fint: Integer;
    procedure InitSubGrid();
    function SaveData(): Boolean;
    { Private declarations }
  public
    FCYID, FCYCode, FCPID, FCPNO, FCPName: string;
    CopyInt: Integer;

    { Public declarations }
  end;

var
  frmCPAddPN: TfrmCPAddPN;

implementation

uses
  U_DataLink, U_Fun, U_iniParam, U_ZDYHelp, U_ZdyAttachGYS, U_FileUp,
  U_ZdyAttachment;

{$R *.dfm}

procedure TfrmCPAddPN.InitSubGrid();
var
  FXHInt: integer;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from CP_YDang where CYID=''' + Trim(FCYID) + '''');
      Open;
    end;
    SCSHDataNew(ADOQueryMain, ScrollBox1, 2);
    SCSHDataNew(ADOQueryMain, ScrollBox1, 0);
  finally
    ADOQueryMain.EnableControls;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CP_YDangSpec where CYID=''' + Trim(FCYID) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);

  if CDS_Sub.IsEmpty then
  begin
    for FXHInt := 1 to 15 do
    begin
      with CDS_Sub do
      begin
        Append;
        fieldbyname('XHInt').value := FXHInt;
      end;
    end;
  end;
end;

procedure TfrmCPAddPN.FormShow(Sender: TObject);
begin
  InitSubGrid();
  if CopyInt = 1 then
  begin
    FCYID := '';
    CYID.text := '';
  end;
end;

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

function TfrmCPAddPN.SaveData(): Boolean;
var
  maxId, FCYNo, maxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCYID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, maxId, 'Y', 'CP_YDang', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('区最大号失败！', '提示', 0);
        Exit;
      end;
      CYID.Text := trim(maxId);
      CYNO.Text := trim(maxId);
    end
    else
    begin
      maxId := Trim(FCYID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from CP_YDang where CYID=''' + Trim(FCYID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID) = '' then
      begin
        Append;
        FieldByName('CYType').Value := Trim(FCPID);
      end
      else
      begin
        Edit;
      end;
      FieldByName('CYID').Value := Trim(maxId);
      SSetsaveSqlNew(ADOQueryCmd, 'CP_YDang', ScrollBox1, 2);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from CP_YDang where CYNo=''' + Trim(CYNo.Text) + '''');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('编号重复！', '提示', 0);
        Exit;
      end;
    end;
    //保存配比表
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Sub.fieldbyname('PBID').AsString) = '' then
        begin
          if GetLSNo(ADOQueryCmd, maxno, 'YPS', 'CP_YDangSpec', 4, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(CDS_Sub.fieldbyname('PBID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CP_YDangSpec where PBID=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('PBID').AsString) = '' then
            Append
          else
            Edit;
          FieldByName('CYID').Value := Trim(maxId);
          FieldByName('PBID').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CP_YDangSpec', 0);
          Post;
        end;
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('PBID').Value := Trim(maxno);

        Next;
      end;
    end;
//    with ADOCmd do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('Update JYOrderCon_Main_PB Set ');
//      sql.Add('ConMoney=(select Sum(ConMXMoney) from JYOrderCon_Sub_PB A where A.MainId=JYOrderCon_Main_PB.MainId)');
//      sql.Add(',ConQty=(select Sum(PRTOrderQty) from JYOrderCon_Sub_PB A where A.MainId=JYOrderCon_Main_PB.MainId)');
//      sql.Add('where MainId='''+Trim(FMainId)+'''');
//      ExecSQL ;
//    end;
    ADOQueryCmd.Connection.CommitTrans;
    FCYID := maxId;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmCPAddPN.ToolButton1Click(Sender: TObject);
begin
  if CYName.Text = '' then
  begin
    Application.MessageBox('品名不能为空！', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmCPAddPN.CYColorKeyPress(Sender: TObject; var Key: Char);
var
  c: Integer;
begin
  if Key = '.' then
  begin
    for c := 1 to length(TEdit(Sender).text) do
    begin
      if Tedit(Sender).text[c] = '.' then
        Key := #0;
    end;
  end
  else if Key = #13 then
    PerForm(WM_NEXTDLGCTL, 0, 0)
  else if Key = #8 then
    Key := #8
  else if (Key < '0') or (Key > '9') then
    Key := #0;
end;

procedure TfrmCPAddPN.CYNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    SelectNext(ActiveControl as TWinControl, True, True);
  end;
end;

procedure TfrmCPAddPN.ForCustomerBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal = 1 then
      begin
        ForCustomer.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

end.

