Exemplo n.º 1
0
    /**
     *    	Add a discount for third party
     *    	@param      remise      Montant de la remise
     *    	@param      user        Utilisateur qui accorde la remise
     *    	@param      desc		Motif de l'avoir
     *      @param      tva_tx      VAT rate
     *		@return		int			<0 if KO, id or record if OK
     */
    function set_remise_except($remise, $user, $desc, $tva_tx=0)
    {
        global $langs;

        // Nettoyage des parametres
        $remise = price2num($remise);
        $desc = trim($desc);

        // Check parameters
        if (! $remise > 0)
        {
            $this->error=$langs->trans("ErrorWrongValueForParameter","1");
            return -1;
        }
        if (! $desc)
        {
            $this->error=$langs->trans("ErrorWrongValueForParameter","3");
            return -2;
        }

        if ($this->id)
        {
            require_once(DOL_DOCUMENT_ROOT.'/core/class/discount.class.php');

            $discount = new DiscountAbsolute($this->db);
            $discount->fk_soc=$this->id;
            $discount->amount_ht=price2num($remise,'MT');
            $discount->amount_tva=price2num($remise*$tva_tx/100,'MT');
            $discount->amount_ttc=price2num($discount->amount_ht+$discount->amount_tva,'MT');
            $discount->tva_tx=price2num($tva_tx,'MT');
            $discount->description=$desc;
            $result=$discount->create($user);
            if ($result > 0)
            {
                return $result;
            }
            else
            {
                $this->error=$discount->error;
                return -3;
            }
        }
        else return 0;
    }
Exemplo n.º 2
0
 $label = !empty($lines[$i]->label) ? $lines[$i]->label : '';
 $desc = !empty($lines[$i]->desc) ? $lines[$i]->desc : $lines[$i]->libelle;
 if ($object->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 = $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);
         // 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;
Exemplo n.º 3
0
        $newdiscount1->datec = $discount->datec;
        $newdiscount2->datec = $discount->datec;
        $newdiscount1->tva_tx = $discount->tva_tx;
        $newdiscount2->tva_tx = $discount->tva_tx;
        $newdiscount1->amount_ttc = $_POST["amount_ttc_1"];
        $newdiscount2->amount_ttc = price2num($discount->amount_ttc - $newdiscount1->amount_ttc);
        $newdiscount1->amount_ht = price2num($newdiscount1->amount_ttc / (1 + $newdiscount1->tva_tx / 100), 'MT');
        $newdiscount2->amount_ht = price2num($newdiscount2->amount_ttc / (1 + $newdiscount2->tva_tx / 100), 'MT');
        $newdiscount1->amount_tva = price2num($newdiscount1->amount_ttc - $newdiscount2->amount_ht);
        $newdiscount2->amount_tva = price2num($newdiscount2->amount_ttc - $newdiscount2->amount_ht);
        $db->begin();
        $discount->fk_facture_source = 0;
        // This is to delete only the require record (that we will recreate with two records) and not all family with same fk_facture_source
        $res = $discount->delete($user);
        $newid1 = $newdiscount1->create($user);
        $newid2 = $newdiscount2->create($user);
        if ($res > 0 && $newid1 > 0 && $newid2 > 0) {
            $db->commit();
            header("Location: " . $_SERVER["PHP_SELF"] . '?id=' . $_REQUEST['id']);
            // To avoid pb whith back
            exit;
        } else {
            $db->rollback();
        }
    }
}
if ($action == 'setremise') {
    //if ($user->rights->societe->creer)
    //if ($user->rights->facture->creer)
    if (price2num($_POST["amount_ht"]) > 0) {
        $error = 0;
 /**
  *
  * Add facture Payment
  *
  * @param array $aryTicket	Ticket data array
  */
 private function addPaymentFac($aryTicket)
 {
     global $db, $langs, $conf;
     require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
     $now = dol_now();
     $userstatic = new User($db);
     $error = 0;
     if (!$aryTicket['employeeId']) {
         $employee = $_SESSION['uid'];
     } else {
         $employee = $aryTicket['employeeId'];
     }
     $userstatic->fetch($employee);
     $max_ite = 3;
     if ($aryTicket['convertDis']) {
         require_once DOL_DOCUMENT_ROOT . '/core/class/discount.class.php';
         $object = new Facture($db);
         $object->fetch($aryTicket['id']);
         $object->fetch_thirdparty();
         // Check if there is already a discount (protection to avoid duplicate creation when resubmit post)
         $discountcheck = new DiscountAbsolute($db);
         $result = $discountcheck->fetch(0, $object->id);
         $canconvert = 0;
         if ($object->type == Facture::TYPE_CREDIT_NOTE && $object->paye == 0 && empty($discountcheck->id)) {
             $canconvert = 1;
         }
         // we can convert credit note into discount if credit note is not payed back and not already converted and amount of payment is 0 (see real condition into condition used to show button converttoreduc)
         if ($canconvert) {
             $db->begin();
             // Boucle sur chaque taux de tva
             $i = 0;
             foreach ($object->lines as $line) {
                 $amount_ht[$line->tva_tx] += $line->total_ht;
                 $amount_tva[$line->tva_tx] += $line->total_tva;
                 $amount_ttc[$line->tva_tx] += $line->total_ttc;
                 $i++;
             }
             // Insert one discount by VAT rate category
             $discount = new DiscountAbsolute($db);
             if ($object->type == Facture::TYPE_CREDIT_NOTE) {
                 $discount->description = $langs->trans('DiscountOf', $object->ref);
             }
             $discount->tva_tx = abs($object->total_ttc);
             $discount->fk_soc = $object->socid;
             $discount->fk_facture_source = $object->id;
             $error = 0;
             foreach ($amount_ht as $tva_tx => $xxx) {
                 $discount->amount_ht = abs($amount_ht[$tva_tx]);
                 $discount->amount_tva = abs($amount_tva[$tva_tx]);
                 $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
                 $discount->tva_tx = abs($tva_tx);
                 $paiement_id = $discount->create($userstatic);
                 if ($paiement_id < 0) {
                     $error++;
                     break;
                 }
             }
             if (empty($error)) {
                 // Classe facture
                 $paiement_id = $object->set_paid($user);
                 if ($result >= 0) {
                     //$mesgs[]='OK'.$discount->id;
                     $db->commit();
                 } else {
                     $db->rollback();
                 }
             } else {
                 $db->rollback();
             }
         }
     } else {
         if ($aryTicket['type'] == 1) {
             if ($aryTicket['total'] > $aryTicket['customerpay'] && $aryTicket['difpayment'] == 0) {
                 dol_include_once('/rewards/class/rewards.class.php');
                 $reward = new Rewards($db);
                 $facture = new Facture($db);
                 $facture->fetch($aryTicket['id']);
                 $modepay[4] = dol_getIdFromCode($db, 'PNT', 'c_paiement');
                 $amount[4] = $aryTicket['total'] - $aryTicket['customerpay'];
                 $result = $reward->create($facture, price2num($amount[4]) / $conf->global->REWARDS_DISCOUNT);
                 $max_ite++;
                 $amount[4] = $amount[4] * -1;
                 //TODO tot molt bonico, pero que pasa si no gaste punts?
             }
             $aryTicket['total'] = $aryTicket['total'] * -1;
             $aryTicket['customerpay1'] = $aryTicket['customerpay1'] * -1;
             $aryTicket['customerpay2'] = $aryTicket['customerpay2'] * -1;
             $aryTicket['customerpay3'] = $aryTicket['customerpay3'] * -1;
         }
         $cash = new Cash($db);
         $terminal = $_SESSION['TERMINAL_ID'];
         $cash->fetch($terminal);
         if ($aryTicket['customerpay1'] != 0) {
             $bankaccountid[1] = $cash->fk_paycash;
             $modepay[1] = $cash->fk_modepaycash;
             $amount[1] = $aryTicket['customerpay1'] + ($aryTicket['difpayment'] < 0 ? $aryTicket['difpayment'] : 0);
         }
         if ($aryTicket['customerpay2'] != 0) {
             $bankaccountid[2] = $cash->fk_paybank;
             $modepay[2] = $cash->fk_modepaybank;
             $amount[2] = $aryTicket['customerpay2'];
         }
         if ($aryTicket['customerpay3'] != 0) {
             $bankaccountid[3] = $cash->fk_paybank_extra;
             $modepay[3] = $cash->fk_modepaybank_extra;
             $amount[3] = $aryTicket['customerpay3'];
         }
         //Añadir el posible pago de puntos
         if ($aryTicket['points'] > 0) {
             dol_include_once('/rewards/class/rewards.class.php');
             $reward = new Rewards($db);
             $facture = new Facture($db);
             $facture->fetch($aryTicket['id']);
             $res = $reward->usePoints($facture, $aryTicket['points']);
         }
         $i = 1;
         $payment = new Paiement($db);
         while ($i <= $max_ite) {
             $payment->datepaye = $now;
             $payment->bank_account = $bankaccountid[$i];
             $payment->amounts[$aryTicket['id']] = $amount[$i];
             $payment->note = $langs->trans("Payment") . ' ' . $langs->trans("Facture") . ' ' . $aryTicket['ref'];
             $payment->paiementid = $modepay[$i];
             $payment->num_paiement = '';
             if ($amount[$i] != 0) {
                 $paiement_id = $payment->create($userstatic, 1);
                 if ($paiement_id > 0) {
                     if ($payment->paiementid != dol_getIdFromCode($db, 'PNT', 'c_paiement')) {
                         $result = $payment->addPaymentToBank($userstatic, 'payment', '(CustomerFacturePayment)', $bankaccountid[$i], $aryTicket['customerId'], '', '');
                         if ($result < 0) {
                             $error++;
                         }
                     }
                 } else {
                     $error++;
                 }
             }
             $i++;
         }
     }
     if ($error > 0) {
         return -1;
     } else {
         return 1;
     }
     //$paiement_id;
 }
Exemplo n.º 5
0
    /**
     * testDiscountCreate
     *
     * @return	int
     */
    public function testDiscountCreate()
    {
    	global $conf,$user,$langs,$db;
		$conf=$this->savconf;
		$user=$this->savuser;
		$langs=$this->savlangs;
		$db=$this->savdb;

		$localobject=new DiscountAbsolute($this->savdb);
    	$localobject->initAsSpecimen();
    	$result=$localobject->create($user);

    	$this->assertLessThan($result, 0);
    	print __METHOD__." result=".$result."\n";
    	return $result;
    }
 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));
         }
     }
 }
function _createAvoir(&$PDOdb, &$db, &$user, &$conf, &$langs)
{
    dol_include_once('/compta/facture/class/facture.class.php');
    dol_include_once('/core/class/discount.class.php');
    $sql = _getSql2();
    $PDOdb->Execute($sql);
    $TFacnumberFetchError = array();
    $TFacnumberCreateError = array();
    $TDiscountCreateError = array();
    $nbValidate = 0;
    while ($row = $PDOdb->Get_line()) {
        $fk_soc = $row->fk_soc;
        $facnumber = $row->facnumber;
        $factureImpayee = new Facture($db);
        if ($factureImpayee->fetch(null, $facnumber) <= 0) {
            $TFacnumberFetchError[] = $facnumber;
            continue;
        }
        $dateinvoice = dol_mktime(12, 0, 0, date('m'), date('d'), date('Y'));
        $facture = new Facture($db);
        $facture->socid = $fk_soc;
        $facture->fk_facture_source = $factureImpayee->id;
        $facture->type = Facture::TYPE_CREDIT_NOTE;
        $facture->date = $dateinvoice;
        if ($facture->create($user) <= 0) {
            $TFacnumberCreateError[] = $facnumber;
            continue;
        }
        foreach ($factureImpayee->lines as $line) {
            $line->fk_facture = $facture->id;
            $line->subprice = -$line->subprice;
            // invert price for object
            $line->pa_ht = -$line->pa_ht;
            $line->total_ht = -$line->total_ht;
            $line->total_tva = -$line->total_tva;
            $line->total_ttc = -$line->total_ttc;
            $line->total_localtax1 = -$line->total_localtax1;
            $line->total_localtax2 = -$line->total_localtax2;
            $line->insert();
            $facture->lines[] = $line;
            // insert new line in current object
        }
        $facture->update_price(1);
        $facture->validate($user);
        $discountcheck = new DiscountAbsolute($db);
        $result = $discountcheck->fetch(0, $facture->id);
        if (!empty($discountcheck->id)) {
            //can't convert
            $facture->delete();
            continue;
        }
        $i = 0;
        $amount_ht = $amount_tva = $amount_ttc = array();
        foreach ($facture->lines as $line) {
            if ($line->total_ht != 0) {
                // no need to create discount if amount is null
                $amount_ht[$line->tva_tx] += $line->total_ht;
                $amount_tva[$line->tva_tx] += $line->total_tva;
                $amount_ttc[$line->tva_tx] += $line->total_ttc;
                $i++;
            }
        }
        // Insert one discount by VAT rate category
        $discount = new DiscountAbsolute($db);
        $discount->description = '(CREDIT_NOTE)';
        $discount->tva_tx = abs($facture->total_ttc);
        $discount->fk_soc = $facture->socid;
        $discount->fk_facture_source = $facture->id;
        foreach ($amount_ht as $tva_tx => $xxx) {
            $discount->amount_ht = abs($amount_ht[$tva_tx]);
            $discount->amount_tva = abs($amount_tva[$tva_tx]);
            $discount->amount_ttc = abs($amount_ttc[$tva_tx]);
            $discount->tva_tx = abs($tva_tx);
            $result = $discount->create($user);
            if ($result < 0) {
                $TDiscountCreateError[] = $facnumber;
                $error++;
                break;
            }
            $result = $facture->set_paid($user);
            $result = $discount->link_to_invoice(0, $factureImpayee->id);
            $r = $factureImpayee->set_paid($user);
        }
        /******/
        $nbValidate++;
    }
    if ($nbValidate) {
        setEventMessages($langs->trans('sendinvoicetoadherentAvoirValidate', $nbValidate), null);
    }
    if (count($TFacnumberFetchError) > 0) {
        setEventMessages($langs->trans('sendinvoicetoadherentErrorFetchFacture', count($TFacnumberFetchError)), null, 'errors');
    }
    if (count($TFacnumberCreateError) > 0) {
        setEventMessages($langs->trans('sendinvoicetoadherentErrorCreateAvoir', count($TFacnumberCreateError)), null, 'errors');
    }
    $_SESSION['SENDTOINVOICETOADHERENT_TFETCHERROR'] = $TFacnumberFetchError;
    $_SESSION['SENDTOINVOICETOADHERENT_TCREATEERROR'] = $TFacnumberCreateError;
    header('Location: ' . dol_buildpath('/sendinvoicetoadherent/sendinvoicetoadherent.php?action=listAvoir', 2));
    exit;
}