jeudi 17 mars 2016

Model Updated event sometimes not firing

I've been stuck with this problem for days now:

I have a service provider that monitors for the following Model events:

DeliveryOrder::created(function (DeliveryOrder $order) {
            $user = Sentinel::getUser();
            Activity::create([
                'company_id'  => $order->company_id,
                'user_id'     => $user->getUserId(),
                'label_type'  => 'success',
                'icon'        => 'fa fa-map-marker',
                'description' => "{$user->first_name} {$user->last_name} created a new <a href='{$order->getShowLink()}'>{$order->delivery_type}-type delivery order</a> Delivery OR number  <a href='{$order->getShowLink()}'>{$order->delivery_or_no}</a>",
            ]);

            Movement::create([
                'company_id'        => $order->company_id,
                'delivery_order_id' => $order->id,
                'label_type'        => 'success',
            ]);


        });

        DeliveryOrder::updated(function (DeliveryOrder $order) {
            Log::info($order);
            $movement = Movement::where('delivery_order_id', $order->id)->first();
            $movement->icon = ($order->getType() == 'incoming') ? 'fa fa-reply' : 'fa fa-share';
            $user = $order->user->getFullName();
            $operation = ($order->getType() == 'incoming') ? 'Incoming' : 'Outgoing';
            $items = $order->items()->where('isAdjustment', false)->get()->groupBy('attribute_values');

            $countedItems = $items->map(function ($item, $key) {
                $item->put('count', $item->count());
                return $item;
            });
            $content = "";

            foreach ($countedItems as $item) {

                $attributes = ($item[0]->attribute_values == "0") ? 'No Attributes' : $item[0]->attribute_values;
                $text = $item[0]->product->ItemCode . ' : ' . $item[0]->product->parent->ItemName . ' ' . $attributes . ' for ' . $item["count"];

                $content .= "<div class='desc'>{$text}</div>";
            }
            $movement->description = "{$user} created an {$operation} order for the following: {$content}";
            $movement->save();
         //   dd($movement);

        });

        Adjustment::created(function (Adjustment $adjustment) {
            Movement::create([
                'company_id'    => $adjustment->company_id,
                'adjustment_id' => $adjustment->id,
                'label_type'    => 'info',
            ]);
        });
        Adjustment::updated(function (Adjustment $adjustment) {

            Log::info($adjustment);
            $attributes = ($adjustment->items[0]->attribute_values == "0") ? 'No Attributes' : $adjustment->items[0]->attribute_values;
            $item = $adjustment->items[0]->product->ItemCode . ' : ' . $adjustment->items[0]->product->parent->ItemName . ' '
                .$attributes;

            $encoder = $adjustment->user->getFullName();
            $count = $adjustment->items->count();

            if ($adjustment->override()->count() > 0) {
                $override_movement = Movement::where('override_id', $adjustment->override->id)->first();
                $approver = $adjustment->override->user->getFullName();
                $override_movement->description = "{$approver} approved {$encoder}\'s request for negative inventory adjustment for {$item} by {$count}  ";
                $override_movement->save();
            }

            $operation = (($adjustment->items[0]->type == '+') ? 'Added' : 'Reduced');
            $movement = Movement::where('adjustment_id', $adjustment->id)->first();

            $movement->icon = ($adjustment->items[0]->type == '+') ? 'fa fa-plus' : 'fa fa-minus';
            $movement->description = "{$encoder} Adjusted a product: {$operation} {$item} by {$count} ";

            $movement->save();
          //  dd("hehe");


        });

        Override::created(function (Override $override) {
            Movement::create([
                'company_id'  => $override->company_id,
                'override_id' => $override->id,
                'label_type'  => 'danger',
                'icon'        => 'fa fa-exclamation',
            ]);
        });
~~~

the created event fires so I was able to create a Movement instance but the updated event often does not, which is quite a headache. I trigger the event via Model->touch() since I merely add relations to the created model, like so:

$delivery_order = DeliveryOrder::create($data);

foreach($items as $item)
{
   //some code creating an item here

  $delivery_order->items()->save($item);
}
$delivery_order->touch();
return redirect()->action('DeliveryOrdersController@show',$delivery_order->id)->with('status','success!');

Earlier I suspected that it's because of me using redirect()->back() (I have my reasons for using this) But even if I use redirect()->action() it still does not work at times.

Any suggestions on how I should go with this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire