View Single Post
  #8  
Old 06-05-2020, 03:01 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Custom Order Numbering

One thing I see, although it may not be the issue you have is


Code:
... new DateTime(date("Y")."-01-01");


The way you have it PHP will assume \XLite\Model\Order\DateTime. Change it to


Code:
... new \DateTime(date("Y")."-01-01");


Also use $this->getReference() == '' instead of $this->reference == '' - that's why you have the get method. However the reference in this case will always be empty. setOrderNumber() is called only when order is placed and turned into an order.


Also you can make $reference nullable and assign default value of NULL instead of empty string. Then if you still want to check you can do


Code:
if (is_null($this->getReference())) { $this->setReference($this->generateReference()); }


or


Code:
if ($this->getReference() === null) { $this->setReference($this->generateReference()); }


keeping in mind === will evaluate exact value of null while == will evaluate 0, '', false being null as well.


While \ DateTime(date("Y")."-01-01") will give you what you want - Jan 1st @ 00:00:00am same is true for \DateTime(date("Y")."-12-31") - @ the very beginning of the day. You need to add 23 hrs 59 min and 59 seconds to end date to get to the end of year.


Change your query to select max reference directly and get single result instead of looping through all. And since I assume you will always want to get the max number which will be current year anyway there is no need of min and max timestamp.
I suggest you get the reference for the latest order that has one (meaning reference is not null, order number is not null and is_order is true) and then manipulate reference to increase by 1 for the next order.
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote