<!DOCTYPE html>
<html>

<head>
    <style>
        @import url('https://fonts.googleapis.com/css?family=Roboto:400,500,700');

        * {
            box-sizing: border-box;
        }

        body {
        	margin: 0px;
        	padding: 0px;
        	font-family: 'Roboto', sans-serif;
        	/* background-color: #FBFBFD; */
        	color: #000;
        	font-weight: 400;
        	line-height: 1.5;
        }

        .container {
        	max-width: 950px;
        	margin: 0 auto;
        	/* -webkit-box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.04); */
        	/* box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.04); */
        	/* border: 1px solid #EAEDF3; */
        	background-color: #fff;
        }



        table {
            border-collapse: collapse;
            width: 100%;
            margin-top: 20px;
        }

        table td, table th {
        	border: 1px solid #B3B3B3;
        	padding: 7px 10px;
        	font-size: 14px;
        	text-align: left;
        }

        table th {
        	font-weight: 600;
        }

        .heading {
        	border-bottom: none;
        	font-size: 18px;
        	font-weight: 600;
        	padding-top: 20px;
        }

        .sub_table
        {
            border-top:none;
        }

        .sub_table table
        {
            margin-top: 0;
            margin-bottom: 10px;
        }

        .text_right
        {
            text-align: right;
        }

        /**developer's**/
        .break_div{
			
			page-break-after: always;

		}

		.not_break{

			page-break-after: avoid !important;

		}

    </style>
</head>

<body>
	<!--footer for the first page as without this it will not print on first page of pdf-->
	<div id="footer">
	<div class="page-number"></div>
	</div>
	<!-- footer -->
    <div class="container">
         <table>
            <tbody>
                <tr>
                    <td colspan="2" rowspan="4" style="text-align:center;">
                        <img src="images/onilogo.png" alt="Company Logo" width="100px">
                    </td>
                    <th>Contributor Name</th>
                    <td><?php foreach ($contributors1 as $contributordet){ echo $contributordet["user_details"]["name"];  $contri_n = $contributordet["user_details"]["name"]; } ?>
                    </td>
                </tr>
                <tr>
                    <th>Statement Date</th>
                    <td><?php echo date("m/d/Y")?></td>
                </tr>
                <tr>
                    <th>Statement Date Range</th>
                    <td>
                    <?php

						#if we are selecting the current quarter from drop down then show, statement date range as first date of selected quarter to current date of the quarter we are in
						if(!empty(\Session::get('week_from'))){
							$week_from = date("m/d/Y", strtotime(\Session::get('week_from')));
						}else{
							$week_from = date("m/d/Y", strtotime($week_from));
						}
				       	
				       	if(!empty(\Session::get('week_from'))){
							$week_to = date("m/d/Y", strtotime(\Session::get('week_to')));
						}else{
							$week_to = date("m/d/Y", strtotime($week_to));
						}
				       	echo $week_from.' To '.$week_to;
				       	

						$qtrs_val=array();
						/* getting the selected quarter*/
						if($selected_quarter == 1){
							$selected_q = '1st Quarter (Jan-March)';
							$this_quarter = '1st Quarter (Jan-March)';
							$select_q_footer = '1st Quarter';
						}
						if($selected_quarter == 2){
							$selected_q = '2nd Quarter (Apr-June)';
							$this_quarter = '2nd Quarter (Apr-June)';
							$select_q_footer = '2nd Quarter';
						}
						if($selected_quarter == 3){
							$selected_q = '3rd Quarter (July-Sept)';
							$this_quarter = '3rd Quarter (July-Sept)';
							$select_q_footer = '3rd Quarter';
						}
						if($selected_quarter == 4){
							$selected_q = '4th Quarter (Oct-Dec)';
							$this_quarter = '4th Quarter (Oct-Dec)';
							$select_q_footer = '4th Quarter';
						}
						if($selected_quarter == 5){
							$selected_q = 'Custom Quarter (Jan-Dec)';
							$this_quarter = 'Custom Quarter (Jan-Dec)';
							$select_q_footer = 'Custom Quarter';

							//get the quarter of week from date
							$month_name1 = date('M', strtotime($week_from));
							$month1 = date('m', strtotime($week_from));
							$q1 = ceil($month1/3);

							//get the quarter of week to date
							$month_name2 = date('M', strtotime($week_to));
							$month2 = date('m', strtotime($week_to));
							$q2 = ceil($month2/3);  

							//collect the quarter in array
							for($qtrs=$q1; $qtrs <= $q2; $qtrs++){
								$qtrs_val[]=$qtrs;
							}
						}
						$currentDate = floor((date('n') - 1) / 3);

					        $year = date('Y');
					        $quarters = array();
					        $q = "";
					        for ($i = 0; $i < 5; $i++)
					        {
					            $q = (($currentDate+$i)%4) + 1;
					         
					        }
					        setlocale(LC_MONETARY, 'en_US.UTF-8'); //for money
					?>
                    </td>
                </tr>
                
                <tr>
                    <th>Selected Quarter</th>
                    <td><?php echo $this_quarter; ?></td>
                </tr>
            </tbody>
        </table>
        <!-- Sales starts here-->
		<?php 
		#balance_end_of_quarter 
		$sum_beq = array();
		?>

		<!-- CONTAINER COLLECTION  -->
		<?php 
		if(isset($final_array['container_collection'])){ 
			$divcount = 0;
			$class_name = 'break_div';
			$arr_count = count($final_array['container_collection']);
			foreach($final_array['container_collection'] as $book_id => $data){

				if($divcount == $arr_count-1 && $divcount != 0){
				$class_name = "not_break";
				}

				#show quarter name on header of each page
				if($divcount > 0){
		?>	
				<table>
				  <tbody>
				    <tr>
				      <td>QUARTER : {{$this_quarter}} - {{$selected_year}}</td>
				    </tr>
				  </tbody>
				</table>
				
		<?php 	} ?>

				<!--div for page pag!e-->
				<div class="{{$class_name}}">
				<table>
		            <tbody>
		                <tr>
		                    <th>Main Container</th>
		                    <th>Contributor's Previous Quarter Balance</th>
		                    <th>Contributor's Royalty Due This Quarter</th>
		                    <th>Contributor's Contained Items Royalty Due This Quarter</th>
		                    <th  class='sum_beq'>Contributor's Current Advance Balance/Royalty Due</th>
		                 </tr>
		                <tr>
		                    <td>{{$data['main_array']['main_title']}}</td>
		                    <?php 
		                    if(isset($bal_prev_container['container_collection'][$book_id]) && $bal_prev_container['container_collection'][$book_id] < 0){
		                    ?>
		                    	 <td>{{money_format('%+n', round($bal_prev_container['container_collection'][$book_id],2))}}</td>
		                    <?php	}else{ ?>
		                    		 <td>$0</td>
		                    <?php	}
		                    ?>
		                   
		                    <td>{{money_format('%+n', round($data['main_array']['main_b_e_q'],2))}}</td>
		                    <?php
		                    #contained items sum
							if(isset($data['contained_array'])){
								$contained_royalty_due = 0;
								foreach($data['contained_array'] as  $item){
									$contained_royalty_due += $item['b_e_q'];
								}
								?>
							<td>{{money_format('%+n', round($contained_royalty_due,2))}}</td>
							<?php }else{ ?>
							<td>-</td>
							<?php } 
							if(isset($bal_prev_container['container_collection'][$book_id]) && $bal_prev_container['container_collection'][$book_id] < 0){
								$final_bal = $data['main_array']['b_e_q_final']+$bal_prev_container['container_collection'][$book_id];
							}else{
								$final_bal = $data['main_array']['b_e_q_final']+0;
							}
							
							$sum_beq[]=$final_bal;
							?>
		                    
		                    <td>{{money_format('%+n', round($final_bal,2))}}</td>
		                </tr>

		                <!--CONTANED ITEMS-->
		                <?php if(isset($data['contained_array'])){ 

		                	$count = 0;

							#sum of contained items
							$contained_sum = array();

							#remaning data
							$remaining_arr = array();?>
							<tr>
			                    <td class="heading" colspan="5">Contained Items</td>
			                </tr>
			                <tr>
			                    <td class="sub_table" colspan="5">
			                        <table>
			                            <tr>
			                                <th>Book Title</th>
			                                <th>Total Sales</th>
			                                <th>Advance</th>
			                                <th>Creators' Royalty Due This Quarter</th>
			                                <!--th>Royalty Due</th-->
			                                <th>Contributor's Balance This Quarter</th>
			                            </tr>
			                            
								<?php 
								foreach($data['contained_array'] as  $item){
									$contained_sum[]= $item['b_e_q'];
									if($count < 6){
			                	?>
			                
			                			<tr>
			                                <td>{{$item['book_name']}}</td>
			                                <td>{{money_format('%+n', round($item['total_sales'],2))}}</td>
			                                <td>{{money_format('%+n', round($item['advance'],2))}}</td>
			                                <td>{{money_format('%+n', round($item['contri_part'],2))}}</td>
			                                <!--td>{{money_format('%+n', round($item['royalty_due'],2))}}</td-->
			                                <td>{{money_format('%+n', round($item['b_e_q'],2))}}</td>
			                            </tr>
				                <?php
				                	}if($count >= 6){

										$remaining_arr[]=[
										'book_name'=>$item['book_name'],
										'total_sales'=>$item['total_sales'],
										'advance'=>$item['advance'],
										'contri_part'=>$item['contri_part'],
										'b_e_q'=>$item['b_e_q'],
										'royalty_due'=>$item['royalty_due']
										];
									}
				                	$count ++;
				                } #contained items foreach
				                ?>
				                <?php if($count <= 6){ ?> 
				                <tr>
		                            <th class="text_right" colspan="4">Contained Items Royalty Due</th>
		                           <td>
		                           <?php echo  money_format('%+n', round(array_sum($contained_sum),2)) ;?>
		                           </td>
		                        </tr>
				                <?php } ?>
		                	
		                	</table>
			                    </td>
			                </tr>

			            <?php
		                } #contained isset check
		                ?>
		            </tbody>
		        </table>

				</div>
				<?php
				//echo '<tr><td>'.$data['main_array']['main_title'].'</td>';
				if(!empty($remaining_arr)){ ?>
				
					<table  class='<?php echo $class_name; ?> '>
					<tbody>
					<tr>
						<th colspan='5'>(Continued) Main Container : {{$data['main_array']['main_title']}}</th>
					</tr>
			
					<tr>
						<th>Book Title</th>
						<th>Total Sales</th>
						<th>Advance</th>
						<th>Creators' Royalty Due This Quarter</th>
						<!--th>Royalty Due </th-->
						<th>Contributor's Balance This Quarter </th>
					</tr>

					
					<?php foreach($remaining_arr as  $item){ ?>
					<tr>
						<td>{{$item['book_name']}}</td>
						<td>{{money_format('%+n', round($item['total_sales'],2))}}</td>
						<td>{{money_format('%+n', round($item['advance'],2))}}</td>
						<td>{{money_format('%+n', round($item['contri_part'],2))}}</td>
						<!--td>{{money_format('%+n', round($item['royalty_due'],2))}}</td-->
						<td>{{money_format('%+n', round($item['b_e_q'],2))}}</td>
					</tr>
					<?php } ?>

					<tr>
						<th class="text_right" colspan="4">Contained Items Royalty Due</th>
						<!-- <td> -->
						<?php //print_r($contained_sum);?>
						<!-- </td> -->
						<td><?php echo money_format('%+n', round(array_sum($contained_sum),2)) ;?></td>
					</tr>
					</tbody>
					</table>
				<?php
				}
				?>
		<?php

			} #foreach main container
		} #main isset container 
		?>

		<!-- SIMPLE SALES -->
		<?php
		##simple collection
		if(isset($final_array['simple_array'])){
		?>
		<table>
            <tbody>
            <tr>
                <td class="heading" colspan="5">Single Items</td>
            </tr>
            <tr>
	       
				<th>Book Title</th>
				<th>Balance Previous Quarter</th>
				<!--th>Profit This Quarter</th-->
				<th>Creator's Royalty Due This Quarter </th>
				<th>Contributor's Royalty Due this Quarter</th>
				<th>Contributor's Balance This Quarter</th>

			</tr>


            <?php foreach($final_array['simple_array'] as $book_id => $data){ ?>
            	

            	<tr>
                <td>{{$data['book_title']}}</td>
                <?php
                	if(isset($data['b_e_q_prev'])){
						$prev_val = $data['b_e_q_prev'];
					}else{
						$prev_val = 0;
					}
					
					$final_bal =$data['b_e_q'] + $prev_val;
					$sum_beq[]=$final_bal; 

					#show only if it is negative
					if($final_bal > 0){
						$final_bal_display = 0;
					}else{
						$final_bal_display = $final_bal;
					}
                ?>
                <td>{{money_format('%+n', round($prev_val,2))}}</td>
                <!--td>{{money_format('%+n', round($data['total_sales'],2))}}</td-->
                <td>{{money_format('%+n', round($data['contri_part'],2))}}</td>
                <td>{{money_format('%+n', round($data['b_e_q'],2))}}</td>
                <td class="sum_beq">{{money_format('%+n', round($final_bal_display,2))}}</td>
            	</tr>
		<?php
			} #simple foreach ends here
		?>
			</tbody>
			</table>
		<?php
		}  #simple isset check
		?>
		<!-- CONTRIBUTOR COMMENT -->
		<?php if($contri_comment) { ?>
		<table>
			<tbody>
			<tr>
			<th>Contributor Comment</th>
			</tr>
			<tr>
				<td>{{$contri_comment}}</td>
			</tr>
			</tbody>
		</table>
		<?php } ?>
		<!-- FINAL BALANCE -->
		<table>
			<tbody>
			<tr>
			<th>Final Ballance This Quarter</th>
			</tr>
			<tr>
				<td>
					<?php
					$final_sum = 0 ;
					foreach($sum_beq as $sum){
						if($sum < 0){
							$sum = 0;
						}
						$final_sum += $sum;
					}
					echo money_format('%+n', round($final_sum,2));
					?>
				</td>
			</tr>
			</tbody>
		</table>
		<?php 
		//if(\Session::get('login_type') == 2){

		if(\Session::get('login_type')){
	        $pdf_type = "Summary Report";
	    }
	?>
    </div>
    <!--footer for the first page as without this it will not print on first page of pdf-->
	<div id="footer">
	<div class="page-number"></div>
	</div>
	
	<style>
		#footer {
		  position: fixed;
		  left: 0;
		  right: 0;
		  color: #aaa;
		  font-size: 0.9em;
		}
		
		#footer {

		  bottom: 0;
		  border-top: 0.1pt solid #aaa;
		}
		.page-number:before {
		  content: "<?php  echo $contri_n.", ".$select_q_footer.", ".$pdf_type; ?>, Page " counter(page);
		}
	</style>
	<!-- footer -->
</body>
</html>
