<?php
defined('COT_CODE') or die('Wrong URL');

if(cot_plugin_active('pricelist')) {
  require_once cot_incfile('pricelist', 'plug');

  list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('pricelist', 'any', 'RWA');
  if(!$usr['isadmin'] && $usr['maingrp'] == 6) $usr['isadmin'] = true;

  $uid = ($usr['isadmin'] ? cot_import('uid', 'P', 'INT') : $usr['id']);

  if($uid > 0) {
    $db->delete($db_pricelist, 'pl_uid='.$uid);

    $urrpl = array(
      'pl_uid' => $uid,
      'pl_cat' => '',
      'pl_name' => 0,
      'pl_type' => 0,
      'pl_cost_min' => 0,
      'pl_cost_max' => 0
    );
    $plarr = cot_import('pricelist', 'P', 'ARR');
    $pl_names = cot_import('pricelist_names', 'P', 'ARR');
    $pl_descs = cot_import('pricelist_descs', 'P', 'ARR');

    foreach($structure['market'] as $code => $cat) {
      $catcntpath = mb_substr_count($cat['path'], ".");
      if($catcntpath == 0) {
        if(count($plarr[$code]['name']) > 0) {
          foreach($plarr[$code]['name'] as $pc_i => $pc_name) {
            if(!empty($pc_name)) {
              $urrpl['pl_cat'] = $code;
              $urrpl['pl_name'] = $pc_name;
              $urrpl['pl_desc'] = $plarr[$code]['desc'][$pc_i];

              $pc_data = $plarr[$code];

              foreach($pricelis_types as $type => $data) {
                if($pc_data[$type]['min'][$pc_i] > 0) {
                  $urrpl['pl_type'] = $type;
                  $urrpl['pl_cost_min'] = (int)$pc_data[$type]['min'][$pc_i];
                  $urrpl['pl_cost_max'] = (int)((in_array('max', $data['fields']) && $pc_data[$type]['max'][$pc_i] > 0 && $pc_data[$type]['max'][$pc_i] > $pc_data[$type]['min'][$pc_i]) ? $pc_data[$type]['max'][$pc_i] : 0);

                  $db->insert($db_pricelist, $urrpl);
                }
              }
            }
          }
          if(!empty($pl_descs[$code]) || (!empty($pl_names[$code]) && $pl_names[$code] != $cat['title'])) {
            $urrpl['pl_cat'] = $code;
            $urrpl['pl_name'] = $pl_names[$code];
            $urrpl['pl_desc'] = $pl_descs[$code];

            $urrpl['pl_type'] = -1;
            $urrpl['pl_cost_min'] = 0;
            $urrpl['pl_cost_max'] = 0;

            $db->insert($db_pricelist, $urrpl);
          }
        }
      } elseif($catcntpath == 1) {
        $urrpl['pl_cat'] = $code;
        $urrpl['pl_desc'] = (!empty($pl_descs[$code]) ? $pl_descs[$code] : '');
        $urrpl['pl_name'] = ((!empty($pl_names[$code]) && $pl_names[$code] != $cat['title']) ? $pl_names[$code] : '');

        foreach($pricelis_types as $type => $data) {
          if($plarr[$code][$type]['min'] > 0) {
            $urrpl['pl_type'] = $type;
            $urrpl['pl_cost_min'] = (int)$plarr[$code][$type]['min'];
            $urrpl['pl_cost_max'] = (int)((in_array('max', $data['fields']) && $plarr[$code][$type]['max'] > 0 && $plarr[$code][$type]['max'] > $plarr[$code][$type]['min']) ? $plarr[$code][$type]['max'] : 0);

            $db->insert($db_pricelist, $urrpl);
          }
        }
      }
    }

    $return['success'] = 1;
  }
}

?>
