Netsuite - get custom record with php toolkit - php

I've been handed a project to complete and the clients have asked for a field from a customrecord attached to each customer to appear on their website. We're integrating with Netsuite on login, and saving their data in our database so we don't have to keep accessing Netsuite (very slow).
On login, we access Netsuite to do a SearchMultiSelectCustomField and find the customer's company, and then do a CustomRecordSearchBasic and use their company ID to get a list of items they have access to.
We loop over each of those items, and then loop over their custom fields. One of the fields has a typeId of -10, which means we do an ItemSearchBasic to get this item's record and the item's custom fields, saving the internalId of this item.
At the end of this loop, we have an array of item IDs that a company is linked to. We also have the company ID (custrecord_nn_item_customer) and the Item ID (custrecord_nn_item_customer_list).
I need to perform a get request on a custom record to check if that customer has been approved for that item.
The customrecord's ID is 'customrecord_custitem', and internal Id is '1'.
The record has 3 fields (although only 2 show up for the customer's Netsuite Record page):
custrecord_lookup_item - this is the Item record code (custrecord_nn_item_customer_list from above)
custrecord_custitem_code is the code I need
My question (after all that) is does anyone have any examples or can point me in the right direction on how I can access a customrecord attached to a customer? I think all of the necessary information is provided, but I've never used Netsuite before or the PHP toolkit.
$this->depends('netsuite');
$this->netsuite->start();
// get the "customer" (aka company) that the user's contact record belongs to
$companySearch = $this->netsuite->complexObject('SearchMultiSelectCustomField')
->setFields(array(
'searchValue' => new nsListOrRecordRef(array('internalId' => $companyId)),
'internalId' => 'custrecord_nn_item_customer',
'operator' => 'anyOf'
));
// Fetch items that the user's company has access to
$search = $this->netsuite->complexObject('CustomRecordSearchBasic')
->setFields(array(
'recType' => new nsCustomRecordRef(array(
'internalId' => 260,
'type' => 'customRecord')
),
'customFieldList' => array($companySearch)
));
$response = $this->netsuite->client->search($search);
// loop over the items
foreach($this->netsuite->complexToSimple($response->recordList) as $record){
//var_dump($record);
$processor = null;
$this_item = '';
$this_person = '';
// foreach custom field (all the fields we're interested in, common name etc. are custom)
foreach($record['customFieldList']['customField'] as $customField){
$processor = $customField['value'];
$id = $processor['internalId'];
$typeId = $processor['typeId'];
if($customField['internalId']=='custrecord_nn_item_customer'){
$this_person = $id;
}elseif($customField['internalId']=='custrecord_nn_item_customer_list'){
$this_item = $id;
}
// a typeId of -10 = an Inventory Item
if($typeId == -10){
// do an ItemSearchBasic to fetch the item with it's custom fields
$itemSearch = $this->netsuite->complexObject('ItemSearchBasic')
->setFields(array(
'internalId' => array(
'operator' => 'anyOf',
'searchValue' => array('type' => 'inventoryItem', 'internalId' => $id)
)
));
$itemSearch = $this->netsuite->client->search($itemSearch);
// foreach custom item field
if($v=#$this->netsuite->complexToSimple($itemSearch->recordList)){
foreach($v as $itemRecord){
//var_dump($itemRecord);
$item = array('id' => $itemRecord['internalId']);
$items[] = $item;
}
}
}
}
}
It is inside the foreach loop that I need to get the customrecord field for the company ID and the current iteration of the item ID.

Set up a saved search with the results columns you need. Don't worry about filtering by customer.
Call the search from your code, and dynamically filter for the current customer.
Your code should be about 5-10 lines long to get that done, and should be super quick.

Related

How to delete item ID from array used as session shopping cart

I have a SESSION['cart'] with ID numbers only. I have a form passing the ID with a remove button. Once passed to my controller, I cannot figure out how to write the code that uses the ID ($_POST['id']) to delete the item from the SESSION['cart'].
I can loop through and display the array contents, but I cannot figure out how to delete based on ID passed from the form.
How do I loop through the SESSION['cart'] array to find a match with the ID passed from my delete form, and then delete that ID? I know that unset($_SESSION['cart'][X] deletes the ID at index X, but I cannot figure out how to loop through all the elements to find a match.
I have read a number of related issues in this forum but have been unable to apply any of those solutions to resolve this challenge. Any assistance is appreciated.
The way you have your values ($products = array(3,7,99,152)) isn't a very good method. Every time you want to perform an action, you have to loop through the array, you don't want that. Apart from that, how do you store quantity? Or variations like e.g. size or color?
if your structure is $array[ ID_OF_PRODUCT ], you can simply do this:
unset( $_SESSION['cart'][$_POST['id']] ); // Instant access via the key!
This should be the method to use. This allows you to create an array like this, with advanced info, but with easy access (42/63 are example id's)
$_SESSION['cart']['products'][42] = array(
'quantity' = 11,
'size' = 'large',
'color' = 'blue'
);
$_SESSION['cart']['products'][63] = array(
'quantity' = 9,
'size' = 'small',
'color' = 'red'
);
This way you can access a lot of info with the product ID, and now also see which size and color (both just examples) the user selected. You may not have need for this now, but you will further down the road :)
As you might see, you can easily do stuff with the item:
isset($_SESSION['cart'][$_POST['id']]); // check if the product exists
unset($_SESSION['cart'][$_POST['id']]); // remove the product
echo $_SESSION['cart'][$_POST['id']]['quantity']; // get the quantity.
Not a loop in the code. You should only use loops when you have to, try to somewhat avoid them because often their slow. Say you have an extreme case of 1000 items in your shop, and you need to delete no999... That'll take a noticable moment.
Here is the code to do it right:
$id = $_POST['id'];
$items = $_SESSION["cart"];
if(($key = array_search($id, $items)) !== false) {
unset($items[$key]);
}
$_SESSION["cart"] = array_values($items);
Advice
Beside item ID, you can also sve item count in SESSION array because user can add several times same item into cart. In that case your $_SESSION["card"] should be structured like:
array(
'1'=>12,//Item with ID=1 is added 12 times in shopping cart
'17'=>2,//Item with ID=17 is added 2 times in shopping cart etc.
'32'=>12,
)

how tocreate records in sugarcrmCE 6.5 custom subpanel using rest api

i have created record using REST API for account module, now i want to add all related record for the sub-panel like payment information of an account using REST API which is provided by sugarCRM, and want to have one to many relationship.
For related record sugarcrm basically maintaining separate table
for example
For product module and inventory module ( one - many relationship ) it will maintain table some thing like below .
aos_products_i_inventory_1_c
so if we want to add related records , we can map the id of product and inventory in this table .
so , basically you need to have a RestApi Call for inserting ( mapping ) account id and payment id in your relation ship table .
you can search for which method to use from documentation
Rest Methods call documentation
And if the no method suites your requirement then you can create you own methods for RestApi according to your needs , under Sugarcrm/service/core/SugarWebServiceImpl.php .
consider this example of custom method for Api which will return the product price from ( product module ) and available count from ( inventory module) based on product.
function get_inventory_count_Productprice($session, $model_number){
$GLOBALS['log']->info('Begin: SugarWebServiceImpl->get_count_relation');
$error = new SoapError();
// check the access for particular sesstion
if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', '', '', '', $error)) {
$error->set_error('invalid_login');
$GLOBALS['log']->info('End: SugarWebServiceImpl->get_available_modules');
return;
} // if
$qur = "SELECT price,id FROM aos_products where part_number='".$model_number."' AND deleted=0 LIMIT 0,1";
$res = $GLOBALS['db']->query($qur);
$row = $GLOBALS['db']->fetchByAssoc($res);
$focus = BeanFactory::getBean('AOS_Products', $row['id']); //Retrieve bean
$inventory = $focus->get_linked_beans('aos_products_i_inventory_1','i_inventory'); // loading relation ship
$count = 0 ;
foreach ($inventory as $product) {
if($product->status=='available'){
$count++;
}
}
$GLOBALS['log']->info('End: SugarWebServiceImpl->get_count_relation');
return array(
'price' => $row['price'],
'count' => $count,
);
}
i hope this will help you .

Adding new column to ordered products in osCommerce

I have a store using osCommerce with the Dynamo one page checkout addon. I added a new column called "drop_ship_id" to the orders_products table and products table. Under the checkout_process.php file is where I believe the products that the user has ordered, are entered into the orders_products table in the database. I'm trying to pull the drop_ship_id field from each ordered product, and enter it into the orders_products table where I can call on it later on in the checkout process. Here is my current code snip:
// initialized for the email confirmation
$products_ordered = '';
$subtotal = 0;
$total_tax = 0;
for ($i=0; $i<sizeof($order->products); $i++) {
if (!in_array($payment, $suspended_payment)) {
$checkout->reduce_stock($order->products[$i]);
}
$sql_data_array = array('orders_id' => $insert_id,
'products_id' => tep_get_prid($order->products[$i]['id']),
'products_model' => $order->products[$i]['model'],
'products_name' => $order->products[$i]['name'],
'products_price' => $order->products[$i]['price'],
'final_price' => $order->products[$i]['final_price'],
'products_tax' => $order->products[$i]['tax'],
'products_quantity' => $order->products[$i]['qty'],
'drop_ship_id' => $order->products[$i]['drop_ship_id']);
tep_db_perform($database['ORDERS_PRODUCTS'], $sql_data_array);
$order_products_id = tep_db_insert_id();
Not sure if this is where it belongs, but I'm unable to get it to transfer the drop_ship_id field with this code. Does anyone know a good way of getting this to work?
You can use the error log to try and dump some information to the logs:
error_log("product id: " . $order->products[$i]['products_id'] . ", dropship order id: " . $order->products[$i]['drop_ship_id']);
As to why the value is not being transferred, I would check a few things in includes/classes/order.php - when creating a new order, it will copy the values from the cart's list of products. So you should ensure either:
the products in the cart contain this field (i.e. this field should be one of the select values in the SQL when the product is retrieved from the database and put into the cart).
you add the field in afterwards by explicitly writing the value within order.php.
And later on, when loading a saved order from the database, you should ensure you add the drop_ship_id to your SQL query.

Codeigniter cart can't insert data

I'm trying to add some data to a codeigniter (HMVC codeigniter) chopping cart and display it, i'm using this method in the main cart controller:
function add_to_cart(){
$this->load->library('cart');
// Get data
$userID = $this->input->post('userID');
$eventID = $this->input->post('eventID');
$tickedID = $this->input->post('tickedID');
// Get ticket data
$this->load->module('ticket');
$ticket_query = $this->ticket->get_where($tickedID);
//echo $this->session->all_userdata();
foreach($ticket_query->result() as $ticket_data){
$ticketPrice = $ticket_data->price;
$ticketCategory = $ticket_data->category;
}
//echo 'tickedID: '.$tickedID.' price: '.$ticketPrice.' category: '.$ticketCategory;
// Add item to cart
$data_items = array(
'id' => $tickedID,
'qty' => 1,
'price' => $ticketPrice,
'category' => $ticketCategory,
'options' => array()
);
$this->cart->insert($data_items);
$cart = $this->cart->contents();
echo '<pre>';
echo print_r($cart);
echo '</pre>';
}
Basically i'm getting the userID, eventID and tickedID variables from the session, then I run a query to get the ticked with the specific id. I run through the results of the query and get the $thicketPrice and $ticketCategory variables from it. Then I attempt to set the variables in $data_items to insert in the cart itself. FInally I attempt to echo the contents of the care and all I get is an empty array.
The session, database and cart libraries are all autoloaded and the sessions are using the database, they have the ci_sessions table. THe sessions also have an ecrypted key, what is wrong?
Some attention for successful cart insert:
'price' > 0
'name' (or similar field) better not in unicode
You need a name index as it's mandatory.
id - Each product in your store must have a unique identifier. Typically this will be an "sku" or other such identifier.
qty - The quantity being purchased.
price - The price of the item.
name - The name of the item.
options - Any additional attributes that are needed to identify the product. These must be passed via an array.
Important: The first four array indexes above (id, qty, price, and name) are required. If you omit any of them the data will not be saved to the cart. The fifth index (options) is optional. It is intended to be used in cases where your product has options associated with it. Use an array for options, as shown above.
From http://ellislab.com/codeigniter/user-guide/libraries/cart.html
So, something like this then:
$data_items = array(
'id' => $tickedID,
'qty' => 1,
'price' => $ticketPrice,
'name' => $someName,
'options' => array('category'=>$ticketCategory)
);

post rating for an individual item in a list using codeigniter

I’m having a heck of a time figuring out how to post a rating for an individual item in a list of items,
this code let’s me rate multiple items, but not single items:
for($i=0;$i<2;$i++){
$doc_item_id = $_POST['item_id0'][$i];
$doc_rating = $_POST['document_rating'][$i];
$it_rt = array(
'item_id' => $doc_item_id,
'rating' => $doc_rating,
);
$this->purchases_model->update_document($it_rt);
}
whereas this code let’s me rate only the first item (or last item depending on where i put the "break;"):
foreach($_POST['item_id0'] as $doc_item_id){
foreach($_POST['document_rating'] as $doc_rating){
}
break;
}
$it_rt = array(
'item_id' => $doc_item_id,
'rating' => $doc_rating,
);
$this->purchases_model->update_document($it_rt);
any thoughts on how to correct either of these such that the user could rate the individual item of their choosing would be greatly appreciated,
If the user is supposed to choose the item to rate (instead of rating all items at the same time), you should allow him to do so (showing only one item, let him select one using radio buttons...), and then you should be able, by PHP side, to retrieve the index of the item to modify.
Finally, in order to modify only one item, your code should look like (PHP side, you will certainly have to update your HTML form as well)
$i = $_POST['item_index']; // Here I'm supposing that you have added radio buttons
// named 'item_index' to allow user to choose the item to rate
$doc_item_id = $_POST['item_id0'][$i];
$doc_rating = $_POST['document_rating'][$i] ;
$it_rt = array(
'item_id'=> $doc_item_id,
'rating' => $doc_rating,
);
$this->purchases_model->update_document($it_rt);
In fact it would be nearly your original code without the for loop.
Looping through the entire list just to limit the item you want, is kinda bad.
Here's an example on how to do it using some array functions:
$last=true; // false for first
if($last){
$id=end($_POST['item_id0']);
}else{
$id=reset($_POST['item_id0']);
}
// alternative: $id=($last)?end($_POST['item_id0']):reset($_POST['item_id0']);
// test id
if($id===false){
// no item was supplied
}
if(!isset($_POST['document_rating'][$id])){
// somehow, the item id doesn't have a matching document rating
}
// everything is okay!
$doc_item_id = $_POST['item_id0'][$id];
$doc_rating = $_POST['document_rating'][$id];
$it_rt = array(
'item_id' => $doc_item_id,
'rating' => $doc_rating,
);
$this->purchases_model->update_document($it_rt);

Categories