unit U_JYOrderCDOne_LR;

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, cxCheckBox, cxCalendar;

type
  TfrmJYOrderCDOne_LR = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOZDY: TADOQuery;
    CDS_ZDY: TClientDataSet;
    ADOQuery1: TADOQuery;
    TADD: TToolButton;
    TDel: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column1: TcxGridDBColumn;
    v2PRTColor: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1KGQty: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1CRType: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure TADDClick(Sender: TObject);
    procedure TDelClick(Sender: TObject);
    procedure v2PRTColorPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1CRTypePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    FXS:Integer;
    procedure InitData();
    procedure ZDYHelp(FButn:TcxButtonEdit;LType:string);
    function  SaveData():Boolean;
    { Private declarations }
  public
    PState,PCopyInt:Integer;
    FCRID,FConNo:String;

    { Public declarations }
  end;

var
  frmJYOrderCDOne_LR: TfrmJYOrderCDOne_LR;

implementation
uses
  U_DataLink,U_ZDYHelp,U_Fun,U_ProductOrderListSel;

{$R *.dfm}

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

procedure TfrmJYOrderCDOne_LR.InitData();
begin
    with ADOQuery1 do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.* ');
      sql.add('from CK_BanCP_CR A ');
      sql.add('where 1=2');
      Open;
    end;
    SCreateCDS20(ADOQuery1,Order_Sub);
    SInitCDSData20(ADOQuery1,Order_Sub);
end;

procedure TfrmJYOrderCDOne_LR.ZDYHelp(FButn:TcxButtonEdit;LType:string);
begin

end;

procedure TfrmJYOrderCDOne_LR.FormShow(Sender: TObject);
begin

  InitData();

end;

function TfrmJYOrderCDOne_LR.SaveData():Boolean;
var
  maxno,maxSubNo,FInt:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存子表
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('BCID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,maxno,'CR','CK_BanCP_CR',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('生成流水号异常！','提示',0);
            exit;
          end;
        end else
        begin
          maxno:=Trim(FCRID);
        end;
        if Trim(Order_Sub.fieldbyname('BCID').AsString)='' then
        begin
          if GetLSNo(ADOCmd,FInt,'3','CK_BanCP_CR',3,1)=False then
          begin
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('生成流水号异常！','提示',0);
            exit;
          end;
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from CK_BanCP_CR where BCID='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(Order_Sub.fieldbyname('BCID').AsString)='' then
          begin
            ADOCmd.Append;
            fieldbyname('CRID').AsFloat:=strtofloat(FInt);
          end
          else
            ADOCmd.Edit;
          FieldByName('BCID').Value:=Trim(maxno);
          FieldByName('MJID').Value:=Trim(maxno);
          FieldByName('APID').Value:=Trim(maxno);
          SSetSaveDataCDSNew(ADOCmd,Tv1,Order_Sub,'CK_BanCP_CR',0);
          FieldByName('KGQty').Value:=Order_Sub.fieldbyname('KGQty').AsFloat;
          FieldByName('BCFK').Value:=Order_Sub.fieldbyname('BCFK').AsFloat;
          FieldByName('BCKZ').Value:=Order_Sub.fieldbyname('BCKZ').AsFloat;
          FieldByName('Qty').Value:=Order_Sub.fieldbyname('Qty').AsFloat;
          fieldbyname('CRFlag').Value:='入库';
          ADOCmd.Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('BCID').Value:=Trim(maxno);
        Next;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmJYOrderCDOne_LR.TBSaveClick(Sender: TObject);
begin
  
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！','提示',0);
    exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    ModalResult:=1;
  end;

end;

procedure TfrmJYOrderCDOne_LR.v1Column1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  frmProductOrderListSel:=TfrmProductOrderListSel.Create(self);
  with frmProductOrderListSel do
  begin
    if showmodal=1 then
    begin
      with Order_Sub do
      begin
        edit;
        fieldbyname('orderNo').AsString:=trim(Order_Main.fieldbyname('OrderNo').asstring);
        fieldbyname('MPRTCodeName').AsString:=trim(Order_Main.fieldbyname('MPRTCodeName').asstring);
        fieldbyname('PRTColor').AsString:=trim(Order_Main.fieldbyname('PRTColor').asstring);
        fieldbyname('PRTColorNo').AsString:=trim(Order_Main.fieldbyname('PRTColorNo').asstring);
        fieldbyname('PRTHX').AsString:=trim(Order_Main.fieldbyname('PRTHX').asstring);
        fieldbyname('Mainid').AsString:=trim(Order_Main.fieldbyname('Mainid').asstring);
        fieldbyname('Subid').AsString:=trim(Order_Main.fieldbyname('Subid').asstring);
        post;
      end;
    end;
    free;
  end;
end;

procedure TfrmJYOrderCDOne_LR.TADDClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty=False then
  begin
    CopyAddRowCDS(Order_Sub);
    with Order_Sub do
    begin
      Edit;
      FieldByName('CRID').Value:=null;
      post;
    end;
  end else
  begin
    with Order_Sub do
    begin
      Append;
      fieldbyname('filltime').AsDateTime:=now;
      fieldbyname('CRTime').AsDateTime:=now;
      Post;
    end;
  end;
end;

procedure TfrmJYOrderCDOne_LR.TDelClick(Sender: TObject);
begin
  if Order_Sub.IsEmpty then Exit;
  Order_Sub.Delete;
end;

procedure TfrmJYOrderCDOne_LR.v2PRTColorPropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
   try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrdColor';
      flagname:='颜色';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTColor').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJYOrderCDOne_LR.v2Column1PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='MPRTCodeName';
      flagname:='中文名称';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('MPRTCodeName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJYOrderCDOne_LR.v1Column11PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PRTHX';
      flagname:='花型花号';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('PRTHX').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJYOrderCDOne_LR.v1CRTypePropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CRType';
      flagname:='入库类型';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('CRType').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJYOrderCDOne_LR.v1Column7PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='QtyUnit';
      flagname:='长度单位';
      if ShowModal=1 then
      begin
        Self.Order_Sub.Edit;
        Self.Order_Sub.FieldByName('QtyUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
