unit U_BpDY;

interface

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

type
  TfrmbpDY = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    Label2: TLabel;
    CRType: TComboBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1P_SPEC: TcxGridDBColumn;
    v1P_MF: TcxGridDBColumn;
    v1P_KZ: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    ToCustName: TBtnEditA;
    v1CPType: TcxGridDBColumn;
    ckName: TComboBox;
    Label4: TLabel;
    Label5: TLabel;
    orderNO: TBtnEditA;
    Label6: TLabel;
    Label7: TLabel;
    defstr2: TEdit;
    v1P_Color: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    Label8: TLabel;
    InoutNo: TEdit;
    Label9: TLabel;
    custName: TBtnEditA;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    ywy: TBtnEditA;
    Label12: TLabel;
    v1Column4: TcxGridDBColumn;
    defstr1: TBtnEditA;
    TBSave: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1P_CodeNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToCustNameBtnClick(Sender: TObject);
    procedure orderNOBtnClick(Sender: TObject);
    procedure custNameBtnClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure ywyBtnClick(Sender: TObject);
    procedure defstr1BtnClick(Sender: TObject);
    procedure v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    procedure InitGrid();
    function savedate(): Boolean;
    procedure SetComboBox();
   
    { Private declarations }
  public
    canshu1: string;
    fkeyNO: string;
    FType: integer;
    fCKName: string;
    { Public declarations }
  end;

var
  frmbpDY: TfrmbpDY;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_ZdyAttachment, U_CPManageSel;
{$R *.dfm}

procedure TfrmbpDY.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''YPCK'' ');
    if trim(fCKName) <> '' then
      sql.Add('and zdyName=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      ckName.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if ckName.Items.Count > 0 then
    ckName.ItemIndex := 0;

  CRType.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''YPCKTYPE''');
    if trim(fCKName) <> '' then
      sql.Add('and note=' + quotedstr(trim(fCKName)));
    open;
    while not eof do
    begin
      CRType.Items.Add(trim(fieldbyname('zdyName').AsString));
      next;
    end;
  end;
  if CRType.Items.Count > 0 then
    CRType.ItemIndex := 0;
end;

function TfrmbpDY.savedate(): Boolean;
var
  fkcID: integer;
  Filler: string;
  FillTime: TdateTime;
  maxno: string;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;

    if FType = 0 then
    begin
      Filler := DName;
      FillTime := SGetServerDateTime(adoqueryTmp);
    end
    else
    begin
      with adoqueryTmp do
      begin
        close;
        sql.Clear;
        sql.Add('select * from YP_InOut');
        sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
        open;
        Filler := adoqueryTmp.fieldbyname('Filler').AsString;
        FillTime := adoqueryTmp.fieldbyname('FillTime').AsDateTime;
      end;
    end;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from YP_INout');
      sql.Add('where CRNO=' + quotedstr(trim(fkeyNO)));
      execsql;
    end;
    CDS_Main.DisableControls;
    CDS_Main.First;
    with CDS_Main do
    begin
      while not eof do
      begin

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from YP_INout');
          sql.Add('where 1=2 ');
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('CRNO').Value := trim(fkeyNO);
        ADOQueryCmd.FieldByName('CRID').Value := recNo;
        ADOQueryCmd.FieldByName('CKName').Value := trim(CKName.Text);
        ADOQueryCmd.FieldByName('InoutNO').Value := trim(InoutNO.Text);
        ADOQueryCmd.FieldByName('custNo').Value := trim(custName.TxtCode);
        ADOQueryCmd.FieldByName('custName').Value := trim(custName.Text);
        ADOQueryCmd.FieldByName('TocustNo').Value := trim(TocustName.TxtCode);
        ADOQueryCmd.FieldByName('TocustName').Value := trim(TocustName.Text);
        ADOQueryCmd.FieldByName('ywy').Value := trim(ywy.Text);
        ADOQueryCmd.FieldByName('factoryName').Value := trim(fieldbyname('factoryName').AsString);
        ADOQueryCmd.FieldByName('mainID').Value := trim(fieldbyname('mainID').AsString);
        ADOQueryCmd.FieldByName('subID').Value := trim(fieldbyname('subID').AsString);
        ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
        ADOQueryCmd.FieldByName('CRType').Value := '寄样';
        ADOQueryCmd.FieldByName('CRFlag').Value := '出库';
        ADOQueryCmd.FieldByName('kuwei').Value := trim(fieldbyname('kuwei').AsString);
        ADOQueryCmd.FieldByName('MJID').Value := trim(fieldbyname('MJID').AsString);
        ADOQueryCmd.FieldByName('MJXH').Value := trim(fieldbyname('MJXH').AsString);
        ADOQueryCmd.FieldByName('P_Code').Value := trim(fieldbyname('P_Code').AsString);
        ADOQueryCmd.FieldByName('P_CodeName').Value := trim(fieldbyname('P_CodeName').AsString);
        ADOQueryCmd.FieldByName('P_spec').Value := trim(fieldbyname('P_spec').AsString);
        ADOQueryCmd.FieldByName('P_CF').Value := trim(fieldbyname('P_CF').AsString);
        ADOQueryCmd.FieldByName('P_Color').Value := trim(fieldbyname('P_Color').AsString);
        ADOQueryCmd.FieldByName('P_MF').Value := fieldbyname('P_MF').AsString;
        ADOQueryCmd.FieldByName('P_KZ').Value := fieldbyname('P_KZ').AsString;
        ADOQueryCmd.FieldByName('RollNum').Value := fieldbyname('RollNum').AsFloat;
        ADOQueryCmd.FieldByName('Qty').Value := fieldbyname('Qty').AsFloat;
        ADOQueryCmd.FieldByName('QtyUnit').Value := trim(fieldbyname('QtyUnit').AsString);
        ADOQueryCmd.FieldByName('note').Value := trim(fieldbyname('note').AsString);
        ADOQueryCmd.FieldByName('Price').Value := fieldbyname('Price').AsFloat;
        ADOQueryCmd.FieldByName('money').Value := fieldbyname('money').AsFloat;
        ADOQueryCmd.FieldByName('Filler').Value := trim(Filler);
        ADOQueryCmd.FieldByName('FillTime').Value := dateTimetostr(FillTime);
        ADOQueryCmd.FieldByName('inoutNo').Value := trim(inoutNo.Text);
        ADOQueryCmd.FieldByName('defstr1').Value := trim(defstr1.Text);
        ADOQueryCmd.FieldByName('defstr2').Value := trim(defstr2.Text);
        if FType = 1 then
        begin
          ADOQueryCmd.FieldByName('editer').Value := trim(dName);
          ADOQueryCmd.FieldByName('editTime').Value := SGetServerDateTime(adoqueryTmp);
        end;
        ADOQueryCmd.FieldByName('kcID').Value := FieldByName('kcID').AsInteger;
        ADOQueryCmd.Post;

        CDS_Main.edit;
        CDS_Main.FieldByName('CRNO').Value := trim(fkeyNO);
        CDS_Main.FieldByName('CRID').Value := recNo;

        next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmbpDY.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*  from YP_InOut A');
      sql.Add('where A.CRNO=' + quotedstr(trim(fkeyNO)));
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
    if not CDS_Main.IsEmpty then
    begin
      CRTime.Date := CDS_Main.fieldbyname('CRTime').AsDateTime;
      CKName.ItemIndex := CKName.Items.IndexOf(trim(CDS_Main.fieldbyname('CKName').AsString));
      CRType.ItemIndex := CRType.Items.IndexOf(trim(CDS_Main.fieldbyname('CRType').AsString));
      InoutNO.Text := trim(CDS_Main.fieldbyname('InoutNO').AsString);
    //  orderNo.Text:=trim(CDS_Main.fieldbyname('orderNo').AsString);
    //  PRTCodeName.Text:=trim(CDS_Main.fieldbyname('PRTCodeName').AsString);
     // PRTColor.Text:=trim(CDS_Main.fieldbyname('PRTColor').AsString);
      custName.TxtCode := trim(CDS_Main.fieldbyname('custNo').AsString);
      custName.Text := trim(CDS_Main.fieldbyname('custName').AsString);
      ywy.Text := trim(CDS_Main.fieldbyname('ywy').AsString);
      TocustName.TxtCode := trim(CDS_Main.fieldbyname('TocustNo').AsString);
      TocustName.Text := trim(CDS_Main.fieldbyname('TocustName').AsString);
      defstr1.Text := trim(CDS_Main.fieldbyname('defstr1').AsString);
      defstr2.Text := trim(CDS_Main.fieldbyname('defstr2').AsString);
    end;
  finally
  end;
end;

procedure TfrmbpDY.FormDestroy(Sender: TObject);
begin
  frmbpDY := nil;
end;

procedure TfrmbpDY.FormCreate(Sender: TObject);
begin
  CRTime.Date := SGetServerDate(ADOQueryTmp);

  cxGrid1.Align := alclient;
end;

procedure TfrmbpDY.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption, Tv1, '成品仓库');
  Action := cahide;
end;

procedure TfrmbpDY.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption, Tv1, '成品仓库');
  SetComboBox();
  InitGrid();
  if fType = 0 then
  begin
    if GetLSNo(ADOQueryCmd, fkeyNo, 'CK', 'YP_InOut', 3, 1) = False then
    begin
      //  ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    inoutNo.Text := 'C' + Trim(RightBStr(fkeyNo, Length(fkeyNo) - 2));
  end;
end;

procedure TfrmbpDY.TBSaveClick(Sender: TObject);
begin
  if cds_main.IsEmpty then
    exit;
  CRTime.SetFocus;
  if cds_main.Locate('QtyUnit', NULL, []) then
  begin
    application.MessageBox('数量单位不能为空！', '提示信息', 0);
    exit;
  end;

  if savedate() then
  begin
    application.MessageBox('数据保存成功！', '提示信息');
    ModalResult := 1;
  end
  else
  begin
    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

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

procedure TfrmbpDY.ToolButton1Click(Sender: TObject);
begin
//  frmBpkcsumlist:=TfrmBpkcsumlist.create(self);
//  with frmBpkcsumlist do
//  begin
//    fType:=10;
//    fckName:=self.fckName;
//    if showmodal=1 then
//    begin
//      cds_main.DisableControls;
//      cds_main.First;
//      while not cds_main.Eof  do
//      begin
//        if (cds_main.fieldbyname('ssel').AsBoolean)  then
//        begin
//        self.cds_main.Append;
//       // self.cds_main.FieldByName('kcid').Value:=trim(cds_main.fieldbyname('kcid').AsString);
//        self.cds_main.FieldByName('factoryName').Value:=trim(cds_main.fieldbyname('factoryName').AsString);
//    //    self.cds_main.FieldByName('mainID').Value:=trim(cds_main.fieldbyname('mainID').AsString);
//     //   self.cds_main.FieldByName('subID').Value:=trim(cds_main.fieldbyname('subID').AsString);
//     //   self.cds_main.FieldByName('kuwei').Value:=trim(cds_main.fieldbyname('kuwei').AsString);
//     //   self.cds_main.FieldByName('MJID').Value:=trim(cds_main.fieldbyname('MJID').AsString);
//     //   self.cds_main.FieldByName('MJXH').Value:=trim(cds_main.fieldbyname('MJXH').AsString);
//     //   self.cds_main.FieldByName('CPTYPE').Value:=trim(cds_main.fieldbyname('CPTYPE').AsString);
//        self.cds_main.FieldByName('P_Code').Value:=trim(cds_main.fieldbyname('P_Code').AsString);
//        self.cds_main.FieldByName('P_CodeName').Value:=trim(cds_main.fieldbyname('P_CodeName').AsString);
//        self.cds_main.FieldByName('P_cf').Value:=trim(cds_main.fieldbyname('P_cf').AsString);
//        self.cds_main.FieldByName('P_spec').Value:=trim(cds_main.fieldbyname('P_spec').AsString);
//        self.cds_main.FieldByName('defnote5').Value:=trim(cds_main.fieldbyname('defnote5').AsString);
//        self.cds_main.FieldByName('P_Color').Value:=trim(cds_main.fieldbyname('P_Color').AsString);
//        self.cds_main.FieldByName('CYMF').Value:=trim(cds_main.fieldbyname('CYMF').AsString);
//        self.cds_main.FieldByName('CYKZ').Value:=trim(cds_main.fieldbyname('CYKZ').AsString);
//        self.cds_main.FieldByName('P_MF').Value:=0;
//        self.cds_main.FieldByName('P_KZ').Value:=0;
//        self.cds_main.FieldByName('RollNum').Value:=cds_main.fieldbyname('kcRollNum').AsFloat;
//        self.cds_main.FieldByName('Qty').Value:=cds_main.fieldbyname('kcQty').AsFloat;
//        self.cds_main.FieldByName('QtyUnit').Value:=trim(cds_main.fieldbyname('QtyUnit').AsString);
//        self.cds_main.FieldByName('kuwei').Value:=trim(cds_main.fieldbyname('kuwei').AsString);
//  //      self.cds_main.FieldByName('Price').Value:=cds_main.fieldbyname('Price').AsFloat;
//   //     self.cds_main.FieldByName('money').Value:=cds_main.fieldbyname('money').AsFloat;
//     //   self.cds_main.FieldByName('defstr1').Value:=trim(cds_main.fieldbyname('defstr1').AsString);
//        self.cds_main.Post;
//        end;
//        cds_main.Next;
//      end;
//      cds_main.EnableControls;
//    end;
//    free;
//  end;
  cds_main.Append;
  CDS_Main.FieldByName('price').Value := '0';
  cds_Main.fieldbyname('qtyUnit').Value := 'M';
  cds_main.post;
end;

procedure TfrmbpDY.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  CDS_Main.Delete;
end;

procedure TfrmbpDY.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmbpDY.ToCustNameBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'CustomerNoName';
      flagname := '客户名称';
      if ShowModal = 1 then
      begin
        ToCustName.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        ToCustName.TxtCode := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmbpDY.orderNOBtnClick(Sender: TObject);
begin
 {  frmProductOrderNewList:=TfrmProductOrderNewList.Create(Application);
   with  frmProductOrderNewList do
   begin
     DParameters1:='高权限';
     if showmodal=1 then
     begin
       self.fmainID:=trim(Order_Main.fieldbyname('MainID').AsString);
       self.fsubID:=trim(Order_Main.fieldbyname('subID').AsString);
       self.orderNo.Text:=trim(Order_Main.fieldbyname('orderNo').AsString);
       self.PRTCodeName.Text:=trim(Order_Main.fieldbyname('PRTCodeName').AsString);
       self.PRTColor.Text:=trim(Order_Main.fieldbyname('PRTColor').AsString);
     end;
     free;
   end;  }
end;

procedure TfrmbpDY.custNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if Self.canshu1 = '录入' then
        fywy := trim(ywy.Text);
      if ShowModal = 1 then
      begin
        CustName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        CustName.TxtCode := Trim(CDS_HZ.fieldbyname('CoCode').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;

end;

procedure TfrmbpDY.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  fieldname: string;
begin
  fieldname := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with CDS_Main 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;

procedure TfrmbpDY.ywyBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'ywy';
      flagname := '业务员名称';
      if ShowModal = 1 then
      begin
        ywy.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmbpDY.defstr1BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'YPYSType';
      flagname := '样品运输方式';
      if ShowModal = 1 then
      begin
        defstr1.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmbpDY.v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmCPManageSel := TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if showmodal = 1 then
    begin

      Self.CDS_Main.Edit;
      Self.CDS_Main.FieldByName('P_Code').Value := Trim(CDS_Main.fieldbyname('CYNo').AsString);
      Self.CDS_Main.FieldByName('P_CodeName').Value := Trim(CDS_Main.fieldbyname('CYName').AsString);
      Self.CDS_Main.FieldByName('P_SPEC').Value := Trim(CDS_Main.fieldbyname('CYSpec').AsString);
      Self.CDS_Main.FieldByName('P_CF').Value := Trim(CDS_Main.fieldbyname('CYCF').AsString);
      Self.CDS_Main.FieldByName('P_MF').Value := Trim(CDS_Main.fieldbyname('CYmf').AsString);
      Self.CDS_Main.FieldByName('P_KZ').Value := Trim(CDS_Main.fieldbyname('CYkz').AsString);

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

end.

