<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace factory;

/**
 * Description of class
 *
 * @author Project-Pro - Jacek
 */
class Crm
{
  public static $source = [ 0 => 'ceidg', 1 => 'kontakt własny', 2 => 'BNI' ];
  public static $status = [ 0 => 'kontakt pozyskany', 1 => 'oferta wysłana', 2 => 'follow up', 3 => 'klient niezainteresowany' ];

  static public function settings()
  {
    global $mdb;

    $results = $mdb -> select( 'settings', '*' );
    foreach ( $results as $result )
    {
      $settings[ $result['name'] ] = $result['value'];
    }
    return $settings;
  }

  static public function get_client_name( int $client_id ) {
    global $mdb;
    return $mdb -> get( 'crm_client', 'firm', [ 'id' => $client_id ] );
  }

  static public function get_client_list() {
    global $mdb;
    return $mdb -> select( 'crm_client', '*', [ 'ORDER' => [ 'firm' => 'ASC' ] ] );
  }

  static public function client_delete( int $client_id )
  {
    global $mdb;
    return $mdb -> delete( 'crm_client', [ 'id' => $client_id ] );
  }

  public static function client_details( int $client_id )
  {
    return \R::load( 'crm_client', $client_id );
  }

  public static function client_save( $client_id, $firm, $emails, $phones, $notes )
  {
    $client = \R::load( 'crm_client', $client_id );
    if ( !$client )
    {
      $client = \R::xdispense( 'crm_client' );
      $client -> date_add = date( 'Y-m-d H:i:s' );
    }
    $client -> firm = $firm;
    $client -> emails = $emails;
    $client -> phones = $phones;
    $client -> notes = $notes;
    $client -> date_update = date( 'Y-m-d H:i:s' );
    return \R::store( $client );
  }
}