/**
  * Execute the console command.
  *
  * @return mixed
  */
 public function fire()
 {
     $delivereds = Orderlog::where('appname', '=', Config::get('jex.tracker_app'))->where('status', '=', 'delivered')->orderBy('deliveryId', 'desc')->orderBy('created_at', 'desc')->groupBy('deliveryId')->get(array('deliveryId', 'merchantTransId', 'deliverytime'));
     /*
     $pendingan = Orderlog::where('appname','=',Config::get('jex.tracker_app'))
                     ->where('pendingCount','!=', strval(0))
                     ->orderBy('created_at','desc')
                     ->orderBy('deliveryId','desc')
                     ->groupBy('deliveryNote')
                     ->get(array( 'deliveryId', 'deliveryNote','status' ));
     */
     $count = 0;
     $data = '';
     foreach ($delivereds as $d) {
         $shipment = \Shipment::where('delivery_id', '=', $d->deliveryId)->first();
         if ($shipment) {
             if (date('Y-m-d', strtotime($d->deliverytime)) != date('Y-m-d', strtotime($shipment->deliverytime))) {
                 //print $d->deliveryId." ".$d->deliverytime." ".$shipment->deliverytime."\r\n";
                 $data .= '"' . $d->deliveryId . '","' . $d->merchantTransId . '","' . $d->deliverytime . '","' . $shipment->deliverytime . '"' . "\r\n";
                 $shipment->deliverytime = $d->deliverytime;
                 $shipment->save();
                 $count++;
             }
         }
     }
     print $data;
     //print "\r\ndifferent date : ".$count;
     /*
     $pc = array();
     foreach ($pendingan as $p) {
         if(isset($pc[$p->deliveryId])){
             if($p->status == 'pending'){
                 $pc[$p->deliveryId] = $pc[$p->deliveryId] + 1;
             }
         }else{
             $pc[$p->deliveryId] = 1;
         }
     }
     */
     //print_r($pc);
     /*
     foreach($pc as $d=>$c){
         //print $d->deliveryId." ".$d->deliverytime."\r\n";
         $count++;
         $shipment = \Shipment::where('delivery_id','=',$d)->first();
     
         if($shipment){
             $shipment->pending_count = $c;
             //$shipment->status = 'delivered';
             //$shipment->deliverytime = $d->deliverytime;
             //$shipment->save();
         }
     
         //print_r($d->toArray());
     }
     */
     print "\r\n" . $count;
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function postOrder()
 {
     $key = \Input::get('key');
     //$user = \Apiauth::user($key);
     $user = \Device::where('key', '=', $key)->first();
     if (!$user) {
         $actor = 'no id : no name';
         \Event::fire('log.api', array($this->controller_name, 'post', $actor, 'device not found, upload image failed'));
         return \Response::json(array('status' => 'ERR:NODEVICE', 'timestamp' => time(), 'message' => 'Device Unregistered'));
     }
     $json = \Input::all();
     $batch = \Input::get('batch');
     $result = array();
     foreach ($json as $j) {
         //$j['mtimestamp'] = new \MongoDate(time());
         if (is_array($j)) {
             $olog = new \Orderlog();
             foreach ($j as $k => $v) {
                 $olog->{$k} = $v;
             }
             $olog->mtimestamp = new \MongoDate(time());
             $r = $olog->save();
             $shipment = \Shipment::where('delivery_id', '=', $olog->deliveryId)->first();
             if ($shipment) {
                 //$shipment->status = $olog->status;
                 $shipment->courier_status = $olog->courierStatus;
                 $shipment->pending_count = new \MongoInt32($olog->pendingCount);
                 if ($olog->courierStatus == \Config::get('jayon.trans_cr_oncr') || $olog->courierStatus == \Config::get('jayon.trans_cr_oncr_partial')) {
                     $shipment->pickup_status = \Config::get('jayon.trans_status_pickup');
                 }
                 $shipment->save();
             }
             if ($r) {
                 $result[] = array('status' => 'OK', 'timestamp' => time(), 'message' => 'log inserted');
             } else {
                 $result[] = array('status' => 'NOK', 'timestamp' => time(), 'message' => 'insertion failed');
             }
         }
     }
     //print_r($result);
     //die();
     $actor = $user->identifier . ' : ' . $user->devname;
     \Event::fire('log.api', array($this->controller_name, 'get', $actor, 'sync scan log'));
     return Response::json($result);
 }
 public function checkPickedUp($delivery_id, $status_field, $status, $appname, $devicename)
 {
     $exist = \Orderlog::where('deliveryId', '=', $delivery_id)->where($status_field, '=', $status)->where('appname', '=', $appname)->where('pickupDevId', '!=', $devicename)->count();
     if ($exist > 0) {
         return true;
     } else {
         return false;
     }
 }