'CTG_TCKT - release routine for Cutting Tickets
' 24/10/05 - confirmation for each ticket to check if printed; SWIP style ticket

' Check for [requsn.Time_Allowed]=Y; use LOOKUP in REQUSN.DB"
' If Y then do NOT adjust PhysicalBalance; otherwise adjust"
' [requsn.Time_Allowed]=Y has been set for all req'ns up to 25/01/98

external   fentrybox() messbox() progress() vloadif() vunloadif() chkdate()
external   dpath vloadexcif() chg_dsc() sch scw cpath remove() fgp bgp
external   mess4() PrintReport() shopmask makeidx() bgs fgs popuplist() dsa
external   messboxwait() increment() cplpath chkstr() addidxrec() strcount()
external   Background() userid bpopdb() getidxrecs()
external   X_path _SWIP_Crystal() Xreppath ctgpath

public     ftgdate ptstr job_names[1] prod_code[1] ftr_codes[1] ptval jobnr
public     jobsperftr[1] $itemtype $cplrefnr ftrname custname deladdr1 $title
public     deladdr2 deladdr3 deladdr4 offtel hometel ftginstr ftgcomm $repnr
public     cr_status mobile balancedue $carptitle ftrcode $nickname datecode
public     #origsize $rollnr $refnr #out docref #newbal prodcode custcode
public     $jobseq $ctgref ctgref $pdf #sumcuts

global     CheckFitDate() x ReturnToMenu() nrjobs nrftrs gdsinstck apptsconfd
global     anclinstck nrcodes tot_need tot_avail ChkGdsOut() $permit $jobnrs
global     p1 p2 p3 p4 p5 p6 $comment cplrep i #jobnrs #prec StoreReport()
global     $ftrs #ftrs CuttingList() datenumber sr er #precnr Chk4Tkt()
global     EnterDocRef() CuttingTicket() Trans_STCK() $cpl $oldcpl Check4OtherAllocns()


MAIN
local l1 l2 l3 l4 l5 l6 l2a l7
single-step off
'--------------------------------------Setup
  Background()
  file unload all
  $permit = "TBELDI"
' message "Printing to port1"
  p2 = ""               ' p2 = title at top of choice popup ("LABEL")
  p3 = 1                ' p3 = printer to be used (1=HPIII_QC; 2=GEN_EPSN etc)
  p4 = 1                ' p4 = printer port to use (1,2 etc - network set to use 2=LASER; 3=LABEL)
  p5 = 1                ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
'   p4 = 1                ' p4 = printer port to use (1,2 etc - network set to use 2=LASER; 3=LABEL)
'   p5 = 3                ' p5 = choose VIEW/PRINT 1=PRINT; 2=VIEW; 3=CHOOSE
  p6 = 1                ' p6 = nr of copies

'--------------------------------------suggest day to prepare list for
  CheckFitDate()
  if x = -1
    ReturnToMenu()
  end if

  x = ChkGdsOut()                    ' 1=Not all rec'd; 0=OK
  if x = 1
    if userid = $permit
      messbox(" Do you want to continue? (y/n) ",1,0,1)
      if ptstr == "n"
        ReturnToMenu()
      end if
    end if
  elseif x = 2
    ReturnToMenu()
  end if

  vloadif(dpath|"goodsout.vws")
  order change index "todays1.idx"  'look at Job_Nrs in GOODSOUT/todays1.idx and remove duplicates
  data goto record first
  $jobnrs = [Job_Nr]
  data goto record next
  for i = 2 to records                 'create text string of Job Nrs
    x = chkstr([Job_Nr],$jobnrs)
    if x = -1
      $jobnrs = $jobnrs&[Job_Nr]
    end if
    data goto record next
  end for

'create index of CUST_ORD by stepping thro' text string and using ADDIDXRECS
  x=strcount($jobnrs)                  ' message "x is:"&str(x)
  #jobnrs = ptval
  vloadif(dpath|"cust_ord.vws")
  order change key "[Job_Nr]"
  x = makeidx("cust_ord","jobs2fit","0",3)     'message "x is:"&str(x)
  for i = 1 to #jobnrs
    jobnr = group($jobnrs,i)
    data find "[Job_Nr]" equal jobnr options ""
    x = addidxrec("jobs2fit.idx",precord,7)    'message "x is:"&str(x)
  end for
  vunloadif("cust_ord.vws")

  vloadif(dpath|"lststk_G.vw")
  order change index "todays1.idx"
  data query execute "ctg_list.dfq" index "list.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [Itemtype] = "C"                                                   ³
' ³ or                                                                 ³
' ³ [Itemtype] = "S"                                                   ³
' ³ or                                                                 ³
' ³ [Itemtype] = "V"                                                   ³
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  if cerror
    messboxwait(" No carpets to cut or stock tiles/vinyl to prepare ",0,0,1)
    ReturnToMenu()
  end if
  order sort now dictionary "listcarp.idx" fields "[ViewDesc]" ascending

  CuttingList()

  ReturnToMenu()

END MAIN


FUNCTION CheckFitDate()
  while true
    ftgdate = days(today) + 1
    if dayname(date2(ftgdate)) == "Saturday"
      ftgdate = days(today) + 3
    end if
    if dayname(date2(ftgdate)) == "Sunday"
      ftgdate = days(today) + 2
    end if
    x=fentrybox(" Date to prepare Cutting Tickets for ",10,"##\/##\/####",date2(ftgdate))
    if x = -1
      return (-1)
    end if
    ftgdate = ptstr
    if chkdate(ftgdate,1) = -1
      messbox(" Incorrect date - re-enter ",0,0,1)
      continue while
    end if
    exit while
  end while
END FUNCTION ' CheckFitDate()


FUNCTION ReturnToMenu()
  screen clear box 1 1 sch scw 0 0 no-border
  repaint off
  file unload all
  transfer cpath|"pm_menu.psl" in-memory
END FUNCTION ' ReturnToMenu()


FUNCTION CuttingList()                 'vloadif(dpath|"lststk_G.vw")
	while record<=records
    		#out = [QuantOut]
    		$refnr = [Reference_Nr]            'message "$refnr is:"&str($refnr)
    		#precnr = precord                  'message "precord in LSTSTK_G is:"&str(#precnr)
    		$cpl    = [CPL_Ref]                'message "$cpl is:"&str($cpl)
    		x=Chk4Tkt()              'x=2 already printed; x=0 not yet printed
    		if x = 1
      		vloadif(dpath|"lststk_G.vw")
      		data goto record next
      		continue while
    		elseif x = 2
      		vloadif(dpath|"lststk_G.vw")
      		lock-record
        			[CPL_Ref]=$oldcpl
      		write-record
      		data goto record next
      		continue while
    		end if
    		vloadif(dpath|"lststk_G.vw")
    		increment(dpath|"ctg_tkts.dat",1)   'get document ref
    		ctgref = "CT"|right("0000"|str(ptval),4)
    		x = CuttingTicket()
    		if x = 1
      		data goto record next
      		continue while
    		end if

    		Trans_STCK()

    		EnterDocRef(ctgref,"Cutting")

    		vloadif(dpath|"lststk_G.vw")
    		data goto record next
  	end while
END FUNCTION ' CuttingList()


FUNCTION Chk4Tkt()
  vloadif(dpath|"ctgtkt_2.vw")
  order change key "[Requsn_Nr]"
  while record<=records                'message "$refnr is:"&str($refnr)
    data find "[Requsn_Nr]" equal $refnr options ""
    if cerror                               '   if none - then return
      return (0)
    else
'       if days([Date_Out])=days(ftgdate)
      if left([CPL_Ref],2)="CT"
        $oldcpl = [CPL_Ref]
        messboxwait(" Cutting Ticket for"&$refnr&"already printed ("|$oldcpl|")",0,0,1)
        if len($cpl)=0
          return (2)
        elseif $cpl=blank
          return (2)
        elseif $cpl=null
          return (2)
        elseif $cpl="0"
          return (2)
        else
          return (1)
        end if
      elseif (deleted)
        data goto record next
        continue while
      else
        return (0)
      end if
'       end if
      data goto record next
    end if
  end while
END FUNCTION ' Chk4Tkt()


FUNCTION CuttingTicket()
local $prn $prn_nr $carpdatafile
	$rollnr = [RollNr]                   'message "$rollnr is:"&str($rollnr)
  	if $rollnr = "00000/00"
    		messboxwait([Product_MRC]&"for"&[Job_Nr]&"not yet allocated ",0,0,1)
    		return (1)
  	else
    		lock-record
      		[CPL_Ref]=ctgref
    		write-record
  	end if
	$ctgref=[CPL_Ref]
	if len($ctgref)=0
    		messboxwait("Ctg Tkt Ref: for"&str(ctgref)&"not posted in GOODSOUT - report to HO ",0,0,1)
    		return (1)
	end if
  	vloadif(dpath|"stk_carp.vws")
  	#origsize = filelookup([RollNr],[PhysicalBalance],$rollnr)
  	remove("label.idx")
  	x=makeidx("goodsout","label.idx","0",5) ' message "x is:"&str(x)
  	vloadif(dpath|"ctgtktS3.vw")
' message "records is:"&str(records)
' message "#precnr is:"&str(#precnr)
  	data goto record record-number #precnr
  	if cerror                               '   if none - then return
    		x = messboxwait(" Record not found ",0,0,1)
  	end if
  	x = addidxrec("label.idx",str(precord),7)
  	order change index "label.idx"
	vunloadif("Xctgtkt1.vws")
	remove(X_path|"Xctgtkt1.*")
	data query execute "not_del.dfq" Smart4 X_path|"Xctgtkt1" fields\
"[RollNr;QuantOut;Date_Requisitioned;Product_MRC;Description_MRC;Job_Nr;Location;Name;origsize;ftgdate;CPL_Ref;Width]"

	while true
   		_SWIP_Crystal(Xreppath|"X_ctgtkt","P",0,1,"")
' 		messbox(" Has the Cutting Ticket printed correctly? (y/n) ",1,1,1)
		messbox(" Has Cutting Ticket"&str(ctgref)&"printed correctly? (y/n) ",1,1,1)
  		if ptstr == "y"                      '  store file & exit
			$pdf=ctgpath|$ctgref|".pdf"        'message "$pdf is:"&str($pdf)
	   		_SWIP_Crystal(Xreppath|"X_ctgtkt","EP",0,1,$pdf)
    			exit while
  		else
    			continue while
  		end if
	end while
'check for Remaining allocations on the rest of the roll

	Check4OtherAllocns()

  	vloadif(dpath|"ctgtktS3.vw")
	
  	order change physical
END FUNCTION ' CuttingTicket()


FUNCTION Check4OtherAllocns()
' message "$rollnr is:"&str($rollnr)
	vloadif(dpath|"ctgtktun.vw")
    	order change key "[RollNr]"
    	data query execute "OR_allocs.dfq" index "OR_allocs.idx"	'find if there any REQUSNs for
' 	the same [RollNr] but no [CPL_Ref] ref except Z requsns
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' [RollNr]=$rollnr
' and
' not (deleted)
' and
' left([Reference_Nr],1)<>"Z"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  	if cerror
    		return (1)
	else
		#sumcuts=filesum([Length_Quantity],[CPL_Ref]=blank)
    		messboxwait(" NOTE!! - this roll ("|$rollnr|") still has"&str(#sumcuts)|"m to be cut off for other allocations ",0,0,1)
    		return (1)
  	end if

END FUNCTION ' Check4OtherAllocns()


FUNCTION ChkGdsOut()
	vloadif(dpath|"goodsout.vws")
  	order change key "[Date_Out]"
  	data query execute "cplprep1.dfq" index "todays1.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Date_Out]=days(ftgdate)
'   and
'   not (deleted)
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  	if cerror
    		messboxwait(" No goods due for despatch on"&date2(ftgdate),0,0,1)
    		return (2)
  	end if

'check that all records for Stock or Bespoke have been alloc'd/rec'd
	data query execute "cplprep2.dfq" index "todays2.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
' ³ [RollNr] = "00000/00"
'   or
' ³ [RollNr] = "BESPOK"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
  	if cerror
    		return (0)
  	else
    		data query execute "notype_o.dfq" index "unalloc.idx"
' ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'   [Itemtype] <> "O"
' ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
    		if cerror
      		return (0)
    		else
	     	messboxwait(" Not all goods for despatch have been received or allocated ",0,0,1)
	     	repaint off
     		order change physical
     		vloadif(dpath|"notrecvd.vw")
	     	order change index "unalloc.idx"

 			vunloadif("X_notrcv.vws")
			remove(X_path|"X_notrcv.*")
 			data query execute "not_del.dfq" Smart4 X_path|"X_notrcv" fields\
"[Job_Nr;FittingDate;Product_MRC;Description_MRC;QuantOut;Requsn_Nr;Name;Delivery_Quoted;Order_Reference;Date_Ordered;Telephone]"

' 			_SWIP_Crystal(Xreppath|"Xnotrcv1","S",0,1,"")
			while true
				_SWIP_Crystal(Xreppath|"Xnotrcv1","S",0,1,"")
'   PrintReport(p1,"Not Received",p3,p4,p5,p6)
  				messbox(" Has the 'Not Received List' printed correctly? (y/n) ",1,1,1)
  				if ptstr == "y"                      '  store file & exit
    					exit while
  				else
    					continue while
  				end if
			end while
		     return (1)
    		end if
  	end if
END FUNCTION ' ChkGdsOut()


FUNCTION StoreReport(p1,ext)
' local dlyrep
  cplrep = cplpath|$cplrefnr|"."|ext  'message "cplrep is:"&str(cplrep)
  print report execute p1 disk cplrep detail start 1 end 0 copies 1
END FUNCTION ' StoreReport()


FUNCTION Trans_STCK()
local $lu
  vloadif(dpath|"requsn.vws")
  $lu = filelookup([Reference_Nr],[Time_Allowed],$refnr) 'message "$lu is:"&str($lu)
  vunloadif("requsn.vws")
  if $lu <> "Y"
    vloadif(dpath|"stk_carp.vws")
    order change key "[RollNr]"
    data find "[RollNr]" equal $rollnr options ""
    if cerror                               '   if none - then return
      x = messboxwait(" Roll Nr"&$rollnr&"not found - inform office ",1,0,1)
    else
      #newbal = [PhysicalBalance]-#out  'message "#newbal is:"&str(#newbal)
      lock-record
        [PhysicalBalance] = #newbal
      write-record
    end if
  end if
END FUNCTION ' Trans_STCK()


FUNCTION EnterDocRef(ref1,ref2)
  vloadif(dpath|"ctg_tkts.vws")
  data enter lock
    [Doc_Ref]   = ref1
    [Dated]     = today
    [Timed]     = now
    [Creator]   = userid
    [RollNr]    = $rollnr
    [Length]    = #out
  write-record
END FUNCTION ' EnterDocRef()

