<?php
global $app_list_strings;
if($_REQUEST['record']!=''){
    $pp=new EcmProduct();
    $pp->retrieve($_REQUEST['record']);
    $ar_vendor = array();
    foreach ($pp->component_list as $key => $v) {
        $pr= new EcmProduct();
        $pr->retrieve($v['ecmcomponent_id']);
        if ($pr->vendor_id != '') {
                $item_id = create_guid();
                $v2= new EcmVat;
                $v2->retrieve($pr->vat_id);
                $arr = array(
                        'id' => $item_id,
                        'ecmpurchaseorder_id' => $id,
                        'product_id' => $pr->id,
                        'position' => $position,
                        'product_code' => $pr->code,
                        'name' => $pr->name,
                        'price_start' => '0',
                        'price_cbm' => '0',
                        'price_moq' => '0',
                        'ecmvat_id' => $pr->vat_id,
                        'ecmvat_name' => $v2->name,
                        'ecmvat_value' => $v2->value,
                        'total_netto' => '0',
                        'vendor_id' => $pr->vendor_id,
                        'vendor_name' => $pr->vendor_name,
                        'quantity' => $zam2,
                        'unit_id' => $pr->unit_id,
                        'unit_name' =>  $app_list_strings['ecmproducts_unit_dom'][$pr->unit_id]
                );
                
                $ar_vendor[$pr->vendor_id][] = $arr;
        
        } else {
            $v2= new EcmVat;
            $v2->retrieve($pr->vat_id);
                $item_id = create_guid();
                $arr = array(
                        'id' => $item_id,
                        'ecmpurchaseorder_id' => $id,
                        'product_id' => $pr->id,
                        'position' => $position,
                        'product_code' => $pr->code,
                        'name' => $pr->name,
                        'price_start' => '0',
                        'price_cbm' => '0',
                        'price_moq' => '0',
                        'ecmvat_id' => $pr->vat_id,
                        'ecmvat_name' => $v2->name,
                        'ecmvat_value' => $v2->value,
                        'total_netto' => '0',
                        'vendor_id' => $pr->vendor_id,
                        'vendor_name' => $pr->vendor_name,
                        'quantity' => $zam2,
                        'unit_id' => $pr->unit_id,
                        'unit_name' =>  $app_list_strings['ecmproducts_unit_dom'][$pr->unit_id]
                );
                $ar_vendor[0][] = $arr;
            
        }
    }

    foreach ($ar_vendor as $key => $v) {
        echo 'Utwórzono nowe zamówienia:<br>';
        if (count($ar_vendor[$key]) > 0) {
            // / mk PO
            $po = new EcmPurchaseOrder();
            $po->number = $po->generateNumber();
            $po->document_no = $po->formatNumber($po->number);
            $po->register_date = date('Y-m-d');
            $po->status = 'registered';
            $po->assigned_user_id = $current_user->id;
            if ($key == '0') {
                $po->parent_name = 'Dominik Brzóska "KOMPIX"';
                $po->parent_id = 'cd2c8ed8-f74a-ab2c-dfec-53f3395be206';
            } else {
                
                $ac = new Account();
                $ac->retrieve($key);
                $po->parent_id = $key;
                $po->parent_name = $ac->name;
            }
            $po->position_list = $ar_vendor[$key];
            $id=$po->save();
           echo "<a href='index.php?module=EcmPurchaseOrders&action=DetailView&record=".$id."'>".$po->document_no."</a><br>";
        }
    }
}
?>