<?php

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

class Fixes_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
       $this->load->model('fixes_model');
        if ($this->session->userdata('role') == '') {
            redirect('logout');
        }
    }

    
    public function index(){
        
        $this->load->view('includes/header');
        $this->load->view('setting/fixes_view');
        $this->load->view('includes/footer');
        
    }
    
    public function fix_total_sales(){
        
        $result = $this->fixes_model->fix_all_sales();
        echo $result;
                
    }
    
    public function fix_services_sales(){
        
        $result = $this->fixes_model->fix_services_sales();
        echo $result;
                
    }
    
    public function fix_retail_sales(){
        
        $result = $this->fixes_model->fix_retail_sales();
        echo $result;
                
    }
    
    
    public function fix_invioce_details() {
        
        
        $invoices = $this->fixes_model->get_invoices();
                
        foreach ($invoices as $invoice){
            print_r($invoice['id_invoice']);
            $total_services=0;    
            $result = $this->fixes_model->get_invoice_details($invoice['id_invoice']);
//            foreach($result as $row){
//                $total_services++;
//            }
            
            foreach ($result as $row){
                if($invoice['net_amount']>0){
                    $rate=$row['discounted_price']*100/$invoice['net_amount'];
                }else {$rate=0;}
                $data = array(
                    'paid'=>($invoice['paid_amount']*$rate)/100,
                    'invoice_detail_date'=>$invoice['invoice_date']
                );
                
                $a = $this->fixes_model->update_invoice_details($data, $row['id_invoice_details']);
                echo '<pre>';
                print_r($a);
                echo '<pre>';
                
            }
        }

        $this->load->view('settings/fixes_view');
    
    }
    
    public function fix_invoice_products() {
        
        
        $invoices = $this->fixes_model->get_sale_invoices();
                
        foreach ($invoices as $invoice){
            print_r($invoice['id_invoice']);
            $total_services=0;    
            $result = $this->fixes_model->get_invoice_products($invoice['id_invoice']);
//            foreach($result as $row){
//                $total_services++;
//            }
            
            foreach ($result as $row){
                if($invoice['net_amount']>0){
                    $rate=$row['discounted_price']*100/$invoice['net_amount'];
                }else {$rate=0;}
                $data = array(
                    'paid'=>($invoice['paid_amount']*$rate)/100
                );
                
                $a = $this->fixes_model->update_invoice_products($data, $row['id_invoice_products']);
                echo '<pre>';
                print_r($a);
                echo '<pre>';
                
            }
        }

        $this->load->view('settings/fixes_view');
    
    }

   

    public function createretainedvouchers(){
        $this->load->model('accounting_model');
        
        $advances = $this->fixes_model->getdataforretained();
        
        foreach($advances as $advance){
            echo $advance['branch_id'].'<br>';
            
            $accounts = $this->accounting_model->get_mapped_accounts(28 , $advance['branch_id']);
            if(sizeof($accounts)>=1){
                
                $creditaccount=0; $debitaccount=0;
                foreach($accounts as $account){
                    if($account['transaction_type']=='credit'){
                        $creditaccount = $account['account_head_id'];
                    } elseif($account['transaction_type']=='debit'){
                        $debitaccount = $account['account_head_id'];
                    }
                } 

                //add voucher
                $voucherdata = array( 
                    'business_id' => $advance['branch_id'],
                    'description' => 'Advance amount carried forward from previous system',
                    'voucher_date' => date('Y-d-m'),
                    'voucher_amount' => $advance['advance'],
                    'voucher_status' => 'Active',
                    'created_by' => $this->session->userdata('username'),
                    'cost_center'=> 1,
                    'cost_center_name'=> 'Main Outlet',
                    'business_partner'=> 1,
                    'business_partner_name'=> $advance['customer_name'],
                    'business_partner_id'=> $advance['id_customers'],
                    'payment_mode' => 'Cash',
                    'voucher_type'=> 3
                );

                $voucherid = $this->fixes_model->insertvoucher($voucherdata);

                ///add voucher details
                $debitdata = array(
                    'account_voucher_id' => $voucherid,
                    'debit' => $advance['advance'],
                    'credit' => 0,
                    'cost_center'=> 'Main Outlet',                
                    'instrument_number' => '',
                    'payment_mode' => 'Cash',
                    'account_head_id' => $debitaccount
                );

                $detailid1 = $this->fixes_model->insertvoucherdetail($debitdata); 

                $creditdata = array(
                    'account_voucher_id' => $voucherid,
                    'debit' => 0,
                    'credit' => $advance['advance'],
                    'cost_center'=> 'Main Outlet',                
                    'instrument_number' => '',
                    'payment_mode' => 'Cash',
                    'account_head_id' => $creditaccount
                );

                $detailid2 = $this->fixes_model->insertvoucherdetail($creditdata); 

                echo $voucherid.'</br>';
            }
            echo 'no branch</br>';
        }
            
        
        
    }
    
    
    public function fix_auto_increments() {
    
        $tablename='';
        
        $sql = "SELECT t.table_name FROM INFORMATION_SCHEMA.TABLES t WHERE t.table_schema = 'sked_gym';";
        
        $query = $this->db->query($sql);        
        $tables = $query->result_array();
       
        //print_r($tables); exit();
        foreach($tables as $tablename){
            $sql = "ALTER TABLE ".$tablename['table_name']." AUTO_INCREMENT = 0 ;";
            $query = $this->db->query($sql);
//            /print_r($result);
        }
        
        
        //$sql ="ALTER TABLE some_table AUTO_INCREMENT = 0";
        
        
        
    }
}
