<?php
class MainServerRepository extends MainServerEntityRepository {
    /*Obtener informacion de Shipment de MainServer para insertar y/o actualizar en tbl receiving local*/
    public function getDataShipment($numShipment){        
        try {
            $query = "SELECT * FROM stores_shipments WHERE num_shipment = '$numShipment'";
            $result = $this->query($query);
            
            if($result->num_rows > 0 ){
                $data = $this->resultToArray($result);
                $data = $data[0];

                $query = "SELECT * FROM stores_shipments_details WHERE id_shipment = '{$data['id']}' ";
                $result = $this->query($query);
                $details = $this->resultToArray($result);
                
                return array(
                    'response'=>true,
                    'data'=>$data,
                    'details'=>$details
                );              
            }       
            
            return array('response'=>null,'msg'=>"No se ha generado Envio #{$numShipment} en servidor principal.");         
            
        } catch (Exception $exc) {
            return array(
                'response'=>null,
                'msg'=>$exc->getMessage()
            ); 
        } 
    }
    
    /*Update tbl Shipment de MainServer desde sucursal*/
    public function updateShipment($options){
        $data = $options['data'];
        $details = $options['details'];
        $numShipment = $data['num_shipment'];
        
        $options = array(
            'receiving_date'=>$data['date'],
            'receiving_comments'=>$data['comments'],
            'status'=>$data['status'],
            'last_sinc'=>date('Y-m-d H:i:s')
        );
        
        try{
            $this->updateString($options, " num_shipment = '{$numShipment}'", 'stores_shipments');
            
            foreach($details as $detail){
                $options = array('received'=>$detail['received']);
                $this->updateString(
                        $options, 
                        " s.id = d.id_shipment AND s.num_shipment = '{$numShipment}' AND d.id_product = '{$detail['id_product']}'", 
                        'stores_shipments s, stores_shipments_details d'
                                );
            }
            
        } catch (Exception $ex) {
            return true;
        }
        return true;
    }
    
    public function saveImageForSR(array $images){
         foreach($images as $image){
            $this->save($image, 'images');
        }
        /*
        $query = "INSERT INTO images (req_number,image,type,size) VALUES ";
        foreach($images as $image){
            $query .= "('{$image['req_number']}','{$image['image']}','{$image['type']}','{$image['size']}'),";
        }
        
        $query = trim($query,',');
        $this->query($query);
         */
         
    }
}

