<?php
namespace App\Controllers;
// use App\Models\VinDescriptionModel;
use Config\Services;
//use Firebase\JWT\JWT;

class Pos extends BaseController{
    private $dbx;

    public function completevendorsale(){
    	$error = 1 ;
        $this->dbx   = \Config\Database::connect();
        
        $products = json_decode($this->request->getVar('products'), true);
        $client = json_decode($this->request->getVar('client'), true);
        $vendor = json_decode($this->request->getVar('vendor'), true);
        $saletotalamount = $this->request->getVar('saletotalamount');
        
        $now = date('Y-m-d H:i:s', time());
        
        $invno = 100001;
        $R = $this->dbx->query("select count(distinct sal_invoiceno) as 'all' from `sales` where `vnd_companyid`='{$vendor['rbun']['companyid']}'");
        $A = $R->getResultArray();
        if(sizeof($A)>0){
            $invno = $invno + $A[0]['all'];
        }
        
        for($i = 0; $i < count($products); $i++){
            $theVehicle = $products[$i]['item']['extra'];
            $theProduct = $products[$i]['item']['raw'];
            $data = [
                'cli_firstname'     => $client['firstname'],
                'cli_phone'         => $client['phone'],
                'cli_whatsapp'      => $client['whatsapp'],
                'cli_region'        => $client['region'],
                'cli_town'          => $client['town'],
                'cli_district'      => $client['district'],
                'cli_address'       => $client['address'],
                'cli_vin'           => $theVehicle['cvin'],
                
                'veh_vfam'          => $theVehicle['vfam'],
                'veh_make'          => $theVehicle['make'],
                'veh_model'         => $theVehicle['model'],
                'veh_makecode'      => $theVehicle['makecode'],
                'veh_modelcode'     => $theVehicle['modelcode'],
                'veh_year'          => $theVehicle['year'],
                'veh_module'        => $theVehicle['module'],
                'veh_modulesub'     => $theVehicle['modulesub'],
                
                'pro_brand'         => $theProduct['probrand'],
                'pro_partno'        => $theProduct['afpartno'],
                'pro_desc'          => $theProduct['plugtype'],
                'pro_type'          => '',
                'pro_qty'           => $theProduct['proqty'],
                'pro_localref'      => $theProduct['localref'],
                'pro_oemno'         => $theProduct['oemno'],
                'pro_oemman'        => $theProduct['oemman'],
                'pro_price'         => $theProduct['price'],
                
                'sal_invoiceno'     => $invno,
                'sal_type'          => 'Cash',
                'sal_qty'           => $products[$i]['saleqty'],
                'sal_uplift'        => '0',
                'sal_shipping'      => '0',
                'sal_tax'           => '0',
                'sal_commission'    => '0',
                'sal_totalamount'   => $saletotalamount,
                'sal_paymentmode'   => 'N/A',
                'sal_date'          => $now,
                
                'vnd_loginid'       => $vendor['uid'],
                'vnd_loginname'     => $vendor['firstname']." ".$vendor['lastname'],
                'vnd_companyid'     => $vendor['rbun']['companyid'],
                'vnd_companyname'   => $vendor['rbun']['companyname'],
                'vnd_companyemail'  => $vendor['rbun']['companyemail'],
                'vnd_streetname'    => $vendor['rbun']['streetname'],
                'vnd_streetno'      => $vendor['rbun']['streetno'],
                'vnd_companyphone'  => $vendor['rbun']['companyphone'],
                'vnd_region'        => $vendor['rbun']['region'],
                'vnd_district'      => $vendor['rbun']['district'],
                'vnd_town'          => $vendor['rbun']['town'],
                'vnd_mdname'        => $vendor['rbun']['mdname'],
                'vnd_mdphone'       => $vendor['rbun']['mdphone'],
                'vnd_storekeepername' => $vendor['rbun']['storekeepername'],
                'vnd_storekeeperphone' => $vendor['rbun']['storekeeperphone'],
    		];
    		
    		$this->dbx->table('sales')->insert($data);
        }
        
		$W = [];
		$W['status'] = $this->dbx->error()['code'];
		$W['extra'] = $invno;
		return json_encode($W, JSON_HEX_APOS | JSON_HEX_QUOT);
    }
}