unit U_CpCkSaoMNew;

interface

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

type
  TfrmCpCkSaoMNew = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    DataSource1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel1: TPanel;
    BaoID: TEdit;
    Label1: TLabel;
    v1Column5: TcxGridDBColumn;
    Button2: TButton;
    Button3: TButton;
    CDS_Sub: TClientDataSet;
    DataSource2: TDataSource;
    ADOQuerySub: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    v3Column1: TcxGridDBColumn;
    Button1: TButton;
    cxGridPopupMenu4: TcxGridPopupMenu;
    MovePanel1: TMovePanel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    Button4: TButton;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1OrderNo: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1PRTMF: TcxGridDBColumn;
    v1PRTKZ: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column2: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BaoIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    procedure InitSubGrid();
  public
    { Public declarations }
  end;

var
  frmCpCkSaoMNew: TfrmCpCkSaoMNew;

implementation
uses
U_DataLink,U_Fun,U_OrderSel ;

{$R *.dfm}

procedure TfrmCpCkSaoMNew.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;

procedure TfrmCpCkSaoMNew.FormDestroy(Sender: TObject);
begin
  frmCpCkSaoMNew:=nil;
end;
procedure TfrmCpCkSaoMNew.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) from CK_BanCP_CR A');
      sql.add('where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) from CK_BanCP_CR A');
      sql.add('where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_MainSel);
    SInitCDSData20(ADOQueryMain,CDS_MainSel);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCpCkSaoMNew.FormShow(Sender: TObject);
begin
  ReadCxGrid('订单选择',Tv2,'成品仓库');
  ReadCxGrid('成品出库',Tv1,'成品仓库');
  ReadCxGrid('成品出库Sels',Tv3,'成品仓库');
  InitSubGrid();
  InitGrid(); 
end;

procedure TfrmCpCkSaoMNew.BaoIDKeyPress(Sender: TObject; var Key: Char);
var
  maxno:String;
begin
  if Key=#13 then
  begin
    if CDS_Sub.IsEmpty then
    begin
      BaoID.Text:='';
      Application.MessageBox('未选单不能扫描出库！','提示',0);
      Exit;
    end;
    if CDS_Main.Locate('MJId',Trim(BaoID.Text),[])=False then
    begin
      BaoID.Text:='';
      Application.MessageBox('此卷不包含在待出库的卷数据中！','提示',0);
      Exit;
    end;
    CDS_Main.Locate('MJId',Trim(BaoID.Text),[]);
    try
      ADOQueryCmd.Connection.BeginTrans;
      with CDS_Main do
      begin
        if GetLSNo(ADOQueryCmd,maxno,'CC','CK_BanCp_CR',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取出库最大号失败！','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCp_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('BCID').Value:=Trim(maxno);
          FieldByName('CRID').Value:=CDS_Main.fieldbyname('CRID').Value;
          FieldByName('CRTime').Value:=SGetServerDateTime(ADOQueryTemp);
          FieldByName('KGQty').Value:=CDS_Main.fieldbyname('KGQty').Value;
          FieldByName('Qty').Value:=CDS_Main.fieldbyname('Qty').Value;
          FieldByName('QtyUnit').Value:=CDS_Main.fieldbyname('QtyUnit').Value;
          FieldByName('MJID').Value:=CDS_Main.fieldbyname('MJID').Value;
          FieldByName('MainID').Value:=CDS_Main.fieldbyname('MainID').Value;
          FieldByName('SubID').Value:=CDS_Main.fieldbyname('SubID').Value;
          FieldByName('APID').Value:=CDS_Main.fieldbyname('APID').Value;
          FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRFlag').Value:='出库';
          FieldByName('CRType').Value:='正常出库';
          //FieldByName('JZXNo').Value:=Trim(JZXNo.Text);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_BanCp_KC set KCKgQty=0,KCQty=0 where CRID='+CDS_Main.fieldbyname('CRID').AsString);
          ExecSQL;
        end;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      with CDS_MainSel do
      begin
        Append;
        FieldByName('MainId').Value:=CDS_Main.fieldbyname('MainId').Value;
        FieldByName('SubId').Value:=Self.CDS_Main.fieldbyname('SubId').Value;
        FieldByName('OrderNo').Value:=Self.CDS_Main.fieldbyname('OrderNo').Value;
        FieldByName('KGQty').Value:=Self.CDS_Main.fieldbyname('KGQty').Value;
        FieldByName('Qty').Value:=Self.CDS_Main.fieldbyname('Qty').Value;
        FieldByName('QtyUnit').Value:=Self.CDS_Main.fieldbyname('QtyUnit').Value;
        FieldByName('MJId').Value:=Self.CDS_Main.fieldbyname('MJId').Value;
        FieldByName('CRId').Value:=Self.CDS_Main.fieldbyname('CRId').Value;
        FieldByName('APID').Value:=Self.CDS_Main.fieldbyname('APID').Value;
        FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
        FieldByName('BCID').Value:=Trim(maxno);
        //FieldByName('JZXNo').Value:=Trim(JZXNo.Text);
        Post;
      end;
      CDS_Main.Delete;
      MovePanel1.Visible:=True;
      if CDS_MainSel.IsEmpty=False then
        Edit1.Text:=IntToStr(Tv3.DataController.Summary.FooterSummaryValues[2])
      else
        Edit1.Text:='0';
      Edit2.Text:=Trim(BaoID.Text);
      BaoID.Text:='';
      Exit;
    except
      BaoID.Text:='';
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('出库异常！','提示',0);
    end;
  end;
end;

procedure TfrmCpCkSaoMNew.Button2Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('订单选择',Tv2,'成品仓库');
  WriteCxGrid('成品出库',Tv1,'成品仓库');
  WriteCxGrid('成品出库Sels',Tv3,'成品仓库');
end;

procedure TfrmCpCkSaoMNew.Button3Click(Sender: TObject);
begin
  {if CDS_Main.IsEmpty=False then
  begin
    Application.MessageBox('已扫描不能更改单号！','提示',0);
    Exit;
  end;}
  try
    frmOrderSel:=TfrmOrderSel.Create(Application);
    with frmOrderSel do
    begin
      if ShowModal=1 then
      begin
        CDS_OrderSel.DisableControls;
        with CDS_OrderSel do
        begin
          First;
          while not Eof do
          begin
            if FieldByName('SSel').Value=True then
            begin
              if Self.CDS_Sub.Locate('SubId',Trim(CDS_OrderSel.fieldbyname('SubId').AsString),[])=False then
              begin
                with Self.CDS_Sub do
                begin
                  Append;
                  FieldByName('MainId').Value:=Trim(CDS_OrderSel.fieldbyname('MainId').AsString);
                  FieldByName('SubId').Value:=Trim(CDS_OrderSel.fieldbyname('SubId').AsString);
                  FieldByName('CustomerNo').Value:=Trim(CDS_OrderSel.fieldbyname('CustomerNo').AsString);
                  FieldByName('OrderNo').Value:=Trim(CDS_OrderSel.fieldbyname('OrderNo').AsString);
                  FieldByName('CustomerNoName').Value:=Trim(CDS_OrderSel.fieldbyname('CustomerNoName').AsString);
                  FieldByName('MPRTCodeName').Value:=Trim(CDS_OrderSel.fieldbyname('MPRTCodeName').AsString);
                  FieldByName('PRTOrderQty').Value:=Trim(CDS_OrderSel.fieldbyname('PRTOrderQty').AsString);
                  FieldByName('OrderUnit').Value:=Trim(CDS_OrderSel.fieldbyname('OrderUnit').AsString);
                  FieldByName('PRTColor').Value:=Trim(CDS_OrderSel.fieldbyname('PRTColor').AsString);
                  FieldByName('MPRTMF').Value:=Trim(CDS_OrderSel.fieldbyname('MPRTMF').AsString);
                  FieldByName('MPRTKZ').Value:=Trim(CDS_OrderSel.fieldbyname('MPRTKZ').AsString);
                  Post;
                end;
              end;
            end;
            Next;
          end;
        end;
        CDS_OrderSel.EnableControls;
        CDS_Sub.DisableControls;
        with CDS_Sub do
        begin
          First;
          while not Eof do
          begin
            if Trim(Self.CDS_Sub.fieldbyname('SFlag').AsString)<>'2' then
            begin
              with Self.ADOQueryTemp do
              begin
                Close;
                SQL.Clear;
                sql.Add('select orderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId), A.*,B.KCQty,B.KCKgQty ');
                sql.Add(' from CK_BanCP_CR A inner join CK_BanCP_KC B on A.CRID=B.CRID');
                sql.Add(' where B.KCqty>0 and A.CRType=''检验入库'' ');
                SQL.Add(' and A.SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
                Open;
              end;
              with Self.ADOQueryTemp do
              begin
                First;
                while not Eof do
                begin
                  with CDS_Main do
                  begin
                    Append;
                    FieldByName('MainId').Value:=Self.ADOQueryTemp.fieldbyname('MainId').Value;
                    FieldByName('SubId').Value:=Self.ADOQueryTemp.fieldbyname('SubId').Value;
                    FieldByName('APId').Value:=Self.ADOQueryTemp.fieldbyname('APId').Value;
                    FieldByName('OrderNo').Value:=Self.ADOQueryTemp.fieldbyname('OrderNo').Value;
                    FieldByName('KgQty').Value:=Self.ADOQueryTemp.fieldbyname('KCKgQty').Value;
                    FieldByName('Qty').Value:=Self.ADOQueryTemp.fieldbyname('KCQty').Value;
                    FieldByName('QtyUnit').Value:=Self.ADOQueryTemp.fieldbyname('QtyUnit').Value;
                    FieldByName('MJId').Value:=Self.ADOQueryTemp.fieldbyname('MJId').Value;
                    FieldByName('CRId').Value:=Self.ADOQueryTemp.fieldbyname('CRId').Value;
                    FieldByName('CPType').Value:=Self.ADOQueryTemp.fieldbyname('CPType').Value;
                    Post;
                  end;
                  Next;
                end;
              end;
            end;
            CDS_Sub.Edit;
            CDS_Sub.FieldByName('SFlag').Value:='2';
            CDS_Sub.Post;
            Next;
          end;
        end;
        CDS_Sub.EnableControls;
      end;
    end;
  finally
    frmOrderSel.Free;
  end;
end;

procedure TfrmCpCkSaoMNew.InitSubGrid();
begin
  try
    ADOQuerySub.DisableControls;
    with ADOQuerySub do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*');
      sql.Add(' from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.Mainid');
      sql.Add(' where 1<>1  ');

      Open;
    end;
    SCreateCDS20(ADOQuerySub,CDS_Sub);
    SInitCDSData20(ADOQuerySub,CDS_Sub);
  finally
    ADOQuerySub.EnableControls;
  end;
end;


procedure TfrmCpCkSaoMNew.Button1Click(Sender: TObject);
begin
  if CDS_MainSel.IsEmpty then Exit;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CK_BanCP_CR where BCID='''+Trim(CDS_MainSel.fieldbyname('BCID').AsString)+'''');
      sql.Add('UPdate CK_BanCP_KC Set KCKgQty=(select KgQty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
      sql.Add(',KCQty=(select Qty from CK_BanCP_CR A where  A.CRID=CK_BanCP_KC.CRID and A.CRType=''检验入库'') ');
      SQL.Add(' where CRID='+CDS_MainSel.fieldbyname('CRID').AsString);
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    with CDS_Main do
    begin
      Append;
      FieldByName('MainId').Value:=CDS_MainSel.fieldbyname('MainId').Value;
      FieldByName('SubId').Value:=Self.CDS_MainSel.fieldbyname('SubId').Value;
      FieldByName('OrderNo').Value:=Self.CDS_MainSel.fieldbyname('OrderNo').Value;
      FieldByName('KgQty').Value:=Self.CDS_MainSel.fieldbyname('KgQty').Value;
      FieldByName('Qty').Value:=Self.CDS_MainSel.fieldbyname('Qty').Value;
      FieldByName('QtyUnit').Value:=Self.CDS_MainSel.fieldbyname('QtyUnit').Value;
      FieldByName('MJId').Value:=Self.CDS_MainSel.fieldbyname('MJId').Value;
      FieldByName('CRId').Value:=Self.CDS_MainSel.fieldbyname('CRId').Value;
      FieldByName('APID').Value:=Self.CDS_MainSel.fieldbyname('APID').Value;
      FieldByName('CPType').Value:=Self.CDS_MainSel.fieldbyname('CPType').Value;
      Post;
    end;
    CDS_MainSel.Delete;
    MovePanel1.Visible:=True;
    if CDS_MainSel.IsEmpty=False then
      Edit1.Text:=IntToStr(Tv3.DataController.Summary.FooterSummaryValues[2])
    else
      Edit1.Text:='0';
    Edit2.Text:=Trim(CDS_Main.fieldbyname('MJId').AsString);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销成功！','提示',0);
  end;
end;

procedure TfrmCpCkSaoMNew.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_MainSel.IsEmpty=False then Exit;
  if CDS_Sub.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while not Eof do
    begin
      if Trim(CDS_Main.fieldbyname('SubId').AsString)=Trim(CDS_Sub.fieldbyname('SubId').AsString) then
      begin
        CDS_Main.Delete;
      end else
        Next;
    end;
  end;
  CDS_Main.EnableControls;
  CDS_Sub.Delete;
end;

procedure TfrmCpCkSaoMNew.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=False;
end;

end.
