Ejemplo n.º 1
0
function _createOFCommande(&$PDOdb, $TProduct, $TQuantites, $fk_commande, $fk_soc, $oneOF = false)
{
    global $db, $langs;
    if (!empty($TProduct)) {
        if ($oneOF) {
            $assetOf = new TAssetOF();
            $assetOf->fk_commande = $fk_commande;
        }
        foreach ($TProduct as $fk_commandedet => $v) {
            foreach ($v as $fk_product => $dummy) {
                if (!$oneOF) {
                    $assetOf = new TAssetOF();
                    $assetOf->fk_commande = $fk_commande;
                }
                if ($assetOf->fk_commande > 0) {
                    $com = new Commande($db);
                    //TODO on est pas censé toujours être sur la même commande ? AA
                    $com->fetch($assetOf->fk_commande);
                    $assetOf->fk_project = $com->fk_project;
                    if (!empty($com->date_livraison)) {
                        $assetOf->date_besoin = $com->date_livraison;
                    }
                }
                /*				pre($TQuantites,true);
                				pre($TProduct,true);exit;*/
                $qty = $TQuantites[$fk_commandedet];
                //print "$fk_product x $qty<br />";
                $assetOf->fk_soc = $fk_soc;
                $assetOf->addLine($PDOdb, $fk_product, 'TO_MAKE', $qty, 0, '', 0, $fk_commandedet);
                $assetOf->save($PDOdb);
            }
        }
        setEventMessage($langs->trans('OFAsset') . " créé(s) avec succès", 'mesgs');
    }
}
function _addofproduct(&$PDOdb, $id_assetOf, $fk_product, $type, $qty = 1, $lot_number = '')
{
    global $db, $conf;
    if (!$fk_product) {
        return;
    }
    $TassetOF = new TAssetOF();
    $TassetOF->load($PDOdb, $id_assetOf);
    $TassetOF->addLine($PDOdb, $fk_product, $type, $qty, 0, $lot_number, GETPOST('fk_nomenclature', 'int'));
    $TassetOF->save($PDOdb);
}
Ejemplo n.º 3
0
function _action()
{
    global $user, $db, $conf, $langs;
    $PDOdb = new TPDOdb();
    //$PDOdb->debug=true;
    /*******************************************************************
     * ACTIONS
     *
     * Put here all code to do according to value of "action" parameter
     ********************************************************************/
    $action = __get('action', 'view');
    switch ($action) {
        case 'new':
        case 'add':
            $assetOf = new TAssetOF();
            $assetOf->set_values($_REQUEST);
            $fk_product = __get('fk_product', 0, 'int');
            $fk_nomenclature = __get('fk_nomenclature', 0, 'int');
            _fiche($PDOdb, $assetOf, 'edit', $fk_product, $fk_nomenclature);
            break;
        case 'edit':
            $assetOf = new TAssetOF();
            $assetOf->load($PDOdb, $_REQUEST['id']);
            _fiche($PDOdb, $assetOf, 'edit');
            break;
        case 'create':
        case 'save':
            $assetOf = new TAssetOF();
            if (!empty($_REQUEST['id'])) {
                $assetOf->load($PDOdb, $_REQUEST['id'], false);
                $mode = 'view';
            } else {
                $mode = $action == 'create' ? 'view' : 'edit';
            }
            $assetOf->set_values($_REQUEST);
            $fk_product = __get('fk_product_to_add', 0);
            $quantity_to_create = __get('quantity_to_create', 1);
            $fk_nomenclature = __get('fk_nomenclature', 0);
            if ($fk_product > 0) {
                $assetOf->addLine($PDOdb, $fk_product, 'TO_MAKE', $quantity_to_create, 0, '', $fk_nomenclature);
            }
            if (!empty($_REQUEST['TAssetOFLine'])) {
                foreach ($_REQUEST['TAssetOFLine'] as $k => $row) {
                    if (!isset($assetOf->TAssetOFLine[$k])) {
                        $assetOf->TAssetOFLine[$k] = new TAssetOFLine();
                    }
                    if (!empty($conf->global->ASSET_DEFINED_WORKSTATION_BY_NEEDED)) {
                        $assetOf->TAssetOFLine[$k]->set_workstations($PDOdb, $row['fk_workstation']);
                        unset($row['fk_workstation']);
                    }
                    $assetOf->TAssetOFLine[$k]->set_values($row);
                }
                foreach ($assetOf->TAssetOFLine as &$line) {
                    $line->TAssetOFLine = array();
                }
            }
            if (!empty($_REQUEST['TAssetWorkstationOF'])) {
                foreach ($_REQUEST['TAssetWorkstationOF'] as $k => $row) {
                    //Association des utilisateurs à un poste de travail
                    if (!empty($conf->global->ASSET_DEFINED_USER_BY_WORKSTATION)) {
                        $assetOf->TAssetWorkstationOF[$k]->set_users($PDOdb, $row['fk_user']);
                        unset($row['fk_user']);
                    }
                    //Association des opérations à une poste de travail (mode opératoire)
                    if (!empty($conf->global->ASSET_DEFINED_OPERATION_BY_WORKSTATION)) {
                        $assetOf->TAssetWorkstationOF[$k]->set_tasks($PDOdb, $row['fk_task']);
                        unset($row['fk_task']);
                    }
                    $assetOf->TAssetWorkstationOF[$k]->set_values($row);
                }
            }
            $assetOf->entity = $conf->entity;
            //Permet de mettre à jour le lot de l'OF parent
            if (!empty($assetOf->fk_assetOf_parent)) {
                $assetOf->update_parent = true;
            }
            $assetOf->save($PDOdb);
            _fiche($PDOdb, $assetOf, $mode);
            break;
        case 'valider':
            $error = 0;
            $assetOf = new TAssetOF();
            $id = GETPOST('id');
            if (empty($id)) {
                exit('Where is Waldo ?');
            }
            $assetOf->load($PDOdb, $id);
            //Si use_lot alors check de la saisie du lot pour chaque ligne avant validation
            if (!empty($conf->global->USE_LOT_IN_OF) && !empty($conf->global->OF_LOT_MANDATORY)) {
                if (!$assetOf->checkLotIsFill()) {
                    _fiche($PDOdb, $assetOf, 'view');
                    break;
                }
            }
            $res = $assetOf->validate($PDOdb);
            if ($res > 0) {
                //Relaod de l'objet OF parce que createOfAndCommandesFourn() fait tellement de truc que c'est le bordel
                $assetOf = new TAssetOF();
                if (!empty($_REQUEST['id'])) {
                    $assetOf->load($PDOdb, $_REQUEST['id'], false);
                }
            }
            _fiche($PDOdb, $assetOf, 'view');
            break;
        case 'lancer':
            $assetOf = new TAssetOF();
            $id = GETPOST('id');
            if (empty($id)) {
                exit('Where is Waldo ?');
            }
            $assetOf->load($PDOdb, $id);
            $assetOf->openOF($PDOdb);
            $assetOf->load($PDOdb, $id);
            _fiche($PDOdb, $assetOf, 'view');
            break;
        case 'terminer':
            $assetOf = new TAssetOF();
            $assetOf->load($PDOdb, $_REQUEST['id']);
            $assetOf->closeOF($PDOdb);
            $assetOf->load($PDOdb, $_REQUEST['id']);
            _fiche($PDOdb, $assetOf, 'view');
            break;
        case 'delete':
            $assetOf = new TAssetOF();
            $assetOf->load($PDOdb, $_REQUEST['id']);
            //$PDOdb->db->debug=true;
            $assetOf->delete($PDOdb);
            header('Location: ' . dol_buildpath('/of/liste_of.php?delete_ok=1', 1));
            exit;
            break;
        case 'view':
            $assetOf = new TAssetOF();
            $assetOf->load($PDOdb, $_REQUEST['id']);
            _fiche($PDOdb, $assetOf, 'view');
            break;
        case 'createDocOF':
            $id_of = $_REQUEST['id'];
            $assetOf = new TAssetOF();
            $assetOf->load($PDOdb, $id_of, false);
            $TOFToGenerate = array($assetOf->rowid);
            if ($conf->global->ASSET_CONCAT_PDF) {
                $assetOf->getListeOFEnfants($PDOdb, $TOFToGenerate, $assetOf->rowid);
            }
            //			var_dump($TOFToGenerate);exit;
            foreach ($TOFToGenerate as $id_of) {
                $assetOf = new TAssetOF();
                $assetOf->load($PDOdb, $id_of, false);
                //echo $id_of;
                $TRes[] = generateODTOF($PDOdb, $assetOf);
                //echo '...ok<br />';
            }
            $TFilePath = get_tab_file_path($TRes);
            //	var_dump($TFilePath);exit;
            if ($conf->global->ASSET_CONCAT_PDF) {
                ob_start();
                $pdf = pdf_getInstance();
                if (class_exists('TCPDF')) {
                    $pdf->setPrintHeader(false);
                    $pdf->setPrintFooter(false);
                }
                $pdf->SetFont(pdf_getPDFFont($langs));
                if ($conf->global->MAIN_DISABLE_PDF_COMPRESSION) {
                    $pdf->SetCompression(false);
                }
                //$pdf->SetCompression(false);
                $pagecount = concatPDFOF($pdf, $TFilePath);
                if ($pagecount) {
                    $pdf->Output($TFilePath[0], 'F');
                    if (!empty($conf->global->MAIN_UMASK)) {
                        @chmod($file, octdec($conf->global->MAIN_UMASK));
                    }
                }
                ob_clean();
            }
            header("Location: " . DOL_URL_ROOT . "/document.php?modulepart=of&entity=1&file=" . $TRes[0]['dir_name'] . "/" . $TRes[0]['num_of'] . ".pdf");
            break;
        case 'control':
            $assetOf = new TAssetOF();
            $assetOf->load($PDOdb, $_REQUEST['id']);
            $subAction = __get('subAction', false);
            if ($subAction) {
                $assetOf->updateControl($PDOdb, $subAction);
            }
            _fiche_control($PDOdb, $assetOf);
            break;
        case 'addAssetLink':
            $assetOf = new TAssetOF();
            $assetOf->load($PDOdb, __get('id', 0, 'int'));
            $idLine = __get('idLine', 0, 'int');
            $idAsset = __get('idAsset', 0, 'int');
            if ($idLine && $idAsset) {
                $find = false;
                foreach ($assetOf->TAssetOFLine as $TAssetOFLine) {
                    if ($TAssetOFLine->getId() == $idLine) {
                        $find = true;
                        $asset = new TAsset();
                        $asset->load($PDOdb, $idAsset);
                        $TAssetOFLine->addAssetLink($asset);
                        break;
                    }
                }
                if (!$find) {
                    setEventMessage('Erreur sur l\'identifiant de l\'équipement', 'errors');
                }
            } else {
                setEventMessage('Erreur sur la saisie de l\'équipement.', 'errors');
            }
            _fiche($PDOdb, $assetOf, 'edit');
            break;
        case 'deleteAssetLink':
            $assetOf = new TAssetOF();
            $assetOf->load($PDOdb, __get('id', 0, 'int'));
            $idLine = __get('idLine', 0, 'int');
            $idAsset = __get('idAsset', 0, 'int');
            if ($idLine && $idAsset) {
                TAsset::del_element_element($PDOdb, $idLine, $idAsset, 'TAsset');
            } else {
                setEventMessage('Erreur sur un des identifiants', 'errors');
            }
            _fiche($PDOdb, $assetOf, 'edit');
            break;
        default:
            $assetOf = new TAssetOF();
            $assetOf->load($PDOdb, $_REQUEST['id'], false);
            _fiche($PDOdb, $assetOf, 'view');
            break;
    }
}
 /**
  * Overloading the doActions function : replacing the parent's function with the one below
  *
  * @param   array()         $parameters     Hook metadatas (context, etc...)
  * @param   CommonObject    &$object        The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  * @param   string          &$action        Current action (if set). Generally create or edit or null
  * @param   HookManager     $hookmanager    Hook manager propagated to allow calling another hook
  * @return  int                             < 0 on error, 0 on success, 1 to replace standard code
  */
 function doActions($parameters, &$object, &$action, $hookmanager)
 {
     global $langs, $db, $conf, $user;
     // Constante PRODUIT_SOUSPRODUITS passée à 0 pour ne pas déstocker les sous produits lors de la validation de l'expédition
     /*if(in_array('expeditioncard',explode(':',$parameters['context'])) && $action === "confirm_valid") {
     			
     			$conf->global->PRODUIT_SOUSPRODUITS = 0;
     			
     		}*/
     // --> Maintenant Géré grâce à la constante INDEPENDANT_SUBPRODUCT_STOCK que j'ai rajoutée sur notre Dolibarr
     if ($parameters['currentcontext'] === 'ordersuppliercard') {
         if (GETPOST('action') === 'confirm_commande' && GETPOST('confirm') === 'yes') {
             $time_livraison = $object->date_livraison;
             $res = $db->query("SELECT fk_source as 'fk_of' \n                            FROM " . MAIN_DB_PREFIX . "element_element \n                            WHERE sourcetype='ordre_fabrication' AND fk_target=" . $object->id . " AND targettype='order_supplier' ");
             define('INC_FROM_DOLIBARR', true);
             dol_include_once("/of/config.php");
             dol_include_once("/of/class/ordre_fabrication_asset.class.php");
             if ($obj = $db->fetch_object($res)) {
                 // of lié à la commande
                 $PDOdb = new TPDOdb();
                 $OF = new TAssetOF();
                 $OF->load($PDOdb, $obj->fk_of);
                 $OF->date_lancement = $time_livraison;
                 $OF->save($PDOdb);
             } else {
                 // pas d'of liés directement
                 $TProduct = $TProd = array();
                 foreach ($object->lines as &$l) {
                     if ($l->product_type == 0) {
                         if (empty($l->fk_product)) {
                             continue;
                         }
                         $TProduct[] = $l->fk_product;
                         if (!isset($TProd[$l->fk_product])) {
                             $TProd[$l->fk_product] = 0;
                         }
                         $TProd[$l->fk_product] += $l->qty;
                     }
                 }
                 $res = $db->query("SELECT DISTINCT of.rowid as 'fk_of' \n                            FROM " . MAIN_DB_PREFIX . "assetOf_line ofl\n                            LEFT JOIN " . MAIN_DB_PREFIX . "assetOf of ON (of.rowid = ofl.fk_assetOf)\n                            WHERE ofl.fk_product IN (" . implode(',', $TProduct) . ")\n                            AND of.status='ONORDER'\n                            ORDER BY of.date_besoin ASC");
                 $PDOdb = new TPDOdb();
                 while ($obj = $db->fetch_object($res)) {
                     $OF = new TAssetOF();
                     $OF->load($PDOdb, $obj->fk_of);
                     $to_save = false;
                     foreach ($OF->TAssetOFLine as &$line) {
                         if (isset($TProd[$line->fk_product]) && $TProd[$line->fk_product] > 0) {
                             $TProd[$line->fk_product] -= $line->qty_needed > 0 ? $line->qty_needed : $line->qty;
                             if ($OF->date_lancement < $time_livraison) {
                                 $OF->date_lancement = $time_livraison;
                                 $to_save = true;
                             }
                         }
                     }
                     if ($to_save) {
                         // print 'OF '.$OF->getId().'$time_livraison'.$time_livraison;
                         $OF->save($PDOdb);
                     }
                 }
                 //exit;
             }
         }
     }
     return 0;
 }
 /**
  * Function called when a Dolibarrr business event is done.
  * All functions "run_trigger" are triggered if file
  * is inside directory core/triggers
  *
  * 	@param		string		$action		Event action code
  * 	@param		Object		$object		Object
  * 	@param		User		$user		Object user
  * 	@param		Translate	$langs		Object langs
  * 	@param		conf		$conf		Object conf
  * 	@return		int						<0 if KO, 0 if no triggered ran, >0 if OK
  */
 public function run_trigger($action, $object, $user, $langs, $conf)
 {
     if ($action === 'ORDER_VALIDATE') {
         global $conf, $db;
         if ($conf->global->CREATE_OF_ON_ORDER_VALIDATE) {
             define('INC_FROM_DOLIBARR', true);
             dol_include_once('/product/class/product.class.php');
             dol_include_once('/of/config.php');
             dol_include_once('/of/class/ordre_fabrication_asset.class.php');
             $PDOdb = new TPDOdb();
             foreach ($object->lines as $line) {
                 // Uniquement si c'est un produit
                 if (!empty($line->fk_product) && $line->fk_product_type == 0) {
                     // On charge le produit pour vérifier son stock
                     $prod = new Product($db);
                     $prod->fetch($line->fk_product);
                     $prod->load_stock();
                     if ($prod->stock_reel < $line->qty) {
                         $assetOF = new TAssetOF();
                         $assetOF->fk_commande = $_REQUEST['id'];
                         $assetOF->fk_soc = $object->socid;
                         $assetOF->addLine($PDOdb, $line->fk_product, 'TO_MAKE', $line->qty);
                         $assetOF->save($PDOdb);
                     }
                 }
             }
         }
     } elseif ($action === 'ORDER_CANCEL') {
         if ($conf->global->DELETE_OF_ON_ORDER_CANCEL) {
             define('INC_FROM_DOLIBARR', true);
             dol_include_once('/of/config.php');
             dol_include_once('/of/class/ordre_fabrication_asset.class.php');
             $PDOdb = new TPDOdb();
             // On récupère les identifiants des of créés à partir de cette commande
             $TID_OF_command = TAssetOF::getTID_OF_command($_REQUEST['id']);
             foreach ($TID_OF_command as $id_of) {
                 $asset = new TAssetOF();
                 $asset->load($PDOdb, $id_of);
                 if ($asset->status == "DRAFT" || $asset->status == "VALID") {
                     $asset->delete($PDOdb);
                 }
             }
         }
     } elseif ($action === 'TASK_TIMESPENT_CREATE') {
         if ($conf->workstation->enabled) {
             define('INC_FROM_DOLIBARR', true);
             dol_include_once('/of/config.php');
             dol_include_once('/of/class/ordre_fabrication_asset.class.php');
             $PDOdb = new TPDOdb();
             $PDOdb->Execute("SELECT rowid \n\t\t\t\t\t\tFROM " . MAIN_DB_PREFIX . "asset_workstation_of \n\t\t\t\t\t\tWHERE fk_project_task=" . $object->id);
             if ($obj = $PDOdb->Get_line()) {
                 $wsof = new TAssetWorkstationOF();
                 $wsof->load($PDOdb, $obj->rowid);
                 $wsof->nb_hour_real = ($object->duration_effective + $object->timespent_duration) / 3600;
                 // Parce que Dolibarr mets le THM à jour après la création de la tâche :/
                 $sql = "UPDATE " . MAIN_DB_PREFIX . "projet_task_time";
                 $sql .= " SET thm = (SELECT thm FROM " . MAIN_DB_PREFIX . "user WHERE rowid = " . $object->timespent_fk_user . ")";
                 // set average hour rate of user
                 $sql .= " WHERE rowid = " . $object->timespent_id;
                 $object->db->query($sql);
                 $wsof->db =& $object->db;
                 $wsof->save($PDOdb);
             }
         }
     } elseif ($action === 'ORDERSUPPLIER_ADD_LIVRAISON') {
         global $db;
         if ($conf->of->enabled) {
             define('INC_FROM_DOLIBARR', true);
             dol_include_once('/of/config.php');
             dol_include_once('/of/class/ordre_fabrication_asset.class.php');
             $resql = $db->query('SELECT fk_statut FROM llx_commande_fournisseur WHERE rowid = ' . $_REQUEST['id']);
             $res = $db->fetch_object($resql);
             if ($res->fk_statut == 5) {
                 // La livraison est totale
                 //On cherche l'OF lié
                 $resql = $db->query("SELECT fk_source \n\t\t\t\t\t\t\t\t\t\t\tFROM " . MAIN_DB_PREFIX . "element_element \n\t\t\t\t\t\t\t\t\t\t\tWHERE fk_target = " . $_REQUEST['id'] . " \n\t\t\t\t\t\t\t\t\t\t\t\tAND sourcetype = 'ordre_fabrication' \n\t\t\t\t\t\t\t\t\t\t\t\tAND targettype = 'order_supplier'");
                 $res = $db->fetch_object($resql);
                 $id_of = $res->fk_source;
                 if ($id_of > 0) {
                     $of = new TAssetOF();
                     $of->load($PDOdb, $id_of);
                     if ($of->status != 'CLOSE') {
                         $of->closeOF($PDOdb);
                         setEventMessage($langs->trans('OFAttachedClosedAutomatically', '<a href="' . dol_buildpath('/of/fiche_of.php?id=' . $id_of, 2) . '">' . $of->numero . '</a>'));
                     }
                 }
             }
         }
     }
     return 0;
 }
 function createOfAndCommandesFourn(&$PDOdb)
 {
     global $db, $user;
     dol_include_once("fourn/class/fournisseur.commande.class.php");
     $TabOF = array();
     $TabOF[] = $this->rowid;
     $this->getListeOFEnfants($PDOdb, $TabOF);
     // Boucle pour chaque OF de l'arbre
     foreach ($TabOF as $idOf) {
         // On charge l'OF
         $assetOF = new TAssetOF();
         $assetOF->load($PDOdb, $idOf);
         // Boucle pour chaque produit de l'OF
         foreach ($assetOF->TAssetOFLine as $ofLigne) {
             //pre($ofLigne,true);
             // On cherche le produit "TO_MAKE"
             if ($ofLigne->type == "TO_MAKE") {
                 //pre($ofLigne,true); exit;
                 if ($ofLigne->fk_product_fournisseur_price > 0) {
                     // Fournisseur externe
                     // On récupère la ligne prix fournisseur correspondante
                     $sql = "SELECT rowid, fk_soc, fk_product, price, compose_fourni, quantity, ref_fourn";
                     $sql .= " FROM " . MAIN_DB_PREFIX . "product_fournisseur_price";
                     $sql .= " WHERE rowid = " . $ofLigne->fk_product_fournisseur_price;
                     $resql = $db->query($sql);
                     $res = $db->fetch_object($resql);
                     // Si fabrication interne
                     if ($res->compose_fourni) {
                         // On charge le produit "TO_MAKE"
                         $prod = new Product($db);
                         $prod->fetch($ofLigne->fk_product);
                         $prod->load_stock();
                         $stockProd = 0;
                         // On récupère son stock
                         foreach ($prod->stock_warehouse as $stock) {
                             $stockProd += $stock->real;
                         }
                         // S'il y a suffisemment de stock, on destocke
                         // Sinon, commande fournisseur :
                         if ($stockProd < $ofLigne->qty_needed) {
                             $this->addCommandeFourn($PDOdb, $ofLigne, $res);
                         } else {
                             // Suffisemment de stock, donc destockage :
                             $assetOF->openOF($PDOdb);
                         }
                     } elseif (!$res->compose_fourni) {
                         //Commande Fournisseur
                         $this->addCommandeFourn($PDOdb, $ofLigne, $res);
                         // On récupère les OF enfants pour les supprimer
                         $TabIdEnfantsDirects = $assetOF->getEnfantsDirects();
                         foreach ($TabIdEnfantsDirects as $idOF) {
                             $assetOF->removeChild("TAssetOF", $idOF);
                         }
                         //Suppression des lignes NEEDED puisque inutiles
                         $assetOF->delLineNeeded($PDOdb);
                         $assetOF->unsetChildDeleted = true;
                         $assetOF->save($PDOdb);
                         // On casse la boucle
                         break;
                     }
                 } else {
                     // Fournisseur interne (Bourguignon) [PH - 14/04/15 - FIXME c'est pas que pour bourguignon ?]
                     if ($ofLigne->fk_product_fournisseur_price == -1) {
                         // Sortie de stock, kill OF enfants
                         $TabIdEnfantsDirects = $assetOF->getEnfantsDirects();
                         foreach ($TabIdEnfantsDirects as $idOF) {
                             $assetOF->removeChild("TAssetOF", $idOF);
                         }
                         $assetOF->save($PDOdb);
                         // On casse la boucle
                         break;
                     } elseif ($ofLigne->fk_product_fournisseur_price == -2) {
                         // Fabrication interne
                         //[PH] FIXME - pourquoi on destock maintenant ? On valide tt juste l'OF
                         $prod = new Product($db);
                         $prod->fetch($ofLigne->fk_product);
                         $prod->load_stock();
                         $stockProd = 0;
                         // On récupère son stock
                         foreach ($prod->stock_warehouse as $stock) {
                             $stockProd += $stock->real;
                         }
                         // S'il y a sufisemment de stock, on destocke
                         if ($stockProd >= $ofLigne->qty_needed) {
                             //$assetOF->openOF($PDOdb);
                             $assetOF->status = 'VALID';
                         }
                     }
                 }
             }
         }
     }
 }