unit U_CKSMEdit_other;

interface

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

type
  TfrmCKSMEdit_other = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    PHYG: TBtnEditA;
    defstr2: TComboBox;
    Label16: TLabel;
    Label15: TLabel;
    packNo: TcxCurrencyEdit;
    Label14: TLabel;
    Label1: TLabel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1MJID: TcxGridDBColumn;
    v1MJXH: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1P_Color: TcxGridDBColumn;
    v1RollNum: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDSOrder: TClientDataSet;
    CDSMJID: TClientDataSet;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    SmNO: TEdit;
    ADOQueryTmp: TADOQuery;
    MovePanel1: TMovePanel;
    Label2: TLabel;
    Edit2: TEdit;
    Button4: TButton;
    V2Column1: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);

    procedure FormCreate(Sender: TObject);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure TBSaveClick(Sender: TObject);
    procedure PHYGBtnClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitOrder();
    procedure InitMJID();
    function  SavePH():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCKSMEdit_other: TfrmCKSMEdit_other;

implementation
uses
  U_DataLink,U_Fun, U_UserHelp, U_ZDYHelp;
{$R *.dfm}
function TfrmCKSMEdit_other.SavePH():Boolean;
var
  phID:string;
  P_ID:integer;
begin
  Result:=false;
  ADOQueryCmd.Connection.BeginTrans;
  P_ID:=1;
  try
    with CDSMJID do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        if trim(CDSMJID.fieldbyname('SDefNote').AsString)='扫描成功' then
        begin
        if GetLSNo(ADOQueryTmp,phID,'PT','CK_BanCP_PH1',4,1)=False then
        begin
          Application.MessageBox('取配货单号失败！','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_PH1 ');
          sql.Add('where 1=2 ');
          open;
        end;
        ADOQueryCmd.Append;
        ADOQueryCmd.FieldByName('phID').Value:=trim(phID);
        ADOQueryCmd.FieldByName('p_No').Value:=trim(fieldbyname('p_No').AsString);
        ADOQueryCmd.FieldByName('p_ID').Value:=P_ID;
//        ADOQueryCmd.FieldByName('conNo').Value:=trim(fieldbyname('conNo').AsString);
        ADOQueryCmd.FieldByName('MainID').Value:=trim(fieldbyname('MainID').AsString);
        ADOQueryCmd.FieldByName('SubID').Value:=trim(fieldbyname('SubID').AsString);
        ADOQueryCmd.FieldByName('PMainID').Value:=trim(fieldbyname('PMainID').AsString);
        ADOQueryCmd.FieldByName('PSubID').Value:=trim(fieldbyname('PSubID').AsString);
        ADOQueryCmd.FieldByName('mjID').Value:=trim(fieldbyname('mjID').AsString);
        ADOQueryCmd.FieldByName('CRID').Value:=trim(fieldbyname('CRID').AsString);
        ADOQueryCmd.FieldByName('qtyunit').Value:=trim(fieldbyname('qtyunit').AsString);
        ADOQueryCmd.FieldByName('defstr2').Value:=trim(defstr2.Text);
        ADOQueryCmd.FieldByName('packNo').Value:=trim(packNO.Text);
        ADOQueryCmd.FieldByName('qty').Value:=fieldbyname('qty').AsFloat;
        ADOQueryCmd.FieldByName('Rollnum').Value:=fieldbyname('Rollnum').AsFloat;
        ADOQueryCmd.FieldByName('Filler').Value:=trim(DName);
        ADOQueryCmd.FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTmp);
        ADOQueryCmd.FieldByName('BCGangNo').Value:=trim(fieldbyname('BCgangNO').AsString);
        ADOQueryCmd.FieldByName('PHYG').Value:=trim(PHYG.Text);
        ADOQueryCmd.Post;

        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CK_BanCP_KC1 ');
          sql.Add('where mjid='+quotedstr(trim(CDSMJID.fieldbyname('mjid').AsString)));
          sql.Add('and CRID='+quotedstr(trim(CDSMJID.fieldbyname('CRID').AsString)));
          open;
          edit;
          fieldbyname('kcstatus').Value:='1';
          post;
        end;
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from JYOrder_sub ');
          sql.Add('where mainID='+quotedstr(trim(CDSMJID.fieldbyname('mainID').AsString)));
          sql.Add('and subID='+quotedstr(trim(CDSMJID.fieldbyname('subID').AsString)));
          open;
          edit;
          fieldbyname('substatus').Value:='2';
          post;
        end;
        P_ID:=P_ID+1;
        end;
        next;
      end;
      first;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmCKSMEdit_other.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.CustomerNoName,PHRollNum=0.00,PHQty=0.00 from JYOrder_sub A');
    SQL.Add('inner join JYOrder_main B on B.mainID=A.mainID');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDSOrder);
  SInitCDSData20(ADOQueryMain,CDSOrder);

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.PmainID,PSubID,P_NO from CK_BanCP_CR A ');
    SQL.Add('inner join CK_BanCP_PH B on B.PHID=A.PHID  ');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDSMJID);
  SInitCDSData20(ADOQueryMain,CDSMJID);


end;
procedure TfrmCKSMEdit_other.InitOrder();
var
  fP_No:string;
begin
  MovePanel1.Visible:=false;
  CDSMJID.EmptyDataSet;
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.CustomerNoName,PHRollNum=0.00,PHQty=0.00,P_No=A.SOrdDefNote2 from JYOrder_sub A');
    SQL.Add('inner join JYOrder_main B on B.mainID=A.mainID');
    SQL.Add('where A.mainID='+quotedstr(trim(SmNO.text)));
   // SQL.Add('and syrName=''湖州门市部''');
    Open;
  end;
  IF ADOQueryMain.IsEmpty then
  begin
    MovePanel1.Visible:=true;
    Edit2.text:=SmNO.Text;
    Label2.Caption:='订单号不存在！';
    exit;
  end
  else
  begin
  SCreateCDS20(ADOQueryMain,CDSOrder);
  SInitCDSData20(ADOQueryMain,CDSOrder);
  with CDSOrder do
  begin
    DisableControls;
    first;
    while not eof do
    begin
      if GetLSNo(ADOQueryTmp,fP_No,'PNT','CK_BanCP_PH1',4,1) then
      begin
        edit;
        fieldbyname('P_No').Value:=trim(fP_No);
        post;
      end
      else
      begin
        MovePanel1.Visible:=true;
        Edit2.Enabled:=false;
        Label2.Caption:='生成编号错误！';
        EnableControls;
        exit;
      end;
      next;
    end;
    first;
    EnableControls;
  end;
  end;

end;
procedure TfrmCKSMEdit_other.InitMJID();
var
  fP_No:string;
begin
  MovePanel1.Visible:=false;
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*,B.mainID,B.subID from CK_BanCP_KC1 A');
    sql.add('left join  CK_BanCP_CR1 B on B.BCID=A.BCID and B.CRFlag=''入库'' ');
    SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
    sql.Add('and isnull(A.KCStatus,''0'')=''0''  ');
    sql.Add('and KCQty>0  ');
    Open;
    if not IsEmpty  then
    begin
      IF Fieldbyname('KCQty').AsFloat<=0 then
      begin
        CDSMJID.Append;
        CDSMJID.FieldByName('SDefNote').Value:='此条码已出库';
        CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
        CDSMJID.Post;
      end
      else
      begin
        with ADOQueryTmp do
        begin
          Close;
          sql.Clear;
          SQL.Add('select B.mainID,B.subID from CK_BanCP_KC1 A');
          sql.Add('inner join JYorder_Sub B on B.PRTCodeName=A.C_CodeName and B.PRtColor=A.C_Color');
          SQL.Add('where A.MJID='+quotedstr(trim(SmNO.text)));
          SQL.Add('and B.mainID='+quotedstr(trim(CDSOrder.Fieldbyname('mainID').AsString)));
          Open;
          IF ADOQueryTmp.IsEmpty then
          begin
            CDSMJID.Append;
            CDSMJID.FieldByName('SDefNote').Value:='品名和颜色不对应';
            CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
            CDSMJID.Post;
          end
          else
          begin
            if CDSOrder.Locate('subID',trim(ADOQueryTmp.fieldbyname('subID').AsString),[]) then
            begin
              fP_No:=CDSOrder.fieldbyname('P_NO').AsString;
            end;
            CDSMJID.Append;
            CDSMJID.FieldByName('SDefNote').Value:='扫描成功';
            CDSMJID.FieldByName('MJID').Value:=trim(ADOQueryMain.Fieldbyname('MJID').AsString);
            CDSMJID.FieldByName('CRID').Value:=trim(ADOQueryMain.Fieldbyname('CRID').AsString);
            CDSMJID.FieldByName('C_CodeName').Value:=trim(ADOQueryMain.Fieldbyname('C_CodeName').AsString);
            CDSMJID.FieldByName('C_Color').Value:=trim(ADOQueryMain.Fieldbyname('C_Color').AsString);
            CDSMJID.FieldByName('CPType').Value:=trim(ADOQueryMain.Fieldbyname('CPType').AsString);
            CDSMJID.FieldByName('RollNum').Value:=ADOQueryMain.Fieldbyname('kcRollNum').AsFloat;
            CDSMJID.FieldByName('Qty').Value:=ADOQueryMain.Fieldbyname('kcQty').AsFloat;
            CDSMJID.FieldByName('QtyUnit').Value:=trim(ADOQueryMain.Fieldbyname('kcQtyUnit').AsString);
            CDSMJID.FieldByName('mainID').Value:=trim(ADOQueryTmp.Fieldbyname('mainID').AsString);
            CDSMJID.FieldByName('subID').Value:=trim(ADOQueryTmp.Fieldbyname('subID').AsString);
            CDSMJID.FieldByName('P_NO').Value:=trim(fP_No);
            CDSMJID.FieldByName('PmainID').Value:=trim(ADOQueryMain.Fieldbyname('mainID').AsString);
            CDSMJID.FieldByName('PsubID').Value:=trim(ADOQueryMain.Fieldbyname('subID').AsString);
            CDSMJID.FieldByName('BCgangNO').Value:=trim(ADOQueryMain.Fieldbyname('BCgangNO').AsString);
          //  CDSMJID.FieldByName('conNo').Value:=trim(ADOQueryMain.Fieldbyname('conNo').AsString);
            CDSMJID.Post;
          end;
        end;
      end;
    end
    else
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value:='此条码不存在或已经出库';
      CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
      CDSMJID.Post;
    end;
  end;
  IF trim(CDSMJID.FieldByName('SDefNote').AsString)<>'扫描成功' then
  begin
    MovePanel1.Visible:=true;
    Edit2.Text:=SmNO.Text;
    Label2.Caption:=trim(CDSMJID.FieldByName('SDefNote').AsString);
  end;
end;

procedure TfrmCKSMEdit_other.FormDestroy(Sender: TObject);
begin
  wRITECxGrid(self.Caption+tV1.Name,Tv1,'成品仓库');
  wRITECxGrid(self.Caption+TV2.Name,Tv2,'成品仓库');
  frmCKSMEdit_other:=nil;
end;

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

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

procedure TfrmCKSMEdit_other.FormCreate(Sender: TObject);
begin
  Panel2.Align:=alClient;
end;

procedure TfrmCKSMEdit_other.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  IF key=#13 then
  begin
    MovePanel1.Visible:=false;
    if trim(smno.Text)='' then exit;
    IF uppercase(leftBstr(trim(smno.Text),2))='JM' then
    begin
      inItorder();
      
    end
    else
    begin
      IF CDSOrder.IsEmpty then
      begin
        MovePanel1.Visible:=true;
        Edit2.Text:=SmNO.Text;
        Label2.Caption:='订单不能为空！';
        smno.Text:='';
        exit;
      end;
      if not CDSMJID.Locate('MJID',trim(smNo.Text),[]) then
      initMjid();
    end;
    smno.Text:='';
    smno.SetFocus;
  end;
end;

procedure TfrmCKSMEdit_other.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+tV1.Name,Tv1,'成品仓库');
  ReadCxGrid(self.Caption+TV2.Name,Tv2,'成品仓库');
  MovePanel1.Left:=(Width-MovePanel1.Width) div 2;
  MovePanel1.top:=(Height-MovePanel1.Height-200) div 2;
  InitGrid();
end;

procedure TfrmCKSMEdit_other.TV2CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
var
  i:integer;
begin
  i:=tv2.GetColumnByFieldName('SDefNote').Index;
  if (AViewInfo.GridRecord.Values[i]<>'扫描成功')  then
  ACanvas.Brush.Color:=clred;
end;

procedure TfrmCKSMEdit_other.TBSaveClick(Sender: TObject);
begin
  IF CDSOrder.IsEmpty then exit;
  if CDSMJID.IsEmpty then exit;
  IF (trim(CDSOrder.FieldByName('CustomerNoName').AsString)='广东门市部') or (trim(CDSOrder.FieldByName('CustomerNoName').AsString)='湖州门市部') then
  begin
    IF trim(packNo.Text)='' then
    begin
      application.MessageBox('包号不能为空！','提示信息',0);
      exit;
    end;
    IF trim(defstr2.Text)='' then
    begin
      application.MessageBox('货运类型不能为空！','提示信息',0);
      exit;
    end;
  end;
  IF trim(CDSOrder.FieldByName('CustomerNoName').AsString)<>'广东门市部'then
  begin
    IF tv1.DataController.Summary.FooterSummaryValues[0]<>0 then
    begin
      IF tv1.DataController.Summary.FooterSummaryValues[0]<> tv2.DataController.Summary.FooterSummaryValues[0] then
      begin
        application.MessageBox('配货匹数不相同，不能保存数据！','提示信息',MB_ICONERROR);
        exit;
      end;
    end;
    IF tv1.DataController.Summary.FooterSummaryValues[1]<>0 then
    begin
      IF tv1.DataController.Summary.FooterSummaryValues[1]<> tv2.DataController.Summary.FooterSummaryValues[1] then
      begin
        if Application.MessageBox('配货数量不相同，是否保存？','提示',32+4)<>IDYES then Exit;
    //  application.MessageBox('配货数量不相同，不能保存数据！','提示信息',MB_ICONERROR);
    //  exit;
      end;
    end;
  end;
  try
  //  ADOQueryCmd.Connection.BeginTrans;
    IF not savePh() then
    begin
      application.MessageBox('配货保存失败！','提示信息',MB_ICONERROR);
      exit;
    end
    else
    begin
      application.MessageBox('配货保存成功！','提示信息');
      initGrid();
      exit;
    end;
  //  ADOQueryCmd.Connection.CommitTrans;
  except
   // ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmCKSMEdit_other.PHYGBtnClick(Sender: TObject);
begin
{  frmUserHelp:=TfrmUserHelp.Create(self);
  with frmUserHelp do
  begin
    if showmodal=1 then
    begin
      PHYG.Text:=trim(ADOQueryHelp.fieldbyname('userName').AsString);
    end;
    free;
  end;  }
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PHYGFLAG';
      flagname:='配货员工';
      if ShowModal=1 then
      begin
        PHYG.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCKSMEdit_other.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=false;  
end;

end.
