unit U_PBCk;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, MMSystem,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxButtonEdit, cxDropDownEdit,
  DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, BtnEdit, cxContainer,
  cxTextEdit, cxCurrencyEdit, cxCheckBox, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport;

type
  TfrmPBCk = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    Label2: TLabel;
    CRType: TComboBox;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label8: TLabel;
    CRNo: TEdit;
    Label9: TLabel;
    ToFactoryName: TBtnEditA;
    Panel3: TPanel;
    ADOQueryMain: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_Code: TcxGridDBColumn;
    v1P_CodeName: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1Column2: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    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 TBCloseClick(Sender: TObject);
    procedure ToFactoryNameBtnClick(Sender: TObject);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure FHAddressBtnDnClick(Sender: TObject);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetFrom();
    function savedate(): Boolean;
    function CheckData(): Boolean;
    { Private declarations }
  public
    fkeyNO: string;
    FType: integer;
    fCKName, FZZType: string;
    { Public declarations }
  end;

var
  frmPBCk: TfrmPBCk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_PBKcList, U_ZdyAttachment, U_ZDYHelpSel,
  U_ZdyAttachGYS;

var
  frmPBKcList: TfrmPBKcList;
{$R *.dfm}

function TfrmPBCk.CheckData(): Boolean;
begin
  try
    if Trim(CRType.Text) = '' then
      raise Exception.Create('出库类型不能为空！');
    if Trim(ToFactoryName.Text) = '' then
      raise Exception.Create('收货单位不能为空！');

    if CDS_Main.IsEmpty then
      raise Exception.Create('明细不能为空！');
    if CDS_Main.Locate('Code', null, []) then
      raise Exception.Create('产品编号不能为空！');
    if CDS_Main.Locate('Code', '', []) then
      raise Exception.Create('产品编号不能为空！');
    if CDS_Main.Locate('CodeName', null, []) then
      raise Exception.Create('品名不能为空！');
    if CDS_Main.Locate('CodeName', '', []) then
      raise Exception.Create('品名不能为空！');

    if CDS_Main.Locate('Qty', null, []) then
      raise Exception.Create('数量不能为空！');
    if CDS_Main.Locate('Qty', 0, []) then
      raise Exception.Create('数量不能为0！');
    if CDS_Main.Locate('qtyUnit', null, []) then
      raise Exception.Create('数量单位不能为空！');
    if CDS_Main.Locate('qtyUnit', '', []) then
      raise Exception.Create('数量单位不能为空！');

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

procedure TfrmPBCk.SetFrom();
var
  i: integer;
begin
  if fType = 0 then
  begin
    if GetLSNo(ADOQueryCmd, fkeyNo, 'PCK', 'CK_PB_CR', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    CRNO.Text := trim(fkeyNo);
  end
  else
  begin

//    for i:=0 to tv1.ColumnCount-1 do
//    begin
//      IF uppercase(tv1.Columns[i].DataBinding.FieldName)<>'SSEL' then
//      tv1.Columns[i].Options.Editing:=false;
//    end;
//    v1Qty.Options.Editing:=true;
//    v1Price.Options.Editing:=true;
//    v1Note.Options.Editing:=true;
  end;
end;

function TfrmPBCk.savedate(): Boolean;
var
  MaxNo: string;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    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 CK_PB_CR');
          sql.Add('where CRID=' + quotedstr(Trim(CDS_Main.FieldByName('CRID').AsString)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'PC', 'CK_PB_CR', 3, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!', '提示', 0);
            Exit;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('Filler').Value := trim(DName);
          ADOQueryCmd.FieldByName('FillTime').Value := dateTimetostr(SGetServerDateTime(adoqueryTmp));
        end
        else
        begin
          MaxNo := Trim(CDS_Main.FieldByName('CRID').AsString);
          ADOQueryCmd.Edit;
          ADOQueryCmd.FieldByName('editer').Value := trim(dName);
          ADOQueryCmd.FieldByName('editTime').Value := SGetServerDateTime(adoqueryTmp);
        end;

        ADOQueryCmd.FieldByName('CRNO').Value := trim(CRNO.Text);
        ADOQueryCmd.FieldByName('CRID').Value := trim(MaxNo);
        ADOQueryCmd.FieldByName('CKName').Value := '坯布仓库';
        ADOQueryCmd.FieldByName('ToFactoryName').Value := trim(ToFactoryName.Text);
        ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
        ADOQueryCmd.FieldByName('CRType').Value := trim(CRType.Text);
        ADOQueryCmd.FieldByName('CRFlag').Value := '出库';
        ADOQueryCmd.FieldByName('CRQtyFlag').Value := -1;

        RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Main, 'CK_PB_CR', 0);
        ADOQueryCmd.FieldByName('CloMainId').Value := CDS_Main.fieldbyname('CloMainId').Value;
        ADOQueryCmd.FieldByName('CloSubId').Value := CDS_Main.fieldbyname('CloSubId').Value;
        ADOQueryCmd.Post;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add('exec P_Up_PBKc ' + quotedstr(trim(MaxNo)));
          Open;
        end;
        if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
        begin
          CDS_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数量少于出库数量!', '提示', 0);
          Exit;
        end;
        Edit;
        FieldByName('CRID').Value := trim(MaxNo);
        post;
        next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmPBCk.InitGrid();
begin
  try
    with adoqueryMain do
    begin
      close;
      sql.Clear;
      sql.Add('select A.* from CK_PB_CR A');
      sql.Add('where A.CRID=' + quotedstr(trim(fkeyNO)));
      open;
    end;
    SCreateCDS20(adoqueryMain, CDS_Main);
    SInitCDSData20(adoqueryMain, CDS_Main);
    if not CDS_Main.IsEmpty then
    begin
      CRTime.Date := CDS_Main.fieldbyname('CRTime').AsDateTime;
      CRType.ItemIndex := CRType.Items.IndexOf(trim(CDS_Main.fieldbyname('CRType').AsString));
      CRNO.Text := trim(CDS_Main.fieldbyname('CRNO').AsString);
      ToFactoryName.Text := trim(CDS_Main.fieldbyname('ToFactoryName').AsString);
    end;
  finally
  end;
end;

procedure TfrmPBCk.FormDestroy(Sender: TObject);
begin
  frmPBCk := nil;
end;

procedure TfrmPBCk.FormCreate(Sender: TObject);
begin
  CRTime.Date := SGetServerDate10(ADOQueryTmp);
end;

procedure TfrmPBCk.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cahide;
end;

procedure TfrmPBCk.FormShow(Sender: TObject);
var
  fbhType, fInoutNo: string;
begin
  readCxGrid(self.Caption + tv1.Name, Tv1, '面料仓库');
  InitGrid();
  SetFrom();
end;

procedure TfrmPBCk.TBSaveClick(Sender: TObject);
begin

  CRTime.SetFocus;
  if CheckData() = false then
    exit;

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

procedure TfrmPBCk.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 TfrmPBCk.ToolButton1Click(Sender: TObject);
var
  FMJID: string;
begin
  frmPBKcList := TfrmPBKcList.create(self);
  with frmPBKcList do
  begin
    fType := 10;
    FZZType := self.FZZType;
    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('CloOrderno').Value := trim(cds_main.fieldbyname('CloOrderno').AsString);
          self.cds_main.FieldByName('CloMainId').Value := trim(cds_main.fieldbyname('CloMainId').AsString);
          self.cds_main.FieldByName('CloSubId').Value := trim(cds_main.fieldbyname('CloSubId').AsString);
          self.cds_main.FieldByName('BatchNo').Value := trim(cds_main.fieldbyname('BatchNo').AsString);
          self.cds_main.FieldByName('gongyi').Value := trim(cds_main.fieldbyname('gongyi').AsString);
          self.cds_main.FieldByName('ZZType').Value := trim(cds_main.fieldbyname('ZZType').AsString);
          self.cds_main.FieldByName('Code').Value := trim(cds_main.fieldbyname('Code').AsString);
          self.cds_main.FieldByName('CodeName').Value := trim(cds_main.fieldbyname('CodeName').AsString);
          self.cds_main.FieldByName('spec').Value := trim(cds_main.fieldbyname('spec').AsString);
          self.cds_main.FieldByName('MF').Value := trim(cds_main.fieldbyname('MF').AsString);
          self.cds_main.FieldByName('KZ').Value := trim(cds_main.fieldbyname('KZ').AsString);
          self.cds_main.FieldByName('FactoryName').Value := trim(cds_main.fieldbyname('FactoryName').AsString);
          self.cds_main.FieldByName('fromFactoryName').Value := trim(cds_main.fieldbyname('fromFactoryName').AsString);
          self.cds_main.FieldByName('ps').Value := cds_main.fieldbyname('ps').Value;
          self.cds_main.FieldByName('Qty').Value := cds_main.fieldbyname('Qty').AsFloat;
          self.cds_main.FieldByName('QtyUnit').Value := trim(cds_main.fieldbyname('QtyUnit').AsString);
          self.cds_main.FieldByName('Note').Value := trim(cds_main.fieldbyname('Note').AsString);
          self.cds_main.FieldByName('KuWei').Value := trim(cds_main.fieldbyname('KuWei').AsString);
          self.cds_main.FieldByName('Price').Value := 0;
          self.cds_main.FieldByName('Money').Value := 0;
          self.cds_main.Post;
        end;
        cds_main.Next;
      end;
      cds_main.EnableControls;
    end;
    free;
  end;
end;

procedure TfrmPBCk.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(self.Caption + tv1.Name, Tv1, '面料仓库');
  close;
end;

procedure TfrmPBCk.ToFactoryNameBtnClick(Sender: TObject);
begin
  if (trim(CRType.Text) = '加工出库') or (trim(CRType.Text) = '生产出库') or (trim(CRType.Text) = '退货出库') then
  begin
    try
      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin
        if ShowModal = 1 then
        begin
          ToFactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachment := TfrmZdyAttachment.Create(Application);
      with frmZdyAttachment do
      begin
        if ShowModal = 1 then
        begin
          ToFactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
        end;
      end;
    finally
      frmZdyAttachment.Free;
    end;
  end;
end;

procedure TfrmPBCk.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBCk.FHAddressBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

procedure TfrmPBCk.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'MLType';
      flagname := '面料类型';
      if ShowModal = 1 then
      begin
        CDS_Main.Edit;
        CDS_Main.FieldByName('P_Type').Value := Trim(ClientDataSet1.fieldbyname('zdyName').AsString);
        CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBCk.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if Trim(CDS_Main.FieldByName('CRID').AsString) = '' then
  begin
    CDS_Main.Delete;
  end
  else
  begin
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update CK_PB_CR set Valid=''N''  where CRID=' + quotedstr(Trim(CDS_Main.FieldByName('CRID').AsString)));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Up_PBKc ' + quotedstr(Trim(CDS_Main.FieldByName('CRID').AsString)));
      Open;
    end;
    if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
    begin
      CDS_Main.EnableControls;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('出库数量超出库存数量!', '提示', 0);
      Exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.Delete;
  end;
end;

procedure TfrmPBCk.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmPBCk.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

end.

