<commit_before><?php
/**
 * Created by PhpStorm.
 * User: genjerator
 * Date: 14.10.16.
 * Time: 16.44
 */

namespace Numa\DOADMSBundle\Util;


use Numa\DOADMSBundle\Entity\Billing;
use Numa\DOAAdminBundle\Entity\Item;
use Numa\DOADMSBundle\Entity\RelatedDoc;
use Numa\DOADMSBundle\Entity\Sale;
use Numa\DOADMSBundle\Entity\SaleRelatedDoc;
use Numa\DOADMSBundle\Entity\Vendor;
use Symfony\Component\HttpFoundation\File\File;

class SaleLib
{
    protected $container;

    /**
     * ListingFormHandler constructor.
     * @param ContainerInterface $container
     */
    public function __construct($container) // this is @service_container
    {
        $this->container = $container;
    }

    public function createSaleByBilling(Billing $billing)
    {
//        dump($billing->getItem());die();
        if (!empty($billing->getSalePrice()) || !empty($billing->getWarranty()) || !empty($billing->getAdminFee()) || !empty($billing->getBankRegistrationFee()) || !empty($billing->getProtectionPkg()) || !empty($billing->getLifeInsurance()) || !empty($billing->getDisabilityInsurance()) || !empty($billing->getOtherMisc1()) || !empty($billing->getOtherMisc2()) || !empty($billing->getTaxt1Name()) || !empty($billing->getTaxt2Name()) || !empty($billing->getTaxt3Name()) || !empty($billing->getTax1()) || !empty($billing->getTax2()) || !empty($billing->getTax3()) || !empty($billing->getSalesPerson())) {
            $em = $this->container->get('doctrine.orm.entity_manager');
            //check if vin exists already
            $item = $billing->getItem();
            if ($item instanceof Item) {
                $sale = $item->getSale();
//                dump($item->getSaleId());
                if (!$sale instanceof Sale) {
                    $sale = new Sale();
//                    $sale->setItem($item);
                    $em->persist($sale);
                }
                $sale->setSellingPrice($billing->getSalePrice());
                $sale->setWarranty1($billing->getWarranty());
                $sale->setAdminFees1($billing->getAdminFee());
                $sale->setDocFees1($billing->getBankRegistrationFee());
                $sale->setProtectPkg1($billing->getProtectionPkg());
                $sale->setLifeInsur($billing->getLifeInsurance());
                $sale->setDisabilityIns1($billing->getDisabilityInsurance());
                $sale->setTax1In($billing->getOtherMisc1());
                $sale->setTax2In($billing->getOtherMisc2());
                $sale->setOther1Des($billing->getTaxt1Name());
                $sale->setOther2Des($billing->getTaxt2Name());
                $sale->setOther3Des($billing->getTaxt3Name());
                $sale->setOther1($billing->getTax1());
                $sale->setOther2($billing->getTax2());
                $sale->setOther3($billing->getTax3());
                $sale->setSalesPerson($billing->getSalesPerson());

                $em->flush($sale);
                $item->setSaleId($sale->getId());
                $em->flush($item);
            }
        }
    }

    public function uploadRelatedDocs(Sale $sale, File $file, $upload_url, $upload_path)
    {
        $filename = $file->getClientOriginalName();

        //make a folder if not exists
        $dir = $upload_path.$sale->getId();

        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }

        //prepare filename for the image
        $filename = $sale->getId() . "_" . $filename;
        $filename = str_replace(array(" ", '%'), "-", $filename);

        $dirandfile = $sale->getId()."/".$filename;

        //full path to the uploaded image
        $fullpathfile = $dir . "/" . $filename;

        $filemoved = $file->move($dir, $filename);
        if($filemoved instanceof File) {

            $em = $this->container->get('doctrine.orm.entity_manager');
            $relatedDoc = new RelatedDoc();
            $relatedDoc->setName($filename);
            $relatedDoc->setTitle($filename);
            $relatedDoc->setSrc($dirandfile);

            $saleRelatedDocs = new SaleRelatedDoc();
            $saleRelatedDocs->setSale($sale);
            $saleRelatedDocs->setRelatedDoc($relatedDoc);
            $em->persist($relatedDoc);
            $em->persist($saleRelatedDocs);
            $em->flush();
        }
    }

    public function deleteRelatedDoc(RelatedDoc $doc){
        $em = $this->container->get('doctrine.orm.entity_manager');
        $file = $this->container->getParameter("related_docs_path").$doc->getSrc();

        if(file_exists($file)){
            unlink($file);
        }
        $em->remove($doc);
        $em->flush();
    }

    public function getAllVendors(Item $item){
        $sale = $item->getSale();
        if($sale instanceof Sale){
            $class_methods = get_class_methods(Sale::class);
            $output = array_filter($class_methods, function ($f) { return stripos($f,"get")===0 && stripos($f,"vendorid")>0 && $f!="getVendorId"; });
            $props = array();
            foreach($output as $vendorF){
                $temp = array();
                //$prop = str_replace("get","",$vendorF);
                $prop = str_replace("VendorId","",$vendorF);
                $descF = str_replace("get","",$prop);
                $descF = "getDesc".$descF;

                $vendorId = $sale->{$vendorF}();
                $amount   = $sale->{$prop}();
                if(method_exists($sale,$descF)) {
                    $desc = $sale->{$descF}();
                }
                if(!empty($vendorId)){
                    $em = $this->container->get('doctrine.orm.entity_manager');
                    dump($vendorId);
                    $vendor  =$em->getRepository(Vendor::class)->find($vendorId);
                    if($vendor instanceof Vendor){
                        $temp['vendor'] = $vendor;
                        $temp['amount'] = $amount;
                        if(method_exists($sale,$descF)) {
                            $desc = $sale->{$descF}();
                            $temp['desc']   = $desc;
                        }

                    }
                }
                $props[$prop] = $temp;
                //dump($sale->{$vendorF}());
            }
        }
        dump($props);
        return $props;
    }
}<commit_msg>POs ale<commit_after><?php
/**
 * Created by PhpStorm.
 * User: genjerator
 * Date: 14.10.16.
 * Time: 16.44
 */

namespace Numa\DOADMSBundle\Util;


use Numa\DOADMSBundle\Entity\Billing;
use Numa\DOAAdminBundle\Entity\Item;
use Numa\DOADMSBundle\Entity\RelatedDoc;
use Numa\DOADMSBundle\Entity\Sale;
use Numa\DOADMSBundle\Entity\SaleRelatedDoc;
use Numa\DOADMSBundle\Entity\Vendor;
use Symfony\Component\HttpFoundation\File\File;

class SaleLib
{
    protected $container;

    /**
     * ListingFormHandler constructor.
     * @param ContainerInterface $container
     */
    public function __construct($container) // this is @service_container
    {
        $this->container = $container;
    }

    public function createSaleByBilling(Billing $billing)
    {
//        dump($billing->getItem());die();
        if (!empty($billing->getSalePrice()) || !empty($billing->getWarranty()) || !empty($billing->getAdminFee()) || !empty($billing->getBankRegistrationFee()) || !empty($billing->getProtectionPkg()) || !empty($billing->getLifeInsurance()) || !empty($billing->getDisabilityInsurance()) || !empty($billing->getOtherMisc1()) || !empty($billing->getOtherMisc2()) || !empty($billing->getTaxt1Name()) || !empty($billing->getTaxt2Name()) || !empty($billing->getTaxt3Name()) || !empty($billing->getTax1()) || !empty($billing->getTax2()) || !empty($billing->getTax3()) || !empty($billing->getSalesPerson())) {
            $em = $this->container->get('doctrine.orm.entity_manager');
            //check if vin exists already
            $item = $billing->getItem();
            if ($item instanceof Item) {
                $sale = $item->getSale();
//                dump($item->getSaleId());
                if (!$sale instanceof Sale) {
                    $sale = new Sale();
//                    $sale->setItem($item);
                    $em->persist($sale);
                }
                $sale->setSellingPrice($billing->getSalePrice());
                $sale->setWarranty1($billing->getWarranty());
                $sale->setAdminFees1($billing->getAdminFee());
                $sale->setDocFees1($billing->getBankRegistrationFee());
                $sale->setProtectPkg1($billing->getProtectionPkg());
                $sale->setLifeInsur($billing->getLifeInsurance());
                $sale->setDisabilityIns1($billing->getDisabilityInsurance());
                $sale->setTax1In($billing->getOtherMisc1());
                $sale->setTax2In($billing->getOtherMisc2());
                $sale->setOther1Des($billing->getTaxt1Name());
                $sale->setOther2Des($billing->getTaxt2Name());
                $sale->setOther3Des($billing->getTaxt3Name());
                $sale->setOther1($billing->getTax1());
                $sale->setOther2($billing->getTax2());
                $sale->setOther3($billing->getTax3());
                $sale->setSalesPerson($billing->getSalesPerson());

                $em->flush($sale);
                $item->setSaleId($sale->getId());
                $em->flush($item);
            }
        }
    }

    public function uploadRelatedDocs(Sale $sale, File $file, $upload_url, $upload_path)
    {
        $filename = $file->getClientOriginalName();

        //make a folder if not exists
        $dir = $upload_path.$sale->getId();

        if (!file_exists($dir)) {
            mkdir($dir, 0777, true);
        }

        //prepare filename for the image
        $filename = $sale->getId() . "_" . $filename;
        $filename = str_replace(array(" ", '%'), "-", $filename);

        $dirandfile = $sale->getId()."/".$filename;

        //full path to the uploaded image
        $fullpathfile = $dir . "/" . $filename;

        $filemoved = $file->move($dir, $filename);
        if($filemoved instanceof File) {

            $em = $this->container->get('doctrine.orm.entity_manager');
            $relatedDoc = new RelatedDoc();
            $relatedDoc->setName($filename);
            $relatedDoc->setTitle($filename);
            $relatedDoc->setSrc($dirandfile);

            $saleRelatedDocs = new SaleRelatedDoc();
            $saleRelatedDocs->setSale($sale);
            $saleRelatedDocs->setRelatedDoc($relatedDoc);
            $em->persist($relatedDoc);
            $em->persist($saleRelatedDocs);
            $em->flush();
        }
    }

    public function deleteRelatedDoc(RelatedDoc $doc){
        $em = $this->container->get('doctrine.orm.entity_manager');
        $file = $this->container->getParameter("related_docs_path").$doc->getSrc();

        if(file_exists($file)){
            unlink($file);
        }
        $em->remove($doc);
        $em->flush();
    }

    public function getAllVendors(Item $item){
        $sale = $item->getSale();
        if($sale instanceof Sale){
            $class_methods = get_class_methods(Sale::class);
            $output = array_filter($class_methods, function ($f) { return stripos($f,"get")===0 && stripos($f,"vendorid")>0 && $f!="getVendorId"; });
            $props = array();
            $byVendors = array();
            foreach($output as $vendorF){
                $temp = array();
                //$prop = str_replace("get","",$vendorF);
                $prop = str_replace("VendorId","",$vendorF);
                $descF = str_replace("get","",$prop);
                $descF = "getDesc".$descF;

                $vendorId = $sale->{$vendorF}();
                $amount   = $sale->{$prop}();
                if(method_exists($sale,$descF)) {
                    $desc = $sale->{$descF}();
                }

                if(!empty($vendorId)){
                    $em = $this->container->get('doctrine.orm.entity_manager');
                    dump($vendorId);
                    $vendor  =$em->getRepository(Vendor::class)->find($vendorId);
                    if($vendor instanceof Vendor){

                        $temp['vendor'] = $vendor;
                        $temp['amount'] = $amount;
                        $temp['property'] = $prop;
                        if(method_exists($sale,$descF)) {
                            $desc = $sale->{$descF}();
                            $temp['desc']   = $desc;
                        }

                        $byVendors[$vendorId][]=$temp;
                    }
                }
                $props[$prop] = $temp;
                //dump($sale->{$vendorF}());
            }
        }
        dump($byVendors);
        return $props;
    }
}
{'fraction_non_alphanumeric': 0.17167584579071596, 'fraction_numerical': 0.007081038552321007, 'mean_word_length': 2.05626352488579, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 191, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4214008', 'n_tokens_mistral': 3928, 'n_tokens_neox': 3652, 'n_words': 512}