<?php

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

	if (!$CI->db->table_exists(db_prefix() . 'billing_series')) {
		$CI->db->query('CREATE TABLE `' . db_prefix() . "billing_series` (
		`id` int(11) NOT NULL,
		`type` varchar(64) NOT NULL,
		`name` varchar(64) NOT NULL,
		`start_number` int(11) NOT NULL,
		`current_number` int(11) NOT NULL,
		`description` varchar(255) DEFAULT NULL,
		`default` tinyint(1) DEFAULT 0
		) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

		$CI->db->query('ALTER TABLE `' . db_prefix() . 'billing_series`
  	ADD PRIMARY KEY (`id`);');

		$CI->db->query('ALTER TABLE `' . db_prefix() . 'billing_series`
  	MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
	}

	if (!$CI->db->table_exists(db_prefix() . 'billing_bank')) {
		$CI->db->query('CREATE TABLE `' . db_prefix() . "billing_bank` (
		`id` int(11) NOT NULL,
		`iban` varchar(64) NOT NULL,
		`name` varchar(64) NOT NULL,
		`currency_id` int(11) NOT NULL,
		`swift` varchar(64) NOT NULL,
		`description` varchar(255) DEFAULT NULL,
		`status` tinyint(1) DEFAULT 1
		) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

		$CI->db->query('ALTER TABLE `' . db_prefix() . 'billing_bank`
  	ADD PRIMARY KEY (`id`);');

		$CI->db->query('ALTER TABLE `' . db_prefix() . 'billing_bank`
  	MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
	}

if (!$CI->db->table_exists(db_prefix() . 'billing_template')) {
  $CI->db->query('CREATE TABLE `' . db_prefix() . "billing_template` (
		`id` int(11) NOT NULL,
		`template` tinyint(2) DEFAULT 1,
		`color` varchar(10) NOT NULL,
		`format` varchar(10) NOT NULL,
		`logo` varchar(255) DEFAULT NULL,
		`background` varchar(255) DEFAULT NULL,
		`stamp` varchar(255) DEFAULT NULL,
		`is_stamp_text` varchar(255) DEFAULT NULL,
		`billing_series_id` int(11) NOT NULL
		) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

  $CI->db->query('ALTER TABLE `' . db_prefix() . 'billing_template`
  	ADD PRIMARY KEY (`id`);');

  $CI->db->query('ALTER TABLE `' . db_prefix() . 'billing_template`
  	MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1');
}

if (!$CI->db->table_exists(db_prefix() . 'billing_settings')) {
  $CI->db->query('CREATE TABLE `' . db_prefix() . "billing_settings` (
		`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
		`view_phone` tinyint(2) DEFAULT 1,
		`view_fax` tinyint(2) DEFAULT 0,
		`view_email` tinyint(2) DEFAULT 1,
		`view_website` tinyint(2) DEFAULT 0,
		`view_logo` tinyint(2) DEFAULT 1,
		`view_background` tinyint(2) DEFAULT 1,
		`view_signature` tinyint(2) DEFAULT 0,
		`view_send_date` tinyint(2) DEFAULT 0,
		`view_client_code` tinyint(2) DEFAULT 0,
		`due_date` tinyint(3) DEFAULT NULL,
		`currency_price` tinyint(2) DEFAULT 0,
		`use_product_code` tinyint(2) DEFAULT 0,
		`view_product_code` tinyint(2) DEFAULT 0,
		`use_description` tinyint(2) DEFAULT 1,
		`calculate_total` tinyint(2) DEFAULT 1,
		`use_special_list_price` tinyint(2) DEFAULT 0,
    primary key (id)
		) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

    /*$CI->db->query("INSERT INTO `".db_prefix()."billing_settings` (`view_phone`, `view_fax`, `view_email`, `view_website`, `view_logo`, `view_background`, `view_signature`, `view_send_date`, `view_client_code`, `due_date`, `currency_price`, `use_product_code`, `view_product_code`, `use_description`, `calculate_total`, `use_special_list_price`)
      VALUES
      (1, 0, 1, 0, 1, 1, 0, 0, 0, NULL, 0, 0, 0, 1, 1, 0);"
    );*/
}
if (!$CI->db->table_exists(db_prefix() . 'billing_notifications')) {
  $CI->db->query('CREATE TABLE `' . db_prefix() . "billing_notifications` (
		`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
		`active` tinyint(2) DEFAULT 0,
		`days` tinyint(4) DEFAULT 1,
		`type` enum('before', 'after') DEFAULT 'before',
		`subject` varchar(255) DEFAULT NULL,
		`message` varchar(255) DEFAULT NULL,
		`fullname` varchar(255) DEFAULT NULL,
		`cc` varchar(255) DEFAULT NULL,
		`bcc` varchar(255) DEFAULT NULL,
		`language` varchar(255) DEFAULT 'romanian',
    primary key (id)
		) ENGINE=InnoDB DEFAULT CHARSET=" . $CI->db->char_set . ';');

  /*$CI->load->model('payment_modes_model');
  $CI->load->model('settings_model');

  $data = [
    'settings' => [
      'billing_email_subject' => "Documentul #serie_numar#",
      'billing_email_message' => "Buna ziua,\n
Va anuntam ca s-a emis #tip_document# #serie_numar#. Documentul poate fi vizualizat dand click pe link-ul de mai jos.\n

Va multumim pentru colaborare!\n\n",
    ]
  ];

  $success = $CI->settings_model->update($data);

  $subject = "Factura #serie_numar# este scadenta in #data_scadenta#";
  $message = "Buna ziua,\n\n
Va reamintim ca factura #serie_numar# din data de #data_emiterii# este scadenta in #data_scadenta#.\n
Daca plata a fost deja efectuata, va rugam sa ne contactati.\n\n
Va multumim pentru colaborare!\n\n";
  $staf = get_staff(get_staff_user_id());

  $query = "INSERT INTO `".db_prefix()."billing_notifications` (`days`, `type`, `subject`, `message`, `fullname`, `cc`)
      VALUES
      (1, 'before',  '".$subject."', '".$message."', '".$staf->firstname . ' ' . $staf->lastname."', '".$staf->email."');";

  $CI->db->query($query);

  $subject = "Factura #serie_numar# are scadenta depasita";
  $message = "Va reamintim ca factura #serie_numar# din data de #data_emiterii# si scadenta in #data_scadenta# a depasit termenul de plata cu #numar_zile# zile.\n
Atasam inca o data documentul pentru a putea face dumneavoastra verificarile necesare.\n\n
Pentru neclaritati sau neconcordante va rugam sa ne contactati in scopul lamuririi situatiei
";
  $query = "INSERT INTO `".db_prefix()."billing_notifications` (`days`, `type`, `subject`, `message`, `fullname`, `cc`)
      VALUES
      (10, 'after', '".$subject."' , '".$message."', '".$staf->firstname . ' ' . $staf->lastname."', '".$staf->email."');";

  $CI->db->query($query);

  $query = "INSERT INTO `".db_prefix()."billing_notifications` (`days`, `type`, `subject`, `message`, `fullname`, `cc`)
      VALUES
      (30, 'after', '".$subject."' , '".$message."', '".$staf->firstname . ' ' . $staf->lastname."', '".$staf->email."');";

  $CI->db->query($query);
	*/
}
