<?php

function display_process_inventory_adjustments_summary(&$po, $is_self=false, $editable=false)
{
    global $approval_statuses, $Refs;
    
    start_outer_table(TABLESTYLE2, "width=85%"); // outer table
        start_table(TABLESTYLE2);  
    
            table_section(1);

                label_row(_("Location: "), get_location_name($po->location_name));
                $project_name = get_project_record($po->project_id);
                label_row(_("Project: "), $project_name['description']);
                
//              label_row(_("Reference: "), $po->reference);
                if($po->trans_type == ST_INVENTORY_ISSUE_REQUEST){
                    $trans_type = ST_INVENTORY_ISSUE;
                }elseif($po->trans_type == ST_INVENTORY_RECEIPT_REQUEST){
                    $trans_type = ST_INVENTORY_RECEIPT;
                }
                $reference = $Refs->get_next($trans_type);
                ref_row(_("Reference:"), 'ref', '', $reference);
                
            table_section(2);

//                label_row(_("Date: "), ($po->tran_date));
            
                set_post_not_set_date('in_trans_date', $po->tran_date);
                date_row(_("Date: "), 'in_trans_date');
                if($po->sample_items == 1){
                    $yes_no = 'Yes';
                }else{
                    $yes_no = 'No';
                }
                label_row(_("Sample Items: "), $yes_no);
                
                $s = get_project_trans_view_str(ST_APPROVAL_LOG, $po->trans_no."&source_type_id=".$po->trans_type, "Approval Status");
                label_row($s,$approval_statuses[$po->approval_status]);
     
            table_section(3);

                label_row(_("Type: "), $po->movement_type_name);
                
                $linked_field_values = get_movement_type_linked_field_values($po->linked_field_type, $po->linked_field_id);
                if(strlen($linked_field_values["field_name"]) > 0){
                    label_row($linked_field_values["field_name"].':', $linked_field_values['value']);
                }

                $trans_no = $po->trans_no;
                label_row(_("Requisition No:"), get_trans_view_str($po->trans_type, $trans_no));

        end_table();
                
    
    end_outer_table(1);
}


function display_process_inventory_adjustments_details($title, &$order)
{
    global $path_to_root;

    if ( count($order->line_items)) {
        display_heading($title);
    }else{
        display_heading('No items to display.');
    }

    div_start('items_table');
        start_table(TABLESTYLE, "width=91%");
        
        
            
        $total = 0;
        $k = 0;  //row colour counter

        $id = find_submit('Edit');
        
        if(isset($order->line_items) && is_array($order->line_items)){
            $th = array(_("Item Code"), _("Item Description"), _("Unit"), _("Quantity"), _("Unit Cost"), _("Task"), _("Job"), _("Total"));
            table_header($th);
            
            foreach ($order->line_items as $line_no => $stock_item)
            {
                $total += ($stock_item->standard_cost * ($stock_item->tran_qty * $stock_item->convertion_factor));

                if ($id != $line_no){
                    alt_table_row_color($k);

                            view_stock_status_cell($stock_item->stock_id);
                            label_cell($stock_item->item_description);
                            label_cell($stock_item->alternate_unit_name, "align=center");
                            qty_cell($stock_item->tran_qty, false, get_qty_dec($stock_item->stock_id));
                            amount_decimal_cell(round($stock_item->standard_cost, 2));
                            label_cell($stock_item->task_description);
                            label_cell($stock_item->project_site_description);
                            amount_cell($stock_item->standard_cost * ($stock_item->tran_qty * $stock_item->convertion_factor));

                    end_row();
                }                
            }
            label_row(_("Total"), number_format2($total,user_price_dec()), "align=right colspan=7", "align=right");
        }

        end_table(2);
    div_end();
}


function display_process_inventory_adjustments_options_controls(&$order)
{
    start_table(TABLESTYLE);
    
        if(isset($order->comment) && strlen($order->comment) > 0){
            label_row(_("Memo:"), nl2br($order->comment)); 
        }
        
    end_table(2);
    
}

function can_process_inventory_adjustment_request(&$purchase_order)
{
    global $Refs;

    if($_SESSION['stock_adj']->trans_no > 0){
        $sah = get_stock_adjustment_request_header($_SESSION['stock_adj']->trans_no);
        if(!($sah['approval_status'] == AS_APPROVED)){
            $message = ("This Inventory Adjustment Request status does not allow processing it.");
            return array("success" => false, "focus_to" => '', "message" => $message);
        }
    }
    
	if (count($purchase_order->line_items) == 0){
		display_error(_("There must be at least one non empty item line."));
		return false;
	}
        if (!($purchase_order->project_id > 0)){
                $input_error = 1;
                display_error(_("A project should not be empty."));
                return;
        }

//	if (!$Refs->is_valid($purchase_order->reference)){
	if (!$Refs->is_valid($_POST['ref'])){
		display_error( _("Reference should not be empty."));
		return false;
	}

        $adj_req_type = $purchase_order->trans_type ;
        if($adj_req_type == ST_INVENTORY_ISSUE_REQUEST){
            $trans_type = ST_INVENTORY_ISSUE;
        }elseif($adj_req_type == ST_INVENTORY_RECEIPT_REQUEST){
            $trans_type = ST_INVENTORY_RECEIPT;
        }
    
        $ref_msg = reference_exists($_POST['ref'], $trans_type);
        if($ref_msg){
            display_error( $ref_msg);
            return false;
        }

        if (!($purchase_order->movement_type > 0)){
                $input_error = 1;
                display_error( _('Movement Type should not be empty.'));
                return false;
        }

//	if (!is_date(($purchase_order->tran_date))){
	if (!is_date($_POST['in_trans_date'])){
		display_error(_("The entered date for the adjustment is invalid."));
		return false;
	}elseif (!is_date_in_fiscalyear($_POST['in_trans_date'])){
		display_error(_("The entered date is not in fiscal year."));
		return false;
	} else {
//            if($purchase_order->trans_type == ST_INVENTORY_ISSUE){               
//		$failed_item = $purchase_order->check_qoh($purchase_order->location_name, $purchase_order->tran_date, !$_POST['Increase']);
//		if ($failed_item >= 0){
//			$line = $purchase_order->line_items[$failed_item];
//    		display_error(_("The adjustment cannot be processed because an adjustment item would cause a negative inventory balance :") .
//    			" " . $purchase_order->stock_id . " - " .  $purchase_order->item_description);
//			$_POST['Edit'.$failed_item] = 1; // enter edit mode
//			unset($_POST['Process']);
//		return false;
//		}
//            }
	}

        $msg = is_accounting_period_open($_POST['in_trans_date']);
        if ($msg['success'] !== true)
        {
            display_error($msg['message']);
            set_focus('in_trans_date');
            return false;
        }
    
//        $vtqd_msg = validate_transaction_quantity_details($purchase_order);
//        if ($vtqd_msg['success'] !== true){
//            display_error($vtqd_msg['message']);
//            return false;
//        }
	return array("success" => true);
}


?>