Exemplo n.º 1
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 {
             setEventMessages($discount->error, $discount->errors, 'errors');
             $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) {
 /**
  *
  * Create facture into Database
  *
  * @param	array	$aryTicket		Ticket object
  */
 private function CreateFacture($aryTicket)
 {
     global $db, $user, $conf;
     $function = "CreateFacture";
     $idFacture = -1;
     $data = $aryTicket['data'];
     $lines = $data['lines'];
     $idTicket = $data["id"];
     if ($data['idsource'] > 0) {
         $prods_returned = self::testSourceFac($aryTicket);
         if (sizeof($prods_returned) > 0) {
             return -6;
         }
         $vater = new Facture($db);
         $vater->fetch($data['idsource']);
         $data['payment_type'] = $vater->mode_reglement_id;
     }
     $cash = new Cash($db);
     $terminal = $data['cashId'];
     $cash->fetch($terminal);
     if (!$data['customerId']) {
         $socid = $cash->fk_soc;
         $data['customerId'] = $socid;
     } else {
         $socid = $data['customerId'];
     }
     if (!$data['employeeId']) {
         $employee = $_SESSION['uid'];
     } else {
         $employee = $data['employeeId'];
     }
     if ($data['mode'] == 1) {
         $object = new Facturesim($db);
     } else {
         $object = new Facture($db);
     }
     $object->type = $data['type'] == 0 ? 0 : 2;
     $object->socid = $socid;
     $object->statut = $data['state'];
     $object->fk_cash = $terminal;
     $object->remise_percent = $data['discount_percent'];
     $object->remise_absolue = $data['discount_qty'];
     if ($data['customerpay1'] > 0) {
         $object->mode_reglement_id = $cash->fk_modepaycash;
     } else {
         if ($data['customerpay2'] > 0) {
             $object->mode_reglement_id = $cash->fk_modepaybank;
         } else {
             $object->mode_reglement_id = $cash->fk_modepaybank_extra;
         }
     }
     $object->fk_place = $data['id_place'];
     $object->note_private = $data['note'];
     $object->customer_pay = $data['customerpay'];
     if ($object->customer_pay > 0) {
         $object->diff_payment = $data['difpayment'];
     } else {
         $object->diff_payment = $data['total'];
     }
     $object->fk_facture_source = $data['idsource'];
     $employ = new User($db);
     $employ->fetch($employee);
     $employ->getrights();
     $now = dol_now();
     $object->date = $now;
     $db->begin;
     $idFacture = $object->create($employ);
     if ($object->statut == 1 || $object->type == 2) {
         $res = $object->validate($employ);
         if ($res < 0) {
             $soc = new Societe($db);
             $soc->fetch($socid);
             $num = $object->getNextNumRef($soc);
             // Validate
             $sql = 'UPDATE ' . MAIN_DB_PREFIX . 'facture';
             $sql .= " SET facnumber='" . $num . "', fk_statut = 1, fk_user_valid = " . $employ->id . ", date_valid = '" . $db->idate($now) . "'";
             if (!empty($conf->global->FAC_FORCE_DATE_VALIDATION)) {
                 $sql .= ', datef=' . $db->idate($now);
                 $sql .= ', date_lim_reglement=' . $db->idate($now);
             }
             $sql .= ' WHERE rowid = ' . $object->id;
             dol_syslog(get_class($this) . "::validate sql=" . $sql);
             $resql = $db->query($sql);
             $object->ref = $num;
         }
     }
     $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'pos_facture (fk_cash, fk_place,fk_facture,customer_pay) VALUES (' . $object->fk_cash . ',' . ($object->fk_place ? $object->fk_place : 'null') . ',' . $idFacture . ',' . $object->customer_pay . ')';
     dol_syslog("pos_facture::update sql=" . $sql);
     $resql = $db->query($sql);
     if (!$resql) {
         $this->db->rollback();
         return -1;
     }
     $data['ref'] = $object->ref;
     if ($idFacture < 0) {
         $db->rollback();
         return -1;
     } else {
         //Adding lines
         $data['id'] = $idFacture;
         //introducir descuentos
         if (!empty($data['idCoupon'])) {
             $res_dis = $object->insert_discount($data['idCoupon']);
         } else {
             $res_dis = 1;
         }
         $idLines = self::addFactureLines($lines, $idFacture, $object->type == 1 ? true : false);
         if ($idLines < 0 || $res_dis < 0) {
             $db->rollback();
             return -2;
         } else {
             //Adding Payments
             $payment = self::addPaymentFac($data);
             if ($payment < 0) {
                 $db->rollback();
                 return -3;
             }
             //Decrease stock
             $stock = self::quitSotck($lines, $object->type == 2 ? true : false);
             if ($stock) {
                 $db->rollback();
                 return -4;
             }
         }
     }
     if ($idTicket) {
         $ticket = new Ticket($db);
         $ticket->fetch($idTicket);
         $ticket->delete_ticket();
     }
     return $idFacture;
 }
 static function createFactureFromObject(&$object)
 {
     global $db, $conf, $user, $langs;
     dol_include_once('/compta/facture/class/facture.class.php');
     $langs->load('grapefruit@grapefruit');
     $dateinvoice = dol_mktime(0, 0, 0, date('m'), date('d'), date('Y'));
     $f = new Facture($db);
     $f->socid = $object->socid;
     $f->type = Facture::TYPE_STANDARD;
     $f->number = $_POST['facnumber'];
     $f->date = $dateinvoice;
     $f->note_public = $object->note_public;
     $f->note_private = $object->note_private;
     $f->ref_client = $object->ref_client;
     $f->fk_project = $object->fk_project;
     $f->cond_reglement_id = $object->cond_reglement_id;
     $f->mode_reglement_id = $object->mode_reglement_id;
     $origin = 'commande';
     $originid = $object->id;
     $f->linked_objects[$origin] = $originid;
     $id = $f->create($user);
     $lines = $object->lines;
     if (empty($lines) && method_exists($object, 'fetch_lines')) {
         $object->fetch_lines();
         $lines = $object->lines;
     }
     $fk_parent_line = 0;
     $num = count($lines);
     for ($i = 0; $i < $num; $i++) {
         $label = !empty($lines[$i]->label) ? $lines[$i]->label : '';
         $desc = !empty($lines[$i]->desc) ? $lines[$i]->desc : $lines[$i]->libelle;
         if ($f->situation_counter == 1) {
             $lines[$i]->situation_percent = 0;
         }
         if ($lines[$i]->subprice < 0) {
             // Negative line, we create a discount line
             $discount = new DiscountAbsolute($db);
             $discount->fk_soc = $f->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 = $f->insert_discount($discountid);
                 // This include link_to_invoice
             } else {
                 setEventMessages($discount->error, $discount->errors, 'errors');
                 $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;
             }
             // Extrafields
             if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED) && method_exists($lines[$i], 'fetch_optionals')) {
                 $lines[$i]->fetch_optionals($lines[$i]->rowid);
                 $array_options = $lines[$i]->array_options;
             }
             // View third's localtaxes for now
             $localtax1_tx = get_localtax($lines[$i]->tva_tx, 1, $f->client);
             $localtax2_tx = get_localtax($lines[$i]->tva_tx, 2, $f->client);
             $result = $f->addline($desc, $lines[$i]->subprice, $lines[$i]->qty, $lines[$i]->tva_tx, $localtax1_tx, $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, $f->origin, $lines[$i]->rowid, $fk_parent_line, $lines[$i]->fk_fournprice, $lines[$i]->pa_ht, $label, $array_options, $lines[$i]->situation_percent, $lines[$i]->fk_prev_id, $lines[$i]->fk_unit);
             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;
             }
         }
     }
     if (empty($error)) {
         if ($f->validate($user) > 0) {
             $object->classifyBilled();
             // Redirection vers écrand de paiement
             setEventMessage($langs->trans('BillCreated'));
             header('Location: ' . dol_buildpath('/compta/paiement.php?action=create&facid=' . $f->id, 1));
         }
     }
 }