<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Billing_setting_model extends App_Model
{
  public $table_name = '';

  public function __construct()
  {
    parent::__construct();
    $this->table_name = db_prefix().'billing_settings';
  }

  public function get()
  {
    //$this->db->where('id', 1);

    $setting = [];
    /*$fields = ['view_phone','view_fax','view_email','view_website','view_logo','view_background','','','','','','','','','','',''];
    if(!empty($fields)){ foreach($fields as $field){
      $setting['invoice_'.$field] = get_option('invoice_'.$field);
    } }*/

    $setting = get_option('invoice_settings');
    $setting = json_decode($setting);

     //$this->db->get($this->table_name)->row();

    return $setting;
  }


  public function update($data = []) {
    if(!isset($data['view_phone'])){ $data['view_phone'] = 0; }
    if(!isset($data['view_fax'])){ $data['view_fax'] = 0; }
    if(!isset($data['view_email'])){ $data['view_email'] = 0; }
    if(!isset($data['view_website'])){ $data['view_website'] = 0; }
    if(!isset($data['view_logo'])){ $data['view_logo'] = 0; }
    if(!isset($data['view_background'])){ $data['view_background'] = 0; }
    if(!isset($data['view_signature'])){ $data['view_signature'] = 0; }
    if(!isset($data['view_send_date'])){ $data['view_send_date'] = 0; }
    if(!isset($data['view_client_code'])){ $data['view_client_code'] = 0; }
    if(!isset($data['due_date'])){ $data['due_date'] = 0; }
    if(!isset($data['view_due_date'])){ $data['view_due_date'] = 0; }
    if(!isset($data['currency_price'])){ $data['currency_price'] = 0; }
    if(!isset($data['use_product_code'])){ $data['use_product_code'] = 0; }
    if(!isset($data['view_product_code'])){ $data['view_product_code'] = 0; }
    if(!isset($data['use_description'])){ $data['use_description'] = 0; }
    if(!isset($data['calculate_total'])){ $data['calculate_total'] = 0; }
    if(!isset($data['use_special_list_price'])){ $data['use_special_list_price'] = 0; }

    /*foreach($data as $field=>$value){
      update_option('invoice_'.$field,$value);
    }*/

    update_option('invoice_settings',json_encode($data));

    //$this->db->where('id', 1);
    //$this->db->update($this->table_name, $data);

    log_activity('Invoice general settings Updated');
    return $this->get();
  }

}

?>
