unit U_YpBaoJiaInput;

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
  TfrmYpBaoJiaInput = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label4: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    Label20: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    BjNo: TEdit;
    Jhtime: TDateTimePicker;
    Bjtime: TDateTimePicker;
    CustomerName: TcxButtonEdit;
    BjCode: TcxButtonEdit;
    BjName: TEdit;
    BjMF: TEdit;
    BjKZ: TEdit;
    BJer: TcxButtonEdit;
    BJPhone: TEdit;
    ScrollBox2: TScrollBox;
    Label37: TLabel;
    Label39: TLabel;
    Label40: TLabel;
    Label51: TLabel;
    Label52: TLabel;
    Label53: TLabel;
    Label54: TLabel;
    YfCost: TEdit;
    DjBzCost: TEdit;
    Price: TEdit;
    CbPrice: TEdit;
    Lr: TEdit;
    Lrd: TEdit;
    ZzCost: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column10: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column5: TcxGridDBColumn;
    BjNote: TMemo;
    Label24: TLabel;
    Qty: TEdit;
    Label5: TLabel;
    zhenxing: TEdit;
    Label7: TLabel;
    huohao: TEdit;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Label8: TLabel;
    Label9: TLabel;
    zongzhenqty: TEdit;
    Label10: TLabel;
    MFZZS: TEdit;
    btn1: TToolButton;
    Tzhuanshu: TEdit;
    Label11: TLabel;
    wlzl: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxButtonEdit3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure CustomerNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure BjZKSExit(Sender: TObject);
    procedure ScrollBox2Click(Sender: TObject);
    procedure ScrollBox1Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
    procedure DjBzCostKeyPress(Sender: TObject; var Key: Char);
  private
    FXS: Integer;
    procedure InitData();
    procedure InitWlPb();
    procedure upWlPb();
    procedure InitYLQty();
    function SaveData(): Boolean;
    function RoundFloat(f: double; i: integer): double;
    procedure CBHJ();
    function Setstring(cString: string): string; 
    { Private declarations }
  public
    PState, PCopyInt: Integer;
    FMainId, FConNo: string;
    { Public declarations }
  end;

var
  frmYpBaoJiaInput: TfrmYpBaoJiaInput;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_CPManageFMSel;                //,U_ZdyAttachment

{$R *.dfm}

procedure TfrmYpBaoJiaInput.InitYLQty();
var
  MMmkz, MYlShb: double;
begin

  with Order_Sub do
  begin
    first;
    while not eof do
    begin
      with Order_Sub do
      begin
        MMmkz := FieldByName('Mmkz').AsFloat;
        MYlShb := FieldByName('YlShb').AsFloat;
        edit;
        FieldByName('YLQty').Value := RoundFloat(MMmkz * StrToFloatDef(Setstring(Qty.Text), 0) / 1000 * (MYlShb + 100) / 100, 2);

        post;
      end;

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

end;

function TfrmYpBaoJiaInput.Setstring(cString: string): string;   {提取数字}
var
  i: integer;
  str: string;
begin
  str := '';
  for i := 1 to length(cString) do
  begin
    if Pos(cString[i], '0123456789.') <= 0 then
      break
    else
      case cString[i] of
        '.':
          str := str + '.';

        '0':
          str := str + '0';
        '1':
          str := str + '1';
        '2':
          str := str + '2';
        '3':
          str := str + '3';
        '4':
          str := str + '4';
        '5':
          str := str + '5';
        '6':
          str := str + '6';
        '7':
          str := str + '7';
        '8':
          str := str + '8';
        '9':
          str := str + '9';
      end;
  end;
  Setstring := str;
end;

procedure TfrmYpBaoJiaInput.upWlPb();
var
  MBiLi, MYlShb, MWlPrice, MWlMoney: Double;
begin
  if Order_Sub.IsEmpty then
    Exit;

  with Order_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      MBiLi := fieldbyname('BiLi').AsFloat;
      MYlShb := fieldbyname('YlShb').AsFloat;
      MWlPrice := fieldbyname('WlPrice').AsFloat;

      MWlMoney := RoundFloat(MWlPrice * (MYlShb + 100) * MBiLi / 10000, 2);
      Edit;
      fieldbyname('WlMoney').Value := MWlMoney;
      Next;
    end;
    EnableControls;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmYpBaoJiaInput.InitWlPb();
begin
  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select WLID='''',WLName=A.YCLName,BiLi=A.PBQty,Mmkz=0,WlPrice=cast(0 as decimal(18,2)),WlMoney=cast(0 as decimal(18,2)),YlShb=A.ZPSH,YongShaLuShu,ShaChang,YCLSpec');
    sql.Add(',BjMainID='''',BjSubID='''',YLQty=0  from Cloth_YCL_PB A where MainId=(select  top 1 B.MainId from Cloth_Main B  inner join Cloth_Sub C on B.MainId=C.MainId');
    SQL.Add('where C_Code=' + Quotedstr(Trim(BjCode.Text)));
    SQL.Add('order by B.Filltime desc)');
   // ShowMessage(SQL.Text);
//    sql.Add('select BjMainID='''',BjSubID='''',Mmkz=0.00,WlMoney=0.00, YLQty=0.00,YLbl BiLi,WLID='''', YLName WLName, YlShb=0.00, WlPrice=0.00 ');
//    sql.Add('from CP_YDangSpec A  ');
//    sql.Add(' where A.CYID=(select top 1 CYID from CP_YDang where CYNo='+Quotedstr(Trim(BjCode.Text))+')');
//    sql.Add(' and isnull(YLName,'''')<>'''' and  isnull(YLbl,0)<>0 ');
//    ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOTemp, Order_Sub);
  SInitCDSData20(ADOTemp, Order_Sub);
end;

function TfrmYpBaoJiaInput.RoundFloat(f: double; i: integer): double;
var
  s: string;
  ef: extended;
begin
  s := '#.' + StringOfChar('0', i);
  ef := StrToFloat(FloatToStr(f)); //防止浮点运算的误差
  result := StrToFloat(FormatFloat(s, ef));
end;

procedure TfrmYpBaoJiaInput.CBHJ();
var
  MWlCost, MHclPrice, MZzCost, MHclCost, MDjBzCost, MQty, MKfCost, MBlKfCost, MYfCost, MLxCost, MBlLxCost, MYwCost, MBlYwCost, MSdCost, MBlSdCost, MBlZshCost, MZshCost, MBjZKS, MCbPrice, MPrice, MLr, MLrd: Double;
begin
  if TV1.DataController.Summary.FooterSummaryValues[0] <> NULL then
    MWlCost := TV1.DataController.Summary.FooterSummaryValues[0]
  else
    MWlCost := 0;
   //产量
  MQty := StrToFloatDef(Qty.Text, 0);

   //机台费
  MDjBzCost := StrToFloatDef(DjBzCost.Text, 0);

    //白坯加工费
  if MQty > 0 then
  begin
    MZzCost := RoundFloat(MDjBzCost / MQty, 2);
    ZzCost.Text := FloatToStr(MZzCost);
  end
  else
  begin
    ZzCost.Text := '0';
  end;
   //卖价
  MPrice := StrToFloatDef(Price.Text, 0);
   //运费
  MYfCost := StrToFloatDef(YfCost.Text, 0);


   //成本
  MCbPrice := MWlCost + MZzCost + MYfCost;

  MCbPrice := RoundFloat(MCbPrice, 2);

  CbPrice.Text := FloatToStr(MCbPrice);

  if MPrice > 0 then
  begin

    MLr := RoundFloat(MPrice - MCbPrice, 2);
    MLrd := RoundFloat(MLr / MPrice * 100, 2);
    Lr.Text := FloatToStr(MLr);
    Lrd.Text := FloatToStr(MLrd);
  end;
end;

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

procedure TfrmYpBaoJiaInput.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add('select A.*');
    sql.Add('from BaoJiaSub A');
    sql.Add('where A.BjMainID=''' + 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 BaoJiaMain A');
    sql.Add('where A.BJMainID=''' + FMainId + '''');
    Open;
  end;
  SCSHData(ADOQuery1, ScrollBox1, 0);
  SCSHData(ADOQuery1, ScrollBox2, 0);
end;

procedure TfrmYpBaoJiaInput.FormShow(Sender: TObject);
begin
  InitData();
  if PState = 0 then
  begin

  end;

  if PState = 2 then
  begin
    ScrollBox1.Enabled := False;
    ScrollBox2.Enabled := False;
    TBSave.Visible := False;
    cxGrid1.Enabled := False;
  end;

  if PCopyInt = 1 then
  begin
    with Order_Sub do
    begin
      first;
      while not eof do
      begin
        Edit;
        fieldbyname('BjSubID').value := '';
        Next;
      end;
    end;
    FMainId := '';
    PState := 0;
    bjno.Text := '自动生成';
  end;
end;

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

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from BaoJiaMain where BjMainID=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if ADOCmd.IsEmpty then
        Append
      else
        edit;
      FieldByName('ZzType').Value := '针织';
      FieldByName('BjMainID').Value := Trim(maxno);
      SSetsaveSql(ADOCmd, 'Contract_Main', ScrollBox1, 0);
      SSetsaveSql(ADOCmd, 'Contract_Main', ScrollBox2, 0);
      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillTime').Value := SGetServerDateTime(ADOTemp);
      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('BjSubID').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxSubNo, 'BJS', 'BaoJiaSub', 3, 1) = False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxSubNo := Trim(Order_Sub.fieldbyname('BjSubID').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from BaoJiaSub where BjMainId=''' + Trim(FMainId) + '''');
          sql.Add(' and BjSubId=''' + Trim(maxSubNo) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then
            Append
          else
            edit;
//          SSetSaveDataCDSNew(ADOCmd,Tv1,Order_Sub,'BaoJiaSub',1);
          FieldByName('WLID').Value := Order_Sub.FieldByName('WLID').Value;
          FieldByName('WLName').Value := Order_Sub.FieldByName('wlname').Value; //ylname
          FieldByName('BiLi').Value := Order_Sub.FieldByName('bili').Value;    //bili
          FieldByName('WlMoney').Value := Order_Sub.FieldByName('WlMoney').Value;
          FieldByName('Mmkz').Value := Order_Sub.FieldByName('Mmkz').Value;
          FieldByName('WlPrice').Value := Order_Sub.FieldByName('WlPrice').Value;
          FieldByName('YlShb').Value := Order_Sub.FieldByName('YlShb').Value;  //YlShb
          FieldByName('YLQty').Value := Order_Sub.FieldByName('YLQty').Value;
          FieldByName('YCLSpec').Value := Order_Sub.FieldByName('YCLSpec').Value;
          FieldByName('ShaChang').Value := Order_Sub.FieldByName('ShaChang').Value;
          FieldByName('yongshalushu').Value := Order_Sub.FieldByName('yongshalushu').Value;
          FieldByName('BjMainId').Value := Trim(maxno);
          FieldByName('BjSubId').Value := Trim(maxSubNo);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('BjSubId').Value := Trim(maxSubNo);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmYpBaoJiaInput.TBSaveClick(Sender: TObject);
begin
  lrd.SetFocus;
//  if Trim(CustomerName.Text)='' then
//  begin
//    Application.MessageBox('客户不能为空！','提示',0);
//    Exit;
//  end;
//
//  if TV1.DataController.Summary.FooterSummaryValues[1]<>100 then
//  begin
//     Application.MessageBox('比例合计必须为100！','提示',0);
//     Exit;
//  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
  end;
end;

procedure TfrmYpBaoJiaInput.FormCreate(Sender: TObject);
begin
  SetWindowLong(Qty.Handle, GWL_STYLE, GetWindowLong(Qty.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(ZzCost.Handle, GWL_STYLE, GetWindowLong(ZzCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(DjBzCost.Handle, GWL_STYLE, GetWindowLong(DjBzCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(YfCost.Handle, GWL_STYLE, GetWindowLong(YfCost.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(CbPrice.Handle, GWL_STYLE, GetWindowLong(CbPrice.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(Price.Handle, GWL_STYLE, GetWindowLong(Price.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(Lr.Handle, GWL_STYLE, GetWindowLong(Lr.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);
  SetWindowLong(Lrd.Handle, GWL_STYLE, GetWindowLong(Lrd.Handle, GWL_STYLE) or ES_MULTILINE or ES_RIGHT);

  Bjtime.DateTime := SGetServerDate(ADOTemp);

end;

procedure TfrmYpBaoJiaInput.cxButtonEdit3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := TcxButtonEdit(Sender).Name;
      flagname := TcxButtonEdit(Sender).Hint;
      V1Name.Caption := '联系人';
      fnote := True;
      V1Note.Caption := '联系电话';
      if ShowModal = 1 then
      begin
        BJer.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        BJPhone.Text := Trim(ClientDataSet1.fieldbyname('Note').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYpBaoJiaInput.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCPManageFMSel := TfrmCPManageFMSel.Create(Application);
    with frmCPManageFMSel do
    begin
      if ShowModal = 1 then
      begin
        BjCode.Text := Trim(CDS_Main.fieldbyname('CYNo').AsString);
        BjName.Text := Trim(CDS_Main.fieldbyname('CYName').AsString);
        BjMF.Text := Trim(CDS_Main.fieldbyname('CYMF').AsString);
        BjKZ.Text := Trim(CDS_Main.fieldbyname('CYKZ').AsString);
        ZhenXing.Text := Trim(CDS_Main.fieldbyname('ZhenXing').AsString);
        Qty.Text := Trim(CDS_Main.fieldbyname('CYCL').AsString);
  //      zongzhenqty.Text := Trim(CDS_Main.fieldbyname('zongzhenqty').AsString);
    //    MFZZS.Text := Trim(CDS_Main.fieldbyname('MFZZS').AsString);
        InitWlPb();
        CBHJ();
      end;
    end;
  finally
    frmCPManageFMSel.Free;
  end;
end;

procedure TfrmYpBaoJiaInput.v1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mavlue, FFieldName: string;
  MBiLi, MYlShb, MWlPrice: Double;
begin
  mavlue := TcxTextEdit(Sender).EditingText;
  FFieldName := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if mavlue = '' then
    mavlue := '0';
  with Order_Sub do
  begin
    edit;
    FieldByName(FFieldName).Value := mavlue;
    post;
    MBiLi := FieldByName('BiLi').AsFloat;
    MWlPrice := FieldByName('WlPrice').AsFloat;
    MYlShb := FieldByName('YlShb').AsFloat;
  end;

  with Order_Sub do
  begin
    edit;
    FieldByName('WlMoney').Value := RoundFloat(MWlPrice * (MYlShb + 100) * MBiLi / 10000, 2);
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();
  CBHJ();
end;

procedure TfrmYpBaoJiaInput.CustomerNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  try
//    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
//    with frmZdyAttachment do
//    begin
//      if ShowModal=1 then
//      begin
//        CustomerName.Text:=Trim(CDS_HZ.fieldbyname('ZDYName').AsString);
//        CustomerLevel.Text:=Trim(CDS_HZ.fieldbyname('DEFStr6').AsString);
//        if  Trim(CustomerLevel.Text)='A' then
//        begin
//           ZzCost.Text:='1.5';
//           CBHJ();
//        end;
//      end;
//    end;
//  finally
//    frmZdyAttachment.Free;
//  end;
end;

procedure TfrmYpBaoJiaInput.BjZKSExit(Sender: TObject);
begin
  upWlPb();
  CBHJ();
end;

procedure TfrmYpBaoJiaInput.ScrollBox2Click(Sender: TObject);
begin
  lrd.SetFocus;
end;

procedure TfrmYpBaoJiaInput.ScrollBox1Click(Sender: TObject);
begin
  lrd.SetFocus;
end;

procedure TfrmYpBaoJiaInput.btn1Click(Sender: TObject);
var
  SSY, ZS, xx, xx2, xx3: Double;
begin
  SSY := 0;
  with Order_Sub do
  begin
    first;
    while not Eof do
    begin
      SSY := SSY + FieldByName('YCLSpec').AsFloat * FieldByName('ShaChang').AsFloat * FieldByName('yongshalushu').AsFloat / 45000;
      Next;
    end;
  end;
  ZS := StrToFloatDef(wlzl.Text, 0) * 1000000 / StrToFloatDef(zongzhenqty.Text, 0) / SSY;
  xx := RoundFloat(ZS, 0);
  Tzhuanshu.Text := floattostr(xx);
//         Qty.Text:=floattostr(  ZS);
 // xx2 := floattostr(StrToFloatDef(MFZZS.Text, 0) / ZS * StrToFloatDef(wlzl.Text, 0) * 1440 * 0.88);
  xx2 := StrToFloatDef(MFZZS.Text, 0) / ZS * StrToFloatDef(wlzl.Text, 0) * 1440 * 0.88;
  xx3 := RoundFloat(xx2, 1);
  Qty.Text := floattostr(xx3);
end;

procedure TfrmYpBaoJiaInput.DjBzCostKeyPress(Sender: TObject; var Key: Char);
var
  zs, xx: Double;
begin
  if Key = #13 then
  begin
        CBHJ();
//    zs := StrToFloatDef(DjBzCost.Text, 0) / StrToFloatDef(qty.Text, 0);
//    xx := RoundFloat(zs, 2);
//
////         Qty.Text:=floattostr(  ZS);
// // xx2 := floattostr(StrToFloatDef(MFZZS.Text, 0) / ZS * StrToFloatDef(wlzl.Text, 0) * 1440 * 0.88);
//    CbPrice.Text := FloatToStr(StrToFloatDef(CbPrice.Text, 0) + xx);

  end;
end;

end.

