<?php
$general->createLog('customer_visit_add',$_POST);
    $entry_date = $_POST['entry_date'];
    $houre = $_POST['houre'];
    $minuet = $_POST['minuet'];

    $entry_time = strtotime($entry_date.' '.$houre.':'.$minuet);
    $customer_id = intval($_POST['customer_id']);
    $finished_product = $_POST['finished_product']??[];
    $gift_product = $_POST['gift_product']??[];
    $note = $_POST['note']??'';
    $customer = $db->get_rowData('customer','id',$customer_id);
    if($entry_time==false){$error=fl(); setMessage(63,'entry time');}
    elseif($entry_time<strtotime('-2 years')){$error=fl();setMessage(63,'entry time');}
    elseif($customer_id<1){$error=fl();setMessage(36,'customer');}
    elseif(empty($customer)){$error=fl();setMessage(63,'customer');}
    else{
        $product_ids=[];
        if(!empty($finished_product)){
            foreach($finished_product as $data){
                $product_id = $data['product_id'];
                $product_ids[$product_id] = $product_id;
            }
        }
        if(!empty($gift_product)){
            foreach($gift_product as $data){
                $product_id = $data['product_id'];
                $product_ids[$product_id] = $product_id;
            }
        }
        if(!empty($product_ids)){
            $products = $db->selectAll('products','where isActive=1 and id in('.implode(',',$product_ids).')','id,title,type');
            $general->arrayIndexChange($products);
        }
        else{
            $products=[];
        }
        if(!empty($finished_product)){
            foreach($finished_product as $data){
                $product_id = $data['product_id'];
                if(!isset($products[$product_id])){$error=fl(); setMessage(63,'product'); break;}
                $product =  $products[$product_id];
                if($product['type']!=PRODUCT_TYPE_FINISHED){$error=fl(); setMessage(63,'product');break;}
                if($data['quaintity']<1){$error=fl(); setMessage(63,'quaintity for '.$product['title']);break;}
            }
        }
        if(!isset($error) && !empty($gift_product)){
            foreach($gift_product as $data){
                $product_id = $data['product_id'];
                if(!isset($products[$product_id])){$error=fl(); setMessage(63,'product'); break;}
                $product =  $products[$product_id];
                if($product['type']!=PRODUCT_TYPE_GIFT_ITEM){$error=fl(); setMessage(63,'product');break;}
                if($data['quaintity']<1){$error=fl(); setMessage(63,'quaintity for '.$product['title']);break;}
            }
        }
        if(!isset($error)){
            $db->transactionStart();
            $data=[
                'entry_time'=>$entry_time,
                'customer_id'=>$customer_id,
                'note'=>$note,
            ];
            $db->arrayUserInfoAdd($data);
            $id = $db->insert('customer_visit',$data,true);
            if($id==false){$error=fl(); setMessage(66);}
            else{
                $code = $db->setAutoCode('customer_visit',$id);
                if(!$code){$error=fl();setMessage(66);}
                else{
                    if(!empty($finished_product)){
                        foreach($finished_product as $data){
                            $data['customer_visit_id'] = $id;
                            $insert = $db->insert('customer_visit_finished_product',$data);
                            if(!$insert){$error=fl(); setMessage(66);break;}
                        }
                    }
                    if(!isset($error) && !empty($gift_product)){
                        foreach($gift_product as $data){
                            $data['customer_visit_id'] = $id;
                            $insert = $db->insert('customer_visit_gift_product',$data);
                            if(!$insert){$error=fl(); setMessage(66);break;}
                        }
                    }
                }
            }
            $ac = false; 
            if(!isset($error)){
                $jArray['status'] = 1;
                $ac = true;
                setMessage(2,'Customer visit successfully');
            }
            $db->transactionStop($ac);
        }

    }
    $general->createLog('customer_visit_add',$jArray);