OLDataProvider.m 451 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "UIProgressView+AFNetworking.h"
  15. #import "AFHTTPSessionManager.h"
  16. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. @interface OLDataProvider ()
  21. @end
  22. @implementation OLDataProvider
  23. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  24. {
  25. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  26. NSString* ret=@"No Price.";
  27. sqlite3_stmt * statement;
  28. // int count=0;
  29. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  30. {
  31. if (sqlite3_step(statement) == SQLITE_ROW)
  32. {
  33. char *price = (char*)sqlite3_column_text(statement, 0);
  34. if(price==nil)
  35. price="";
  36. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  37. double dp= [nsprice doubleValue];
  38. ret=[NSString stringWithFormat:@"%.2f",dp];
  39. }
  40. sqlite3_finalize(statement);
  41. }
  42. return ret;
  43. // // [iSalesDB close_db:db];
  44. //
  45. // return nil;
  46. //// if(dprice==DBL_MAX)
  47. //// ret= nil;
  48. //// else
  49. //// ret= [NSNumber numberWithDouble:dprice];
  50. //// return ret;
  51. }
  52. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  53. {
  54. NSString* ret= nil;
  55. NSString *sqlQuery = nil;
  56. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  57. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  58. sqlite3_stmt * statement;
  59. // int count=0;
  60. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  61. {
  62. if (sqlite3_step(statement) == SQLITE_ROW)
  63. {
  64. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  65. if(imgurl==nil)
  66. imgurl="";
  67. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  68. ret=nsimgurl;
  69. }
  70. sqlite3_finalize(statement);
  71. }
  72. else
  73. {
  74. ret=@"";
  75. }
  76. // [iSalesDB close_db:db];
  77. // DebugLog(@"data string: %@",ret );
  78. return ret;
  79. }
  80. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  81. {
  82. NSMutableDictionary* values=params[@"replaceValue"];
  83. //生成portfolio pdf需要的数据
  84. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  85. data[@"npd_url"]=@"www.newpacificdirect.com";
  86. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  87. [formatter setDateFormat:@"MMMM yyyy"];
  88. NSString* date = [formatter stringFromDate:[NSDate date]];
  89. data[@"create_month"]=date;
  90. data[@"company_name"]=@"New Pacific Direct,Inc.";
  91. data[@"catalog_name"]=params[@"catalog_name"];
  92. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  93. data[@"TOTAL_PAGE"]=0;
  94. data[@"CURRENT_PAGE"]=0;
  95. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  96. bool show_stockout=[params[@"show_stockout"] boolValue];
  97. NSString *sql = [NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  98. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  99. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  100. sqlite3 *db = [iSalesDB get_db];
  101. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  102. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  103. int product_id = sqlite3_column_int(stmt, 0);
  104. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  105. NSString *name = [self textAtColumn:1 statement:stmt];
  106. NSString *description = [self textAtColumn:2 statement:stmt];
  107. double price = sqlite3_column_double(stmt, 3);
  108. NSString *s_price = [self textAtColumn:3 statement:stmt];
  109. double discount = sqlite3_column_double(stmt,4);
  110. int qty = sqlite3_column_int(stmt, 5);
  111. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  112. int is_percentage = sqlite3_column_int(stmt, 6);
  113. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  114. int item_id = sqlite3_column_int(stmt, 7);
  115. // int fashion_id = sqlite3_column_int(stmt, 8);
  116. NSString *line_note = [self textAtColumn:9 statement:stmt];
  117. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  118. double percent = sqlite3_column_double(stmt, 10);
  119. // int portfolio_id = sqlite3_column_int(stmt, 11);
  120. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  121. int availability = sqlite3_column_int(stmt, 12);
  122. NSString *color = [self textAtColumn:13 statement:stmt];
  123. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  124. NSString *demension = [self textAtColumn:15 statement:stmt];
  125. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  126. NSString *material = [self textAtColumn:17 statement:stmt];
  127. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  128. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  129. double volume = sqlite3_column_double(stmt, 19);
  130. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  131. double weight = sqlite3_column_double(stmt, 20);
  132. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  133. int model_set = sqlite3_column_int(stmt, 21);
  134. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  135. double load_ability = sqlite3_column_double(stmt, 22);
  136. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  137. NSString *assembling = [self textAtColumn:24 statement:stmt];
  138. NSString *made_in = [self textAtColumn:25 statement:stmt];
  139. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  140. if(is_percentage==0&&qty==0&&!show_stockout)
  141. {
  142. return;
  143. }
  144. if((is_percentage!=0||s_is_percentage.length==0)&&availability==0&&!show_stockout)
  145. {
  146. return;
  147. }
  148. if(is_percentage)
  149. {
  150. qty=availability;
  151. }
  152. else
  153. {
  154. percent=1;
  155. }
  156. qty=qty*percent+0.5;
  157. for(int i=0;i<[values[@"count"] intValue];i++)
  158. {
  159. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  160. if([valueitem[@"product_id"] intValue]==product_id)
  161. {
  162. line_note=valueitem[@"linenotes"];
  163. qty=[valueitem[@"available_qty"] intValue];
  164. s_qty=valueitem[@"available_qty"] ;
  165. if(valueitem[@"regular_price"]!=nil)
  166. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  167. s_price=valueitem[@"special_price"];
  168. price=[valueitem[@"special_price"] floatValue];
  169. }
  170. }
  171. NSString* set_price=@"";
  172. if([params[@"entered_price"] boolValue])
  173. {
  174. if (s_price==nil ) {
  175. set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  176. }
  177. else
  178. {
  179. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  180. }
  181. }
  182. NSString* get_price=@"";
  183. {
  184. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  185. // DebugLog(@"price time interval");
  186. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  187. if(gprice==nil)
  188. get_price=@"Price:No Price.";
  189. else
  190. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  191. }
  192. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  193. // if ([qty_null isEqualToString:@"null"]) {
  194. // qty=availability;
  195. // }
  196. //
  197. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  198. // if ([qty_percent_null isEqualToString:@"null"]) {
  199. // percentage=1;
  200. // }
  201. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  202. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  203. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  204. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  205. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  206. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  207. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  208. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  209. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  210. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  211. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  212. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  213. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  214. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  215. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  216. if([params[@"availability"] boolValue]==false || (s_qty.length==0 && s_availability.length==0))
  217. str_availability=@"";
  218. if([params[@"color"] boolValue]==false || color.length==0 )
  219. str_color=@"";
  220. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  221. str_model_set=@"";
  222. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  223. str_legcolor=@"";
  224. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  225. str_demension=@"";
  226. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  227. str_seat_height=@"";
  228. if([params[@"material"] boolValue]==false || material.length==0 )
  229. str_meterial=@"";
  230. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  231. str_box_dim=@"";
  232. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  233. str_volume=@"";
  234. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  235. str_weight=@"";
  236. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  237. str_load_ability=@"";
  238. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  239. str_fabric_content=@"";
  240. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  241. str_assembling=@"";
  242. if([params[@"made"] boolValue]==false || made_in.length==0 )
  243. str_made_in=@"";
  244. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  245. str_line_note=@"";
  246. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  247. [arr_detail addObject:str_color];
  248. [arr_detail addObject:str_model_set];
  249. [arr_detail addObject:str_legcolor];
  250. [arr_detail addObject:str_demension];
  251. [arr_detail addObject:str_seat_height];
  252. [arr_detail addObject:str_meterial];
  253. [arr_detail addObject:str_box_dim];
  254. [arr_detail addObject:str_volume];
  255. [arr_detail addObject:str_weight];
  256. [arr_detail addObject:str_load_ability];
  257. [arr_detail addObject:str_fabric_content];
  258. [arr_detail addObject:str_assembling];
  259. [arr_detail addObject:str_made_in];
  260. [arr_detail addObject:str_line_note];
  261. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  262. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  263. //model image;
  264. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  265. // qr image
  266. NSString* qrpath=nil;
  267. if([params[@"show_barcode"] boolValue])
  268. {
  269. NSString* temp = NSTemporaryDirectory();
  270. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  271. qrpath=[temp stringByAppendingPathComponent:filename];
  272. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  273. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  274. }
  275. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  276. item[@"img"]=img_path;
  277. item[@"img_qr"]=qrpath;
  278. item[@"special_price"]=set_price;
  279. item[@"price"]=get_price;
  280. item[@"name"]=name;
  281. item[@"description"]=description;
  282. item[@"detail"]=detail;
  283. item[@"img"]=img_path;
  284. // @{
  285. // //@"linenotes": line_note,
  286. //// @"product_id": product_id_string,
  287. //// @"available_qty": @(qty),
  288. //// @"available_percent" : @(percent),
  289. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  290. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  291. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  292. // @"": ,
  293. // @"": ,
  294. // @"": ,
  295. // @"": ,
  296. // @"": ,
  297. // @"": ,
  298. // @"":
  299. // }.mutableCopy;
  300. // if (percentage) {
  301. // [item removeObjectForKey:@"available_qty"];
  302. // } else {
  303. // [item removeObjectForKey:@"available_percent"];
  304. // }
  305. //
  306. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  307. // if ([qty_null isEqualToString:@"null"]) {
  308. // [item removeObjectForKey:@"available_qty"];
  309. // }
  310. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  311. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  312. }];
  313. [iSalesDB close_db:db];
  314. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  315. // [dic setValue:@"" forKey:@"email_content"];
  316. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  317. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  318. //
  319. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  320. // cell[@"count"]=[NSNumber numberWithInt:10];
  321. grid[@"cell0"]=dic;
  322. data[@"grid0"]=grid;
  323. return data;
  324. }
  325. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  326. {
  327. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  328. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  329. NSString *cache_folder=[paths objectAtIndex:0];
  330. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  331. BOOL bdir=NO;
  332. NSFileManager* fileManager = [NSFileManager defaultManager];
  333. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir])
  334. {
  335. NSError * error=nil;
  336. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  337. {
  338. return nil;
  339. }
  340. }
  341. NSData *data = [NSData dataWithContentsOfFile:template_path];
  342. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  343. return ret;
  344. }
  345. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  346. {
  347. NSString* template_file=nil;
  348. switch([params[@"pdf_style"] intValue])
  349. {
  350. case 0:
  351. template_file= @"portfolio_2x3.json";
  352. break;
  353. case 1:
  354. template_file= @"portfolio_3x2.json";
  355. break;
  356. default:
  357. template_file= @"portfolio_3x2.json";
  358. }
  359. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  360. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  361. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  362. DebugLog(@"%@",file);
  363. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  364. [dic setObject:@"Regular Mode" forKey:@"mode"];
  365. if (file) {
  366. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  367. [dic setObject:file forKey:@"pdf_path"];
  368. dic[@"isLocalFile"]=@"true";
  369. return [RAUtils dict2data:dic];
  370. }
  371. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  372. dic[@"isLocalFile"]=@"true";
  373. return [RAUtils dict2data:dic];
  374. }
  375. +(NSString*) get_offline_soid:(sqlite3*)db
  376. {
  377. NSString* soid=nil;
  378. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  379. [formatter setDateFormat:@"yyMMdd"];
  380. NSString* date = [formatter stringFromDate:[NSDate date]];
  381. for(int i=1;i<999;i++)
  382. {
  383. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  384. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  385. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  386. if(count==0)
  387. return soid;
  388. }
  389. return nil;
  390. }
  391. +(NSArray*) enumOfflineOrder
  392. {
  393. NSMutableArray* ret = [[NSMutableArray alloc] init];
  394. sqlite3 *db = [iSalesDB get_db];
  395. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  396. sqlite3_stmt * statement;
  397. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  398. if ( dbresult== SQLITE_OK)
  399. {
  400. while (sqlite3_step(statement) == SQLITE_ROW)
  401. {
  402. char *so_id = (char*)sqlite3_column_text(statement, 0);
  403. NSString* nsso_id=nil;
  404. if(so_id!=nil)
  405. {
  406. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  407. }
  408. [ret addObject:nsso_id];
  409. }
  410. sqlite3_finalize(statement);
  411. }
  412. [iSalesDB close_db:db];
  413. return ret;
  414. }
  415. +(void) uploadFile:(NSString*) file
  416. {
  417. NSData* data = [NSData dataWithContentsOfFile: file];
  418. UIApplication * app = [UIApplication sharedApplication];
  419. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  420. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  421. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  422. if(appDelegate.user!=nil)
  423. [params setValue:appDelegate.user forKey:@"user"];
  424. // if(appDelegate.contact_id!=nil)
  425. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  426. if(appDelegate.password!=nil)
  427. [params setValue:appDelegate.password forKey:@"password"];
  428. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  429. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  430. } error:nil];
  431. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  432. NSProgress *progress = nil;
  433. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  434. if (error) {
  435. NSString* err_msg = [error localizedDescription];
  436. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  437. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  438. NSLog(@"data string: %@",str);
  439. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  440. } else {
  441. NSLog(@"response ");
  442. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  443. // 再将NSData转为字符串
  444. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  445. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  446. NSLog(@"data string: %@",jsonStr);
  447. NSDictionary* json = responseObject;
  448. if([[json valueForKey:@"result"] intValue]==2)
  449. {
  450. // NSString* img_url_down = json[@"img_url_aname"];
  451. // NSString* img_url_up = json[@"img_url"];
  452. }
  453. else
  454. {
  455. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  456. }
  457. }
  458. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  459. }];
  460. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  461. //
  462. [uploadTask resume];
  463. }
  464. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  465. {
  466. NSString* serial= [[NSUUID UUID] UUIDString];
  467. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  468. NSString *cachefolder = [paths objectAtIndex:0];
  469. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  470. upfolder = [upfolder stringByAppendingPathComponent:serial];
  471. NSFileManager* fileManager = [NSFileManager defaultManager];
  472. BOOL bdir=YES;
  473. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  474. {
  475. NSError *error = nil;
  476. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  477. if(!bsuccess)
  478. DebugLog(@"Create UPLOAD folder failed");
  479. }
  480. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  481. ret[@"contact"]=[self prepareContact:serial];
  482. ret[@"wishlist"]=[self prepareWishlist:serial];
  483. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  484. ret[@"portfolio"] = [self preparePortfolio:serial];
  485. ret[@"view_portfolio"] = [self preparePDF:serial];
  486. NSString* str= [RAUtils dict2string:ret];
  487. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  488. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  489. NSError *error=nil;
  490. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  491. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  492. ZipArchive* zip = [[ZipArchive alloc] init];
  493. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  494. for(int i=0;i<arr_files.count;i++)
  495. {
  496. NSString* file=arr_files[i];
  497. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  498. }
  499. if( ![zip CloseZipFile2] )
  500. {
  501. zippath = @"";
  502. }
  503. ret[@"file"]=zippath;
  504. return ret;
  505. }
  506. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  507. {
  508. if(filename.length==0)
  509. return false;
  510. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  511. bool ret=false;
  512. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  513. NSString *cachefolder = [paths objectAtIndex:0];
  514. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  515. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  516. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  517. upfolder = [upfolder stringByAppendingPathComponent:serial];
  518. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  519. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  520. NSFileManager* fileManager = [NSFileManager defaultManager];
  521. BOOL bdir=NO;
  522. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  523. {
  524. NSError *error = nil;
  525. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  526. {
  527. ret=false;
  528. }
  529. else
  530. {
  531. ret=true;
  532. }
  533. }
  534. return ret;
  535. }
  536. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  537. {
  538. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  539. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  540. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  541. NSString* where=@" 1=1";
  542. if (ver!=nil) {
  543. where=@"is_dirty=1";
  544. }
  545. sqlite3 *db = [iSalesDB get_db];
  546. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  547. sqlite3_stmt * statement;
  548. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  549. int count=0;
  550. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  551. if ( dbresult== SQLITE_OK)
  552. {
  553. while (sqlite3_step(statement) == SQLITE_ROW)
  554. {
  555. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  556. int _id = sqlite3_column_int(statement, 0);
  557. int product_id = sqlite3_column_int(statement, 1);
  558. int item_id = sqlite3_column_int(statement, 2);
  559. int qty = sqlite3_column_int(statement, 3);
  560. char *create_time = (char*)sqlite3_column_text(statement, 4);
  561. NSString* nscreate_time=nil;
  562. if(create_time!=nil)
  563. {
  564. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  565. }
  566. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  567. NSString* nsmodify_time=nil;
  568. if(modify_time!=nil)
  569. {
  570. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  571. }
  572. int is_delete = sqlite3_column_int(statement, 6);
  573. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  574. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  575. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  576. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  577. itemjson[@"create_time"]=nscreate_time;
  578. itemjson[@"modify_time"]=nsmodify_time;
  579. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  580. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  581. count++;
  582. }
  583. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  584. sqlite3_finalize(statement);
  585. }
  586. ret[@"count"]=[NSNumber numberWithInt:count ];
  587. [iSalesDB close_db:db];
  588. return ret;
  589. }
  590. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  591. {
  592. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  593. UIApplication * app = [UIApplication sharedApplication];
  594. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  595. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",orderCode ];
  596. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  597. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  598. DebugLog(@"offline_login sql:%@",sqlQuery);
  599. sqlite3_stmt * statement;
  600. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  601. int count=0;
  602. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  603. if ( dbresult== SQLITE_OK)
  604. {
  605. while (sqlite3_step(statement) == SQLITE_ROW)
  606. {
  607. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  608. int product_id = sqlite3_column_int(statement, 0);
  609. char *str_price = (char*)sqlite3_column_text(statement, 1);
  610. int item_id = sqlite3_column_int(statement, 7);
  611. NSString* Price=nil;
  612. if(str_price==nil)
  613. {
  614. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  615. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  616. if(price==nil)
  617. Price=@"No Price.";
  618. else
  619. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  620. }
  621. else
  622. {
  623. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  624. }
  625. double discount = sqlite3_column_double(statement, 2);
  626. int item_count = sqlite3_column_int(statement, 3);
  627. char *line_note = (char*)sqlite3_column_text(statement, 4);
  628. NSString *nsline_note=nil;
  629. if(line_note!=nil)
  630. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  631. char *name = (char*)sqlite3_column_text(statement, 5);
  632. NSString *nsname=nil;
  633. if(name!=nil)
  634. nsname= [[NSString alloc]initWithUTF8String:name];
  635. char *description = (char*)sqlite3_column_text(statement, 6);
  636. NSString *nsdescription=nil;
  637. if(description!=nil)
  638. nsdescription= [[NSString alloc]initWithUTF8String:description];
  639. // int stockUom = sqlite3_column_int(statement, 8);
  640. // int _id = sqlite3_column_int(statement, 9);
  641. //
  642. //
  643. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  644. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  645. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  646. // int carton=[bsubtotaljson[@"carton"] intValue];
  647. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  648. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  649. itemjson[@"model"]=nsname;
  650. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  651. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  652. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  653. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  654. // itemjson[@"check"]=@"true";
  655. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  656. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  657. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  658. itemjson[@"unit_price"]=Price;
  659. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  660. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  661. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  662. itemjson[@"note"]=nsline_note;
  663. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  664. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  665. count++;
  666. }
  667. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  668. sqlite3_finalize(statement);
  669. }
  670. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  671. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  672. return ret;
  673. }
  674. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  675. {
  676. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  677. sqlite3 *db = [iSalesDB get_db];
  678. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  679. // for(int i=0;i<arr_soid.count;i++)
  680. sqlite3_stmt * statement;
  681. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  682. //int count=0;
  683. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  684. if ( dbresult== SQLITE_OK)
  685. {
  686. int count=0;
  687. while (sqlite3_step(statement) == SQLITE_ROW)
  688. {
  689. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  690. int _id = sqlite3_column_int(statement, 0);
  691. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  692. NSString* nssync_data=nil;
  693. if(sync_data!=nil)
  694. {
  695. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  696. }
  697. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  698. NSString* nsimg_1=nil;
  699. if(img_1!=nil)
  700. {
  701. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  702. [self copy_upImg:serial file:nsimg_1];
  703. }
  704. char *so_no = (char*)sqlite3_column_text(statement, 3);
  705. NSString* nsso_no=nil;
  706. if(so_no!=nil)
  707. {
  708. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  709. }
  710. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  711. itemjson[@"order_type"]=@"submit order";
  712. else
  713. itemjson[@"order_type"]=@"archive order";
  714. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  715. itemjson[@"json_data"]= nssync_data;
  716. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  717. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  718. count++;
  719. }
  720. ret[@"count"]=[NSNumber numberWithInt:count ];
  721. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  722. }
  723. sqlite3_finalize(statement);
  724. [iSalesDB close_db:db];
  725. return ret;
  726. }
  727. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  728. {
  729. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  730. sqlite3 *db = [iSalesDB get_db];
  731. // UIApplication * app = [UIApplication sharedApplication];
  732. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  733. NSString *sqlQuery =@"select _id,img_0,img_1,img_2,sync_data from offline_contact where is_active=1 and not sync_data is null";
  734. sqlite3_stmt * statement;
  735. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  736. int count=0;
  737. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  738. if ( dbresult== SQLITE_OK)
  739. {
  740. while (sqlite3_step(statement) == SQLITE_ROW)
  741. {
  742. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  743. int _id = sqlite3_column_int(statement, 0);
  744. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  745. NSString* nsimg_0=nil;
  746. if(img_0!=nil)
  747. {
  748. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  749. [self copy_upImg:serial file:nsimg_0];
  750. }
  751. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  752. NSString* nsimg_1=nil;
  753. if(img_1!=nil)
  754. {
  755. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  756. [self copy_upImg:serial file:nsimg_1];
  757. }
  758. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  759. NSString* nsimg_2=nil;
  760. if(img_2!=nil)
  761. {
  762. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  763. [self copy_upImg:serial file:nsimg_2];
  764. }
  765. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  766. NSString* nssync_data=nil;
  767. if(sync_data!=nil)
  768. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  769. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  770. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  771. itemjson[@"json_data"]= nssync_data;
  772. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  773. count++;
  774. }
  775. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  776. sqlite3_finalize(statement);
  777. }
  778. ret[@"count"]=[NSNumber numberWithInt:count ];
  779. [iSalesDB close_db:db];
  780. return ret;
  781. }
  782. +(bool) check_offlinedata
  783. {
  784. UIApplication * app = [UIApplication sharedApplication];
  785. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  786. sqlite3 *db = [iSalesDB get_db];
  787. NSString * where=@"1=1";
  788. // if(appDelegate.user!=nil)
  789. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  790. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  791. [iSalesDB close_db:db];
  792. if(count==0)
  793. {
  794. return false;
  795. }
  796. return true;
  797. //
  798. //[iSalesDB close_db:db];
  799. }
  800. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  801. {
  802. UIApplication * app = [UIApplication sharedApplication];
  803. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  804. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  805. sqlite3 *db = [iSalesDB get_db];
  806. NSString* collectId=params[@"collectId"];
  807. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  808. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  809. [iSalesDB execSql:sqlQuery db:db];
  810. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  811. [iSalesDB close_db:db];
  812. appDelegate.wish_count =count;
  813. [appDelegate update_count_mark];
  814. ret[@"result"]= [NSNumber numberWithInt:2];
  815. return ret;
  816. }
  817. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  818. //{
  819. //
  820. // UIApplication * app = [UIApplication sharedApplication];
  821. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  822. //
  823. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  824. // sqlite3 *db = [iSalesDB get_db];
  825. // NSString* product_id=params[@"product_id"];
  826. //
  827. //
  828. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  829. //
  830. //
  831. // for(int i=0;i<arr.count;i++)
  832. // {
  833. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  834. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  835. // if(count==0)
  836. // {
  837. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  838. // [iSalesDB execSql:sqlQuery db:db];
  839. // }
  840. // }
  841. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  842. // [iSalesDB close_db:db];
  843. //
  844. // appDelegate.wish_count =count;
  845. //
  846. // [appDelegate update_count_mark];
  847. // ret[@"result"]= [NSNumber numberWithInt:2];
  848. // return ret;
  849. // //
  850. // //return ret;
  851. //}
  852. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  853. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  854. DebugLog(@"time interval: %lf",interval);
  855. }
  856. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  857. {
  858. UIApplication * app = [UIApplication sharedApplication];
  859. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  860. int sort = [[params objectForKey:@"sort"] intValue];
  861. NSString *sort_str = @"";
  862. switch (sort) {
  863. case 0:{
  864. sort_str = @"order by w.modify_time desc";
  865. }
  866. break;
  867. case 1:{
  868. sort_str = @"order by w.modify_time asc";
  869. }
  870. break;
  871. case 2:{
  872. sort_str = @"order by m.name asc";
  873. }
  874. break;
  875. case 3:{
  876. sort_str = @"order by m.name desc";
  877. }
  878. break;
  879. case 4:{
  880. sort_str = @"order by m.description asc";
  881. }
  882. break;
  883. default:
  884. break;
  885. }
  886. NSString* user = appDelegate.user;
  887. sqlite3 *db = [iSalesDB get_db];
  888. // order by w.create_time
  889. NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id where w.is_delete is NULL or w.is_delete != 1 %@ ",sort_str];
  890. // NSString* sqlQuery = [NSString stringWithFormat:@"select m.product_id,(m.name||'\n'||m.description) as des,w._id from product as m,wishlist as w where m.product_id = w.product_id %@",sort_str];
  891. sqlite3_stmt * statement;
  892. NSDate *date1 = [NSDate date];
  893. NSDate *date2 = nil;
  894. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  895. int count=0;
  896. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  897. {
  898. while (sqlite3_step(statement) == SQLITE_ROW)
  899. {
  900. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  901. int product_id = sqlite3_column_double(statement, 0);
  902. char *description = (char*)sqlite3_column_text(statement, 1);
  903. if(description==nil)
  904. description= "";
  905. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  906. int item_id = sqlite3_column_double(statement, 2);
  907. NSDate *date_image = [NSDate date];
  908. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  909. printf("image : ");
  910. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  911. // char *url = (char*)sqlite3_column_text(statement, 3);
  912. // if(url==nil)
  913. // url="";
  914. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  915. int qty = sqlite3_column_int(statement, 3);
  916. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  917. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  918. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  919. item[@"description"]= nsdescription;
  920. item[@"img"]= nsurl;
  921. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  922. count++;
  923. }
  924. printf("total time:");
  925. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  926. ret[@"count"]= [NSNumber numberWithInt:count];
  927. ret[@"total_count"]= [NSNumber numberWithInt:count];
  928. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  929. ret[@"result"]= [NSNumber numberWithInt:2];
  930. appDelegate.wish_count =count;
  931. [appDelegate update_count_mark];
  932. sqlite3_finalize(statement);
  933. }
  934. [iSalesDB close_db:db];
  935. return ret;
  936. }
  937. +(NSDictionary*) offline_notimpl
  938. {
  939. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  940. ret[@"result"]=@"8";
  941. ret[@"err_msg"]=@"offline mode does not support this function.";
  942. return ret;
  943. }
  944. +(NSDictionary*) offline_home
  945. {
  946. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  947. NSString *cachefolder = [paths objectAtIndex:0];
  948. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  949. NSData* json =nil;
  950. json=[NSData dataWithContentsOfFile:img_cache];
  951. NSError *error=nil;
  952. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  953. return menu;
  954. }
  955. +(NSDictionary*) offline_category_menu
  956. {
  957. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  958. NSString *cachefolder = [paths objectAtIndex:0];
  959. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  960. NSData* json =nil;
  961. json=[NSData dataWithContentsOfFile:img_cache];
  962. NSError *error=nil;
  963. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  964. return menu;
  965. }
  966. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  967. {
  968. NSString* offline_command=params[@"offline_Command"];
  969. NSDictionary* ret=nil;
  970. if([offline_command isEqualToString:@"model_NIYMAL"])
  971. {
  972. NSString* category = params[@"category"];
  973. ret = [self refresh_model_NIYMAL:category];
  974. }
  975. return ret;
  976. }
  977. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  978. {
  979. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  980. [ret setValue:@"2" forKey:@"result"];
  981. [ret setValue:@"Regular Mode" forKey:@"mode"];
  982. sqlite3* db= [iSalesDB get_db];
  983. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  984. [iSalesDB close_db:db];
  985. [ret setObject:detail1_section forKey:@"detail_1"];
  986. return ret;
  987. }
  988. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  989. {
  990. UIApplication * app = [UIApplication sharedApplication];
  991. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  992. // NSArray* arr1 = [self get_user_all_price_type];
  993. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  994. // NSSet *set1 = [NSSet setWithArray:arr1];
  995. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  996. // if(appDelegate.contact_id==nil)
  997. // set2=[set1 mutableCopy];
  998. // else
  999. // [set2 intersectsSet:set1];
  1000. // NSArray *retarr = [set2 allObjects];
  1001. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1002. // sqlite3 *db = [iSalesDB get_db];
  1003. NSString* sqlQuery = nil;
  1004. if(appDelegate.contact_id==nil)
  1005. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d order by a.order_by;",item_id];
  1006. else
  1007. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  1008. sqlite3_stmt * statement;
  1009. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1010. int count=0;
  1011. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1012. {
  1013. while (sqlite3_step(statement) == SQLITE_ROW)
  1014. {
  1015. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1016. char *name = (char*)sqlite3_column_text(statement, 0);
  1017. if(name==nil)
  1018. name="";
  1019. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1020. // double price = sqlite3_column_double(statement, 1);
  1021. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1022. // if(isnull==nil)
  1023. // item[nsname]= @"No Price";
  1024. // else
  1025. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  1026. char *price = (char*)sqlite3_column_text(statement, 1);
  1027. if(price!=nil)
  1028. {
  1029. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1030. nsprice=[AESCrypt fastdecrypt:nsprice];
  1031. if(nsprice.length>0)
  1032. {
  1033. double dp= [nsprice doubleValue];
  1034. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  1035. }
  1036. }
  1037. else
  1038. {
  1039. item[nsname]= @"No Price";
  1040. }
  1041. // int type= sqlite3_column_int(statement, 2);
  1042. // item[@"type"]=[NSNumber numberWithInt:type ];
  1043. // item[nsname]= nsprice;
  1044. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1045. count++;
  1046. }
  1047. ret[@"count"]= [NSNumber numberWithInt:count];
  1048. sqlite3_finalize(statement);
  1049. }
  1050. // [iSalesDB close_db:db];
  1051. return ret;
  1052. }
  1053. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  1054. //{
  1055. // NSArray* arr1 = [self get_user_all_price_type:db];
  1056. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1057. //
  1058. // // NSSet *set1 = [NSSet setWithArray:arr1];
  1059. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1060. // // [set2 intersectsSet:set1];
  1061. // //
  1062. // //
  1063. // // NSArray *retarr = [set2 allObjects];
  1064. //
  1065. // NSString* whereprice=nil;
  1066. // if(contact_id==nil)
  1067. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1068. // else
  1069. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1070. //
  1071. //
  1072. // // sqlite3 *db = [iSalesDB get_db];
  1073. //
  1074. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  1075. // sqlite3_stmt * statement;
  1076. //
  1077. //
  1078. // NSNumber* ret = nil;
  1079. // double dprice=DBL_MAX;
  1080. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1081. // {
  1082. //
  1083. //
  1084. // while (sqlite3_step(statement) == SQLITE_ROW)
  1085. // {
  1086. //
  1087. // // double val = sqlite3_column_double(statement, 0);
  1088. // char *price = (char*)sqlite3_column_text(statement, 0);
  1089. // if(price!=nil)
  1090. // {
  1091. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1092. // nsprice=[AESCrypt fastdecrypt:nsprice];
  1093. // if(nsprice.length>0)
  1094. // {
  1095. // double dp= [nsprice doubleValue];
  1096. // if(dp<dprice)
  1097. // dprice=dp;
  1098. // }
  1099. // }
  1100. // }
  1101. //
  1102. //
  1103. //
  1104. //
  1105. // sqlite3_finalize(statement);
  1106. //
  1107. //
  1108. //
  1109. //
  1110. // }
  1111. //
  1112. // // [iSalesDB close_db:db];
  1113. //
  1114. // if(dprice==DBL_MAX)
  1115. // ret= nil;
  1116. // else
  1117. // ret= [NSNumber numberWithDouble:dprice];
  1118. // return ret;
  1119. //}
  1120. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  1121. {
  1122. NSArray* arr1 = [self get_user_all_price_type:db];
  1123. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1124. NSString* whereprice=nil;
  1125. if(contact_id==nil)
  1126. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  1127. else
  1128. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1129. // sqlite3 *db = [iSalesDB get_db];
  1130. NSString *productIdCondition = @"1 = 1";
  1131. if (product_id) {
  1132. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  1133. }
  1134. NSString *itemIdCondition = @"";
  1135. if (item_id) {
  1136. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  1137. }
  1138. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  1139. sqlite3_stmt * statement;
  1140. NSNumber* ret = nil;
  1141. double dprice=DBL_MAX;
  1142. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1143. {
  1144. while (sqlite3_step(statement) == SQLITE_ROW)
  1145. {
  1146. // double val = sqlite3_column_double(statement, 0);
  1147. char *price = (char*)sqlite3_column_text(statement, 0);
  1148. if(price!=nil)
  1149. {
  1150. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  1151. nsprice=[AESCrypt fastdecrypt:nsprice];
  1152. if(nsprice.length>0)
  1153. {
  1154. double dp= [nsprice doubleValue];
  1155. if(dp<dprice)
  1156. dprice=dp;
  1157. }
  1158. }
  1159. }
  1160. sqlite3_finalize(statement);
  1161. }
  1162. // [iSalesDB close_db:db];
  1163. if(dprice==DBL_MAX)
  1164. ret= nil;
  1165. else
  1166. ret= [NSNumber numberWithDouble:dprice];
  1167. return ret;
  1168. }
  1169. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  1170. {
  1171. NSArray* ret=nil;
  1172. // sqlite3 *db = [iSalesDB get_db];
  1173. // no customer assigned , use login user contact_id
  1174. UIApplication * app = [UIApplication sharedApplication];
  1175. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1176. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  1177. sqlite3_stmt * statement;
  1178. // int count=0;
  1179. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1180. {
  1181. if (sqlite3_step(statement) == SQLITE_ROW)
  1182. {
  1183. char *val = (char*)sqlite3_column_text(statement, 0);
  1184. if(val==nil)
  1185. val="";
  1186. NSString* price = [[NSString alloc]initWithUTF8String:val];
  1187. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  1188. }
  1189. sqlite3_finalize(statement);
  1190. }
  1191. // [iSalesDB close_db:db];
  1192. return ret;
  1193. }
  1194. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  1195. {
  1196. // sqlite3 *db = [iSalesDB get_db];
  1197. if(contact_id==nil)
  1198. {
  1199. // no customer assigned , use login user contact_id
  1200. UIApplication * app = [UIApplication sharedApplication];
  1201. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1202. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  1203. sqlite3_stmt * statement;
  1204. // int count=0;
  1205. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1206. {
  1207. if (sqlite3_step(statement) == SQLITE_ROW)
  1208. {
  1209. char *val = (char*)sqlite3_column_text(statement, 0);
  1210. if(val==nil)
  1211. val="";
  1212. contact_id = [[NSString alloc]initWithUTF8String:val];
  1213. }
  1214. sqlite3_finalize(statement);
  1215. }
  1216. if(contact_id.length<=0)
  1217. {
  1218. // [iSalesDB close_db:db];
  1219. return nil;
  1220. }
  1221. }
  1222. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  1223. sqlite3_stmt * statement;
  1224. NSArray* ret=nil;
  1225. // int count=0;
  1226. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1227. {
  1228. if (sqlite3_step(statement) == SQLITE_ROW)
  1229. {
  1230. char *price_type = (char*)sqlite3_column_text(statement, 0);
  1231. if(price_type==nil)
  1232. price_type="";
  1233. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  1234. if(nsprice_type.length>0)
  1235. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  1236. else
  1237. ret=nil;
  1238. }
  1239. sqlite3_finalize(statement);
  1240. }
  1241. // [iSalesDB close_db:db];
  1242. return ret;
  1243. }
  1244. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  1245. {
  1246. NSString* ret= nil;
  1247. // sqlite3 *db = [iSalesDB get_db];
  1248. NSString *sqlQuery = nil;
  1249. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  1250. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  1251. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  1252. if(product_id==nil && model_name)
  1253. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where product_id in (select product_id from product where name='%@') order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  1254. else if (product_id)
  1255. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where i.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  1256. sqlite3_stmt * statement;
  1257. // int count=0;
  1258. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1259. {
  1260. if (sqlite3_step(statement) == SQLITE_ROW)
  1261. {
  1262. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  1263. if(imgurl==nil)
  1264. imgurl="";
  1265. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  1266. ret=nsimgurl;
  1267. }
  1268. sqlite3_finalize(statement);
  1269. }
  1270. else
  1271. {
  1272. [ret setValue:@"8" forKey:@"result"];
  1273. }
  1274. // [iSalesDB close_db:db];
  1275. DebugLog(@"data string: %@",ret );
  1276. return ret;
  1277. }
  1278. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  1279. {
  1280. NSString* ret= nil;
  1281. sqlite3 *db = [iSalesDB get_db];
  1282. NSString *sqlQuery = nil;
  1283. if(product_id==nil)
  1284. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  1285. else
  1286. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  1287. sqlite3_stmt * statement;
  1288. // int count=0;
  1289. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1290. {
  1291. if (sqlite3_step(statement) == SQLITE_ROW)
  1292. {
  1293. char *default_category = (char*)sqlite3_column_text(statement, 0);
  1294. if(default_category==nil)
  1295. default_category="";
  1296. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1297. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  1298. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  1299. ret=nsdefault_category;
  1300. }
  1301. sqlite3_finalize(statement);
  1302. }
  1303. else
  1304. {
  1305. [ret setValue:@"8" forKey:@"result"];
  1306. }
  1307. [iSalesDB close_db:db];
  1308. DebugLog(@"data string: %@",ret );
  1309. return ret;
  1310. }
  1311. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  1312. {
  1313. NSString* model_name = [params valueForKey:@"product_name"];
  1314. NSString* product_id = [params valueForKey:@"product_id"];
  1315. NSString* category = [params valueForKey:@"category"];
  1316. if(category==nil)
  1317. category = [self model_default_category:product_id model_name:model_name];
  1318. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1319. sqlite3 *db = [iSalesDB get_db];
  1320. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  1321. NSString *sqlQuery = nil;
  1322. if(product_id==nil)
  1323. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  1324. else
  1325. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,ETA,item_id from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  1326. sqlite3_stmt * statement;
  1327. [ret setValue:@"2" forKey:@"result"];
  1328. [ret setValue:@"3" forKey:@"detail_section_count"];
  1329. // int count=0;
  1330. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1331. {
  1332. if (sqlite3_step(statement) == SQLITE_ROW)
  1333. {
  1334. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1335. char *name = (char*)sqlite3_column_text(statement, 0);
  1336. if(name==nil)
  1337. name="";
  1338. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1339. char *description = (char*)sqlite3_column_text(statement, 1);
  1340. if(description==nil)
  1341. description="";
  1342. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1343. int product_id = sqlite3_column_int(statement, 2);
  1344. char *color = (char*)sqlite3_column_text(statement, 3);
  1345. if(color==nil)
  1346. color="";
  1347. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  1348. //
  1349. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  1350. // if(legcolor==nil)
  1351. // legcolor="";
  1352. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  1353. //
  1354. //
  1355. int availability = sqlite3_column_int(statement, 5);
  1356. //
  1357. int incoming_stock = sqlite3_column_int(statement, 6);
  1358. char *demension = (char*)sqlite3_column_text(statement, 7);
  1359. if(demension==nil)
  1360. demension="";
  1361. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  1362. // ,,,,,,,,,
  1363. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  1364. if(seat_height==nil)
  1365. seat_height="";
  1366. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  1367. char *material = (char*)sqlite3_column_text(statement, 9);
  1368. if(material==nil)
  1369. material="";
  1370. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  1371. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  1372. if(box_dim==nil)
  1373. box_dim="";
  1374. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  1375. char *volume = (char*)sqlite3_column_text(statement, 11);
  1376. if(volume==nil)
  1377. volume="";
  1378. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  1379. double weight = sqlite3_column_double(statement, 12);
  1380. char *model_set = (char*)sqlite3_column_text(statement, 13);
  1381. if(model_set==nil)
  1382. model_set="";
  1383. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  1384. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  1385. if(load_ability==nil)
  1386. load_ability="";
  1387. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  1388. char *default_category = (char*)sqlite3_column_text(statement, 15);
  1389. if(default_category==nil)
  1390. default_category="";
  1391. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1392. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1393. if(fabric_content==nil)
  1394. fabric_content="";
  1395. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1396. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1397. if(assembling==nil)
  1398. assembling="";
  1399. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1400. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1401. if(made_in==nil)
  1402. made_in="";
  1403. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1404. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1405. if(special_remarks==nil)
  1406. special_remarks="";
  1407. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1408. int stockUcom = sqlite3_column_double(statement, 20);
  1409. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1410. if(product_group==nil)
  1411. product_group="";
  1412. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1413. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1414. // if(fashion_selector==nil)
  1415. // fashion_selector="";
  1416. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1417. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1418. if(selector_field==nil)
  1419. selector_field="";
  1420. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1421. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1422. if(property_field==nil)
  1423. property_field="";
  1424. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1425. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1426. if(packaging==nil)
  1427. packaging="";
  1428. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1429. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1430. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1431. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  1432. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1433. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1434. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1435. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1436. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1437. NSString* Availability=nil;
  1438. if(availability>0)
  1439. Availability=[NSString stringWithFormat:@"%d",availability];
  1440. else
  1441. Availability = @"In Production";
  1442. [img_section setValue:Availability forKey:@"Availability"];
  1443. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1444. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1445. char *eta = (char*)sqlite3_column_text(statement, 25);
  1446. if(eta==nil)
  1447. eta="";
  1448. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  1449. [img_section setValue:nseta forKey:@"ETA"];
  1450. int item_id = sqlite3_column_int(statement, 26);
  1451. NSString* Price=nil;
  1452. if(appDelegate.bLogin==false)
  1453. Price=@"Must Sign in.";
  1454. else
  1455. {
  1456. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1457. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1458. if(price==nil)
  1459. Price=@"No Price.";
  1460. else
  1461. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1462. }
  1463. [img_section setValue:Price forKey:@"price"];
  1464. [img_section setValue:nsname forKey:@"model_name"];
  1465. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1466. [ret setObject:img_section forKey:@"img_section"];
  1467. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1468. int detail0_item_count=0;
  1469. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1470. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1471. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1472. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1473. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1474. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1475. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1476. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1477. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1478. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1479. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1480. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1481. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1482. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1483. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1484. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1485. {
  1486. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1487. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1488. }
  1489. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1490. [detail0_section setValue:@"kv" forKey:@"type"];
  1491. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1492. [ret setObject:detail0_section forKey:@"detail_0"];
  1493. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1494. // [detail1_section setValue:@"detail" forKey:@"target"];
  1495. // [detail1_section setValue:@"popup" forKey:@"action"];
  1496. // [detail1_section setValue:@"content" forKey:@"type"];
  1497. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1498. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1499. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1500. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1501. [ret setObject:detail1_section forKey:@"detail_1"];
  1502. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1503. [detail2_section setValue:@"detail" forKey:@"target"];
  1504. [detail2_section setValue:@"popup" forKey:@"action"];
  1505. [detail2_section setValue:@"content" forKey:@"type"];
  1506. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1507. [detail2_section setValue:@"true" forKey:@"single_row"];
  1508. [detail2_section setValue:@"local" forKey:@"data"];
  1509. [ret setObject:detail2_section forKey:@"detail_2"];
  1510. }
  1511. sqlite3_finalize(statement);
  1512. }
  1513. else
  1514. {
  1515. [ret setValue:@"8" forKey:@"result"];
  1516. }
  1517. NSLog(@"count:%d",count);
  1518. [iSalesDB close_db:db];
  1519. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1520. return ret;
  1521. }
  1522. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1523. NSString* orderCode = [params valueForKey:@"orderCode"];
  1524. NSString* keyword = [params valueForKey:@"keyword"];
  1525. keyword=keyword.lowercaseString;
  1526. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1527. int limit = [[params valueForKey:@"limit"] intValue];
  1528. int offset = [[params valueForKey:@"offset"] intValue];
  1529. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1530. NSString *limit_str = @"";
  1531. if (limited) {
  1532. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1533. }
  1534. sqlite3 *db = [iSalesDB get_db];
  1535. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1536. UIApplication * app = [UIApplication sharedApplication];
  1537. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1538. NSString *sqlQuery = nil;
  1539. if(exactMatch )
  1540. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",appDelegate.order_code, keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  1541. else
  1542. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  1543. DebugLog(@"offline_search sql:%@",sqlQuery);
  1544. sqlite3_stmt * statement;
  1545. [ret setValue:@"2" forKey:@"result"];
  1546. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1547. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1548. // int count=0;
  1549. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1550. {
  1551. int i=0;
  1552. while (sqlite3_step(statement) == SQLITE_ROW)
  1553. {
  1554. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1555. // char *name = (char*)sqlite3_column_text(statement, 1);
  1556. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1557. char *name = (char*)sqlite3_column_text(statement, 0);
  1558. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1559. char *description = (char*)sqlite3_column_text(statement, 1);
  1560. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1561. int product_id = sqlite3_column_int(statement, 2);
  1562. // char *url = (char*)sqlite3_column_text(statement, 3);
  1563. // if(url==nil)
  1564. // url="";
  1565. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1566. int wid = sqlite3_column_int(statement, 3);
  1567. int closeout = sqlite3_column_int(statement, 4);
  1568. int cid = sqlite3_column_int(statement, 5);
  1569. int wisdelete = sqlite3_column_int(statement, 6);
  1570. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  1571. if(wid !=0 && wisdelete != 1)
  1572. [item setValue:@"true" forKey:@"wish_exists"];
  1573. else
  1574. [item setValue:@"false" forKey:@"wish_exists"];
  1575. if(closeout==0)
  1576. [item setValue:@"false" forKey:@"is_closeout"];
  1577. else
  1578. [item setValue:@"true" forKey:@"is_closeout"];
  1579. if(cid==0)
  1580. [item setValue:@"false" forKey:@"cart_exists"];
  1581. else
  1582. [item setValue:@"true" forKey:@"cart_exists"];
  1583. [item addEntriesFromDictionary:imgjson];
  1584. // [item setValue:nsurl forKey:@"img"];
  1585. [item setValue:nsname forKey:@"fash_name"];
  1586. [item setValue:nsdescription forKey:@"description"];
  1587. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1588. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1589. i++;
  1590. }
  1591. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1592. [ret setObject:items forKey:@"items"];
  1593. sqlite3_finalize(statement);
  1594. }
  1595. NSLog(@"count:%d",count);
  1596. [iSalesDB close_db:db];
  1597. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1598. return ret;
  1599. }
  1600. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  1601. {
  1602. return [self search:params limited:YES];
  1603. }
  1604. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  1605. {
  1606. UIApplication * app = [UIApplication sharedApplication];
  1607. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1608. appDelegate.disable_trigger=true;
  1609. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1610. appDelegate.disable_trigger=false;
  1611. //
  1612. // NSString* user = [params valueForKey:@"user"];
  1613. //
  1614. // NSString* password = [params valueForKey:@"password"];
  1615. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1616. ret[@"result"]=[NSNumber numberWithInt:2 ];
  1617. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  1618. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  1619. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  1620. [appDelegate update_count_mark];
  1621. appDelegate.can_show_price =false;
  1622. appDelegate.can_see_price =false;
  1623. appDelegate.can_create_portfolio =false;
  1624. appDelegate.can_create_order =false;
  1625. appDelegate.can_cancel_order =false;
  1626. appDelegate.can_set_cart_price =false;
  1627. appDelegate.can_delete_order =false;
  1628. appDelegate.can_submit_order =false;
  1629. appDelegate.can_set_tearsheet_price =false;
  1630. appDelegate.can_update_contact_info = false;
  1631. appDelegate.save_order_logout = false;
  1632. appDelegate.submit_order_logout = false;
  1633. appDelegate.alert_sold_in_quantities = false;
  1634. appDelegate.ipad_perm =nil ;
  1635. appDelegate.user_type = USER_ROLE_UNKNOWN;
  1636. appDelegate.OrderFilter= nil;
  1637. [appDelegate SetSo:nil];
  1638. [appDelegate set_main_button_panel];
  1639. // sqlite3 *db = [iSalesDB get_db];
  1640. //
  1641. //
  1642. //
  1643. //
  1644. //
  1645. // NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  1646. //
  1647. //
  1648. //
  1649. //
  1650. // DebugLog(@"offline_login sql:%@",sqlQuery);
  1651. // sqlite3_stmt * statement;
  1652. //
  1653. //
  1654. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1655. //
  1656. //
  1657. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1658. // {
  1659. //
  1660. //
  1661. // if (sqlite3_step(statement) == SQLITE_ROW)
  1662. // {
  1663. //
  1664. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1665. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1666. //
  1667. //
  1668. //
  1669. // int can_show_price = sqlite3_column_int(statement, 0);
  1670. // int can_see_price = sqlite3_column_int(statement, 1);
  1671. //
  1672. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1673. // if(contact_id==nil)
  1674. // contact_id="";
  1675. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1676. //
  1677. // int user_type = sqlite3_column_int(statement, 3);
  1678. //
  1679. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1680. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1681. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1682. // int can_delete_order = sqlite3_column_int(statement, 7);
  1683. // int can_submit_order = sqlite3_column_int(statement, 8);
  1684. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1685. // int can_create_order = sqlite3_column_int(statement, 10);
  1686. //
  1687. //
  1688. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1689. // if(mode==nil)
  1690. // mode="";
  1691. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1692. //
  1693. //
  1694. // char *username = (char*)sqlite3_column_text(statement, 12);
  1695. // if(username==nil)
  1696. // username="";
  1697. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1698. //
  1699. //
  1700. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1701. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1702. // [header setValue:nscontact_id forKey:@"contact_id"];
  1703. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1704. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1705. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1706. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1707. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1708. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1709. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1710. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1711. //
  1712. // [header setValue:nsusername forKey:@"username"];
  1713. //
  1714. //
  1715. // [ret setObject:header forKey:@"header"];
  1716. // [ret setValue:nsmode forKey:@"mode"];
  1717. //
  1718. //
  1719. // }
  1720. //
  1721. //
  1722. //
  1723. // sqlite3_finalize(statement);
  1724. // }
  1725. //
  1726. //
  1727. //
  1728. // [iSalesDB close_db:db];
  1729. //
  1730. //
  1731. //
  1732. //
  1733. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1734. return ret;
  1735. }
  1736. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1737. {
  1738. UIApplication * app = [UIApplication sharedApplication];
  1739. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1740. appDelegate.disable_trigger=true;
  1741. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1742. appDelegate.disable_trigger=false;
  1743. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1744. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1745. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1746. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1747. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1748. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1749. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1750. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1751. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1752. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1753. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1754. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1755. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1756. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1757. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1758. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1759. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1760. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1761. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1762. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1763. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1764. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1765. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1766. [arr_name addObject:customer_first_name];
  1767. [arr_name addObject:customer_last_name];
  1768. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1769. // default ship from
  1770. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  1771. __block NSString *cid = @"";
  1772. __block NSString *name = @"";
  1773. __block NSString *ext = @"";
  1774. __block NSString *contact = @"";
  1775. __block NSString *email = @"";
  1776. __block NSString *fax = @"";
  1777. __block NSString *phone = @"";
  1778. sqlite3 *db = [iSalesDB get_db];
  1779. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  1780. cid = [self textAtColumn:0 statement:statment];
  1781. name = [self textAtColumn:1 statement:statment];
  1782. ext = [self textAtColumn:2 statement:statment];
  1783. contact = [self textAtColumn:3 statement:statment];
  1784. email = [self textAtColumn:4 statement:statment];
  1785. fax = [self textAtColumn:5 statement:statment];
  1786. phone = [self textAtColumn:6 statement:statment];
  1787. }];
  1788. NSString* so_id = [self get_offline_soid:db];
  1789. if(so_id==nil)
  1790. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  1791. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email];
  1792. int result =[iSalesDB execSql:sql_neworder db:db];
  1793. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1794. //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  1795. //soId
  1796. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1797. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1798. sqlite3_stmt * statement;
  1799. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1800. {
  1801. if (sqlite3_step(statement) == SQLITE_ROW)
  1802. {
  1803. // char *name = (char*)sqlite3_column_text(statement, 1);
  1804. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1805. //ret = sqlite3_column_int(statement, 0);
  1806. char *soId = (char*)sqlite3_column_text(statement, 0);
  1807. if(soId==nil)
  1808. soId="";
  1809. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1810. [ret setValue:nssoId forKey:@"soId"];
  1811. [ret setValue:nssoId forKey:@"orderCode"];
  1812. }
  1813. sqlite3_finalize(statement);
  1814. }
  1815. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1816. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1817. [iSalesDB close_db:db];
  1818. return [RAUtils dict2data:ret];
  1819. }
  1820. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1821. {
  1822. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1823. NSString* orderCode = [params valueForKey:@"orderCode"];
  1824. UIApplication * app = [UIApplication sharedApplication];
  1825. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1826. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  1827. {
  1828. appDelegate.disable_trigger=true;
  1829. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1830. appDelegate.disable_trigger=false;
  1831. }
  1832. sqlite3 *db = [iSalesDB get_db];
  1833. int cart_count=[self query_ordercartcount:orderCode db:db];
  1834. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1835. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1836. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1837. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1838. [iSalesDB close_db:db];
  1839. return [RAUtils dict2data:ret];
  1840. }
  1841. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1842. {
  1843. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1844. sqlite3 *db = [iSalesDB get_db];
  1845. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  1846. int count =0;
  1847. if(params[@"count"]!=nil)
  1848. {
  1849. count = [params[@"count"] intValue];
  1850. }
  1851. NSString* product_id=params[@"product_id"];
  1852. NSString* orderCode=params[@"orderCode"];
  1853. NSString *qty = params[@"qty"];
  1854. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1855. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  1856. for(int i=0;i<arr_id.count;i++)
  1857. {
  1858. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1859. int item_qty= count;
  1860. if (qty) {
  1861. item_qty = [qty_arr[i] integerValue];
  1862. }
  1863. if(item_qty==0)
  1864. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1865. NSString* sql=nil;
  1866. sqlite3_stmt *stmt;
  1867. if(_id<0)
  1868. {
  1869. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1870. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  1871. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1872. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  1873. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  1874. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1875. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  1876. }
  1877. else
  1878. {
  1879. if (qty) {
  1880. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  1881. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1882. sqlite3_bind_int(stmt, 1, _id);
  1883. } else {
  1884. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  1885. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1886. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1887. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  1888. }
  1889. }
  1890. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  1891. [iSalesDB execSql:@"ROLLBACK" db:db];
  1892. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  1893. [iSalesDB close_db:db];
  1894. DebugLog(@"add to cart error");
  1895. return [RAUtils dict2data:ret];
  1896. }
  1897. }
  1898. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1899. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1900. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1901. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1902. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1903. [iSalesDB execSql:@"END TRANSACTION" db:db];
  1904. [iSalesDB close_db:db];
  1905. return [RAUtils dict2data:ret];
  1906. }
  1907. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1908. {
  1909. UIApplication * app = [UIApplication sharedApplication];
  1910. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1911. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1912. NSString *sqlQuery = [NSString stringWithFormat:@"select b.item_id,b.qty,c.name,c.description,b.unit_price,b.use_unitprice from (select item_id,qty,decrypt(unit_price) as unit_price,use_unitprice from bundle where product_item_id=%d) b left join catalog c on b.item_id=c.item_id;",item_id];
  1913. sqlite3_stmt * statement;
  1914. int count=0;
  1915. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1916. {
  1917. while (sqlite3_step(statement) == SQLITE_ROW)
  1918. {
  1919. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1920. int bitem_id = sqlite3_column_int(statement, 0);
  1921. int bitem_qty = sqlite3_column_int(statement, 1);
  1922. char *name = (char*)sqlite3_column_text(statement, 2);
  1923. if(name==nil)
  1924. name="";
  1925. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1926. char *description = (char*)sqlite3_column_text(statement, 3);
  1927. if(description==nil)
  1928. description="";
  1929. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1930. double unit_price = sqlite3_column_double(statement, 4);
  1931. int use_unitprice = sqlite3_column_int(statement, 5);
  1932. if(use_unitprice!=1)
  1933. {
  1934. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1935. }
  1936. itemjson[@"model"]=nsname;
  1937. itemjson[@"description"]=nsdescription;
  1938. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1939. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1940. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  1941. if(compute)
  1942. {
  1943. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1944. }
  1945. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1946. count++;
  1947. }
  1948. sqlite3_finalize(statement);
  1949. }
  1950. ret[@"count"]=@(count);
  1951. if(count==0)
  1952. return nil;
  1953. else
  1954. return ret;
  1955. }
  1956. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1957. {
  1958. //compute: add part to subtotal;
  1959. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  1960. dict_item[@(item_id)]=@"1";
  1961. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1962. double cuft=0;
  1963. double weight=0;
  1964. int carton=0;
  1965. NSString *sqlQuery = [NSString stringWithFormat:@"select ulength ,uwidth ,uheight ,uweight ,mlength ,mwidth ,mheight ,mweigth ,ilength ,iwidth ,iheight , iweight ,pcs ,mpack ,ipack,ucbf,icbf,mcbf from catalog where item_id=%d;",item_id];
  1966. sqlite3_stmt * statement;
  1967. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1968. {
  1969. if (sqlite3_step(statement) == SQLITE_ROW)
  1970. {
  1971. double ulength = sqlite3_column_double(statement, 0);
  1972. double uwidth = sqlite3_column_double(statement, 1);
  1973. double uheight = sqlite3_column_double(statement, 2);
  1974. double uweight = sqlite3_column_double(statement, 3);
  1975. double mlength = sqlite3_column_double(statement, 4);
  1976. double mwidth = sqlite3_column_double(statement, 5);
  1977. double mheight = sqlite3_column_double(statement, 6);
  1978. double mweight = sqlite3_column_double(statement, 7);
  1979. double ilength = sqlite3_column_double(statement, 8);
  1980. double iwidth = sqlite3_column_double(statement, 9);
  1981. double iheight = sqlite3_column_double(statement, 10);
  1982. double iweight = sqlite3_column_double(statement, 11);
  1983. int pcs = sqlite3_column_int(statement,12);
  1984. int mpack = sqlite3_column_int(statement, 13);
  1985. int ipack = sqlite3_column_int(statement, 14);
  1986. double ucbf = sqlite3_column_double(statement, 15);
  1987. double icbf = sqlite3_column_double(statement, 16);
  1988. double mcbf = sqlite3_column_double(statement, 17);
  1989. if(ipack==0)
  1990. {
  1991. carton= count/mpack ;
  1992. weight = mweight*carton;
  1993. cuft= carton*(mlength*mwidth*mheight);
  1994. int remain=count%mpack;
  1995. if(remain==0)
  1996. {
  1997. //do nothing;
  1998. }
  1999. else
  2000. {
  2001. carton++;
  2002. weight += uweight*remain;
  2003. cuft += (ulength*uwidth*uheight)*remain;
  2004. }
  2005. }
  2006. else
  2007. {
  2008. carton = count/(mpack*ipack);
  2009. weight = mweight*carton;
  2010. cuft= carton*(mlength*mwidth*mheight);
  2011. int remain=count%(mpack*ipack);
  2012. if(remain==0)
  2013. {
  2014. // do nothing;
  2015. }
  2016. else
  2017. {
  2018. carton++;
  2019. int icarton =remain/ipack;
  2020. int iremain=remain%ipack;
  2021. weight += iweight*icarton;
  2022. cuft += (ilength*iwidth*iheight)*icarton;
  2023. if(iremain==0)
  2024. {
  2025. //do nothing;
  2026. }
  2027. else
  2028. {
  2029. weight += uweight*iremain;
  2030. cuft += (ulength*uwidth*uheight)*iremain;
  2031. }
  2032. }
  2033. }
  2034. #ifdef BUILD_NPD
  2035. cuft=ucbf*count;
  2036. weight= uweight*count;
  2037. #endif
  2038. }
  2039. sqlite3_finalize(statement);
  2040. }
  2041. if(compute)
  2042. {
  2043. NSArray * arr_count=nil;
  2044. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  2045. for(int i=0;i<arr_bundle.count;i++)
  2046. {
  2047. dict_item[arr_bundle[i]]=@"1";
  2048. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  2049. cuft+=[bundlejson[@"cuft"] doubleValue];
  2050. weight+=[bundlejson[@"weight"] doubleValue];
  2051. carton+=[bundlejson[@"carton"] intValue];
  2052. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  2053. }
  2054. }
  2055. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  2056. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  2057. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  2058. ret[@"items"]=dict_item;
  2059. return ret;
  2060. }
  2061. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  2062. {
  2063. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  2064. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  2065. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2066. DebugLog(@"offline_login sql:%@",sqlQuery);
  2067. sqlite3_stmt * statement;
  2068. int cart_count=0;
  2069. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2070. if ( dbresult== SQLITE_OK)
  2071. {
  2072. while (sqlite3_step(statement) == SQLITE_ROW)
  2073. {
  2074. int item_id = sqlite3_column_int(statement, 0);
  2075. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  2076. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2077. }
  2078. sqlite3_finalize(statement);
  2079. }
  2080. return cart_count;
  2081. }
  2082. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  2083. {
  2084. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2085. sqlite3 *db = [iSalesDB get_db];
  2086. UIApplication * app = [UIApplication sharedApplication];
  2087. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2088. NSString* orderCode=params[@"orderCode"];
  2089. int sort = [[params objectForKey:@"sort"] intValue];
  2090. NSString *sort_str = @"";
  2091. switch (sort) {
  2092. case 0:{
  2093. sort_str = @"order by c.modify_time desc";
  2094. }
  2095. break;
  2096. case 1:{
  2097. sort_str = @"order by c.modify_time asc";
  2098. }
  2099. break;
  2100. case 2:{
  2101. sort_str = @"order by m.name asc";
  2102. }
  2103. break;
  2104. case 3:{
  2105. sort_str = @"order by m.name desc";
  2106. }
  2107. break;
  2108. case 4:{
  2109. sort_str = @"order by m.description asc";
  2110. }
  2111. break;
  2112. default:
  2113. break;
  2114. }
  2115. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  2116. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  2117. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  2118. DebugLog(@"offline_login sql:%@",sqlQuery);
  2119. sqlite3_stmt * statement;
  2120. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2121. NSDate *date1 = [NSDate date];
  2122. int count=0;
  2123. int cart_count=0;
  2124. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  2125. if ( dbresult== SQLITE_OK)
  2126. {
  2127. while (sqlite3_step(statement) == SQLITE_ROW)
  2128. {
  2129. NSDate *row_date = [NSDate date];
  2130. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  2131. int product_id = sqlite3_column_int(statement, 0);
  2132. char *str_price = (char*)sqlite3_column_text(statement, 1);
  2133. int item_id = sqlite3_column_int(statement, 7);
  2134. NSString* Price=nil;
  2135. if(str_price==nil)
  2136. {
  2137. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2138. // NSDate *price_date = [NSDate date];
  2139. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2140. // DebugLog(@"price time interval");
  2141. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  2142. if(price==nil)
  2143. Price=@"No Price.";
  2144. else
  2145. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2146. }
  2147. else
  2148. {
  2149. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  2150. }
  2151. double discount = sqlite3_column_double(statement, 2);
  2152. int item_count = sqlite3_column_int(statement, 3);
  2153. char *line_note = (char*)sqlite3_column_text(statement, 4);
  2154. NSString *nsline_note=nil;
  2155. if(line_note!=nil)
  2156. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  2157. char *name = (char*)sqlite3_column_text(statement, 5);
  2158. NSString *nsname=nil;
  2159. if(name!=nil)
  2160. nsname= [[NSString alloc]initWithUTF8String:name];
  2161. char *description = (char*)sqlite3_column_text(statement, 6);
  2162. NSString *nsdescription=nil;
  2163. if(description!=nil)
  2164. nsdescription= [[NSString alloc]initWithUTF8String:description];
  2165. int stockUom = sqlite3_column_int(statement, 8);
  2166. int _id = sqlite3_column_int(statement, 9);
  2167. // NSDate *subtotal_date = [NSDate date];
  2168. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  2169. // DebugLog(@"subtotal_date time interval");
  2170. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  2171. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  2172. double weight=[bsubtotaljson[@"weight"] doubleValue];
  2173. int carton=[bsubtotaljson[@"carton"] intValue];
  2174. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  2175. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  2176. // NSDate *img_date = [NSDate date];
  2177. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  2178. // DebugLog(@"img_date time interval");
  2179. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  2180. itemjson[@"model"]=nsname;
  2181. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  2182. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  2183. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  2184. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  2185. itemjson[@"check"]=@"true";
  2186. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  2187. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  2188. itemjson[@"unit_price"]=Price;
  2189. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  2190. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  2191. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  2192. itemjson[@"note"]=nsline_note;
  2193. // NSDate *date2 = [NSDate date];
  2194. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  2195. // DebugLog(@"model_bundle time interval");
  2196. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  2197. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2198. count++;
  2199. // DebugLog(@"row time interval");
  2200. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  2201. }
  2202. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2203. sqlite3_finalize(statement);
  2204. }
  2205. DebugLog(@"request cart total time interval");
  2206. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  2207. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2208. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2209. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2210. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2211. ret[@"count"]=[NSNumber numberWithInt:count ];
  2212. ret[@"mode"]=@"Regular Mode";
  2213. [iSalesDB close_db:db];
  2214. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  2215. DebugLog(@"general notes :%@",general_note);
  2216. ret[@"general_note"]= general_note;
  2217. return [RAUtils dict2data:ret];
  2218. }
  2219. +(NSData*) offline_login :(NSMutableDictionary *) params
  2220. {
  2221. NSString* user = [params valueForKey:@"user"];
  2222. NSString* password = [params valueForKey:@"password"];
  2223. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2224. sqlite3 *db = [iSalesDB get_db];
  2225. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  2226. DebugLog(@"offline_login sql:%@",sqlQuery);
  2227. sqlite3_stmt * statement;
  2228. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2229. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2230. {
  2231. if (sqlite3_step(statement) == SQLITE_ROW)
  2232. {
  2233. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2234. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2235. int can_show_price = sqlite3_column_int(statement, 0);
  2236. int can_see_price = sqlite3_column_int(statement, 1);
  2237. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2238. if(contact_id==nil)
  2239. contact_id="";
  2240. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2241. int user_type = sqlite3_column_int(statement, 3);
  2242. int can_cancel_order = sqlite3_column_int(statement, 4);
  2243. int can_set_cart_price = sqlite3_column_int(statement, 5);
  2244. int can_create_portfolio = sqlite3_column_int(statement, 6);
  2245. int can_delete_order = sqlite3_column_int(statement, 7);
  2246. int can_submit_order = sqlite3_column_int(statement, 8);
  2247. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2248. int can_create_order = sqlite3_column_int(statement, 10);
  2249. char *mode = (char*)sqlite3_column_text(statement, 11);
  2250. if(mode==nil)
  2251. mode="";
  2252. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2253. char *username = (char*)sqlite3_column_text(statement, 12);
  2254. if(username==nil)
  2255. username="";
  2256. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2257. int can_update_contact_info = sqlite3_column_int(statement, 13);
  2258. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2259. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2260. [header setValue:nscontact_id forKey:@"contact_id"];
  2261. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2262. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2263. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2264. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2265. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2266. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2267. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2268. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2269. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  2270. [header setValue:nsusername forKey:@"username"];
  2271. NSError* error=nil;
  2272. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  2273. [header setValue:statusFilter forKey:@"statusFilter"];
  2274. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2275. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  2276. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  2277. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  2278. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  2279. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  2280. [ret setObject:header forKey:@"header"];
  2281. [ret setValue:nsmode forKey:@"mode"];
  2282. }
  2283. sqlite3_finalize(statement);
  2284. }
  2285. [iSalesDB close_db:db];
  2286. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2287. return [RAUtils dict2data:ret];
  2288. }
  2289. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  2290. {
  2291. NSString* contactId = [params valueForKey:@"contactId"];
  2292. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2293. sqlite3 *db = [iSalesDB get_db];
  2294. NSString *sqlQuery = nil;
  2295. {
  2296. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep from offline_contact where contact_id='%@'",contactId];
  2297. }
  2298. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  2299. sqlite3_stmt * statement;
  2300. [ret setValue:@"2" forKey:@"result"];
  2301. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2302. {
  2303. //int i = 0;
  2304. if (sqlite3_step(statement) == SQLITE_ROW)
  2305. {
  2306. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2307. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2308. // int editable = sqlite3_column_int(statement, 0);
  2309. char *company_name = (char*)sqlite3_column_text(statement, 1);
  2310. NSString *nscompany_name =nil;
  2311. if(company_name==nil)
  2312. nscompany_name=@"";
  2313. else
  2314. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  2315. char *country = (char*)sqlite3_column_text(statement, 2);
  2316. if(country==nil)
  2317. country="";
  2318. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  2319. // char *addr = (char*)sqlite3_column_text(statement, 3);
  2320. // if(addr==nil)
  2321. // addr="";
  2322. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  2323. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  2324. if(zipcode==nil)
  2325. zipcode="";
  2326. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  2327. char *state = (char*)sqlite3_column_text(statement, 5);
  2328. if(state==nil)
  2329. state="";
  2330. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  2331. char *city = (char*)sqlite3_column_text(statement, 6);
  2332. if(city==nil)
  2333. city="";
  2334. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  2335. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  2336. // NSString *nscontact_name = nil;
  2337. // if(contact_name==nil)
  2338. // nscontact_name=@"";
  2339. // else
  2340. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  2341. char *phone = (char*)sqlite3_column_text(statement, 8);
  2342. NSString *nsphone = nil;
  2343. if(phone==nil)
  2344. nsphone=@"";
  2345. else
  2346. nsphone= [[NSString alloc]initWithUTF8String:phone];
  2347. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  2348. if(contact_id==nil)
  2349. contact_id="";
  2350. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2351. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  2352. if(addr_1==nil)
  2353. addr_1="";
  2354. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  2355. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  2356. if(addr_2==nil)
  2357. addr_2="";
  2358. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  2359. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  2360. if(addr_3==nil)
  2361. addr_3="";
  2362. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  2363. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  2364. if(addr_4==nil)
  2365. addr_4="";
  2366. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  2367. char *first_name = (char*)sqlite3_column_text(statement, 14);
  2368. if(first_name==nil)
  2369. first_name="";
  2370. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  2371. char *last_name = (char*)sqlite3_column_text(statement, 15);
  2372. if(last_name==nil)
  2373. last_name="";
  2374. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  2375. char *fax = (char*)sqlite3_column_text(statement, 16);
  2376. NSString *nsfax = nil;
  2377. if(fax==nil)
  2378. nsfax=@"";
  2379. else
  2380. nsfax= [[NSString alloc]initWithUTF8String:fax];
  2381. char *email = (char*)sqlite3_column_text(statement, 17);
  2382. NSString *nsemail = nil;
  2383. if(email==nil)
  2384. nsemail=@"";
  2385. else
  2386. nsemail= [[NSString alloc]initWithUTF8String:email];
  2387. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  2388. NSString *nsimg_0 = nil;
  2389. if(img_0==nil)
  2390. nsimg_0=@"";
  2391. else
  2392. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  2393. [self copy_bcardImg:nsimg_0];
  2394. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  2395. NSString *nsimg_1 = nil;
  2396. if(img_1==nil)
  2397. nsimg_1=@"";
  2398. else
  2399. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  2400. [self copy_bcardImg:nsimg_1];
  2401. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  2402. NSString *nsimg_2 = nil;
  2403. if(img_2==nil)
  2404. nsimg_2=@"";
  2405. else
  2406. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2407. [self copy_bcardImg:nsimg_2];
  2408. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2409. NSString *nsprice_type = nil;
  2410. if(price_type==nil)
  2411. nsprice_type=@"";
  2412. else
  2413. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2414. char *notes = (char*)sqlite3_column_text(statement, 22);
  2415. NSString *nsnotes = nil;
  2416. if(notes==nil)
  2417. nsnotes=@"";
  2418. else
  2419. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2420. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2421. NSString *nssalesrep = nil;
  2422. if(salesrep==nil)
  2423. nssalesrep=@"";
  2424. else
  2425. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2426. {
  2427. // decrypt
  2428. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2429. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2430. nsphone=[AESCrypt fastdecrypt:nsphone];
  2431. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2432. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2433. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2434. }
  2435. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2436. [arr_name addObject:nsfirst_name];
  2437. [arr_name addObject:nslast_name];
  2438. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2439. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2440. [arr_ext addObject:nsaddr_1];
  2441. [arr_ext addObject:nsaddr_2];
  2442. [arr_ext addObject:nsaddr_3];
  2443. [arr_ext addObject:nsaddr_4];
  2444. [arr_ext addObject:@"\r\n"];
  2445. [arr_ext addObject:nscity];
  2446. [arr_ext addObject:nsstate];
  2447. [arr_ext addObject:nszipcode];
  2448. [arr_ext addObject:nscountry];
  2449. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2450. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2451. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2452. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2453. [item setValue:nscountry forKey:@"customer_country"];
  2454. [item setValue:nsphone forKey:@"customer_phone"];
  2455. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2456. [item setValue:nscompany_name forKey:@"customer_name"];
  2457. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2458. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2459. [item setValue:nsext forKey:@"customer_contact_ext"];
  2460. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2461. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2462. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2463. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2464. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2465. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2466. [item setValue:nslast_name forKey:@"customer_last_name"];
  2467. [item setValue:nscity forKey:@"customer_city"];
  2468. [item setValue:nsstate forKey:@"customer_state"];
  2469. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2470. [item setValue:contactId forKey:@"customer_cid"];
  2471. [item setValue:nscontact_name forKey:@"customer_contact"];
  2472. [item setValue:nsfax forKey:@"customer_fax"];
  2473. [item setValue:nsemail forKey:@"customer_email"];
  2474. [ret setObject:item forKey:@"customerInfo"];
  2475. // i++;
  2476. }
  2477. UIApplication * app = [UIApplication sharedApplication];
  2478. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2479. [ret setValue:appDelegate.mode forKey:@"mode"];
  2480. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2481. sqlite3_finalize(statement);
  2482. }
  2483. [iSalesDB close_db:db];
  2484. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2485. return ret;
  2486. }
  2487. + (bool) copy_bcardImg:(NSString*) filename
  2488. {
  2489. if(filename.length==0)
  2490. return false;
  2491. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2492. bool ret=false;
  2493. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2494. NSString *cachefolder = [paths objectAtIndex:0];
  2495. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2496. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2497. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2498. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2499. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2500. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2501. //
  2502. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2503. NSFileManager* fileManager = [NSFileManager defaultManager];
  2504. BOOL bdir=NO;
  2505. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2506. {
  2507. NSError *error = nil;
  2508. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2509. {
  2510. ret=false;
  2511. }
  2512. else
  2513. {
  2514. ret=true;
  2515. }
  2516. // NSError *error = nil;
  2517. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2518. //
  2519. // if(!bsuccess)
  2520. // {
  2521. // DebugLog(@"Create offline_createimg folder failed");
  2522. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2523. // return [RAUtils dict2data:ret];
  2524. // }
  2525. // if(bsuccess)
  2526. // {
  2527. // sqlite3 *db = [self get_db];
  2528. //
  2529. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2530. // [iSalesDB close_db:db];
  2531. // }
  2532. }
  2533. return ret;
  2534. //
  2535. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2536. // if(bsuccess)
  2537. // {
  2538. // NSError *error = nil;
  2539. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2540. // {
  2541. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2542. // }
  2543. // else
  2544. // {
  2545. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2546. //
  2547. // ret[@"img_url_aname"]=filename;
  2548. // ret[@"img_url"]=savedImagePath;
  2549. // }
  2550. // }
  2551. }
  2552. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2553. {
  2554. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2555. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2556. NSString *cachefolder = [paths objectAtIndex:0];
  2557. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2558. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2559. NSFileManager* fileManager = [NSFileManager defaultManager];
  2560. BOOL bdir=YES;
  2561. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2562. {
  2563. NSError *error = nil;
  2564. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2565. if(!bsuccess)
  2566. {
  2567. DebugLog(@"Create offline_createimg folder failed");
  2568. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2569. return [RAUtils dict2data:ret];
  2570. }
  2571. // if(bsuccess)
  2572. // {
  2573. // sqlite3 *db = [self get_db];
  2574. //
  2575. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2576. // [iSalesDB close_db:db];
  2577. // }
  2578. }
  2579. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  2580. //JEPG格式
  2581. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  2582. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  2583. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2584. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2585. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2586. if(bsuccess)
  2587. {
  2588. NSError *error = nil;
  2589. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2590. {
  2591. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2592. }
  2593. else
  2594. {
  2595. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2596. ret[@"img_url_aname"]=filename;
  2597. ret[@"img_url"]=filename;
  2598. }
  2599. }
  2600. else
  2601. {
  2602. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2603. }
  2604. return [RAUtils dict2data:ret];
  2605. }
  2606. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  2607. {
  2608. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  2609. category = [category substringToIndex:3];
  2610. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2611. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  2612. params[@"category"]= category;
  2613. ret[@"params"]= params;
  2614. [ret setValue:@"detail" forKey:@"target"];
  2615. [ret setValue:@"popup" forKey:@"action"];
  2616. [ret setValue:@"content" forKey:@"type"];
  2617. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  2618. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2619. [ret setValue:@"true" forKey:@"single_row"];
  2620. [ret setValue:@"true" forKey:@"partial_refresh"];
  2621. // sqlite3 *db = [iSalesDB get_db];
  2622. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2623. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY]; // select m.name,m.product_id,i.url,random() as aa from model m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;
  2624. sqlite3_stmt * statement;
  2625. int count = 0;
  2626. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  2627. // int count=0;
  2628. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2629. {
  2630. int i=0;
  2631. while (sqlite3_step(statement) == SQLITE_ROW)
  2632. {
  2633. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2634. // char *name = (char*)sqlite3_column_text(statement, 1);
  2635. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2636. char *name = (char*)sqlite3_column_text(statement, 0);
  2637. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2638. int product_id = sqlite3_column_int(statement, 1);
  2639. char *url = (char*)sqlite3_column_text(statement, 2);
  2640. if(url==nil)
  2641. url="";
  2642. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2643. [item setValue:nsurl forKey:@"picture_path"];
  2644. [item setValue:nsname forKey:@"fash_name"];
  2645. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2646. [item setValue:category forKey:@"category"];
  2647. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2648. i++;
  2649. }
  2650. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2651. sqlite3_finalize(statement);
  2652. }
  2653. NSLog(@"count:%d",count);
  2654. // [iSalesDB close_db:db];
  2655. return ret;
  2656. }
  2657. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  2658. {
  2659. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2660. [ret setValue:key forKey:@"key"];
  2661. [ret setValue:value forKey:@"val"];
  2662. return ret;
  2663. }
  2664. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  2665. {
  2666. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2667. [ret setValue:@"0" forKey:@"img_count"];
  2668. // sqlite3 *db = [iSalesDB get_db];
  2669. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from product where product_id=%d ;",field,product_id]; // select %@,property_display from model where product_id=%d ;
  2670. sqlite3_stmt * statement;
  2671. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2672. {
  2673. int i=0;
  2674. if (sqlite3_step(statement) == SQLITE_ROW)
  2675. {
  2676. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2677. // char *name = (char*)sqlite3_column_text(statement, 1);
  2678. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2679. char *value = (char*)sqlite3_column_text(statement, 0);
  2680. if(value==nil)
  2681. value="";
  2682. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2683. char *key = (char*)sqlite3_column_text(statement, 1);
  2684. if(key==nil)
  2685. key="";
  2686. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  2687. [item setValue:nsvalue forKey:@"val"];
  2688. [item setValue:nskey forKey:@"key"];
  2689. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2690. [ret setValue:@"1" forKey:@"count"];
  2691. i++;
  2692. }
  2693. sqlite3_finalize(statement);
  2694. }
  2695. // [iSalesDB close_db:db];
  2696. return ret;
  2697. }
  2698. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  2699. {
  2700. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2701. [ret setValue:@"0" forKey:@"count"];
  2702. // sqlite3 *db = [iSalesDB get_db];
  2703. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from product where product_group='%@' order by name;",field,product_group]; // select %@,selector_display,product_id,category from model where product_group='%@' order by name;
  2704. sqlite3_stmt * statement;
  2705. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2706. {
  2707. int i=0;
  2708. while (sqlite3_step(statement) == SQLITE_ROW)
  2709. {
  2710. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2711. // char *name = (char*)sqlite3_column_text(statement, 1);
  2712. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2713. char *value = (char*)sqlite3_column_text(statement, 0);
  2714. if(value==nil)
  2715. value="";
  2716. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2717. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  2718. if(selector_display==nil)
  2719. selector_display="";
  2720. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  2721. int product_id = sqlite3_column_int(statement, 2);
  2722. char *category = (char*)sqlite3_column_text(statement, 3);
  2723. if(category==nil)
  2724. category="";
  2725. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  2726. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  2727. [item setValue:nsvalue forKey:@"title"];
  2728. [item setValue:url forKey:@"pic_url"];
  2729. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  2730. [params setValue:@"2" forKey:@"count"];
  2731. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  2732. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  2733. [param0 setValue:@"product_id" forKey:@"name"];
  2734. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  2735. [param1 setValue:nscategory forKey:@"val"];
  2736. [param1 setValue:@"category" forKey:@"name"];
  2737. [params setObject:param0 forKey:@"param_0"];
  2738. [params setObject:param1 forKey:@"param_1"];
  2739. [item setObject:params forKey:@"params"];
  2740. [ret setValue:nsselector_display forKey:@"name"];
  2741. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2742. i++;
  2743. }
  2744. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2745. [ret setValue:@"switch" forKey:@"action"];
  2746. sqlite3_finalize(statement);
  2747. }
  2748. // [iSalesDB close_db:db];
  2749. return ret;
  2750. }
  2751. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  2752. {
  2753. // model 在 category search 显示的图片。
  2754. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  2755. // sqlite3 *db = [iSalesDB get_db];
  2756. NSString *sqlQuery = [NSString stringWithFormat:@"select url,type from model_image where product_id=%d and type<2 order by default_img desc , _id asc limit 2;",product_id];
  2757. sqlite3_stmt * statement;
  2758. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2759. {
  2760. while (sqlite3_step(statement) == SQLITE_ROW)
  2761. {
  2762. char *url = (char*)sqlite3_column_text(statement, 0);
  2763. if(url==nil)
  2764. url="";
  2765. int type = sqlite3_column_int(statement, 1);
  2766. if(type==0)
  2767. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  2768. else
  2769. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  2770. }
  2771. sqlite3_finalize(statement);
  2772. }
  2773. // [iSalesDB close_db:db];
  2774. return ret;
  2775. }
  2776. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  2777. {
  2778. int item_id=-1;
  2779. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  2780. sqlite3_stmt * statement;
  2781. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2782. {
  2783. if (sqlite3_step(statement) == SQLITE_ROW)
  2784. {
  2785. item_id = sqlite3_column_int(statement, 0);
  2786. }
  2787. sqlite3_finalize(statement);
  2788. }
  2789. return item_id;
  2790. }
  2791. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  2792. {
  2793. // NSString* ret = @"";
  2794. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  2795. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  2796. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  2797. sqlite3_stmt * statement;
  2798. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2799. {
  2800. while (sqlite3_step(statement) == SQLITE_ROW)
  2801. {
  2802. int bitem_id = sqlite3_column_int(statement, 0);
  2803. int bitem_qty = sqlite3_column_int(statement, 1);
  2804. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  2805. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  2806. }
  2807. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  2808. sqlite3_finalize(statement);
  2809. }
  2810. // if(ret==nil)
  2811. // ret=@"";
  2812. *count=arr_count;
  2813. return arr_bundle;
  2814. }
  2815. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2816. {
  2817. // get default sold qty, return -1 if model not found;
  2818. int ret = -1;
  2819. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2820. sqlite3_stmt * statement;
  2821. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2822. {
  2823. if (sqlite3_step(statement) == SQLITE_ROW)
  2824. {
  2825. ret = sqlite3_column_int(statement, 0);
  2826. }
  2827. sqlite3_finalize(statement);
  2828. }
  2829. return ret;
  2830. }
  2831. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2832. {
  2833. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2834. [ret setValue:@"0" forKey:@"img_count"];
  2835. // sqlite3 *db = [iSalesDB get_db];
  2836. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2837. sqlite3_stmt * statement;
  2838. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2839. {
  2840. int i=0;
  2841. while (sqlite3_step(statement) == SQLITE_ROW)
  2842. {
  2843. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2844. // char *name = (char*)sqlite3_column_text(statement, 1);
  2845. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2846. char *url = (char*)sqlite3_column_text(statement, 0);
  2847. if(url==nil)
  2848. url="";
  2849. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2850. [item setValue:nsurl forKey:@"s"];
  2851. [item setValue:nsurl forKey:@"l"];
  2852. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2853. i++;
  2854. }
  2855. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2856. sqlite3_finalize(statement);
  2857. }
  2858. // [iSalesDB close_db:db];
  2859. return ret;
  2860. }
  2861. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  2862. UIApplication * app = [UIApplication sharedApplication];
  2863. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2864. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2865. sqlite3 *db = [iSalesDB get_db];
  2866. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2867. NSString* product_id=params[@"product_id"];
  2868. NSString *item_count_str = params[@"item_count"];
  2869. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2870. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  2871. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2872. // NSString *sql = @"";
  2873. for(int i=0;i<arr.count;i++)
  2874. {
  2875. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2876. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2877. __block int cart_count = 0;
  2878. if (!item_count_str) {
  2879. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  2880. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2881. NSString *model_set = [self textAtColumn:0 statement:stmt];
  2882. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  2883. cart_count = [[model_set_components lastObject] integerValue];
  2884. }];
  2885. }
  2886. if(count==0)
  2887. {
  2888. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2889. sqlite3_stmt *stmt;
  2890. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  2891. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  2892. if (item_count_arr) {
  2893. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  2894. } else {
  2895. sqlite3_bind_int(stmt,2,cart_count);
  2896. }
  2897. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2898. [iSalesDB execSql:@"ROLLBACK" db:db];
  2899. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2900. [iSalesDB close_db:db];
  2901. DebugLog(@"add to wishlist error");
  2902. return ret;
  2903. }
  2904. } else {
  2905. int qty = 0;
  2906. if (item_count_arr) {
  2907. qty = [item_count_arr[i] integerValue];
  2908. } else {
  2909. qty = cart_count;
  2910. }
  2911. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@ and (is_delete is null or is_delete != 1);update wishlist set qty = %d,is_delete = 0 where product_id = %@ and is_delete = 1;",arr[i],qty,arr[i]];
  2912. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  2913. [iSalesDB execSql:@"ROLLBACK" db:db];
  2914. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  2915. [iSalesDB close_db:db];
  2916. DebugLog(@"add to wishlist error");
  2917. return ret;
  2918. }
  2919. }
  2920. }
  2921. // [iSalesDB execSql:sql db:db];
  2922. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  2923. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2924. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2925. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  2926. [iSalesDB close_db:db];
  2927. appDelegate.wish_count =count;
  2928. [appDelegate update_count_mark];
  2929. ret[@"result"]= [NSNumber numberWithInt:2];
  2930. return ret;
  2931. }
  2932. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  2933. // 0 category
  2934. // 1 search
  2935. // 2 itemsearch
  2936. NSData *ret = nil;
  2937. NSDictionary *items = nil;
  2938. switch (from) {
  2939. case 0:{
  2940. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  2941. }
  2942. break;
  2943. case 1:{
  2944. items = [[self search:params limited:NO] objectForKey:@"items"];
  2945. }
  2946. break;
  2947. case 2:{
  2948. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  2949. }
  2950. break;
  2951. default:
  2952. break;
  2953. }
  2954. if (!items) {
  2955. return ret;
  2956. }
  2957. int count = [[items objectForKey:@"count"] intValue];
  2958. NSMutableString *product_id_str = [@"" mutableCopy];
  2959. for (int i = 0; i < count; i++) {
  2960. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  2961. NSDictionary *item = [items objectForKey:key];
  2962. NSString *product_id = [item objectForKey:@"product_id"];
  2963. if (i == 0) {
  2964. [product_id_str appendString:product_id];
  2965. } else {
  2966. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  2967. }
  2968. }
  2969. NSString *add_to = [self valueInParams:params key:@"addTo"];
  2970. if ([add_to isEqualToString:@"cart"]) {
  2971. NSString *order_code = [params objectForKey:@"orderCode"];
  2972. if (order_code.length) {
  2973. NSDictionary *newParams = @{
  2974. @"product_id" : product_id_str,
  2975. @"orderCode" : order_code
  2976. };
  2977. ret = [self offline_add2cart:newParams.mutableCopy];
  2978. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2979. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2980. ret = [RAUtils dict2data:retDic];
  2981. }
  2982. } else if([add_to isEqualToString:@"wishlist"]) {
  2983. NSDictionary *newParams = @{
  2984. @"product_id" : product_id_str
  2985. };
  2986. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  2987. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2988. ret = [RAUtils dict2data:retDic];
  2989. } else if([add_to isEqualToString:@"portfolio"]) {
  2990. }
  2991. return ret;
  2992. }
  2993. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  2994. {
  2995. return [self addAll:params from:0];
  2996. }
  2997. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  2998. {
  2999. return [self addAll:params from:1];
  3000. }
  3001. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  3002. {
  3003. return [self addAll:params from:2];
  3004. }
  3005. #pragma mark - Jack
  3006. #warning 做SQL操作时转义!!
  3007. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  3008. // "val_227" : {
  3009. // "check" : 1,
  3010. // "value" : "US United States",
  3011. // "value_id" : "228"
  3012. // },
  3013. if (!countryCode) {
  3014. countryCode = @"US";
  3015. }
  3016. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  3017. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  3018. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  3019. int code_id = sqlite3_column_int(stmt, 3); // id
  3020. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3021. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  3022. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  3023. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3024. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  3025. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3026. }
  3027. long n = *count;
  3028. *count = n + 1;
  3029. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  3030. [container setValue:countryDic forKey:key];
  3031. }] mutableCopy];
  3032. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3033. return ret;
  3034. }
  3035. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  3036. countryCode = [self translateSingleQuote:countryCode];
  3037. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  3038. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3039. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  3040. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  3041. if (name == NULL) {
  3042. name = "";
  3043. }
  3044. if (code == NULL) {
  3045. code = "";
  3046. }
  3047. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3048. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3049. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3050. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3051. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  3052. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3053. }
  3054. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3055. [container setValue:stateDic forKey:key];
  3056. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  3057. DebugLog(@"query all state error: %@",err_msg);
  3058. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3059. // [stateDic setValue:@"Other" forKey:@"value"];
  3060. // [stateDic setValue:@"" forKey:@"value_id"];
  3061. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3062. //
  3063. // if (state_code && [@"" isEqualToString:state_code]) {
  3064. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3065. // }
  3066. //
  3067. // NSString *key = [NSString stringWithFormat:@"val_0"];
  3068. // [container setValue:stateDic forKey:key];
  3069. }] mutableCopy];
  3070. // failure 可以不用了,一样的
  3071. if (ret.allKeys.count == 0) {
  3072. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3073. [stateDic setValue:@"Other" forKey:@"value"];
  3074. [stateDic setValue:@"" forKey:@"value_id"];
  3075. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3076. if (state_code && [@"" isEqualToString:state_code]) {
  3077. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3078. }
  3079. NSString *key = [NSString stringWithFormat:@"val_0"];
  3080. [ret setValue:stateDic forKey:key];
  3081. }
  3082. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3083. return ret;
  3084. }
  3085. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  3086. codeId = [self translateSingleQuote:codeId];
  3087. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  3088. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3089. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  3090. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  3091. if (name == NULL) {
  3092. name = "";
  3093. }
  3094. if (code == NULL) {
  3095. code = "";
  3096. }
  3097. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  3098. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3099. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  3100. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3101. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  3102. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3103. }
  3104. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  3105. [container setValue:stateDic forKey:key];
  3106. }] mutableCopy];
  3107. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3108. return ret;
  3109. }
  3110. + (NSDictionary *)offline_getPrice {
  3111. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  3112. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3113. char *name = (char *) sqlite3_column_text(stmt, 1);
  3114. int type = sqlite3_column_int(stmt, 2);
  3115. int orderBy = sqlite3_column_int(stmt, 3);
  3116. if (name == NULL) {
  3117. name = "";
  3118. }
  3119. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  3120. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  3121. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  3122. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  3123. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  3124. if (orderBy == 0) {
  3125. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3126. }
  3127. [container setValue:priceDic forKey:key];
  3128. }] mutableCopy];
  3129. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3130. return ret;
  3131. }
  3132. + (NSDictionary *)offline_getSalesRep {
  3133. // 首先从offline_login表中取出sales_code
  3134. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3135. NSString *user = app.user;
  3136. user = [self translateSingleQuote:user];
  3137. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  3138. __block NSString *user_code = @"";
  3139. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3140. char *code = (char *)sqlite3_column_text(stmt, 0);
  3141. if (code == NULL) {
  3142. code = "";
  3143. }
  3144. user_code = [NSString stringWithUTF8String:code];
  3145. }];
  3146. // 再取所有salesRep
  3147. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  3148. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3149. // 1 name 2 code 3 salesrep_id
  3150. char *name = (char *)sqlite3_column_text(stmt, 1);
  3151. char *code = (char *)sqlite3_column_text(stmt, 2);
  3152. int salesrep_id = sqlite3_column_int(stmt, 3);
  3153. if (name == NULL) {
  3154. name = "";
  3155. }
  3156. if (code == NULL) {
  3157. code = "";
  3158. }
  3159. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  3160. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  3161. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  3162. // 比较code 相等则check
  3163. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  3164. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  3165. }
  3166. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  3167. }] mutableCopy];
  3168. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  3169. return ret;
  3170. }
  3171. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  3172. zipcode = [self translateSingleQuote:zipcode];
  3173. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  3174. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3175. char *country = (char *)sqlite3_column_text(stmt, 0);
  3176. char *state = (char *)sqlite3_column_text(stmt, 1);
  3177. char *city = (char *)sqlite3_column_text(stmt, 2);
  3178. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  3179. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  3180. if (country == NULL) {
  3181. country = "";
  3182. }
  3183. if (state == NULL) {
  3184. state = "";
  3185. }
  3186. if (city == NULL) {
  3187. city = "";
  3188. }
  3189. if (country_code == NULL) {
  3190. country_code = "";
  3191. }
  3192. if (state_code == NULL) {
  3193. state_code = "";
  3194. }
  3195. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  3196. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  3197. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  3198. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  3199. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  3200. }] mutableCopy];
  3201. return ret;
  3202. }
  3203. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3204. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  3205. [item setValue:value forKey:valueKey];
  3206. [dic setValue:item forKey:itemKey];
  3207. }
  3208. + (NSString *)countryCodeByid:(NSString *)code_id {
  3209. NSString *ret = nil;
  3210. code_id = [self translateSingleQuote:code_id];
  3211. sqlite3 *db = [iSalesDB get_db];
  3212. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  3213. sqlite3_stmt * statement;
  3214. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3215. while (sqlite3_step(statement) == SQLITE_ROW) {
  3216. char *code = (char *)sqlite3_column_text(statement, 0);
  3217. if (code == NULL) {
  3218. code = "";
  3219. }
  3220. ret = [NSString stringWithUTF8String:code];
  3221. }
  3222. sqlite3_finalize(statement);
  3223. }
  3224. [iSalesDB close_db:db];
  3225. return ret;
  3226. }
  3227. + (NSString *)countryCodeIdByCode:(NSString *)code {
  3228. NSString *ret = nil;
  3229. code = [self translateSingleQuote:code];
  3230. sqlite3 *db = [iSalesDB get_db];
  3231. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  3232. sqlite3_stmt * statement;
  3233. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3234. while (sqlite3_step(statement) == SQLITE_ROW) {
  3235. char *_id = (char *)sqlite3_column_text(statement, 0);
  3236. if (_id == NULL) {
  3237. _id = "";
  3238. }
  3239. ret = [NSString stringWithFormat:@"%s",_id];
  3240. }
  3241. sqlite3_finalize(statement);
  3242. }
  3243. [iSalesDB close_db:db];
  3244. return ret;
  3245. }
  3246. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  3247. NSString *name = nil;
  3248. codeId = [self translateSingleQuote:codeId];
  3249. sqlite3 *db = [iSalesDB get_db];
  3250. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  3251. sqlite3_stmt * statement;
  3252. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3253. while (sqlite3_step(statement) == SQLITE_ROW) {
  3254. char *value = (char *)sqlite3_column_text(statement, 0);
  3255. if (value == NULL) {
  3256. value = "";
  3257. }
  3258. name = [NSString stringWithUTF8String:value];
  3259. }
  3260. sqlite3_finalize(statement);
  3261. }
  3262. [iSalesDB close_db:db];
  3263. return name;
  3264. }
  3265. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  3266. NSString *ret = nil;
  3267. sqlite3 *db = [iSalesDB get_db];
  3268. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  3269. sqlite3_stmt * statement;
  3270. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3271. while (sqlite3_step(statement) == SQLITE_ROW) {
  3272. char *name = (char *)sqlite3_column_text(statement, 0);
  3273. if (name == NULL) {
  3274. name = "";
  3275. }
  3276. ret = [NSString stringWithUTF8String:name];
  3277. }
  3278. sqlite3_finalize(statement);
  3279. }
  3280. [iSalesDB close_db:db];
  3281. return ret;
  3282. }
  3283. + (NSString *)salesRepCodeById:(NSString *)_id {
  3284. NSString *ret = nil;
  3285. _id = [self translateSingleQuote:_id];
  3286. sqlite3 *db = [iSalesDB get_db];
  3287. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  3288. sqlite3_stmt * statement;
  3289. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  3290. while (sqlite3_step(statement) == SQLITE_ROW) {
  3291. char *rep = (char *)sqlite3_column_text(statement, 0);
  3292. if (rep == NULL) {
  3293. rep = "";
  3294. }
  3295. ret = [NSString stringWithUTF8String:rep];
  3296. }
  3297. sqlite3_finalize(statement);
  3298. }
  3299. [iSalesDB close_db:db];
  3300. return ret;
  3301. }
  3302. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  3303. char *tx = (char *)sqlite3_column_text(stmt, col);
  3304. if (tx == NULL) {
  3305. tx = "";
  3306. }
  3307. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  3308. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  3309. if (!text) {
  3310. text = @"";
  3311. }
  3312. // 将字符全部为' '的字符串干掉
  3313. int spaceCount = 0;
  3314. for (int i = 0; i < text.length; i++) {
  3315. if ([text characterAtIndex:i] == ' ') {
  3316. spaceCount++;
  3317. }
  3318. }
  3319. if (spaceCount == text.length) {
  3320. text = @"";
  3321. }
  3322. return text;
  3323. }
  3324. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  3325. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3326. NSData *data = [NSData dataWithContentsOfFile:path];
  3327. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3328. return ret;
  3329. }
  3330. + (NSString *)textFileName:(NSString *)name {
  3331. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  3332. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  3333. if (!text) {
  3334. text = @"";
  3335. }
  3336. return text;
  3337. }
  3338. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  3339. return [[dic objectForKey:key] mutableCopy];
  3340. }
  3341. + (id)translateSingleQuote:(NSString *)string {
  3342. if ([string isKindOfClass:[NSString class]])
  3343. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3344. return string;
  3345. }
  3346. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  3347. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  3348. }
  3349. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  3350. NSString* ret= nil;
  3351. NSString *sqlQuery = nil;
  3352. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  3353. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ order by i.default_img desc, i._id asc limit 1;",item_id];
  3354. sqlite3_stmt * statement;
  3355. // int count=0;
  3356. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3357. {
  3358. if (sqlite3_step(statement) == SQLITE_ROW)
  3359. {
  3360. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  3361. if(imgurl==nil)
  3362. imgurl="";
  3363. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  3364. ret=nsimgurl;
  3365. }
  3366. sqlite3_finalize(statement);
  3367. }
  3368. else
  3369. {
  3370. [ret setValue:@"8" forKey:@"result"];
  3371. }
  3372. // [iSalesDB close_db:db];
  3373. // DebugLog(@"data string: %@",ret );
  3374. return ret;
  3375. }
  3376. #pragma mark contact list
  3377. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  3378. {
  3379. // contactType = "Sales_Order_Customer";
  3380. // limit = 25;
  3381. // offset = 0;
  3382. // password = 123456;
  3383. // "price_name" = 16;
  3384. // user = EvanK;
  3385. sqlite3 *db = [iSalesDB get_db];
  3386. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  3387. if (contactType) {
  3388. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  3389. } else {
  3390. contactType = @"1 = 1";
  3391. }
  3392. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  3393. DebugLog(@"offline contact list keyword: %@",keyword);
  3394. // advanced search
  3395. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  3396. if (contact_name) {
  3397. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3398. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  3399. } else {
  3400. contact_name = @"";
  3401. }
  3402. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  3403. if (customer_phone) {
  3404. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3405. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  3406. } else {
  3407. customer_phone = @"";
  3408. }
  3409. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  3410. if (customer_fax) {
  3411. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3412. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3413. } else {
  3414. customer_fax = @"";
  3415. }
  3416. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3417. if (customer_zipcode) {
  3418. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3419. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3420. } else {
  3421. customer_zipcode = @"";
  3422. }
  3423. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3424. if (customer_sales_rep) {
  3425. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3426. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3427. } else {
  3428. customer_sales_rep = @"";
  3429. }
  3430. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3431. if (customer_state) {
  3432. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3433. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3434. } else {
  3435. customer_state = @"";
  3436. }
  3437. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3438. if (customer_name) {
  3439. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3440. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3441. } else {
  3442. customer_name = @"";
  3443. }
  3444. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3445. if (customer_country) {
  3446. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3447. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3448. } else {
  3449. customer_country = @"";
  3450. }
  3451. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3452. if (customer_cid) {
  3453. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3454. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3455. } else {
  3456. customer_cid = @"";
  3457. }
  3458. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3459. if (customer_city) {
  3460. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3461. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3462. } else {
  3463. customer_city = @"";
  3464. }
  3465. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3466. if (customer_address) {
  3467. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3468. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3469. } else {
  3470. customer_address = @"";
  3471. }
  3472. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3473. if (customer_email) {
  3474. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3475. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3476. } else {
  3477. customer_email = @"";
  3478. }
  3479. NSString *price_name = [params valueForKey:@"price_name"];
  3480. if (price_name) {
  3481. if ([price_name containsString:@","]) {
  3482. // 首先从 price表中查处name
  3483. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3484. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3485. for (int i = 1;i < pArray.count;i++) {
  3486. NSString *p = pArray[i];
  3487. [mutablePStr appendFormat:@" or type = %@ ",p];
  3488. }
  3489. [mutablePStr appendString:@";"];
  3490. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3491. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3492. char *name = (char *)sqlite3_column_text(stmt, 0);
  3493. if (!name)
  3494. name = "";
  3495. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3496. }];
  3497. // 再根据name 拼sql
  3498. NSMutableString *mutable_price_name = [NSMutableString string];
  3499. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3500. for (int i = 1; i < price_name_array.count; i++) {
  3501. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3502. }
  3503. [mutable_price_name appendString:@")"];
  3504. price_name = mutable_price_name;
  3505. } else {
  3506. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3507. if ([price_name isEqualToString:@""]) {
  3508. price_name = @"";
  3509. } else {
  3510. __block NSString *price;
  3511. price_name = [self translateSingleQuote:price_name];
  3512. [iSalesDB jk_query:[NSString stringWithFormat:@"SELECT name FROM price where type = %@ ;",price_name] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3513. char *p = (char *)sqlite3_column_text(stmt, 0);
  3514. if (p == NULL) {
  3515. p = "";
  3516. }
  3517. price = [NSString stringWithUTF8String:p];
  3518. }];
  3519. if ([price isEqualToString:@""]) {
  3520. price_name = @"";
  3521. } else {
  3522. price = [self translateSingleQuote:price];
  3523. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3524. }
  3525. }
  3526. }
  3527. } else {
  3528. price_name = @"";
  3529. }
  3530. int limit = [[params valueForKey:@"limit"] intValue];
  3531. int offset = [[params valueForKey:@"offset"] intValue];
  3532. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3533. NSString *where = [NSString stringWithFormat:@" %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ and is_active=1 ",contactType,contact_name,customer_phone,customer_fax,customer_zipcode,customer_sales_rep,customer_state,customer_name,customer_country,customer_cid,customer_city,customer_address,customer_email,price_name];
  3534. NSString *sql = [NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ order by decrypt(company_name) limit %d offset %d",where,limit,offset];
  3535. // int result= [iSalesDB AddExFunction:db];
  3536. int count =0;
  3537. NSString *sqlQuery = nil;
  3538. if(keyword.length==0)
  3539. {
  3540. // sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  3541. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3542. sqlQuery = sql;
  3543. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3544. }
  3545. else
  3546. {
  3547. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  3548. keyword = keyword.lowercaseString;
  3549. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3550. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%' or lower(decrypt(phone)) like '%%%@%%' or lower(decrypt(addr)) like '%%%@%%') and is_active=1 order by decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,keyword,keyword,limit, offset];
  3551. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@=1 and (lower(contact_id ) like'%@' or lower(company_name) like'%@') and is_active=1",contactType,keyword,keyword]];
  3552. }
  3553. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3554. sqlite3_stmt * statement;
  3555. [ret setValue:@"2" forKey:@"result"];
  3556. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  3557. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3558. {
  3559. int i = 0;
  3560. while (sqlite3_step(statement) == SQLITE_ROW)
  3561. {
  3562. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3563. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3564. int editable = sqlite3_column_int(statement, 0);
  3565. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3566. NSString *nscompany_name =nil;
  3567. if(company_name==nil)
  3568. nscompany_name=@"";
  3569. else
  3570. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  3571. char *country = (char*)sqlite3_column_text(statement, 2);
  3572. if(country==nil)
  3573. country="";
  3574. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3575. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3576. // if(addr==nil)
  3577. // addr="";
  3578. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3579. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3580. if(zipcode==nil)
  3581. zipcode="";
  3582. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3583. char *state = (char*)sqlite3_column_text(statement, 5);
  3584. if(state==nil)
  3585. state="";
  3586. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3587. char *city = (char*)sqlite3_column_text(statement, 6);
  3588. if(city==nil)
  3589. city="";
  3590. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3591. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3592. // NSString *nscontact_name = nil;
  3593. // if(contact_name==nil)
  3594. // nscontact_name=@"";
  3595. // else
  3596. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3597. char *phone = (char*)sqlite3_column_text(statement, 8);
  3598. NSString *nsphone = nil;
  3599. if(phone==nil)
  3600. nsphone=@"";
  3601. else
  3602. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3603. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3604. if(contact_id==nil)
  3605. contact_id="";
  3606. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3607. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3608. if(addr_1==nil)
  3609. addr_1="";
  3610. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3611. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3612. if(addr_2==nil)
  3613. addr_2="";
  3614. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3615. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3616. if(addr_3==nil)
  3617. addr_3="";
  3618. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3619. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3620. if(addr_4==nil)
  3621. addr_4="";
  3622. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3623. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3624. if(first_name==nil)
  3625. first_name="";
  3626. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3627. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3628. if(last_name==nil)
  3629. last_name="";
  3630. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3631. char *fax = (char*)sqlite3_column_text(statement, 16);
  3632. NSString *nsfax = nil;
  3633. if(fax==nil)
  3634. nsfax=@"";
  3635. else
  3636. {
  3637. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3638. if(nsfax.length>0)
  3639. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  3640. }
  3641. char *email = (char*)sqlite3_column_text(statement, 17);
  3642. NSString *nsemail = nil;
  3643. if(email==nil)
  3644. nsemail=@"";
  3645. else
  3646. {
  3647. nsemail= [[NSString alloc]initWithUTF8String:email];
  3648. if(nsemail.length>0)
  3649. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  3650. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  3651. }
  3652. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3653. [arr_name addObject:nsfirst_name];
  3654. [arr_name addObject:nslast_name];
  3655. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3656. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3657. {
  3658. // decrypt
  3659. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3660. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3661. nsphone=[AESCrypt fastdecrypt:nsphone];
  3662. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3663. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3664. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3665. }
  3666. [arr_addr addObject:nscompany_name];
  3667. [arr_addr addObject:nscontact_name];
  3668. [arr_addr addObject:@"<br>"];
  3669. [arr_addr addObject:nsaddr_1];
  3670. [arr_addr addObject:nsaddr_2];
  3671. [arr_addr addObject:nsaddr_3];
  3672. [arr_addr addObject:nsaddr_4];
  3673. //[arr_addr addObject:nsaddr];
  3674. [arr_addr addObject:nszipcode];
  3675. [arr_addr addObject:nscity];
  3676. [arr_addr addObject:nsstate];
  3677. [arr_addr addObject:nscountry];
  3678. [arr_addr addObject:@"<br>"];
  3679. [arr_addr addObject:nsphone];
  3680. [arr_addr addObject:nsfax];
  3681. [arr_addr addObject:nsemail];
  3682. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  3683. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  3684. [item setValue:name forKey:@"name"];
  3685. [item setValue:nscontact_id forKey:@"contact_id"];
  3686. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  3687. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3688. i++;
  3689. }
  3690. UIApplication * app = [UIApplication sharedApplication];
  3691. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3692. [ret setValue:appDelegate.mode forKey:@"mode"];
  3693. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  3694. sqlite3_finalize(statement);
  3695. }
  3696. [iSalesDB close_db:db];
  3697. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3698. return ret;
  3699. }
  3700. #pragma mark contact Advanced search
  3701. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  3702. {
  3703. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  3704. return [RAUtils dict2data:contactAdvanceDic];
  3705. }
  3706. #pragma mark create new contact
  3707. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  3708. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  3709. NSData *data = [NSData dataWithContentsOfFile:path];
  3710. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3711. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3712. NSString *countryCode = nil;
  3713. NSString *countryCode_id = nil;
  3714. NSString *stateCode = nil;
  3715. NSString *city = nil;
  3716. NSString *zipCode = nil;
  3717. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3718. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  3719. NSString *code_id = params[@"country"];
  3720. countryCode_id = code_id;
  3721. countryCode = [self countryCodeByid:code_id];
  3722. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  3723. NSString *zip_code = params[@"zipcode"];
  3724. // 剔除全部为空格
  3725. int spaceCount = 0;
  3726. for (int i = 0; i < zip_code.length; i++) {
  3727. if ([zip_code characterAtIndex:i] == ' ') {
  3728. spaceCount++;
  3729. }
  3730. }
  3731. if (spaceCount == zip_code.length) {
  3732. zip_code = @"";
  3733. }
  3734. zipCode = zip_code;
  3735. if (zipCode.length > 0) {
  3736. countryCode_id = params[@"country"];
  3737. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3738. countryCode = [dic valueForKey:@"country_code"];
  3739. if (!countryCode) {
  3740. countryCode = @"US";
  3741. }
  3742. stateCode = [dic valueForKey:@"state_code"];
  3743. city = [dic valueForKey:@"city"];
  3744. // zip code
  3745. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  3746. [zipDic setValue:zipCode forKey:@"value"];
  3747. [section_0 setValue:zipDic forKey:@"item_10"];
  3748. }
  3749. }
  3750. } else {
  3751. // default: US United States
  3752. countryCode = @"US";
  3753. countryCode_id = @"228";
  3754. }
  3755. // country
  3756. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3757. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  3758. // state
  3759. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3760. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  3761. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  3762. // city
  3763. if (city) {
  3764. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  3765. [cityDic setValue:city forKey:@"value"];
  3766. [section_0 setValue:cityDic forKey:@"item_12"];
  3767. }
  3768. // price type
  3769. NSDictionary *priceDic = [self offline_getPrice];
  3770. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  3771. // Sales Rep
  3772. NSDictionary *repDic = [self offline_getSalesRep];
  3773. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3774. [ret setValue:section_0 forKey:@"section_0"];
  3775. return [RAUtils dict2data:ret];
  3776. }
  3777. #pragma mark save
  3778. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  3779. NSString *addr = nil;
  3780. NSString *contact_name = nil;
  3781. NSString *companyName = [params objectForKey:@"company"];
  3782. if (companyName) {
  3783. companyName = [AESCrypt fastencrypt:companyName];
  3784. } else {
  3785. companyName = @"";
  3786. }
  3787. DebugLog(@"company");
  3788. companyName = [self translateSingleQuote:companyName];
  3789. NSString *addr1 = [params objectForKey:@"address"];
  3790. NSString *addr2 = [params objectForKey:@"address2"];
  3791. NSString *addr3 = [params objectForKey:@"address_3"];
  3792. NSString *addr4 = [params objectForKey:@"address_4"];
  3793. if (!addr2) {
  3794. addr2 = @"";
  3795. }
  3796. if (!addr3) {
  3797. addr3 = @"";
  3798. }
  3799. if (!addr4) {
  3800. addr4 = @"";
  3801. }
  3802. if (!addr1) {
  3803. addr1 = @"";
  3804. }
  3805. DebugLog(@"addr");
  3806. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  3807. addr = [AESCrypt fastencrypt:addr];
  3808. addr = [self translateSingleQuote:addr];
  3809. if (addr1 && ![addr1 isEqualToString:@""]) {
  3810. addr1 = [AESCrypt fastencrypt:addr1];
  3811. }
  3812. addr1 = [self translateSingleQuote:addr1];
  3813. addr2 = [self translateSingleQuote:addr2];
  3814. addr3 = [self translateSingleQuote:addr3];
  3815. addr4 = [self translateSingleQuote:addr4];
  3816. NSString *country = [params objectForKey:@"country"];
  3817. if (country) {
  3818. country = [self countryNameByCountryCodeId:country];
  3819. } else {
  3820. country = @"";
  3821. }
  3822. DebugLog(@"country");
  3823. country = [self translateSingleQuote:country];
  3824. NSString *state = [params objectForKey:@"state"];
  3825. if (!state) {
  3826. state = @"";
  3827. }
  3828. DebugLog(@"state");
  3829. state = [self translateSingleQuote:state];
  3830. NSString *city = [params objectForKey:@"city"];
  3831. if (!city) {
  3832. city = @"";
  3833. }
  3834. city = [self translateSingleQuote:city];
  3835. NSString *zipcode = [params objectForKey:@"zipcode"];
  3836. if (!zipcode) {
  3837. zipcode = @"";
  3838. }
  3839. DebugLog(@"zip");
  3840. zipcode = [self translateSingleQuote:zipcode];
  3841. NSString *fistName = [params objectForKey:@"firstname"];
  3842. if (!fistName) {
  3843. fistName = @"";
  3844. }
  3845. NSString *lastName = [params objectForKey:@"lastname"];
  3846. if (!lastName) {
  3847. lastName = @"";
  3848. }
  3849. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  3850. DebugLog(@"contact_name");
  3851. contact_name = [self translateSingleQuote:contact_name];
  3852. fistName = [self translateSingleQuote:fistName];
  3853. lastName = [self translateSingleQuote:lastName];
  3854. NSString *phone = [params objectForKey:@"phone"];
  3855. if (phone) {
  3856. phone = [AESCrypt fastencrypt:phone];
  3857. } else {
  3858. phone = @"";
  3859. }
  3860. DebugLog(@"PHONE");
  3861. phone = [self translateSingleQuote:phone];
  3862. NSString *fax = [params objectForKey:@"fax"];
  3863. if (!fax) {
  3864. fax = @"";
  3865. }
  3866. DebugLog(@"FAX");
  3867. fax = [self translateSingleQuote:fax];
  3868. NSString *email = [params objectForKey:@"email"];
  3869. if (!email) {
  3870. email = @"";
  3871. }
  3872. DebugLog(@"EMAIL:%@",email);
  3873. email = [self translateSingleQuote:email];
  3874. NSString *notes = [params objectForKey:@"contact_notes"];
  3875. if (!notes) {
  3876. notes = @"";
  3877. }
  3878. DebugLog(@"NOTE:%@",notes);
  3879. notes = [self translateSingleQuote:notes];
  3880. NSString *price = [params objectForKey:@"price_name"];
  3881. if (price) {
  3882. price = [self priceNameByPriceId:price];
  3883. } else {
  3884. price = @"";
  3885. }
  3886. DebugLog(@"PRICE");
  3887. price = [self translateSingleQuote:price];
  3888. NSString *salesRep = [params objectForKey:@"sales_rep"];
  3889. if (salesRep) {
  3890. salesRep = [self salesRepCodeById:salesRep];
  3891. } else {
  3892. salesRep = @"";
  3893. }
  3894. salesRep = [self translateSingleQuote:salesRep];
  3895. NSString *img = [params objectForKey:@"business_card"];
  3896. NSArray *array = [img componentsSeparatedByString:@","];
  3897. NSString *img_0 = array[0];
  3898. if (!img_0) {
  3899. img_0 = @"";
  3900. }
  3901. img_0 = [self translateSingleQuote:img_0];
  3902. NSString *img_1 = array[1];
  3903. if (!img_1) {
  3904. img_1 = @"";
  3905. }
  3906. img_1 = [self translateSingleQuote:img_1];
  3907. NSString *img_2 = array[2];
  3908. if (!img_2) {
  3909. img_2 = @"";
  3910. }
  3911. img_2 = [self translateSingleQuote:img_2];
  3912. NSString *contact_id = [NSUUID UUID].UUIDString;
  3913. // 判断更新时是否为customer
  3914. if (update) {
  3915. contact_id = [params objectForKey:@"contact_id"];
  3916. if (!contact_id) {
  3917. contact_id = @"";
  3918. }
  3919. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  3920. __block int customer = 0;
  3921. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3922. customer = sqlite3_column_int(stmt, 0);
  3923. }];
  3924. isCustomer = customer ? YES : NO;
  3925. }
  3926. NSMutableDictionary *sync_dic = [params mutableCopy];
  3927. if (isCustomer) {
  3928. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  3929. } else {
  3930. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  3931. }
  3932. NSString *sync_data = nil;
  3933. NSString *sql = nil;
  3934. if (update){
  3935. contact_id = [params objectForKey:@"contact_id"];
  3936. if (!contact_id) {
  3937. contact_id = @"";
  3938. }
  3939. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3940. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3941. sync_data = [RAUtils dict2string:sync_dic];
  3942. sync_data = [self translateSingleQuote:sync_data];
  3943. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_id];
  3944. } else {
  3945. contact_id = [self translateSingleQuote:contact_id];
  3946. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3947. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3948. sync_data = [RAUtils dict2string:sync_dic];
  3949. sync_data = [self translateSingleQuote:sync_data];
  3950. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr];
  3951. }
  3952. int result = [iSalesDB execSql:sql];
  3953. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  3954. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  3955. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  3956. }
  3957. #pragma mark save new contact
  3958. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  3959. {
  3960. return [self offline_saveContact:params update:NO isCustomer:YES];
  3961. }
  3962. #pragma mark edit contact
  3963. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  3964. {
  3965. // {
  3966. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  3967. // password = 123456;
  3968. // user = EvanK;
  3969. // }
  3970. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  3971. NSData *data = [NSData dataWithContentsOfFile:path];
  3972. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3973. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3974. NSString *countryCode = nil;
  3975. NSString *countryCode_id = nil;
  3976. NSString *stateCode = nil;
  3977. /*------contact infor------*/
  3978. __block NSString *country = nil;
  3979. __block NSString *company_name = nil;
  3980. __block NSString *contact_id = params[@"contact_id"];
  3981. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  3982. __block NSString *zipcode = nil;
  3983. __block NSString *state = nil; // state_code
  3984. __block NSString *city = nil; //
  3985. __block NSString *firt_name,*last_name;
  3986. __block NSString *phone,*fax,*email;
  3987. __block NSString *notes,*price_type,*sales_rep;
  3988. __block NSString *img_0,*img_1,*img_2;
  3989. contact_id = [self translateSingleQuote:contact_id];
  3990. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2 from offline_contact where contact_id = '%@';",contact_id];
  3991. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3992. country = [self textAtColumn:0 statement:stmt]; // country name
  3993. company_name = [self textAtColumn:1 statement:stmt];
  3994. addr_1 = [self textAtColumn:2 statement:stmt];
  3995. addr_2 = [self textAtColumn:3 statement:stmt];
  3996. addr_3 = [self textAtColumn:4 statement:stmt];
  3997. addr_4 = [self textAtColumn:5 statement:stmt];
  3998. zipcode = [self textAtColumn:6 statement:stmt];
  3999. state = [self textAtColumn:7 statement:stmt]; // state code
  4000. city = [self textAtColumn:8 statement:stmt];
  4001. firt_name = [self textAtColumn:9 statement:stmt];
  4002. last_name = [self textAtColumn:10 statement:stmt];
  4003. phone = [self textAtColumn:11 statement:stmt];
  4004. fax = [self textAtColumn:12 statement:stmt];
  4005. email = [self textAtColumn:13 statement:stmt];
  4006. notes = [self textAtColumn:14 statement:stmt];
  4007. price_type = [self textAtColumn:15 statement:stmt]; // name
  4008. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  4009. img_0 = [self textAtColumn:17 statement:stmt];
  4010. img_1 = [self textAtColumn:18 statement:stmt];
  4011. img_2 = [self textAtColumn:19 statement:stmt];
  4012. }];
  4013. // decrypt
  4014. if (company_name) {
  4015. company_name = [AESCrypt fastdecrypt:company_name];
  4016. }
  4017. if (addr_1) {
  4018. addr_1 = [AESCrypt fastdecrypt:addr_1];
  4019. }
  4020. if (phone) {
  4021. phone = [AESCrypt fastdecrypt:phone];
  4022. }
  4023. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  4024. countryCode = [iSalesDB jk_queryText:countrySql];
  4025. stateCode = state;
  4026. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4027. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  4028. NSString *code_id = params[@"country"];
  4029. countryCode_id = code_id;
  4030. countryCode = [self countryCodeByid:code_id];
  4031. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  4032. NSString *zip_code = params[@"zipcode"];
  4033. // 剔除全部为空格
  4034. int spaceCount = 0;
  4035. for (int i = 0; i < zip_code.length; i++) {
  4036. if ([zip_code characterAtIndex:i] == ' ') {
  4037. spaceCount++;
  4038. }
  4039. }
  4040. if (spaceCount == zip_code.length) {
  4041. zip_code = @"";
  4042. }
  4043. if (zipcode.length > 0) {
  4044. zipcode = zip_code;
  4045. countryCode_id = params[@"country"];
  4046. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4047. countryCode = [dic valueForKey:@"country_code"];
  4048. stateCode = [dic valueForKey:@"state_code"];
  4049. city = [dic valueForKey:@"city"];
  4050. // zip code
  4051. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  4052. [zipDic setValue:zipcode forKey:@"value"];
  4053. [section_0 setValue:zipDic forKey:@"item_8"];
  4054. }
  4055. }
  4056. }
  4057. // 0 Country
  4058. // 1 Company Name
  4059. // 2 Contact ID
  4060. // 3 Picture
  4061. // 4 Address 1
  4062. // 5 Address 2
  4063. // 6 Address 3
  4064. // 7 Address 4
  4065. // 8 Zip Code
  4066. // 9 State/Province
  4067. // 10 City
  4068. // 11 Contact First Name
  4069. // 12 Contact Last Name
  4070. // 13 Phone
  4071. // 14 Fax
  4072. // 15 Email
  4073. // 16 Contact Notes
  4074. // 17 Price Type
  4075. // 18 Sales Rep
  4076. // country
  4077. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4078. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  4079. // company
  4080. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  4081. // contact_id
  4082. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  4083. // picture
  4084. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  4085. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  4086. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  4087. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  4088. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  4089. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  4090. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  4091. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  4092. // addr 1 2 3 4
  4093. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  4094. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  4095. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  4096. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  4097. // zip code
  4098. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  4099. // state
  4100. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4101. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  4102. // city
  4103. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  4104. // first last
  4105. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  4106. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  4107. // phone fax email
  4108. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  4109. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  4110. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  4111. // notes
  4112. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  4113. // price
  4114. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  4115. for (NSString *key in priceDic.allKeys) {
  4116. if ([key containsString:@"val_"]) {
  4117. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  4118. if ([dic[@"value"] isEqualToString:price_type]) {
  4119. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4120. [priceDic setValue:dic forKey:key];
  4121. }
  4122. }
  4123. }
  4124. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  4125. // Sales Rep
  4126. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  4127. for (NSString *key in repDic.allKeys) {
  4128. if ([key containsString:@"val_"]) {
  4129. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  4130. NSString *value = dic[@"value"];
  4131. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  4132. if (code && [code isEqualToString:sales_rep]) {
  4133. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4134. [repDic setValue:dic forKey:key];
  4135. }
  4136. }
  4137. }
  4138. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4139. [ret setValue:section_0 forKey:@"section_0"];
  4140. return [RAUtils dict2data:ret];
  4141. }
  4142. #pragma mark save contact
  4143. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  4144. {
  4145. return [self offline_saveContact:params update:YES isCustomer:YES];
  4146. }
  4147. #pragma mark category
  4148. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4149. if (ck) {
  4150. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  4151. for (NSString *key in res.allKeys) {
  4152. if (![key isEqualToString:@"count"]) {
  4153. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  4154. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4155. if ([val[@"value"] isEqualToString:ck]) {
  4156. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4157. }
  4158. [res setValue:val forKey:key];
  4159. }
  4160. }
  4161. [dic setValue:res forKey:valueKey];
  4162. }
  4163. }
  4164. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  4165. // NSString* orderCode = [params valueForKey:@"orderCode"];
  4166. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4167. NSString* category = [params valueForKey:@"category"];
  4168. if (!category || [category isEqualToString:@""]) {
  4169. category = @"%";
  4170. }
  4171. category = [self translateSingleQuote:category];
  4172. int limit = [[params valueForKey:@"limit"] intValue];
  4173. int offset = [[params valueForKey:@"offset"] intValue];
  4174. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4175. NSString *limit_str = @"";
  4176. if (limited) {
  4177. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  4178. }
  4179. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4180. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4181. sqlite3 *db = [iSalesDB get_db];
  4182. // [iSalesDB AddExFunction:db];
  4183. int count;
  4184. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  4185. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  4186. double price_min = 0;
  4187. double price_max = 0;
  4188. if ([params.allKeys containsObject:@"alert"]) {
  4189. // alert
  4190. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4191. NSString *alert = params[@"alert"];
  4192. if ([alert isEqualToString:@"Display All"]) {
  4193. alert = [NSString stringWithFormat:@""];
  4194. } else {
  4195. alert = [self translateSingleQuote:alert];
  4196. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4197. }
  4198. // available
  4199. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4200. NSString *available = params[@"available"];
  4201. NSString *available_condition;
  4202. if ([available isEqualToString:@"Display All"]) {
  4203. available_condition = @"";
  4204. } else if ([available isEqualToString:@"Available Now"]) {
  4205. available_condition = @"and availability > 0";
  4206. } else {
  4207. available_condition = @"and availability == 0";
  4208. }
  4209. // best seller
  4210. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4211. NSString *best_seller = @"";
  4212. NSString *order_best_seller = @"m.name asc";
  4213. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4214. best_seller = @"and best_seller > 0";
  4215. order_best_seller = @"m.best_seller desc,m.name asc";
  4216. }
  4217. // price
  4218. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4219. NSString *price = params[@"price"];
  4220. price_min = 0;
  4221. price_max = MAXFLOAT;
  4222. if (appDelegate.user) {
  4223. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4224. NSMutableString *priceName = [NSMutableString string];
  4225. for (int i = 0; i < priceTypeArray.count; i++) {
  4226. NSString *pricetype = priceTypeArray[i];
  4227. pricetype = [self translateSingleQuote:pricetype];
  4228. if (i == 0) {
  4229. [priceName appendFormat:@"'%@'",pricetype];
  4230. } else {
  4231. [priceName appendFormat:@",'%@'",pricetype];
  4232. }
  4233. }
  4234. if ([price isEqualToString:@"Display All"]) {
  4235. price = [NSString stringWithFormat:@""];
  4236. } else if([price containsString:@"+"]){
  4237. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4238. price_min = [price doubleValue];
  4239. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  4240. } else {
  4241. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4242. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4243. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4244. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  4245. }
  4246. } else {
  4247. price = @"";
  4248. }
  4249. // sold_by_qty : Sold in quantities of %@
  4250. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4251. NSString *qty = params[@"sold_by_qty"];
  4252. if ([qty isEqualToString:@"Display All"]) {
  4253. qty = @"";
  4254. } else {
  4255. qty = [self translateSingleQuote:qty];
  4256. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4257. }
  4258. // cate
  4259. category = [self translateSingleQuote:category];
  4260. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  4261. // where bestseller > 0 order by bestseller desc
  4262. // sql query: alert availability(int) best_seller(int) price qty
  4263. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  4264. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4265. }
  4266. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4267. if (!appDelegate.user) {
  4268. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4269. }
  4270. [ret setValue:filter forKey:@"filter"];
  4271. DebugLog(@"offline_category sql:%@",sqlQuery);
  4272. sqlite3_stmt * statement;
  4273. [ret setValue:@"2" forKey:@"result"];
  4274. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4275. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4276. // int count=0;
  4277. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4278. {
  4279. int i=0;
  4280. while (sqlite3_step(statement) == SQLITE_ROW)
  4281. {
  4282. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4283. char *name = (char*)sqlite3_column_text(statement, 0);
  4284. if(name==nil)
  4285. name="";
  4286. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4287. char *description = (char*)sqlite3_column_text(statement, 1);
  4288. if(description==nil)
  4289. description="";
  4290. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4291. int product_id = sqlite3_column_int(statement, 2);
  4292. int wid = sqlite3_column_int(statement, 3);
  4293. int closeout = sqlite3_column_int(statement, 4);
  4294. int cid = sqlite3_column_int(statement, 5);
  4295. int wisdelete = sqlite3_column_int(statement, 6);
  4296. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4297. if(wid !=0 && wisdelete != 1)
  4298. [item setValue:@"true" forKey:@"wish_exists"];
  4299. else
  4300. [item setValue:@"false" forKey:@"wish_exists"];
  4301. if(closeout==0)
  4302. [item setValue:@"false" forKey:@"is_closeout"];
  4303. else
  4304. [item setValue:@"true" forKey:@"is_closeout"];
  4305. if(cid==0)
  4306. [item setValue:@"false" forKey:@"cart_exists"];
  4307. else
  4308. [item setValue:@"true" forKey:@"cart_exists"];
  4309. [item addEntriesFromDictionary:imgjson];
  4310. // [item setValue:nsurl forKey:@"img"];
  4311. [item setValue:nsname forKey:@"name"];
  4312. [item setValue:nsdescription forKey:@"description"];
  4313. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4314. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4315. i++;
  4316. }
  4317. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4318. [ret setObject:items forKey:@"items"];
  4319. sqlite3_finalize(statement);
  4320. } else {
  4321. DebugLog(@"nothing...");
  4322. }
  4323. DebugLog(@"count:%d",count);
  4324. [iSalesDB close_db:db];
  4325. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4326. return ret;
  4327. }
  4328. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  4329. {
  4330. return [self categoryList:params limited:YES];
  4331. }
  4332. # pragma mark item search
  4333. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  4334. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4335. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  4336. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  4337. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  4338. // category
  4339. NSDictionary *category_menu = [self offline_category_menu];
  4340. [filter setValue:category_menu forKey:@"category"];
  4341. NSString* where= nil;
  4342. NSString* orderby= @"m.name";
  4343. if (!filterSearch) {
  4344. int covertype = [[params valueForKey:@"covertype"] intValue];
  4345. switch (covertype) {
  4346. case 0:
  4347. {
  4348. where=@"m.category like'%%#005#%%'";
  4349. break;
  4350. }
  4351. case 1:
  4352. {
  4353. where=@"m.alert like '%QS%'";
  4354. break;
  4355. }
  4356. case 2:
  4357. {
  4358. where=@"m.availability>0";
  4359. break;
  4360. }
  4361. case 3:
  4362. {
  4363. where=@"m.best_seller>0";
  4364. orderby=@"m.best_seller desc,m.name asc";
  4365. break;
  4366. }
  4367. default:
  4368. where=@"1=1";
  4369. break;
  4370. }
  4371. }
  4372. int limit = [[params valueForKey:@"limit"] intValue];
  4373. int offset = [[params valueForKey:@"offset"] intValue];
  4374. NSString *limit_str = @"";
  4375. if (limited) {
  4376. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  4377. }
  4378. sqlite3 *db = [iSalesDB get_db];
  4379. // [iSalesDB AddExFunction:db];
  4380. int count;
  4381. NSString *sqlQuery = nil;
  4382. where = [where stringByAppendingString:@" and m.is_active = 1"];
  4383. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  4384. double price_min = 0;
  4385. double price_max = 0;
  4386. if (filterSearch) {
  4387. // alert
  4388. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  4389. NSString *alert = params[@"alert"];
  4390. if ([alert isEqualToString:@"Display All"]) {
  4391. alert = [NSString stringWithFormat:@""];
  4392. } else {
  4393. alert = [self translateSingleQuote:alert];
  4394. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  4395. }
  4396. // available
  4397. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  4398. NSString *available = params[@"available"];
  4399. NSString *available_condition;
  4400. if ([available isEqualToString:@"Display All"]) {
  4401. available_condition = @"";
  4402. } else if ([available isEqualToString:@"Available Now"]) {
  4403. available_condition = @"and availability > 0";
  4404. } else {
  4405. available_condition = @"and availability == 0";
  4406. }
  4407. // best seller
  4408. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  4409. NSString *best_seller = @"";
  4410. NSString *order_best_seller = @"m.name asc";
  4411. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4412. best_seller = @"and best_seller > 0";
  4413. order_best_seller = @"m.best_seller desc,m.name asc";
  4414. }
  4415. // price
  4416. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4417. NSString *price = params[@"price"];
  4418. price_min = 0;
  4419. price_max = MAXFLOAT;
  4420. if (appDelegate.user) {
  4421. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4422. NSMutableString *priceName = [NSMutableString string];
  4423. for (int i = 0; i < priceTypeArray.count; i++) {
  4424. NSString *pricetype = priceTypeArray[i];
  4425. pricetype = [self translateSingleQuote:pricetype];
  4426. if (i == 0) {
  4427. [priceName appendFormat:@"'%@'",pricetype];
  4428. } else {
  4429. [priceName appendFormat:@",'%@'",pricetype];
  4430. }
  4431. }
  4432. if ([price isEqualToString:@"Display All"]) {
  4433. price = [NSString stringWithFormat:@""];
  4434. } else if([price containsString:@"+"]){
  4435. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4436. price_min = [price doubleValue];
  4437. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  4438. } else {
  4439. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4440. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4441. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4442. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  4443. }
  4444. } else {
  4445. price = @"";
  4446. }
  4447. // sold_by_qty : Sold in quantities of %@
  4448. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4449. NSString *qty = params[@"sold_by_qty"];
  4450. if ([qty isEqualToString:@"Display All"]) {
  4451. qty = @"";
  4452. } else {
  4453. qty = [self translateSingleQuote:qty];
  4454. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4455. }
  4456. // category
  4457. NSString *category_id = params[@"ctgId"];
  4458. NSMutableArray *cate_id_array = nil;
  4459. NSMutableString *cateWhere = [NSMutableString string];
  4460. if ([category_id isEqualToString:@""] || !category_id) {
  4461. [cateWhere appendString:@"1 = 1"];
  4462. } else {
  4463. if ([category_id containsString:@","]) {
  4464. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4465. } else {
  4466. cate_id_array = [@[category_id] mutableCopy];
  4467. }
  4468. /*-----------*/
  4469. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4470. for (int i = 0; i < cate_id_array.count; i++) {
  4471. for (NSString *key0 in cateDic.allKeys) {
  4472. if ([key0 containsString:@"category_"]) {
  4473. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4474. for (NSString *key1 in category0.allKeys) {
  4475. if ([key1 containsString:@"category_"]) {
  4476. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4477. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4478. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4479. cate_id_array[i] = [category1 objectForKey:@"id"];
  4480. if (i == 0) {
  4481. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4482. } else {
  4483. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4484. }
  4485. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4486. [category0 setValue:category1 forKey:key1];
  4487. [cateDic setValue:category0 forKey:key0];
  4488. }
  4489. }
  4490. }
  4491. }
  4492. }
  4493. }
  4494. [filter setValue:cateDic forKey:@"category"];
  4495. }
  4496. // where bestseller > 0 order by bestseller desc
  4497. // sql query: alert availability(int) best_seller(int) price qty
  4498. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  4499. // count
  4500. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4501. }
  4502. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4503. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4504. if (!appDelegate.user) {
  4505. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4506. }
  4507. [ret setValue:filter forKey:@"filter"];
  4508. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4509. sqlite3_stmt * statement;
  4510. [ret setValue:@"2" forKey:@"result"];
  4511. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4512. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4513. // int count=0;
  4514. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4515. {
  4516. int i=0;
  4517. while (sqlite3_step(statement) == SQLITE_ROW)
  4518. {
  4519. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4520. char *name = (char*)sqlite3_column_text(statement, 0);
  4521. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4522. char *description = (char*)sqlite3_column_text(statement, 1);
  4523. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4524. int product_id = sqlite3_column_int(statement, 2);
  4525. int wid = sqlite3_column_int(statement, 3);
  4526. int closeout = sqlite3_column_int(statement, 4);
  4527. int cid = sqlite3_column_int(statement, 5);
  4528. int wisdelete = sqlite3_column_int(statement, 6);
  4529. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4530. if(wid !=0 && wisdelete != 1)
  4531. [item setValue:@"true" forKey:@"wish_exists"];
  4532. else
  4533. [item setValue:@"false" forKey:@"wish_exists"];
  4534. if(closeout==0)
  4535. [item setValue:@"false" forKey:@"is_closeout"];
  4536. else
  4537. [item setValue:@"true" forKey:@"is_closeout"];
  4538. if(cid==0)
  4539. [item setValue:@"false" forKey:@"cart_exists"];
  4540. else
  4541. [item setValue:@"true" forKey:@"cart_exists"];
  4542. [item addEntriesFromDictionary:imgjson];
  4543. // [item setValue:nsurl forKey:@"img"];
  4544. [item setValue:nsname forKey:@"fash_name"];
  4545. [item setValue:nsdescription forKey:@"description"];
  4546. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4547. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4548. i++;
  4549. }
  4550. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4551. [ret setObject:items forKey:@"items"];
  4552. sqlite3_finalize(statement);
  4553. }
  4554. [iSalesDB close_db:db];
  4555. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4556. return ret;
  4557. }
  4558. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  4559. {
  4560. return [self itemsearch:params limited:YES];
  4561. }
  4562. #pragma mark order detail
  4563. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  4564. if (str1.length == 0) {
  4565. str1 = @"&nbsp";
  4566. }
  4567. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  4568. return str;
  4569. }
  4570. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  4571. {
  4572. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  4573. [fromformatter setDateFormat:from];
  4574. NSDate *date = [fromformatter dateFromString:datetime];
  4575. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  4576. [toformatter setDateFormat:to];
  4577. NSString * ret = [toformatter stringFromDate:date];
  4578. return ret;
  4579. }
  4580. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  4581. // 把毫秒去掉
  4582. if ([dateTime containsString:@"."]) {
  4583. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  4584. }
  4585. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  4586. formatter.dateFormat = formate;
  4587. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  4588. NSDate *date = [formatter dateFromString:dateTime];
  4589. formatter.dateFormat = newFormate;
  4590. NSString *result = [formatter stringFromDate:date];
  4591. return result ? result : @"";
  4592. }
  4593. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  4594. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  4595. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  4596. }
  4597. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  4598. {
  4599. DebugLog(@"offline oderdetail params: %@",params);
  4600. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4601. int orderId = [params[@"orderId"] intValue];
  4602. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  4603. // decrypt card number and card security code
  4604. // NSString* sql=[NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,o.TotalCuft,o.TotalWeight,o.TotalCarton,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),o.credit_card_expiration,o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus from (select _id,so_id,poNumber,create_time,status,TotalCuft,TotalWeight,TotalCarton,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId ];
  4605. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  4606. sqlite3 *db = [iSalesDB get_db];
  4607. sqlite3_stmt * statement;
  4608. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  4609. NSString *nssoid = @"";
  4610. NSString* orderinfo = @"";
  4611. NSString *moreInfo = @"";
  4612. double handlingFee = 0;
  4613. double payments_and_credist = 0;
  4614. double totalPrice = 0;
  4615. double shippingFee = 0;
  4616. double lift_gate = 0;
  4617. NSString *customer_contact = @"";
  4618. NSString *customer_email = @"";
  4619. NSString *customer_fax = @"";
  4620. NSString *customer_phone = @"";
  4621. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  4622. {
  4623. if (sqlite3_step(statement) == SQLITE_ROW)
  4624. {
  4625. int order_id = sqlite3_column_int(statement, 0);
  4626. NSString *sign_url = [self textAtColumn:51 statement:statement];
  4627. ret[@"sign_url"] = sign_url;
  4628. customer_contact = [self textAtColumn:52 statement:statement];
  4629. customer_email = [self textAtColumn:53 statement:statement];
  4630. customer_phone = [self textAtColumn:54 statement:statement];
  4631. customer_fax = [self textAtColumn:55 statement:statement];
  4632. int offline_edit=sqlite3_column_int(statement, 56);
  4633. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  4634. char *soid = (char*)sqlite3_column_text(statement, 1);
  4635. if(soid==nil)
  4636. soid= "";
  4637. nssoid= [[NSString alloc]initWithUTF8String:soid];
  4638. // so#
  4639. ret[@"so#"] = nssoid;
  4640. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  4641. if(poNumber==nil)
  4642. poNumber= "";
  4643. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  4644. char *create_time = (char*)sqlite3_column_text(statement, 3);
  4645. if(create_time==nil)
  4646. create_time= "";
  4647. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4648. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  4649. int status = sqlite3_column_int(statement, 4);
  4650. int erpStatus = sqlite3_column_int(statement, 49);
  4651. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4652. // status
  4653. if (status > 1 && status != 3) {
  4654. status = erpStatus;
  4655. } else if (status == 3) {
  4656. status = 15;
  4657. }
  4658. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  4659. ret[@"order_status"] = nsstatus;
  4660. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4661. if(company_name==nil)
  4662. company_name= "";
  4663. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4664. // company name
  4665. ret[@"company_name"] = nscompany_name;
  4666. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  4667. if(customer_contact==nil)
  4668. customer_contact= "";
  4669. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  4670. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  4671. if(addr_1==nil)
  4672. addr_1="";
  4673. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4674. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  4675. if(addr_2==nil)
  4676. addr_2="";
  4677. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4678. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  4679. if(addr_3==nil)
  4680. addr_3="";
  4681. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4682. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  4683. if(addr_4==nil)
  4684. addr_4="";
  4685. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4686. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4687. [arr_addr addObject:nsaddr_1];
  4688. [arr_addr addObject:nsaddr_2];
  4689. [arr_addr addObject:nsaddr_3];
  4690. [arr_addr addObject:nsaddr_4];
  4691. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  4692. char *logist = (char*)sqlite3_column_text(statement, 11);
  4693. if(logist==nil)
  4694. logist= "";
  4695. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  4696. if (status == -11 || status == 10 || status == 11) {
  4697. nslogist = [self textAtColumn:59 statement:statement];
  4698. };
  4699. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  4700. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  4701. shipping = @"Shipping To Be Quoted";
  4702. } else {
  4703. shippingFee = sqlite3_column_double(statement, 12);
  4704. }
  4705. // Shipping
  4706. ret[@"Shipping"] = shipping;
  4707. int have_lift_gate = sqlite3_column_int(statement, 17);
  4708. lift_gate = sqlite3_column_double(statement, 13);
  4709. // Liftgate Fee(No loading dock)
  4710. if (!have_lift_gate) {
  4711. lift_gate = 0;
  4712. }
  4713. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  4714. if (sqlite3_column_int(statement, 57)) {
  4715. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  4716. }
  4717. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  4718. if(general_notes==nil)
  4719. general_notes= "";
  4720. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  4721. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  4722. if(internal_notes==nil)
  4723. internal_notes= "";
  4724. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  4725. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  4726. if(payment_type==nil)
  4727. payment_type= "";
  4728. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  4729. // order info
  4730. orderinfo = [self textFileName:@"order_info.html"];
  4731. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  4732. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  4733. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  4734. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  4735. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  4736. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  4737. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  4738. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  4739. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  4740. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  4741. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  4742. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  4743. NSString *payment = nil;
  4744. if([nspayment_type isEqualToString:@"Credit Card"])
  4745. {
  4746. payment = [self textFileName:@"creditcardpayment.html"];
  4747. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  4748. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  4749. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  4750. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  4751. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  4752. NSString *card_type = [self textAtColumn:42 statement:statement];
  4753. if (card_type.length > 0) { // 显示星号
  4754. card_type = @"****";
  4755. }
  4756. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  4757. if (card_number.length > 0 && card_number.length > 4) {
  4758. for (int i = 0; i < card_number.length - 4; i++) {
  4759. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  4760. }
  4761. } else {
  4762. card_number = @"";
  4763. }
  4764. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  4765. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  4766. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  4767. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  4768. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  4769. card_expiration = @"****";
  4770. }
  4771. NSString *card_city = [self textAtColumn:46 statement:statement];
  4772. NSString *card_state = [self textAtColumn:47 statement:statement];
  4773. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  4774. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  4775. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  4776. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  4777. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  4778. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  4779. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  4780. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  4781. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  4782. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  4783. }
  4784. else
  4785. {
  4786. payment=[self textFileName:@"normalpayment.html"];
  4787. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  4788. }
  4789. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  4790. ret[@"result"]= [NSNumber numberWithInt:2];
  4791. // more info
  4792. moreInfo = [self textFileName:@"more_info.html"];
  4793. /*****ship to******/
  4794. // ShipToCompany_or_&nbsp
  4795. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  4796. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  4797. NSString *shipToName = [self textAtColumn:19 statement:statement];
  4798. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  4799. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  4800. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  4801. /*****ship from******/
  4802. // ShipFromCompany_or_&nbsp
  4803. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  4804. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  4805. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  4806. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  4807. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  4808. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  4809. /*****freight to******/
  4810. // FreightBillToCompany_or_&nbsp
  4811. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  4812. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  4813. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  4814. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  4815. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  4816. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  4817. /*****merchandise to******/
  4818. // MerchandiseBillToCompany_or_&nbsp
  4819. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  4820. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  4821. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  4822. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  4823. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  4824. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  4825. /*****return to******/
  4826. // ReturnToCompany_or_&nbsp
  4827. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  4828. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  4829. NSString *returnToName = [self textAtColumn:31 statement:statement];
  4830. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  4831. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  4832. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  4833. //
  4834. // DebugLog(@"more info : %@",moreInfo);
  4835. // handling fee
  4836. handlingFee = sqlite3_column_double(statement, 33);
  4837. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  4838. if (sqlite3_column_int(statement, 58)) {
  4839. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  4840. }
  4841. //
  4842. // customer info
  4843. customerID = [self textAtColumn:36 statement:statement];
  4844. // mode
  4845. ret[@"mode"] = appDelegate.mode;
  4846. // model_count
  4847. ret[@"model_count"] = @(0);
  4848. }
  4849. sqlite3_finalize(statement);
  4850. }
  4851. [iSalesDB close_db:db];
  4852. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  4853. // "customer_email" = "Shui Hu";
  4854. // "customer_fax" = "";
  4855. // "customer_first_name" = F;
  4856. // "customer_last_name" = L;
  4857. // "customer_name" = ",da He Xiang Dong Liu A";
  4858. // "customer_phone" = "Hey Xuan Feng";
  4859. contactInfo[@"customer_phone"] = customer_phone;
  4860. contactInfo[@"customer_fax"] = customer_fax;
  4861. contactInfo[@"customer_email"] = customer_email;
  4862. NSString *first_name = @"";
  4863. NSString *last_name = @"";
  4864. if ([customer_contact isEqualToString:@""]) {
  4865. } else if ([customer_contact containsString:@" "]) {
  4866. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  4867. first_name = [customer_contact substringToIndex:first_space_index];
  4868. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  4869. }
  4870. contactInfo[@"customer_first_name"] = first_name;
  4871. contactInfo[@"customer_last_name"] = last_name;
  4872. ret[@"customerInfo"] = contactInfo;
  4873. // models
  4874. if (nssoid) {
  4875. __block double TotalCuft = 0;
  4876. __block double TotalWeight = 0;
  4877. __block int TotalCarton = 0;
  4878. __block double allItemPrice = 0;
  4879. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  4880. sqlite3 *db1 = [iSalesDB get_db];
  4881. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4882. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4883. int product_id = sqlite3_column_int(stmt, 0);
  4884. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4885. int item_id = sqlite3_column_int(stmt, 7);
  4886. NSString* Price=nil;
  4887. if(str_price==nil)
  4888. {
  4889. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4890. if(price==nil)
  4891. Price=@"No Price.";
  4892. else
  4893. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4894. }
  4895. else
  4896. {
  4897. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4898. }
  4899. double discount = sqlite3_column_double(stmt, 2);
  4900. int item_count = sqlite3_column_int(stmt, 3);
  4901. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  4902. NSString *nsline_note=nil;
  4903. if(line_note!=nil)
  4904. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4905. // char *name = (char*)sqlite3_column_text(stmt, 5);
  4906. // NSString *nsname = nil;
  4907. // if(name!=nil)
  4908. // nsname= [[NSString alloc]initWithUTF8String:name];
  4909. NSString *nsname = [self textAtColumn:5 statement:stmt];
  4910. // char *description = (char*)sqlite3_column_text(stmt, 6);
  4911. // NSString *nsdescription=nil;
  4912. // if(description!=nil)
  4913. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4914. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  4915. // int stockUom = sqlite3_column_int(stmt, 8);
  4916. // int _id = sqlite3_column_int(stmt, 9);
  4917. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  4918. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4919. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4920. int carton=[bsubtotaljson[@"carton"] intValue];
  4921. TotalCuft += cuft;
  4922. TotalWeight += weight;
  4923. TotalCarton += carton;
  4924. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4925. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  4926. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  4927. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4928. itemjson[@"note"]=nsline_note;
  4929. itemjson[@"origin_price"] = Price;
  4930. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4931. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4932. itemjson[@"order_item_status"] = @""; // 暂时不处理
  4933. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4934. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4935. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  4936. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4937. if(itemjson[@"combine"] != nil)
  4938. {
  4939. // int citem=0;
  4940. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4941. for(int bc=0;bc<bcount;bc++)
  4942. {
  4943. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4944. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4945. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4946. subTotal += uprice * modulus * item_count;
  4947. }
  4948. }
  4949. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4950. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  4951. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  4952. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  4953. allItemPrice += subTotal;
  4954. }];
  4955. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  4956. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  4957. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  4958. // payments/Credits
  4959. // payments_and_credist = sqlite3_column_double(statement, 34);
  4960. payments_and_credist = allItemPrice;
  4961. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4962. // // total
  4963. // totalPrice = sqlite3_column_double(statement, 35);
  4964. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4965. } else {
  4966. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  4967. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  4968. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  4969. // payments/Credits
  4970. payments_and_credist = 0;
  4971. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4972. }
  4973. // total
  4974. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4975. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4976. ret[@"order_info"]= orderinfo;
  4977. ret[@"more_order_info"] = moreInfo;
  4978. return [RAUtils dict2data:ret];
  4979. }
  4980. #pragma mark order list
  4981. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  4982. NSString *nsstatus = @"";
  4983. switch (status) {
  4984. case 0:
  4985. {
  4986. nsstatus=@"Temp Order";
  4987. break;
  4988. }
  4989. case 1:
  4990. {
  4991. nsstatus=@"Saved Order";
  4992. break;
  4993. }
  4994. case 2: {
  4995. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  4996. break;
  4997. }
  4998. case 3:
  4999. case 15:
  5000. {
  5001. nsstatus=@"Cancelled";
  5002. break;
  5003. }
  5004. case 10:
  5005. {
  5006. nsstatus=@"Quote Submitted";
  5007. break;
  5008. }
  5009. case 11:
  5010. {
  5011. nsstatus=@"Sales Order Submitted";
  5012. break;
  5013. }
  5014. case 12:
  5015. {
  5016. nsstatus=@"Processing";
  5017. break;
  5018. }
  5019. case 13:
  5020. {
  5021. nsstatus=@"Shipped";
  5022. break;
  5023. }
  5024. case 14:
  5025. {
  5026. nsstatus=@"Closed";
  5027. break;
  5028. }
  5029. case -11:
  5030. {
  5031. nsstatus = @"Ready For Submit";
  5032. break;
  5033. }
  5034. default:
  5035. break;
  5036. }
  5037. return nsstatus;
  5038. }
  5039. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  5040. double total = 0;
  5041. __block double payments_and_credist = 0;
  5042. __block double allItemPrice = 0;
  5043. // sqlite3 *db1 = [iSalesDB get_db];
  5044. if (so_id) {
  5045. // NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",so_id];
  5046. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id from (select product_id,decrypt(str_price) as price,discount,item_count,item_id from offline_cart where so_no='%@');",so_id];
  5047. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5048. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5049. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  5050. int product_id = sqlite3_column_int(stmt, 0);
  5051. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  5052. int discount = sqlite3_column_int(stmt, 2);
  5053. int item_count = sqlite3_column_int(stmt, 3);
  5054. // int item_id = sqlite3_column_int(stmt, 7);
  5055. int item_id = sqlite3_column_int(stmt, 4);
  5056. NSString* Price=nil;
  5057. if(str_price==nil)
  5058. {
  5059. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  5060. if(price==nil)
  5061. Price=@"No Price.";
  5062. else
  5063. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5064. }
  5065. else
  5066. {
  5067. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  5068. }
  5069. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  5070. itemjson[@"The unit price"]=Price;
  5071. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  5072. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  5073. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5074. if(itemjson[@"combine"] != nil)
  5075. {
  5076. // int citem=0;
  5077. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  5078. for(int bc=0;bc<bcount;bc++)
  5079. {
  5080. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5081. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5082. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5083. subTotal += uprice * modulus * item_count;
  5084. }
  5085. }
  5086. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  5087. allItemPrice += subTotal;
  5088. }];
  5089. payments_and_credist = allItemPrice;
  5090. } else {
  5091. // payments/Credits
  5092. payments_and_credist = 0;
  5093. }
  5094. // lift_gate handlingFee shippingFee
  5095. __block double lift_gate = 0;
  5096. __block double handlingFee = 0;
  5097. __block double shippingFee = 0;
  5098. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  5099. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5100. int have_lift_gate = sqlite3_column_int(stmt, 0);
  5101. if (have_lift_gate) {
  5102. lift_gate = sqlite3_column_double(stmt, 1);
  5103. }
  5104. handlingFee = sqlite3_column_double(stmt, 2);
  5105. shippingFee = sqlite3_column_double(stmt, 3);
  5106. }];
  5107. // total
  5108. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  5109. if (close) {
  5110. [iSalesDB close_db:db1];
  5111. }
  5112. return total;
  5113. }
  5114. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  5115. {
  5116. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5117. int limit = [[params valueForKey:@"limit"] intValue];
  5118. int offset = [[params valueForKey:@"offset"] intValue];
  5119. NSString* keyword = [params valueForKey:@"keyWord"];
  5120. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  5121. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  5122. NSString* where=@"1 = 1";
  5123. if(keyword.length>0)
  5124. where=[where stringByAppendingString:[NSString stringWithFormat:@" and (lower(o.so_id) like '%%%@%%' or lower(o.sales_rep) like '%%%@%%' or lower(decrypt(c.company_name)) like '%%%@%%' or lower(o.create_by) like '%%%@%%')",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString]];
  5125. if (orderStatus.length > 0) {
  5126. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  5127. if (order_status_array.count == 1) {
  5128. int status_value = [[order_status_array firstObject] integerValue];
  5129. if (status_value <= 1 || status_value == 3) {
  5130. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  5131. } else {
  5132. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  5133. }
  5134. } else if (order_status_array.count > 1) {
  5135. for (int i = 0; i < order_status_array.count;i++) {
  5136. NSString *status = order_status_array[i];
  5137. NSString *condition = @" or";
  5138. if (i == 0) {
  5139. condition = @" and (";
  5140. }
  5141. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  5142. int status_value = [status integerValue];
  5143. if (status_value <= 1 || status_value == 3) {
  5144. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  5145. } else {
  5146. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  5147. }
  5148. }
  5149. where = [where stringByAppendingString:@" )"];
  5150. }
  5151. }
  5152. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.modify_time desc,o.create_time desc limit %d offset %d" ,where, limit, offset];
  5153. // DebugLog(@"order list sql: %@",sqlQuery);
  5154. sqlite3 *db = [iSalesDB get_db];
  5155. sqlite3_stmt * statement;
  5156. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  5157. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5158. {
  5159. int count=0;
  5160. while (sqlite3_step(statement) == SQLITE_ROW)
  5161. {
  5162. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  5163. int order_id = sqlite3_column_double(statement, 0);
  5164. char *soid = (char*)sqlite3_column_text(statement, 1);
  5165. if(soid==nil)
  5166. soid= "";
  5167. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  5168. int status = sqlite3_column_double(statement, 2);
  5169. int erpStatus = sqlite3_column_double(statement, 9);
  5170. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  5171. if(sales_rep==nil)
  5172. sales_rep= "";
  5173. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  5174. char *create_by = (char*)sqlite3_column_text(statement, 4);
  5175. if(create_by==nil)
  5176. create_by= "";
  5177. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  5178. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5179. if(company_name==nil)
  5180. company_name= "";
  5181. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5182. char *create_time = (char*)sqlite3_column_text(statement, 6);
  5183. if(create_time==nil)
  5184. create_time= "";
  5185. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5186. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  5187. // double total_price = sqlite3_column_double(statement, 7);
  5188. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  5189. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  5190. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5191. int offline_edit = sqlite3_column_int(statement, 10);
  5192. // ": "JH",
  5193. // "": "$8307.00",
  5194. // "": "MOB1608050001",
  5195. // "": "ArpithaT",
  5196. // "": "1st Stage Property Transformations",
  5197. // "": "JANICE SUTTON",
  5198. // "": 2255,
  5199. // "": "08/02/2016 09:49:18",
  5200. // "": 1,
  5201. // "": "Saved Order"
  5202. // "": "1470384050483",
  5203. // "model_count": "6 / 28"
  5204. item[@"sales_rep"]= nssales_rep;
  5205. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  5206. item[@"so#"]= nssoid;
  5207. item[@"create_by"]= nscreate_by;
  5208. item[@"customer_name"]= nscompany_name;
  5209. item[@"customer_contact"] = customer_contact;
  5210. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  5211. item[@"purchase_time"]= nscreate_time;
  5212. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  5213. item[@"order_status"]= nsstatus;
  5214. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  5215. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  5216. // item[@"model_count"]
  5217. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  5218. count++;
  5219. }
  5220. ret[@"count"]= [NSNumber numberWithInt:count];
  5221. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  5222. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  5223. ret[@"result"]= [NSNumber numberWithInt:2];
  5224. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  5225. ret[@"time_zone"] = @"PST";
  5226. sqlite3_finalize(statement);
  5227. }
  5228. int total_count = [iSalesDB get_recordcount:db table:@"offline_order o left join offline_contact c on o.customer_cid=c.contact_id" where:where];
  5229. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  5230. [iSalesDB execSql:@"END TRANSACTION" db:db];
  5231. [iSalesDB close_db:db];
  5232. return [RAUtils dict2data:ret];
  5233. }
  5234. #pragma mark update gnotes
  5235. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  5236. {
  5237. DebugLog(@"params: %@",params);
  5238. // comments = Meyoyoyoyoyoyoy;
  5239. // orderCode = MOB1608110001;
  5240. // password = 123456;
  5241. // user = EvanK;
  5242. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments" translateSingleQuote:YES],[self valueInParams:params key:@"orderCode" translateSingleQuote:YES]];
  5243. int ret = [iSalesDB execSql:sql];
  5244. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5245. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5246. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5247. // [dic setValue:@"160409" forKey:@"min_ver"];
  5248. return [RAUtils dict2data:dic];
  5249. }
  5250. #pragma mark move to wishlist
  5251. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  5252. {
  5253. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5254. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5255. _id = [NSString stringWithFormat:@"(%@)",_id];
  5256. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  5257. sqlite3 *db = [iSalesDB get_db];
  5258. __block NSString *product_id = @"";
  5259. __block NSString *item_count_str = @"";
  5260. // __block NSString *item_id = nil;
  5261. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5262. // product_id = [self textAtColumn:0 statement:stmt];
  5263. int item_count = sqlite3_column_int(stmt, 1);
  5264. // item_id = [self textAtColumn:2 statement:stmt];
  5265. NSString *p_id = [self textAtColumn:0 statement:stmt];
  5266. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  5267. if (p_id.length) {
  5268. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  5269. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  5270. }
  5271. }];
  5272. [iSalesDB close_db:db];
  5273. // 去除第一个,
  5274. product_id = [product_id substringFromIndex:1];
  5275. item_count_str = [item_count_str substringFromIndex:1];
  5276. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5277. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  5278. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  5279. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  5280. sqlite3 *db1 = [iSalesDB get_db];
  5281. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  5282. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5283. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  5284. // 删除
  5285. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  5286. int ret = [iSalesDB execSql:deleteSql db:db1];
  5287. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5288. [iSalesDB close_db:db1];
  5289. return [RAUtils dict2data:dic];
  5290. }
  5291. #pragma mark cart delete
  5292. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  5293. {
  5294. // cartItemId = 548;
  5295. // orderCode = MOB1608110001;
  5296. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  5297. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5298. _id = [NSString stringWithFormat:@"(%@)",_id];
  5299. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  5300. int ret = [iSalesDB execSql:sql];
  5301. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5302. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5303. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5304. // [dic setValue:@"160409" forKey:@"min_ver"];
  5305. return [RAUtils dict2data:dic];
  5306. }
  5307. #pragma mark set price
  5308. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  5309. {
  5310. DebugLog(@"cart set price params: %@",params);
  5311. // "cartitem_id" = 1;
  5312. // discount = "0.000000";
  5313. // "item_note" = "";
  5314. // price = "269.000000";
  5315. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5316. NSString *notes = [self valueInParams:params key:@"item_note"];
  5317. NSString *discount = [self valueInParams:params key:@"discount"];
  5318. NSString *price = [self valueInParams:params key:@"price"];
  5319. // bool badd_price_changed=false;
  5320. // sqlite3* db=[iSalesDB get_db];
  5321. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5322. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  5323. // NSRange range;
  5324. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  5325. //
  5326. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  5327. // badd_price_changed=true;
  5328. // [iSalesDB close_db:db];
  5329. //
  5330. // if(badd_price_changed)
  5331. // {
  5332. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  5333. // }
  5334. //
  5335. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  5336. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  5337. int ret = [iSalesDB execSql:sql];
  5338. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5339. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5340. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5341. // [dic setValue:@"160409" forKey:@"min_ver"];
  5342. return [RAUtils dict2data:dic];
  5343. }
  5344. #pragma mark set line notes
  5345. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  5346. {
  5347. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5348. NSString *notes = [self valueInParams:params key:@"notes"];
  5349. notes = [self translateSingleQuote:notes];
  5350. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  5351. int ret = [iSalesDB execSql:sql];
  5352. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5353. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5354. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5355. // [dic setValue:@"160409" forKey:@"min_ver"];
  5356. return [RAUtils dict2data:dic];
  5357. }
  5358. #pragma mark set qty
  5359. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  5360. {
  5361. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  5362. int item_count = [params[@"inputInt"] integerValue];
  5363. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  5364. int ret = [iSalesDB execSql:sql];
  5365. __block int item_id = 0;
  5366. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5367. item_id = sqlite3_column_int(stmt, 0);
  5368. }];
  5369. sqlite3 *db = [iSalesDB get_db];
  5370. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5371. [iSalesDB close_db:db];
  5372. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5373. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  5374. [dic setValue:@"Regular Mode" forKey:@"mode"];
  5375. // [dic setValue:@"160409" forKey:@"min_ver"];
  5376. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  5377. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  5378. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  5379. return [RAUtils dict2data:dic];
  5380. }
  5381. #pragma mark place order
  5382. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  5383. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5384. if (aname) {
  5385. [dic setValue:aname forKey:@"aname"];
  5386. }
  5387. if (control) {
  5388. [dic setValue:control forKey:@"control"];
  5389. }
  5390. if (keyboard) {
  5391. [dic setValue:keyboard forKey:@"keyboard"];
  5392. }
  5393. if (name) {
  5394. [dic setValue:name forKey:@"name"];
  5395. }
  5396. if (value) {
  5397. [dic setValue:value forKey:@"value"];
  5398. }
  5399. return dic;
  5400. }
  5401. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  5402. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5403. orderCode = [self translateSingleQuote:orderCode];
  5404. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  5405. NSString *customerSql = [NSString stringWithFormat:@"select c.contact_id,c.img_0,c.img_1,c.img_2,decrypt(c.company_name),decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,c.first_name,c.last_name,o.customer_email,o.customer_phone,o.customer_fax,c.zipcode,c.city,c.state,c.country,o.customer_contact from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  5406. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5407. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  5408. NSString *img0 = [self textAtColumn:1 statement:stmt];
  5409. NSString *img1 = [self textAtColumn:2 statement:stmt];
  5410. NSString *img2 = [self textAtColumn:3 statement:stmt];
  5411. NSString *company_name = [self textAtColumn:4 statement:stmt];
  5412. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  5413. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5414. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5415. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5416. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5417. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5418. NSString *email = [self textAtColumn:11 statement:stmt];
  5419. NSString *phone = [self textAtColumn:12 statement:stmt];
  5420. NSString *fax = [self textAtColumn:13 statement:stmt];
  5421. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5422. NSString *city = [self textAtColumn:15 statement:stmt];
  5423. NSString *state = [self textAtColumn:16 statement:stmt];
  5424. NSString *country = [self textAtColumn:17 statement:stmt];
  5425. NSString *name = [self textAtColumn:18 statement:stmt];
  5426. // contact id
  5427. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5428. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5429. [contact_id_dic setValue:@"text" forKey:@"control"];
  5430. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5431. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5432. [contact_id_dic setValue:@"true" forKey:@"required"];
  5433. [contact_id_dic setValue:contact_id forKey:@"value"];
  5434. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5435. // business card
  5436. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5437. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5438. [business_card_dic setValue:@"img" forKey:@"control"];
  5439. [business_card_dic setValue:@"1" forKey:@"disable"];
  5440. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5441. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5442. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5443. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5444. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5445. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5446. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5447. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5448. // fax
  5449. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5450. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5451. // zipcode
  5452. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5453. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5454. // city
  5455. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5456. [customer_dic setValue:city_dic forKey:@"item_12"];
  5457. // state
  5458. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5459. [customer_dic setValue:state_dic forKey:@"item_13"];
  5460. // country
  5461. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5462. [customer_dic setValue:country_dic forKey:@"item_14"];
  5463. // company name
  5464. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5465. [customer_dic setValue:company_dic forKey:@"item_2"];
  5466. // addr_1
  5467. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5468. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5469. // addr_2
  5470. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5471. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5472. // addr_3
  5473. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5474. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5475. // addr_4
  5476. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5477. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5478. // Contact
  5479. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5480. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5481. // email
  5482. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5483. [customer_dic setValue:email_dic forKey:@"item_8"];
  5484. // phone
  5485. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5486. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5487. // title
  5488. [customer_dic setValue:@"Customer" forKey:@"title"];
  5489. // count
  5490. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5491. }];
  5492. // setting
  5493. NSDictionary *setting = params[@"setting"];
  5494. NSNumber *hide = setting[@"CustomerHide"];
  5495. [customer_dic setValue:hide forKey:@"hide"];
  5496. return customer_dic;
  5497. }
  5498. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5499. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5500. // setting
  5501. NSDictionary *setting = params[@"setting"];
  5502. NSNumber *hide = setting[@"ShipToHide"];
  5503. [dic setValue:hide forKey:@"hide"];
  5504. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5505. orderCode = [self translateSingleQuote:orderCode];
  5506. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone from offline_order where so_id = '%@';",orderCode];
  5507. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5508. NSString *cid = [self textAtColumn:0 statement:stmt];
  5509. NSString *name = [self textAtColumn:1 statement:stmt];
  5510. NSString *ext = [self textAtColumn:2 statement:stmt];
  5511. NSString *contact = [self textAtColumn:3 statement:stmt];
  5512. NSString *email = [self textAtColumn:4 statement:stmt];
  5513. NSString *fax = [self textAtColumn:5 statement:stmt];
  5514. NSString *phone = [self textAtColumn:6 statement:stmt];
  5515. // count
  5516. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5517. // title
  5518. [dic setValue:@"Ship To" forKey:@"title"];
  5519. // choose
  5520. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5521. [choose_dic setValue:@"choose" forKey:@"aname"];
  5522. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5523. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5524. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5525. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5526. @"receive_contact" : @"customer_contact",
  5527. @"receive_email" : @"customer_email",
  5528. @"receive_ext" : @"customer_contact_ext",
  5529. @"receive_fax" : @"customer_fax",
  5530. @"receive_name" : @"customer_name",
  5531. @"receive_phone" : @"customer_phone"},
  5532. @"refresh" : [NSNumber numberWithInteger:1],
  5533. @"type" : @"pull"};
  5534. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  5535. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  5536. @"name" : @"Add new address",
  5537. @"refresh" : [NSNumber numberWithInteger:1],
  5538. @"value" : @"new_addr"
  5539. };
  5540. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  5541. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  5542. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5543. @"receive_contact" : @"customer_contact",
  5544. @"receive_email" : @"customer_email",
  5545. @"receive_ext" : @"customer_contact_ext",
  5546. @"receive_fax" : @"customer_fax",
  5547. @"receive_name" : @"customer_name",
  5548. @"receive_phone" : @"customer_phone"},
  5549. @"name" : @"select_ship_to",
  5550. @"refresh" : [NSNumber numberWithInteger:1],
  5551. @"value" : @"Sales_Order_Ship_To"};
  5552. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  5553. [dic setValue:choose_dic forKey:@"item_0"];
  5554. // contact id
  5555. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5556. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5557. [contact_id_dic setValue:@"true" forKey:@"required"];
  5558. [dic setValue:contact_id_dic forKey:@"item_1"];
  5559. // company name
  5560. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5561. [dic setValue:company_name_dic forKey:@"item_2"];
  5562. // address
  5563. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5564. NSNumber *required = setting[@"ShippingToAddressRequire"];
  5565. if ([required integerValue]) {
  5566. [ext_dic setValue:@"true" forKey:@"required"];
  5567. }
  5568. [dic setValue:ext_dic forKey:@"item_3"];
  5569. // contact
  5570. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5571. [dic setValue:contact_dic forKey:@"item_4"];
  5572. // phone
  5573. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5574. [dic setValue:phone_dic forKey:@"item_5"];
  5575. // fax
  5576. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5577. [dic setValue:fax_dic forKey:@"item_6"];
  5578. // email
  5579. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5580. [dic setValue:email_dic forKey:@"item_7"];
  5581. }];
  5582. return dic;
  5583. }
  5584. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  5585. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5586. // setting
  5587. NSDictionary *setting = params[@"setting"];
  5588. NSNumber *hide = setting[@"ShipFromHide"];
  5589. [dic setValue:hide forKey:@"hide"];
  5590. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5591. orderCode = [self translateSingleQuote:orderCode];
  5592. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone from offline_order where so_id = '%@';",orderCode];
  5593. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  5594. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5595. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  5596. __block NSString *name = [self textAtColumn:1 statement:stmt];
  5597. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  5598. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  5599. __block NSString *email = [self textAtColumn:4 statement:stmt];
  5600. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  5601. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  5602. if (!cid.length) {
  5603. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5604. cid = [self textAtColumn:0 statement:statment];
  5605. name = [self textAtColumn:1 statement:statment];
  5606. ext = [self textAtColumn:2 statement:statment];
  5607. contact = [self textAtColumn:3 statement:statment];
  5608. email = [self textAtColumn:4 statement:statment];
  5609. fax = [self textAtColumn:5 statement:statment];
  5610. phone = [self textAtColumn:6 statement:statment];
  5611. }];
  5612. }
  5613. // count
  5614. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5615. // title
  5616. [dic setValue:@"Ship From" forKey:@"title"];
  5617. // hide
  5618. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5619. // choose
  5620. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5621. [choose_dic setValue:@"choose" forKey:@"aname"];
  5622. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5623. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5624. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  5625. @"key_map" : @{@"sender_cid" : @"customer_cid",
  5626. @"sender_contact" : @"customer_contact",
  5627. @"sender_email" : @"customer_email",
  5628. @"sender_ext" : @"customer_contact_ext",
  5629. @"sender_fax" : @"customer_fax",
  5630. @"sender_name" : @"customer_name",
  5631. @"sender_phone" : @"customer_phone"},
  5632. @"name" : @"select_cid",
  5633. @"refresh" : [NSNumber numberWithInteger:0],
  5634. @"value" : @"Sales_Order_Ship_From"};
  5635. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  5636. [dic setValue:choose_dic forKey:@"item_0"];
  5637. // contact id
  5638. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5639. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5640. [contact_id_dic setValue:@"true" forKey:@"required"];
  5641. [dic setValue:contact_id_dic forKey:@"item_1"];
  5642. // company name
  5643. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5644. [dic setValue:company_name_dic forKey:@"item_2"];
  5645. // address
  5646. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5647. [dic setValue:ext_dic forKey:@"item_3"];
  5648. // contact
  5649. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5650. [dic setValue:contact_dic forKey:@"item_4"];
  5651. // phone
  5652. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5653. [dic setValue:phone_dic forKey:@"item_5"];
  5654. // fax
  5655. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5656. [dic setValue:fax_dic forKey:@"item_6"];
  5657. // email
  5658. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5659. [dic setValue:email_dic forKey:@"item_7"];
  5660. }];
  5661. return dic;
  5662. }
  5663. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  5664. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5665. // setting
  5666. NSDictionary *setting = params[@"setting"];
  5667. NSNumber *hide = setting[@"FreightBillToHide"];
  5668. [dic setValue:hide forKey:@"hide"];
  5669. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5670. orderCode = [self translateSingleQuote:orderCode];
  5671. NSString *sql = [NSString stringWithFormat:@"select shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_email,shipping_billto_fax,shipping_billto_phone from offline_order where so_id = '%@';",orderCode];
  5672. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5673. NSString *cid = [self textAtColumn:0 statement:stmt];
  5674. NSString *name = [self textAtColumn:1 statement:stmt];
  5675. NSString *ext = [self textAtColumn:2 statement:stmt];
  5676. NSString *contact = [self textAtColumn:3 statement:stmt];
  5677. NSString *email = [self textAtColumn:4 statement:stmt];
  5678. NSString *fax = [self textAtColumn:5 statement:stmt];
  5679. NSString *phone = [self textAtColumn:6 statement:stmt];
  5680. // count
  5681. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5682. // title
  5683. [dic setValue:@"Freight Bill To" forKey:@"title"];
  5684. // hide
  5685. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5686. // choose
  5687. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5688. [choose_dic setValue:@"choose" forKey:@"aname"];
  5689. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5690. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5691. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5692. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  5693. @"shipping_billto_contact" : @"receive_contact",
  5694. @"shipping_billto_email" : @"receive_email",
  5695. @"shipping_billto_ext" : @"receive_ext",
  5696. @"shipping_billto_fax" : @"receive_fax",
  5697. @"shipping_billto_name" : @"receive_name",
  5698. @"shipping_billto_phone" : @"receive_phone"},
  5699. @"type" : @"pull"};
  5700. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5701. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5702. @"type" : @"pull",
  5703. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  5704. @"shipping_billto_contact" : @"customer_contact",
  5705. @"shipping_billto_email" : @"customer_email",
  5706. @"shipping_billto_ext" : @"customer_contact_ext",
  5707. @"shipping_billto_fax" : @"customer_fax",
  5708. @"shipping_billto_name" : @"customer_name",
  5709. @"shipping_billto_phone" : @"customer_phone"}
  5710. };
  5711. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5712. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5713. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  5714. @"shipping_billto_contact" : @"sender_contact",
  5715. @"shipping_billto_email" : @"sender_email",
  5716. @"shipping_billto_ext" : @"sender_ext",
  5717. @"shipping_billto_fax" : @"sender_fax",
  5718. @"shipping_billto_name" : @"sender_name",
  5719. @"shipping_billto_phone" : @"sender_phone"},
  5720. @"type" : @"pull"
  5721. };
  5722. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  5723. NSDictionary *select_freight_bill_to_dic = @{
  5724. @"aname" : @"Select freight bill to",
  5725. @"name" : @"select_cid",
  5726. @"refresh" : [NSNumber numberWithInteger:0],
  5727. @"value" : @"Sales_Order_Freight_Bill_To",
  5728. @"key_map" : @{
  5729. @"shipping_billto_cid" : @"customer_cid",
  5730. @"shipping_billto_contact" : @"customer_contact",
  5731. @"shipping_billto_email" : @"customer_email",
  5732. @"shipping_billto_ext" : @"customer_contact_ext",
  5733. @"shipping_billto_fax" : @"customer_fax",
  5734. @"shipping_billto_name" : @"customer_name",
  5735. @"shipping_billto_phone" : @"customer_phone"
  5736. }
  5737. };
  5738. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  5739. [dic setValue:choose_dic forKey:@"item_0"];
  5740. // contact id
  5741. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5742. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5743. [contact_id_dic setValue:@"true" forKey:@"required"];
  5744. [dic setValue:contact_id_dic forKey:@"item_1"];
  5745. // company name
  5746. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5747. [dic setValue:company_name_dic forKey:@"item_2"];
  5748. // address
  5749. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5750. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  5751. if ([ext_required integerValue]) {
  5752. [ext_dic setValue:@"true" forKey:@"required"];
  5753. }
  5754. [dic setValue:ext_dic forKey:@"item_3"];
  5755. // contact
  5756. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5757. [dic setValue:contact_dic forKey:@"item_4"];
  5758. // phone
  5759. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5760. [dic setValue:phone_dic forKey:@"item_5"];
  5761. // fax
  5762. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5763. [dic setValue:fax_dic forKey:@"item_6"];
  5764. // email
  5765. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5766. [dic setValue:email_dic forKey:@"item_7"];
  5767. }];
  5768. return dic;
  5769. }
  5770. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5771. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5772. // setting
  5773. NSDictionary *setting = params[@"setting"];
  5774. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  5775. [dic setValue:hide forKey:@"hide"];
  5776. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5777. orderCode = [self translateSingleQuote:orderCode];
  5778. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone from offline_order where so_id = '%@';",orderCode];
  5779. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5780. NSString *cid = [self textAtColumn:0 statement:stmt];
  5781. NSString *name = [self textAtColumn:1 statement:stmt];
  5782. NSString *ext = [self textAtColumn:2 statement:stmt];
  5783. NSString *contact = [self textAtColumn:3 statement:stmt];
  5784. NSString *email = [self textAtColumn:4 statement:stmt];
  5785. NSString *fax = [self textAtColumn:5 statement:stmt];
  5786. NSString *phone = [self textAtColumn:6 statement:stmt];
  5787. // count
  5788. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5789. // title
  5790. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  5791. // hide
  5792. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5793. // choose
  5794. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5795. [choose_dic setValue:@"choose" forKey:@"aname"];
  5796. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5797. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5798. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5799. @"key_map" : @{@"billing_cid" : @"receive_cid",
  5800. @"billing_contact" : @"receive_contact",
  5801. @"billing_email" : @"receive_email",
  5802. @"billing_ext" : @"receive_ext",
  5803. @"billing_fax" : @"receive_fax",
  5804. @"billing_name" : @"receive_name",
  5805. @"billing_phone" : @"receive_phone"},
  5806. @"type" : @"pull"};
  5807. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5808. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5809. @"type" : @"pull",
  5810. @"key_map" : @{@"billing_cid" : @"customer_cid",
  5811. @"billing_contact" : @"customer_contact",
  5812. @"billing_email" : @"customer_email",
  5813. @"billing_ext" : @"customer_contact_ext",
  5814. @"billing_fax" : @"customer_fax",
  5815. @"billing_name" : @"customer_name",
  5816. @"billing_phone" : @"customer_phone"}
  5817. };
  5818. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5819. NSDictionary *select_bill_to_dic = @{
  5820. @"aname" : @"Select bill to",
  5821. @"name" : @"select_cid",
  5822. @"refresh" : [NSNumber numberWithInteger:0],
  5823. @"value" : @"Sales_Order_Merchandise_Bill_To",
  5824. @"key_map" : @{
  5825. @"billing_cid" : @"customer_cid",
  5826. @"billing_contact" : @"customer_contact",
  5827. @"billing_email" : @"customer_email",
  5828. @"billing_ext" : @"customer_contact_ext",
  5829. @"billing_fax" : @"customer_fax",
  5830. @"billing_name" : @"customer_name",
  5831. @"billing_phone" : @"customer_phone"
  5832. }
  5833. };
  5834. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  5835. [dic setValue:choose_dic forKey:@"item_0"];
  5836. // contact id
  5837. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5838. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5839. [contact_id_dic setValue:@"true" forKey:@"required"];
  5840. [dic setValue:contact_id_dic forKey:@"item_1"];
  5841. // company name
  5842. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5843. [dic setValue:company_name_dic forKey:@"item_2"];
  5844. // address
  5845. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5846. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  5847. if ([ext_required integerValue]) {
  5848. [ext_dic setValue:@"true" forKey:@"required"];
  5849. }
  5850. [dic setValue:ext_dic forKey:@"item_3"];
  5851. // contact
  5852. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5853. [dic setValue:contact_dic forKey:@"item_4"];
  5854. // phone
  5855. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5856. [dic setValue:phone_dic forKey:@"item_5"];
  5857. // fax
  5858. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5859. [dic setValue:fax_dic forKey:@"item_6"];
  5860. // email
  5861. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5862. [dic setValue:email_dic forKey:@"item_7"];
  5863. }];
  5864. return dic;
  5865. }
  5866. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5867. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5868. // setting
  5869. NSDictionary *setting = params[@"setting"];
  5870. NSNumber *hide = setting[@"ReturnToHide"];
  5871. [dic setValue:hide forKey:@"hide"];
  5872. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5873. orderCode = [self translateSingleQuote:orderCode];
  5874. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  5875. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5876. NSString *cid = [self textAtColumn:0 statement:stmt];
  5877. NSString *name = [self textAtColumn:1 statement:stmt];
  5878. NSString *ext = [self textAtColumn:2 statement:stmt];
  5879. NSString *contact = [self textAtColumn:3 statement:stmt];
  5880. NSString *email = [self textAtColumn:4 statement:stmt];
  5881. NSString *fax = [self textAtColumn:5 statement:stmt];
  5882. NSString *phone = [self textAtColumn:6 statement:stmt];
  5883. // count
  5884. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5885. // title
  5886. [dic setValue:@"Return To" forKey:@"title"];
  5887. // hide
  5888. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5889. // choose
  5890. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5891. [choose_dic setValue:@"choose" forKey:@"aname"];
  5892. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5893. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  5894. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5895. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  5896. @"returnto_contact" : @"sender_contact",
  5897. @"returnto_email" : @"sender_email",
  5898. @"returnto_ext" : @"sender_ext",
  5899. @"returnto_fax" : @"sender_fax",
  5900. @"returnto_name" : @"sender_name",
  5901. @"returnto_phone" : @"sender_phone"},
  5902. @"type" : @"pull"};
  5903. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  5904. NSDictionary *select_return_to_dic = @{
  5905. @"aname" : @"Select return to",
  5906. @"name" : @"select_cid",
  5907. @"refresh" : [NSNumber numberWithInteger:0],
  5908. @"value" : @"Contact_Return_To",
  5909. @"key_map" : @{
  5910. @"returnto_cid" : @"customer_cid",
  5911. @"returnto_contact" : @"customer_contact",
  5912. @"returnto_email" : @"customer_email",
  5913. @"returnto_ext" : @"customer_contact_ext",
  5914. @"returnto_fax" : @"customer_fax",
  5915. @"returnto_name" : @"customer_name",
  5916. @"returnto_phone" : @"customer_phone"
  5917. }
  5918. };
  5919. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  5920. [dic setValue:choose_dic forKey:@"item_0"];
  5921. // contact id
  5922. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5923. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5924. [contact_id_dic setValue:@"true" forKey:@"required"];
  5925. [dic setValue:contact_id_dic forKey:@"item_1"];
  5926. // company name
  5927. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5928. [dic setValue:company_name_dic forKey:@"item_2"];
  5929. // address
  5930. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5931. [dic setValue:ext_dic forKey:@"item_3"];
  5932. // contact
  5933. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5934. [dic setValue:contact_dic forKey:@"item_4"];
  5935. // phone
  5936. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5937. [dic setValue:phone_dic forKey:@"item_5"];
  5938. // fax
  5939. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5940. [dic setValue:fax_dic forKey:@"item_6"];
  5941. // email
  5942. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5943. [dic setValue:email_dic forKey:@"item_7"];
  5944. }];
  5945. return dic;
  5946. }
  5947. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5948. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5949. orderCode = [self translateSingleQuote:orderCode];
  5950. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5951. __block double TotalCuft = 0;
  5952. __block double TotalWeight = 0;
  5953. __block int TotalCarton = 0;
  5954. __block double payments = 0;
  5955. // setting
  5956. NSDictionary *setting = params[@"setting"];
  5957. NSNumber *hide = setting[@"ModelInformationHide"];
  5958. [dic setValue:hide forKey:@"hide"];
  5959. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description,c.line_note,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id;",orderCode];
  5960. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5961. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5962. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  5963. // item id
  5964. int item_id = sqlite3_column_int(stmt, 0);
  5965. // count
  5966. int item_count = sqlite3_column_int(stmt, 1);
  5967. // stockUom
  5968. int stockUom = sqlite3_column_int(stmt, 2);
  5969. // unit price
  5970. NSString *str_price = [self textAtColumn:3 statement:stmt];
  5971. NSString* Price=nil;
  5972. if([str_price isEqualToString:@""])
  5973. {
  5974. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  5975. if(price==nil)
  5976. Price=@"No Price.";
  5977. else
  5978. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5979. }
  5980. else
  5981. {
  5982. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  5983. }
  5984. // discount
  5985. double discount = sqlite3_column_double(stmt, 4);
  5986. // name
  5987. NSString *name = [self textAtColumn:5 statement:stmt];
  5988. // description
  5989. NSString *description = [self textAtColumn:6 statement:stmt];
  5990. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  5991. // line note
  5992. NSString *line_note = [self textAtColumn:7 statement:stmt];
  5993. int avaulability = sqlite3_column_int(stmt, 8);
  5994. // img
  5995. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  5996. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  5997. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5998. if(combine != nil)
  5999. {
  6000. // int citem=0;
  6001. int bcount=[[combine valueForKey:@"count"] intValue];
  6002. for(int bc=0;bc<bcount;bc++)
  6003. {
  6004. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6005. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6006. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6007. subTotal += uprice * modulus * item_count;
  6008. }
  6009. }
  6010. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  6011. [model_dic setValue:@"model" forKey:@"control"];
  6012. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  6013. [model_dic setValue:description forKey:@"description"];
  6014. [model_dic setValue:line_note forKey:@"note"];
  6015. [model_dic setValue:img forKey:@"img_url"];
  6016. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  6017. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  6018. [model_dic setValue:Price forKey:@"unit_price"];
  6019. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  6020. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  6021. if (combine) {
  6022. [model_dic setValue:combine forKey:@"combine"];
  6023. }
  6024. // well,what under the row is the info for total
  6025. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6026. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6027. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6028. int carton=[bsubtotaljson[@"carton"] intValue];
  6029. TotalCuft += cuft;
  6030. TotalWeight += weight;
  6031. TotalCarton += carton;
  6032. payments += subTotal;
  6033. //---------------------------
  6034. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  6035. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  6036. }];
  6037. [dic setValue:@"Model Information" forKey:@"title"];
  6038. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  6039. return dic;
  6040. }
  6041. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  6042. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6043. [dic setValue:@"Remarks Content" forKey:@"title"];
  6044. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6045. // setting
  6046. NSDictionary *setting = params[@"setting"];
  6047. NSNumber *hide = setting[@"RemarksContentHide"];
  6048. [dic setValue:hide forKey:@"hide"];
  6049. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6050. orderCode = [self translateSingleQuote:orderCode];
  6051. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  6052. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6053. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  6054. int mustCall = sqlite3_column_int(stmt, 1);
  6055. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  6056. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  6057. NSDictionary *po_dic = @{
  6058. @"aname" : @"PO#",
  6059. @"control" : @"edit",
  6060. @"keyboard" : @"text",
  6061. @"name" : @"poNumber",
  6062. @"value" : poNumber
  6063. };
  6064. NSDictionary *must_call_dic = @{
  6065. @"aname" : @"MUST CALL BEFORE DELIVERY",
  6066. @"control" : @"switch",
  6067. @"name" : @"must_call",
  6068. @"value" : mustCall ? @"true" : @"false"
  6069. };
  6070. NSDictionary *general_notes_dic = @{
  6071. @"aname" : @"General notes",
  6072. @"control" : @"text_view",
  6073. @"keyboard" : @"text",
  6074. @"name" : @"comments",
  6075. @"value" : generalNotes
  6076. };
  6077. NSDictionary *internal_notes_dic = @{
  6078. @"aname" : @"Internal notes",
  6079. @"control" : @"text_view",
  6080. @"keyboard" : @"text",
  6081. @"name" : @"internal_notes",
  6082. @"value" : internalNotes
  6083. };
  6084. [dic setValue:po_dic forKey:@"item_0"];
  6085. [dic setValue:must_call_dic forKey:@"item_1"];
  6086. [dic setValue:general_notes_dic forKey:@"item_2"];
  6087. [dic setValue:internal_notes_dic forKey:@"item_3"];
  6088. }];
  6089. return dic;
  6090. }
  6091. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  6092. // params
  6093. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6094. orderCode = [self translateSingleQuote:orderCode];
  6095. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6096. // setting
  6097. NSDictionary *setting = params[@"setting"];
  6098. NSNumber *hide = setting[@"OrderTotalHide"];
  6099. [dic setValue:hide forKey:@"hide"];
  6100. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  6101. __block double lift_gate_value = 0;
  6102. __block double handling_fee = 0;
  6103. __block double shipping = 0;
  6104. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6105. int lift_gate = sqlite3_column_int(stmt, 0);
  6106. lift_gate_value = sqlite3_column_double(stmt, 1);
  6107. shipping = sqlite3_column_double(stmt, 2);
  6108. handling_fee = sqlite3_column_double(stmt, 3);
  6109. if (!lift_gate) {
  6110. lift_gate_value = 0;
  6111. }
  6112. }];
  6113. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  6114. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  6115. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  6116. double payments = [[total valueForKey:@"payments"] doubleValue];
  6117. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  6118. double totalPrice = payments;
  6119. [dic setValue:@"Order Total" forKey:@"title"];
  6120. NSDictionary *payments_dic = @{
  6121. @"align" : @"right",
  6122. @"aname" : @"Payments/Credits",
  6123. @"control" : @"text",
  6124. @"name" : @"paymentsAndCredits",
  6125. @"type" : @"price",
  6126. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  6127. };
  6128. [dic setValue:payments_dic forKey:@"item_0"];
  6129. NSDictionary *handling_fee_dic = @{
  6130. @"align" : @"right",
  6131. @"aname" : @"Handling Fee",
  6132. @"control" : @"text",
  6133. @"name" : @"handling_fee_value",
  6134. @"required" : @"true",
  6135. @"type" : @"price",
  6136. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  6137. };
  6138. NSDictionary *shipping_dic = @{
  6139. @"align" : @"right",
  6140. @"aname" : @"Shipping*",
  6141. @"control" : @"text",
  6142. @"name" : @"shipping",
  6143. @"required" : @"true",
  6144. @"type" : @"price",
  6145. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  6146. };
  6147. NSDictionary *lift_gate_dic = @{
  6148. @"align" : @"right",
  6149. @"aname" : @"Liftgate Fee(No Loading Dock)",
  6150. @"control" : @"text",
  6151. @"name" : @"lift_gate_value",
  6152. @"required" : @"true",
  6153. @"type" : @"price",
  6154. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  6155. };
  6156. int item_count = 1;
  6157. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  6158. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  6159. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6160. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6161. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  6162. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6163. } else {
  6164. }
  6165. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6166. NSDictionary *total_price_dic = @{
  6167. @"align" : @"right",
  6168. @"aname" : @"Total",
  6169. @"control" : @"text",
  6170. @"name" : @"totalPrice",
  6171. @"type" : @"price",
  6172. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  6173. };
  6174. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6175. NSDictionary *total_cuft_dic = @{
  6176. @"align" : @"right",
  6177. @"aname" : @"Total Cuft",
  6178. @"control" : @"text",
  6179. @"name" : @"",
  6180. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  6181. };
  6182. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6183. NSDictionary *total_weight_dic = @{
  6184. @"align" : @"right",
  6185. @"aname" : @"Total Weight",
  6186. @"control" : @"text",
  6187. @"name" : @"",
  6188. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  6189. };
  6190. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6191. NSDictionary *total_carton_dic = @{
  6192. @"align" : @"right",
  6193. @"aname" : @"Total Carton",
  6194. @"control" : @"text",
  6195. @"name" : @"",
  6196. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  6197. };
  6198. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  6199. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  6200. return dic;
  6201. }
  6202. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  6203. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6204. orderCode = [self translateSingleQuote:orderCode];
  6205. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6206. // setting
  6207. NSDictionary *setting = params[@"setting"];
  6208. NSNumber *hide = setting[@"SignatureHide"];
  6209. [dic setValue:hide forKey:@"hide"];
  6210. [dic setValue:@"Signature" forKey:@"title"];
  6211. __block NSString *pic_path = @"";
  6212. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  6213. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6214. pic_path = [self textAtColumn:0 statement:stmt];
  6215. }];
  6216. NSDictionary *pic_dic = @{
  6217. @"aname" : @"Signature",
  6218. @"avalue" :pic_path,
  6219. @"control" : @"signature",
  6220. @"name" : @"sign_picpath",
  6221. @"value" : pic_path
  6222. };
  6223. [dic setValue:pic_dic forKey:@"item_0"];
  6224. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6225. return dic;
  6226. }
  6227. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  6228. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6229. // setting
  6230. NSDictionary *setting = params[@"setting"];
  6231. NSNumber *hide = setting[@"ShippingMethodHide"];
  6232. [dic setValue:hide forKey:@"hide"];
  6233. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6234. orderCode = [self translateSingleQuote:orderCode];
  6235. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  6236. __block NSString *logist = @"";
  6237. __block NSString *lift_gate = @"";
  6238. __block int lift_gate_integer = 0;
  6239. __block NSString *logistic_note = @"";
  6240. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6241. logist = [self textAtColumn:0 statement:stmt];
  6242. lift_gate_integer = sqlite3_column_int(stmt, 1);
  6243. logistic_note = [self textAtColumn:2 statement:stmt];
  6244. }];
  6245. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6246. [dic setValue:@"Shipping Method" forKey:@"title"];
  6247. // val_0
  6248. int PERSONAL_PICK_UP_check = 0;
  6249. int USE_MY_CARRIER_check = 0;
  6250. NSString *logistic_note_text = @"";
  6251. int will_call_check = 0;
  6252. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  6253. will_call_check = 1;
  6254. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  6255. PERSONAL_PICK_UP_check = 1;
  6256. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  6257. USE_MY_CARRIER_check = 1;
  6258. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  6259. logistic_note = [logistic_note_array firstObject];
  6260. if (logistic_note_array.count > 1) {
  6261. logistic_note_text = [logistic_note_array lastObject];
  6262. }
  6263. }
  6264. }
  6265. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  6266. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  6267. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  6268. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  6269. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  6270. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6271. NSDictionary *val0_subItem_item0 = @{
  6272. @"aname" : @"Option",
  6273. @"cadedate" : @{
  6274. @"count" : [NSNumber numberWithInteger:2],
  6275. @"val_0" : @{
  6276. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  6277. @"refresh" : [NSNumber numberWithInteger:0],
  6278. @"value" : @"PERSONAL PICK UP",
  6279. @"value_id" : @"PERSONAL PICK UP"
  6280. },
  6281. @"val_1" : @{
  6282. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  6283. @"refresh" : [NSNumber numberWithInteger:0],
  6284. @"sub_item" : @{
  6285. @"count" : [NSNumber numberWithInteger:1],
  6286. @"item_0" : @{
  6287. @"aname" : @"BOL",
  6288. @"control" : @"edit",
  6289. @"keyboard" : @"text",
  6290. @"name" : @"logist_note_text",
  6291. @"refresh" : [NSNumber numberWithInteger:0],
  6292. @"required" : @"false",
  6293. @"value" : logistic_note_text
  6294. }
  6295. },
  6296. @"value" : @"USE MY CARRIER",
  6297. @"value_id" : @"USE MY CARRIER"
  6298. }
  6299. },
  6300. @"control" : @"enum",
  6301. @"name" : @"logistic_note",
  6302. @"required" : @"true",
  6303. @"single_select" : @"true"
  6304. };
  6305. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  6306. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  6307. // val_1
  6308. lift_gate = [self valueInParams:params key:@"lift_gate"];
  6309. if([lift_gate isEqualToString:@""]) {
  6310. if (lift_gate_integer == 1) {
  6311. lift_gate = @"true";
  6312. } else {
  6313. lift_gate = @"false";
  6314. }
  6315. }
  6316. int common_carrier_check = 0;
  6317. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  6318. common_carrier_check = 1;
  6319. [params setValue:lift_gate forKey:@"lift_gate"];
  6320. }
  6321. NSDictionary *val_1 = @{
  6322. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  6323. @"sub_item" : @{
  6324. @"count" : [NSNumber numberWithInteger:1],
  6325. @"item_0" : @{
  6326. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  6327. @"control" : @"switch",
  6328. @"name" : @"lift_gate",
  6329. @"refresh" : [NSNumber numberWithInteger:1],
  6330. @"required" : @"true",
  6331. @"value" : lift_gate
  6332. }
  6333. },
  6334. @"value" : @"COMMON CARRIER",
  6335. @"value_id" : @"COMMON CARRIER"
  6336. };
  6337. // cadedate
  6338. NSDictionary *cadedate = @{
  6339. @"count" : [NSNumber numberWithInteger:2],
  6340. @"val_0" : val_0,
  6341. @"val_1" : val_1
  6342. };
  6343. // item_0
  6344. NSMutableDictionary *item_0 = @{
  6345. @"aname" : @"Shipping",
  6346. @"cadedate" : cadedate,
  6347. @"control" : @"enum",
  6348. @"name" : @"logist",
  6349. @"refresh" : [NSNumber numberWithInteger:1],
  6350. @"single_select" : @"true",
  6351. }.mutableCopy;
  6352. NSNumber *required = setting[@"ShippingMethodRequire"];
  6353. if ([required integerValue]) {
  6354. [item_0 setValue:@"true" forKey:@"required"];
  6355. }
  6356. [dic setValue:item_0 forKey:@"item_0"];
  6357. return dic;
  6358. }
  6359. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  6360. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6361. orderCode = [self translateSingleQuote:orderCode];
  6362. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  6363. __block int submit_as_integer = 0;
  6364. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6365. submit_as_integer = sqlite3_column_int(stmt, 0);
  6366. }];
  6367. int check11 = 0;
  6368. int check10 = 0;
  6369. if (submit_as_integer == 11) {
  6370. check11 = 1;
  6371. }
  6372. if (submit_as_integer == 10) {
  6373. check10 = 1;
  6374. }
  6375. // section_0
  6376. NSMutableDictionary *dic = @{
  6377. @"count" : @(1),
  6378. @"item_0" : @{
  6379. @"aname" : @"Submit Order As",
  6380. @"cadedate" : @{
  6381. @"count" : @(2),
  6382. @"val_0" : @{
  6383. @"check" : [NSNumber numberWithInteger:check11],
  6384. @"value" : @"Sales Order",
  6385. @"value_id" : @(11)
  6386. },
  6387. @"val_1" : @{
  6388. @"check" : [NSNumber numberWithInteger:check10],
  6389. @"value" : @"Quote",
  6390. @"value_id" : @(10)
  6391. }
  6392. },
  6393. @"control" : @"enum",
  6394. @"name" : @"erpOrderStatus",
  6395. @"required" : @"true",
  6396. @"single_select" : @"true"
  6397. },
  6398. @"title" : @"Order Type"
  6399. }.mutableCopy;
  6400. // setting
  6401. NSDictionary *setting = params[@"setting"];
  6402. NSNumber *hide = setting[@"OrderTypeHide"];
  6403. [dic setValue:hide forKey:@"hide"];
  6404. return dic;
  6405. }
  6406. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  6407. countryCode = [self translateSingleQuote:countryCode];
  6408. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  6409. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6410. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  6411. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  6412. if (name == NULL) {
  6413. name = "";
  6414. }
  6415. if (code == NULL) {
  6416. code = "";
  6417. }
  6418. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6419. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6420. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6421. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6422. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6423. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6424. }
  6425. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6426. [container setValue:stateDic forKey:key];
  6427. }] mutableCopy];
  6428. // failure 可以不用了,一样的
  6429. if (ret.allKeys.count == 0) {
  6430. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6431. [stateDic setValue:@"Other" forKey:@"value"];
  6432. [stateDic setValue:@"" forKey:@"value_id"];
  6433. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6434. if (state_code && [@"" isEqualToString:state_code]) {
  6435. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6436. }
  6437. NSString *key = [NSString stringWithFormat:@"val_0"];
  6438. [ret setValue:stateDic forKey:key];
  6439. }
  6440. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6441. return ret;
  6442. }
  6443. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6444. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6445. orderCode = [self translateSingleQuote:orderCode];
  6446. NSString *sql = [NSString stringWithFormat:@"select paymentType,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,decrypt(credit_card_number),decrypt(credit_card_security_code),decrypt(credit_card_expiration_month),decrypt(credit_card_expiration_year),credit_card_city,credit_card_state from offline_order where so_id = '%@';",orderCode];
  6447. __block NSString *payType = @"";
  6448. __block NSString *firstName = @"";
  6449. __block NSString *lastName = @"";
  6450. __block NSString *addr1 = @"";
  6451. __block NSString *addr2 = @"";
  6452. __block NSString *zipcode = @"";
  6453. __block NSString *cardType = @"";
  6454. __block NSString *cardNumber = @"";
  6455. __block NSString *securityCode = @"";
  6456. __block NSString *month = @"";
  6457. __block NSString *year = @"";
  6458. __block NSString *city = @"";
  6459. __block NSString *state = @"";
  6460. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6461. payType = [self textAtColumn:0 statement:stmt];
  6462. firstName = [self textAtColumn:1 statement:stmt];
  6463. lastName = [self textAtColumn:2 statement:stmt];
  6464. addr1 = [self textAtColumn:3 statement:stmt];
  6465. addr2 = [self textAtColumn:4 statement:stmt];
  6466. zipcode = [self textAtColumn:5 statement:stmt];
  6467. cardType = [self textAtColumn:6 statement:stmt];
  6468. cardNumber = [self textAtColumn:7 statement:stmt];
  6469. securityCode = [self textAtColumn:8 statement:stmt];
  6470. month = [self textAtColumn:9 statement:stmt];
  6471. year = [self textAtColumn:10 statement:stmt];
  6472. city = [self textAtColumn:11 statement:stmt];
  6473. state = [self textAtColumn:12 statement:stmt];
  6474. }];
  6475. NSString *required = @"true";
  6476. // setting
  6477. NSDictionary *setting = params[@"setting"];
  6478. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6479. if ([requiredNumber integerValue]) {
  6480. required = @"true";
  6481. } else {
  6482. required = @"false";
  6483. }
  6484. // "section_2"
  6485. NSMutableDictionary *dic = @{
  6486. @"count" : @(1),
  6487. @"item_0" : @{
  6488. @"aname" : @"Payment",
  6489. @"required" : required,
  6490. @"cadedate" : @{
  6491. @"count" : @(6),
  6492. @"val_0" : @{
  6493. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6494. @"value" : @"Check",
  6495. @"value_id" : @"Check"
  6496. },
  6497. @"val_1" : @{
  6498. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6499. @"value" : @"Cash",
  6500. @"value_id" : @"Cash"
  6501. },
  6502. @"val_2" : @{
  6503. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6504. @"value" : @"NET 30",
  6505. @"value_id" : @"NET 30"
  6506. },
  6507. @"val_3" : @{
  6508. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6509. @"value" : @"Wire Transfer",
  6510. @"value_id" : @"Wire Transfer"
  6511. },
  6512. @"val_4" : @{
  6513. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6514. @"sub_item" : @{
  6515. @"count" : @(3),
  6516. @"item_0" : @{
  6517. @"aname" : @"choose",
  6518. @"control" : @"multi_action",
  6519. @"count" : @(1),
  6520. @"item_0" : @{
  6521. @"aname" : @"Same as customer",
  6522. @"key_map" : @{
  6523. @"credit_card_address1" : @"customer_address1",
  6524. @"credit_card_address2" : @"customer_address2",
  6525. @"credit_card_city" : @"customer_city",
  6526. @"credit_card_first_name" : @"customer_first_name",
  6527. @"credit_card_last_name" : @"customer_last_name",
  6528. @"credit_card_state" : @"customer_state",
  6529. @"credit_card_zipcode" : @"customer_zipcode"
  6530. },
  6531. @"type" : @"pull"
  6532. }
  6533. },
  6534. @"item_1" : @{
  6535. @"aname" : @"",
  6536. @"color" : @"red",
  6537. @"control" : @"text",
  6538. @"name" : @"",
  6539. @"value" : @"USA Credit cards only"
  6540. },
  6541. @"item_2" : @{
  6542. @"aname" : @"Fill",
  6543. @"cadedate" : @{
  6544. @"count" : @(2),
  6545. @"val_0" : @{
  6546. @"check" : @(1),
  6547. @"sub_item" : @{
  6548. @"count" : @(11),
  6549. @"item_0" : @{
  6550. @"aname" : @"Type",
  6551. @"cadedate" : @{
  6552. @"count" : @(2),
  6553. @"val_0" : @{
  6554. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  6555. @"value" : @"VISA",
  6556. @"value_id" : @(0)
  6557. },
  6558. @"val_1" : @{
  6559. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  6560. @"value" : @"MASTER CARD",
  6561. @"value_id" : @(1)
  6562. }
  6563. },
  6564. @"control" : @"enum",
  6565. @"name" : @"credit_card_type",
  6566. @"required" : @"true",
  6567. @"single_select" : @"true"
  6568. },
  6569. @"item_1" : @{
  6570. @"aname" : @"Number",
  6571. @"control" : @"edit",
  6572. @"keyboard" : @"int",
  6573. @"length" : @"16",
  6574. @"name" : @"credit_card_number",
  6575. @"required" : @"true",
  6576. @"value" : cardNumber
  6577. },
  6578. @"item_10" : @{
  6579. @"aname" : @"State",
  6580. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  6581. @"control" : @"enum",
  6582. @"enum" : @"true",
  6583. @"name" : @"credit_card_state",
  6584. @"required" : @"true",
  6585. @"single_select" : @"true"
  6586. },
  6587. @"item_2" : @{
  6588. @"aname" : @"Expiration Date",
  6589. @"control" : @"monthpicker",
  6590. @"name" : @"credit_card_expiration",
  6591. @"required" : @"true",
  6592. @"type" : @"date",
  6593. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  6594. },
  6595. @"item_3" : @{
  6596. @"aname" : @"Security Code",
  6597. @"control" : @"edit",
  6598. @"keyboard" : @"int",
  6599. @"length" : @"3",
  6600. @"name" : @"credit_card_security_code",
  6601. @"required" : @"true",
  6602. @"value" : securityCode
  6603. },
  6604. @"item_4" : @{
  6605. @"aname" : @"First Name",
  6606. @"control" : @"edit",
  6607. @"keyboard" : @"text",
  6608. @"name" : @"credit_card_first_name",
  6609. @"required" : @"true",
  6610. @"value" : firstName
  6611. },
  6612. @"item_5" : @{
  6613. @"aname" : @"Last Name",
  6614. @"control" : @"edit",
  6615. @"keyboard" : @"text",
  6616. @"name" : @"credit_card_last_name",
  6617. @"required" : @"true",
  6618. @"value" : lastName
  6619. },
  6620. @"item_6" : @{
  6621. @"aname" : @"Address 1",
  6622. @"control" : @"edit",
  6623. @"keyboard" : @"text",
  6624. @"name" : @"credit_card_address1",
  6625. @"required" : @"true",
  6626. @"value" : addr1
  6627. },
  6628. @"item_7" : @{
  6629. @"aname" : @"Address 2",
  6630. @"control" : @"edit",
  6631. @"keyboard" : @"text",
  6632. @"name" : @"credit_card_address2",
  6633. @"value" : addr2
  6634. },
  6635. @"item_8" : @{
  6636. @"aname" : @"zip code",
  6637. @"control" : @"edit",
  6638. @"keyboard" : @"text",
  6639. @"name" : @"credit_card_zipcode",
  6640. @"required" : @"true",
  6641. @"value" : zipcode
  6642. },
  6643. @"item_9" : @{
  6644. @"aname" : @"City",
  6645. @"control" : @"edit",
  6646. @"keyboard" : @"text",
  6647. @"name" : @"credit_card_city",
  6648. @"required" : @"true",
  6649. @"value" : city
  6650. }
  6651. },
  6652. @"value" : @"Fill Now",
  6653. @"value_id" : @""
  6654. },
  6655. @"val_1" : @{
  6656. @"check" : @(0),
  6657. @"value" : @"Fill Later",
  6658. @"value_id" : @""
  6659. }
  6660. },
  6661. @"control" : @"enum",
  6662. @"name" : @"",
  6663. @"single_select" : @"true"
  6664. }
  6665. },
  6666. @"value" : @"Credit Card",
  6667. @"value_id" : @"Credit Card"
  6668. },
  6669. @"val_5" : @{
  6670. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  6671. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  6672. @"value_id" : @"FOLLOW EXISTING"
  6673. }
  6674. },
  6675. @"control" : @"enum",
  6676. @"name" : @"paymentType",
  6677. @"single_select" : @"true"
  6678. },
  6679. @"title" : @"Payment Information"
  6680. }.mutableCopy;
  6681. NSNumber *hide = setting[@"PaymentInformationHide"];
  6682. [dic setValue:hide forKey:@"hide"];
  6683. return dic;
  6684. }
  6685. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  6686. {
  6687. sqlite3 *db = [iSalesDB get_db];
  6688. // UISetting
  6689. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  6690. NSString *cachefolder = [paths objectAtIndex:0];
  6691. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  6692. NSData* json =nil;
  6693. json=[NSData dataWithContentsOfFile:img_cache];
  6694. NSError *error=nil;
  6695. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  6696. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  6697. [params setObject:setting forKey:@"setting"];
  6698. int section_count = 0;
  6699. // params
  6700. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6701. orderCode = [self translateSingleQuote:orderCode];
  6702. // 0 Order Type 1 Shipping Method 2 Payment Information
  6703. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  6704. // 0 Order Type
  6705. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  6706. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  6707. [ret setValue:order_type_dic forKey:key0];
  6708. // 1 Shipping Method
  6709. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  6710. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  6711. [ret setValue:shipping_method_dic forKey:key1];
  6712. // 2 Payment Information
  6713. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  6714. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  6715. [ret setValue:payment_info_dic forKey:key2];
  6716. // 3 Customer
  6717. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  6718. NSDictionary *customer_dic = [self customerDic:params db:db];
  6719. [ret setValue:customer_dic forKey:key3];
  6720. // 4 Ship To
  6721. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  6722. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  6723. [ret setValue:ship_to_dic forKey:key4];
  6724. // 5 Ship From
  6725. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  6726. if (![shipFromDisable integerValue]) {
  6727. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  6728. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  6729. [ret setValue:ship_from_dic forKey:key5];
  6730. }
  6731. // 6 Freight Bill To
  6732. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  6733. if (![freightBillToDisable integerValue]) {
  6734. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  6735. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  6736. [ret setValue:freight_bill_to forKey:key6];
  6737. }
  6738. // 7 Merchandise Bill To
  6739. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  6740. if (![merchandiseBillToDisable integerValue]) {
  6741. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  6742. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  6743. [ret setValue:merchandise_bill_to_dic forKey:key7];
  6744. }
  6745. // 8 Return To
  6746. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  6747. if (![returnToDisable integerValue]) {
  6748. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  6749. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  6750. [ret setValue:return_to_dic forKey:key8];
  6751. }
  6752. // 9 Model Information
  6753. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  6754. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  6755. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  6756. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  6757. [ret setValue:model_info_dic forKey:key9];
  6758. // 10 Remarks Content
  6759. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  6760. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  6761. [ret setValue:remarks_content_dic forKey:key10];
  6762. // 11 Order Total
  6763. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  6764. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  6765. [ret setValue:order_total_dic forKey:key11];
  6766. // 12 Signature
  6767. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  6768. NSDictionary *sign_dic = [self signatureDic:params db:db];
  6769. [ret setValue:sign_dic forKey:key12];
  6770. [ret setValue:@(section_count) forKey:@"section_count"];
  6771. [iSalesDB close_db:db];
  6772. return [RAUtils dict2data:ret];
  6773. // return nil;
  6774. }
  6775. #pragma mark addr editor
  6776. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  6777. NSMutableDictionary *new_item = [item mutableCopy];
  6778. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  6779. return new_item;
  6780. }
  6781. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  6782. {
  6783. // "is_subaction" = true;
  6784. // orderCode = MOB1608240002;
  6785. // password = 123456;
  6786. // "subaction_tag" = 1;
  6787. // user = EvanK;
  6788. // {
  6789. // "is_subaction" = true;
  6790. // orderCode = MOB1608240002;
  6791. // password = 123456;
  6792. // "refresh_trigger" = zipcode;
  6793. // "subaction_tag" = 1;
  6794. // user = EvanK;
  6795. // }
  6796. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  6797. [ret removeObjectForKey:@"up_params"];
  6798. [ret setObject:@"New Address" forKey:@"title"];
  6799. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  6800. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  6801. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  6802. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  6803. [country_dic removeObjectForKey:@"refresh"];
  6804. [country_dic removeObjectForKey:@"restore"];
  6805. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  6806. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  6807. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  6808. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  6809. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  6810. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  6811. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  6812. [zip_code_dic removeObjectForKey:@"refresh"];
  6813. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  6814. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  6815. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  6816. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  6817. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  6818. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  6819. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  6820. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  6821. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  6822. // country
  6823. NSString *countryCode = @"US";
  6824. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6825. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  6826. // state
  6827. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  6828. NSDictionary *tmpDic = @{
  6829. @"value" : @"Other",
  6830. @"value_id" : @"",
  6831. @"check" : [NSNumber numberWithInteger:0]
  6832. };
  6833. for (int i = 0; i < allState.allKeys.count; i++) {
  6834. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  6835. NSDictionary *tmp = allState[key];
  6836. [allState setValue:tmpDic forKey:key];
  6837. tmpDic = tmp;
  6838. }
  6839. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  6840. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  6841. [ret setValue:new_section_0 forKey:@"section_0"];
  6842. return [RAUtils dict2data:ret];
  6843. }
  6844. #pragma mark save addr
  6845. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  6846. {
  6847. // NSString *companyName = [self valueInParams:params key:@"company"];
  6848. // NSString *addr1 = [self valueInParams:params key:@"address"];
  6849. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  6850. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  6851. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  6852. // NSString *countryId = [self valueInParams:params key:@"country"];
  6853. // NSString *stateCode = [self valueInParams:params key:@"state"];
  6854. // NSString *city = [self valueInParams:params key:@"city"];
  6855. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  6856. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  6857. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  6858. // NSString *phone = [self valueInParams:params key:@"phone"];
  6859. // NSString *fax = [self valueInParams:params key:@"fax"];
  6860. // NSString *email = [self valueInParams:params key:@"email"];
  6861. return [self offline_saveContact:params update:NO isCustomer:NO];
  6862. }
  6863. #pragma mark cancel order
  6864. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  6865. {
  6866. // NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6867. NSString *order_id = [self valueInParams:params key:@"orderId"];
  6868. // NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  6869. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  6870. int ret = [iSalesDB execSql:sql];
  6871. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6872. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6873. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6874. // [dic setValue:@"160409" forKey:@"min_ver"];
  6875. return [RAUtils dict2data:dic];
  6876. }
  6877. #pragma mark sign order
  6878. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  6879. {
  6880. //参考 offline_saveBusinesscard
  6881. NSLog(@"sign order params: %@",params);
  6882. // orderCode = MOB1608240002;
  6883. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  6884. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6885. orderCode = [self translateSingleQuote:orderCode];
  6886. NSString *picPath = [self valueInParams:params key:@"picpath"];
  6887. picPath = [self translateSingleQuote:picPath];
  6888. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  6889. int ret = [iSalesDB execSql:sql];
  6890. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6891. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6892. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6893. // [dic setValue:@"160409" forKey:@"min_ver"];
  6894. return [RAUtils dict2data:dic];
  6895. }
  6896. #pragma mark save order
  6897. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  6898. NSString *ret = [self valueInParams:params key:key];
  6899. if (translate) {
  6900. ret = [self translateSingleQuote:ret];
  6901. }
  6902. return ret;
  6903. }
  6904. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  6905. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  6906. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  6907. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  6908. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  6909. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  6910. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  6911. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  6912. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  6913. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  6914. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  6915. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  6916. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  6917. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  6918. if (![total_price isEqualToString:@""]) {
  6919. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  6920. } else {
  6921. total_price = @"";
  6922. }
  6923. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  6924. if ([paymentsAndCredits isEqualToString:@""]) {
  6925. paymentsAndCredits = @"";
  6926. } else {
  6927. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  6928. }
  6929. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  6930. if ([handling_fee_value isEqualToString:@""]) {
  6931. handling_fee_value = @"";
  6932. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  6933. handling_fee_value = @"";
  6934. } else {
  6935. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  6936. }
  6937. NSString *handling_fee_placeholder = handling_fee_value;
  6938. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  6939. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  6940. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  6941. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  6942. NSString *lift_gate_placeholder = @"";
  6943. if ([lift_gate_value isEqualToString:@""]) {
  6944. lift_gate_placeholder = @"";
  6945. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  6946. lift_gate_placeholder = @"";
  6947. } else {
  6948. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  6949. }
  6950. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  6951. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  6952. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  6953. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  6954. logistic_note = [logistic_note isEqualToString:@"USE MY CARRIER"] ? [NSString stringWithFormat:@"logistic_note = '%@\n\t%@',",logistic_note,logistic_note_text] : [NSString stringWithFormat:@"logistic_note = '%@',",logistic_note];
  6955. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  6956. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  6957. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  6958. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  6959. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  6960. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  6961. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  6962. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  6963. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  6964. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  6965. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  6966. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  6967. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  6968. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  6969. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  6970. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  6971. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  6972. BOOL number_nil = [credit_card_number isEqualToString:@""];
  6973. if (!number_nil) {
  6974. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  6975. }
  6976. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  6977. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  6978. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  6979. if (!security_code_nil) {
  6980. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  6981. }
  6982. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  6983. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  6984. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  6985. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  6986. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  6987. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  6988. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  6989. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  6990. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  6991. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  6992. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  6993. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  6994. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  6995. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  6996. NSString *contact_id = customer_cid;
  6997. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  6998. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  6999. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  7000. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  7001. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  7002. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  7003. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  7004. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  7005. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  7006. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  7007. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  7008. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  7009. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  7010. //
  7011. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  7012. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  7013. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  7014. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  7015. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  7016. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  7017. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  7018. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  7019. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  7020. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  7021. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  7022. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  7023. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  7024. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  7025. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  7026. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  7027. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  7028. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  7029. NSString *contactSql = [NSString stringWithFormat:@"update offline_contact set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ where contact_id = '%@'",contact_contact,contact_email,contact_phone,contact_fax,customer_name,customer_address1,customer_address2,customer_address3,customer_address4,customer_country,customer_state,customer_city,customer_zipcode,contact_id];
  7030. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  7031. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  7032. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  7033. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  7034. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  7035. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  7036. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  7037. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  7038. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  7039. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  7040. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  7041. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  7042. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  7043. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  7044. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  7045. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  7046. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  7047. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  7048. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  7049. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  7050. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  7051. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  7052. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  7053. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  7054. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  7055. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  7056. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  7057. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  7058. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  7059. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  7060. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  7061. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  7062. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  7063. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  7064. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  7065. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  7066. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  7067. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  7068. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  7069. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  7070. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  7071. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  7072. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  7073. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  7074. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  7075. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  7076. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  7077. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  7078. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  7079. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  7080. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  7081. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  7082. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  7083. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  7084. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  7085. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  7086. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  7087. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  7088. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  7089. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  7090. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  7091. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  7092. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  7093. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  7094. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  7095. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  7096. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  7097. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  7098. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  7099. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  7100. NSString *order_status = @"status = 1,";
  7101. if (submit) {
  7102. order_status = @"status = -11,";
  7103. }
  7104. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  7105. NSString *sync_sql = @"";
  7106. if (submit) {
  7107. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  7108. [param setValue:sales_rep forKey:@"sales_rep"];
  7109. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  7110. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  7111. }
  7112. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_sql,so_id];
  7113. DebugLog(@"save order contactSql: %@",contactSql);
  7114. DebugLog(@"save order orderSql: %@",orderSql);
  7115. // int contact_ret = [iSalesDB execSql:contactSql];
  7116. int order_ret = [iSalesDB execSql:orderSql];
  7117. int ret = order_ret;
  7118. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7119. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  7120. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7121. // [dic setValue:@"160409" forKey:@"min_ver"];
  7122. [dic setObject:so_id forKey:@"so#"];
  7123. return [RAUtils dict2data:dic];
  7124. }
  7125. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  7126. {
  7127. // id foo = nil;
  7128. // NSMutableArray *a = @[].mutableCopy;
  7129. // [a addObject:foo];
  7130. return [self saveorder:param submit:NO];
  7131. }
  7132. #pragma mark add to cart by name
  7133. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  7134. {
  7135. NSString *orderCode = [params objectForKey:@"orderCode"];
  7136. NSString *product_name = [params objectForKey:@"product_name"];
  7137. product_name = [self translateSingleQuote:product_name];
  7138. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  7139. sqlite3 *db = [iSalesDB get_db];
  7140. __block NSMutableString *product_id_string = [NSMutableString string];
  7141. for (int i = 0; i < product_name_array.count; i++) {
  7142. NSString *name = [product_name_array objectAtIndex:i];
  7143. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  7144. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7145. int product_id = sqlite3_column_int(stmt, 0);
  7146. if (i == product_name_array.count - 1) {
  7147. [product_id_string appendFormat:@"%d",product_id];
  7148. } else {
  7149. [product_id_string appendFormat:@"%d,",product_id];
  7150. }
  7151. }];
  7152. }
  7153. NSDictionary *newParams = @{
  7154. @"product_id" : product_id_string,
  7155. @"orderCode" : orderCode
  7156. };
  7157. [iSalesDB close_db:db];
  7158. return [self offline_add2cart:[newParams mutableCopy]];
  7159. }
  7160. #pragma mark reset order
  7161. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  7162. {
  7163. UIApplication * app = [UIApplication sharedApplication];
  7164. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  7165. appDelegate.disable_trigger=true;
  7166. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  7167. appDelegate.disable_trigger=false;
  7168. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7169. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  7170. return [RAUtils dict2data:dic];
  7171. }
  7172. #pragma mark submit order
  7173. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  7174. {
  7175. return [self saveorder:params submit:YES];
  7176. }
  7177. #pragma mark copy order
  7178. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  7179. {
  7180. NSMutableDictionary *ret = @{}.mutableCopy;
  7181. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  7182. sqlite3 *db = [iSalesDB get_db];
  7183. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  7184. // 首先查看联系人是否active
  7185. NSString *customer_sql = [NSString stringWithFormat:@"select is_active from offline_contact where contact_id in (select customer_cid from offline_order where so_id = '%@');",order_code];
  7186. __block int customer_is_active = 1;
  7187. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7188. customer_is_active = sqlite3_column_int(stmt, 0);
  7189. }];
  7190. if (!customer_is_active) {
  7191. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7192. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7193. [iSalesDB close_db:db];
  7194. return [RAUtils dict2data:ret];
  7195. }
  7196. // new order
  7197. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  7198. NSString *new_order_code = [self get_offline_soid:db];
  7199. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  7200. user = [self translateSingleQuote:user];
  7201. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,total_price,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext) select '%@',1,submit_as,internal_notes,sales_rep,'%@',total_price,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  7202. __block int result = 1;
  7203. result = [iSalesDB execSql:insert_order_sql db:db];
  7204. if (!result) {
  7205. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7206. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7207. [iSalesDB close_db:db];
  7208. return [RAUtils dict2data:ret];
  7209. }
  7210. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  7211. // __block NSString *product_id = @"";
  7212. __weak typeof(self) weakSelf = self;
  7213. NSString *product_id_sql = [NSString stringWithFormat:@"select product_id,is_active from product WHERE product_id in (select product_id from offline_cart where so_no = '%@');",order_code];
  7214. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7215. int is_active = sqlite3_column_int(stmt, 1);
  7216. if (is_active) {
  7217. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  7218. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  7219. NSString *insert_cart_sql = [NSString stringWithFormat:@"insert into offline_cart (so_no,orderitem_id,product_id,item_id,item_count,bundle_item,type) select '%@',orderitem_id,product_id,item_id,item_count,bundle_item,type from offline_cart where product_id = %@ and so_no = '%@'",new_order_code,product_id,order_code];
  7220. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  7221. }
  7222. }];
  7223. // product_id = [product_id substringFromIndex:1];
  7224. //
  7225. // [self offline_add2cart:@{}.mutableCopy];
  7226. [iSalesDB execSql:@"END TRANSACTION" db:db];
  7227. [iSalesDB close_db:db];
  7228. if (result) {
  7229. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7230. } else {
  7231. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7232. }
  7233. [ret setObject:new_order_code forKey:@"so_id"];
  7234. return [RAUtils dict2data:ret];
  7235. }
  7236. #pragma mark move wish list to cart
  7237. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  7238. NSString *collectId = params[@"collectId"];
  7239. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  7240. __block NSString *product_id = @"";
  7241. __block NSString *qty = @"";
  7242. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7243. int productId = sqlite3_column_int(stmt, 0);
  7244. int item_qty = sqlite3_column_int(stmt, 1);
  7245. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  7246. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  7247. }];
  7248. product_id = [product_id substringFromIndex:1];
  7249. qty = [qty substringFromIndex:1];
  7250. NSDictionary *newParams = @{
  7251. @"product_id" : product_id,
  7252. @"orderCode" : params[@"orderCode"],
  7253. @"qty" : qty
  7254. };
  7255. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  7256. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  7257. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  7258. // NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  7259. //
  7260. // int ret = [iSalesDB execSql:deleteSQL];
  7261. int ret = [[[self offline_deletewishlist:@{@"collectId" : collectId}.mutableCopy] valueForKey:@"result"] integerValue];
  7262. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  7263. }
  7264. return [RAUtils dict2data:retDic];
  7265. }
  7266. #pragma mark - portfolio
  7267. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  7268. int sort = [[params valueForKey:@"sort"] integerValue];
  7269. int offset = [[params valueForKey:@"offset"] integerValue];
  7270. int limit = [[params valueForKey:@"limit"] integerValue];
  7271. NSString *orderBy = @"";
  7272. switch (sort) {
  7273. case 0:{
  7274. orderBy = @"modify_time desc";
  7275. }
  7276. break;
  7277. case 1:{
  7278. orderBy = @"modify_time asc";
  7279. }
  7280. break;
  7281. case 2:{
  7282. orderBy = @"name asc";
  7283. }
  7284. break;
  7285. case 3:{
  7286. orderBy = @"name desc";
  7287. }
  7288. break;
  7289. case 4:{
  7290. orderBy = @"description asc";
  7291. }
  7292. break;
  7293. default:
  7294. break;
  7295. }
  7296. NSString *sql = [NSString stringWithFormat:@"select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  7297. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7298. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7299. sqlite3 *db = [iSalesDB get_db];
  7300. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7301. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7302. int product_id = sqlite3_column_int(stmt, 0);
  7303. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7304. NSString *name = [self textAtColumn:1 statement:stmt];
  7305. NSString *description = [self textAtColumn:2 statement:stmt];
  7306. double price = sqlite3_column_double(stmt, 3);
  7307. double discount = sqlite3_column_double(stmt,4);
  7308. int qty = sqlite3_column_int(stmt, 5);
  7309. int percentage = sqlite3_column_int(stmt, 6);
  7310. int item_id = sqlite3_column_int(stmt, 7);
  7311. // int fashion_id = sqlite3_column_int(stmt, 8);
  7312. NSString *img_path = [self textAtColumn:9 statement:stmt];
  7313. NSString *line_note = [self textAtColumn:10 statement:stmt];
  7314. double percent = sqlite3_column_double(stmt, 11);
  7315. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7316. if ([price_null isEqualToString:@"null"]) {
  7317. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7318. }
  7319. NSMutableDictionary *item = @{
  7320. @"linenotes": line_note,
  7321. @"check": @(1),
  7322. @"product_id": product_id_string,
  7323. @"available_qty": @(qty),
  7324. @"available_percent" : @(percent),
  7325. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  7326. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7327. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  7328. @"img": img_path,
  7329. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  7330. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  7331. }.mutableCopy;
  7332. if (percentage) {
  7333. [item removeObjectForKey:@"available_qty"];
  7334. } else {
  7335. [item removeObjectForKey:@"available_percent"];
  7336. }
  7337. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  7338. if ([qty_null isEqualToString:@"null"]) {
  7339. [item removeObjectForKey:@"available_qty"];
  7340. }
  7341. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7342. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7343. }];
  7344. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7345. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7346. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7347. }
  7348. [iSalesDB close_db:db];
  7349. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  7350. [dic setValue:@"" forKey:@"email_content"];
  7351. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  7352. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7353. return [RAUtils dict2data:dic];
  7354. }
  7355. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  7356. __block int qty = 0;
  7357. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7358. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7359. qty = sqlite3_column_int(stmt, 0);
  7360. }];
  7361. return qty;
  7362. }
  7363. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  7364. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  7365. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  7366. NSMutableDictionary * values = params[@"replaceValue"];
  7367. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  7368. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  7369. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  7370. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  7371. NSString *pdf_path = @"";
  7372. if (direct) {
  7373. configureParams = [self valueInParams:params key:@"pdfUrl"];
  7374. } else {
  7375. pdf_path = [self valueInParams:params key:@"pdfPath"];
  7376. }
  7377. NSString *create_user = [self valueInParams:params key:@"user"];
  7378. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  7379. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  7380. // model info
  7381. NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  7382. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7383. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  7384. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7385. sqlite3 *db = [iSalesDB get_db];
  7386. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7387. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7388. int product_id = sqlite3_column_int(stmt, 0);
  7389. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  7390. double price = sqlite3_column_double(stmt, 1);
  7391. double discount = sqlite3_column_double(stmt,2);
  7392. int qty = sqlite3_column_int(stmt, 3);
  7393. int percentage = sqlite3_column_int(stmt, 4);
  7394. int item_id = sqlite3_column_int(stmt, 5);
  7395. NSString *line_note = [self textAtColumn:6 statement:stmt];
  7396. double percent = sqlite3_column_double(stmt, 7);
  7397. NSString *price_null = [self textAtColumn:3 statement:stmt];
  7398. /* if ([price_null isEqualToString:@"null"]) {
  7399. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7400. }
  7401. */
  7402. [product_ids_string appendFormat:@"%@,",product_id_string];
  7403. // Regular Price
  7404. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  7405. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  7406. // QTY
  7407. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  7408. if ([qty_null isEqualToString:@"null"]) {
  7409. // 查available
  7410. qty = [self model_QTY:product_id_string db:db];
  7411. }
  7412. if (percentage) {
  7413. qty = qty * percent;
  7414. }
  7415. // Special Price
  7416. if ([price_null isEqualToString:@"null"]) {
  7417. // price = regular price
  7418. price = [regular_price_str doubleValue];
  7419. }
  7420. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  7421. if (![discount_null isEqualToString:@"null"]) {
  7422. price = price * (1 - discount);
  7423. }
  7424. NSMutableDictionary *item = @{
  7425. @"linenotes": line_note,
  7426. @"product_id": product_id_string,
  7427. @"available_qty": @(qty),
  7428. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  7429. @"regular_price" : regular_price_str,
  7430. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  7431. }.mutableCopy;
  7432. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7433. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7434. }];
  7435. [iSalesDB close_db:db];
  7436. if (product_ids_string.length > 0) {
  7437. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  7438. }
  7439. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  7440. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  7441. return [RAUtils dict2data:resultDictionary];
  7442. }
  7443. NSString *model_info = [RAUtils dict2string:dic];
  7444. // 创建PDF
  7445. // 在preview情况下保存,则不需要新建了
  7446. if (direct) {
  7447. NSMutableDictionary *tear_sheet_params = params;
  7448. // if (tear_sheet_id) {
  7449. // tear_sheet_params = values;
  7450. // }
  7451. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  7452. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  7453. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  7454. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  7455. resultDictionary = pdfInfo.mutableCopy;
  7456. } else { // 创建PDF失败
  7457. return pdfData;
  7458. }
  7459. } else {
  7460. // pdf_path 就是本地路径
  7461. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  7462. }
  7463. // 将文件移动到PDF Cache文件夹
  7464. NSString *newPath = [pdf_path lastPathComponent];
  7465. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7466. NSString *cachefolder = [paths objectAtIndex:0];
  7467. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7468. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  7469. NSFileManager *fileManager = [NSFileManager defaultManager];
  7470. NSError *error = nil;
  7471. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  7472. if (error) { // 移动文件失败
  7473. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7474. return [RAUtils dict2data:resultDictionary];
  7475. }
  7476. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  7477. pdf_path = [newPath lastPathComponent];
  7478. // 保存信息
  7479. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  7480. NSString *off_params = [RAUtils dict2string:params];
  7481. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  7482. pdf_path = [self translateSingleQuote:pdf_path];
  7483. create_user = [self translateSingleQuote:create_user];
  7484. tear_note = [self translateSingleQuote:tear_note];
  7485. tear_name = [self translateSingleQuote:tear_name];
  7486. model_info = [self translateSingleQuote:model_info];
  7487. configureParams = [self translateSingleQuote:configureParams];
  7488. off_params = [self translateSingleQuote:off_params];
  7489. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ((select ifnull(max(tearsheets_id),0) from offline_pdf) + 1,'%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  7490. if (tear_sheet_id) {
  7491. int _id = [tear_sheet_id integerValue];
  7492. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  7493. }
  7494. int result = [iSalesDB execSql:save_pdf_sql];
  7495. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7496. //
  7497. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  7498. if (remove_Item) {
  7499. // portfolioId
  7500. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  7501. }
  7502. return [RAUtils dict2data:resultDictionary];
  7503. }
  7504. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  7505. return [self offline_savePDF:params direct:YES];
  7506. }
  7507. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  7508. return [self offline_savePDF:params direct:NO];
  7509. }
  7510. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  7511. int offset = [[params valueForKey:@"offset"] integerValue];
  7512. int limit = [[params valueForKey:@"limit"] integerValue];
  7513. NSString *keyword = [params valueForKey:@"keyWord"];
  7514. NSString *where = @"where is_delete is null or is_delete = 0";
  7515. if (keyword.length) {
  7516. keyword = [self translateSingleQuote:keyword];
  7517. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  7518. }
  7519. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  7520. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7521. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7522. NSString *cachefolder = [paths objectAtIndex:0];
  7523. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7524. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7525. NSString *name = [self textAtColumn:0 statement:stmt];
  7526. NSString *note = [self textAtColumn:1 statement:stmt];
  7527. NSString *time = [self textAtColumn:2 statement:stmt];
  7528. NSString *user = [self textAtColumn:3 statement:stmt];
  7529. NSString *path = [self textAtColumn:4 statement:stmt];
  7530. NSString *model_info = [self textAtColumn:6 statement:stmt];
  7531. NSString *off_params = [self textAtColumn:7 statement:stmt];
  7532. path = [pdfFolder stringByAppendingPathComponent:path];
  7533. BOOL bdir=NO;
  7534. NSFileManager* fileManager = [NSFileManager defaultManager];
  7535. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  7536. {
  7537. //pdf文件不存在
  7538. path=nil;
  7539. }
  7540. time = [self changeDateTimeFormate:time];
  7541. time = [time stringByAppendingString:@" PST"];
  7542. int sheet_id = sqlite3_column_int(stmt, 5);
  7543. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  7544. item[@"tearsheetsId"]=@(sheet_id);
  7545. item[@"pdf_path"]=path;
  7546. item[@"create_time"]=time;
  7547. item[@"create_user"]=user;
  7548. item[@"tear_note"]=note;
  7549. item[@"tear_name"]=name;
  7550. item[@"isLocalFile"]=@"true";
  7551. item[@"model_info"]=model_info;
  7552. item[@"off_params"]=off_params;
  7553. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  7554. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  7555. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7556. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7557. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  7558. }];
  7559. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7560. return [RAUtils dict2data:dic];
  7561. }
  7562. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  7563. NSString *prodct_ids = [params objectForKey:@"product_id"];
  7564. // NSString *user = [params objectForKey:@"user"];
  7565. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  7566. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  7567. int result = [iSalesDB execSql:sql];
  7568. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7569. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  7570. if (result == RESULT_TRUE) {
  7571. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  7572. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  7573. sqlite3 *db = [iSalesDB get_db];
  7574. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  7575. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  7576. [iSalesDB close_db:db];
  7577. }
  7578. [dic setValue:@"Regular Mode" forKey:@"mode"];
  7579. return [RAUtils dict2data:dic];
  7580. }
  7581. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  7582. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  7583. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  7584. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  7585. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  7586. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  7587. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  7588. // {
  7589. // "count": 6,
  7590. // "val_1": {
  7591. // "value": "West Wholesale Price",
  7592. // "value_id": 17
  7593. // },
  7594. //
  7595. // }
  7596. __block long val_count = 0;
  7597. NSString *sql = @"select name,type,order_by from price order by order_by";
  7598. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7599. NSString *name = [self textAtColumn:0 statement:stmt];
  7600. int type = sqlite3_column_int(stmt, 1);
  7601. int order_by = sqlite3_column_int(stmt, 2);
  7602. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7603. NSDictionary *price_dic = @{
  7604. @"value" : name,
  7605. @"value_id" : [NSNumber numberWithInteger:type],
  7606. @"check" : order_by == 0 ? @(1) : @(0)
  7607. };
  7608. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  7609. val_count = ++(*count);
  7610. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7611. val_count = 0;
  7612. }];
  7613. [cadedate setObject:@{@"value" : @"None",
  7614. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  7615. val_count++;
  7616. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  7617. [price setObject:cadedate forKey:@"cadedate"];
  7618. [section1 setObject:price forKey:@"item_2"];
  7619. [dic setObject:section1 forKey:@"section_1"];
  7620. return [RAUtils dict2data:dic];
  7621. }
  7622. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  7623. NSString *product_id = [params objectForKey:@"fashionId"];
  7624. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  7625. __block int result = RESULT_TRUE;
  7626. __block int qty = 0;
  7627. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7628. qty = sqlite3_column_int(stmt, 0);
  7629. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7630. result = RESULT_FALSE;
  7631. }];
  7632. NSMutableDictionary *dic = @{
  7633. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  7634. @"mode" : @"Regular Mode",
  7635. @"quantity_available" : @(qty),
  7636. @"result" : @(result),
  7637. }.mutableCopy;
  7638. return [RAUtils dict2data:dic];
  7639. }
  7640. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  7641. NSString *item_ids = [params objectForKey:@"item_id"];
  7642. NSString *line_notes = [params objectForKey:@"notes"];
  7643. NSString *price_str = [params objectForKey:@"price"];
  7644. NSString *discount_str = [params objectForKey:@"discount"];
  7645. NSString *percent = [params objectForKey:@"available_percent"];
  7646. NSString *qty = [params objectForKey:@"available_qty"];
  7647. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  7648. int dot = 0;
  7649. if (line_notes) {
  7650. line_notes = [self translateSingleQuote:line_notes];
  7651. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  7652. sql = [sql stringByAppendingString:line_notes];
  7653. dot = 1;
  7654. } else {
  7655. line_notes = @"";
  7656. }
  7657. if (price_str) {
  7658. if (dot) {
  7659. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  7660. } else {
  7661. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  7662. dot = 1;
  7663. }
  7664. sql = [sql stringByAppendingString:price_str];
  7665. } else {
  7666. price_str = @"";
  7667. }
  7668. if (discount_str) {
  7669. if (dot) {
  7670. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  7671. } else {
  7672. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  7673. dot = 1;
  7674. }
  7675. sql = [sql stringByAppendingString:discount_str];
  7676. } else {
  7677. discount_str = @"";
  7678. }
  7679. if (percent) {
  7680. if (dot) {
  7681. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  7682. } else {
  7683. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  7684. dot = 1;
  7685. }
  7686. sql = [sql stringByAppendingString:percent];
  7687. } else {
  7688. percent = @"";
  7689. }
  7690. if (qty) {
  7691. if (dot) {
  7692. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  7693. } else {
  7694. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  7695. dot = 1;
  7696. }
  7697. sql = [sql stringByAppendingString:qty];
  7698. } else {
  7699. qty = @"";
  7700. }
  7701. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  7702. sql = [sql stringByAppendingString:where];
  7703. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7704. int result = [iSalesDB execSql:sql];
  7705. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7706. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7707. return [RAUtils dict2data:dic];
  7708. }
  7709. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  7710. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  7711. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7712. sqlite3 *db = [iSalesDB get_db];
  7713. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  7714. int result = [iSalesDB execSql:sql db:db];
  7715. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7716. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7717. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  7718. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  7719. [iSalesDB close_db:db];
  7720. return [RAUtils dict2data:dic];
  7721. }
  7722. + (void)offline_removePDFWithName:(NSString *)name {
  7723. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7724. NSString *cachefolder = [paths objectAtIndex:0];
  7725. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7726. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  7727. NSFileManager *fileManager = [NSFileManager defaultManager];
  7728. [fileManager removeItemAtPath:path error:nil];
  7729. }
  7730. + (void)offline_clear_PDFCache {
  7731. NSFileManager *fileManager = [NSFileManager defaultManager];
  7732. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7733. NSString *cachefolder = [paths objectAtIndex:0];
  7734. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  7735. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  7736. for (NSString *path in pdf_files) {
  7737. [self offline_removePDFWithName:[path lastPathComponent]];
  7738. }
  7739. }
  7740. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  7741. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7742. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  7743. NSString *user = [params objectForKey:@"user"];
  7744. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  7745. if (![create_user isEqualToString:user]) {
  7746. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  7747. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7748. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  7749. return [RAUtils dict2data:dic];
  7750. }
  7751. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7752. __block int is_local = 0;
  7753. __block NSString *path = @"";
  7754. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7755. is_local = sqlite3_column_int(stmt, 0);
  7756. path = [self textAtColumn:1 statement:stmt];
  7757. }];
  7758. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7759. if (is_local == 1) {
  7760. sql = [NSString stringWithFormat:@"delete from offline_pdf where tearsheets_id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  7761. // 删除文件
  7762. [self offline_removePDFWithName:path];
  7763. }
  7764. int result = [iSalesDB execSql:sql];
  7765. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  7766. [dic setObject:@"Regular Mode" forKey:@"mode"];
  7767. return [RAUtils dict2data:dic];
  7768. }
  7769. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  7770. {
  7771. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7772. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7773. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7774. NSString* where=@"1=1";
  7775. if (ver!=nil) {
  7776. where=@"is_dirty=1";
  7777. }
  7778. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  7779. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7780. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7781. sqlite3 *db = [iSalesDB get_db];
  7782. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7783. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7784. NSInteger _id = sqlite3_column_int(stmt, 0);
  7785. NSInteger product_id = sqlite3_column_int(stmt, 1);
  7786. NSString *name = [self textAtColumn:2 statement:stmt];
  7787. NSString *desc = [self textAtColumn:3 statement:stmt];
  7788. NSInteger item_id = sqlite3_column_int(stmt, 4);
  7789. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  7790. NSInteger qty = sqlite3_column_int(stmt, 6);
  7791. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  7792. double percent = sqlite3_column_double(stmt, 8);
  7793. double price = sqlite3_column_double(stmt, 9);
  7794. double discount = sqlite3_column_double(stmt, 10);
  7795. NSString *img = [self textAtColumn:11 statement:stmt];
  7796. NSString *line_note = [self textAtColumn:12 statement:stmt];
  7797. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  7798. NSString *create_time = [self textAtColumn:14 statement:stmt];
  7799. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  7800. NSString *price_null = [self textAtColumn:9 statement:stmt];
  7801. if ([price_null isEqualToString:@"null"]) {
  7802. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  7803. }
  7804. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  7805. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  7806. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  7807. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7808. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  7809. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  7810. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  7811. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  7812. if ([qty_null isEqualToString:@"null"]) {
  7813. // [item setValue:@"null" forKey:@"available_qty"];
  7814. } else {
  7815. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  7816. }
  7817. if ([is_percent_null isEqualToString:@"null"]) {
  7818. // [item setValue:@"null" forKey:@"percentage"];
  7819. } else {
  7820. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  7821. }
  7822. if ([percent_null isEqualToString:@"null"]) {
  7823. // [item setValue:@"null" forKey:@"percent"];
  7824. } else {
  7825. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  7826. }
  7827. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  7828. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  7829. [item setValue:name forKey:@"name"];
  7830. [item setValue:desc forKey:@"description"];
  7831. [item setValue:img forKey:@"img"];
  7832. [item setValue:line_note forKey:@"line_note"];
  7833. [item setValue:create_time forKey:@"createtime"];
  7834. [item setValue:modify_time forKey:@"modifytime"];
  7835. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7836. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7837. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7838. }];
  7839. [iSalesDB close_db:db];
  7840. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  7841. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7842. }
  7843. return ret;
  7844. }
  7845. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  7846. {
  7847. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  7848. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  7849. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  7850. NSString* where=@"1=1";
  7851. if (ver!=nil) {
  7852. where=@"is_dirty=1 and is_local = 1";
  7853. }
  7854. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,tearsheets_id,pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params from offline_pdf where %@",where];
  7855. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  7856. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7857. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  7858. NSInteger _id = sqlite3_column_int(stmt, 0);
  7859. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  7860. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  7861. NSString *create_user = [self textAtColumn:3 statement:stmt];
  7862. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  7863. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  7864. NSString *model_info = [self textAtColumn:6 statement:stmt];
  7865. NSString *createtime = [self textAtColumn:7 statement:stmt];
  7866. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  7867. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  7868. NSString *off_params = [self textAtColumn:10 statement:stmt];
  7869. NSString *uuid = [NSUUID UUID].UUIDString;
  7870. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  7871. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  7872. [item setObject:pdf_path forKey:@"pdf_path"];
  7873. [item setObject:create_user forKey:@"create_user"];
  7874. [item setObject:tear_note forKey:@"tear_note"];
  7875. [item setObject:tear_name forKey:@"tear_name"];
  7876. [item setObject:model_info forKey:@"model_info"];
  7877. [item setObject:createtime forKey:@"createtime"];
  7878. [item setObject:modifytime forKey:@"modifytime"];
  7879. [item setObject:urlParams forKey:@"urlParams"];
  7880. [item setObject:off_params forKey:@"off_params"];
  7881. [item setObject:uuid forKey:@"pdf_token"];
  7882. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  7883. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  7884. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7885. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  7886. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  7887. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  7888. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  7889. }];
  7890. return ret;
  7891. }
  7892. @end