26 require
'../main.inc.php';
27 require_once DOL_DOCUMENT_ROOT.
'/cashdesk/include/environnement.php';
28 require_once DOL_DOCUMENT_ROOT.
'/cashdesk/class/Facturation.class.php';
29 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
30 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
31 require_once DOL_DOCUMENT_ROOT.
'/compta/paiement/class/paiement.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
34 $obj_facturation = unserialize($_SESSION[
'serObjFacturation']);
36 $action =
GETPOST(
'action',
'aZ09');
37 $bankaccountid =
GETPOST(
'cashdeskbank');
42 $redirection = DOL_URL_ROOT.
'/cashdesk/affIndex.php?menutpl=validation';
46 $thirdpartyid = $_SESSION[
'CASHDESK_ID_THIRDPARTY'];
49 $company->fetch($thirdpartyid);
57 $sav_FACTURE_ADDON =
'';
58 if (!empty($conf->global->POS_ADDON))
60 $sav_FACTURE_ADDON = $conf->global->FACTURE_ADDON;
61 $conf->global->FACTURE_ADDON = $conf->global->POS_ADDON;
64 if (!empty($conf->global->POS_NUMBERING_TERRE_FORCE_PREFIX)) $conf->global->INVOICE_NUMBERING_TERRE_FORCE_PREFIX = $conf->global->POS_NUMBERING_TERRE_FORCE_PREFIX;
66 if (!empty($conf->global->POS_NUMBERING_MARS_FORCE_PREFIX)) $conf->global->INVOICE_NUMBERING_MARS_FORCE_PREFIX = $conf->global->POS_NUMBERING_MARS_FORCE_PREFIX;
71 $num = $invoice->getNextNumRef($company);
74 if (!empty($sav_FACTURE_ADDON))
76 $conf->global->FACTURE_ADDON = $sav_FACTURE_ADDON;
79 $obj_facturation->numInvoice($num);
81 $obj_facturation->getSetPaymentMode($_POST[
'hdnChoix']);
84 $mode_reglement = $obj_facturation->getSetPaymentMode();
85 if ($mode_reglement !=
'ESP') {
86 $montant = $obj_facturation->amountWithTax();
88 $montant = $_POST[
'txtEncaisse'];
91 if ($mode_reglement !=
'DIF') {
92 $obj_facturation->amountCollected($montant);
95 $total = $obj_facturation->amountWithTax();
96 $encaisse = $obj_facturation->amountCollected();
98 $obj_facturation->amountReturned($encaisse - $total);
101 $datePaiement =
dol_mktime(0, 0, 0, $_POST[
'txtDatePaiementmonth'], $_POST[
'txtDatePaiementday'], $_POST[
'txtDatePaiementyear']);
103 $obj_facturation->paiementLe($txtDatePaiement);
106 $redirection =
'affIndex.php?menutpl=validation';
111 $redirection =
'affIndex.php?menutpl=facturation';
115 case 'validate_invoice':
123 if (!is_object($obj_facturation))
129 switch ($obj_facturation->getSetPaymentMode())
132 $mode_reglement_id = 0;
134 $cond_reglement_id = 0;
137 $mode_reglement_id =
dol_getIdFromCode($db,
'LIQ',
'c_paiement',
'code',
'id', 1);
138 $cond_reglement_id = 0;
139 $note .= $langs->trans(
"Cash").
"\n";
140 $note .= $langs->trans(
"Received").
' : '.$obj_facturation->amountCollected().
" ".$conf->currency.
"\n";
141 $note .= $langs->trans(
"Rendu").
' : '.$obj_facturation->amountReturned().
" ".$conf->currency.
"\n";
143 $note .=
'--------------------------------------'.
"\n\n";
146 $mode_reglement_id =
dol_getIdFromCode($db,
'CB',
'c_paiement',
'code',
'id', 1);
147 $cond_reglement_id = 0;
150 $mode_reglement_id =
dol_getIdFromCode($db,
'CHQ',
'c_paiement',
'code',
'id', 1);
151 $cond_reglement_id = 0;
154 if (empty($mode_reglement_id)) $mode_reglement_id = 0;
155 if (empty($cond_reglement_id)) $cond_reglement_id = 0;
156 $note .= $_POST[
'txtaNotes'];
157 dol_syslog(
"obj_facturation->getSetPaymentMode()=".$obj_facturation->getSetPaymentMode().
" mode_reglement_id=".$mode_reglement_id.
" cond_reglement_id=".$cond_reglement_id);
164 $user->fetch($_SESSION[
'uid']);
167 $thirdpartyid = $_SESSION[
'CASHDESK_ID_THIRDPARTY'];
169 $societe->fetch($thirdpartyid);
174 $tab_liste = $_SESSION[
'poscart'];
177 $tab_liste_size = count($tab_liste);
178 for ($i = 0; $i < $tab_liste_size; $i++)
181 $vat_rate = $tmp[
'rate'];
182 $vat_npr = $tmp[
'npr'];
183 $vat_src_code = $tmp[
'code'];
186 $invoiceline->fk_product = $tab_liste[$i][
'fk_article'];
187 $invoiceline->desc = $tab_liste[$i][
'label'];
188 $invoiceline->qty = $tab_liste[$i][
'qte'];
189 $invoiceline->remise_percent = $tab_liste[$i][
'remise_percent'];
190 $invoiceline->price = $tab_liste[$i][
'price'];
191 $invoiceline->subprice = $tab_liste[$i][
'price'];
193 $invoiceline->tva_tx = empty($vat_rate) ? 0 : $vat_rate;
194 $invoiceline->info_bits = empty($vat_npr) ? 0 : $vat_npr;
195 $invoiceline->vat_src_code = $vat_src_code;
197 $invoiceline->total_ht = $tab_liste[$i][
'total_ht'];
198 $invoiceline->total_ttc = $tab_liste[$i][
'total_ttc'];
199 $invoiceline->total_tva = $tab_liste[$i][
'total_vat'];
200 $invoiceline->total_localtax1 = $tab_liste[$i][
'total_localtax1'];
201 $invoiceline->total_localtax2 = $tab_liste[$i][
'total_localtax2'];
203 $invoice->lines[] = $invoiceline;
206 $invoice->socid = $conf_fksoc;
207 $invoice->date_creation = $now;
208 $invoice->date = $now;
209 $invoice->date_lim_reglement = 0;
210 $invoice->total_ht = $obj_facturation->amountWithoutTax();
211 $invoice->total_tva = $obj_facturation->amountVat();
212 $invoice->total_ttc = $obj_facturation->amountWithTax();
213 $invoice->note_private = $note;
214 $invoice->cond_reglement_id = $cond_reglement_id;
215 $invoice->mode_reglement_id = $mode_reglement_id;
216 $invoice->module_source =
'cashdesk';
217 $invoice->pos_source =
'0';
221 if ($obj_facturation->getSetPaymentMode() ==
'DIF')
223 $resultcreate = $invoice->create($user, 0,
dol_stringtotime($obj_facturation->paiementLe()));
224 if ($resultcreate > 0)
226 $warehouseidtodecrease = (isset($_SESSION[
"CASHDESK_ID_WAREHOUSE"]) ? $_SESSION[
"CASHDESK_ID_WAREHOUSE"] : 0);
227 if (!empty($conf->global->CASHDESK_NO_DECREASE_STOCK)) $warehouseidtodecrease = 0;
229 $resultvalid = $invoice->validate($user, $obj_facturation->numInvoice(), 0);
231 if ($warehouseidtodecrease > 0)
234 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
235 $langs->load(
"agenda");
237 $cpt = count($invoice->lines);
238 for ($i = 0; $i < $cpt; $i++)
240 if ($invoice->lines[$i]->fk_product > 0)
243 $mouvP->origin = &$invoice;
245 if ($invoice->type == $invoice::TYPE_CREDIT_NOTE) $result = $mouvP->reception($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans(
"InvoiceValidatedInDolibarrFromPos", $invoice->newref));
246 else $result = $mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans(
"InvoiceValidatedInDolibarrFromPos", $invoice->newref));
260 $resultcreate = $invoice->create($user, 0, 0);
261 if ($resultcreate > 0)
263 $warehouseidtodecrease = (isset($_SESSION[
"CASHDESK_ID_WAREHOUSE"]) ? $_SESSION[
"CASHDESK_ID_WAREHOUSE"] : 0);
264 if (!empty($conf->global->CASHDESK_NO_DECREASE_STOCK)) $warehouseidtodecrease = 0;
266 $resultvalid = $invoice->validate($user, $obj_facturation->numInvoice(), 0);
268 if ($warehouseidtodecrease > 0)
271 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
272 $langs->load(
"agenda");
274 $cpt = count($invoice->lines);
275 for ($i = 0; $i < $cpt; $i++)
277 if ($invoice->lines[$i]->fk_product > 0)
280 $mouvP->origin = &$invoice;
282 if ($invoice->type == $invoice::TYPE_CREDIT_NOTE) $result = $mouvP->reception($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans(
"InvoiceValidatedInDolibarrFromPos", $invoice->newref));
283 else $result = $mouvP->livraison($user, $invoice->lines[$i]->fk_product, $warehouseidtodecrease, $invoice->lines[$i]->qty, $invoice->lines[$i]->subprice, $langs->trans(
"InvoiceValidatedInDolibarrFromPos", $invoice->newref));
296 $payment->datepaye = $now;
297 $payment->amounts[$invoice->id] = $obj_facturation->amountWithTax();
298 $payment->note_public = $langs->trans(
"Payment").
' '.$langs->trans(
"Invoice").
' '.$obj_facturation->numInvoice();
299 $payment->paiementid = $invoice->mode_reglement_id;
300 $payment->num_paiement =
'';
301 $payment->num_payment =
'';
303 $paiement_id = $payment->create($user);
304 if ($paiement_id > 0)
308 $result = $payment->addPaymentToBank($user,
'payment',
'(CustomerInvoicePayment)', $bankaccountid,
'',
'');
311 $errmsg = $paiement->error;
318 if ($invoice->total_ttc == $obj_facturation->amountWithTax()
319 && $obj_facturation->getSetPaymentMode() !=
'DIFF')
322 $result = $invoice->set_paid($user);
340 $redirection =
'affIndex.php?menutpl=validation_ok&facid='.$id;
343 $redirection =
'affIndex.php?facid='.$id.
'&error=1&mesg=ErrorFailedToCreateInvoice';
350 unset($_SESSION[
'serObjFacturation']);
352 $_SESSION[
'serObjFacturation'] = serialize($obj_facturation);
354 header(
'Location: '.$redirection);
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage stock movements.
const TYPE_STANDARD
Standard invoice.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_now($mode= 'auto')
Return date for now.
getTaxesFromId($vatrate, $buyer=null, $seller=null, $firstparamisid=1)
Get tax (VAT) main information from Id.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage payments of customer invoices.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Class to manage invoices.
Class to manage invoice lines.