<?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.
 */

/**
 * Description of Customer_Controller
 *
 * @author Code sys PC 7
 */
class Customer_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('customer');
        $this->load->helper('form');
        $this->load->helper('url');
    }

    public function addCustomer() {
        $customer_name = $this->input->post("add_cus_name");
        $customer_office_address = $this->input->post("add_cus_office_address");
        $customer_office_contact = $this->input->post("add_cus_office_contact");
        $customer_factory_address = $this->input->post("add_cus_factory_address");
        $customer_factory_contact = $this->input->post("add_cus_factoy_contact");
        $customer_cnp1_name = $this->input->post("cnp1_name");
        $customer_cnp1_cotact_no = $this->input->post("cnp1_contact_no");
        $customer_cnp1_email = $this->input->post("cnp1_email");
        $customer_cnp2_name = $this->input->post("cnp2_name");
        $customer_cnp2_contact_no = $this->input->post("cnp2_contact_no");
        $customer_cno2_email = $this->input->post("cnp2_email");
        $customer_vat_type = $this->input->post("vat_type");
        $customer_payement_type = $this->input->post("payment_type");
        $customer_credit_limit = $this->input->post("credit_limit");
        $customer_credit_period = $this->input->post("credit_period");
        $customer_outstanding = $this->input->post("current_outstanding");
        $customer_data = array('customer_name' => $customer_name,'office_address' => $customer_office_address, 'office_contact' => $customer_office_contact, 'factory_address' => $customer_factory_address,
            'factory_contact' => $customer_factory_contact, 'cnp1_name' => $customer_cnp1_name, 'cnp1_cotact_no' => $customer_cnp1_cotact_no, 'cnp1_email' => $customer_cnp1_email, 'cnp2_name' => $customer_cnp2_name,
            'cnp2_contact_no' => $customer_cnp1_cotact_no, 'cno2_email' => $customer_cno2_email,
            'vat_type' => $customer_vat_type, 'payement_type' => $customer_payement_type, 'credit_limit' => $customer_credit_limit, 'credit_period' => $customer_credit_period,'curent_outstanding' => $customer_outstanding);
        $this->customer->saveCustomer($customer_data);
    }

    public function addNewProductForCustomer() {
        $customer = $this->input->post("add_prn_customer");
        $product_code = $this->input->post("add_prnt_code");
        $product_description = $this->input->post("add_prnt_description");
        $product_price = $this->input->post("add_prnt_price");
        $price = 0;
        if ($product_price != "") {
            $price = $product_price;
        }
        $cutomer_product = array('customers_product_code' => $product_code, 'product_description' => $product_description, 'product_price' => $price, 'customer_customer_id' => $customer);
        $this->customer->saveCustomerProduct($cutomer_product);
    }

    public function updateCustomer() {
        $customer_id = $this->input->post("psm_ac_customer_id");
        $customer_name = $this->input->post("add_cus_name");
        $customer_office_address = $this->input->post("add_cus_office_address");
        $customer_office_contact = $this->input->post("add_cus_office_contact");
        $customer_factory_address = $this->input->post("add_cus_factory_address");
        $customer_factory_contact = $this->input->post("add_cus_factoy_contact");
        $customer_cnp1_name = $this->input->post("cnp1_name");
        $customer_cnp1_cotact_no = $this->input->post("cnp1_contact_no");
        $customer_cnp1_email = $this->input->post("cnp1_email");
        $customer_cnp2_name = $this->input->post("cnp2_name");
        $customer_cnp2_contact_no = $this->input->post("cnp2_contact_no");
        $customer_cno2_email = $this->input->post("cnp2_email");
        $customer_vat_type = $this->input->post("vat_type");
        $customer_payement_type = $this->input->post("payment_type");
        $customer_credit_limit = $this->input->post("credit_limit");
        $customer_credit_period = $this->input->post("credit_period");
        $customer_outstanding = $this->input->post("current_outstanding");
        $customer_data = array('customer_name' => $customer_name,'office_address' => $customer_office_address, 'office_contact' => $customer_office_contact, 'factory_address' => $customer_factory_address,
            'factory_contact' => $customer_factory_contact, 'cnp1_name' => $customer_cnp1_name, 'cnp1_cotact_no' => $customer_cnp1_cotact_no, 'cnp1_email' => $customer_cnp1_email, 'cnp2_name' => $customer_cnp2_name,
            'cnp2_contact_no' => $customer_cnp1_cotact_no, 'cno2_email' => $customer_cno2_email,
            'vat_type' => $customer_vat_type, 'payement_type' => $customer_payement_type, 'credit_limit' => $customer_credit_limit, 'credit_period' => $customer_credit_period,'curent_outstanding' => $customer_outstanding);
        $this->customer->updateCustomerDetails($customer_id, $customer_data);
        redirect(base_url() . "index.php/Ui_controller/load_all_customers");
    }

    public function deleteCustomer() {
        $customer_id = $this->input->get("customer_id");
        $this->customer->deleteCustomer($customer_id);
        redirect(base_url() . "index.php/Ui_controller/load_all_customers");
    }

    public function updateProductOFCustomer() {
        $product_id = $this->input->post("psm_ap_customer_print_id");
        $product_code = $this->input->post("psm_ap_customer_print_code");
        $product_description = $this->input->post("psm_ap_print_details");
        $product_price = $this->input->post("psm_ap_print_price");
        $product_delete_status = $this->input->post("psm_ap_delete_status");
        $price = 0;
        if ($product_price != "") {
            $price = $product_price;
        }
        $cutomer_product = array('customers_product_code' => $product_code, 'product_description' => $product_description, 'product_price' => $price, 'delete_status' => $product_delete_status);
        $this->customer->updateCustomerProductDetails($product_id, $cutomer_product);
        redirect(base_url() . "index.php/Ui_controller/load_all_customer_product");
    }

    public function deleteCustomerProduct() {
        $customer_print_id = $this->input->get("customer_print_id");
        $this->customer->deleteCustomerProduct($customer_print_id);
        redirect(base_url() . "index.php/Ui_controller/load_all_customer_product");
    }

}
