// Ñêðèïò ãåíåðèðóåò íîâûõ êîíòðàãåíòîâ äëÿ äèñêîíòíîé ñèñòåìû


const
    InitSummary = 10000.00;

var Form :TForm;
    Lab :TLabel;
    EditCnt :TEdit;
    EditFirstBarcode :TEdit;
    Bevel,Bevel1 :Tbevel;
    Button1 :Tbutton;
    Button2 :tButton;

    crd1,crd2 :integer;

    CNT :integer;
    Barcode :double;
    maxid :integer;

    i :integer;

    maxid1 :integer;

    table :TTAble;
begin


  Form := Tform.create(nil);
  try
    begin // ñîçäàåì ôîðìó

      Form.Caption := 'Ãåíåðàöèÿ êëèåíòîâ äèñêîíòíîé ñèñòåìû';
      Form.BorderStyle := bsDialog;
      Form.Width := 350;
      Form.Height := 200;
      Form.position := poScreenCenter;
      Form.Font.name := 'Tahoma';


      Bevel := tbevel.create(form);
      Bevel.Shape := bsTopLine;
      Bevel.Height := 4;
      Bevel.align := alTop;
      bevel.parent := Form;


      Lab := TLabel.create(form); // óäàëÿòü íå íóæíî, Form åãî owner, ñàì óäàëèò
      Lab.caption := 'Óêàæèòå êîëè÷åñòâî êàðòî÷åê:';
      Lab.Top := 20;
      Lab.Left := 10;
      Lab.parent := Form;

      EditCNT := TEdit.create(form);
      EditCNT.left := Lab.left + lab.width + 10;
      EditCNT.top := Lab.top;
      EditCNT.width := 50;
      EditCNT.parent := form;


      crd1 := Lab.top + lab.height;
      crd2 := Lab.left;

      Lab := TLabel.create(Form);
      Lab.caption := 'Óêàæèòå ïåðâûé øòðèõêîä:';
      Lab.top := crd1 + 10;
      Lab.left := crd2;
      Lab.parent := Form;

      crd1 := Lab.top+ lab.height;


      EditFirstBarcode := TEdit.create(form);
      EditFirstBarcode.left := Editcnt.left;
      EditFirstbarcode.top := Lab.top;
      EditFirstBarcode.width := 100;
      EditFirstBarcode.parent := form;


      Lab := TLabel.create(Form);
      Lab.caption := '(íå áîëåå 12 ñèìâîëîâ)';
      Lab.top := crd1 + 5;
      Lab.left := crd2;
      Lab.parent := Form;


      crd1 := Lab.top+ lab.height;


      Bevel1 := TBevel.create(Form);
      Bevel1.shape := bsTopLine;
      Bevel1.Height := 4;
      Bevel1.Width := Form.clientWidth - 30;
      Bevel1.Left := (Form.ClientWidth - Bevel1.Width) div 2;
      Bevel1.Top := crd1 + 20;
      Bevel1.parent := Form;

      Button1 := Tbutton.create(Form);
      Button1.Caption := 'Ñòàðò';
      Button1.Width := 70;
      Button1.ModalResult := mrOk;
      Button1.Top := Bevel1.Top + 13;
      button1.Default := true;
      Button1.Parent := Form;


      Button2 := Tbutton.create(Form);
      Button2.Caption := 'Îòìåíà';
      Button2.Width := 70;
      Button2.ModalResult := mrCancel;
      Button2.Top := Button1.top;
      Button2.left := Form.ClientWidth - Button2.Width - 35;
      Button2.Cancel := true;
      Button2.Parent := Form;

      Button1.left := Button2.left - Button1.width - 5;


      Form.clientHeight := Button1.Top + Button1.height + 15;

    end;

    if Form.ShowModal = mrOk
    then begin
      cnt := strtointprotected(Editcnt.text);
      //bvmessage(inttostr(cnt));
      barcode := strtofloatprotected(EditFirstBarcode.text);
      //bvmessage(floattostr(barcode));

      with TMyWait.create('Èäåò ñîçäàíèå êëèåíòîâ') do
      try
        table := ttable.create(nil);
        try
          table.databasename := 'dbkassa';
          table.tablename := 'agents.db';
          table.open;
          table.last;
          maxid := table.fieldbyname('id').asinteger;

          maxid1 := asinteger(BDEQueryValue('select max(id) from agentsum',[null],'dbkassa'));

          for i := 1 to cnt do begin
            table.append;
            maxid := maxid +1;
            table.fieldbyname('id').asinteger := maxid;
            table.fieldbyname('name').asstring := 'Êëèåíò äèñêîíòíîé ïðîãðàììû ÂÈÏ';
            table.fieldbyname('iscashklient').asinteger := 1;
            table.fieldbyname('barcode').asstring := floattostr(barcode);
            //table.fieldbyname('DISCOUNT_CASH').asinteger := 25;
         	  table.fieldbyname('comment').asstring := 'auto generated VIP';

            barcode := barcode + 1;
            table.post;

            inc(maxid1);
            BDEExecSQL('insert into agentsum(id,id_org,date_doc,summ) values (:id,:id_org,:date_doc,:summ)',[maxid1,maxid,date(),InitSummary],'dbkassa');

          end;
        finally
          table.free;
        end;
      finally
        free
      end;

      CreateHInt('Îïåðàöèÿ çàâåðøåíà');

    end;
  finally
    Form.free;
  end;


end.
