<?php

	require_once '../../../library_html/elements/core/lib/lib_elementsCore.php';
	require_once '../../../library_html/components/card/lib/lib_card.php';
	require_once '../../../library_html/components/table/lib/lib_table.php';

    function draw_acc_income_statement() {
	    //Elements
	    include_once '../../../library_html/elements/core/lib/lib_elementsDraw.php';
	    //Accounting
	    include_once '../../../library_html/accounting/core/js/js_acc_accounts.php';
	    //include_once '../../../library_html/technical/core/js/js_technicalNavButtons.php';
	    $page = get_accounting_income_statement();

	    //Draw page
	    new drawElement($page);
	}

    function get_accounting_income_statement() {
    	$l_card = new lib_card();
    	$l_table = new lib_table();

		require_once '../../../library_html/properties/core/dat/dat_properties.php';
		require_once '../../../library_html/accounting/core/lib/lib_acc_basic.php';
    	$dat = new dat_properties();
    	$result = $dat->select_settings(["user_account"=>$_COOKIE['user_account'],"property"=>"ACC_SEL_ORG"]);

    	if (count($result) > 0) {
	    	$org_id = $result[0]['setting'];

			require_once '../../../library_html/accounting/core/dat/dat_accounting.php';
	    	$dat = new dat_accounting();
	    	$result = $dat->select_acc_organizations(["org_id"=>$org_id]);

	    	if (count($result) > 0) {
	    		$month = get_acc_month();
				$str_length = 2;
	    		$month = substr("00{$month}", -$str_length);
	    		$year = get_acc_year();
				$str_length = 4;
	    		$year = substr("0000{$year}", -$str_length);

				//Get background
				$page = $l_card->get_card(['title'=>'Income Statement - '.$result[0]['name'].' ('.$result[0]['org_id'].') - '.$month.'/'.$year]);
				$page->children[0]->children[1] = $l_table->get_table(null);
				$page->children[0]->children[1]->children[0]->children[] = [];
				$object = $page->children[0]->children[1]->children[0]->children[0];

				$lines = [];
				$lines[0] = get_acc_income_statement_header();
				$lines = get_acc_income_statement_pre_carry($lines);
				$lines = get_acc_income_statement_rows($lines);
				$lines = get_acc_income_statement_totals($lines);
				$lines = get_acc_income_statement_post_carry($lines);
				$object = $page->children[0]->children[1]->children[0]->children = $lines;
				return $page;
			}
		}
		return [];
	}

	function get_acc_income_statement_header() {
		require_once '../../../library_html/elements/table/lib/table.php';
		$result = get_acc_accounts_list();

		$items = [];
		$items[] = ['type'=>true,'item'=>'Day'];
		$items[] = ['type'=>true,'item'=>'Outgoing'];
		$items[] = ['type'=>true,'item'=>'Incomming'];
		$items[] = ['type'=>true,'item'=>'Description'];
		return Tr($items, null);
	}

	function get_acc_income_statement_rows($lines) {
		$org_id = get_acc_selected_org();
    	if ($org_id != null) {
			require_once '../../../library_html/accounting/core/dat/dat_accounting_adv.php';
	    	$dat = new dat_accounting_adv();

			$year = get_acc_year();
			$month = get_acc_month();
			$total = 0;

			$entries = $dat->select_dual_acc_entries(["org_id"=>$org_id, "year"=>$year, "month"=>$month]);
			$indexer = get_acc_accounts_indexer();
			$from = 0;
			$to = 0;

			foreach($entries as $y => $x) {
				if (!isset($x['from']) || !isset($x['to'])) {
					$line = [];
					$from_idx = 1;
					$to_idx = 2;
					$descr_idx = 3;

					$line[] = ['type'=>false,'item'=>$x['day']];
					$line[] = ['type'=>false,'item'=>''];
					$line[] = ['type'=>false,'item'=>''];
					$line[] = ['type'=>false,'item'=>$x['description']];

					if (isset($x['from'])) {
						$line[$from_idx]['item'] = number_format((float)($x['ammount']*-1), 2, '.', '');
						$line[$from_idx]['class'] = ['bg-danger'];
						$line[$to_idx]['item'] = number_format((float)(0), 2, '.', '');
						$from += ($x['ammount']*-1);
					} else if (isset($x['to'])) {
						$line[$from_idx]['item'] = number_format((float)(0), 2, '.', '');
						$line[$to_idx]['item'] = number_format((float)($x['ammount']), 2, '.', '');
						$line[$to_idx]['class'] = ['bg-success'];
						$to += ($x['ammount']);
					}
					$lines[] = Tr($line, null);
				}
			}
			$line = [];

			$line[] = ['type'=>true,'item'=>'--'];
			if ($from < 0) $from_c = "bg-danger"; else $from_c = "";
			$line[] = ['type'=>true,'item'=>number_format((float)($from), 2, '.', ''), 'class'=>[$from_c]];
			if ($to > 0) $to_c = "bg-success"; else $to_c = "";
			$line[] = ['type'=>true,'item'=>number_format((float)($to), 2, '.', ''), 'class'=>[$to_c]];
			$line[] = ['type'=>true,'item'=>'Sub Totals'];
			$lines[] = Tr($line, null);
		} else {
			//Let user know that no organization is selected
		}
		return $lines;
	}

	function get_acc_income_statement_pre_carry($lines) {
		$org_id = get_acc_selected_org();
    	if ($org_id != null) {
			require_once '../../../library_html/accounting/core/dat/dat_accounting.php';
	    	$dat = new dat_accounting();

			$year = get_acc_year();
			$month = get_acc_month();
			$month--;
			if ($month <= 0) {
				$month = 12;
				$year--;
			}
			$total = 0;

			$entries = $dat->select_acc_carries(["org_id"=>$org_id, "year"=>$year, "month"=>$month]);
			$indexer = get_acc_accounts_indexer();

			$line = [];
			$from_idx = 1;
			$to_idx = 2;
			$descr_idx = 3;

			$line[] = ['type'=>true,'item'=>'--'];
			$line[] = ['type'=>true,'item'=>''];
			$line[] = ['type'=>true,'item'=>''];
			$line[] = ['type'=>true,'item'=>'Carry'];

			foreach($entries as $y => $x) {
				$amm = $x['ammount'];
				$total += $amm;
			}
			$line[$to_idx]['item'] = number_format((float)($total), 2, '.', '');
			if ($total > 0) {
				$line[$to_idx]['class'] = ['bg-success'];
			} else if ($total < 0) {
				$line[$to_idx]['class'] = ['bg-danger'];
			}
			$lines[] = Tr($line, null);
		} else {
			//Let user know that no organization is selected
		}
		return $lines;
	}

	function get_acc_income_statement_post_carry($lines) {
		$org_id = get_acc_selected_org();
    	if ($org_id != null) {
			require_once '../../../library_html/accounting/core/dat/dat_accounting.php';
	    	$dat = new dat_accounting();

			$year = get_acc_year();
			$month = get_acc_month();
			$total = 0;

			$entries = $dat->select_acc_carries(["org_id"=>$org_id, "year"=>$year, "month"=>$month]);
			$indexer = get_acc_accounts_indexer();

			$line = [];
			$from_idx = 1;
			$to_idx = 2;
			$descr_idx = 3;

			$line[] = ['type'=>true,'item'=>'--'];
			$line[] = ['type'=>true,'item'=>''];
			$line[] = ['type'=>true,'item'=>''];
			$line[] = ['type'=>true,'item'=>'Total'];

			foreach($entries as $y => $x) {
				$total += $x['ammount'];
			}
			$line[$to_idx]['item'] = number_format((float)($total), 2, '.', '');
			if ($total > 0) {
				$line[$to_idx]['class'] = ['bg-success'];
			} else if ($total < 0) {
				$line[$to_idx]['class'] = ['bg-danger'];
			}
			$lines[] = Tr($line, null);
		} else {
			//Let user know that no organization is selected
		}
		return $lines;
	}

	function get_acc_income_statement_totals($lines) {
		$org_id = get_acc_selected_org();
    	if ($org_id != null) {
			require_once '../../../library_html/accounting/core/dat/dat_accounting.php';
	    	$dat = new dat_accounting();

			$year = get_acc_year();
			$month = get_acc_month();
			$total = 0;

			$entries = $dat->select_acc_totals(["org_id"=>$org_id, "year"=>$year, "month"=>$month]);
			$indexer = get_acc_accounts_indexer();

			$line = [];
			$from_idx = 1;
			$to_idx = 2;
			$descr_idx = 3;

			$line[] = ['type'=>true,'item'=>'--'];
			$line[] = ['type'=>true,'item'=>''];
			$line[] = ['type'=>true,'item'=>''];
			$line[] = ['type'=>true,'item'=>'Combined Total'];

			foreach($entries as $y => $x) {
				$total += $x['ammount'];
			}
			$line[$to_idx]['item'] = number_format((float)($total), 2, '.', '');
			if ($total > 0) {
				$line[$to_idx]['class'] = ['bg-success'];
			} else if ($total < 0) {
				$line[$to_idx]['class'] = ['bg-danger'];
			}
			$lines[] = Tr($line, null);
		} else {
			//Let user know that no organization is selected
		}
		return $lines;
	}
?>