diff --git a/htdocs/comm/fiche.php b/htdocs/comm/fiche.php index 2abee28066e..bd3b96977c1 100644 --- a/htdocs/comm/fiche.php +++ b/htdocs/comm/fiche.php @@ -493,14 +493,29 @@ if ($id > 0) if ($num > 0) { - print ''; + // Check if there are orders billable + $sql2 = 'SELECT s.nom, s.rowid as socid, s.client, c.rowid, c.ref, c.total_ht, c.ref_client,'; + $sql2.= ' c.date_valid, c.date_commande, c.date_livraison, c.fk_statut, c.facture as facturee'; + $sql2.= ' FROM '.MAIN_DB_PREFIX.'societe as s'; + $sql2.= ', '.MAIN_DB_PREFIX.'commande as c'; + $sql2.= ' WHERE c.fk_soc = s.rowid'; + $sql2.= ' AND s.rowid = '.$object->id; + // Show orders with status validated, shipping started and delivered (well any order we can bill) + $sql2.= " AND ((c.fk_statut IN (1,2)) OR (c.fk_statut = 3 AND c.facture = 0))"; + + $resql2=$db->query($sql2); + $num2 = $db->num_rows($resql2); + $db->free($resql2); + + print '
'; - print ''; - print ''; - print ''; + print ''; + print ''; + print ''; } $i = 0; diff --git a/htdocs/comm/propal/propalstoorder.php b/htdocs/comm/propal/propalstoorder.php new file mode 100755 index 00000000000..77905ed3170 --- /dev/null +++ b/htdocs/comm/propal/propalstoorder.php @@ -0,0 +1,688 @@ + + * Copyright (C) 2004-2011 Laurent Destailleur + * Copyright (C) 2005 Marc Barilley / Ocebo + * Copyright (C) 2005-2012 Regis Houssin + * Copyright (C) 2012 Andreu Bisquerra Gaya + * Copyright (C) 2012 David Rodriguez Martinez + * Copyright (C) 2012 Juanjo Menent + * Copyright (C) 2012 Maxime Kohlhaas + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file htdocs/comm/propal/propalstoorder.php + * \ingroup propal + * \brief Page to order multiple propals + */ + +require '../../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'; +require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; +require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php'; +if (! empty($conf->projet->enabled)) require_once DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php'; + +$langs->load('orders'); +$langs->load('deliveries'); +$langs->load('companies'); + +if (! $user->rights->commande->creer) + accessforbidden(); + +$id = (GETPOST('id')?GETPOST("id"):GETPOST("facid")); // For backward compatibility +$ref = GETPOST('ref','alpha'); +$action = GETPOST('action','alpha'); +$confirm = GETPOST('confirm','alpha'); +$sref = GETPOST('sref'); +$sref_client = GETPOST('sref_client'); +$sall = GETPOST('sall'); +$socid = GETPOST('socid','int'); + + +$sortfield = GETPOST("sortfield",'alpha'); +$sortorder = GETPOST("sortorder",'alpha'); +if (! $sortfield) $sortfield='p.rowid'; +if (! $sortorder) $sortorder='DESC'; + +$date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]); // Date for local PHP server +$date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]); +$date_starty=dol_mktime(0,0,0,$_REQUEST["date_start_delymonth"],$_REQUEST["date_start_delyday"],$_REQUEST["date_start_delyyear"]); // Date for local PHP server +$date_endy=dol_mktime(23,59,59,$_REQUEST["date_end_delymonth"],$_REQUEST["date_end_delyday"],$_REQUEST["date_end_delyyear"]); +$selected=GETPOST('propals_to_order'); +$action=GETPOST('action','alpha'); +if ($action == 'create') +{ + if (! is_array($selected)) + { + $mesgs[]='
'.$langs->trans('Error_OrderNotChecked').'
'; + } + else + { + $socid=GETPOST('socid'); + $action=GETPOST('action'); + $origin=GETPOST('origin'); + $originid=GETPOST('originid'); + } +} +$now=dol_now(); + +$viewstatut=GETPOST('viewstatut'); + + +/* + * Actions + */ + +if (($action == 'create' || $action == 'add') && empty($mesgs)) +{ + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php'; + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; + require_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php'; + require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; + require_once DOL_DOCUMENT_ROOT.'/core/class/discount.class.php'; + require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; + require_once DOL_DOCUMENT_ROOT.'/core/lib/invoice.lib.php'; + require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; + if (! empty($conf->projet->enabled)) { + require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; + require_once DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php'; + } + $langs->load('bills'); + $langs->load('products'); + $langs->load('main'); + if (isset($_GET['propals_to_order'])) + { + $orders_id = $_GET['propals_to_order']; + $n = count($orders_id); + $i = 0; + + $originid=$orders_id[0]; + $_GET['originid']=$orders_id[0]; + + } + if (isset($_POST['propals_to_order'])) + { + $orders_id = $_POST['propals_to_order']; + $nn = count($orders_id); + $ii = 0; + + $originid=$orders_id[0]; + $_POST['originid']=$orders_id[0]; + + } + + $projectid = GETPOST('projectid','int')?GETPOST('projectid','int'):0; + $lineid = GETPOST('lineid','int'); + $userid = GETPOST('userid','int'); + $search_ref = GETPOST('sf_ref')?GETPOST('sf_ref'):GETPOST('search_ref'); + $closeOrders = GETPOST('autocloseorders') != '' ? true : false; + + // Security check + $fieldid = GETPOST('ref','alpha')?'facnumber':'rowid'; + if ($user->societe_id) $socid=$user->societe_id; + $result = restrictedArea($user, 'facture', $id,'','','fk_soc',$fieldid); + + $usehm=$conf->global->MAIN_USE_HOURMIN_IN_DATE_RANGE; + $object=new Facture($db); + + // Insert new invoice in database + if ($action == 'add' && $user->rights->facture->creer) + { + $object->socid=GETPOST('socid'); + $db->begin(); + $error=0; + + // Standard or deposit or proforma invoice + if ($_POST['type'] == 0 ) + { + $datefacture = dol_mktime(12, 0, 0, $_POST['remonth'], $_POST['reday'], $_POST['reyear']); + if (empty($datefacture)) + { + $datefacture = dol_mktime(12, 0, 0, date("m"), date("d"), date("Y")); + } + if (! $error) + { + // Si facture standard + $object->socid = $_POST['socid']; + $object->type = $_POST['type']; + $object->number = $_POST['facnumber']; + $object->date = $datefacture; + $object->note_public = trim($_POST['note_public']); + $object->note = trim($_POST['note']); + $object->ref_client = $_POST['ref_client']; + $object->ref_int = $_POST['ref_int']; + $object->modelpdf = $_POST['model']; + $object->fk_project = $_POST['projectid']; + $object->cond_reglement_id = ($_POST['type'] == 3?1:$_POST['cond_reglement_id']); + $object->mode_reglement_id = $_POST['mode_reglement_id']; + $object->amount = $_POST['amount']; + $object->remise_absolue = $_POST['remise_absolue']; + $object->remise_percent = $_POST['remise_percent']; + + if ($_POST['origin'] && $_POST['originid']) + { + $object->origin = $_POST['origin']; + $object->origin_id = $orders_id[$ii]; + $object->linked_objects = $orders_id; + $id = $object->create($user); + + if ($id>0) + { + foreach($orders_id as $origin => $origin_id) + { + $origin_id = (! empty($origin_id) ? $origin_id : $object->origin_id); + $db->begin(); + $sql = "INSERT INTO ".MAIN_DB_PREFIX."element_element ("; + $sql.= "fk_source"; + $sql.= ", sourcetype"; + $sql.= ", fk_target"; + $sql.= ", targettype"; + $sql.= ") VALUES ("; + $sql.= $origin_id; + $sql.= ", '".$object->origin."'"; + $sql.= ", ".$id; + $sql.= ", '".$object->element."'"; + $sql.= ")"; + + if ($db->query($sql)) + { + $db->commit(); + } + else + { + $db->rollback(); + } + } + + while ($ii < $nn) + { + dol_include_once('/commande/class/commande.class.php'); + $srcobject = new Commande($db); + dol_syslog("Try to find source object origin=".$object->origin." originid=".$object->origin_id." to add lines"); + $result=$srcobject->fetch($orders_id[$ii]); + if ($result > 0) + { + if($closeOrders) { + $srcobject->classer_facturee(); + $srcobject->setStatut(3); + } + $lines = $srcobject->lines; + if (empty($lines) && method_exists($srcobject,'fetch_lines')) $lines = $srcobject->fetch_lines(); + $fk_parent_line=0; + $num=count($lines); + for ($i=0;$i<$num;$i++) + { + $desc=($lines[$i]->desc?$lines[$i]->desc:$lines[$i]->libelle); + if ($lines[$i]->subprice < 0) + { + // Negative line, we create a discount line + $discount = new DiscountAbsolute($db); + $discount->fk_soc=$object->socid; + $discount->amount_ht=abs($lines[$i]->total_ht); + $discount->amount_tva=abs($lines[$i]->total_tva); + $discount->amount_ttc=abs($lines[$i]->total_ttc); + $discount->tva_tx=$lines[$i]->tva_tx; + $discount->fk_user=$user->id; + $discount->description=$desc; + $discountid=$discount->create($user); + if ($discountid > 0) + { + $result=$object->insert_discount($discountid); + //$result=$discount->link_to_invoice($lineid,$id); + } + else + { + $mesgs[]=$discount->error; + $error++; + break; + } + } + else + { + // Positive line + $product_type=($lines[$i]->product_type?$lines[$i]->product_type:0); + // Date start + $date_start=false; + if ($lines[$i]->date_debut_prevue) $date_start=$lines[$i]->date_debut_prevue; + if ($lines[$i]->date_debut_reel) $date_start=$lines[$i]->date_debut_reel; + if ($lines[$i]->date_start) $date_start=$lines[$i]->date_start; + //Date end + $date_end=false; + if ($lines[$i]->date_fin_prevue) $date_end=$lines[$i]->date_fin_prevue; + if ($lines[$i]->date_fin_reel) $date_end=$lines[$i]->date_fin_reel; + if ($lines[$i]->date_end) $date_end=$lines[$i]->date_end; + // Reset fk_parent_line for no child products and special product + if (($lines[$i]->product_type != 9 && empty($lines[$i]->fk_parent_line)) || $lines[$i]->product_type == 9) + { + $fk_parent_line = 0; + } + $result = $object->addline( + $id, + $desc, + $lines[$i]->subprice, + $lines[$i]->qty, + $lines[$i]->tva_tx, + $lines[$i]->localtax1_tx, + $lines[$i]->localtax2_tx, + $lines[$i]->fk_product, + $lines[$i]->remise_percent, + $date_start, + $date_end, + 0, + $lines[$i]->info_bits, + $lines[$i]->fk_remise_except, + 'HT', + 0, + $product_type, + $lines[$i]->rang, + $lines[$i]->special_code, + $object->origin, + $lines[$i]->rowid, + $fk_parent_line + ); + if ($result > 0) + { + $lineid=$result; + } + else + { + $lineid=0; + $error++; + break; + } + // Defined the new fk_parent_line + if ($result > 0 && $lines[$i]->product_type == 9) + { + $fk_parent_line = $result; + } + } + } + } + else + { + $mesgs[]=$srcobject->error; + $error++; + } + $ii++; + } + } + else + { + $mesgs[]=$object->error; + $error++; + } + } + } + } + + // End of object creation, we show it + if ($id > 0 && ! $error) + { + $db->commit(); + header('Location: '.DOL_URL_ROOT.'/compta/facture.php?facid='.$id); + exit; + } + else + { + $db->rollback(); + $action='create'; + $_GET["origin"]=$_POST["origin"]; + $_GET["originid"]=$_POST["originid"]; + $mesgs[]='
'.$object->error.'
'; + } + } +} + + +/* + * View + */ + +$html = new Form($db); +$htmlother = new FormOther($db); +$formfile = new FormFile($db); +$companystatic = new Societe($db); + +// Mode creation +if ($action == 'create') +{ + $facturestatic=new Facture($db); + + llxHeader(); + print_fiche_titre($langs->trans('NewBill')); + + $soc = new Societe($db); + if ($socid) $res=$soc->fetch($socid); + if ($res) + { + $cond_reglement_id = $soc->cond_reglement_id; + $mode_reglement_id = $soc->mode_reglement_id; + $remise_percent = $soc->remise_percent; + } + $remise_absolue = 0; + $dateinvoice = empty($conf->global->MAIN_AUTOFILL_DATE)?-1:0; + + $absolute_discount=$soc->getAvailableDiscounts(); + print '
'; + print ''; + print ''; + print '' ."\n"; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print '
'; - print ''; - print ''; - print '
'.$langs->trans("LastOrders",($num<=$MAXLIST?"":$MAXLIST)).''.$langs->trans("AllOrders").' ('.$num.')'.img_picto($langs->trans("Statistics"),'stats').''.img_picto($langs->trans("CreateInvoiceForThisCustomer"),'object_bill').'
'; + print ''; + if($num2 > 0) print ''; + else print ''; + print '
'.$langs->trans("LastOrders",($num<=$MAXLIST?"":$MAXLIST)).''.$langs->trans("AllOrders").' ('.$num.')'.img_picto($langs->trans("Statistics"),'stats').''.img_picto($langs->trans("CreateInvoiceForThisCustomer"),'object_bill').''.img_picto($langs->trans("NoOrdersToInvoice"),'object_bill').'
'; + // Ref + print ''; + // Tiers + print ''; + print ''."\n"; + print ''; + // Payment term + print ''; + // Payment mode + print ''; + // Project + if (! empty($conf->projet->enabled)) + { + $langs->load('projects'); + print ''; + } + // Modele PDF + print ''; + print '"; + + // Public note + print ''; + print ''; + print ''; + // Private note + if (! $user->societe_id) + { + print ''; + print ''; + print ''; + } + + print '
'.$langs->trans('Ref').''.$langs->trans('Draft').'
'.$langs->trans('Customer').''; + print $soc->getNomUrl(1); + print ''; + print '
'.$langs->trans('Type').''; + print ''."\n"; + // Standard invoice + print ''."\n"; + print '
'; + print ''; + print ''; + $desc=$html->textwithpicto($langs->trans("InvoiceStandardAsk"),$langs->transnoentities("InvoiceStandardDesc"),1); + print $desc; + print '
'; + + // Date invoice + print '
'.$langs->trans('Date').''; + $html->select_date(0,'','','','',"add",1,1); + print '
'.$langs->trans('PaymentConditionsShort').''; + $html->select_conditions_paiements(isset($_POST['cond_reglement_id'])?$_POST['cond_reglement_id']:$cond_reglement_id,'cond_reglement_id'); + print '
'.$langs->trans('PaymentMode').''; + $html->select_types_paiements(isset($_POST['mode_reglement_id'])?$_POST['mode_reglement_id']:$mode_reglement_id,'mode_reglement_id'); + print '
'.$langs->trans('Project').''; + select_projects($soc->id, $projectid, 'projectid'); + print '
'.$langs->trans('Model').''; + include_once DOL_DOCUMENT_ROOT.'/core/modules/facture/modules_facture.php'; + $liste=ModelePDFFactures::liste_modeles($db); + print $html->selectarray('model',$liste,$conf->global->FACTURE_ADDON_PDF); + print "
'.$langs->trans('NotePublic').''; + print '
'.$langs->trans('NotePrivate').''; + print '
'; + + while ($i < $n) + { + print ''; + + $i++; + } + + // Button "Create Draft" + print '
'; + print "\n"; + + print ''; + print "\n"; +} + + +//Mode liste +if (($action != 'create' && $action != 'add') || ! empty($mesgs)) +{ + llxHeader(); + ?> + + rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql.= ' WHERE p.entity = '.$conf->entity; + $sql.= ' AND p.fk_soc = s.rowid'; + + // Show orders with status validated, shipping started and delivered (well any order we can bill) + $sql.= " AND p.fk_statut IN (1,2)"; + + if ($socid) $sql.= ' AND s.rowid = '.$socid; + if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; + if ($sref) + { + $sql.= " AND p.ref LIKE '%".$db->escape($sref)."%'"; + } + if ($sall) + { + $sql.= " AND (p.ref LIKE '%".$db->escape($sall)."%' OR p.note LIKE '%".$db->escape($sall)."%')"; + } + + //Date filter + if ($date_start && $date_end) $sql.= " AND p.date_valid >= '".$db->idate($date_start)."' AND p.date_valid <= '".$db->idate($date_end)."'"; + if ($date_starty && $date_endy) $sql.= " AND p.fin_validite >= '".$db->idate($date_starty)."' AND p.fin_validite <= '".$db->idate($date_endy)."'"; + + if (!empty($sref_client)) + { + $sql.= ' AND p.ref_client LIKE \'%'.$db->escape($sref_client).'%\''; + } + $sql.= ' ORDER BY '.$sortfield.' '.$sortorder; + $resql = $db->query($sql); + + if ($resql) + { + if ($socid) + { + $soc = new Societe($db); + $soc->fetch($socid); + } + $title.= $langs->trans('ListOfProposals').' - '.$langs->trans('PropalStatusOpened').', '.$langs->trans("PropalStatusSigned"); + $num = $db->num_rows($resql); + print_fiche_titre($title); + $i = 0; + $period=$html->select_date($date_start,'date_start',0,0,1,'',1,0,1).' - '.$html->select_date($date_end,'date_end',0,0,1,'',1,0,1); + $periodely=$html->select_date($date_starty,'date_start_dely',0,0,1,'',1,0,1).' - '.$html->select_date($date_endy,'date_end_dely',0,0,1,'',1,0,1); + + if (! empty($socid)) + { + // Company + $companystatic->id=$socid; + $companystatic->nom=$soc->nom; + print '

'.$companystatic->getNomUrl(1,'customer').'

'; + } + + print ''; + print ''; + print_liste_field_titre($langs->trans('Ref'),'propalstoorder.php','p.ref','','&socid='.$socid,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('RefCustomerOrder'),'propalstoorder.php','p.ref_client','','&socid='.$socid,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Date'),'propalstoorder.php','p.date_valid','','&socid='.$socid, 'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('DateEndPropal'),'propalstoorder.php','p.fin_validite','','&socid='.$socid, 'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Status'),'','','','','align="right"'); + print_liste_field_titre($langs->trans('GenerateBill'),'','','','','align="center"'); + print ''; + + // Lignes des champs de filtre + print ''; + print ''; + print ''; + print ''; + //print ''; + + //DATE DELIVERY + print ''; + + //SEARCH BUTTON + print ''; + + print ''; + print ''; + + print ''; + $var=True; + $generic_propal = new Propal($db); + + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ''; + print ''; + + print ''; + + // Validation date + print ''; + + // Limit valid date + print ''; + + // Statut + print ''; + + // Checkbox + print '' ; + + print ''; + + $total = $total + $objp->price; + $subtotal = $subtotal + $objp->price; + $i++; + } + print '
'; + //REF + print ''; + print ''; + print ''; + print ''; + + //DATE ORDER + print ''; + print $period; + print ''; + print $periodely; + print ''; + print ''; + + //ALL/NONE + print ''; + if ($conf->use_javascript_ajax) print ''.$langs->trans("All").' / '.$langs->trans("None").''; + print '
'; + + $generic_propal->id=$objp->rowid; + $generic_propal->ref=$objp->ref; + + print ''; + print ''; + + print ''; + + print '
'; + print $generic_propal->getNomUrl(1); + print ''; + if (($objp->fk_statut > 0) && ($objp->fk_statut < 3) && $db->jdate($objp->fin_validite) < ($now - $conf->propal->client->warning_delay)) print img_picto($langs->trans("Late"),"warning"); + print ''; + $filename=dol_sanitizeFileName($objp->ref); + $filedir=$conf->propal->dir_output . '/' . dol_sanitizeFileName($objp->ref); + $urlsource=$_SERVER['PHP_SELF'].'?id='.$objp->rowid; + print $formfile->getDocumentsLink($generic_propal->element, $filename, $filedir); + print '
'; + print '
'.$objp->ref_client.''; + print dol_print_date($db->jdate($objp->date_valid),'day'); + print ''; + print dol_print_date($db->jdate($objp->fin_validite),'day'); + print ''.$generic_propal->LibStatut($objp->fk_statut,5).''; + print ''; + print '
'; + + /* + * Boutons actions + */ + print '
'; + print ''; + print ''; + print '
'; + print ''.$langs->trans("GoBack").''; + print ''; + print '

'.$langs->trans("ClosePropalsAutomatically"); + print '
'; + print ''; + $db->free($resql); + } + else + { + print dol_print_error($db); + } + +} + +dol_htmloutput_mesg($mesg,$mesgs); + +llxFooter(); +$db->close(); +?> \ No newline at end of file diff --git a/htdocs/compta/facture_multi_origin.php b/htdocs/compta/facture_multi_origin.php new file mode 100644 index 00000000000..0d6424f3ecd --- /dev/null +++ b/htdocs/compta/facture_multi_origin.php @@ -0,0 +1,213 @@ +global->FACTURE_MULTI_ORIGIN) include_once(DOL_DOCUMENT_ROOT.'/compta/facture_multi_origin.php'); + +if(empty($origin)) return 0; +else if ($origin == 'commande') { + + // Show other origin objects mergeable into same invoice + print_titre($langs->trans('OtherOrders')); + + // Display list of elements + // 1 - Filters + $sref = GETPOST('sref'); + $sref_client = GETPOST('sref_client'); + $sall = GETPOST('sall'); + $date_start=dol_mktime(0,0,0,$_REQUEST["date_startmonth"],$_REQUEST["date_startday"],$_REQUEST["date_startyear"]); // Date for local PHP server + $date_end=dol_mktime(23,59,59,$_REQUEST["date_endmonth"],$_REQUEST["date_endday"],$_REQUEST["date_endyear"]); + $date_starty=dol_mktime(0,0,0,$_REQUEST["date_start_delymonth"],$_REQUEST["date_start_delyday"],$_REQUEST["date_start_delyyear"]); // Date for local PHP server + $date_endy=dol_mktime(23,59,59,$_REQUEST["date_end_delymonth"],$_REQUEST["date_end_delyday"],$_REQUEST["date_end_delyyear"]); + + $sortfield = GETPOST("sortfield",'alpha'); + $sortorder = GETPOST("sortorder",'alpha'); + if (! $sortfield) $sortfield='c.rowid'; + if (! $sortorder) $sortorder='DESC'; + + $html = new Form($db); + $htmlother = new FormOther($db); + $formfile = new FormFile($db); + $companystatic = new Societe($db); + + // 2 - Query for orders + $sql = 'SELECT s.nom, s.rowid as socid, s.client, c.rowid, c.ref, c.total_ht, c.ref_client,'; + $sql.= ' c.date_valid, c.date_commande, c.date_livraison, c.fk_statut, c.facture as facturee'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'societe as s'; + $sql.= ', '.MAIN_DB_PREFIX.'commande as c'; + if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql.= ' WHERE c.entity = '.$conf->entity; + $sql.= ' AND c.fk_soc = s.rowid'; + + // Show orders with status validated, shipping started and delivered (well any order we can bill) + $sql.= " AND ((c.fk_statut IN (1,2)) OR (c.fk_statut = 3 AND c.facture = 0))"; + + if ($socid) $sql.= ' AND s.rowid = '.$socid; + if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; + if ($sref) + { + $sql.= " AND c.ref LIKE '%".$db->escape($sref)."%'"; + } + if ($sall) + { + $sql.= " AND (c.ref LIKE '%".$db->escape($sall)."%' OR c.note LIKE '%".$db->escape($sall)."%')"; + } + if (!empty($sref_client)) + { + $sql.= ' AND c.ref_client LIKE \'%'.$db->escape($sref_client).'%\''; + } + + // Date filter + if ($date_start && $date_end) $sql.= " AND c.date_commande >= '".$db->idate($date_start)."' AND c.date_commande <= '".$db->idate($date_end)."'"; + if ($date_starty && $date_endy) $sql.= " AND c.date_livraison >= '".$db->idate($date_starty)."' AND c.date_livraison <= '".$db->idate($date_endy)."'"; + + $sql.= ' ORDER BY '.$sortfield.' '.$sortorder; + $resql = $db->query($sql); + + if ($resql) + { + if ($socid) + { + $soc = new Societe($db); + $soc->fetch($socid); + } + $num = $db->num_rows($resql); + + $i = 0; + $period=$html->select_date($date_start,'date_start',0,0,1,'',1,0,1).' - '.$html->select_date($date_end,'date_end',0,0,1,'',1,0,1); + $periodely=$html->select_date($date_starty,'date_start_dely',0,0,1,'',1,0,1).' - '.$html->select_date($date_endy,'date_end_dely',0,0,1,'',1,0,1); + + print ''; + print ''; + print_liste_field_titre($langs->trans('Ref'),'orderstoinvoice.php','c.ref','','&socid='.$socid,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('RefCustomerOrder'),'orderstoinvoice.php','c.ref_client','','&socid='.$socid,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('OrderDate'),'orderstoinvoice.php','c.date_commande','','&socid='.$socid, 'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('DeliveryDate'),'orderstoinvoice.php','c.date_livraison','','&socid='.$socid, 'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans('Status'),'','','','','align="right"'); + print_liste_field_titre($langs->trans('GenerateBill'),'','','','','align="center"'); + print ''; + + // Lignes des champs de filtre + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + //print ''; + + //DATE DELIVERY + print ''; + + //SEARCH BUTTON + print ''; + + print ''; + print ''; + + print ''; + print ''; + print ''; + print ''; + print ''; + $var=True; + $generic_commande = new Commande($db); + + while ($i < $num) + { + $objp = $db->fetch_object($resql); + $var=!$var; + print ''; + print ''; + + print ''; + + // Order date + print ''; + + //Delivery date + print ''; + + // Statut + print ''; + + // Checkbox + print '' ; + + print ''; + + $total = $total + $objp->price; + $subtotal = $subtotal + $objp->price; + $i++; + } + print '
'; + //REF + print ''; + print ''; + print ''; + print ''; + + //DATE ORDER + print ''; + print $period; + print ''; + print $periodely; + print ''; + print ''; + + //ALL/NONE + print ''; + if ($conf->use_javascript_ajax) print ''.$langs->trans("All").' / '.$langs->trans("None").''; + print '
'; + + $generic_commande->id=$objp->rowid; + $generic_commande->ref=$objp->ref; + + print ''; + print ''; + + print ''; + + print '
'; + print $generic_commande->getNomUrl(1,$objp->fk_statut); + print ''; + if (($objp->fk_statut > 0) && ($objp->fk_statut < 3) && $db->jdate($objp->date_valid) < ($now - $conf->commande->client->warning_delay)) print img_picto($langs->trans("Late"),"warning"); + print ''; + $filename=dol_sanitizeFileName($objp->ref); + $filedir=$conf->commande->dir_output . '/' . dol_sanitizeFileName($objp->ref); + $urlsource=$_SERVER['PHP_SELF'].'?id='.$objp->rowid; + print $formfile->getDocumentsLink($generic_commande->element, $filename, $filedir); + print '
'; + print '
'.$objp->ref_client.''; + print dol_print_date($db->jdate($objp->date_commande),'day'); + print ''; + print dol_print_date($db->jdate($objp->date_livraison),'day'); + print ''.$generic_commande->LibStatut($objp->fk_statut,$objp->facturee,5).''; + print ''; + print '
'; + print '

'.$langs->trans("CloseProcessedOrdersAutomatically"); + // Button "Create Draft" + print '
'; + print ''; + $db->free($resql); + ?> + + \ No newline at end of file