Filtering invoice collection by created_at in magento - magento

I'm trying to find a way to filter the results of getInvoiceCollection in the function below:
// "eachInvoice" is each of the Invoice object of the order "$orders"
if ($order->hasInvoices()) {
foreach ($order->getInvoiceCollection() as $eachInvoice) {
$invoiceIncrementId = $eachInvoice->getIncrementId();
}
}
Can adding the filter below to $order->getInvoiceCollection()
$order->getInvoiceCollection()
->addAttributeToFilter('created_at', array(
'from' => $from_date,
'to' => $today,
'date' => true,));
So the entire function would be, is the correct way to do this, it doesn't seem like its right.
// "eachInvoice" is each of the Invoice object of the order "$orders"
if ($order->hasInvoices()) {
foreach ($order->getInvoiceCollection()$order->getInvoiceCollection()
->addAttributeToFilter('created_at', array( 'from' => $from_date,
'to' => $today, 'date' => true,)) as $eachInvoice) {
$invoiceIncrementId = $eachInvoice->getIncrementId();
}
}
Whats the "correct way" to go about doing this?

To get the increment ids as is shown in your sample code there is no need to loop:
$invoiceCollection = $order->getInvoiceCollection();
$invoiceCollection
->addAttributeToFilter('created_at', array(
'from' => $from,
'to' => $to,
));
$incrementIds = $invoiceCollection->getColumnValues('increment_id');

Related

Undefinde offset:1 when importing laravel excel

this my code cause the trouble,
$cust = Customer::where('name', '=', $data[$i][0]['customer_name'])
->pluck('customer_id')[0];
this one for get customer id when i do store to sales order
$sales = array(
'customer_id' => Customer::where('name', '=', $data[$i][0]['customer_name'])->pluck('customer_id')[0],
'logistics_id' => Logistic::where('logistics_name', '=', $data[$i][0]['logistics'])->pluck('logistics_id')[0],
'subtotal' => $data[$i][0]['subtotal_rp'],
'shipping_cost' => $data[$i][0]['shipping_cost_rp'],
'discount_code' => 0,
'date_of_sales' => $data[$i][0]['date'],
'grand_total' => $data[$i][0]['grand_total_rp'],
'tax' => $data[$i][0]['tax_rp'],
'status' => $data[$i][0]['status'],
'discount_amount' => $data[$i][0]['discount_amount_rp']
);
$store_so = SalesOrder::create($sales);
but, when i do dd(), i get the right data
First of all, you need to check if the $data variable returns the data as you expect.
dd($data);
Next, you need to check that the $data array has the number of elements according to $total_data.
dd(count($data) == $total_data));
So basically, you just need to give condition or try-catch (recommended) :
if (isset($data[$i][0])) {
$customer = Customer::where('name', $data[$i][0]['customer_name'])->first();
$logistic = Logistic::where('logistics_name', $data[$i][0]['logistics'])->first();
if(!$customer){
dd('No customer found!');
}
if(!$logistic){
dd('No logistic found!');
}
$sales = [
'customer_id' => $customer->customer_id,
'logistics_id' => $logistic->logistics_id,
'subtotal' => $data[$i][0]['subtotal_rp'],
'shipping_cost' => $data[$i][0]['shipping_cost_rp'],
'discount_code' => 0,
'date_of_sales' => $data[$i][0]['date'],
'grand_total' => $data[$i][0]['grand_total_rp'],
'tax' => $data[$i][0]['tax_rp'],
'status' => $data[$i][0]['status'],
'discount_amount' => $data[$i][0]['discount_amount_rp'],
];
$store_so = SalesOrder::create($sales);
}
else{
dd('No $data[$i][0] found!');
}
PS : I recommend using the first() method instead of pluck('customer_id')[0].
It seems you need to get a customer_id from a customer_name.
Try to make everything simple:
$sales = array(
'customer_id' => Customer::where('name', $data[$i][0]['customer_name'])->first()->id,
...
);

How to add an array within a foreach in laravel controller?

I am trying to insert data from a form which i get it in the form of an array and i am using a foreach to insert the data each in to the database. amongst these data i have another array that i need to insert it in the form of an array.
The Amendities should be saved in the form of an array! i tried using implode but didn't quite get the results i expected.
foreach ($request->room_type as $item => $v) {
$data2 = array(
'room_type' => $request->room_type[$item],
'no_of_pax' => $request->no_of_pax[$item],
'no_of_pax_children' => $request->no_of_pax_children[$item],
'cost_per_adult' => $request->cost_per_adult[$item],
'cost_per_child' => $request->cost_per_child[$item],
'room_description' => $request->room_description[$item],
'amendities' => implode($request->amendities),
'hotel_id' => $hotel->id,
);
dd($data2);
Rooms::insert($data2);
}
impoding Amendities array saves the data as a string but i am trying to get the values as an array.
u need to prepare a datasets to insert multiple record
so u can try like this
$data2 = [];
foreach ($request->room_type as $item => $v) {
$opt = array(
'room_type' => $request->room_type[$item],
'no_of_pax' => $request->no_of_pax[$item],
'no_of_pax_children' => $request->no_of_pax_children[$item],
'cost_per_adult' => $request->cost_per_adult[$item],
'cost_per_child' => $request->cost_per_child[$item],
'room_description' => $request->room_description[$item],
'amendities' => json_encode($request->amendities),
'hotel_id' => $hotel->id,
);
array_push($data2,$opt);
}
Rooms::insert($data2);
Thank you for the help guys this is final solution which worked for as per your help
foreach ($request->room_type as $item => $v) {
$opt = array(
'room_type' => $request->room_type[$item],
'no_of_pax' => $request->no_of_pax[$item],
'no_of_pax_children' => $request->no_of_pax_children[$item],
'cost_per_adult' => $request->cost_per_adult[$item],
'cost_per_child' => $request->cost_per_child[$item],
'room_description' => $request->room_description[$item],
'amendities' => json_encode($request->amendities),
'hotel_id' => $hotel->id,
);
array_push($data2, $opt);
}
Rooms::insert($data2);
Thank you for your help all of you!

Magento invoice grid filter by joined attribute value

I'm using the following code to include a custom attribute, titled "sales rep" within the Magento invoice grid:
protected function _prepareCollection() {
$sales_rep = Mage::getResourceSingleton('customer/customer')->getAttribute('sales_rep');
$collection = Mage::getResourceModel('sales/order_invoice_grid_collection');
$collection->join('invoice', 'main_table.entity_id = invoice.entity_id',array('order_id as order_id'));
$collection->join('order', 'invoice.order_id = order.entity_id',array('customer_id as customer_id'));
$collection->getSelect()->joinLeft(
array('customer_sales_rep_table' => Mage::getSingleton('core/resource')->getTableName($sales_rep->getBackend()->getTable())),
'customer_sales_rep_table.entity_id = order.customer_id
AND customer_sales_rep_table.attribute_id = '.(int) $sales_rep->getAttributeId() . '
',
array('sales_rep'=>'value')
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
...
$this->addColumn('sales_rep', array(
'header' => Mage::helper('sales')->__('Sales Rep'),
'index' => 'sales_rep',
'filter' => false
));
This is working perfectly, just as long as the "addColumn" property "filter" is set to "false".
How would I go about allowing users to filter by this joined attribute?
You should add your column like this:
$this->addColumn('sales_rep', array(
'header' => Mage::helper('sales')->__('Sales Rep'),
'index' => 'sales_rep',
'filter_index' => 'customer_sales_rep_table.value'
));

Laravel 4 - Return the id of the current insert

I have the following query
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
return $results;
}
How would i return the id of the row just inserted?
Cheers,
Instead of doing a raw query, why not create a model...
Call it Conversation, or whatever...
And then you can just do....
$result = Conversation::create(array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now ))->id;
Which will return an id...
Or if you're using Laravel 4, you can use the insertGetId method...In Laravel 3 its insert_get_id() I believe
$results = DB::table('pm_conversations')->insertGetId(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
This method requires that the id of the table be auto-incrementing, so watch out for that...
The last method, is that you can just return the last inserted mysql object....
Like so...
$result = DB::connection('mysql')->pdo->lastInsertId();
So if you choose that last road...
It'll go...
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
$theid= DB::connection('mysql')->pdo->lastInsertId();
return $theid;
}
I would personally choose the first method of creating an actual model. That way you can actually have objects of the item in question.
Then instead of creating a model and just save()....you calll YourModel::create() and that will return the id of the latest model creation
You can use DB::getPdo()->lastInsertId().
Using Eloquent you can do:
$new = Conversation();
$new->currentId = $currentId;
$new->toUserId = $toUserId;
$new->ip = Request::getClientIp();
$new->time = $now;
$new->save();
$the_id = $new->id; //the id of created row
The way I made it work was I ran an insert statement, then I returned the inserted row ID (This is from a self-learning project to for invoicing):
WorkOrder::create(array(
'cust_id' => $c_id,
'date' => Input::get('date'),
'invoice' => Input::get('invoice'),
'qty' => Input::get('qty'),
'description' => Input::get('description'),
'unit_price' => Input::get('unit_price'),
'line_total' => Input::get('line_total'),
'notes' => Input::get('notes'),
'total' => Input::get('total')
));
$w_id = WorkOrder::where('cust_id', '=', $c_id)->pluck('w_order_id');
return $w_id;

Sending a campaign to a segment

Anyone know how to get a campaign to send to a segment? This code isn't working. It will send an email to all people in the campaign. It will not use the segment. (This is some more text so I can get it to pass the validation of SO.)
My code:
//get member list
$memberArray = $api->listMembers($inStockListId);
foreach ($memberArray['data'] as $member) {
$memberInfo = $api->listMemberInfo($inStockListId, $member['email']);
$_productId = $memberInfo['data'][0]['merges']['PRODUCTID'];
$productId='34';
if ($productId == $_productId) {
array_push($emailArray, $member['email']);
}
}
//create new segment for campaign
$listStaticSegmentId = $api->listStaticSegmentAdd($inStockListId, 'inStockStaticSegment');
//add members to segment
$val = $api->listStaticSegmentMembersAdd($inStockListId, $listStaticSegmentId, $emailArray);
$conditions = array();
$conditions[] = array(
'field' => 'email',
'op' => 'like',
'value' => '%'
);
$segment_options = array(
'match' => 'all',
'conditions' => $conditions
);
$type = 'regular';
$options = array(
'template_id' => $campaignTemplateId,
'list_id' => $inStockListId,
'subject' => 'In-Stock Notification',
'from_email' => 'from#email.com',
'from_name' => 'My From Name'
);
$content = array(
'html_main' => 'some pretty html content',
'html_sidecolumn' => 'this goes in a side column',
'html_header' => 'this gets placed in the header',
'html_footer' => 'the footer with an *|UNSUB|* message',
'text' => 'text content text content *|UNSUB|*'
);
$newCampaignId = $api->campaignCreate($type, $options, $content, $segment_options);
I figured it out. Essentially, here is the flow. If you want detailed code, send me a message and I'll be glad to help.
$api = new MCAPI($this->_apiKey);
$api->listMemberInfo($this->_listId, $member['email']);
$api->listUpdateMember($this->_listId, $member['email'], $mergeVars);
$api->listStaticSegmentDel($this->_listId, $segment['id']);
$api->listStaticSegmentAdd($this->_listId, $segmentName);
$api->listStaticSegmentMembersAdd($this->_listId, $segmentId, $emailArray);
$api->campaignCreate($type, $options, $content, $segment_options);
//$api->campaignSendTest($newCampaignId, array($member['email']));
$api->campaignSendNow($newCampaignId);
Note, this is done via the MailChimp PHP API.

Resources