<?php

/**
 * Class that operate on table 'sellbilloffer'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2020-08-24 15:24
 */
class SellbillofferMySqlExtDAO extends SellbillofferMySqlDAO {

    public function queryallWithOrder($queryString) {
        $sql = 'SELECT sellbill.*, client.clientname, user.employeename , user2.employeename  as  useremployeename
			 FROM sellbilloffer as sellbill
                         LEFT JOIN client ON sellbill.sellbillclientid = client.clientid
                         LEFT JOIN user ON sellbill.sellerid = user.userid
			 LEFT JOIN user as user2 ON sellbill.userid = user2.userid

			 ' . $queryString . '
			 ORDER BY  `sellbilldate`  desc ,sellbillid desc  ';
//        print_r($sql . '<br>');
        $sqlQuery = new SqlQuery($sql);
        return $this->getList($sqlQuery);
    }

    public function loadSellBillById($sellbillid) {
        $sql = 'SELECT sellbill.*, client.clientname, store.storeName, user.employeename , user2.employeename  as  useremployeename
		 FROM sellbilloffer as sellbill LEFT JOIN client
		 ON sellbill.sellbillclientid = client.clientid JOIN store
		 ON sellbill.sellbillstoreid = store.storeId LEFT JOIN user
		 ON sellbill.sellerid = user.userid
		 LEFT JOIN user as user2
		  ON sellbill.userid = user2.userid
		 WHERE sellbill.sellbillid = ' . $sellbillid . ' ';
        // print_r('<br>'.$sql.'<br>');
        $sqlQuery = new SqlQuery($sql);
        return $this->getRow($sqlQuery);
    }

    public function getalldata($queryString) {
        $sql = 'SELECT sellbill.*, client.clientname
        -- , store.storeName, user.employeename , user2.employeename  as  useremployeename
         FROM sellbilloffer as sellbill LEFT JOIN client
         ON sellbill.sellbillclientid = client.clientid
         -- JOIN store
         -- ON sellbill.sellbillstoreid = store.storeId LEFT JOIN user
         -- ON sellbill.sellerid = user.userid
         -- LEFT JOIN user as user2
         --  ON sellbill.userid = user2.userid
         WHERE 1 ' . $sellbillid . ' ';
        $sqlQuery = new SqlQuery($sql);
        return $this->getRow($sqlQuery);
    }

    public function insertupdate($sellbilloffer) {
        $sql = 'INSERT INTO sellbilloffer (sellbilldate, sellbillserial, sellbillclientid, sellbilltotaldeptbefor, sellbilltotaldeptafter, sellbilldiscount, sellbilldiscounttype, sellbilltotalbill, sellbillaftertotalbill, sellbilltotalpayed, sellbillfinalbill, sellbillstoreid, sellbillsysdate,comment, userid, conditions, sellbillclientname, sellbillsaveid, sellbildirectpayment, billnameid, sellerid, tempclientName, sellQuantity,tax ,carnumber, cartype, carchase, carmotor,costcenterid,dailyentryid,obygyVisitId,onlineOrderId,delbyuserid,delivery,totalafterdelivery,billReservation,billReservationDate,webApiId,visaAccountId,cashPayed,visaPayed,isBankAccountTransfer,extraDiscountPer,extraDiscountVal,taxOfDiscountPer,taxOfDiscountVal,taxBillNumber,collectionType,collectionDate,qrerpid,qrerpids,autoSaveFlag,ereason,estatus,epdf,obgyBillType,billoperationid,datestarting,pricetype,orderSavedBillId) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
        $sqlQuery = new SqlQuery($sql);

        $sqlQuery->set($sellbilloffer->sellbilldate);
        $sqlQuery->set($sellbilloffer->sellbillserial);
        $sqlQuery->setNumber($sellbilloffer->sellbillclientid);
        $sqlQuery->set($sellbilloffer->sellbilltotaldeptbefor);
        $sqlQuery->set($sellbilloffer->sellbilltotaldeptafter);
        $sqlQuery->set($sellbilloffer->sellbilldiscount);
        $sqlQuery->setNumber($sellbilloffer->sellbilldiscounttype);
        $sqlQuery->set($sellbilloffer->sellbilltotalbill);
        $sqlQuery->set($sellbilloffer->sellbillaftertotalbill);
        $sqlQuery->set($sellbilloffer->sellbilltotalpayed);
        $sqlQuery->set($sellbilloffer->sellbillfinalbill);
        $sqlQuery->setNumber($sellbilloffer->sellbillstoreid);
        $sqlQuery->set($sellbilloffer->sellbillsysdate);
        $sqlQuery->set($sellbilloffer->comment);
        $sqlQuery->setNumber($sellbilloffer->userid);
        $sqlQuery->setNumber($sellbilloffer->conditions);
        $sqlQuery->set($sellbilloffer->sellbillclientname);
        $sqlQuery->setNumber($sellbilloffer->sellbillsaveid);
        $sqlQuery->setNumber($sellbilloffer->sellbildirectpayment);
        $sqlQuery->setNumber($sellbilloffer->billnameid);
        $sqlQuery->setNumber($sellbilloffer->sellerid);
        $sqlQuery->set($sellbilloffer->tempclientName);
        $sqlQuery->setNumber($sellbilloffer->sellQuantity);

        $sqlQuery->set($sellbilloffer->tax);
        $sqlQuery->set($sellbilloffer->carnumber);
        $sqlQuery->set($sellbilloffer->cartype);
        $sqlQuery->set($sellbilloffer->carchase);
        $sqlQuery->set($sellbilloffer->carmotor);

        $sqlQuery->setNumber($sellbilloffer->costcenterid);
        $sqlQuery->set($sellbilloffer->dailyentryid);
        $sqlQuery->setNumber((int) $sellbilloffer->obygyVisitId);
        $sqlQuery->setNumber((int) $sellbilloffer->onlineOrderId);
        $sqlQuery->setNumber((int) $sellbilloffer->delbyuserid);
        $sqlQuery->setNumber((int) $sellbilloffer->delivery);
        $sqlQuery->setNumber((int) $sellbilloffer->totalafterdelivery);
        $sqlQuery->setNumber((int) $sellbilloffer->billReservation);
        $sqlQuery->set($sellbilloffer->billReservationDate);
        $sqlQuery->setNumber((int) $sellbilloffer->webApiId);
        $sqlQuery->setNumber((int) $sellbilloffer->visaAccountId);
        $sqlQuery->setNumber((float) $sellbilloffer->cashPayed);
        $sqlQuery->setNumber((float) $sellbilloffer->visaPayed);
        $sqlQuery->setNumber((int) $sellbilloffer->isBankAccountTransfer);
        $sqlQuery->setNumber((float) $sellbilloffer->extraDiscountPer);
        $sqlQuery->setNumber((float) $sellbilloffer->extraDiscountVal);
        $sqlQuery->setNumber((float) $sellbilloffer->taxOfDiscountPer);
        $sqlQuery->setNumber((float) $sellbilloffer->taxOfDiscountVal);
        $sqlQuery->setNumber((int) $sellbilloffer->taxBillNumber);
        $sqlQuery->setNumber((int) $sellbilloffer->collectionType);
        $sqlQuery->set($sellbilloffer->collectionDate);
        $sqlQuery->set($sellbilloffer->qrerpid);
        $sqlQuery->set($sellbilloffer->qrerpids);
        $sqlQuery->setNumber((int) $sellbilloffer->autoSaveFlag);
        $sqlQuery->set($sellbilloffer->ereason);
        $sqlQuery->setNumber((int) $sellbilloffer->estatus);
        $sqlQuery->set($sellbilloffer->epdf);
        $sqlQuery->setNumber((int) $sellbilloffer->obgyBillType);
        $sqlQuery->setNumber((int) $sellbilloffer->billoperationid);
        $sqlQuery->set($sellbilloffer->datestarting);
        $sqlQuery->setNumber((int) $sellbilloffer->pricetype);
        $sqlQuery->setNumber((int) $sellbilloffer->orderSavedBillId);

        $id = $this->executeInsert($sqlQuery);
        $sellbilloffer->sellbillid = $id;
        return $id;
    }

    public function getAutoIncrementValue($DBName) {
        $sql = 'SELECT `AUTO_INCREMENT`
                FROM  INFORMATION_SCHEMA.TABLES
                WHERE TABLE_SCHEMA = "' . $DBName . '"
                AND   TABLE_NAME   = "sellbilloffer" ';

        $sqlQuery = new SqlQuery($sql); //
        return $this->querySingleResult($sqlQuery);
    }

    public function updateOrderSavedBillId($sellbillId, $offerId) {
        $sql = 'UPDATE sellbilloffer SET orderSavedBillId = ' . $sellbillId . ' WHERE sellbillid = ' . $offerId;
        $sqlQuery = new SqlQuery($sql);

        return $this->executeUpdate($sqlQuery);
    }

}

?>