<?php

require_once("./../../import/helper.php");

/* Prep. */
$sql = mysql_connect('localhost', 'root', '1ptimu6');

mysql_select_db('crm');
mysql_query('SET NAMES utf8;');

function buildStatement(array $array) {
    $filteredArray = array_filter($array, 'strlen');
    $statementParts = array(
        'keys' => array(
            //
        ),
        'values' => array(
            //
        ),
    );
    
    foreach($filteredArray as $key => $value) {
        $statementKey = '`' . $key . '`';
        $statementValue = '\'' . $value . '\'';
        
        array_push($statementParts['keys'], $statementKey);
        array_push($statementParts['values'], $statementValue);
    }
    
    return '(' . implode(', ', $statementParts['keys']) . ') values (' . implode(', ', $statementParts['values']) . ')';
}

/* Db structure. */
$data = array(
    'ecmreceipts' => array(
        'id' => null,
        'name' => null,
        'description' => null,
        'date_entered' => null,
        'date_modified' => null,
        'modified_user_id' => null,
        'assigned_user_id' => null,
        'created_by' => null,
        'deleted' => null,
        'number' => null,
        'parent_type' => null,
        'parent_name' => null,
        'parent_id' => null,
        'type' => null,
        'ecmreceipt_id' => null,
        'ecmreceipt_name' => null,
        'status' => null,
        'register_date' => null,
        'sell_date' => null,
        'payment_date' => null,
        'parent_address_street' => null,
        'parent_address_city' => null,
        'parent_address_postalcode' => null,
        'parent_address_country' => null,
        'to_nip' => null,
        'subtotal' => null,
        'total' => null,
        'discount' => null,
        'to_vatid' => null,
        'to_is_vat_free' => null,
        'header_text' => null,
        'footer_text' => null,
        'ads_text' => null,
        'template_id' => null,
        'template_name' => null,
        'accepted' => null,
        'email_id' => null,
        'ecmlanguage' => null,
        'ecmpaymentcondition_id' => null,
        'ecmpaymentcondition_text' => null,
        'wz_id' => null,
        'document_no' => null,
        'parent_contact_name' => null,
        'parent_contact_title' => null,
        'contact_id' => null,
        'correct_id' => null,
        'currency_id' => null,
        'order_no' => null,
        'supplier_code' => null,
        'delivery_place' => null,
        'order_qty' => null,
        'currency_value' => null,
        'paid' => null,
        'wz_name' => null,
        'correct_reason' => null,
        'so_id' => null,
        'netto_weight' => null,
        'brutto_weight' => null,
        'stock_id' => null,
        'pdf_type' => null,
        'parent_shipping_address_name' => null,
        'parent_shipping_address_street' => null,
        'parent_shipping_address_city' => null,
        'parent_shipping_address_postalcode' => null,
        'parent_shipping_address_country' => null,
        'prepaid' => null,
        'prepaid_nr' => null,
        'paid_val' => null,
        'payment_date_d' => null,
        'payment_method' => null,
        'payment_method_paid' => null,
        'vats_summary' => null,
        'inside_inv_number' => null,
        'printed' => null,
    ),
    'ecmreceiptitems' => array(
        'id' => null,
        'date_entered' => null,
        'date_modified' => null,
        'modified_user_id' => null,
        'assigned_user_id' => null,
        'created_by' => null,
        'deleted' => null,
        'ecmreceipt_id' => null,
        'ecmproduct_id' => null,
        'position' => null,
        'code' => null,
        'name' => null,
        'quantity' => null,
        'subprice' => null,
        'price' => null,
        'startprice' => null,
        'discount' => null,
        'subtotal' => null,
        'total' => null,
        'dd_unit_id' => null,
        'dd_unit_name' => null,
        'ecmvat_id' => null,
        'ecmvat_name' => null,
        'ecmvat_value' => null,
        'ecmproductcategory_id' => null,
        'currency_id' => null,
        'currency_name' => null,
        'tax_code' => null,
        'purchase_price' => null,
        'recipient_code' => null,
        'ecmreceiptitem_id' => null,
        'netto_weight' => null,
        'brutto_weight' => null,
        'sub_price' => null,
        'ecmstockdocout_id' => null,
        'product_type' => null,
        'parent_doc_id' => null,
        'parent_doc_type' => null,
        'parent_doc_item_id' => null,
        'old_subprice' => null,
        'old_price' => null,
        'old_quantity' => null,
        'old_ecmvat_id' => null,
        'old_ecmvat_name' => null,
        'old_ecmvat_value' => null,
        'old_discount' => null,
    ),
);

$duplicatedData = array(
    'ecmreceipts' => array(
        'parent_type' => 'Accounts',
        'parent_name' => '"KARDAN" SP. Z O.O.',
        'parent_id' => 3674695,
        'type' => 0,
        'status' => 'accepted',
        'parent_address_street' => 'UL.ZIELONOGÓRSKA 36',
        'parent_address_city' => 'SZCZECIN',
        'parent_address_postalcode' => '71-084',
        'subtotal' => 100,
        'total' => 123,
        'discount' => 0,
        'template_id' => 'ef0c4dea-ddf3-de51-9d08-512257d4b675',
        'template_name' => 'BSL Truck',
        'ecmlanguage' => 'pl_pl',
        'stock_id' => '2962f1ad-79f4-a1ad-385e-4f286ad7acb5',
        'pdf_type' => 'K',
    ),
    'ecmreceiptitems' => array(
        'ecmproduct_id' => '75c7104b-816f-996a-9739-501aa06921d5',
        'position' =>  0,
        'code' =>  '9ES120743001',
        'name' =>  'SC31 SZKŁO LAMPY GŁÓWNEJ',
        'quantity' =>  2,
        'subprice' =>  40.65,
        'price' =>  50,
        'startprice' =>  40.65,
        'discount' =>  0,
        'subtotal' =>  81.3,
        'total' =>  100,
        'dd_unit_id' =>  'SZT',
        'ecmvat_id' =>  '9ec174d1-db5e-8db6-9195-4e6e4a041e40',
        'ecmvat_name' =>  '23%',
        'ecmvat_value' =>  23,
        'currency_id' => 'undefined',
        'currency_name' => 'Polski Złoty',
        'purchase_price' =>  0,
        'netto_weight' =>  0.00,
        'brutto_weight' => 0.00,
    ),
);

/* Main functionality. */
$startTime = microtime(true);

for($i = 0, $iCounter = 100000; $i < $iCounter; $i++) {
    $date = date('Y-m-d');
    
    $receiptId = create_guid();
    $duplicatedData['ecmreceiptitems']['id'] = create_guid();
    $duplicatedData['ecmreceipts']['document_no'] = ($i+1).'/2013';
    
    $duplicatedData['ecmreceipts']['id'] = $duplicatedData['ecmreceiptitems']['ecmreceipt_id'] = $receiptId;

    $duplicatedData['ecmreceipts']['date_entered'] = $duplicatedData['ecmreceiptitems']['date_entered'] = $date;
    $duplicatedData['ecmreceipts']['date_modified'] = $duplicatedData['ecmreceiptitems']['date_modified'] = $date;

    // dump(buildStatement($duplicatedData['ecmreceipts']));
    // dump(buildStatement($duplicatedData['ecmreceiptitems']));
    // exit;
    
    $query1 = 'insert into `ecmreceipts` ' . buildStatement($duplicatedData['ecmreceipts']) . ';';
    $query2 = 'insert into `ecmreceiptitems` ' . buildStatement($duplicatedData['ecmreceiptitems']) . ';';
    
    // dump($query1);
    // dump($query2);
    // exit;
    
    $res1 = mysql_query($query1);
    $res2 = mysql_query($query2);
    
    // dump($res1, $res2);
    // exit;
}

$stopTime = microtime(true);