Ejemplo n.º 1
0
function _fiche_ligne(&$form, &$of, $type)
{
    global $db, $conf, $langs, $hookmanager;
    //TODO rules guys ! To Facto ! AA
    $formProduct = new FormProduct($db);
    $PDOdb = new TPDOdb();
    $TRes = array();
    foreach ($of->TAssetOFLine as $k => &$TAssetOFLine) {
        $product =& $TAssetOFLine->product;
        if (is_null($product)) {
            $product = new Product($db);
            $product->fetch($TAssetOFLine->fk_product);
            $product->fetch_optionals();
        }
        $conditionnement = $TAssetOFLine->conditionnement;
        if (!empty($conf->asset->enabled)) {
            $TAssetType = new TAsset_type();
            $TAssetType->load($PDOdb, $product->array_options['options_type_asset']);
            $conditionnement_unit = $TAssetType->measuring_units == 'unit' || $TAssetType->gestion_stock == 'UNIT' ? 'unité(s)' : $TAssetOFLine->libUnite();
        } else {
            $conditionnement_unit = 'unité(s)';
            // TODO translate
        }
        //$conditionnement_unit = $TAssetOFLine->libUnite();
        if ($TAssetOFLine->measuring_units != 'unit' && !empty($TAssetOFLine->measuring_units)) {
            $conditionnement_label = ' / ' . $conditionnement . " " . $conditionnement_unit;
            $conditionnement_label_edit = ' par ' . $form->texte('', 'TAssetOFLine[' . $k . '][conditionnement]', $conditionnement, 5, 5, '', '') . $conditionnement_unit;
        } else {
            $conditionnement_label = $conditionnement_label_edit = '';
        }
        if ($TAssetOFLine->type == "NEEDED" && $type == "NEEDED") {
            $stock_needed = TAssetOF::getProductStock($product->id);
            $TLine = array('id' => $TAssetOFLine->getId(), 'idprod' => $form->hidden('TAssetOFLine[' . $k . '][fk_product]', $product->id), 'lot_number' => $of->status == 'DRAFT' ? $form->texte('', 'TAssetOFLine[' . $k . '][lot_number]', $TAssetOFLine->lot_number, 15, 50, 'type_product="NEEDED" fk_product="' . $product->id . '" rel="lot-' . $TAssetOFLine->getId() . '" ', 'TAssetOFLineLot') : $TAssetOFLine->lot_number, 'libelle' => $product->getNomUrl(1) . ' ' . $product->label . ' - ' . ($stock_needed > 0 ? $langs->trans("Stock") . " : " . $stock_needed : '<span style="color:red;font-weight:bold;">' . $langs->trans("Stock") . " : " . $stock_needed . '</span>') . _fiche_ligne_asset($PDOdb, $form, $of, $TAssetOFLine, 'NEEDED'), 'qty_needed' => $TAssetOFLine->qty_needed . $conditionnement_label, 'qty' => $of->status == 'DRAFT' ? $form->texte('', 'TAssetOFLine[' . $k . '][qty]', $TAssetOFLine->qty, 5, 50) : $TAssetOFLine->qty, 'qty_used' => $of->status == 'OPEN' || $of->status == 'CLOSE' ? $form->texte('', 'TAssetOFLine[' . $k . '][qty_used]', $TAssetOFLine->qty_used, 5, 50) : $TAssetOFLine->qty_used, 'qty_toadd' => $TAssetOFLine->qty - $TAssetOFLine->qty_used, 'workstations' => $conf->workstation->enabled ? $TAssetOFLine->visu_checkbox_workstation($db, $of, $form, 'TAssetOFLine[' . $k . '][fk_workstation][]') : '', 'delete' => $form->type_aff == 'edit' && ($of->status == 'DRAFT' || !empty($conf->global->OF_USE_DESTOCKAGE_PARTIEL) && $of->status != 'CLOSE' && empty($TAssetOFLine->qty_used)) ? '<a href="javascript:deleteLine(' . $TAssetOFLine->getId() . ',\'NEEDED\');">' . img_picto('Supprimer', 'delete.png') . '</a>' : '', 'fk_entrepot' => !empty($conf->global->ASSET_MANUAL_WAREHOUSE) && ($of->status == 'DRAFT' || $of->status == 'VALID') && $form->type_aff == 'edit' ? $formProduct->selectWarehouses($TAssetOFLine->fk_entrepot, 'TAssetOFLine[' . $k . '][fk_entrepot]', '', 0, 0, $TAssetOFLine->fk_product) : $TAssetOFLine->getLibelleEntrepot($PDOdb), 'note_private' => $of->status == 'DRAFT' ? $form->zonetexte('', 'TAssetOFLine[' . $k . '][note_private]', $TAssetOFLine->note_private, 50, 1) : $TAssetOFLine->note_private);
            $action = $form->type_aff;
            $parameter = array('of' => &$of, 'line' => &$TLine, 'type' => 'NEEDED');
            $res = $hookmanager->executeHooks('lineObjectOptions', $parameter, $TAssetOFLine, $action);
            if ($res > 0 && !empty($hookmanager->resArray)) {
                $TLine = $hookmanager->resArray;
            }
            $TRes[] = $TLine;
        } elseif ($TAssetOFLine->type == "TO_MAKE" && $type == "TO_MAKE") {
            if (empty($TAssetOFLine->TFournisseurPrice)) {
                $TAssetOFLine->loadFournisseurPrice($PDOdb);
            }
            // Permet de sélectionner par défaut "(Fournisseur "Interne" => Fabrication interne)" si le produit TO_MAKE n'a pas de stock lorsqu'on est en mode edit et que la ligne TO_MAKE n'a pas encore de prix fournisseur enregistré
            dol_include_once('/product/class/product.class.php');
            $p = new Product($db);
            $selected = 0;
            if ($p->fetch($TAssetOFLine->fk_product)) {
                $p->load_stock();
                $p->stock_reel;
                if ($TAssetOFLine->type === 'TO_MAKE' && $p->stock_reel <= 0 && $_REQUEST['action'] === 'edit') {
                    $selected = -2;
                }
            }
            // *************************************************************
            $Tab = array();
            foreach ($TAssetOFLine->TFournisseurPrice as &$objPrice) {
                $label = "";
                //Si on a un prix fournisseur pour le produit
                if ($objPrice->price > 0) {
                    $unit = $objPrice->quantity == 1 ? 'Unité' : 'Unités';
                    $label .= floatval($objPrice->price) . ' ' . $conf->currency . ' - ' . $objPrice->quantity . ' ' . $unit . ' -';
                }
                //Affiche le nom du fournisseur
                $label .= ' (Fournisseur "' . utf8_encode($objPrice->name) . '"';
                //Prix unitaire minimum si renseigné dans le PF
                if ($objPrice->quantity > 0) {
                    ' ' . $objPrice->quantity . ' pièce(s) min,';
                }
                //Affiche le type du PF :
                if ($objPrice->compose_fourni) {
                    //			soit on fabrique les composants
                    $label .= ' => Fabrication interne';
                } elseif ($objPrice->quantity <= 0) {
                    //			soit on a le produit finis déjà en stock
                    $label .= ' => Sortie de stock';
                }
                if ($objPrice->quantity > 0) {
                    //				soit on commande a un fournisseur
                    $label .= ' => Commande fournisseur';
                }
                $label .= ")";
                $Tab[$objPrice->rowid] = array('label' => $label, 'compose_fourni' => $objPrice->compose_fourni ? $objPrice->compose_fourni : 0);
            }
            if ($conf->nomenclature->enabled) {
                dol_include_once('/nomenclature/class/nomenclature.class.php');
                if ($of->status == 'DRAFT' && !$TAssetOFLine->nomenclature_valide) {
                    $TNomenclature = TNomenclature::get($PDOdb, $TAssetOFLine->fk_product, true);
                    if (count($TNomenclature) > 0) {
                        $nomenclature = '<div>' . $form->combo('', 'TAssetOFLine[' . $k . '][fk_nomenclature]', $TNomenclature, $TAssetOFLine->fk_nomenclature);
                        if ($form->type_aff == 'edit') {
                            $nomenclature .= '<a href="#" class="valider_nomenclature" data-id_of="' . $of->getId() . '" data-product="' . $TAssetOFLine->fk_product . '" data-of_line="' . $TAssetOFLine->rowid . '">Valider</a>';
                        } else {
                            $nomenclature .= " - Nomenclature à sélectionner";
                        }
                        $nomenclature .= '</div>';
                    } else {
                        $nomenclature = '';
                    }
                } else {
                    $n = new TNomenclature();
                    $n->load($PDOdb, $TAssetOFLine->fk_nomenclature);
                    $nomenclature = '<div>' . (string) $n;
                    $picture = $TAssetOFLine->nomenclature_valide ? 'ok.png' : 'no.png';
                    $nomenclature .= ' <img src="img/' . $picture . '" style="padding-left: 2px; vertical-align: middle;" /></div>';
                }
            }
            //($of->status=='DRAFT') ? $form->combo('', 'TAssetOFLine['.$k.'][fk_nomenclature]', _getArrayNomenclature($PDOdb, $TAssetOFLine), $TAssetOFLine->fk_nomenclature) : _getTitleNomenclature($PDOdb, $TAssetOFLine->fk_nomenclature)
            $stock_tomake = TAssetOF::getProductStock($product->id);
            $TLine = array('id' => $TAssetOFLine->getId(), 'idprod' => $form->hidden('TAssetOFLine[' . $k . '][fk_product]', $product->id), 'lot_number' => $of->status == 'DRAFT' ? $form->texte('', 'TAssetOFLine[' . $k . '][lot_number]', $TAssetOFLine->lot_number, 15, 50, 'type_product="TO_MAKE" fk_product="' . $product->id . '"', 'TAssetOFLineLot') : $TAssetOFLine->lot_number, 'libelle' => $product->getNomUrl(1) . ' ' . $product->label . ' - ' . $langs->trans("Stock") . " : " . $stock_tomake . _fiche_ligne_asset($PDOdb, $form, $of, $TAssetOFLine, false), 'nomenclature' => $nomenclature, 'addneeded' => $form->type_aff == 'edit' && $of->status == 'DRAFT' ? '<a href="#null" statut="' . $of->status . '" onclick="addAllLines(' . $of->getId() . ',' . $TAssetOFLine->getId() . ',this);">' . img_picto('Mettre à jour les produits nécessaires', 'object_technic.png') . '</a>' : '', 'qty' => $of->status == 'DRAFT' ? $form->texte('', 'TAssetOFLine[' . $k . '][qty]', $TAssetOFLine->qty, 5, 5, '', '') . $conditionnement_label_edit : $TAssetOFLine->qty . $conditionnement_label, 'qty_used' => $of->status == 'OPEN' || $of->status == 'CLOSE' ? $form->texte('', 'TAssetOFLine[' . $k . '][qty_used]', $TAssetOFLine->qty_used, 5, 5, '', '') . $conditionnement_label_edit : $TAssetOFLine->qty_used . $conditionnement_label, 'fk_product_fournisseur_price' => $form->combo('', 'TAssetOFLine[' . $k . '][fk_product_fournisseur_price]', $Tab, $TAssetOFLine->fk_product_fournisseur_price != 0 ? $TAssetOFLine->fk_product_fournisseur_price : $selected, 1, '', 'style="max-width:250px;"'), 'delete' => $form->type_aff == 'edit' && $of->status == 'DRAFT' ? '<a href="#null" onclick="deleteLine(' . $TAssetOFLine->getId() . ',\'TO_MAKE\');">' . img_picto('Supprimer', 'delete.png') . '</a>' : '', 'fk_entrepot' => !empty($conf->global->ASSET_MANUAL_WAREHOUSE) && ($of->status == 'DRAFT' || $of->status == 'VALID' || $of->status == 'NEEDOFFER' || $of->status == 'ONORDER' || $of->status == 'OPEN') && $form->type_aff == 'edit' ? $formProduct->selectWarehouses($TAssetOFLine->fk_entrepot, 'TAssetOFLine[' . $k . '][fk_entrepot]', '', 0, 0, $TAssetOFLine->fk_product) : $TAssetOFLine->getLibelleEntrepot($PDOdb));
            $action = $form->type_aff;
            $parameter = array('of' => &$of, 'line' => &$TLine, 'type' => 'TO_MAKE');
            $res = $hookmanager->executeHooks('lineObjectOptions', $parameter, $TAssetOFLine, $action);
            if ($res > 0 && !empty($hookmanager->resArray)) {
                $TLine = $hookmanager->resArray;
            }
            $TRes[] = $TLine;
        }
    }
    return $TRes;
}
 function makeAsset(&$PDOdb, &$AssetOf, $fk_product, $qty_to_make, $idAsset = 0, $lot_number = '')
 {
     global $user, $conf;
     //INFO : si on utilise pas les lots on a pas besoin de créer des équipements => on gère uniquement des mvt de stock
     if (empty($conf->asset->enabled) || empty($conf->global->USE_LOT_IN_OF)) {
         return true;
     }
     if (!dol_include_once('/asset/class/asset.class.php')) {
         return true;
     }
     $assetType = new TAsset_type();
     if ($assetType->load_by_fk_product($PDOdb, $fk_product)) {
         /* On fabrique de la contenance et non pas une quantité de produit au sens strict
          * Si on fabrique un produit au sens strict, le type d'équipement de ce produit aura une contenance max à 1, donc ça marche pareil
          * En revanche, on a un sac de sable à moitier vide, s'il est réutilisable on va le remplir puis en créer si besoin
          *
          * A penser :	   [1] [2] [3]
          * Périsable :		1	0	1
          * Réutilisable :	0	1	1
          *
          * [1] => on crée de nouveaux équipements
          * [2] => on réutilise
          * [3] => si la qté de l'équipement courant = 0 alors on réutilise
          *
          * Dans le process de la validation de la production, les TO_MAKE doivent pré-séléctionner les équipements possibles à la réutilisation
          * Quand on "Termine" l'OF il faudra prendre la liste des équipements liés pour les remplir puis en créer si nécessaire
          */
         $contenance_max = $assetType->contenance_value;
         $nb_asset_to_create = ceil($qty_to_make / $contenance_max);
         //Qté restante a fabriquer
         $qty_to_make_rest = $qty_to_make;
         for ($i = 0; $i < $nb_asset_to_create; $i++) {
             $TAsset = new TAsset();
             $TAsset->fk_soc = $AssetOf->fk_soc;
             $TAsset->fk_societe_localisation = $conf->global->ASSET_DEFAULT_LOCATION;
             $TAsset->fk_product = $fk_product;
             $TAsset->entity = $conf->entity;
             if (!empty($conf->global->ASSET_DEFAULT_DLUO)) {
                 $TAsset->dluo = strtotime(date('Y-m-d') . ' +' . $conf->global->ASSET_DEFAULT_DLUO . ' days');
             } else {
                 $TAsset->dluo = strtotime(date('Y-m-d'));
             }
             //pre($assetType,true);exit;
             $TAsset->fk_asset_type = $assetType->getId();
             $TAsset->load_asset_type($PDOdb);
             if ($qty_to_make_rest > $TAsset->contenance_value) {
                 $qty_to_make_asset = $TAsset->contenance_value;
             } else {
                 $qty_to_make_asset = $qty_to_make_rest;
             }
             $qty_to_make_rest -= $qty_to_make_asset;
             $TAsset->contenancereel_value = $qty_to_make_asset;
             $TAsset->lot_number = $lot_number;
             if (!empty($conf->global->ASSET_USE_DEFAULT_WAREHOUSE)) {
                 $fk_entrepot = $conf->global->ASSET_DEFAULT_WAREHOUSE_ID_TO_MAKE;
             }
             if (!$fk_entrepot) {
                 exit('ASSET_USE_DEFAULT_WAREHOUSE non définis dans la configuration du module');
             }
             $TAsset->fk_entrepot = $fk_entrepot;
             $TAsset->save($PDOdb, '', '', 0, false, 0, true, $fk_entrepot);
             //Save une première fois pour avoir le serial_number + 2ème save pour mvt de stock
             $this->addAssetLink($TAsset);
         }
         return true;
     } else {
         return false;
     }
 }