<?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_balance_sheet() {
	    //Elements
	    include_once '../../../library_html/elements/core/lib/lib_elementsDraw.php';
	    //include_once '../../../library_html/technical/core/js/js_technicalNavButtons.php';
	    $page = get_accounting_balance_sheet();

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

    function get_accounting_balance_sheet() {
    	$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'=>'Balance sheet - '.$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_balance_sheet_header();
				$lines = get_acc_balance_pre_carry($lines);
				$lines = get_acc_balance_sheet_rows($lines);
				$lines = get_acc_balance_totals($lines);
				$lines = get_acc_balance_post_carry($lines);
				$object = $page->children[0]->children[1]->children[0]->children = $lines;
				return $page;
			}
		}
		return [];
	}

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

		$items = [];
		$items[] = ['type'=>true,'item'=>'Day'];
		foreach($result as $y => $x) {
			$items[] = ['type'=>true,'item'=>$x['name']];
		}
		$items[] = ['type'=>true,'item'=>'Equity'];
		$items[] = ['type'=>true,'item'=>'Description'];
		$items[] = ['type'=>true,'item'=>'X'];
		return Tr($items, null);
	}

	function get_acc_balance_sheet_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();

			foreach($entries as $y => $x) {
				$line = [];
				$equity_idx = count($indexer) + 1;
				$descr_idx = count($indexer) + 2;

				$line[] = ['type'=>false,'item'=>$x['day']];
				foreach($indexer as $b => $a) {
					$line[] = ['type'=>false,'item'=>''];
				}
				$line[] = ['type'=>false,'item'=>''];
				$line[] = ['type'=>false,'item'=>$x['description']];
				$line[] = ['type'=>false,'item'=>'<button class="btn btn-danger btn-sm" onclick="acc_delete_entry(\''.$x['ent_id'].'\');">X</button>'.PHP_EOL];

				if (isset($x['from']) && isset($x['to'])) {
					$line[$indexer[$x['from']] + 1]['item'] = number_format((float)($x['ammount']*-1), 2, '.', '');
					$line[$indexer[$x['from']] + 1]['class'] = ["bg-danger"];
					$line[$indexer[$x['to']] + 1]['item'] = number_format((float)($x['ammount']), 2, '.', '');
					$line[$indexer[$x['to']] + 1]['class'] = ["bg-success"];
				} else if (isset($x['from'])) {
					$line[$indexer[$x['from']] + 1]['item'] = number_format((float)($x['ammount']*-1), 2, '.', '');
					$line[$indexer[$x['from']] + 1]['class'] = ["bg-danger"];
					$line[$equity_idx]['item'] = number_format((float)($x['ammount']*-1), 2, '.', '');
					$line[$equity_idx]['class'] = ["bg-danger"];
				} else if (isset($x['to'])) {
					$line[$indexer[$x['to']] + 1]['item'] = number_format((float)($x['ammount']), 2, '.', '');
					$line[$indexer[$x['to']] + 1]['class'] = ["bg-success"];
					$line[$equity_idx]['item'] = number_format((float)($x['ammount']), 2, '.', '');
					$line[$equity_idx]['class'] = ["bg-success"];
				}
				$row = Tr($line, null);
				$lines[] = $row;
			}
		} else {
			//Let user know that no organization is selected
		}
		return $lines;
	}

	function get_acc_balance_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 = [];
			$equity_idx = count($indexer) + 1;
			$descr_idx = count($indexer) + 2;

			$line[] = ['type'=>true,'item'=>'--'];
			foreach($indexer as $b => $a) {
				$line[] = ['type'=>true,'item'=>number_format((float)(0), 2, '.', '')];
			}
			$line[] = ['type'=>true,'item'=>''];
			$line[] = ['type'=>true,'item'=>'Carry'];
			$line[] = ['type'=>true,'item'=>''];

			$total = 0;

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

	function get_acc_balance_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 = [];
			$equity_idx = count($indexer) + 1;
			$descr_idx = count($indexer) + 2;

			$line[] = ['type'=>true,'item'=>'--'];
			foreach($indexer as $b => $a) {
				$line[] = ['type'=>true,'item'=>number_format((float)(0), 2, '.', '')];
			}
			$line[] = ['type'=>true,'item'=>''];
			$line[] = ['type'=>true,'item'=>'Total'];
			$line[] = ['type'=>true,'item'=>''];

			$total = 0;

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

	function get_acc_balance_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 = [];
			$equity_idx = count($indexer) + 1;
			$descr_idx = count($indexer) + 2;

			$line[] = ['type'=>true,'item'=>'--'];
			foreach($indexer as $b => $a) {
				$line[] = ['type'=>true,'item'=>number_format((float)(0), 2, '.', '')];
			}
			$line[] = ['type'=>true,'item'=>''];
			$line[] = ['type'=>true,'item'=>'Sub Total'];
			$line[] = ['type'=>true,'item'=>''];

			$total = 0;

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