<?php

/**
 * $Id$
 *
 * @package    Mediboard
 * @subpackage Stock
 * @author     SARL OpenXtrem <*****@*****.**>
 * @license    GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 * @version    $Revision$
 */
CCanDo::checkEdit();
$stock_location_id = CValue::getOrSession('stock_location_id');
$stock_location = new CProductStockLocation();
$stock_location->load($stock_location_id);
$stock_location->loadRefsStocks();
$stock_location->loadTargetObject();
$stock_location->_type = $stock_location->_id ? $stock_location->_ref_object->_guid : null;
$classes = $stock_location->_specs["object_class"]->_locales;
$where = array("group_id" => "='" . CGroups::loadCurrent()->_id . "'");
$types = array();
foreach ($classes as $_class => $_locale) {
    $object = new $_class();
    $types[$_locale] = $object->loadListWithPerms(PERM_READ, $where);
}
// Smarty template
$smarty = new CSmartyDP();
$smarty->assign('stock_location', $stock_location);
$smarty->assign('types', $types);
$smarty->assign("host_group_id", CProductStockGroup::getHostGroup());
$smarty->display('inc_form_stock_location.tpl');
 /**
  * Load locations
  *
  * @return CProductStockLocation[]
  */
 function loadRelatedLocations()
 {
     $where = array("object_class" => "= 'CGroups'", "object_id" => "= '{$this->group_id}'");
     $location = new CProductStockLocation();
     return $this->_ref_related_locations = $location->loadList($where, "name");
 }
 /**
  * @see parent::store()
  */
 function store()
 {
     $this->completeField("location_id");
     if (!$this->location_id) {
         $location = CProductStockLocation::getDefaultLocation($this->loadRefHost(), $this->loadRefProduct());
         $this->location_id = $location->_id;
     }
     return parent::store();
 }
 /**
  * @see parent::store()
  */
 function store()
 {
     $this->completeField("code", 'quantity', 'unit_quantity');
     if (!$this->quantity) {
         $this->quantity = 1;
     }
     if ($this->unit_quantity == 0) {
         $this->unit_quantity = '';
     }
     if ($this->code !== null && (!$this->_id || $this->fieldModified("code"))) {
         $this->code_canonical = preg_replace("/[^0-9a-z]/i", "", $this->code);
     }
     $cc = trim($this->classe_comptable, "0\n\r\t ");
     if (preg_match('/^\\d+$/', $cc)) {
         $this->classe_comptable = str_pad($cc, 9, "0", STR_PAD_RIGHT);
     } else {
         $this->classe_comptable = "";
     }
     if ($this->fieldModified("cancelled", 1)) {
         $references = $this->loadRefsReferences();
         foreach ($references as $_ref) {
             $_ref->cancelled = 1;
             $_ref->store();
         }
     }
     $create_stock_quantity = $this->_create_stock_quantity;
     if ($msg = parent::store()) {
         return $msg;
     }
     if ($create_stock_quantity) {
         $stock = $this->loadRefStock();
         $stock->quantity = $create_stock_quantity;
         $stock->order_threshold_min = $stock->quantity;
         $group = CGroups::loadCurrent();
         $stock->location_id = CProductStockLocation::getDefaultLocation($group, $this)->_id;
         if ($msg = $stock->store()) {
             CAppUI::setMsg($msg, UI_MSG_WARNING);
         }
         $this->_create_stock_quantity = null;
     }
     return null;
 }
 /**
  * @see parent::store()
  */
 function store()
 {
     $this->completeField('delivery_id', 'quantity');
     if (!$this->_id && !$this->preparateur_id) {
         $this->preparateur_id = CMediusers::get()->_id;
     }
     $this->loadRefsFwd();
     $stock = $this->getStock();
     $stock->loadRefsFwd();
     $infinite_group_stock = CAppUI::conf('dPstock CProductStockGroup infinite_quantity') == '1';
     $negative_allowed = CAppUI::conf('dPstock CProductStockGroup negative_allowed') == '1';
     $stock_service = new CProductStockService();
     $stock_service->product_id = $stock->product_id;
     $stock_service->object_id = $this->_ref_delivery->service_id;
     $stock_service->object_class = "CService";
     // XXX
     //$stock_service->location_id = $this->target_location_id;
     $stock_service->loadMatchingObject();
     if ($this->date_delivery && !$negative_allowed && !$infinite_group_stock && ($this->quantity == 0 || $stock->quantity < $this->quantity)) {
         $unit = $stock->_ref_product->_unit_title ? $stock->_ref_product->_unit_title : $stock->_ref_product->_view;
         return "Impossible de délivrer ce nombre de {$unit}";
     }
     // Un-deliver
     if ($this->_undeliver) {
         $this->_undeliver = null;
         // If we can't delete (it has a back ref or something else: interop, etc)
         if ($msg = $this->delete()) {
             $this->quantity = 0;
             return parent::store();
         }
         return null;
     }
     // If we want to deliver, just provide a delivery date
     if ($this->date_delivery && !$infinite_group_stock) {
         $stock->quantity -= $this->quantity;
         if ($msg = $stock->store()) {
             return $msg;
         }
     } else {
         if ($this->_unreceive) {
             if ($stock_service->_id) {
                 $stock_service->quantity -= $this->quantity;
             }
             $this->_unreceive = null;
             $this->date_reception = '';
         }
     }
     // If we want to receive, just provide a reception date
     // if not dispensation nominative
     if ($this->date_reception && !$this->_ref_delivery->patient_id) {
         /*if (!$this->_ref_delivery->patient_id && (
             !$this->_id && $this->date_reception || $this->fieldModified("date_reception")
           )) {*/
         // If a stock already exist, its quantity is updated
         if ($stock_service->_id) {
             $stock_service->quantity += $this->quantity;
         } else {
             $stock_service->order_threshold_min = abs($this->quantity) + 1;
             $stock_service->order_threshold_max = $stock_service->order_threshold_min * 2;
             $stock_service->quantity = $this->quantity;
             $default_location = CProductStockLocation::getDefaultLocation($this->_ref_delivery->loadRefService(), $stock->_ref_product);
             $stock_service->location_id = $default_location->_id;
         }
         if ($stock_service->object_id && $stock_service->object_class) {
             if ($msg = $stock_service->store()) {
                 return $msg;
             }
         }
     }
     // dispensation nominative
     if (!$this->_ref_delivery->patient_id) {
         if (!$stock_service->_id) {
             $stock_service->quantity = $this->quantity;
             $stock_service->order_threshold_min = 0;
         }
         if ($this->_ref_delivery->service_id) {
             if ($msg = $stock_service->store()) {
                 return $msg;
             }
         }
     }
     // Calcul du stock du sejour
     if ($this->_ref_delivery->sejour_id && $this->_code_cis && $this->_code_cip && $this->_datetime_min && CModule::getActive("pharmacie")) {
         $stock_sejour = CStockSejour::getFromCIS($this->_code_cis, $this->_ref_delivery->sejour_id);
         // Mise a jour de la quantité du stock en quantité d'administration
         $ratio = CMedicamentArticle::get($this->_code_cip)->_ratio_cis_cip;
         $quantity = $this->quantity / $ratio;
         // Mise à jour du stock
         if ($stock_sejour->_id) {
             $count_quantity = $stock_sejour->countQuantityForDates($this->_datetime_min);
             $quantite = $stock_sejour->quantite + $quantity - $count_quantity;
         } else {
             $quantite = $quantity;
         }
         $stock_sejour->quantite = round($quantite, 4);
         $stock_sejour->datetime = $this->_datetime_min;
         if ($msg = $stock_sejour->store()) {
             return $msg;
         }
     }
     return parent::store();
 }
<?php

/**
 * $Id$
 *
 * @package    Mediboard
 * @subpackage Stock
 * @author     SARL OpenXtrem <*****@*****.**>
 * @license    GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 * @version    $Revision$
 */
CCanDo::checkEdit();
$stock_location = new CProductStockLocation();
$group_id = CGroups::loadCurrent()->_id;
$classes = array("CGroups" => "Pharmacie", "CService" => CAppUI::tr("CService"), "CBlocOperatoire" => CAppUI::tr("CBlocOperatoire"));
$ds = $stock_location->getDS();
$lists = array();
foreach ($classes as $_class => $_label) {
    $where = array("product_stock_location.object_class" => $ds->prepare("=?", $_class));
    $ljoin = array();
    switch ($_class) {
        case "CGroups":
            $where["product_stock_location.object_id"] = $ds->prepare("=?", $group_id);
            break;
        case "CService":
            $ljoin["service"] = "service.service_id = product_stock_location.object_id";
            $where["service.group_id"] = $ds->prepare("=?", $group_id);
            break;
        case "CBlocOperatoire":
            $ljoin["bloc_operatoire"] = "bloc_operatoire.bloc_operatoire_id = product_stock_location.object_id";
            $where["bloc_operatoire.group_id"] = $ds->prepare("=?", $group_id);
 /**
  * @see parent::store()
  */
 function store()
 {
     $is_new = !$this->_id;
     if ($is_new) {
         if (!$this->datetime_min) {
             $this->datetime_min = $this->date_dispensation;
         }
         if (!$this->datetime_max) {
             $this->datetime_max = $this->date_dispensation;
         }
         if (!$this->preparateur_id) {
             $this->preparateur_id = CMediusers::get()->_id;
         }
     }
     $order_to_0 = $this->fieldModified("order", "0");
     if ($msg = parent::store()) {
         return $msg;
     }
     if ($this->_prises) {
         $prises = json_decode(stripslashes($this->_prises), true);
         foreach ($prises as $_prise) {
             $prise_dispensation = new CPriseDispensation();
             $prise_dispensation->delivery_id = $this->_id;
             $prise_dispensation->datetime = $_prise["datetime"];
             $prise_dispensation->quantite_adm = $_prise["quantite_adm"];
             $prise_dispensation->unite_adm = utf8_decode($_prise["unite_adm"]);
             $prise_dispensation->quantite_disp = $_prise["quantite_disp"];
             $prise_dispensation->object_id = $_prise["object_id"];
             $prise_dispensation->object_class = $_prise["object_class"];
             $prise_dispensation->store();
         }
         $this->_prises = null;
     }
     // Sortie manuelle ou autotrace et passage de "commande" à "pas commande"
     if ($is_new && $this->manual || ($is_new || $order_to_0) && $this->_auto_trace) {
         $delivery_trace = new CProductDeliveryTrace();
         $delivery_trace->delivery_id = $this->_id;
         $delivery_trace->quantity = $this->quantity;
         $delivery_trace->date_delivery = $this->date_delivery ? $this->date_delivery : CMbDT::dateTime();
         $delivery_trace->_code_cis = $this->_code_cis;
         $delivery_trace->_code_cip = $this->_code_cip;
         $delivery_trace->_datetime_min = $this->datetime_min;
         if ($this->manual) {
             $delivery_trace->date_reception = $delivery_trace->date_delivery;
         }
         $product = $this->loadRefStock()->loadRefProduct();
         $location = CProductStockLocation::getDefaultLocation($this->loadRefService(), $product);
         $delivery_trace->target_location_id = $location->_id;
         if ($msg = $delivery_trace->store()) {
             CAppUI::setMsg("La commande a été validée, mais n'a pas pu etre délivrée automatiquement pour la raison suivante: <br />{$msg}", UI_MSG_WARNING);
         } else {
             CAppUI::setMsg("CProductDeliveryTrace-msg-create");
         }
     }
     if (!$is_new) {
         return null;
     }
     $this->loadRefStock()->loadRefsFwd();
     if ($this->_auto_deliver || $this->_ref_stock->_ref_product->auto_dispensed || $this->_auto_trace) {
         $this->date_dispensation = CMbDT::dateTime();
         $this->order = 0;
         return parent::store();
     }
     return null;
 }
 /**
  * Returns the existing location for the product in the host,
  * if it doesn't exist, will return the first location found in the host
  * 
  * @param CGroups|CService|CBlocOperatoire|CMbObject $host    Stock location's host object
  * @param CProduct                                   $product Product
  * 
  * @return CProductStockLocation The location
  */
 static function getDefaultLocation(CMbObject $host, CProduct $product = null)
 {
     $stock_class = self::getStockClass($host->_class);
     /** @var CProductStock $stock */
     $stock = new $stock_class();
     $stock->setHost($host);
     $stock->product_id = $product->_id;
     $stock->loadMatchingObject();
     if (!$stock->_id || !$stock->location_id) {
         $ds = $host->_spec->ds;
         $where = array("object_class" => $ds->prepare("=%", $host->_class), "object_id" => $ds->prepare("=%", $host->_id));
         // pas loadMatchingObject a cause du "position" pré-rempli :(
         $location = new CProductStockLocation();
         if (!$location->loadObject($where, "position")) {
             $location->name = "Lieu par défaut";
             $location->group_id = $host instanceof CGroups ? $host->_id : $host->group_id;
             $location->setObject($host);
             $location->store();
         }
         return $location;
     } else {
         return $stock->loadRefLocation();
     }
 }
<?php

/**
 * $Id$
 *
 * @package    Mediboard
 * @subpackage Stock
 * @author     SARL OpenXtrem <*****@*****.**>
 * @license    GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 * @version    $Revision$
 */
CCanDo::checkRead();
$owner_guid = CValue::get('owner_guid');
$exclude_location_id = CValue::get('exclude_location_id');
$parts = explode("-", $owner_guid);
$where = array("object_class" => " = '{$parts[0]}'", "object_id" => " = '{$parts[1]}'");
if ($exclude_location_id) {
    $where["stock_location_id"] = " != '{$exclude_location_id}'";
}
$location = new CProductStockLocation();
$locations = $location->loadList($where);
// Smarty template
$smarty = new CSmartyDP();
$smarty->assign('locations', $locations);
$smarty->display('inc_autocomplete_related_locations.tpl');