<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UpdateSerialNo extends CI_Controller {
	public function __construct(){
		parent::__construct();
		//$this->load->library('layouts');
		//$this->load->model('menu');
		
	}
	
	public function index()
	{
		$table = array(
				'40'=>'comp_purchase_order',
				'41'=>'comp_supplier_invoice',
				'42'=>'comp_supplier_debitnote',
				'43'=>'comp_supplier_creditnote',
				'44'=>'comp_payment',
				'45'=>'comp_receive_item',
				'46'=>'comp_location_transfer',
				'47'=>'comp_quotation',
				'48'=>'comp_sales_order',
				'49'=>'comp_delivery_order',
				'50'=>'comp_sales_invoice',
				'51'=>'comp_sales_debitnote',
				'52'=>'comp_sales_creditnote',
				'53'=>'comp_receive_payment',
				'55'=>'comp_payment_others',
				'56'=>'comp_receive_payment_others',
//				'118'=>'comp_',
//				'124'=>'comp_', //cash purchase
				'121'=>'comp_cash_sales',
				); 
		$sql = 'select ID,companyName from company order by ID';
		$rsCompany = $this->db->query($sql);
		if ($rsCompany->num_rows > 0) {
			$rs = $rsCompany->result();
			foreach ($rs as $rsCompanyRow) {
				echo '<br>'.$rsCompanyRow->ID.' '.$rsCompanyRow->companyName;
				foreach($table as $row => $val) {
					echo '<br>'.$row.'/'.$val.':Checking';
					$formControl = 'select formInitialNo from comp_form_control where companyID = '.$rsCompanyRow->ID.' and formID = '.$row;
					$rsF =  $this->db->query($formControl);
					$rsFormControl = ($rsF->num_rows > 0)?$rsF->result():NULL;
//					var_dump($rsFormControl);
					$serialNo = ($rsFormControl)?$rsFormControl[0]->formInitialNo:1;
					$sqlSelect = 'select ID, serialNo from '.$val.' where companyID ='.$rsCompanyRow->ID.' order by ID';
					$rsSelect = $this->db->query($sqlSelect);
					if ($rsSelect->num_rows > 0) {
						$rsU = $rsSelect->result();
						foreach ($rsU as $rsUpdate ) {
							$sqlUpdate = 'update '.$val.' set serialNo ='.$serialNo.' where ID = '.$rsUpdate->ID;
							$serialNo++; 
							$this->db->query($sqlUpdate);
//							echo '<br>'.$sqlUpdate; 
						}	
					}
					echo ':success';
				}
			}
		}
		echo '<h1>alhamdulillah selamat. salam kemaafan dari kami team SPS. terima kasih</h1>';
	}
}