Magento - custom invoice number - magento

I need to modify my default invoice number from 100000001 to 2012 - 00001.
I know where I can found increment_last_id in table eav_entity_store. But I don't know what I must set that to be taken new format of invoice number.
Please help with some advice.

If you want to do it manually then take a look # How to Change the Invoice Increment ID and Prefix in Magento (remember to always make a backup)

You can customize order/invoice/creditmemo/shipment number (increment_id) by editing the following class:
Mage_Eav_Model_Entity_Increment_Numeric
Especially, closely look at the code of the following methods:
getNextId(), getPrefix(), getPadLength(), format($id)
Now, you won't find the method definition for methods getPrefix(), getPadLength() because these are magic getter methods. You can define these methods according to your desire.
For an example:
public function getPrefix(){
$prefix = $this->_getData('prefix');
/* Do some customization */
return $prefix;
}
public function getPadLength()
{
$padLength = $this->_getData('pad_length');
/* Do some customization */
return $padLength;
}
This way, you don't have to manually change anything in the database structures for this to achieve.
Hope this will help you.

The best way to change invoice id is to run following simple sql query:
Check if invoice record exist in eav_entity_store table by running following query
select * from eav_entity_store where entity_type_id in (select entity_type_id from eav_entity_type where entity_type_code='invoice');
If no record exists, create one dummy invoice from magento backend. Then you will have one record in the table, now run following script:
update eav_entity_store set increment_last_id="YOUR_DESIRED_INVOICE_ID", increment_prefix='X-' where entity_type_id in (select entity_type_id from eav_entity_type where entity_type_code='invoice')
Try this out and create new invoice:
update eav_entity_store set increment_last_id="0001", increment_prefix='2002' where entity_type_id in (select entity_type_id from eav_entity_type where entity_type_code='invoice')
http://deepakbhatta.com/magento-set-custom-invoice-id/
This works fine to me.
Thanks

Related

I cannot find certain fields on the MAGENTO database

I was trying to find this fields because i want to obtain all the products and then, make a query but i was not able to do that(they dont appear in the database diagram for Magento 1.9):
sku, code_brand, code_business_unit, code_line, code_group, code_business, min_sale_qty
This is the diagram:
https://anna.voelkl.at/uploads/magento/ce1922.png
I only found the fields "SKU" and "min_sale_qty". What happened to the others?
Do these fields : code_brand, code_business_unit, code_line, code_group, code_business even exist or they were created by someone?
Anyway, i am using MYSQL connected to the MAGENTO database.
I suppose that code_brand, code_business_unit, code_line, code_group, code_business are custom product attributed that you added right?
When you add a custom attribute, Magento add a row in the table eav_attribute.
There we can find attribute_id and backend_type.
For example if code_brand is a integer the backend_type should be int. So in catalog_product_entity_int we can find rows with the same attribute_id set in the eav_attribute table.
Here you are a sumple query to search that:
SELECT
*
FROM
catalog_product_entity_int
WHERE
attribute_id IN (
SELECT
attribute_id
FROM
eav_attribute
WHERE
attribute_code = 'code_brand'
);

Newest items and GROUP By with Eloquent

I have the following prices-table:
shop_id (int)
product_id (int)
price (float)
created (DateTime)
Every hour a cronjob checks the shops and inserts new entries (current prices) into these price-table.
Now I want to display the newest price for a product. I have to GROUP BY the shop_id because I only want one price per shop but I only want the newest entry (created).
Can I solve this with Eloquent Query-Builder or do I have to use raw SQL? Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
You can try it as:
Price::select('*', DB::raw('MAX(created_at) as max_created_at'))
->groupBy('shop_id')
->get()
Assuming model name is Price
Eloquent (purist) approach:
Price::orderBy('created', 'desc')->groupBy('shop_id')
->get('shop_id', 'price');
References:
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_orderBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_groupBy
https://laravel.com/api/5.3/Illuminate/Database/Query/Builder.html#method_get
*untested though
Q: Is it possible to pass the result of a raw SQL-query into a model if the columns are the same?
A: you could pass it to Model's contructor - but it might need model's field to be fillable - or hydrate a model. Alternatively, just access it like an keyed-array, ie. $something[0]['price'] <-- assuming an array of prices with price column.
I solved the problem without QueryBuilder. Instead I use a raw SQL-statement and generating the models with the hydrateRaw()-function of the Model-class.
$prices = Price::hydrateRaw( 'SELECT p.*
FROM prices p
INNER JOIN (
SELECT shop_id, max(created_at) AS max_ca
FROM prices p1
GROUP BY shop_id
) m ON p.shop_id = m.shop_id AND p.created_at = m.max_ca');

Magento addFieldToFilter: how to getCollection() from table using forigen key

I am new to magento, tried to solved it, but not succeeded.
My Problem is that,I have to apply "addFieldToFilter" filter on base if "store_id" , I have to drop all orders for a specific store/stores from collection on table
sales_flat_order
but store_id is in table
sales_flat_order_address
"sales_flat_order_address" table's primary key "entity_id" is used as foreign key in "sales_flat_order_address" as "parent_id", I have written a query some thing like this,
$ordercollection = Mage::getModel('sales/order')->getCollection();
$ordercollection->getSelect()->joinLeft(array('sfoa' => 'sales_flat_order_address'),'main_table.entity_id = sfoa.store_id',array('store_id'=>'sfoa.store_id'));
$ordercollection->addFieldToFilter('store_id',array('neq'=>'555'));
I don't understand it, And it's not working
You can use this to filter your orders by store id
$ordercollection = Mage::getModel('sales/order')->getCollection();
$ordercollection->addFieldToFilter('store_id',array('neq'=>'4'));
$ordercollection->getSelect()->joinLeft('sales_flat_order_address', "main_table.entity_id = sales_flat_order_address.parent_id",array('country_id'));
try this extension for deleting orders..
http://www.magentocommerce.com/magento-connect/delete-orders-6.html

Laravel 4.2 - Update a pivot by its own id

I'm having some trouble with my pivot table. I've recognized too late, that it is possible, that some pivot rows doesn't have unique values in my project, means I've to add an auto_increment ID field to my pivot table.
This is my structure:
Order.php
public function items()
{
return $this->belongsToMany('Item', 'orders_items', 'order_id', 'item_id')->withPivot(['single_price', 'hours', 'prov', 'nanny_id', 'vat', 'vat_perc', 'invoice_id','id']);
}
orders_items
id, order_id, item_id, nanny_id, hours
I've conntected Orders and Items through a pivot table ('orders_items)'. It is possible, that one order has 2 or more same items in the pivot table. So I've to add an unique ID to identify and update them.
Now I try to update a pivot row. Problem is, if I have 2 or more items, he updates them all, not only one. This is my update command:
$order = Order::find($orderId);
$items = $order->items()->whereNull('nanny_id');
$free_item = $items->first();
$free_item->pivot->nanny_id = 123;
$free_item->pivot->save();
With this command, he updates all pivot rows from the order. I know the problem: Laravel uses here the wrong identifiers (it uses order_id and item_id as defined in my belongsToMany relationship - and they aren't unique). For example, Laravel tries to execute this code on save():
UPDATE orders_items SET [...] WHERE order_id = 123 AND item_id = 2;
I want, that Laravel changes the query to this one:
UPDATE orders_items SET [...] WHERE order_id = 123 AND item_id = 2 AND id = 45;
// Edit
Okay, this solution works:
$free_item->pivot->where('id',$free_item->pivot->id)->update('nanny_id',123);
But is there an easier way, f.e. adding a custom pivot model that adds the id automatically to save() and update() methods?

Set Include in Navigation Menu to Yes in database

I want to set Include in Navigation Menu for all categories to Yes.
Can anybody tell me in which table this value is?
Magento use an eav model to save values in the database.
You have to search an attribute called "include_in_menu" in the "eav_attribute" table.
This attribute have an "attribute_id" which will be retrieve in the other tables.
On my installation, this attribute has attribute_id = 67 which is stored as an INTEGER (int)
On magento all the attributes have a type which you can find on the eav_attribute table.
You want to update your categories, we have to update the table where the integer attribute "include_in_menu" of the categories is saved.
As you can see in your database, you have a lot of tables for the categories :
catalog_category_entity, catalog_category_entity_datetime, catalog_category_entity_decimal, catalog_category_entity_int...
You have to select all your categories from the first table : select entity_id from catalog_category_entity
After you have to select the attribute_id in the table related to the good attribute type. Here is "catalog_category_entity_int" for all the INTEGER attribute...
select value from catalog_category_entity_int where attribute_id = 67 and store_id = ...
Be careful if you have multiple stores...All the stores are on the same table.
And now you just have to update value to "1" on the selected rows.
Sorry for my English, i m french...
Best regards
Cédric
This answer worked on Magento EE 2.2.2.
Based on Cedric's answer I used the following query to remove all of the categories from the menu via Sequel Pro.
You can remove all of your categories from your menu by running:
UPDATE catalog_category_entity_int SET value='0' WHERE attribute_id='67'
You can add all of your categories to your menu by running:
UPDATE catalog_category_entity_int SET value='1' WHERE attribute_id='67'
Then flush your cache (php bin/magento cache:flush) and you should see your changes on the frontend.
Again, be careful of what 'store_id' you're affecting.
You can use following query to update all categories:
update catalog_category_entity_int cci
inner join eav_attribute a on a.attribute_id = cci.attribute_id
set cci.value = 1
where a.attribute_code = 'include_in_menu';

Resources