<?php

	include '../Connect/cms-config.php' ;
	include '../Requires/session.php' ;
	include '../Requires/function.php' ;

	$branch = escapeString( $_POST['branch'] ) ;
	$type = escapeString( $_POST['type'] ) ;
	$id = escapeString( $_POST['id'] ) ;
	$tax = escapeString( $_POST['tax'] ) ;
	$date = escapeString( $_POST['date'] ) ;

	$content = '' ;

	$query_search = '' ;
	$array_payment_id = array() ;
	switch( $type ){
		case 'paid-by-date' :
			$mysqli_payment = $mysqli->query( "SELECT payment_id, payment_quotation_id, payment_created_date FROM $prefixQuotationPayment
										   	   WHERE payment_created_date LIKE '".$date."-%' AND payment_status = 'completed' AND payment_trash = '0'
										   	   ORDER BY payment_id") ;
			$array_quotation_id = array() ;
			if( $mysqli_payment->num_rows > 0 ){
				while( $row_payment = $mysqli_payment->fetch_assoc() ){
					$array_quotation_id[$row_payment['payment_quotation_id']] = $row_payment['payment_quotation_id'] ;

					$array_payment_id[] = $row_payment['payment_id'] ;
				}
			}
			$array_quotation_id = array_values( $array_quotation_id ) ;

			$query_search .= " AND quotation_id IN (".implode( ",", $array_quotation_id ).")" ;
		break ;
		case 'non-paid' :
			$mysqli_payment = $mysqli->query( "SELECT payment_id, payment_quotation_id, payment_created_date FROM $prefixQuotationPayment
										   	   WHERE payment_created_date LIKE '".$date."-%' AND payment_status = 'completed' AND payment_trash = '0'
										   	   ORDER BY payment_id") ;
			$array_quotation_id = array() ;
			if( $mysqli_payment->num_rows > 0 ){
				while( $row_payment = $mysqli_payment->fetch_assoc() ){
					$array_quotation_id[$row_payment['payment_quotation_id']] = $row_payment['payment_quotation_id'] ;

					$array_payment_id[] = $row_payment['payment_id'] ;
				}
			}
			$array_quotation_id = array_values( $array_quotation_id ) ;

			$query_search .= " AND quotation_id IN (".implode( ",", $array_quotation_id ).") AND quotation_date_start NOT LIKE '".$date."-%'" ;
		break ;
		default :
			$query_search .= " AND quotation_date_start LIKE '".$date."-%'" ;
		break ;
	}
					
	$mysqli_data = $mysqli->query( "SELECT * FROM $prefixQuotation
					 				WHERE quotation_type = 'tax-invoice' AND quotation_status != 'cancelled' AND quotation_status != 'revise' AND quotation_trash = '0'".$query_search."
					 				ORDER BY quotation_id" ) ;
	$array_data = array() ;
	if( $mysqli_data->num_rows > 0 ){
		while( $row = $mysqli_data->fetch_assoc() ){
			$array_data['all']['details'][] = $row ;
			$array_data[$row['quotation_branch']]['details'][] = $row ;
		}
	}

	if( arrayCheck( $array_data[$branch]['details'] ) ){
		foreach( $array_data[$branch]['details'] as $key => $value ){
			$array_company = jsonEncodeDecode( 'decode', $value['quotation_company_details'] ) ;
			$item_details = jsonEncodeDecode( 'decode', $value['quotation_details'] ) ;
			$invoice_total = numberFormat( $value['quotation_total'] ) ;

			switch( $type ){
				case 'costing-list' :
					if( $value['quotation_total_costing'] > 0 ){
						$count = 0 ;
						if( arrayCheck( $item_details['costing'] ) ){
							foreach( $item_details['costing'] as $key2 => $value2 ){
								if( $value2['ID'] == $id ){
									$count++ ;
								}
							}
						}

						if( $count > 0 ){
							$content .= '
							<tr>
								<td>INV'.$value['quotation_so'].'</td>
								<td>'.dataFilterDash( $array_company['company_name'] ).'</td>
								<td>
									'.numberFormat( $value['quotation_total_costing'], 2, ',' ).'
									<ul>' ;
										if( arrayCheck( $item_details['costing'] ) ){
											foreach( $item_details['costing'] as $key2 => $value2 ){
												$content .= '
												<li>'.$value2['Title'].' - '.$value2['Quantity'].'</li>' ;
											}
										}
									$content .= '
									</ul>
								</td>
								<td>'.resetDateFormat( $value['quotation_date_start'] ).'</td>
							</tr>' ;

							$total_costing += $value['quotation_total_costing'] ;
						}
					}
				break ;
				default :
					$calculate_sst = 0 ;
					if( arrayCheck( $item_details['total'] ) ){
						$calculate_sst = $item_details['total']['cart_gst_amout'] ;
					}
					$calculate_sst = numberFormat( $calculate_sst ) ;

					if( $tax == 'no' ){
						$invoice_total -= $calculate_sst ;
					}
					$invoice_total = numberFormat( $invoice_total ) ;

					$query_search_payment = '' ;
					switch( $type ){
						case 'paid-by-date' :
						case 'non-paid' :
							$query_search_payment .= " AND payment_id IN (".implode( ",", $array_payment_id ).")" ;
						break ;
					}

					$mysqli_data = $mysqli->query( "SELECT ( payment_by_cash + payment_by_online + payment_by_cheque ) as payment_total FROM $prefixQuotationPayment
													WHERE payment_quotation_id = '".$value['quotation_id']."' AND payment_status = 'completed' AND payment_trash = '0'".$query_search_payment."
													ORDER BY payment_id" ) ;
					$calculate_payment = 0 ;
					if( $mysqli_data->num_rows > 0 ){
						while( $row = $mysqli_data->fetch_assoc() ){
							$calculate_payment += $row['payment_total'] ;
						}
					}

					if( $tax == 'no' ){
						if( $calculate_payment > 0 ){
							$calculate_payment -= $calculate_sst ;
						}
					}
					$calculate_payment = numberFormat( $calculate_payment ) ;

					$mysqli_cn = $mysqli->query( "SELECT * FROM $prefixCredit
									 			  WHERE credit_quotation_id = '".$value['quotation_id']."' AND credit_note_trash = '0'
									 			  LIMIT 1" ) ;
					$calculate_cn = 0 ;
					if( $mysqli_cn->num_rows > 0 ){
						$row_cn = $mysqli_cn->fetch_assoc() ;

						$array_content = jsonEncodeDecode( 'decode', $row_cn['credit_note_content'] ) ;

						if( arrayCheck( $array_content ) ){
							foreach( $array_content as $key2 => $value2 ){
								if( $value2['quantity'] > 0 && $value2['amount_note'] > 0 ){
									$calculate_cn += $value2['quantity'] * $value2['amount_note'] ;
								}
							}
						}
					}

					$final_unpaid = ( $invoice_total - $calculate_payment ) ;

					if( $final_unpaid > 0 && $calculate_cn > 0 ){
						$final_unpaid -= $calculate_cn ;
					}

					$total_costing += $value['quotation_total_costing'] ;

					switch( $type ){
						case 'sales' :
							$total_unpaid += $final_unpaid ;
							$total_amount += $invoice_total ;
							$total_paid += $calculate_payment ;
							$total_cn += $calculate_cn ;
							$total_sst += $calculate_sst ;

							$content .= '
							<tr>
								<td>INV'.$value['quotation_so'].'</td>
								<td>'.dataFilterDash( $array_company['company_name'] ).'</td>
								<td>'.numberFormat( $final_unpaid, 2, ',' ).'</td>
								<td>'.numberFormat( $invoice_total, 2, ',' ).'</td>
								<td>'.numberFormat( $calculate_payment, 2, ',' ).'</td>
								<td>'.numberFormat( $calculate_cn, 2, ',' ).'</td>
								<td>'.numberFormat( $value['quotation_total_costing'], 2, ',' ).'</td>
								'.( $tax == 'yes' ? '<td>'.numberFormat( $calculate_sst, 2, ',' ).'</td>' : '' ).'
								<td>'.resetDateFormat( $value['quotation_date_start'] ).'</td>
							</tr>' ;
						break ;
						case 'paid' :
							if( $calculate_payment > 0 ){
								$total_unpaid += $final_unpaid ;
								$total_amount += $invoice_total ;
								$total_paid += $calculate_payment ;
								$total_cn += $calculate_cn ;
								$total_sst += $calculate_sst ;

								$content .= '
								<tr>
									<td>INV'.$value['quotation_so'].'</td>
									<td>'.dataFilterDash( $array_company['company_name'] ).'</td>
									<td>'.numberFormat( $final_unpaid, 2, ',' ).'</td>
									<td>'.numberFormat( $invoice_total, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_payment, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_cn, 2, ',' ).'</td>
									<td>'.numberFormat( $value['quotation_total_costing'], 2, ',' ).'</td>
									'.( $tax == 'yes' ? '<td>'.numberFormat( $calculate_sst, 2, ',' ).'</td>' : '' ).'
									<td>'.resetDateFormat( $value['quotation_date_start'] ).'</td>
								</tr>' ;
							}
						break ;
						case 'paid-by-date' :
							if( $calculate_payment > 0 ){
								$total_unpaid += $final_unpaid ;
								$total_amount += $invoice_total ;
								$total_paid += $calculate_payment ;
								$total_cn += $calculate_cn ;
								$total_sst += $calculate_sst ;

								$content .= '
								<tr>
									<td>INV'.$value['quotation_so'].'</td>
									<td>'.dataFilterDash( $array_company['company_name'] ).'</td>
									<td>'.numberFormat( $final_unpaid, 2, ',' ).'</td>
									<td>'.numberFormat( $invoice_total, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_payment, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_cn, 2, ',' ).'</td>
									<td>'.numberFormat( $value['quotation_total_costing'], 2, ',' ).'</td>
									'.( $tax == 'yes' ? '<td>'.numberFormat( $calculate_sst, 2, ',' ).'</td>' : '' ).'
									<td>'.resetDateFormat( $value['quotation_date_start'] ).'</td>
								</tr>' ;
							}
						break ;
						case 'non-paid' :
							if( $calculate_payment > 0 ){
								$total_unpaid += $final_unpaid ;
								$total_amount += $invoice_total ;
								$total_paid += $calculate_payment ;
								$total_cn += $calculate_cn ;
								$total_sst += $calculate_sst ;

								$content .= '
								<tr>
									<td>INV'.$value['quotation_so'].'</td>
									<td>'.dataFilterDash( $array_company['company_name'] ).'</td>
									<td>'.numberFormat( $final_unpaid, 2, ',' ).'</td>
									<td>'.numberFormat( $invoice_total, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_payment, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_cn, 2, ',' ).'</td>
									<td>'.numberFormat( $value['quotation_total_costing'], 2, ',' ).'</td>
									'.( $tax == 'yes' ? '<td>'.numberFormat( $calculate_sst, 2, ',' ).'</td>' : '' ).'
									<td>'.resetDateFormat( $value['quotation_date_start'] ).'</td>
								</tr>' ;
							}
						break ;
						case 'unpaid' :
							if( $calculate_payment < $invoice_total ){
								if( $final_unpaid > 0 ){
									$total_unpaid += $final_unpaid ;
									$total_amount += $invoice_total ;
									$total_paid += $calculate_payment ;
									$total_cn += $calculate_cn ;
									$total_sst += $calculate_sst ;

									$content .= '
									<tr>
										<td>INV'.$value['quotation_so'].'</td>
										<td>'.dataFilterDash( $array_company['company_name'] ).'</td>
										<td>'.numberFormat( $final_unpaid, 2, ',' ).'</td>
										<td>'.numberFormat( $invoice_total, 2, ',' ).'</td>
										<td>'.numberFormat( $calculate_payment, 2, ',' ).'</td>
										<td>'.numberFormat( $calculate_cn, 2, ',' ).'</td>
										<td>'.numberFormat( $value['quotation_total_costing'], 2, ',' ).'</td>
										'.( $tax == 'yes' ? '<td>'.numberFormat( $calculate_sst, 2, ',' ).'</td>' : '' ).'
										<td>'.resetDateFormat( $value['quotation_date_start'] ).'</td>
									</tr>' ;
								}
							}
						break ;
						case 'sst' :
							if( $calculate_sst > 0 ){
								$total_unpaid += $final_unpaid ;
								$total_amount += $invoice_total ;
								$total_paid += $calculate_payment ;
								$total_cn += $calculate_cn ;
								$total_sst += $calculate_sst ;

								$content .= '
								<tr>
									<td>INV'.$value['quotation_so'].'</td>
									<td>'.dataFilterDash( $array_company['company_name'] ).'</td>
									<td>'.numberFormat( $final_unpaid, 2, ',' ).'</td>
									<td>'.numberFormat( $invoice_total, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_payment, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_cn, 2, ',' ).'</td>
									<td>'.numberFormat( $value['quotation_total_costing'], 2, ',' ).'</td>
									'.( $tax == 'yes' ? '<td>'.numberFormat( $calculate_sst, 2, ',' ).'</td>' : '' ).'
									<td>'.resetDateFormat( $value['quotation_date_start'] ).'</td>
								</tr>' ;
							}
						break ;
						case 'costing' :
							if( $value['quotation_total_costing'] > 0 ){
								$total_unpaid += $final_unpaid ;
								$total_amount += $invoice_total ;
								$total_paid += $calculate_payment ;
								$total_cn += $calculate_cn ;
								$total_sst += $calculate_sst ;

								$content .= '
								<tr>
									<td>INV'.$value['quotation_so'].'</td>
									<td>'.dataFilterDash( $array_company['company_name'] ).'</td>
									<td>'.numberFormat( $final_unpaid, 2, ',' ).'</td>
									<td>'.numberFormat( $invoice_total, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_payment, 2, ',' ).'</td>
									<td>'.numberFormat( $calculate_cn, 2, ',' ).'</td>
									<td>'.numberFormat( $value['quotation_total_costing'], 2, ',' ).'</td>
									'.( $tax == 'yes' ? '<td>'.numberFormat( $calculate_sst, 2, ',' ).'</td>' : '' ).'
									<td>'.resetDateFormat( $value['quotation_date_start'] ).'</td>
								</tr>' ;
							}
						break ;
					}
				break ;
			}
		}
	}

	switch( $type ){
		case 'costing-list' :
			if( !empty( $content ) ){
				$content .= '
				<tr>
					<th colspan="2"></th>
					<th>'.numberFormat( $total_costing, 2, ',' ).'</th>
					<th></th>
				</tr>' ;
			}
		break ;
		default :
			if( !empty( $content ) ){
				$content .= '
				<tr>
					<th colspan="2"></th>
					<th>'.numberFormat( $total_unpaid, 2, ',' ).'</th>
					<th>'.numberFormat( $total_amount, 2, ',' ).'</th>
					<th>'.numberFormat( $total_paid, 2, ',' ).'</th>
					<th>'.numberFormat( $total_cn, 2, ',' ).'</th>
					<th>'.numberFormat( $total_costing, 2, ',' ).'</th>
					'.( $tax == 'yes' ? '<th>'.numberFormat( $total_sst, 2, ',' ).'</th>' : '' ).'
					<th></th>
				</tr>' ;
			}
		break ;
	}

	if( empty( $content ) ){
		$content .= '
		<tr>
			<td colspan="'.( $tax == 'yes' ? '9' : '8' ).'">No Data.</td>
		</tr>' ;
	}

	$array['content'] = $content ;

	echo json_encode( $array ) ;
	exit ;

?>