unit U_ProductOrderCarSplit;

interface

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

type
  TfrmProductOrderCarSplit = class(TForm)
    Panel1: TPanel;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    customerNoName: TEdit;
    Label4: TLabel;
    MprtCodeName: TEdit;
    Label5: TLabel;
    prtColor: TEdit;
    Qty: TEdit;
    Label6: TLabel;
    OrderUnit: TEdit;
    Panel2: TPanel;
    TV1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    CRTime: TDateTimePicker;
    Label1: TLabel;
    Label7: TLabel;
    Button1: TButton;
    Button2: TButton;
    V1Column1: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    CDS_Glide: TClientDataSet;
    DS_Glide: TDataSource;
    CDS_Car: TClientDataSet;
    DS_Car: TDataSource;
    ADO_Car: TADOQuery;
    ADO_Glide: TADOQuery;
    ADO_Order: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    V1Column5: TcxGridDBColumn;
    carnum: TcxCurrencyEdit;
    Button3: TButton;
    V1Column7: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    N1: TMenuItem;
    Label8: TLabel;
    RollNum: TEdit;
    GroupBox2: TGroupBox;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton6: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label9: TLabel;
    orderType: TEdit;
    carNo: TEdit;
    Label10: TLabel;
    OrderNo: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TV1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure ToolButton6Click(Sender: TObject);
    procedure TV1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
  private
    fOrderType: string;
    procedure InitOrder();
    procedure InitCar();
    procedure InitGlide();
 
    { Private declarations }
  public
    fsyrName, fMainID, fsubID, fcarNo: string;

    { Public declarations }
  end;

var
  frmProductOrderCarSplit: TfrmProductOrderCarSplit;

implementation

uses
  U_DataLink, U_ZDYHelp, U_Fun, U_ProductOrderListSel, U_GXGL_List_DXCX,
  U_PBKcList;
{$R *.dfm}

procedure TfrmProductOrderCarSplit.InitGlide();
begin
  try
    with Ado_Glide do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*');
      sql.Add('from JYOrder_Glide A ');
      sql.Add('where A.carNo=''' + trim(cds_Car.fieldbyname('carNo').AsString) + ''' ');
      open;
    end;
    SCreateCDS20(Ado_Glide, CDS_Glide);
    SInitCDSData20(Ado_Glide, CDS_Glide);
  finally
  end;
end;

procedure TfrmProductOrderCarSplit.InitCar();
begin
  try
    with Ado_car do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,oldRollNum=A.RollNum');
      sql.Add('from JYOrder_car A ');
      sql.Add('where A.FromCarNo=' + QuotedStr(Trim(FCarNo)));
      sql.Add('order by A.carNo  ');
      open;
    end;
    SCreateCDS20(Ado_car, CDS_car);
    SInitCDSData20(Ado_car, CDS_car);
  finally
  end;
end;

procedure TfrmProductOrderCarSplit.InitOrder();
begin
  try
    with Ado_Order do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*,B.*,C.*');
      sql.Add('from  JYOrder_Main  A ');
      sql.Add('inner join JYOrder_sub B on B.MainID=A.mainID ');
      sql.Add('inner join JYOrder_car C on C.SubID=B.SubID ');
      sql.Add('where C.CarNo=' + QuotedStr(Trim(FCarNo)));
      open;
    end;
    if not Ado_Order.IsEmpty then
    begin
      SCSHDataNew(Ado_Order, Panel1, 2);
    end;
  finally
  end;
end;

procedure TfrmProductOrderCarSplit.FormDestroy(Sender: TObject);
begin
  frmProductOrderCarSplit := nil;
end;

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

procedure TfrmProductOrderCarSplit.FormCreate(Sender: TObject);
begin
  GroupBox1.Align := AlClient;
  CRTime.Date := DServerDate;
end;

procedure TfrmProductOrderCarSplit.ToolButton2Click(Sender: TObject);
begin
  with CDS_Glide do
  begin
    if IsEmpty then
      Exit;
    if RecNo >= 1 then
    begin
      //Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value - 1;
      Post;
      Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value + 1;
      Post;
    end;
    Prior;
  end;
end;

procedure TfrmProductOrderCarSplit.ToolButton3Click(Sender: TObject);
begin
  CDS_Glide.IndexFieldNames := 'GlideIdx';
  with CDS_Glide do
  begin
    if IsEmpty then
      Exit;
    Next;
    if not Eof then
      Prior
    else
    begin
      exit;
    end;
    if not Eof then
    begin
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value + 1;
      Post;
      Prior;
      Edit;
      fieldByName('GlideIdx').Value := fieldByName('GlideIdx').Value - 1;
      Post;
    end;
    Next;
  end;
end;

procedure TfrmProductOrderCarSplit.ToolButton4Click(Sender: TObject);
var
  k: integer;
begin
  if cds_Glide.IsEmpty then
    exit;
  cds_Glide.Delete;
  cds_Glide.IndexFieldNames := 'GlideIdx';
  cds_Glide.DisableControls;
  cds_Glide.First;
  k := 1;
  while not cds_Glide.Eof do
  begin
    cds_Glide.Edit;
    cds_Glide.FieldByName('GlideIdx').Value := k;
    cds_Glide.Post;
    inc(k);
    cds_Glide.Next;
  end;
  cds_Glide.First;
  cds_Glide.EnableControls;
end;

procedure TfrmProductOrderCarSplit.FormShow(Sender: TObject);
begin
  InitOrder();
  InitCar();
  initGlide();

end;

procedure TfrmProductOrderCarSplit.Button1Click(Sender: TObject);
var
  i, k, HS: integer;
  maxId: string;
  MRollNum: Double;
begin
  if trim(OrderNo.Text) = '' then
  begin
    application.MessageBox('计划单号不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;

  if strtointdef(trim(carNum.text), 0) <= 1 then
    k := 1
  else
    k := strtointdef(trim(carNum.text), 0);

  MRollNum := RoundFloat(strtointdef(trim(RollNum.text), 0) / k, 0);

  CDS_Car.Last;
  for i := 1 to k do
  begin
    CopyAddRowCDS(CDS_Car);
    maxId := CDS_Car.FieldByName('carNo').AsString;
    if CDS_Car.RecordCount = 0 then
      HS := 1
    else
      HS := CDS_Car.RecordCount;

    while CDS_Car.Locate('carNo', (Trim(carNo.Text) + '-' + inttostr(HS)), []) do
    begin
      HS := HS + 1;
    end;
    CDS_Car.Locate('carNo', maxId, []);
    CDS_Car.edit;
    CDS_Car.FieldByName('carNo').Value := Trim(carNo.Text) + '-' + inttostr(HS);
    CDS_Car.FieldByName('CRTime').Value := CRTime.date;
    CDS_Car.FieldByName('Sflag').Value := '0';
    CDS_Car.FieldByName('RollNum').Value := MRollNum;

    CDS_Car.Post;
  end;
end;

procedure TfrmProductOrderCarSplit.Button2Click(Sender: TObject);
begin
  if CDS_car.IsEmpty then
    exit;
  if CDS_car.Locate('Qty', null, []) then
  begin
    application.MessageBox('开卡数量不能为零！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if CDS_car.Locate('Qty', 0, []) then
  begin
    application.MessageBox('开卡数量不能为零！', '提示信息', MB_ICONERROR);
    exit;
  end;
  ADOQueryCmd.Connection.BeginTrans;
  try
    CDS_Car.DisableControls;
    CDS_Car.First;
    while not CDS_Car.Eof do
    begin
      if CDS_Car.FieldByName('GlideIDx').AsInteger = 0 then
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from JYOrder_Car A');
          sql.Add('where CarNO=''' + trim(CDS_Car.fieldbyname('CarNo').AsString) + '''');
          open;
          if IsEmpty then
          begin
            append;
            fieldbyname('Filler').Value := trim(DName);
          end
          else
          begin
            edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTmp);
          end;
          FieldByName('carNo').Value := trim(CDS_Car.fieldbyname('carNo').AsString);
          FieldByName('FromcarNo').Value := trim(carNo.Text);
          FieldByName('CrTime').Value := formatdateTime('yyyy-MM-dd', CDS_Car.fieldbyname('CrTime').AsDateTime);
          FieldByName('MainID').Value := trim(fMainID);
          FieldByName('subID').Value := trim(fsubID);
          FieldByName('RollNum').Value := CDS_Car.fieldbyname('RollNum').AsInteger;
          FieldByName('Qty').Value := CDS_Car.fieldbyname('Qty').AsFloat;
          FieldByName('QtyUnit').Value := trim(CDS_Car.fieldbyname('QtyUnit').AsString);
          FieldByName('Note').Value := trim(CDS_Car.fieldbyname('Note').AsString);
          FieldByName('valid').Value := 'Y';
          post;
        end;
      end;
      CDS_Car.Next;
    end;

    CDS_Car.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！', '提示信息');
  except
    CDS_Car.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmProductOrderCarSplit.Button3Click(Sender: TObject);
begin
  if CDS_car.IsEmpty then
    exit;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_PB_CR where Valid=''Y'' and  CRType=''自动扣匹''');
    sql.Add('and CarNo=' + quotedstr(Trim(CDS_car.fieldbyname('carNO').AsString)));
    Open;
  end;
  if ADOQueryTmp.IsEmpty = False then
  begin
    Application.MessageBox('已扣匹不能删除数据!', '提示', 0);
    Exit;
  end;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' delete JYOrder_car where carNO=''' + Trim(CDS_car.fieldbyname('carNo').AsString) + '''');
      execsql;
    end;
    CDS_car.Delete;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmProductOrderCarSplit.ToolButton5Click(Sender: TObject);
var
  maxID: string;
begin
  if CDS_Glide.IsEmpty then
    exit;
  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('delete from JYOrder_Glide ');
      sql.Add('where carNO=''' + trim(cds_car.fieldbyname('carNo').asstring) + '''');
      execsql;
    end;
    CDS_Glide.DisableControls;
    CDS_Glide.First;
    while not CDS_Glide.Eof do
    begin
      if trim(CDS_Glide.FieldByName('GlideNo').asstring) = '' then
      begin
        if GetLSNo(ADOQueryCmd, maxID, 'CG', 'JYOrder_Glide', 4, 1) = False then
        begin
          Application.MessageBox('取最大号失败!', '提示', 0);
          Exit;
        end;
        CDS_Glide.Edit;
        CDS_Glide.FieldByName('GlideNo').Value := trim(maxID);
      end;
      with ADOQueryCmd do
      begin
        close;
        sql.Clear;
        sql.Add('select * from JYOrder_Glide A');
        sql.Add('where GlideNo=''' + trim(CDS_Glide.fieldbyname('GlideNo').AsString) + '''');
        open;
        if IsEmpty then
        begin
          append;
          fieldbyname('Filler').Value := trim(DName);
        end
        else
        begin
          edit;
          FieldByName('Editer').Value := Trim(DName);
          FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTmp);
        end;
        FieldByName('GlideNo').Value := trim(CDS_Glide.fieldbyname('GlideNo').AsString);
        FieldByName('Glideidx').Value := CDS_Glide.fieldbyname('GlideIdx').AsInteger;
        FieldByName('MainID').Value := trim(fMainID);
        FieldByName('subID').Value := trim(fsubID);
        FieldByName('carNo').Value := trim(CDS_car.fieldbyname('carNo').AsString);
        FieldByName('GlideName').Value := trim(CDS_Glide.fieldbyname('GlideName').AsString);
        FieldByName('RollNum').Value := CDS_Glide.fieldbyname('RollNum').AsInteger;
      //  FieldByName('Qty').Value:=CDS_Glide.fieldbyname('Qty').AsFloat;
      //  FieldByName('QtyUnit').Value:=trim(CDS_Glide.fieldbyname('QtyUnit').AsString);
        FieldByName('Note').Value := trim(CDS_Glide.fieldbyname('Note').AsString);
        post;
      end;
      CDS_Glide.Next;
    end;
    CDS_Glide.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('保存成功！', '提示信息');
  except
    CDS_Glide.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmProductOrderCarSplit.ToolButton1Click(Sender: TObject);
var
  maxId: string;
begin
  if CDS_Car.IsEmpty then
    exit;
  if not CDS_Car.Locate('Ssel', true, []) then
  begin
    Application.MessageBox('没有选择流程卡！', '提示信息', MB_ICONERROR);
    exit;
  end;
  frmGXGL_List_DXCX := TfrmGXGL_List_DXCX.create(self);
  with frmGXGL_List_DXCX do
  begin
    if showmodal = 1 then
    begin
      try
        ADOQueryCmd.Connection.BeginTrans;
        CDS_Car.DisableControls;
        CDS_Car.First;
        Order_FZ.DisableControls;
        while not CDS_Car.Eof do
        begin
          if CDS_Car.fieldbyname('Ssel').AsBoolean then
          begin
            with ADOQueryCmd do
            begin
              close;
              sql.Clear;
              sql.Add('delete from JYOrder_Glide ');
              sql.Add('where carNo=''' + trim(CDS_Car.fieldbyname('carNo').AsString) + '''');
              execsql;
            end;

            Order_FZ.First;
            while not Order_FZ.eof do
            begin
              if GetLSNo(ADOQueryCmd, maxId, 'CG', 'JYOrder_Glide', 4, 1) = False then
              begin
                Application.MessageBox('取最大号失败!', '提示', 0);
                Exit;
              end;
              with ADOQueryCmd do
              begin
                close;
                sql.Clear;
                sql.Add('select * from JYOrder_Glide where 1=2');
                open;
                ;

                ADOQueryCmd.Append;
                ADOQueryCmd.FieldByName('GlideNo').Value := maxId;
                ADOQueryCmd.FieldByName('Glideidx').Value := Order_FZ.RecNo;
                ADOQueryCmd.FieldByName('MainID').Value := trim(fMainID);
                ADOQueryCmd.FieldByName('subID').Value := trim(fsubID);
                ADOQueryCmd.FieldByName('carNo').Value := trim(CDS_Car.fieldbyname('carNo').AsString);
                ADOQueryCmd.FieldByName('GlideName').Value := trim(Order_FZ.fieldbyname('PSName').AsString);
          //    ADOQueryCmd.FieldByName('RollNum').Value:=CDS_Car.fieldbyname('RollNum').AsInteger;
           //   ADOQueryCmd.FieldByName('Qty').Value:=CDS_Car.fieldbyname('Qty').AsFloat;
                ADOQueryCmd.FieldByName('QtyUnit').Value := trim(CDS_Glide.fieldbyname('QtyUnit').AsString);
           //   ADOQueryCmd.FieldByName('Note').Value:=trim(Order_FZ.fieldbyname('Note').AsString);
                ADOQueryCmd.Post;
              end;
              Order_FZ.Next;
            end;
            Order_FZ.Next;
          end;
          CDS_Car.Next;
        end;
        CDS_Car.EnableControls;
        ADOQueryCmd.Connection.CommitTrans;
        initGlide();
      except
        CDS_Car.EnableControls;
        ADOQueryCmd.Connection.RollbackTrans;
        frmGXGL_List_DXCX.Free;
        application.MessageBox('保存失败！', '提示信息', 0);
      end;
    end;
    free;
  end;
end;

procedure TfrmProductOrderCarSplit.TV1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    FBounds := AViewInfo.Bounds;
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    InflateRect(FBounds, -1, -1);
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;

end;

procedure TfrmProductOrderCarSplit.ToolButton6Click(Sender: TObject);
begin
  if CDS_Car.IsEmpty then
    exit;
  frmGXGL_List_DXCX := TfrmGXGL_List_DXCX.create(self);
  with frmGXGL_List_DXCX do
  begin
    if showmodal = 1 then
    begin
      Order_FZ.First;
      while not Order_FZ.eof do
      begin
        CDS_Glide.Append;
        CDS_Glide.FieldByName('Glideidx').Value := CDS_Glide.RecordCount + 1;
        CDS_Glide.FieldByName('GlideName').Value := Order_FZ.fieldbyname('PSName').AsString;
        CDS_Glide.Post;
        Order_FZ.next;
      end;
    end;
    free;
  end;

end;

procedure TfrmProductOrderCarSplit.TV1CellClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  InitGlide();
end;

procedure TfrmProductOrderCarSplit.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Car, true);
end;

procedure TfrmProductOrderCarSplit.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Car, true);
end;

end.

