get Order_Item_Id using magento api - php

I want to create invoice in to magento store using magento api in php.For that I want to create invoice for particular quantity and item means If anyone wants to invoice one item in paricular quantity then It shoud be done.My code is working for array() or all quantity.
Below is pseudo code for creating invoice
$client = new Zend_XmlRpc_Client('http://127.0.0.1:8080/AndroidMagento/api/xmlrpc')
$session = $client->call('login', array('tester','tester'));
$saleorderno = '100000007';
Mage::init();
$order = Mage::getModel('sales/order')->load($saleorderno);
$orderItems = $order->getAllItems();
$invoiceItems = array();
foreach ($orderItems as $_eachItem) {
$invoiceItems[$_eachItem->getItemId()] = $_eachItem->getQtyOrdered();
}
$result = $client->call('call',array($session,'sales_order_invoice.create',array($saleorderno,array('order_item_id' => 9474, 'qty' => 1),'Invoice Created by Test',false,false)));
I have seen this link where i found somewhat idea but i can't understand exactly.I can't understand how to get value of order_item_id.???
Any idea??? Please suggest me Thanks in advance...

item_id and product_id are different id.
order or quote has item_id and product_id.
You can try this:
$order = Mage::getModel('sales/order')->load($saleorderno);
$orderItems = $order->getAllItems();
foreach ($orderItems as $item){
print_r($item->getData());
print_r($item->getItemId()); //magic function to get ['item_id']
}
You can do it in 'sales/quote' Model.
Cheers ^^

Try this,
echo "<pre>";
$result = $client->call($session, 'sales_order.info', 'orderIncrementId');
print_r($result['item_id']);
print_r($result['product_id']);
and the $result will return all info of order including item_id and product_id,
with $result['item_id'] you can pass it to call
sales_order_invoice.create
then do
$result = $client->call(
$session,
'sales_order_invoice.create',
array('orderIncrementId' => '200000008',
array('order_item_id' => $result['item_id'],
'qty' => $result['total_qty_ordered'])
)
);
and the qty, You have to get it from $result['total_qty_ordered']
First, try to print_r[$result]; then You'll get some hints from it.
^^

Related

Sum of Foreach Loop

I'm trying to total a bill balance with a program I'm working on in PHP.
The code I use to pull the pricing is as such.
public function PackagePricing($arg) {
$query = <<<SQL
SELECT packageID
FROM customer_packages
WHERE active = :true
AND customerID = :arg
SQL;
$resource = $this->db->db->prepare( $query );
$resource->execute( array (
":true" => 1,
":arg" => 1,
));
foreach($resource as $row) {
self::GetPricing($row['packageID']);
}
}
public function GetPricing($arg) {
$query = <<<SQL
SELECT price
FROM products
WHERE id = :arg
SQL;
$resource = $this->db->db->prepare( $query );
$resource->execute( array (
":arg" => $arg,
));
$totalBill = 0;
foreach($resource as $row) {
$totalBill+= $row['price'];
}
echo $totalBill;
}
Now by my understanding this should work, but what I'm getting in turn is:
On the right you can see the billing total and rather than totaling out it's giving me each individually.
The error seems quite obvious. Here's your sequence in different words :
Get all packages ID.
Foreach package ID, get the price of the item (only one result is returned)
For that single result, add up all the prices (you only get one). Print it and go back to 2.
What you see is not 2 prices that have been added as strings in some sort of way. You simply prints subsequently 2 different prices. 10 & 30
GetPricing should return a price, and the foreach loop that calls it should make the sum.
$total = 0;
foreach($resource as $row)
{
$total += self::GetPricing($row['packageID']);
}
Hope this helps.

Collection of zero tracking number shipments magento

I need to get a collection of shipments that has no tracking numbers associated with it in magento.
$collection = Mage::getModel('sales/order_shipment')->getCollection();
$collection->addAttributeTofilter('track_number', ''); //not working
Please give me a solution for this if you have.
I needed the same and managed to get it with the following code (magento 1.9.3):
$collection = Mage::getModel('sales/order_shipment')->getCollection()
->addAttributeTofilter('track_number', array('null' => true));
$trackTableName = Mage::getSingleton('core/resource')->getTableName('sales/shipment_track');
$collection->getSelect()->joinLeft(
array('tracking' => $trackTableName), 'main_table.order_id=tracking.order_id',
array()
);
foreach ($collection as $shipment) {
print_r($shipment->getData());
}

Associative array's loaded from database and display as 'columns'

I am trying to create an overview of product properties, for an invoice system.
So far, most things are comming together using classes and PDO.
I have the following issue.
In my class, i've created a function that builds my products array.
It loads some information from the database, to build this array.
This array, i want to use to display all the products i have selected:
$prod1 - $prod1Name - $prod1Descr - $prod1Price
$prod2 - $prod2name - $prod2Descr - $prod2Price
etc.
I figured that the Associative array would help me creating columns.
Though the problem is, that i do not understand a bit how to create multiple lines and columns this way.
I was thinking of something like:
$prod[1]["name"] - $prod[1]["descr"] - etc
Then to use this in a foreach loop to create as many new lines as required.
The only thing i could come up with is on my index.php (as shown below), cause using an index (the [1] defenition) does not seem to work the way i think it should be implemented.
For my understanding, i assigend the var in my class as an array, then redefine an array when loading the database information.
Could anyone tell me how i could try to solve this issue?
I have the following class:
<?
class Invoice{
var $vendorID;
var $product = array();
function product_array(){
global $db;
$query = $db->conn->prepare('
SELECT ProductName, ProductDescription, ProductDuration, ProductPriceInclVat, ProductPriceExclVat, ProductVatType
FROM products WHERE VendorID = :VendorID
');
$array = array (
'VendorID' => $this->vendorID
);
$query->execute($array);
$result = $query->fetchall();
if (empty($result)){
echo"Could not find any products matching your criteria.";
die;
} else {
foreach($result as $row) {
$this->product = array("Name" => $row['ProductName'],
"Description" => $row['ProductDescription'],
"Duration" => $row['ProductDuration'],
"PriceExclVat" => $row['ProductPriceExclVat'],
"PriceInclVat" => $row['ProductPriceInclVat'],
"VatType" => $row['ProductVatType']
);
}
}
}
}
?>
and then i have the following code on my index.php:
<?
$invoice = new Invoice();
foreach ($invoice->product as $key => $value){
echo $key . "<br>";
echo $value . "$value";
echo "<br>";
}
?>
When you are assigning the result arrays to the product property you are overwriting the array every time. You need to append to the array instead, so something like:
$this->product = array();
foreach($result as $row) {
$this->product[] = array(...);
}
Alternatively, you could just assign the results of fetchAll to the product property if you don't need to rename the field keys (or you could alias them in the SQL).
$query = $db->conn->prepare('
SELECT ProductName as Name,
ProductDescription as Description,
ProductDuration as Duration,
ProductPriceInclVat as PriceInclVat,
ProductPriceExclVat as PriceExclVat,
ProductVatType as VatType
FROM products WHERE VendorID = :VendorID
');
$array = array (
'VendorID' => $this->vendorID
);
$query->execute($array);
$product = $query->fetchall(PDO::FETCH_ASSOC);
The $product is now in the format you require.
After this you can avoid foreach loop in class invoice.
Other thing i noticed that you have made function product_array() which is not called,
so in index.php you are getting empty array (defined in class Invoice).
So in Invoice class it should be
$product = product_array()
and product_array function should return the value.

Magento how to get attribute value by option id?

So I have
$attribute_option_id = Mage::getResourceModel('catalog/product')
->getAttributeRawValue($productId, 'my_attribute', $storeId);
This gives me something like $attribute_option_id = 12345
Now how do I get the (text) value for this id?
Thanks
This should work.
$product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId);
$text = $product->getAttributeText('my_attribute');
[EDIT]
If you don't want to load the full product you can do a sneaky think. Impersonate a product.
So you get the option id like you already do
$attribute_option_id = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'my_attribute', $storeId);
Then create just an empty instance of the product and set some attributes to it.
$product = Mage::getModel('catalog/product')
->setStoreId($storeId)
->setData('my_attribute', $attribute_option_id);//the result from above
$text = $product->getAttributeText('my_attribute');
There is already a confirmation that this works. See it here.
Here is a method that is a different approach that works for me.
Assume Known: $_current_productid
$_Current_OptionId = Mage::getResourceModel('catalog/product')->getAttributeRawValue($_current_productid, $attribute_id,$store_id);
$_write = Mage::getSingleton('core/resource')->getConnection('core_write');
// now $_write is an instance of Zend_Db_Adapter_Abstract
$_readresult=$_write->query("SELECT value FROM eav_attribute_option_value WHERE option_id ='".$_Current_OptionId."'");
while ($row = $_readresult->fetch() ) {$_Current_OptionValue=$row['value'];}

How can I get all order tracking numbers in Magento?

I have order object and I need to get all shipped package tracking numbers. I tried following code, but it returns nothing even orders I tried had packages sent with tracking numbers.
$order = Mage::getModel('sales/order')->loadByIncrementId($orderid);
$trackingNumbers = $order->getTrackingNumbers();
Order object is not null, because it returns data for other methods like getShippingMethod etc.
If I click from "Shipping & Handling" link "Information Track Order", then popup shows all tracking numbers.
Try this..
$shipmentCollection = Mage::getResourceModel('sales/order_shipment_collection')
->setOrderFilter($order)
->load();
foreach ($shipmentCollection as $shipment){
$shipment->getAllTracks();
}
Edit:-
foreach ($shipmentCollection as $shipment){
foreach($shipment->getAllTracks() as $tracknum)
{
$tracknums[]=$tracknum->getNumber();
}
}
Now $tracknums will have the array of tracking numbers.
You can simply do this:
$orderIncrementId = 100000016; // YOUR ORDER INCREMENT ID;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$trackNumber = array();
foreach ($order->getTracksCollection() as $track){
$trackNumber[] = $track->getNumber();
}

Categories