unit U_BpkcCk;

interface

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

type
  TfrmBpkcCk = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1P_CodeName: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    TOk: TToolButton;
    v1P_Color: TcxGridDBColumn;
    v1ckqty: TcxGridDBColumn;
    v1price: TcxGridDBColumn;
    v1money: TcxGridDBColumn;
    Panel2: TPanel;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    P_CodeName: TEdit;
    P_spec: TEdit;
    CheckBox1: TCheckBox;
    ckName: TComboBox;
    P_Color: TEdit;
    GroupBox1: TGroupBox;
    CRTime: TDateTimePicker;
    Label8: TLabel;
    CRType: TComboBox;
    Label5: TLabel;
    Label9: TLabel;
    cust: TBtnEditA;
    ADOQuery1: TADOQuery;
    v1Ssel: TcxGridDBColumn;
    CheckBox2: TCheckBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure P_CodeNameChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TOkClick(Sender: TObject);
    procedure custBtnClick(Sender: TObject);
    procedure v1ckqtyPropertiesEditValueChanged(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetComboBox();
    function  savedate():Boolean;
    function YSData(ckNo,CkID:string;Order_Main10:TClientDataSet):Boolean;
    { Private declarations }
  public
    fType:integer;
    fCKName:string;
    fkeyNO:string;
    
    { Public declarations }
  end;

var
  frmBpkcCk: TfrmBpkcCk;

implementation
uses
  U_DataLink,U_Fun10,U_ZDYHelp, U_BpRk;
{$R *.dfm}
function TfrmBpkcCk.YSData(ckNo,CkID:string;Order_Main10:TClientDataSet):Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou:String;
begin
  Result:=False;
  with Order_Main10 do
  begin
    with ADOQuery1 do
    begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(cust.text)+'''');
        sql.Add('and YFDefFlag1=1 ');
        Open;
    end;
    if not ADOQuery1.IsEmpty then
    begin
      CRID:=ADOQuery1.fieldbyname('CRID').AsString;
    end
    else
    begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CRID set CRID=CRID+1');
          sql.Add('select * from YF_Money_CRID ');
          Open;
        end;
        CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_KC where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('FactoryName').Value:=Trim(cust.text);
          FieldByName('ZdyStr1').Value:='应收收';
          FieldByName('YFDefFlag1').Value:=1;
          Post;
        end;
    end;
    with ADOQuery1 do
    begin
      Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR ');
        sql.Add(' where MainID='''+Trim(ckNo)+'''');
        sql.Add(' and subID='''+Trim(ckID)+'''');
        Open;
    end;
      if ADOQuery1.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',3,1)=False then
        begin
          Application.MessageBox('取坯布应付最大号失败!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value:=Trim(YFID);
          FieldByName('YFTypeId').Value:=Trim(ckNo);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(cust.Text);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',date()));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Qty').Value:=Order_Main10.fieldbyname('ckQty').AsFloat;
          FieldByName('Price').Value:=Order_Main10.fieldbyname('Price').AsFloat;
          FieldByName('Money').Value:=Order_Main10.fieldbyname('Money').AsFloat;
          FieldByName('BBMoney').Value:=Order_Main10.fieldbyname('Money').AsFloat;
          //FieldByName('HuiLv').Value:=1;
          FieldByName('HuiLv').Value:=1;
          FieldByName('BZType').Value:='人民币';
          FieldByName('QtyUnit').Value:=Trim(Order_Main10.fieldbyname('QtyUnit').AsString);
          FieldByName('ComTaiTou').Value:=Trim(cust.Text);
          FieldByName('YFName').Value:='销售金额';
          FieldByName('MainId').Value:=Trim(ckNo);
          FieldByName('subID').Value:=Trim(ckID);
          FieldByName('YFDefFlag1').Value:=1;
          Post;
        end;
      end
      else
      begin
        YFID:=Trim(ADOQueryTmp.fieldbyname('YFID').AsString);
      end;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(' where CRID='+CRID);
        ExecSQL;
      end;
  end;
  Result:=True;
end;
function TfrmBpkcCk.savedate():Boolean;
var
  fkcID,i:integer;
  Filler:string;
  FillTime:TdateTime;
  maxno:string;
begin
  result:=false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,fkeyNO,'CK','BP_InOut',3,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
      Filler:=DName;
      FillTime:=SGetServerDateTime(ADOQuery1);


    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from BP_InOut');
      sql.Add('where CRNO='+quotedstr(trim(fkeyNO)));
      execsql;
    end;
    CDS_Main.DisableControls;
    CDS_Main.First;
    i:=1;
    with  CDS_Main do
    begin
      while not eof do
      begin
        if Fieldbyname('ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from BP_InOut');
            sql.Add('where 1=2 ');
            open;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('CRNO').Value:=trim(fkeyNO);
          ADOQueryCmd.FieldByName('CRID').Value:=i;
          ADOQueryCmd.FieldByName('CKName').Value:=trim(fckName);
       //   ADOQueryCmd.FieldByName('mainID').Value:=trim(fmainID);
       //   ADOQueryCmd.FieldByName('subID').Value:=trim(fsubID);
          ADOQueryCmd.FieldByName('CRTime').Value:=formatdateTime('yyyy-MM-dd',CRTime.Date);
          ADOQueryCmd.FieldByName('CRType').Value:=trim(crType.Text);
          ADOQueryCmd.FieldByName('CRFlag').Value:='出库';
       //   ADOQueryCmd.FieldByName('MJID').Value:=trim(fieldbyname('MJID').AsString);
       //   ADOQueryCmd.FieldByName('MJXH').Value:=trim(fieldbyname('MJXH').AsString);
      //    ADOQueryCmd.FieldByName('CPTYPE').Value:=trim(fieldbyname('CPTYPE').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_Color').Value:=trim(fieldbyname('P_Color').AsString);
      //    ADOQueryCmd.FieldByName('P_MF').Value:=fieldbyname('P_MF').AsFloat;
      //    ADOQueryCmd.FieldByName('P_KZ').Value:=fieldbyname('P_KZ').AsFloat;
          ADOQueryCmd.FieldByName('RollNum').Value:=0;
          ADOQueryCmd.FieldByName('Qty').Value:=fieldbyname('ckQty').AsFloat;
          ADOQueryCmd.FieldByName('Price').Value:=fieldbyname('Price').AsFloat;
          ADOQueryCmd.FieldByName('money').Value:=fieldbyname('money').AsFloat;
          ADOQueryCmd.FieldByName('QtyUnit').Value:=trim(fieldbyname('QtyUnit').AsString);
      //    ADOQueryCmd.FieldByName('note').Value:=trim(fieldbyname('note').AsString);
          ADOQueryCmd.FieldByName('Filler').Value:=trim(Filler);
          ADOQueryCmd.FieldByName('FillTime').Value:=dateTimetostr(FillTime);
          ADOQueryCmd.FieldByName('cust').Value:=trim(cust.Text);
   //     ADOQueryCmd.FieldByName('FirstNo').Value:=trim(FirstName.TxtCode);
   //     ADOQueryCmd.FieldByName('FirstName').Value:=trim(FirstName.Text);
          if FType=1 then
          begin
            ADOQueryCmd.FieldByName('editer').Value:=trim(dName);
            ADOQueryCmd.FieldByName('editTime').Value:=SGetServerDateTime(ADOQuery1);
          end;
          ADOQueryCmd.Post;

          if not YSData(fkeyNo,inttostr(i),CDS_Main) then
          begin
            CDS_Main.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            exit;
          end;
          i:=i+1;
        end;
        next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result:=true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;
procedure TfrmBpkcCk.SetComboBox();
begin
  ckName.Items.Clear;
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''BPCK'' ');
    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;
 // CRType.Items.Add('');
  with adoQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select * from KH_ZDY where Type=''BPCKTYPE''');
    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;
procedure TfrmBpkcCk.InitGrid();
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select AA.*,QtyUnit=''M''  ');
      sql.Add('FROM( select A.ckName,P_CodeName,A.P_Color,ckQty=0.00,Price=0.00,money=0.00, ');
      sql.Add('   SUM(case when CRFlag=''入库'' then Qty else -1 * Qty end ) as Qty ');
      sql.Add('from BP_InOut A                                                ');
      sql.Add('group by A.ckName,P_CodeName,A.P_Color)AA               ');
      if trim(FckName)<>'' then
      sql.Add('where ckName='+quotedstr(trim(FckName)) );
      if not checkbox1.Checked then
      sql.Add('and qty<>0  ');
      open;
    end;
    SCreateCDS20(adoqueryTmp,CDS_Main);
    SInitCDSData20(adoqueryTmp,CDS_Main);
  finally
  end;
end;

procedure TfrmBpkcCk.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  writeCxGrid(self.Caption,Tv1,'布匹仓库');
  if fType=10 then Action:=cahide
  else Action:=cafree;
end;

procedure TfrmBpkcCk.FormDestroy(Sender: TObject);
begin
  frmBpkcCk:=nil;
end;

procedure TfrmBpkcCk.P_CodeNameChange(Sender: TObject);
begin
  if ADOQueryTmp.Active=False then Exit;
  SDofilter(ADOQueryTmp,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryTmp,CDS_Main);
  SInitCDSData20(ADOQueryTmp,CDS_Main);
end;

procedure TfrmBpkcCk.FormShow(Sender: TObject);
begin
  if fType=10 then
  begin
    TOk.Visible:=true;
    v1Ssel.Visible:=true;
    v1ckqty.Visible:=true;
    v1price.Visible:=true;
    v1money.Visible:=true;
    GroupBox1.Visible:=true;
  end
  else
  begin
  //  TOk.Visible:=false;
  //  v1Ssel.Visible:=false;
    v1ckqty.Visible:=false;
    v1price.Visible:=false;
    v1money.Visible:=false;
    GroupBox1.Visible:=false;
  end;
  readCxGrid(self.Caption,Tv1,'布匹仓库');
  SetComboBox();
  InitGrid();
end;

procedure TfrmBpkcCk.FormCreate(Sender: TObject);
begin
  BegDate.Date:=DServerDate-7;
  EndDate.Date:=DServerDate;
  CRTime.date:= DServerDate;
  cxGrid1.Align:=alclient;

end;

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

procedure TfrmBpkcCk.TBRafreshClick(Sender: TObject);
begin

  initGrid();
end;

procedure TfrmBpkcCk.TBPrintClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid1);
end;

procedure TfrmBpkcCk.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  TcxGridToExcel(self.Caption,cxgrid1);
end;

procedure TfrmBpkcCk.TOkClick(Sender: TObject);
begin
  frmBpkcCk.ModalResult:=1;
{  CRTIME.SetFocus;
  if not CDS_Main.Locate('SSel',True,[]) then
  begin
    Application.MessageBox('没有选择出库数据!','提示',0);
    Exit;
  end;
  if  trim(CRType.Text)='' then
  begin
    application.MessageBox('出库类型不能为空！','提示信息',0);
    exit;
  end;
  if  trim(cust.Text)=''then
  begin
    application.MessageBox('客户不能为空！','提示信息',0);
    exit;
  end;
  if savedate() then
  begin
    application.MessageBox('数据保存成功！','提示信息');
    InitGrid();
    fkeyNo:='';
  //  ModalResult:=1;
  end
  else
  begin
    application.MessageBox('数据保存失败！','提示信息',MB_ICONERROR);
  end;   }
end;

procedure TfrmBpkcCk.custBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GDCustFlag';
      flagname:='客户名称';
      if ShowModal=1 then
      begin
        cust.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmBpkcCk.v1ckqtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue,fieldname,qty1,qty2:String;
  num1:double;

begin
  fieldname:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  with CDS_Main do
  begin
    Edit;
    FieldByName(Trim(fieldname)).Value:=TcxTextEdit(Sender).EditingText;
    num1:=FieldByName('ckQty').AsFloat * FieldByName('Price').AsFloat;
    FieldByName('Money').Value:=strtofloat(format('%8.2f',[num1]));
    Post;
  end;
  Tv1.Controller.EditingController.ShowEdit();

end;

procedure TfrmBpkcCk.CheckBox1Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmBpkcCk.CheckBox2Click(Sender: TObject);
begin
  IF  CDS_Main.IsEmpty then exit;
  with CDS_Main do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      edit;
      fieldbyname('ssel').Value:=checkbox2.Checked;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

end.
