Jump to content

How to change 'id' with a custom field as Order Slip number


ub.django

Recommended Posts

Hi,

First time in the International forum, I am here to kindly ask you some help to solve a problem I have.

I need to change the Order Slip Number, from the default 'id' to a custom field I created. The purpose is to avoid deleting all the slips created in the past, everytime a new year starts, as the Italian laws need you to start the number every new year from 1.

The change must me made to header informations contained in /classes/pdf/HTMLTemplateOrderSlip.php. On the third line of the following code is the "id" variable I want to change to mine that is called invoice_number:
 

// header informations
$this->date = Tools::displayDate($this->order_slip->date_add);
$prefix = Configuration::get('PS_CREDIT_SLIP_PREFIX', Context::getContext()->language->id);
$this->title = sprintf(HTMLTemplateOrderSlip::l('Credit Slip #%1$s%2$06d'), $prefix, (int)$this->order_slip->id);

But on the PDF it shows al zeros instead of the correct number that is stored in the database.

 

 

What I've done till now to obtain the result, is:

1) add a column in the slips' table called "invoice_number" (integer) to store the progressive number of the slips:

ALTER TABLE `ps_order_slip` ADD `invoice_number` INT NULL AFTER `date_upd`;
UPDATE `ps_order_slip` SET `invoice_number`=`id_order_slip`;

As you can see at same time I set invoice_number equal to the id for the records already stored in the db.

2) I then added a couple of new variables to the PS configuration to easily track the latest value and year of the slips number:
 

INSERT INTO `ps_configuration` ( `name`, `value`) VALUES ('PS_NEXT_SLIP_NUMBER', '1');
INSERT INTO `ps_configuration` ( `name`, `value`) VALUES ('PS_NEXT_SLIP_YEAR', '2017');

3) I edited the /classes/order/OrderSlip.php class files to introduce my alternative numbering, as it follows.

I added the new field to the definition (see last line):

	public static $definition = array(
		'table' => 'order_slip',
		'primary' => 'id_order_slip',
		'fields' => array(
			'id_customer' => 			array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
			'id_order' => 				array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
			'conversion_rate' => 		array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true),
			'total_products_tax_excl' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true),
			'total_products_tax_incl' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true),
			'total_shipping_tax_excl' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true),
			'total_shipping_tax_incl' => array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat', 'required' => true),
			'amount' => 				array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
			'shipping_cost' => 			array('type' => self::TYPE_INT),
			'shipping_cost_amount' =>	array('type' => self::TYPE_FLOAT, 'validate' => 'isFloat'),
			'partial' =>				array('type' => self::TYPE_INT),
			'date_add' => 				array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
			'date_upd' => 				array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
			'order_slip_type' =>		array('type' => self::TYPE_INT, 'validate' => 'isInt'),
			'invoice_number' =>			array('type' => self::TYPE_INT),
		),
	);

I edited the "create" function to perform the calculation of the invoice number:

	public static function create(Order $order, $product_list, $shipping_cost = false, $amount = 0, $amount_choosen = false)
	{
		$currency = new Currency((int)$order->id_currency);
		$order_slip = new OrderSlip();
		$order_slip->id_customer = (int)$order->id_customer;
		$order_slip->id_order = (int)$order->id;
		$order_slip->conversion_rate = $currency->conversion_rate;
		$order_slip->total_shipping_tax_excl = 0;
		$order_slip->total_shipping_tax_incl = 0;
		$order_slip->partial = 0;
		
		//*** BEGIN CUSTOMIZATION *************************************************
		$configuration_slip_year = (int)Configuration::get('PS_NEXT_SLIP_YEAR');
		$today_year = date("Y");
		
		if ($configuration_slip_year == $today_year)
		{
			$order_slip->invoice_number = (int)Configuration::get('PS_NEXT_SLIP_NUMBER');
			Configuration::updateValue('PS_NEXT_SLIP_NUMBER', $order_slip->invoice_number + 1);		
		} else {
                        $order_slip->invoice_number = 1;
                        Configuration::updateValue('PS_NEXT_SLIP_NUMBER', 2);
                        Configuration::updateValue('PS_NEXT_SLIP_YEAR', $today_year);   
		}
		//*** END CUSTOMIZATION **************************************************

		if ($shipping_cost !== false)
		{
			$order_slip->shipping_cost = true;

4) I changed the variable from "id" to "invoice_number", as said above, in the HTML Template of slips (/classes/pdf/HTMLTemplateOrderSlip.php):
 

// header informations
$this->date = Tools::displayDate($this->order_slip->date_add);
$prefix = Configuration::get('PS_CREDIT_SLIP_PREFIX', Context::getContext()->language->id);
$this->title = sprintf(HTMLTemplateOrderSlip::l('Credit Slip #%1$s%2$06d'), $prefix, (int)$this->order_slip->invoice_number);

But here is my problem as it doesn't work. On the PDF it shows al zeros instead of the correct number that is stored in the database.

Please help me figure out what I made wrong. Thanks!

Umberto

PS: I'm aware that all the modifications would have had a better home in the override folder, but I'm not so much into OOP to do it.

Edited by ub.django (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 year later...

Ho cercato anch'io una soluzione, questa di creare un nuovo campo è complicata e implica la modifica di vari file, ma è teoricamente giusta, dato che in database non riusciamo a modificare il numero esistente, perché è un "autoincrement", quindi non si può reimpostare la partenza a un numero inferiore di quello esistente, l'unico modo sarebbe svuotare la tabella e ripartire con il contatore. 

Ci sono troppi file da considerare e, dopo aver perso tanto tempo inutilmente, penso che l'unica via sia usare un modulo che gestisce i numeri di tutti i documenti, e ha il reset automatico. https://addons.prestashop.com/it/contabilita-fatturazione/20202-impostazioni-avanzate-per-numeri-di-tutti-documenti.html

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...